GNU Linux-libre 4.19.286-gnu1
[releases.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
479         down(&nic_dev->mgmt_lock);
480
481         flags = nic_dev->flags;
482         nic_dev->flags &= ~HINIC_INTF_UP;
483
484         netif_carrier_off(netdev);
485         netif_tx_disable(netdev);
486
487         update_nic_stats(nic_dev);
488
489         up(&nic_dev->mgmt_lock);
490
491         hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
492
493         hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
494
495         free_rxqs(nic_dev);
496         free_txqs(nic_dev);
497
498         if (flags & HINIC_INTF_UP)
499                 hinic_hwdev_ifdown(nic_dev->hwdev);
500
501         netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
502         return 0;
503 }
504
505 static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
506 {
507         struct hinic_dev *nic_dev = netdev_priv(netdev);
508         int err;
509
510         netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
511
512         err = hinic_port_set_mtu(nic_dev, new_mtu);
513         if (err)
514                 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
515         else
516                 netdev->mtu = new_mtu;
517
518         return err;
519 }
520
521 /**
522  * change_mac_addr - change the main mac address of network device
523  * @netdev: network device
524  * @addr: mac address to set
525  *
526  * Return 0 - Success, negative - Failure
527  **/
528 static int change_mac_addr(struct net_device *netdev, const u8 *addr)
529 {
530         struct hinic_dev *nic_dev = netdev_priv(netdev);
531         u16 vid = 0;
532         int err;
533
534         if (!is_valid_ether_addr(addr))
535                 return -EADDRNOTAVAIL;
536
537         netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
538                    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
539
540         down(&nic_dev->mgmt_lock);
541
542         do {
543                 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
544                 if (err) {
545                         netif_err(nic_dev, drv, netdev,
546                                   "Failed to delete mac\n");
547                         break;
548                 }
549
550                 err = hinic_port_add_mac(nic_dev, addr, vid);
551                 if (err) {
552                         netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
553                         break;
554                 }
555
556                 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
557         } while (vid != VLAN_N_VID);
558
559         up(&nic_dev->mgmt_lock);
560         return err;
561 }
562
563 static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
564 {
565         unsigned char new_mac[ETH_ALEN];
566         struct sockaddr *saddr = addr;
567         int err;
568
569         memcpy(new_mac, saddr->sa_data, ETH_ALEN);
570
571         err = change_mac_addr(netdev, new_mac);
572         if (!err)
573                 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
574
575         return err;
576 }
577
578 /**
579  * add_mac_addr - add mac address to network device
580  * @netdev: network device
581  * @addr: mac address to add
582  *
583  * Return 0 - Success, negative - Failure
584  **/
585 static int add_mac_addr(struct net_device *netdev, const u8 *addr)
586 {
587         struct hinic_dev *nic_dev = netdev_priv(netdev);
588         u16 vid = 0;
589         int err;
590
591         netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
592                    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
593
594         down(&nic_dev->mgmt_lock);
595
596         do {
597                 err = hinic_port_add_mac(nic_dev, addr, vid);
598                 if (err) {
599                         netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
600                         break;
601                 }
602
603                 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
604         } while (vid != VLAN_N_VID);
605
606         up(&nic_dev->mgmt_lock);
607         return err;
608 }
609
610 /**
611  * remove_mac_addr - remove mac address from network device
612  * @netdev: network device
613  * @addr: mac address to remove
614  *
615  * Return 0 - Success, negative - Failure
616  **/
617 static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
618 {
619         struct hinic_dev *nic_dev = netdev_priv(netdev);
620         u16 vid = 0;
621         int err;
622
623         if (!is_valid_ether_addr(addr))
624                 return -EADDRNOTAVAIL;
625
626         netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
627                    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
628
629         down(&nic_dev->mgmt_lock);
630
631         do {
632                 err = hinic_port_del_mac(nic_dev, addr, vid);
633                 if (err) {
634                         netif_err(nic_dev, drv, netdev,
635                                   "Failed to delete mac\n");
636                         break;
637                 }
638
639                 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
640         } while (vid != VLAN_N_VID);
641
642         up(&nic_dev->mgmt_lock);
643         return err;
644 }
645
646 static int hinic_vlan_rx_add_vid(struct net_device *netdev,
647                                  __always_unused __be16 proto, u16 vid)
648 {
649         struct hinic_dev *nic_dev = netdev_priv(netdev);
650         int ret, err;
651
652         netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
653
654         down(&nic_dev->mgmt_lock);
655
656         err = hinic_port_add_vlan(nic_dev, vid);
657         if (err) {
658                 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
659                 goto err_vlan_add;
660         }
661
662         err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
663         if (err) {
664                 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
665                 goto err_add_mac;
666         }
667
668         bitmap_set(nic_dev->vlan_bitmap, vid, 1);
669
670         up(&nic_dev->mgmt_lock);
671         return 0;
672
673 err_add_mac:
674         ret = hinic_port_del_vlan(nic_dev, vid);
675         if (ret)
676                 netif_err(nic_dev, drv, netdev,
677                           "Failed to revert by removing vlan\n");
678
679 err_vlan_add:
680         up(&nic_dev->mgmt_lock);
681         return err;
682 }
683
684 static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
685                                   __always_unused __be16 proto, u16 vid)
686 {
687         struct hinic_dev *nic_dev = netdev_priv(netdev);
688         int err;
689
690         netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
691
692         down(&nic_dev->mgmt_lock);
693
694         err = hinic_port_del_vlan(nic_dev, vid);
695         if (err) {
696                 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
697                 goto err_del_vlan;
698         }
699
700         bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
701
702         up(&nic_dev->mgmt_lock);
703         return 0;
704
705 err_del_vlan:
706         up(&nic_dev->mgmt_lock);
707         return err;
708 }
709
710 static void set_rx_mode(struct work_struct *work)
711 {
712         struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
713         struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
714         struct netdev_hw_addr *ha;
715
716         netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
717
718         hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
719
720         __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
721         __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
722
723         netdev_for_each_mc_addr(ha, nic_dev->netdev)
724                 add_mac_addr(nic_dev->netdev, ha->addr);
725 }
726
727 static void hinic_set_rx_mode(struct net_device *netdev)
728 {
729         struct hinic_dev *nic_dev = netdev_priv(netdev);
730         struct hinic_rx_mode_work *rx_mode_work;
731         u32 rx_mode;
732
733         rx_mode_work = &nic_dev->rx_mode_work;
734
735         rx_mode = HINIC_RX_MODE_UC |
736                   HINIC_RX_MODE_MC |
737                   HINIC_RX_MODE_BC;
738
739         if (netdev->flags & IFF_PROMISC)
740                 rx_mode |= HINIC_RX_MODE_PROMISC;
741         else if (netdev->flags & IFF_ALLMULTI)
742                 rx_mode |= HINIC_RX_MODE_MC_ALL;
743
744         rx_mode_work->rx_mode = rx_mode;
745
746         queue_work(nic_dev->workq, &rx_mode_work->work);
747 }
748
749 static void hinic_tx_timeout(struct net_device *netdev)
750 {
751         struct hinic_dev *nic_dev = netdev_priv(netdev);
752
753         netif_err(nic_dev, drv, netdev, "Tx timeout\n");
754 }
755
756 static void hinic_get_stats64(struct net_device *netdev,
757                               struct rtnl_link_stats64 *stats)
758 {
759         struct hinic_dev *nic_dev = netdev_priv(netdev);
760         struct hinic_rxq_stats *nic_rx_stats;
761         struct hinic_txq_stats *nic_tx_stats;
762
763         nic_rx_stats = &nic_dev->rx_stats;
764         nic_tx_stats = &nic_dev->tx_stats;
765
766         down(&nic_dev->mgmt_lock);
767
768         if (nic_dev->flags & HINIC_INTF_UP)
769                 update_nic_stats(nic_dev);
770
771         up(&nic_dev->mgmt_lock);
772
773         stats->rx_bytes   = nic_rx_stats->bytes;
774         stats->rx_packets = nic_rx_stats->pkts;
775
776         stats->tx_bytes   = nic_tx_stats->bytes;
777         stats->tx_packets = nic_tx_stats->pkts;
778         stats->tx_errors  = nic_tx_stats->tx_dropped;
779 }
780
781 static const struct net_device_ops hinic_netdev_ops = {
782         .ndo_open = hinic_open,
783         .ndo_stop = hinic_close,
784         .ndo_change_mtu = hinic_change_mtu,
785         .ndo_set_mac_address = hinic_set_mac_addr,
786         .ndo_validate_addr = eth_validate_addr,
787         .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
788         .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
789         .ndo_set_rx_mode = hinic_set_rx_mode,
790         .ndo_start_xmit = hinic_xmit_frame,
791         .ndo_tx_timeout = hinic_tx_timeout,
792         .ndo_get_stats64 = hinic_get_stats64,
793 };
794
795 static void netdev_features_init(struct net_device *netdev)
796 {
797         netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA;
798
799         netdev->vlan_features = netdev->hw_features;
800
801         netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
802 }
803
804 /**
805  * link_status_event_handler - link event handler
806  * @handle: nic device for the handler
807  * @buf_in: input buffer
808  * @in_size: input size
809  * @buf_in: output buffer
810  * @out_size: returned output size
811  *
812  * Return 0 - Success, negative - Failure
813  **/
814 static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
815                                       void *buf_out, u16 *out_size)
816 {
817         struct hinic_port_link_status *link_status, *ret_link_status;
818         struct hinic_dev *nic_dev = handle;
819
820         link_status = buf_in;
821
822         if (link_status->link == HINIC_LINK_STATE_UP) {
823                 down(&nic_dev->mgmt_lock);
824
825                 nic_dev->flags |= HINIC_LINK_UP;
826
827                 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
828                     (HINIC_LINK_UP | HINIC_INTF_UP)) {
829                         netif_carrier_on(nic_dev->netdev);
830                         netif_tx_wake_all_queues(nic_dev->netdev);
831                 }
832
833                 up(&nic_dev->mgmt_lock);
834
835                 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
836         } else {
837                 down(&nic_dev->mgmt_lock);
838
839                 nic_dev->flags &= ~HINIC_LINK_UP;
840
841                 netif_carrier_off(nic_dev->netdev);
842                 netif_tx_disable(nic_dev->netdev);
843
844                 up(&nic_dev->mgmt_lock);
845
846                 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
847         }
848
849         ret_link_status = buf_out;
850         ret_link_status->status = 0;
851
852         *out_size = sizeof(*ret_link_status);
853 }
854
855 /**
856  * nic_dev_init - Initialize the NIC device
857  * @pdev: the NIC pci device
858  *
859  * Return 0 - Success, negative - Failure
860  **/
861 static int nic_dev_init(struct pci_dev *pdev)
862 {
863         struct hinic_rx_mode_work *rx_mode_work;
864         struct hinic_txq_stats *tx_stats;
865         struct hinic_rxq_stats *rx_stats;
866         struct hinic_dev *nic_dev;
867         struct net_device *netdev;
868         struct hinic_hwdev *hwdev;
869         int err, num_qps;
870
871         hwdev = hinic_init_hwdev(pdev);
872         if (IS_ERR(hwdev)) {
873                 dev_err(&pdev->dev, "Failed to initialize HW device\n");
874                 return PTR_ERR(hwdev);
875         }
876
877         num_qps = hinic_hwdev_num_qps(hwdev);
878         if (num_qps <= 0) {
879                 dev_err(&pdev->dev, "Invalid number of QPS\n");
880                 err = -EINVAL;
881                 goto err_num_qps;
882         }
883
884         netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
885         if (!netdev) {
886                 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
887                 err = -ENOMEM;
888                 goto err_alloc_etherdev;
889         }
890
891         netdev->netdev_ops = &hinic_netdev_ops;
892         netdev->ethtool_ops = &hinic_ethtool_ops;
893         netdev->max_mtu = ETH_MAX_MTU;
894
895         nic_dev = netdev_priv(netdev);
896         nic_dev->netdev = netdev;
897         nic_dev->hwdev  = hwdev;
898         nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
899         nic_dev->flags = 0;
900         nic_dev->txqs = NULL;
901         nic_dev->rxqs = NULL;
902         nic_dev->tx_weight = tx_weight;
903         nic_dev->rx_weight = rx_weight;
904
905         sema_init(&nic_dev->mgmt_lock, 1);
906
907         tx_stats = &nic_dev->tx_stats;
908         rx_stats = &nic_dev->rx_stats;
909
910         u64_stats_init(&tx_stats->syncp);
911         u64_stats_init(&rx_stats->syncp);
912
913         nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
914                                             VLAN_BITMAP_SIZE(nic_dev),
915                                             GFP_KERNEL);
916         if (!nic_dev->vlan_bitmap) {
917                 err = -ENOMEM;
918                 goto err_vlan_bitmap;
919         }
920
921         nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
922         if (!nic_dev->workq) {
923                 err = -ENOMEM;
924                 goto err_workq;
925         }
926
927         pci_set_drvdata(pdev, netdev);
928
929         err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
930         if (err)
931                 dev_warn(&pdev->dev, "Failed to get mac address\n");
932
933         err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
934         if (err) {
935                 dev_err(&pdev->dev, "Failed to add mac\n");
936                 goto err_add_mac;
937         }
938
939         err = hinic_port_set_mtu(nic_dev, netdev->mtu);
940         if (err) {
941                 dev_err(&pdev->dev, "Failed to set mtu\n");
942                 goto err_set_mtu;
943         }
944
945         rx_mode_work = &nic_dev->rx_mode_work;
946         INIT_WORK(&rx_mode_work->work, set_rx_mode);
947
948         netdev_features_init(netdev);
949
950         netif_carrier_off(netdev);
951
952         hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
953                                 nic_dev, link_status_event_handler);
954
955         SET_NETDEV_DEV(netdev, &pdev->dev);
956         err = register_netdev(netdev);
957         if (err) {
958                 dev_err(&pdev->dev, "Failed to register netdev\n");
959                 goto err_reg_netdev;
960         }
961
962         return 0;
963
964 err_reg_netdev:
965         hinic_hwdev_cb_unregister(nic_dev->hwdev,
966                                   HINIC_MGMT_MSG_CMD_LINK_STATUS);
967         cancel_work_sync(&rx_mode_work->work);
968
969 err_set_mtu:
970 err_add_mac:
971         pci_set_drvdata(pdev, NULL);
972         destroy_workqueue(nic_dev->workq);
973
974 err_workq:
975 err_vlan_bitmap:
976         free_netdev(netdev);
977
978 err_alloc_etherdev:
979 err_num_qps:
980         hinic_free_hwdev(hwdev);
981         return err;
982 }
983
984 static int hinic_probe(struct pci_dev *pdev,
985                        const struct pci_device_id *id)
986 {
987         int err = pci_enable_device(pdev);
988
989         if (err) {
990                 dev_err(&pdev->dev, "Failed to enable PCI device\n");
991                 return err;
992         }
993
994         err = pci_request_regions(pdev, HINIC_DRV_NAME);
995         if (err) {
996                 dev_err(&pdev->dev, "Failed to request PCI regions\n");
997                 goto err_pci_regions;
998         }
999
1000         pci_set_master(pdev);
1001
1002         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1003         if (err) {
1004                 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1005                 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1006                 if (err) {
1007                         dev_err(&pdev->dev, "Failed to set DMA mask\n");
1008                         goto err_dma_mask;
1009                 }
1010         }
1011
1012         err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1013         if (err) {
1014                 dev_warn(&pdev->dev,
1015                          "Couldn't set 64-bit consistent DMA mask\n");
1016                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1017                 if (err) {
1018                         dev_err(&pdev->dev,
1019                                 "Failed to set consistent DMA mask\n");
1020                         goto err_dma_consistent_mask;
1021                 }
1022         }
1023
1024         err = nic_dev_init(pdev);
1025         if (err) {
1026                 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1027                 goto err_nic_dev_init;
1028         }
1029
1030         dev_info(&pdev->dev, "HiNIC driver - probed\n");
1031         return 0;
1032
1033 err_nic_dev_init:
1034 err_dma_consistent_mask:
1035 err_dma_mask:
1036         pci_release_regions(pdev);
1037
1038 err_pci_regions:
1039         pci_disable_device(pdev);
1040         return err;
1041 }
1042
1043 static void hinic_remove(struct pci_dev *pdev)
1044 {
1045         struct net_device *netdev = pci_get_drvdata(pdev);
1046         struct hinic_dev *nic_dev = netdev_priv(netdev);
1047         struct hinic_rx_mode_work *rx_mode_work;
1048
1049         unregister_netdev(netdev);
1050
1051         hinic_hwdev_cb_unregister(nic_dev->hwdev,
1052                                   HINIC_MGMT_MSG_CMD_LINK_STATUS);
1053
1054         rx_mode_work = &nic_dev->rx_mode_work;
1055         cancel_work_sync(&rx_mode_work->work);
1056
1057         pci_set_drvdata(pdev, NULL);
1058
1059         destroy_workqueue(nic_dev->workq);
1060
1061         hinic_free_hwdev(nic_dev->hwdev);
1062
1063         free_netdev(netdev);
1064
1065         pci_release_regions(pdev);
1066         pci_disable_device(pdev);
1067
1068         dev_info(&pdev->dev, "HiNIC driver - removed\n");
1069 }
1070
1071 static const struct pci_device_id hinic_pci_table[] = {
1072         { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1073         { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_25GE), 0},
1074         { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1075         { 0, 0}
1076 };
1077 MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1078
1079 static struct pci_driver hinic_driver = {
1080         .name           = HINIC_DRV_NAME,
1081         .id_table       = hinic_pci_table,
1082         .probe          = hinic_probe,
1083         .remove         = hinic_remove,
1084 };
1085
1086 module_pci_driver(hinic_driver);