hnae.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) 2014-2015 Hisilicon Limited.
  4. */
  5. #ifndef __HNAE_H
  6. #define __HNAE_H
  7. /* Names used in this framework:
  8. * ae handle (handle):
  9. * a set of queues provided by AE
  10. * ring buffer queue (rbq):
  11. * the channel between upper layer and the AE, can do tx and rx
  12. * ring:
  13. * a tx or rx channel within a rbq
  14. * ring description (desc):
  15. * an element in the ring with packet information
  16. * buffer:
  17. * a memory region referred by desc with the full packet payload
  18. *
  19. * "num" means a static number set as a parameter, "count" mean a dynamic
  20. * number set while running
  21. * "cb" means control block
  22. */
  23. #include <linux/acpi.h>
  24. #include <linux/delay.h>
  25. #include <linux/device.h>
  26. #include <linux/module.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/notifier.h>
  29. #include <linux/phy.h>
  30. #include <linux/types.h>
  31. #define HNAE_DRIVER_VERSION "2.0"
  32. #define HNAE_DRIVER_NAME "hns"
  33. #define HNAE_COPYRIGHT "Copyright(c) 2015 Huawei Corporation."
  34. #define HNAE_DRIVER_STRING "Hisilicon Network Subsystem Driver"
  35. #define HNAE_DEFAULT_DEVICE_DESCR "Hisilicon Network Subsystem"
  36. #ifdef DEBUG
  37. #ifndef assert
  38. #define assert(expr) \
  39. do { \
  40. if (!(expr)) { \
  41. pr_err("Assertion failed! %s, %s, %s, line %d\n", \
  42. #expr, __FILE__, __func__, __LINE__); \
  43. } \
  44. } while (0)
  45. #endif
  46. #else
  47. #ifndef assert
  48. #define assert(expr)
  49. #endif
  50. #endif
  51. #define AE_VERSION_1 ('6' << 16 | '6' << 8 | '0')
  52. #define AE_VERSION_2 ('1' << 24 | '6' << 16 | '1' << 8 | '0')
  53. #define AE_IS_VER1(ver) ((ver) == AE_VERSION_1)
  54. #define AE_NAME_SIZE 16
  55. #define BD_SIZE_2048_MAX_MTU 6000
  56. /* some said the RX and TX RCB format should not be the same in the future. But
  57. * it is the same now...
  58. */
  59. #define RCB_REG_BASEADDR_L 0x00 /* P660 support only 32bit accessing */
  60. #define RCB_REG_BASEADDR_H 0x04
  61. #define RCB_REG_BD_NUM 0x08
  62. #define RCB_REG_BD_LEN 0x0C
  63. #define RCB_REG_PKTLINE 0x10
  64. #define RCB_REG_TAIL 0x18
  65. #define RCB_REG_HEAD 0x1C
  66. #define RCB_REG_FBDNUM 0x20
  67. #define RCB_REG_OFFSET 0x24 /* pkt num to be handled */
  68. #define RCB_REG_PKTNUM_RECORD 0x2C /* total pkt received */
  69. #define HNS_RX_HEAD_SIZE 256
  70. #define HNAE_AE_REGISTER 0x1
  71. #define RCB_RING_NAME_LEN (IFNAMSIZ + 4)
  72. #define HNAE_LOWEST_LATENCY_COAL_PARAM 30
  73. #define HNAE_LOW_LATENCY_COAL_PARAM 80
  74. #define HNAE_BULK_LATENCY_COAL_PARAM 150
  75. enum hnae_led_state {
  76. HNAE_LED_INACTIVE,
  77. HNAE_LED_ACTIVE,
  78. HNAE_LED_ON,
  79. HNAE_LED_OFF
  80. };
  81. #define HNS_RX_FLAG_VLAN_PRESENT 0x1
  82. #define HNS_RX_FLAG_L3ID_IPV4 0x0
  83. #define HNS_RX_FLAG_L3ID_IPV6 0x1
  84. #define HNS_RX_FLAG_L4ID_UDP 0x0
  85. #define HNS_RX_FLAG_L4ID_TCP 0x1
  86. #define HNS_RX_FLAG_L4ID_SCTP 0x3
  87. #define HNS_TXD_ASID_S 0
  88. #define HNS_TXD_ASID_M (0xff << HNS_TXD_ASID_S)
  89. #define HNS_TXD_BUFNUM_S 8
  90. #define HNS_TXD_BUFNUM_M (0x3 << HNS_TXD_BUFNUM_S)
  91. #define HNS_TXD_PORTID_S 10
  92. #define HNS_TXD_PORTID_M (0x7 << HNS_TXD_PORTID_S)
  93. #define HNS_TXD_RA_B 8
  94. #define HNS_TXD_RI_B 9
  95. #define HNS_TXD_L4CS_B 10
  96. #define HNS_TXD_L3CS_B 11
  97. #define HNS_TXD_FE_B 12
  98. #define HNS_TXD_VLD_B 13
  99. #define HNS_TXD_IPOFFSET_S 14
  100. #define HNS_TXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
  101. #define HNS_RXD_IPOFFSET_S 0
  102. #define HNS_RXD_IPOFFSET_M (0xff << HNS_TXD_IPOFFSET_S)
  103. #define HNS_RXD_BUFNUM_S 8
  104. #define HNS_RXD_BUFNUM_M (0x3 << HNS_RXD_BUFNUM_S)
  105. #define HNS_RXD_PORTID_S 10
  106. #define HNS_RXD_PORTID_M (0x7 << HNS_RXD_PORTID_S)
  107. #define HNS_RXD_DMAC_S 13
  108. #define HNS_RXD_DMAC_M (0x3 << HNS_RXD_DMAC_S)
  109. #define HNS_RXD_VLAN_S 15
  110. #define HNS_RXD_VLAN_M (0x3 << HNS_RXD_VLAN_S)
  111. #define HNS_RXD_L3ID_S 17
  112. #define HNS_RXD_L3ID_M (0xf << HNS_RXD_L3ID_S)
  113. #define HNS_RXD_L4ID_S 21
  114. #define HNS_RXD_L4ID_M (0xf << HNS_RXD_L4ID_S)
  115. #define HNS_RXD_FE_B 25
  116. #define HNS_RXD_FRAG_B 26
  117. #define HNS_RXD_VLD_B 27
  118. #define HNS_RXD_L2E_B 28
  119. #define HNS_RXD_L3E_B 29
  120. #define HNS_RXD_L4E_B 30
  121. #define HNS_RXD_DROP_B 31
  122. #define HNS_RXD_VLANID_S 8
  123. #define HNS_RXD_VLANID_M (0xfff << HNS_RXD_VLANID_S)
  124. #define HNS_RXD_CFI_B 20
  125. #define HNS_RXD_PRI_S 21
  126. #define HNS_RXD_PRI_M (0x7 << HNS_RXD_PRI_S)
  127. #define HNS_RXD_ASID_S 24
  128. #define HNS_RXD_ASID_M (0xff << HNS_RXD_ASID_S)
  129. #define HNSV2_TXD_BUFNUM_S 0
  130. #define HNSV2_TXD_BUFNUM_M (0x7 << HNSV2_TXD_BUFNUM_S)
  131. #define HNSV2_TXD_PORTID_S 4
  132. #define HNSV2_TXD_PORTID_M (0X7 << HNSV2_TXD_PORTID_S)
  133. #define HNSV2_TXD_RI_B 1
  134. #define HNSV2_TXD_L4CS_B 2
  135. #define HNSV2_TXD_L3CS_B 3
  136. #define HNSV2_TXD_FE_B 4
  137. #define HNSV2_TXD_VLD_B 5
  138. #define HNSV2_TXD_TSE_B 0
  139. #define HNSV2_TXD_VLAN_EN_B 1
  140. #define HNSV2_TXD_SNAP_B 2
  141. #define HNSV2_TXD_IPV6_B 3
  142. #define HNSV2_TXD_SCTP_B 4
  143. /* hardware spec ring buffer format */
  144. struct __packed hnae_desc {
  145. __le64 addr;
  146. union {
  147. struct {
  148. union {
  149. __le16 asid_bufnum_pid;
  150. __le16 asid;
  151. };
  152. __le16 send_size;
  153. union {
  154. __le32 flag_ipoffset;
  155. struct {
  156. __u8 bn_pid;
  157. __u8 ra_ri_cs_fe_vld;
  158. __u8 ip_offset;
  159. __u8 tse_vlan_snap_v6_sctp_nth;
  160. };
  161. };
  162. __le16 mss;
  163. __u8 l4_len;
  164. __u8 reserved1;
  165. __le16 paylen;
  166. __u8 vmid;
  167. __u8 qid;
  168. __le32 reserved2[2];
  169. } tx;
  170. struct {
  171. __le32 ipoff_bnum_pid_flag;
  172. __le16 pkt_len;
  173. __le16 size;
  174. union {
  175. __le32 vlan_pri_asid;
  176. struct {
  177. __le16 asid;
  178. __le16 vlan_cfi_pri;
  179. };
  180. };
  181. __le32 rss_hash;
  182. __le32 reserved_1[2];
  183. } rx;
  184. };
  185. };
  186. struct hnae_desc_cb {
  187. dma_addr_t dma; /* dma address of this desc */
  188. void *buf; /* cpu addr for a desc */
  189. /* priv data for the desc, e.g. skb when use with ip stack*/
  190. void *priv;
  191. u32 page_offset;
  192. u32 length; /* length of the buffer */
  193. u16 reuse_flag;
  194. /* desc type, used by the ring user to mark the type of the priv data */
  195. u16 type;
  196. };
  197. #define setflags(flags, bits) ((flags) |= (bits))
  198. #define unsetflags(flags, bits) ((flags) &= ~(bits))
  199. /* hnae_ring->flags fields */
  200. #define RINGF_DIR 0x1 /* TX or RX ring, set if TX */
  201. #define is_tx_ring(ring) ((ring)->flags & RINGF_DIR)
  202. #define is_rx_ring(ring) (!is_tx_ring(ring))
  203. #define ring_to_dma_dir(ring) (is_tx_ring(ring) ? \
  204. DMA_TO_DEVICE : DMA_FROM_DEVICE)
  205. struct ring_stats {
  206. u64 io_err_cnt;
  207. u64 sw_err_cnt;
  208. u64 seg_pkt_cnt;
  209. union {
  210. struct {
  211. u64 tx_pkts;
  212. u64 tx_bytes;
  213. u64 tx_err_cnt;
  214. u64 restart_queue;
  215. u64 tx_busy;
  216. };
  217. struct {
  218. u64 rx_pkts;
  219. u64 rx_bytes;
  220. u64 rx_err_cnt;
  221. u64 reuse_pg_cnt;
  222. u64 err_pkt_len;
  223. u64 non_vld_descs;
  224. u64 err_bd_num;
  225. u64 l2_err;
  226. u64 l3l4_csum_err;
  227. };
  228. };
  229. };
  230. struct hnae_queue;
  231. struct hnae_ring {
  232. u8 __iomem *io_base; /* base io address for the ring */
  233. struct hnae_desc *desc; /* dma map address space */
  234. struct hnae_desc_cb *desc_cb;
  235. struct hnae_queue *q;
  236. int irq;
  237. char ring_name[RCB_RING_NAME_LEN];
  238. /* statistic */
  239. struct ring_stats stats;
  240. dma_addr_t desc_dma_addr;
  241. u32 buf_size; /* size for hnae_desc->addr, preset by AE */
  242. u16 desc_num; /* total number of desc */
  243. u16 max_desc_num_per_pkt;
  244. u16 max_raw_data_sz_per_desc;
  245. u16 max_pkt_size;
  246. int next_to_use; /* idx of next spare desc */
  247. /* idx of lastest sent desc, the ring is empty when equal to
  248. * next_to_use
  249. */
  250. int next_to_clean;
  251. int flags; /* ring attribute */
  252. int irq_init_flag;
  253. /* total rx bytes after last rx rate calucated */
  254. u64 coal_last_rx_bytes;
  255. unsigned long coal_last_jiffies;
  256. u32 coal_param;
  257. u32 coal_rx_rate; /* rx rate in MB */
  258. };
  259. #define ring_ptr_move_fw(ring, p) \
  260. ((ring)->p = ((ring)->p + 1) % (ring)->desc_num)
  261. #define ring_ptr_move_bw(ring, p) \
  262. ((ring)->p = ((ring)->p - 1 + (ring)->desc_num) % (ring)->desc_num)
  263. enum hns_desc_type {
  264. DESC_TYPE_SKB,
  265. DESC_TYPE_PAGE,
  266. };
  267. #define assert_is_ring_idx(ring, idx) \
  268. assert((idx) >= 0 && (idx) < (ring)->desc_num)
  269. /* the distance between [begin, end) in a ring buffer
  270. * note: there is a unuse slot between the begin and the end
  271. */
  272. static inline int ring_dist(struct hnae_ring *ring, int begin, int end)
  273. {
  274. assert_is_ring_idx(ring, begin);
  275. assert_is_ring_idx(ring, end);
  276. return (end - begin + ring->desc_num) % ring->desc_num;
  277. }
  278. static inline int ring_space(struct hnae_ring *ring)
  279. {
  280. return ring->desc_num -
  281. ring_dist(ring, ring->next_to_clean, ring->next_to_use) - 1;
  282. }
  283. static inline int is_ring_empty(struct hnae_ring *ring)
  284. {
  285. assert_is_ring_idx(ring, ring->next_to_use);
  286. assert_is_ring_idx(ring, ring->next_to_clean);
  287. return ring->next_to_use == ring->next_to_clean;
  288. }
  289. #define hnae_buf_size(_ring) ((_ring)->buf_size)
  290. #define hnae_page_order(_ring) (get_order(hnae_buf_size(_ring)))
  291. #define hnae_page_size(_ring) (PAGE_SIZE << hnae_page_order(_ring))
  292. struct hnae_handle;
  293. /* allocate and dma map space for hnae desc */
  294. struct hnae_buf_ops {
  295. int (*alloc_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
  296. void (*free_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
  297. int (*map_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
  298. void (*unmap_buffer)(struct hnae_ring *ring, struct hnae_desc_cb *cb);
  299. };
  300. struct hnae_queue {
  301. u8 __iomem *io_base;
  302. phys_addr_t phy_base;
  303. struct hnae_ae_dev *dev; /* the device who use this queue */
  304. struct hnae_ring rx_ring ____cacheline_internodealigned_in_smp;
  305. struct hnae_ring tx_ring ____cacheline_internodealigned_in_smp;
  306. struct hnae_handle *handle;
  307. };
  308. /*hnae loop mode*/
  309. enum hnae_loop {
  310. MAC_INTERNALLOOP_MAC = 0,
  311. MAC_INTERNALLOOP_SERDES,
  312. MAC_INTERNALLOOP_PHY,
  313. MAC_LOOP_PHY_NONE,
  314. MAC_LOOP_NONE,
  315. };
  316. /*hnae port type*/
  317. enum hnae_port_type {
  318. HNAE_PORT_SERVICE = 0,
  319. HNAE_PORT_DEBUG
  320. };
  321. /* mac media type */
  322. enum hnae_media_type {
  323. HNAE_MEDIA_TYPE_UNKNOWN = 0,
  324. HNAE_MEDIA_TYPE_FIBER,
  325. HNAE_MEDIA_TYPE_COPPER,
  326. HNAE_MEDIA_TYPE_BACKPLANE,
  327. };
  328. /* This struct defines the operation on the handle.
  329. *
  330. * get_handle(): (mandatory)
  331. * Get a handle from AE according to its name and options.
  332. * the AE driver should manage the space used by handle and its queues while
  333. * the HNAE framework will allocate desc and desc_cb for all rings in the
  334. * queues.
  335. * put_handle():
  336. * Release the handle.
  337. * start():
  338. * Enable the hardware, include all queues
  339. * stop():
  340. * Disable the hardware
  341. * set_opts(): (mandatory)
  342. * Set options to the AE
  343. * get_opts(): (mandatory)
  344. * Get options from the AE
  345. * get_status():
  346. * Get the carrier state of the back channel of the handle, 1 for ok, 0 for
  347. * non-ok
  348. * toggle_ring_irq(): (mandatory)
  349. * Set the ring irq to be enabled(0) or disable(1)
  350. * toggle_queue_status(): (mandatory)
  351. * Set the queue to be enabled(1) or disable(0), this will not change the
  352. * ring irq state
  353. * adjust_link()
  354. * adjust link status
  355. * set_loopback()
  356. * set loopback
  357. * get_ring_bdnum_limit()
  358. * get ring bd number limit
  359. * get_pauseparam()
  360. * get tx and rx of pause frame use
  361. * set_pauseparam()
  362. * set tx and rx of pause frame use
  363. * get_coalesce_usecs()
  364. * get usecs to delay a TX interrupt after a packet is sent
  365. * get_rx_max_coalesced_frames()
  366. * get Maximum number of packets to be sent before a TX interrupt.
  367. * set_coalesce_usecs()
  368. * set usecs to delay a TX interrupt after a packet is sent
  369. * set_coalesce_frames()
  370. * set Maximum number of packets to be sent before a TX interrupt.
  371. * get_ringnum()
  372. * get RX/TX ring number
  373. * get_max_ringnum()
  374. * get RX/TX ring maximum number
  375. * get_mac_addr()
  376. * get mac address
  377. * set_mac_addr()
  378. * set mac address
  379. * clr_mc_addr()
  380. * clear mcast tcam table
  381. * set_mc_addr()
  382. * set multicast mode
  383. * add_uc_addr()
  384. * add ucast address
  385. * rm_uc_addr()
  386. * remove ucast address
  387. * set_mtu()
  388. * set mtu
  389. * update_stats()
  390. * update Old network device statistics
  391. * get_ethtool_stats()
  392. * get ethtool network device statistics
  393. * get_strings()
  394. * get a set of strings that describe the requested objects
  395. * get_sset_count()
  396. * get number of strings that @get_strings will write
  397. * update_led_status()
  398. * update the led status
  399. * set_led_id()
  400. * set led id
  401. * get_regs()
  402. * get regs dump
  403. * get_regs_len()
  404. * get the len of the regs dump
  405. */
  406. struct hnae_ae_ops {
  407. struct hnae_handle *(*get_handle)(struct hnae_ae_dev *dev,
  408. u32 port_id);
  409. void (*put_handle)(struct hnae_handle *handle);
  410. void (*init_queue)(struct hnae_queue *q);
  411. void (*fini_queue)(struct hnae_queue *q);
  412. int (*start)(struct hnae_handle *handle);
  413. void (*stop)(struct hnae_handle *handle);
  414. void (*reset)(struct hnae_handle *handle);
  415. int (*set_opts)(struct hnae_handle *handle, int type, void *opts);
  416. int (*get_opts)(struct hnae_handle *handle, int type, void **opts);
  417. int (*get_status)(struct hnae_handle *handle);
  418. int (*get_info)(struct hnae_handle *handle,
  419. u8 *auto_neg, u16 *speed, u8 *duplex);
  420. void (*toggle_ring_irq)(struct hnae_ring *ring, u32 val);
  421. void (*adjust_link)(struct hnae_handle *handle, int speed, int duplex);
  422. bool (*need_adjust_link)(struct hnae_handle *handle,
  423. int speed, int duplex);
  424. int (*set_loopback)(struct hnae_handle *handle,
  425. enum hnae_loop loop_mode, int en);
  426. void (*get_ring_bdnum_limit)(struct hnae_queue *queue,
  427. u32 *uplimit);
  428. void (*get_pauseparam)(struct hnae_handle *handle,
  429. u32 *auto_neg, u32 *rx_en, u32 *tx_en);
  430. int (*set_pauseparam)(struct hnae_handle *handle,
  431. u32 auto_neg, u32 rx_en, u32 tx_en);
  432. void (*get_coalesce_usecs)(struct hnae_handle *handle,
  433. u32 *tx_usecs, u32 *rx_usecs);
  434. void (*get_max_coalesced_frames)(struct hnae_handle *handle,
  435. u32 *tx_frames, u32 *rx_frames);
  436. int (*set_coalesce_usecs)(struct hnae_handle *handle, u32 timeout);
  437. int (*set_coalesce_frames)(struct hnae_handle *handle,
  438. u32 tx_frames, u32 rx_frames);
  439. void (*get_coalesce_range)(struct hnae_handle *handle,
  440. u32 *tx_frames_low, u32 *rx_frames_low,
  441. u32 *tx_frames_high, u32 *rx_frames_high,
  442. u32 *tx_usecs_low, u32 *rx_usecs_low,
  443. u32 *tx_usecs_high, u32 *rx_usecs_high);
  444. void (*set_promisc_mode)(struct hnae_handle *handle, u32 en);
  445. int (*get_mac_addr)(struct hnae_handle *handle, void **p);
  446. int (*set_mac_addr)(struct hnae_handle *handle, const void *p);
  447. int (*add_uc_addr)(struct hnae_handle *handle,
  448. const unsigned char *addr);
  449. int (*rm_uc_addr)(struct hnae_handle *handle,
  450. const unsigned char *addr);
  451. int (*clr_mc_addr)(struct hnae_handle *handle);
  452. int (*set_mc_addr)(struct hnae_handle *handle, void *addr);
  453. int (*set_mtu)(struct hnae_handle *handle, int new_mtu);
  454. void (*set_tso_stats)(struct hnae_handle *handle, int enable);
  455. void (*update_stats)(struct hnae_handle *handle,
  456. struct net_device_stats *net_stats);
  457. void (*get_stats)(struct hnae_handle *handle, u64 *data);
  458. void (*get_strings)(struct hnae_handle *handle,
  459. u32 stringset, u8 *data);
  460. int (*get_sset_count)(struct hnae_handle *handle, int stringset);
  461. void (*update_led_status)(struct hnae_handle *handle);
  462. int (*set_led_id)(struct hnae_handle *handle,
  463. enum hnae_led_state status);
  464. void (*get_regs)(struct hnae_handle *handle, void *data);
  465. int (*get_regs_len)(struct hnae_handle *handle);
  466. u32 (*get_rss_key_size)(struct hnae_handle *handle);
  467. u32 (*get_rss_indir_size)(struct hnae_handle *handle);
  468. int (*get_rss)(struct hnae_handle *handle, u32 *indir, u8 *key,
  469. u8 *hfunc);
  470. int (*set_rss)(struct hnae_handle *handle, const u32 *indir,
  471. const u8 *key, const u8 hfunc);
  472. };
  473. struct hnae_ae_dev {
  474. struct device cls_dev; /* the class dev */
  475. struct device *dev; /* the presented dev */
  476. struct hnae_ae_ops *ops;
  477. struct list_head node;
  478. struct module *owner; /* the module who provides this dev */
  479. int id;
  480. char name[AE_NAME_SIZE];
  481. struct list_head handle_list;
  482. spinlock_t lock; /* lock to protect the handle_list */
  483. };
  484. struct hnae_handle {
  485. struct device *owner_dev; /* the device which make use of this handle */
  486. struct hnae_ae_dev *dev; /* the device who provides this handle */
  487. struct phy_device *phy_dev;
  488. phy_interface_t phy_if;
  489. u32 if_support;
  490. int q_num;
  491. int vf_id;
  492. unsigned long coal_last_jiffies;
  493. u32 coal_param; /* self adapt coalesce param */
  494. /* the ring index of last ring that set coal param */
  495. u32 coal_ring_idx;
  496. u32 eport_id;
  497. u32 dport_id; /* v2 tx bd should fill the dport_id */
  498. bool coal_adapt_en;
  499. enum hnae_port_type port_type;
  500. enum hnae_media_type media_type;
  501. struct list_head node; /* list to hnae_ae_dev->handle_list */
  502. struct hnae_buf_ops *bops; /* operation for the buffer */
  503. struct hnae_queue *qs[]; /* flexible array of all queues */
  504. };
  505. #define ring_to_dev(ring) ((ring)->q->dev->dev)
  506. struct hnae_handle *hnae_get_handle(struct device *owner_dev,
  507. const struct fwnode_handle *fwnode,
  508. u32 port_id,
  509. struct hnae_buf_ops *bops);
  510. void hnae_put_handle(struct hnae_handle *handle);
  511. int hnae_ae_register(struct hnae_ae_dev *dev, struct module *owner);
  512. void hnae_ae_unregister(struct hnae_ae_dev *dev);
  513. int hnae_register_notifier(struct notifier_block *nb);
  514. void hnae_unregister_notifier(struct notifier_block *nb);
  515. int hnae_reinit_handle(struct hnae_handle *handle);
  516. #define hnae_queue_xmit(q, buf_num) writel_relaxed(buf_num, \
  517. (q)->tx_ring.io_base + RCB_REG_TAIL)
  518. #ifndef assert
  519. #define assert(cond)
  520. #endif
  521. static inline int hnae_reserve_buffer_map(struct hnae_ring *ring,
  522. struct hnae_desc_cb *cb)
  523. {
  524. struct hnae_buf_ops *bops = ring->q->handle->bops;
  525. int ret;
  526. ret = bops->alloc_buffer(ring, cb);
  527. if (ret)
  528. goto out;
  529. ret = bops->map_buffer(ring, cb);
  530. if (ret)
  531. goto out_with_buf;
  532. return 0;
  533. out_with_buf:
  534. bops->free_buffer(ring, cb);
  535. out:
  536. return ret;
  537. }
  538. static inline int hnae_alloc_buffer_attach(struct hnae_ring *ring, int i)
  539. {
  540. int ret = hnae_reserve_buffer_map(ring, &ring->desc_cb[i]);
  541. if (ret)
  542. return ret;
  543. ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
  544. return 0;
  545. }
  546. static inline void hnae_buffer_detach(struct hnae_ring *ring, int i)
  547. {
  548. ring->q->handle->bops->unmap_buffer(ring, &ring->desc_cb[i]);
  549. ring->desc[i].addr = 0;
  550. }
  551. static inline void hnae_free_buffer_detach(struct hnae_ring *ring, int i)
  552. {
  553. struct hnae_buf_ops *bops = ring->q->handle->bops;
  554. struct hnae_desc_cb *cb = &ring->desc_cb[i];
  555. if (!ring->desc_cb[i].dma)
  556. return;
  557. hnae_buffer_detach(ring, i);
  558. bops->free_buffer(ring, cb);
  559. }
  560. /* detach a in-used buffer and replace with a reserved one */
  561. static inline void hnae_replace_buffer(struct hnae_ring *ring, int i,
  562. struct hnae_desc_cb *res_cb)
  563. {
  564. struct hnae_buf_ops *bops = ring->q->handle->bops;
  565. bops->unmap_buffer(ring, &ring->desc_cb[i]);
  566. ring->desc_cb[i] = *res_cb;
  567. ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma);
  568. ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
  569. }
  570. static inline void hnae_reuse_buffer(struct hnae_ring *ring, int i)
  571. {
  572. ring->desc_cb[i].reuse_flag = 0;
  573. ring->desc[i].addr = cpu_to_le64(ring->desc_cb[i].dma
  574. + ring->desc_cb[i].page_offset);
  575. ring->desc[i].rx.ipoff_bnum_pid_flag = 0;
  576. }
  577. /* when reinit buffer size, we should reinit buffer description */
  578. static inline void hnae_reinit_all_ring_desc(struct hnae_handle *h)
  579. {
  580. int i, j;
  581. struct hnae_ring *ring;
  582. for (i = 0; i < h->q_num; i++) {
  583. ring = &h->qs[i]->rx_ring;
  584. for (j = 0; j < ring->desc_num; j++)
  585. ring->desc[j].addr = cpu_to_le64(ring->desc_cb[j].dma);
  586. }
  587. wmb(); /* commit all data before submit */
  588. }
  589. /* when reinit buffer size, we should reinit page offset */
  590. static inline void hnae_reinit_all_ring_page_off(struct hnae_handle *h)
  591. {
  592. int i, j;
  593. struct hnae_ring *ring;
  594. for (i = 0; i < h->q_num; i++) {
  595. ring = &h->qs[i]->rx_ring;
  596. for (j = 0; j < ring->desc_num; j++) {
  597. ring->desc_cb[j].page_offset = 0;
  598. if (ring->desc[j].addr !=
  599. cpu_to_le64(ring->desc_cb[j].dma))
  600. ring->desc[j].addr =
  601. cpu_to_le64(ring->desc_cb[j].dma);
  602. }
  603. }
  604. wmb(); /* commit all data before submit */
  605. }
  606. #define hnae_set_field(origin, mask, shift, val) \
  607. do { \
  608. (origin) &= (~(mask)); \
  609. (origin) |= ((val) << (shift)) & (mask); \
  610. } while (0)
  611. #define hnae_set_bit(origin, shift, val) \
  612. hnae_set_field((origin), (0x1 << (shift)), (shift), (val))
  613. #define hnae_get_field(origin, mask, shift) (((origin) & (mask)) >> (shift))
  614. #define hnae_get_bit(origin, shift) \
  615. hnae_get_field((origin), (0x1 << (shift)), (shift))
  616. #endif