1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014-2015 Hisilicon Limited.
6 #include <linux/etherdevice.h>
7 #include <linux/netdevice.h>
8 #include <linux/spinlock.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"
16 static struct hns_mac_cb
*hns_get_mac_cb(struct hnae_handle
*handle
)
18 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
23 static struct dsaf_device
*hns_ae_get_dsaf_dev(struct hnae_ae_dev
*dev
)
25 return container_of(dev
, struct dsaf_device
, ae_dev
);
28 static struct hns_ppe_cb
*hns_get_ppe_cb(struct hnae_handle
*handle
)
31 struct ppe_common_cb
*ppe_comm
;
32 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
34 ppe_comm
= vf_cb
->dsaf_dev
->ppe_common
[0];
35 ppe_index
= vf_cb
->port_index
;
37 return &ppe_comm
->ppe_cb
[ppe_index
];
40 static int hns_ae_get_q_num_per_vf(
41 struct dsaf_device
*dsaf_dev
, int port
)
43 return dsaf_dev
->rcb_common
[0]->max_q_per_vf
;
46 static int hns_ae_get_vf_num_per_port(
47 struct dsaf_device
*dsaf_dev
, int port
)
49 return dsaf_dev
->rcb_common
[0]->max_vfn
;
52 static struct ring_pair_cb
*hns_ae_get_base_ring_pair(
53 struct dsaf_device
*dsaf_dev
, int port
)
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
;
59 return &rcb_comm
->ring_pair_cb
[port
* q_num
* vf_num
];
62 static struct ring_pair_cb
*hns_ae_get_ring_pair(struct hnae_queue
*q
)
64 return container_of(q
, struct ring_pair_cb
, q
);
67 static struct hnae_handle
*hns_ae_get_handle(struct hnae_ae_dev
*dev
,
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
;
78 dsaf_dev
= hns_ae_get_dsaf_dev(dev
);
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
);
84 vf_cb
= kzalloc(sizeof(*vf_cb
) +
85 qnum_per_vf
* sizeof(struct hnae_queue
*), GFP_KERNEL
);
86 if (unlikely(!vf_cb
)) {
87 dev_err(dsaf_dev
->dev
, "malloc vf_cb fail!\n");
88 ae_handle
= ERR_PTR(-ENOMEM
);
91 ae_handle
= &vf_cb
->ae_handle
;
93 ae_handle
->owner_dev
= dsaf_dev
->dev
;
95 ae_handle
->q_num
= qnum_per_vf
;
96 ae_handle
->coal_param
= HNAE_LOWEST_LATENCY_COAL_PARAM
;
98 /* find ring pair, and set vf id*/
99 for (ae_handle
->vf_id
= 0;
100 ae_handle
->vf_id
< vfnum_per_port
; ae_handle
->vf_id
++) {
101 if (!ring_pair_cb
->used_by_vf
)
103 ring_pair_cb
+= qnum_per_vf
;
105 if (ae_handle
->vf_id
>= vfnum_per_port
) {
106 dev_err(dsaf_dev
->dev
, "malloc queue fail!\n");
107 ae_handle
= ERR_PTR(-EINVAL
);
111 ae_handle
->qs
= (struct hnae_queue
**)(&ae_handle
->qs
+ 1);
112 for (i
= 0; i
< qnum_per_vf
; i
++) {
113 ae_handle
->qs
[i
] = &ring_pair_cb
->q
;
114 ae_handle
->qs
[i
]->rx_ring
.q
= ae_handle
->qs
[i
];
115 ae_handle
->qs
[i
]->tx_ring
.q
= ae_handle
->qs
[i
];
117 ring_pair_cb
->used_by_vf
= 1;
121 vf_cb
->dsaf_dev
= dsaf_dev
;
122 vf_cb
->port_index
= port_id
;
123 vf_cb
->mac_cb
= dsaf_dev
->mac_cb
[port_id
];
125 ae_handle
->phy_if
= vf_cb
->mac_cb
->phy_if
;
126 ae_handle
->phy_dev
= vf_cb
->mac_cb
->phy_dev
;
127 ae_handle
->if_support
= vf_cb
->mac_cb
->if_support
;
128 ae_handle
->port_type
= vf_cb
->mac_cb
->mac_type
;
129 ae_handle
->media_type
= vf_cb
->mac_cb
->media_type
;
130 ae_handle
->dport_id
= port_id
;
139 static void hns_ae_put_handle(struct hnae_handle
*handle
)
141 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
144 for (i
= 0; i
< handle
->q_num
; i
++)
145 hns_ae_get_ring_pair(handle
->qs
[i
])->used_by_vf
= 0;
150 static int hns_ae_wait_flow_down(struct hnae_handle
*handle
)
152 struct dsaf_device
*dsaf_dev
;
153 struct hns_ppe_cb
*ppe_cb
;
154 struct hnae_vf_cb
*vf_cb
;
158 for (i
= 0; i
< handle
->q_num
; i
++) {
159 ret
= hns_rcb_wait_tx_ring_clean(handle
->qs
[i
]);
164 ppe_cb
= hns_get_ppe_cb(handle
);
165 ret
= hns_ppe_wait_tx_fifo_clean(ppe_cb
);
169 dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
172 ret
= hns_dsaf_wait_pkt_clean(dsaf_dev
, handle
->dport_id
);
176 vf_cb
= hns_ae_get_vf_cb(handle
);
177 ret
= hns_mac_wait_fifo_clean(vf_cb
->mac_cb
);
185 static void hns_ae_ring_enable_all(struct hnae_handle
*handle
, int val
)
187 int q_num
= handle
->q_num
;
190 for (i
= 0; i
< q_num
; i
++)
191 hns_rcb_ring_enable_hw(handle
->qs
[i
], val
);
194 static void hns_ae_init_queue(struct hnae_queue
*q
)
196 struct ring_pair_cb
*ring
=
197 container_of(q
, struct ring_pair_cb
, q
);
199 hns_rcb_init_hw(ring
);
202 static void hns_ae_fini_queue(struct hnae_queue
*q
)
204 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(q
->handle
);
206 if (vf_cb
->mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
207 hns_rcb_reset_ring_hw(q
);
210 static int hns_ae_set_mac_address(struct hnae_handle
*handle
, void *p
)
213 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
215 if (!p
|| !is_valid_ether_addr((const u8
*)p
)) {
216 dev_err(handle
->owner_dev
, "is not valid ether addr !\n");
217 return -EADDRNOTAVAIL
;
220 ret
= hns_mac_change_vf_addr(mac_cb
, handle
->vf_id
, p
);
222 dev_err(handle
->owner_dev
,
223 "set_mac_address fail, ret=%d!\n", ret
);
230 static int hns_ae_add_uc_address(struct hnae_handle
*handle
,
231 const unsigned char *addr
)
233 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
235 if (mac_cb
->mac_type
!= HNAE_PORT_SERVICE
)
238 return hns_mac_add_uc_addr(mac_cb
, handle
->vf_id
, addr
);
241 static int hns_ae_rm_uc_address(struct hnae_handle
*handle
,
242 const unsigned char *addr
)
244 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
246 if (mac_cb
->mac_type
!= HNAE_PORT_SERVICE
)
249 return hns_mac_rm_uc_addr(mac_cb
, handle
->vf_id
, addr
);
252 static int hns_ae_set_multicast_one(struct hnae_handle
*handle
, void *addr
)
255 char *mac_addr
= (char *)addr
;
256 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
261 if (mac_cb
->mac_type
!= HNAE_PORT_SERVICE
)
264 ret
= hns_mac_set_multi(mac_cb
, mac_cb
->mac_id
, mac_addr
, true);
266 dev_err(handle
->owner_dev
,
267 "mac add mul_mac:%pM port%d fail, ret = %#x!\n",
268 mac_addr
, mac_cb
->mac_id
, ret
);
272 ret
= hns_mac_get_inner_port_num(mac_cb
, handle
->vf_id
, &port_num
);
276 ret
= hns_mac_set_multi(mac_cb
, port_num
, mac_addr
, true);
278 dev_err(handle
->owner_dev
,
279 "mac add mul_mac:%pM port%d fail, ret = %#x!\n",
280 mac_addr
, DSAF_BASE_INNER_PORT_NUM
, ret
);
285 static int hns_ae_clr_multicast(struct hnae_handle
*handle
)
287 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
289 if (mac_cb
->mac_type
!= HNAE_PORT_SERVICE
)
292 return hns_mac_clr_multicast(mac_cb
, handle
->vf_id
);
295 static int hns_ae_set_mtu(struct hnae_handle
*handle
, int new_mtu
)
297 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
298 struct hnae_queue
*q
;
302 /* when buf_size is 2048, max mtu is 6K for rx ring max bd num is 3. */
303 if (!AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
)) {
304 if (new_mtu
<= BD_SIZE_2048_MAX_MTU
)
309 rx_buf_size
= mac_cb
->dsaf_dev
->buf_size
;
312 ret
= hns_mac_set_mtu(mac_cb
, new_mtu
, rx_buf_size
);
315 /* reinit ring buf_size */
316 for (i
= 0; i
< handle
->q_num
; i
++) {
318 q
->rx_ring
.buf_size
= rx_buf_size
;
319 hns_rcb_set_rx_ring_bs(q
, rx_buf_size
);
326 static void hns_ae_set_tso_stats(struct hnae_handle
*handle
, int enable
)
328 struct hns_ppe_cb
*ppe_cb
= hns_get_ppe_cb(handle
);
330 hns_ppe_set_tso_enable(ppe_cb
, enable
);
333 static int hns_ae_start(struct hnae_handle
*handle
)
337 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
339 ret
= hns_mac_vm_config_bc_en(mac_cb
, 0, true);
343 for (k
= 0; k
< handle
->q_num
; k
++) {
344 if (AE_IS_VER1(mac_cb
->dsaf_dev
->dsaf_ver
))
345 hns_rcb_int_clr_hw(handle
->qs
[k
],
346 RCB_INT_FLAG_TX
| RCB_INT_FLAG_RX
);
348 hns_rcbv2_int_clr_hw(handle
->qs
[k
],
349 RCB_INT_FLAG_TX
| RCB_INT_FLAG_RX
);
351 hns_ae_ring_enable_all(handle
, 1);
354 hns_mac_start(mac_cb
);
359 static void hns_ae_stop(struct hnae_handle
*handle
)
361 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
363 /* just clean tx fbd, neednot rx fbd*/
364 hns_rcb_wait_fbd_clean(handle
->qs
, handle
->q_num
, RCB_INT_FLAG_TX
);
368 hns_mac_stop(mac_cb
);
370 usleep_range(10000, 20000);
372 hns_ae_ring_enable_all(handle
, 0);
375 hns_rcb_wait_fbd_clean(handle
->qs
, handle
->q_num
, RCB_INT_FLAG_RX
);
377 (void)hns_mac_vm_config_bc_en(mac_cb
, 0, false);
380 static void hns_ae_reset(struct hnae_handle
*handle
)
382 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
384 if (vf_cb
->mac_cb
->mac_type
== HNAE_PORT_DEBUG
) {
385 hns_mac_reset(vf_cb
->mac_cb
);
386 hns_ppe_reset_common(vf_cb
->dsaf_dev
, 0);
390 static void hns_ae_toggle_ring_irq(struct hnae_ring
*ring
, u32 mask
)
394 if (is_tx_ring(ring
))
395 flag
= RCB_INT_FLAG_TX
;
397 flag
= RCB_INT_FLAG_RX
;
399 hns_rcb_int_ctrl_hw(ring
->q
, flag
, mask
);
402 static void hns_aev2_toggle_ring_irq(struct hnae_ring
*ring
, u32 mask
)
406 if (is_tx_ring(ring
))
407 flag
= RCB_INT_FLAG_TX
;
409 flag
= RCB_INT_FLAG_RX
;
411 hns_rcbv2_int_ctrl_hw(ring
->q
, flag
, mask
);
414 static int hns_ae_get_link_status(struct hnae_handle
*handle
)
417 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
419 hns_mac_get_link_status(mac_cb
, &link_status
);
421 return !!link_status
;
424 static int hns_ae_get_mac_info(struct hnae_handle
*handle
,
425 u8
*auto_neg
, u16
*speed
, u8
*duplex
)
427 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
429 return hns_mac_get_port_info(mac_cb
, auto_neg
, speed
, duplex
);
432 static bool hns_ae_need_adjust_link(struct hnae_handle
*handle
, int speed
,
435 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
437 return hns_mac_need_adjust_link(mac_cb
, speed
, duplex
);
440 static void hns_ae_adjust_link(struct hnae_handle
*handle
, int speed
,
443 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
445 switch (mac_cb
->dsaf_dev
->dsaf_ver
) {
447 hns_mac_adjust_link(mac_cb
, speed
, duplex
);
451 /* chip need to clear all pkt inside */
452 hns_mac_disable(mac_cb
, MAC_COMM_MODE_RX
);
453 if (hns_ae_wait_flow_down(handle
)) {
454 hns_mac_enable(mac_cb
, MAC_COMM_MODE_RX
);
458 hns_mac_adjust_link(mac_cb
, speed
, duplex
);
459 hns_mac_enable(mac_cb
, MAC_COMM_MODE_RX
);
469 static void hns_ae_get_ring_bdnum_limit(struct hnae_queue
*queue
,
472 *uplimit
= HNS_RCB_RING_MAX_PENDING_BD
;
475 static void hns_ae_get_pauseparam(struct hnae_handle
*handle
,
476 u32
*auto_neg
, u32
*rx_en
, u32
*tx_en
)
478 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
479 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
481 hns_mac_get_autoneg(mac_cb
, auto_neg
);
483 hns_mac_get_pauseparam(mac_cb
, rx_en
, tx_en
);
485 /* Service port's pause feature is provided by DSAF, not mac */
486 if (handle
->port_type
== HNAE_PORT_SERVICE
)
487 hns_dsaf_get_rx_mac_pause_en(dsaf_dev
, mac_cb
->mac_id
, rx_en
);
490 static int hns_ae_set_autoneg(struct hnae_handle
*handle
, u8 enable
)
494 return hns_mac_set_autoneg(hns_get_mac_cb(handle
), enable
);
497 static void hns_ae_set_promisc_mode(struct hnae_handle
*handle
, u32 en
)
499 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
501 hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle
->dev
), en
);
502 hns_mac_set_promisc(mac_cb
, (u8
)!!en
);
505 static int hns_ae_get_autoneg(struct hnae_handle
*handle
)
511 hns_mac_get_autoneg(hns_get_mac_cb(handle
), &auto_neg
);
516 static int hns_ae_set_pauseparam(struct hnae_handle
*handle
,
517 u32 autoneg
, u32 rx_en
, u32 tx_en
)
519 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
520 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
523 ret
= hns_mac_set_autoneg(mac_cb
, autoneg
);
527 /* Service port's pause feature is provided by DSAF, not mac */
528 if (handle
->port_type
== HNAE_PORT_SERVICE
) {
529 ret
= hns_dsaf_set_rx_mac_pause_en(dsaf_dev
,
530 mac_cb
->mac_id
, rx_en
);
535 return hns_mac_set_pauseparam(mac_cb
, rx_en
, tx_en
);
538 static void hns_ae_get_coalesce_usecs(struct hnae_handle
*handle
,
539 u32
*tx_usecs
, u32
*rx_usecs
)
541 struct ring_pair_cb
*ring_pair
=
542 container_of(handle
->qs
[0], struct ring_pair_cb
, q
);
544 *tx_usecs
= hns_rcb_get_coalesce_usecs(ring_pair
->rcb_common
,
545 ring_pair
->port_id_in_comm
);
546 *rx_usecs
= hns_rcb_get_coalesce_usecs(ring_pair
->rcb_common
,
547 ring_pair
->port_id_in_comm
);
550 static void hns_ae_get_max_coalesced_frames(struct hnae_handle
*handle
,
551 u32
*tx_frames
, u32
*rx_frames
)
553 struct ring_pair_cb
*ring_pair
=
554 container_of(handle
->qs
[0], struct ring_pair_cb
, q
);
555 struct dsaf_device
*dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
557 if (AE_IS_VER1(dsaf_dev
->dsaf_ver
) ||
558 handle
->port_type
== HNAE_PORT_DEBUG
)
559 *tx_frames
= hns_rcb_get_rx_coalesced_frames(
560 ring_pair
->rcb_common
, ring_pair
->port_id_in_comm
);
562 *tx_frames
= hns_rcb_get_tx_coalesced_frames(
563 ring_pair
->rcb_common
, ring_pair
->port_id_in_comm
);
564 *rx_frames
= hns_rcb_get_rx_coalesced_frames(ring_pair
->rcb_common
,
565 ring_pair
->port_id_in_comm
);
568 static int hns_ae_set_coalesce_usecs(struct hnae_handle
*handle
,
571 struct ring_pair_cb
*ring_pair
=
572 container_of(handle
->qs
[0], struct ring_pair_cb
, q
);
574 return hns_rcb_set_coalesce_usecs(
575 ring_pair
->rcb_common
, ring_pair
->port_id_in_comm
, timeout
);
578 static int hns_ae_set_coalesce_frames(struct hnae_handle
*handle
,
579 u32 tx_frames
, u32 rx_frames
)
582 struct ring_pair_cb
*ring_pair
=
583 container_of(handle
->qs
[0], struct ring_pair_cb
, q
);
584 struct dsaf_device
*dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
586 if (AE_IS_VER1(dsaf_dev
->dsaf_ver
) ||
587 handle
->port_type
== HNAE_PORT_DEBUG
) {
588 if (tx_frames
!= rx_frames
)
590 return hns_rcb_set_rx_coalesced_frames(
591 ring_pair
->rcb_common
,
592 ring_pair
->port_id_in_comm
, rx_frames
);
596 ret
= hns_rcb_set_tx_coalesced_frames(
597 ring_pair
->rcb_common
,
598 ring_pair
->port_id_in_comm
, tx_frames
);
602 return hns_rcb_set_rx_coalesced_frames(
603 ring_pair
->rcb_common
,
604 ring_pair
->port_id_in_comm
, rx_frames
);
608 static void hns_ae_get_coalesce_range(struct hnae_handle
*handle
,
609 u32
*tx_frames_low
, u32
*rx_frames_low
,
610 u32
*tx_frames_high
, u32
*rx_frames_high
,
611 u32
*tx_usecs_low
, u32
*rx_usecs_low
,
612 u32
*tx_usecs_high
, u32
*rx_usecs_high
)
614 struct dsaf_device
*dsaf_dev
;
618 dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
620 *tx_frames_low
= HNS_RCB_TX_FRAMES_LOW
;
621 *rx_frames_low
= HNS_RCB_RX_FRAMES_LOW
;
623 if (AE_IS_VER1(dsaf_dev
->dsaf_ver
) ||
624 handle
->port_type
== HNAE_PORT_DEBUG
)
626 (dsaf_dev
->desc_num
- 1 > HNS_RCB_TX_FRAMES_HIGH
) ?
627 HNS_RCB_TX_FRAMES_HIGH
: dsaf_dev
->desc_num
- 1;
631 *rx_frames_high
= (dsaf_dev
->desc_num
- 1 > HNS_RCB_RX_FRAMES_HIGH
) ?
632 HNS_RCB_RX_FRAMES_HIGH
: dsaf_dev
->desc_num
- 1;
633 *tx_usecs_low
= HNS_RCB_TX_USECS_LOW
;
634 *rx_usecs_low
= HNS_RCB_RX_USECS_LOW
;
635 *tx_usecs_high
= HNS_RCB_TX_USECS_HIGH
;
636 *rx_usecs_high
= HNS_RCB_RX_USECS_HIGH
;
639 static void hns_ae_update_stats(struct hnae_handle
*handle
,
640 struct net_device_stats
*net_stats
)
644 struct dsaf_device
*dsaf_dev
;
645 struct hns_mac_cb
*mac_cb
;
646 struct hns_ppe_cb
*ppe_cb
;
647 struct hnae_queue
*queue
;
648 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
649 u64 tx_bytes
= 0, rx_bytes
= 0, tx_packets
= 0, rx_packets
= 0;
650 u64 rx_errors
= 0, tx_errors
= 0, tx_dropped
= 0;
651 u64 rx_missed_errors
= 0;
653 dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
656 port
= vf_cb
->port_index
;
657 ppe_cb
= hns_get_ppe_cb(handle
);
658 mac_cb
= hns_get_mac_cb(handle
);
660 for (idx
= 0; idx
< handle
->q_num
; idx
++) {
661 queue
= handle
->qs
[idx
];
662 hns_rcb_update_stats(queue
);
664 tx_bytes
+= queue
->tx_ring
.stats
.tx_bytes
;
665 tx_packets
+= queue
->tx_ring
.stats
.tx_pkts
;
666 rx_bytes
+= queue
->rx_ring
.stats
.rx_bytes
;
667 rx_packets
+= queue
->rx_ring
.stats
.rx_pkts
;
669 rx_errors
+= queue
->rx_ring
.stats
.err_pkt_len
670 + queue
->rx_ring
.stats
.l2_err
671 + queue
->rx_ring
.stats
.l3l4_csum_err
;
674 hns_ppe_update_stats(ppe_cb
);
675 rx_missed_errors
= ppe_cb
->hw_stats
.rx_drop_no_buf
;
676 tx_errors
+= ppe_cb
->hw_stats
.tx_err_checksum
677 + ppe_cb
->hw_stats
.tx_err_fifo_empty
;
679 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
) {
680 hns_dsaf_update_stats(dsaf_dev
, port
);
681 /* for port upline direction, i.e., rx. */
682 rx_missed_errors
+= dsaf_dev
->hw_stats
[port
].bp_drop
;
683 rx_missed_errors
+= dsaf_dev
->hw_stats
[port
].pad_drop
;
684 rx_missed_errors
+= dsaf_dev
->hw_stats
[port
].crc_false
;
686 /* for port downline direction, i.e., tx. */
687 port
= port
+ DSAF_PPE_INODE_BASE
;
688 hns_dsaf_update_stats(dsaf_dev
, port
);
689 tx_dropped
+= dsaf_dev
->hw_stats
[port
].bp_drop
;
690 tx_dropped
+= dsaf_dev
->hw_stats
[port
].pad_drop
;
691 tx_dropped
+= dsaf_dev
->hw_stats
[port
].crc_false
;
692 tx_dropped
+= dsaf_dev
->hw_stats
[port
].rslt_drop
;
693 tx_dropped
+= dsaf_dev
->hw_stats
[port
].vlan_drop
;
694 tx_dropped
+= dsaf_dev
->hw_stats
[port
].stp_drop
;
697 hns_mac_update_stats(mac_cb
);
698 rx_errors
+= mac_cb
->hw_stats
.rx_fifo_overrun_err
;
700 tx_errors
+= mac_cb
->hw_stats
.tx_bad_pkts
701 + mac_cb
->hw_stats
.tx_fragment_err
702 + mac_cb
->hw_stats
.tx_jabber_err
703 + mac_cb
->hw_stats
.tx_underrun_err
704 + mac_cb
->hw_stats
.tx_crc_err
;
706 net_stats
->tx_bytes
= tx_bytes
;
707 net_stats
->tx_packets
= tx_packets
;
708 net_stats
->rx_bytes
= rx_bytes
;
709 net_stats
->rx_dropped
= 0;
710 net_stats
->rx_packets
= rx_packets
;
711 net_stats
->rx_errors
= rx_errors
;
712 net_stats
->tx_errors
= tx_errors
;
713 net_stats
->tx_dropped
= tx_dropped
;
714 net_stats
->rx_missed_errors
= rx_missed_errors
;
715 net_stats
->rx_crc_errors
= mac_cb
->hw_stats
.rx_fcs_err
;
716 net_stats
->rx_frame_errors
= mac_cb
->hw_stats
.rx_align_err
;
717 net_stats
->rx_fifo_errors
= mac_cb
->hw_stats
.rx_fifo_overrun_err
;
718 net_stats
->rx_length_errors
= mac_cb
->hw_stats
.rx_len_err
;
719 net_stats
->multicast
= mac_cb
->hw_stats
.rx_mc_pkts
;
722 static void hns_ae_get_stats(struct hnae_handle
*handle
, u64
*data
)
725 struct hns_mac_cb
*mac_cb
;
726 struct hns_ppe_cb
*ppe_cb
;
728 struct hnae_vf_cb
*vf_cb
;
730 if (!handle
|| !data
) {
731 pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
735 vf_cb
= hns_ae_get_vf_cb(handle
);
736 mac_cb
= hns_get_mac_cb(handle
);
737 ppe_cb
= hns_get_ppe_cb(handle
);
739 for (idx
= 0; idx
< handle
->q_num
; idx
++) {
740 hns_rcb_get_stats(handle
->qs
[idx
], p
);
741 p
+= hns_rcb_get_ring_sset_count((int)ETH_SS_STATS
);
744 hns_ppe_get_stats(ppe_cb
, p
);
745 p
+= hns_ppe_get_sset_count((int)ETH_SS_STATS
);
747 hns_mac_get_stats(mac_cb
, p
);
748 p
+= hns_mac_get_sset_count(mac_cb
, (int)ETH_SS_STATS
);
750 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
751 hns_dsaf_get_stats(vf_cb
->dsaf_dev
, p
, vf_cb
->port_index
);
754 static void hns_ae_get_strings(struct hnae_handle
*handle
,
755 u32 stringset
, u8
*data
)
759 struct hns_mac_cb
*mac_cb
;
760 struct hns_ppe_cb
*ppe_cb
;
761 struct dsaf_device
*dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
763 struct hnae_vf_cb
*vf_cb
;
767 vf_cb
= hns_ae_get_vf_cb(handle
);
768 port
= vf_cb
->port_index
;
769 mac_cb
= hns_get_mac_cb(handle
);
770 ppe_cb
= hns_get_ppe_cb(handle
);
772 for (idx
= 0; idx
< handle
->q_num
; idx
++) {
773 hns_rcb_get_strings(stringset
, p
, idx
);
774 p
+= ETH_GSTRING_LEN
* hns_rcb_get_ring_sset_count(stringset
);
777 hns_ppe_get_strings(ppe_cb
, stringset
, p
);
778 p
+= ETH_GSTRING_LEN
* hns_ppe_get_sset_count(stringset
);
780 hns_mac_get_strings(mac_cb
, stringset
, p
);
781 p
+= ETH_GSTRING_LEN
* hns_mac_get_sset_count(mac_cb
, stringset
);
783 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
784 hns_dsaf_get_strings(stringset
, p
, port
, dsaf_dev
);
787 static int hns_ae_get_sset_count(struct hnae_handle
*handle
, int stringset
)
790 struct hns_mac_cb
*mac_cb
;
791 struct dsaf_device
*dsaf_dev
= hns_ae_get_dsaf_dev(handle
->dev
);
795 mac_cb
= hns_get_mac_cb(handle
);
797 sset_count
+= hns_rcb_get_ring_sset_count(stringset
) * handle
->q_num
;
798 sset_count
+= hns_ppe_get_sset_count(stringset
);
799 sset_count
+= hns_mac_get_sset_count(mac_cb
, stringset
);
801 if (mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
802 sset_count
+= hns_dsaf_get_sset_count(dsaf_dev
, stringset
);
807 static int hns_ae_config_loopback(struct hnae_handle
*handle
,
808 enum hnae_loop loop
, int en
)
811 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
812 struct hns_mac_cb
*mac_cb
= hns_get_mac_cb(handle
);
813 struct dsaf_device
*dsaf_dev
= mac_cb
->dsaf_dev
;
816 case MAC_INTERNALLOOP_PHY
:
819 case MAC_INTERNALLOOP_SERDES
:
820 ret
= dsaf_dev
->misc_op
->cfg_serdes_loopback(vf_cb
->mac_cb
,
823 case MAC_INTERNALLOOP_MAC
:
824 ret
= hns_mac_config_mac_loopback(vf_cb
->mac_cb
, loop
, en
);
833 static void hns_ae_update_led_status(struct hnae_handle
*handle
)
835 struct hns_mac_cb
*mac_cb
;
838 mac_cb
= hns_get_mac_cb(handle
);
839 if (mac_cb
->media_type
!= HNAE_MEDIA_TYPE_FIBER
)
842 hns_set_led_opt(mac_cb
);
845 static int hns_ae_cpld_set_led_id(struct hnae_handle
*handle
,
846 enum hnae_led_state status
)
848 struct hns_mac_cb
*mac_cb
;
852 mac_cb
= hns_get_mac_cb(handle
);
854 return hns_cpld_led_set_id(mac_cb
, status
);
857 static void hns_ae_get_regs(struct hnae_handle
*handle
, void *data
)
861 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
862 struct hns_ppe_cb
*ppe_cb
= hns_get_ppe_cb(handle
);
864 hns_ppe_get_regs(ppe_cb
, p
);
865 p
+= hns_ppe_get_regs_count();
867 hns_rcb_get_common_regs(vf_cb
->dsaf_dev
->rcb_common
[0], p
);
868 p
+= hns_rcb_get_common_regs_count();
870 for (i
= 0; i
< handle
->q_num
; i
++) {
871 hns_rcb_get_ring_regs(handle
->qs
[i
], p
);
872 p
+= hns_rcb_get_ring_regs_count();
875 hns_mac_get_regs(vf_cb
->mac_cb
, p
);
876 p
+= hns_mac_get_regs_count(vf_cb
->mac_cb
);
878 if (vf_cb
->mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
879 hns_dsaf_get_regs(vf_cb
->dsaf_dev
, vf_cb
->port_index
, p
);
882 static int hns_ae_get_regs_len(struct hnae_handle
*handle
)
885 struct hnae_vf_cb
*vf_cb
= hns_ae_get_vf_cb(handle
);
887 total_num
= hns_ppe_get_regs_count();
888 total_num
+= hns_rcb_get_common_regs_count();
889 total_num
+= hns_rcb_get_ring_regs_count() * handle
->q_num
;
890 total_num
+= hns_mac_get_regs_count(vf_cb
->mac_cb
);
892 if (vf_cb
->mac_cb
->mac_type
== HNAE_PORT_SERVICE
)
893 total_num
+= hns_dsaf_get_regs_count();
898 static u32
hns_ae_get_rss_key_size(struct hnae_handle
*handle
)
900 return HNS_PPEV2_RSS_KEY_SIZE
;
903 static u32
hns_ae_get_rss_indir_size(struct hnae_handle
*handle
)
905 return HNS_PPEV2_RSS_IND_TBL_SIZE
;
908 static int hns_ae_get_rss(struct hnae_handle
*handle
, u32
*indir
, u8
*key
,
911 struct hns_ppe_cb
*ppe_cb
= hns_get_ppe_cb(handle
);
913 /* currently we support only one type of hash function i.e. Toep hash */
915 *hfunc
= ETH_RSS_HASH_TOP
;
917 /* get the RSS Key required by the user */
919 memcpy(key
, ppe_cb
->rss_key
, HNS_PPEV2_RSS_KEY_SIZE
);
921 /* update the current hash->queue mappings from the shadow RSS table */
923 memcpy(indir
, ppe_cb
->rss_indir_table
,
924 HNS_PPEV2_RSS_IND_TBL_SIZE
* sizeof(*indir
));
929 static int hns_ae_set_rss(struct hnae_handle
*handle
, const u32
*indir
,
930 const u8
*key
, const u8 hfunc
)
932 struct hns_ppe_cb
*ppe_cb
= hns_get_ppe_cb(handle
);
934 /* set the RSS Hash Key if specififed by the user */
936 memcpy(ppe_cb
->rss_key
, key
, HNS_PPEV2_RSS_KEY_SIZE
);
937 hns_ppe_set_rss_key(ppe_cb
, ppe_cb
->rss_key
);
941 /* update the shadow RSS table with user specified qids */
942 memcpy(ppe_cb
->rss_indir_table
, indir
,
943 HNS_PPEV2_RSS_IND_TBL_SIZE
* sizeof(*indir
));
945 /* now update the hardware */
946 hns_ppe_set_indir_table(ppe_cb
, ppe_cb
->rss_indir_table
);
952 static struct hnae_ae_ops hns_dsaf_ops
= {
953 .get_handle
= hns_ae_get_handle
,
954 .put_handle
= hns_ae_put_handle
,
955 .init_queue
= hns_ae_init_queue
,
956 .fini_queue
= hns_ae_fini_queue
,
957 .start
= hns_ae_start
,
959 .reset
= hns_ae_reset
,
960 .toggle_ring_irq
= hns_ae_toggle_ring_irq
,
961 .get_status
= hns_ae_get_link_status
,
962 .get_info
= hns_ae_get_mac_info
,
963 .adjust_link
= hns_ae_adjust_link
,
964 .need_adjust_link
= hns_ae_need_adjust_link
,
965 .set_loopback
= hns_ae_config_loopback
,
966 .get_ring_bdnum_limit
= hns_ae_get_ring_bdnum_limit
,
967 .get_pauseparam
= hns_ae_get_pauseparam
,
968 .set_autoneg
= hns_ae_set_autoneg
,
969 .get_autoneg
= hns_ae_get_autoneg
,
970 .set_pauseparam
= hns_ae_set_pauseparam
,
971 .get_coalesce_usecs
= hns_ae_get_coalesce_usecs
,
972 .get_max_coalesced_frames
= hns_ae_get_max_coalesced_frames
,
973 .set_coalesce_usecs
= hns_ae_set_coalesce_usecs
,
974 .set_coalesce_frames
= hns_ae_set_coalesce_frames
,
975 .get_coalesce_range
= hns_ae_get_coalesce_range
,
976 .set_promisc_mode
= hns_ae_set_promisc_mode
,
977 .set_mac_addr
= hns_ae_set_mac_address
,
978 .add_uc_addr
= hns_ae_add_uc_address
,
979 .rm_uc_addr
= hns_ae_rm_uc_address
,
980 .set_mc_addr
= hns_ae_set_multicast_one
,
981 .clr_mc_addr
= hns_ae_clr_multicast
,
982 .set_mtu
= hns_ae_set_mtu
,
983 .update_stats
= hns_ae_update_stats
,
984 .set_tso_stats
= hns_ae_set_tso_stats
,
985 .get_stats
= hns_ae_get_stats
,
986 .get_strings
= hns_ae_get_strings
,
987 .get_sset_count
= hns_ae_get_sset_count
,
988 .update_led_status
= hns_ae_update_led_status
,
989 .set_led_id
= hns_ae_cpld_set_led_id
,
990 .get_regs
= hns_ae_get_regs
,
991 .get_regs_len
= hns_ae_get_regs_len
,
992 .get_rss_key_size
= hns_ae_get_rss_key_size
,
993 .get_rss_indir_size
= hns_ae_get_rss_indir_size
,
994 .get_rss
= hns_ae_get_rss
,
995 .set_rss
= hns_ae_set_rss
998 int hns_dsaf_ae_init(struct dsaf_device
*dsaf_dev
)
1000 struct hnae_ae_dev
*ae_dev
= &dsaf_dev
->ae_dev
;
1001 static atomic_t id
= ATOMIC_INIT(-1);
1003 switch (dsaf_dev
->dsaf_ver
) {
1005 hns_dsaf_ops
.toggle_ring_irq
= hns_ae_toggle_ring_irq
;
1008 hns_dsaf_ops
.toggle_ring_irq
= hns_aev2_toggle_ring_irq
;
1014 snprintf(ae_dev
->name
, AE_NAME_SIZE
, "%s%d", DSAF_DEVICE_NAME
,
1015 (int)atomic_inc_return(&id
));
1016 ae_dev
->ops
= &hns_dsaf_ops
;
1017 ae_dev
->dev
= dsaf_dev
->dev
;
1019 return hnae_ae_register(ae_dev
, THIS_MODULE
);
1022 void hns_dsaf_ae_uninit(struct dsaf_device
*dsaf_dev
)
1024 hnae_ae_unregister(&dsaf_dev
->ae_dev
);