core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/core.h
  3. * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
  5. * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
  6. * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the names of the copyright holders nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * Alternatively, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") version 2 as published by the Free
  22. * Software Foundation.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef _MLXSW_CORE_H
  37. #define _MLXSW_CORE_H
  38. #include <linux/module.h>
  39. #include <linux/device.h>
  40. #include <linux/slab.h>
  41. #include <linux/gfp.h>
  42. #include <linux/types.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/workqueue.h>
  45. #include <net/devlink.h>
  46. #include "trap.h"
  47. #include "reg.h"
  48. #include "cmd.h"
  49. #include "resources.h"
  50. struct mlxsw_core;
  51. struct mlxsw_core_port;
  52. struct mlxsw_driver;
  53. struct mlxsw_bus;
  54. struct mlxsw_bus_info;
  55. unsigned int mlxsw_core_max_ports(const struct mlxsw_core *mlxsw_core);
  56. void *mlxsw_core_driver_priv(struct mlxsw_core *mlxsw_core);
  57. int mlxsw_core_driver_register(struct mlxsw_driver *mlxsw_driver);
  58. void mlxsw_core_driver_unregister(struct mlxsw_driver *mlxsw_driver);
  59. int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
  60. const struct mlxsw_bus *mlxsw_bus,
  61. void *bus_priv);
  62. void mlxsw_core_bus_device_unregister(struct mlxsw_core *mlxsw_core);
  63. struct mlxsw_tx_info {
  64. u8 local_port;
  65. bool is_emad;
  66. };
  67. bool mlxsw_core_skb_transmit_busy(struct mlxsw_core *mlxsw_core,
  68. const struct mlxsw_tx_info *tx_info);
  69. int mlxsw_core_skb_transmit(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
  70. const struct mlxsw_tx_info *tx_info);
  71. struct mlxsw_rx_listener {
  72. void (*func)(struct sk_buff *skb, u8 local_port, void *priv);
  73. u8 local_port;
  74. u16 trap_id;
  75. enum mlxsw_reg_hpkt_action action;
  76. };
  77. struct mlxsw_event_listener {
  78. void (*func)(const struct mlxsw_reg_info *reg,
  79. char *payload, void *priv);
  80. enum mlxsw_event_trap_id trap_id;
  81. };
  82. struct mlxsw_listener {
  83. u16 trap_id;
  84. union {
  85. struct mlxsw_rx_listener rx_listener;
  86. struct mlxsw_event_listener event_listener;
  87. } u;
  88. enum mlxsw_reg_hpkt_action action;
  89. enum mlxsw_reg_hpkt_action unreg_action;
  90. u8 trap_group;
  91. bool is_ctrl; /* should go via control buffer or not */
  92. bool is_event;
  93. };
  94. #define MLXSW_RXL(_func, _trap_id, _action, _is_ctrl, _trap_group, \
  95. _unreg_action) \
  96. { \
  97. .trap_id = MLXSW_TRAP_ID_##_trap_id, \
  98. .u.rx_listener = \
  99. { \
  100. .func = _func, \
  101. .local_port = MLXSW_PORT_DONT_CARE, \
  102. .trap_id = MLXSW_TRAP_ID_##_trap_id, \
  103. }, \
  104. .action = MLXSW_REG_HPKT_ACTION_##_action, \
  105. .unreg_action = MLXSW_REG_HPKT_ACTION_##_unreg_action, \
  106. .trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_trap_group, \
  107. .is_ctrl = _is_ctrl, \
  108. .is_event = false, \
  109. }
  110. #define MLXSW_EVENTL(_func, _trap_id, _trap_group) \
  111. { \
  112. .trap_id = MLXSW_TRAP_ID_##_trap_id, \
  113. .u.event_listener = \
  114. { \
  115. .func = _func, \
  116. .trap_id = MLXSW_TRAP_ID_##_trap_id, \
  117. }, \
  118. .action = MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU, \
  119. .trap_group = MLXSW_REG_HTGT_TRAP_GROUP_##_trap_group, \
  120. .is_ctrl = false, \
  121. .is_event = true, \
  122. }
  123. int mlxsw_core_rx_listener_register(struct mlxsw_core *mlxsw_core,
  124. const struct mlxsw_rx_listener *rxl,
  125. void *priv);
  126. void mlxsw_core_rx_listener_unregister(struct mlxsw_core *mlxsw_core,
  127. const struct mlxsw_rx_listener *rxl,
  128. void *priv);
  129. int mlxsw_core_event_listener_register(struct mlxsw_core *mlxsw_core,
  130. const struct mlxsw_event_listener *el,
  131. void *priv);
  132. void mlxsw_core_event_listener_unregister(struct mlxsw_core *mlxsw_core,
  133. const struct mlxsw_event_listener *el,
  134. void *priv);
  135. int mlxsw_core_trap_register(struct mlxsw_core *mlxsw_core,
  136. const struct mlxsw_listener *listener,
  137. void *priv);
  138. void mlxsw_core_trap_unregister(struct mlxsw_core *mlxsw_core,
  139. const struct mlxsw_listener *listener,
  140. void *priv);
  141. typedef void mlxsw_reg_trans_cb_t(struct mlxsw_core *mlxsw_core, char *payload,
  142. size_t payload_len, unsigned long cb_priv);
  143. int mlxsw_reg_trans_query(struct mlxsw_core *mlxsw_core,
  144. const struct mlxsw_reg_info *reg, char *payload,
  145. struct list_head *bulk_list,
  146. mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv);
  147. int mlxsw_reg_trans_write(struct mlxsw_core *mlxsw_core,
  148. const struct mlxsw_reg_info *reg, char *payload,
  149. struct list_head *bulk_list,
  150. mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv);
  151. int mlxsw_reg_trans_bulk_wait(struct list_head *bulk_list);
  152. int mlxsw_reg_query(struct mlxsw_core *mlxsw_core,
  153. const struct mlxsw_reg_info *reg, char *payload);
  154. int mlxsw_reg_write(struct mlxsw_core *mlxsw_core,
  155. const struct mlxsw_reg_info *reg, char *payload);
  156. struct mlxsw_rx_info {
  157. bool is_lag;
  158. union {
  159. u16 sys_port;
  160. u16 lag_id;
  161. } u;
  162. u8 lag_port_index;
  163. int trap_id;
  164. };
  165. void mlxsw_core_skb_receive(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
  166. struct mlxsw_rx_info *rx_info);
  167. void mlxsw_core_lag_mapping_set(struct mlxsw_core *mlxsw_core,
  168. u16 lag_id, u8 port_index, u8 local_port);
  169. u8 mlxsw_core_lag_mapping_get(struct mlxsw_core *mlxsw_core,
  170. u16 lag_id, u8 port_index);
  171. void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
  172. u16 lag_id, u8 local_port);
  173. void *mlxsw_core_port_driver_priv(struct mlxsw_core_port *mlxsw_core_port);
  174. int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core, u8 local_port);
  175. void mlxsw_core_port_fini(struct mlxsw_core *mlxsw_core, u8 local_port);
  176. void mlxsw_core_port_eth_set(struct mlxsw_core *mlxsw_core, u8 local_port,
  177. void *port_driver_priv, struct net_device *dev,
  178. bool split, u32 split_group);
  179. void mlxsw_core_port_ib_set(struct mlxsw_core *mlxsw_core, u8 local_port,
  180. void *port_driver_priv);
  181. void mlxsw_core_port_clear(struct mlxsw_core *mlxsw_core, u8 local_port,
  182. void *port_driver_priv);
  183. enum devlink_port_type mlxsw_core_port_type_get(struct mlxsw_core *mlxsw_core,
  184. u8 local_port);
  185. int mlxsw_core_schedule_dw(struct delayed_work *dwork, unsigned long delay);
  186. bool mlxsw_core_schedule_work(struct work_struct *work);
  187. void mlxsw_core_flush_owq(void);
  188. #define MLXSW_CONFIG_PROFILE_SWID_COUNT 8
  189. struct mlxsw_swid_config {
  190. u8 used_type:1,
  191. used_properties:1;
  192. u8 type;
  193. u8 properties;
  194. };
  195. struct mlxsw_config_profile {
  196. u16 used_max_vepa_channels:1,
  197. used_max_mid:1,
  198. used_max_pgt:1,
  199. used_max_system_port:1,
  200. used_max_vlan_groups:1,
  201. used_max_regions:1,
  202. used_flood_tables:1,
  203. used_flood_mode:1,
  204. used_max_ib_mc:1,
  205. used_max_pkey:1,
  206. used_ar_sec:1,
  207. used_adaptive_routing_group_cap:1,
  208. used_kvd_split_data:1; /* indicate for the kvd's values */
  209. u8 max_vepa_channels;
  210. u16 max_mid;
  211. u16 max_pgt;
  212. u16 max_system_port;
  213. u16 max_vlan_groups;
  214. u16 max_regions;
  215. u8 max_flood_tables;
  216. u8 max_vid_flood_tables;
  217. u8 flood_mode;
  218. u8 max_fid_offset_flood_tables;
  219. u16 fid_offset_flood_table_size;
  220. u8 max_fid_flood_tables;
  221. u16 fid_flood_table_size;
  222. u16 max_ib_mc;
  223. u16 max_pkey;
  224. u8 ar_sec;
  225. u16 adaptive_routing_group_cap;
  226. u8 arn;
  227. u32 kvd_linear_size;
  228. u16 kvd_hash_granularity;
  229. u8 kvd_hash_single_parts;
  230. u8 kvd_hash_double_parts;
  231. u8 resource_query_enable;
  232. struct mlxsw_swid_config swid_config[MLXSW_CONFIG_PROFILE_SWID_COUNT];
  233. };
  234. struct mlxsw_driver {
  235. struct list_head list;
  236. const char *kind;
  237. size_t priv_size;
  238. int (*init)(struct mlxsw_core *mlxsw_core,
  239. const struct mlxsw_bus_info *mlxsw_bus_info);
  240. void (*fini)(struct mlxsw_core *mlxsw_core);
  241. int (*basic_trap_groups_set)(struct mlxsw_core *mlxsw_core);
  242. int (*port_type_set)(struct mlxsw_core *mlxsw_core, u8 local_port,
  243. enum devlink_port_type new_type);
  244. int (*port_split)(struct mlxsw_core *mlxsw_core, u8 local_port,
  245. unsigned int count);
  246. int (*port_unsplit)(struct mlxsw_core *mlxsw_core, u8 local_port);
  247. int (*sb_pool_get)(struct mlxsw_core *mlxsw_core,
  248. unsigned int sb_index, u16 pool_index,
  249. struct devlink_sb_pool_info *pool_info);
  250. int (*sb_pool_set)(struct mlxsw_core *mlxsw_core,
  251. unsigned int sb_index, u16 pool_index, u32 size,
  252. enum devlink_sb_threshold_type threshold_type);
  253. int (*sb_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
  254. unsigned int sb_index, u16 pool_index,
  255. u32 *p_threshold);
  256. int (*sb_port_pool_set)(struct mlxsw_core_port *mlxsw_core_port,
  257. unsigned int sb_index, u16 pool_index,
  258. u32 threshold);
  259. int (*sb_tc_pool_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
  260. unsigned int sb_index, u16 tc_index,
  261. enum devlink_sb_pool_type pool_type,
  262. u16 *p_pool_index, u32 *p_threshold);
  263. int (*sb_tc_pool_bind_set)(struct mlxsw_core_port *mlxsw_core_port,
  264. unsigned int sb_index, u16 tc_index,
  265. enum devlink_sb_pool_type pool_type,
  266. u16 pool_index, u32 threshold);
  267. int (*sb_occ_snapshot)(struct mlxsw_core *mlxsw_core,
  268. unsigned int sb_index);
  269. int (*sb_occ_max_clear)(struct mlxsw_core *mlxsw_core,
  270. unsigned int sb_index);
  271. int (*sb_occ_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
  272. unsigned int sb_index, u16 pool_index,
  273. u32 *p_cur, u32 *p_max);
  274. int (*sb_occ_tc_port_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
  275. unsigned int sb_index, u16 tc_index,
  276. enum devlink_sb_pool_type pool_type,
  277. u32 *p_cur, u32 *p_max);
  278. void (*txhdr_construct)(struct sk_buff *skb,
  279. const struct mlxsw_tx_info *tx_info);
  280. u8 txhdr_len;
  281. const struct mlxsw_config_profile *profile;
  282. };
  283. bool mlxsw_core_res_valid(struct mlxsw_core *mlxsw_core,
  284. enum mlxsw_res_id res_id);
  285. #define MLXSW_CORE_RES_VALID(res, short_res_id) \
  286. mlxsw_core_res_valid(res, MLXSW_RES_ID_##short_res_id)
  287. u64 mlxsw_core_res_get(struct mlxsw_core *mlxsw_core,
  288. enum mlxsw_res_id res_id);
  289. #define MLXSW_CORE_RES_GET(res, short_res_id) \
  290. mlxsw_core_res_get(res, MLXSW_RES_ID_##short_res_id)
  291. #define MLXSW_BUS_F_TXRX BIT(0)
  292. struct mlxsw_bus {
  293. const char *kind;
  294. int (*init)(void *bus_priv, struct mlxsw_core *mlxsw_core,
  295. const struct mlxsw_config_profile *profile,
  296. struct mlxsw_res *res);
  297. void (*fini)(void *bus_priv);
  298. bool (*skb_transmit_busy)(void *bus_priv,
  299. const struct mlxsw_tx_info *tx_info);
  300. int (*skb_transmit)(void *bus_priv, struct sk_buff *skb,
  301. const struct mlxsw_tx_info *tx_info);
  302. int (*cmd_exec)(void *bus_priv, u16 opcode, u8 opcode_mod,
  303. u32 in_mod, bool out_mbox_direct,
  304. char *in_mbox, size_t in_mbox_size,
  305. char *out_mbox, size_t out_mbox_size,
  306. u8 *p_status);
  307. u8 features;
  308. };
  309. struct mlxsw_bus_info {
  310. const char *device_kind;
  311. const char *device_name;
  312. struct device *dev;
  313. struct {
  314. u16 major;
  315. u16 minor;
  316. u16 subminor;
  317. } fw_rev;
  318. u8 vsd[MLXSW_CMD_BOARDINFO_VSD_LEN];
  319. u8 psid[MLXSW_CMD_BOARDINFO_PSID_LEN];
  320. };
  321. struct mlxsw_hwmon;
  322. #ifdef CONFIG_MLXSW_CORE_HWMON
  323. int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
  324. const struct mlxsw_bus_info *mlxsw_bus_info,
  325. struct mlxsw_hwmon **p_hwmon);
  326. void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon);
  327. #else
  328. static inline int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
  329. const struct mlxsw_bus_info *mlxsw_bus_info,
  330. struct mlxsw_hwmon **p_hwmon)
  331. {
  332. return 0;
  333. }
  334. #endif
  335. struct mlxsw_thermal;
  336. #ifdef CONFIG_MLXSW_CORE_THERMAL
  337. int mlxsw_thermal_init(struct mlxsw_core *mlxsw_core,
  338. const struct mlxsw_bus_info *mlxsw_bus_info,
  339. struct mlxsw_thermal **p_thermal);
  340. void mlxsw_thermal_fini(struct mlxsw_thermal *thermal);
  341. #else
  342. static inline int mlxsw_thermal_init(struct mlxsw_core *mlxsw_core,
  343. const struct mlxsw_bus_info *mlxsw_bus_info,
  344. struct mlxsw_thermal **p_thermal)
  345. {
  346. return 0;
  347. }
  348. static inline void mlxsw_thermal_fini(struct mlxsw_thermal *thermal)
  349. {
  350. }
  351. #endif
  352. #endif