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 static int ef100_alloc_vis(struct efx_nic *efx, unsigned int *allocated_vis)
33 {
34 	/* EF100 uses a single TXQ per channel, as all checksum offloading
35 	 * is configured in the TX descriptor, and there is no TX Pacer for
36 	 * HIGHPRI queues.
37 	 */
38 	unsigned int tx_vis = efx->n_tx_channels + efx->n_extra_tx_channels;
39 	unsigned int rx_vis = efx->n_rx_channels;
40 	unsigned int min_vis, max_vis;
41 
42 	EFX_WARN_ON_PARANOID(efx->tx_queues_per_channel != 1);
43 
44 	tx_vis += efx->n_xdp_channels * efx->xdp_tx_per_channel;
45 
46 	max_vis = max(rx_vis, tx_vis);
47 	/* Currently don't handle resource starvation and only accept
48 	 * our maximum needs and no less.
49 	 */
50 	min_vis = max_vis;
51 
52 	return efx_mcdi_alloc_vis(efx, min_vis, max_vis,
53 				  NULL, allocated_vis);
54 }
55 
56 static int ef100_remap_bar(struct efx_nic *efx, int max_vis)
57 {
58 	unsigned int uc_mem_map_size;
59 	void __iomem *membase;
60 
61 	efx->max_vis = max_vis;
62 	uc_mem_map_size = PAGE_ALIGN(max_vis * efx->vi_stride);
63 
64 	/* Extend the original UC mapping of the memory BAR */
65 	membase = ioremap(efx->membase_phys, uc_mem_map_size);
66 	if (!membase) {
67 		netif_err(efx, probe, efx->net_dev,
68 			  "could not extend memory BAR to %x\n",
69 			  uc_mem_map_size);
70 		return -ENOMEM;
71 	}
72 	iounmap(efx->membase);
73 	efx->membase = membase;
74 	return 0;
75 }
76 
77 /* Context: process, rtnl_lock() held.
78  * Note that the kernel will ignore our return code; this method
79  * should really be a void.
80  */
81 static int ef100_net_stop(struct net_device *net_dev)
82 {
83 	struct efx_nic *efx = netdev_priv(net_dev);
84 
85 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
86 		  raw_smp_processor_id());
87 
88 	netif_stop_queue(net_dev);
89 	efx_stop_all(efx);
90 	efx_disable_interrupts(efx);
91 	efx_clear_interrupt_affinity(efx);
92 	efx_nic_fini_interrupt(efx);
93 	efx_fini_napi(efx);
94 	efx_remove_channels(efx);
95 	efx_mcdi_free_vis(efx);
96 	efx_remove_interrupts(efx);
97 
98 	return 0;
99 }
100 
101 /* Context: process, rtnl_lock() held. */
102 static int ef100_net_open(struct net_device *net_dev)
103 {
104 	struct efx_nic *efx = netdev_priv(net_dev);
105 	unsigned int allocated_vis;
106 	int rc;
107 
108 	ef100_update_name(efx);
109 	netif_dbg(efx, ifup, net_dev, "opening device on CPU %d\n",
110 		  raw_smp_processor_id());
111 
112 	rc = efx_check_disabled(efx);
113 	if (rc)
114 		goto fail;
115 
116 	rc = efx_probe_interrupts(efx);
117 	if (rc)
118 		goto fail;
119 
120 	rc = efx_set_channels(efx);
121 	if (rc)
122 		goto fail;
123 
124 	rc = efx_mcdi_free_vis(efx);
125 	if (rc)
126 		goto fail;
127 
128 	rc = ef100_alloc_vis(efx, &allocated_vis);
129 	if (rc)
130 		goto fail;
131 
132 	rc = efx_probe_channels(efx);
133 	if (rc)
134 		return rc;
135 
136 	rc = ef100_remap_bar(efx, allocated_vis);
137 	if (rc)
138 		goto fail;
139 
140 	efx_init_napi(efx);
141 
142 	rc = efx_nic_init_interrupt(efx);
143 	if (rc)
144 		goto fail;
145 	efx_set_interrupt_affinity(efx);
146 
147 	rc = efx_enable_interrupts(efx);
148 	if (rc)
149 		goto fail;
150 
151 	/* in case the MC rebooted while we were stopped, consume the change
152 	 * to the warm reboot count
153 	 */
154 	(void) efx_mcdi_poll_reboot(efx);
155 
156 	efx_start_all(efx);
157 
158 	/* Link state detection is normally event-driven; we have
159 	 * to poll now because we could have missed a change
160 	 */
161 	mutex_lock(&efx->mac_lock);
162 	if (efx_mcdi_phy_poll(efx))
163 		efx_link_status_changed(efx);
164 	mutex_unlock(&efx->mac_lock);
165 
166 	return 0;
167 
168 fail:
169 	ef100_net_stop(net_dev);
170 	return rc;
171 }
172 
173 /* Initiate a packet transmission.  We use one channel per CPU
174  * (sharing when we have more CPUs than channels).
175  *
176  * Context: non-blocking.
177  * Note that returning anything other than NETDEV_TX_OK will cause the
178  * OS to free the skb.
179  */
180 static netdev_tx_t ef100_hard_start_xmit(struct sk_buff *skb,
181 					 struct net_device *net_dev)
182 {
183 	struct efx_nic *efx = netdev_priv(net_dev);
184 	struct efx_tx_queue *tx_queue;
185 	struct efx_channel *channel;
186 	int rc;
187 
188 	channel = efx_get_tx_channel(efx, skb_get_queue_mapping(skb));
189 	netif_vdbg(efx, tx_queued, efx->net_dev,
190 		   "%s len %d data %d channel %d\n", __func__,
191 		   skb->len, skb->data_len, channel->channel);
192 	if (!efx->n_channels || !efx->n_tx_channels || !channel) {
193 		netif_stop_queue(net_dev);
194 		goto err;
195 	}
196 
197 	tx_queue = &channel->tx_queue[0];
198 	rc = ef100_enqueue_skb(tx_queue, skb);
199 	if (rc == 0)
200 		return NETDEV_TX_OK;
201 
202 err:
203 	net_dev->stats.tx_dropped++;
204 	return NETDEV_TX_OK;
205 }
206 
207 static const struct net_device_ops ef100_netdev_ops = {
208 	.ndo_open               = ef100_net_open,
209 	.ndo_stop               = ef100_net_stop,
210 	.ndo_start_xmit         = ef100_hard_start_xmit,
211 	.ndo_get_phys_port_id   = efx_get_phys_port_id,
212 	.ndo_get_phys_port_name = efx_get_phys_port_name,
213 };
214 
215 /*	Netdev registration
216  */
217 int ef100_netdev_event(struct notifier_block *this,
218 		       unsigned long event, void *ptr)
219 {
220 	struct efx_nic *efx = container_of(this, struct efx_nic, netdev_notifier);
221 	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
222 
223 	if (netdev_priv(net_dev) == efx && event == NETDEV_CHANGENAME)
224 		ef100_update_name(efx);
225 
226 	return NOTIFY_DONE;
227 }
228 
229 int ef100_register_netdev(struct efx_nic *efx)
230 {
231 	struct net_device *net_dev = efx->net_dev;
232 	int rc;
233 
234 	net_dev->watchdog_timeo = 5 * HZ;
235 	net_dev->irq = efx->pci_dev->irq;
236 	net_dev->netdev_ops = &ef100_netdev_ops;
237 	net_dev->min_mtu = EFX_MIN_MTU;
238 	net_dev->max_mtu = EFX_MAX_MTU;
239 	net_dev->ethtool_ops = &ef100_ethtool_ops;
240 
241 	rtnl_lock();
242 
243 	rc = dev_alloc_name(net_dev, net_dev->name);
244 	if (rc < 0)
245 		goto fail_locked;
246 	ef100_update_name(efx);
247 
248 	rc = register_netdevice(net_dev);
249 	if (rc)
250 		goto fail_locked;
251 
252 	/* Always start with carrier off; PHY events will detect the link */
253 	netif_carrier_off(net_dev);
254 
255 	efx->state = STATE_READY;
256 	rtnl_unlock();
257 	efx_init_mcdi_logging(efx);
258 
259 	return 0;
260 
261 fail_locked:
262 	rtnl_unlock();
263 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
264 	return rc;
265 }
266 
267 void ef100_unregister_netdev(struct efx_nic *efx)
268 {
269 	if (efx_dev_registered(efx)) {
270 		efx_fini_mcdi_logging(efx);
271 		efx->state = STATE_UNINIT;
272 		unregister_netdev(efx->net_dev);
273 	}
274 }
275