hnae.h 18 KB

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