1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/bitops.h>
10 #include "resources.h"
12 struct mlxsw_sp2_kvdl_part_info
{
14 /* For each defined partititon we need to know how many
15 * usage bits we need and how many indexes there are
16 * represented by a single bit. This could be got from FW
17 * querying appropriate resources. So have the resource
18 * ids for for this purpose in partition definition.
20 enum mlxsw_res_id usage_bit_count_res_id
;
21 enum mlxsw_res_id index_range_res_id
;
24 #define MLXSW_SP2_KVDL_PART_INFO(_entry_type, _res_type, \
25 _usage_bit_count_res_id, _index_range_res_id) \
26 [MLXSW_SP_KVDL_ENTRY_TYPE_##_entry_type] = { \
27 .res_type = _res_type, \
28 .usage_bit_count_res_id = MLXSW_RES_ID_##_usage_bit_count_res_id, \
29 .index_range_res_id = MLXSW_RES_ID_##_index_range_res_id, \
32 static const struct mlxsw_sp2_kvdl_part_info mlxsw_sp2_kvdl_parts_info
[] = {
33 MLXSW_SP2_KVDL_PART_INFO(ADJ
, 0x21, KVD_SIZE
, MAX_KVD_LINEAR_RANGE
),
34 MLXSW_SP2_KVDL_PART_INFO(ACTSET
, 0x23, MAX_KVD_ACTION_SETS
,
36 MLXSW_SP2_KVDL_PART_INFO(PBS
, 0x24, KVD_SIZE
, KVD_SIZE
),
37 MLXSW_SP2_KVDL_PART_INFO(MCRIGR
, 0x26, KVD_SIZE
, KVD_SIZE
),
38 MLXSW_SP2_KVDL_PART_INFO(TNUMT
, 0x29, KVD_SIZE
, KVD_SIZE
),
41 #define MLXSW_SP2_KVDL_PARTS_INFO_LEN ARRAY_SIZE(mlxsw_sp2_kvdl_parts_info)
43 struct mlxsw_sp2_kvdl_part
{
44 const struct mlxsw_sp2_kvdl_part_info
*info
;
45 unsigned int usage_bit_count
;
46 unsigned int indexes_per_usage_bit
;
47 unsigned int last_allocated_bit
;
48 unsigned long usage
[]; /* Usage bits */
51 struct mlxsw_sp2_kvdl
{
52 struct mlxsw_sp2_kvdl_part
*parts
[MLXSW_SP2_KVDL_PARTS_INFO_LEN
];
55 static int mlxsw_sp2_kvdl_part_find_zero_bits(struct mlxsw_sp2_kvdl_part
*part
,
56 unsigned int bit_count
,
59 unsigned int start_bit
;
64 start_bit
= part
->last_allocated_bit
+ 1;
65 if (start_bit
== part
->usage_bit_count
)
69 bit
= find_next_zero_bit(part
->usage
, part
->usage_bit_count
, bit
);
70 if (!wrap
&& bit
+ bit_count
>= part
->usage_bit_count
) {
75 if (wrap
&& bit
+ bit_count
>= start_bit
)
77 for (i
= 0; i
< bit_count
; i
++) {
78 if (test_bit(bit
+ i
, part
->usage
)) {
87 static int mlxsw_sp2_kvdl_part_alloc(struct mlxsw_sp2_kvdl_part
*part
,
91 unsigned int bit_count
;
96 bit_count
= DIV_ROUND_UP(size
, part
->indexes_per_usage_bit
);
97 err
= mlxsw_sp2_kvdl_part_find_zero_bits(part
, bit_count
, &bit
);
100 for (i
= 0; i
< bit_count
; i
++)
101 __set_bit(bit
+ i
, part
->usage
);
102 *p_kvdl_index
= bit
* part
->indexes_per_usage_bit
;
106 static int mlxsw_sp2_kvdl_rec_del(struct mlxsw_sp
*mlxsw_sp
, u8 res_type
,
107 u16 size
, u32 kvdl_index
)
112 iedr_pl
= kmalloc(MLXSW_REG_IEDR_LEN
, GFP_KERNEL
);
116 mlxsw_reg_iedr_pack(iedr_pl
);
117 mlxsw_reg_iedr_rec_pack(iedr_pl
, 0, res_type
, size
, kvdl_index
);
118 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(iedr
), iedr_pl
);
123 static void mlxsw_sp2_kvdl_part_free(struct mlxsw_sp
*mlxsw_sp
,
124 struct mlxsw_sp2_kvdl_part
*part
,
125 unsigned int size
, u32 kvdl_index
)
127 unsigned int bit_count
;
132 /* We need to ask FW to delete previously used KVD linear index */
133 err
= mlxsw_sp2_kvdl_rec_del(mlxsw_sp
, part
->info
->res_type
,
138 bit_count
= DIV_ROUND_UP(size
, part
->indexes_per_usage_bit
);
139 bit
= kvdl_index
/ part
->indexes_per_usage_bit
;
140 for (i
= 0; i
< bit_count
; i
++)
141 __clear_bit(bit
+ i
, part
->usage
);
144 static int mlxsw_sp2_kvdl_alloc(struct mlxsw_sp
*mlxsw_sp
, void *priv
,
145 enum mlxsw_sp_kvdl_entry_type type
,
146 unsigned int entry_count
,
149 unsigned int size
= entry_count
* mlxsw_sp_kvdl_entry_size(type
);
150 struct mlxsw_sp2_kvdl
*kvdl
= priv
;
151 struct mlxsw_sp2_kvdl_part
*part
= kvdl
->parts
[type
];
153 return mlxsw_sp2_kvdl_part_alloc(part
, size
, p_entry_index
);
156 static void mlxsw_sp2_kvdl_free(struct mlxsw_sp
*mlxsw_sp
, void *priv
,
157 enum mlxsw_sp_kvdl_entry_type type
,
158 unsigned int entry_count
,
161 unsigned int size
= entry_count
* mlxsw_sp_kvdl_entry_size(type
);
162 struct mlxsw_sp2_kvdl
*kvdl
= priv
;
163 struct mlxsw_sp2_kvdl_part
*part
= kvdl
->parts
[type
];
165 return mlxsw_sp2_kvdl_part_free(mlxsw_sp
, part
, size
, entry_index
);
168 static int mlxsw_sp2_kvdl_alloc_size_query(struct mlxsw_sp
*mlxsw_sp
,
170 enum mlxsw_sp_kvdl_entry_type type
,
171 unsigned int entry_count
,
172 unsigned int *p_alloc_count
)
174 *p_alloc_count
= entry_count
;
178 static struct mlxsw_sp2_kvdl_part
*
179 mlxsw_sp2_kvdl_part_init(struct mlxsw_sp
*mlxsw_sp
,
180 const struct mlxsw_sp2_kvdl_part_info
*info
)
182 unsigned int indexes_per_usage_bit
;
183 struct mlxsw_sp2_kvdl_part
*part
;
184 unsigned int index_range
;
185 unsigned int usage_bit_count
;
188 if (!mlxsw_core_res_valid(mlxsw_sp
->core
,
189 info
->usage_bit_count_res_id
) ||
190 !mlxsw_core_res_valid(mlxsw_sp
->core
,
191 info
->index_range_res_id
))
192 return ERR_PTR(-EIO
);
193 usage_bit_count
= mlxsw_core_res_get(mlxsw_sp
->core
,
194 info
->usage_bit_count_res_id
);
195 index_range
= mlxsw_core_res_get(mlxsw_sp
->core
,
196 info
->index_range_res_id
);
198 /* For some partitions, one usage bit represents a group of indexes.
199 * That's why we compute the number of indexes per usage bit here,
200 * according to queried resources.
202 indexes_per_usage_bit
= index_range
/ usage_bit_count
;
204 usage_size
= BITS_TO_LONGS(usage_bit_count
) * sizeof(unsigned long);
205 part
= kzalloc(sizeof(*part
) + usage_size
, GFP_KERNEL
);
207 return ERR_PTR(-ENOMEM
);
209 part
->usage_bit_count
= usage_bit_count
;
210 part
->indexes_per_usage_bit
= indexes_per_usage_bit
;
211 part
->last_allocated_bit
= usage_bit_count
- 1;
215 static void mlxsw_sp2_kvdl_part_fini(struct mlxsw_sp2_kvdl_part
*part
)
220 static int mlxsw_sp2_kvdl_parts_init(struct mlxsw_sp
*mlxsw_sp
,
221 struct mlxsw_sp2_kvdl
*kvdl
)
223 const struct mlxsw_sp2_kvdl_part_info
*info
;
227 for (i
= 0; i
< MLXSW_SP2_KVDL_PARTS_INFO_LEN
; i
++) {
228 info
= &mlxsw_sp2_kvdl_parts_info
[i
];
229 kvdl
->parts
[i
] = mlxsw_sp2_kvdl_part_init(mlxsw_sp
, info
);
230 if (IS_ERR(kvdl
->parts
[i
])) {
231 err
= PTR_ERR(kvdl
->parts
[i
]);
232 goto err_kvdl_part_init
;
238 for (i
--; i
>= 0; i
--)
239 mlxsw_sp2_kvdl_part_fini(kvdl
->parts
[i
]);
243 static void mlxsw_sp2_kvdl_parts_fini(struct mlxsw_sp2_kvdl
*kvdl
)
247 for (i
= 0; i
< MLXSW_SP2_KVDL_PARTS_INFO_LEN
; i
++)
248 mlxsw_sp2_kvdl_part_fini(kvdl
->parts
[i
]);
251 static int mlxsw_sp2_kvdl_init(struct mlxsw_sp
*mlxsw_sp
, void *priv
)
253 struct mlxsw_sp2_kvdl
*kvdl
= priv
;
255 return mlxsw_sp2_kvdl_parts_init(mlxsw_sp
, kvdl
);
258 static void mlxsw_sp2_kvdl_fini(struct mlxsw_sp
*mlxsw_sp
, void *priv
)
260 struct mlxsw_sp2_kvdl
*kvdl
= priv
;
262 mlxsw_sp2_kvdl_parts_fini(kvdl
);
265 const struct mlxsw_sp_kvdl_ops mlxsw_sp2_kvdl_ops
= {
266 .priv_size
= sizeof(struct mlxsw_sp2_kvdl
),
267 .init
= mlxsw_sp2_kvdl_init
,
268 .fini
= mlxsw_sp2_kvdl_fini
,
269 .alloc
= mlxsw_sp2_kvdl_alloc
,
270 .free
= mlxsw_sp2_kvdl_free
,
271 .alloc_size_query
= mlxsw_sp2_kvdl_alloc_size_query
,