Merge tag 'lsm-pr-20230626' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/lsm
[linux-block.git] / drivers / net / ethernet / sfc / ef100_netdev.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2018 Solarflare Communications Inc.
5  * Copyright 2019-2020 Xilinx Inc.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published
9  * by the Free Software Foundation, incorporated herein by reference.
10  */
11 #include "net_driver.h"
12 #include "mcdi_port_common.h"
13 #include "mcdi_functions.h"
14 #include "efx_common.h"
15 #include "efx_channels.h"
16 #include "tx_common.h"
17 #include "ef100_netdev.h"
18 #include "ef100_ethtool.h"
19 #include "nic_common.h"
20 #include "ef100_nic.h"
21 #include "ef100_tx.h"
22 #include "ef100_regs.h"
23 #include "mcdi_filters.h"
24 #include "rx_common.h"
25 #include "ef100_sriov.h"
26 #include "tc_bindings.h"
27 #include "efx_devlink.h"
28
29 static void ef100_update_name(struct efx_nic *efx)
30 {
31         strcpy(efx->name, efx->net_dev->name);
32 }
33
34 static int ef100_alloc_vis(struct efx_nic *efx, unsigned int *allocated_vis)
35 {
36         /* EF100 uses a single TXQ per channel, as all checksum offloading
37          * is configured in the TX descriptor, and there is no TX Pacer for
38          * HIGHPRI queues.
39          */
40         unsigned int tx_vis = efx->n_tx_channels + efx->n_extra_tx_channels;
41         unsigned int rx_vis = efx->n_rx_channels;
42         unsigned int min_vis, max_vis;
43
44         EFX_WARN_ON_PARANOID(efx->tx_queues_per_channel != 1);
45
46         tx_vis += efx->n_xdp_channels * efx->xdp_tx_per_channel;
47
48         max_vis = max(rx_vis, tx_vis);
49         /* Currently don't handle resource starvation and only accept
50          * our maximum needs and no less.
51          */
52         min_vis = max_vis;
53
54         return efx_mcdi_alloc_vis(efx, min_vis, max_vis,
55                                   NULL, allocated_vis);
56 }
57
58 static int ef100_remap_bar(struct efx_nic *efx, int max_vis)
59 {
60         unsigned int uc_mem_map_size;
61         void __iomem *membase;
62
63         efx->max_vis = max_vis;
64         uc_mem_map_size = PAGE_ALIGN(max_vis * efx->vi_stride);
65
66         /* Extend the original UC mapping of the memory BAR */
67         membase = ioremap(efx->membase_phys, uc_mem_map_size);
68         if (!membase) {
69                 netif_err(efx, probe, efx->net_dev,
70                           "could not extend memory BAR to %x\n",
71                           uc_mem_map_size);
72                 return -ENOMEM;
73         }
74         iounmap(efx->membase);
75         efx->membase = membase;
76         return 0;
77 }
78
79 /* Context: process, rtnl_lock() held.
80  * Note that the kernel will ignore our return code; this method
81  * should really be a void.
82  */
83 static int ef100_net_stop(struct net_device *net_dev)
84 {
85         struct efx_nic *efx = efx_netdev_priv(net_dev);
86
87         netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
88                   raw_smp_processor_id());
89
90         efx_detach_reps(efx);
91         netif_stop_queue(net_dev);
92         efx_stop_all(efx);
93         efx_mcdi_mac_fini_stats(efx);
94         efx_disable_interrupts(efx);
95         efx_clear_interrupt_affinity(efx);
96         efx_nic_fini_interrupt(efx);
97         efx_remove_filters(efx);
98         efx_fini_napi(efx);
99         efx_remove_channels(efx);
100         efx_mcdi_free_vis(efx);
101         efx_remove_interrupts(efx);
102
103         efx->state = STATE_NET_DOWN;
104
105         return 0;
106 }
107
108 /* Context: process, rtnl_lock() held. */
109 static int ef100_net_open(struct net_device *net_dev)
110 {
111         struct efx_nic *efx = efx_netdev_priv(net_dev);
112         unsigned int allocated_vis;
113         int rc;
114
115         ef100_update_name(efx);
116         netif_dbg(efx, ifup, net_dev, "opening device on CPU %d\n",
117                   raw_smp_processor_id());
118
119         rc = efx_check_disabled(efx);
120         if (rc)
121                 goto fail;
122
123         rc = efx_probe_interrupts(efx);
124         if (rc)
125                 goto fail;
126
127         rc = efx_set_channels(efx);
128         if (rc)
129                 goto fail;
130
131         rc = efx_mcdi_free_vis(efx);
132         if (rc)
133                 goto fail;
134
135         rc = ef100_alloc_vis(efx, &allocated_vis);
136         if (rc)
137                 goto fail;
138
139         rc = efx_probe_channels(efx);
140         if (rc)
141                 return rc;
142
143         rc = ef100_remap_bar(efx, allocated_vis);
144         if (rc)
145                 goto fail;
146
147         efx_init_napi(efx);
148
149         rc = efx_probe_filters(efx);
150         if (rc)
151                 goto fail;
152
153         rc = efx_nic_init_interrupt(efx);
154         if (rc)
155                 goto fail;
156         efx_set_interrupt_affinity(efx);
157
158         rc = efx_enable_interrupts(efx);
159         if (rc)
160                 goto fail;
161
162         /* in case the MC rebooted while we were stopped, consume the change
163          * to the warm reboot count
164          */
165         (void) efx_mcdi_poll_reboot(efx);
166
167         rc = efx_mcdi_mac_init_stats(efx);
168         if (rc)
169                 goto fail;
170
171         efx_start_all(efx);
172
173         /* Link state detection is normally event-driven; we have
174          * to poll now because we could have missed a change
175          */
176         mutex_lock(&efx->mac_lock);
177         if (efx_mcdi_phy_poll(efx))
178                 efx_link_status_changed(efx);
179         mutex_unlock(&efx->mac_lock);
180
181         efx->state = STATE_NET_UP;
182         if (netif_running(efx->net_dev))
183                 efx_attach_reps(efx);
184
185         return 0;
186
187 fail:
188         ef100_net_stop(net_dev);
189         return rc;
190 }
191
192 /* Initiate a packet transmission.  We use one channel per CPU
193  * (sharing when we have more CPUs than channels).
194  *
195  * Context: non-blocking.
196  * Note that returning anything other than NETDEV_TX_OK will cause the
197  * OS to free the skb.
198  */
199 static netdev_tx_t ef100_hard_start_xmit(struct sk_buff *skb,
200                                          struct net_device *net_dev)
201 {
202         struct efx_nic *efx = efx_netdev_priv(net_dev);
203
204         return __ef100_hard_start_xmit(skb, efx, net_dev, NULL);
205 }
206
207 netdev_tx_t __ef100_hard_start_xmit(struct sk_buff *skb,
208                                     struct efx_nic *efx,
209                                     struct net_device *net_dev,
210                                     struct efx_rep *efv)
211 {
212         struct efx_tx_queue *tx_queue;
213         struct efx_channel *channel;
214         int rc;
215
216         channel = efx_get_tx_channel(efx, skb_get_queue_mapping(skb));
217         netif_vdbg(efx, tx_queued, efx->net_dev,
218                    "%s len %d data %d channel %d\n", __func__,
219                    skb->len, skb->data_len, channel->channel);
220         if (!efx->n_channels || !efx->n_tx_channels || !channel) {
221                 netif_stop_queue(net_dev);
222                 dev_kfree_skb_any(skb);
223                 goto err;
224         }
225
226         tx_queue = &channel->tx_queue[0];
227         rc = __ef100_enqueue_skb(tx_queue, skb, efv);
228         if (rc == 0)
229                 return NETDEV_TX_OK;
230
231 err:
232         net_dev->stats.tx_dropped++;
233         return NETDEV_TX_OK;
234 }
235
236 static const struct net_device_ops ef100_netdev_ops = {
237         .ndo_open               = ef100_net_open,
238         .ndo_stop               = ef100_net_stop,
239         .ndo_start_xmit         = ef100_hard_start_xmit,
240         .ndo_tx_timeout         = efx_watchdog,
241         .ndo_get_stats64        = efx_net_stats,
242         .ndo_change_mtu         = efx_change_mtu,
243         .ndo_validate_addr      = eth_validate_addr,
244         .ndo_set_mac_address    = efx_set_mac_address,
245         .ndo_set_rx_mode        = efx_set_rx_mode, /* Lookout */
246         .ndo_set_features       = efx_set_features,
247         .ndo_get_phys_port_id   = efx_get_phys_port_id,
248         .ndo_get_phys_port_name = efx_get_phys_port_name,
249 #ifdef CONFIG_RFS_ACCEL
250         .ndo_rx_flow_steer      = efx_filter_rfs,
251 #endif
252 #ifdef CONFIG_SFC_SRIOV
253         .ndo_setup_tc           = efx_tc_setup,
254 #endif
255 };
256
257 /*      Netdev registration
258  */
259 int ef100_netdev_event(struct notifier_block *this,
260                        unsigned long event, void *ptr)
261 {
262         struct efx_nic *efx = container_of(this, struct efx_nic, netdev_notifier);
263         struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
264
265         if (efx->net_dev == net_dev &&
266             (event == NETDEV_CHANGENAME || event == NETDEV_REGISTER))
267                 ef100_update_name(efx);
268
269         return NOTIFY_DONE;
270 }
271
272 static int ef100_register_netdev(struct efx_nic *efx)
273 {
274         struct net_device *net_dev = efx->net_dev;
275         int rc;
276
277         net_dev->watchdog_timeo = 5 * HZ;
278         net_dev->irq = efx->pci_dev->irq;
279         net_dev->netdev_ops = &ef100_netdev_ops;
280         net_dev->min_mtu = EFX_MIN_MTU;
281         net_dev->max_mtu = EFX_MAX_MTU;
282         net_dev->ethtool_ops = &ef100_ethtool_ops;
283
284         rtnl_lock();
285
286         rc = dev_alloc_name(net_dev, net_dev->name);
287         if (rc < 0)
288                 goto fail_locked;
289         ef100_update_name(efx);
290
291         rc = register_netdevice(net_dev);
292         if (rc)
293                 goto fail_locked;
294
295         /* Always start with carrier off; PHY events will detect the link */
296         netif_carrier_off(net_dev);
297
298         efx->state = STATE_NET_DOWN;
299         rtnl_unlock();
300         efx_init_mcdi_logging(efx);
301
302         return 0;
303
304 fail_locked:
305         rtnl_unlock();
306         netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
307         return rc;
308 }
309
310 static void ef100_unregister_netdev(struct efx_nic *efx)
311 {
312         if (efx_dev_registered(efx)) {
313                 efx_fini_mcdi_logging(efx);
314                 efx->state = STATE_PROBED;
315                 unregister_netdev(efx->net_dev);
316         }
317 }
318
319 void ef100_remove_netdev(struct efx_probe_data *probe_data)
320 {
321         struct efx_nic *efx = &probe_data->efx;
322
323         if (!efx->net_dev)
324                 return;
325
326         rtnl_lock();
327         dev_close(efx->net_dev);
328         rtnl_unlock();
329
330         unregister_netdevice_notifier(&efx->netdev_notifier);
331 #if defined(CONFIG_SFC_SRIOV)
332         if (!efx->type->is_vf)
333                 efx_ef100_pci_sriov_disable(efx, true);
334 #endif
335
336         efx_fini_devlink_lock(efx);
337         ef100_unregister_netdev(efx);
338
339 #ifdef CONFIG_SFC_SRIOV
340         ef100_pf_unset_devlink_port(efx);
341         efx_fini_tc(efx);
342 #endif
343
344         down_write(&efx->filter_sem);
345         efx_mcdi_filter_table_remove(efx);
346         up_write(&efx->filter_sem);
347         efx_fini_channels(efx);
348         kfree(efx->phy_data);
349         efx->phy_data = NULL;
350
351         efx_fini_devlink_and_unlock(efx);
352
353         free_netdev(efx->net_dev);
354         efx->net_dev = NULL;
355         efx->state = STATE_PROBED;
356 }
357
358 int ef100_probe_netdev(struct efx_probe_data *probe_data)
359 {
360         struct efx_nic *efx = &probe_data->efx;
361         struct efx_probe_data **probe_ptr;
362         struct ef100_nic_data *nic_data;
363         struct net_device *net_dev;
364         int rc;
365
366         if (efx->mcdi->fn_flags &
367                         (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_NO_ACTIVE_PORT)) {
368                 pci_info(efx->pci_dev, "No network port on this PCI function");
369                 return 0;
370         }
371
372         /* Allocate and initialise a struct net_device */
373         net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
374         if (!net_dev)
375                 return -ENOMEM;
376         probe_ptr = netdev_priv(net_dev);
377         *probe_ptr = probe_data;
378         efx->net_dev = net_dev;
379         SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
380
381         /* enable all supported features except rx-fcs and rx-all */
382         net_dev->features |= efx->type->offload_features &
383                              ~(NETIF_F_RXFCS | NETIF_F_RXALL);
384         net_dev->hw_features |= efx->type->offload_features;
385         net_dev->hw_enc_features |= efx->type->offload_features;
386         net_dev->vlan_features |= NETIF_F_HW_CSUM | NETIF_F_SG |
387                                   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO;
388         netif_set_tso_max_segs(net_dev,
389                                ESE_EF100_DP_GZ_TSO_MAX_HDR_NUM_SEGS_DEFAULT);
390         efx->mdio.dev = net_dev;
391
392         rc = efx_ef100_init_datapath_caps(efx);
393         if (rc < 0)
394                 goto fail;
395
396         rc = ef100_phy_probe(efx);
397         if (rc)
398                 goto fail;
399
400         rc = efx_init_channels(efx);
401         if (rc)
402                 goto fail;
403
404         down_write(&efx->filter_sem);
405         rc = ef100_filter_table_probe(efx);
406         up_write(&efx->filter_sem);
407         if (rc)
408                 goto fail;
409
410         netdev_rss_key_fill(efx->rss_context.rx_hash_key,
411                             sizeof(efx->rss_context.rx_hash_key));
412
413         /* Don't fail init if RSS setup doesn't work. */
414         efx_mcdi_push_default_indir_table(efx, efx->n_rx_channels);
415
416         nic_data = efx->nic_data;
417         rc = ef100_get_mac_address(efx, net_dev->perm_addr, CLIENT_HANDLE_SELF,
418                                    efx->type->is_vf);
419         if (rc)
420                 return rc;
421         /* Assign MAC address */
422         eth_hw_addr_set(net_dev, net_dev->perm_addr);
423         ether_addr_copy(nic_data->port_id, net_dev->perm_addr);
424
425         /* devlink creation, registration and lock */
426         rc = efx_probe_devlink_and_lock(efx);
427         if (rc)
428                 pci_info(efx->pci_dev, "devlink registration failed");
429
430         rc = ef100_register_netdev(efx);
431         if (rc)
432                 goto fail;
433
434         if (!efx->type->is_vf) {
435                 rc = ef100_probe_netdev_pf(efx);
436                 if (rc)
437                         goto fail;
438 #ifdef CONFIG_SFC_SRIOV
439                 ef100_pf_set_devlink_port(efx);
440 #endif
441         }
442
443         efx->netdev_notifier.notifier_call = ef100_netdev_event;
444         rc = register_netdevice_notifier(&efx->netdev_notifier);
445         if (rc) {
446                 netif_err(efx, probe, efx->net_dev,
447                           "Failed to register netdevice notifier, rc=%d\n", rc);
448                 goto fail;
449         }
450
451         efx_probe_devlink_unlock(efx);
452         return rc;
453 fail:
454 #ifdef CONFIG_SFC_SRIOV
455         /* remove devlink port if does exist */
456         ef100_pf_unset_devlink_port(efx);
457 #endif
458         efx_probe_devlink_unlock(efx);
459         return rc;
460 }