core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. #define MLXSW_MODULE_ALIAS_PREFIX "mlxsw-driver-"
  50. #define MODULE_MLXSW_DRIVER_ALIAS(kind) \
  51. MODULE_ALIAS(MLXSW_MODULE_ALIAS_PREFIX kind)
  52. #define MLXSW_DEVICE_KIND_SWITCHX2 "switchx2"
  53. #define MLXSW_DEVICE_KIND_SPECTRUM "spectrum"
  54. struct mlxsw_core;
  55. struct mlxsw_driver;
  56. struct mlxsw_bus;
  57. struct mlxsw_bus_info;
  58. void *mlxsw_core_driver_priv(struct mlxsw_core *mlxsw_core);
  59. int mlxsw_core_driver_register(struct mlxsw_driver *mlxsw_driver);
  60. void mlxsw_core_driver_unregister(struct mlxsw_driver *mlxsw_driver);
  61. int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
  62. const struct mlxsw_bus *mlxsw_bus,
  63. void *bus_priv);
  64. void mlxsw_core_bus_device_unregister(struct mlxsw_core *mlxsw_core);
  65. struct mlxsw_tx_info {
  66. u8 local_port;
  67. bool is_emad;
  68. };
  69. bool mlxsw_core_skb_transmit_busy(struct mlxsw_core *mlxsw_core,
  70. const struct mlxsw_tx_info *tx_info);
  71. int mlxsw_core_skb_transmit(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
  72. const struct mlxsw_tx_info *tx_info);
  73. struct mlxsw_rx_listener {
  74. void (*func)(struct sk_buff *skb, u8 local_port, void *priv);
  75. u8 local_port;
  76. u16 trap_id;
  77. enum mlxsw_reg_hpkt_action action;
  78. };
  79. struct mlxsw_event_listener {
  80. void (*func)(const struct mlxsw_reg_info *reg,
  81. char *payload, void *priv);
  82. enum mlxsw_event_trap_id trap_id;
  83. };
  84. int mlxsw_core_rx_listener_register(struct mlxsw_core *mlxsw_core,
  85. const struct mlxsw_rx_listener *rxl,
  86. void *priv);
  87. void mlxsw_core_rx_listener_unregister(struct mlxsw_core *mlxsw_core,
  88. const struct mlxsw_rx_listener *rxl,
  89. void *priv);
  90. int mlxsw_core_event_listener_register(struct mlxsw_core *mlxsw_core,
  91. const struct mlxsw_event_listener *el,
  92. void *priv);
  93. void mlxsw_core_event_listener_unregister(struct mlxsw_core *mlxsw_core,
  94. const struct mlxsw_event_listener *el,
  95. void *priv);
  96. typedef void mlxsw_reg_trans_cb_t(struct mlxsw_core *mlxsw_core, char *payload,
  97. size_t payload_len, unsigned long cb_priv);
  98. int mlxsw_reg_trans_query(struct mlxsw_core *mlxsw_core,
  99. const struct mlxsw_reg_info *reg, char *payload,
  100. struct list_head *bulk_list,
  101. mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv);
  102. int mlxsw_reg_trans_write(struct mlxsw_core *mlxsw_core,
  103. const struct mlxsw_reg_info *reg, char *payload,
  104. struct list_head *bulk_list,
  105. mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv);
  106. int mlxsw_reg_trans_bulk_wait(struct list_head *bulk_list);
  107. int mlxsw_reg_query(struct mlxsw_core *mlxsw_core,
  108. const struct mlxsw_reg_info *reg, char *payload);
  109. int mlxsw_reg_write(struct mlxsw_core *mlxsw_core,
  110. const struct mlxsw_reg_info *reg, char *payload);
  111. struct mlxsw_rx_info {
  112. bool is_lag;
  113. union {
  114. u16 sys_port;
  115. u16 lag_id;
  116. } u;
  117. u8 lag_port_index;
  118. int trap_id;
  119. };
  120. void mlxsw_core_skb_receive(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
  121. struct mlxsw_rx_info *rx_info);
  122. void mlxsw_core_lag_mapping_set(struct mlxsw_core *mlxsw_core,
  123. u16 lag_id, u8 port_index, u8 local_port);
  124. u8 mlxsw_core_lag_mapping_get(struct mlxsw_core *mlxsw_core,
  125. u16 lag_id, u8 port_index);
  126. void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
  127. u16 lag_id, u8 local_port);
  128. struct mlxsw_core_port {
  129. struct devlink_port devlink_port;
  130. };
  131. static inline void *
  132. mlxsw_core_port_driver_priv(struct mlxsw_core_port *mlxsw_core_port)
  133. {
  134. /* mlxsw_core_port is ensured to always be the first field in driver
  135. * port structure.
  136. */
  137. return mlxsw_core_port;
  138. }
  139. int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core,
  140. struct mlxsw_core_port *mlxsw_core_port, u8 local_port,
  141. struct net_device *dev, bool split, u32 split_group);
  142. void mlxsw_core_port_fini(struct mlxsw_core_port *mlxsw_core_port);
  143. int mlxsw_core_schedule_dw(struct delayed_work *dwork, unsigned long delay);
  144. #define MLXSW_CONFIG_PROFILE_SWID_COUNT 8
  145. struct mlxsw_swid_config {
  146. u8 used_type:1,
  147. used_properties:1;
  148. u8 type;
  149. u8 properties;
  150. };
  151. struct mlxsw_config_profile {
  152. u16 used_max_vepa_channels:1,
  153. used_max_mid:1,
  154. used_max_pgt:1,
  155. used_max_system_port:1,
  156. used_max_vlan_groups:1,
  157. used_max_regions:1,
  158. used_flood_tables:1,
  159. used_flood_mode:1,
  160. used_max_ib_mc:1,
  161. used_max_pkey:1,
  162. used_ar_sec:1,
  163. used_adaptive_routing_group_cap:1,
  164. used_kvd_split_data:1; /* indicate for the kvd's values */
  165. u8 max_vepa_channels;
  166. u16 max_mid;
  167. u16 max_pgt;
  168. u16 max_system_port;
  169. u16 max_vlan_groups;
  170. u16 max_regions;
  171. u8 max_flood_tables;
  172. u8 max_vid_flood_tables;
  173. u8 flood_mode;
  174. u8 max_fid_offset_flood_tables;
  175. u16 fid_offset_flood_table_size;
  176. u8 max_fid_flood_tables;
  177. u16 fid_flood_table_size;
  178. u16 max_ib_mc;
  179. u16 max_pkey;
  180. u8 ar_sec;
  181. u16 adaptive_routing_group_cap;
  182. u8 arn;
  183. u32 kvd_linear_size;
  184. u16 kvd_hash_granularity;
  185. u8 kvd_hash_single_parts;
  186. u8 kvd_hash_double_parts;
  187. u8 resource_query_enable;
  188. struct mlxsw_swid_config swid_config[MLXSW_CONFIG_PROFILE_SWID_COUNT];
  189. };
  190. struct mlxsw_driver {
  191. struct list_head list;
  192. const char *kind;
  193. struct module *owner;
  194. size_t priv_size;
  195. int (*init)(struct mlxsw_core *mlxsw_core,
  196. const struct mlxsw_bus_info *mlxsw_bus_info);
  197. void (*fini)(struct mlxsw_core *mlxsw_core);
  198. int (*port_split)(struct mlxsw_core *mlxsw_core, u8 local_port,
  199. unsigned int count);
  200. int (*port_unsplit)(struct mlxsw_core *mlxsw_core, u8 local_port);
  201. int (*sb_pool_get)(struct mlxsw_core *mlxsw_core,
  202. unsigned int sb_index, u16 pool_index,
  203. struct devlink_sb_pool_info *pool_info);
  204. int (*sb_pool_set)(struct mlxsw_core *mlxsw_core,
  205. unsigned int sb_index, u16 pool_index, u32 size,
  206. enum devlink_sb_threshold_type threshold_type);
  207. int (*sb_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
  208. unsigned int sb_index, u16 pool_index,
  209. u32 *p_threshold);
  210. int (*sb_port_pool_set)(struct mlxsw_core_port *mlxsw_core_port,
  211. unsigned int sb_index, u16 pool_index,
  212. u32 threshold);
  213. int (*sb_tc_pool_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
  214. unsigned int sb_index, u16 tc_index,
  215. enum devlink_sb_pool_type pool_type,
  216. u16 *p_pool_index, u32 *p_threshold);
  217. int (*sb_tc_pool_bind_set)(struct mlxsw_core_port *mlxsw_core_port,
  218. unsigned int sb_index, u16 tc_index,
  219. enum devlink_sb_pool_type pool_type,
  220. u16 pool_index, u32 threshold);
  221. int (*sb_occ_snapshot)(struct mlxsw_core *mlxsw_core,
  222. unsigned int sb_index);
  223. int (*sb_occ_max_clear)(struct mlxsw_core *mlxsw_core,
  224. unsigned int sb_index);
  225. int (*sb_occ_port_pool_get)(struct mlxsw_core_port *mlxsw_core_port,
  226. unsigned int sb_index, u16 pool_index,
  227. u32 *p_cur, u32 *p_max);
  228. int (*sb_occ_tc_port_bind_get)(struct mlxsw_core_port *mlxsw_core_port,
  229. unsigned int sb_index, u16 tc_index,
  230. enum devlink_sb_pool_type pool_type,
  231. u32 *p_cur, u32 *p_max);
  232. void (*txhdr_construct)(struct sk_buff *skb,
  233. const struct mlxsw_tx_info *tx_info);
  234. u8 txhdr_len;
  235. const struct mlxsw_config_profile *profile;
  236. };
  237. struct mlxsw_resources {
  238. u32 max_span_valid:1,
  239. max_lag_valid:1,
  240. max_ports_in_lag_valid:1,
  241. kvd_size_valid:1,
  242. kvd_single_min_size_valid:1,
  243. kvd_double_min_size_valid:1,
  244. max_virtual_routers_valid:1,
  245. max_system_ports_valid:1,
  246. max_vlan_groups_valid:1,
  247. max_regions_valid:1,
  248. max_rif_valid:1;
  249. u8 max_span;
  250. u8 max_lag;
  251. u8 max_ports_in_lag;
  252. u32 kvd_size;
  253. u32 kvd_single_min_size;
  254. u32 kvd_double_min_size;
  255. u16 max_virtual_routers;
  256. u16 max_system_ports;
  257. u16 max_vlan_groups;
  258. u16 max_regions;
  259. u16 max_rif;
  260. /* Internal resources.
  261. * Determined by the SW, not queried from the HW.
  262. */
  263. u32 kvd_single_size;
  264. u32 kvd_double_size;
  265. u32 kvd_linear_size;
  266. };
  267. struct mlxsw_resources *mlxsw_core_resources_get(struct mlxsw_core *mlxsw_core);
  268. struct mlxsw_bus {
  269. const char *kind;
  270. int (*init)(void *bus_priv, struct mlxsw_core *mlxsw_core,
  271. const struct mlxsw_config_profile *profile,
  272. struct mlxsw_resources *resources);
  273. void (*fini)(void *bus_priv);
  274. bool (*skb_transmit_busy)(void *bus_priv,
  275. const struct mlxsw_tx_info *tx_info);
  276. int (*skb_transmit)(void *bus_priv, struct sk_buff *skb,
  277. const struct mlxsw_tx_info *tx_info);
  278. int (*cmd_exec)(void *bus_priv, u16 opcode, u8 opcode_mod,
  279. u32 in_mod, bool out_mbox_direct,
  280. char *in_mbox, size_t in_mbox_size,
  281. char *out_mbox, size_t out_mbox_size,
  282. u8 *p_status);
  283. };
  284. struct mlxsw_bus_info {
  285. const char *device_kind;
  286. const char *device_name;
  287. struct device *dev;
  288. struct {
  289. u16 major;
  290. u16 minor;
  291. u16 subminor;
  292. } fw_rev;
  293. u8 vsd[MLXSW_CMD_BOARDINFO_VSD_LEN];
  294. u8 psid[MLXSW_CMD_BOARDINFO_PSID_LEN];
  295. };
  296. struct mlxsw_hwmon;
  297. #ifdef CONFIG_MLXSW_CORE_HWMON
  298. int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
  299. const struct mlxsw_bus_info *mlxsw_bus_info,
  300. struct mlxsw_hwmon **p_hwmon);
  301. void mlxsw_hwmon_fini(struct mlxsw_hwmon *mlxsw_hwmon);
  302. #else
  303. static inline int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
  304. const struct mlxsw_bus_info *mlxsw_bus_info,
  305. struct mlxsw_hwmon **p_hwmon)
  306. {
  307. return 0;
  308. }
  309. #endif
  310. #endif