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