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 	return;
467 }
468 
469 static void hns_ae_get_ring_bdnum_limit(struct hnae_queue *queue,
470 					u32 *uplimit)
471 {
472 	*uplimit = HNS_RCB_RING_MAX_PENDING_BD;
473 }
474 
475 static void hns_ae_get_pauseparam(struct hnae_handle *handle,
476 				  u32 *auto_neg, u32 *rx_en, u32 *tx_en)
477 {
478 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
479 	struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
480 
481 	hns_mac_get_autoneg(mac_cb, auto_neg);
482 
483 	hns_mac_get_pauseparam(mac_cb, rx_en, tx_en);
484 
485 	/* Service port's pause feature is provided by DSAF, not mac */
486 	if (handle->port_type == HNAE_PORT_SERVICE)
487 		hns_dsaf_get_rx_mac_pause_en(dsaf_dev, mac_cb->mac_id, rx_en);
488 }
489 
490 static int hns_ae_set_autoneg(struct hnae_handle *handle, u8 enable)
491 {
492 	assert(handle);
493 
494 	return hns_mac_set_autoneg(hns_get_mac_cb(handle), enable);
495 }
496 
497 static void hns_ae_set_promisc_mode(struct hnae_handle *handle, u32 en)
498 {
499 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
500 
501 	hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle->dev), en);
502 	hns_mac_set_promisc(mac_cb, (u8)!!en);
503 }
504 
505 static int hns_ae_get_autoneg(struct hnae_handle *handle)
506 {
507 	u32     auto_neg;
508 
509 	assert(handle);
510 
511 	hns_mac_get_autoneg(hns_get_mac_cb(handle), &auto_neg);
512 
513 	return auto_neg;
514 }
515 
516 static int hns_ae_set_pauseparam(struct hnae_handle *handle,
517 				 u32 autoneg, u32 rx_en, u32 tx_en)
518 {
519 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
520 	struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
521 	int ret;
522 
523 	ret = hns_mac_set_autoneg(mac_cb, autoneg);
524 	if (ret)
525 		return ret;
526 
527 	/* Service port's pause feature is provided by DSAF, not mac */
528 	if (handle->port_type == HNAE_PORT_SERVICE) {
529 		ret = hns_dsaf_set_rx_mac_pause_en(dsaf_dev,
530 						   mac_cb->mac_id, rx_en);
531 		if (ret)
532 			return ret;
533 		rx_en = 0;
534 	}
535 	return hns_mac_set_pauseparam(mac_cb, rx_en, tx_en);
536 }
537 
538 static void hns_ae_get_coalesce_usecs(struct hnae_handle *handle,
539 				      u32 *tx_usecs, u32 *rx_usecs)
540 {
541 	struct ring_pair_cb *ring_pair =
542 		container_of(handle->qs[0], struct ring_pair_cb, q);
543 
544 	*tx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
545 					       ring_pair->port_id_in_comm);
546 	*rx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
547 					       ring_pair->port_id_in_comm);
548 }
549 
550 static void hns_ae_get_max_coalesced_frames(struct hnae_handle *handle,
551 					    u32 *tx_frames, u32 *rx_frames)
552 {
553 	struct ring_pair_cb *ring_pair =
554 		container_of(handle->qs[0], struct ring_pair_cb, q);
555 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
556 
557 	if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
558 	    handle->port_type == HNAE_PORT_DEBUG)
559 		*tx_frames = hns_rcb_get_rx_coalesced_frames(
560 			ring_pair->rcb_common, ring_pair->port_id_in_comm);
561 	else
562 		*tx_frames = hns_rcb_get_tx_coalesced_frames(
563 			ring_pair->rcb_common, ring_pair->port_id_in_comm);
564 	*rx_frames = hns_rcb_get_rx_coalesced_frames(ring_pair->rcb_common,
565 						  ring_pair->port_id_in_comm);
566 }
567 
568 static int hns_ae_set_coalesce_usecs(struct hnae_handle *handle,
569 				     u32 timeout)
570 {
571 	struct ring_pair_cb *ring_pair =
572 		container_of(handle->qs[0], struct ring_pair_cb, q);
573 
574 	return hns_rcb_set_coalesce_usecs(
575 		ring_pair->rcb_common, ring_pair->port_id_in_comm, timeout);
576 }
577 
578 static int hns_ae_set_coalesce_frames(struct hnae_handle *handle,
579 				      u32 tx_frames, u32 rx_frames)
580 {
581 	int ret;
582 	struct ring_pair_cb *ring_pair =
583 		container_of(handle->qs[0], struct ring_pair_cb, q);
584 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
585 
586 	if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
587 	    handle->port_type == HNAE_PORT_DEBUG) {
588 		if (tx_frames != rx_frames)
589 			return -EINVAL;
590 		return hns_rcb_set_rx_coalesced_frames(
591 			ring_pair->rcb_common,
592 			ring_pair->port_id_in_comm, rx_frames);
593 	} else {
594 		if (tx_frames != 1)
595 			return -EINVAL;
596 		ret = hns_rcb_set_tx_coalesced_frames(
597 			ring_pair->rcb_common,
598 			ring_pair->port_id_in_comm, tx_frames);
599 		if (ret)
600 			return ret;
601 
602 		return hns_rcb_set_rx_coalesced_frames(
603 			ring_pair->rcb_common,
604 			ring_pair->port_id_in_comm, rx_frames);
605 	}
606 }
607 
608 static void hns_ae_get_coalesce_range(struct hnae_handle *handle,
609 				      u32 *tx_frames_low, u32 *rx_frames_low,
610 				      u32 *tx_frames_high, u32 *rx_frames_high,
611 				      u32 *tx_usecs_low, u32 *rx_usecs_low,
612 				      u32 *tx_usecs_high, u32 *rx_usecs_high)
613 {
614 	struct dsaf_device *dsaf_dev;
615 
616 	assert(handle);
617 
618 	dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
619 
620 	*tx_frames_low  = HNS_RCB_TX_FRAMES_LOW;
621 	*rx_frames_low  = HNS_RCB_RX_FRAMES_LOW;
622 
623 	if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
624 	    handle->port_type == HNAE_PORT_DEBUG)
625 		*tx_frames_high =
626 			(dsaf_dev->desc_num - 1 > HNS_RCB_TX_FRAMES_HIGH) ?
627 			HNS_RCB_TX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
628 	else
629 		*tx_frames_high = 1;
630 
631 	*rx_frames_high = (dsaf_dev->desc_num - 1 > HNS_RCB_RX_FRAMES_HIGH) ?
632 		HNS_RCB_RX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
633 	*tx_usecs_low   = HNS_RCB_TX_USECS_LOW;
634 	*rx_usecs_low   = HNS_RCB_RX_USECS_LOW;
635 	*tx_usecs_high  = HNS_RCB_TX_USECS_HIGH;
636 	*rx_usecs_high  = HNS_RCB_RX_USECS_HIGH;
637 }
638 
639 static void hns_ae_update_stats(struct hnae_handle *handle,
640 				struct net_device_stats *net_stats)
641 {
642 	int port;
643 	int idx;
644 	struct dsaf_device *dsaf_dev;
645 	struct hns_mac_cb *mac_cb;
646 	struct hns_ppe_cb *ppe_cb;
647 	struct hnae_queue *queue;
648 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
649 	u64 tx_bytes = 0, rx_bytes = 0, tx_packets = 0, rx_packets = 0;
650 	u64 rx_errors = 0, tx_errors = 0, tx_dropped = 0;
651 	u64 rx_missed_errors = 0;
652 
653 	dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
654 	if (!dsaf_dev)
655 		return;
656 	port = vf_cb->port_index;
657 	ppe_cb = hns_get_ppe_cb(handle);
658 	mac_cb = hns_get_mac_cb(handle);
659 
660 	for (idx = 0; idx < handle->q_num; idx++) {
661 		queue = handle->qs[idx];
662 		hns_rcb_update_stats(queue);
663 
664 		tx_bytes += queue->tx_ring.stats.tx_bytes;
665 		tx_packets += queue->tx_ring.stats.tx_pkts;
666 		rx_bytes += queue->rx_ring.stats.rx_bytes;
667 		rx_packets += queue->rx_ring.stats.rx_pkts;
668 
669 		rx_errors += queue->rx_ring.stats.err_pkt_len
670 				+ queue->rx_ring.stats.l2_err
671 				+ queue->rx_ring.stats.l3l4_csum_err;
672 	}
673 
674 	hns_ppe_update_stats(ppe_cb);
675 	rx_missed_errors = ppe_cb->hw_stats.rx_drop_no_buf;
676 	tx_errors += ppe_cb->hw_stats.tx_err_checksum
677 		+ ppe_cb->hw_stats.tx_err_fifo_empty;
678 
679 	if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
680 		hns_dsaf_update_stats(dsaf_dev, port);
681 		/* for port upline direction, i.e., rx. */
682 		rx_missed_errors += dsaf_dev->hw_stats[port].bp_drop;
683 		rx_missed_errors += dsaf_dev->hw_stats[port].pad_drop;
684 		rx_missed_errors += dsaf_dev->hw_stats[port].crc_false;
685 
686 		/* for port downline direction, i.e., tx. */
687 		port = port + DSAF_PPE_INODE_BASE;
688 		hns_dsaf_update_stats(dsaf_dev, port);
689 		tx_dropped += dsaf_dev->hw_stats[port].bp_drop;
690 		tx_dropped += dsaf_dev->hw_stats[port].pad_drop;
691 		tx_dropped += dsaf_dev->hw_stats[port].crc_false;
692 		tx_dropped += dsaf_dev->hw_stats[port].rslt_drop;
693 		tx_dropped += dsaf_dev->hw_stats[port].vlan_drop;
694 		tx_dropped += dsaf_dev->hw_stats[port].stp_drop;
695 	}
696 
697 	hns_mac_update_stats(mac_cb);
698 	rx_errors += mac_cb->hw_stats.rx_fifo_overrun_err;
699 
700 	tx_errors += mac_cb->hw_stats.tx_bad_pkts
701 		+ mac_cb->hw_stats.tx_fragment_err
702 		+ mac_cb->hw_stats.tx_jabber_err
703 		+ mac_cb->hw_stats.tx_underrun_err
704 		+ mac_cb->hw_stats.tx_crc_err;
705 
706 	net_stats->tx_bytes = tx_bytes;
707 	net_stats->tx_packets = tx_packets;
708 	net_stats->rx_bytes = rx_bytes;
709 	net_stats->rx_dropped = 0;
710 	net_stats->rx_packets = rx_packets;
711 	net_stats->rx_errors = rx_errors;
712 	net_stats->tx_errors = tx_errors;
713 	net_stats->tx_dropped = tx_dropped;
714 	net_stats->rx_missed_errors = rx_missed_errors;
715 	net_stats->rx_crc_errors = mac_cb->hw_stats.rx_fcs_err;
716 	net_stats->rx_frame_errors = mac_cb->hw_stats.rx_align_err;
717 	net_stats->rx_fifo_errors = mac_cb->hw_stats.rx_fifo_overrun_err;
718 	net_stats->rx_length_errors = mac_cb->hw_stats.rx_len_err;
719 	net_stats->multicast = mac_cb->hw_stats.rx_mc_pkts;
720 }
721 
722 static void hns_ae_get_stats(struct hnae_handle *handle, u64 *data)
723 {
724 	int idx;
725 	struct hns_mac_cb *mac_cb;
726 	struct hns_ppe_cb *ppe_cb;
727 	u64 *p = data;
728 	struct  hnae_vf_cb *vf_cb;
729 
730 	if (!handle || !data) {
731 		pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
732 		return;
733 	}
734 
735 	vf_cb = hns_ae_get_vf_cb(handle);
736 	mac_cb = hns_get_mac_cb(handle);
737 	ppe_cb = hns_get_ppe_cb(handle);
738 
739 	for (idx = 0; idx < handle->q_num; idx++) {
740 		hns_rcb_get_stats(handle->qs[idx], p);
741 		p += hns_rcb_get_ring_sset_count((int)ETH_SS_STATS);
742 	}
743 
744 	hns_ppe_get_stats(ppe_cb, p);
745 	p += hns_ppe_get_sset_count((int)ETH_SS_STATS);
746 
747 	hns_mac_get_stats(mac_cb, p);
748 	p += hns_mac_get_sset_count(mac_cb, (int)ETH_SS_STATS);
749 
750 	if (mac_cb->mac_type == HNAE_PORT_SERVICE)
751 		hns_dsaf_get_stats(vf_cb->dsaf_dev, p, vf_cb->port_index);
752 }
753 
754 static void hns_ae_get_strings(struct hnae_handle *handle,
755 			       u32 stringset, u8 *data)
756 {
757 	int port;
758 	int idx;
759 	struct hns_mac_cb *mac_cb;
760 	struct hns_ppe_cb *ppe_cb;
761 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
762 	u8 *p = data;
763 	struct	hnae_vf_cb *vf_cb;
764 
765 	assert(handle);
766 
767 	vf_cb = hns_ae_get_vf_cb(handle);
768 	port = vf_cb->port_index;
769 	mac_cb = hns_get_mac_cb(handle);
770 	ppe_cb = hns_get_ppe_cb(handle);
771 
772 	for (idx = 0; idx < handle->q_num; idx++) {
773 		hns_rcb_get_strings(stringset, p, idx);
774 		p += ETH_GSTRING_LEN * hns_rcb_get_ring_sset_count(stringset);
775 	}
776 
777 	hns_ppe_get_strings(ppe_cb, stringset, p);
778 	p += ETH_GSTRING_LEN * hns_ppe_get_sset_count(stringset);
779 
780 	hns_mac_get_strings(mac_cb, stringset, p);
781 	p += ETH_GSTRING_LEN * hns_mac_get_sset_count(mac_cb, stringset);
782 
783 	if (mac_cb->mac_type == HNAE_PORT_SERVICE)
784 		hns_dsaf_get_strings(stringset, p, port, dsaf_dev);
785 }
786 
787 static int hns_ae_get_sset_count(struct hnae_handle *handle, int stringset)
788 {
789 	u32 sset_count = 0;
790 	struct hns_mac_cb *mac_cb;
791 	struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
792 
793 	assert(handle);
794 
795 	mac_cb = hns_get_mac_cb(handle);
796 
797 	sset_count += hns_rcb_get_ring_sset_count(stringset) * handle->q_num;
798 	sset_count += hns_ppe_get_sset_count(stringset);
799 	sset_count += hns_mac_get_sset_count(mac_cb, stringset);
800 
801 	if (mac_cb->mac_type == HNAE_PORT_SERVICE)
802 		sset_count += hns_dsaf_get_sset_count(dsaf_dev, stringset);
803 
804 	return sset_count;
805 }
806 
807 static int hns_ae_config_loopback(struct hnae_handle *handle,
808 				  enum hnae_loop loop, int en)
809 {
810 	int ret;
811 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
812 	struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
813 	struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
814 
815 	switch (loop) {
816 	case MAC_INTERNALLOOP_PHY:
817 		ret = 0;
818 		break;
819 	case MAC_INTERNALLOOP_SERDES:
820 		ret = dsaf_dev->misc_op->cfg_serdes_loopback(vf_cb->mac_cb,
821 							     !!en);
822 		break;
823 	case MAC_INTERNALLOOP_MAC:
824 		ret = hns_mac_config_mac_loopback(vf_cb->mac_cb, loop, en);
825 		break;
826 	default:
827 		ret = -EINVAL;
828 	}
829 
830 	return ret;
831 }
832 
833 static void hns_ae_update_led_status(struct hnae_handle *handle)
834 {
835 	struct hns_mac_cb *mac_cb;
836 
837 	assert(handle);
838 	mac_cb = hns_get_mac_cb(handle);
839 	if (mac_cb->media_type != HNAE_MEDIA_TYPE_FIBER)
840 		return;
841 
842 	hns_set_led_opt(mac_cb);
843 }
844 
845 static int hns_ae_cpld_set_led_id(struct hnae_handle *handle,
846 				  enum hnae_led_state status)
847 {
848 	struct hns_mac_cb *mac_cb;
849 
850 	assert(handle);
851 
852 	mac_cb = hns_get_mac_cb(handle);
853 
854 	return hns_cpld_led_set_id(mac_cb, status);
855 }
856 
857 static void hns_ae_get_regs(struct hnae_handle *handle, void *data)
858 {
859 	u32 *p = data;
860 	int i;
861 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
862 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
863 
864 	hns_ppe_get_regs(ppe_cb, p);
865 	p += hns_ppe_get_regs_count();
866 
867 	hns_rcb_get_common_regs(vf_cb->dsaf_dev->rcb_common[0], p);
868 	p += hns_rcb_get_common_regs_count();
869 
870 	for (i = 0; i < handle->q_num; i++) {
871 		hns_rcb_get_ring_regs(handle->qs[i], p);
872 		p += hns_rcb_get_ring_regs_count();
873 	}
874 
875 	hns_mac_get_regs(vf_cb->mac_cb, p);
876 	p += hns_mac_get_regs_count(vf_cb->mac_cb);
877 
878 	if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
879 		hns_dsaf_get_regs(vf_cb->dsaf_dev, vf_cb->port_index, p);
880 }
881 
882 static int hns_ae_get_regs_len(struct hnae_handle *handle)
883 {
884 	u32 total_num;
885 	struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
886 
887 	total_num = hns_ppe_get_regs_count();
888 	total_num += hns_rcb_get_common_regs_count();
889 	total_num += hns_rcb_get_ring_regs_count() * handle->q_num;
890 	total_num += hns_mac_get_regs_count(vf_cb->mac_cb);
891 
892 	if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
893 		total_num += hns_dsaf_get_regs_count();
894 
895 	return total_num;
896 }
897 
898 static u32 hns_ae_get_rss_key_size(struct hnae_handle *handle)
899 {
900 	return HNS_PPEV2_RSS_KEY_SIZE;
901 }
902 
903 static u32 hns_ae_get_rss_indir_size(struct hnae_handle *handle)
904 {
905 	return HNS_PPEV2_RSS_IND_TBL_SIZE;
906 }
907 
908 static int hns_ae_get_rss(struct hnae_handle *handle, u32 *indir, u8 *key,
909 			  u8 *hfunc)
910 {
911 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
912 
913 	/* currently we support only one type of hash function i.e. Toep hash */
914 	if (hfunc)
915 		*hfunc = ETH_RSS_HASH_TOP;
916 
917 	/* get the RSS Key required by the user */
918 	if (key)
919 		memcpy(key, ppe_cb->rss_key, HNS_PPEV2_RSS_KEY_SIZE);
920 
921 	/* update the current hash->queue mappings from the shadow RSS table */
922 	if (indir)
923 		memcpy(indir, ppe_cb->rss_indir_table,
924 		       HNS_PPEV2_RSS_IND_TBL_SIZE  * sizeof(*indir));
925 
926 	return 0;
927 }
928 
929 static int hns_ae_set_rss(struct hnae_handle *handle, const u32 *indir,
930 			  const u8 *key, const u8 hfunc)
931 {
932 	struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
933 
934 	/* set the RSS Hash Key if specififed by the user */
935 	if (key) {
936 		memcpy(ppe_cb->rss_key, key, HNS_PPEV2_RSS_KEY_SIZE);
937 		hns_ppe_set_rss_key(ppe_cb, ppe_cb->rss_key);
938 	}
939 
940 	if (indir) {
941 		/* update the shadow RSS table with user specified qids */
942 		memcpy(ppe_cb->rss_indir_table, indir,
943 		       HNS_PPEV2_RSS_IND_TBL_SIZE  * sizeof(*indir));
944 
945 		/* now update the hardware */
946 		hns_ppe_set_indir_table(ppe_cb, ppe_cb->rss_indir_table);
947 	}
948 
949 	return 0;
950 }
951 
952 static struct hnae_ae_ops hns_dsaf_ops = {
953 	.get_handle = hns_ae_get_handle,
954 	.put_handle = hns_ae_put_handle,
955 	.init_queue = hns_ae_init_queue,
956 	.fini_queue = hns_ae_fini_queue,
957 	.start = hns_ae_start,
958 	.stop = hns_ae_stop,
959 	.reset = hns_ae_reset,
960 	.toggle_ring_irq = hns_ae_toggle_ring_irq,
961 	.get_status = hns_ae_get_link_status,
962 	.get_info = hns_ae_get_mac_info,
963 	.adjust_link = hns_ae_adjust_link,
964 	.need_adjust_link = hns_ae_need_adjust_link,
965 	.set_loopback = hns_ae_config_loopback,
966 	.get_ring_bdnum_limit = hns_ae_get_ring_bdnum_limit,
967 	.get_pauseparam = hns_ae_get_pauseparam,
968 	.set_autoneg = hns_ae_set_autoneg,
969 	.get_autoneg = hns_ae_get_autoneg,
970 	.set_pauseparam = hns_ae_set_pauseparam,
971 	.get_coalesce_usecs = hns_ae_get_coalesce_usecs,
972 	.get_max_coalesced_frames = hns_ae_get_max_coalesced_frames,
973 	.set_coalesce_usecs = hns_ae_set_coalesce_usecs,
974 	.set_coalesce_frames = hns_ae_set_coalesce_frames,
975 	.get_coalesce_range = hns_ae_get_coalesce_range,
976 	.set_promisc_mode = hns_ae_set_promisc_mode,
977 	.set_mac_addr = hns_ae_set_mac_address,
978 	.add_uc_addr = hns_ae_add_uc_address,
979 	.rm_uc_addr = hns_ae_rm_uc_address,
980 	.set_mc_addr = hns_ae_set_multicast_one,
981 	.clr_mc_addr = hns_ae_clr_multicast,
982 	.set_mtu = hns_ae_set_mtu,
983 	.update_stats = hns_ae_update_stats,
984 	.set_tso_stats = hns_ae_set_tso_stats,
985 	.get_stats = hns_ae_get_stats,
986 	.get_strings = hns_ae_get_strings,
987 	.get_sset_count = hns_ae_get_sset_count,
988 	.update_led_status = hns_ae_update_led_status,
989 	.set_led_id = hns_ae_cpld_set_led_id,
990 	.get_regs = hns_ae_get_regs,
991 	.get_regs_len = hns_ae_get_regs_len,
992 	.get_rss_key_size = hns_ae_get_rss_key_size,
993 	.get_rss_indir_size = hns_ae_get_rss_indir_size,
994 	.get_rss = hns_ae_get_rss,
995 	.set_rss = hns_ae_set_rss
996 };
997 
998 int hns_dsaf_ae_init(struct dsaf_device *dsaf_dev)
999 {
1000 	struct hnae_ae_dev *ae_dev = &dsaf_dev->ae_dev;
1001 	static atomic_t id = ATOMIC_INIT(-1);
1002 
1003 	switch (dsaf_dev->dsaf_ver) {
1004 	case AE_VERSION_1:
1005 		hns_dsaf_ops.toggle_ring_irq = hns_ae_toggle_ring_irq;
1006 		break;
1007 	case AE_VERSION_2:
1008 		hns_dsaf_ops.toggle_ring_irq = hns_aev2_toggle_ring_irq;
1009 		break;
1010 	default:
1011 		break;
1012 	}
1013 
1014 	snprintf(ae_dev->name, AE_NAME_SIZE, "%s%d", DSAF_DEVICE_NAME,
1015 		 (int)atomic_inc_return(&id));
1016 	ae_dev->ops = &hns_dsaf_ops;
1017 	ae_dev->dev = dsaf_dev->dev;
1018 
1019 	return hnae_ae_register(ae_dev, THIS_MODULE);
1020 }
1021 
1022 void hns_dsaf_ae_uninit(struct dsaf_device *dsaf_dev)
1023 {
1024 	hnae_ae_unregister(&dsaf_dev->ae_dev);
1025 }
1026