hnae.h 20 KB

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