1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/pci.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/slab.h>
17 #include <linux/if_vlan.h>
18 #include <linux/semaphore.h>
19 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/bitmap.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
26 #include "hinic_hw_qp.h"
27 #include "hinic_hw_dev.h"
28 #include "hinic_port.h"
31 #include "hinic_dev.h"
33 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
34 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
35 MODULE_LICENSE("GPL");
37 static unsigned int tx_weight
= 64;
38 module_param(tx_weight
, uint
, 0644);
39 MODULE_PARM_DESC(tx_weight
, "Number Tx packets for NAPI budget (default=64)");
41 static unsigned int rx_weight
= 64;
42 module_param(rx_weight
, uint
, 0644);
43 MODULE_PARM_DESC(rx_weight
, "Number Rx packets for NAPI budget (default=64)");
45 #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
46 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200
47 #define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205
48 #define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210
50 #define HINIC_WQ_NAME "hinic_dev"
52 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
54 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
56 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
58 #define work_to_rx_mode_work(work) \
59 container_of(work, struct hinic_rx_mode_work, work)
61 #define rx_mode_work_to_nic_dev(rx_mode_work) \
62 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
64 static int change_mac_addr(struct net_device
*netdev
, const u8
*addr
);
66 static void set_link_speed(struct ethtool_link_ksettings
*link_ksettings
,
67 enum hinic_speed speed
)
70 case HINIC_SPEED_10MB_LINK
:
71 link_ksettings
->base
.speed
= SPEED_10
;
74 case HINIC_SPEED_100MB_LINK
:
75 link_ksettings
->base
.speed
= SPEED_100
;
78 case HINIC_SPEED_1000MB_LINK
:
79 link_ksettings
->base
.speed
= SPEED_1000
;
82 case HINIC_SPEED_10GB_LINK
:
83 link_ksettings
->base
.speed
= SPEED_10000
;
86 case HINIC_SPEED_25GB_LINK
:
87 link_ksettings
->base
.speed
= SPEED_25000
;
90 case HINIC_SPEED_40GB_LINK
:
91 link_ksettings
->base
.speed
= SPEED_40000
;
94 case HINIC_SPEED_100GB_LINK
:
95 link_ksettings
->base
.speed
= SPEED_100000
;
99 link_ksettings
->base
.speed
= SPEED_UNKNOWN
;
104 static int hinic_get_link_ksettings(struct net_device
*netdev
,
105 struct ethtool_link_ksettings
108 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
109 enum hinic_port_link_state link_state
;
110 struct hinic_port_cap port_cap
;
113 ethtool_link_ksettings_zero_link_mode(link_ksettings
, advertising
);
114 ethtool_link_ksettings_add_link_mode(link_ksettings
, supported
,
117 link_ksettings
->base
.speed
= SPEED_UNKNOWN
;
118 link_ksettings
->base
.autoneg
= AUTONEG_DISABLE
;
119 link_ksettings
->base
.duplex
= DUPLEX_UNKNOWN
;
121 err
= hinic_port_get_cap(nic_dev
, &port_cap
);
123 netif_err(nic_dev
, drv
, netdev
,
124 "Failed to get port capabilities\n");
128 err
= hinic_port_link_state(nic_dev
, &link_state
);
130 netif_err(nic_dev
, drv
, netdev
,
131 "Failed to get port link state\n");
135 if (link_state
!= HINIC_LINK_STATE_UP
) {
136 netif_info(nic_dev
, drv
, netdev
, "No link\n");
140 set_link_speed(link_ksettings
, port_cap
.speed
);
142 if (!!(port_cap
.autoneg_cap
& HINIC_AUTONEG_SUPPORTED
))
143 ethtool_link_ksettings_add_link_mode(link_ksettings
,
144 advertising
, Autoneg
);
146 if (port_cap
.autoneg_state
== HINIC_AUTONEG_ACTIVE
)
147 link_ksettings
->base
.autoneg
= AUTONEG_ENABLE
;
149 link_ksettings
->base
.duplex
= (port_cap
.duplex
== HINIC_DUPLEX_FULL
) ?
150 DUPLEX_FULL
: DUPLEX_HALF
;
154 static void hinic_get_drvinfo(struct net_device
*netdev
,
155 struct ethtool_drvinfo
*info
)
157 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
158 struct hinic_hwdev
*hwdev
= nic_dev
->hwdev
;
159 struct hinic_hwif
*hwif
= hwdev
->hwif
;
161 strlcpy(info
->driver
, HINIC_DRV_NAME
, sizeof(info
->driver
));
162 strlcpy(info
->bus_info
, pci_name(hwif
->pdev
), sizeof(info
->bus_info
));
165 static void hinic_get_ringparam(struct net_device
*netdev
,
166 struct ethtool_ringparam
*ring
)
168 ring
->rx_max_pending
= HINIC_RQ_DEPTH
;
169 ring
->tx_max_pending
= HINIC_SQ_DEPTH
;
170 ring
->rx_pending
= HINIC_RQ_DEPTH
;
171 ring
->tx_pending
= HINIC_SQ_DEPTH
;
174 static void hinic_get_channels(struct net_device
*netdev
,
175 struct ethtool_channels
*channels
)
177 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
178 struct hinic_hwdev
*hwdev
= nic_dev
->hwdev
;
180 channels
->max_rx
= hwdev
->nic_cap
.max_qps
;
181 channels
->max_tx
= hwdev
->nic_cap
.max_qps
;
182 channels
->max_other
= 0;
183 channels
->max_combined
= 0;
184 channels
->rx_count
= hinic_hwdev_num_qps(hwdev
);
185 channels
->tx_count
= hinic_hwdev_num_qps(hwdev
);
186 channels
->other_count
= 0;
187 channels
->combined_count
= 0;
190 static const struct ethtool_ops hinic_ethtool_ops
= {
191 .get_link_ksettings
= hinic_get_link_ksettings
,
192 .get_drvinfo
= hinic_get_drvinfo
,
193 .get_link
= ethtool_op_get_link
,
194 .get_ringparam
= hinic_get_ringparam
,
195 .get_channels
= hinic_get_channels
,
198 static void update_rx_stats(struct hinic_dev
*nic_dev
, struct hinic_rxq
*rxq
)
200 struct hinic_rxq_stats
*nic_rx_stats
= &nic_dev
->rx_stats
;
201 struct hinic_rxq_stats rx_stats
;
203 u64_stats_init(&rx_stats
.syncp
);
205 hinic_rxq_get_stats(rxq
, &rx_stats
);
207 u64_stats_update_begin(&nic_rx_stats
->syncp
);
208 nic_rx_stats
->bytes
+= rx_stats
.bytes
;
209 nic_rx_stats
->pkts
+= rx_stats
.pkts
;
210 u64_stats_update_end(&nic_rx_stats
->syncp
);
212 hinic_rxq_clean_stats(rxq
);
215 static void update_tx_stats(struct hinic_dev
*nic_dev
, struct hinic_txq
*txq
)
217 struct hinic_txq_stats
*nic_tx_stats
= &nic_dev
->tx_stats
;
218 struct hinic_txq_stats tx_stats
;
220 u64_stats_init(&tx_stats
.syncp
);
222 hinic_txq_get_stats(txq
, &tx_stats
);
224 u64_stats_update_begin(&nic_tx_stats
->syncp
);
225 nic_tx_stats
->bytes
+= tx_stats
.bytes
;
226 nic_tx_stats
->pkts
+= tx_stats
.pkts
;
227 nic_tx_stats
->tx_busy
+= tx_stats
.tx_busy
;
228 nic_tx_stats
->tx_wake
+= tx_stats
.tx_wake
;
229 nic_tx_stats
->tx_dropped
+= tx_stats
.tx_dropped
;
230 u64_stats_update_end(&nic_tx_stats
->syncp
);
232 hinic_txq_clean_stats(txq
);
235 static void update_nic_stats(struct hinic_dev
*nic_dev
)
237 int i
, num_qps
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
239 for (i
= 0; i
< num_qps
; i
++)
240 update_rx_stats(nic_dev
, &nic_dev
->rxqs
[i
]);
242 for (i
= 0; i
< num_qps
; i
++)
243 update_tx_stats(nic_dev
, &nic_dev
->txqs
[i
]);
247 * create_txqs - Create the Logical Tx Queues of specific NIC device
248 * @nic_dev: the specific NIC device
250 * Return 0 - Success, negative - Failure
252 static int create_txqs(struct hinic_dev
*nic_dev
)
254 int err
, i
, j
, num_txqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
255 struct net_device
*netdev
= nic_dev
->netdev
;
261 txq_size
= num_txqs
* sizeof(*nic_dev
->txqs
);
262 nic_dev
->txqs
= devm_kzalloc(&netdev
->dev
, txq_size
, GFP_KERNEL
);
266 for (i
= 0; i
< num_txqs
; i
++) {
267 struct hinic_sq
*sq
= hinic_hwdev_get_sq(nic_dev
->hwdev
, i
);
269 err
= hinic_init_txq(&nic_dev
->txqs
[i
], sq
, netdev
);
271 netif_err(nic_dev
, drv
, netdev
,
272 "Failed to init Txq\n");
280 for (j
= 0; j
< i
; j
++)
281 hinic_clean_txq(&nic_dev
->txqs
[j
]);
283 devm_kfree(&netdev
->dev
, nic_dev
->txqs
);
288 * free_txqs - Free the Logical Tx Queues of specific NIC device
289 * @nic_dev: the specific NIC device
291 static void free_txqs(struct hinic_dev
*nic_dev
)
293 int i
, num_txqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
294 struct net_device
*netdev
= nic_dev
->netdev
;
299 for (i
= 0; i
< num_txqs
; i
++)
300 hinic_clean_txq(&nic_dev
->txqs
[i
]);
302 devm_kfree(&netdev
->dev
, nic_dev
->txqs
);
303 nic_dev
->txqs
= NULL
;
307 * create_txqs - Create the Logical Rx Queues of specific NIC device
308 * @nic_dev: the specific NIC device
310 * Return 0 - Success, negative - Failure
312 static int create_rxqs(struct hinic_dev
*nic_dev
)
314 int err
, i
, j
, num_rxqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
315 struct net_device
*netdev
= nic_dev
->netdev
;
321 rxq_size
= num_rxqs
* sizeof(*nic_dev
->rxqs
);
322 nic_dev
->rxqs
= devm_kzalloc(&netdev
->dev
, rxq_size
, GFP_KERNEL
);
326 for (i
= 0; i
< num_rxqs
; i
++) {
327 struct hinic_rq
*rq
= hinic_hwdev_get_rq(nic_dev
->hwdev
, i
);
329 err
= hinic_init_rxq(&nic_dev
->rxqs
[i
], rq
, netdev
);
331 netif_err(nic_dev
, drv
, netdev
,
332 "Failed to init rxq\n");
340 for (j
= 0; j
< i
; j
++)
341 hinic_clean_rxq(&nic_dev
->rxqs
[j
]);
343 devm_kfree(&netdev
->dev
, nic_dev
->rxqs
);
348 * free_txqs - Free the Logical Rx Queues of specific NIC device
349 * @nic_dev: the specific NIC device
351 static void free_rxqs(struct hinic_dev
*nic_dev
)
353 int i
, num_rxqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
354 struct net_device
*netdev
= nic_dev
->netdev
;
359 for (i
= 0; i
< num_rxqs
; i
++)
360 hinic_clean_rxq(&nic_dev
->rxqs
[i
]);
362 devm_kfree(&netdev
->dev
, nic_dev
->rxqs
);
363 nic_dev
->rxqs
= NULL
;
366 static int hinic_open(struct net_device
*netdev
)
368 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
369 enum hinic_port_link_state link_state
;
370 int err
, ret
, num_qps
;
372 if (!(nic_dev
->flags
& HINIC_INTF_UP
)) {
373 err
= hinic_hwdev_ifup(nic_dev
->hwdev
);
375 netif_err(nic_dev
, drv
, netdev
,
376 "Failed - HW interface up\n");
381 err
= create_txqs(nic_dev
);
383 netif_err(nic_dev
, drv
, netdev
,
384 "Failed to create Tx queues\n");
385 goto err_create_txqs
;
388 err
= create_rxqs(nic_dev
);
390 netif_err(nic_dev
, drv
, netdev
,
391 "Failed to create Rx queues\n");
392 goto err_create_rxqs
;
395 num_qps
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
396 netif_set_real_num_tx_queues(netdev
, num_qps
);
397 netif_set_real_num_rx_queues(netdev
, num_qps
);
399 err
= hinic_port_set_state(nic_dev
, HINIC_PORT_ENABLE
);
401 netif_err(nic_dev
, drv
, netdev
,
402 "Failed to set port state\n");
406 err
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_ENABLE
);
408 netif_err(nic_dev
, drv
, netdev
,
409 "Failed to set func port state\n");
410 goto err_func_port_state
;
413 /* Wait up to 3 sec between port enable to link state */
416 down(&nic_dev
->mgmt_lock
);
418 err
= hinic_port_link_state(nic_dev
, &link_state
);
420 netif_err(nic_dev
, drv
, netdev
, "Failed to get link state\n");
424 if (link_state
== HINIC_LINK_STATE_UP
)
425 nic_dev
->flags
|= HINIC_LINK_UP
;
427 nic_dev
->flags
|= HINIC_INTF_UP
;
429 if ((nic_dev
->flags
& (HINIC_LINK_UP
| HINIC_INTF_UP
)) ==
430 (HINIC_LINK_UP
| HINIC_INTF_UP
)) {
431 netif_info(nic_dev
, drv
, netdev
, "link + intf UP\n");
432 netif_carrier_on(netdev
);
433 netif_tx_wake_all_queues(netdev
);
436 up(&nic_dev
->mgmt_lock
);
438 netif_info(nic_dev
, drv
, netdev
, "HINIC_INTF is UP\n");
442 up(&nic_dev
->mgmt_lock
);
443 ret
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_DISABLE
);
445 netif_warn(nic_dev
, drv
, netdev
,
446 "Failed to revert func port state\n");
449 ret
= hinic_port_set_state(nic_dev
, HINIC_PORT_DISABLE
);
451 netif_warn(nic_dev
, drv
, netdev
,
452 "Failed to revert port state\n");
461 if (!(nic_dev
->flags
& HINIC_INTF_UP
))
462 hinic_hwdev_ifdown(nic_dev
->hwdev
);
466 static int hinic_close(struct net_device
*netdev
)
468 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
472 down(&nic_dev
->mgmt_lock
);
474 flags
= nic_dev
->flags
;
475 nic_dev
->flags
&= ~HINIC_INTF_UP
;
477 netif_carrier_off(netdev
);
478 netif_tx_disable(netdev
);
480 update_nic_stats(nic_dev
);
482 up(&nic_dev
->mgmt_lock
);
484 err
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_DISABLE
);
486 netif_err(nic_dev
, drv
, netdev
,
487 "Failed to set func port state\n");
488 nic_dev
->flags
|= (flags
& HINIC_INTF_UP
);
492 err
= hinic_port_set_state(nic_dev
, HINIC_PORT_DISABLE
);
494 netif_err(nic_dev
, drv
, netdev
, "Failed to set port state\n");
495 nic_dev
->flags
|= (flags
& HINIC_INTF_UP
);
502 if (flags
& HINIC_INTF_UP
)
503 hinic_hwdev_ifdown(nic_dev
->hwdev
);
505 netif_info(nic_dev
, drv
, netdev
, "HINIC_INTF is DOWN\n");
509 static int hinic_change_mtu(struct net_device
*netdev
, int new_mtu
)
511 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
514 netif_info(nic_dev
, drv
, netdev
, "set_mtu = %d\n", new_mtu
);
516 err
= hinic_port_set_mtu(nic_dev
, new_mtu
);
518 netif_err(nic_dev
, drv
, netdev
, "Failed to set port mtu\n");
520 netdev
->mtu
= new_mtu
;
526 * change_mac_addr - change the main mac address of network device
527 * @netdev: network device
528 * @addr: mac address to set
530 * Return 0 - Success, negative - Failure
532 static int change_mac_addr(struct net_device
*netdev
, const u8
*addr
)
534 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
538 if (!is_valid_ether_addr(addr
))
539 return -EADDRNOTAVAIL
;
541 netif_info(nic_dev
, drv
, netdev
, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
542 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
544 down(&nic_dev
->mgmt_lock
);
547 err
= hinic_port_del_mac(nic_dev
, netdev
->dev_addr
, vid
);
549 netif_err(nic_dev
, drv
, netdev
,
550 "Failed to delete mac\n");
554 err
= hinic_port_add_mac(nic_dev
, addr
, vid
);
556 netif_err(nic_dev
, drv
, netdev
, "Failed to add mac\n");
560 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
561 } while (vid
!= VLAN_N_VID
);
563 up(&nic_dev
->mgmt_lock
);
567 static int hinic_set_mac_addr(struct net_device
*netdev
, void *addr
)
569 unsigned char new_mac
[ETH_ALEN
];
570 struct sockaddr
*saddr
= addr
;
573 memcpy(new_mac
, saddr
->sa_data
, ETH_ALEN
);
575 err
= change_mac_addr(netdev
, new_mac
);
577 memcpy(netdev
->dev_addr
, new_mac
, ETH_ALEN
);
583 * add_mac_addr - add mac address to network device
584 * @netdev: network device
585 * @addr: mac address to add
587 * Return 0 - Success, negative - Failure
589 static int add_mac_addr(struct net_device
*netdev
, const u8
*addr
)
591 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
595 netif_info(nic_dev
, drv
, netdev
, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
596 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
598 down(&nic_dev
->mgmt_lock
);
601 err
= hinic_port_add_mac(nic_dev
, addr
, vid
);
603 netif_err(nic_dev
, drv
, netdev
, "Failed to add mac\n");
607 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
608 } while (vid
!= VLAN_N_VID
);
610 up(&nic_dev
->mgmt_lock
);
615 * remove_mac_addr - remove mac address from network device
616 * @netdev: network device
617 * @addr: mac address to remove
619 * Return 0 - Success, negative - Failure
621 static int remove_mac_addr(struct net_device
*netdev
, const u8
*addr
)
623 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
627 if (!is_valid_ether_addr(addr
))
628 return -EADDRNOTAVAIL
;
630 netif_info(nic_dev
, drv
, netdev
, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
631 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
633 down(&nic_dev
->mgmt_lock
);
636 err
= hinic_port_del_mac(nic_dev
, addr
, vid
);
638 netif_err(nic_dev
, drv
, netdev
,
639 "Failed to delete mac\n");
643 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
644 } while (vid
!= VLAN_N_VID
);
646 up(&nic_dev
->mgmt_lock
);
650 static int hinic_vlan_rx_add_vid(struct net_device
*netdev
,
651 __always_unused __be16 proto
, u16 vid
)
653 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
656 netif_info(nic_dev
, drv
, netdev
, "add vid = %d\n", vid
);
658 down(&nic_dev
->mgmt_lock
);
660 err
= hinic_port_add_vlan(nic_dev
, vid
);
662 netif_err(nic_dev
, drv
, netdev
, "Failed to add vlan\n");
666 err
= hinic_port_add_mac(nic_dev
, netdev
->dev_addr
, vid
);
668 netif_err(nic_dev
, drv
, netdev
, "Failed to set mac\n");
672 bitmap_set(nic_dev
->vlan_bitmap
, vid
, 1);
674 up(&nic_dev
->mgmt_lock
);
678 ret
= hinic_port_del_vlan(nic_dev
, vid
);
680 netif_err(nic_dev
, drv
, netdev
,
681 "Failed to revert by removing vlan\n");
684 up(&nic_dev
->mgmt_lock
);
688 static int hinic_vlan_rx_kill_vid(struct net_device
*netdev
,
689 __always_unused __be16 proto
, u16 vid
)
691 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
694 netif_info(nic_dev
, drv
, netdev
, "remove vid = %d\n", vid
);
696 down(&nic_dev
->mgmt_lock
);
698 err
= hinic_port_del_vlan(nic_dev
, vid
);
700 netif_err(nic_dev
, drv
, netdev
, "Failed to delete vlan\n");
704 bitmap_clear(nic_dev
->vlan_bitmap
, vid
, 1);
706 up(&nic_dev
->mgmt_lock
);
710 up(&nic_dev
->mgmt_lock
);
714 static void set_rx_mode(struct work_struct
*work
)
716 struct hinic_rx_mode_work
*rx_mode_work
= work_to_rx_mode_work(work
);
717 struct hinic_dev
*nic_dev
= rx_mode_work_to_nic_dev(rx_mode_work
);
718 struct netdev_hw_addr
*ha
;
720 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "set rx mode work\n");
722 hinic_port_set_rx_mode(nic_dev
, rx_mode_work
->rx_mode
);
724 __dev_uc_sync(nic_dev
->netdev
, add_mac_addr
, remove_mac_addr
);
725 __dev_mc_sync(nic_dev
->netdev
, add_mac_addr
, remove_mac_addr
);
727 netdev_for_each_mc_addr(ha
, nic_dev
->netdev
)
728 add_mac_addr(nic_dev
->netdev
, ha
->addr
);
731 static void hinic_set_rx_mode(struct net_device
*netdev
)
733 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
734 struct hinic_rx_mode_work
*rx_mode_work
;
737 rx_mode_work
= &nic_dev
->rx_mode_work
;
739 rx_mode
= HINIC_RX_MODE_UC
|
743 if (netdev
->flags
& IFF_PROMISC
)
744 rx_mode
|= HINIC_RX_MODE_PROMISC
;
745 else if (netdev
->flags
& IFF_ALLMULTI
)
746 rx_mode
|= HINIC_RX_MODE_MC_ALL
;
748 rx_mode_work
->rx_mode
= rx_mode
;
750 queue_work(nic_dev
->workq
, &rx_mode_work
->work
);
753 static void hinic_tx_timeout(struct net_device
*netdev
)
755 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
757 netif_err(nic_dev
, drv
, netdev
, "Tx timeout\n");
760 static void hinic_get_stats64(struct net_device
*netdev
,
761 struct rtnl_link_stats64
*stats
)
763 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
764 struct hinic_rxq_stats
*nic_rx_stats
;
765 struct hinic_txq_stats
*nic_tx_stats
;
767 nic_rx_stats
= &nic_dev
->rx_stats
;
768 nic_tx_stats
= &nic_dev
->tx_stats
;
770 down(&nic_dev
->mgmt_lock
);
772 if (nic_dev
->flags
& HINIC_INTF_UP
)
773 update_nic_stats(nic_dev
);
775 up(&nic_dev
->mgmt_lock
);
777 stats
->rx_bytes
= nic_rx_stats
->bytes
;
778 stats
->rx_packets
= nic_rx_stats
->pkts
;
780 stats
->tx_bytes
= nic_tx_stats
->bytes
;
781 stats
->tx_packets
= nic_tx_stats
->pkts
;
782 stats
->tx_errors
= nic_tx_stats
->tx_dropped
;
785 static const struct net_device_ops hinic_netdev_ops
= {
786 .ndo_open
= hinic_open
,
787 .ndo_stop
= hinic_close
,
788 .ndo_change_mtu
= hinic_change_mtu
,
789 .ndo_set_mac_address
= hinic_set_mac_addr
,
790 .ndo_validate_addr
= eth_validate_addr
,
791 .ndo_vlan_rx_add_vid
= hinic_vlan_rx_add_vid
,
792 .ndo_vlan_rx_kill_vid
= hinic_vlan_rx_kill_vid
,
793 .ndo_set_rx_mode
= hinic_set_rx_mode
,
794 .ndo_start_xmit
= hinic_xmit_frame
,
795 .ndo_tx_timeout
= hinic_tx_timeout
,
796 .ndo_get_stats64
= hinic_get_stats64
,
799 static void netdev_features_init(struct net_device
*netdev
)
801 netdev
->hw_features
= NETIF_F_SG
| NETIF_F_HIGHDMA
| NETIF_F_IP_CSUM
|
802 NETIF_F_IPV6_CSUM
| NETIF_F_TSO
| NETIF_F_TSO6
|
805 netdev
->vlan_features
= netdev
->hw_features
;
807 netdev
->features
= netdev
->hw_features
| NETIF_F_HW_VLAN_CTAG_FILTER
;
811 * link_status_event_handler - link event handler
812 * @handle: nic device for the handler
813 * @buf_in: input buffer
814 * @in_size: input size
815 * @buf_in: output buffer
816 * @out_size: returned output size
818 * Return 0 - Success, negative - Failure
820 static void link_status_event_handler(void *handle
, void *buf_in
, u16 in_size
,
821 void *buf_out
, u16
*out_size
)
823 struct hinic_port_link_status
*link_status
, *ret_link_status
;
824 struct hinic_dev
*nic_dev
= handle
;
826 link_status
= buf_in
;
828 if (link_status
->link
== HINIC_LINK_STATE_UP
) {
829 down(&nic_dev
->mgmt_lock
);
831 nic_dev
->flags
|= HINIC_LINK_UP
;
833 if ((nic_dev
->flags
& (HINIC_LINK_UP
| HINIC_INTF_UP
)) ==
834 (HINIC_LINK_UP
| HINIC_INTF_UP
)) {
835 netif_carrier_on(nic_dev
->netdev
);
836 netif_tx_wake_all_queues(nic_dev
->netdev
);
839 up(&nic_dev
->mgmt_lock
);
841 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "HINIC_Link is UP\n");
843 down(&nic_dev
->mgmt_lock
);
845 nic_dev
->flags
&= ~HINIC_LINK_UP
;
847 netif_carrier_off(nic_dev
->netdev
);
848 netif_tx_disable(nic_dev
->netdev
);
850 up(&nic_dev
->mgmt_lock
);
852 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "HINIC_Link is DOWN\n");
855 ret_link_status
= buf_out
;
856 ret_link_status
->status
= 0;
858 *out_size
= sizeof(*ret_link_status
);
861 static int set_features(struct hinic_dev
*nic_dev
,
862 netdev_features_t pre_features
,
863 netdev_features_t features
, bool force_change
)
865 netdev_features_t changed
= force_change
? ~0 : pre_features
^ features
;
866 u32 csum_en
= HINIC_RX_CSUM_OFFLOAD_EN
;
869 if (changed
& NETIF_F_TSO
)
870 err
= hinic_port_set_tso(nic_dev
, (features
& NETIF_F_TSO
) ?
871 HINIC_TSO_ENABLE
: HINIC_TSO_DISABLE
);
873 if (changed
& NETIF_F_RXCSUM
)
874 err
= hinic_set_rx_csum_offload(nic_dev
, csum_en
);
880 * nic_dev_init - Initialize the NIC device
881 * @pdev: the NIC pci device
883 * Return 0 - Success, negative - Failure
885 static int nic_dev_init(struct pci_dev
*pdev
)
887 struct hinic_rx_mode_work
*rx_mode_work
;
888 struct hinic_txq_stats
*tx_stats
;
889 struct hinic_rxq_stats
*rx_stats
;
890 struct hinic_dev
*nic_dev
;
891 struct net_device
*netdev
;
892 struct hinic_hwdev
*hwdev
;
895 hwdev
= hinic_init_hwdev(pdev
);
897 dev_err(&pdev
->dev
, "Failed to initialize HW device\n");
898 return PTR_ERR(hwdev
);
901 num_qps
= hinic_hwdev_num_qps(hwdev
);
903 dev_err(&pdev
->dev
, "Invalid number of QPS\n");
908 netdev
= alloc_etherdev_mq(sizeof(*nic_dev
), num_qps
);
910 dev_err(&pdev
->dev
, "Failed to allocate Ethernet device\n");
912 goto err_alloc_etherdev
;
915 netdev
->netdev_ops
= &hinic_netdev_ops
;
916 netdev
->ethtool_ops
= &hinic_ethtool_ops
;
917 netdev
->max_mtu
= ETH_MAX_MTU
;
919 nic_dev
= netdev_priv(netdev
);
920 nic_dev
->netdev
= netdev
;
921 nic_dev
->hwdev
= hwdev
;
922 nic_dev
->msg_enable
= MSG_ENABLE_DEFAULT
;
924 nic_dev
->txqs
= NULL
;
925 nic_dev
->rxqs
= NULL
;
926 nic_dev
->tx_weight
= tx_weight
;
927 nic_dev
->rx_weight
= rx_weight
;
929 sema_init(&nic_dev
->mgmt_lock
, 1);
931 tx_stats
= &nic_dev
->tx_stats
;
932 rx_stats
= &nic_dev
->rx_stats
;
934 u64_stats_init(&tx_stats
->syncp
);
935 u64_stats_init(&rx_stats
->syncp
);
937 nic_dev
->vlan_bitmap
= devm_kzalloc(&pdev
->dev
,
938 VLAN_BITMAP_SIZE(nic_dev
),
940 if (!nic_dev
->vlan_bitmap
) {
942 goto err_vlan_bitmap
;
945 nic_dev
->workq
= create_singlethread_workqueue(HINIC_WQ_NAME
);
946 if (!nic_dev
->workq
) {
951 pci_set_drvdata(pdev
, netdev
);
953 err
= hinic_port_get_mac(nic_dev
, netdev
->dev_addr
);
955 dev_warn(&pdev
->dev
, "Failed to get mac address\n");
957 err
= hinic_port_add_mac(nic_dev
, netdev
->dev_addr
, 0);
959 dev_err(&pdev
->dev
, "Failed to add mac\n");
963 err
= hinic_port_set_mtu(nic_dev
, netdev
->mtu
);
965 dev_err(&pdev
->dev
, "Failed to set mtu\n");
969 rx_mode_work
= &nic_dev
->rx_mode_work
;
970 INIT_WORK(&rx_mode_work
->work
, set_rx_mode
);
972 netdev_features_init(netdev
);
974 netif_carrier_off(netdev
);
976 hinic_hwdev_cb_register(nic_dev
->hwdev
, HINIC_MGMT_MSG_CMD_LINK_STATUS
,
977 nic_dev
, link_status_event_handler
);
979 err
= set_features(nic_dev
, 0, nic_dev
->netdev
->features
, true);
981 goto err_set_features
;
983 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
985 err
= register_netdev(netdev
);
987 dev_err(&pdev
->dev
, "Failed to register netdev\n");
995 hinic_hwdev_cb_unregister(nic_dev
->hwdev
,
996 HINIC_MGMT_MSG_CMD_LINK_STATUS
);
997 cancel_work_sync(&rx_mode_work
->work
);
1001 pci_set_drvdata(pdev
, NULL
);
1002 destroy_workqueue(nic_dev
->workq
);
1006 free_netdev(netdev
);
1010 hinic_free_hwdev(hwdev
);
1014 static int hinic_probe(struct pci_dev
*pdev
,
1015 const struct pci_device_id
*id
)
1017 int err
= pci_enable_device(pdev
);
1020 dev_err(&pdev
->dev
, "Failed to enable PCI device\n");
1024 err
= pci_request_regions(pdev
, HINIC_DRV_NAME
);
1026 dev_err(&pdev
->dev
, "Failed to request PCI regions\n");
1027 goto err_pci_regions
;
1030 pci_set_master(pdev
);
1032 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
1034 dev_warn(&pdev
->dev
, "Couldn't set 64-bit DMA mask\n");
1035 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
1037 dev_err(&pdev
->dev
, "Failed to set DMA mask\n");
1042 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
1044 dev_warn(&pdev
->dev
,
1045 "Couldn't set 64-bit consistent DMA mask\n");
1046 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
1049 "Failed to set consistent DMA mask\n");
1050 goto err_dma_consistent_mask
;
1054 err
= nic_dev_init(pdev
);
1056 dev_err(&pdev
->dev
, "Failed to initialize NIC device\n");
1057 goto err_nic_dev_init
;
1060 dev_info(&pdev
->dev
, "HiNIC driver - probed\n");
1064 err_dma_consistent_mask
:
1066 pci_release_regions(pdev
);
1069 pci_disable_device(pdev
);
1073 static void hinic_remove(struct pci_dev
*pdev
)
1075 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1076 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
1077 struct hinic_rx_mode_work
*rx_mode_work
;
1079 unregister_netdev(netdev
);
1081 hinic_hwdev_cb_unregister(nic_dev
->hwdev
,
1082 HINIC_MGMT_MSG_CMD_LINK_STATUS
);
1084 rx_mode_work
= &nic_dev
->rx_mode_work
;
1085 cancel_work_sync(&rx_mode_work
->work
);
1087 pci_set_drvdata(pdev
, NULL
);
1089 destroy_workqueue(nic_dev
->workq
);
1091 hinic_free_hwdev(nic_dev
->hwdev
);
1093 free_netdev(netdev
);
1095 pci_release_regions(pdev
);
1096 pci_disable_device(pdev
);
1098 dev_info(&pdev
->dev
, "HiNIC driver - removed\n");
1101 static void hinic_shutdown(struct pci_dev
*pdev
)
1103 pci_disable_device(pdev
);
1106 static const struct pci_device_id hinic_pci_table
[] = {
1107 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_QUAD_PORT_25GE
), 0},
1108 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_DUAL_PORT_100GE
), 0},
1109 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ
), 0},
1110 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ
), 0},
1113 MODULE_DEVICE_TABLE(pci
, hinic_pci_table
);
1115 static struct pci_driver hinic_driver
= {
1116 .name
= HINIC_DRV_NAME
,
1117 .id_table
= hinic_pci_table
,
1118 .probe
= hinic_probe
,
1119 .remove
= hinic_remove
,
1120 .shutdown
= hinic_shutdown
,
1123 module_pci_driver(hinic_driver
);