1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */ 2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */ 3 4 #ifndef _MLXSW_CORE_H 5 #define _MLXSW_CORE_H 6 7 #include <linux/module.h> 8 #include <linux/device.h> 9 #include <linux/slab.h> 10 #include <linux/gfp.h> 11 #include <linux/types.h> 12 #include <linux/skbuff.h> 13 #include <linux/workqueue.h> 14 #include <linux/net_namespace.h> 15 #include <net/devlink.h> 16 17 #include "trap.h" 18 #include "reg.h" 19 #include "cmd.h" 20 #include "resources.h" 21 22 struct mlxsw_core; 23 struct mlxsw_core_port; 24 struct mlxsw_driver; 25 struct mlxsw_bus; 26 struct mlxsw_bus_info; 27 struct mlxsw_fw_rev; 28 29 unsigned int mlxsw_core_max_ports(const struct mlxsw_core *mlxsw_core); 30 31 void *mlxsw_core_driver_priv(struct mlxsw_core *mlxsw_core); 32 33 bool mlxsw_core_res_query_enabled(const struct mlxsw_core *mlxsw_core); 34 35 bool 36 mlxsw_core_fw_rev_minor_subminor_validate(const struct mlxsw_fw_rev *rev, 37 const struct mlxsw_fw_rev *req_rev); 38 39 int mlxsw_core_driver_register(struct mlxsw_driver *mlxsw_driver); 40 void mlxsw_core_driver_unregister(struct mlxsw_driver *mlxsw_driver); 41 42 int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info, 43 const struct mlxsw_bus *mlxsw_bus, 44 void *bus_priv, bool reload, 45 struct devlink *devlink, 46 struct netlink_ext_ack *extack); 47 void mlxsw_core_bus_device_unregister(struct mlxsw_core *mlxsw_core, bool reload); 48 49 struct mlxsw_tx_info { 50 u8 local_port; 51 bool is_emad; 52 }; 53 54 bool mlxsw_core_skb_transmit_busy(struct mlxsw_core *mlxsw_core, 55 const struct mlxsw_tx_info *tx_info); 56 int mlxsw_core_skb_transmit(struct mlxsw_core *mlxsw_core, struct sk_buff *skb, 57 const struct mlxsw_tx_info *tx_info); 58 void mlxsw_core_ptp_transmitted(struct mlxsw_core *mlxsw_core, 59 struct sk_buff *skb, u8 local_port); 60 61 struct mlxsw_rx_listener { 62 void (*func)(struct sk_buff *skb, u8 local_port, void *priv); 63 u8 local_port; 64 u16 trap_id; 65 }; 66 67 struct mlxsw_event_listener { 68 void (*func)(const struct mlxsw_reg_info *reg, 69 char *payload, void *priv); 70 enum mlxsw_event_trap_id trap_id; 71 }; 72 73 struct mlxsw_listener { 74 u16 trap_id; 75 union { 76 struct mlxsw_rx_listener rx_listener; 77 struct mlxsw_event_listener event_listener; 78 }; 79 enum mlxsw_reg_hpkt_action en_action; /* Action when enabled */ 80 enum mlxsw_reg_hpkt_action dis_action; /* Action when disabled */ 81 u8 en_trap_group; /* Trap group when enabled */ 82 u8 dis_trap_group; /* Trap group when disabled */ 83 u8 is_ctrl:1, /* should go via control buffer or not */ 84 is_event:1, 85 enabled_on_register:1; /* Trap should be enabled when listener 86 * is registered. 87 */ 88 }; 89 90 #define __MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _en_trap_group, \ 91 _dis_action, _enabled_on_register, _dis_trap_group) \ 92 { \ 93 .trap_id = MLXSW_TRAP_ID_##_trap_id, \ 94 .rx_listener = \ 95 { \ 96 .func = _func, \ 97 .local_port = MLXSW_PORT_DONT_CARE, \ 98 .trap_id = MLXSW_TRAP_ID_##_trap_id, \ 99 }, \ 100 .en_action = MLXSW_REG_HPKT_ACTION_##_en_action, \ 101 .dis_action = MLXSW_REG_HPKT_ACTION_##_dis_action, \ 102 .en_trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_en_trap_group, \ 103 .dis_trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_dis_trap_group, \ 104 .is_ctrl = _is_ctrl, \ 105 .enabled_on_register = _enabled_on_register, \ 106 } 107 108 #define MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _trap_group, \ 109 _dis_action) \ 110 __MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _trap_group, \ 111 _dis_action, true, _trap_group) 112 113 #define MLXSW_RXL_DIS(_func, _trap_id, _en_action, _is_ctrl, _en_trap_group, \ 114 _dis_action, _dis_trap_group) \ 115 __MLXSW_RXL(_func, _trap_id, _en_action, _is_ctrl, _en_trap_group, \ 116 _dis_action, false, _dis_trap_group) 117 118 #define MLXSW_EVENTL(_func, _trap_id, _trap_group) \ 119 { \ 120 .trap_id = MLXSW_TRAP_ID_##_trap_id, \ 121 .event_listener = \ 122 { \ 123 .func = _func, \ 124 .trap_id = MLXSW_TRAP_ID_##_trap_id, \ 125 }, \ 126 .en_action = MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU, \ 127 .en_trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_trap_group, \ 128 .is_event = true, \ 129 .enabled_on_register = true, \ 130 } 131 132 int mlxsw_core_rx_listener_register(struct mlxsw_core *mlxsw_core, 133 const struct mlxsw_rx_listener *rxl, 134 void *priv, bool enabled); 135 void mlxsw_core_rx_listener_unregister(struct mlxsw_core *mlxsw_core, 136 const struct mlxsw_rx_listener *rxl); 137 138 int mlxsw_core_event_listener_register(struct mlxsw_core *mlxsw_core, 139 const struct mlxsw_event_listener *el, 140 void *priv); 141 void mlxsw_core_event_listener_unregister(struct mlxsw_core *mlxsw_core, 142 const struct mlxsw_event_listener *el); 143 144 int mlxsw_core_trap_register(struct mlxsw_core *mlxsw_core, 145 const struct mlxsw_listener *listener, 146 void *priv); 147 void mlxsw_core_trap_unregister(struct mlxsw_core *mlxsw_core, 148 const struct mlxsw_listener *listener, 149 void *priv); 150 int mlxsw_core_trap_state_set(struct mlxsw_core *mlxsw_core, 151 const struct mlxsw_listener *listener, 152 bool enabled); 153 154 typedef void mlxsw_reg_trans_cb_t(struct mlxsw_core *mlxsw_core, char *payload, 155 size_t payload_len, unsigned long cb_priv); 156 157 int mlxsw_reg_trans_query(struct mlxsw_core *mlxsw_core, 158 const struct mlxsw_reg_info *reg, char *payload, 159 struct list_head *bulk_list, 160 mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv); 161 int mlxsw_reg_trans_write(struct mlxsw_core *mlxsw_core, 162 const struct mlxsw_reg_info *reg, char *payload, 163 struct list_head *bulk_list, 164 mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv); 165 int mlxsw_reg_trans_bulk_wait(struct list_head *bulk_list); 166 167 int mlxsw_reg_query(struct mlxsw_core *mlxsw_core, 168 const struct mlxsw_reg_info *reg, char *payload); 169 int mlxsw_reg_write(struct mlxsw_core *mlxsw_core, 170 const struct mlxsw_reg_info *reg, char *payload); 171 172 struct mlxsw_rx_info { 173 bool is_lag; 174 union { 175 u16 sys_port; 176 u16 lag_id; 177 } u; 178 u8 lag_port_index; 179 int trap_id; 180 }; 181 182 void mlxsw_core_skb_receive(struct mlxsw_core *mlxsw_core, struct sk_buff *skb, 183 struct mlxsw_rx_info *rx_info); 184 185 void mlxsw_core_lag_mapping_set(struct mlxsw_core *mlxsw_core, 186 u16 lag_id, u8 port_index, u8 local_port); 187 u8 mlxsw_core_lag_mapping_get(struct mlxsw_core *mlxsw_core, 188 u16 lag_id, u8 port_index); 189 void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core, 190 u16 lag_id, u8 local_port); 191 192 void *mlxsw_core_port_driver_priv(struct mlxsw_core_port *mlxsw_core_port); 193 int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core, u8 local_port, 194 u32 port_number, bool split, 195 u32 split_port_subnumber, 196 const unsigned char *switch_id, 197 unsigned char switch_id_len); 198 void mlxsw_core_port_fini(struct mlxsw_core *mlxsw_core, u8 local_port); 199 int mlxsw_core_cpu_port_init(struct mlxsw_core *mlxsw_core, 200 void *port_driver_priv, 201 const unsigned char *switch_id, 202 unsigned char switch_id_len); 203 void mlxsw_core_cpu_port_fini(struct mlxsw_core *mlxsw_core); 204 void mlxsw_core_port_eth_set(struct mlxsw_core *mlxsw_core, u8 local_port, 205 void *port_driver_priv, struct net_device *dev); 206 void mlxsw_core_port_ib_set(struct mlxsw_core *mlxsw_core, u8 local_port, 207 void *port_driver_priv); 208 void mlxsw_core_port_clear(struct mlxsw_core *mlxsw_core, u8 local_port, 209 void *port_driver_priv); 210 enum devlink_port_type mlxsw_core_port_type_get(struct mlxsw_core *mlxsw_core, 211 u8 local_port); 212 struct devlink_port * 213 mlxsw_core_port_devlink_port_get(struct mlxsw_core *mlxsw_core, 214 u8 local_port); 215 int mlxsw_core_module_max_width(struct mlxsw_core *mlxsw_core, u8 module); 216 217 int mlxsw_core_schedule_dw(struct delayed_work *dwork, unsigned long delay); 218 bool mlxsw_core_schedule_work(struct work_struct *work); 219 void mlxsw_core_flush_owq(void); 220 int mlxsw_core_resources_query(struct mlxsw_core *mlxsw_core, char *mbox, 221 struct mlxsw_res *res); 222 223 #define MLXSW_CONFIG_PROFILE_SWID_COUNT 8 224 225 struct mlxsw_swid_config { 226 u8 used_type:1, 227 used_properties:1; 228 u8 type; 229 u8 properties; 230 }; 231 232 struct mlxsw_config_profile { 233 u16 used_max_vepa_channels:1, 234 used_max_mid:1, 235 used_max_pgt:1, 236 used_max_system_port:1, 237 used_max_vlan_groups:1, 238 used_max_regions:1, 239 used_flood_tables:1, 240 used_flood_mode:1, 241 used_max_ib_mc:1, 242 used_max_pkey:1, 243 used_ar_sec:1, 244 used_adaptive_routing_group_cap:1, 245 used_kvd_sizes:1; 246 u8 max_vepa_channels; 247 u16 max_mid; 248 u16 max_pgt; 249 u16 max_system_port; 250 u16 max_vlan_groups; 251 u16 max_regions; 252 u8 max_flood_tables; 253 u8 max_vid_flood_tables; 254 u8 flood_mode; 255 u8 max_fid_offset_flood_tables; 256 u16 fid_offset_flood_table_size; 257 u8 max_fid_flood_tables; 258 u16 fid_flood_table_size; 259 u16 max_ib_mc; 260 u16 max_pkey; 261 u8 ar_sec; 262 u16 adaptive_routing_group_cap; 263 u8 arn; 264 u32 kvd_linear_size; 265 u8 kvd_hash_single_parts; 266 u8 kvd_hash_double_parts; 267 struct mlxsw_swid_config swid_config[MLXSW_CONFIG_PROFILE_SWID_COUNT]; 268 }; 269 270 struct mlxsw_driver { 271 struct list_head list; 272 const char *kind; 273 size_t priv_size; 274 int (*init)(struct mlxsw_core *mlxsw_core, 275 const struct mlxsw_bus_info *mlxsw_bus_info, 276 struct netlink_ext_ack *extack); 277 void (*fini)(struct mlxsw_core *mlxsw_core); 278 int (*basic_trap_groups_set)(struct mlxsw_core *mlxsw_core); 279 int (*port_type_set)(struct mlxsw_core *mlxsw_core, u8 local_port, 280 enum devlink_port_type new_type); 281 int (*port_split)(struct mlxsw_core *mlxsw_core, u8 local_port, 282 unsigned int count, struct netlink_ext_ack *extack); 283 int (*port_unsplit)(struct mlxsw_core *mlxsw_core, u8 local_port, 284 struct netlink_ext_ack *extack); 285 int (*sb_pool_get)(struct mlxsw_core *mlxsw_core, 286 unsigned int sb_index, u16 pool_index, 287 struct devlink_sb_pool_info *pool_info); 288 int (*sb_pool_set)(struct mlxsw_core *mlxsw_core, 289 unsigned int sb_index, u16 pool_index, u32 size, 290 enum devlink_sb_threshold_type threshold_type, 291 struct netlink_ext_ack *extack); 292 int (*sb_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port, 293 unsigned int sb_index, u16 pool_index, 294 u32 *p_threshold); 295 int (*sb_port_pool_set)(struct mlxsw_core_port *mlxsw_core_port, 296 unsigned int sb_index, u16 pool_index, 297 u32 threshold, struct netlink_ext_ack *extack); 298 int (*sb_tc_pool_bind_get)(struct mlxsw_core_port *mlxsw_core_port, 299 unsigned int sb_index, u16 tc_index, 300 enum devlink_sb_pool_type pool_type, 301 u16 *p_pool_index, u32 *p_threshold); 302 int (*sb_tc_pool_bind_set)(struct mlxsw_core_port *mlxsw_core_port, 303 unsigned int sb_index, u16 tc_index, 304 enum devlink_sb_pool_type pool_type, 305 u16 pool_index, u32 threshold, 306 struct netlink_ext_ack *extack); 307 int (*sb_occ_snapshot)(struct mlxsw_core *mlxsw_core, 308 unsigned int sb_index); 309 int (*sb_occ_max_clear)(struct mlxsw_core *mlxsw_core, 310 unsigned int sb_index); 311 int (*sb_occ_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port, 312 unsigned int sb_index, u16 pool_index, 313 u32 *p_cur, u32 *p_max); 314 int (*sb_occ_tc_port_bind_get)(struct mlxsw_core_port *mlxsw_core_port, 315 unsigned int sb_index, u16 tc_index, 316 enum devlink_sb_pool_type pool_type, 317 u32 *p_cur, u32 *p_max); 318 int (*flash_update)(struct mlxsw_core *mlxsw_core, 319 const char *file_name, const char *component, 320 struct netlink_ext_ack *extack); 321 int (*trap_init)(struct mlxsw_core *mlxsw_core, 322 const struct devlink_trap *trap, void *trap_ctx); 323 void (*trap_fini)(struct mlxsw_core *mlxsw_core, 324 const struct devlink_trap *trap, void *trap_ctx); 325 int (*trap_action_set)(struct mlxsw_core *mlxsw_core, 326 const struct devlink_trap *trap, 327 enum devlink_trap_action action); 328 int (*trap_group_init)(struct mlxsw_core *mlxsw_core, 329 const struct devlink_trap_group *group); 330 int (*trap_group_set)(struct mlxsw_core *mlxsw_core, 331 const struct devlink_trap_group *group, 332 const struct devlink_trap_policer *policer); 333 int (*trap_policer_init)(struct mlxsw_core *mlxsw_core, 334 const struct devlink_trap_policer *policer); 335 void (*trap_policer_fini)(struct mlxsw_core *mlxsw_core, 336 const struct devlink_trap_policer *policer); 337 int (*trap_policer_set)(struct mlxsw_core *mlxsw_core, 338 const struct devlink_trap_policer *policer, 339 u64 rate, u64 burst, 340 struct netlink_ext_ack *extack); 341 int (*trap_policer_counter_get)(struct mlxsw_core *mlxsw_core, 342 const struct devlink_trap_policer *policer, 343 u64 *p_drops); 344 void (*txhdr_construct)(struct sk_buff *skb, 345 const struct mlxsw_tx_info *tx_info); 346 int (*resources_register)(struct mlxsw_core *mlxsw_core); 347 int (*kvd_sizes_get)(struct mlxsw_core *mlxsw_core, 348 const struct mlxsw_config_profile *profile, 349 u64 *p_single_size, u64 *p_double_size, 350 u64 *p_linear_size); 351 int (*params_register)(struct mlxsw_core *mlxsw_core); 352 void (*params_unregister)(struct mlxsw_core *mlxsw_core); 353 354 /* Notify a driver that a timestamped packet was transmitted. Driver 355 * is responsible for freeing the passed-in SKB. 356 */ 357 void (*ptp_transmitted)(struct mlxsw_core *mlxsw_core, 358 struct sk_buff *skb, u8 local_port); 359 360 u8 txhdr_len; 361 const struct mlxsw_config_profile *profile; 362 bool res_query_enabled; 363 }; 364 365 int mlxsw_core_kvd_sizes_get(struct mlxsw_core *mlxsw_core, 366 const struct mlxsw_config_profile *profile, 367 u64 *p_single_size, u64 *p_double_size, 368 u64 *p_linear_size); 369 370 void mlxsw_core_fw_flash_start(struct mlxsw_core *mlxsw_core); 371 void mlxsw_core_fw_flash_end(struct mlxsw_core *mlxsw_core); 372 373 u32 mlxsw_core_read_frc_h(struct mlxsw_core *mlxsw_core); 374 u32 mlxsw_core_read_frc_l(struct mlxsw_core *mlxsw_core); 375 376 void mlxsw_core_emad_string_tlv_enable(struct mlxsw_core *mlxsw_core); 377 378 bool mlxsw_core_res_valid(struct mlxsw_core *mlxsw_core, 379 enum mlxsw_res_id res_id); 380 381 #define MLXSW_CORE_RES_VALID(mlxsw_core, short_res_id) \ 382 mlxsw_core_res_valid(mlxsw_core, MLXSW_RES_ID_##short_res_id) 383 384 u64 mlxsw_core_res_get(struct mlxsw_core *mlxsw_core, 385 enum mlxsw_res_id res_id); 386 387 #define MLXSW_CORE_RES_GET(mlxsw_core, short_res_id) \ 388 mlxsw_core_res_get(mlxsw_core, MLXSW_RES_ID_##short_res_id) 389 390 static inline struct net *mlxsw_core_net(struct mlxsw_core *mlxsw_core) 391 { 392 return devlink_net(priv_to_devlink(mlxsw_core)); 393 } 394 395 #define MLXSW_BUS_F_TXRX BIT(0) 396 #define MLXSW_BUS_F_RESET BIT(1) 397 398 struct mlxsw_bus { 399 const char *kind; 400 int (*init)(void *bus_priv, struct mlxsw_core *mlxsw_core, 401 const struct mlxsw_config_profile *profile, 402 struct mlxsw_res *res); 403 void (*fini)(void *bus_priv); 404 bool (*skb_transmit_busy)(void *bus_priv, 405 const struct mlxsw_tx_info *tx_info); 406 int (*skb_transmit)(void *bus_priv, struct sk_buff *skb, 407 const struct mlxsw_tx_info *tx_info); 408 int (*cmd_exec)(void *bus_priv, u16 opcode, u8 opcode_mod, 409 u32 in_mod, bool out_mbox_direct, 410 char *in_mbox, size_t in_mbox_size, 411 char *out_mbox, size_t out_mbox_size, 412 u8 *p_status); 413 u32 (*read_frc_h)(void *bus_priv); 414 u32 (*read_frc_l)(void *bus_priv); 415 u8 features; 416 }; 417 418 struct mlxsw_fw_rev { 419 u16 major; 420 u16 minor; 421 u16 subminor; 422 u16 can_reset_minor; 423 }; 424 425 struct mlxsw_bus_info { 426 const char *device_kind; 427 const char *device_name; 428 struct device *dev; 429 struct mlxsw_fw_rev fw_rev; 430 u8 vsd[MLXSW_CMD_BOARDINFO_VSD_LEN]; 431 u8 psid[MLXSW_CMD_BOARDINFO_PSID_LEN]; 432 u8 low_frequency:1, 433 read_frc_capable:1; 434 }; 435 436 struct mlxsw_hwmon; 437 438 #ifdef CONFIG_MLXSW_CORE_HWMON 439 440 int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core, 441 const struct mlxsw_bus_info *mlxsw_bus_info, 442 struct mlxsw_hwmon **p_hwmon); 443 void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon); 444 445 #else 446 447 static inline int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core, 448 const struct mlxsw_bus_info *mlxsw_bus_info, 449 struct mlxsw_hwmon **p_hwmon) 450 { 451 return 0; 452 } 453 454 static inline void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon) 455 { 456 } 457 458 #endif 459 460 struct mlxsw_thermal; 461 462 #ifdef CONFIG_MLXSW_CORE_THERMAL 463 464 int mlxsw_thermal_init(struct mlxsw_core *mlxsw_core, 465 const struct mlxsw_bus_info *mlxsw_bus_info, 466 struct mlxsw_thermal **p_thermal); 467 void mlxsw_thermal_fini(struct mlxsw_thermal *thermal); 468 469 #else 470 471 static inline int mlxsw_thermal_init(struct mlxsw_core *mlxsw_core, 472 const struct mlxsw_bus_info *mlxsw_bus_info, 473 struct mlxsw_thermal **p_thermal) 474 { 475 return 0; 476 } 477 478 static inline void mlxsw_thermal_fini(struct mlxsw_thermal *thermal) 479 { 480 } 481 482 #endif 483 484 enum mlxsw_devlink_param_id { 485 MLXSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX, 486 MLXSW_DEVLINK_PARAM_ID_ACL_REGION_REHASH_INTERVAL, 487 }; 488 489 struct mlxsw_skb_cb { 490 union { 491 struct mlxsw_tx_info tx_info; 492 u32 cookie_index; /* Only used during receive */ 493 }; 494 }; 495 496 static inline struct mlxsw_skb_cb *mlxsw_skb_cb(struct sk_buff *skb) 497 { 498 BUILD_BUG_ON(sizeof(mlxsw_skb_cb) > sizeof(skb->cb)); 499 return (struct mlxsw_skb_cb *) skb->cb; 500 } 501 502 #endif 503