1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3
4 #include <linux/etherdevice.h>
5 #include <linux/ipv6.h>
6 #include <linux/types.h>
7 #include <net/netdev_queues.h>
8
9 #include "fbnic.h"
10 #include "fbnic_netdev.h"
11 #include "fbnic_txrx.h"
12
__fbnic_open(struct fbnic_net * fbn)13 int __fbnic_open(struct fbnic_net *fbn)
14 {
15 struct fbnic_dev *fbd = fbn->fbd;
16 int err;
17
18 err = fbnic_alloc_napi_vectors(fbn);
19 if (err)
20 return err;
21
22 err = fbnic_alloc_resources(fbn);
23 if (err)
24 goto free_napi_vectors;
25
26 err = netif_set_real_num_tx_queues(fbn->netdev,
27 fbn->num_tx_queues);
28 if (err)
29 goto free_resources;
30
31 err = netif_set_real_num_rx_queues(fbn->netdev,
32 fbn->num_rx_queues);
33 if (err)
34 goto free_resources;
35
36 /* Send ownership message and flush to verify FW has seen it */
37 err = fbnic_fw_xmit_ownership_msg(fbd, true);
38 if (err) {
39 dev_warn(fbd->dev,
40 "Error %d sending host ownership message to the firmware\n",
41 err);
42 goto free_resources;
43 }
44
45 err = fbnic_time_start(fbn);
46 if (err)
47 goto release_ownership;
48
49 err = fbnic_fw_init_heartbeat(fbd, false);
50 if (err)
51 goto time_stop;
52
53 err = fbnic_pcs_irq_enable(fbd);
54 if (err)
55 goto time_stop;
56 /* Pull the BMC config and initialize the RPC */
57 fbnic_bmc_rpc_init(fbd);
58 fbnic_rss_reinit(fbd, fbn);
59
60 return 0;
61 time_stop:
62 fbnic_time_stop(fbn);
63 release_ownership:
64 fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
65 free_resources:
66 fbnic_free_resources(fbn);
67 free_napi_vectors:
68 fbnic_free_napi_vectors(fbn);
69 return err;
70 }
71
fbnic_open(struct net_device * netdev)72 static int fbnic_open(struct net_device *netdev)
73 {
74 struct fbnic_net *fbn = netdev_priv(netdev);
75 int err;
76
77 err = __fbnic_open(fbn);
78 if (!err)
79 fbnic_up(fbn);
80
81 return err;
82 }
83
fbnic_stop(struct net_device * netdev)84 static int fbnic_stop(struct net_device *netdev)
85 {
86 struct fbnic_net *fbn = netdev_priv(netdev);
87
88 fbnic_down(fbn);
89 fbnic_pcs_irq_disable(fbn->fbd);
90
91 fbnic_time_stop(fbn);
92 fbnic_fw_xmit_ownership_msg(fbn->fbd, false);
93
94 fbnic_free_resources(fbn);
95 fbnic_free_napi_vectors(fbn);
96
97 return 0;
98 }
99
fbnic_uc_sync(struct net_device * netdev,const unsigned char * addr)100 static int fbnic_uc_sync(struct net_device *netdev, const unsigned char *addr)
101 {
102 struct fbnic_net *fbn = netdev_priv(netdev);
103 struct fbnic_mac_addr *avail_addr;
104
105 if (WARN_ON(!is_valid_ether_addr(addr)))
106 return -EADDRNOTAVAIL;
107
108 avail_addr = __fbnic_uc_sync(fbn->fbd, addr);
109 if (!avail_addr)
110 return -ENOSPC;
111
112 /* Add type flag indicating this address is in use by the host */
113 set_bit(FBNIC_MAC_ADDR_T_UNICAST, avail_addr->act_tcam);
114
115 return 0;
116 }
117
fbnic_uc_unsync(struct net_device * netdev,const unsigned char * addr)118 static int fbnic_uc_unsync(struct net_device *netdev, const unsigned char *addr)
119 {
120 struct fbnic_net *fbn = netdev_priv(netdev);
121 struct fbnic_dev *fbd = fbn->fbd;
122 int i, ret;
123
124 /* Scan from middle of list to bottom, filling bottom up.
125 * Skip the first entry which is reserved for dev_addr and
126 * leave the last entry to use for promiscuous filtering.
127 */
128 for (i = fbd->mac_addr_boundary, ret = -ENOENT;
129 i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX && ret; i++) {
130 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
131
132 if (!ether_addr_equal(mac_addr->value.addr8, addr))
133 continue;
134
135 ret = __fbnic_uc_unsync(mac_addr);
136 }
137
138 return ret;
139 }
140
fbnic_mc_sync(struct net_device * netdev,const unsigned char * addr)141 static int fbnic_mc_sync(struct net_device *netdev, const unsigned char *addr)
142 {
143 struct fbnic_net *fbn = netdev_priv(netdev);
144 struct fbnic_mac_addr *avail_addr;
145
146 if (WARN_ON(!is_multicast_ether_addr(addr)))
147 return -EADDRNOTAVAIL;
148
149 avail_addr = __fbnic_mc_sync(fbn->fbd, addr);
150 if (!avail_addr)
151 return -ENOSPC;
152
153 /* Add type flag indicating this address is in use by the host */
154 set_bit(FBNIC_MAC_ADDR_T_MULTICAST, avail_addr->act_tcam);
155
156 return 0;
157 }
158
fbnic_mc_unsync(struct net_device * netdev,const unsigned char * addr)159 static int fbnic_mc_unsync(struct net_device *netdev, const unsigned char *addr)
160 {
161 struct fbnic_net *fbn = netdev_priv(netdev);
162 struct fbnic_dev *fbd = fbn->fbd;
163 int i, ret;
164
165 /* Scan from middle of list to top, filling top down.
166 * Skip over the address reserved for the BMC MAC and
167 * exclude index 0 as that belongs to the broadcast address
168 */
169 for (i = fbd->mac_addr_boundary, ret = -ENOENT;
170 --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX && ret;) {
171 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
172
173 if (!ether_addr_equal(mac_addr->value.addr8, addr))
174 continue;
175
176 ret = __fbnic_mc_unsync(mac_addr);
177 }
178
179 return ret;
180 }
181
__fbnic_set_rx_mode(struct net_device * netdev)182 void __fbnic_set_rx_mode(struct net_device *netdev)
183 {
184 struct fbnic_net *fbn = netdev_priv(netdev);
185 bool uc_promisc = false, mc_promisc = false;
186 struct fbnic_dev *fbd = fbn->fbd;
187 struct fbnic_mac_addr *mac_addr;
188 int err;
189
190 /* Populate host address from dev_addr */
191 mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX];
192 if (!ether_addr_equal(mac_addr->value.addr8, netdev->dev_addr) ||
193 mac_addr->state != FBNIC_TCAM_S_VALID) {
194 ether_addr_copy(mac_addr->value.addr8, netdev->dev_addr);
195 mac_addr->state = FBNIC_TCAM_S_UPDATE;
196 set_bit(FBNIC_MAC_ADDR_T_UNICAST, mac_addr->act_tcam);
197 }
198
199 /* Populate broadcast address if broadcast is enabled */
200 mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
201 if (netdev->flags & IFF_BROADCAST) {
202 if (!is_broadcast_ether_addr(mac_addr->value.addr8) ||
203 mac_addr->state != FBNIC_TCAM_S_VALID) {
204 eth_broadcast_addr(mac_addr->value.addr8);
205 mac_addr->state = FBNIC_TCAM_S_ADD;
206 }
207 set_bit(FBNIC_MAC_ADDR_T_BROADCAST, mac_addr->act_tcam);
208 } else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
209 __fbnic_xc_unsync(mac_addr, FBNIC_MAC_ADDR_T_BROADCAST);
210 }
211
212 /* Synchronize unicast and multicast address lists */
213 err = __dev_uc_sync(netdev, fbnic_uc_sync, fbnic_uc_unsync);
214 if (err == -ENOSPC)
215 uc_promisc = true;
216 err = __dev_mc_sync(netdev, fbnic_mc_sync, fbnic_mc_unsync);
217 if (err == -ENOSPC)
218 mc_promisc = true;
219
220 uc_promisc |= !!(netdev->flags & IFF_PROMISC);
221 mc_promisc |= !!(netdev->flags & IFF_ALLMULTI) || uc_promisc;
222
223 /* Populate last TCAM entry with promiscuous entry and 0/1 bit mask */
224 mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_PROMISC_IDX];
225 if (uc_promisc) {
226 if (!is_zero_ether_addr(mac_addr->value.addr8) ||
227 mac_addr->state != FBNIC_TCAM_S_VALID) {
228 eth_zero_addr(mac_addr->value.addr8);
229 eth_broadcast_addr(mac_addr->mask.addr8);
230 clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
231 mac_addr->act_tcam);
232 set_bit(FBNIC_MAC_ADDR_T_PROMISC,
233 mac_addr->act_tcam);
234 mac_addr->state = FBNIC_TCAM_S_ADD;
235 }
236 } else if (mc_promisc &&
237 (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi)) {
238 /* We have to add a special handler for multicast as the
239 * BMC may have an all-multi rule already in place. As such
240 * adding a rule ourselves won't do any good so we will have
241 * to modify the rules for the ALL MULTI below if the BMC
242 * already has the rule in place.
243 */
244 if (!is_multicast_ether_addr(mac_addr->value.addr8) ||
245 mac_addr->state != FBNIC_TCAM_S_VALID) {
246 eth_zero_addr(mac_addr->value.addr8);
247 eth_broadcast_addr(mac_addr->mask.addr8);
248 mac_addr->value.addr8[0] ^= 1;
249 mac_addr->mask.addr8[0] ^= 1;
250 set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
251 mac_addr->act_tcam);
252 clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
253 mac_addr->act_tcam);
254 mac_addr->state = FBNIC_TCAM_S_ADD;
255 }
256 } else if (mac_addr->state == FBNIC_TCAM_S_VALID) {
257 if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) {
258 clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
259 mac_addr->act_tcam);
260 clear_bit(FBNIC_MAC_ADDR_T_PROMISC,
261 mac_addr->act_tcam);
262 } else {
263 mac_addr->state = FBNIC_TCAM_S_DELETE;
264 }
265 }
266
267 /* Add rules for BMC all multicast if it is enabled */
268 fbnic_bmc_rpc_all_multi_config(fbd, mc_promisc);
269
270 /* Sift out any unshared BMC rules and place them in BMC only section */
271 fbnic_sift_macda(fbd);
272
273 /* Write updates to hardware */
274 fbnic_write_rules(fbd);
275 fbnic_write_macda(fbd);
276 fbnic_write_tce_tcam(fbd);
277 }
278
fbnic_set_rx_mode(struct net_device * netdev)279 static void fbnic_set_rx_mode(struct net_device *netdev)
280 {
281 /* No need to update the hardware if we are not running */
282 if (netif_running(netdev))
283 __fbnic_set_rx_mode(netdev);
284 }
285
fbnic_set_mac(struct net_device * netdev,void * p)286 static int fbnic_set_mac(struct net_device *netdev, void *p)
287 {
288 struct sockaddr *addr = p;
289
290 if (!is_valid_ether_addr(addr->sa_data))
291 return -EADDRNOTAVAIL;
292
293 eth_hw_addr_set(netdev, addr->sa_data);
294
295 fbnic_set_rx_mode(netdev);
296
297 return 0;
298 }
299
fbnic_clear_rx_mode(struct net_device * netdev)300 void fbnic_clear_rx_mode(struct net_device *netdev)
301 {
302 struct fbnic_net *fbn = netdev_priv(netdev);
303 struct fbnic_dev *fbd = fbn->fbd;
304 int idx;
305
306 for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
307 struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
308
309 if (mac_addr->state != FBNIC_TCAM_S_VALID)
310 continue;
311
312 bitmap_clear(mac_addr->act_tcam,
313 FBNIC_MAC_ADDR_T_HOST_START,
314 FBNIC_MAC_ADDR_T_HOST_LEN);
315
316 if (bitmap_empty(mac_addr->act_tcam,
317 FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
318 mac_addr->state = FBNIC_TCAM_S_DELETE;
319 }
320
321 /* Write updates to hardware */
322 fbnic_write_macda(fbd);
323
324 __dev_uc_unsync(netdev, NULL);
325 __dev_mc_unsync(netdev, NULL);
326 }
327
fbnic_hwtstamp_get(struct net_device * netdev,struct kernel_hwtstamp_config * config)328 static int fbnic_hwtstamp_get(struct net_device *netdev,
329 struct kernel_hwtstamp_config *config)
330 {
331 struct fbnic_net *fbn = netdev_priv(netdev);
332
333 *config = fbn->hwtstamp_config;
334
335 return 0;
336 }
337
fbnic_hwtstamp_set(struct net_device * netdev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)338 static int fbnic_hwtstamp_set(struct net_device *netdev,
339 struct kernel_hwtstamp_config *config,
340 struct netlink_ext_ack *extack)
341 {
342 struct fbnic_net *fbn = netdev_priv(netdev);
343 int old_rx_filter;
344
345 if (config->source != HWTSTAMP_SOURCE_NETDEV)
346 return -EOPNOTSUPP;
347
348 if (!kernel_hwtstamp_config_changed(config, &fbn->hwtstamp_config))
349 return 0;
350
351 /* Upscale the filters */
352 switch (config->rx_filter) {
353 case HWTSTAMP_FILTER_NONE:
354 case HWTSTAMP_FILTER_ALL:
355 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
356 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
357 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
358 case HWTSTAMP_FILTER_PTP_V2_EVENT:
359 break;
360 case HWTSTAMP_FILTER_NTP_ALL:
361 config->rx_filter = HWTSTAMP_FILTER_ALL;
362 break;
363 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
364 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
365 config->rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
366 break;
367 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
368 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
369 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
370 break;
371 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
372 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
373 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
374 break;
375 case HWTSTAMP_FILTER_PTP_V2_SYNC:
376 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
377 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
378 break;
379 default:
380 return -ERANGE;
381 }
382
383 /* Configure */
384 old_rx_filter = fbn->hwtstamp_config.rx_filter;
385 memcpy(&fbn->hwtstamp_config, config, sizeof(*config));
386
387 if (old_rx_filter != config->rx_filter && netif_running(fbn->netdev)) {
388 fbnic_rss_reinit(fbn->fbd, fbn);
389 fbnic_write_rules(fbn->fbd);
390 }
391
392 /* Save / report back filter configuration
393 * Note that our filter configuration is inexact. Instead of
394 * filtering for a specific UDP port or L2 Ethertype we are
395 * filtering in all UDP or all non-IP packets for timestamping. So
396 * if anything other than FILTER_ALL is requested we report
397 * FILTER_SOME indicating that we will be timestamping a few
398 * additional packets.
399 */
400 if (config->rx_filter > HWTSTAMP_FILTER_ALL)
401 config->rx_filter = HWTSTAMP_FILTER_SOME;
402
403 return 0;
404 }
405
fbnic_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats64)406 static void fbnic_get_stats64(struct net_device *dev,
407 struct rtnl_link_stats64 *stats64)
408 {
409 u64 tx_bytes, tx_packets, tx_dropped = 0;
410 u64 rx_bytes, rx_packets, rx_dropped = 0;
411 struct fbnic_net *fbn = netdev_priv(dev);
412 struct fbnic_queue_stats *stats;
413 unsigned int start, i;
414
415 stats = &fbn->tx_stats;
416
417 tx_bytes = stats->bytes;
418 tx_packets = stats->packets;
419 tx_dropped = stats->dropped;
420
421 stats64->tx_bytes = tx_bytes;
422 stats64->tx_packets = tx_packets;
423 stats64->tx_dropped = tx_dropped;
424
425 for (i = 0; i < fbn->num_tx_queues; i++) {
426 struct fbnic_ring *txr = fbn->tx[i];
427
428 if (!txr)
429 continue;
430
431 stats = &txr->stats;
432 do {
433 start = u64_stats_fetch_begin(&stats->syncp);
434 tx_bytes = stats->bytes;
435 tx_packets = stats->packets;
436 tx_dropped = stats->dropped;
437 } while (u64_stats_fetch_retry(&stats->syncp, start));
438
439 stats64->tx_bytes += tx_bytes;
440 stats64->tx_packets += tx_packets;
441 stats64->tx_dropped += tx_dropped;
442 }
443
444 stats = &fbn->rx_stats;
445
446 rx_bytes = stats->bytes;
447 rx_packets = stats->packets;
448 rx_dropped = stats->dropped;
449
450 stats64->rx_bytes = rx_bytes;
451 stats64->rx_packets = rx_packets;
452 stats64->rx_dropped = rx_dropped;
453
454 for (i = 0; i < fbn->num_rx_queues; i++) {
455 struct fbnic_ring *rxr = fbn->rx[i];
456
457 if (!rxr)
458 continue;
459
460 stats = &rxr->stats;
461 do {
462 start = u64_stats_fetch_begin(&stats->syncp);
463 rx_bytes = stats->bytes;
464 rx_packets = stats->packets;
465 rx_dropped = stats->dropped;
466 } while (u64_stats_fetch_retry(&stats->syncp, start));
467
468 stats64->rx_bytes += rx_bytes;
469 stats64->rx_packets += rx_packets;
470 stats64->rx_dropped += rx_dropped;
471 }
472 }
473
474 static const struct net_device_ops fbnic_netdev_ops = {
475 .ndo_open = fbnic_open,
476 .ndo_stop = fbnic_stop,
477 .ndo_validate_addr = eth_validate_addr,
478 .ndo_start_xmit = fbnic_xmit_frame,
479 .ndo_features_check = fbnic_features_check,
480 .ndo_set_mac_address = fbnic_set_mac,
481 .ndo_set_rx_mode = fbnic_set_rx_mode,
482 .ndo_get_stats64 = fbnic_get_stats64,
483 .ndo_hwtstamp_get = fbnic_hwtstamp_get,
484 .ndo_hwtstamp_set = fbnic_hwtstamp_set,
485 };
486
fbnic_get_queue_stats_rx(struct net_device * dev,int idx,struct netdev_queue_stats_rx * rx)487 static void fbnic_get_queue_stats_rx(struct net_device *dev, int idx,
488 struct netdev_queue_stats_rx *rx)
489 {
490 struct fbnic_net *fbn = netdev_priv(dev);
491 struct fbnic_ring *rxr = fbn->rx[idx];
492 struct fbnic_queue_stats *stats;
493 unsigned int start;
494 u64 bytes, packets;
495
496 if (!rxr)
497 return;
498
499 stats = &rxr->stats;
500 do {
501 start = u64_stats_fetch_begin(&stats->syncp);
502 bytes = stats->bytes;
503 packets = stats->packets;
504 } while (u64_stats_fetch_retry(&stats->syncp, start));
505
506 rx->bytes = bytes;
507 rx->packets = packets;
508 }
509
fbnic_get_queue_stats_tx(struct net_device * dev,int idx,struct netdev_queue_stats_tx * tx)510 static void fbnic_get_queue_stats_tx(struct net_device *dev, int idx,
511 struct netdev_queue_stats_tx *tx)
512 {
513 struct fbnic_net *fbn = netdev_priv(dev);
514 struct fbnic_ring *txr = fbn->tx[idx];
515 struct fbnic_queue_stats *stats;
516 unsigned int start;
517 u64 bytes, packets;
518
519 if (!txr)
520 return;
521
522 stats = &txr->stats;
523 do {
524 start = u64_stats_fetch_begin(&stats->syncp);
525 bytes = stats->bytes;
526 packets = stats->packets;
527 } while (u64_stats_fetch_retry(&stats->syncp, start));
528
529 tx->bytes = bytes;
530 tx->packets = packets;
531 }
532
fbnic_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)533 static void fbnic_get_base_stats(struct net_device *dev,
534 struct netdev_queue_stats_rx *rx,
535 struct netdev_queue_stats_tx *tx)
536 {
537 struct fbnic_net *fbn = netdev_priv(dev);
538
539 tx->bytes = fbn->tx_stats.bytes;
540 tx->packets = fbn->tx_stats.packets;
541
542 rx->bytes = fbn->rx_stats.bytes;
543 rx->packets = fbn->rx_stats.packets;
544 }
545
546 static const struct netdev_stat_ops fbnic_stat_ops = {
547 .get_queue_stats_rx = fbnic_get_queue_stats_rx,
548 .get_queue_stats_tx = fbnic_get_queue_stats_tx,
549 .get_base_stats = fbnic_get_base_stats,
550 };
551
fbnic_reset_queues(struct fbnic_net * fbn,unsigned int tx,unsigned int rx)552 void fbnic_reset_queues(struct fbnic_net *fbn,
553 unsigned int tx, unsigned int rx)
554 {
555 struct fbnic_dev *fbd = fbn->fbd;
556 unsigned int max_napis;
557
558 max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS;
559
560 tx = min(tx, max_napis);
561 fbn->num_tx_queues = tx;
562
563 rx = min(rx, max_napis);
564 fbn->num_rx_queues = rx;
565
566 fbn->num_napi = max(tx, rx);
567 }
568
569 /**
570 * fbnic_netdev_free - Free the netdev associate with fbnic
571 * @fbd: Driver specific structure to free netdev from
572 *
573 * Allocate and initialize the netdev and netdev private structure. Bind
574 * together the hardware, netdev, and pci data structures.
575 **/
fbnic_netdev_free(struct fbnic_dev * fbd)576 void fbnic_netdev_free(struct fbnic_dev *fbd)
577 {
578 struct fbnic_net *fbn = netdev_priv(fbd->netdev);
579
580 if (fbn->phylink)
581 phylink_destroy(fbn->phylink);
582
583 free_netdev(fbd->netdev);
584 fbd->netdev = NULL;
585 }
586
587 /**
588 * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic
589 * @fbd: Driver specific structure to associate netdev with
590 *
591 * Allocate and initialize the netdev and netdev private structure. Bind
592 * together the hardware, netdev, and pci data structures.
593 *
594 * Return: 0 on success, negative on failure
595 **/
fbnic_netdev_alloc(struct fbnic_dev * fbd)596 struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd)
597 {
598 struct net_device *netdev;
599 struct fbnic_net *fbn;
600 int default_queues;
601
602 netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS);
603 if (!netdev)
604 return NULL;
605
606 SET_NETDEV_DEV(netdev, fbd->dev);
607 fbd->netdev = netdev;
608
609 netdev->netdev_ops = &fbnic_netdev_ops;
610 netdev->stat_ops = &fbnic_stat_ops;
611
612 fbnic_set_ethtool_ops(netdev);
613
614 fbn = netdev_priv(netdev);
615
616 fbn->netdev = netdev;
617 fbn->fbd = fbd;
618 INIT_LIST_HEAD(&fbn->napis);
619
620 fbn->txq_size = FBNIC_TXQ_SIZE_DEFAULT;
621 fbn->hpq_size = FBNIC_HPQ_SIZE_DEFAULT;
622 fbn->ppq_size = FBNIC_PPQ_SIZE_DEFAULT;
623 fbn->rcq_size = FBNIC_RCQ_SIZE_DEFAULT;
624
625 default_queues = netif_get_num_default_rss_queues();
626 if (default_queues > fbd->max_num_queues)
627 default_queues = fbd->max_num_queues;
628
629 fbnic_reset_queues(fbn, default_queues, default_queues);
630
631 fbnic_reset_indir_tbl(fbn);
632 fbnic_rss_key_fill(fbn->rss_key);
633 fbnic_rss_init_en_mask(fbn);
634
635 netdev->features |=
636 NETIF_F_RXHASH |
637 NETIF_F_SG |
638 NETIF_F_HW_CSUM |
639 NETIF_F_RXCSUM;
640
641 netdev->hw_features |= netdev->features;
642 netdev->vlan_features |= netdev->features;
643 netdev->hw_enc_features |= netdev->features;
644
645 netdev->min_mtu = IPV6_MIN_MTU;
646 netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
647
648 /* TBD: This is workaround for BMC as phylink doesn't have support
649 * for leavling the link enabled if a BMC is present.
650 */
651 netdev->ethtool->wol_enabled = true;
652
653 fbn->fec = FBNIC_FEC_AUTO | FBNIC_FEC_RS;
654 fbn->link_mode = FBNIC_LINK_AUTO | FBNIC_LINK_50R2;
655 netif_carrier_off(netdev);
656
657 netif_tx_stop_all_queues(netdev);
658
659 if (fbnic_phylink_init(netdev)) {
660 fbnic_netdev_free(fbd);
661 return NULL;
662 }
663
664 return netdev;
665 }
666
fbnic_dsn_to_mac_addr(u64 dsn,char * addr)667 static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr)
668 {
669 addr[0] = (dsn >> 56) & 0xFF;
670 addr[1] = (dsn >> 48) & 0xFF;
671 addr[2] = (dsn >> 40) & 0xFF;
672 addr[3] = (dsn >> 16) & 0xFF;
673 addr[4] = (dsn >> 8) & 0xFF;
674 addr[5] = dsn & 0xFF;
675
676 return is_valid_ether_addr(addr) ? 0 : -EINVAL;
677 }
678
679 /**
680 * fbnic_netdev_register - Initialize general software structures
681 * @netdev: Netdev containing structure to initialize and register
682 *
683 * Initialize the MAC address for the netdev and register it.
684 *
685 * Return: 0 on success, negative on failure
686 **/
fbnic_netdev_register(struct net_device * netdev)687 int fbnic_netdev_register(struct net_device *netdev)
688 {
689 struct fbnic_net *fbn = netdev_priv(netdev);
690 struct fbnic_dev *fbd = fbn->fbd;
691 u64 dsn = fbd->dsn;
692 u8 addr[ETH_ALEN];
693 int err;
694
695 err = fbnic_dsn_to_mac_addr(dsn, addr);
696 if (!err) {
697 ether_addr_copy(netdev->perm_addr, addr);
698 eth_hw_addr_set(netdev, addr);
699 } else {
700 /* A randomly assigned MAC address will cause provisioning
701 * issues so instead just fail to spawn the netdev and
702 * avoid any confusion.
703 */
704 dev_err(fbd->dev, "MAC addr %pM invalid\n", addr);
705 return err;
706 }
707
708 return register_netdev(netdev);
709 }
710
fbnic_netdev_unregister(struct net_device * netdev)711 void fbnic_netdev_unregister(struct net_device *netdev)
712 {
713 unregister_netdev(netdev);
714 }
715