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