Skip to content

Commit c1a3831

Browse files
jpirkodavem330
authored andcommitted
mlxsw: Convert resources into array
Since the number of resources is going to get much bigger, ease up the addition by simly defining IDs. Convert the existing structure members to a set array, one for validity, one for values. Introduce a set of getters and setters for easy access. Signed-off-by: Jiri Pirko <[email protected]> Reviewed-by: Ido Schimmel <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f38a231 commit c1a3831

File tree

8 files changed

+223
-190
lines changed

8 files changed

+223
-190
lines changed

drivers/net/ethernet/mellanox/mlxsw/core.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#include "trap.h"
6868
#include "emad.h"
6969
#include "reg.h"
70+
#include "resources.h"
7071

7172
static LIST_HEAD(mlxsw_core_driver_list);
7273
static DEFINE_SPINLOCK(mlxsw_core_driver_list_lock);
@@ -111,7 +112,7 @@ struct mlxsw_core {
111112
struct {
112113
u8 *mapping; /* lag_id+port_index to local_port mapping */
113114
} lag;
114-
struct mlxsw_resources resources;
115+
struct mlxsw_res res;
115116
struct mlxsw_hwmon *hwmon;
116117
unsigned long driver_priv[0];
117118
/* driver_priv has to be always the last item */
@@ -1101,14 +1102,15 @@ int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
11011102
}
11021103

11031104
err = mlxsw_bus->init(bus_priv, mlxsw_core, mlxsw_driver->profile,
1104-
&mlxsw_core->resources);
1105+
&mlxsw_core->res);
11051106
if (err)
11061107
goto err_bus_init;
11071108

1108-
if (mlxsw_core->resources.max_lag_valid &&
1109-
mlxsw_core->resources.max_ports_in_lag_valid) {
1110-
alloc_size = sizeof(u8) * mlxsw_core->resources.max_lag *
1111-
mlxsw_core->resources.max_ports_in_lag;
1109+
if (MLXSW_CORE_RES_VALID(mlxsw_core, MAX_LAG) &&
1110+
MLXSW_CORE_RES_VALID(mlxsw_core, MAX_LAG_MEMBERS)) {
1111+
alloc_size = sizeof(u8) *
1112+
MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG) *
1113+
MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG_MEMBERS);
11121114
mlxsw_core->lag.mapping = kzalloc(alloc_size, GFP_KERNEL);
11131115
if (!mlxsw_core->lag.mapping) {
11141116
err = -ENOMEM;
@@ -1615,7 +1617,7 @@ EXPORT_SYMBOL(mlxsw_core_skb_receive);
16151617
static int mlxsw_core_lag_mapping_index(struct mlxsw_core *mlxsw_core,
16161618
u16 lag_id, u8 port_index)
16171619
{
1618-
return mlxsw_core->resources.max_ports_in_lag * lag_id +
1620+
return MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG_MEMBERS) * lag_id +
16191621
port_index;
16201622
}
16211623

@@ -1644,7 +1646,7 @@ void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
16441646
{
16451647
int i;
16461648

1647-
for (i = 0; i < mlxsw_core->resources.max_ports_in_lag; i++) {
1649+
for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG_MEMBERS); i++) {
16481650
int index = mlxsw_core_lag_mapping_index(mlxsw_core,
16491651
lag_id, i);
16501652

@@ -1654,11 +1656,19 @@ void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
16541656
}
16551657
EXPORT_SYMBOL(mlxsw_core_lag_mapping_clear);
16561658

1657-
struct mlxsw_resources *mlxsw_core_resources_get(struct mlxsw_core *mlxsw_core)
1659+
bool mlxsw_core_res_valid(struct mlxsw_core *mlxsw_core,
1660+
enum mlxsw_res_id res_id)
16581661
{
1659-
return &mlxsw_core->resources;
1662+
return mlxsw_res_valid(&mlxsw_core->res, res_id);
16601663
}
1661-
EXPORT_SYMBOL(mlxsw_core_resources_get);
1664+
EXPORT_SYMBOL(mlxsw_core_res_valid);
1665+
1666+
u64 mlxsw_core_res_get(struct mlxsw_core *mlxsw_core,
1667+
enum mlxsw_res_id res_id)
1668+
{
1669+
return mlxsw_res_get(&mlxsw_core->res, res_id);
1670+
}
1671+
EXPORT_SYMBOL(mlxsw_core_res_get);
16621672

