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