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