WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / huawei / hinic / hinic_main.c
blob9a9b09401d01dc76c8094018c159e6f9c66d5c20
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
5 */
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>
20 #include <net/ip.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_debugfs.h"
28 #include "hinic_hw_qp.h"
29 #include "hinic_hw_dev.h"
30 #include "hinic_devlink.h"
31 #include "hinic_port.h"
32 #include "hinic_tx.h"
33 #include "hinic_rx.h"
34 #include "hinic_dev.h"
35 #include "hinic_sriov.h"
37 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
38 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
39 MODULE_LICENSE("GPL");
41 static unsigned int tx_weight = 64;
42 module_param(tx_weight, uint, 0644);
43 MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
45 static unsigned int rx_weight = 64;
46 module_param(rx_weight, uint, 0644);
47 MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
49 #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
50 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200
51 #define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205
52 #define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210
53 #define HINIC_DEV_ID_VF 0x375e
55 #define HINIC_WQ_NAME "hinic_dev"
57 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
58 NETIF_MSG_IFUP | \
59 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
61 #define HINIC_LRO_MAX_WQE_NUM_DEFAULT 8
63 #define HINIC_LRO_RX_TIMER_DEFAULT 16
65 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
67 #define work_to_rx_mode_work(work) \
68 container_of(work, struct hinic_rx_mode_work, work)
70 #define rx_mode_work_to_nic_dev(rx_mode_work) \
71 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
73 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
75 #define HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT 2
76 #define HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG 32
77 #define HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG 7
79 static int change_mac_addr(struct net_device *netdev, const u8 *addr);
81 static int set_features(struct hinic_dev *nic_dev,
82 netdev_features_t pre_features,
83 netdev_features_t features, bool force_change);
85 static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
87 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
88 struct hinic_rxq_stats rx_stats;
90 u64_stats_init(&rx_stats.syncp);
92 hinic_rxq_get_stats(rxq, &rx_stats);
94 u64_stats_update_begin(&nic_rx_stats->syncp);
95 nic_rx_stats->bytes += rx_stats.bytes;
96 nic_rx_stats->pkts += rx_stats.pkts;
97 nic_rx_stats->errors += rx_stats.errors;
98 nic_rx_stats->csum_errors += rx_stats.csum_errors;
99 nic_rx_stats->other_errors += rx_stats.other_errors;
100 u64_stats_update_end(&nic_rx_stats->syncp);
102 hinic_rxq_clean_stats(rxq);
105 static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
107 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
108 struct hinic_txq_stats tx_stats;
110 u64_stats_init(&tx_stats.syncp);
112 hinic_txq_get_stats(txq, &tx_stats);
114 u64_stats_update_begin(&nic_tx_stats->syncp);
115 nic_tx_stats->bytes += tx_stats.bytes;
116 nic_tx_stats->pkts += tx_stats.pkts;
117 nic_tx_stats->tx_busy += tx_stats.tx_busy;
118 nic_tx_stats->tx_wake += tx_stats.tx_wake;
119 nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
120 nic_tx_stats->big_frags_pkts += tx_stats.big_frags_pkts;
121 u64_stats_update_end(&nic_tx_stats->syncp);
123 hinic_txq_clean_stats(txq);
126 static void update_nic_stats(struct hinic_dev *nic_dev)
128 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
130 for (i = 0; i < num_qps; i++)
131 update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
133 for (i = 0; i < num_qps; i++)
134 update_tx_stats(nic_dev, &nic_dev->txqs[i]);
138 * create_txqs - Create the Logical Tx Queues of specific NIC device
139 * @nic_dev: the specific NIC device
141 * Return 0 - Success, negative - Failure
143 static int create_txqs(struct hinic_dev *nic_dev)
145 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
146 struct net_device *netdev = nic_dev->netdev;
147 size_t txq_size;
149 if (nic_dev->txqs)
150 return -EINVAL;
152 txq_size = num_txqs * sizeof(*nic_dev->txqs);
153 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
154 if (!nic_dev->txqs)
155 return -ENOMEM;
157 hinic_sq_dbgfs_init(nic_dev);
159 for (i = 0; i < num_txqs; i++) {
160 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
162 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
163 if (err) {
164 netif_err(nic_dev, drv, netdev,
165 "Failed to init Txq\n");
166 goto err_init_txq;
169 err = hinic_sq_debug_add(nic_dev, i);
170 if (err) {
171 netif_err(nic_dev, drv, netdev,
172 "Failed to add SQ%d debug\n", i);
173 goto err_add_sq_dbg;
178 return 0;
180 err_add_sq_dbg:
181 hinic_clean_txq(&nic_dev->txqs[i]);
182 err_init_txq:
183 for (j = 0; j < i; j++) {
184 hinic_sq_debug_rem(nic_dev->txqs[j].sq);
185 hinic_clean_txq(&nic_dev->txqs[j]);
188 hinic_sq_dbgfs_uninit(nic_dev);
190 devm_kfree(&netdev->dev, nic_dev->txqs);
191 return err;
194 static void enable_txqs_napi(struct hinic_dev *nic_dev)
196 int num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
197 int i;
199 for (i = 0; i < num_txqs; i++)
200 napi_enable(&nic_dev->txqs[i].napi);
203 static void disable_txqs_napi(struct hinic_dev *nic_dev)
205 int num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
206 int i;
208 for (i = 0; i < num_txqs; i++)
209 napi_disable(&nic_dev->txqs[i].napi);
213 * free_txqs - Free the Logical Tx Queues of specific NIC device
214 * @nic_dev: the specific NIC device
216 static void free_txqs(struct hinic_dev *nic_dev)
218 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
219 struct net_device *netdev = nic_dev->netdev;
221 if (!nic_dev->txqs)
222 return;
224 for (i = 0; i < num_txqs; i++) {
225 hinic_sq_debug_rem(nic_dev->txqs[i].sq);
226 hinic_clean_txq(&nic_dev->txqs[i]);
229 hinic_sq_dbgfs_uninit(nic_dev);
231 devm_kfree(&netdev->dev, nic_dev->txqs);
232 nic_dev->txqs = NULL;
236 * create_txqs - Create the Logical Rx Queues of specific NIC device
237 * @nic_dev: the specific NIC device
239 * Return 0 - Success, negative - Failure
241 static int create_rxqs(struct hinic_dev *nic_dev)
243 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
244 struct net_device *netdev = nic_dev->netdev;
245 size_t rxq_size;
247 if (nic_dev->rxqs)
248 return -EINVAL;
250 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
251 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
252 if (!nic_dev->rxqs)
253 return -ENOMEM;
255 hinic_rq_dbgfs_init(nic_dev);
257 for (i = 0; i < num_rxqs; i++) {
258 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
260 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
261 if (err) {
262 netif_err(nic_dev, drv, netdev,
263 "Failed to init rxq\n");
264 goto err_init_rxq;
267 err = hinic_rq_debug_add(nic_dev, i);
268 if (err) {
269 netif_err(nic_dev, drv, netdev,
270 "Failed to add RQ%d debug\n", i);
271 goto err_add_rq_dbg;
275 return 0;
277 err_add_rq_dbg:
278 hinic_clean_rxq(&nic_dev->rxqs[i]);
279 err_init_rxq:
280 for (j = 0; j < i; j++) {
281 hinic_rq_debug_rem(nic_dev->rxqs[j].rq);
282 hinic_clean_rxq(&nic_dev->rxqs[j]);
285 hinic_rq_dbgfs_uninit(nic_dev);
287 devm_kfree(&netdev->dev, nic_dev->rxqs);
288 return err;
292 * free_txqs - Free the Logical Rx Queues of specific NIC device
293 * @nic_dev: the specific NIC device
295 static void free_rxqs(struct hinic_dev *nic_dev)
297 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
298 struct net_device *netdev = nic_dev->netdev;
300 if (!nic_dev->rxqs)
301 return;
303 for (i = 0; i < num_rxqs; i++) {
304 hinic_rq_debug_rem(nic_dev->rxqs[i].rq);
305 hinic_clean_rxq(&nic_dev->rxqs[i]);
308 hinic_rq_dbgfs_uninit(nic_dev);
310 devm_kfree(&netdev->dev, nic_dev->rxqs);
311 nic_dev->rxqs = NULL;
314 static int hinic_configure_max_qnum(struct hinic_dev *nic_dev)
316 return hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps);
319 static int hinic_rss_init(struct hinic_dev *nic_dev)
321 u8 default_rss_key[HINIC_RSS_KEY_SIZE];
322 u8 tmpl_idx = nic_dev->rss_tmpl_idx;
323 u32 *indir_tbl;
324 int err, i;
326 indir_tbl = kcalloc(HINIC_RSS_INDIR_SIZE, sizeof(u32), GFP_KERNEL);
327 if (!indir_tbl)
328 return -ENOMEM;
330 netdev_rss_key_fill(default_rss_key, sizeof(default_rss_key));
331 for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
332 indir_tbl[i] = ethtool_rxfh_indir_default(i, nic_dev->num_rss);
334 err = hinic_rss_set_template_tbl(nic_dev, tmpl_idx, default_rss_key);
335 if (err)
336 goto out;
338 err = hinic_rss_set_indir_tbl(nic_dev, tmpl_idx, indir_tbl);
339 if (err)
340 goto out;
342 err = hinic_set_rss_type(nic_dev, tmpl_idx, nic_dev->rss_type);
343 if (err)
344 goto out;
346 err = hinic_rss_set_hash_engine(nic_dev, tmpl_idx,
347 nic_dev->rss_hash_engine);
348 if (err)
349 goto out;
351 err = hinic_rss_cfg(nic_dev, 1, tmpl_idx);
352 if (err)
353 goto out;
355 out:
356 kfree(indir_tbl);
357 return err;
360 static void hinic_rss_deinit(struct hinic_dev *nic_dev)
362 hinic_rss_cfg(nic_dev, 0, nic_dev->rss_tmpl_idx);
365 static void hinic_init_rss_parameters(struct hinic_dev *nic_dev)
367 nic_dev->rss_hash_engine = HINIC_RSS_HASH_ENGINE_TYPE_XOR;
368 nic_dev->rss_type.tcp_ipv6_ext = 1;
369 nic_dev->rss_type.ipv6_ext = 1;
370 nic_dev->rss_type.tcp_ipv6 = 1;
371 nic_dev->rss_type.ipv6 = 1;
372 nic_dev->rss_type.tcp_ipv4 = 1;
373 nic_dev->rss_type.ipv4 = 1;
374 nic_dev->rss_type.udp_ipv6 = 1;
375 nic_dev->rss_type.udp_ipv4 = 1;
378 static void hinic_enable_rss(struct hinic_dev *nic_dev)
380 struct net_device *netdev = nic_dev->netdev;
381 struct hinic_hwdev *hwdev = nic_dev->hwdev;
382 struct hinic_hwif *hwif = hwdev->hwif;
383 struct pci_dev *pdev = hwif->pdev;
384 int i, node, err = 0;
385 u16 num_cpus = 0;
387 if (nic_dev->max_qps <= 1) {
388 nic_dev->flags &= ~HINIC_RSS_ENABLE;
389 nic_dev->rss_limit = nic_dev->max_qps;
390 nic_dev->num_qps = nic_dev->max_qps;
391 nic_dev->num_rss = nic_dev->max_qps;
393 return;
396 err = hinic_rss_template_alloc(nic_dev, &nic_dev->rss_tmpl_idx);
397 if (err) {
398 netif_err(nic_dev, drv, netdev,
399 "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
400 nic_dev->flags &= ~HINIC_RSS_ENABLE;
401 nic_dev->max_qps = 1;
402 nic_dev->rss_limit = nic_dev->max_qps;
403 nic_dev->num_qps = nic_dev->max_qps;
404 nic_dev->num_rss = nic_dev->max_qps;
406 return;
409 nic_dev->flags |= HINIC_RSS_ENABLE;
411 for (i = 0; i < num_online_cpus(); i++) {
412 node = cpu_to_node(i);
413 if (node == dev_to_node(&pdev->dev))
414 num_cpus++;
417 if (!num_cpus)
418 num_cpus = num_online_cpus();
420 nic_dev->num_qps = hinic_hwdev_num_qps(hwdev);
421 nic_dev->num_qps = min_t(u16, nic_dev->num_qps, num_cpus);
423 nic_dev->rss_limit = nic_dev->num_qps;
424 nic_dev->num_rss = nic_dev->num_qps;
426 hinic_init_rss_parameters(nic_dev);
427 err = hinic_rss_init(nic_dev);
428 if (err)
429 netif_err(nic_dev, drv, netdev, "Failed to init rss\n");
432 int hinic_open(struct net_device *netdev)
434 struct hinic_dev *nic_dev = netdev_priv(netdev);
435 enum hinic_port_link_state link_state;
436 int err, ret;
438 if (!(nic_dev->flags & HINIC_INTF_UP)) {
439 err = hinic_hwdev_ifup(nic_dev->hwdev, nic_dev->sq_depth,
440 nic_dev->rq_depth);
441 if (err) {
442 netif_err(nic_dev, drv, netdev,
443 "Failed - HW interface up\n");
444 return err;
448 err = create_txqs(nic_dev);
449 if (err) {
450 netif_err(nic_dev, drv, netdev,
451 "Failed to create Tx queues\n");
452 goto err_create_txqs;
455 enable_txqs_napi(nic_dev);
457 err = create_rxqs(nic_dev);
458 if (err) {
459 netif_err(nic_dev, drv, netdev,
460 "Failed to create Rx queues\n");
461 goto err_create_rxqs;
464 hinic_enable_rss(nic_dev);
466 err = hinic_configure_max_qnum(nic_dev);
467 if (err) {
468 netif_err(nic_dev, drv, nic_dev->netdev,
469 "Failed to configure the maximum number of queues\n");
470 goto err_port_state;
473 netif_set_real_num_tx_queues(netdev, nic_dev->num_qps);
474 netif_set_real_num_rx_queues(netdev, nic_dev->num_qps);
476 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
477 if (err) {
478 netif_err(nic_dev, drv, netdev,
479 "Failed to set port state\n");
480 goto err_port_state;
483 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
484 if (err) {
485 netif_err(nic_dev, drv, netdev,
486 "Failed to set func port state\n");
487 goto err_func_port_state;
490 down(&nic_dev->mgmt_lock);
492 err = hinic_port_link_state(nic_dev, &link_state);
493 if (err) {
494 netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
495 goto err_port_link;
498 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
499 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, link_state);
501 if (link_state == HINIC_LINK_STATE_UP) {
502 nic_dev->flags |= HINIC_LINK_UP;
503 nic_dev->cable_unplugged = false;
504 nic_dev->module_unrecognized = false;
507 nic_dev->flags |= HINIC_INTF_UP;
509 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
510 (HINIC_LINK_UP | HINIC_INTF_UP)) {
511 netif_info(nic_dev, drv, netdev, "link + intf UP\n");
512 netif_carrier_on(netdev);
513 netif_tx_wake_all_queues(netdev);
516 up(&nic_dev->mgmt_lock);
518 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
519 return 0;
521 err_port_link:
522 up(&nic_dev->mgmt_lock);
523 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
524 if (ret)
525 netif_warn(nic_dev, drv, netdev,
526 "Failed to revert func port state\n");
528 err_func_port_state:
529 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
530 if (ret)
531 netif_warn(nic_dev, drv, netdev,
532 "Failed to revert port state\n");
533 err_port_state:
534 free_rxqs(nic_dev);
535 if (nic_dev->flags & HINIC_RSS_ENABLE) {
536 hinic_rss_deinit(nic_dev);
537 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
540 err_create_rxqs:
541 disable_txqs_napi(nic_dev);
542 free_txqs(nic_dev);
544 err_create_txqs:
545 if (!(nic_dev->flags & HINIC_INTF_UP))
546 hinic_hwdev_ifdown(nic_dev->hwdev);
547 return err;
550 int hinic_close(struct net_device *netdev)
552 struct hinic_dev *nic_dev = netdev_priv(netdev);
553 unsigned int flags;
555 /* Disable txq napi firstly to aviod rewaking txq in free_tx_poll */
556 disable_txqs_napi(nic_dev);
558 down(&nic_dev->mgmt_lock);
560 flags = nic_dev->flags;
561 nic_dev->flags &= ~HINIC_INTF_UP;
563 netif_carrier_off(netdev);
564 netif_tx_disable(netdev);
566 update_nic_stats(nic_dev);
568 up(&nic_dev->mgmt_lock);
570 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
571 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
573 hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
575 hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
577 if (nic_dev->flags & HINIC_RSS_ENABLE) {
578 hinic_rss_deinit(nic_dev);
579 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
582 free_rxqs(nic_dev);
583 free_txqs(nic_dev);
585 if (flags & HINIC_INTF_UP)
586 hinic_hwdev_ifdown(nic_dev->hwdev);
588 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
589 return 0;
592 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
594 struct hinic_dev *nic_dev = netdev_priv(netdev);
595 int err;
597 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
599 err = hinic_port_set_mtu(nic_dev, new_mtu);
600 if (err)
601 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
602 else
603 netdev->mtu = new_mtu;
605 return err;
609 * change_mac_addr - change the main mac address of network device
610 * @netdev: network device
611 * @addr: mac address to set
613 * Return 0 - Success, negative - Failure
615 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
617 struct hinic_dev *nic_dev = netdev_priv(netdev);
618 u16 vid = 0;
619 int err;
621 if (!is_valid_ether_addr(addr))
622 return -EADDRNOTAVAIL;
624 netif_info(nic_dev, drv, netdev, "change 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);
629 do {
630 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
631 if (err) {
632 netif_err(nic_dev, drv, netdev,
633 "Failed to delete mac\n");
634 break;
637 err = hinic_port_add_mac(nic_dev, addr, vid);
638 if (err) {
639 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
640 break;
643 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
644 } while (vid != VLAN_N_VID);
646 up(&nic_dev->mgmt_lock);
647 return err;
650 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
652 unsigned char new_mac[ETH_ALEN];
653 struct sockaddr *saddr = addr;
654 int err;
656 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
658 err = change_mac_addr(netdev, new_mac);
659 if (!err)
660 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
662 return err;
666 * add_mac_addr - add mac address to network device
667 * @netdev: network device
668 * @addr: mac address to add
670 * Return 0 - Success, negative - Failure
672 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
674 struct hinic_dev *nic_dev = netdev_priv(netdev);
675 u16 vid = 0;
676 int err;
678 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
679 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
681 down(&nic_dev->mgmt_lock);
683 do {
684 err = hinic_port_add_mac(nic_dev, addr, vid);
685 if (err) {
686 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
687 break;
690 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
691 } while (vid != VLAN_N_VID);
693 up(&nic_dev->mgmt_lock);
694 return err;
698 * remove_mac_addr - remove mac address from network device
699 * @netdev: network device
700 * @addr: mac address to remove
702 * Return 0 - Success, negative - Failure
704 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
706 struct hinic_dev *nic_dev = netdev_priv(netdev);
707 u16 vid = 0;
708 int err;
710 if (!is_valid_ether_addr(addr))
711 return -EADDRNOTAVAIL;
713 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
714 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
716 down(&nic_dev->mgmt_lock);
718 do {
719 err = hinic_port_del_mac(nic_dev, addr, vid);
720 if (err) {
721 netif_err(nic_dev, drv, netdev,
722 "Failed to delete mac\n");
723 break;
726 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
727 } while (vid != VLAN_N_VID);
729 up(&nic_dev->mgmt_lock);
730 return err;
733 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
734 __always_unused __be16 proto, u16 vid)
736 struct hinic_dev *nic_dev = netdev_priv(netdev);
737 int ret, err;
739 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
741 down(&nic_dev->mgmt_lock);
743 err = hinic_port_add_vlan(nic_dev, vid);
744 if (err) {
745 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
746 goto err_vlan_add;
749 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
750 if (err && err != HINIC_PF_SET_VF_ALREADY) {
751 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
752 goto err_add_mac;
755 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
757 up(&nic_dev->mgmt_lock);
758 return 0;
760 err_add_mac:
761 ret = hinic_port_del_vlan(nic_dev, vid);
762 if (ret)
763 netif_err(nic_dev, drv, netdev,
764 "Failed to revert by removing vlan\n");
766 err_vlan_add:
767 up(&nic_dev->mgmt_lock);
768 return err;
771 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
772 __always_unused __be16 proto, u16 vid)
774 struct hinic_dev *nic_dev = netdev_priv(netdev);
775 int err;
777 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
779 down(&nic_dev->mgmt_lock);
781 err = hinic_port_del_vlan(nic_dev, vid);
782 if (err) {
783 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
784 goto err_del_vlan;
787 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
789 up(&nic_dev->mgmt_lock);
790 return 0;
792 err_del_vlan:
793 up(&nic_dev->mgmt_lock);
794 return err;
797 static void set_rx_mode(struct work_struct *work)
799 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
800 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
802 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
804 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
805 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
808 static void hinic_set_rx_mode(struct net_device *netdev)
810 struct hinic_dev *nic_dev = netdev_priv(netdev);
811 struct hinic_rx_mode_work *rx_mode_work;
812 u32 rx_mode;
814 rx_mode_work = &nic_dev->rx_mode_work;
816 rx_mode = HINIC_RX_MODE_UC |
817 HINIC_RX_MODE_MC |
818 HINIC_RX_MODE_BC;
820 if (netdev->flags & IFF_PROMISC) {
821 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
822 rx_mode |= HINIC_RX_MODE_PROMISC;
823 } else if (netdev->flags & IFF_ALLMULTI) {
824 rx_mode |= HINIC_RX_MODE_MC_ALL;
827 rx_mode_work->rx_mode = rx_mode;
829 queue_work(nic_dev->workq, &rx_mode_work->work);
832 static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
834 struct hinic_dev *nic_dev = netdev_priv(netdev);
835 u16 sw_pi, hw_ci, sw_ci;
836 struct hinic_sq *sq;
837 u16 num_sqs, q_id;
839 num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
841 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
843 for (q_id = 0; q_id < num_sqs; q_id++) {
844 if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
845 continue;
847 sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
848 sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
849 hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
850 sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
851 netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
852 q_id, sw_pi, hw_ci, sw_ci,
853 nic_dev->txqs[q_id].napi.state);
857 static void hinic_get_stats64(struct net_device *netdev,
858 struct rtnl_link_stats64 *stats)
860 struct hinic_dev *nic_dev = netdev_priv(netdev);
861 struct hinic_rxq_stats *nic_rx_stats;
862 struct hinic_txq_stats *nic_tx_stats;
864 nic_rx_stats = &nic_dev->rx_stats;
865 nic_tx_stats = &nic_dev->tx_stats;
867 down(&nic_dev->mgmt_lock);
869 if (nic_dev->flags & HINIC_INTF_UP)
870 update_nic_stats(nic_dev);
872 up(&nic_dev->mgmt_lock);
874 stats->rx_bytes = nic_rx_stats->bytes;
875 stats->rx_packets = nic_rx_stats->pkts;
876 stats->rx_errors = nic_rx_stats->errors;
878 stats->tx_bytes = nic_tx_stats->bytes;
879 stats->tx_packets = nic_tx_stats->pkts;
880 stats->tx_errors = nic_tx_stats->tx_dropped;
883 static int hinic_set_features(struct net_device *netdev,
884 netdev_features_t features)
886 struct hinic_dev *nic_dev = netdev_priv(netdev);
888 return set_features(nic_dev, nic_dev->netdev->features,
889 features, false);
892 static netdev_features_t hinic_fix_features(struct net_device *netdev,
893 netdev_features_t features)
895 struct hinic_dev *nic_dev = netdev_priv(netdev);
897 /* If Rx checksum is disabled, then LRO should also be disabled */
898 if (!(features & NETIF_F_RXCSUM)) {
899 netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
900 features &= ~NETIF_F_LRO;
903 return features;
906 static const struct net_device_ops hinic_netdev_ops = {
907 .ndo_open = hinic_open,
908 .ndo_stop = hinic_close,
909 .ndo_change_mtu = hinic_change_mtu,
910 .ndo_set_mac_address = hinic_set_mac_addr,
911 .ndo_validate_addr = eth_validate_addr,
912 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
913 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
914 .ndo_set_rx_mode = hinic_set_rx_mode,
915 .ndo_start_xmit = hinic_xmit_frame,
916 .ndo_tx_timeout = hinic_tx_timeout,
917 .ndo_get_stats64 = hinic_get_stats64,
918 .ndo_fix_features = hinic_fix_features,
919 .ndo_set_features = hinic_set_features,
920 .ndo_set_vf_mac = hinic_ndo_set_vf_mac,
921 .ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
922 .ndo_get_vf_config = hinic_ndo_get_vf_config,
923 .ndo_set_vf_trust = hinic_ndo_set_vf_trust,
924 .ndo_set_vf_rate = hinic_ndo_set_vf_bw,
925 .ndo_set_vf_spoofchk = hinic_ndo_set_vf_spoofchk,
926 .ndo_set_vf_link_state = hinic_ndo_set_vf_link_state,
929 static const struct net_device_ops hinicvf_netdev_ops = {
930 .ndo_open = hinic_open,
931 .ndo_stop = hinic_close,
932 .ndo_change_mtu = hinic_change_mtu,
933 .ndo_set_mac_address = hinic_set_mac_addr,
934 .ndo_validate_addr = eth_validate_addr,
935 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
936 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
937 .ndo_set_rx_mode = hinic_set_rx_mode,
938 .ndo_start_xmit = hinic_xmit_frame,
939 .ndo_tx_timeout = hinic_tx_timeout,
940 .ndo_get_stats64 = hinic_get_stats64,
941 .ndo_fix_features = hinic_fix_features,
942 .ndo_set_features = hinic_set_features,
945 static void netdev_features_init(struct net_device *netdev)
947 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
948 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
949 NETIF_F_RXCSUM | NETIF_F_LRO |
950 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
951 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_UDP_TUNNEL_CSUM;
953 netdev->vlan_features = netdev->hw_features;
955 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
957 netdev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SCTP_CRC |
958 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
959 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_UDP_TUNNEL;
962 static void hinic_refresh_nic_cfg(struct hinic_dev *nic_dev)
964 struct hinic_nic_cfg *nic_cfg = &nic_dev->hwdev->func_to_io.nic_cfg;
965 struct hinic_pause_config pause_info = {0};
966 struct hinic_port_cap port_cap = {0};
968 if (hinic_port_get_cap(nic_dev, &port_cap))
969 return;
971 mutex_lock(&nic_cfg->cfg_mutex);
972 if (nic_cfg->pause_set || !port_cap.autoneg_state) {
973 nic_cfg->auto_neg = port_cap.autoneg_state;
974 pause_info.auto_neg = nic_cfg->auto_neg;
975 pause_info.rx_pause = nic_cfg->rx_pause;
976 pause_info.tx_pause = nic_cfg->tx_pause;
977 hinic_set_hw_pause_info(nic_dev->hwdev, &pause_info);
979 mutex_unlock(&nic_cfg->cfg_mutex);
983 * link_status_event_handler - link event handler
984 * @handle: nic device for the handler
985 * @buf_in: input buffer
986 * @in_size: input size
987 * @buf_out: output buffer
988 * @out_size: returned output size
990 * Return 0 - Success, negative - Failure
992 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
993 void *buf_out, u16 *out_size)
995 struct hinic_port_link_status *link_status, *ret_link_status;
996 struct hinic_dev *nic_dev = handle;
998 link_status = buf_in;
1000 if (link_status->link == HINIC_LINK_STATE_UP) {
1001 down(&nic_dev->mgmt_lock);
1003 nic_dev->flags |= HINIC_LINK_UP;
1004 nic_dev->cable_unplugged = false;
1005 nic_dev->module_unrecognized = false;
1007 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
1008 (HINIC_LINK_UP | HINIC_INTF_UP)) {
1009 netif_carrier_on(nic_dev->netdev);
1010 netif_tx_wake_all_queues(nic_dev->netdev);
1013 up(&nic_dev->mgmt_lock);
1015 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
1016 hinic_refresh_nic_cfg(nic_dev);
1018 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
1019 } else {
1020 down(&nic_dev->mgmt_lock);
1022 nic_dev->flags &= ~HINIC_LINK_UP;
1024 netif_carrier_off(nic_dev->netdev);
1025 netif_tx_disable(nic_dev->netdev);
1027 up(&nic_dev->mgmt_lock);
1029 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
1032 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
1033 hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
1034 link_status->link);
1036 ret_link_status = buf_out;
1037 ret_link_status->status = 0;
1039 *out_size = sizeof(*ret_link_status);
1042 static void cable_plug_event(void *handle,
1043 void *buf_in, u16 in_size,
1044 void *buf_out, u16 *out_size)
1046 struct hinic_cable_plug_event *plug_event = buf_in;
1047 struct hinic_dev *nic_dev = handle;
1049 nic_dev->cable_unplugged = plug_event->plugged ? false : true;
1051 *out_size = sizeof(*plug_event);
1052 plug_event = buf_out;
1053 plug_event->status = 0;
1056 static void link_err_event(void *handle,
1057 void *buf_in, u16 in_size,
1058 void *buf_out, u16 *out_size)
1060 struct hinic_link_err_event *link_err = buf_in;
1061 struct hinic_dev *nic_dev = handle;
1063 if (link_err->err_type >= LINK_ERR_NUM)
1064 netif_info(nic_dev, link, nic_dev->netdev,
1065 "Link failed, Unknown error type: 0x%x\n",
1066 link_err->err_type);
1067 else
1068 nic_dev->module_unrecognized = true;
1070 *out_size = sizeof(*link_err);
1071 link_err = buf_out;
1072 link_err->status = 0;
1075 static int set_features(struct hinic_dev *nic_dev,
1076 netdev_features_t pre_features,
1077 netdev_features_t features, bool force_change)
1079 netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
1080 u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
1081 netdev_features_t failed_features = 0;
1082 int ret = 0;
1083 int err = 0;
1085 if (changed & NETIF_F_TSO) {
1086 ret = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
1087 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
1088 if (ret) {
1089 err = ret;
1090 failed_features |= NETIF_F_TSO;
1094 if (changed & NETIF_F_RXCSUM) {
1095 ret = hinic_set_rx_csum_offload(nic_dev, csum_en);
1096 if (ret) {
1097 err = ret;
1098 failed_features |= NETIF_F_RXCSUM;
1102 if (changed & NETIF_F_LRO) {
1103 ret = hinic_set_rx_lro_state(nic_dev,
1104 !!(features & NETIF_F_LRO),
1105 HINIC_LRO_RX_TIMER_DEFAULT,
1106 HINIC_LRO_MAX_WQE_NUM_DEFAULT);
1107 if (ret) {
1108 err = ret;
1109 failed_features |= NETIF_F_LRO;
1113 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1114 ret = hinic_set_rx_vlan_offload(nic_dev,
1115 !!(features &
1116 NETIF_F_HW_VLAN_CTAG_RX));
1117 if (ret) {
1118 err = ret;
1119 failed_features |= NETIF_F_HW_VLAN_CTAG_RX;
1123 if (err) {
1124 nic_dev->netdev->features = features ^ failed_features;
1125 return -EIO;
1128 return 0;
1131 static int hinic_init_intr_coalesce(struct hinic_dev *nic_dev)
1133 u64 size;
1134 u16 i;
1136 size = sizeof(struct hinic_intr_coal_info) * nic_dev->max_qps;
1137 nic_dev->rx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1138 if (!nic_dev->rx_intr_coalesce)
1139 return -ENOMEM;
1140 nic_dev->tx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1141 if (!nic_dev->tx_intr_coalesce) {
1142 kfree(nic_dev->rx_intr_coalesce);
1143 return -ENOMEM;
1146 for (i = 0; i < nic_dev->max_qps; i++) {
1147 nic_dev->rx_intr_coalesce[i].pending_limt =
1148 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1149 nic_dev->rx_intr_coalesce[i].coalesce_timer_cfg =
1150 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1151 nic_dev->rx_intr_coalesce[i].resend_timer_cfg =
1152 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1153 nic_dev->tx_intr_coalesce[i].pending_limt =
1154 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1155 nic_dev->tx_intr_coalesce[i].coalesce_timer_cfg =
1156 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1157 nic_dev->tx_intr_coalesce[i].resend_timer_cfg =
1158 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1161 return 0;
1164 static void hinic_free_intr_coalesce(struct hinic_dev *nic_dev)
1166 kfree(nic_dev->tx_intr_coalesce);
1167 kfree(nic_dev->rx_intr_coalesce);
1171 * nic_dev_init - Initialize the NIC device
1172 * @pdev: the NIC pci device
1174 * Return 0 - Success, negative - Failure
1176 static int nic_dev_init(struct pci_dev *pdev)
1178 struct hinic_rx_mode_work *rx_mode_work;
1179 struct hinic_txq_stats *tx_stats;
1180 struct hinic_rxq_stats *rx_stats;
1181 struct hinic_dev *nic_dev;
1182 struct net_device *netdev;
1183 struct hinic_hwdev *hwdev;
1184 struct devlink *devlink;
1185 int err, num_qps;
1187 devlink = hinic_devlink_alloc();
1188 if (!devlink) {
1189 dev_err(&pdev->dev, "Hinic devlink alloc failed\n");
1190 return -ENOMEM;
1193 hwdev = hinic_init_hwdev(pdev, devlink);
1194 if (IS_ERR(hwdev)) {
1195 dev_err(&pdev->dev, "Failed to initialize HW device\n");
1196 hinic_devlink_free(devlink);
1197 return PTR_ERR(hwdev);
1200 num_qps = hinic_hwdev_num_qps(hwdev);
1201 if (num_qps <= 0) {
1202 dev_err(&pdev->dev, "Invalid number of QPS\n");
1203 err = -EINVAL;
1204 goto err_num_qps;
1207 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1208 if (!netdev) {
1209 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1210 err = -ENOMEM;
1211 goto err_alloc_etherdev;
1214 if (!HINIC_IS_VF(hwdev->hwif))
1215 netdev->netdev_ops = &hinic_netdev_ops;
1216 else
1217 netdev->netdev_ops = &hinicvf_netdev_ops;
1219 netdev->max_mtu = ETH_MAX_MTU;
1221 nic_dev = netdev_priv(netdev);
1222 nic_dev->netdev = netdev;
1223 nic_dev->hwdev = hwdev;
1224 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1225 nic_dev->flags = 0;
1226 nic_dev->txqs = NULL;
1227 nic_dev->rxqs = NULL;
1228 nic_dev->tx_weight = tx_weight;
1229 nic_dev->rx_weight = rx_weight;
1230 nic_dev->sq_depth = HINIC_SQ_DEPTH;
1231 nic_dev->rq_depth = HINIC_RQ_DEPTH;
1232 nic_dev->sriov_info.hwdev = hwdev;
1233 nic_dev->sriov_info.pdev = pdev;
1234 nic_dev->max_qps = num_qps;
1235 nic_dev->devlink = devlink;
1237 hinic_set_ethtool_ops(netdev);
1239 sema_init(&nic_dev->mgmt_lock, 1);
1241 tx_stats = &nic_dev->tx_stats;
1242 rx_stats = &nic_dev->rx_stats;
1244 u64_stats_init(&tx_stats->syncp);
1245 u64_stats_init(&rx_stats->syncp);
1247 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1248 VLAN_BITMAP_SIZE(nic_dev),
1249 GFP_KERNEL);
1250 if (!nic_dev->vlan_bitmap) {
1251 err = -ENOMEM;
1252 goto err_vlan_bitmap;
1255 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1256 if (!nic_dev->workq) {
1257 err = -ENOMEM;
1258 goto err_workq;
1261 pci_set_drvdata(pdev, netdev);
1263 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1264 if (err) {
1265 dev_err(&pdev->dev, "Failed to get mac address\n");
1266 goto err_get_mac;
1269 if (!is_valid_ether_addr(netdev->dev_addr)) {
1270 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1271 dev_err(&pdev->dev, "Invalid MAC address\n");
1272 err = -EIO;
1273 goto err_add_mac;
1276 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1277 netdev->dev_addr);
1278 eth_hw_addr_random(netdev);
1281 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1282 if (err && err != HINIC_PF_SET_VF_ALREADY) {
1283 dev_err(&pdev->dev, "Failed to add mac\n");
1284 goto err_add_mac;
1287 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1288 if (err) {
1289 dev_err(&pdev->dev, "Failed to set mtu\n");
1290 goto err_set_mtu;
1293 rx_mode_work = &nic_dev->rx_mode_work;
1294 INIT_WORK(&rx_mode_work->work, set_rx_mode);
1296 netdev_features_init(netdev);
1298 netif_carrier_off(netdev);
1300 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1301 nic_dev, link_status_event_handler);
1302 hinic_hwdev_cb_register(nic_dev->hwdev,
1303 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT,
1304 nic_dev, cable_plug_event);
1305 hinic_hwdev_cb_register(nic_dev->hwdev,
1306 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT,
1307 nic_dev, link_err_event);
1309 err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1310 if (err)
1311 goto err_set_features;
1313 /* enable pause and disable pfc by default */
1314 err = hinic_dcb_set_pfc(nic_dev->hwdev, 0, 0);
1315 if (err)
1316 goto err_set_pfc;
1318 SET_NETDEV_DEV(netdev, &pdev->dev);
1320 err = hinic_init_intr_coalesce(nic_dev);
1321 if (err) {
1322 dev_err(&pdev->dev, "Failed to init_intr_coalesce\n");
1323 goto err_init_intr;
1326 hinic_dbg_init(nic_dev);
1328 hinic_func_tbl_dbgfs_init(nic_dev);
1330 err = hinic_func_table_debug_add(nic_dev);
1331 if (err) {
1332 dev_err(&pdev->dev, "Failed to add func_table debug\n");
1333 goto err_add_func_table_dbg;
1336 err = register_netdev(netdev);
1337 if (err) {
1338 dev_err(&pdev->dev, "Failed to register netdev\n");
1339 goto err_reg_netdev;
1342 return 0;
1344 err_reg_netdev:
1345 hinic_func_table_debug_rem(nic_dev);
1346 err_add_func_table_dbg:
1347 hinic_func_tbl_dbgfs_uninit(nic_dev);
1348 hinic_dbg_uninit(nic_dev);
1349 hinic_free_intr_coalesce(nic_dev);
1350 err_init_intr:
1351 err_set_pfc:
1352 err_set_features:
1353 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1354 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1355 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1356 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1357 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1358 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1359 cancel_work_sync(&rx_mode_work->work);
1361 err_set_mtu:
1362 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1363 err_add_mac:
1364 err_get_mac:
1365 pci_set_drvdata(pdev, NULL);
1366 destroy_workqueue(nic_dev->workq);
1367 err_workq:
1368 err_vlan_bitmap:
1369 free_netdev(netdev);
1371 err_alloc_etherdev:
1372 err_num_qps:
1373 hinic_free_hwdev(hwdev);
1374 hinic_devlink_free(devlink);
1375 return err;
1378 static int hinic_probe(struct pci_dev *pdev,
1379 const struct pci_device_id *id)
1381 int err = pci_enable_device(pdev);
1383 if (err) {
1384 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1385 return err;
1388 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1389 if (err) {
1390 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1391 goto err_pci_regions;
1394 pci_set_master(pdev);
1396 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1397 if (err) {
1398 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1399 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1400 if (err) {
1401 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1402 goto err_dma_mask;
1406 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1407 if (err) {
1408 dev_warn(&pdev->dev,
1409 "Couldn't set 64-bit consistent DMA mask\n");
1410 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1411 if (err) {
1412 dev_err(&pdev->dev,
1413 "Failed to set consistent DMA mask\n");
1414 goto err_dma_consistent_mask;
1418 err = nic_dev_init(pdev);
1419 if (err) {
1420 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1421 goto err_nic_dev_init;
1424 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1425 return 0;
1427 err_nic_dev_init:
1428 err_dma_consistent_mask:
1429 err_dma_mask:
1430 pci_release_regions(pdev);
1432 err_pci_regions:
1433 pci_disable_device(pdev);
1434 return err;
1437 #define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
1439 static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1441 struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1442 u32 loop_cnt = 0;
1444 set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1445 usleep_range(9900, 10000);
1447 while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1448 if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1449 !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1450 return;
1452 usleep_range(9900, 10000);
1453 loop_cnt++;
1457 static void hinic_remove(struct pci_dev *pdev)
1459 struct net_device *netdev = pci_get_drvdata(pdev);
1460 struct hinic_dev *nic_dev = netdev_priv(netdev);
1461 struct devlink *devlink = nic_dev->devlink;
1462 struct hinic_rx_mode_work *rx_mode_work;
1464 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1465 wait_sriov_cfg_complete(nic_dev);
1466 hinic_pci_sriov_disable(pdev);
1469 unregister_netdev(netdev);
1471 hinic_func_table_debug_rem(nic_dev);
1473 hinic_func_tbl_dbgfs_uninit(nic_dev);
1475 hinic_dbg_uninit(nic_dev);
1477 hinic_free_intr_coalesce(nic_dev);
1479 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1481 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1482 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1483 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1484 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1485 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1486 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1488 rx_mode_work = &nic_dev->rx_mode_work;
1489 cancel_work_sync(&rx_mode_work->work);
1491 pci_set_drvdata(pdev, NULL);
1493 destroy_workqueue(nic_dev->workq);
1495 hinic_free_hwdev(nic_dev->hwdev);
1497 free_netdev(netdev);
1499 hinic_devlink_free(devlink);
1501 pci_release_regions(pdev);
1502 pci_disable_device(pdev);
1504 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1507 static void hinic_shutdown(struct pci_dev *pdev)
1509 pci_disable_device(pdev);
1512 static const struct pci_device_id hinic_pci_table[] = {
1513 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1514 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1515 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1516 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1517 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1518 { 0, 0}
1520 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1522 static struct pci_driver hinic_driver = {
1523 .name = HINIC_DRV_NAME,
1524 .id_table = hinic_pci_table,
1525 .probe = hinic_probe,
1526 .remove = hinic_remove,
1527 .shutdown = hinic_shutdown,
1528 .sriov_configure = hinic_pci_sriov_configure,
1531 static int __init hinic_module_init(void)
1533 hinic_dbg_register_debugfs(HINIC_DRV_NAME);
1534 return pci_register_driver(&hinic_driver);
1537 static void __exit hinic_module_exit(void)
1539 pci_unregister_driver(&hinic_driver);
1540 hinic_dbg_unregister_debugfs();
1543 module_init(hinic_module_init);
1544 module_exit(hinic_module_exit);