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