f900b375d9b15441342fef5f3612d8f0d0974701
[linux-2.6-microblaze.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 "efx_common.h"
20 #include "nic_common.h"
21 #include "ef100_nic.h"
22 #include "ef100_tx.h"
23 #include "ef100_regs.h"
24 #include "mcdi_filters.h"
25 #include "rx_common.h"
26
27 static void ef100_update_name(struct efx_nic *efx)
28 {
29         strcpy(efx->name, efx->net_dev->name);
30 }
31
32 /* Initiate a packet transmission.  We use one channel per CPU
33  * (sharing when we have more CPUs than channels).
34  *
35  * Context: non-blocking.
36  * Note that returning anything other than NETDEV_TX_OK will cause the
37  * OS to free the skb.
38  */
39 static netdev_tx_t ef100_hard_start_xmit(struct sk_buff *skb,
40                                          struct net_device *net_dev)
41 {
42         struct efx_nic *efx = netdev_priv(net_dev);
43         struct efx_tx_queue *tx_queue;
44         struct efx_channel *channel;
45         int rc;
46
47         channel = efx_get_tx_channel(efx, skb_get_queue_mapping(skb));
48         netif_vdbg(efx, tx_queued, efx->net_dev,
49                    "%s len %d data %d channel %d\n", __func__,
50                    skb->len, skb->data_len, channel->channel);
51         if (!efx->n_channels || !efx->n_tx_channels || !channel) {
52                 netif_stop_queue(net_dev);
53                 goto err;
54         }
55
56         tx_queue = &channel->tx_queue[0];
57         rc = ef100_enqueue_skb(tx_queue, skb);
58         if (rc == 0)
59                 return NETDEV_TX_OK;
60
61 err:
62         net_dev->stats.tx_dropped++;
63         return NETDEV_TX_OK;
64 }
65
66 static const struct net_device_ops ef100_netdev_ops = {
67         .ndo_start_xmit         = ef100_hard_start_xmit,
68 };
69
70 /*      Netdev registration
71  */
72 int ef100_netdev_event(struct notifier_block *this,
73                        unsigned long event, void *ptr)
74 {
75         struct efx_nic *efx = container_of(this, struct efx_nic, netdev_notifier);
76         struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
77
78         if (netdev_priv(net_dev) == efx && event == NETDEV_CHANGENAME)
79                 ef100_update_name(efx);
80
81         return NOTIFY_DONE;
82 }
83
84 int ef100_register_netdev(struct efx_nic *efx)
85 {
86         struct net_device *net_dev = efx->net_dev;
87         int rc;
88
89         net_dev->watchdog_timeo = 5 * HZ;
90         net_dev->irq = efx->pci_dev->irq;
91         net_dev->netdev_ops = &ef100_netdev_ops;
92         net_dev->min_mtu = EFX_MIN_MTU;
93         net_dev->max_mtu = EFX_MAX_MTU;
94         net_dev->ethtool_ops = &ef100_ethtool_ops;
95
96         rtnl_lock();
97
98         rc = dev_alloc_name(net_dev, net_dev->name);
99         if (rc < 0)
100                 goto fail_locked;
101         ef100_update_name(efx);
102
103         rc = register_netdevice(net_dev);
104         if (rc)
105                 goto fail_locked;
106
107         /* Always start with carrier off; PHY events will detect the link */
108         netif_carrier_off(net_dev);
109
110         efx->state = STATE_READY;
111         rtnl_unlock();
112         efx_init_mcdi_logging(efx);
113
114         return 0;
115
116 fail_locked:
117         rtnl_unlock();
118         netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
119         return rc;
120 }
121
122 void ef100_unregister_netdev(struct efx_nic *efx)
123 {
124         if (efx_dev_registered(efx)) {
125                 efx_fini_mcdi_logging(efx);
126                 efx->state = STATE_UNINIT;
127                 unregister_netdev(efx->net_dev);
128         }
129 }