1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2018 Netronome Systems, Inc. */
3 
4 #include <linux/kernel.h>
5 #include <net/devlink.h>
6 
7 #include "nfpcore/nfp_cpp.h"
8 #include "nfpcore/nfp_nffw.h"
9 #include "nfp_abi.h"
10 #include "nfp_app.h"
11 #include "nfp_main.h"
12 
13 static u32 nfp_shared_buf_pool_unit(struct nfp_pf *pf, unsigned int sb)
14 {
15 	__le32 sb_id = cpu_to_le32(sb);
16 	unsigned int i;
17 
18 	for (i = 0; i < pf->num_shared_bufs; i++)
19 		if (pf->shared_bufs[i].id == sb_id)
20 			return le32_to_cpu(pf->shared_bufs[i].pool_size_unit);
21 
22 	WARN_ON_ONCE(1);
23 	return 0;
24 }
25 
26 int nfp_shared_buf_pool_get(struct nfp_pf *pf, unsigned int sb, u16 pool_index,
27 			    struct devlink_sb_pool_info *pool_info)
28 {
29 	struct nfp_shared_buf_pool_info_get get_data;
30 	struct nfp_shared_buf_pool_id id = {
31 		.shared_buf	= cpu_to_le32(sb),
32 		.pool		= cpu_to_le32(pool_index),
33 	};
34 	unsigned int unit_size;
35 	int n;
36 
37 	unit_size = nfp_shared_buf_pool_unit(pf, sb);
38 	if (!unit_size)
39 		return -EINVAL;
40 
41 	n = nfp_mbox_cmd(pf, NFP_MBOX_POOL_GET, &id, sizeof(id),
42 			 &get_data, sizeof(get_data));
43 	if (n < 0)
44 		return n;
45 	if (n < sizeof(get_data))
46 		return -EIO;
47 
48 	pool_info->pool_type = le32_to_cpu(get_data.pool_type);
49 	pool_info->threshold_type = le32_to_cpu(get_data.threshold_type);
50 	pool_info->size = le32_to_cpu(get_data.size) * unit_size;
51 
52 	return 0;
53 }
54 
55 int nfp_shared_buf_pool_set(struct nfp_pf *pf, unsigned int sb,
56 			    u16 pool_index, u32 size,
57 			    enum devlink_sb_threshold_type threshold_type)
58 {
59 	struct nfp_shared_buf_pool_info_set set_data = {
60 		.id = {
61 			.shared_buf	= cpu_to_le32(sb),
62 			.pool		= cpu_to_le32(pool_index),
63 		},
64 		.threshold_type	= cpu_to_le32(threshold_type),
65 	};
66 	unsigned int unit_size;
67 
68 	unit_size = nfp_shared_buf_pool_unit(pf, sb);
69 	if (!unit_size || size % unit_size)
70 		return -EINVAL;
71 	set_data.size = cpu_to_le32(size / unit_size);
72 
73 	return nfp_mbox_cmd(pf, NFP_MBOX_POOL_SET, &set_data, sizeof(set_data),
74 			    NULL, 0);
75 }
76 
77 int nfp_shared_buf_register(struct nfp_pf *pf)
78 {
79 	struct devlink *devlink = priv_to_devlink(pf);
80 	unsigned int i, num_entries, entry_sz;
81 	struct nfp_cpp_area *sb_desc_area;
82 	u8 __iomem *sb_desc;
83 	int n, err;
84 
85 	if (!pf->mbox)
86 		return 0;
87 
88 	n = nfp_pf_rtsym_read_optional(pf, NFP_SHARED_BUF_COUNT_SYM_NAME, 0);
89 	if (n <= 0)
90 		return n;
91 	num_entries = n;
92 
93 	sb_desc = nfp_pf_map_rtsym(pf, "sb_tbl", NFP_SHARED_BUF_TABLE_SYM_NAME,
94 				   num_entries * sizeof(pf->shared_bufs[0]),
95 				   &sb_desc_area);
96 	if (IS_ERR(sb_desc))
97 		return PTR_ERR(sb_desc);
98 
99 	entry_sz = nfp_cpp_area_size(sb_desc_area) / num_entries;
100 
101 	pf->shared_bufs = kmalloc_array(num_entries, sizeof(pf->shared_bufs[0]),
102 					GFP_KERNEL);
103 	if (!pf->shared_bufs) {
104 		err = -ENOMEM;
105 		goto err_release_area;
106 	}
107 
108 	for (i = 0; i < num_entries; i++) {
109 		struct nfp_shared_buf *sb = &pf->shared_bufs[i];
110 
111 		/* Entries may be larger in future FW */
112 		memcpy_fromio(sb, sb_desc + i * entry_sz, sizeof(*sb));
113 
114 		err = devlink_sb_register(devlink,
115 					  le32_to_cpu(sb->id),
116 					  le32_to_cpu(sb->size),
117 					  le16_to_cpu(sb->ingress_pools_count),
118 					  le16_to_cpu(sb->egress_pools_count),
119 					  le16_to_cpu(sb->ingress_tc_count),
120 					  le16_to_cpu(sb->egress_tc_count));
121 		if (err)
122 			goto err_unreg_prev;
123 	}
124 	pf->num_shared_bufs = num_entries;
125 
126 	nfp_cpp_area_release_free(sb_desc_area);
127 
128 	return 0;
129 
130 err_unreg_prev:
131 	while (i--)
132 		devlink_sb_unregister(devlink,
133 				      le32_to_cpu(pf->shared_bufs[i].id));
134 	kfree(pf->shared_bufs);
135 err_release_area:
136 	nfp_cpp_area_release_free(sb_desc_area);
137 	return err;
138 }
139 
140 void nfp_shared_buf_unregister(struct nfp_pf *pf)
141 {
142 	struct devlink *devlink = priv_to_devlink(pf);
143 	unsigned int i;
144 
145 	for (i = 0; i < pf->num_shared_bufs; i++)
146 		devlink_sb_unregister(devlink,
147 				      le32_to_cpu(pf->shared_bufs[i].id));
148 	kfree(pf->shared_bufs);
149 }
150