1 // SPDX-License-Identifier: GPL-2.0-only
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/pci.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
16 #include <linux/slab.h>
17 #include <linux/if_vlan.h>
18 #include <linux/semaphore.h>
19 #include <linux/workqueue.h>
21 #include <linux/bitops.h>
22 #include <linux/bitmap.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
26 #include "hinic_hw_qp.h"
27 #include "hinic_hw_dev.h"
28 #include "hinic_port.h"
31 #include "hinic_dev.h"
33 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
34 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
35 MODULE_LICENSE("GPL");
37 static unsigned int tx_weight
= 64;
38 module_param(tx_weight
, uint
, 0644);
39 MODULE_PARM_DESC(tx_weight
, "Number Tx packets for NAPI budget (default=64)");
41 static unsigned int rx_weight
= 64;
42 module_param(rx_weight
, uint
, 0644);
43 MODULE_PARM_DESC(rx_weight
, "Number Rx packets for NAPI budget (default=64)");
45 #define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
46 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200
47 #define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205
48 #define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210
50 #define HINIC_WQ_NAME "hinic_dev"
52 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
54 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
56 #define HINIC_LRO_MAX_WQE_NUM_DEFAULT 8
58 #define HINIC_LRO_RX_TIMER_DEFAULT 16
60 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
62 #define work_to_rx_mode_work(work) \
63 container_of(work, struct hinic_rx_mode_work, work)
65 #define rx_mode_work_to_nic_dev(rx_mode_work) \
66 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
68 static int change_mac_addr(struct net_device
*netdev
, const u8
*addr
);
70 static int set_features(struct hinic_dev
*nic_dev
,
71 netdev_features_t pre_features
,
72 netdev_features_t features
, bool force_change
);
74 static void update_rx_stats(struct hinic_dev
*nic_dev
, struct hinic_rxq
*rxq
)
76 struct hinic_rxq_stats
*nic_rx_stats
= &nic_dev
->rx_stats
;
77 struct hinic_rxq_stats rx_stats
;
79 u64_stats_init(&rx_stats
.syncp
);
81 hinic_rxq_get_stats(rxq
, &rx_stats
);
83 u64_stats_update_begin(&nic_rx_stats
->syncp
);
84 nic_rx_stats
->bytes
+= rx_stats
.bytes
;
85 nic_rx_stats
->pkts
+= rx_stats
.pkts
;
86 nic_rx_stats
->errors
+= rx_stats
.errors
;
87 nic_rx_stats
->csum_errors
+= rx_stats
.csum_errors
;
88 nic_rx_stats
->other_errors
+= rx_stats
.other_errors
;
89 u64_stats_update_end(&nic_rx_stats
->syncp
);
91 hinic_rxq_clean_stats(rxq
);
94 static void update_tx_stats(struct hinic_dev
*nic_dev
, struct hinic_txq
*txq
)
96 struct hinic_txq_stats
*nic_tx_stats
= &nic_dev
->tx_stats
;
97 struct hinic_txq_stats tx_stats
;
99 u64_stats_init(&tx_stats
.syncp
);
101 hinic_txq_get_stats(txq
, &tx_stats
);
103 u64_stats_update_begin(&nic_tx_stats
->syncp
);
104 nic_tx_stats
->bytes
+= tx_stats
.bytes
;
105 nic_tx_stats
->pkts
+= tx_stats
.pkts
;
106 nic_tx_stats
->tx_busy
+= tx_stats
.tx_busy
;
107 nic_tx_stats
->tx_wake
+= tx_stats
.tx_wake
;
108 nic_tx_stats
->tx_dropped
+= tx_stats
.tx_dropped
;
109 nic_tx_stats
->big_frags_pkts
+= tx_stats
.big_frags_pkts
;
110 u64_stats_update_end(&nic_tx_stats
->syncp
);
112 hinic_txq_clean_stats(txq
);
115 static void update_nic_stats(struct hinic_dev
*nic_dev
)
117 int i
, num_qps
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
119 for (i
= 0; i
< num_qps
; i
++)
120 update_rx_stats(nic_dev
, &nic_dev
->rxqs
[i
]);
122 for (i
= 0; i
< num_qps
; i
++)
123 update_tx_stats(nic_dev
, &nic_dev
->txqs
[i
]);
127 * create_txqs - Create the Logical Tx Queues of specific NIC device
128 * @nic_dev: the specific NIC device
130 * Return 0 - Success, negative - Failure
132 static int create_txqs(struct hinic_dev
*nic_dev
)
134 int err
, i
, j
, num_txqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
135 struct net_device
*netdev
= nic_dev
->netdev
;
141 txq_size
= num_txqs
* sizeof(*nic_dev
->txqs
);
142 nic_dev
->txqs
= devm_kzalloc(&netdev
->dev
, txq_size
, GFP_KERNEL
);
146 for (i
= 0; i
< num_txqs
; i
++) {
147 struct hinic_sq
*sq
= hinic_hwdev_get_sq(nic_dev
->hwdev
, i
);
149 err
= hinic_init_txq(&nic_dev
->txqs
[i
], sq
, netdev
);
151 netif_err(nic_dev
, drv
, netdev
,
152 "Failed to init Txq\n");
160 for (j
= 0; j
< i
; j
++)
161 hinic_clean_txq(&nic_dev
->txqs
[j
]);
163 devm_kfree(&netdev
->dev
, nic_dev
->txqs
);
168 * free_txqs - Free the Logical Tx Queues of specific NIC device
169 * @nic_dev: the specific NIC device
171 static void free_txqs(struct hinic_dev
*nic_dev
)
173 int i
, num_txqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
174 struct net_device
*netdev
= nic_dev
->netdev
;
179 for (i
= 0; i
< num_txqs
; i
++)
180 hinic_clean_txq(&nic_dev
->txqs
[i
]);
182 devm_kfree(&netdev
->dev
, nic_dev
->txqs
);
183 nic_dev
->txqs
= NULL
;
187 * create_txqs - Create the Logical Rx Queues of specific NIC device
188 * @nic_dev: the specific NIC device
190 * Return 0 - Success, negative - Failure
192 static int create_rxqs(struct hinic_dev
*nic_dev
)
194 int err
, i
, j
, num_rxqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
195 struct net_device
*netdev
= nic_dev
->netdev
;
201 rxq_size
= num_rxqs
* sizeof(*nic_dev
->rxqs
);
202 nic_dev
->rxqs
= devm_kzalloc(&netdev
->dev
, rxq_size
, GFP_KERNEL
);
206 for (i
= 0; i
< num_rxqs
; i
++) {
207 struct hinic_rq
*rq
= hinic_hwdev_get_rq(nic_dev
->hwdev
, i
);
209 err
= hinic_init_rxq(&nic_dev
->rxqs
[i
], rq
, netdev
);
211 netif_err(nic_dev
, drv
, netdev
,
212 "Failed to init rxq\n");
220 for (j
= 0; j
< i
; j
++)
221 hinic_clean_rxq(&nic_dev
->rxqs
[j
]);
223 devm_kfree(&netdev
->dev
, nic_dev
->rxqs
);
228 * free_txqs - Free the Logical Rx Queues of specific NIC device
229 * @nic_dev: the specific NIC device
231 static void free_rxqs(struct hinic_dev
*nic_dev
)
233 int i
, num_rxqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
234 struct net_device
*netdev
= nic_dev
->netdev
;
239 for (i
= 0; i
< num_rxqs
; i
++)
240 hinic_clean_rxq(&nic_dev
->rxqs
[i
]);
242 devm_kfree(&netdev
->dev
, nic_dev
->rxqs
);
243 nic_dev
->rxqs
= NULL
;
246 static int hinic_configure_max_qnum(struct hinic_dev
*nic_dev
)
250 err
= hinic_set_max_qnum(nic_dev
, nic_dev
->hwdev
->nic_cap
.max_qps
);
257 static int hinic_rss_init(struct hinic_dev
*nic_dev
)
259 u8 default_rss_key
[HINIC_RSS_KEY_SIZE
];
260 u8 tmpl_idx
= nic_dev
->rss_tmpl_idx
;
264 indir_tbl
= kcalloc(HINIC_RSS_INDIR_SIZE
, sizeof(u32
), GFP_KERNEL
);
268 netdev_rss_key_fill(default_rss_key
, sizeof(default_rss_key
));
269 for (i
= 0; i
< HINIC_RSS_INDIR_SIZE
; i
++)
270 indir_tbl
[i
] = ethtool_rxfh_indir_default(i
, nic_dev
->num_rss
);
272 err
= hinic_rss_set_template_tbl(nic_dev
, tmpl_idx
, default_rss_key
);
276 err
= hinic_rss_set_indir_tbl(nic_dev
, tmpl_idx
, indir_tbl
);
280 err
= hinic_set_rss_type(nic_dev
, tmpl_idx
, nic_dev
->rss_type
);
284 err
= hinic_rss_set_hash_engine(nic_dev
, tmpl_idx
,
285 nic_dev
->rss_hash_engine
);
289 err
= hinic_rss_cfg(nic_dev
, 1, tmpl_idx
);
298 static void hinic_rss_deinit(struct hinic_dev
*nic_dev
)
300 hinic_rss_cfg(nic_dev
, 0, nic_dev
->rss_tmpl_idx
);
303 static void hinic_init_rss_parameters(struct hinic_dev
*nic_dev
)
305 nic_dev
->rss_hash_engine
= HINIC_RSS_HASH_ENGINE_TYPE_XOR
;
306 nic_dev
->rss_type
.tcp_ipv6_ext
= 1;
307 nic_dev
->rss_type
.ipv6_ext
= 1;
308 nic_dev
->rss_type
.tcp_ipv6
= 1;
309 nic_dev
->rss_type
.ipv6
= 1;
310 nic_dev
->rss_type
.tcp_ipv4
= 1;
311 nic_dev
->rss_type
.ipv4
= 1;
312 nic_dev
->rss_type
.udp_ipv6
= 1;
313 nic_dev
->rss_type
.udp_ipv4
= 1;
316 static void hinic_enable_rss(struct hinic_dev
*nic_dev
)
318 struct net_device
*netdev
= nic_dev
->netdev
;
319 struct hinic_hwdev
*hwdev
= nic_dev
->hwdev
;
320 struct hinic_hwif
*hwif
= hwdev
->hwif
;
321 struct pci_dev
*pdev
= hwif
->pdev
;
322 int i
, node
, err
= 0;
325 nic_dev
->max_qps
= hinic_hwdev_max_num_qps(hwdev
);
326 if (nic_dev
->max_qps
<= 1) {
327 nic_dev
->flags
&= ~HINIC_RSS_ENABLE
;
328 nic_dev
->rss_limit
= nic_dev
->max_qps
;
329 nic_dev
->num_qps
= nic_dev
->max_qps
;
330 nic_dev
->num_rss
= nic_dev
->max_qps
;
335 err
= hinic_rss_template_alloc(nic_dev
, &nic_dev
->rss_tmpl_idx
);
337 netif_err(nic_dev
, drv
, netdev
,
338 "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
339 nic_dev
->flags
&= ~HINIC_RSS_ENABLE
;
340 nic_dev
->max_qps
= 1;
341 nic_dev
->rss_limit
= nic_dev
->max_qps
;
342 nic_dev
->num_qps
= nic_dev
->max_qps
;
343 nic_dev
->num_rss
= nic_dev
->max_qps
;
348 nic_dev
->flags
|= HINIC_RSS_ENABLE
;
350 for (i
= 0; i
< num_online_cpus(); i
++) {
351 node
= cpu_to_node(i
);
352 if (node
== dev_to_node(&pdev
->dev
))
357 num_cpus
= num_online_cpus();
359 nic_dev
->num_qps
= min_t(u16
, nic_dev
->max_qps
, num_cpus
);
361 nic_dev
->rss_limit
= nic_dev
->num_qps
;
362 nic_dev
->num_rss
= nic_dev
->num_qps
;
364 hinic_init_rss_parameters(nic_dev
);
365 err
= hinic_rss_init(nic_dev
);
367 netif_err(nic_dev
, drv
, netdev
, "Failed to init rss\n");
370 static int hinic_open(struct net_device
*netdev
)
372 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
373 enum hinic_port_link_state link_state
;
376 if (!(nic_dev
->flags
& HINIC_INTF_UP
)) {
377 err
= hinic_hwdev_ifup(nic_dev
->hwdev
);
379 netif_err(nic_dev
, drv
, netdev
,
380 "Failed - HW interface up\n");
385 err
= create_txqs(nic_dev
);
387 netif_err(nic_dev
, drv
, netdev
,
388 "Failed to create Tx queues\n");
389 goto err_create_txqs
;
392 err
= create_rxqs(nic_dev
);
394 netif_err(nic_dev
, drv
, netdev
,
395 "Failed to create Rx queues\n");
396 goto err_create_rxqs
;
399 hinic_enable_rss(nic_dev
);
401 err
= hinic_configure_max_qnum(nic_dev
);
403 netif_err(nic_dev
, drv
, nic_dev
->netdev
,
404 "Failed to configure the maximum number of queues\n");
408 netif_set_real_num_tx_queues(netdev
, nic_dev
->num_qps
);
409 netif_set_real_num_rx_queues(netdev
, nic_dev
->num_qps
);
411 err
= hinic_port_set_state(nic_dev
, HINIC_PORT_ENABLE
);
413 netif_err(nic_dev
, drv
, netdev
,
414 "Failed to set port state\n");
418 err
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_ENABLE
);
420 netif_err(nic_dev
, drv
, netdev
,
421 "Failed to set func port state\n");
422 goto err_func_port_state
;
425 /* Wait up to 3 sec between port enable to link state */
428 down(&nic_dev
->mgmt_lock
);
430 err
= hinic_port_link_state(nic_dev
, &link_state
);
432 netif_err(nic_dev
, drv
, netdev
, "Failed to get link state\n");
436 if (link_state
== HINIC_LINK_STATE_UP
)
437 nic_dev
->flags
|= HINIC_LINK_UP
;
439 nic_dev
->flags
|= HINIC_INTF_UP
;
441 if ((nic_dev
->flags
& (HINIC_LINK_UP
| HINIC_INTF_UP
)) ==
442 (HINIC_LINK_UP
| HINIC_INTF_UP
)) {
443 netif_info(nic_dev
, drv
, netdev
, "link + intf UP\n");
444 netif_carrier_on(netdev
);
445 netif_tx_wake_all_queues(netdev
);
448 up(&nic_dev
->mgmt_lock
);
450 netif_info(nic_dev
, drv
, netdev
, "HINIC_INTF is UP\n");
454 up(&nic_dev
->mgmt_lock
);
455 ret
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_DISABLE
);
457 netif_warn(nic_dev
, drv
, netdev
,
458 "Failed to revert func port state\n");
461 ret
= hinic_port_set_state(nic_dev
, HINIC_PORT_DISABLE
);
463 netif_warn(nic_dev
, drv
, netdev
,
464 "Failed to revert port state\n");
467 if (nic_dev
->flags
& HINIC_RSS_ENABLE
) {
468 hinic_rss_deinit(nic_dev
);
469 hinic_rss_template_free(nic_dev
, nic_dev
->rss_tmpl_idx
);
476 if (!(nic_dev
->flags
& HINIC_INTF_UP
))
477 hinic_hwdev_ifdown(nic_dev
->hwdev
);
481 static int hinic_close(struct net_device
*netdev
)
483 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
487 down(&nic_dev
->mgmt_lock
);
489 flags
= nic_dev
->flags
;
490 nic_dev
->flags
&= ~HINIC_INTF_UP
;
492 netif_carrier_off(netdev
);
493 netif_tx_disable(netdev
);
495 update_nic_stats(nic_dev
);
497 up(&nic_dev
->mgmt_lock
);
499 err
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_DISABLE
);
501 netif_err(nic_dev
, drv
, netdev
,
502 "Failed to set func port state\n");
503 nic_dev
->flags
|= (flags
& HINIC_INTF_UP
);
507 err
= hinic_port_set_state(nic_dev
, HINIC_PORT_DISABLE
);
509 netif_err(nic_dev
, drv
, netdev
, "Failed to set port state\n");
510 nic_dev
->flags
|= (flags
& HINIC_INTF_UP
);
514 if (nic_dev
->flags
& HINIC_RSS_ENABLE
) {
515 hinic_rss_deinit(nic_dev
);
516 hinic_rss_template_free(nic_dev
, nic_dev
->rss_tmpl_idx
);
522 if (flags
& HINIC_INTF_UP
)
523 hinic_hwdev_ifdown(nic_dev
->hwdev
);
525 netif_info(nic_dev
, drv
, netdev
, "HINIC_INTF is DOWN\n");
529 static int hinic_change_mtu(struct net_device
*netdev
, int new_mtu
)
531 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
534 netif_info(nic_dev
, drv
, netdev
, "set_mtu = %d\n", new_mtu
);
536 err
= hinic_port_set_mtu(nic_dev
, new_mtu
);
538 netif_err(nic_dev
, drv
, netdev
, "Failed to set port mtu\n");
540 netdev
->mtu
= new_mtu
;
546 * change_mac_addr - change the main mac address of network device
547 * @netdev: network device
548 * @addr: mac address to set
550 * Return 0 - Success, negative - Failure
552 static int change_mac_addr(struct net_device
*netdev
, const u8
*addr
)
554 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
558 if (!is_valid_ether_addr(addr
))
559 return -EADDRNOTAVAIL
;
561 netif_info(nic_dev
, drv
, netdev
, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
562 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
564 down(&nic_dev
->mgmt_lock
);
567 err
= hinic_port_del_mac(nic_dev
, netdev
->dev_addr
, vid
);
569 netif_err(nic_dev
, drv
, netdev
,
570 "Failed to delete mac\n");
574 err
= hinic_port_add_mac(nic_dev
, addr
, vid
);
576 netif_err(nic_dev
, drv
, netdev
, "Failed to add mac\n");
580 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
581 } while (vid
!= VLAN_N_VID
);
583 up(&nic_dev
->mgmt_lock
);
587 static int hinic_set_mac_addr(struct net_device
*netdev
, void *addr
)
589 unsigned char new_mac
[ETH_ALEN
];
590 struct sockaddr
*saddr
= addr
;
593 memcpy(new_mac
, saddr
->sa_data
, ETH_ALEN
);
595 err
= change_mac_addr(netdev
, new_mac
);
597 memcpy(netdev
->dev_addr
, new_mac
, ETH_ALEN
);
603 * add_mac_addr - add mac address to network device
604 * @netdev: network device
605 * @addr: mac address to add
607 * Return 0 - Success, negative - Failure
609 static int add_mac_addr(struct net_device
*netdev
, const u8
*addr
)
611 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
615 netif_info(nic_dev
, drv
, netdev
, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
616 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
618 down(&nic_dev
->mgmt_lock
);
621 err
= hinic_port_add_mac(nic_dev
, addr
, vid
);
623 netif_err(nic_dev
, drv
, netdev
, "Failed to add mac\n");
627 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
628 } while (vid
!= VLAN_N_VID
);
630 up(&nic_dev
->mgmt_lock
);
635 * remove_mac_addr - remove mac address from network device
636 * @netdev: network device
637 * @addr: mac address to remove
639 * Return 0 - Success, negative - Failure
641 static int remove_mac_addr(struct net_device
*netdev
, const u8
*addr
)
643 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
647 if (!is_valid_ether_addr(addr
))
648 return -EADDRNOTAVAIL
;
650 netif_info(nic_dev
, drv
, netdev
, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
651 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
653 down(&nic_dev
->mgmt_lock
);
656 err
= hinic_port_del_mac(nic_dev
, addr
, vid
);
658 netif_err(nic_dev
, drv
, netdev
,
659 "Failed to delete mac\n");
663 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
664 } while (vid
!= VLAN_N_VID
);
666 up(&nic_dev
->mgmt_lock
);
670 static int hinic_vlan_rx_add_vid(struct net_device
*netdev
,
671 __always_unused __be16 proto
, u16 vid
)
673 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
676 netif_info(nic_dev
, drv
, netdev
, "add vid = %d\n", vid
);
678 down(&nic_dev
->mgmt_lock
);
680 err
= hinic_port_add_vlan(nic_dev
, vid
);
682 netif_err(nic_dev
, drv
, netdev
, "Failed to add vlan\n");
686 err
= hinic_port_add_mac(nic_dev
, netdev
->dev_addr
, vid
);
688 netif_err(nic_dev
, drv
, netdev
, "Failed to set mac\n");
692 bitmap_set(nic_dev
->vlan_bitmap
, vid
, 1);
694 up(&nic_dev
->mgmt_lock
);
698 ret
= hinic_port_del_vlan(nic_dev
, vid
);
700 netif_err(nic_dev
, drv
, netdev
,
701 "Failed to revert by removing vlan\n");
704 up(&nic_dev
->mgmt_lock
);
708 static int hinic_vlan_rx_kill_vid(struct net_device
*netdev
,
709 __always_unused __be16 proto
, u16 vid
)
711 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
714 netif_info(nic_dev
, drv
, netdev
, "remove vid = %d\n", vid
);
716 down(&nic_dev
->mgmt_lock
);
718 err
= hinic_port_del_vlan(nic_dev
, vid
);
720 netif_err(nic_dev
, drv
, netdev
, "Failed to delete vlan\n");
724 bitmap_clear(nic_dev
->vlan_bitmap
, vid
, 1);
726 up(&nic_dev
->mgmt_lock
);
730 up(&nic_dev
->mgmt_lock
);
734 static void set_rx_mode(struct work_struct
*work
)
736 struct hinic_rx_mode_work
*rx_mode_work
= work_to_rx_mode_work(work
);
737 struct hinic_dev
*nic_dev
= rx_mode_work_to_nic_dev(rx_mode_work
);
739 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "set rx mode work\n");
741 hinic_port_set_rx_mode(nic_dev
, rx_mode_work
->rx_mode
);
743 __dev_uc_sync(nic_dev
->netdev
, add_mac_addr
, remove_mac_addr
);
744 __dev_mc_sync(nic_dev
->netdev
, add_mac_addr
, remove_mac_addr
);
747 static void hinic_set_rx_mode(struct net_device
*netdev
)
749 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
750 struct hinic_rx_mode_work
*rx_mode_work
;
753 rx_mode_work
= &nic_dev
->rx_mode_work
;
755 rx_mode
= HINIC_RX_MODE_UC
|
759 if (netdev
->flags
& IFF_PROMISC
)
760 rx_mode
|= HINIC_RX_MODE_PROMISC
;
761 else if (netdev
->flags
& IFF_ALLMULTI
)
762 rx_mode
|= HINIC_RX_MODE_MC_ALL
;
764 rx_mode_work
->rx_mode
= rx_mode
;
766 queue_work(nic_dev
->workq
, &rx_mode_work
->work
);
769 static void hinic_tx_timeout(struct net_device
*netdev
, unsigned int txqueue
)
771 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
773 netif_err(nic_dev
, drv
, netdev
, "Tx timeout\n");
776 static void hinic_get_stats64(struct net_device
*netdev
,
777 struct rtnl_link_stats64
*stats
)
779 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
780 struct hinic_rxq_stats
*nic_rx_stats
;
781 struct hinic_txq_stats
*nic_tx_stats
;
783 nic_rx_stats
= &nic_dev
->rx_stats
;
784 nic_tx_stats
= &nic_dev
->tx_stats
;
786 down(&nic_dev
->mgmt_lock
);
788 if (nic_dev
->flags
& HINIC_INTF_UP
)
789 update_nic_stats(nic_dev
);
791 up(&nic_dev
->mgmt_lock
);
793 stats
->rx_bytes
= nic_rx_stats
->bytes
;
794 stats
->rx_packets
= nic_rx_stats
->pkts
;
795 stats
->rx_errors
= nic_rx_stats
->errors
;
797 stats
->tx_bytes
= nic_tx_stats
->bytes
;
798 stats
->tx_packets
= nic_tx_stats
->pkts
;
799 stats
->tx_errors
= nic_tx_stats
->tx_dropped
;
802 static int hinic_set_features(struct net_device
*netdev
,
803 netdev_features_t features
)
805 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
807 return set_features(nic_dev
, nic_dev
->netdev
->features
,
811 static netdev_features_t
hinic_fix_features(struct net_device
*netdev
,
812 netdev_features_t features
)
814 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
816 /* If Rx checksum is disabled, then LRO should also be disabled */
817 if (!(features
& NETIF_F_RXCSUM
)) {
818 netif_info(nic_dev
, drv
, netdev
, "disabling LRO as RXCSUM is off\n");
819 features
&= ~NETIF_F_LRO
;
825 static const struct net_device_ops hinic_netdev_ops
= {
826 .ndo_open
= hinic_open
,
827 .ndo_stop
= hinic_close
,
828 .ndo_change_mtu
= hinic_change_mtu
,
829 .ndo_set_mac_address
= hinic_set_mac_addr
,
830 .ndo_validate_addr
= eth_validate_addr
,
831 .ndo_vlan_rx_add_vid
= hinic_vlan_rx_add_vid
,
832 .ndo_vlan_rx_kill_vid
= hinic_vlan_rx_kill_vid
,
833 .ndo_set_rx_mode
= hinic_set_rx_mode
,
834 .ndo_start_xmit
= hinic_xmit_frame
,
835 .ndo_tx_timeout
= hinic_tx_timeout
,
836 .ndo_get_stats64
= hinic_get_stats64
,
837 .ndo_fix_features
= hinic_fix_features
,
838 .ndo_set_features
= hinic_set_features
,
841 static void netdev_features_init(struct net_device
*netdev
)
843 netdev
->hw_features
= NETIF_F_SG
| NETIF_F_HIGHDMA
| NETIF_F_IP_CSUM
|
844 NETIF_F_IPV6_CSUM
| NETIF_F_TSO
| NETIF_F_TSO6
|
845 NETIF_F_RXCSUM
| NETIF_F_LRO
|
846 NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
;
848 netdev
->vlan_features
= netdev
->hw_features
;
850 netdev
->features
= netdev
->hw_features
| NETIF_F_HW_VLAN_CTAG_FILTER
;
854 * link_status_event_handler - link event handler
855 * @handle: nic device for the handler
856 * @buf_in: input buffer
857 * @in_size: input size
858 * @buf_in: output buffer
859 * @out_size: returned output size
861 * Return 0 - Success, negative - Failure
863 static void link_status_event_handler(void *handle
, void *buf_in
, u16 in_size
,
864 void *buf_out
, u16
*out_size
)
866 struct hinic_port_link_status
*link_status
, *ret_link_status
;
867 struct hinic_dev
*nic_dev
= handle
;
869 link_status
= buf_in
;
871 if (link_status
->link
== HINIC_LINK_STATE_UP
) {
872 down(&nic_dev
->mgmt_lock
);
874 nic_dev
->flags
|= HINIC_LINK_UP
;
876 if ((nic_dev
->flags
& (HINIC_LINK_UP
| HINIC_INTF_UP
)) ==
877 (HINIC_LINK_UP
| HINIC_INTF_UP
)) {
878 netif_carrier_on(nic_dev
->netdev
);
879 netif_tx_wake_all_queues(nic_dev
->netdev
);
882 up(&nic_dev
->mgmt_lock
);
884 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "HINIC_Link is UP\n");
886 down(&nic_dev
->mgmt_lock
);
888 nic_dev
->flags
&= ~HINIC_LINK_UP
;
890 netif_carrier_off(nic_dev
->netdev
);
891 netif_tx_disable(nic_dev
->netdev
);
893 up(&nic_dev
->mgmt_lock
);
895 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "HINIC_Link is DOWN\n");
898 ret_link_status
= buf_out
;
899 ret_link_status
->status
= 0;
901 *out_size
= sizeof(*ret_link_status
);
904 static int set_features(struct hinic_dev
*nic_dev
,
905 netdev_features_t pre_features
,
906 netdev_features_t features
, bool force_change
)
908 netdev_features_t changed
= force_change
? ~0 : pre_features
^ features
;
909 u32 csum_en
= HINIC_RX_CSUM_OFFLOAD_EN
;
912 if (changed
& NETIF_F_TSO
)
913 err
= hinic_port_set_tso(nic_dev
, (features
& NETIF_F_TSO
) ?
914 HINIC_TSO_ENABLE
: HINIC_TSO_DISABLE
);
916 if (changed
& NETIF_F_RXCSUM
)
917 err
= hinic_set_rx_csum_offload(nic_dev
, csum_en
);
919 if (changed
& NETIF_F_LRO
) {
920 err
= hinic_set_rx_lro_state(nic_dev
,
921 !!(features
& NETIF_F_LRO
),
922 HINIC_LRO_RX_TIMER_DEFAULT
,
923 HINIC_LRO_MAX_WQE_NUM_DEFAULT
);
926 if (changed
& NETIF_F_HW_VLAN_CTAG_RX
)
927 err
= hinic_set_rx_vlan_offload(nic_dev
,
929 NETIF_F_HW_VLAN_CTAG_RX
));
935 * nic_dev_init - Initialize the NIC device
936 * @pdev: the NIC pci device
938 * Return 0 - Success, negative - Failure
940 static int nic_dev_init(struct pci_dev
*pdev
)
942 struct hinic_rx_mode_work
*rx_mode_work
;
943 struct hinic_txq_stats
*tx_stats
;
944 struct hinic_rxq_stats
*rx_stats
;
945 struct hinic_dev
*nic_dev
;
946 struct net_device
*netdev
;
947 struct hinic_hwdev
*hwdev
;
950 hwdev
= hinic_init_hwdev(pdev
);
952 dev_err(&pdev
->dev
, "Failed to initialize HW device\n");
953 return PTR_ERR(hwdev
);
956 num_qps
= hinic_hwdev_num_qps(hwdev
);
958 dev_err(&pdev
->dev
, "Invalid number of QPS\n");
963 netdev
= alloc_etherdev_mq(sizeof(*nic_dev
), num_qps
);
965 dev_err(&pdev
->dev
, "Failed to allocate Ethernet device\n");
967 goto err_alloc_etherdev
;
970 hinic_set_ethtool_ops(netdev
);
971 netdev
->netdev_ops
= &hinic_netdev_ops
;
972 netdev
->max_mtu
= ETH_MAX_MTU
;
974 nic_dev
= netdev_priv(netdev
);
975 nic_dev
->netdev
= netdev
;
976 nic_dev
->hwdev
= hwdev
;
977 nic_dev
->msg_enable
= MSG_ENABLE_DEFAULT
;
979 nic_dev
->txqs
= NULL
;
980 nic_dev
->rxqs
= NULL
;
981 nic_dev
->tx_weight
= tx_weight
;
982 nic_dev
->rx_weight
= rx_weight
;
984 sema_init(&nic_dev
->mgmt_lock
, 1);
986 tx_stats
= &nic_dev
->tx_stats
;
987 rx_stats
= &nic_dev
->rx_stats
;
989 u64_stats_init(&tx_stats
->syncp
);
990 u64_stats_init(&rx_stats
->syncp
);
992 nic_dev
->vlan_bitmap
= devm_kzalloc(&pdev
->dev
,
993 VLAN_BITMAP_SIZE(nic_dev
),
995 if (!nic_dev
->vlan_bitmap
) {
997 goto err_vlan_bitmap
;
1000 nic_dev
->workq
= create_singlethread_workqueue(HINIC_WQ_NAME
);
1001 if (!nic_dev
->workq
) {
1006 pci_set_drvdata(pdev
, netdev
);
1008 err
= hinic_port_get_mac(nic_dev
, netdev
->dev_addr
);
1010 dev_warn(&pdev
->dev
, "Failed to get mac address\n");
1012 err
= hinic_port_add_mac(nic_dev
, netdev
->dev_addr
, 0);
1014 dev_err(&pdev
->dev
, "Failed to add mac\n");
1018 err
= hinic_port_set_mtu(nic_dev
, netdev
->mtu
);
1020 dev_err(&pdev
->dev
, "Failed to set mtu\n");
1024 rx_mode_work
= &nic_dev
->rx_mode_work
;
1025 INIT_WORK(&rx_mode_work
->work
, set_rx_mode
);
1027 netdev_features_init(netdev
);
1029 netif_carrier_off(netdev
);
1031 hinic_hwdev_cb_register(nic_dev
->hwdev
, HINIC_MGMT_MSG_CMD_LINK_STATUS
,
1032 nic_dev
, link_status_event_handler
);
1034 err
= set_features(nic_dev
, 0, nic_dev
->netdev
->features
, true);
1036 goto err_set_features
;
1038 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
1040 err
= register_netdev(netdev
);
1042 dev_err(&pdev
->dev
, "Failed to register netdev\n");
1043 goto err_reg_netdev
;
1050 hinic_hwdev_cb_unregister(nic_dev
->hwdev
,
1051 HINIC_MGMT_MSG_CMD_LINK_STATUS
);
1052 cancel_work_sync(&rx_mode_work
->work
);
1056 pci_set_drvdata(pdev
, NULL
);
1057 destroy_workqueue(nic_dev
->workq
);
1061 free_netdev(netdev
);
1065 hinic_free_hwdev(hwdev
);
1069 static int hinic_probe(struct pci_dev
*pdev
,
1070 const struct pci_device_id
*id
)
1072 int err
= pci_enable_device(pdev
);
1075 dev_err(&pdev
->dev
, "Failed to enable PCI device\n");
1079 err
= pci_request_regions(pdev
, HINIC_DRV_NAME
);
1081 dev_err(&pdev
->dev
, "Failed to request PCI regions\n");
1082 goto err_pci_regions
;
1085 pci_set_master(pdev
);
1087 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
1089 dev_warn(&pdev
->dev
, "Couldn't set 64-bit DMA mask\n");
1090 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
1092 dev_err(&pdev
->dev
, "Failed to set DMA mask\n");
1097 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
1099 dev_warn(&pdev
->dev
,
1100 "Couldn't set 64-bit consistent DMA mask\n");
1101 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
1104 "Failed to set consistent DMA mask\n");
1105 goto err_dma_consistent_mask
;
1109 err
= nic_dev_init(pdev
);
1111 dev_err(&pdev
->dev
, "Failed to initialize NIC device\n");
1112 goto err_nic_dev_init
;
1115 dev_info(&pdev
->dev
, "HiNIC driver - probed\n");
1119 err_dma_consistent_mask
:
1121 pci_release_regions(pdev
);
1124 pci_disable_device(pdev
);
1128 static void hinic_remove(struct pci_dev
*pdev
)
1130 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1131 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
1132 struct hinic_rx_mode_work
*rx_mode_work
;
1134 unregister_netdev(netdev
);
1136 hinic_hwdev_cb_unregister(nic_dev
->hwdev
,
1137 HINIC_MGMT_MSG_CMD_LINK_STATUS
);
1139 rx_mode_work
= &nic_dev
->rx_mode_work
;
1140 cancel_work_sync(&rx_mode_work
->work
);
1142 pci_set_drvdata(pdev
, NULL
);
1144 destroy_workqueue(nic_dev
->workq
);
1146 hinic_free_hwdev(nic_dev
->hwdev
);
1148 free_netdev(netdev
);
1150 pci_release_regions(pdev
);
1151 pci_disable_device(pdev
);
1153 dev_info(&pdev
->dev
, "HiNIC driver - removed\n");
1156 static void hinic_shutdown(struct pci_dev
*pdev
)
1158 pci_disable_device(pdev
);
1161 static const struct pci_device_id hinic_pci_table
[] = {
1162 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_QUAD_PORT_25GE
), 0},
1163 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_DUAL_PORT_100GE
), 0},
1164 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ
), 0},
1165 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ
), 0},
1168 MODULE_DEVICE_TABLE(pci
, hinic_pci_table
);
1170 static struct pci_driver hinic_driver
= {
1171 .name
= HINIC_DRV_NAME
,
1172 .id_table
= hinic_pci_table
,
1173 .probe
= hinic_probe
,
1174 .remove
= hinic_remove
,
1175 .shutdown
= hinic_shutdown
,
1178 module_pci_driver(hinic_driver
);