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;
449 nic_dev->cable_unplugged = false;
450 nic_dev->module_unrecognized = false;
453 nic_dev->flags |= HINIC_INTF_UP;
455 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
456 (HINIC_LINK_UP | HINIC_INTF_UP)) {
457 netif_info(nic_dev, drv, netdev, "link + intf UP\n");
458 netif_carrier_on(netdev);
459 netif_tx_wake_all_queues(netdev);
462 up(&nic_dev->mgmt_lock);
464 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
468 up(&nic_dev->mgmt_lock);
469 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
471 netif_warn(nic_dev, drv, netdev,
472 "Failed to revert func port state\n");
475 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
477 netif_warn(nic_dev, drv, netdev,
478 "Failed to revert port state\n");
481 if (nic_dev->flags & HINIC_RSS_ENABLE) {
482 hinic_rss_deinit(nic_dev);
483 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
490 if (!(nic_dev->flags & HINIC_INTF_UP))
491 hinic_hwdev_ifdown(nic_dev->hwdev);
495 int hinic_close(struct net_device *netdev)
497 struct hinic_dev *nic_dev = netdev_priv(netdev);
500 down(&nic_dev->mgmt_lock);
502 flags = nic_dev->flags;
503 nic_dev->flags &= ~HINIC_INTF_UP;
505 netif_carrier_off(netdev);
506 netif_tx_disable(netdev);
508 update_nic_stats(nic_dev);
510 up(&nic_dev->mgmt_lock);
512 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
513 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
515 hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
517 hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
519 if (nic_dev->flags & HINIC_RSS_ENABLE) {
520 hinic_rss_deinit(nic_dev);
521 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
527 if (flags & HINIC_INTF_UP)
528 hinic_hwdev_ifdown(nic_dev->hwdev);
530 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
534 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
536 struct hinic_dev *nic_dev = netdev_priv(netdev);
539 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
541 err = hinic_port_set_mtu(nic_dev, new_mtu);
543 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
545 netdev->mtu = new_mtu;
551 * change_mac_addr - change the main mac address of network device
552 * @netdev: network device
553 * @addr: mac address to set
555 * Return 0 - Success, negative - Failure
557 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
559 struct hinic_dev *nic_dev = netdev_priv(netdev);
563 if (!is_valid_ether_addr(addr))
564 return -EADDRNOTAVAIL;
566 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
567 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
569 down(&nic_dev->mgmt_lock);
572 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
574 netif_err(nic_dev, drv, netdev,
575 "Failed to delete mac\n");
579 err = hinic_port_add_mac(nic_dev, addr, vid);
581 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
585 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
586 } while (vid != VLAN_N_VID);
588 up(&nic_dev->mgmt_lock);
592 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
594 unsigned char new_mac[ETH_ALEN];
595 struct sockaddr *saddr = addr;
598 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
600 err = change_mac_addr(netdev, new_mac);
602 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
608 * add_mac_addr - add mac address to network device
609 * @netdev: network device
610 * @addr: mac address to add
612 * Return 0 - Success, negative - Failure
614 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
616 struct hinic_dev *nic_dev = netdev_priv(netdev);
620 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
621 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
623 down(&nic_dev->mgmt_lock);
626 err = hinic_port_add_mac(nic_dev, addr, vid);
628 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
632 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
633 } while (vid != VLAN_N_VID);
635 up(&nic_dev->mgmt_lock);
640 * remove_mac_addr - remove mac address from network device
641 * @netdev: network device
642 * @addr: mac address to remove
644 * Return 0 - Success, negative - Failure
646 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
648 struct hinic_dev *nic_dev = netdev_priv(netdev);
652 if (!is_valid_ether_addr(addr))
653 return -EADDRNOTAVAIL;
655 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
656 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
658 down(&nic_dev->mgmt_lock);
661 err = hinic_port_del_mac(nic_dev, addr, vid);
663 netif_err(nic_dev, drv, netdev,
664 "Failed to delete mac\n");
668 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
669 } while (vid != VLAN_N_VID);
671 up(&nic_dev->mgmt_lock);
675 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
676 __always_unused __be16 proto, u16 vid)
678 struct hinic_dev *nic_dev = netdev_priv(netdev);
681 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
683 down(&nic_dev->mgmt_lock);
685 err = hinic_port_add_vlan(nic_dev, vid);
687 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
691 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
692 if (err && err != HINIC_PF_SET_VF_ALREADY) {
693 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
697 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
699 up(&nic_dev->mgmt_lock);
703 ret = hinic_port_del_vlan(nic_dev, vid);
705 netif_err(nic_dev, drv, netdev,
706 "Failed to revert by removing vlan\n");
709 up(&nic_dev->mgmt_lock);
713 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
714 __always_unused __be16 proto, u16 vid)
716 struct hinic_dev *nic_dev = netdev_priv(netdev);
719 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
721 down(&nic_dev->mgmt_lock);
723 err = hinic_port_del_vlan(nic_dev, vid);
725 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
729 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
731 up(&nic_dev->mgmt_lock);
735 up(&nic_dev->mgmt_lock);
739 static void set_rx_mode(struct work_struct *work)
741 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
742 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
744 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
746 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
747 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
750 static void hinic_set_rx_mode(struct net_device *netdev)
752 struct hinic_dev *nic_dev = netdev_priv(netdev);
753 struct hinic_rx_mode_work *rx_mode_work;
756 rx_mode_work = &nic_dev->rx_mode_work;
758 rx_mode = HINIC_RX_MODE_UC |
762 if (netdev->flags & IFF_PROMISC) {
763 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
764 rx_mode |= HINIC_RX_MODE_PROMISC;
765 } else if (netdev->flags & IFF_ALLMULTI) {
766 rx_mode |= HINIC_RX_MODE_MC_ALL;
769 rx_mode_work->rx_mode = rx_mode;
771 queue_work(nic_dev->workq, &rx_mode_work->work);
774 static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
776 struct hinic_dev *nic_dev = netdev_priv(netdev);
777 u16 sw_pi, hw_ci, sw_ci;
781 num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
783 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
785 for (q_id = 0; q_id < num_sqs; q_id++) {
786 if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
789 sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
790 sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
791 hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
792 sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
793 netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
794 q_id, sw_pi, hw_ci, sw_ci,
795 nic_dev->txqs[q_id].napi.state);
799 static void hinic_get_stats64(struct net_device *netdev,
800 struct rtnl_link_stats64 *stats)
802 struct hinic_dev *nic_dev = netdev_priv(netdev);
803 struct hinic_rxq_stats *nic_rx_stats;
804 struct hinic_txq_stats *nic_tx_stats;
806 nic_rx_stats = &nic_dev->rx_stats;
807 nic_tx_stats = &nic_dev->tx_stats;
809 down(&nic_dev->mgmt_lock);
811 if (nic_dev->flags & HINIC_INTF_UP)
812 update_nic_stats(nic_dev);
814 up(&nic_dev->mgmt_lock);
816 stats->rx_bytes = nic_rx_stats->bytes;
817 stats->rx_packets = nic_rx_stats->pkts;
818 stats->rx_errors = nic_rx_stats->errors;
820 stats->tx_bytes = nic_tx_stats->bytes;
821 stats->tx_packets = nic_tx_stats->pkts;
822 stats->tx_errors = nic_tx_stats->tx_dropped;
825 static int hinic_set_features(struct net_device *netdev,
826 netdev_features_t features)
828 struct hinic_dev *nic_dev = netdev_priv(netdev);
830 return set_features(nic_dev, nic_dev->netdev->features,
834 static netdev_features_t hinic_fix_features(struct net_device *netdev,
835 netdev_features_t features)
837 struct hinic_dev *nic_dev = netdev_priv(netdev);
839 /* If Rx checksum is disabled, then LRO should also be disabled */
840 if (!(features & NETIF_F_RXCSUM)) {
841 netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
842 features &= ~NETIF_F_LRO;
848 static const struct net_device_ops hinic_netdev_ops = {
849 .ndo_open = hinic_open,
850 .ndo_stop = hinic_close,
851 .ndo_change_mtu = hinic_change_mtu,
852 .ndo_set_mac_address = hinic_set_mac_addr,
853 .ndo_validate_addr = eth_validate_addr,
854 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
855 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
856 .ndo_set_rx_mode = hinic_set_rx_mode,
857 .ndo_start_xmit = hinic_xmit_frame,
858 .ndo_tx_timeout = hinic_tx_timeout,
859 .ndo_get_stats64 = hinic_get_stats64,
860 .ndo_fix_features = hinic_fix_features,
861 .ndo_set_features = hinic_set_features,
862 .ndo_set_vf_mac = hinic_ndo_set_vf_mac,
863 .ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
864 .ndo_get_vf_config = hinic_ndo_get_vf_config,
865 .ndo_set_vf_trust = hinic_ndo_set_vf_trust,
866 .ndo_set_vf_rate = hinic_ndo_set_vf_bw,
867 .ndo_set_vf_spoofchk = hinic_ndo_set_vf_spoofchk,
868 .ndo_set_vf_link_state = hinic_ndo_set_vf_link_state,
871 static const struct net_device_ops hinicvf_netdev_ops = {
872 .ndo_open = hinic_open,
873 .ndo_stop = hinic_close,
874 .ndo_change_mtu = hinic_change_mtu,
875 .ndo_set_mac_address = hinic_set_mac_addr,
876 .ndo_validate_addr = eth_validate_addr,
877 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
878 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
879 .ndo_set_rx_mode = hinic_set_rx_mode,
880 .ndo_start_xmit = hinic_xmit_frame,
881 .ndo_tx_timeout = hinic_tx_timeout,
882 .ndo_get_stats64 = hinic_get_stats64,
883 .ndo_fix_features = hinic_fix_features,
884 .ndo_set_features = hinic_set_features,
887 static void netdev_features_init(struct net_device *netdev)
889 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
890 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
891 NETIF_F_RXCSUM | NETIF_F_LRO |
892 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
894 netdev->vlan_features = netdev->hw_features;
896 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
899 static void hinic_refresh_nic_cfg(struct hinic_dev *nic_dev)
901 struct hinic_nic_cfg *nic_cfg = &nic_dev->hwdev->func_to_io.nic_cfg;
902 struct hinic_pause_config pause_info = {0};
903 struct hinic_port_cap port_cap = {0};
905 if (hinic_port_get_cap(nic_dev, &port_cap))
908 mutex_lock(&nic_cfg->cfg_mutex);
909 if (nic_cfg->pause_set || !port_cap.autoneg_state) {
910 nic_cfg->auto_neg = port_cap.autoneg_state;
911 pause_info.auto_neg = nic_cfg->auto_neg;
912 pause_info.rx_pause = nic_cfg->rx_pause;
913 pause_info.tx_pause = nic_cfg->tx_pause;
914 hinic_set_hw_pause_info(nic_dev->hwdev, &pause_info);
916 mutex_unlock(&nic_cfg->cfg_mutex);
920 * link_status_event_handler - link event handler
921 * @handle: nic device for the handler
922 * @buf_in: input buffer
923 * @in_size: input size
924 * @buf_in: output buffer
925 * @out_size: returned output size
927 * Return 0 - Success, negative - Failure
929 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
930 void *buf_out, u16 *out_size)
932 struct hinic_port_link_status *link_status, *ret_link_status;
933 struct hinic_dev *nic_dev = handle;
935 link_status = buf_in;
937 if (link_status->link == HINIC_LINK_STATE_UP) {
938 down(&nic_dev->mgmt_lock);
940 nic_dev->flags |= HINIC_LINK_UP;
941 nic_dev->cable_unplugged = false;
942 nic_dev->module_unrecognized = false;
944 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
945 (HINIC_LINK_UP | HINIC_INTF_UP)) {
946 netif_carrier_on(nic_dev->netdev);
947 netif_tx_wake_all_queues(nic_dev->netdev);
950 up(&nic_dev->mgmt_lock);
952 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
953 hinic_refresh_nic_cfg(nic_dev);
955 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
957 down(&nic_dev->mgmt_lock);
959 nic_dev->flags &= ~HINIC_LINK_UP;
961 netif_carrier_off(nic_dev->netdev);
962 netif_tx_disable(nic_dev->netdev);
964 up(&nic_dev->mgmt_lock);
966 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
969 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
970 hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
973 ret_link_status = buf_out;
974 ret_link_status->status = 0;
976 *out_size = sizeof(*ret_link_status);
979 static void cable_plug_event(void *handle,
980 void *buf_in, u16 in_size,
981 void *buf_out, u16 *out_size)
983 struct hinic_cable_plug_event *plug_event = buf_in;
984 struct hinic_dev *nic_dev = handle;
986 nic_dev->cable_unplugged = plug_event->plugged ? false : true;
988 *out_size = sizeof(*plug_event);
989 plug_event = buf_out;
990 plug_event->status = 0;
993 static void link_err_event(void *handle,
994 void *buf_in, u16 in_size,
995 void *buf_out, u16 *out_size)
997 struct hinic_link_err_event *link_err = buf_in;
998 struct hinic_dev *nic_dev = handle;
1000 if (link_err->err_type >= LINK_ERR_NUM)
1001 netif_info(nic_dev, link, nic_dev->netdev,
1002 "Link failed, Unknown error type: 0x%x\n",
1003 link_err->err_type);
1005 nic_dev->module_unrecognized = true;
1007 *out_size = sizeof(*link_err);
1009 link_err->status = 0;
1012 static int set_features(struct hinic_dev *nic_dev,
1013 netdev_features_t pre_features,
1014 netdev_features_t features, bool force_change)
1016 netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
1017 u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
1018 netdev_features_t failed_features = 0;
1022 if (changed & NETIF_F_TSO) {
1023 ret = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
1024 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
1027 failed_features |= NETIF_F_TSO;
1031 if (changed & NETIF_F_RXCSUM) {
1032 ret = hinic_set_rx_csum_offload(nic_dev, csum_en);
1035 failed_features |= NETIF_F_RXCSUM;
1039 if (changed & NETIF_F_LRO) {
1040 ret = hinic_set_rx_lro_state(nic_dev,
1041 !!(features & NETIF_F_LRO),
1042 HINIC_LRO_RX_TIMER_DEFAULT,
1043 HINIC_LRO_MAX_WQE_NUM_DEFAULT);
1046 failed_features |= NETIF_F_LRO;
1050 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1051 ret = hinic_set_rx_vlan_offload(nic_dev,
1053 NETIF_F_HW_VLAN_CTAG_RX));
1056 failed_features |= NETIF_F_HW_VLAN_CTAG_RX;
1061 nic_dev->netdev->features = features ^ failed_features;
1068 static int hinic_init_intr_coalesce(struct hinic_dev *nic_dev)
1073 size = sizeof(struct hinic_intr_coal_info) * nic_dev->max_qps;
1074 nic_dev->rx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1075 if (!nic_dev->rx_intr_coalesce)
1077 nic_dev->tx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1078 if (!nic_dev->tx_intr_coalesce) {
1079 kfree(nic_dev->rx_intr_coalesce);
1083 for (i = 0; i < nic_dev->max_qps; i++) {
1084 nic_dev->rx_intr_coalesce[i].pending_limt =
1085 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1086 nic_dev->rx_intr_coalesce[i].coalesce_timer_cfg =
1087 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1088 nic_dev->rx_intr_coalesce[i].resend_timer_cfg =
1089 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1090 nic_dev->tx_intr_coalesce[i].pending_limt =
1091 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1092 nic_dev->tx_intr_coalesce[i].coalesce_timer_cfg =
1093 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1094 nic_dev->tx_intr_coalesce[i].resend_timer_cfg =
1095 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1101 static void hinic_free_intr_coalesce(struct hinic_dev *nic_dev)
1103 kfree(nic_dev->tx_intr_coalesce);
1104 kfree(nic_dev->rx_intr_coalesce);
1108 * nic_dev_init - Initialize the NIC device
1109 * @pdev: the NIC pci device
1111 * Return 0 - Success, negative - Failure
1113 static int nic_dev_init(struct pci_dev *pdev)
1115 struct hinic_rx_mode_work *rx_mode_work;
1116 struct hinic_txq_stats *tx_stats;
1117 struct hinic_rxq_stats *rx_stats;
1118 struct hinic_dev *nic_dev;
1119 struct net_device *netdev;
1120 struct hinic_hwdev *hwdev;
1121 struct devlink *devlink;
1124 devlink = hinic_devlink_alloc();
1126 dev_err(&pdev->dev, "Hinic devlink alloc failed\n");
1130 hwdev = hinic_init_hwdev(pdev, devlink);
1131 if (IS_ERR(hwdev)) {
1132 dev_err(&pdev->dev, "Failed to initialize HW device\n");
1133 hinic_devlink_free(devlink);
1134 return PTR_ERR(hwdev);
1137 num_qps = hinic_hwdev_num_qps(hwdev);
1139 dev_err(&pdev->dev, "Invalid number of QPS\n");
1144 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1146 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1148 goto err_alloc_etherdev;
1151 if (!HINIC_IS_VF(hwdev->hwif))
1152 netdev->netdev_ops = &hinic_netdev_ops;
1154 netdev->netdev_ops = &hinicvf_netdev_ops;
1156 netdev->max_mtu = ETH_MAX_MTU;
1158 nic_dev = netdev_priv(netdev);
1159 nic_dev->netdev = netdev;
1160 nic_dev->hwdev = hwdev;
1161 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1163 nic_dev->txqs = NULL;
1164 nic_dev->rxqs = NULL;
1165 nic_dev->tx_weight = tx_weight;
1166 nic_dev->rx_weight = rx_weight;
1167 nic_dev->sq_depth = HINIC_SQ_DEPTH;
1168 nic_dev->rq_depth = HINIC_RQ_DEPTH;
1169 nic_dev->sriov_info.hwdev = hwdev;
1170 nic_dev->sriov_info.pdev = pdev;
1171 nic_dev->max_qps = num_qps;
1172 nic_dev->devlink = devlink;
1174 hinic_set_ethtool_ops(netdev);
1176 sema_init(&nic_dev->mgmt_lock, 1);
1178 tx_stats = &nic_dev->tx_stats;
1179 rx_stats = &nic_dev->rx_stats;
1181 u64_stats_init(&tx_stats->syncp);
1182 u64_stats_init(&rx_stats->syncp);
1184 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1185 VLAN_BITMAP_SIZE(nic_dev),
1187 if (!nic_dev->vlan_bitmap) {
1189 goto err_vlan_bitmap;
1192 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1193 if (!nic_dev->workq) {
1198 pci_set_drvdata(pdev, netdev);
1200 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1202 dev_err(&pdev->dev, "Failed to get mac address\n");
1206 if (!is_valid_ether_addr(netdev->dev_addr)) {
1207 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1208 dev_err(&pdev->dev, "Invalid MAC address\n");
1213 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1215 eth_hw_addr_random(netdev);
1218 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1219 if (err && err != HINIC_PF_SET_VF_ALREADY) {
1220 dev_err(&pdev->dev, "Failed to add mac\n");
1224 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1226 dev_err(&pdev->dev, "Failed to set mtu\n");
1230 rx_mode_work = &nic_dev->rx_mode_work;
1231 INIT_WORK(&rx_mode_work->work, set_rx_mode);
1233 netdev_features_init(netdev);
1235 netif_carrier_off(netdev);
1237 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1238 nic_dev, link_status_event_handler);
1239 hinic_hwdev_cb_register(nic_dev->hwdev,
1240 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT,
1241 nic_dev, cable_plug_event);
1242 hinic_hwdev_cb_register(nic_dev->hwdev,
1243 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT,
1244 nic_dev, link_err_event);
1246 err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1248 goto err_set_features;
1250 /* enable pause and disable pfc by default */
1251 err = hinic_dcb_set_pfc(nic_dev->hwdev, 0, 0);
1255 SET_NETDEV_DEV(netdev, &pdev->dev);
1257 err = hinic_init_intr_coalesce(nic_dev);
1259 dev_err(&pdev->dev, "Failed to init_intr_coalesce\n");
1263 err = register_netdev(netdev);
1265 dev_err(&pdev->dev, "Failed to register netdev\n");
1266 goto err_reg_netdev;
1272 hinic_free_intr_coalesce(nic_dev);
1276 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1277 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1278 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1279 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1280 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1281 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1282 cancel_work_sync(&rx_mode_work->work);
1285 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1288 pci_set_drvdata(pdev, NULL);
1289 destroy_workqueue(nic_dev->workq);
1292 free_netdev(netdev);
1296 hinic_free_hwdev(hwdev);
1297 hinic_devlink_free(devlink);
1301 static int hinic_probe(struct pci_dev *pdev,
1302 const struct pci_device_id *id)
1304 int err = pci_enable_device(pdev);
1307 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1311 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1313 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1314 goto err_pci_regions;
1317 pci_set_master(pdev);
1319 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1321 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1322 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1324 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1329 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1331 dev_warn(&pdev->dev,
1332 "Couldn't set 64-bit consistent DMA mask\n");
1333 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1336 "Failed to set consistent DMA mask\n");
1337 goto err_dma_consistent_mask;
1341 err = nic_dev_init(pdev);
1343 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1344 goto err_nic_dev_init;
1347 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1351 err_dma_consistent_mask:
1353 pci_release_regions(pdev);
1356 pci_disable_device(pdev);
1360 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
1362 static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1364 struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1367 set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1368 usleep_range(9900, 10000);
1370 while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1371 if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1372 !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1375 usleep_range(9900, 10000);
1380 static void hinic_remove(struct pci_dev *pdev)
1382 struct net_device *netdev = pci_get_drvdata(pdev);
1383 struct hinic_dev *nic_dev = netdev_priv(netdev);
1384 struct devlink *devlink = nic_dev->devlink;
1385 struct hinic_rx_mode_work *rx_mode_work;
1387 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1388 wait_sriov_cfg_complete(nic_dev);
1389 hinic_pci_sriov_disable(pdev);
1392 unregister_netdev(netdev);
1394 hinic_free_intr_coalesce(nic_dev);
1396 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1398 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1399 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1400 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1401 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1402 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1403 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1405 rx_mode_work = &nic_dev->rx_mode_work;
1406 cancel_work_sync(&rx_mode_work->work);
1408 pci_set_drvdata(pdev, NULL);
1410 destroy_workqueue(nic_dev->workq);
1412 hinic_free_hwdev(nic_dev->hwdev);
1414 free_netdev(netdev);
1416 hinic_devlink_free(devlink);
1418 pci_release_regions(pdev);
1419 pci_disable_device(pdev);
1421 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1424 static void hinic_shutdown(struct pci_dev *pdev)
1426 pci_disable_device(pdev);
1429 static const struct pci_device_id hinic_pci_table[] = {
1430 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1431 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1432 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1433 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1434 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1437 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1439 static struct pci_driver hinic_driver = {
1440 .name = HINIC_DRV_NAME,
1441 .id_table = hinic_pci_table,
1442 .probe = hinic_probe,
1443 .remove = hinic_remove,
1444 .shutdown = hinic_shutdown,
1445 .sriov_configure = hinic_pci_sriov_configure,
1448 module_pci_driver(hinic_driver);