2 * Huawei HiNIC PCI Express Linux driver
3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/pci.h>
20 #include <linux/device.h>
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/etherdevice.h>
24 #include <linux/netdevice.h>
25 #include <linux/slab.h>
26 #include <linux/if_vlan.h>
27 #include <linux/semaphore.h>
28 #include <linux/workqueue.h>
30 #include <linux/bitops.h>
31 #include <linux/bitmap.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
35 #include "hinic_hw_qp.h"
36 #include "hinic_hw_dev.h"
37 #include "hinic_port.h"
40 #include "hinic_dev.h"
42 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
43 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
44 MODULE_LICENSE("GPL");
46 static unsigned int tx_weight = 64;
47 module_param(tx_weight, uint, 0644);
48 MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
50 static unsigned int rx_weight = 64;
51 module_param(rx_weight, uint, 0644);
52 MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
54 #define PCI_DEVICE_ID_HI1822_PF 0x1822
56 #define HINIC_WQ_NAME "hinic_dev"
58 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
60 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
62 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
64 #define work_to_rx_mode_work(work) \
65 container_of(work, struct hinic_rx_mode_work, work)
67 #define rx_mode_work_to_nic_dev(rx_mode_work) \
68 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
70 static int change_mac_addr(struct net_device *netdev, const u8 *addr);
72 static void set_link_speed(struct ethtool_link_ksettings *link_ksettings,
73 enum hinic_speed speed)
76 case HINIC_SPEED_10MB_LINK:
77 link_ksettings->base.speed = SPEED_10;
80 case HINIC_SPEED_100MB_LINK:
81 link_ksettings->base.speed = SPEED_100;
84 case HINIC_SPEED_1000MB_LINK:
85 link_ksettings->base.speed = SPEED_1000;
88 case HINIC_SPEED_10GB_LINK:
89 link_ksettings->base.speed = SPEED_10000;
92 case HINIC_SPEED_25GB_LINK:
93 link_ksettings->base.speed = SPEED_25000;
96 case HINIC_SPEED_40GB_LINK:
97 link_ksettings->base.speed = SPEED_40000;
100 case HINIC_SPEED_100GB_LINK:
101 link_ksettings->base.speed = SPEED_100000;
105 link_ksettings->base.speed = SPEED_UNKNOWN;
110 static int hinic_get_link_ksettings(struct net_device *netdev,
111 struct ethtool_link_ksettings
114 struct hinic_dev *nic_dev = netdev_priv(netdev);
115 enum hinic_port_link_state link_state;
116 struct hinic_port_cap port_cap;
119 ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
120 ethtool_link_ksettings_add_link_mode(link_ksettings, supported,
123 link_ksettings->base.speed = SPEED_UNKNOWN;
124 link_ksettings->base.autoneg = AUTONEG_DISABLE;
125 link_ksettings->base.duplex = DUPLEX_UNKNOWN;
127 err = hinic_port_get_cap(nic_dev, &port_cap);
129 netif_err(nic_dev, drv, netdev,
130 "Failed to get port capabilities\n");
134 err = hinic_port_link_state(nic_dev, &link_state);
136 netif_err(nic_dev, drv, netdev,
137 "Failed to get port link state\n");
141 if (link_state != HINIC_LINK_STATE_UP) {
142 netif_info(nic_dev, drv, netdev, "No link\n");
146 set_link_speed(link_ksettings, port_cap.speed);
148 if (!!(port_cap.autoneg_cap & HINIC_AUTONEG_SUPPORTED))
149 ethtool_link_ksettings_add_link_mode(link_ksettings,
150 advertising, Autoneg);
152 if (port_cap.autoneg_state == HINIC_AUTONEG_ACTIVE)
153 link_ksettings->base.autoneg = AUTONEG_ENABLE;
155 link_ksettings->base.duplex = (port_cap.duplex == HINIC_DUPLEX_FULL) ?
156 DUPLEX_FULL : DUPLEX_HALF;
160 static void hinic_get_drvinfo(struct net_device *netdev,
161 struct ethtool_drvinfo *info)
163 struct hinic_dev *nic_dev = netdev_priv(netdev);
164 struct hinic_hwdev *hwdev = nic_dev->hwdev;
165 struct hinic_hwif *hwif = hwdev->hwif;
167 strlcpy(info->driver, HINIC_DRV_NAME, sizeof(info->driver));
168 strlcpy(info->bus_info, pci_name(hwif->pdev), sizeof(info->bus_info));
171 static void hinic_get_ringparam(struct net_device *netdev,
172 struct ethtool_ringparam *ring)
174 ring->rx_max_pending = HINIC_RQ_DEPTH;
175 ring->tx_max_pending = HINIC_SQ_DEPTH;
176 ring->rx_pending = HINIC_RQ_DEPTH;
177 ring->tx_pending = HINIC_SQ_DEPTH;
180 static void hinic_get_channels(struct net_device *netdev,
181 struct ethtool_channels *channels)
183 struct hinic_dev *nic_dev = netdev_priv(netdev);
184 struct hinic_hwdev *hwdev = nic_dev->hwdev;
186 channels->max_rx = hwdev->nic_cap.max_qps;
187 channels->max_tx = hwdev->nic_cap.max_qps;
188 channels->max_other = 0;
189 channels->max_combined = 0;
190 channels->rx_count = hinic_hwdev_num_qps(hwdev);
191 channels->tx_count = hinic_hwdev_num_qps(hwdev);
192 channels->other_count = 0;
193 channels->combined_count = 0;
196 static const struct ethtool_ops hinic_ethtool_ops = {
197 .get_link_ksettings = hinic_get_link_ksettings,
198 .get_drvinfo = hinic_get_drvinfo,
199 .get_link = ethtool_op_get_link,
200 .get_ringparam = hinic_get_ringparam,
201 .get_channels = hinic_get_channels,
204 static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
206 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
207 struct hinic_rxq_stats rx_stats;
209 u64_stats_init(&rx_stats.syncp);
211 hinic_rxq_get_stats(rxq, &rx_stats);
213 u64_stats_update_begin(&nic_rx_stats->syncp);
214 nic_rx_stats->bytes += rx_stats.bytes;
215 nic_rx_stats->pkts += rx_stats.pkts;
216 u64_stats_update_end(&nic_rx_stats->syncp);
218 hinic_rxq_clean_stats(rxq);
221 static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
223 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
224 struct hinic_txq_stats tx_stats;
226 u64_stats_init(&tx_stats.syncp);
228 hinic_txq_get_stats(txq, &tx_stats);
230 u64_stats_update_begin(&nic_tx_stats->syncp);
231 nic_tx_stats->bytes += tx_stats.bytes;
232 nic_tx_stats->pkts += tx_stats.pkts;
233 nic_tx_stats->tx_busy += tx_stats.tx_busy;
234 nic_tx_stats->tx_wake += tx_stats.tx_wake;
235 nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
236 u64_stats_update_end(&nic_tx_stats->syncp);
238 hinic_txq_clean_stats(txq);
241 static void update_nic_stats(struct hinic_dev *nic_dev)
243 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
245 for (i = 0; i < num_qps; i++)
246 update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
248 for (i = 0; i < num_qps; i++)
249 update_tx_stats(nic_dev, &nic_dev->txqs[i]);
253 * create_txqs - Create the Logical Tx Queues of specific NIC device
254 * @nic_dev: the specific NIC device
256 * Return 0 - Success, negative - Failure
258 static int create_txqs(struct hinic_dev *nic_dev)
260 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
261 struct net_device *netdev = nic_dev->netdev;
267 txq_size = num_txqs * sizeof(*nic_dev->txqs);
268 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
272 for (i = 0; i < num_txqs; i++) {
273 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
275 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
277 netif_err(nic_dev, drv, netdev,
278 "Failed to init Txq\n");
286 for (j = 0; j < i; j++)
287 hinic_clean_txq(&nic_dev->txqs[j]);
289 devm_kfree(&netdev->dev, nic_dev->txqs);
294 * free_txqs - Free the Logical Tx Queues of specific NIC device
295 * @nic_dev: the specific NIC device
297 static void free_txqs(struct hinic_dev *nic_dev)
299 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
300 struct net_device *netdev = nic_dev->netdev;
305 for (i = 0; i < num_txqs; i++)
306 hinic_clean_txq(&nic_dev->txqs[i]);
308 devm_kfree(&netdev->dev, nic_dev->txqs);
309 nic_dev->txqs = NULL;
313 * create_txqs - Create the Logical Rx Queues of specific NIC device
314 * @nic_dev: the specific NIC device
316 * Return 0 - Success, negative - Failure
318 static int create_rxqs(struct hinic_dev *nic_dev)
320 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
321 struct net_device *netdev = nic_dev->netdev;
327 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
328 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
332 for (i = 0; i < num_rxqs; i++) {
333 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
335 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
337 netif_err(nic_dev, drv, netdev,
338 "Failed to init rxq\n");
346 for (j = 0; j < i; j++)
347 hinic_clean_rxq(&nic_dev->rxqs[j]);
349 devm_kfree(&netdev->dev, nic_dev->rxqs);
354 * free_txqs - Free the Logical Rx Queues of specific NIC device
355 * @nic_dev: the specific NIC device
357 static void free_rxqs(struct hinic_dev *nic_dev)
359 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
360 struct net_device *netdev = nic_dev->netdev;
365 for (i = 0; i < num_rxqs; i++)
366 hinic_clean_rxq(&nic_dev->rxqs[i]);
368 devm_kfree(&netdev->dev, nic_dev->rxqs);
369 nic_dev->rxqs = NULL;
372 static int hinic_open(struct net_device *netdev)
374 struct hinic_dev *nic_dev = netdev_priv(netdev);
375 enum hinic_port_link_state link_state;
376 int err, ret, num_qps;
378 if (!(nic_dev->flags & HINIC_INTF_UP)) {
379 err = hinic_hwdev_ifup(nic_dev->hwdev);
381 netif_err(nic_dev, drv, netdev,
382 "Failed - HW interface up\n");
387 err = create_txqs(nic_dev);
389 netif_err(nic_dev, drv, netdev,
390 "Failed to create Tx queues\n");
391 goto err_create_txqs;
394 err = create_rxqs(nic_dev);
396 netif_err(nic_dev, drv, netdev,
397 "Failed to create Rx queues\n");
398 goto err_create_rxqs;
401 num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
402 netif_set_real_num_tx_queues(netdev, num_qps);
403 netif_set_real_num_rx_queues(netdev, num_qps);
405 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
407 netif_err(nic_dev, drv, netdev,
408 "Failed to set port state\n");
412 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
414 netif_err(nic_dev, drv, netdev,
415 "Failed to set func port state\n");
416 goto err_func_port_state;
419 /* Wait up to 3 sec between port enable to link state */
422 down(&nic_dev->mgmt_lock);
424 err = hinic_port_link_state(nic_dev, &link_state);
426 netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
430 if (link_state == HINIC_LINK_STATE_UP)
431 nic_dev->flags |= HINIC_LINK_UP;
433 nic_dev->flags |= HINIC_INTF_UP;
435 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
436 (HINIC_LINK_UP | HINIC_INTF_UP)) {
437 netif_info(nic_dev, drv, netdev, "link + intf UP\n");
438 netif_carrier_on(netdev);
439 netif_tx_wake_all_queues(netdev);
442 up(&nic_dev->mgmt_lock);
444 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
448 up(&nic_dev->mgmt_lock);
449 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
451 netif_warn(nic_dev, drv, netdev,
452 "Failed to revert func port state\n");
455 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
457 netif_warn(nic_dev, drv, netdev,
458 "Failed to revert port state\n");
467 if (!(nic_dev->flags & HINIC_INTF_UP))
468 hinic_hwdev_ifdown(nic_dev->hwdev);
472 static int hinic_close(struct net_device *netdev)
474 struct hinic_dev *nic_dev = netdev_priv(netdev);
477 down(&nic_dev->mgmt_lock);
479 flags = nic_dev->flags;
480 nic_dev->flags &= ~HINIC_INTF_UP;
482 netif_carrier_off(netdev);
483 netif_tx_disable(netdev);
485 update_nic_stats(nic_dev);
487 up(&nic_dev->mgmt_lock);
489 hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
491 hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
496 if (flags & HINIC_INTF_UP)
497 hinic_hwdev_ifdown(nic_dev->hwdev);
499 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
503 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
505 struct hinic_dev *nic_dev = netdev_priv(netdev);
508 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
510 err = hinic_port_set_mtu(nic_dev, new_mtu);
512 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
514 netdev->mtu = new_mtu;
520 * change_mac_addr - change the main mac address of network device
521 * @netdev: network device
522 * @addr: mac address to set
524 * Return 0 - Success, negative - Failure
526 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
528 struct hinic_dev *nic_dev = netdev_priv(netdev);
532 if (!is_valid_ether_addr(addr))
533 return -EADDRNOTAVAIL;
535 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
536 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
538 down(&nic_dev->mgmt_lock);
541 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
543 netif_err(nic_dev, drv, netdev,
544 "Failed to delete mac\n");
548 err = hinic_port_add_mac(nic_dev, addr, vid);
550 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
554 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
555 } while (vid != VLAN_N_VID);
557 up(&nic_dev->mgmt_lock);
561 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
563 unsigned char new_mac[ETH_ALEN];
564 struct sockaddr *saddr = addr;
567 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
569 err = change_mac_addr(netdev, new_mac);
571 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
577 * add_mac_addr - add mac address to network device
578 * @netdev: network device
579 * @addr: mac address to add
581 * Return 0 - Success, negative - Failure
583 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
585 struct hinic_dev *nic_dev = netdev_priv(netdev);
589 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
590 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
592 down(&nic_dev->mgmt_lock);
595 err = hinic_port_add_mac(nic_dev, addr, vid);
597 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
601 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
602 } while (vid != VLAN_N_VID);
604 up(&nic_dev->mgmt_lock);
609 * remove_mac_addr - remove mac address from network device
610 * @netdev: network device
611 * @addr: mac address to remove
613 * Return 0 - Success, negative - Failure
615 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
617 struct hinic_dev *nic_dev = netdev_priv(netdev);
621 if (!is_valid_ether_addr(addr))
622 return -EADDRNOTAVAIL;
624 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
625 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
627 down(&nic_dev->mgmt_lock);
630 err = hinic_port_del_mac(nic_dev, addr, vid);
632 netif_err(nic_dev, drv, netdev,
633 "Failed to delete mac\n");
637 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
638 } while (vid != VLAN_N_VID);
640 up(&nic_dev->mgmt_lock);
644 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
645 __always_unused __be16 proto, u16 vid)
647 struct hinic_dev *nic_dev = netdev_priv(netdev);
650 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
652 down(&nic_dev->mgmt_lock);
654 err = hinic_port_add_vlan(nic_dev, vid);
656 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
660 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
662 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
666 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
668 up(&nic_dev->mgmt_lock);
672 ret = hinic_port_del_vlan(nic_dev, vid);
674 netif_err(nic_dev, drv, netdev,
675 "Failed to revert by removing vlan\n");
678 up(&nic_dev->mgmt_lock);
682 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
683 __always_unused __be16 proto, u16 vid)
685 struct hinic_dev *nic_dev = netdev_priv(netdev);
688 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
690 down(&nic_dev->mgmt_lock);
692 err = hinic_port_del_vlan(nic_dev, vid);
694 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
698 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
700 up(&nic_dev->mgmt_lock);
704 up(&nic_dev->mgmt_lock);
708 static void set_rx_mode(struct work_struct *work)
710 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
711 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
712 struct netdev_hw_addr *ha;
714 netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
716 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
718 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
719 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
721 netdev_for_each_mc_addr(ha, nic_dev->netdev)
722 add_mac_addr(nic_dev->netdev, ha->addr);
725 static void hinic_set_rx_mode(struct net_device *netdev)
727 struct hinic_dev *nic_dev = netdev_priv(netdev);
728 struct hinic_rx_mode_work *rx_mode_work;
731 rx_mode_work = &nic_dev->rx_mode_work;
733 rx_mode = HINIC_RX_MODE_UC |
737 if (netdev->flags & IFF_PROMISC)
738 rx_mode |= HINIC_RX_MODE_PROMISC;
739 else if (netdev->flags & IFF_ALLMULTI)
740 rx_mode |= HINIC_RX_MODE_MC_ALL;
742 rx_mode_work->rx_mode = rx_mode;
744 queue_work(nic_dev->workq, &rx_mode_work->work);
747 static void hinic_tx_timeout(struct net_device *netdev)
749 struct hinic_dev *nic_dev = netdev_priv(netdev);
751 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
754 static void hinic_get_stats64(struct net_device *netdev,
755 struct rtnl_link_stats64 *stats)
757 struct hinic_dev *nic_dev = netdev_priv(netdev);
758 struct hinic_rxq_stats *nic_rx_stats;
759 struct hinic_txq_stats *nic_tx_stats;
761 nic_rx_stats = &nic_dev->rx_stats;
762 nic_tx_stats = &nic_dev->tx_stats;
764 down(&nic_dev->mgmt_lock);
766 if (nic_dev->flags & HINIC_INTF_UP)
767 update_nic_stats(nic_dev);
769 up(&nic_dev->mgmt_lock);
771 stats->rx_bytes = nic_rx_stats->bytes;
772 stats->rx_packets = nic_rx_stats->pkts;
774 stats->tx_bytes = nic_tx_stats->bytes;
775 stats->tx_packets = nic_tx_stats->pkts;
776 stats->tx_errors = nic_tx_stats->tx_dropped;
779 #ifdef CONFIG_NET_POLL_CONTROLLER
780 static void hinic_netpoll(struct net_device *netdev)
782 struct hinic_dev *nic_dev = netdev_priv(netdev);
785 num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
786 for (i = 0; i < num_qps; i++) {
787 struct hinic_txq *txq = &nic_dev->txqs[i];
788 struct hinic_rxq *rxq = &nic_dev->rxqs[i];
790 napi_schedule(&txq->napi);
791 napi_schedule(&rxq->napi);
796 static const struct net_device_ops hinic_netdev_ops = {
797 .ndo_open = hinic_open,
798 .ndo_stop = hinic_close,
799 .ndo_change_mtu = hinic_change_mtu,
800 .ndo_set_mac_address = hinic_set_mac_addr,
801 .ndo_validate_addr = eth_validate_addr,
802 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
803 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
804 .ndo_set_rx_mode = hinic_set_rx_mode,
805 .ndo_start_xmit = hinic_xmit_frame,
806 .ndo_tx_timeout = hinic_tx_timeout,
807 .ndo_get_stats64 = hinic_get_stats64,
808 #ifdef CONFIG_NET_POLL_CONTROLLER
809 .ndo_poll_controller = hinic_netpoll,
813 static void netdev_features_init(struct net_device *netdev)
815 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
817 netdev->vlan_features = netdev->hw_features;
819 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
823 * link_status_event_handler - link event handler
824 * @handle: nic device for the handler
825 * @buf_in: input buffer
826 * @in_size: input size
827 * @buf_in: output buffer
828 * @out_size: returned output size
830 * Return 0 - Success, negative - Failure
832 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
833 void *buf_out, u16 *out_size)
835 struct hinic_port_link_status *link_status, *ret_link_status;
836 struct hinic_dev *nic_dev = handle;
838 link_status = buf_in;
840 if (link_status->link == HINIC_LINK_STATE_UP) {
841 down(&nic_dev->mgmt_lock);
843 nic_dev->flags |= HINIC_LINK_UP;
845 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
846 (HINIC_LINK_UP | HINIC_INTF_UP)) {
847 netif_carrier_on(nic_dev->netdev);
848 netif_tx_wake_all_queues(nic_dev->netdev);
851 up(&nic_dev->mgmt_lock);
853 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
855 down(&nic_dev->mgmt_lock);
857 nic_dev->flags &= ~HINIC_LINK_UP;
859 netif_carrier_off(nic_dev->netdev);
860 netif_tx_disable(nic_dev->netdev);
862 up(&nic_dev->mgmt_lock);
864 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
867 ret_link_status = buf_out;
868 ret_link_status->status = 0;
870 *out_size = sizeof(*ret_link_status);
874 * nic_dev_init - Initialize the NIC device
875 * @pdev: the NIC pci device
877 * Return 0 - Success, negative - Failure
879 static int nic_dev_init(struct pci_dev *pdev)
881 struct hinic_rx_mode_work *rx_mode_work;
882 struct hinic_txq_stats *tx_stats;
883 struct hinic_rxq_stats *rx_stats;
884 struct hinic_dev *nic_dev;
885 struct net_device *netdev;
886 struct hinic_hwdev *hwdev;
889 hwdev = hinic_init_hwdev(pdev);
891 dev_err(&pdev->dev, "Failed to initialize HW device\n");
892 return PTR_ERR(hwdev);
895 num_qps = hinic_hwdev_num_qps(hwdev);
897 dev_err(&pdev->dev, "Invalid number of QPS\n");
902 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
904 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
906 goto err_alloc_etherdev;
909 netdev->netdev_ops = &hinic_netdev_ops;
910 netdev->ethtool_ops = &hinic_ethtool_ops;
911 netdev->max_mtu = ETH_MAX_MTU;
913 nic_dev = netdev_priv(netdev);
914 nic_dev->netdev = netdev;
915 nic_dev->hwdev = hwdev;
916 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
918 nic_dev->txqs = NULL;
919 nic_dev->rxqs = NULL;
920 nic_dev->tx_weight = tx_weight;
921 nic_dev->rx_weight = rx_weight;
923 sema_init(&nic_dev->mgmt_lock, 1);
925 tx_stats = &nic_dev->tx_stats;
926 rx_stats = &nic_dev->rx_stats;
928 u64_stats_init(&tx_stats->syncp);
929 u64_stats_init(&rx_stats->syncp);
931 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
932 VLAN_BITMAP_SIZE(nic_dev),
934 if (!nic_dev->vlan_bitmap) {
936 goto err_vlan_bitmap;
939 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
940 if (!nic_dev->workq) {
945 pci_set_drvdata(pdev, netdev);
947 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
949 dev_warn(&pdev->dev, "Failed to get mac address\n");
951 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
953 dev_err(&pdev->dev, "Failed to add mac\n");
957 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
959 dev_err(&pdev->dev, "Failed to set mtu\n");
963 rx_mode_work = &nic_dev->rx_mode_work;
964 INIT_WORK(&rx_mode_work->work, set_rx_mode);
966 netdev_features_init(netdev);
968 netif_carrier_off(netdev);
970 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
971 nic_dev, link_status_event_handler);
973 SET_NETDEV_DEV(netdev, &pdev->dev);
974 err = register_netdev(netdev);
976 dev_err(&pdev->dev, "Failed to register netdev\n");
983 hinic_hwdev_cb_unregister(nic_dev->hwdev,
984 HINIC_MGMT_MSG_CMD_LINK_STATUS);
985 cancel_work_sync(&rx_mode_work->work);
989 pci_set_drvdata(pdev, NULL);
990 destroy_workqueue(nic_dev->workq);
998 hinic_free_hwdev(hwdev);
1002 static int hinic_probe(struct pci_dev *pdev,
1003 const struct pci_device_id *id)
1005 int err = pci_enable_device(pdev);
1008 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1012 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1014 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1015 goto err_pci_regions;
1018 pci_set_master(pdev);
1020 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1022 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1023 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1025 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1030 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1032 dev_warn(&pdev->dev,
1033 "Couldn't set 64-bit consistent DMA mask\n");
1034 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1037 "Failed to set consistent DMA mask\n");
1038 goto err_dma_consistent_mask;
1042 err = nic_dev_init(pdev);
1044 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1045 goto err_nic_dev_init;
1048 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1052 err_dma_consistent_mask:
1054 pci_release_regions(pdev);
1057 pci_disable_device(pdev);
1061 static void hinic_remove(struct pci_dev *pdev)
1063 struct net_device *netdev = pci_get_drvdata(pdev);
1064 struct hinic_dev *nic_dev = netdev_priv(netdev);
1065 struct hinic_rx_mode_work *rx_mode_work;
1067 unregister_netdev(netdev);
1069 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1070 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1072 rx_mode_work = &nic_dev->rx_mode_work;
1073 cancel_work_sync(&rx_mode_work->work);
1075 pci_set_drvdata(pdev, NULL);
1077 destroy_workqueue(nic_dev->workq);
1079 hinic_free_hwdev(nic_dev->hwdev);
1081 free_netdev(netdev);
1083 pci_release_regions(pdev);
1084 pci_disable_device(pdev);
1086 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1089 static const struct pci_device_id hinic_pci_table[] = {
1090 { PCI_VDEVICE(HUAWEI, PCI_DEVICE_ID_HI1822_PF), 0},
1093 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1095 static struct pci_driver hinic_driver = {
1096 .name = HINIC_DRV_NAME,
1097 .id_table = hinic_pci_table,
1098 .probe = hinic_probe,
1099 .remove = hinic_remove,
1102 module_pci_driver(hinic_driver);