xref: /openbmc/linux/drivers/net/ethernet/sfc/efx_common.c (revision 0760aad038b5a032c31ea124feed63d88627d2f1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2018 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10 
11 #include "net_driver.h"
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include "efx_common.h"
15 #include "efx_channels.h"
16 #include "efx.h"
17 #include "mcdi.h"
18 #include "selftest.h"
19 #include "rx_common.h"
20 #include "tx_common.h"
21 #include "nic.h"
22 #include "mcdi_port_common.h"
23 #include "io.h"
24 #include "mcdi_pcol.h"
25 
26 static unsigned int debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
27 			     NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
28 			     NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
29 			     NETIF_MSG_TX_ERR | NETIF_MSG_HW);
30 module_param(debug, uint, 0);
31 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
32 
33 /* This is the time (in jiffies) between invocations of the hardware
34  * monitor.
35  * On Falcon-based NICs, this will:
36  * - Check the on-board hardware monitor;
37  * - Poll the link state and reconfigure the hardware as necessary.
38  * On Siena-based NICs for power systems with EEH support, this will give EEH a
39  * chance to start.
40  */
41 static unsigned int efx_monitor_interval = 1 * HZ;
42 
43 /* How often and how many times to poll for a reset while waiting for a
44  * BIST that another function started to complete.
45  */
46 #define BIST_WAIT_DELAY_MS	100
47 #define BIST_WAIT_DELAY_COUNT	100
48 
49 /* Default stats update time */
50 #define STATS_PERIOD_MS_DEFAULT 1000
51 
52 const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
53 const char *const efx_reset_type_names[] = {
54 	[RESET_TYPE_INVISIBLE]          = "INVISIBLE",
55 	[RESET_TYPE_ALL]                = "ALL",
56 	[RESET_TYPE_RECOVER_OR_ALL]     = "RECOVER_OR_ALL",
57 	[RESET_TYPE_WORLD]              = "WORLD",
58 	[RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
59 	[RESET_TYPE_DATAPATH]           = "DATAPATH",
60 	[RESET_TYPE_MC_BIST]		= "MC_BIST",
61 	[RESET_TYPE_DISABLE]            = "DISABLE",
62 	[RESET_TYPE_TX_WATCHDOG]        = "TX_WATCHDOG",
63 	[RESET_TYPE_INT_ERROR]          = "INT_ERROR",
64 	[RESET_TYPE_DMA_ERROR]          = "DMA_ERROR",
65 	[RESET_TYPE_TX_SKIP]            = "TX_SKIP",
66 	[RESET_TYPE_MC_FAILURE]         = "MC_FAILURE",
67 	[RESET_TYPE_MCDI_TIMEOUT]	= "MCDI_TIMEOUT (FLR)",
68 };
69 
70 #define RESET_TYPE(type) \
71 	STRING_TABLE_LOOKUP(type, efx_reset_type)
72 
73 /* Loopback mode names (see LOOPBACK_MODE()) */
74 const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
75 const char *const efx_loopback_mode_names[] = {
76 	[LOOPBACK_NONE]		= "NONE",
77 	[LOOPBACK_DATA]		= "DATAPATH",
78 	[LOOPBACK_GMAC]		= "GMAC",
79 	[LOOPBACK_XGMII]	= "XGMII",
80 	[LOOPBACK_XGXS]		= "XGXS",
81 	[LOOPBACK_XAUI]		= "XAUI",
82 	[LOOPBACK_GMII]		= "GMII",
83 	[LOOPBACK_SGMII]	= "SGMII",
84 	[LOOPBACK_XGBR]		= "XGBR",
85 	[LOOPBACK_XFI]		= "XFI",
86 	[LOOPBACK_XAUI_FAR]	= "XAUI_FAR",
87 	[LOOPBACK_GMII_FAR]	= "GMII_FAR",
88 	[LOOPBACK_SGMII_FAR]	= "SGMII_FAR",
89 	[LOOPBACK_XFI_FAR]	= "XFI_FAR",
90 	[LOOPBACK_GPHY]		= "GPHY",
91 	[LOOPBACK_PHYXS]	= "PHYXS",
92 	[LOOPBACK_PCS]		= "PCS",
93 	[LOOPBACK_PMAPMD]	= "PMA/PMD",
94 	[LOOPBACK_XPORT]	= "XPORT",
95 	[LOOPBACK_XGMII_WS]	= "XGMII_WS",
96 	[LOOPBACK_XAUI_WS]	= "XAUI_WS",
97 	[LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
98 	[LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
99 	[LOOPBACK_GMII_WS]	= "GMII_WS",
100 	[LOOPBACK_XFI_WS]	= "XFI_WS",
101 	[LOOPBACK_XFI_WS_FAR]	= "XFI_WS_FAR",
102 	[LOOPBACK_PHYXS_WS]	= "PHYXS_WS",
103 };
104 
105 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
106  * queued onto this work queue. This is not a per-nic work queue, because
107  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
108  */
109 static struct workqueue_struct *reset_workqueue;
110 
111 int efx_create_reset_workqueue(void)
112 {
113 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
114 	if (!reset_workqueue) {
115 		printk(KERN_ERR "Failed to create reset workqueue\n");
116 		return -ENOMEM;
117 	}
118 
119 	return 0;
120 }
121 
122 void efx_queue_reset_work(struct efx_nic *efx)
123 {
124 	queue_work(reset_workqueue, &efx->reset_work);
125 }
126 
127 void efx_flush_reset_workqueue(struct efx_nic *efx)
128 {
129 	cancel_work_sync(&efx->reset_work);
130 }
131 
132 void efx_destroy_reset_workqueue(void)
133 {
134 	if (reset_workqueue) {
135 		destroy_workqueue(reset_workqueue);
136 		reset_workqueue = NULL;
137 	}
138 }
139 
140 /* We assume that efx->type->reconfigure_mac will always try to sync RX
141  * filters and therefore needs to read-lock the filter table against freeing
142  */
143 void efx_mac_reconfigure(struct efx_nic *efx, bool mtu_only)
144 {
145 	if (efx->type->reconfigure_mac) {
146 		down_read(&efx->filter_sem);
147 		efx->type->reconfigure_mac(efx, mtu_only);
148 		up_read(&efx->filter_sem);
149 	}
150 }
151 
152 /* Asynchronous work item for changing MAC promiscuity and multicast
153  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
154  * MAC directly.
155  */
156 static void efx_mac_work(struct work_struct *data)
157 {
158 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
159 
160 	mutex_lock(&efx->mac_lock);
161 	if (efx->port_enabled)
162 		efx_mac_reconfigure(efx, false);
163 	mutex_unlock(&efx->mac_lock);
164 }
165 
166 int efx_set_mac_address(struct net_device *net_dev, void *data)
167 {
168 	struct efx_nic *efx = netdev_priv(net_dev);
169 	struct sockaddr *addr = data;
170 	u8 *new_addr = addr->sa_data;
171 	u8 old_addr[6];
172 	int rc;
173 
174 	if (!is_valid_ether_addr(new_addr)) {
175 		netif_err(efx, drv, efx->net_dev,
176 			  "invalid ethernet MAC address requested: %pM\n",
177 			  new_addr);
178 		return -EADDRNOTAVAIL;
179 	}
180 
181 	/* save old address */
182 	ether_addr_copy(old_addr, net_dev->dev_addr);
183 	ether_addr_copy(net_dev->dev_addr, new_addr);
184 	if (efx->type->set_mac_address) {
185 		rc = efx->type->set_mac_address(efx);
186 		if (rc) {
187 			ether_addr_copy(net_dev->dev_addr, old_addr);
188 			return rc;
189 		}
190 	}
191 
192 	/* Reconfigure the MAC */
193 	mutex_lock(&efx->mac_lock);
194 	efx_mac_reconfigure(efx, false);
195 	mutex_unlock(&efx->mac_lock);
196 
197 	return 0;
198 }
199 
200 /* Context: netif_addr_lock held, BHs disabled. */
201 void efx_set_rx_mode(struct net_device *net_dev)
202 {
203 	struct efx_nic *efx = netdev_priv(net_dev);
204 
205 	if (efx->port_enabled)
206 		queue_work(efx->workqueue, &efx->mac_work);
207 	/* Otherwise efx_start_port() will do this */
208 }
209 
210 int efx_set_features(struct net_device *net_dev, netdev_features_t data)
211 {
212 	struct efx_nic *efx = netdev_priv(net_dev);
213 	int rc;
214 
215 	/* If disabling RX n-tuple filtering, clear existing filters */
216 	if (net_dev->features & ~data & NETIF_F_NTUPLE) {
217 		rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
218 		if (rc)
219 			return rc;
220 	}
221 
222 	/* If Rx VLAN filter is changed, update filters via mac_reconfigure.
223 	 * If rx-fcs is changed, mac_reconfigure updates that too.
224 	 */
225 	if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
226 					  NETIF_F_RXFCS)) {
227 		/* efx_set_rx_mode() will schedule MAC work to update filters
228 		 * when a new features are finally set in net_dev.
229 		 */
230 		efx_set_rx_mode(net_dev);
231 	}
232 
233 	return 0;
234 }
235 
236 /* This ensures that the kernel is kept informed (via
237  * netif_carrier_on/off) of the link status, and also maintains the
238  * link status's stop on the port's TX queue.
239  */
240 void efx_link_status_changed(struct efx_nic *efx)
241 {
242 	struct efx_link_state *link_state = &efx->link_state;
243 
244 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
245 	 * that no events are triggered between unregister_netdev() and the
246 	 * driver unloading. A more general condition is that NETDEV_CHANGE
247 	 * can only be generated between NETDEV_UP and NETDEV_DOWN
248 	 */
249 	if (!netif_running(efx->net_dev))
250 		return;
251 
252 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
253 		efx->n_link_state_changes++;
254 
255 		if (link_state->up)
256 			netif_carrier_on(efx->net_dev);
257 		else
258 			netif_carrier_off(efx->net_dev);
259 	}
260 
261 	/* Status message for kernel log */
262 	if (link_state->up)
263 		netif_info(efx, link, efx->net_dev,
264 			   "link up at %uMbps %s-duplex (MTU %d)\n",
265 			   link_state->speed, link_state->fd ? "full" : "half",
266 			   efx->net_dev->mtu);
267 	else
268 		netif_info(efx, link, efx->net_dev, "link down\n");
269 }
270 
271 unsigned int efx_xdp_max_mtu(struct efx_nic *efx)
272 {
273 	/* The maximum MTU that we can fit in a single page, allowing for
274 	 * framing, overhead and XDP headroom + tailroom.
275 	 */
276 	int overhead = EFX_MAX_FRAME_LEN(0) + sizeof(struct efx_rx_page_state) +
277 		       efx->rx_prefix_size + efx->type->rx_buffer_padding +
278 		       efx->rx_ip_align + EFX_XDP_HEADROOM + EFX_XDP_TAILROOM;
279 
280 	return PAGE_SIZE - overhead;
281 }
282 
283 /* Context: process, rtnl_lock() held. */
284 int efx_change_mtu(struct net_device *net_dev, int new_mtu)
285 {
286 	struct efx_nic *efx = netdev_priv(net_dev);
287 	int rc;
288 
289 	rc = efx_check_disabled(efx);
290 	if (rc)
291 		return rc;
292 
293 	if (rtnl_dereference(efx->xdp_prog) &&
294 	    new_mtu > efx_xdp_max_mtu(efx)) {
295 		netif_err(efx, drv, efx->net_dev,
296 			  "Requested MTU of %d too big for XDP (max: %d)\n",
297 			  new_mtu, efx_xdp_max_mtu(efx));
298 		return -EINVAL;
299 	}
300 
301 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
302 
303 	efx_device_detach_sync(efx);
304 	efx_stop_all(efx);
305 
306 	mutex_lock(&efx->mac_lock);
307 	net_dev->mtu = new_mtu;
308 	efx_mac_reconfigure(efx, true);
309 	mutex_unlock(&efx->mac_lock);
310 
311 	efx_start_all(efx);
312 	efx_device_attach_if_not_resetting(efx);
313 	return 0;
314 }
315 
316 /**************************************************************************
317  *
318  * Hardware monitor
319  *
320  **************************************************************************/
321 
322 /* Run periodically off the general workqueue */
323 static void efx_monitor(struct work_struct *data)
324 {
325 	struct efx_nic *efx = container_of(data, struct efx_nic,
326 					   monitor_work.work);
327 
328 	netif_vdbg(efx, timer, efx->net_dev,
329 		   "hardware monitor executing on CPU %d\n",
330 		   raw_smp_processor_id());
331 	BUG_ON(efx->type->monitor == NULL);
332 
333 	/* If the mac_lock is already held then it is likely a port
334 	 * reconfiguration is already in place, which will likely do
335 	 * most of the work of monitor() anyway.
336 	 */
337 	if (mutex_trylock(&efx->mac_lock)) {
338 		if (efx->port_enabled && efx->type->monitor)
339 			efx->type->monitor(efx);
340 		mutex_unlock(&efx->mac_lock);
341 	}
342 
343 	efx_start_monitor(efx);
344 }
345 
346 void efx_start_monitor(struct efx_nic *efx)
347 {
348 	if (efx->type->monitor)
349 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
350 				   efx_monitor_interval);
351 }
352 
353 /**************************************************************************
354  *
355  * Event queue processing
356  *
357  *************************************************************************/
358 
359 /* Channels are shutdown and reinitialised whilst the NIC is running
360  * to propagate configuration changes (mtu, checksum offload), or
361  * to clear hardware error conditions
362  */
363 static void efx_start_datapath(struct efx_nic *efx)
364 {
365 	netdev_features_t old_features = efx->net_dev->features;
366 	bool old_rx_scatter = efx->rx_scatter;
367 	size_t rx_buf_len;
368 
369 	/* Calculate the rx buffer allocation parameters required to
370 	 * support the current MTU, including padding for header
371 	 * alignment and overruns.
372 	 */
373 	efx->rx_dma_len = (efx->rx_prefix_size +
374 			   EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
375 			   efx->type->rx_buffer_padding);
376 	rx_buf_len = (sizeof(struct efx_rx_page_state)   + EFX_XDP_HEADROOM +
377 		      efx->rx_ip_align + efx->rx_dma_len + EFX_XDP_TAILROOM);
378 
379 	if (rx_buf_len <= PAGE_SIZE) {
380 		efx->rx_scatter = efx->type->always_rx_scatter;
381 		efx->rx_buffer_order = 0;
382 	} else if (efx->type->can_rx_scatter) {
383 		BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
384 		BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
385 			     2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
386 				       EFX_RX_BUF_ALIGNMENT) >
387 			     PAGE_SIZE);
388 		efx->rx_scatter = true;
389 		efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
390 		efx->rx_buffer_order = 0;
391 	} else {
392 		efx->rx_scatter = false;
393 		efx->rx_buffer_order = get_order(rx_buf_len);
394 	}
395 
396 	efx_rx_config_page_split(efx);
397 	if (efx->rx_buffer_order)
398 		netif_dbg(efx, drv, efx->net_dev,
399 			  "RX buf len=%u; page order=%u batch=%u\n",
400 			  efx->rx_dma_len, efx->rx_buffer_order,
401 			  efx->rx_pages_per_batch);
402 	else
403 		netif_dbg(efx, drv, efx->net_dev,
404 			  "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
405 			  efx->rx_dma_len, efx->rx_page_buf_step,
406 			  efx->rx_bufs_per_page, efx->rx_pages_per_batch);
407 
408 	/* Restore previously fixed features in hw_features and remove
409 	 * features which are fixed now
410 	 */
411 	efx->net_dev->hw_features |= efx->net_dev->features;
412 	efx->net_dev->hw_features &= ~efx->fixed_features;
413 	efx->net_dev->features |= efx->fixed_features;
414 	if (efx->net_dev->features != old_features)
415 		netdev_features_change(efx->net_dev);
416 
417 	/* RX filters may also have scatter-enabled flags */
418 	if ((efx->rx_scatter != old_rx_scatter) &&
419 	    efx->type->filter_update_rx_scatter)
420 		efx->type->filter_update_rx_scatter(efx);
421 
422 	/* We must keep at least one descriptor in a TX ring empty.
423 	 * We could avoid this when the queue size does not exactly
424 	 * match the hardware ring size, but it's not that important.
425 	 * Therefore we stop the queue when one more skb might fill
426 	 * the ring completely.  We wake it when half way back to
427 	 * empty.
428 	 */
429 	efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
430 	efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
431 
432 	/* Initialise the channels */
433 	efx_start_channels(efx);
434 
435 	efx_ptp_start_datapath(efx);
436 
437 	if (netif_device_present(efx->net_dev))
438 		netif_tx_wake_all_queues(efx->net_dev);
439 }
440 
441 static void efx_stop_datapath(struct efx_nic *efx)
442 {
443 	EFX_ASSERT_RESET_SERIALISED(efx);
444 	BUG_ON(efx->port_enabled);
445 
446 	efx_ptp_stop_datapath(efx);
447 
448 	efx_stop_channels(efx);
449 }
450 
451 /**************************************************************************
452  *
453  * Port handling
454  *
455  **************************************************************************/
456 
457 /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
458  * force the Autoneg bit on.
459  */
460 void efx_link_clear_advertising(struct efx_nic *efx)
461 {
462 	bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
463 	efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
464 }
465 
466 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
467 {
468 	efx->wanted_fc = wanted_fc;
469 	if (efx->link_advertising[0]) {
470 		if (wanted_fc & EFX_FC_RX)
471 			efx->link_advertising[0] |= (ADVERTISED_Pause |
472 						     ADVERTISED_Asym_Pause);
473 		else
474 			efx->link_advertising[0] &= ~(ADVERTISED_Pause |
475 						      ADVERTISED_Asym_Pause);
476 		if (wanted_fc & EFX_FC_TX)
477 			efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
478 	}
479 }
480 
481 static void efx_start_port(struct efx_nic *efx)
482 {
483 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
484 	BUG_ON(efx->port_enabled);
485 
486 	mutex_lock(&efx->mac_lock);
487 	efx->port_enabled = true;
488 
489 	/* Ensure MAC ingress/egress is enabled */
490 	efx_mac_reconfigure(efx, false);
491 
492 	mutex_unlock(&efx->mac_lock);
493 }
494 
495 /* Cancel work for MAC reconfiguration, periodic hardware monitoring
496  * and the async self-test, wait for them to finish and prevent them
497  * being scheduled again.  This doesn't cover online resets, which
498  * should only be cancelled when removing the device.
499  */
500 static void efx_stop_port(struct efx_nic *efx)
501 {
502 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
503 
504 	EFX_ASSERT_RESET_SERIALISED(efx);
505 
506 	mutex_lock(&efx->mac_lock);
507 	efx->port_enabled = false;
508 	mutex_unlock(&efx->mac_lock);
509 
510 	/* Serialise against efx_set_multicast_list() */
511 	netif_addr_lock_bh(efx->net_dev);
512 	netif_addr_unlock_bh(efx->net_dev);
513 
514 	cancel_delayed_work_sync(&efx->monitor_work);
515 	efx_selftest_async_cancel(efx);
516 	cancel_work_sync(&efx->mac_work);
517 }
518 
519 /* If the interface is supposed to be running but is not, start
520  * the hardware and software data path, regular activity for the port
521  * (MAC statistics, link polling, etc.) and schedule the port to be
522  * reconfigured.  Interrupts must already be enabled.  This function
523  * is safe to call multiple times, so long as the NIC is not disabled.
524  * Requires the RTNL lock.
525  */
526 void efx_start_all(struct efx_nic *efx)
527 {
528 	EFX_ASSERT_RESET_SERIALISED(efx);
529 	BUG_ON(efx->state == STATE_DISABLED);
530 
531 	/* Check that it is appropriate to restart the interface. All
532 	 * of these flags are safe to read under just the rtnl lock
533 	 */
534 	if (efx->port_enabled || !netif_running(efx->net_dev) ||
535 	    efx->reset_pending)
536 		return;
537 
538 	efx_start_port(efx);
539 	efx_start_datapath(efx);
540 
541 	/* Start the hardware monitor if there is one */
542 	efx_start_monitor(efx);
543 
544 	/* Link state detection is normally event-driven; we have
545 	 * to poll now because we could have missed a change
546 	 */
547 	mutex_lock(&efx->mac_lock);
548 	if (efx_mcdi_phy_poll(efx))
549 		efx_link_status_changed(efx);
550 	mutex_unlock(&efx->mac_lock);
551 
552 	if (efx->type->start_stats) {
553 		efx->type->start_stats(efx);
554 		efx->type->pull_stats(efx);
555 		spin_lock_bh(&efx->stats_lock);
556 		efx->type->update_stats(efx, NULL, NULL);
557 		spin_unlock_bh(&efx->stats_lock);
558 	}
559 }
560 
561 /* Quiesce the hardware and software data path, and regular activity
562  * for the port without bringing the link down.  Safe to call multiple
563  * times with the NIC in almost any state, but interrupts should be
564  * enabled.  Requires the RTNL lock.
565  */
566 void efx_stop_all(struct efx_nic *efx)
567 {
568 	EFX_ASSERT_RESET_SERIALISED(efx);
569 
570 	/* port_enabled can be read safely under the rtnl lock */
571 	if (!efx->port_enabled)
572 		return;
573 
574 	if (efx->type->update_stats) {
575 		/* update stats before we go down so we can accurately count
576 		 * rx_nodesc_drops
577 		 */
578 		efx->type->pull_stats(efx);
579 		spin_lock_bh(&efx->stats_lock);
580 		efx->type->update_stats(efx, NULL, NULL);
581 		spin_unlock_bh(&efx->stats_lock);
582 		efx->type->stop_stats(efx);
583 	}
584 
585 	efx_stop_port(efx);
586 
587 	/* Stop the kernel transmit interface.  This is only valid if
588 	 * the device is stopped or detached; otherwise the watchdog
589 	 * may fire immediately.
590 	 */
591 	WARN_ON(netif_running(efx->net_dev) &&
592 		netif_device_present(efx->net_dev));
593 	netif_tx_disable(efx->net_dev);
594 
595 	efx_stop_datapath(efx);
596 }
597 
598 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
599 void efx_net_stats(struct net_device *net_dev, struct rtnl_link_stats64 *stats)
600 {
601 	struct efx_nic *efx = netdev_priv(net_dev);
602 
603 	spin_lock_bh(&efx->stats_lock);
604 	efx->type->update_stats(efx, NULL, stats);
605 	spin_unlock_bh(&efx->stats_lock);
606 }
607 
608 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
609  * the MAC appropriately. All other PHY configuration changes are pushed
610  * through phy_op->set_settings(), and pushed asynchronously to the MAC
611  * through efx_monitor().
612  *
613  * Callers must hold the mac_lock
614  */
615 int __efx_reconfigure_port(struct efx_nic *efx)
616 {
617 	enum efx_phy_mode phy_mode;
618 	int rc = 0;
619 
620 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
621 
622 	/* Disable PHY transmit in mac level loopbacks */
623 	phy_mode = efx->phy_mode;
624 	if (LOOPBACK_INTERNAL(efx))
625 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
626 	else
627 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
628 
629 	if (efx->type->reconfigure_port)
630 		rc = efx->type->reconfigure_port(efx);
631 
632 	if (rc)
633 		efx->phy_mode = phy_mode;
634 
635 	return rc;
636 }
637 
638 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
639  * disabled.
640  */
641 int efx_reconfigure_port(struct efx_nic *efx)
642 {
643 	int rc;
644 
645 	EFX_ASSERT_RESET_SERIALISED(efx);
646 
647 	mutex_lock(&efx->mac_lock);
648 	rc = __efx_reconfigure_port(efx);
649 	mutex_unlock(&efx->mac_lock);
650 
651 	return rc;
652 }
653 
654 /**************************************************************************
655  *
656  * Device reset and suspend
657  *
658  **************************************************************************/
659 
660 static void efx_wait_for_bist_end(struct efx_nic *efx)
661 {
662 	int i;
663 
664 	for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
665 		if (efx_mcdi_poll_reboot(efx))
666 			goto out;
667 		msleep(BIST_WAIT_DELAY_MS);
668 	}
669 
670 	netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
671 out:
672 	/* Either way unset the BIST flag. If we found no reboot we probably
673 	 * won't recover, but we should try.
674 	 */
675 	efx->mc_bist_for_other_fn = false;
676 }
677 
678 /* Try recovery mechanisms.
679  * For now only EEH is supported.
680  * Returns 0 if the recovery mechanisms are unsuccessful.
681  * Returns a non-zero value otherwise.
682  */
683 int efx_try_recovery(struct efx_nic *efx)
684 {
685 #ifdef CONFIG_EEH
686 	/* A PCI error can occur and not be seen by EEH because nothing
687 	 * happens on the PCI bus. In this case the driver may fail and
688 	 * schedule a 'recover or reset', leading to this recovery handler.
689 	 * Manually call the eeh failure check function.
690 	 */
691 	struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
692 	if (eeh_dev_check_failure(eehdev)) {
693 		/* The EEH mechanisms will handle the error and reset the
694 		 * device if necessary.
695 		 */
696 		return 1;
697 	}
698 #endif
699 	return 0;
700 }
701 
702 /* Tears down the entire software state and most of the hardware state
703  * before reset.
704  */
705 void efx_reset_down(struct efx_nic *efx, enum reset_type method)
706 {
707 	EFX_ASSERT_RESET_SERIALISED(efx);
708 
709 	if (method == RESET_TYPE_MCDI_TIMEOUT)
710 		efx->type->prepare_flr(efx);
711 
712 	efx_stop_all(efx);
713 	efx_disable_interrupts(efx);
714 
715 	mutex_lock(&efx->mac_lock);
716 	down_write(&efx->filter_sem);
717 	mutex_lock(&efx->rss_lock);
718 	efx->type->fini(efx);
719 }
720 
721 /* Context: netif_tx_lock held, BHs disabled. */
722 void efx_watchdog(struct net_device *net_dev, unsigned int txqueue)
723 {
724 	struct efx_nic *efx = netdev_priv(net_dev);
725 
726 	netif_err(efx, tx_err, efx->net_dev,
727 		  "TX stuck with port_enabled=%d: resetting channels\n",
728 		  efx->port_enabled);
729 
730 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
731 }
732 
733 /* This function will always ensure that the locks acquired in
734  * efx_reset_down() are released. A failure return code indicates
735  * that we were unable to reinitialise the hardware, and the
736  * driver should be disabled. If ok is false, then the rx and tx
737  * engines are not restarted, pending a RESET_DISABLE.
738  */
739 int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
740 {
741 	int rc;
742 
743 	EFX_ASSERT_RESET_SERIALISED(efx);
744 
745 	if (method == RESET_TYPE_MCDI_TIMEOUT)
746 		efx->type->finish_flr(efx);
747 
748 	/* Ensure that SRAM is initialised even if we're disabling the device */
749 	rc = efx->type->init(efx);
750 	if (rc) {
751 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
752 		goto fail;
753 	}
754 
755 	if (!ok)
756 		goto fail;
757 
758 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
759 	    method != RESET_TYPE_DATAPATH) {
760 		rc = efx_mcdi_port_reconfigure(efx);
761 		if (rc && rc != -EPERM)
762 			netif_err(efx, drv, efx->net_dev,
763 				  "could not restore PHY settings\n");
764 	}
765 
766 	rc = efx_enable_interrupts(efx);
767 	if (rc)
768 		goto fail;
769 
770 #ifdef CONFIG_SFC_SRIOV
771 	rc = efx->type->vswitching_restore(efx);
772 	if (rc) /* not fatal; the PF will still work fine */
773 		netif_warn(efx, probe, efx->net_dev,
774 			   "failed to restore vswitching rc=%d;"
775 			   " VFs may not function\n", rc);
776 #endif
777 
778 	if (efx->type->rx_restore_rss_contexts)
779 		efx->type->rx_restore_rss_contexts(efx);
780 	mutex_unlock(&efx->rss_lock);
781 	efx->type->filter_table_restore(efx);
782 	up_write(&efx->filter_sem);
783 	if (efx->type->sriov_reset)
784 		efx->type->sriov_reset(efx);
785 
786 	mutex_unlock(&efx->mac_lock);
787 
788 	efx_start_all(efx);
789 
790 	if (efx->type->udp_tnl_push_ports)
791 		efx->type->udp_tnl_push_ports(efx);
792 
793 	return 0;
794 
795 fail:
796 	efx->port_initialized = false;
797 
798 	mutex_unlock(&efx->rss_lock);
799 	up_write(&efx->filter_sem);
800 	mutex_unlock(&efx->mac_lock);
801 
802 	return rc;
803 }
804 
805 /* Reset the NIC using the specified method.  Note that the reset may
806  * fail, in which case the card will be left in an unusable state.
807  *
808  * Caller must hold the rtnl_lock.
809  */
810 int efx_reset(struct efx_nic *efx, enum reset_type method)
811 {
812 	int rc, rc2 = 0;
813 	bool disabled;
814 
815 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
816 		   RESET_TYPE(method));
817 
818 	efx_device_detach_sync(efx);
819 	/* efx_reset_down() grabs locks that prevent recovery on EF100.
820 	 * EF100 reset is handled in the efx_nic_type callback below.
821 	 */
822 	if (efx_nic_rev(efx) != EFX_REV_EF100)
823 		efx_reset_down(efx, method);
824 
825 	rc = efx->type->reset(efx, method);
826 	if (rc) {
827 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
828 		goto out;
829 	}
830 
831 	/* Clear flags for the scopes we covered.  We assume the NIC and
832 	 * driver are now quiescent so that there is no race here.
833 	 */
834 	if (method < RESET_TYPE_MAX_METHOD)
835 		efx->reset_pending &= -(1 << (method + 1));
836 	else /* it doesn't fit into the well-ordered scope hierarchy */
837 		__clear_bit(method, &efx->reset_pending);
838 
839 	/* Reinitialise bus-mastering, which may have been turned off before
840 	 * the reset was scheduled. This is still appropriate, even in the
841 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
842 	 * can respond to requests.
843 	 */
844 	pci_set_master(efx->pci_dev);
845 
846 out:
847 	/* Leave device stopped if necessary */
848 	disabled = rc ||
849 		method == RESET_TYPE_DISABLE ||
850 		method == RESET_TYPE_RECOVER_OR_DISABLE;
851 	if (efx_nic_rev(efx) != EFX_REV_EF100)
852 		rc2 = efx_reset_up(efx, method, !disabled);
853 	if (rc2) {
854 		disabled = true;
855 		if (!rc)
856 			rc = rc2;
857 	}
858 
859 	if (disabled) {
860 		dev_close(efx->net_dev);
861 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
862 		efx->state = STATE_DISABLED;
863 	} else {
864 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
865 		efx_device_attach_if_not_resetting(efx);
866 	}
867 	return rc;
868 }
869 
870 /* The worker thread exists so that code that cannot sleep can
871  * schedule a reset for later.
872  */
873 static void efx_reset_work(struct work_struct *data)
874 {
875 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
876 	unsigned long pending;
877 	enum reset_type method;
878 
879 	pending = READ_ONCE(efx->reset_pending);
880 	method = fls(pending) - 1;
881 
882 	if (method == RESET_TYPE_MC_BIST)
883 		efx_wait_for_bist_end(efx);
884 
885 	if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
886 	     method == RESET_TYPE_RECOVER_OR_ALL) &&
887 	    efx_try_recovery(efx))
888 		return;
889 
890 	if (!pending)
891 		return;
892 
893 	rtnl_lock();
894 
895 	/* We checked the state in efx_schedule_reset() but it may
896 	 * have changed by now.  Now that we have the RTNL lock,
897 	 * it cannot change again.
898 	 */
899 	if (efx->state == STATE_READY)
900 		(void)efx_reset(efx, method);
901 
902 	rtnl_unlock();
903 }
904 
905 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
906 {
907 	enum reset_type method;
908 
909 	if (efx->state == STATE_RECOVERY) {
910 		netif_dbg(efx, drv, efx->net_dev,
911 			  "recovering: skip scheduling %s reset\n",
912 			  RESET_TYPE(type));
913 		return;
914 	}
915 
916 	switch (type) {
917 	case RESET_TYPE_INVISIBLE:
918 	case RESET_TYPE_ALL:
919 	case RESET_TYPE_RECOVER_OR_ALL:
920 	case RESET_TYPE_WORLD:
921 	case RESET_TYPE_DISABLE:
922 	case RESET_TYPE_RECOVER_OR_DISABLE:
923 	case RESET_TYPE_DATAPATH:
924 	case RESET_TYPE_MC_BIST:
925 	case RESET_TYPE_MCDI_TIMEOUT:
926 		method = type;
927 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
928 			  RESET_TYPE(method));
929 		break;
930 	default:
931 		method = efx->type->map_reset_reason(type);
932 		netif_dbg(efx, drv, efx->net_dev,
933 			  "scheduling %s reset for %s\n",
934 			  RESET_TYPE(method), RESET_TYPE(type));
935 		break;
936 	}
937 
938 	set_bit(method, &efx->reset_pending);
939 	smp_mb(); /* ensure we change reset_pending before checking state */
940 
941 	/* If we're not READY then just leave the flags set as the cue
942 	 * to abort probing or reschedule the reset later.
943 	 */
944 	if (READ_ONCE(efx->state) != STATE_READY)
945 		return;
946 
947 	/* efx_process_channel() will no longer read events once a
948 	 * reset is scheduled. So switch back to poll'd MCDI completions.
949 	 */
950 	efx_mcdi_mode_poll(efx);
951 
952 	efx_queue_reset_work(efx);
953 }
954 
955 /**************************************************************************
956  *
957  * Dummy NIC operations
958  *
959  * Can be used for some unimplemented operations
960  * Needed so all function pointers are valid and do not have to be tested
961  * before use
962  *
963  **************************************************************************/
964 int efx_port_dummy_op_int(struct efx_nic *efx)
965 {
966 	return 0;
967 }
968 void efx_port_dummy_op_void(struct efx_nic *efx) {}
969 
970 /**************************************************************************
971  *
972  * Data housekeeping
973  *
974  **************************************************************************/
975 
976 /* This zeroes out and then fills in the invariants in a struct
977  * efx_nic (including all sub-structures).
978  */
979 int efx_init_struct(struct efx_nic *efx,
980 		    struct pci_dev *pci_dev, struct net_device *net_dev)
981 {
982 	int rc = -ENOMEM;
983 
984 	/* Initialise common structures */
985 	INIT_LIST_HEAD(&efx->node);
986 	INIT_LIST_HEAD(&efx->secondary_list);
987 	spin_lock_init(&efx->biu_lock);
988 #ifdef CONFIG_SFC_MTD
989 	INIT_LIST_HEAD(&efx->mtd_list);
990 #endif
991 	INIT_WORK(&efx->reset_work, efx_reset_work);
992 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
993 	efx_selftest_async_init(efx);
994 	efx->pci_dev = pci_dev;
995 	efx->msg_enable = debug;
996 	efx->state = STATE_UNINIT;
997 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
998 
999 	efx->net_dev = net_dev;
1000 	efx->rx_prefix_size = efx->type->rx_prefix_size;
1001 	efx->rx_ip_align =
1002 		NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
1003 	efx->rx_packet_hash_offset =
1004 		efx->type->rx_hash_offset - efx->type->rx_prefix_size;
1005 	efx->rx_packet_ts_offset =
1006 		efx->type->rx_ts_offset - efx->type->rx_prefix_size;
1007 	INIT_LIST_HEAD(&efx->rss_context.list);
1008 	efx->rss_context.context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
1009 	mutex_init(&efx->rss_lock);
1010 	efx->vport_id = EVB_PORT_ID_ASSIGNED;
1011 	spin_lock_init(&efx->stats_lock);
1012 	efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
1013 	efx->num_mac_stats = MC_CMD_MAC_NSTATS;
1014 	BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
1015 	mutex_init(&efx->mac_lock);
1016 #ifdef CONFIG_RFS_ACCEL
1017 	mutex_init(&efx->rps_mutex);
1018 	spin_lock_init(&efx->rps_hash_lock);
1019 	/* Failure to allocate is not fatal, but may degrade ARFS performance */
1020 	efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
1021 				      sizeof(*efx->rps_hash_table), GFP_KERNEL);
1022 #endif
1023 	efx->mdio.dev = net_dev;
1024 	INIT_WORK(&efx->mac_work, efx_mac_work);
1025 	init_waitqueue_head(&efx->flush_wq);
1026 
1027 	efx->tx_queues_per_channel = 1;
1028 	efx->rxq_entries = EFX_DEFAULT_DMAQ_SIZE;
1029 	efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1030 
1031 	efx->mem_bar = UINT_MAX;
1032 
1033 	rc = efx_init_channels(efx);
1034 	if (rc)
1035 		goto fail;
1036 
1037 	/* Would be good to use the net_dev name, but we're too early */
1038 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
1039 		 pci_name(pci_dev));
1040 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
1041 	if (!efx->workqueue) {
1042 		rc = -ENOMEM;
1043 		goto fail;
1044 	}
1045 
1046 	return 0;
1047 
1048 fail:
1049 	efx_fini_struct(efx);
1050 	return rc;
1051 }
1052 
1053 void efx_fini_struct(struct efx_nic *efx)
1054 {
1055 #ifdef CONFIG_RFS_ACCEL
1056 	kfree(efx->rps_hash_table);
1057 #endif
1058 
1059 	efx_fini_channels(efx);
1060 
1061 	kfree(efx->vpd_sn);
1062 
1063 	if (efx->workqueue) {
1064 		destroy_workqueue(efx->workqueue);
1065 		efx->workqueue = NULL;
1066 	}
1067 }
1068 
1069 /* This configures the PCI device to enable I/O and DMA. */
1070 int efx_init_io(struct efx_nic *efx, int bar, dma_addr_t dma_mask,
1071 		unsigned int mem_map_size)
1072 {
1073 	struct pci_dev *pci_dev = efx->pci_dev;
1074 	int rc;
1075 
1076 	efx->mem_bar = UINT_MAX;
1077 
1078 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O bar=%d\n", bar);
1079 
1080 	rc = pci_enable_device(pci_dev);
1081 	if (rc) {
1082 		netif_err(efx, probe, efx->net_dev,
1083 			  "failed to enable PCI device\n");
1084 		goto fail1;
1085 	}
1086 
1087 	pci_set_master(pci_dev);
1088 
1089 	rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1090 	if (rc) {
1091 		netif_err(efx, probe, efx->net_dev,
1092 			  "could not find a suitable DMA mask\n");
1093 		goto fail2;
1094 	}
1095 	netif_dbg(efx, probe, efx->net_dev,
1096 		  "using DMA mask %llx\n", (unsigned long long)dma_mask);
1097 
1098 	efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1099 	if (!efx->membase_phys) {
1100 		netif_err(efx, probe, efx->net_dev,
1101 			  "ERROR: No BAR%d mapping from the BIOS. "
1102 			  "Try pci=realloc on the kernel command line\n", bar);
1103 		rc = -ENODEV;
1104 		goto fail3;
1105 	}
1106 
1107 	rc = pci_request_region(pci_dev, bar, "sfc");
1108 	if (rc) {
1109 		netif_err(efx, probe, efx->net_dev,
1110 			  "request for memory BAR[%d] failed\n", bar);
1111 		rc = -EIO;
1112 		goto fail3;
1113 	}
1114 	efx->mem_bar = bar;
1115 	efx->membase = ioremap(efx->membase_phys, mem_map_size);
1116 	if (!efx->membase) {
1117 		netif_err(efx, probe, efx->net_dev,
1118 			  "could not map memory BAR[%d] at %llx+%x\n", bar,
1119 			  (unsigned long long)efx->membase_phys, mem_map_size);
1120 		rc = -ENOMEM;
1121 		goto fail4;
1122 	}
1123 	netif_dbg(efx, probe, efx->net_dev,
1124 		  "memory BAR[%d] at %llx+%x (virtual %p)\n", bar,
1125 		  (unsigned long long)efx->membase_phys, mem_map_size,
1126 		  efx->membase);
1127 
1128 	return 0;
1129 
1130 fail4:
1131 	pci_release_region(efx->pci_dev, bar);
1132 fail3:
1133 	efx->membase_phys = 0;
1134 fail2:
1135 	pci_disable_device(efx->pci_dev);
1136 fail1:
1137 	return rc;
1138 }
1139 
1140 void efx_fini_io(struct efx_nic *efx)
1141 {
1142 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1143 
1144 	if (efx->membase) {
1145 		iounmap(efx->membase);
1146 		efx->membase = NULL;
1147 	}
1148 
1149 	if (efx->membase_phys) {
1150 		pci_release_region(efx->pci_dev, efx->mem_bar);
1151 		efx->membase_phys = 0;
1152 		efx->mem_bar = UINT_MAX;
1153 	}
1154 
1155 	/* Don't disable bus-mastering if VFs are assigned */
1156 	if (!pci_vfs_assigned(efx->pci_dev))
1157 		pci_disable_device(efx->pci_dev);
1158 }
1159 
1160 #ifdef CONFIG_SFC_MCDI_LOGGING
1161 static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
1162 			     char *buf)
1163 {
1164 	struct efx_nic *efx = dev_get_drvdata(dev);
1165 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1166 
1167 	return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
1168 }
1169 
1170 static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
1171 			    const char *buf, size_t count)
1172 {
1173 	struct efx_nic *efx = dev_get_drvdata(dev);
1174 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1175 	bool enable = count > 0 && *buf != '0';
1176 
1177 	mcdi->logging_enabled = enable;
1178 	return count;
1179 }
1180 
1181 static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
1182 
1183 void efx_init_mcdi_logging(struct efx_nic *efx)
1184 {
1185 	int rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
1186 
1187 	if (rc) {
1188 		netif_warn(efx, drv, efx->net_dev,
1189 			   "failed to init net dev attributes\n");
1190 	}
1191 }
1192 
1193 void efx_fini_mcdi_logging(struct efx_nic *efx)
1194 {
1195 	device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
1196 }
1197 #endif
1198 
1199 /* A PCI error affecting this device was detected.
1200  * At this point MMIO and DMA may be disabled.
1201  * Stop the software path and request a slot reset.
1202  */
1203 static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
1204 					      pci_channel_state_t state)
1205 {
1206 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
1207 	struct efx_nic *efx = pci_get_drvdata(pdev);
1208 
1209 	if (state == pci_channel_io_perm_failure)
1210 		return PCI_ERS_RESULT_DISCONNECT;
1211 
1212 	rtnl_lock();
1213 
1214 	if (efx->state != STATE_DISABLED) {
1215 		efx->state = STATE_RECOVERY;
1216 		efx->reset_pending = 0;
1217 
1218 		efx_device_detach_sync(efx);
1219 
1220 		efx_stop_all(efx);
1221 		efx_disable_interrupts(efx);
1222 
1223 		status = PCI_ERS_RESULT_NEED_RESET;
1224 	} else {
1225 		/* If the interface is disabled we don't want to do anything
1226 		 * with it.
1227 		 */
1228 		status = PCI_ERS_RESULT_RECOVERED;
1229 	}
1230 
1231 	rtnl_unlock();
1232 
1233 	pci_disable_device(pdev);
1234 
1235 	return status;
1236 }
1237 
1238 /* Fake a successful reset, which will be performed later in efx_io_resume. */
1239 static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
1240 {
1241 	struct efx_nic *efx = pci_get_drvdata(pdev);
1242 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
1243 
1244 	if (pci_enable_device(pdev)) {
1245 		netif_err(efx, hw, efx->net_dev,
1246 			  "Cannot re-enable PCI device after reset.\n");
1247 		status =  PCI_ERS_RESULT_DISCONNECT;
1248 	}
1249 
1250 	return status;
1251 }
1252 
1253 /* Perform the actual reset and resume I/O operations. */
1254 static void efx_io_resume(struct pci_dev *pdev)
1255 {
1256 	struct efx_nic *efx = pci_get_drvdata(pdev);
1257 	int rc;
1258 
1259 	rtnl_lock();
1260 
1261 	if (efx->state == STATE_DISABLED)
1262 		goto out;
1263 
1264 	rc = efx_reset(efx, RESET_TYPE_ALL);
1265 	if (rc) {
1266 		netif_err(efx, hw, efx->net_dev,
1267 			  "efx_reset failed after PCI error (%d)\n", rc);
1268 	} else {
1269 		efx->state = STATE_READY;
1270 		netif_dbg(efx, hw, efx->net_dev,
1271 			  "Done resetting and resuming IO after PCI error.\n");
1272 	}
1273 
1274 out:
1275 	rtnl_unlock();
1276 }
1277 
1278 /* For simplicity and reliability, we always require a slot reset and try to
1279  * reset the hardware when a pci error affecting the device is detected.
1280  * We leave both the link_reset and mmio_enabled callback unimplemented:
1281  * with our request for slot reset the mmio_enabled callback will never be
1282  * called, and the link_reset callback is not used by AER or EEH mechanisms.
1283  */
1284 const struct pci_error_handlers efx_err_handlers = {
1285 	.error_detected = efx_io_error_detected,
1286 	.slot_reset	= efx_io_slot_reset,
1287 	.resume		= efx_io_resume,
1288 };
1289 
1290 int efx_get_phys_port_id(struct net_device *net_dev,
1291 			 struct netdev_phys_item_id *ppid)
1292 {
1293 	struct efx_nic *efx = netdev_priv(net_dev);
1294 
1295 	if (efx->type->get_phys_port_id)
1296 		return efx->type->get_phys_port_id(efx, ppid);
1297 	else
1298 		return -EOPNOTSUPP;
1299 }
1300 
1301 int efx_get_phys_port_name(struct net_device *net_dev, char *name, size_t len)
1302 {
1303 	struct efx_nic *efx = netdev_priv(net_dev);
1304 
1305 	if (snprintf(name, len, "p%u", efx->port_num) >= len)
1306 		return -EINVAL;
1307 	return 0;
1308 }
1309