]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/ethernet/huawei/hinic/hinic_main.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / drivers / net / ethernet / huawei / hinic / hinic_main.c
CommitLineData
2025cf9e 1// SPDX-License-Identifier: GPL-2.0-only
51ba902a
AK
2/*
3 * Huawei HiNIC PCI Express Linux driver
4 * Copyright(c) 2017 Huawei Technologies Co., Ltd
51ba902a
AK
5 */
6
25a3ba61 7#include <linux/kernel.h>
51ba902a 8#include <linux/module.h>
e2585ea7 9#include <linux/moduleparam.h>
51ba902a
AK
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>
25a3ba61
AK
16#include <linux/slab.h>
17#include <linux/if_vlan.h>
18#include <linux/semaphore.h>
c4d06d2d 19#include <linux/workqueue.h>
25a3ba61
AK
20#include <net/ip.h>
21#include <linux/bitops.h>
22#include <linux/bitmap.h>
c4d06d2d 23#include <linux/delay.h>
51ba902a
AK
24#include <linux/err.h>
25
c3e79baf 26#include "hinic_hw_qp.h"
51ba902a 27#include "hinic_hw_dev.h"
25a3ba61 28#include "hinic_port.h"
c3e79baf
AK
29#include "hinic_tx.h"
30#include "hinic_rx.h"
51ba902a
AK
31#include "hinic_dev.h"
32
33MODULE_AUTHOR("Huawei Technologies CO., Ltd");
34MODULE_DESCRIPTION("Huawei Intelligent NIC driver");
35MODULE_LICENSE("GPL");
36
00e57a6d
AK
37static unsigned int tx_weight = 64;
38module_param(tx_weight, uint, 0644);
39MODULE_PARM_DESC(tx_weight, "Number Tx packets for NAPI budget (default=64)");
40
e2585ea7
AK
41static unsigned int rx_weight = 64;
42module_param(rx_weight, uint, 0644);
43MODULE_PARM_DESC(rx_weight, "Number Rx packets for NAPI budget (default=64)");
44
6679cf09
XC
45#define HINIC_DEV_ID_QUAD_PORT_25GE 0x1822
46#define HINIC_DEV_ID_DUAL_PORT_100GE 0x0200
47#define HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ 0x0205
48#define HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ 0x0210
51ba902a 49
c4d06d2d
AK
50#define HINIC_WQ_NAME "hinic_dev"
51
51ba902a
AK
52#define MSG_ENABLE_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
53 NETIF_MSG_IFUP | \
54 NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR)
55
1e007181
XC
56#define HINIC_LRO_MAX_WQE_NUM_DEFAULT 8
57
58#define HINIC_LRO_RX_TIMER_DEFAULT 16
59
25a3ba61
AK
60#define VLAN_BITMAP_SIZE(nic_dev) (ALIGN(VLAN_N_VID, 8) / 8)
61
c4d06d2d
AK
62#define work_to_rx_mode_work(work) \
63 container_of(work, struct hinic_rx_mode_work, work)
64
65#define rx_mode_work_to_nic_dev(rx_mode_work) \
66 container_of(rx_mode_work, struct hinic_dev, rx_mode_work)
67
68static int change_mac_addr(struct net_device *netdev, const u8 *addr);
69
1e007181
XC
70static int set_features(struct hinic_dev *nic_dev,
71 netdev_features_t pre_features,
72 netdev_features_t features, bool force_change);
73
edd384f6
AK
74static void update_rx_stats(struct hinic_dev *nic_dev, struct hinic_rxq *rxq)
75{
76 struct hinic_rxq_stats *nic_rx_stats = &nic_dev->rx_stats;
77 struct hinic_rxq_stats rx_stats;
78
79 u64_stats_init(&rx_stats.syncp);
80
81 hinic_rxq_get_stats(rxq, &rx_stats);
82
83 u64_stats_update_begin(&nic_rx_stats->syncp);
84 nic_rx_stats->bytes += rx_stats.bytes;
85 nic_rx_stats->pkts += rx_stats.pkts;
e54fbbdf
XC
86 nic_rx_stats->errors += rx_stats.errors;
87 nic_rx_stats->csum_errors += rx_stats.csum_errors;
88 nic_rx_stats->other_errors += rx_stats.other_errors;
edd384f6
AK
89 u64_stats_update_end(&nic_rx_stats->syncp);
90
91 hinic_rxq_clean_stats(rxq);
92}
93
94static void update_tx_stats(struct hinic_dev *nic_dev, struct hinic_txq *txq)
95{
96 struct hinic_txq_stats *nic_tx_stats = &nic_dev->tx_stats;
97 struct hinic_txq_stats tx_stats;
98
99 u64_stats_init(&tx_stats.syncp);
100
101 hinic_txq_get_stats(txq, &tx_stats);
102
103 u64_stats_update_begin(&nic_tx_stats->syncp);
104 nic_tx_stats->bytes += tx_stats.bytes;
105 nic_tx_stats->pkts += tx_stats.pkts;
106 nic_tx_stats->tx_busy += tx_stats.tx_busy;
107 nic_tx_stats->tx_wake += tx_stats.tx_wake;
108 nic_tx_stats->tx_dropped += tx_stats.tx_dropped;
e54fbbdf 109 nic_tx_stats->big_frags_pkts += tx_stats.big_frags_pkts;
edd384f6
AK
110 u64_stats_update_end(&nic_tx_stats->syncp);
111
112 hinic_txq_clean_stats(txq);
113}
114
115static void update_nic_stats(struct hinic_dev *nic_dev)
116{
117 int i, num_qps = hinic_hwdev_num_qps(nic_dev->hwdev);
118
119 for (i = 0; i < num_qps; i++)
120 update_rx_stats(nic_dev, &nic_dev->rxqs[i]);
121
122 for (i = 0; i < num_qps; i++)
123 update_tx_stats(nic_dev, &nic_dev->txqs[i]);
124}
125
c3e79baf
AK
126/**
127 * create_txqs - Create the Logical Tx Queues of specific NIC device
128 * @nic_dev: the specific NIC device
129 *
130 * Return 0 - Success, negative - Failure
131 **/
132static int create_txqs(struct hinic_dev *nic_dev)
133{
134 int err, i, j, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
135 struct net_device *netdev = nic_dev->netdev;
136 size_t txq_size;
137
138 if (nic_dev->txqs)
139 return -EINVAL;
140
141 txq_size = num_txqs * sizeof(*nic_dev->txqs);
142 nic_dev->txqs = devm_kzalloc(&netdev->dev, txq_size, GFP_KERNEL);
143 if (!nic_dev->txqs)
144 return -ENOMEM;
145
146 for (i = 0; i < num_txqs; i++) {
147 struct hinic_sq *sq = hinic_hwdev_get_sq(nic_dev->hwdev, i);
148
149 err = hinic_init_txq(&nic_dev->txqs[i], sq, netdev);
150 if (err) {
151 netif_err(nic_dev, drv, netdev,
152 "Failed to init Txq\n");
153 goto err_init_txq;
154 }
155 }
156
157 return 0;
158
159err_init_txq:
160 for (j = 0; j < i; j++)
161 hinic_clean_txq(&nic_dev->txqs[j]);
162
163 devm_kfree(&netdev->dev, nic_dev->txqs);
164 return err;
165}
166
167/**
168 * free_txqs - Free the Logical Tx Queues of specific NIC device
169 * @nic_dev: the specific NIC device
170 **/
171static void free_txqs(struct hinic_dev *nic_dev)
172{
173 int i, num_txqs = hinic_hwdev_num_qps(nic_dev->hwdev);
174 struct net_device *netdev = nic_dev->netdev;
175
176 if (!nic_dev->txqs)
177 return;
178
179 for (i = 0; i < num_txqs; i++)
180 hinic_clean_txq(&nic_dev->txqs[i]);
181
182 devm_kfree(&netdev->dev, nic_dev->txqs);
183 nic_dev->txqs = NULL;
184}
185
186/**
187 * create_txqs - Create the Logical Rx Queues of specific NIC device
188 * @nic_dev: the specific NIC device
189 *
190 * Return 0 - Success, negative - Failure
191 **/
192static int create_rxqs(struct hinic_dev *nic_dev)
193{
194 int err, i, j, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
195 struct net_device *netdev = nic_dev->netdev;
196 size_t rxq_size;
197
198 if (nic_dev->rxqs)
199 return -EINVAL;
200
201 rxq_size = num_rxqs * sizeof(*nic_dev->rxqs);
202 nic_dev->rxqs = devm_kzalloc(&netdev->dev, rxq_size, GFP_KERNEL);
203 if (!nic_dev->rxqs)
204 return -ENOMEM;
205
206 for (i = 0; i < num_rxqs; i++) {
207 struct hinic_rq *rq = hinic_hwdev_get_rq(nic_dev->hwdev, i);
208
209 err = hinic_init_rxq(&nic_dev->rxqs[i], rq, netdev);
210 if (err) {
211 netif_err(nic_dev, drv, netdev,
212 "Failed to init rxq\n");
213 goto err_init_rxq;
214 }
215 }
216
217 return 0;
218
219err_init_rxq:
220 for (j = 0; j < i; j++)
221 hinic_clean_rxq(&nic_dev->rxqs[j]);
222
223 devm_kfree(&netdev->dev, nic_dev->rxqs);
224 return err;
225}
226
227/**
228 * free_txqs - Free the Logical Rx Queues of specific NIC device
229 * @nic_dev: the specific NIC device
230 **/
231static void free_rxqs(struct hinic_dev *nic_dev)
232{
233 int i, num_rxqs = hinic_hwdev_num_qps(nic_dev->hwdev);
234 struct net_device *netdev = nic_dev->netdev;
235
236 if (!nic_dev->rxqs)
237 return;
238
239 for (i = 0; i < num_rxqs; i++)
240 hinic_clean_rxq(&nic_dev->rxqs[i]);
241
242 devm_kfree(&netdev->dev, nic_dev->rxqs);
243 nic_dev->rxqs = NULL;
244}
245
1e007181
XC
246static int hinic_configure_max_qnum(struct hinic_dev *nic_dev)
247{
248 int err;
249
250 err = hinic_set_max_qnum(nic_dev, nic_dev->hwdev->nic_cap.max_qps);
251 if (err)
252 return err;
253
254 return 0;
255}
256
421e9526
XC
257static int hinic_rss_init(struct hinic_dev *nic_dev)
258{
421e9526
XC
259 u8 default_rss_key[HINIC_RSS_KEY_SIZE];
260 u8 tmpl_idx = nic_dev->rss_tmpl_idx;
f7110b75 261 u32 *indir_tbl;
421e9526
XC
262 int err, i;
263
f7110b75
AB
264 indir_tbl = kcalloc(HINIC_RSS_INDIR_SIZE, sizeof(u32), GFP_KERNEL);
265 if (!indir_tbl)
266 return -ENOMEM;
267
421e9526
XC
268 netdev_rss_key_fill(default_rss_key, sizeof(default_rss_key));
269 for (i = 0; i < HINIC_RSS_INDIR_SIZE; i++)
270 indir_tbl[i] = ethtool_rxfh_indir_default(i, nic_dev->num_rss);
271
272 err = hinic_rss_set_template_tbl(nic_dev, tmpl_idx, default_rss_key);
273 if (err)
f7110b75 274 goto out;
421e9526
XC
275
276 err = hinic_rss_set_indir_tbl(nic_dev, tmpl_idx, indir_tbl);
277 if (err)
f7110b75 278 goto out;
421e9526
XC
279
280 err = hinic_set_rss_type(nic_dev, tmpl_idx, nic_dev->rss_type);
281 if (err)
f7110b75 282 goto out;
421e9526
XC
283
284 err = hinic_rss_set_hash_engine(nic_dev, tmpl_idx,
285 nic_dev->rss_hash_engine);
286 if (err)
f7110b75 287 goto out;
421e9526
XC
288
289 err = hinic_rss_cfg(nic_dev, 1, tmpl_idx);
290 if (err)
f7110b75 291 goto out;
421e9526 292
f7110b75
AB
293out:
294 kfree(indir_tbl);
295 return err;
421e9526
XC
296}
297
298static void hinic_rss_deinit(struct hinic_dev *nic_dev)
299{
300 hinic_rss_cfg(nic_dev, 0, nic_dev->rss_tmpl_idx);
301}
302
303static void hinic_init_rss_parameters(struct hinic_dev *nic_dev)
304{
305 nic_dev->rss_hash_engine = HINIC_RSS_HASH_ENGINE_TYPE_XOR;
306 nic_dev->rss_type.tcp_ipv6_ext = 1;
307 nic_dev->rss_type.ipv6_ext = 1;
308 nic_dev->rss_type.tcp_ipv6 = 1;
309 nic_dev->rss_type.ipv6 = 1;
310 nic_dev->rss_type.tcp_ipv4 = 1;
311 nic_dev->rss_type.ipv4 = 1;
312 nic_dev->rss_type.udp_ipv6 = 1;
313 nic_dev->rss_type.udp_ipv4 = 1;
314}
315
316static void hinic_enable_rss(struct hinic_dev *nic_dev)
317{
318 struct net_device *netdev = nic_dev->netdev;
319 struct hinic_hwdev *hwdev = nic_dev->hwdev;
320 struct hinic_hwif *hwif = hwdev->hwif;
321 struct pci_dev *pdev = hwif->pdev;
322 int i, node, err = 0;
323 u16 num_cpus = 0;
324
325 nic_dev->max_qps = hinic_hwdev_max_num_qps(hwdev);
326 if (nic_dev->max_qps <= 1) {
327 nic_dev->flags &= ~HINIC_RSS_ENABLE;
328 nic_dev->rss_limit = nic_dev->max_qps;
329 nic_dev->num_qps = nic_dev->max_qps;
330 nic_dev->num_rss = nic_dev->max_qps;
331
332 return;
333 }
334
335 err = hinic_rss_template_alloc(nic_dev, &nic_dev->rss_tmpl_idx);
336 if (err) {
337 netif_err(nic_dev, drv, netdev,
338 "Failed to alloc tmpl_idx for rss, can't enable rss for this function\n");
339 nic_dev->flags &= ~HINIC_RSS_ENABLE;
340 nic_dev->max_qps = 1;
341 nic_dev->rss_limit = nic_dev->max_qps;
342 nic_dev->num_qps = nic_dev->max_qps;
343 nic_dev->num_rss = nic_dev->max_qps;
344
345 return;
346 }
347
348 nic_dev->flags |= HINIC_RSS_ENABLE;
349
350 for (i = 0; i < num_online_cpus(); i++) {
351 node = cpu_to_node(i);
352 if (node == dev_to_node(&pdev->dev))
353 num_cpus++;
354 }
355
356 if (!num_cpus)
357 num_cpus = num_online_cpus();
358
386d4716
L
359 nic_dev->num_qps = hinic_hwdev_num_qps(hwdev);
360 nic_dev->num_qps = min_t(u16, nic_dev->num_qps, num_cpus);
421e9526
XC
361
362 nic_dev->rss_limit = nic_dev->num_qps;
363 nic_dev->num_rss = nic_dev->num_qps;
364
365 hinic_init_rss_parameters(nic_dev);
366 err = hinic_rss_init(nic_dev);
367 if (err)
368 netif_err(nic_dev, drv, netdev, "Failed to init rss\n");
369}
370
c4d06d2d
AK
371static int hinic_open(struct net_device *netdev)
372{
373 struct hinic_dev *nic_dev = netdev_priv(netdev);
374 enum hinic_port_link_state link_state;
421e9526 375 int err, ret;
c3e79baf
AK
376
377 if (!(nic_dev->flags & HINIC_INTF_UP)) {
378 err = hinic_hwdev_ifup(nic_dev->hwdev);
379 if (err) {
380 netif_err(nic_dev, drv, netdev,
381 "Failed - HW interface up\n");
382 return err;
383 }
384 }
385
386 err = create_txqs(nic_dev);
387 if (err) {
388 netif_err(nic_dev, drv, netdev,
389 "Failed to create Tx queues\n");
390 goto err_create_txqs;
391 }
392
393 err = create_rxqs(nic_dev);
394 if (err) {
395 netif_err(nic_dev, drv, netdev,
396 "Failed to create Rx queues\n");
397 goto err_create_rxqs;
398 }
399
421e9526
XC
400 hinic_enable_rss(nic_dev);
401
1e007181
XC
402 err = hinic_configure_max_qnum(nic_dev);
403 if (err) {
404 netif_err(nic_dev, drv, nic_dev->netdev,
405 "Failed to configure the maximum number of queues\n");
406 goto err_port_state;
407 }
408
421e9526
XC
409 netif_set_real_num_tx_queues(netdev, nic_dev->num_qps);
410 netif_set_real_num_rx_queues(netdev, nic_dev->num_qps);
c4d06d2d
AK
411
412 err = hinic_port_set_state(nic_dev, HINIC_PORT_ENABLE);
413 if (err) {
414 netif_err(nic_dev, drv, netdev,
415 "Failed to set port state\n");
c3e79baf 416 goto err_port_state;
c4d06d2d
AK
417 }
418
e2585ea7
AK
419 err = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_ENABLE);
420 if (err) {
421 netif_err(nic_dev, drv, netdev,
422 "Failed to set func port state\n");
423 goto err_func_port_state;
424 }
425
c4d06d2d
AK
426 /* Wait up to 3 sec between port enable to link state */
427 msleep(3000);
428
429 down(&nic_dev->mgmt_lock);
430
431 err = hinic_port_link_state(nic_dev, &link_state);
432 if (err) {
433 netif_err(nic_dev, drv, netdev, "Failed to get link state\n");
434 goto err_port_link;
435 }
436
437 if (link_state == HINIC_LINK_STATE_UP)
438 nic_dev->flags |= HINIC_LINK_UP;
439
440 nic_dev->flags |= HINIC_INTF_UP;
441
442 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
443 (HINIC_LINK_UP | HINIC_INTF_UP)) {
444 netif_info(nic_dev, drv, netdev, "link + intf UP\n");
445 netif_carrier_on(netdev);
446 netif_tx_wake_all_queues(netdev);
447 }
448
449 up(&nic_dev->mgmt_lock);
450
451 netif_info(nic_dev, drv, netdev, "HINIC_INTF is UP\n");
452 return 0;
453
454err_port_link:
455 up(&nic_dev->mgmt_lock);
e2585ea7
AK
456 ret = hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
457 if (ret)
458 netif_warn(nic_dev, drv, netdev,
459 "Failed to revert func port state\n");
460
461err_func_port_state:
c4d06d2d
AK
462 ret = hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
463 if (ret)
464 netif_warn(nic_dev, drv, netdev,
465 "Failed to revert port state\n");
c3e79baf
AK
466err_port_state:
467 free_rxqs(nic_dev);
421e9526
XC
468 if (nic_dev->flags & HINIC_RSS_ENABLE) {
469 hinic_rss_deinit(nic_dev);
470 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
471 }
c3e79baf
AK
472
473err_create_rxqs:
474 free_txqs(nic_dev);
475
476err_create_txqs:
477 if (!(nic_dev->flags & HINIC_INTF_UP))
478 hinic_hwdev_ifdown(nic_dev->hwdev);
c4d06d2d
AK
479 return err;
480}
481
482static int hinic_close(struct net_device *netdev)
483{
484 struct hinic_dev *nic_dev = netdev_priv(netdev);
485 unsigned int flags;
c4d06d2d
AK
486
487 down(&nic_dev->mgmt_lock);
488
489 flags = nic_dev->flags;
490 nic_dev->flags &= ~HINIC_INTF_UP;
491
492 netif_carrier_off(netdev);
493 netif_tx_disable(netdev);
494
edd384f6
AK
495 update_nic_stats(nic_dev);
496
c4d06d2d
AK
497 up(&nic_dev->mgmt_lock);
498
e8a1b0ef 499 hinic_port_set_state(nic_dev, HINIC_PORT_DISABLE);
e2585ea7 500
e8a1b0ef 501 hinic_port_set_func_state(nic_dev, HINIC_FUNC_PORT_DISABLE);
c4d06d2d 502
421e9526
XC
503 if (nic_dev->flags & HINIC_RSS_ENABLE) {
504 hinic_rss_deinit(nic_dev);
505 hinic_rss_template_free(nic_dev, nic_dev->rss_tmpl_idx);
506 }
507
c3e79baf
AK
508 free_rxqs(nic_dev);
509 free_txqs(nic_dev);
510
511 if (flags & HINIC_INTF_UP)
512 hinic_hwdev_ifdown(nic_dev->hwdev);
513
c4d06d2d
AK
514 netif_info(nic_dev, drv, netdev, "HINIC_INTF is DOWN\n");
515 return 0;
516}
517
25a3ba61
AK
518static int hinic_change_mtu(struct net_device *netdev, int new_mtu)
519{
520 struct hinic_dev *nic_dev = netdev_priv(netdev);
521 int err;
522
523 netif_info(nic_dev, drv, netdev, "set_mtu = %d\n", new_mtu);
524
525 err = hinic_port_set_mtu(nic_dev, new_mtu);
526 if (err)
527 netif_err(nic_dev, drv, netdev, "Failed to set port mtu\n");
528 else
529 netdev->mtu = new_mtu;
530
531 return err;
532}
533
534/**
535 * change_mac_addr - change the main mac address of network device
536 * @netdev: network device
537 * @addr: mac address to set
538 *
539 * Return 0 - Success, negative - Failure
540 **/
541static int change_mac_addr(struct net_device *netdev, const u8 *addr)
542{
543 struct hinic_dev *nic_dev = netdev_priv(netdev);
544 u16 vid = 0;
545 int err;
546
547 if (!is_valid_ether_addr(addr))
548 return -EADDRNOTAVAIL;
549
550 netif_info(nic_dev, drv, netdev, "change mac addr = %02x %02x %02x %02x %02x %02x\n",
551 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
552
553 down(&nic_dev->mgmt_lock);
554
555 do {
556 err = hinic_port_del_mac(nic_dev, netdev->dev_addr, vid);
557 if (err) {
558 netif_err(nic_dev, drv, netdev,
559 "Failed to delete mac\n");
560 break;
561 }
562
563 err = hinic_port_add_mac(nic_dev, addr, vid);
564 if (err) {
565 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
566 break;
567 }
568
569 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
570 } while (vid != VLAN_N_VID);
571
572 up(&nic_dev->mgmt_lock);
573 return err;
574}
575
576static int hinic_set_mac_addr(struct net_device *netdev, void *addr)
577{
578 unsigned char new_mac[ETH_ALEN];
579 struct sockaddr *saddr = addr;
580 int err;
581
582 memcpy(new_mac, saddr->sa_data, ETH_ALEN);
583
584 err = change_mac_addr(netdev, new_mac);
585 if (!err)
586 memcpy(netdev->dev_addr, new_mac, ETH_ALEN);
587
588 return err;
589}
590
c4d06d2d
AK
591/**
592 * add_mac_addr - add mac address to network device
593 * @netdev: network device
594 * @addr: mac address to add
595 *
596 * Return 0 - Success, negative - Failure
597 **/
598static int add_mac_addr(struct net_device *netdev, const u8 *addr)
599{
600 struct hinic_dev *nic_dev = netdev_priv(netdev);
601 u16 vid = 0;
602 int err;
603
c4d06d2d
AK
604 netif_info(nic_dev, drv, netdev, "set mac addr = %02x %02x %02x %02x %02x %02x\n",
605 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
606
607 down(&nic_dev->mgmt_lock);
608
609 do {
610 err = hinic_port_add_mac(nic_dev, addr, vid);
611 if (err) {
612 netif_err(nic_dev, drv, netdev, "Failed to add mac\n");
613 break;
614 }
615
616 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
617 } while (vid != VLAN_N_VID);
618
619 up(&nic_dev->mgmt_lock);
620 return err;
621}
622
623/**
624 * remove_mac_addr - remove mac address from network device
625 * @netdev: network device
626 * @addr: mac address to remove
627 *
628 * Return 0 - Success, negative - Failure
629 **/
630static int remove_mac_addr(struct net_device *netdev, const u8 *addr)
631{
632 struct hinic_dev *nic_dev = netdev_priv(netdev);
633 u16 vid = 0;
634 int err;
635
636 if (!is_valid_ether_addr(addr))
637 return -EADDRNOTAVAIL;
638
639 netif_info(nic_dev, drv, netdev, "remove mac addr = %02x %02x %02x %02x %02x %02x\n",
640 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
641
642 down(&nic_dev->mgmt_lock);
643
644 do {
645 err = hinic_port_del_mac(nic_dev, addr, vid);
646 if (err) {
647 netif_err(nic_dev, drv, netdev,
648 "Failed to delete mac\n");
649 break;
650 }
651
652 vid = find_next_bit(nic_dev->vlan_bitmap, VLAN_N_VID, vid + 1);
653 } while (vid != VLAN_N_VID);
654
655 up(&nic_dev->mgmt_lock);
656 return err;
657}
658
25a3ba61
AK
659static int hinic_vlan_rx_add_vid(struct net_device *netdev,
660 __always_unused __be16 proto, u16 vid)
661{
662 struct hinic_dev *nic_dev = netdev_priv(netdev);
663 int ret, err;
664
665 netif_info(nic_dev, drv, netdev, "add vid = %d\n", vid);
666
667 down(&nic_dev->mgmt_lock);
668
669 err = hinic_port_add_vlan(nic_dev, vid);
670 if (err) {
671 netif_err(nic_dev, drv, netdev, "Failed to add vlan\n");
672 goto err_vlan_add;
673 }
674
675 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, vid);
676 if (err) {
677 netif_err(nic_dev, drv, netdev, "Failed to set mac\n");
678 goto err_add_mac;
679 }
680
681 bitmap_set(nic_dev->vlan_bitmap, vid, 1);
682
683 up(&nic_dev->mgmt_lock);
684 return 0;
685
686err_add_mac:
687 ret = hinic_port_del_vlan(nic_dev, vid);
688 if (ret)
689 netif_err(nic_dev, drv, netdev,
690 "Failed to revert by removing vlan\n");
691
692err_vlan_add:
693 up(&nic_dev->mgmt_lock);
694 return err;
695}
696
697static int hinic_vlan_rx_kill_vid(struct net_device *netdev,
698 __always_unused __be16 proto, u16 vid)
699{
700 struct hinic_dev *nic_dev = netdev_priv(netdev);
701 int err;
702
703 netif_info(nic_dev, drv, netdev, "remove vid = %d\n", vid);
704
705 down(&nic_dev->mgmt_lock);
706
707 err = hinic_port_del_vlan(nic_dev, vid);
708 if (err) {
709 netif_err(nic_dev, drv, netdev, "Failed to delete vlan\n");
710 goto err_del_vlan;
711 }
712
713 bitmap_clear(nic_dev->vlan_bitmap, vid, 1);
714
715 up(&nic_dev->mgmt_lock);
716 return 0;
717
718err_del_vlan:
719 up(&nic_dev->mgmt_lock);
720 return err;
721}
722
c4d06d2d
AK
723static void set_rx_mode(struct work_struct *work)
724{
725 struct hinic_rx_mode_work *rx_mode_work = work_to_rx_mode_work(work);
726 struct hinic_dev *nic_dev = rx_mode_work_to_nic_dev(rx_mode_work);
727
728 netif_info(nic_dev, drv, nic_dev->netdev, "set rx mode work\n");
729
730 hinic_port_set_rx_mode(nic_dev, rx_mode_work->rx_mode);
731
732 __dev_uc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
733 __dev_mc_sync(nic_dev->netdev, add_mac_addr, remove_mac_addr);
734}
735
736static void hinic_set_rx_mode(struct net_device *netdev)
737{
738 struct hinic_dev *nic_dev = netdev_priv(netdev);
739 struct hinic_rx_mode_work *rx_mode_work;
740 u32 rx_mode;
741
742 rx_mode_work = &nic_dev->rx_mode_work;
743
744 rx_mode = HINIC_RX_MODE_UC |
745 HINIC_RX_MODE_MC |
746 HINIC_RX_MODE_BC;
747
748 if (netdev->flags & IFF_PROMISC)
749 rx_mode |= HINIC_RX_MODE_PROMISC;
750 else if (netdev->flags & IFF_ALLMULTI)
751 rx_mode |= HINIC_RX_MODE_MC_ALL;
752
753 rx_mode_work->rx_mode = rx_mode;
754
755 queue_work(nic_dev->workq, &rx_mode_work->work);
756}
757
0290bd29 758static void hinic_tx_timeout(struct net_device *netdev, unsigned int txqueue)
c4d06d2d 759{
00e57a6d
AK
760 struct hinic_dev *nic_dev = netdev_priv(netdev);
761
762 netif_err(nic_dev, drv, netdev, "Tx timeout\n");
c4d06d2d
AK
763}
764
edd384f6
AK
765static void hinic_get_stats64(struct net_device *netdev,
766 struct rtnl_link_stats64 *stats)
767{
768 struct hinic_dev *nic_dev = netdev_priv(netdev);
769 struct hinic_rxq_stats *nic_rx_stats;
770 struct hinic_txq_stats *nic_tx_stats;
771
772 nic_rx_stats = &nic_dev->rx_stats;
773 nic_tx_stats = &nic_dev->tx_stats;
774
775 down(&nic_dev->mgmt_lock);
776
777 if (nic_dev->flags & HINIC_INTF_UP)
778 update_nic_stats(nic_dev);
779
780 up(&nic_dev->mgmt_lock);
781
782 stats->rx_bytes = nic_rx_stats->bytes;
783 stats->rx_packets = nic_rx_stats->pkts;
e54fbbdf 784 stats->rx_errors = nic_rx_stats->errors;
edd384f6
AK
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
1e007181
XC
791static int hinic_set_features(struct net_device *netdev,
792 netdev_features_t features)
793{
794 struct hinic_dev *nic_dev = netdev_priv(netdev);
795
796 return set_features(nic_dev, nic_dev->netdev->features,
797 features, false);
798}
799
800static netdev_features_t hinic_fix_features(struct net_device *netdev,
801 netdev_features_t features)
802{
803 struct hinic_dev *nic_dev = netdev_priv(netdev);
804
805 /* If Rx checksum is disabled, then LRO should also be disabled */
806 if (!(features & NETIF_F_RXCSUM)) {
807 netif_info(nic_dev, drv, netdev, "disabling LRO as RXCSUM is off\n");
808 features &= ~NETIF_F_LRO;
809 }
810
811 return features;
812}
813
51ba902a 814static const struct net_device_ops hinic_netdev_ops = {
c4d06d2d
AK
815 .ndo_open = hinic_open,
816 .ndo_stop = hinic_close,
25a3ba61
AK
817 .ndo_change_mtu = hinic_change_mtu,
818 .ndo_set_mac_address = hinic_set_mac_addr,
819 .ndo_validate_addr = eth_validate_addr,
820 .ndo_vlan_rx_add_vid = hinic_vlan_rx_add_vid,
821 .ndo_vlan_rx_kill_vid = hinic_vlan_rx_kill_vid,
c4d06d2d
AK
822 .ndo_set_rx_mode = hinic_set_rx_mode,
823 .ndo_start_xmit = hinic_xmit_frame,
00e57a6d 824 .ndo_tx_timeout = hinic_tx_timeout,
edd384f6 825 .ndo_get_stats64 = hinic_get_stats64,
1e007181
XC
826 .ndo_fix_features = hinic_fix_features,
827 .ndo_set_features = hinic_set_features,
51ba902a
AK
828};
829
25a3ba61
AK
830static void netdev_features_init(struct net_device *netdev)
831{
cc18a754 832 netdev->hw_features = NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_IP_CSUM |
4a61abb1 833 NETIF_F_IPV6_CSUM | NETIF_F_TSO | NETIF_F_TSO6 |
aebd17b7
XC
834 NETIF_F_RXCSUM | NETIF_F_LRO |
835 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
25a3ba61
AK
836
837 netdev->vlan_features = netdev->hw_features;
838
839 netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
840}
841
c4d06d2d
AK
842/**
843 * link_status_event_handler - link event handler
844 * @handle: nic device for the handler
845 * @buf_in: input buffer
846 * @in_size: input size
847 * @buf_in: output buffer
848 * @out_size: returned output size
849 *
850 * Return 0 - Success, negative - Failure
851 **/
852static void link_status_event_handler(void *handle, void *buf_in, u16 in_size,
853 void *buf_out, u16 *out_size)
854{
855 struct hinic_port_link_status *link_status, *ret_link_status;
856 struct hinic_dev *nic_dev = handle;
857
858 link_status = buf_in;
859
860 if (link_status->link == HINIC_LINK_STATE_UP) {
861 down(&nic_dev->mgmt_lock);
862
863 nic_dev->flags |= HINIC_LINK_UP;
864
865 if ((nic_dev->flags & (HINIC_LINK_UP | HINIC_INTF_UP)) ==
866 (HINIC_LINK_UP | HINIC_INTF_UP)) {
867 netif_carrier_on(nic_dev->netdev);
868 netif_tx_wake_all_queues(nic_dev->netdev);
869 }
870
871 up(&nic_dev->mgmt_lock);
872
873 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is UP\n");
874 } else {
875 down(&nic_dev->mgmt_lock);
876
877 nic_dev->flags &= ~HINIC_LINK_UP;
878
879 netif_carrier_off(nic_dev->netdev);
880 netif_tx_disable(nic_dev->netdev);
881
882 up(&nic_dev->mgmt_lock);
883
884 netif_info(nic_dev, drv, nic_dev->netdev, "HINIC_Link is DOWN\n");
885 }
886
887 ret_link_status = buf_out;
888 ret_link_status->status = 0;
889
890 *out_size = sizeof(*ret_link_status);
891}
892
cc18a754
ZC
893static int set_features(struct hinic_dev *nic_dev,
894 netdev_features_t pre_features,
895 netdev_features_t features, bool force_change)
896{
897 netdev_features_t changed = force_change ? ~0 : pre_features ^ features;
4a61abb1 898 u32 csum_en = HINIC_RX_CSUM_OFFLOAD_EN;
cc18a754
ZC
899 int err = 0;
900
901 if (changed & NETIF_F_TSO)
902 err = hinic_port_set_tso(nic_dev, (features & NETIF_F_TSO) ?
903 HINIC_TSO_ENABLE : HINIC_TSO_DISABLE);
904
4a61abb1
XC
905 if (changed & NETIF_F_RXCSUM)
906 err = hinic_set_rx_csum_offload(nic_dev, csum_en);
907
1e007181
XC
908 if (changed & NETIF_F_LRO) {
909 err = hinic_set_rx_lro_state(nic_dev,
910 !!(features & NETIF_F_LRO),
911 HINIC_LRO_RX_TIMER_DEFAULT,
912 HINIC_LRO_MAX_WQE_NUM_DEFAULT);
913 }
914
aebd17b7
XC
915 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
916 err = hinic_set_rx_vlan_offload(nic_dev,
917 !!(features &
918 NETIF_F_HW_VLAN_CTAG_RX));
919
cc18a754
ZC
920 return err;
921}
922
51ba902a
AK
923/**
924 * nic_dev_init - Initialize the NIC device
925 * @pdev: the NIC pci device
926 *
927 * Return 0 - Success, negative - Failure
928 **/
929static int nic_dev_init(struct pci_dev *pdev)
930{
c4d06d2d 931 struct hinic_rx_mode_work *rx_mode_work;
edd384f6
AK
932 struct hinic_txq_stats *tx_stats;
933 struct hinic_rxq_stats *rx_stats;
51ba902a
AK
934 struct hinic_dev *nic_dev;
935 struct net_device *netdev;
936 struct hinic_hwdev *hwdev;
937 int err, num_qps;
938
939 hwdev = hinic_init_hwdev(pdev);
940 if (IS_ERR(hwdev)) {
941 dev_err(&pdev->dev, "Failed to initialize HW device\n");
942 return PTR_ERR(hwdev);
943 }
944
945 num_qps = hinic_hwdev_num_qps(hwdev);
946 if (num_qps <= 0) {
947 dev_err(&pdev->dev, "Invalid number of QPS\n");
948 err = -EINVAL;
949 goto err_num_qps;
950 }
951
952 netdev = alloc_etherdev_mq(sizeof(*nic_dev), num_qps);
953 if (!netdev) {
954 dev_err(&pdev->dev, "Failed to allocate Ethernet device\n");
955 err = -ENOMEM;
956 goto err_alloc_etherdev;
957 }
958
eb8ce9ac 959 hinic_set_ethtool_ops(netdev);
51ba902a 960 netdev->netdev_ops = &hinic_netdev_ops;
52f31422 961 netdev->max_mtu = ETH_MAX_MTU;
51ba902a
AK
962
963 nic_dev = netdev_priv(netdev);
964 nic_dev->netdev = netdev;
965 nic_dev->hwdev = hwdev;
966 nic_dev->msg_enable = MSG_ENABLE_DEFAULT;
c4d06d2d 967 nic_dev->flags = 0;
c3e79baf
AK
968 nic_dev->txqs = NULL;
969 nic_dev->rxqs = NULL;
00e57a6d 970 nic_dev->tx_weight = tx_weight;
e2585ea7 971 nic_dev->rx_weight = rx_weight;
51ba902a 972
25a3ba61
AK
973 sema_init(&nic_dev->mgmt_lock, 1);
974
edd384f6
AK
975 tx_stats = &nic_dev->tx_stats;
976 rx_stats = &nic_dev->rx_stats;
977
978 u64_stats_init(&tx_stats->syncp);
979 u64_stats_init(&rx_stats->syncp);
980
25a3ba61
AK
981 nic_dev->vlan_bitmap = devm_kzalloc(&pdev->dev,
982 VLAN_BITMAP_SIZE(nic_dev),
983 GFP_KERNEL);
984 if (!nic_dev->vlan_bitmap) {
985 err = -ENOMEM;
986 goto err_vlan_bitmap;
987 }
988
c4d06d2d
AK
989 nic_dev->workq = create_singlethread_workqueue(HINIC_WQ_NAME);
990 if (!nic_dev->workq) {
991 err = -ENOMEM;
992 goto err_workq;
993 }
994
51ba902a
AK
995 pci_set_drvdata(pdev, netdev);
996
25a3ba61
AK
997 err = hinic_port_get_mac(nic_dev, netdev->dev_addr);
998 if (err)
999 dev_warn(&pdev->dev, "Failed to get mac address\n");
1000
1001 err = hinic_port_add_mac(nic_dev, netdev->dev_addr, 0);
1002 if (err) {
1003 dev_err(&pdev->dev, "Failed to add mac\n");
1004 goto err_add_mac;
1005 }
1006
1007 err = hinic_port_set_mtu(nic_dev, netdev->mtu);
1008 if (err) {
1009 dev_err(&pdev->dev, "Failed to set mtu\n");
1010 goto err_set_mtu;
1011 }
1012
c4d06d2d
AK
1013 rx_mode_work = &nic_dev->rx_mode_work;
1014 INIT_WORK(&rx_mode_work->work, set_rx_mode);
1015
25a3ba61
AK
1016 netdev_features_init(netdev);
1017
51ba902a
AK
1018 netif_carrier_off(netdev);
1019
c4d06d2d
AK
1020 hinic_hwdev_cb_register(nic_dev->hwdev, HINIC_MGMT_MSG_CMD_LINK_STATUS,
1021 nic_dev, link_status_event_handler);
1022
cc18a754
ZC
1023 err = set_features(nic_dev, 0, nic_dev->netdev->features, true);
1024 if (err)
1025 goto err_set_features;
1026
7856e861 1027 SET_NETDEV_DEV(netdev, &pdev->dev);
cc18a754 1028
51ba902a
AK
1029 err = register_netdev(netdev);
1030 if (err) {
1031 dev_err(&pdev->dev, "Failed to register netdev\n");
1032 goto err_reg_netdev;
1033 }
1034
1035 return 0;
1036
1037err_reg_netdev:
cc18a754 1038err_set_features:
c4d06d2d
AK
1039 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1040 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1041 cancel_work_sync(&rx_mode_work->work);
1042
25a3ba61
AK
1043err_set_mtu:
1044err_add_mac:
51ba902a 1045 pci_set_drvdata(pdev, NULL);
c4d06d2d 1046 destroy_workqueue(nic_dev->workq);
25a3ba61 1047
c4d06d2d 1048err_workq:
25a3ba61 1049err_vlan_bitmap:
51ba902a
AK
1050 free_netdev(netdev);
1051
1052err_alloc_etherdev:
1053err_num_qps:
1054 hinic_free_hwdev(hwdev);
1055 return err;
1056}
1057
1058static int hinic_probe(struct pci_dev *pdev,
1059 const struct pci_device_id *id)
1060{
1061 int err = pci_enable_device(pdev);
1062
1063 if (err) {
1064 dev_err(&pdev->dev, "Failed to enable PCI device\n");
1065 return err;
1066 }
1067
1068 err = pci_request_regions(pdev, HINIC_DRV_NAME);
1069 if (err) {
1070 dev_err(&pdev->dev, "Failed to request PCI regions\n");
1071 goto err_pci_regions;
1072 }
1073
1074 pci_set_master(pdev);
1075
1076 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1077 if (err) {
1078 dev_warn(&pdev->dev, "Couldn't set 64-bit DMA mask\n");
1079 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1080 if (err) {
1081 dev_err(&pdev->dev, "Failed to set DMA mask\n");
1082 goto err_dma_mask;
1083 }
1084 }
1085
1086 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1087 if (err) {
1088 dev_warn(&pdev->dev,
1089 "Couldn't set 64-bit consistent DMA mask\n");
1090 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1091 if (err) {
1092 dev_err(&pdev->dev,
1093 "Failed to set consistent DMA mask\n");
1094 goto err_dma_consistent_mask;
1095 }
1096 }
1097
1098 err = nic_dev_init(pdev);
1099 if (err) {
1100 dev_err(&pdev->dev, "Failed to initialize NIC device\n");
1101 goto err_nic_dev_init;
1102 }
1103
1104 dev_info(&pdev->dev, "HiNIC driver - probed\n");
1105 return 0;
1106
1107err_nic_dev_init:
1108err_dma_consistent_mask:
1109err_dma_mask:
1110 pci_release_regions(pdev);
1111
1112err_pci_regions:
1113 pci_disable_device(pdev);
1114 return err;
1115}
1116
1117static void hinic_remove(struct pci_dev *pdev)
1118{
1119 struct net_device *netdev = pci_get_drvdata(pdev);
1120 struct hinic_dev *nic_dev = netdev_priv(netdev);
c4d06d2d 1121 struct hinic_rx_mode_work *rx_mode_work;
51ba902a
AK
1122
1123 unregister_netdev(netdev);
1124
c4d06d2d
AK
1125 hinic_hwdev_cb_unregister(nic_dev->hwdev,
1126 HINIC_MGMT_MSG_CMD_LINK_STATUS);
1127
1128 rx_mode_work = &nic_dev->rx_mode_work;
1129 cancel_work_sync(&rx_mode_work->work);
1130
51ba902a
AK
1131 pci_set_drvdata(pdev, NULL);
1132
c4d06d2d
AK
1133 destroy_workqueue(nic_dev->workq);
1134
51ba902a
AK
1135 hinic_free_hwdev(nic_dev->hwdev);
1136
1137 free_netdev(netdev);
1138
1139 pci_release_regions(pdev);
1140 pci_disable_device(pdev);
1141
1142 dev_info(&pdev->dev, "HiNIC driver - removed\n");
1143}
1144
53fe3ed1
XC
1145static void hinic_shutdown(struct pci_dev *pdev)
1146{
1147 pci_disable_device(pdev);
1148}
1149
51ba902a 1150static const struct pci_device_id hinic_pci_table[] = {
724e47a1 1151 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE), 0},
724e47a1 1152 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE), 0},
6679cf09
XC
1153 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_DUAL_PORT_100GE_MEZZ), 0},
1154 { PCI_VDEVICE(HUAWEI, HINIC_DEV_ID_QUAD_PORT_25GE_MEZZ), 0},
51ba902a
AK
1155 { 0, 0}
1156};
1157MODULE_DEVICE_TABLE(pci, hinic_pci_table);
1158
1159static struct pci_driver hinic_driver = {
1160 .name = HINIC_DRV_NAME,
1161 .id_table = hinic_pci_table,
1162 .probe = hinic_probe,
1163 .remove = hinic_remove,
53fe3ed1 1164 .shutdown = hinic_shutdown,
51ba902a
AK
1165};
1166
1167module_pci_driver(hinic_driver);