OSDN Git Service

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