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