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