hnae3.h 16 KB

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