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