16631673
int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core,
16641674
struct mlxsw_core_port *mlxsw_core_port, u8 local_port,

drivers/net/ethernet/mellanox/mlxsw/core.h

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848

4949
#include "trap.h"
5050
#include "reg.h"
51-
5251
#include "cmd.h"
52+
#include "resources.h"
5353

5454
#define MLXSW_MODULE_ALIAS_PREFIX "mlxsw-driver-"
5555
#define MODULE_MLXSW_DRIVER_ALIAS(kind) \
@@ -266,45 +266,23 @@ struct mlxsw_driver {
266266
const struct mlxsw_config_profile *profile;
267267
};
268268

269-
struct mlxsw_resources {
270-
u32 max_span_valid:1,
271-
max_lag_valid:1,
272-
max_ports_in_lag_valid:1,
273-
kvd_size_valid:1,
274-
kvd_single_min_size_valid:1,
275-
kvd_double_min_size_valid:1,
276-
max_virtual_routers_valid:1,
277-
max_system_ports_valid:1,
278-
max_vlan_groups_valid:1,
279-
max_regions_valid:1,
280-
max_rif_valid:1;
281-
u8 max_span;
282-
u8 max_lag;
283-
u8 max_ports_in_lag;
284-
u32 kvd_size;
285-
u32 kvd_single_min_size;
286-
u32 kvd_double_min_size;
287-
u16 max_virtual_routers;
288-
u16 max_system_ports;
289-
u16 max_vlan_groups;
290-
u16 max_regions;
291-
u16 max_rif;
269+
bool mlxsw_core_res_valid(struct mlxsw_core *mlxsw_core,
270+
enum mlxsw_res_id res_id);
292271

293-
/* Internal resources.
294-
* Determined by the SW, not queried from the HW.
295-
*/
296-
u32 kvd_single_size;
297-
u32 kvd_double_size;
298-
u32 kvd_linear_size;
299-
};
272+
#define MLXSW_CORE_RES_VALID(res, short_res_id) \
273+
mlxsw_core_res_valid(res, MLXSW_RES_ID_##short_res_id)
274+
275+
u64 mlxsw_core_res_get(struct mlxsw_core *mlxsw_core,
276+
enum mlxsw_res_id res_id);
300277

301-
struct mlxsw_resources *mlxsw_core_resources_get(struct mlxsw_core *mlxsw_core);
278+
#define MLXSW_CORE_RES_GET(res, short_res_id) \
279+
mlxsw_core_res_get(res, MLXSW_RES_ID_##short_res_id)
302280

303281
struct mlxsw_bus {
304282
const char *kind;
305283
int (*init)(void *bus_priv, struct mlxsw_core *mlxsw_core,
306284
const struct mlxsw_config_profile *profile,
307-
struct mlxsw_resources *resources);
285+
struct mlxsw_res *res);
308286
void (*fini)(void *bus_priv);
309287
bool (*skb_transmit_busy)(void *bus_priv,
310288
const struct mlxsw_tx_info *tx_info);

drivers/net/ethernet/mellanox/mlxsw/pci.c

Lines changed: 32 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "core.h"
5353
#include "cmd.h"
5454
#include "port.h"
55+
#include "resources.h"
5556

5657
static const char mlxsw_pci_driver_name[] = "mlxsw_pci";
5758

@@ -1155,73 +1156,8 @@ mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
11551156
mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
11561157
}
11571158

1158-
#define MLXSW_MAX_SPAN_ID 0x2420
1159-
#define MLXSW_MAX_LAG_ID 0x2520
1160-
#define MLXSW_MAX_PORTS_IN_LAG_ID 0x2521
1161-
#define MLXSW_KVD_SIZE_ID 0x1001
1162-
#define MLXSW_KVD_SINGLE_MIN_SIZE_ID 0x1002
1163-
#define MLXSW_KVD_DOUBLE_MIN_SIZE_ID 0x1003
1164-
#define MLXSW_MAX_VIRTUAL_ROUTERS_ID 0x2C01
1165-
#define MLXSW_MAX_SYSTEM_PORT_ID 0x2502
1166-
#define MLXSW_MAX_VLAN_GROUPS_ID 0x2906
1167-
#define MLXSW_MAX_REGIONS_ID 0x2901
1168-
#define MLXSW_MAX_RIF_ID 0x2C02
1169-
1170-
static void mlxsw_pci_resources_query_parse(int id, u64 val,
1171-
struct mlxsw_resources *resources)
1172-
{
1173-
switch (id) {
1174-
case MLXSW_MAX_SPAN_ID:
1175-
resources->max_span = val;
1176-
resources->max_span_valid = 1;
1177-
break;
1178-
case MLXSW_MAX_LAG_ID:
1179-
resources->max_lag = val;
1180-
resources->max_lag_valid = 1;
1181-
break;
1182-
case MLXSW_MAX_PORTS_IN_LAG_ID:
1183-
resources->max_ports_in_lag = val;
1184-
resources->max_ports_in_lag_valid = 1;
1185-
break;
1186-
case MLXSW_KVD_SIZE_ID:
1187-
resources->kvd_size = val;
1188-
resources->kvd_size_valid = 1;
1189-
break;
1190-
case MLXSW_KVD_SINGLE_MIN_SIZE_ID:
1191-
resources->kvd_single_min_size = val;
1192-
resources->kvd_single_min_size_valid = 1;
1193-
break;
1194-
case MLXSW_KVD_DOUBLE_MIN_SIZE_ID:
1195-
resources->kvd_double_min_size = val;
1196-
resources->kvd_double_min_size_valid = 1;
1197-
break;
1198-
case MLXSW_MAX_VIRTUAL_ROUTERS_ID:
1199-
resources->max_virtual_routers = val;
1200-
resources->max_virtual_routers_valid = 1;
1201-
break;
1202-
case MLXSW_MAX_SYSTEM_PORT_ID:
1203-
resources->max_system_ports = val;
1204-
resources->max_system_ports_valid = 1;
1205-
break;
1206-
case MLXSW_MAX_VLAN_GROUPS_ID:
1207-
resources->max_vlan_groups = val;
1208-
resources->max_vlan_groups_valid = 1;
1209-
break;
1210-
case MLXSW_MAX_REGIONS_ID:
1211-
resources->max_regions = val;
1212-
resources->max_regions_valid = 1;
1213-
break;
1214-
case MLXSW_MAX_RIF_ID:
1215-
resources->max_rif = val;
1216-
resources->max_rif_valid = 1;
1217-
break;
1218-
default:
1219-
break;
1220-
}
1221-
}
1222-
12231159
static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
1224-
struct mlxsw_resources *resources,
1160+
struct mlxsw_res *res,
12251161
u8 query_enabled)
12261162
{
12271163
int index, i;
@@ -1248,7 +1184,7 @@ static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
12481184
if (id == MLXSW_CMD_QUERY_RESOURCES_TABLE_END_ID)
12491185
return 0;
12501186

1251-
mlxsw_pci_resources_query_parse(id, data, resources);
1187+
mlxsw_res_parse(res, id, data);
12521188
}
12531189
}
12541190

