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 <net/devlink.h>
22 #include <linux/bitops.h>
23 #include <linux/bitmap.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
27 #include "hinic_hw_qp.h"
28 #include "hinic_hw_dev.h"
29 #include "hinic_devlink.h"
30 #include "hinic_port.h"
33 #include "hinic_dev.h"
34 #include "hinic_sriov.h"
36 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
37 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
38 MODULE_LICENSE("GPL");
40 static unsigned int tx_weight = 64;
41 module_param(tx_weight, uint, 0644);
42 MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
44 static unsigned int rx_weight = 64;
45 module_param(rx_weight, uint, 0644);
46 MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
48 #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
49 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200
50 #define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205
51 #define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210
52 #define HINIC_DEV_ID_VF 0x375e
54 #define HINIC_WQ_NAME "hinic_dev"
56 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
58 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
60 #define HINIC_LRO_MAX_WQE_NUM_DEFAULT 8
62 #define HINIC_LRO_RX_TIMER_DEFAULT 16
64 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
66 #define work_to_rx_mode_work(work) \
67 container_of(work, struct hinic_rx_mode_work, work)
69 #define rx_mode_work_to_nic_dev(rx_mode_work) \
70 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
72 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
74 #define HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT 2
75 #define HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG 32
76 #define HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG 7
78 static int change_mac_addr(struct net_device *netdev, const u8 *addr);
80 static int set_features(struct hinic_dev *nic_dev,
81 netdev_features_t pre_features,
82 netdev_features_t features, bool force_change);
84 static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
86 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
87 struct hinic_rxq_stats rx_stats;
89 u64_stats_init(&rx_stats.syncp);
91 hinic_rxq_get_stats(rxq, &rx_stats);
93 u64_stats_update_begin(&nic_rx_stats->syncp);
94 nic_rx_stats->bytes += rx_stats.bytes;
95 nic_rx_stats->pkts += rx_stats.pkts;
96 nic_rx_stats->errors += rx_stats.errors;
97 nic_rx_stats->csum_errors += rx_stats.csum_errors;
98 nic_rx_stats->other_errors += rx_stats.other_errors;
99 u64_stats_update_end(&nic_rx_stats->syncp);
101 hinic_rxq_clean_stats(rxq);
104 static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
106 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
107 struct hinic_txq_stats tx_stats;
109 u64_stats_init(&tx_stats.syncp);
111 hinic_txq_get_stats(txq, &tx_stats);
113 u64_stats_update_begin(&nic_tx_stats->syncp);
114 nic_tx_stats->bytes += tx_stats.bytes;
115 nic_tx_stats->pkts += tx_stats.pkts;
116 nic_tx_stats->tx_busy += tx_stats.tx_busy;
117 nic_tx_stats->tx_wake += tx_stats.tx_wake;
118 nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
119 nic_tx_stats->big_frags_pkts += tx_stats.big_frags_pkts;
120 u64_stats_update_end(&nic_tx_stats->syncp);
122 hinic_txq_clean_stats(txq);
125 static void update_nic_stats(struct hinic_dev *nic_dev)
127 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
129 for (i = 0; i < num_qps; i++)
130 update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
132 for (i = 0; i < num_qps; i++)
133 update_tx_stats(nic_dev, &nic_dev->txqs[i]);
137 * create_txqs - Create the Logical Tx Queues of specific NIC device
138 * @nic_dev: the specific NIC device
140 * Return 0 - Success, negative - Failure
142 static int create_txqs(struct hinic_dev *nic_dev)
144 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
145 struct net_device *netdev = nic_dev->netdev;
151 txq_size = num_txqs * sizeof(*nic_dev->txqs);
152 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
156 for (i = 0; i < num_txqs; i++) {
157 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
159 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
161 netif_err(nic_dev, drv, netdev,
162 "Failed to init Txq\n");
170 for (j = 0; j < i; j++)
171 hinic_clean_txq(&nic_dev->txqs[j]);
173 devm_kfree(&netdev->dev, nic_dev->txqs);
178 * free_txqs - Free the Logical Tx Queues of specific NIC device
179 * @nic_dev: the specific NIC device
181 static void free_txqs(struct hinic_dev *nic_dev)
183 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
184 struct net_device *netdev = nic_dev->netdev;
189 for (i = 0; i < num_txqs; i++)
190 hinic_clean_txq(&nic_dev->txqs[i]);
192 devm_kfree(&netdev->dev, nic_dev->txqs);
193 nic_dev->txqs = NULL;
197 * create_txqs - Create the Logical Rx Queues of specific NIC device
198 * @nic_dev: the specific NIC device
200 * Return 0 - Success, negative - Failure
202 static int create_rxqs(struct hinic_dev *nic_dev)
204 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
205 struct net_device *netdev = nic_dev->netdev;
211 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
212 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
216 for (i = 0; i < num_rxqs; i++) {
217 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
219 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
221 netif_err(nic_dev, drv, netdev,
222 "Failed to init rxq\n");
230 for (j = 0; j < i; j++)
231 hinic_clean_rxq(&nic_dev->rxqs[j]);
233 devm_kfree(&netdev->dev, nic_dev->rxqs);
238 * free_txqs - Free the Logical Rx Queues of specific NIC device
239 * @nic_dev: the specific NIC device
241 static void free_rxqs(struct hinic_dev *nic_dev)
243 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
244 struct net_device *netdev = nic_dev->netdev;
249 for (i = 0; i < num_rxqs; i++)
250 hinic_clean_rxq(&nic_dev->rxqs[i]);
252 devm_kfree(&netdev->dev, nic_dev->rxqs);
253 nic_dev->rxqs = NULL;
256 static int hinic_configure_max_qnum(struct hinic_dev *nic_dev)
260 err = hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps);
267 static int hinic_rss_init(struct hinic_dev *nic_dev)
269 u8 default_rss_key[HINIC_RSS_KEY_SIZE];
270 u8 tmpl_idx = nic_dev->rss_tmpl_idx;
274 indir_tbl = kcalloc(HINIC_RSS_INDIR_SIZE, sizeof(u32), GFP_KERNEL);
278 netdev_rss_key_fill(default_rss_key, sizeof(default_rss_key));
279 for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
280 indir_tbl[i] = ethtool_rxfh_indir_default(i, nic_dev->num_rss);
282 err = hinic_rss_set_template_tbl(nic_dev, tmpl_idx, default_rss_key);
286 err = hinic_rss_set_indir_tbl(nic_dev, tmpl_idx, indir_tbl);
290 err = hinic_set_rss_type(nic_dev, tmpl_idx, nic_dev->rss_type);
294 err = hinic_rss_set_hash_engine(nic_dev, tmpl_idx,
295 nic_dev->rss_hash_engine);
299 err = hinic_rss_cfg(nic_dev, 1, tmpl_idx);
308 static void hinic_rss_deinit(struct hinic_dev *nic_dev)
310 hinic_rss_cfg(nic_dev, 0, nic_dev->rss_tmpl_idx);
313 static void hinic_init_rss_parameters(struct hinic_dev *nic_dev)
315 nic_dev->rss_hash_engine = HINIC_RSS_HASH_ENGINE_TYPE_XOR;
316 nic_dev->rss_type.tcp_ipv6_ext = 1;
317 nic_dev->rss_type.ipv6_ext = 1;
318 nic_dev->rss_type.tcp_ipv6 = 1;
319 nic_dev->rss_type.ipv6 = 1;
320 nic_dev->rss_type.tcp_ipv4 = 1;
321 nic_dev->rss_type.ipv4 = 1;
322 nic_dev->rss_type.udp_ipv6 = 1;
323 nic_dev->rss_type.udp_ipv4 = 1;
326 static void hinic_enable_rss(struct hinic_dev *nic_dev)
328 struct net_device *netdev = nic_dev->netdev;
329 struct hinic_hwdev *hwdev = nic_dev->hwdev;
330 struct hinic_hwif *hwif = hwdev->hwif;
331 struct pci_dev *pdev = hwif->pdev;
332 int i, node, err = 0;
335 if (nic_dev->max_qps <= 1) {
336 nic_dev->flags &= ~HINIC_RSS_ENABLE;
337 nic_dev->rss_limit = nic_dev->max_qps;
338 nic_dev->num_qps = nic_dev->max_qps;
339 nic_dev->num_rss = nic_dev->max_qps;
344 err = hinic_rss_template_alloc(nic_dev, &nic_dev->rss_tmpl_idx);
346 netif_err(nic_dev, drv, netdev,
347 "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
348 nic_dev->flags &= ~HINIC_RSS_ENABLE;
349 nic_dev->max_qps = 1;
350 nic_dev->rss_limit = nic_dev->max_qps;
351 nic_dev->num_qps = nic_dev->max_qps;
352 nic_dev->num_rss = nic_dev->max_qps;
357 nic_dev->flags |= HINIC_RSS_ENABLE;
359 for (i = 0; i < num_online_cpus(); i++) {
360 node = cpu_to_node(i);
361 if (node == dev_to_node(&pdev->dev))
366 num_cpus = num_online_cpus();
368 nic_dev->num_qps = hinic_hwdev_num_qps(hwdev);
369 nic_dev->num_qps = min_t(u16, nic_dev->num_qps, num_cpus);
371 nic_dev->rss_limit = nic_dev->num_qps;
372 nic_dev->num_rss = nic_dev->num_qps;
374 hinic_init_rss_parameters(nic_dev);
375 err = hinic_rss_init(nic_dev);
377 netif_err(nic_dev, drv, netdev, "Failed to init rss\n");
380 int hinic_open(struct net_device *netdev)
382 struct hinic_dev *nic_dev = netdev_priv(netdev);
383 enum hinic_port_link_state link_state;
386 if (!(nic_dev->flags & HINIC_INTF_UP)) {
387 err = hinic_hwdev_ifup(nic_dev->hwdev, nic_dev->sq_depth,
390 netif_err(nic_dev, drv, netdev,
391 "Failed - HW interface up\n");
396 err = create_txqs(nic_dev);
398 netif_err(nic_dev, drv, netdev,
399 "Failed to create Tx queues\n");
400 goto err_create_txqs;
403 err = create_rxqs(nic_dev);
405 netif_err(nic_dev, drv, netdev,
406 "Failed to create Rx queues\n");
407 goto err_create_rxqs;
410 hinic_enable_rss(nic_dev);
412 err = hinic_configure_max_qnum(nic_dev);
414 netif_err(nic_dev, drv, nic_dev->netdev,
415 "Failed to configure the maximum number of queues\n");
419 netif_set_real_num_tx_queues(netdev, nic_dev->num_qps);
420 netif_set_real_num_rx_queues(netdev, nic_dev->num_qps);
422 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
424 netif_err(nic_dev, drv, netdev,
425 "Failed to set port state\n");
429 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
431 netif_err(nic_dev, drv, netdev,
432 "Failed to set func port state\n");
433 goto err_func_port_state;
436 down(&nic_dev->mgmt_lock);
438 err = hinic_port_link_state(nic_dev, &link_state);
440 netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
444 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
445 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, link_state);
447 if (link_state == HINIC_LINK_STATE_UP)
448 nic_dev->flags |= HINIC_LINK_UP;
450 nic_dev->flags |= HINIC_INTF_UP;
452 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
453 (HINIC_LINK_UP | HINIC_INTF_UP)) {
454 netif_info(nic_dev, drv, netdev, "link + intf UP\n");
455 netif_carrier_on(netdev);
456 netif_tx_wake_all_queues(netdev);
459 up(&nic_dev->mgmt_lock);
461 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
465 up(&nic_dev->mgmt_lock);
466 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
468 netif_warn(nic_dev, drv, netdev,
469 "Failed to revert func port state\n");
472 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
474 netif_warn(nic_dev, drv, netdev,
475 "Failed to revert port state\n");
478 if (nic_dev->flags & HINIC_RSS_ENABLE) {
479 hinic_rss_deinit(nic_dev);
480 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
487 if (!(nic_dev->flags & HINIC_INTF_UP))
488 hinic_hwdev_ifdown(nic_dev->hwdev);
492 int hinic_close(struct net_device *netdev)
494 struct hinic_dev *nic_dev = netdev_priv(netdev);
497 down(&nic_dev->mgmt_lock);
499 flags = nic_dev->flags;
500 nic_dev->flags &= ~HINIC_INTF_UP;
502 netif_carrier_off(netdev);
503 netif_tx_disable(netdev);
505 update_nic_stats(nic_dev);
507 up(&nic_dev->mgmt_lock);
509 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
510 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
512 hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
514 hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
516 if (nic_dev->flags & HINIC_RSS_ENABLE) {
517 hinic_rss_deinit(nic_dev);
518 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
524 if (flags & HINIC_INTF_UP)
525 hinic_hwdev_ifdown(nic_dev->hwdev);
527 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
531 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
533 struct hinic_dev *nic_dev = netdev_priv(netdev);
536 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
538 err = hinic_port_set_mtu(nic_dev, new_mtu);
540 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
542 netdev->mtu = new_mtu;
548 * change_mac_addr - change the main mac address of network device
549 * @netdev: network device
550 * @addr: mac address to set
552 * Return 0 - Success, negative - Failure
554 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
556 struct hinic_dev *nic_dev = netdev_priv(netdev);
560 if (!is_valid_ether_addr(addr))
561 return -EADDRNOTAVAIL;
563 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
564 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
566 down(&nic_dev->mgmt_lock);
569 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
571 netif_err(nic_dev, drv, netdev,
572 "Failed to delete mac\n");
576 err = hinic_port_add_mac(nic_dev, addr, vid);
578 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
582 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
583 } while (vid != VLAN_N_VID);
585 up(&nic_dev->mgmt_lock);
589 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
591 unsigned char new_mac[ETH_ALEN];
592 struct sockaddr *saddr = addr;
595 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
597 err = change_mac_addr(netdev, new_mac);
599 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
605 * add_mac_addr - add mac address to network device
606 * @netdev: network device
607 * @addr: mac address to add
609 * Return 0 - Success, negative - Failure
611 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
613 struct hinic_dev *nic_dev = netdev_priv(netdev);
617 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
618 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
620 down(&nic_dev->mgmt_lock);
623 err = hinic_port_add_mac(nic_dev, addr, vid);
625 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
629 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
630 } while (vid != VLAN_N_VID);
632 up(&nic_dev->mgmt_lock);
637 * remove_mac_addr - remove mac address from network device
638 * @netdev: network device
639 * @addr: mac address to remove
641 * Return 0 - Success, negative - Failure
643 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
645 struct hinic_dev *nic_dev = netdev_priv(netdev);
649 if (!is_valid_ether_addr(addr))
650 return -EADDRNOTAVAIL;
652 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
653 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
655 down(&nic_dev->mgmt_lock);
658 err = hinic_port_del_mac(nic_dev, addr, vid);
660 netif_err(nic_dev, drv, netdev,
661 "Failed to delete mac\n");
665 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
666 } while (vid != VLAN_N_VID);
668 up(&nic_dev->mgmt_lock);
672 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
673 __always_unused __be16 proto, u16 vid)
675 struct hinic_dev *nic_dev = netdev_priv(netdev);
678 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
680 down(&nic_dev->mgmt_lock);
682 err = hinic_port_add_vlan(nic_dev, vid);
684 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
688 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
689 if (err && err != HINIC_PF_SET_VF_ALREADY) {
690 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
694 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
696 up(&nic_dev->mgmt_lock);
700 ret = hinic_port_del_vlan(nic_dev, vid);
702 netif_err(nic_dev, drv, netdev,
703 "Failed to revert by removing vlan\n");
706 up(&nic_dev->mgmt_lock);
710 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
711 __always_unused __be16 proto, u16 vid)
713 struct hinic_dev *nic_dev = netdev_priv(netdev);
716 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
718 down(&nic_dev->mgmt_lock);
720 err = hinic_port_del_vlan(nic_dev, vid);
722 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
726 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
728 up(&nic_dev->mgmt_lock);
732 up(&nic_dev->mgmt_lock);
736 static void set_rx_mode(struct work_struct *work)
738 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
739 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
741 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
743 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
744 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
747 static void hinic_set_rx_mode(struct net_device *netdev)
749 struct hinic_dev *nic_dev = netdev_priv(netdev);
750 struct hinic_rx_mode_work *rx_mode_work;
753 rx_mode_work = &nic_dev->rx_mode_work;
755 rx_mode = HINIC_RX_MODE_UC |
759 if (netdev->flags & IFF_PROMISC) {
760 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
761 rx_mode |= HINIC_RX_MODE_PROMISC;
762 } else if (netdev->flags & IFF_ALLMULTI) {
763 rx_mode |= HINIC_RX_MODE_MC_ALL;
766 rx_mode_work->rx_mode = rx_mode;
768 queue_work(nic_dev->workq, &rx_mode_work->work);
771 static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
773 struct hinic_dev *nic_dev = netdev_priv(netdev);
774 u16 sw_pi, hw_ci, sw_ci;
778 num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
780 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
782 for (q_id = 0; q_id < num_sqs; q_id++) {
783 if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
786 sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
787 sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
788 hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
789 sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
790 netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
791 q_id, sw_pi, hw_ci, sw_ci,
792 nic_dev->txqs[q_id].napi.state);
796 static void hinic_get_stats64(struct net_device *netdev,
797 struct rtnl_link_stats64 *stats)
799 struct hinic_dev *nic_dev = netdev_priv(netdev);
800 struct hinic_rxq_stats *nic_rx_stats;
801 struct hinic_txq_stats *nic_tx_stats;
803 nic_rx_stats = &nic_dev->rx_stats;
804 nic_tx_stats = &nic_dev->tx_stats;
806 down(&nic_dev->mgmt_lock);
808 if (nic_dev->flags & HINIC_INTF_UP)
809 update_nic_stats(nic_dev);
811 up(&nic_dev->mgmt_lock);
813 stats->rx_bytes = nic_rx_stats->bytes;
814 stats->rx_packets = nic_rx_stats->pkts;
815 stats->rx_errors = nic_rx_stats->errors;
817 stats->tx_bytes = nic_tx_stats->bytes;
818 stats->tx_packets = nic_tx_stats->pkts;
819 stats->tx_errors = nic_tx_stats->tx_dropped;
822 static int hinic_set_features(struct net_device *netdev,
823 netdev_features_t features)
825 struct hinic_dev *nic_dev = netdev_priv(netdev);
827 return set_features(nic_dev, nic_dev->netdev->features,
831 static netdev_features_t hinic_fix_features(struct net_device *netdev,
832 netdev_features_t features)
834 struct hinic_dev *nic_dev = netdev_priv(netdev);
836 /* If Rx checksum is disabled, then LRO should also be disabled */
837 if (!(features & NETIF_F_RXCSUM)) {
838 netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
839 features &= ~NETIF_F_LRO;
845 static const struct net_device_ops hinic_netdev_ops = {
846 .ndo_open = hinic_open,
847 .ndo_stop = hinic_close,
848 .ndo_change_mtu = hinic_change_mtu,
849 .ndo_set_mac_address = hinic_set_mac_addr,
850 .ndo_validate_addr = eth_validate_addr,
851 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
852 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
853 .ndo_set_rx_mode = hinic_set_rx_mode,
854 .ndo_start_xmit = hinic_xmit_frame,
855 .ndo_tx_timeout = hinic_tx_timeout,
856 .ndo_get_stats64 = hinic_get_stats64,
857 .ndo_fix_features = hinic_fix_features,
858 .ndo_set_features = hinic_set_features,
859 .ndo_set_vf_mac = hinic_ndo_set_vf_mac,
860 .ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
861 .ndo_get_vf_config = hinic_ndo_get_vf_config,
862 .ndo_set_vf_trust = hinic_ndo_set_vf_trust,
863 .ndo_set_vf_rate = hinic_ndo_set_vf_bw,
864 .ndo_set_vf_spoofchk = hinic_ndo_set_vf_spoofchk,
865 .ndo_set_vf_link_state = hinic_ndo_set_vf_link_state,
868 static const struct net_device_ops hinicvf_netdev_ops = {
869 .ndo_open = hinic_open,
870 .ndo_stop = hinic_close,
871 .ndo_change_mtu = hinic_change_mtu,
872 .ndo_set_mac_address = hinic_set_mac_addr,
873 .ndo_validate_addr = eth_validate_addr,
874 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
875 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
876 .ndo_set_rx_mode = hinic_set_rx_mode,
877 .ndo_start_xmit = hinic_xmit_frame,
878 .ndo_tx_timeout = hinic_tx_timeout,
879 .ndo_get_stats64 = hinic_get_stats64,
880 .ndo_fix_features = hinic_fix_features,
881 .ndo_set_features = hinic_set_features,
884 static void netdev_features_init(struct net_device *netdev)
886 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
887 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
888 NETIF_F_RXCSUM | NETIF_F_LRO |
889 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
891 netdev->vlan_features = netdev->hw_features;
893 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
896 static void hinic_refresh_nic_cfg(struct hinic_dev *nic_dev)
898 struct hinic_nic_cfg *nic_cfg = &nic_dev->hwdev->func_to_io.nic_cfg;
899 struct hinic_pause_config pause_info = {0};
900 struct hinic_port_cap port_cap = {0};
902 if (hinic_port_get_cap(nic_dev, &port_cap))
905 mutex_lock(&nic_cfg->cfg_mutex);
906 if (nic_cfg->pause_set || !port_cap.autoneg_state) {
907 nic_cfg->auto_neg = port_cap.autoneg_state;
908 pause_info.auto_neg = nic_cfg->auto_neg;
909 pause_info.rx_pause = nic_cfg->rx_pause;
910 pause_info.tx_pause = nic_cfg->tx_pause;
911 hinic_set_hw_pause_info(nic_dev->hwdev, &pause_info);
913 mutex_unlock(&nic_cfg->cfg_mutex);
917 * link_status_event_handler - link event handler
918 * @handle: nic device for the handler
919 * @buf_in: input buffer
920 * @in_size: input size
921 * @buf_in: output buffer
922 * @out_size: returned output size
924 * Return 0 - Success, negative - Failure
926 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
927 void *buf_out, u16 *out_size)
929 struct hinic_port_link_status *link_status, *ret_link_status;
930 struct hinic_dev *nic_dev = handle;
932 link_status = buf_in;
934 if (link_status->link == HINIC_LINK_STATE_UP) {
935 down(&nic_dev->mgmt_lock);
937 nic_dev->flags |= HINIC_LINK_UP;
939 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
940 (HINIC_LINK_UP | HINIC_INTF_UP)) {
941 netif_carrier_on(nic_dev->netdev);
942 netif_tx_wake_all_queues(nic_dev->netdev);
945 up(&nic_dev->mgmt_lock);
947 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
948 hinic_refresh_nic_cfg(nic_dev);
950 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
952 down(&nic_dev->mgmt_lock);
954 nic_dev->flags &= ~HINIC_LINK_UP;
956 netif_carrier_off(nic_dev->netdev);
957 netif_tx_disable(nic_dev->netdev);
959 up(&nic_dev->mgmt_lock);
961 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
964 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
965 hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
968 ret_link_status = buf_out;
969 ret_link_status->status = 0;
971 *out_size = sizeof(*ret_link_status);
974 static int set_features(struct hinic_dev *nic_dev,
975 netdev_features_t pre_features,
976 netdev_features_t features, bool force_change)
978 netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
979 u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
980 netdev_features_t failed_features = 0;
984 if (changed & NETIF_F_TSO) {
985 ret = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
986 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
989 failed_features |= NETIF_F_TSO;
993 if (changed & NETIF_F_RXCSUM) {
994 ret = hinic_set_rx_csum_offload(nic_dev, csum_en);
997 failed_features |= NETIF_F_RXCSUM;
1001 if (changed & NETIF_F_LRO) {
1002 ret = hinic_set_rx_lro_state(nic_dev,
1003 !!(features & NETIF_F_LRO),
1004 HINIC_LRO_RX_TIMER_DEFAULT,
1005 HINIC_LRO_MAX_WQE_NUM_DEFAULT);
1008 failed_features |= NETIF_F_LRO;
1012 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1013 ret = hinic_set_rx_vlan_offload(nic_dev,
1015 NETIF_F_HW_VLAN_CTAG_RX));
1018 failed_features |= NETIF_F_HW_VLAN_CTAG_RX;
1023 nic_dev->netdev->features = features ^ failed_features;
1030 static int hinic_init_intr_coalesce(struct hinic_dev *nic_dev)
1035 size = sizeof(struct hinic_intr_coal_info) * nic_dev->max_qps;
1036 nic_dev->rx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1037 if (!nic_dev->rx_intr_coalesce)
1039 nic_dev->tx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1040 if (!nic_dev->tx_intr_coalesce) {
1041 kfree(nic_dev->rx_intr_coalesce);
1045 for (i = 0; i < nic_dev->max_qps; i++) {
1046 nic_dev->rx_intr_coalesce[i].pending_limt =
1047 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1048 nic_dev->rx_intr_coalesce[i].coalesce_timer_cfg =
1049 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1050 nic_dev->rx_intr_coalesce[i].resend_timer_cfg =
1051 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1052 nic_dev->tx_intr_coalesce[i].pending_limt =
1053 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1054 nic_dev->tx_intr_coalesce[i].coalesce_timer_cfg =
1055 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1056 nic_dev->tx_intr_coalesce[i].resend_timer_cfg =
1057 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1063 static void hinic_free_intr_coalesce(struct hinic_dev *nic_dev)
1065 kfree(nic_dev->tx_intr_coalesce);
1066 kfree(nic_dev->rx_intr_coalesce);
1070 * nic_dev_init - Initialize the NIC device
1071 * @pdev: the NIC pci device
1073 * Return 0 - Success, negative - Failure
1075 static int nic_dev_init(struct pci_dev *pdev)
1077 struct hinic_rx_mode_work *rx_mode_work;
1078 struct hinic_txq_stats *tx_stats;
1079 struct hinic_rxq_stats *rx_stats;
1080 struct hinic_devlink_priv *priv;
1081 struct hinic_dev *nic_dev;
1082 struct net_device *netdev;
1083 struct hinic_hwdev *hwdev;
1084 struct devlink *devlink;
1087 hwdev = hinic_init_hwdev(pdev);
1088 if (IS_ERR(hwdev)) {
1089 dev_err(&pdev->dev, "Failed to initialize HW device\n");
1090 return PTR_ERR(hwdev);
1093 devlink = hinic_devlink_alloc();
1095 dev_err(&pdev->dev, "Hinic devlink alloc failed\n");
1097 goto err_devlink_alloc;
1100 priv = devlink_priv(devlink);
1101 priv->hwdev = hwdev;
1103 num_qps = hinic_hwdev_num_qps(hwdev);
1105 dev_err(&pdev->dev, "Invalid number of QPS\n");
1110 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1112 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1114 goto err_alloc_etherdev;
1117 if (!HINIC_IS_VF(hwdev->hwif))
1118 netdev->netdev_ops = &hinic_netdev_ops;
1120 netdev->netdev_ops = &hinicvf_netdev_ops;
1122 netdev->max_mtu = ETH_MAX_MTU;
1124 nic_dev = netdev_priv(netdev);
1125 nic_dev->netdev = netdev;
1126 nic_dev->hwdev = hwdev;
1127 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1129 nic_dev->txqs = NULL;
1130 nic_dev->rxqs = NULL;
1131 nic_dev->tx_weight = tx_weight;
1132 nic_dev->rx_weight = rx_weight;
1133 nic_dev->sq_depth = HINIC_SQ_DEPTH;
1134 nic_dev->rq_depth = HINIC_RQ_DEPTH;
1135 nic_dev->sriov_info.hwdev = hwdev;
1136 nic_dev->sriov_info.pdev = pdev;
1137 nic_dev->max_qps = num_qps;
1138 nic_dev->devlink = devlink;
1140 hinic_set_ethtool_ops(netdev);
1142 sema_init(&nic_dev->mgmt_lock, 1);
1144 tx_stats = &nic_dev->tx_stats;
1145 rx_stats = &nic_dev->rx_stats;
1147 u64_stats_init(&tx_stats->syncp);
1148 u64_stats_init(&rx_stats->syncp);
1150 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1151 VLAN_BITMAP_SIZE(nic_dev),
1153 if (!nic_dev->vlan_bitmap) {
1155 goto err_vlan_bitmap;
1158 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1159 if (!nic_dev->workq) {
1164 err = hinic_devlink_register(devlink, &pdev->dev);
1166 goto err_devlink_reg;
1168 pci_set_drvdata(pdev, netdev);
1170 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1172 dev_err(&pdev->dev, "Failed to get mac address\n");
1176 if (!is_valid_ether_addr(netdev->dev_addr)) {
1177 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1178 dev_err(&pdev->dev, "Invalid MAC address\n");
1183 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1185 eth_hw_addr_random(netdev);
1188 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1189 if (err && err != HINIC_PF_SET_VF_ALREADY) {
1190 dev_err(&pdev->dev, "Failed to add mac\n");
1194 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1196 dev_err(&pdev->dev, "Failed to set mtu\n");
1200 rx_mode_work = &nic_dev->rx_mode_work;
1201 INIT_WORK(&rx_mode_work->work, set_rx_mode);
1203 netdev_features_init(netdev);
1205 netif_carrier_off(netdev);
1207 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1208 nic_dev, link_status_event_handler);
1210 err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1212 goto err_set_features;
1214 /* enable pause and disable pfc by default */
1215 err = hinic_dcb_set_pfc(nic_dev->hwdev, 0, 0);
1219 SET_NETDEV_DEV(netdev, &pdev->dev);
1221 err = hinic_init_intr_coalesce(nic_dev);
1223 dev_err(&pdev->dev, "Failed to init_intr_coalesce\n");
1227 err = register_netdev(netdev);
1229 dev_err(&pdev->dev, "Failed to register netdev\n");
1230 goto err_reg_netdev;
1236 hinic_free_intr_coalesce(nic_dev);
1240 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1241 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1242 cancel_work_sync(&rx_mode_work->work);
1245 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1248 pci_set_drvdata(pdev, NULL);
1250 destroy_workqueue(nic_dev->workq);
1254 free_netdev(netdev);
1259 hinic_free_hwdev(hwdev);
1263 static int hinic_probe(struct pci_dev *pdev,
1264 const struct pci_device_id *id)
1266 int err = pci_enable_device(pdev);
1269 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1273 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1275 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1276 goto err_pci_regions;
1279 pci_set_master(pdev);
1281 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1283 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1284 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1286 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1291 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1293 dev_warn(&pdev->dev,
1294 "Couldn't set 64-bit consistent DMA mask\n");
1295 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1298 "Failed to set consistent DMA mask\n");
1299 goto err_dma_consistent_mask;
1303 err = nic_dev_init(pdev);
1305 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1306 goto err_nic_dev_init;
1309 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1313 err_dma_consistent_mask:
1315 pci_release_regions(pdev);
1318 pci_disable_device(pdev);
1322 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
1324 static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1326 struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1329 set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1330 usleep_range(9900, 10000);
1332 while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1333 if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1334 !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1337 usleep_range(9900, 10000);
1342 static void hinic_remove(struct pci_dev *pdev)
1344 struct net_device *netdev = pci_get_drvdata(pdev);
1345 struct hinic_dev *nic_dev = netdev_priv(netdev);
1346 struct hinic_rx_mode_work *rx_mode_work;
1348 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1349 wait_sriov_cfg_complete(nic_dev);
1350 hinic_pci_sriov_disable(pdev);
1353 unregister_netdev(netdev);
1355 hinic_free_intr_coalesce(nic_dev);
1357 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1359 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1360 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1362 rx_mode_work = &nic_dev->rx_mode_work;
1363 cancel_work_sync(&rx_mode_work->work);
1365 pci_set_drvdata(pdev, NULL);
1367 hinic_devlink_unregister(nic_dev->devlink);
1369 destroy_workqueue(nic_dev->workq);
1371 hinic_devlink_free(nic_dev->devlink);
1373 hinic_free_hwdev(nic_dev->hwdev);
1375 free_netdev(netdev);
1377 pci_release_regions(pdev);
1378 pci_disable_device(pdev);
1380 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1383 static void hinic_shutdown(struct pci_dev *pdev)
1385 pci_disable_device(pdev);
1388 static const struct pci_device_id hinic_pci_table[] = {
1389 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1390 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1391 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1392 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1393 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1396 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1398 static struct pci_driver hinic_driver = {
1399 .name = HINIC_DRV_NAME,
1400 .id_table = hinic_pci_table,
1401 .probe = hinic_probe,
1402 .remove = hinic_remove,
1403 .shutdown = hinic_shutdown,
1404 .sriov_configure = hinic_pci_sriov_configure,
1407 module_pci_driver(hinic_driver);