|
| 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 | + |
| 8 | +#include "fbnic.h" |
| 9 | +#include "fbnic_netdev.h" |
| 10 | +#include "fbnic_txrx.h" |
| 11 | + |
| 12 | +int __fbnic_open(struct fbnic_net *fbn) |
| 13 | +{ |
| 14 | + int err; |
| 15 | + |
| 16 | + err = fbnic_alloc_napi_vectors(fbn); |
| 17 | + if (err) |
| 18 | + return err; |
| 19 | + |
| 20 | + err = netif_set_real_num_tx_queues(fbn->netdev, |
| 21 | + fbn->num_tx_queues); |
| 22 | + if (err) |
| 23 | + goto free_resources; |
| 24 | + |
| 25 | + err = netif_set_real_num_rx_queues(fbn->netdev, |
| 26 | + fbn->num_rx_queues); |
| 27 | + if (err) |
| 28 | + goto free_resources; |
| 29 | + |
| 30 | + return 0; |
| 31 | +free_resources: |
| 32 | + fbnic_free_napi_vectors(fbn); |
| 33 | + return err; |
| 34 | +} |
| 35 | + |
| 36 | +static int fbnic_open(struct net_device *netdev) |
| 37 | +{ |
| 38 | + struct fbnic_net *fbn = netdev_priv(netdev); |
| 39 | + int err; |
| 40 | + |
| 41 | + err = __fbnic_open(fbn); |
| 42 | + if (!err) |
| 43 | + fbnic_up(fbn); |
| 44 | + |
| 45 | + return err; |
| 46 | +} |
| 47 | + |
| 48 | +static int fbnic_stop(struct net_device *netdev) |
| 49 | +{ |
| 50 | + struct fbnic_net *fbn = netdev_priv(netdev); |
| 51 | + |
| 52 | + fbnic_down(fbn); |
| 53 | + |
| 54 | + fbnic_free_napi_vectors(fbn); |
| 55 | + |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +static const struct net_device_ops fbnic_netdev_ops = { |
| 60 | + .ndo_open = fbnic_open, |
| 61 | + .ndo_stop = fbnic_stop, |
| 62 | + .ndo_validate_addr = eth_validate_addr, |
| 63 | + .ndo_start_xmit = fbnic_xmit_frame, |
| 64 | +}; |
| 65 | + |
| 66 | +void fbnic_reset_queues(struct fbnic_net *fbn, |
| 67 | + unsigned int tx, unsigned int rx) |
| 68 | +{ |
| 69 | + struct fbnic_dev *fbd = fbn->fbd; |
| 70 | + unsigned int max_napis; |
| 71 | + |
| 72 | + max_napis = fbd->num_irqs - FBNIC_NON_NAPI_VECTORS; |
| 73 | + |
| 74 | + tx = min(tx, max_napis); |
| 75 | + fbn->num_tx_queues = tx; |
| 76 | + |
| 77 | + rx = min(rx, max_napis); |
| 78 | + fbn->num_rx_queues = rx; |
| 79 | + |
| 80 | + fbn->num_napi = max(tx, rx); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * fbnic_netdev_free - Free the netdev associate with fbnic |
| 85 | + * @fbd: Driver specific structure to free netdev from |
| 86 | + * |
| 87 | + * Allocate and initialize the netdev and netdev private structure. Bind |
| 88 | + * together the hardware, netdev, and pci data structures. |
| 89 | + **/ |
| 90 | +void fbnic_netdev_free(struct fbnic_dev *fbd) |
| 91 | +{ |
| 92 | + free_netdev(fbd->netdev); |
| 93 | + fbd->netdev = NULL; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * fbnic_netdev_alloc - Allocate a netdev and associate with fbnic |
| 98 | + * @fbd: Driver specific structure to associate netdev with |
| 99 | + * |
| 100 | + * Allocate and initialize the netdev and netdev private structure. Bind |
| 101 | + * together the hardware, netdev, and pci data structures. |
| 102 | + * |
| 103 | + * Return: 0 on success, negative on failure |
| 104 | + **/ |
| 105 | +struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd) |
| 106 | +{ |
| 107 | + struct net_device *netdev; |
| 108 | + struct fbnic_net *fbn; |
| 109 | + int default_queues; |
| 110 | + |
| 111 | + netdev = alloc_etherdev_mq(sizeof(*fbn), FBNIC_MAX_RXQS); |
| 112 | + if (!netdev) |
| 113 | + return NULL; |
| 114 | + |
| 115 | + SET_NETDEV_DEV(netdev, fbd->dev); |
| 116 | + fbd->netdev = netdev; |
| 117 | + |
| 118 | + netdev->netdev_ops = &fbnic_netdev_ops; |
| 119 | + |
| 120 | + fbn = netdev_priv(netdev); |
| 121 | + |
| 122 | + fbn->netdev = netdev; |
| 123 | + fbn->fbd = fbd; |
| 124 | + INIT_LIST_HEAD(&fbn->napis); |
| 125 | + |
| 126 | + default_queues = netif_get_num_default_rss_queues(); |
| 127 | + if (default_queues > fbd->max_num_queues) |
| 128 | + default_queues = fbd->max_num_queues; |
| 129 | + |
| 130 | + fbnic_reset_queues(fbn, default_queues, default_queues); |
| 131 | + |
| 132 | + netdev->min_mtu = IPV6_MIN_MTU; |
| 133 | + netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN; |
| 134 | + |
| 135 | + netif_carrier_off(netdev); |
| 136 | + |
| 137 | + netif_tx_stop_all_queues(netdev); |
| 138 | + |
| 139 | + return netdev; |
| 140 | +} |
| 141 | + |
| 142 | +static int fbnic_dsn_to_mac_addr(u64 dsn, char *addr) |
| 143 | +{ |
| 144 | + addr[0] = (dsn >> 56) & 0xFF; |
| 145 | + addr[1] = (dsn >> 48) & 0xFF; |
| 146 | + addr[2] = (dsn >> 40) & 0xFF; |
| 147 | + addr[3] = (dsn >> 16) & 0xFF; |
| 148 | + addr[4] = (dsn >> 8) & 0xFF; |
| 149 | + addr[5] = dsn & 0xFF; |
| 150 | + |
| 151 | + return is_valid_ether_addr(addr) ? 0 : -EINVAL; |
| 152 | +} |
| 153 | + |
| 154 | +/** |
| 155 | + * fbnic_netdev_register - Initialize general software structures |
| 156 | + * @netdev: Netdev containing structure to initialize and register |
| 157 | + * |
| 158 | + * Initialize the MAC address for the netdev and register it. |
| 159 | + * |
| 160 | + * Return: 0 on success, negative on failure |
| 161 | + **/ |
| 162 | +int fbnic_netdev_register(struct net_device *netdev) |
| 163 | +{ |
| 164 | + struct fbnic_net *fbn = netdev_priv(netdev); |
| 165 | + struct fbnic_dev *fbd = fbn->fbd; |
| 166 | + u64 dsn = fbd->dsn; |
| 167 | + u8 addr[ETH_ALEN]; |
| 168 | + int err; |
| 169 | + |
| 170 | + err = fbnic_dsn_to_mac_addr(dsn, addr); |
| 171 | + if (!err) { |
| 172 | + ether_addr_copy(netdev->perm_addr, addr); |
| 173 | + eth_hw_addr_set(netdev, addr); |
| 174 | + } else { |
| 175 | + /* A randomly assigned MAC address will cause provisioning |
| 176 | + * issues so instead just fail to spawn the netdev and |
| 177 | + * avoid any confusion. |
| 178 | + */ |
| 179 | + dev_err(fbd->dev, "MAC addr %pM invalid\n", addr); |
| 180 | + return err; |
| 181 | + } |
| 182 | + |
| 183 | + return register_netdev(netdev); |
| 184 | +} |
| 185 | + |
| 186 | +void fbnic_netdev_unregister(struct net_device *netdev) |
| 187 | +{ |
| 188 | + unregister_netdev(netdev); |
| 189 | +} |
0 commit comments