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 #include <linux/etherdevice.h>
11 #include <linux/netdevice.h>
12 #include <linux/spinlock.h>
13 
14 #include "hnae.h"
15 #include "hns_dsaf_mac.h"
16 #include "hns_dsaf_main.h"
17 #include "hns_dsaf_ppe.h"
18 #include "hns_dsaf_rcb.h"
19 
20 #define AE_NAME_PORT_ID_IDX 6
21 
22 static struct hns_mac_cb *hns_get_mac_cb(struct hnae_handle *handle)
23 {
24 	struct  hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
25 
26 	return vf_cb->mac_cb;
27 }
28 
29 static struct dsaf_device *hns_ae_get_dsaf_dev(struct hnae_ae_dev *dev)
30 {
31 	return container_of(dev, struct dsaf_device, ae_dev);
32 }
33 
34 static struct hns_ppe_cb *hns_get_ppe_cb(struct hnae_handle *handle)
35 {
36 	int ppe_index;
37 	struct ppe_common_cb *ppe_comm;
38 	struct  hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
39 
40 	ppe_comm = vf_cb->dsaf_dev->ppe_common[0];
41 	ppe_index = vf_cb->port_index;
42 
43 	return &ppe_comm->ppe_cb[ppe_index];
44 }
45 
46 static int hns_ae_get_q_num_per_vf(
47 	struct dsaf_device *dsaf_dev, int port)
48 {
49 	return dsaf_dev->rcb_common[0]->max_q_per_vf;
50 }
51 
52 static int hns_ae_get_vf_num_per_port(
53 	struct dsaf_device *dsaf_dev, int port)
54 {
55 	return dsaf_dev->rcb_common[0]->max_vfn;
56 }
57 
58 static struct ring_pair_cb *hns_ae_get_base_ring_pair(
59 	struct dsaf_device *dsaf_dev, int port)
60 {
61 	struct rcb_common_cb *rcb_comm = dsaf_dev->rcb_common[0];
62 	int q_num = rcb_comm->max_q_per_vf;
63 	int vf_num = rcb_comm->max_vfn;
64 
65 	return &rcb_comm->ring_pair_cb[port * q_num * vf_num];
66 }
67 
68 static struct ring_pair_cb *hns_ae_get_ring_pair(struct hnae_queue *q)
69 {
70 	return container_of(q, struct ring_pair_cb, q);
71 }
72 
73 struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
74 				      u32 port_id)
75 {
76 	int vfnum_per_port;
77 	int qnum_per_vf;
78 	int i;
79 	struct dsaf_device *dsaf_dev;
80 	struct hnae_handle *ae_handle;
81 	struct ring_pair_cb *ring_pair_cb;
82 	struct hnae_vf_cb *vf_cb;
83 
84 	dsaf_dev = hns_ae_get_dsaf_dev(dev);
85 
86 	ring_pair_cb = hns_ae_get_base_ring_pair(dsaf_dev, port_id);
87 	vfnum_per_port = hns_ae_get_vf_num_per_port(dsaf_dev, port_id);
88 	qnum_per_vf = hns_ae_get_q_num_per_vf(dsaf_dev, port_id);
89 
90 	vf_cb = kzalloc(sizeof(*vf_cb) +
91 			qnum_per_vf * sizeof(struct hnae_queue *), GFP_KERNEL);
92 	if (unlikely(!vf_cb)) {
93 		dev_err(dsaf_dev->dev, "malloc vf_cb fail!\n");
94 		ae_handle = ERR_PTR(-ENOMEM);
95 		goto handle_err;
96 	}
97 	ae_handle = &vf_cb->ae_handle;
98 	/* ae_handle Init  */
99 	ae_handle->owner_dev = dsaf_dev->dev;
100 	ae_handle->dev = dev;
101 	ae_handle->q_num = qnum_per_vf;
102 
103 	/* find ring pair, and set vf id*/
104 	for (ae_handle->vf_id = 0;
105 		ae_handle->vf_id < vfnum_per_port; ae_handle->vf_id++) {
106 		if (!ring_pair_cb->used_by_vf)
107 			break;
108 		ring_pair_cb += qnum_per_vf;
109 	}
110 	if (ae_handle->vf_id >= vfnum_per_port) {
111 		dev_err(dsaf_dev->dev, "malloc queue fail!\n");
112 		ae_handle = ERR_PTR(-EINVAL);
113 		goto vf_id_err;
114 	}
115 
116 	ae_handle->qs = (struct hnae_queue **)(&ae_handle->qs + 1);
117 	for (i = 0; i < qnum_per_vf; i++) {
118 		ae_handle->qs[i] = &ring_pair_cb->q;
119 		ae_handle->qs[i]->rx_ring.q = ae_handle->qs[i];
120 		ae_handle->qs[i]->tx_ring.q = ae_handle->qs[i];
121 
122 		ring_pair_cb->used_by_vf = 1;
123 		ring_pair_cb++;
124 	}
125 
126 	vf_cb->dsaf_dev = dsaf_dev;
127 	vf_cb->port_index = port_id;
128 	vf_cb->mac_cb = dsaf_dev->mac_cb[port_id];
129 
130 	ae_handle->phy_if = vf_cb->mac_cb->phy_if;
131 	ae_handle->phy_dev = vf_cb->mac_cb->phy_dev;
132 	ae_handle->if_support = vf_cb->mac_cb->if_support;
133 	ae_handle->port_type = vf_cb->mac_cb->mac_type;
134 	ae_handle->media_type = vf_cb->mac_cb->media_type;
135 	ae_handle->dport_id = port_id;
136 
137 	return ae_handle;
138 vf_id_err:
139 	kfree(vf_cb);
140 handle_err:
141 	return ae_handle;
142 }
143 
144 static void hns_ae_put_handle(struct hnae_handle *handle)
145 {
146 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
147 	int i;
148 
149 	vf_cb->mac_cb	 = NULL;
150 
151 	kfree(vf_cb);
152 
153 	for (i = 0; i < handle->q_num; i++)
154 		hns_ae_get_ring_pair(handle->qs[i])->used_by_vf = 0;
155 }
156 
157 static void hns_ae_ring_enable_all(struct hnae_handle *handle, int val)
158 {
159 	int q_num = handle->q_num;
160 	int i;
161 
162 	for (i = 0; i < q_num; i++)
163 		hns_rcb_ring_enable_hw(handle->qs[i], val);
164 }
165 
166 static void hns_ae_init_queue(struct hnae_queue *q)
167 {
168 	struct ring_pair_cb *ring =
169 		container_of(q, struct ring_pair_cb, q);
170 
171 	hns_rcb_init_hw(ring);
172 }
173 
174 static void hns_ae_fini_queue(struct hnae_queue *q)
175 {
176 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(q->handle);
177 
178 	if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
179 		hns_rcb_reset_ring_hw(q);
180 }
181 
182 static int hns_ae_set_mac_address(struct hnae_handle *handle, void *p)
183 {
184 	int ret;
185 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
186 
187 	if (!p || !is_valid_ether_addr((const u8 *)p)) {
188 		dev_err(handle->owner_dev, "is not valid ether addr !\n");
189 		return -EADDRNOTAVAIL;
190 	}
191 
192 	ret = hns_mac_change_vf_addr(mac_cb, handle->vf_id, p);
193 	if (ret != 0) {
194 		dev_err(handle->owner_dev,
195 			"set_mac_address fail, ret=%d!\n", ret);
196 		return ret;
197 	}
198 
199 	return 0;
200 }
201 
202 static int hns_ae_add_uc_address(struct hnae_handle *handle,
203 				 const unsigned char *addr)
204 {
205 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
206 
207 	if (mac_cb->mac_type != HNAE_PORT_SERVICE)
208 		return -ENOSPC;
209 
210 	return hns_mac_add_uc_addr(mac_cb, handle->vf_id, addr);
211 }
212 
213 static int hns_ae_rm_uc_address(struct hnae_handle *handle,
214 				const unsigned char *addr)
215 {
216 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
217 
218 	if (mac_cb->mac_type != HNAE_PORT_SERVICE)
219 		return -ENOSPC;
220 
221 	return hns_mac_rm_uc_addr(mac_cb, handle->vf_id, addr);
222 }
223 
224 static int hns_ae_set_multicast_one(struct hnae_handle *handle, void *addr)
225 {
226 	int ret;
227 	char *mac_addr = (char *)addr;
228 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
229 	u8 port_num;
230 
231 	assert(mac_cb);
232 
233 	if (mac_cb->mac_type != HNAE_PORT_SERVICE)
234 		return 0;
235 
236 	ret = hns_mac_set_multi(mac_cb, mac_cb->mac_id, mac_addr, true);
237 	if (ret) {
238 		dev_err(handle->owner_dev,
239 			"mac add mul_mac:%pM port%d  fail, ret = %#x!\n",
240 			mac_addr, mac_cb->mac_id, ret);
241 		return ret;
242 	}
243 
244 	ret = hns_mac_get_inner_port_num(mac_cb, handle->vf_id, &port_num);
245 	if (ret)
246 		return ret;
247 
248 	ret = hns_mac_set_multi(mac_cb, port_num, mac_addr, true);
249 	if (ret)
250 		dev_err(handle->owner_dev,
251 			"mac add mul_mac:%pM port%d  fail, ret = %#x!\n",
252 			mac_addr, DSAF_BASE_INNER_PORT_NUM, ret);
253 
254 	return ret;
255 }
256 
257 static int hns_ae_clr_multicast(struct hnae_handle *handle)
258 {
259 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
260 
261 	if (mac_cb->mac_type != HNAE_PORT_SERVICE)
262 		return 0;
263 
264 	return hns_mac_clr_multicast(mac_cb, handle->vf_id);
265 }
266 
267 static int hns_ae_set_mtu(struct hnae_handle *handle, int new_mtu)
268 {
269 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
270 	struct hnae_queue *q;
271 	u32 rx_buf_size;
272 	int i, ret;
273 
274 	/* when buf_size is 2048, max mtu is 6K for rx ring max bd num is 3. */
275 	if (!AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver)) {
276 		if (new_mtu <= BD_SIZE_2048_MAX_MTU)
277 			rx_buf_size = 2048;
278 		else
279 			rx_buf_size = 4096;
280 	} else {
281 		rx_buf_size = mac_cb->dsaf_dev->buf_size;
282 	}
283 
284 	ret = hns_mac_set_mtu(mac_cb, new_mtu, rx_buf_size);
285 
286 	if (!ret) {
287 		/* reinit ring buf_size */
288 		for (i = 0; i < handle->q_num; i++) {
289 			q = handle->qs[i];
290 			q->rx_ring.buf_size = rx_buf_size;
291 			hns_rcb_set_rx_ring_bs(q, rx_buf_size);
292 		}
293 	}
294 
295 	return ret;
296 }
297 
298 static void hns_ae_set_tso_stats(struct hnae_handle *handle, int enable)
299 {
300 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
301 
302 	hns_ppe_set_tso_enable(ppe_cb, enable);
303 }
304 
305 static int hns_ae_start(struct hnae_handle *handle)
306 {
307 	int ret;
308 	int k;
309 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
310 
311 	ret = hns_mac_vm_config_bc_en(mac_cb, 0, true);
312 	if (ret)
313 		return ret;
314 
315 	for (k = 0; k < handle->q_num; k++) {
316 		if (AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver))
317 			hns_rcb_int_clr_hw(handle->qs[k],
318 					   RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
319 		else
320 			hns_rcbv2_int_clr_hw(handle->qs[k],
321 					     RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
322 	}
323 	hns_ae_ring_enable_all(handle, 1);
324 	msleep(100);
325 
326 	hns_mac_start(mac_cb);
327 
328 	return 0;
329 }
330 
331 void hns_ae_stop(struct hnae_handle *handle)
332 {
333 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
334 
335 	/* just clean tx fbd, neednot rx fbd*/
336 	hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_TX);
337 
338 	msleep(20);
339 
340 	hns_mac_stop(mac_cb);
341 
342 	usleep_range(10000, 20000);
343 
344 	hns_ae_ring_enable_all(handle, 0);
345 
346 	(void)hns_mac_vm_config_bc_en(mac_cb, 0, false);
347 }
348 
349 static void hns_ae_reset(struct hnae_handle *handle)
350 {
351 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
352 
353 	if (vf_cb->mac_cb->mac_type == HNAE_PORT_DEBUG) {
354 		hns_mac_reset(vf_cb->mac_cb);
355 		hns_ppe_reset_common(vf_cb->dsaf_dev, 0);
356 	}
357 }
358 
359 void hns_ae_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
360 {
361 	u32 flag;
362 
363 	if (is_tx_ring(ring))
364 		flag = RCB_INT_FLAG_TX;
365 	else
366 		flag = RCB_INT_FLAG_RX;
367 
368 	hns_rcb_int_ctrl_hw(ring->q, flag, mask);
369 }
370 
371 static void hns_aev2_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
372 {
373 	u32 flag;
374 
375 	if (is_tx_ring(ring))
376 		flag = RCB_INT_FLAG_TX;
377 	else
378 		flag = RCB_INT_FLAG_RX;
379 
380 	hns_rcbv2_int_ctrl_hw(ring->q, flag, mask);
381 }
382 
383 static int hns_ae_get_link_status(struct hnae_handle *handle)
384 {
385 	u32 link_status;
386 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
387 
388 	hns_mac_get_link_status(mac_cb, &link_status);
389 
390 	return !!link_status;
391 }
392 
393 static int hns_ae_get_mac_info(struct hnae_handle *handle,
394 			       u8 *auto_neg, u16 *speed, u8 *duplex)
395 {
396 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
397 
398 	return hns_mac_get_port_info(mac_cb, auto_neg, speed, duplex);
399 }
400 
401 static void hns_ae_adjust_link(struct hnae_handle *handle, int speed,
402 			       int duplex)
403 {
404 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
405 
406 	hns_mac_adjust_link(mac_cb, speed, duplex);
407 }
408 
409 static void hns_ae_get_ring_bdnum_limit(struct hnae_queue *queue,
410 					u32 *uplimit)
411 {
412 	*uplimit = HNS_RCB_RING_MAX_PENDING_BD;
413 }
414 
415 static void hns_ae_get_pauseparam(struct hnae_handle *handle,
416 				  u32 *auto_neg, u32 *rx_en, u32 *tx_en)
417 {
418 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
419 	struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
420 
421 	hns_mac_get_autoneg(mac_cb, auto_neg);
422 
423 	hns_mac_get_pauseparam(mac_cb, rx_en, tx_en);
424 
425 	/* Service port's pause feature is provided by DSAF, not mac */
426 	if (handle->port_type == HNAE_PORT_SERVICE)
427 		hns_dsaf_get_rx_mac_pause_en(dsaf_dev, mac_cb->mac_id, rx_en);
428 }
429 
430 static int hns_ae_set_autoneg(struct hnae_handle *handle, u8 enable)
431 {
432 	assert(handle);
433 
434 	return hns_mac_set_autoneg(hns_get_mac_cb(handle), enable);
435 }
436 
437 static void hns_ae_set_promisc_mode(struct hnae_handle *handle, u32 en)
438 {
439 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
440 
441 	hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle->dev), en);
442 	hns_mac_set_promisc(mac_cb, (u8)!!en);
443 }
444 
445 static int hns_ae_get_autoneg(struct hnae_handle *handle)
446 {
447 	u32     auto_neg;
448 
449 	assert(handle);
450 
451 	hns_mac_get_autoneg(hns_get_mac_cb(handle), &auto_neg);
452 
453 	return auto_neg;
454 }
455 
456 static int hns_ae_set_pauseparam(struct hnae_handle *handle,
457 				 u32 autoneg, u32 rx_en, u32 tx_en)
458 {
459 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
460 	struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
461 	int ret;
462 
463 	ret = hns_mac_set_autoneg(mac_cb, autoneg);
464 	if (ret)
465 		return ret;
466 
467 	/* Service port's pause feature is provided by DSAF, not mac */
468 	if (handle->port_type == HNAE_PORT_SERVICE) {
469 		ret = hns_dsaf_set_rx_mac_pause_en(dsaf_dev,
470 						   mac_cb->mac_id, rx_en);
471 		if (ret)
472 			return ret;
473 		rx_en = 0;
474 	}
475 	return hns_mac_set_pauseparam(mac_cb, rx_en, tx_en);
476 }
477 
478 static void hns_ae_get_coalesce_usecs(struct hnae_handle *handle,
479 				      u32 *tx_usecs, u32 *rx_usecs)
480 {
481 	struct ring_pair_cb *ring_pair =
482 		container_of(handle->qs[0], struct ring_pair_cb, q);
483 
484 	*tx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
485 					       ring_pair->port_id_in_comm);
486 	*rx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
487 					       ring_pair->port_id_in_comm);
488 }
489 
490 static void hns_ae_get_max_coalesced_frames(struct hnae_handle *handle,
491 					    u32 *tx_frames, u32 *rx_frames)
492 {
493 	struct ring_pair_cb *ring_pair =
494 		container_of(handle->qs[0], struct ring_pair_cb, q);
495 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
496 
497 	if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
498 	    handle->port_type == HNAE_PORT_DEBUG)
499 		*tx_frames = hns_rcb_get_rx_coalesced_frames(
500 			ring_pair->rcb_common, ring_pair->port_id_in_comm);
501 	else
502 		*tx_frames = hns_rcb_get_tx_coalesced_frames(
503 			ring_pair->rcb_common, ring_pair->port_id_in_comm);
504 	*rx_frames = hns_rcb_get_rx_coalesced_frames(ring_pair->rcb_common,
505 						  ring_pair->port_id_in_comm);
506 }
507 
508 static int hns_ae_set_coalesce_usecs(struct hnae_handle *handle,
509 				     u32 timeout)
510 {
511 	struct ring_pair_cb *ring_pair =
512 		container_of(handle->qs[0], struct ring_pair_cb, q);
513 
514 	return hns_rcb_set_coalesce_usecs(
515 		ring_pair->rcb_common, ring_pair->port_id_in_comm, timeout);
516 }
517 
518 static int hns_ae_set_coalesce_frames(struct hnae_handle *handle,
519 				      u32 tx_frames, u32 rx_frames)
520 {
521 	int ret;
522 	struct ring_pair_cb *ring_pair =
523 		container_of(handle->qs[0], struct ring_pair_cb, q);
524 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
525 
526 	if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
527 	    handle->port_type == HNAE_PORT_DEBUG) {
528 		if (tx_frames != rx_frames)
529 			return -EINVAL;
530 		return hns_rcb_set_rx_coalesced_frames(
531 			ring_pair->rcb_common,
532 			ring_pair->port_id_in_comm, rx_frames);
533 	} else {
534 		if (tx_frames != 1)
535 			return -EINVAL;
536 		ret = hns_rcb_set_tx_coalesced_frames(
537 			ring_pair->rcb_common,
538 			ring_pair->port_id_in_comm, tx_frames);
539 		if (ret)
540 			return ret;
541 
542 		return hns_rcb_set_rx_coalesced_frames(
543 			ring_pair->rcb_common,
544 			ring_pair->port_id_in_comm, rx_frames);
545 	}
546 }
547 
548 static void hns_ae_get_coalesce_range(struct hnae_handle *handle,
549 				      u32 *tx_frames_low, u32 *rx_frames_low,
550 				      u32 *tx_frames_high, u32 *rx_frames_high,
551 				      u32 *tx_usecs_low, u32 *rx_usecs_low,
552 				      u32 *tx_usecs_high, u32 *rx_usecs_high)
553 {
554 	struct dsaf_device *dsaf_dev;
555 
556 	assert(handle);
557 
558 	dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
559 
560 	*tx_frames_low  = HNS_RCB_TX_FRAMES_LOW;
561 	*rx_frames_low  = HNS_RCB_RX_FRAMES_LOW;
562 
563 	if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
564 	    handle->port_type == HNAE_PORT_DEBUG)
565 		*tx_frames_high =
566 			(dsaf_dev->desc_num - 1 > HNS_RCB_TX_FRAMES_HIGH) ?
567 			HNS_RCB_TX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
568 	else
569 		*tx_frames_high = 1;
570 
571 	*rx_frames_high = (dsaf_dev->desc_num - 1 > HNS_RCB_RX_FRAMES_HIGH) ?
572 		HNS_RCB_RX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
573 	*tx_usecs_low   = HNS_RCB_TX_USECS_LOW;
574 	*rx_usecs_low   = HNS_RCB_RX_USECS_LOW;
575 	*tx_usecs_high  = HNS_RCB_TX_USECS_HIGH;
576 	*rx_usecs_high  = HNS_RCB_RX_USECS_HIGH;
577 }
578 
579 void hns_ae_update_stats(struct hnae_handle *handle,
580 			 struct net_device_stats *net_stats)
581 {
582 	int port;
583 	int idx;
584 	struct dsaf_device *dsaf_dev;
585 	struct hns_mac_cb *mac_cb;
586 	struct hns_ppe_cb *ppe_cb;
587 	struct hnae_queue *queue;
588 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
589 	u64 tx_bytes = 0, rx_bytes = 0, tx_packets = 0, rx_packets = 0;
590 	u64 rx_errors = 0, tx_errors = 0, tx_dropped = 0;
591 	u64 rx_missed_errors = 0;
592 
593 	dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
594 	if (!dsaf_dev)
595 		return;
596 	port = vf_cb->port_index;
597 	ppe_cb = hns_get_ppe_cb(handle);
598 	mac_cb = hns_get_mac_cb(handle);
599 
600 	for (idx = 0; idx < handle->q_num; idx++) {
601 		queue = handle->qs[idx];
602 		hns_rcb_update_stats(queue);
603 
604 		tx_bytes += queue->tx_ring.stats.tx_bytes;
605 		tx_packets += queue->tx_ring.stats.tx_pkts;
606 		rx_bytes += queue->rx_ring.stats.rx_bytes;
607 		rx_packets += queue->rx_ring.stats.rx_pkts;
608 
609 		rx_errors += queue->rx_ring.stats.err_pkt_len
610 				+ queue->rx_ring.stats.l2_err
611 				+ queue->rx_ring.stats.l3l4_csum_err;
612 	}
613 
614 	hns_ppe_update_stats(ppe_cb);
615 	rx_missed_errors = ppe_cb->hw_stats.rx_drop_no_buf;
616 	tx_errors += ppe_cb->hw_stats.tx_err_checksum
617 		+ ppe_cb->hw_stats.tx_err_fifo_empty;
618 
619 	if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
620 		hns_dsaf_update_stats(dsaf_dev, port);
621 		/* for port upline direction, i.e., rx. */
622 		rx_missed_errors += dsaf_dev->hw_stats[port].bp_drop;
623 		rx_missed_errors += dsaf_dev->hw_stats[port].pad_drop;
624 		rx_missed_errors += dsaf_dev->hw_stats[port].crc_false;
625 
626 		/* for port downline direction, i.e., tx. */
627 		port = port + DSAF_PPE_INODE_BASE;
628 		hns_dsaf_update_stats(dsaf_dev, port);
629 		tx_dropped += dsaf_dev->hw_stats[port].bp_drop;
630 		tx_dropped += dsaf_dev->hw_stats[port].pad_drop;
631 		tx_dropped += dsaf_dev->hw_stats[port].crc_false;
632 		tx_dropped += dsaf_dev->hw_stats[port].rslt_drop;
633 		tx_dropped += dsaf_dev->hw_stats[port].vlan_drop;
634 		tx_dropped += dsaf_dev->hw_stats[port].stp_drop;
635 	}
636 
637 	hns_mac_update_stats(mac_cb);
638 	rx_errors += mac_cb->hw_stats.rx_fifo_overrun_err;
639 
640 	tx_errors += mac_cb->hw_stats.tx_bad_pkts
641 		+ mac_cb->hw_stats.tx_fragment_err
642 		+ mac_cb->hw_stats.tx_jabber_err
643 		+ mac_cb->hw_stats.tx_underrun_err
644 		+ mac_cb->hw_stats.tx_crc_err;
645 
646 	net_stats->tx_bytes = tx_bytes;
647 	net_stats->tx_packets = tx_packets;
648 	net_stats->rx_bytes = rx_bytes;
649 	net_stats->rx_dropped = 0;
650 	net_stats->rx_packets = rx_packets;
651 	net_stats->rx_errors = rx_errors;
652 	net_stats->tx_errors = tx_errors;
653 	net_stats->tx_dropped = tx_dropped;
654 	net_stats->rx_missed_errors = rx_missed_errors;
655 	net_stats->rx_crc_errors = mac_cb->hw_stats.rx_fcs_err;
656 	net_stats->rx_frame_errors = mac_cb->hw_stats.rx_align_err;
657 	net_stats->rx_fifo_errors = mac_cb->hw_stats.rx_fifo_overrun_err;
658 	net_stats->rx_length_errors = mac_cb->hw_stats.rx_len_err;
659 	net_stats->multicast = mac_cb->hw_stats.rx_mc_pkts;
660 }
661 
662 void hns_ae_get_stats(struct hnae_handle *handle, u64 *data)
663 {
664 	int idx;
665 	struct hns_mac_cb *mac_cb;
666 	struct hns_ppe_cb *ppe_cb;
667 	u64 *p = data;
668 	struct  hnae_vf_cb *vf_cb;
669 
670 	if (!handle || !data) {
671 		pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
672 		return;
673 	}
674 
675 	vf_cb = hns_ae_get_vf_cb(handle);
676 	mac_cb = hns_get_mac_cb(handle);
677 	ppe_cb = hns_get_ppe_cb(handle);
678 
679 	for (idx = 0; idx < handle->q_num; idx++) {
680 		hns_rcb_get_stats(handle->qs[idx], p);
681 		p += hns_rcb_get_ring_sset_count((int)ETH_SS_STATS);
682 	}
683 
684 	hns_ppe_get_stats(ppe_cb, p);
685 	p += hns_ppe_get_sset_count((int)ETH_SS_STATS);
686 
687 	hns_mac_get_stats(mac_cb, p);
688 	p += hns_mac_get_sset_count(mac_cb, (int)ETH_SS_STATS);
689 
690 	if (mac_cb->mac_type == HNAE_PORT_SERVICE)
691 		hns_dsaf_get_stats(vf_cb->dsaf_dev, p, vf_cb->port_index);
692 }
693 
694 void hns_ae_get_strings(struct hnae_handle *handle,
695 			u32 stringset, u8 *data)
696 {
697 	int port;
698 	int idx;
699 	struct hns_mac_cb *mac_cb;
700 	struct hns_ppe_cb *ppe_cb;
701 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
702 	u8 *p = data;
703 	struct	hnae_vf_cb *vf_cb;
704 
705 	assert(handle);
706 
707 	vf_cb = hns_ae_get_vf_cb(handle);
708 	port = vf_cb->port_index;
709 	mac_cb = hns_get_mac_cb(handle);
710 	ppe_cb = hns_get_ppe_cb(handle);
711 
712 	for (idx = 0; idx < handle->q_num; idx++) {
713 		hns_rcb_get_strings(stringset, p, idx);
714 		p += ETH_GSTRING_LEN * hns_rcb_get_ring_sset_count(stringset);
715 	}
716 
717 	hns_ppe_get_strings(ppe_cb, stringset, p);
718 	p += ETH_GSTRING_LEN * hns_ppe_get_sset_count(stringset);
719 
720 	hns_mac_get_strings(mac_cb, stringset, p);
721 	p += ETH_GSTRING_LEN * hns_mac_get_sset_count(mac_cb, stringset);
722 
723 	if (mac_cb->mac_type == HNAE_PORT_SERVICE)
724 		hns_dsaf_get_strings(stringset, p, port, dsaf_dev);
725 }
726 
727 int hns_ae_get_sset_count(struct hnae_handle *handle, int stringset)
728 {
729 	u32 sset_count = 0;
730 	struct hns_mac_cb *mac_cb;
731 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
732 
733 	assert(handle);
734 
735 	mac_cb = hns_get_mac_cb(handle);
736 
737 	sset_count += hns_rcb_get_ring_sset_count(stringset) * handle->q_num;
738 	sset_count += hns_ppe_get_sset_count(stringset);
739 	sset_count += hns_mac_get_sset_count(mac_cb, stringset);
740 
741 	if (mac_cb->mac_type == HNAE_PORT_SERVICE)
742 		sset_count += hns_dsaf_get_sset_count(dsaf_dev, stringset);
743 
744 	return sset_count;
745 }
746 
747 static int hns_ae_config_loopback(struct hnae_handle *handle,
748 				  enum hnae_loop loop, int en)
749 {
750 	int ret;
751 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
752 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
753 	struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
754 
755 	switch (loop) {
756 	case MAC_INTERNALLOOP_PHY:
757 		ret = 0;
758 		break;
759 	case MAC_INTERNALLOOP_SERDES:
760 		ret = dsaf_dev->misc_op->cfg_serdes_loopback(vf_cb->mac_cb,
761 							     !!en);
762 		break;
763 	case MAC_INTERNALLOOP_MAC:
764 		ret = hns_mac_config_mac_loopback(vf_cb->mac_cb, loop, en);
765 		break;
766 	default:
767 		ret = -EINVAL;
768 	}
769 
770 	return ret;
771 }
772 
773 void hns_ae_update_led_status(struct hnae_handle *handle)
774 {
775 	struct hns_mac_cb *mac_cb;
776 
777 	assert(handle);
778 	mac_cb = hns_get_mac_cb(handle);
779 	if (!mac_cb->cpld_ctrl)
780 		return;
781 	hns_set_led_opt(mac_cb);
782 }
783 
784 int hns_ae_cpld_set_led_id(struct hnae_handle *handle,
785 			   enum hnae_led_state status)
786 {
787 	struct hns_mac_cb *mac_cb;
788 
789 	assert(handle);
790 
791 	mac_cb = hns_get_mac_cb(handle);
792 
793 	return hns_cpld_led_set_id(mac_cb, status);
794 }
795 
796 void hns_ae_get_regs(struct hnae_handle *handle, void *data)
797 {
798 	u32 *p = data;
799 	int i;
800 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
801 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
802 
803 	hns_ppe_get_regs(ppe_cb, p);
804 	p += hns_ppe_get_regs_count();
805 
806 	hns_rcb_get_common_regs(vf_cb->dsaf_dev->rcb_common[0], p);
807 	p += hns_rcb_get_common_regs_count();
808 
809 	for (i = 0; i < handle->q_num; i++) {
810 		hns_rcb_get_ring_regs(handle->qs[i], p);
811 		p += hns_rcb_get_ring_regs_count();
812 	}
813 
814 	hns_mac_get_regs(vf_cb->mac_cb, p);
815 	p += hns_mac_get_regs_count(vf_cb->mac_cb);
816 
817 	if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
818 		hns_dsaf_get_regs(vf_cb->dsaf_dev, vf_cb->port_index, p);
819 }
820 
821 int hns_ae_get_regs_len(struct hnae_handle *handle)
822 {
823 	u32 total_num;
824 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
825 
826 	total_num = hns_ppe_get_regs_count();
827 	total_num += hns_rcb_get_common_regs_count();
828 	total_num += hns_rcb_get_ring_regs_count() * handle->q_num;
829 	total_num += hns_mac_get_regs_count(vf_cb->mac_cb);
830 
831 	if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
832 		total_num += hns_dsaf_get_regs_count();
833 
834 	return total_num;
835 }
836 
837 static u32 hns_ae_get_rss_key_size(struct hnae_handle *handle)
838 {
839 	return HNS_PPEV2_RSS_KEY_SIZE;
840 }
841 
842 static u32 hns_ae_get_rss_indir_size(struct hnae_handle *handle)
843 {
844 	return HNS_PPEV2_RSS_IND_TBL_SIZE;
845 }
846 
847 static int hns_ae_get_rss(struct hnae_handle *handle, u32 *indir, u8 *key,
848 			  u8 *hfunc)
849 {
850 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
851 
852 	/* currently we support only one type of hash function i.e. Toep hash */
853 	if (hfunc)
854 		*hfunc = ETH_RSS_HASH_TOP;
855 
856 	/* get the RSS Key required by the user */
857 	if (key)
858 		memcpy(key, ppe_cb->rss_key, HNS_PPEV2_RSS_KEY_SIZE);
859 
860 	/* update the current hash->queue mappings from the shadow RSS table */
861 	if (indir)
862 		memcpy(indir, ppe_cb->rss_indir_table,
863 		       HNS_PPEV2_RSS_IND_TBL_SIZE  * sizeof(*indir));
864 
865 	return 0;
866 }
867 
868 static int hns_ae_set_rss(struct hnae_handle *handle, const u32 *indir,
869 			  const u8 *key, const u8 hfunc)
870 {
871 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
872 
873 	/* set the RSS Hash Key if specififed by the user */
874 	if (key) {
875 		memcpy(ppe_cb->rss_key, key, HNS_PPEV2_RSS_KEY_SIZE);
876 		hns_ppe_set_rss_key(ppe_cb, ppe_cb->rss_key);
877 	}
878 
879 	if (indir) {
880 		/* update the shadow RSS table with user specified qids */
881 		memcpy(ppe_cb->rss_indir_table, indir,
882 		       HNS_PPEV2_RSS_IND_TBL_SIZE  * sizeof(*indir));
883 
884 		/* now update the hardware */
885 		hns_ppe_set_indir_table(ppe_cb, ppe_cb->rss_indir_table);
886 	}
887 
888 	return 0;
889 }
890 
891 static struct hnae_ae_ops hns_dsaf_ops = {
892 	.get_handle = hns_ae_get_handle,
893 	.put_handle = hns_ae_put_handle,
894 	.init_queue = hns_ae_init_queue,
895 	.fini_queue = hns_ae_fini_queue,
896 	.start = hns_ae_start,
897 	.stop = hns_ae_stop,
898 	.reset = hns_ae_reset,
899 	.toggle_ring_irq = hns_ae_toggle_ring_irq,
900 	.get_status = hns_ae_get_link_status,
901 	.get_info = hns_ae_get_mac_info,
902 	.adjust_link = hns_ae_adjust_link,
903 	.set_loopback = hns_ae_config_loopback,
904 	.get_ring_bdnum_limit = hns_ae_get_ring_bdnum_limit,
905 	.get_pauseparam = hns_ae_get_pauseparam,
906 	.set_autoneg = hns_ae_set_autoneg,
907 	.get_autoneg = hns_ae_get_autoneg,
908 	.set_pauseparam = hns_ae_set_pauseparam,
909 	.get_coalesce_usecs = hns_ae_get_coalesce_usecs,
910 	.get_max_coalesced_frames = hns_ae_get_max_coalesced_frames,
911 	.set_coalesce_usecs = hns_ae_set_coalesce_usecs,
912 	.set_coalesce_frames = hns_ae_set_coalesce_frames,
913 	.get_coalesce_range = hns_ae_get_coalesce_range,
914 	.set_promisc_mode = hns_ae_set_promisc_mode,
915 	.set_mac_addr = hns_ae_set_mac_address,
916 	.add_uc_addr = hns_ae_add_uc_address,
917 	.rm_uc_addr = hns_ae_rm_uc_address,
918 	.set_mc_addr = hns_ae_set_multicast_one,
919 	.clr_mc_addr = hns_ae_clr_multicast,
920 	.set_mtu = hns_ae_set_mtu,
921 	.update_stats = hns_ae_update_stats,
922 	.set_tso_stats = hns_ae_set_tso_stats,
923 	.get_stats = hns_ae_get_stats,
924 	.get_strings = hns_ae_get_strings,
925 	.get_sset_count = hns_ae_get_sset_count,
926 	.update_led_status = hns_ae_update_led_status,
927 	.set_led_id = hns_ae_cpld_set_led_id,
928 	.get_regs = hns_ae_get_regs,
929 	.get_regs_len = hns_ae_get_regs_len,
930 	.get_rss_key_size = hns_ae_get_rss_key_size,
931 	.get_rss_indir_size = hns_ae_get_rss_indir_size,
932 	.get_rss = hns_ae_get_rss,
933 	.set_rss = hns_ae_set_rss
934 };
935 
936 int hns_dsaf_ae_init(struct dsaf_device *dsaf_dev)
937 {
938 	struct hnae_ae_dev *ae_dev = &dsaf_dev->ae_dev;
939 	static atomic_t id = ATOMIC_INIT(-1);
940 
941 	switch (dsaf_dev->dsaf_ver) {
942 	case AE_VERSION_1:
943 		hns_dsaf_ops.toggle_ring_irq = hns_ae_toggle_ring_irq;
944 		break;
945 	case AE_VERSION_2:
946 		hns_dsaf_ops.toggle_ring_irq = hns_aev2_toggle_ring_irq;
947 		break;
948 	default:
949 		break;
950 	}
951 
952 	snprintf(ae_dev->name, AE_NAME_SIZE, "%s%d", DSAF_DEVICE_NAME,
953 		 (int)atomic_inc_return(&id));
954 	ae_dev->ops = &hns_dsaf_ops;
955 	ae_dev->dev = dsaf_dev->dev;
956 
957 	return hnae_ae_register(ae_dev, THIS_MODULE);
958 }
959 
960 void hns_dsaf_ae_uninit(struct dsaf_device *dsaf_dev)
961 {
962 	hnae_ae_unregister(&dsaf_dev->ae_dev);
963 }
964