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