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