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 HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
55 #define HINIC_DEV_ID_DUAL_PORT_25GE 0x0200
56 #define HINIC_DEV_ID_DUAL_PORT_100GE 0x0201
58 #define HINIC_WQ_NAME "hinic_dev"
60 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
62 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
64 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
66 #define work_to_rx_mode_work(work) \
67 container_of(work, struct hinic_rx_mode_work, work)
69 #define rx_mode_work_to_nic_dev(rx_mode_work) \
70 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
72 static int change_mac_addr(struct net_device
*netdev
, const u8
*addr
);
74 static void set_link_speed(struct ethtool_link_ksettings
*link_ksettings
,
75 enum hinic_speed speed
)
78 case HINIC_SPEED_10MB_LINK
:
79 link_ksettings
->base
.speed
= SPEED_10
;
82 case HINIC_SPEED_100MB_LINK
:
83 link_ksettings
->base
.speed
= SPEED_100
;
86 case HINIC_SPEED_1000MB_LINK
:
87 link_ksettings
->base
.speed
= SPEED_1000
;
90 case HINIC_SPEED_10GB_LINK
:
91 link_ksettings
->base
.speed
= SPEED_10000
;
94 case HINIC_SPEED_25GB_LINK
:
95 link_ksettings
->base
.speed
= SPEED_25000
;
98 case HINIC_SPEED_40GB_LINK
:
99 link_ksettings
->base
.speed
= SPEED_40000
;
102 case HINIC_SPEED_100GB_LINK
:
103 link_ksettings
->base
.speed
= SPEED_100000
;
107 link_ksettings
->base
.speed
= SPEED_UNKNOWN
;
112 static int hinic_get_link_ksettings(struct net_device
*netdev
,
113 struct ethtool_link_ksettings
116 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
117 enum hinic_port_link_state link_state
;
118 struct hinic_port_cap port_cap
;
121 ethtool_link_ksettings_zero_link_mode(link_ksettings
, advertising
);
122 ethtool_link_ksettings_add_link_mode(link_ksettings
, supported
,
125 link_ksettings
->base
.speed
= SPEED_UNKNOWN
;
126 link_ksettings
->base
.autoneg
= AUTONEG_DISABLE
;
127 link_ksettings
->base
.duplex
= DUPLEX_UNKNOWN
;
129 err
= hinic_port_get_cap(nic_dev
, &port_cap
);
131 netif_err(nic_dev
, drv
, netdev
,
132 "Failed to get port capabilities\n");
136 err
= hinic_port_link_state(nic_dev
, &link_state
);
138 netif_err(nic_dev
, drv
, netdev
,
139 "Failed to get port link state\n");
143 if (link_state
!= HINIC_LINK_STATE_UP
) {
144 netif_info(nic_dev
, drv
, netdev
, "No link\n");
148 set_link_speed(link_ksettings
, port_cap
.speed
);
150 if (!!(port_cap
.autoneg_cap
& HINIC_AUTONEG_SUPPORTED
))
151 ethtool_link_ksettings_add_link_mode(link_ksettings
,
152 advertising
, Autoneg
);
154 if (port_cap
.autoneg_state
== HINIC_AUTONEG_ACTIVE
)
155 link_ksettings
->base
.autoneg
= AUTONEG_ENABLE
;
157 link_ksettings
->base
.duplex
= (port_cap
.duplex
== HINIC_DUPLEX_FULL
) ?
158 DUPLEX_FULL
: DUPLEX_HALF
;
162 static void hinic_get_drvinfo(struct net_device
*netdev
,
163 struct ethtool_drvinfo
*info
)
165 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
166 struct hinic_hwdev
*hwdev
= nic_dev
->hwdev
;
167 struct hinic_hwif
*hwif
= hwdev
->hwif
;
169 strlcpy(info
->driver
, HINIC_DRV_NAME
, sizeof(info
->driver
));
170 strlcpy(info
->bus_info
, pci_name(hwif
->pdev
), sizeof(info
->bus_info
));
173 static void hinic_get_ringparam(struct net_device
*netdev
,
174 struct ethtool_ringparam
*ring
)
176 ring
->rx_max_pending
= HINIC_RQ_DEPTH
;
177 ring
->tx_max_pending
= HINIC_SQ_DEPTH
;
178 ring
->rx_pending
= HINIC_RQ_DEPTH
;
179 ring
->tx_pending
= HINIC_SQ_DEPTH
;
182 static void hinic_get_channels(struct net_device
*netdev
,
183 struct ethtool_channels
*channels
)
185 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
186 struct hinic_hwdev
*hwdev
= nic_dev
->hwdev
;
188 channels
->max_rx
= hwdev
->nic_cap
.max_qps
;
189 channels
->max_tx
= hwdev
->nic_cap
.max_qps
;
190 channels
->max_other
= 0;
191 channels
->max_combined
= 0;
192 channels
->rx_count
= hinic_hwdev_num_qps(hwdev
);
193 channels
->tx_count
= hinic_hwdev_num_qps(hwdev
);
194 channels
->other_count
= 0;
195 channels
->combined_count
= 0;
198 static const struct ethtool_ops hinic_ethtool_ops
= {
199 .get_link_ksettings
= hinic_get_link_ksettings
,
200 .get_drvinfo
= hinic_get_drvinfo
,
201 .get_link
= ethtool_op_get_link
,
202 .get_ringparam
= hinic_get_ringparam
,
203 .get_channels
= hinic_get_channels
,
206 static void update_rx_stats(struct hinic_dev
*nic_dev
, struct hinic_rxq
*rxq
)
208 struct hinic_rxq_stats
*nic_rx_stats
= &nic_dev
->rx_stats
;
209 struct hinic_rxq_stats rx_stats
;
211 u64_stats_init(&rx_stats
.syncp
);
213 hinic_rxq_get_stats(rxq
, &rx_stats
);
215 u64_stats_update_begin(&nic_rx_stats
->syncp
);
216 nic_rx_stats
->bytes
+= rx_stats
.bytes
;
217 nic_rx_stats
->pkts
+= rx_stats
.pkts
;
218 u64_stats_update_end(&nic_rx_stats
->syncp
);
220 hinic_rxq_clean_stats(rxq
);
223 static void update_tx_stats(struct hinic_dev
*nic_dev
, struct hinic_txq
*txq
)
225 struct hinic_txq_stats
*nic_tx_stats
= &nic_dev
->tx_stats
;
226 struct hinic_txq_stats tx_stats
;
228 u64_stats_init(&tx_stats
.syncp
);
230 hinic_txq_get_stats(txq
, &tx_stats
);
232 u64_stats_update_begin(&nic_tx_stats
->syncp
);
233 nic_tx_stats
->bytes
+= tx_stats
.bytes
;
234 nic_tx_stats
->pkts
+= tx_stats
.pkts
;
235 nic_tx_stats
->tx_busy
+= tx_stats
.tx_busy
;
236 nic_tx_stats
->tx_wake
+= tx_stats
.tx_wake
;
237 nic_tx_stats
->tx_dropped
+= tx_stats
.tx_dropped
;
238 u64_stats_update_end(&nic_tx_stats
->syncp
);
240 hinic_txq_clean_stats(txq
);
243 static void update_nic_stats(struct hinic_dev
*nic_dev
)
245 int i
, num_qps
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
247 for (i
= 0; i
< num_qps
; i
++)
248 update_rx_stats(nic_dev
, &nic_dev
->rxqs
[i
]);
250 for (i
= 0; i
< num_qps
; i
++)
251 update_tx_stats(nic_dev
, &nic_dev
->txqs
[i
]);
255 * create_txqs - Create the Logical Tx Queues of specific NIC device
256 * @nic_dev: the specific NIC device
258 * Return 0 - Success, negative - Failure
260 static int create_txqs(struct hinic_dev
*nic_dev
)
262 int err
, i
, j
, num_txqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
263 struct net_device
*netdev
= nic_dev
->netdev
;
269 txq_size
= num_txqs
* sizeof(*nic_dev
->txqs
);
270 nic_dev
->txqs
= devm_kzalloc(&netdev
->dev
, txq_size
, GFP_KERNEL
);
274 for (i
= 0; i
< num_txqs
; i
++) {
275 struct hinic_sq
*sq
= hinic_hwdev_get_sq(nic_dev
->hwdev
, i
);
277 err
= hinic_init_txq(&nic_dev
->txqs
[i
], sq
, netdev
);
279 netif_err(nic_dev
, drv
, netdev
,
280 "Failed to init Txq\n");
288 for (j
= 0; j
< i
; j
++)
289 hinic_clean_txq(&nic_dev
->txqs
[j
]);
291 devm_kfree(&netdev
->dev
, nic_dev
->txqs
);
296 * free_txqs - Free the Logical Tx Queues of specific NIC device
297 * @nic_dev: the specific NIC device
299 static void free_txqs(struct hinic_dev
*nic_dev
)
301 int i
, num_txqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
302 struct net_device
*netdev
= nic_dev
->netdev
;
307 for (i
= 0; i
< num_txqs
; i
++)
308 hinic_clean_txq(&nic_dev
->txqs
[i
]);
310 devm_kfree(&netdev
->dev
, nic_dev
->txqs
);
311 nic_dev
->txqs
= NULL
;
315 * create_txqs - Create the Logical Rx Queues of specific NIC device
316 * @nic_dev: the specific NIC device
318 * Return 0 - Success, negative - Failure
320 static int create_rxqs(struct hinic_dev
*nic_dev
)
322 int err
, i
, j
, num_rxqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
323 struct net_device
*netdev
= nic_dev
->netdev
;
329 rxq_size
= num_rxqs
* sizeof(*nic_dev
->rxqs
);
330 nic_dev
->rxqs
= devm_kzalloc(&netdev
->dev
, rxq_size
, GFP_KERNEL
);
334 for (i
= 0; i
< num_rxqs
; i
++) {
335 struct hinic_rq
*rq
= hinic_hwdev_get_rq(nic_dev
->hwdev
, i
);
337 err
= hinic_init_rxq(&nic_dev
->rxqs
[i
], rq
, netdev
);
339 netif_err(nic_dev
, drv
, netdev
,
340 "Failed to init rxq\n");
348 for (j
= 0; j
< i
; j
++)
349 hinic_clean_rxq(&nic_dev
->rxqs
[j
]);
351 devm_kfree(&netdev
->dev
, nic_dev
->rxqs
);
356 * free_txqs - Free the Logical Rx Queues of specific NIC device
357 * @nic_dev: the specific NIC device
359 static void free_rxqs(struct hinic_dev
*nic_dev
)
361 int i
, num_rxqs
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
362 struct net_device
*netdev
= nic_dev
->netdev
;
367 for (i
= 0; i
< num_rxqs
; i
++)
368 hinic_clean_rxq(&nic_dev
->rxqs
[i
]);
370 devm_kfree(&netdev
->dev
, nic_dev
->rxqs
);
371 nic_dev
->rxqs
= NULL
;
374 static int hinic_open(struct net_device
*netdev
)
376 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
377 enum hinic_port_link_state link_state
;
378 int err
, ret
, num_qps
;
380 if (!(nic_dev
->flags
& HINIC_INTF_UP
)) {
381 err
= hinic_hwdev_ifup(nic_dev
->hwdev
);
383 netif_err(nic_dev
, drv
, netdev
,
384 "Failed - HW interface up\n");
389 err
= create_txqs(nic_dev
);
391 netif_err(nic_dev
, drv
, netdev
,
392 "Failed to create Tx queues\n");
393 goto err_create_txqs
;
396 err
= create_rxqs(nic_dev
);
398 netif_err(nic_dev
, drv
, netdev
,
399 "Failed to create Rx queues\n");
400 goto err_create_rxqs
;
403 num_qps
= hinic_hwdev_num_qps(nic_dev
->hwdev
);
404 netif_set_real_num_tx_queues(netdev
, num_qps
);
405 netif_set_real_num_rx_queues(netdev
, num_qps
);
407 err
= hinic_port_set_state(nic_dev
, HINIC_PORT_ENABLE
);
409 netif_err(nic_dev
, drv
, netdev
,
410 "Failed to set port state\n");
414 err
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_ENABLE
);
416 netif_err(nic_dev
, drv
, netdev
,
417 "Failed to set func port state\n");
418 goto err_func_port_state
;
421 /* Wait up to 3 sec between port enable to link state */
424 down(&nic_dev
->mgmt_lock
);
426 err
= hinic_port_link_state(nic_dev
, &link_state
);
428 netif_err(nic_dev
, drv
, netdev
, "Failed to get link state\n");
432 if (link_state
== HINIC_LINK_STATE_UP
)
433 nic_dev
->flags
|= HINIC_LINK_UP
;
435 nic_dev
->flags
|= HINIC_INTF_UP
;
437 if ((nic_dev
->flags
& (HINIC_LINK_UP
| HINIC_INTF_UP
)) ==
438 (HINIC_LINK_UP
| HINIC_INTF_UP
)) {
439 netif_info(nic_dev
, drv
, netdev
, "link + intf UP\n");
440 netif_carrier_on(netdev
);
441 netif_tx_wake_all_queues(netdev
);
444 up(&nic_dev
->mgmt_lock
);
446 netif_info(nic_dev
, drv
, netdev
, "HINIC_INTF is UP\n");
450 up(&nic_dev
->mgmt_lock
);
451 ret
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_DISABLE
);
453 netif_warn(nic_dev
, drv
, netdev
,
454 "Failed to revert func port state\n");
457 ret
= hinic_port_set_state(nic_dev
, HINIC_PORT_DISABLE
);
459 netif_warn(nic_dev
, drv
, netdev
,
460 "Failed to revert port state\n");
469 if (!(nic_dev
->flags
& HINIC_INTF_UP
))
470 hinic_hwdev_ifdown(nic_dev
->hwdev
);
474 static int hinic_close(struct net_device
*netdev
)
476 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
480 down(&nic_dev
->mgmt_lock
);
482 flags
= nic_dev
->flags
;
483 nic_dev
->flags
&= ~HINIC_INTF_UP
;
485 netif_carrier_off(netdev
);
486 netif_tx_disable(netdev
);
488 update_nic_stats(nic_dev
);
490 up(&nic_dev
->mgmt_lock
);
492 err
= hinic_port_set_func_state(nic_dev
, HINIC_FUNC_PORT_DISABLE
);
494 netif_err(nic_dev
, drv
, netdev
,
495 "Failed to set func port state\n");
496 nic_dev
->flags
|= (flags
& HINIC_INTF_UP
);
500 err
= hinic_port_set_state(nic_dev
, HINIC_PORT_DISABLE
);
502 netif_err(nic_dev
, drv
, netdev
, "Failed to set port state\n");
503 nic_dev
->flags
|= (flags
& HINIC_INTF_UP
);
510 if (flags
& HINIC_INTF_UP
)
511 hinic_hwdev_ifdown(nic_dev
->hwdev
);
513 netif_info(nic_dev
, drv
, netdev
, "HINIC_INTF is DOWN\n");
517 static int hinic_change_mtu(struct net_device
*netdev
, int new_mtu
)
519 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
522 netif_info(nic_dev
, drv
, netdev
, "set_mtu = %d\n", new_mtu
);
524 err
= hinic_port_set_mtu(nic_dev
, new_mtu
);
526 netif_err(nic_dev
, drv
, netdev
, "Failed to set port mtu\n");
528 netdev
->mtu
= new_mtu
;
534 * change_mac_addr - change the main mac address of network device
535 * @netdev: network device
536 * @addr: mac address to set
538 * Return 0 - Success, negative - Failure
540 static int change_mac_addr(struct net_device
*netdev
, const u8
*addr
)
542 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
546 if (!is_valid_ether_addr(addr
))
547 return -EADDRNOTAVAIL
;
549 netif_info(nic_dev
, drv
, netdev
, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
550 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
552 down(&nic_dev
->mgmt_lock
);
555 err
= hinic_port_del_mac(nic_dev
, netdev
->dev_addr
, vid
);
557 netif_err(nic_dev
, drv
, netdev
,
558 "Failed to delete mac\n");
562 err
= hinic_port_add_mac(nic_dev
, addr
, vid
);
564 netif_err(nic_dev
, drv
, netdev
, "Failed to add mac\n");
568 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
569 } while (vid
!= VLAN_N_VID
);
571 up(&nic_dev
->mgmt_lock
);
575 static int hinic_set_mac_addr(struct net_device
*netdev
, void *addr
)
577 unsigned char new_mac
[ETH_ALEN
];
578 struct sockaddr
*saddr
= addr
;
581 memcpy(new_mac
, saddr
->sa_data
, ETH_ALEN
);
583 err
= change_mac_addr(netdev
, new_mac
);
585 memcpy(netdev
->dev_addr
, new_mac
, ETH_ALEN
);
591 * add_mac_addr - add mac address to network device
592 * @netdev: network device
593 * @addr: mac address to add
595 * Return 0 - Success, negative - Failure
597 static int add_mac_addr(struct net_device
*netdev
, const u8
*addr
)
599 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
603 if (!is_valid_ether_addr(addr
))
604 return -EADDRNOTAVAIL
;
606 netif_info(nic_dev
, drv
, netdev
, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
607 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
609 down(&nic_dev
->mgmt_lock
);
612 err
= hinic_port_add_mac(nic_dev
, addr
, vid
);
614 netif_err(nic_dev
, drv
, netdev
, "Failed to add mac\n");
618 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
619 } while (vid
!= VLAN_N_VID
);
621 up(&nic_dev
->mgmt_lock
);
626 * remove_mac_addr - remove mac address from network device
627 * @netdev: network device
628 * @addr: mac address to remove
630 * Return 0 - Success, negative - Failure
632 static int remove_mac_addr(struct net_device
*netdev
, const u8
*addr
)
634 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
638 if (!is_valid_ether_addr(addr
))
639 return -EADDRNOTAVAIL
;
641 netif_info(nic_dev
, drv
, netdev
, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
642 addr
[0], addr
[1], addr
[2], addr
[3], addr
[4], addr
[5]);
644 down(&nic_dev
->mgmt_lock
);
647 err
= hinic_port_del_mac(nic_dev
, addr
, vid
);
649 netif_err(nic_dev
, drv
, netdev
,
650 "Failed to delete mac\n");
654 vid
= find_next_bit(nic_dev
->vlan_bitmap
, VLAN_N_VID
, vid
+ 1);
655 } while (vid
!= VLAN_N_VID
);
657 up(&nic_dev
->mgmt_lock
);
661 static int hinic_vlan_rx_add_vid(struct net_device
*netdev
,
662 __always_unused __be16 proto
, u16 vid
)
664 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
667 netif_info(nic_dev
, drv
, netdev
, "add vid = %d\n", vid
);
669 down(&nic_dev
->mgmt_lock
);
671 err
= hinic_port_add_vlan(nic_dev
, vid
);
673 netif_err(nic_dev
, drv
, netdev
, "Failed to add vlan\n");
677 err
= hinic_port_add_mac(nic_dev
, netdev
->dev_addr
, vid
);
679 netif_err(nic_dev
, drv
, netdev
, "Failed to set mac\n");
683 bitmap_set(nic_dev
->vlan_bitmap
, vid
, 1);
685 up(&nic_dev
->mgmt_lock
);
689 ret
= hinic_port_del_vlan(nic_dev
, vid
);
691 netif_err(nic_dev
, drv
, netdev
,
692 "Failed to revert by removing vlan\n");
695 up(&nic_dev
->mgmt_lock
);
699 static int hinic_vlan_rx_kill_vid(struct net_device
*netdev
,
700 __always_unused __be16 proto
, u16 vid
)
702 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
705 netif_info(nic_dev
, drv
, netdev
, "remove vid = %d\n", vid
);
707 down(&nic_dev
->mgmt_lock
);
709 err
= hinic_port_del_vlan(nic_dev
, vid
);
711 netif_err(nic_dev
, drv
, netdev
, "Failed to delete vlan\n");
715 bitmap_clear(nic_dev
->vlan_bitmap
, vid
, 1);
717 up(&nic_dev
->mgmt_lock
);
721 up(&nic_dev
->mgmt_lock
);
725 static void set_rx_mode(struct work_struct
*work
)
727 struct hinic_rx_mode_work
*rx_mode_work
= work_to_rx_mode_work(work
);
728 struct hinic_dev
*nic_dev
= rx_mode_work_to_nic_dev(rx_mode_work
);
730 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "set rx mode work\n");
732 hinic_port_set_rx_mode(nic_dev
, rx_mode_work
->rx_mode
);
734 __dev_uc_sync(nic_dev
->netdev
, add_mac_addr
, remove_mac_addr
);
735 __dev_mc_sync(nic_dev
->netdev
, add_mac_addr
, remove_mac_addr
);
738 static void hinic_set_rx_mode(struct net_device
*netdev
)
740 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
741 struct hinic_rx_mode_work
*rx_mode_work
;
744 rx_mode_work
= &nic_dev
->rx_mode_work
;
746 rx_mode
= HINIC_RX_MODE_UC
|
750 if (netdev
->flags
& IFF_PROMISC
)
751 rx_mode
|= HINIC_RX_MODE_PROMISC
;
752 else if (netdev
->flags
& IFF_ALLMULTI
)
753 rx_mode
|= HINIC_RX_MODE_MC_ALL
;
755 rx_mode_work
->rx_mode
= rx_mode
;
757 queue_work(nic_dev
->workq
, &rx_mode_work
->work
);
760 static void hinic_tx_timeout(struct net_device
*netdev
)
762 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
764 netif_err(nic_dev
, drv
, netdev
, "Tx timeout\n");
767 static void hinic_get_stats64(struct net_device
*netdev
,
768 struct rtnl_link_stats64
*stats
)
770 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
771 struct hinic_rxq_stats
*nic_rx_stats
;
772 struct hinic_txq_stats
*nic_tx_stats
;
774 nic_rx_stats
= &nic_dev
->rx_stats
;
775 nic_tx_stats
= &nic_dev
->tx_stats
;
777 down(&nic_dev
->mgmt_lock
);
779 if (nic_dev
->flags
& HINIC_INTF_UP
)
780 update_nic_stats(nic_dev
);
782 up(&nic_dev
->mgmt_lock
);
784 stats
->rx_bytes
= nic_rx_stats
->bytes
;
785 stats
->rx_packets
= nic_rx_stats
->pkts
;
787 stats
->tx_bytes
= nic_tx_stats
->bytes
;
788 stats
->tx_packets
= nic_tx_stats
->pkts
;
789 stats
->tx_errors
= nic_tx_stats
->tx_dropped
;
792 static const struct net_device_ops hinic_netdev_ops
= {
793 .ndo_open
= hinic_open
,
794 .ndo_stop
= hinic_close
,
795 .ndo_change_mtu
= hinic_change_mtu
,
796 .ndo_set_mac_address
= hinic_set_mac_addr
,
797 .ndo_validate_addr
= eth_validate_addr
,
798 .ndo_vlan_rx_add_vid
= hinic_vlan_rx_add_vid
,
799 .ndo_vlan_rx_kill_vid
= hinic_vlan_rx_kill_vid
,
800 .ndo_set_rx_mode
= hinic_set_rx_mode
,
801 .ndo_start_xmit
= hinic_xmit_frame
,
802 .ndo_tx_timeout
= hinic_tx_timeout
,
803 .ndo_get_stats64
= hinic_get_stats64
,
806 static void netdev_features_init(struct net_device
*netdev
)
808 netdev
->hw_features
= NETIF_F_SG
| NETIF_F_HIGHDMA
;
810 netdev
->vlan_features
= netdev
->hw_features
;
812 netdev
->features
= netdev
->hw_features
| NETIF_F_HW_VLAN_CTAG_FILTER
;
816 * link_status_event_handler - link event handler
817 * @handle: nic device for the handler
818 * @buf_in: input buffer
819 * @in_size: input size
820 * @buf_in: output buffer
821 * @out_size: returned output size
823 * Return 0 - Success, negative - Failure
825 static void link_status_event_handler(void *handle
, void *buf_in
, u16 in_size
,
826 void *buf_out
, u16
*out_size
)
828 struct hinic_port_link_status
*link_status
, *ret_link_status
;
829 struct hinic_dev
*nic_dev
= handle
;
831 link_status
= buf_in
;
833 if (link_status
->link
== HINIC_LINK_STATE_UP
) {
834 down(&nic_dev
->mgmt_lock
);
836 nic_dev
->flags
|= HINIC_LINK_UP
;
838 if ((nic_dev
->flags
& (HINIC_LINK_UP
| HINIC_INTF_UP
)) ==
839 (HINIC_LINK_UP
| HINIC_INTF_UP
)) {
840 netif_carrier_on(nic_dev
->netdev
);
841 netif_tx_wake_all_queues(nic_dev
->netdev
);
844 up(&nic_dev
->mgmt_lock
);
846 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "HINIC_Link is UP\n");
848 down(&nic_dev
->mgmt_lock
);
850 nic_dev
->flags
&= ~HINIC_LINK_UP
;
852 netif_carrier_off(nic_dev
->netdev
);
853 netif_tx_disable(nic_dev
->netdev
);
855 up(&nic_dev
->mgmt_lock
);
857 netif_info(nic_dev
, drv
, nic_dev
->netdev
, "HINIC_Link is DOWN\n");
860 ret_link_status
= buf_out
;
861 ret_link_status
->status
= 0;
863 *out_size
= sizeof(*ret_link_status
);
867 * nic_dev_init - Initialize the NIC device
868 * @pdev: the NIC pci device
870 * Return 0 - Success, negative - Failure
872 static int nic_dev_init(struct pci_dev
*pdev
)
874 struct hinic_rx_mode_work
*rx_mode_work
;
875 struct hinic_txq_stats
*tx_stats
;
876 struct hinic_rxq_stats
*rx_stats
;
877 struct hinic_dev
*nic_dev
;
878 struct net_device
*netdev
;
879 struct hinic_hwdev
*hwdev
;
882 hwdev
= hinic_init_hwdev(pdev
);
884 dev_err(&pdev
->dev
, "Failed to initialize HW device\n");
885 return PTR_ERR(hwdev
);
888 num_qps
= hinic_hwdev_num_qps(hwdev
);
890 dev_err(&pdev
->dev
, "Invalid number of QPS\n");
895 netdev
= alloc_etherdev_mq(sizeof(*nic_dev
), num_qps
);
897 dev_err(&pdev
->dev
, "Failed to allocate Ethernet device\n");
899 goto err_alloc_etherdev
;
902 netdev
->netdev_ops
= &hinic_netdev_ops
;
903 netdev
->ethtool_ops
= &hinic_ethtool_ops
;
904 netdev
->max_mtu
= ETH_MAX_MTU
;
906 nic_dev
= netdev_priv(netdev
);
907 nic_dev
->netdev
= netdev
;
908 nic_dev
->hwdev
= hwdev
;
909 nic_dev
->msg_enable
= MSG_ENABLE_DEFAULT
;
911 nic_dev
->txqs
= NULL
;
912 nic_dev
->rxqs
= NULL
;
913 nic_dev
->tx_weight
= tx_weight
;
914 nic_dev
->rx_weight
= rx_weight
;
916 sema_init(&nic_dev
->mgmt_lock
, 1);
918 tx_stats
= &nic_dev
->tx_stats
;
919 rx_stats
= &nic_dev
->rx_stats
;
921 u64_stats_init(&tx_stats
->syncp
);
922 u64_stats_init(&rx_stats
->syncp
);
924 nic_dev
->vlan_bitmap
= devm_kzalloc(&pdev
->dev
,
925 VLAN_BITMAP_SIZE(nic_dev
),
927 if (!nic_dev
->vlan_bitmap
) {
929 goto err_vlan_bitmap
;
932 nic_dev
->workq
= create_singlethread_workqueue(HINIC_WQ_NAME
);
933 if (!nic_dev
->workq
) {
938 pci_set_drvdata(pdev
, netdev
);
940 err
= hinic_port_get_mac(nic_dev
, netdev
->dev_addr
);
942 dev_warn(&pdev
->dev
, "Failed to get mac address\n");
944 err
= hinic_port_add_mac(nic_dev
, netdev
->dev_addr
, 0);
946 dev_err(&pdev
->dev
, "Failed to add mac\n");
950 err
= hinic_port_set_mtu(nic_dev
, netdev
->mtu
);
952 dev_err(&pdev
->dev
, "Failed to set mtu\n");
956 rx_mode_work
= &nic_dev
->rx_mode_work
;
957 INIT_WORK(&rx_mode_work
->work
, set_rx_mode
);
959 netdev_features_init(netdev
);
961 netif_carrier_off(netdev
);
963 hinic_hwdev_cb_register(nic_dev
->hwdev
, HINIC_MGMT_MSG_CMD_LINK_STATUS
,
964 nic_dev
, link_status_event_handler
);
966 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
967 err
= register_netdev(netdev
);
969 dev_err(&pdev
->dev
, "Failed to register netdev\n");
976 hinic_hwdev_cb_unregister(nic_dev
->hwdev
,
977 HINIC_MGMT_MSG_CMD_LINK_STATUS
);
978 cancel_work_sync(&rx_mode_work
->work
);
982 pci_set_drvdata(pdev
, NULL
);
983 destroy_workqueue(nic_dev
->workq
);
991 hinic_free_hwdev(hwdev
);
995 static int hinic_probe(struct pci_dev
*pdev
,
996 const struct pci_device_id
*id
)
998 int err
= pci_enable_device(pdev
);
1001 dev_err(&pdev
->dev
, "Failed to enable PCI device\n");
1005 err
= pci_request_regions(pdev
, HINIC_DRV_NAME
);
1007 dev_err(&pdev
->dev
, "Failed to request PCI regions\n");
1008 goto err_pci_regions
;
1011 pci_set_master(pdev
);
1013 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(64));
1015 dev_warn(&pdev
->dev
, "Couldn't set 64-bit DMA mask\n");
1016 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
1018 dev_err(&pdev
->dev
, "Failed to set DMA mask\n");
1023 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
1025 dev_warn(&pdev
->dev
,
1026 "Couldn't set 64-bit consistent DMA mask\n");
1027 err
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
1030 "Failed to set consistent DMA mask\n");
1031 goto err_dma_consistent_mask
;
1035 err
= nic_dev_init(pdev
);
1037 dev_err(&pdev
->dev
, "Failed to initialize NIC device\n");
1038 goto err_nic_dev_init
;
1041 dev_info(&pdev
->dev
, "HiNIC driver - probed\n");
1045 err_dma_consistent_mask
:
1047 pci_release_regions(pdev
);
1050 pci_disable_device(pdev
);
1054 static void hinic_remove(struct pci_dev
*pdev
)
1056 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1057 struct hinic_dev
*nic_dev
= netdev_priv(netdev
);
1058 struct hinic_rx_mode_work
*rx_mode_work
;
1060 unregister_netdev(netdev
);
1062 hinic_hwdev_cb_unregister(nic_dev
->hwdev
,
1063 HINIC_MGMT_MSG_CMD_LINK_STATUS
);
1065 rx_mode_work
= &nic_dev
->rx_mode_work
;
1066 cancel_work_sync(&rx_mode_work
->work
);
1068 pci_set_drvdata(pdev
, NULL
);
1070 destroy_workqueue(nic_dev
->workq
);
1072 hinic_free_hwdev(nic_dev
->hwdev
);
1074 free_netdev(netdev
);
1076 pci_release_regions(pdev
);
1077 pci_disable_device(pdev
);
1079 dev_info(&pdev
->dev
, "HiNIC driver - removed\n");
1082 static const struct pci_device_id hinic_pci_table
[] = {
1083 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_QUAD_PORT_25GE
), 0},
1084 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_DUAL_PORT_25GE
), 0},
1085 { PCI_VDEVICE(HUAWEI
, HINIC_DEV_ID_DUAL_PORT_100GE
), 0},
1088 MODULE_DEVICE_TABLE(pci
, hinic_pci_table
);
1090 static struct pci_driver hinic_driver
= {
1091 .name
= HINIC_DRV_NAME
,
1092 .id_table
= hinic_pci_table
,
1093 .probe
= hinic_probe
,
1094 .remove
= hinic_remove
,
1097 module_pci_driver(hinic_driver
);