1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/pci.h>
11#include <linux/device.h>
12#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/etherdevice.h>
15#include <linux/netdevice.h>
16#include <linux/slab.h>
17#include <linux/if_vlan.h>
18#include <linux/semaphore.h>
19#include <linux/workqueue.h>
20#include <net/ip.h>
21#include <net/devlink.h>
22#include <linux/bitops.h>
23#include <linux/bitmap.h>
24#include <linux/delay.h>
25#include <linux/err.h>
26
27#include "hinic_debugfs.h"
28#include "hinic_hw_qp.h"
29#include "hinic_hw_dev.h"
30#include "hinic_devlink.h"
31#include "hinic_port.h"
32#include "hinic_tx.h"
33#include "hinic_rx.h"
34#include "hinic_dev.h"
35#include "hinic_sriov.h"
36
37MODULE_AUTHOR("Huawei Technologies CO., Ltd");
38MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
39MODULE_LICENSE("GPL");
40
41static unsigned int tx_weight = 64;
42module_param(tx_weight, uint, 0644);
43MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
44
45static unsigned int rx_weight = 64;
46module_param(rx_weight, uint, 0644);
47MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
48
49#define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
50#define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200
51#define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205
52#define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210
53#define HINIC_DEV_ID_VF 0x375e
54
55#define HINIC_WQ_NAME "hinic_dev"
56
57#define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
58 NETIF_MSG_IFUP | \
59 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
60
61#define HINIC_LRO_MAX_WQE_NUM_DEFAULT 8
62
63#define HINIC_LRO_RX_TIMER_DEFAULT 16
64
65#define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
66
67#define work_to_rx_mode_work(work) \
68 container_of(work, struct hinic_rx_mode_work, work)
69
70#define rx_mode_work_to_nic_dev(rx_mode_work) \
71 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
72
73#define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
74
75#define HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT 2
76#define HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG 32
77#define HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG 7
78
79static int change_mac_addr(struct net_device *netdev, const u8 *addr);
80
81static int set_features(struct hinic_dev *nic_dev,
82 netdev_features_t pre_features,
83 netdev_features_t features, bool force_change);
84
85static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
86{
87 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
88 struct hinic_rxq_stats rx_stats;
89
90 u64_stats_init(&rx_stats.syncp);
91
92 hinic_rxq_get_stats(rxq, &rx_stats);
93
94 u64_stats_update_begin(&nic_rx_stats->syncp);
95 nic_rx_stats->bytes += rx_stats.bytes;
96 nic_rx_stats->pkts += rx_stats.pkts;
97 nic_rx_stats->errors += rx_stats.errors;
98 nic_rx_stats->csum_errors += rx_stats.csum_errors;
99 nic_rx_stats->other_errors += rx_stats.other_errors;
100 u64_stats_update_end(&nic_rx_stats->syncp);
101
102 hinic_rxq_clean_stats(rxq);
103}
104
105static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
106{
107 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
108 struct hinic_txq_stats tx_stats;
109
110 u64_stats_init(&tx_stats.syncp);
111
112 hinic_txq_get_stats(txq, &tx_stats);
113
114 u64_stats_update_begin(&nic_tx_stats->syncp);
115 nic_tx_stats->bytes += tx_stats.bytes;
116 nic_tx_stats->pkts += tx_stats.pkts;
117 nic_tx_stats->tx_busy += tx_stats.tx_busy;
118 nic_tx_stats->tx_wake += tx_stats.tx_wake;
119 nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
120 nic_tx_stats->big_frags_pkts += tx_stats.big_frags_pkts;
121 u64_stats_update_end(&nic_tx_stats->syncp);
122
123 hinic_txq_clean_stats(txq);
124}
125
126static void update_nic_stats(struct hinic_dev *nic_dev)
127{
128 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
129
130 for (i = 0; i < num_qps; i++)
131 update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
132
133 for (i = 0; i < num_qps; i++)
134 update_tx_stats(nic_dev, &nic_dev->txqs[i]);
135}
136
137
138
139
140
141
142
143static int create_txqs(struct hinic_dev *nic_dev)
144{
145 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
146 struct net_device *netdev = nic_dev->netdev;
147 size_t txq_size;
148
149 if (nic_dev->txqs)
150 return -EINVAL;
151
152 txq_size = num_txqs * sizeof(*nic_dev->txqs);
153 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
154 if (!nic_dev->txqs)
155 return -ENOMEM;
156
157 hinic_sq_dbgfs_init(nic_dev);
158
159 for (i = 0; i < num_txqs; i++) {
160 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
161
162 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
163 if (err) {
164 netif_err(nic_dev, drv, netdev,
165 "Failed to init Txq\n");
166 goto err_init_txq;
167 }
168
169 err = hinic_sq_debug_add(nic_dev, i);
170 if (err) {
171 netif_err(nic_dev, drv, netdev,
172 "Failed to add SQ%d debug\n", i);
173 goto err_add_sq_dbg;
174 }
175
176 }
177
178 return 0;
179
180err_add_sq_dbg:
181 hinic_clean_txq(&nic_dev->txqs[i]);
182err_init_txq:
183 for (j = 0; j < i; j++) {
184 hinic_sq_debug_rem(nic_dev->txqs[j].sq);
185 hinic_clean_txq(&nic_dev->txqs[j]);
186 }
187
188 hinic_sq_dbgfs_uninit(nic_dev);
189
190 devm_kfree(&netdev->dev, nic_dev->txqs);
191 return err;
192}
193
194static void enable_txqs_napi(struct hinic_dev *nic_dev)
195{
196 int num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
197 int i;
198
199 for (i = 0; i < num_txqs; i++)
200 napi_enable(&nic_dev->txqs[i].napi);
201}
202
203static void disable_txqs_napi(struct hinic_dev *nic_dev)
204{
205 int num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
206 int i;
207
208 for (i = 0; i < num_txqs; i++)
209 napi_disable(&nic_dev->txqs[i].napi);
210}
211
212
213
214
215
216static void free_txqs(struct hinic_dev *nic_dev)
217{
218 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
219 struct net_device *netdev = nic_dev->netdev;
220
221 if (!nic_dev->txqs)
222 return;
223
224 for (i = 0; i < num_txqs; i++) {
225 hinic_sq_debug_rem(nic_dev->txqs[i].sq);
226 hinic_clean_txq(&nic_dev->txqs[i]);
227 }
228
229 hinic_sq_dbgfs_uninit(nic_dev);
230
231 devm_kfree(&netdev->dev, nic_dev->txqs);
232 nic_dev->txqs = NULL;
233}
234
235
236
237
238
239
240
241static int create_rxqs(struct hinic_dev *nic_dev)
242{
243 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
244 struct net_device *netdev = nic_dev->netdev;
245 size_t rxq_size;
246
247 if (nic_dev->rxqs)
248 return -EINVAL;
249
250 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
251 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
252 if (!nic_dev->rxqs)
253 return -ENOMEM;
254
255 hinic_rq_dbgfs_init(nic_dev);
256
257 for (i = 0; i < num_rxqs; i++) {
258 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
259
260 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
261 if (err) {
262 netif_err(nic_dev, drv, netdev,
263 "Failed to init rxq\n");
264 goto err_init_rxq;
265 }
266
267 err = hinic_rq_debug_add(nic_dev, i);
268 if (err) {
269 netif_err(nic_dev, drv, netdev,
270 "Failed to add RQ%d debug\n", i);
271 goto err_add_rq_dbg;
272 }
273 }
274
275 return 0;
276
277err_add_rq_dbg:
278 hinic_clean_rxq(&nic_dev->rxqs[i]);
279err_init_rxq:
280 for (j = 0; j < i; j++) {
281 hinic_rq_debug_rem(nic_dev->rxqs[j].rq);
282 hinic_clean_rxq(&nic_dev->rxqs[j]);
283 }
284
285 hinic_rq_dbgfs_uninit(nic_dev);
286
287 devm_kfree(&netdev->dev, nic_dev->rxqs);
288 return err;
289}
290
291
292
293
294
295static void free_rxqs(struct hinic_dev *nic_dev)
296{
297 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
298 struct net_device *netdev = nic_dev->netdev;
299
300 if (!nic_dev->rxqs)
301 return;
302
303 for (i = 0; i < num_rxqs; i++) {
304 hinic_rq_debug_rem(nic_dev->rxqs[i].rq);
305 hinic_clean_rxq(&nic_dev->rxqs[i]);
306 }
307
308 hinic_rq_dbgfs_uninit(nic_dev);
309
310 devm_kfree(&netdev->dev, nic_dev->rxqs);
311 nic_dev->rxqs = NULL;
312}
313
314static int hinic_configure_max_qnum(struct hinic_dev *nic_dev)
315{
316 int err;
317
318 err = hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps);
319 if (err)
320 return err;
321
322 return 0;
323}
324
325static int hinic_rss_init(struct hinic_dev *nic_dev)
326{
327 u8 default_rss_key[HINIC_RSS_KEY_SIZE];
328 u8 tmpl_idx = nic_dev->rss_tmpl_idx;
329 u32 *indir_tbl;
330 int err, i;
331
332 indir_tbl = kcalloc(HINIC_RSS_INDIR_SIZE, sizeof(u32), GFP_KERNEL);
333 if (!indir_tbl)
334 return -ENOMEM;
335
336 netdev_rss_key_fill(default_rss_key, sizeof(default_rss_key));
337 for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
338 indir_tbl[i] = ethtool_rxfh_indir_default(i, nic_dev->num_rss);
339
340 err = hinic_rss_set_template_tbl(nic_dev, tmpl_idx, default_rss_key);
341 if (err)
342 goto out;
343
344 err = hinic_rss_set_indir_tbl(nic_dev, tmpl_idx, indir_tbl);
345 if (err)
346 goto out;
347
348 err = hinic_set_rss_type(nic_dev, tmpl_idx, nic_dev->rss_type);
349 if (err)
350 goto out;
351
352 err = hinic_rss_set_hash_engine(nic_dev, tmpl_idx,
353 nic_dev->rss_hash_engine);
354 if (err)
355 goto out;
356
357 err = hinic_rss_cfg(nic_dev, 1, tmpl_idx);
358 if (err)
359 goto out;
360
361out:
362 kfree(indir_tbl);
363 return err;
364}
365
366static void hinic_rss_deinit(struct hinic_dev *nic_dev)
367{
368 hinic_rss_cfg(nic_dev, 0, nic_dev->rss_tmpl_idx);
369}
370
371static void hinic_init_rss_parameters(struct hinic_dev *nic_dev)
372{
373 nic_dev->rss_hash_engine = HINIC_RSS_HASH_ENGINE_TYPE_XOR;
374 nic_dev->rss_type.tcp_ipv6_ext = 1;
375 nic_dev->rss_type.ipv6_ext = 1;
376 nic_dev->rss_type.tcp_ipv6 = 1;
377 nic_dev->rss_type.ipv6 = 1;
378 nic_dev->rss_type.tcp_ipv4 = 1;
379 nic_dev->rss_type.ipv4 = 1;
380 nic_dev->rss_type.udp_ipv6 = 1;
381 nic_dev->rss_type.udp_ipv4 = 1;
382}
383
384static void hinic_enable_rss(struct hinic_dev *nic_dev)
385{
386 struct net_device *netdev = nic_dev->netdev;
387 struct hinic_hwdev *hwdev = nic_dev->hwdev;
388 struct hinic_hwif *hwif = hwdev->hwif;
389 struct pci_dev *pdev = hwif->pdev;
390 int i, node, err = 0;
391 u16 num_cpus = 0;
392
393 if (nic_dev->max_qps <= 1) {
394 nic_dev->flags &= ~HINIC_RSS_ENABLE;
395 nic_dev->rss_limit = nic_dev->max_qps;
396 nic_dev->num_qps = nic_dev->max_qps;
397 nic_dev->num_rss = nic_dev->max_qps;
398
399 return;
400 }
401
402 err = hinic_rss_template_alloc(nic_dev, &nic_dev->rss_tmpl_idx);
403 if (err) {
404 netif_err(nic_dev, drv, netdev,
405 "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
406 nic_dev->flags &= ~HINIC_RSS_ENABLE;
407 nic_dev->max_qps = 1;
408 nic_dev->rss_limit = nic_dev->max_qps;
409 nic_dev->num_qps = nic_dev->max_qps;
410 nic_dev->num_rss = nic_dev->max_qps;
411
412 return;
413 }
414
415 nic_dev->flags |= HINIC_RSS_ENABLE;
416
417 for (i = 0; i < num_online_cpus(); i++) {
418 node = cpu_to_node(i);
419 if (node == dev_to_node(&pdev->dev))
420 num_cpus++;
421 }
422
423 if (!num_cpus)
424 num_cpus = num_online_cpus();
425
426 nic_dev->num_qps = hinic_hwdev_num_qps(hwdev);
427 nic_dev->num_qps = min_t(u16, nic_dev->num_qps, num_cpus);
428
429 nic_dev->rss_limit = nic_dev->num_qps;
430 nic_dev->num_rss = nic_dev->num_qps;
431
432 hinic_init_rss_parameters(nic_dev);
433 err = hinic_rss_init(nic_dev);
434 if (err)
435 netif_err(nic_dev, drv, netdev, "Failed to init rss\n");
436}
437
438int hinic_open(struct net_device *netdev)
439{
440 struct hinic_dev *nic_dev = netdev_priv(netdev);
441 enum hinic_port_link_state link_state;
442 int err, ret;
443
444 if (!(nic_dev->flags & HINIC_INTF_UP)) {
445 err = hinic_hwdev_ifup(nic_dev->hwdev, nic_dev->sq_depth,
446 nic_dev->rq_depth);
447 if (err) {
448 netif_err(nic_dev, drv, netdev,
449 "Failed - HW interface up\n");
450 return err;
451 }
452 }
453
454 err = create_txqs(nic_dev);
455 if (err) {
456 netif_err(nic_dev, drv, netdev,
457 "Failed to create Tx queues\n");
458 goto err_create_txqs;
459 }
460
461 enable_txqs_napi(nic_dev);
462
463 err = create_rxqs(nic_dev);
464 if (err) {
465 netif_err(nic_dev, drv, netdev,
466 "Failed to create Rx queues\n");
467 goto err_create_rxqs;
468 }
469
470 hinic_enable_rss(nic_dev);
471
472 err = hinic_configure_max_qnum(nic_dev);
473 if (err) {
474 netif_err(nic_dev, drv, nic_dev->netdev,
475 "Failed to configure the maximum number of queues\n");
476 goto err_port_state;
477 }
478
479 netif_set_real_num_tx_queues(netdev, nic_dev->num_qps);
480 netif_set_real_num_rx_queues(netdev, nic_dev->num_qps);
481
482 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
483 if (err) {
484 netif_err(nic_dev, drv, netdev,
485 "Failed to set port state\n");
486 goto err_port_state;
487 }
488
489 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
490 if (err) {
491 netif_err(nic_dev, drv, netdev,
492 "Failed to set func port state\n");
493 goto err_func_port_state;
494 }
495
496 down(&nic_dev->mgmt_lock);
497
498 err = hinic_port_link_state(nic_dev, &link_state);
499 if (err) {
500 netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
501 goto err_port_link;
502 }
503
504 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
505 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, link_state);
506
507 if (link_state == HINIC_LINK_STATE_UP) {
508 nic_dev->flags |= HINIC_LINK_UP;
509 nic_dev->cable_unplugged = false;
510 nic_dev->module_unrecognized = false;
511 }
512
513 nic_dev->flags |= HINIC_INTF_UP;
514
515 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
516 (HINIC_LINK_UP | HINIC_INTF_UP)) {
517 netif_info(nic_dev, drv, netdev, "link + intf UP\n");
518 netif_carrier_on(netdev);
519 netif_tx_wake_all_queues(netdev);
520 }
521
522 up(&nic_dev->mgmt_lock);
523
524 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
525 return 0;
526
527err_port_link:
528 up(&nic_dev->mgmt_lock);
529 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
530 if (ret)
531 netif_warn(nic_dev, drv, netdev,
532 "Failed to revert func port state\n");
533
534err_func_port_state:
535 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
536 if (ret)
537 netif_warn(nic_dev, drv, netdev,
538 "Failed to revert port state\n");
539err_port_state:
540 free_rxqs(nic_dev);
541 if (nic_dev->flags & HINIC_RSS_ENABLE) {
542 hinic_rss_deinit(nic_dev);
543 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
544 }
545
546err_create_rxqs:
547 disable_txqs_napi(nic_dev);
548 free_txqs(nic_dev);
549
550err_create_txqs:
551 if (!(nic_dev->flags & HINIC_INTF_UP))
552 hinic_hwdev_ifdown(nic_dev->hwdev);
553 return err;
554}
555
556int hinic_close(struct net_device *netdev)
557{
558 struct hinic_dev *nic_dev = netdev_priv(netdev);
559 unsigned int flags;
560
561
562 disable_txqs_napi(nic_dev);
563
564 down(&nic_dev->mgmt_lock);
565
566 flags = nic_dev->flags;
567 nic_dev->flags &= ~HINIC_INTF_UP;
568
569 netif_carrier_off(netdev);
570 netif_tx_disable(netdev);
571
572 update_nic_stats(nic_dev);
573
574 up(&nic_dev->mgmt_lock);
575
576 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
577 hinic_notify_all_vfs_link_changed(nic_dev->hwdev, 0);
578
579 hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
580
581 hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
582
583 if (nic_dev->flags & HINIC_RSS_ENABLE) {
584 hinic_rss_deinit(nic_dev);
585 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
586 }
587
588 free_rxqs(nic_dev);
589 free_txqs(nic_dev);
590
591 if (flags & HINIC_INTF_UP)
592 hinic_hwdev_ifdown(nic_dev->hwdev);
593
594 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
595 return 0;
596}
597
598static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
599{
600 struct hinic_dev *nic_dev = netdev_priv(netdev);
601 int err;
602
603 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
604
605 err = hinic_port_set_mtu(nic_dev, new_mtu);
606 if (err)
607 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
608 else
609 netdev->mtu = new_mtu;
610
611 return err;
612}
613
614
615
616
617
618
619
620
621static int change_mac_addr(struct net_device *netdev, const u8 *addr)
622{
623 struct hinic_dev *nic_dev = netdev_priv(netdev);
624 u16 vid = 0;
625 int err;
626
627 if (!is_valid_ether_addr(addr))
628 return -EADDRNOTAVAIL;
629
630 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
631 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
632
633 down(&nic_dev->mgmt_lock);
634
635 do {
636 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
637 if (err) {
638 netif_err(nic_dev, drv, netdev,
639 "Failed to delete mac\n");
640 break;
641 }
642
643 err = hinic_port_add_mac(nic_dev, addr, vid);
644 if (err) {
645 netif_err(nic_dev, drv, netdev, "Failed to add 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
656static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
657{
658 unsigned char new_mac[ETH_ALEN];
659 struct sockaddr *saddr = addr;
660 int err;
661
662 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
663
664 err = change_mac_addr(netdev, new_mac);
665 if (!err)
666 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
667
668 return err;
669}
670
671
672
673
674
675
676
677
678static int add_mac_addr(struct net_device *netdev, const u8 *addr)
679{
680 struct hinic_dev *nic_dev = netdev_priv(netdev);
681 u16 vid = 0;
682 int err;
683
684 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
685 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
686
687 down(&nic_dev->mgmt_lock);
688
689 do {
690 err = hinic_port_add_mac(nic_dev, addr, vid);
691 if (err) {
692 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
693 break;
694 }
695
696 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
697 } while (vid != VLAN_N_VID);
698
699 up(&nic_dev->mgmt_lock);
700 return err;
701}
702
703
704
705
706
707
708
709
710static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
711{
712 struct hinic_dev *nic_dev = netdev_priv(netdev);
713 u16 vid = 0;
714 int err;
715
716 if (!is_valid_ether_addr(addr))
717 return -EADDRNOTAVAIL;
718
719 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
720 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
721
722 down(&nic_dev->mgmt_lock);
723
724 do {
725 err = hinic_port_del_mac(nic_dev, addr, vid);
726 if (err) {
727 netif_err(nic_dev, drv, netdev,
728 "Failed to delete mac\n");
729 break;
730 }
731
732 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
733 } while (vid != VLAN_N_VID);
734
735 up(&nic_dev->mgmt_lock);
736 return err;
737}
738
739static int hinic_vlan_rx_add_vid(struct net_device *netdev,
740 __always_unused __be16 proto, u16 vid)
741{
742 struct hinic_dev *nic_dev = netdev_priv(netdev);
743 int ret, err;
744
745 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
746
747 down(&nic_dev->mgmt_lock);
748
749 err = hinic_port_add_vlan(nic_dev, vid);
750 if (err) {
751 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
752 goto err_vlan_add;
753 }
754
755 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
756 if (err && err != HINIC_PF_SET_VF_ALREADY) {
757 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
758 goto err_add_mac;
759 }
760
761 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
762
763 up(&nic_dev->mgmt_lock);
764 return 0;
765
766err_add_mac:
767 ret = hinic_port_del_vlan(nic_dev, vid);
768 if (ret)
769 netif_err(nic_dev, drv, netdev,
770 "Failed to revert by removing vlan\n");
771
772err_vlan_add:
773 up(&nic_dev->mgmt_lock);
774 return err;
775}
776
777static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
778 __always_unused __be16 proto, u16 vid)
779{
780 struct hinic_dev *nic_dev = netdev_priv(netdev);
781 int err;
782
783 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
784
785 down(&nic_dev->mgmt_lock);
786
787 err = hinic_port_del_vlan(nic_dev, vid);
788 if (err) {
789 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
790 goto err_del_vlan;
791 }
792
793 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
794
795 up(&nic_dev->mgmt_lock);
796 return 0;
797
798err_del_vlan:
799 up(&nic_dev->mgmt_lock);
800 return err;
801}
802
803static void set_rx_mode(struct work_struct *work)
804{
805 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
806 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
807
808 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
809
810 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
811 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
812}
813
814static void hinic_set_rx_mode(struct net_device *netdev)
815{
816 struct hinic_dev *nic_dev = netdev_priv(netdev);
817 struct hinic_rx_mode_work *rx_mode_work;
818 u32 rx_mode;
819
820 rx_mode_work = &nic_dev->rx_mode_work;
821
822 rx_mode = HINIC_RX_MODE_UC |
823 HINIC_RX_MODE_MC |
824 HINIC_RX_MODE_BC;
825
826 if (netdev->flags & IFF_PROMISC) {
827 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
828 rx_mode |= HINIC_RX_MODE_PROMISC;
829 } else if (netdev->flags & IFF_ALLMULTI) {
830 rx_mode |= HINIC_RX_MODE_MC_ALL;
831 }
832
833 rx_mode_work->rx_mode = rx_mode;
834
835 queue_work(nic_dev->workq, &rx_mode_work->work);
836}
837
838static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
839{
840 struct hinic_dev *nic_dev = netdev_priv(netdev);
841 u16 sw_pi, hw_ci, sw_ci;
842 struct hinic_sq *sq;
843 u16 num_sqs, q_id;
844
845 num_sqs = hinic_hwdev_num_qps(nic_dev->hwdev);
846
847 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
848
849 for (q_id = 0; q_id < num_sqs; q_id++) {
850 if (!netif_xmit_stopped(netdev_get_tx_queue(netdev, q_id)))
851 continue;
852
853 sq = hinic_hwdev_get_sq(nic_dev->hwdev, q_id);
854 sw_pi = atomic_read(&sq->wq->prod_idx) & sq->wq->mask;
855 hw_ci = be16_to_cpu(*(u16 *)(sq->hw_ci_addr)) & sq->wq->mask;
856 sw_ci = atomic_read(&sq->wq->cons_idx) & sq->wq->mask;
857 netif_err(nic_dev, drv, netdev, "Txq%d: sw_pi: %d, hw_ci: %d, sw_ci: %d, napi->state: 0x%lx\n",
858 q_id, sw_pi, hw_ci, sw_ci,
859 nic_dev->txqs[q_id].napi.state);
860 }
861}
862
863static void hinic_get_stats64(struct net_device *netdev,
864 struct rtnl_link_stats64 *stats)
865{
866 struct hinic_dev *nic_dev = netdev_priv(netdev);
867 struct hinic_rxq_stats *nic_rx_stats;
868 struct hinic_txq_stats *nic_tx_stats;
869
870 nic_rx_stats = &nic_dev->rx_stats;
871 nic_tx_stats = &nic_dev->tx_stats;
872
873 down(&nic_dev->mgmt_lock);
874
875 if (nic_dev->flags & HINIC_INTF_UP)
876 update_nic_stats(nic_dev);
877
878 up(&nic_dev->mgmt_lock);
879
880 stats->rx_bytes = nic_rx_stats->bytes;
881 stats->rx_packets = nic_rx_stats->pkts;
882 stats->rx_errors = nic_rx_stats->errors;
883
884 stats->tx_bytes = nic_tx_stats->bytes;
885 stats->tx_packets = nic_tx_stats->pkts;
886 stats->tx_errors = nic_tx_stats->tx_dropped;
887}
888
889static int hinic_set_features(struct net_device *netdev,
890 netdev_features_t features)
891{
892 struct hinic_dev *nic_dev = netdev_priv(netdev);
893
894 return set_features(nic_dev, nic_dev->netdev->features,
895 features, false);
896}
897
898static netdev_features_t hinic_fix_features(struct net_device *netdev,
899 netdev_features_t features)
900{
901 struct hinic_dev *nic_dev = netdev_priv(netdev);
902
903
904 if (!(features & NETIF_F_RXCSUM)) {
905 netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
906 features &= ~NETIF_F_LRO;
907 }
908
909 return features;
910}
911
912static const struct net_device_ops hinic_netdev_ops = {
913 .ndo_open = hinic_open,
914 .ndo_stop = hinic_close,
915 .ndo_change_mtu = hinic_change_mtu,
916 .ndo_set_mac_address = hinic_set_mac_addr,
917 .ndo_validate_addr = eth_validate_addr,
918 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
919 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
920 .ndo_set_rx_mode = hinic_set_rx_mode,
921 .ndo_start_xmit = hinic_xmit_frame,
922 .ndo_tx_timeout = hinic_tx_timeout,
923 .ndo_get_stats64 = hinic_get_stats64,
924 .ndo_fix_features = hinic_fix_features,
925 .ndo_set_features = hinic_set_features,
926 .ndo_set_vf_mac = hinic_ndo_set_vf_mac,
927 .ndo_set_vf_vlan = hinic_ndo_set_vf_vlan,
928 .ndo_get_vf_config = hinic_ndo_get_vf_config,
929 .ndo_set_vf_trust = hinic_ndo_set_vf_trust,
930 .ndo_set_vf_rate = hinic_ndo_set_vf_bw,
931 .ndo_set_vf_spoofchk = hinic_ndo_set_vf_spoofchk,
932 .ndo_set_vf_link_state = hinic_ndo_set_vf_link_state,
933};
934
935static const struct net_device_ops hinicvf_netdev_ops = {
936 .ndo_open = hinic_open,
937 .ndo_stop = hinic_close,
938 .ndo_change_mtu = hinic_change_mtu,
939 .ndo_set_mac_address = hinic_set_mac_addr,
940 .ndo_validate_addr = eth_validate_addr,
941 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
942 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
943 .ndo_set_rx_mode = hinic_set_rx_mode,
944 .ndo_start_xmit = hinic_xmit_frame,
945 .ndo_tx_timeout = hinic_tx_timeout,
946 .ndo_get_stats64 = hinic_get_stats64,
947 .ndo_fix_features = hinic_fix_features,
948 .ndo_set_features = hinic_set_features,
949};
950
951static void netdev_features_init(struct net_device *netdev)
952{
953 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
954 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
955 NETIF_F_RXCSUM | NETIF_F_LRO |
956 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
957 NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_UDP_TUNNEL_CSUM;
958
959 netdev->vlan_features = netdev->hw_features;
960
961 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
962
963 netdev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SCTP_CRC |
964 NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
965 NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_UDP_TUNNEL;
966}
967
968static void hinic_refresh_nic_cfg(struct hinic_dev *nic_dev)
969{
970 struct hinic_nic_cfg *nic_cfg = &nic_dev->hwdev->func_to_io.nic_cfg;
971 struct hinic_pause_config pause_info = {0};
972 struct hinic_port_cap port_cap = {0};
973
974 if (hinic_port_get_cap(nic_dev, &port_cap))
975 return;
976
977 mutex_lock(&nic_cfg->cfg_mutex);
978 if (nic_cfg->pause_set || !port_cap.autoneg_state) {
979 nic_cfg->auto_neg = port_cap.autoneg_state;
980 pause_info.auto_neg = nic_cfg->auto_neg;
981 pause_info.rx_pause = nic_cfg->rx_pause;
982 pause_info.tx_pause = nic_cfg->tx_pause;
983 hinic_set_hw_pause_info(nic_dev->hwdev, &pause_info);
984 }
985 mutex_unlock(&nic_cfg->cfg_mutex);
986}
987
988
989
990
991
992
993
994
995
996
997
998static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
999 void *buf_out, u16 *out_size)
1000{
1001 struct hinic_port_link_status *link_status, *ret_link_status;
1002 struct hinic_dev *nic_dev = handle;
1003
1004 link_status = buf_in;
1005
1006 if (link_status->link == HINIC_LINK_STATE_UP) {
1007 down(&nic_dev->mgmt_lock);
1008
1009 nic_dev->flags |= HINIC_LINK_UP;
1010 nic_dev->cable_unplugged = false;
1011 nic_dev->module_unrecognized = false;
1012
1013 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
1014 (HINIC_LINK_UP | HINIC_INTF_UP)) {
1015 netif_carrier_on(nic_dev->netdev);
1016 netif_tx_wake_all_queues(nic_dev->netdev);
1017 }
1018
1019 up(&nic_dev->mgmt_lock);
1020
1021 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
1022 hinic_refresh_nic_cfg(nic_dev);
1023
1024 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
1025 } else {
1026 down(&nic_dev->mgmt_lock);
1027
1028 nic_dev->flags &= ~HINIC_LINK_UP;
1029
1030 netif_carrier_off(nic_dev->netdev);
1031 netif_tx_disable(nic_dev->netdev);
1032
1033 up(&nic_dev->mgmt_lock);
1034
1035 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
1036 }
1037
1038 if (!HINIC_IS_VF(nic_dev->hwdev->hwif))
1039 hinic_notify_all_vfs_link_changed(nic_dev->hwdev,
1040 link_status->link);
1041
1042 ret_link_status = buf_out;
1043 ret_link_status->status = 0;
1044
1045 *out_size = sizeof(*ret_link_status);
1046}
1047
1048static void cable_plug_event(void *handle,
1049 void *buf_in, u16 in_size,
1050 void *buf_out, u16 *out_size)
1051{
1052 struct hinic_cable_plug_event *plug_event = buf_in;
1053 struct hinic_dev *nic_dev = handle;
1054
1055 nic_dev->cable_unplugged = plug_event->plugged ? false : true;
1056
1057 *out_size = sizeof(*plug_event);
1058 plug_event = buf_out;
1059 plug_event->status = 0;
1060}
1061
1062static void link_err_event(void *handle,
1063 void *buf_in, u16 in_size,
1064 void *buf_out, u16 *out_size)
1065{
1066 struct hinic_link_err_event *link_err = buf_in;
1067 struct hinic_dev *nic_dev = handle;
1068
1069 if (link_err->err_type >= LINK_ERR_NUM)
1070 netif_info(nic_dev, link, nic_dev->netdev,
1071 "Link failed, Unknown error type: 0x%x\n",
1072 link_err->err_type);
1073 else
1074 nic_dev->module_unrecognized = true;
1075
1076 *out_size = sizeof(*link_err);
1077 link_err = buf_out;
1078 link_err->status = 0;
1079}
1080
1081static int set_features(struct hinic_dev *nic_dev,
1082 netdev_features_t pre_features,
1083 netdev_features_t features, bool force_change)
1084{
1085 netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
1086 u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
1087 netdev_features_t failed_features = 0;
1088 int ret = 0;
1089 int err = 0;
1090
1091 if (changed & NETIF_F_TSO) {
1092 ret = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
1093 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
1094 if (ret) {
1095 err = ret;
1096 failed_features |= NETIF_F_TSO;
1097 }
1098 }
1099
1100 if (changed & NETIF_F_RXCSUM) {
1101 ret = hinic_set_rx_csum_offload(nic_dev, csum_en);
1102 if (ret) {
1103 err = ret;
1104 failed_features |= NETIF_F_RXCSUM;
1105 }
1106 }
1107
1108 if (changed & NETIF_F_LRO) {
1109 ret = hinic_set_rx_lro_state(nic_dev,
1110 !!(features & NETIF_F_LRO),
1111 HINIC_LRO_RX_TIMER_DEFAULT,
1112 HINIC_LRO_MAX_WQE_NUM_DEFAULT);
1113 if (ret) {
1114 err = ret;
1115 failed_features |= NETIF_F_LRO;
1116 }
1117 }
1118
1119 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1120 ret = hinic_set_rx_vlan_offload(nic_dev,
1121 !!(features &
1122 NETIF_F_HW_VLAN_CTAG_RX));
1123 if (ret) {
1124 err = ret;
1125 failed_features |= NETIF_F_HW_VLAN_CTAG_RX;
1126 }
1127 }
1128
1129 if (err) {
1130 nic_dev->netdev->features = features ^ failed_features;
1131 return -EIO;
1132 }
1133
1134 return 0;
1135}
1136
1137static int hinic_init_intr_coalesce(struct hinic_dev *nic_dev)
1138{
1139 u64 size;
1140 u16 i;
1141
1142 size = sizeof(struct hinic_intr_coal_info) * nic_dev->max_qps;
1143 nic_dev->rx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1144 if (!nic_dev->rx_intr_coalesce)
1145 return -ENOMEM;
1146 nic_dev->tx_intr_coalesce = kzalloc(size, GFP_KERNEL);
1147 if (!nic_dev->tx_intr_coalesce) {
1148 kfree(nic_dev->rx_intr_coalesce);
1149 return -ENOMEM;
1150 }
1151
1152 for (i = 0; i < nic_dev->max_qps; i++) {
1153 nic_dev->rx_intr_coalesce[i].pending_limt =
1154 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1155 nic_dev->rx_intr_coalesce[i].coalesce_timer_cfg =
1156 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1157 nic_dev->rx_intr_coalesce[i].resend_timer_cfg =
1158 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1159 nic_dev->tx_intr_coalesce[i].pending_limt =
1160 HINIC_DEAULT_TXRX_MSIX_PENDING_LIMIT;
1161 nic_dev->tx_intr_coalesce[i].coalesce_timer_cfg =
1162 HINIC_DEAULT_TXRX_MSIX_COALESC_TIMER_CFG;
1163 nic_dev->tx_intr_coalesce[i].resend_timer_cfg =
1164 HINIC_DEAULT_TXRX_MSIX_RESEND_TIMER_CFG;
1165 }
1166
1167 return 0;
1168}
1169
1170static void hinic_free_intr_coalesce(struct hinic_dev *nic_dev)
1171{
1172 kfree(nic_dev->tx_intr_coalesce);
1173 kfree(nic_dev->rx_intr_coalesce);
1174}
1175
1176
1177
1178
1179
1180
1181
1182static int nic_dev_init(struct pci_dev *pdev)
1183{
1184 struct hinic_rx_mode_work *rx_mode_work;
1185 struct hinic_txq_stats *tx_stats;
1186 struct hinic_rxq_stats *rx_stats;
1187 struct hinic_dev *nic_dev;
1188 struct net_device *netdev;
1189 struct hinic_hwdev *hwdev;
1190 struct devlink *devlink;
1191 int err, num_qps;
1192
1193 devlink = hinic_devlink_alloc();
1194 if (!devlink) {
1195 dev_err(&pdev->dev, "Hinic devlink alloc failed\n");
1196 return -ENOMEM;
1197 }
1198
1199 hwdev = hinic_init_hwdev(pdev, devlink);
1200 if (IS_ERR(hwdev)) {
1201 dev_err(&pdev->dev, "Failed to initialize HW device\n");
1202 hinic_devlink_free(devlink);
1203 return PTR_ERR(hwdev);
1204 }
1205
1206 num_qps = hinic_hwdev_num_qps(hwdev);
1207 if (num_qps <= 0) {
1208 dev_err(&pdev->dev, "Invalid number of QPS\n");
1209 err = -EINVAL;
1210 goto err_num_qps;
1211 }
1212
1213 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
1214 if (!netdev) {
1215 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
1216 err = -ENOMEM;
1217 goto err_alloc_etherdev;
1218 }
1219
1220 if (!HINIC_IS_VF(hwdev->hwif))
1221 netdev->netdev_ops = &hinic_netdev_ops;
1222 else
1223 netdev->netdev_ops = &hinicvf_netdev_ops;
1224
1225 netdev->max_mtu = ETH_MAX_MTU;
1226
1227 nic_dev = netdev_priv(netdev);
1228 nic_dev->netdev = netdev;
1229 nic_dev->hwdev = hwdev;
1230 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
1231 nic_dev->flags = 0;
1232 nic_dev->txqs = NULL;
1233 nic_dev->rxqs = NULL;
1234 nic_dev->tx_weight = tx_weight;
1235 nic_dev->rx_weight = rx_weight;
1236 nic_dev->sq_depth = HINIC_SQ_DEPTH;
1237 nic_dev->rq_depth = HINIC_RQ_DEPTH;
1238 nic_dev->sriov_info.hwdev = hwdev;
1239 nic_dev->sriov_info.pdev = pdev;
1240 nic_dev->max_qps = num_qps;
1241 nic_dev->devlink = devlink;
1242
1243 hinic_set_ethtool_ops(netdev);
1244
1245 sema_init(&nic_dev->mgmt_lock, 1);
1246
1247 tx_stats = &nic_dev->tx_stats;
1248 rx_stats = &nic_dev->rx_stats;
1249
1250 u64_stats_init(&tx_stats->syncp);
1251 u64_stats_init(&rx_stats->syncp);
1252
1253 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
1254 VLAN_BITMAP_SIZE(nic_dev),
1255 GFP_KERNEL);
1256 if (!nic_dev->vlan_bitmap) {
1257 err = -ENOMEM;
1258 goto err_vlan_bitmap;
1259 }
1260
1261 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
1262 if (!nic_dev->workq) {
1263 err = -ENOMEM;
1264 goto err_workq;
1265 }
1266
1267 pci_set_drvdata(pdev, netdev);
1268
1269 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
1270 if (err) {
1271 dev_err(&pdev->dev, "Failed to get mac address\n");
1272 goto err_get_mac;
1273 }
1274
1275 if (!is_valid_ether_addr(netdev->dev_addr)) {
1276 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1277 dev_err(&pdev->dev, "Invalid MAC address\n");
1278 err = -EIO;
1279 goto err_add_mac;
1280 }
1281
1282 dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
1283 netdev->dev_addr);
1284 eth_hw_addr_random(netdev);
1285 }
1286
1287 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1288 if (err && err != HINIC_PF_SET_VF_ALREADY) {
1289 dev_err(&pdev->dev, "Failed to add mac\n");
1290 goto err_add_mac;
1291 }
1292
1293 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1294 if (err) {
1295 dev_err(&pdev->dev, "Failed to set mtu\n");
1296 goto err_set_mtu;
1297 }
1298
1299 rx_mode_work = &nic_dev->rx_mode_work;
1300 INIT_WORK(&rx_mode_work->work, set_rx_mode);
1301
1302 netdev_features_init(netdev);
1303
1304 netif_carrier_off(netdev);
1305
1306 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1307 nic_dev, link_status_event_handler);
1308 hinic_hwdev_cb_register(nic_dev->hwdev,
1309 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT,
1310 nic_dev, cable_plug_event);
1311 hinic_hwdev_cb_register(nic_dev->hwdev,
1312 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT,
1313 nic_dev, link_err_event);
1314
1315 err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1316 if (err)
1317 goto err_set_features;
1318
1319
1320 err = hinic_dcb_set_pfc(nic_dev->hwdev, 0, 0);
1321 if (err)
1322 goto err_set_pfc;
1323
1324 SET_NETDEV_DEV(netdev, &pdev->dev);
1325
1326 err = hinic_init_intr_coalesce(nic_dev);
1327 if (err) {
1328 dev_err(&pdev->dev, "Failed to init_intr_coalesce\n");
1329 goto err_init_intr;
1330 }
1331
1332 hinic_dbg_init(nic_dev);
1333
1334 hinic_func_tbl_dbgfs_init(nic_dev);
1335
1336 err = hinic_func_table_debug_add(nic_dev);
1337 if (err) {
1338 dev_err(&pdev->dev, "Failed to add func_table debug\n");
1339 goto err_add_func_table_dbg;
1340 }
1341
1342 err = register_netdev(netdev);
1343 if (err) {
1344 dev_err(&pdev->dev, "Failed to register netdev\n");
1345 goto err_reg_netdev;
1346 }
1347
1348 return 0;
1349
1350err_reg_netdev:
1351 hinic_func_table_debug_rem(nic_dev);
1352err_add_func_table_dbg:
1353 hinic_func_tbl_dbgfs_uninit(nic_dev);
1354 hinic_dbg_uninit(nic_dev);
1355 hinic_free_intr_coalesce(nic_dev);
1356err_init_intr:
1357err_set_pfc:
1358err_set_features:
1359 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1360 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1361 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1362 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1363 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1364 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1365 cancel_work_sync(&rx_mode_work->work);
1366
1367err_set_mtu:
1368 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1369err_add_mac:
1370err_get_mac:
1371 pci_set_drvdata(pdev, NULL);
1372 destroy_workqueue(nic_dev->workq);
1373err_workq:
1374err_vlan_bitmap:
1375 free_netdev(netdev);
1376
1377err_alloc_etherdev:
1378err_num_qps:
1379 hinic_free_hwdev(hwdev);
1380 hinic_devlink_free(devlink);
1381 return err;
1382}
1383
1384static int hinic_probe(struct pci_dev *pdev,
1385 const struct pci_device_id *id)
1386{
1387 int err = pci_enable_device(pdev);
1388
1389 if (err) {
1390 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1391 return err;
1392 }
1393
1394 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1395 if (err) {
1396 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1397 goto err_pci_regions;
1398 }
1399
1400 pci_set_master(pdev);
1401
1402 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1403 if (err) {
1404 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1405 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1406 if (err) {
1407 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1408 goto err_dma_mask;
1409 }
1410 }
1411
1412 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1413 if (err) {
1414 dev_warn(&pdev->dev,
1415 "Couldn't set 64-bit consistent DMA mask\n");
1416 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1417 if (err) {
1418 dev_err(&pdev->dev,
1419 "Failed to set consistent DMA mask\n");
1420 goto err_dma_consistent_mask;
1421 }
1422 }
1423
1424 err = nic_dev_init(pdev);
1425 if (err) {
1426 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1427 goto err_nic_dev_init;
1428 }
1429
1430 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1431 return 0;
1432
1433err_nic_dev_init:
1434err_dma_consistent_mask:
1435err_dma_mask:
1436 pci_release_regions(pdev);
1437
1438err_pci_regions:
1439 pci_disable_device(pdev);
1440 return err;
1441}
1442
1443#define HINIC_WAIT_SRIOV_CFG_TIMEOUT 15000
1444
1445static void wait_sriov_cfg_complete(struct hinic_dev *nic_dev)
1446{
1447 struct hinic_sriov_info *sriov_info = &nic_dev->sriov_info;
1448 u32 loop_cnt = 0;
1449
1450 set_bit(HINIC_FUNC_REMOVE, &sriov_info->state);
1451 usleep_range(9900, 10000);
1452
1453 while (loop_cnt < HINIC_WAIT_SRIOV_CFG_TIMEOUT) {
1454 if (!test_bit(HINIC_SRIOV_ENABLE, &sriov_info->state) &&
1455 !test_bit(HINIC_SRIOV_DISABLE, &sriov_info->state))
1456 return;
1457
1458 usleep_range(9900, 10000);
1459 loop_cnt++;
1460 }
1461}
1462
1463static void hinic_remove(struct pci_dev *pdev)
1464{
1465 struct net_device *netdev = pci_get_drvdata(pdev);
1466 struct hinic_dev *nic_dev = netdev_priv(netdev);
1467 struct devlink *devlink = nic_dev->devlink;
1468 struct hinic_rx_mode_work *rx_mode_work;
1469
1470 if (!HINIC_IS_VF(nic_dev->hwdev->hwif)) {
1471 wait_sriov_cfg_complete(nic_dev);
1472 hinic_pci_sriov_disable(pdev);
1473 }
1474
1475 unregister_netdev(netdev);
1476
1477 hinic_func_table_debug_rem(nic_dev);
1478
1479 hinic_func_tbl_dbgfs_uninit(nic_dev);
1480
1481 hinic_dbg_uninit(nic_dev);
1482
1483 hinic_free_intr_coalesce(nic_dev);
1484
1485 hinic_port_del_mac(nic_dev, netdev->dev_addr, 0);
1486
1487 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1488 HINIC_MGMT_MSG_CMD_LINK_ERR_EVENT);
1489 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1490 HINIC_MGMT_MSG_CMD_CABLE_PLUG_EVENT);
1491 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1492 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1493
1494 rx_mode_work = &nic_dev->rx_mode_work;
1495 cancel_work_sync(&rx_mode_work->work);
1496
1497 pci_set_drvdata(pdev, NULL);
1498
1499 destroy_workqueue(nic_dev->workq);
1500
1501 hinic_free_hwdev(nic_dev->hwdev);
1502
1503 free_netdev(netdev);
1504
1505 hinic_devlink_free(devlink);
1506
1507 pci_release_regions(pdev);
1508 pci_disable_device(pdev);
1509
1510 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1511}
1512
1513static void hinic_shutdown(struct pci_dev *pdev)
1514{
1515 pci_disable_device(pdev);
1516}
1517
1518static const struct pci_device_id hinic_pci_table[] = {
1519 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
1520 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
1521 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1522 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
1523 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_VF), 0},
1524 { 0, 0}
1525};
1526MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1527
1528static struct pci_driver hinic_driver = {
1529 .name = HINIC_DRV_NAME,
1530 .id_table = hinic_pci_table,
1531 .probe = hinic_probe,
1532 .remove = hinic_remove,
1533 .shutdown = hinic_shutdown,
1534 .sriov_configure = hinic_pci_sriov_configure,
1535};
1536
1537static int __init hinic_module_init(void)
1538{
1539 hinic_dbg_register_debugfs(HINIC_DRV_NAME);
1540 return pci_register_driver(&hinic_driver);
1541}
1542
1543static void __exit hinic_module_exit(void)
1544{
1545 pci_unregister_driver(&hinic_driver);
1546 hinic_dbg_unregister_debugfs();
1547}
1548
1549module_init(hinic_module_init);
1550module_exit(hinic_module_exit);
1551