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