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