@@ -1258,13 +1194,14 @@ static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
12581194
return -EIO;
12591195
}
12601196

1261-
static int mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_config_profile *profile,
1262-
struct mlxsw_resources *resources)
1197+
static int
1198+
mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_config_profile *profile,
1199+
struct mlxsw_res *res)
12631200
{
1264-
u32 singles_size, doubles_size, linear_size;
1201+
u32 single_size, double_size, linear_size;
12651202

1266-
if (!resources->kvd_single_min_size_valid ||
1267-
!resources->kvd_double_min_size_valid ||
1203+
if (!MLXSW_RES_VALID(res, KVD_SINGLE_MIN_SIZE) ||
1204+
!MLXSW_RES_VALID(res, KVD_DOUBLE_MIN_SIZE) ||
12681205
!profile->used_kvd_split_data)
12691206
return -EIO;
12701207

@@ -1276,31 +1213,31 @@ static int mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_config_profile *pr
12761213
* Both sizes must be a multiplications of the
12771214
* granularity from the profile.
12781215
*/
1279-
doubles_size = (resources->kvd_size - linear_size);
1280-
doubles_size *= profile->kvd_hash_double_parts;
1281-
doubles_size /= (profile->kvd_hash_double_parts +
1282-
profile->kvd_hash_single_parts);
1283-
doubles_size /= profile->kvd_hash_granularity;
1284-
doubles_size *= profile->kvd_hash_granularity;
1285-
singles_size = resources->kvd_size - doubles_size -
1286-
linear_size;
1216+
double_size = MLXSW_RES_GET(res, KVD_SIZE) - linear_size;
1217+
double_size *= profile->kvd_hash_double_parts;
1218+
double_size /= profile->kvd_hash_double_parts +
1219+
profile->kvd_hash_single_parts;
1220+
double_size /= profile->kvd_hash_granularity;
1221+
double_size *= profile->kvd_hash_granularity;
1222+
single_size = MLXSW_RES_GET(res, KVD_SIZE) - double_size -
1223+
linear_size;
12871224

12881225
/* Check results are legal. */
1289-
if (singles_size < resources->kvd_single_min_size ||
1290-
doubles_size < resources->kvd_double_min_size ||
1291-
resources->kvd_size < linear_size)
1226+
if (single_size < MLXSW_RES_GET(res, KVD_SINGLE_MIN_SIZE) ||
1227+
double_size < MLXSW_RES_GET(res, KVD_DOUBLE_MIN_SIZE) ||
1228+
MLXSW_RES_GET(res, KVD_SIZE) < linear_size)
12921229
return -EIO;
12931230

1294-
resources->kvd_single_size = singles_size;
1295-
resources->kvd_double_size = doubles_size;
1296-
resources->kvd_linear_size = linear_size;
1231+
MLXSW_RES_SET(res, KVD_SINGLE_SIZE, single_size);
1232+
MLXSW_RES_SET(res, KVD_DOUBLE_SIZE, double_size);
1233+
MLXSW_RES_SET(res, KVD_LINEAR_SIZE, linear_size);
12971234

12981235
return 0;
12991236
}
13001237

13011238
static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
13021239
const struct mlxsw_config_profile *profile,
1303-
struct mlxsw_resources *resources)
1240+
struct mlxsw_res *res)
13041241
{
13051242
int i;
13061243
int err;
@@ -1389,22 +1326,22 @@ static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
13891326
mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
13901327
mbox, profile->adaptive_routing_group_cap);
13911328
}
1392-
if (resources->kvd_size_valid) {
1393-
err = mlxsw_pci_profile_get_kvd_sizes(profile, resources);
1329+
if (MLXSW_RES_VALID(res, KVD_SIZE)) {
1330+
err = mlxsw_pci_profile_get_kvd_sizes(profile, res);
13941331
if (err)
13951332
return err;
13961333

13971334
mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(mbox, 1);
13981335
mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(mbox,
1399-
resources->kvd_linear_size);
1336+
MLXSW_RES_GET(res, KVD_LINEAR_SIZE));
14001337
mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(mbox,
14011338
1);
14021339
mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(mbox,
1403-
resources->kvd_single_size);
1340+
MLXSW_RES_GET(res, KVD_SINGLE_SIZE));
14041341
mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set(
14051342
mbox, 1);
14061343
mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(mbox,
1407-
resources->kvd_double_size);
1344+
MLXSW_RES_GET(res, KVD_DOUBLE_SIZE));
14081345
}
14091346

14101347
for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
@@ -1542,7 +1479,7 @@ static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
15421479

15431480
static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
15441481
const struct mlxsw_config_profile *profile,
1545-
struct mlxsw_resources *resources)
1482+
struct mlxsw_res *res)
15461483
{
15471484
struct mlxsw_pci *mlxsw_pci = bus_priv;
15481485
struct pci_dev *pdev = mlxsw_pci->pdev;
@@ -1601,12 +1538,12 @@ static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
16011538
if (err)
16021539
goto err_boardinfo;
16031540

1604-
err = mlxsw_pci_resources_query(mlxsw_pci, mbox, resources,
1541+
err = mlxsw_pci_resources_query(mlxsw_pci, mbox, res,
16051542
profile->resource_query_enable);
16061543
if (err)
16071544
goto err_query_resources;
16081545

1609-
err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, resources);
1546+
err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, res);
16101547
if (err)
16111548
goto err_config_profile;
16121549

0 commit comments

Comments
 (0)