hnae3.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright (c) 2016-2017 Hisilicon Limited.
  3. #ifndef __HNAE3_H
  4. #define __HNAE3_H
  5. /* Names used in this framework:
  6. * ae handle (handle):
  7. * a set of queues provided by AE
  8. * ring buffer queue (rbq):
  9. * the channel between upper layer and the AE, can do tx and rx
  10. * ring:
  11. * a tx or rx channel within a rbq
  12. * ring description (desc):
  13. * an element in the ring with packet information
  14. * buffer:
  15. * a memory region referred by desc with the full packet payload
  16. *
  17. * "num" means a static number set as a parameter, "count" mean a dynamic
  18. * number set while running
  19. * "cb" means control block
  20. */
  21. #include <linux/acpi.h>
  22. #include <linux/dcbnl.h>
  23. #include <linux/delay.h>
  24. #include <linux/device.h>
  25. #include <linux/module.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/pci.h>
  28. #include <linux/types.h>
  29. #define HNAE3_MOD_VERSION "1.0"
  30. /* Device IDs */
  31. #define HNAE3_DEV_ID_GE 0xA220
  32. #define HNAE3_DEV_ID_25GE 0xA221
  33. #define HNAE3_DEV_ID_25GE_RDMA 0xA222
  34. #define HNAE3_DEV_ID_25GE_RDMA_MACSEC 0xA223
  35. #define HNAE3_DEV_ID_50GE_RDMA 0xA224
  36. #define HNAE3_DEV_ID_50GE_RDMA_MACSEC 0xA225
  37. #define HNAE3_DEV_ID_100G_RDMA_MACSEC 0xA226
  38. #define HNAE3_DEV_ID_100G_VF 0xA22E
  39. #define HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF 0xA22F
  40. #define HNAE3_CLASS_NAME_SIZE 16
  41. #define HNAE3_DEV_INITED_B 0x0
  42. #define HNAE3_DEV_SUPPORT_ROCE_B 0x1
  43. #define HNAE3_DEV_SUPPORT_DCB_B 0x2
  44. #define HNAE3_KNIC_CLIENT_INITED_B 0x3
  45. #define HNAE3_UNIC_CLIENT_INITED_B 0x4
  46. #define HNAE3_ROCE_CLIENT_INITED_B 0x5
  47. #define HNAE3_DEV_SUPPORT_ROCE_DCB_BITS (BIT(HNAE3_DEV_SUPPORT_DCB_B) |\
  48. BIT(HNAE3_DEV_SUPPORT_ROCE_B))
  49. #define hnae3_dev_roce_supported(hdev) \
  50. hnae3_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_ROCE_B)
  51. #define hnae3_dev_dcb_supported(hdev) \
  52. hnae3_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_DCB_B)
  53. #define ring_ptr_move_fw(ring, p) \
  54. ((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
  55. #define ring_ptr_move_bw(ring, p) \
  56. ((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
  57. enum hns_desc_type {
  58. DESC_TYPE_SKB,
  59. DESC_TYPE_PAGE,
  60. };
  61. struct hnae3_handle;
  62. struct hnae3_queue {
  63. void __iomem *io_base;
  64. struct hnae3_ae_algo *ae_algo;
  65. struct hnae3_handle *handle;
  66. int tqp_index; /* index in a handle */
  67. u32 buf_size; /* size for hnae_desc->addr, preset by AE */
  68. u16 desc_num; /* total number of desc */
  69. };
  70. /*hnae3 loop mode*/
  71. enum hnae3_loop {
  72. HNAE3_MAC_INTER_LOOP_MAC,
  73. HNAE3_MAC_INTER_LOOP_SERDES,
  74. HNAE3_MAC_INTER_LOOP_PHY,
  75. HNAE3_MAC_LOOP_NONE,
  76. };
  77. enum hnae3_client_type {
  78. HNAE3_CLIENT_KNIC,
  79. HNAE3_CLIENT_UNIC,
  80. HNAE3_CLIENT_ROCE,
  81. };
  82. enum hnae3_dev_type {
  83. HNAE3_DEV_KNIC,
  84. HNAE3_DEV_UNIC,
  85. };
  86. /* mac media type */
  87. enum hnae3_media_type {
  88. HNAE3_MEDIA_TYPE_UNKNOWN,
  89. HNAE3_MEDIA_TYPE_FIBER,
  90. HNAE3_MEDIA_TYPE_COPPER,
  91. HNAE3_MEDIA_TYPE_BACKPLANE,
  92. };
  93. enum hnae3_reset_notify_type {
  94. HNAE3_UP_CLIENT,
  95. HNAE3_DOWN_CLIENT,
  96. HNAE3_INIT_CLIENT,
  97. HNAE3_UNINIT_CLIENT,
  98. };
  99. enum hnae3_reset_type {
  100. HNAE3_VF_RESET,
  101. HNAE3_VF_FULL_RESET,
  102. HNAE3_FUNC_RESET,
  103. HNAE3_CORE_RESET,
  104. HNAE3_GLOBAL_RESET,
  105. HNAE3_IMP_RESET,
  106. HNAE3_NONE_RESET,
  107. };
  108. struct hnae3_vector_info {
  109. u8 __iomem *io_addr;
  110. int vector;
  111. };
  112. #define HNAE3_RING_TYPE_B 0
  113. #define HNAE3_RING_TYPE_TX 0
  114. #define HNAE3_RING_TYPE_RX 1
  115. #define HNAE3_RING_GL_IDX_S 0
  116. #define HNAE3_RING_GL_IDX_M GENMASK(1, 0)
  117. #define HNAE3_RING_GL_RX 0
  118. #define HNAE3_RING_GL_TX 1
  119. struct hnae3_ring_chain_node {
  120. struct hnae3_ring_chain_node *next;
  121. u32 tqp_index;
  122. u32 flag;
  123. u32 int_gl_idx;
  124. };
  125. #define HNAE3_IS_TX_RING(node) \
  126. (((node)->flag & (1 << HNAE3_RING_TYPE_B)) == HNAE3_RING_TYPE_TX)
  127. struct hnae3_client_ops {
  128. int (*init_instance)(struct hnae3_handle *handle);
  129. void (*uninit_instance)(struct hnae3_handle *handle, bool reset);
  130. void (*link_status_change)(struct hnae3_handle *handle, bool state);
  131. int (*setup_tc)(struct hnae3_handle *handle, u8 tc);
  132. int (*reset_notify)(struct hnae3_handle *handle,
  133. enum hnae3_reset_notify_type type);
  134. };
  135. #define HNAE3_CLIENT_NAME_LENGTH 16
  136. struct hnae3_client {
  137. char name[HNAE3_CLIENT_NAME_LENGTH];
  138. unsigned long state;
  139. enum hnae3_client_type type;
  140. const struct hnae3_client_ops *ops;
  141. struct list_head node;
  142. };
  143. struct hnae3_ae_dev {
  144. struct pci_dev *pdev;
  145. const struct hnae3_ae_ops *ops;
  146. struct list_head node;
  147. u32 flag;
  148. enum hnae3_dev_type dev_type;
  149. void *priv;
  150. };
  151. /* This struct defines the operation on the handle.
  152. *
  153. * init_ae_dev(): (mandatory)
  154. * Get PF configure from pci_dev and initialize PF hardware
  155. * uninit_ae_dev()
  156. * Disable PF device and release PF resource
  157. * register_client
  158. * Register client to ae_dev
  159. * unregister_client()
  160. * Unregister client from ae_dev
  161. * start()
  162. * Enable the hardware
  163. * stop()
  164. * Disable the hardware
  165. * get_status()
  166. * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
  167. * non-ok
  168. * get_ksettings_an_result()
  169. * Get negotiation status,speed and duplex
  170. * update_speed_duplex_h()
  171. * Update hardware speed and duplex
  172. * get_media_type()
  173. * Get media type of MAC
  174. * adjust_link()
  175. * Adjust link status
  176. * set_loopback()
  177. * Set loopback
  178. * set_promisc_mode
  179. * Set promisc mode
  180. * set_mtu()
  181. * set mtu
  182. * get_pauseparam()
  183. * get tx and rx of pause frame use
  184. * set_pauseparam()
  185. * set tx and rx of pause frame use
  186. * set_autoneg()
  187. * set auto autonegotiation of pause frame use
  188. * get_autoneg()
  189. * get auto autonegotiation of pause frame use
  190. * get_coalesce_usecs()
  191. * get usecs to delay a TX interrupt after a packet is sent
  192. * get_rx_max_coalesced_frames()
  193. * get Maximum number of packets to be sent before a TX interrupt.
  194. * set_coalesce_usecs()
  195. * set usecs to delay a TX interrupt after a packet is sent
  196. * set_coalesce_frames()
  197. * set Maximum number of packets to be sent before a TX interrupt.
  198. * get_mac_addr()
  199. * get mac address
  200. * set_mac_addr()
  201. * set mac address
  202. * add_uc_addr
  203. * Add unicast addr to mac table
  204. * rm_uc_addr
  205. * Remove unicast addr from mac table
  206. * set_mc_addr()
  207. * Set multicast address
  208. * add_mc_addr
  209. * Add multicast address to mac table
  210. * rm_mc_addr
  211. * Remove multicast address from mac table
  212. * update_stats()
  213. * Update Old network device statistics
  214. * get_ethtool_stats()
  215. * Get ethtool network device statistics
  216. * get_strings()
  217. * Get a set of strings that describe the requested objects
  218. * get_sset_count()
  219. * Get number of strings that @get_strings will write
  220. * update_led_status()
  221. * Update the led status
  222. * set_led_id()
  223. * Set led id
  224. * get_regs()
  225. * Get regs dump
  226. * get_regs_len()
  227. * Get the len of the regs dump
  228. * get_rss_key_size()
  229. * Get rss key size
  230. * get_rss_indir_size()
  231. * Get rss indirection table size
  232. * get_rss()
  233. * Get rss table
  234. * set_rss()
  235. * Set rss table
  236. * get_tc_size()
  237. * Get tc size of handle
  238. * get_vector()
  239. * Get vector number and vector information
  240. * put_vector()
  241. * Put the vector in hdev
  242. * map_ring_to_vector()
  243. * Map rings to vector
  244. * unmap_ring_from_vector()
  245. * Unmap rings from vector
  246. * reset_queue()
  247. * Reset queue
  248. * get_fw_version()
  249. * Get firmware version
  250. * get_mdix_mode()
  251. * Get media typr of phy
  252. * enable_vlan_filter()
  253. * Enable vlan filter
  254. * set_vlan_filter()
  255. * Set vlan filter config of Ports
  256. * set_vf_vlan_filter()
  257. * Set vlan filter config of vf
  258. * enable_hw_strip_rxvtag()
  259. * Enable/disable hardware strip vlan tag of packets received
  260. */
  261. struct hnae3_ae_ops {
  262. int (*init_ae_dev)(struct hnae3_ae_dev *ae_dev);
  263. void (*uninit_ae_dev)(struct hnae3_ae_dev *ae_dev);
  264. int (*init_client_instance)(struct hnae3_client *client,
  265. struct hnae3_ae_dev *ae_dev);
  266. void (*uninit_client_instance)(struct hnae3_client *client,
  267. struct hnae3_ae_dev *ae_dev);
  268. int (*start)(struct hnae3_handle *handle);
  269. void (*stop)(struct hnae3_handle *handle);
  270. int (*get_status)(struct hnae3_handle *handle);
  271. void (*get_ksettings_an_result)(struct hnae3_handle *handle,
  272. u8 *auto_neg, u32 *speed, u8 *duplex);
  273. int (*update_speed_duplex_h)(struct hnae3_handle *handle);
  274. int (*cfg_mac_speed_dup_h)(struct hnae3_handle *handle, int speed,
  275. u8 duplex);
  276. void (*get_media_type)(struct hnae3_handle *handle, u8 *media_type);
  277. void (*adjust_link)(struct hnae3_handle *handle, int speed, int duplex);
  278. int (*set_loopback)(struct hnae3_handle *handle,
  279. enum hnae3_loop loop_mode, bool en);
  280. void (*set_promisc_mode)(struct hnae3_handle *handle, bool en_uc_pmc,
  281. bool en_mc_pmc);
  282. int (*set_mtu)(struct hnae3_handle *handle, int new_mtu);
  283. void (*get_pauseparam)(struct hnae3_handle *handle,
  284. u32 *auto_neg, u32 *rx_en, u32 *tx_en);
  285. int (*set_pauseparam)(struct hnae3_handle *handle,
  286. u32 auto_neg, u32 rx_en, u32 tx_en);
  287. int (*set_autoneg)(struct hnae3_handle *handle, bool enable);
  288. int (*get_autoneg)(struct hnae3_handle *handle);
  289. void (*get_coalesce_usecs)(struct hnae3_handle *handle,
  290. u32 *tx_usecs, u32 *rx_usecs);
  291. void (*get_rx_max_coalesced_frames)(struct hnae3_handle *handle,
  292. u32 *tx_frames, u32 *rx_frames);
  293. int (*set_coalesce_usecs)(struct hnae3_handle *handle, u32 timeout);
  294. int (*set_coalesce_frames)(struct hnae3_handle *handle,
  295. u32 coalesce_frames);
  296. void (*get_coalesce_range)(struct hnae3_handle *handle,
  297. u32 *tx_frames_low, u32 *rx_frames_low,
  298. u32 *tx_frames_high, u32 *rx_frames_high,
  299. u32 *tx_usecs_low, u32 *rx_usecs_low,
  300. u32 *tx_usecs_high, u32 *rx_usecs_high);
  301. void (*get_mac_addr)(struct hnae3_handle *handle, u8 *p);
  302. int (*set_mac_addr)(struct hnae3_handle *handle, void *p,
  303. bool is_first);
  304. int (*add_uc_addr)(struct hnae3_handle *handle,
  305. const unsigned char *addr);
  306. int (*rm_uc_addr)(struct hnae3_handle *handle,
  307. const unsigned char *addr);
  308. int (*set_mc_addr)(struct hnae3_handle *handle, void *addr);
  309. int (*add_mc_addr)(struct hnae3_handle *handle,
  310. const unsigned char *addr);
  311. int (*rm_mc_addr)(struct hnae3_handle *handle,
  312. const unsigned char *addr);
  313. int (*update_mta_status)(struct hnae3_handle *handle);
  314. void (*set_tso_stats)(struct hnae3_handle *handle, int enable);
  315. void (*update_stats)(struct hnae3_handle *handle,
  316. struct net_device_stats *net_stats);
  317. void (*get_stats)(struct hnae3_handle *handle, u64 *data);
  318. void (*get_strings)(struct hnae3_handle *handle,
  319. u32 stringset, u8 *data);
  320. int (*get_sset_count)(struct hnae3_handle *handle, int stringset);
  321. void (*get_regs)(struct hnae3_handle *handle, u32 *version,
  322. void *data);
  323. int (*get_regs_len)(struct hnae3_handle *handle);
  324. u32 (*get_rss_key_size)(struct hnae3_handle *handle);
  325. u32 (*get_rss_indir_size)(struct hnae3_handle *handle);
  326. int (*get_rss)(struct hnae3_handle *handle, u32 *indir, u8 *key,
  327. u8 *hfunc);
  328. int (*set_rss)(struct hnae3_handle *handle, const u32 *indir,
  329. const u8 *key, const u8 hfunc);
  330. int (*set_rss_tuple)(struct hnae3_handle *handle,
  331. struct ethtool_rxnfc *cmd);
  332. int (*get_rss_tuple)(struct hnae3_handle *handle,
  333. struct ethtool_rxnfc *cmd);
  334. int (*get_tc_size)(struct hnae3_handle *handle);
  335. int (*get_vector)(struct hnae3_handle *handle, u16 vector_num,
  336. struct hnae3_vector_info *vector_info);
  337. int (*put_vector)(struct hnae3_handle *handle, int vector_num);
  338. int (*map_ring_to_vector)(struct hnae3_handle *handle,
  339. int vector_num,
  340. struct hnae3_ring_chain_node *vr_chain);
  341. int (*unmap_ring_from_vector)(struct hnae3_handle *handle,
  342. int vector_num,
  343. struct hnae3_ring_chain_node *vr_chain);
  344. void (*reset_queue)(struct hnae3_handle *handle, u16 queue_id);
  345. u32 (*get_fw_version)(struct hnae3_handle *handle);
  346. void (*get_mdix_mode)(struct hnae3_handle *handle,
  347. u8 *tp_mdix_ctrl, u8 *tp_mdix);
  348. void (*enable_vlan_filter)(struct hnae3_handle *handle, bool enable);
  349. int (*set_vlan_filter)(struct hnae3_handle *handle, __be16 proto,
  350. u16 vlan_id, bool is_kill);
  351. int (*set_vf_vlan_filter)(struct hnae3_handle *handle, int vfid,
  352. u16 vlan, u8 qos, __be16 proto);
  353. int (*enable_hw_strip_rxvtag)(struct hnae3_handle *handle, bool enable);
  354. void (*reset_event)(struct hnae3_handle *handle);
  355. void (*get_channels)(struct hnae3_handle *handle,
  356. struct ethtool_channels *ch);
  357. void (*get_tqps_and_rss_info)(struct hnae3_handle *h,
  358. u16 *free_tqps, u16 *max_rss_size);
  359. int (*set_channels)(struct hnae3_handle *handle, u32 new_tqps_num);
  360. void (*get_flowctrl_adv)(struct hnae3_handle *handle,
  361. u32 *flowctrl_adv);
  362. int (*set_led_id)(struct hnae3_handle *handle,
  363. enum ethtool_phys_id_state status);
  364. void (*get_link_mode)(struct hnae3_handle *handle,
  365. unsigned long *supported,
  366. unsigned long *advertising);
  367. void (*get_port_type)(struct hnae3_handle *handle, u8 *port_type);
  368. };
  369. struct hnae3_dcb_ops {
  370. /* IEEE 802.1Qaz std */
  371. int (*ieee_getets)(struct hnae3_handle *, struct ieee_ets *);
  372. int (*ieee_setets)(struct hnae3_handle *, struct ieee_ets *);
  373. int (*ieee_getpfc)(struct hnae3_handle *, struct ieee_pfc *);
  374. int (*ieee_setpfc)(struct hnae3_handle *, struct ieee_pfc *);
  375. /* DCBX configuration */
  376. u8 (*getdcbx)(struct hnae3_handle *);
  377. u8 (*setdcbx)(struct hnae3_handle *, u8);
  378. int (*map_update)(struct hnae3_handle *);
  379. int (*setup_tc)(struct hnae3_handle *, u8, u8 *);
  380. };
  381. struct hnae3_ae_algo {
  382. const struct hnae3_ae_ops *ops;
  383. struct list_head node;
  384. const struct pci_device_id *pdev_id_table;
  385. };
  386. #define HNAE3_INT_NAME_LEN (IFNAMSIZ + 16)
  387. #define HNAE3_ITR_COUNTDOWN_START 100
  388. struct hnae3_tc_info {
  389. u16 tqp_offset; /* TQP offset from base TQP */
  390. u16 tqp_count; /* Total TQPs */
  391. u8 tc; /* TC index */
  392. bool enable; /* If this TC is enable or not */
  393. };
  394. #define HNAE3_MAX_TC 8
  395. #define HNAE3_MAX_USER_PRIO 8
  396. struct hnae3_knic_private_info {
  397. struct net_device *netdev; /* Set by KNIC client when init instance */
  398. u16 rss_size; /* Allocated RSS queues */
  399. u16 rx_buf_len;
  400. u16 num_desc;
  401. u8 num_tc; /* Total number of enabled TCs */
  402. u8 prio_tc[HNAE3_MAX_USER_PRIO]; /* TC indexed by prio */
  403. struct hnae3_tc_info tc_info[HNAE3_MAX_TC]; /* Idx of array is HW TC */
  404. u16 num_tqps; /* total number of TQPs in this handle */
  405. struct hnae3_queue **tqp; /* array base of all TQPs in this instance */
  406. const struct hnae3_dcb_ops *dcb_ops;
  407. u16 int_rl_setting;
  408. };
  409. struct hnae3_roce_private_info {
  410. struct net_device *netdev;
  411. void __iomem *roce_io_base;
  412. int base_vector;
  413. int num_vectors;
  414. };
  415. struct hnae3_unic_private_info {
  416. struct net_device *netdev;
  417. u16 rx_buf_len;
  418. u16 num_desc;
  419. u16 num_tqps; /* total number of tqps in this handle */
  420. struct hnae3_queue **tqp; /* array base of all TQPs of this instance */
  421. };
  422. #define HNAE3_SUPPORT_MAC_LOOPBACK BIT(0)
  423. #define HNAE3_SUPPORT_PHY_LOOPBACK BIT(1)
  424. #define HNAE3_SUPPORT_SERDES_LOOPBACK BIT(2)
  425. #define HNAE3_SUPPORT_VF BIT(3)
  426. struct hnae3_handle {
  427. struct hnae3_client *client;
  428. struct pci_dev *pdev;
  429. void *priv;
  430. struct hnae3_ae_algo *ae_algo; /* the class who provides this handle */
  431. u64 flags; /* Indicate the capabilities for this handle*/
  432. unsigned long last_reset_time;
  433. enum hnae3_reset_type reset_level;
  434. union {
  435. struct net_device *netdev; /* first member */
  436. struct hnae3_knic_private_info kinfo;
  437. struct hnae3_unic_private_info uinfo;
  438. struct hnae3_roce_private_info rinfo;
  439. };
  440. u32 numa_node_mask; /* for multi-chip support */
  441. };
  442. #define hnae3_set_field(origin, mask, shift, val) \
  443. do { \
  444. (origin) &= (~(mask)); \
  445. (origin) |= ((val) << (shift)) & (mask); \
  446. } while (0)
  447. #define hnae3_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
  448. #define hnae3_set_bit(origin, shift, val) \
  449. hnae3_set_field((origin), (0x1 << (shift)), (shift), (val))
  450. #define hnae3_get_bit(origin, shift) \
  451. hnae3_get_field((origin), (0x1 << (shift)), (shift))
  452. int hnae3_register_ae_dev(struct hnae3_ae_dev *ae_dev);
  453. void hnae3_unregister_ae_dev(struct hnae3_ae_dev *ae_dev);
  454. void hnae3_unregister_ae_algo(struct hnae3_ae_algo *ae_algo);
  455. void hnae3_register_ae_algo(struct hnae3_ae_algo *ae_algo);
  456. void hnae3_unregister_client(struct hnae3_client *client);
  457. int hnae3_register_client(struct hnae3_client *client);
  458. void hnae3_set_client_init_flag(struct hnae3_client *client,
  459. struct hnae3_ae_dev *ae_dev, int inited);
  460. #endif