]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/ethernet/huawei/hinic/hinic_main.c
hinic: remove ndo_poll_controller
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ethernet / huawei / hinic / hinic_main.c
1 /*
2 * Huawei HiNIC PCI Express Linux driver
3 * Copyright(c) 2017 Huawei Technologies Co., Ltd
4 *
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.
8 *
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
12 * for more details.
13 *
14 */
15
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>
29 #include <net/ip.h>
30 #include <linux/bitops.h>
31 #include <linux/bitmap.h>
32 #include <linux/delay.h>
33 #include <linux/err.h>
34
35 #include "hinic_hw_qp.h"
36 #include "hinic_hw_dev.h"
37 #include "hinic_port.h"
38 #include "hinic_tx.h"
39 #include "hinic_rx.h"
40 #include "hinic_dev.h"
41
42 MODULE_AUTHOR("Huawei Technologies CO., Ltd");
43 MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
44 MODULE_LICENSE("GPL");
45
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)");
49
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)");
53
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
57
58 #define HINIC_WQ_NAME "hinic_dev"
59
60 #define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
61 NETIF_MSG_IFUP | \
62 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
63
64 #define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
65
66 #define work_to_rx_mode_work(work) \
67 container_of(work, struct hinic_rx_mode_work, work)
68
69 #define rx_mode_work_to_nic_dev(rx_mode_work) \
70 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
71
72 static int change_mac_addr(struct net_device *netdev, const u8 *addr);
73
74 static void set_link_speed(struct ethtool_link_ksettings *link_ksettings,
75 enum hinic_speed speed)
76 {
77 switch (speed) {
78 case HINIC_SPEED_10MB_LINK:
79 link_ksettings->base.speed = SPEED_10;
80 break;
81
82 case HINIC_SPEED_100MB_LINK:
83 link_ksettings->base.speed = SPEED_100;
84 break;
85
86 case HINIC_SPEED_1000MB_LINK:
87 link_ksettings->base.speed = SPEED_1000;
88 break;
89
90 case HINIC_SPEED_10GB_LINK:
91 link_ksettings->base.speed = SPEED_10000;
92 break;
93
94 case HINIC_SPEED_25GB_LINK:
95 link_ksettings->base.speed = SPEED_25000;
96 break;
97
98 case HINIC_SPEED_40GB_LINK:
99 link_ksettings->base.speed = SPEED_40000;
100 break;
101
102 case HINIC_SPEED_100GB_LINK:
103 link_ksettings->base.speed = SPEED_100000;
104 break;
105
106 default:
107 link_ksettings->base.speed = SPEED_UNKNOWN;
108 break;
109 }
110 }
111
112 static int hinic_get_link_ksettings(struct net_device *netdev,
113 struct ethtool_link_ksettings
114 *link_ksettings)
115 {
116 struct hinic_dev *nic_dev = netdev_priv(netdev);
117 enum hinic_port_link_state link_state;
118 struct hinic_port_cap port_cap;
119 int err;
120
121 ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
122 ethtool_link_ksettings_add_link_mode(link_ksettings, supported,
123 Autoneg);
124
125 link_ksettings->base.speed = SPEED_UNKNOWN;
126 link_ksettings->base.autoneg = AUTONEG_DISABLE;
127 link_ksettings->base.duplex = DUPLEX_UNKNOWN;
128
129 err = hinic_port_get_cap(nic_dev, &port_cap);
130 if (err) {
131 netif_err(nic_dev, drv, netdev,
132 "Failed to get port capabilities\n");
133 return err;
134 }
135
136 err = hinic_port_link_state(nic_dev, &link_state);
137 if (err) {
138 netif_err(nic_dev, drv, netdev,
139 "Failed to get port link state\n");
140 return err;
141 }
142
143 if (link_state != HINIC_LINK_STATE_UP) {
144 netif_info(nic_dev, drv, netdev, "No link\n");
145 return err;
146 }
147
148 set_link_speed(link_ksettings, port_cap.speed);
149
150 if (!!(port_cap.autoneg_cap & HINIC_AUTONEG_SUPPORTED))
151 ethtool_link_ksettings_add_link_mode(link_ksettings,
152 advertising, Autoneg);
153
154 if (port_cap.autoneg_state == HINIC_AUTONEG_ACTIVE)
155 link_ksettings->base.autoneg = AUTONEG_ENABLE;
156
157 link_ksettings->base.duplex = (port_cap.duplex == HINIC_DUPLEX_FULL) ?
158 DUPLEX_FULL : DUPLEX_HALF;
159 return 0;
160 }
161
162 static void hinic_get_drvinfo(struct net_device *netdev,
163 struct ethtool_drvinfo *info)
164 {
165 struct hinic_dev *nic_dev = netdev_priv(netdev);
166 struct hinic_hwdev *hwdev = nic_dev->hwdev;
167 struct hinic_hwif *hwif = hwdev->hwif;
168
169 strlcpy(info->driver, HINIC_DRV_NAME, sizeof(info->driver));
170 strlcpy(info->bus_info, pci_name(hwif->pdev), sizeof(info->bus_info));
171 }
172
173 static void hinic_get_ringparam(struct net_device *netdev,
174 struct ethtool_ringparam *ring)
175 {
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;
180 }
181
182 static void hinic_get_channels(struct net_device *netdev,
183 struct ethtool_channels *channels)
184 {
185 struct hinic_dev *nic_dev = netdev_priv(netdev);
186 struct hinic_hwdev *hwdev = nic_dev->hwdev;
187
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;
196 }
197
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,
204 };
205
206 static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
207 {
208 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
209 struct hinic_rxq_stats rx_stats;
210
211 u64_stats_init(&rx_stats.syncp);
212
213 hinic_rxq_get_stats(rxq, &rx_stats);
214
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);
219
220 hinic_rxq_clean_stats(rxq);
221 }
222
223 static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
224 {
225 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
226 struct hinic_txq_stats tx_stats;
227
228 u64_stats_init(&tx_stats.syncp);
229
230 hinic_txq_get_stats(txq, &tx_stats);
231
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);
239
240 hinic_txq_clean_stats(txq);
241 }
242
243 static void update_nic_stats(struct hinic_dev *nic_dev)
244 {
245 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
246
247 for (i = 0; i < num_qps; i++)
248 update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
249
250 for (i = 0; i < num_qps; i++)
251 update_tx_stats(nic_dev, &nic_dev->txqs[i]);
252 }
253
254 /**
255 * create_txqs - Create the Logical Tx Queues of specific NIC device
256 * @nic_dev: the specific NIC device
257 *
258 * Return 0 - Success, negative - Failure
259 **/
260 static int create_txqs(struct hinic_dev *nic_dev)
261 {
262 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
263 struct net_device *netdev = nic_dev->netdev;
264 size_t txq_size;
265
266 if (nic_dev->txqs)
267 return -EINVAL;
268
269 txq_size = num_txqs * sizeof(*nic_dev->txqs);
270 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
271 if (!nic_dev->txqs)
272 return -ENOMEM;
273
274 for (i = 0; i < num_txqs; i++) {
275 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
276
277 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
278 if (err) {
279 netif_err(nic_dev, drv, netdev,
280 "Failed to init Txq\n");
281 goto err_init_txq;
282 }
283 }
284
285 return 0;
286
287 err_init_txq:
288 for (j = 0; j < i; j++)
289 hinic_clean_txq(&nic_dev->txqs[j]);
290
291 devm_kfree(&netdev->dev, nic_dev->txqs);
292 return err;
293 }
294
295 /**
296 * free_txqs - Free the Logical Tx Queues of specific NIC device
297 * @nic_dev: the specific NIC device
298 **/
299 static void free_txqs(struct hinic_dev *nic_dev)
300 {
301 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
302 struct net_device *netdev = nic_dev->netdev;
303
304 if (!nic_dev->txqs)
305 return;
306
307 for (i = 0; i < num_txqs; i++)
308 hinic_clean_txq(&nic_dev->txqs[i]);
309
310 devm_kfree(&netdev->dev, nic_dev->txqs);
311 nic_dev->txqs = NULL;
312 }
313
314 /**
315 * create_txqs - Create the Logical Rx Queues of specific NIC device
316 * @nic_dev: the specific NIC device
317 *
318 * Return 0 - Success, negative - Failure
319 **/
320 static int create_rxqs(struct hinic_dev *nic_dev)
321 {
322 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
323 struct net_device *netdev = nic_dev->netdev;
324 size_t rxq_size;
325
326 if (nic_dev->rxqs)
327 return -EINVAL;
328
329 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
330 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
331 if (!nic_dev->rxqs)
332 return -ENOMEM;
333
334 for (i = 0; i < num_rxqs; i++) {
335 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
336
337 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
338 if (err) {
339 netif_err(nic_dev, drv, netdev,
340 "Failed to init rxq\n");
341 goto err_init_rxq;
342 }
343 }
344
345 return 0;
346
347 err_init_rxq:
348 for (j = 0; j < i; j++)
349 hinic_clean_rxq(&nic_dev->rxqs[j]);
350
351 devm_kfree(&netdev->dev, nic_dev->rxqs);
352 return err;
353 }
354
355 /**
356 * free_txqs - Free the Logical Rx Queues of specific NIC device
357 * @nic_dev: the specific NIC device
358 **/
359 static void free_rxqs(struct hinic_dev *nic_dev)
360 {
361 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
362 struct net_device *netdev = nic_dev->netdev;
363
364 if (!nic_dev->rxqs)
365 return;
366
367 for (i = 0; i < num_rxqs; i++)
368 hinic_clean_rxq(&nic_dev->rxqs[i]);
369
370 devm_kfree(&netdev->dev, nic_dev->rxqs);
371 nic_dev->rxqs = NULL;
372 }
373
374 static int hinic_open(struct net_device *netdev)
375 {
376 struct hinic_dev *nic_dev = netdev_priv(netdev);
377 enum hinic_port_link_state link_state;
378 int err, ret, num_qps;
379
380 if (!(nic_dev->flags & HINIC_INTF_UP)) {
381 err = hinic_hwdev_ifup(nic_dev->hwdev);
382 if (err) {
383 netif_err(nic_dev, drv, netdev,
384 "Failed - HW interface up\n");
385 return err;
386 }
387 }
388
389 err = create_txqs(nic_dev);
390 if (err) {
391 netif_err(nic_dev, drv, netdev,
392 "Failed to create Tx queues\n");
393 goto err_create_txqs;
394 }
395
396 err = create_rxqs(nic_dev);
397 if (err) {
398 netif_err(nic_dev, drv, netdev,
399 "Failed to create Rx queues\n");
400 goto err_create_rxqs;
401 }
402
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);
406
407 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
408 if (err) {
409 netif_err(nic_dev, drv, netdev,
410 "Failed to set port state\n");
411 goto err_port_state;
412 }
413
414 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
415 if (err) {
416 netif_err(nic_dev, drv, netdev,
417 "Failed to set func port state\n");
418 goto err_func_port_state;
419 }
420
421 /* Wait up to 3 sec between port enable to link state */
422 msleep(3000);
423
424 down(&nic_dev->mgmt_lock);
425
426 err = hinic_port_link_state(nic_dev, &link_state);
427 if (err) {
428 netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
429 goto err_port_link;
430 }
431
432 if (link_state == HINIC_LINK_STATE_UP)
433 nic_dev->flags |= HINIC_LINK_UP;
434
435 nic_dev->flags |= HINIC_INTF_UP;
436
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);
442 }
443
444 up(&nic_dev->mgmt_lock);
445
446 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
447 return 0;
448
449 err_port_link:
450 up(&nic_dev->mgmt_lock);
451 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
452 if (ret)
453 netif_warn(nic_dev, drv, netdev,
454 "Failed to revert func port state\n");
455
456 err_func_port_state:
457 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
458 if (ret)
459 netif_warn(nic_dev, drv, netdev,
460 "Failed to revert port state\n");
461
462 err_port_state:
463 free_rxqs(nic_dev);
464
465 err_create_rxqs:
466 free_txqs(nic_dev);
467
468 err_create_txqs:
469 if (!(nic_dev->flags & HINIC_INTF_UP))
470 hinic_hwdev_ifdown(nic_dev->hwdev);
471 return err;
472 }
473
474 static int hinic_close(struct net_device *netdev)
475 {
476 struct hinic_dev *nic_dev = netdev_priv(netdev);
477 unsigned int flags;
478 int err;
479
480 down(&nic_dev->mgmt_lock);
481
482 flags = nic_dev->flags;
483 nic_dev->flags &= ~HINIC_INTF_UP;
484
485 netif_carrier_off(netdev);
486 netif_tx_disable(netdev);
487
488 update_nic_stats(nic_dev);
489
490 up(&nic_dev->mgmt_lock);
491
492 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
493 if (err) {
494 netif_err(nic_dev, drv, netdev,
495 "Failed to set func port state\n");
496 nic_dev->flags |= (flags & HINIC_INTF_UP);
497 return err;
498 }
499
500 err = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
501 if (err) {
502 netif_err(nic_dev, drv, netdev, "Failed to set port state\n");
503 nic_dev->flags |= (flags & HINIC_INTF_UP);
504 return err;
505 }
506
507 free_rxqs(nic_dev);
508 free_txqs(nic_dev);
509
510 if (flags & HINIC_INTF_UP)
511 hinic_hwdev_ifdown(nic_dev->hwdev);
512
513 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
514 return 0;
515 }
516
517 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
518 {
519 struct hinic_dev *nic_dev = netdev_priv(netdev);
520 int err;
521
522 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
523
524 err = hinic_port_set_mtu(nic_dev, new_mtu);
525 if (err)
526 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
527 else
528 netdev->mtu = new_mtu;
529
530 return err;
531 }
532
533 /**
534 * change_mac_addr - change the main mac address of network device
535 * @netdev: network device
536 * @addr: mac address to set
537 *
538 * Return 0 - Success, negative - Failure
539 **/
540 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
541 {
542 struct hinic_dev *nic_dev = netdev_priv(netdev);
543 u16 vid = 0;
544 int err;
545
546 if (!is_valid_ether_addr(addr))
547 return -EADDRNOTAVAIL;
548
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]);
551
552 down(&nic_dev->mgmt_lock);
553
554 do {
555 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
556 if (err) {
557 netif_err(nic_dev, drv, netdev,
558 "Failed to delete mac\n");
559 break;
560 }
561
562 err = hinic_port_add_mac(nic_dev, addr, vid);
563 if (err) {
564 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
565 break;
566 }
567
568 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
569 } while (vid != VLAN_N_VID);
570
571 up(&nic_dev->mgmt_lock);
572 return err;
573 }
574
575 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
576 {
577 unsigned char new_mac[ETH_ALEN];
578 struct sockaddr *saddr = addr;
579 int err;
580
581 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
582
583 err = change_mac_addr(netdev, new_mac);
584 if (!err)
585 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
586
587 return err;
588 }
589
590 /**
591 * add_mac_addr - add mac address to network device
592 * @netdev: network device
593 * @addr: mac address to add
594 *
595 * Return 0 - Success, negative - Failure
596 **/
597 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
598 {
599 struct hinic_dev *nic_dev = netdev_priv(netdev);
600 u16 vid = 0;
601 int err;
602
603 if (!is_valid_ether_addr(addr))
604 return -EADDRNOTAVAIL;
605
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]);
608
609 down(&nic_dev->mgmt_lock);
610
611 do {
612 err = hinic_port_add_mac(nic_dev, addr, vid);
613 if (err) {
614 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
615 break;
616 }
617
618 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
619 } while (vid != VLAN_N_VID);
620
621 up(&nic_dev->mgmt_lock);
622 return err;
623 }
624
625 /**
626 * remove_mac_addr - remove mac address from network device
627 * @netdev: network device
628 * @addr: mac address to remove
629 *
630 * Return 0 - Success, negative - Failure
631 **/
632 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
633 {
634 struct hinic_dev *nic_dev = netdev_priv(netdev);
635 u16 vid = 0;
636 int err;
637
638 if (!is_valid_ether_addr(addr))
639 return -EADDRNOTAVAIL;
640
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]);
643
644 down(&nic_dev->mgmt_lock);
645
646 do {
647 err = hinic_port_del_mac(nic_dev, addr, vid);
648 if (err) {
649 netif_err(nic_dev, drv, netdev,
650 "Failed to delete mac\n");
651 break;
652 }
653
654 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
655 } while (vid != VLAN_N_VID);
656
657 up(&nic_dev->mgmt_lock);
658 return err;
659 }
660
661 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
662 __always_unused __be16 proto, u16 vid)
663 {
664 struct hinic_dev *nic_dev = netdev_priv(netdev);
665 int ret, err;
666
667 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
668
669 down(&nic_dev->mgmt_lock);
670
671 err = hinic_port_add_vlan(nic_dev, vid);
672 if (err) {
673 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
674 goto err_vlan_add;
675 }
676
677 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
678 if (err) {
679 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
680 goto err_add_mac;
681 }
682
683 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
684
685 up(&nic_dev->mgmt_lock);
686 return 0;
687
688 err_add_mac:
689 ret = hinic_port_del_vlan(nic_dev, vid);
690 if (ret)
691 netif_err(nic_dev, drv, netdev,
692 "Failed to revert by removing vlan\n");
693
694 err_vlan_add:
695 up(&nic_dev->mgmt_lock);
696 return err;
697 }
698
699 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
700 __always_unused __be16 proto, u16 vid)
701 {
702 struct hinic_dev *nic_dev = netdev_priv(netdev);
703 int err;
704
705 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
706
707 down(&nic_dev->mgmt_lock);
708
709 err = hinic_port_del_vlan(nic_dev, vid);
710 if (err) {
711 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
712 goto err_del_vlan;
713 }
714
715 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
716
717 up(&nic_dev->mgmt_lock);
718 return 0;
719
720 err_del_vlan:
721 up(&nic_dev->mgmt_lock);
722 return err;
723 }
724
725 static void set_rx_mode(struct work_struct *work)
726 {
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);
729
730 netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
731
732 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
733
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);
736 }
737
738 static void hinic_set_rx_mode(struct net_device *netdev)
739 {
740 struct hinic_dev *nic_dev = netdev_priv(netdev);
741 struct hinic_rx_mode_work *rx_mode_work;
742 u32 rx_mode;
743
744 rx_mode_work = &nic_dev->rx_mode_work;
745
746 rx_mode = HINIC_RX_MODE_UC |
747 HINIC_RX_MODE_MC |
748 HINIC_RX_MODE_BC;
749
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;
754
755 rx_mode_work->rx_mode = rx_mode;
756
757 queue_work(nic_dev->workq, &rx_mode_work->work);
758 }
759
760 static void hinic_tx_timeout(struct net_device *netdev)
761 {
762 struct hinic_dev *nic_dev = netdev_priv(netdev);
763
764 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
765 }
766
767 static void hinic_get_stats64(struct net_device *netdev,
768 struct rtnl_link_stats64 *stats)
769 {
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;
773
774 nic_rx_stats = &nic_dev->rx_stats;
775 nic_tx_stats = &nic_dev->tx_stats;
776
777 down(&nic_dev->mgmt_lock);
778
779 if (nic_dev->flags & HINIC_INTF_UP)
780 update_nic_stats(nic_dev);
781
782 up(&nic_dev->mgmt_lock);
783
784 stats->rx_bytes = nic_rx_stats->bytes;
785 stats->rx_packets = nic_rx_stats->pkts;
786
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;
790 }
791
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,
804 };
805
806 static void netdev_features_init(struct net_device *netdev)
807 {
808 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
809
810 netdev->vlan_features = netdev->hw_features;
811
812 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
813 }
814
815 /**
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
822 *
823 * Return 0 - Success, negative - Failure
824 **/
825 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
826 void *buf_out, u16 *out_size)
827 {
828 struct hinic_port_link_status *link_status, *ret_link_status;
829 struct hinic_dev *nic_dev = handle;
830
831 link_status = buf_in;
832
833 if (link_status->link == HINIC_LINK_STATE_UP) {
834 down(&nic_dev->mgmt_lock);
835
836 nic_dev->flags |= HINIC_LINK_UP;
837
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);
842 }
843
844 up(&nic_dev->mgmt_lock);
845
846 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
847 } else {
848 down(&nic_dev->mgmt_lock);
849
850 nic_dev->flags &= ~HINIC_LINK_UP;
851
852 netif_carrier_off(nic_dev->netdev);
853 netif_tx_disable(nic_dev->netdev);
854
855 up(&nic_dev->mgmt_lock);
856
857 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
858 }
859
860 ret_link_status = buf_out;
861 ret_link_status->status = 0;
862
863 *out_size = sizeof(*ret_link_status);
864 }
865
866 /**
867 * nic_dev_init - Initialize the NIC device
868 * @pdev: the NIC pci device
869 *
870 * Return 0 - Success, negative - Failure
871 **/
872 static int nic_dev_init(struct pci_dev *pdev)
873 {
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;
880 int err, num_qps;
881
882 hwdev = hinic_init_hwdev(pdev);
883 if (IS_ERR(hwdev)) {
884 dev_err(&pdev->dev, "Failed to initialize HW device\n");
885 return PTR_ERR(hwdev);
886 }
887
888 num_qps = hinic_hwdev_num_qps(hwdev);
889 if (num_qps <= 0) {
890 dev_err(&pdev->dev, "Invalid number of QPS\n");
891 err = -EINVAL;
892 goto err_num_qps;
893 }
894
895 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
896 if (!netdev) {
897 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
898 err = -ENOMEM;
899 goto err_alloc_etherdev;
900 }
901
902 netdev->netdev_ops = &hinic_netdev_ops;
903 netdev->ethtool_ops = &hinic_ethtool_ops;
904 netdev->max_mtu = ETH_MAX_MTU;
905
906 nic_dev = netdev_priv(netdev);
907 nic_dev->netdev = netdev;
908 nic_dev->hwdev = hwdev;
909 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
910 nic_dev->flags = 0;
911 nic_dev->txqs = NULL;
912 nic_dev->rxqs = NULL;
913 nic_dev->tx_weight = tx_weight;
914 nic_dev->rx_weight = rx_weight;
915
916 sema_init(&nic_dev->mgmt_lock, 1);
917
918 tx_stats = &nic_dev->tx_stats;
919 rx_stats = &nic_dev->rx_stats;
920
921 u64_stats_init(&tx_stats->syncp);
922 u64_stats_init(&rx_stats->syncp);
923
924 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
925 VLAN_BITMAP_SIZE(nic_dev),
926 GFP_KERNEL);
927 if (!nic_dev->vlan_bitmap) {
928 err = -ENOMEM;
929 goto err_vlan_bitmap;
930 }
931
932 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
933 if (!nic_dev->workq) {
934 err = -ENOMEM;
935 goto err_workq;
936 }
937
938 pci_set_drvdata(pdev, netdev);
939
940 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
941 if (err)
942 dev_warn(&pdev->dev, "Failed to get mac address\n");
943
944 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
945 if (err) {
946 dev_err(&pdev->dev, "Failed to add mac\n");
947 goto err_add_mac;
948 }
949
950 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
951 if (err) {
952 dev_err(&pdev->dev, "Failed to set mtu\n");
953 goto err_set_mtu;
954 }
955
956 rx_mode_work = &nic_dev->rx_mode_work;
957 INIT_WORK(&rx_mode_work->work, set_rx_mode);
958
959 netdev_features_init(netdev);
960
961 netif_carrier_off(netdev);
962
963 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
964 nic_dev, link_status_event_handler);
965
966 SET_NETDEV_DEV(netdev, &pdev->dev);
967 err = register_netdev(netdev);
968 if (err) {
969 dev_err(&pdev->dev, "Failed to register netdev\n");
970 goto err_reg_netdev;
971 }
972
973 return 0;
974
975 err_reg_netdev:
976 hinic_hwdev_cb_unregister(nic_dev->hwdev,
977 HINIC_MGMT_MSG_CMD_LINK_STATUS);
978 cancel_work_sync(&rx_mode_work->work);
979
980 err_set_mtu:
981 err_add_mac:
982 pci_set_drvdata(pdev, NULL);
983 destroy_workqueue(nic_dev->workq);
984
985 err_workq:
986 err_vlan_bitmap:
987 free_netdev(netdev);
988
989 err_alloc_etherdev:
990 err_num_qps:
991 hinic_free_hwdev(hwdev);
992 return err;
993 }
994
995 static int hinic_probe(struct pci_dev *pdev,
996 const struct pci_device_id *id)
997 {
998 int err = pci_enable_device(pdev);
999
1000 if (err) {
1001 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1002 return err;
1003 }
1004
1005 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1006 if (err) {
1007 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1008 goto err_pci_regions;
1009 }
1010
1011 pci_set_master(pdev);
1012
1013 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1014 if (err) {
1015 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1016 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1017 if (err) {
1018 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1019 goto err_dma_mask;
1020 }
1021 }
1022
1023 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1024 if (err) {
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));
1028 if (err) {
1029 dev_err(&pdev->dev,
1030 "Failed to set consistent DMA mask\n");
1031 goto err_dma_consistent_mask;
1032 }
1033 }
1034
1035 err = nic_dev_init(pdev);
1036 if (err) {
1037 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1038 goto err_nic_dev_init;
1039 }
1040
1041 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1042 return 0;
1043
1044 err_nic_dev_init:
1045 err_dma_consistent_mask:
1046 err_dma_mask:
1047 pci_release_regions(pdev);
1048
1049 err_pci_regions:
1050 pci_disable_device(pdev);
1051 return err;
1052 }
1053
1054 static void hinic_remove(struct pci_dev *pdev)
1055 {
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;
1059
1060 unregister_netdev(netdev);
1061
1062 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1063 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1064
1065 rx_mode_work = &nic_dev->rx_mode_work;
1066 cancel_work_sync(&rx_mode_work->work);
1067
1068 pci_set_drvdata(pdev, NULL);
1069
1070 destroy_workqueue(nic_dev->workq);
1071
1072 hinic_free_hwdev(nic_dev->hwdev);
1073
1074 free_netdev(netdev);
1075
1076 pci_release_regions(pdev);
1077 pci_disable_device(pdev);
1078
1079 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1080 }
1081
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},
1086 { 0, 0}
1087 };
1088 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1089
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,
1095 };
1096
1097 module_pci_driver(hinic_driver);