Commit | Line | Data |
---|---|---|
8ceee660 BH |
1 | /**************************************************************************** |
2 | * Driver for Solarflare Solarstorm network controllers and boards | |
3 | * Copyright 2005-2006 Fen Systems Ltd. | |
0a6f40c6 | 4 | * Copyright 2005-2011 Solarflare Communications Inc. |
8ceee660 BH |
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 <linux/module.h> | |
12 | #include <linux/pci.h> | |
13 | #include <linux/netdevice.h> | |
14 | #include <linux/etherdevice.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/notifier.h> | |
17 | #include <linux/ip.h> | |
18 | #include <linux/tcp.h> | |
19 | #include <linux/in.h> | |
20 | #include <linux/crc32.h> | |
21 | #include <linux/ethtool.h> | |
aa6ef27e | 22 | #include <linux/topology.h> |
5a0e3ad6 | 23 | #include <linux/gfp.h> |
64d8ad6d | 24 | #include <linux/cpu_rmap.h> |
8ceee660 | 25 | #include "net_driver.h" |
8ceee660 | 26 | #include "efx.h" |
744093c9 | 27 | #include "nic.h" |
8ceee660 | 28 | |
8880f4ec | 29 | #include "mcdi.h" |
fd371e32 | 30 | #include "workarounds.h" |
8880f4ec | 31 | |
c459302d BH |
32 | /************************************************************************** |
33 | * | |
34 | * Type name strings | |
35 | * | |
36 | ************************************************************************** | |
37 | */ | |
38 | ||
39 | /* Loopback mode names (see LOOPBACK_MODE()) */ | |
40 | const unsigned int efx_loopback_mode_max = LOOPBACK_MAX; | |
18e83e4c | 41 | const char *const efx_loopback_mode_names[] = { |
c459302d | 42 | [LOOPBACK_NONE] = "NONE", |
e58f69f4 | 43 | [LOOPBACK_DATA] = "DATAPATH", |
c459302d BH |
44 | [LOOPBACK_GMAC] = "GMAC", |
45 | [LOOPBACK_XGMII] = "XGMII", | |
46 | [LOOPBACK_XGXS] = "XGXS", | |
9c636baf BH |
47 | [LOOPBACK_XAUI] = "XAUI", |
48 | [LOOPBACK_GMII] = "GMII", | |
49 | [LOOPBACK_SGMII] = "SGMII", | |
e58f69f4 BH |
50 | [LOOPBACK_XGBR] = "XGBR", |
51 | [LOOPBACK_XFI] = "XFI", | |
52 | [LOOPBACK_XAUI_FAR] = "XAUI_FAR", | |
53 | [LOOPBACK_GMII_FAR] = "GMII_FAR", | |
54 | [LOOPBACK_SGMII_FAR] = "SGMII_FAR", | |
55 | [LOOPBACK_XFI_FAR] = "XFI_FAR", | |
c459302d BH |
56 | [LOOPBACK_GPHY] = "GPHY", |
57 | [LOOPBACK_PHYXS] = "PHYXS", | |
9c636baf BH |
58 | [LOOPBACK_PCS] = "PCS", |
59 | [LOOPBACK_PMAPMD] = "PMA/PMD", | |
e58f69f4 BH |
60 | [LOOPBACK_XPORT] = "XPORT", |
61 | [LOOPBACK_XGMII_WS] = "XGMII_WS", | |
9c636baf | 62 | [LOOPBACK_XAUI_WS] = "XAUI_WS", |
e58f69f4 BH |
63 | [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR", |
64 | [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR", | |
9c636baf | 65 | [LOOPBACK_GMII_WS] = "GMII_WS", |
e58f69f4 BH |
66 | [LOOPBACK_XFI_WS] = "XFI_WS", |
67 | [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR", | |
9c636baf | 68 | [LOOPBACK_PHYXS_WS] = "PHYXS_WS", |
c459302d BH |
69 | }; |
70 | ||
c459302d | 71 | const unsigned int efx_reset_type_max = RESET_TYPE_MAX; |
18e83e4c | 72 | const char *const efx_reset_type_names[] = { |
c459302d BH |
73 | [RESET_TYPE_INVISIBLE] = "INVISIBLE", |
74 | [RESET_TYPE_ALL] = "ALL", | |
75 | [RESET_TYPE_WORLD] = "WORLD", | |
76 | [RESET_TYPE_DISABLE] = "DISABLE", | |
77 | [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG", | |
78 | [RESET_TYPE_INT_ERROR] = "INT_ERROR", | |
79 | [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY", | |
80 | [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH", | |
81 | [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH", | |
82 | [RESET_TYPE_TX_SKIP] = "TX_SKIP", | |
8880f4ec | 83 | [RESET_TYPE_MC_FAILURE] = "MC_FAILURE", |
c459302d BH |
84 | }; |
85 | ||
8ceee660 BH |
86 | #define EFX_MAX_MTU (9 * 1024) |
87 | ||
1ab00629 SH |
88 | /* Reset workqueue. If any NIC has a hardware failure then a reset will be |
89 | * queued onto this work queue. This is not a per-nic work queue, because | |
90 | * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised. | |
91 | */ | |
92 | static struct workqueue_struct *reset_workqueue; | |
93 | ||
8ceee660 BH |
94 | /************************************************************************** |
95 | * | |
96 | * Configurable values | |
97 | * | |
98 | *************************************************************************/ | |
99 | ||
8ceee660 BH |
100 | /* |
101 | * Use separate channels for TX and RX events | |
102 | * | |
28b581ab NT |
103 | * Set this to 1 to use separate channels for TX and RX. It allows us |
104 | * to control interrupt affinity separately for TX and RX. | |
8ceee660 | 105 | * |
28b581ab | 106 | * This is only used in MSI-X interrupt mode |
8ceee660 | 107 | */ |
28b581ab | 108 | static unsigned int separate_tx_channels; |
8313aca3 | 109 | module_param(separate_tx_channels, uint, 0444); |
28b581ab NT |
110 | MODULE_PARM_DESC(separate_tx_channels, |
111 | "Use separate channels for TX and RX"); | |
8ceee660 BH |
112 | |
113 | /* This is the weight assigned to each of the (per-channel) virtual | |
114 | * NAPI devices. | |
115 | */ | |
116 | static int napi_weight = 64; | |
117 | ||
118 | /* This is the time (in jiffies) between invocations of the hardware | |
e254c274 BH |
119 | * monitor. On Falcon-based NICs, this will: |
120 | * - Check the on-board hardware monitor; | |
121 | * - Poll the link state and reconfigure the hardware as necessary. | |
8ceee660 | 122 | */ |
d215697f | 123 | static unsigned int efx_monitor_interval = 1 * HZ; |
8ceee660 | 124 | |
8ceee660 BH |
125 | /* Initial interrupt moderation settings. They can be modified after |
126 | * module load with ethtool. | |
127 | * | |
128 | * The default for RX should strike a balance between increasing the | |
129 | * round-trip latency and reducing overhead. | |
130 | */ | |
131 | static unsigned int rx_irq_mod_usec = 60; | |
132 | ||
133 | /* Initial interrupt moderation settings. They can be modified after | |
134 | * module load with ethtool. | |
135 | * | |
136 | * This default is chosen to ensure that a 10G link does not go idle | |
137 | * while a TX queue is stopped after it has become full. A queue is | |
138 | * restarted when it drops below half full. The time this takes (assuming | |
139 | * worst case 3 descriptors per packet and 1024 descriptors) is | |
140 | * 512 / 3 * 1.2 = 205 usec. | |
141 | */ | |
142 | static unsigned int tx_irq_mod_usec = 150; | |
143 | ||
144 | /* This is the first interrupt mode to try out of: | |
145 | * 0 => MSI-X | |
146 | * 1 => MSI | |
147 | * 2 => legacy | |
148 | */ | |
149 | static unsigned int interrupt_mode; | |
150 | ||
151 | /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS), | |
152 | * i.e. the number of CPUs among which we may distribute simultaneous | |
153 | * interrupt handling. | |
154 | * | |
155 | * Cards without MSI-X will only target one CPU via legacy or MSI interrupt. | |
cdb08f8f | 156 | * The default (0) means to assign an interrupt to each core. |
8ceee660 BH |
157 | */ |
158 | static unsigned int rss_cpus; | |
159 | module_param(rss_cpus, uint, 0444); | |
160 | MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling"); | |
161 | ||
84ae48fe BH |
162 | static int phy_flash_cfg; |
163 | module_param(phy_flash_cfg, int, 0644); | |
164 | MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially"); | |
165 | ||
6fb70fd1 BH |
166 | static unsigned irq_adapt_low_thresh = 10000; |
167 | module_param(irq_adapt_low_thresh, uint, 0644); | |
168 | MODULE_PARM_DESC(irq_adapt_low_thresh, | |
169 | "Threshold score for reducing IRQ moderation"); | |
170 | ||
171 | static unsigned irq_adapt_high_thresh = 20000; | |
172 | module_param(irq_adapt_high_thresh, uint, 0644); | |
173 | MODULE_PARM_DESC(irq_adapt_high_thresh, | |
174 | "Threshold score for increasing IRQ moderation"); | |
175 | ||
62776d03 BH |
176 | static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE | |
177 | NETIF_MSG_LINK | NETIF_MSG_IFDOWN | | |
178 | NETIF_MSG_IFUP | NETIF_MSG_RX_ERR | | |
179 | NETIF_MSG_TX_ERR | NETIF_MSG_HW); | |
180 | module_param(debug, uint, 0); | |
181 | MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value"); | |
182 | ||
8ceee660 BH |
183 | /************************************************************************** |
184 | * | |
185 | * Utility functions and prototypes | |
186 | * | |
187 | *************************************************************************/ | |
4642610c | 188 | |
7f967c01 BH |
189 | static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq); |
190 | static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq); | |
191 | static void efx_remove_channel(struct efx_channel *channel); | |
4642610c | 192 | static void efx_remove_channels(struct efx_nic *efx); |
7f967c01 | 193 | static const struct efx_channel_type efx_default_channel_type; |
8ceee660 | 194 | static void efx_remove_port(struct efx_nic *efx); |
7f967c01 | 195 | static void efx_init_napi_channel(struct efx_channel *channel); |
8ceee660 | 196 | static void efx_fini_napi(struct efx_nic *efx); |
e8f14992 | 197 | static void efx_fini_napi_channel(struct efx_channel *channel); |
4642610c BH |
198 | static void efx_fini_struct(struct efx_nic *efx); |
199 | static void efx_start_all(struct efx_nic *efx); | |
200 | static void efx_stop_all(struct efx_nic *efx); | |
8ceee660 BH |
201 | |
202 | #define EFX_ASSERT_RESET_SERIALISED(efx) \ | |
203 | do { \ | |
332c1ce9 BH |
204 | if ((efx->state == STATE_RUNNING) || \ |
205 | (efx->state == STATE_DISABLED)) \ | |
8ceee660 BH |
206 | ASSERT_RTNL(); \ |
207 | } while (0) | |
208 | ||
209 | /************************************************************************** | |
210 | * | |
211 | * Event queue processing | |
212 | * | |
213 | *************************************************************************/ | |
214 | ||
215 | /* Process channel's event queue | |
216 | * | |
217 | * This function is responsible for processing the event queue of a | |
218 | * single channel. The caller must guarantee that this function will | |
219 | * never be concurrently called more than once on the same channel, | |
220 | * though different channels may be being processed concurrently. | |
221 | */ | |
fa236e18 | 222 | static int efx_process_channel(struct efx_channel *channel, int budget) |
8ceee660 | 223 | { |
fa236e18 | 224 | int spent; |
8ceee660 | 225 | |
9f2cb71c | 226 | if (unlikely(!channel->enabled)) |
42cbe2d7 | 227 | return 0; |
8ceee660 | 228 | |
fa236e18 | 229 | spent = efx_nic_process_eventq(channel, budget); |
d9ab7007 BH |
230 | if (spent && efx_channel_has_rx_queue(channel)) { |
231 | struct efx_rx_queue *rx_queue = | |
232 | efx_channel_get_rx_queue(channel); | |
233 | ||
234 | /* Deliver last RX packet. */ | |
235 | if (channel->rx_pkt) { | |
236 | __efx_rx_packet(channel, channel->rx_pkt); | |
237 | channel->rx_pkt = NULL; | |
238 | } | |
9f2cb71c BH |
239 | if (rx_queue->enabled) { |
240 | efx_rx_strategy(channel); | |
241 | efx_fast_push_rx_descriptors(rx_queue); | |
242 | } | |
8ceee660 BH |
243 | } |
244 | ||
fa236e18 | 245 | return spent; |
8ceee660 BH |
246 | } |
247 | ||
248 | /* Mark channel as finished processing | |
249 | * | |
250 | * Note that since we will not receive further interrupts for this | |
251 | * channel before we finish processing and call the eventq_read_ack() | |
252 | * method, there is no need to use the interrupt hold-off timers. | |
253 | */ | |
254 | static inline void efx_channel_processed(struct efx_channel *channel) | |
255 | { | |
5b9e207c BH |
256 | /* The interrupt handler for this channel may set work_pending |
257 | * as soon as we acknowledge the events we've seen. Make sure | |
258 | * it's cleared before then. */ | |
dc8cfa55 | 259 | channel->work_pending = false; |
5b9e207c BH |
260 | smp_wmb(); |
261 | ||
152b6a62 | 262 | efx_nic_eventq_read_ack(channel); |
8ceee660 BH |
263 | } |
264 | ||
265 | /* NAPI poll handler | |
266 | * | |
267 | * NAPI guarantees serialisation of polls of the same device, which | |
268 | * provides the guarantee required by efx_process_channel(). | |
269 | */ | |
270 | static int efx_poll(struct napi_struct *napi, int budget) | |
271 | { | |
272 | struct efx_channel *channel = | |
273 | container_of(napi, struct efx_channel, napi_str); | |
62776d03 | 274 | struct efx_nic *efx = channel->efx; |
fa236e18 | 275 | int spent; |
8ceee660 | 276 | |
62776d03 BH |
277 | netif_vdbg(efx, intr, efx->net_dev, |
278 | "channel %d NAPI poll executing on CPU %d\n", | |
279 | channel->channel, raw_smp_processor_id()); | |
8ceee660 | 280 | |
fa236e18 | 281 | spent = efx_process_channel(channel, budget); |
8ceee660 | 282 | |
fa236e18 | 283 | if (spent < budget) { |
9d9a6973 | 284 | if (efx_channel_has_rx_queue(channel) && |
6fb70fd1 BH |
285 | efx->irq_rx_adaptive && |
286 | unlikely(++channel->irq_count == 1000)) { | |
6fb70fd1 BH |
287 | if (unlikely(channel->irq_mod_score < |
288 | irq_adapt_low_thresh)) { | |
0d86ebd8 BH |
289 | if (channel->irq_moderation > 1) { |
290 | channel->irq_moderation -= 1; | |
ef2b90ee | 291 | efx->type->push_irq_moderation(channel); |
0d86ebd8 | 292 | } |
6fb70fd1 BH |
293 | } else if (unlikely(channel->irq_mod_score > |
294 | irq_adapt_high_thresh)) { | |
0d86ebd8 BH |
295 | if (channel->irq_moderation < |
296 | efx->irq_rx_moderation) { | |
297 | channel->irq_moderation += 1; | |
ef2b90ee | 298 | efx->type->push_irq_moderation(channel); |
0d86ebd8 | 299 | } |
6fb70fd1 | 300 | } |
6fb70fd1 BH |
301 | channel->irq_count = 0; |
302 | channel->irq_mod_score = 0; | |
303 | } | |
304 | ||
64d8ad6d BH |
305 | efx_filter_rfs_expire(channel); |
306 | ||
8ceee660 | 307 | /* There is no race here; although napi_disable() will |
288379f0 | 308 | * only wait for napi_complete(), this isn't a problem |
8ceee660 BH |
309 | * since efx_channel_processed() will have no effect if |
310 | * interrupts have already been disabled. | |
311 | */ | |
288379f0 | 312 | napi_complete(napi); |
8ceee660 BH |
313 | efx_channel_processed(channel); |
314 | } | |
315 | ||
fa236e18 | 316 | return spent; |
8ceee660 BH |
317 | } |
318 | ||
319 | /* Process the eventq of the specified channel immediately on this CPU | |
320 | * | |
321 | * Disable hardware generated interrupts, wait for any existing | |
322 | * processing to finish, then directly poll (and ack ) the eventq. | |
323 | * Finally reenable NAPI and interrupts. | |
324 | * | |
d4fabcc8 BH |
325 | * This is for use only during a loopback self-test. It must not |
326 | * deliver any packets up the stack as this can result in deadlock. | |
8ceee660 BH |
327 | */ |
328 | void efx_process_channel_now(struct efx_channel *channel) | |
329 | { | |
330 | struct efx_nic *efx = channel->efx; | |
331 | ||
8313aca3 | 332 | BUG_ON(channel->channel >= efx->n_channels); |
8ceee660 | 333 | BUG_ON(!channel->enabled); |
d4fabcc8 | 334 | BUG_ON(!efx->loopback_selftest); |
8ceee660 BH |
335 | |
336 | /* Disable interrupts and wait for ISRs to complete */ | |
152b6a62 | 337 | efx_nic_disable_interrupts(efx); |
94dec6a2 | 338 | if (efx->legacy_irq) { |
8ceee660 | 339 | synchronize_irq(efx->legacy_irq); |
94dec6a2 BH |
340 | efx->legacy_irq_enabled = false; |
341 | } | |
64ee3120 | 342 | if (channel->irq) |
8ceee660 BH |
343 | synchronize_irq(channel->irq); |
344 | ||
345 | /* Wait for any NAPI processing to complete */ | |
346 | napi_disable(&channel->napi_str); | |
347 | ||
348 | /* Poll the channel */ | |
ecc910f5 | 349 | efx_process_channel(channel, channel->eventq_mask + 1); |
8ceee660 BH |
350 | |
351 | /* Ack the eventq. This may cause an interrupt to be generated | |
352 | * when they are reenabled */ | |
353 | efx_channel_processed(channel); | |
354 | ||
355 | napi_enable(&channel->napi_str); | |
94dec6a2 BH |
356 | if (efx->legacy_irq) |
357 | efx->legacy_irq_enabled = true; | |
152b6a62 | 358 | efx_nic_enable_interrupts(efx); |
8ceee660 BH |
359 | } |
360 | ||
361 | /* Create event queue | |
362 | * Event queue memory allocations are done only once. If the channel | |
363 | * is reset, the memory buffer will be reused; this guards against | |
364 | * errors during channel reset and also simplifies interrupt handling. | |
365 | */ | |
366 | static int efx_probe_eventq(struct efx_channel *channel) | |
367 | { | |
ecc910f5 SH |
368 | struct efx_nic *efx = channel->efx; |
369 | unsigned long entries; | |
370 | ||
86ee5302 | 371 | netif_dbg(efx, probe, efx->net_dev, |
62776d03 | 372 | "chan %d create event queue\n", channel->channel); |
8ceee660 | 373 | |
ecc910f5 SH |
374 | /* Build an event queue with room for one event per tx and rx buffer, |
375 | * plus some extra for link state events and MCDI completions. */ | |
376 | entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128); | |
377 | EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE); | |
378 | channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1; | |
379 | ||
152b6a62 | 380 | return efx_nic_probe_eventq(channel); |
8ceee660 BH |
381 | } |
382 | ||
383 | /* Prepare channel's event queue */ | |
bc3c90a2 | 384 | static void efx_init_eventq(struct efx_channel *channel) |
8ceee660 | 385 | { |
62776d03 BH |
386 | netif_dbg(channel->efx, drv, channel->efx->net_dev, |
387 | "chan %d init event queue\n", channel->channel); | |
8ceee660 BH |
388 | |
389 | channel->eventq_read_ptr = 0; | |
390 | ||
152b6a62 | 391 | efx_nic_init_eventq(channel); |
8ceee660 BH |
392 | } |
393 | ||
9f2cb71c BH |
394 | /* Enable event queue processing and NAPI */ |
395 | static void efx_start_eventq(struct efx_channel *channel) | |
396 | { | |
397 | netif_dbg(channel->efx, ifup, channel->efx->net_dev, | |
398 | "chan %d start event queue\n", channel->channel); | |
399 | ||
400 | /* The interrupt handler for this channel may set work_pending | |
401 | * as soon as we enable it. Make sure it's cleared before | |
402 | * then. Similarly, make sure it sees the enabled flag set. | |
403 | */ | |
404 | channel->work_pending = false; | |
405 | channel->enabled = true; | |
406 | smp_wmb(); | |
407 | ||
408 | napi_enable(&channel->napi_str); | |
409 | efx_nic_eventq_read_ack(channel); | |
410 | } | |
411 | ||
412 | /* Disable event queue processing and NAPI */ | |
413 | static void efx_stop_eventq(struct efx_channel *channel) | |
414 | { | |
415 | if (!channel->enabled) | |
416 | return; | |
417 | ||
418 | napi_disable(&channel->napi_str); | |
419 | channel->enabled = false; | |
420 | } | |
421 | ||
8ceee660 BH |
422 | static void efx_fini_eventq(struct efx_channel *channel) |
423 | { | |
62776d03 BH |
424 | netif_dbg(channel->efx, drv, channel->efx->net_dev, |
425 | "chan %d fini event queue\n", channel->channel); | |
8ceee660 | 426 | |
152b6a62 | 427 | efx_nic_fini_eventq(channel); |
8ceee660 BH |
428 | } |
429 | ||
430 | static void efx_remove_eventq(struct efx_channel *channel) | |
431 | { | |
62776d03 BH |
432 | netif_dbg(channel->efx, drv, channel->efx->net_dev, |
433 | "chan %d remove event queue\n", channel->channel); | |
8ceee660 | 434 | |
152b6a62 | 435 | efx_nic_remove_eventq(channel); |
8ceee660 BH |
436 | } |
437 | ||
438 | /************************************************************************** | |
439 | * | |
440 | * Channel handling | |
441 | * | |
442 | *************************************************************************/ | |
443 | ||
7f967c01 | 444 | /* Allocate and initialise a channel structure. */ |
4642610c BH |
445 | static struct efx_channel * |
446 | efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel) | |
447 | { | |
448 | struct efx_channel *channel; | |
449 | struct efx_rx_queue *rx_queue; | |
450 | struct efx_tx_queue *tx_queue; | |
451 | int j; | |
452 | ||
7f967c01 BH |
453 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
454 | if (!channel) | |
455 | return NULL; | |
4642610c | 456 | |
7f967c01 BH |
457 | channel->efx = efx; |
458 | channel->channel = i; | |
459 | channel->type = &efx_default_channel_type; | |
4642610c | 460 | |
7f967c01 BH |
461 | for (j = 0; j < EFX_TXQ_TYPES; j++) { |
462 | tx_queue = &channel->tx_queue[j]; | |
463 | tx_queue->efx = efx; | |
464 | tx_queue->queue = i * EFX_TXQ_TYPES + j; | |
465 | tx_queue->channel = channel; | |
466 | } | |
4642610c | 467 | |
7f967c01 BH |
468 | rx_queue = &channel->rx_queue; |
469 | rx_queue->efx = efx; | |
470 | setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill, | |
471 | (unsigned long)rx_queue); | |
4642610c | 472 | |
7f967c01 BH |
473 | return channel; |
474 | } | |
475 | ||
476 | /* Allocate and initialise a channel structure, copying parameters | |
477 | * (but not resources) from an old channel structure. | |
478 | */ | |
479 | static struct efx_channel * | |
480 | efx_copy_channel(const struct efx_channel *old_channel) | |
481 | { | |
482 | struct efx_channel *channel; | |
483 | struct efx_rx_queue *rx_queue; | |
484 | struct efx_tx_queue *tx_queue; | |
485 | int j; | |
4642610c | 486 | |
7f967c01 BH |
487 | channel = kmalloc(sizeof(*channel), GFP_KERNEL); |
488 | if (!channel) | |
489 | return NULL; | |
490 | ||
491 | *channel = *old_channel; | |
492 | ||
493 | channel->napi_dev = NULL; | |
494 | memset(&channel->eventq, 0, sizeof(channel->eventq)); | |
4642610c | 495 | |
7f967c01 BH |
496 | for (j = 0; j < EFX_TXQ_TYPES; j++) { |
497 | tx_queue = &channel->tx_queue[j]; | |
498 | if (tx_queue->channel) | |
4642610c | 499 | tx_queue->channel = channel; |
7f967c01 BH |
500 | tx_queue->buffer = NULL; |
501 | memset(&tx_queue->txd, 0, sizeof(tx_queue->txd)); | |
4642610c BH |
502 | } |
503 | ||
4642610c | 504 | rx_queue = &channel->rx_queue; |
7f967c01 BH |
505 | rx_queue->buffer = NULL; |
506 | memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd)); | |
4642610c BH |
507 | setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill, |
508 | (unsigned long)rx_queue); | |
509 | ||
510 | return channel; | |
511 | } | |
512 | ||
8ceee660 BH |
513 | static int efx_probe_channel(struct efx_channel *channel) |
514 | { | |
515 | struct efx_tx_queue *tx_queue; | |
516 | struct efx_rx_queue *rx_queue; | |
517 | int rc; | |
518 | ||
62776d03 BH |
519 | netif_dbg(channel->efx, probe, channel->efx->net_dev, |
520 | "creating channel %d\n", channel->channel); | |
8ceee660 | 521 | |
7f967c01 BH |
522 | rc = channel->type->pre_probe(channel); |
523 | if (rc) | |
524 | goto fail; | |
525 | ||
8ceee660 BH |
526 | rc = efx_probe_eventq(channel); |
527 | if (rc) | |
7f967c01 | 528 | goto fail; |
8ceee660 BH |
529 | |
530 | efx_for_each_channel_tx_queue(tx_queue, channel) { | |
531 | rc = efx_probe_tx_queue(tx_queue); | |
532 | if (rc) | |
7f967c01 | 533 | goto fail; |
8ceee660 BH |
534 | } |
535 | ||
536 | efx_for_each_channel_rx_queue(rx_queue, channel) { | |
537 | rc = efx_probe_rx_queue(rx_queue); | |
538 | if (rc) | |
7f967c01 | 539 | goto fail; |
8ceee660 BH |
540 | } |
541 | ||
542 | channel->n_rx_frm_trunc = 0; | |
543 | ||
544 | return 0; | |
545 | ||
7f967c01 BH |
546 | fail: |
547 | efx_remove_channel(channel); | |
8ceee660 BH |
548 | return rc; |
549 | } | |
550 | ||
7f967c01 BH |
551 | static void |
552 | efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len) | |
553 | { | |
554 | struct efx_nic *efx = channel->efx; | |
555 | const char *type; | |
556 | int number; | |
557 | ||
558 | number = channel->channel; | |
559 | if (efx->tx_channel_offset == 0) { | |
560 | type = ""; | |
561 | } else if (channel->channel < efx->tx_channel_offset) { | |
562 | type = "-rx"; | |
563 | } else { | |
564 | type = "-tx"; | |
565 | number -= efx->tx_channel_offset; | |
566 | } | |
567 | snprintf(buf, len, "%s%s-%d", efx->name, type, number); | |
568 | } | |
8ceee660 | 569 | |
56536e9c BH |
570 | static void efx_set_channel_names(struct efx_nic *efx) |
571 | { | |
572 | struct efx_channel *channel; | |
56536e9c | 573 | |
7f967c01 BH |
574 | efx_for_each_channel(channel, efx) |
575 | channel->type->get_name(channel, | |
576 | efx->channel_name[channel->channel], | |
577 | sizeof(efx->channel_name[0])); | |
56536e9c BH |
578 | } |
579 | ||
4642610c BH |
580 | static int efx_probe_channels(struct efx_nic *efx) |
581 | { | |
582 | struct efx_channel *channel; | |
583 | int rc; | |
584 | ||
585 | /* Restart special buffer allocation */ | |
586 | efx->next_buffer_table = 0; | |
587 | ||
588 | efx_for_each_channel(channel, efx) { | |
589 | rc = efx_probe_channel(channel); | |
590 | if (rc) { | |
591 | netif_err(efx, probe, efx->net_dev, | |
592 | "failed to create channel %d\n", | |
593 | channel->channel); | |
594 | goto fail; | |
595 | } | |
596 | } | |
597 | efx_set_channel_names(efx); | |
598 | ||
599 | return 0; | |
600 | ||
601 | fail: | |
602 | efx_remove_channels(efx); | |
603 | return rc; | |
604 | } | |
605 | ||
8ceee660 BH |
606 | /* Channels are shutdown and reinitialised whilst the NIC is running |
607 | * to propagate configuration changes (mtu, checksum offload), or | |
608 | * to clear hardware error conditions | |
609 | */ | |
9f2cb71c | 610 | static void efx_start_datapath(struct efx_nic *efx) |
8ceee660 BH |
611 | { |
612 | struct efx_tx_queue *tx_queue; | |
613 | struct efx_rx_queue *rx_queue; | |
614 | struct efx_channel *channel; | |
8ceee660 | 615 | |
f7f13b0b BH |
616 | /* Calculate the rx buffer allocation parameters required to |
617 | * support the current MTU, including padding for header | |
618 | * alignment and overruns. | |
619 | */ | |
620 | efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) + | |
621 | EFX_MAX_FRAME_LEN(efx->net_dev->mtu) + | |
39c9cf07 | 622 | efx->type->rx_buffer_hash_size + |
f7f13b0b | 623 | efx->type->rx_buffer_padding); |
62b330ba SH |
624 | efx->rx_buffer_order = get_order(efx->rx_buffer_len + |
625 | sizeof(struct efx_rx_page_state)); | |
8ceee660 BH |
626 | |
627 | /* Initialise the channels */ | |
628 | efx_for_each_channel(channel, efx) { | |
bc3c90a2 BH |
629 | efx_for_each_channel_tx_queue(tx_queue, channel) |
630 | efx_init_tx_queue(tx_queue); | |
8ceee660 BH |
631 | |
632 | /* The rx buffer allocation strategy is MTU dependent */ | |
633 | efx_rx_strategy(channel); | |
634 | ||
9f2cb71c | 635 | efx_for_each_channel_rx_queue(rx_queue, channel) { |
bc3c90a2 | 636 | efx_init_rx_queue(rx_queue); |
9f2cb71c BH |
637 | efx_nic_generate_fill_event(rx_queue); |
638 | } | |
8ceee660 BH |
639 | |
640 | WARN_ON(channel->rx_pkt != NULL); | |
641 | efx_rx_strategy(channel); | |
642 | } | |
8ceee660 | 643 | |
9f2cb71c BH |
644 | if (netif_device_present(efx->net_dev)) |
645 | netif_tx_wake_all_queues(efx->net_dev); | |
8ceee660 BH |
646 | } |
647 | ||
9f2cb71c | 648 | static void efx_stop_datapath(struct efx_nic *efx) |
8ceee660 BH |
649 | { |
650 | struct efx_channel *channel; | |
651 | struct efx_tx_queue *tx_queue; | |
652 | struct efx_rx_queue *rx_queue; | |
6bc5d3a9 | 653 | int rc; |
8ceee660 BH |
654 | |
655 | EFX_ASSERT_RESET_SERIALISED(efx); | |
656 | BUG_ON(efx->port_enabled); | |
657 | ||
152b6a62 | 658 | rc = efx_nic_flush_queues(efx); |
fd371e32 SH |
659 | if (rc && EFX_WORKAROUND_7803(efx)) { |
660 | /* Schedule a reset to recover from the flush failure. The | |
661 | * descriptor caches reference memory we're about to free, | |
662 | * but falcon_reconfigure_mac_wrapper() won't reconnect | |
663 | * the MACs because of the pending reset. */ | |
62776d03 BH |
664 | netif_err(efx, drv, efx->net_dev, |
665 | "Resetting to recover from flush failure\n"); | |
fd371e32 SH |
666 | efx_schedule_reset(efx, RESET_TYPE_ALL); |
667 | } else if (rc) { | |
62776d03 | 668 | netif_err(efx, drv, efx->net_dev, "failed to flush queues\n"); |
fd371e32 | 669 | } else { |
62776d03 BH |
670 | netif_dbg(efx, drv, efx->net_dev, |
671 | "successfully flushed all queues\n"); | |
fd371e32 | 672 | } |
6bc5d3a9 | 673 | |
8ceee660 | 674 | efx_for_each_channel(channel, efx) { |
9f2cb71c BH |
675 | /* RX packet processing is pipelined, so wait for the |
676 | * NAPI handler to complete. At least event queue 0 | |
677 | * might be kept active by non-data events, so don't | |
678 | * use napi_synchronize() but actually disable NAPI | |
679 | * temporarily. | |
680 | */ | |
681 | if (efx_channel_has_rx_queue(channel)) { | |
682 | efx_stop_eventq(channel); | |
683 | efx_start_eventq(channel); | |
684 | } | |
8ceee660 BH |
685 | |
686 | efx_for_each_channel_rx_queue(rx_queue, channel) | |
687 | efx_fini_rx_queue(rx_queue); | |
94b274bf | 688 | efx_for_each_possible_channel_tx_queue(tx_queue, channel) |
8ceee660 | 689 | efx_fini_tx_queue(tx_queue); |
8ceee660 BH |
690 | } |
691 | } | |
692 | ||
693 | static void efx_remove_channel(struct efx_channel *channel) | |
694 | { | |
695 | struct efx_tx_queue *tx_queue; | |
696 | struct efx_rx_queue *rx_queue; | |
697 | ||
62776d03 BH |
698 | netif_dbg(channel->efx, drv, channel->efx->net_dev, |
699 | "destroy chan %d\n", channel->channel); | |
8ceee660 BH |
700 | |
701 | efx_for_each_channel_rx_queue(rx_queue, channel) | |
702 | efx_remove_rx_queue(rx_queue); | |
94b274bf | 703 | efx_for_each_possible_channel_tx_queue(tx_queue, channel) |
8ceee660 BH |
704 | efx_remove_tx_queue(tx_queue); |
705 | efx_remove_eventq(channel); | |
8ceee660 BH |
706 | } |
707 | ||
4642610c BH |
708 | static void efx_remove_channels(struct efx_nic *efx) |
709 | { | |
710 | struct efx_channel *channel; | |
711 | ||
712 | efx_for_each_channel(channel, efx) | |
713 | efx_remove_channel(channel); | |
714 | } | |
715 | ||
716 | int | |
717 | efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries) | |
718 | { | |
719 | struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel; | |
720 | u32 old_rxq_entries, old_txq_entries; | |
7f967c01 BH |
721 | unsigned i, next_buffer_table = 0; |
722 | int rc = 0; | |
723 | ||
724 | /* Not all channels should be reallocated. We must avoid | |
725 | * reallocating their buffer table entries. | |
726 | */ | |
727 | efx_for_each_channel(channel, efx) { | |
728 | struct efx_rx_queue *rx_queue; | |
729 | struct efx_tx_queue *tx_queue; | |
730 | ||
731 | if (channel->type->copy) | |
732 | continue; | |
733 | next_buffer_table = max(next_buffer_table, | |
734 | channel->eventq.index + | |
735 | channel->eventq.entries); | |
736 | efx_for_each_channel_rx_queue(rx_queue, channel) | |
737 | next_buffer_table = max(next_buffer_table, | |
738 | rx_queue->rxd.index + | |
739 | rx_queue->rxd.entries); | |
740 | efx_for_each_channel_tx_queue(tx_queue, channel) | |
741 | next_buffer_table = max(next_buffer_table, | |
742 | tx_queue->txd.index + | |
743 | tx_queue->txd.entries); | |
744 | } | |
4642610c BH |
745 | |
746 | efx_stop_all(efx); | |
7f967c01 | 747 | efx_stop_interrupts(efx, true); |
4642610c | 748 | |
7f967c01 | 749 | /* Clone channels (where possible) */ |
4642610c BH |
750 | memset(other_channel, 0, sizeof(other_channel)); |
751 | for (i = 0; i < efx->n_channels; i++) { | |
7f967c01 BH |
752 | channel = efx->channel[i]; |
753 | if (channel->type->copy) | |
754 | channel = channel->type->copy(channel); | |
4642610c BH |
755 | if (!channel) { |
756 | rc = -ENOMEM; | |
757 | goto out; | |
758 | } | |
759 | other_channel[i] = channel; | |
760 | } | |
761 | ||
762 | /* Swap entry counts and channel pointers */ | |
763 | old_rxq_entries = efx->rxq_entries; | |
764 | old_txq_entries = efx->txq_entries; | |
765 | efx->rxq_entries = rxq_entries; | |
766 | efx->txq_entries = txq_entries; | |
767 | for (i = 0; i < efx->n_channels; i++) { | |
768 | channel = efx->channel[i]; | |
769 | efx->channel[i] = other_channel[i]; | |
770 | other_channel[i] = channel; | |
771 | } | |
772 | ||
7f967c01 BH |
773 | /* Restart buffer table allocation */ |
774 | efx->next_buffer_table = next_buffer_table; | |
e8f14992 | 775 | |
e8f14992 | 776 | for (i = 0; i < efx->n_channels; i++) { |
7f967c01 BH |
777 | channel = efx->channel[i]; |
778 | if (!channel->type->copy) | |
779 | continue; | |
780 | rc = efx_probe_channel(channel); | |
781 | if (rc) | |
782 | goto rollback; | |
783 | efx_init_napi_channel(efx->channel[i]); | |
e8f14992 | 784 | } |
7f967c01 | 785 | |
4642610c | 786 | out: |
7f967c01 BH |
787 | /* Destroy unused channel structures */ |
788 | for (i = 0; i < efx->n_channels; i++) { | |
789 | channel = other_channel[i]; | |
790 | if (channel && channel->type->copy) { | |
791 | efx_fini_napi_channel(channel); | |
792 | efx_remove_channel(channel); | |
793 | kfree(channel); | |
794 | } | |
795 | } | |
4642610c | 796 | |
7f967c01 | 797 | efx_start_interrupts(efx, true); |
4642610c BH |
798 | efx_start_all(efx); |
799 | return rc; | |
800 | ||
801 | rollback: | |
802 | /* Swap back */ | |
803 | efx->rxq_entries = old_rxq_entries; | |
804 | efx->txq_entries = old_txq_entries; | |
805 | for (i = 0; i < efx->n_channels; i++) { | |
806 | channel = efx->channel[i]; | |
807 | efx->channel[i] = other_channel[i]; | |
808 | other_channel[i] = channel; | |
809 | } | |
810 | goto out; | |
811 | } | |
812 | ||
90d683af | 813 | void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue) |
8ceee660 | 814 | { |
90d683af | 815 | mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100)); |
8ceee660 BH |
816 | } |
817 | ||
7f967c01 BH |
818 | static const struct efx_channel_type efx_default_channel_type = { |
819 | .pre_probe = efx_channel_dummy_op_int, | |
820 | .get_name = efx_get_channel_name, | |
821 | .copy = efx_copy_channel, | |
822 | .keep_eventq = false, | |
823 | }; | |
824 | ||
825 | int efx_channel_dummy_op_int(struct efx_channel *channel) | |
826 | { | |
827 | return 0; | |
828 | } | |
829 | ||
8ceee660 BH |
830 | /************************************************************************** |
831 | * | |
832 | * Port handling | |
833 | * | |
834 | **************************************************************************/ | |
835 | ||
836 | /* This ensures that the kernel is kept informed (via | |
837 | * netif_carrier_on/off) of the link status, and also maintains the | |
838 | * link status's stop on the port's TX queue. | |
839 | */ | |
fdaa9aed | 840 | void efx_link_status_changed(struct efx_nic *efx) |
8ceee660 | 841 | { |
eb50c0d6 BH |
842 | struct efx_link_state *link_state = &efx->link_state; |
843 | ||
8ceee660 BH |
844 | /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure |
845 | * that no events are triggered between unregister_netdev() and the | |
846 | * driver unloading. A more general condition is that NETDEV_CHANGE | |
847 | * can only be generated between NETDEV_UP and NETDEV_DOWN */ | |
848 | if (!netif_running(efx->net_dev)) | |
849 | return; | |
850 | ||
eb50c0d6 | 851 | if (link_state->up != netif_carrier_ok(efx->net_dev)) { |
8ceee660 BH |
852 | efx->n_link_state_changes++; |
853 | ||
eb50c0d6 | 854 | if (link_state->up) |
8ceee660 BH |
855 | netif_carrier_on(efx->net_dev); |
856 | else | |
857 | netif_carrier_off(efx->net_dev); | |
858 | } | |
859 | ||
860 | /* Status message for kernel log */ | |
2aa9ef11 | 861 | if (link_state->up) |
62776d03 BH |
862 | netif_info(efx, link, efx->net_dev, |
863 | "link up at %uMbps %s-duplex (MTU %d)%s\n", | |
864 | link_state->speed, link_state->fd ? "full" : "half", | |
865 | efx->net_dev->mtu, | |
866 | (efx->promiscuous ? " [PROMISC]" : "")); | |
2aa9ef11 | 867 | else |
62776d03 | 868 | netif_info(efx, link, efx->net_dev, "link down\n"); |
8ceee660 BH |
869 | } |
870 | ||
d3245b28 BH |
871 | void efx_link_set_advertising(struct efx_nic *efx, u32 advertising) |
872 | { | |
873 | efx->link_advertising = advertising; | |
874 | if (advertising) { | |
875 | if (advertising & ADVERTISED_Pause) | |
876 | efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX); | |
877 | else | |
878 | efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX); | |
879 | if (advertising & ADVERTISED_Asym_Pause) | |
880 | efx->wanted_fc ^= EFX_FC_TX; | |
881 | } | |
882 | } | |
883 | ||
b5626946 | 884 | void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc) |
d3245b28 BH |
885 | { |
886 | efx->wanted_fc = wanted_fc; | |
887 | if (efx->link_advertising) { | |
888 | if (wanted_fc & EFX_FC_RX) | |
889 | efx->link_advertising |= (ADVERTISED_Pause | | |
890 | ADVERTISED_Asym_Pause); | |
891 | else | |
892 | efx->link_advertising &= ~(ADVERTISED_Pause | | |
893 | ADVERTISED_Asym_Pause); | |
894 | if (wanted_fc & EFX_FC_TX) | |
895 | efx->link_advertising ^= ADVERTISED_Asym_Pause; | |
896 | } | |
897 | } | |
898 | ||
115122af BH |
899 | static void efx_fini_port(struct efx_nic *efx); |
900 | ||
d3245b28 BH |
901 | /* Push loopback/power/transmit disable settings to the PHY, and reconfigure |
902 | * the MAC appropriately. All other PHY configuration changes are pushed | |
903 | * through phy_op->set_settings(), and pushed asynchronously to the MAC | |
904 | * through efx_monitor(). | |
905 | * | |
906 | * Callers must hold the mac_lock | |
907 | */ | |
908 | int __efx_reconfigure_port(struct efx_nic *efx) | |
8ceee660 | 909 | { |
d3245b28 BH |
910 | enum efx_phy_mode phy_mode; |
911 | int rc; | |
8ceee660 | 912 | |
d3245b28 | 913 | WARN_ON(!mutex_is_locked(&efx->mac_lock)); |
8ceee660 | 914 | |
0fca8c97 | 915 | /* Serialise the promiscuous flag with efx_set_rx_mode. */ |
73ba7b68 BH |
916 | netif_addr_lock_bh(efx->net_dev); |
917 | netif_addr_unlock_bh(efx->net_dev); | |
a816f75a | 918 | |
d3245b28 BH |
919 | /* Disable PHY transmit in mac level loopbacks */ |
920 | phy_mode = efx->phy_mode; | |
177dfcd8 BH |
921 | if (LOOPBACK_INTERNAL(efx)) |
922 | efx->phy_mode |= PHY_MODE_TX_DISABLED; | |
923 | else | |
924 | efx->phy_mode &= ~PHY_MODE_TX_DISABLED; | |
177dfcd8 | 925 | |
d3245b28 | 926 | rc = efx->type->reconfigure_port(efx); |
8ceee660 | 927 | |
d3245b28 BH |
928 | if (rc) |
929 | efx->phy_mode = phy_mode; | |
177dfcd8 | 930 | |
d3245b28 | 931 | return rc; |
8ceee660 BH |
932 | } |
933 | ||
934 | /* Reinitialise the MAC to pick up new PHY settings, even if the port is | |
935 | * disabled. */ | |
d3245b28 | 936 | int efx_reconfigure_port(struct efx_nic *efx) |
8ceee660 | 937 | { |
d3245b28 BH |
938 | int rc; |
939 | ||
8ceee660 BH |
940 | EFX_ASSERT_RESET_SERIALISED(efx); |
941 | ||
942 | mutex_lock(&efx->mac_lock); | |
d3245b28 | 943 | rc = __efx_reconfigure_port(efx); |
8ceee660 | 944 | mutex_unlock(&efx->mac_lock); |
d3245b28 BH |
945 | |
946 | return rc; | |
8ceee660 BH |
947 | } |
948 | ||
8be4f3e6 BH |
949 | /* Asynchronous work item for changing MAC promiscuity and multicast |
950 | * hash. Avoid a drain/rx_ingress enable by reconfiguring the current | |
951 | * MAC directly. */ | |
766ca0fa BH |
952 | static void efx_mac_work(struct work_struct *data) |
953 | { | |
954 | struct efx_nic *efx = container_of(data, struct efx_nic, mac_work); | |
955 | ||
956 | mutex_lock(&efx->mac_lock); | |
30b81cda | 957 | if (efx->port_enabled) |
710b208d | 958 | efx->type->reconfigure_mac(efx); |
766ca0fa BH |
959 | mutex_unlock(&efx->mac_lock); |
960 | } | |
961 | ||
8ceee660 BH |
962 | static int efx_probe_port(struct efx_nic *efx) |
963 | { | |
964 | int rc; | |
965 | ||
62776d03 | 966 | netif_dbg(efx, probe, efx->net_dev, "create port\n"); |
8ceee660 | 967 | |
ff3b00a0 SH |
968 | if (phy_flash_cfg) |
969 | efx->phy_mode = PHY_MODE_SPECIAL; | |
970 | ||
ef2b90ee BH |
971 | /* Connect up MAC/PHY operations table */ |
972 | rc = efx->type->probe_port(efx); | |
8ceee660 | 973 | if (rc) |
e42de262 | 974 | return rc; |
8ceee660 | 975 | |
e332bcb3 BH |
976 | /* Initialise MAC address to permanent address */ |
977 | memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN); | |
8ceee660 BH |
978 | |
979 | return 0; | |
8ceee660 BH |
980 | } |
981 | ||
982 | static int efx_init_port(struct efx_nic *efx) | |
983 | { | |
984 | int rc; | |
985 | ||
62776d03 | 986 | netif_dbg(efx, drv, efx->net_dev, "init port\n"); |
8ceee660 | 987 | |
1dfc5cea BH |
988 | mutex_lock(&efx->mac_lock); |
989 | ||
177dfcd8 | 990 | rc = efx->phy_op->init(efx); |
8ceee660 | 991 | if (rc) |
1dfc5cea | 992 | goto fail1; |
8ceee660 | 993 | |
dc8cfa55 | 994 | efx->port_initialized = true; |
1dfc5cea | 995 | |
d3245b28 BH |
996 | /* Reconfigure the MAC before creating dma queues (required for |
997 | * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */ | |
710b208d | 998 | efx->type->reconfigure_mac(efx); |
d3245b28 BH |
999 | |
1000 | /* Ensure the PHY advertises the correct flow control settings */ | |
1001 | rc = efx->phy_op->reconfigure(efx); | |
1002 | if (rc) | |
1003 | goto fail2; | |
1004 | ||
1dfc5cea | 1005 | mutex_unlock(&efx->mac_lock); |
8ceee660 | 1006 | return 0; |
177dfcd8 | 1007 | |
1dfc5cea | 1008 | fail2: |
177dfcd8 | 1009 | efx->phy_op->fini(efx); |
1dfc5cea BH |
1010 | fail1: |
1011 | mutex_unlock(&efx->mac_lock); | |
177dfcd8 | 1012 | return rc; |
8ceee660 BH |
1013 | } |
1014 | ||
8ceee660 BH |
1015 | static void efx_start_port(struct efx_nic *efx) |
1016 | { | |
62776d03 | 1017 | netif_dbg(efx, ifup, efx->net_dev, "start port\n"); |
8ceee660 BH |
1018 | BUG_ON(efx->port_enabled); |
1019 | ||
1020 | mutex_lock(&efx->mac_lock); | |
dc8cfa55 | 1021 | efx->port_enabled = true; |
8be4f3e6 BH |
1022 | |
1023 | /* efx_mac_work() might have been scheduled after efx_stop_port(), | |
1024 | * and then cancelled by efx_flush_all() */ | |
710b208d | 1025 | efx->type->reconfigure_mac(efx); |
8be4f3e6 | 1026 | |
8ceee660 BH |
1027 | mutex_unlock(&efx->mac_lock); |
1028 | } | |
1029 | ||
fdaa9aed | 1030 | /* Prevent efx_mac_work() and efx_monitor() from working */ |
8ceee660 BH |
1031 | static void efx_stop_port(struct efx_nic *efx) |
1032 | { | |
62776d03 | 1033 | netif_dbg(efx, ifdown, efx->net_dev, "stop port\n"); |
8ceee660 BH |
1034 | |
1035 | mutex_lock(&efx->mac_lock); | |
dc8cfa55 | 1036 | efx->port_enabled = false; |
8ceee660 BH |
1037 | mutex_unlock(&efx->mac_lock); |
1038 | ||
1039 | /* Serialise against efx_set_multicast_list() */ | |
73ba7b68 BH |
1040 | netif_addr_lock_bh(efx->net_dev); |
1041 | netif_addr_unlock_bh(efx->net_dev); | |
8ceee660 BH |
1042 | } |
1043 | ||
1044 | static void efx_fini_port(struct efx_nic *efx) | |
1045 | { | |
62776d03 | 1046 | netif_dbg(efx, drv, efx->net_dev, "shut down port\n"); |
8ceee660 BH |
1047 | |
1048 | if (!efx->port_initialized) | |
1049 | return; | |
1050 | ||
177dfcd8 | 1051 | efx->phy_op->fini(efx); |
dc8cfa55 | 1052 | efx->port_initialized = false; |
8ceee660 | 1053 | |
eb50c0d6 | 1054 | efx->link_state.up = false; |
8ceee660 BH |
1055 | efx_link_status_changed(efx); |
1056 | } | |
1057 | ||
1058 | static void efx_remove_port(struct efx_nic *efx) | |
1059 | { | |
62776d03 | 1060 | netif_dbg(efx, drv, efx->net_dev, "destroying port\n"); |
8ceee660 | 1061 | |
ef2b90ee | 1062 | efx->type->remove_port(efx); |
8ceee660 BH |
1063 | } |
1064 | ||
1065 | /************************************************************************** | |
1066 | * | |
1067 | * NIC handling | |
1068 | * | |
1069 | **************************************************************************/ | |
1070 | ||
1071 | /* This configures the PCI device to enable I/O and DMA. */ | |
1072 | static int efx_init_io(struct efx_nic *efx) | |
1073 | { | |
1074 | struct pci_dev *pci_dev = efx->pci_dev; | |
1075 | dma_addr_t dma_mask = efx->type->max_dma_mask; | |
1076 | int rc; | |
1077 | ||
62776d03 | 1078 | netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n"); |
8ceee660 BH |
1079 | |
1080 | rc = pci_enable_device(pci_dev); | |
1081 | if (rc) { | |
62776d03 BH |
1082 | netif_err(efx, probe, efx->net_dev, |
1083 | "failed to enable PCI device\n"); | |
8ceee660 BH |
1084 | goto fail1; |
1085 | } | |
1086 | ||
1087 | pci_set_master(pci_dev); | |
1088 | ||
1089 | /* Set the PCI DMA mask. Try all possibilities from our | |
1090 | * genuine mask down to 32 bits, because some architectures | |
1091 | * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit | |
1092 | * masks event though they reject 46 bit masks. | |
1093 | */ | |
1094 | while (dma_mask > 0x7fffffffUL) { | |
e9e01846 BH |
1095 | if (pci_dma_supported(pci_dev, dma_mask)) { |
1096 | rc = pci_set_dma_mask(pci_dev, dma_mask); | |
1097 | if (rc == 0) | |
1098 | break; | |
1099 | } | |
8ceee660 BH |
1100 | dma_mask >>= 1; |
1101 | } | |
1102 | if (rc) { | |
62776d03 BH |
1103 | netif_err(efx, probe, efx->net_dev, |
1104 | "could not find a suitable DMA mask\n"); | |
8ceee660 BH |
1105 | goto fail2; |
1106 | } | |
62776d03 BH |
1107 | netif_dbg(efx, probe, efx->net_dev, |
1108 | "using DMA mask %llx\n", (unsigned long long) dma_mask); | |
8ceee660 BH |
1109 | rc = pci_set_consistent_dma_mask(pci_dev, dma_mask); |
1110 | if (rc) { | |
1111 | /* pci_set_consistent_dma_mask() is not *allowed* to | |
1112 | * fail with a mask that pci_set_dma_mask() accepted, | |
1113 | * but just in case... | |
1114 | */ | |
62776d03 BH |
1115 | netif_err(efx, probe, efx->net_dev, |
1116 | "failed to set consistent DMA mask\n"); | |
8ceee660 BH |
1117 | goto fail2; |
1118 | } | |
1119 | ||
dc803df8 BH |
1120 | efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR); |
1121 | rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc"); | |
8ceee660 | 1122 | if (rc) { |
62776d03 BH |
1123 | netif_err(efx, probe, efx->net_dev, |
1124 | "request for memory BAR failed\n"); | |
8ceee660 BH |
1125 | rc = -EIO; |
1126 | goto fail3; | |
1127 | } | |
86c432ca BH |
1128 | efx->membase = ioremap_nocache(efx->membase_phys, |
1129 | efx->type->mem_map_size); | |
8ceee660 | 1130 | if (!efx->membase) { |
62776d03 BH |
1131 | netif_err(efx, probe, efx->net_dev, |
1132 | "could not map memory BAR at %llx+%x\n", | |
1133 | (unsigned long long)efx->membase_phys, | |
1134 | efx->type->mem_map_size); | |
8ceee660 BH |
1135 | rc = -ENOMEM; |
1136 | goto fail4; | |
1137 | } | |
62776d03 BH |
1138 | netif_dbg(efx, probe, efx->net_dev, |
1139 | "memory BAR at %llx+%x (virtual %p)\n", | |
1140 | (unsigned long long)efx->membase_phys, | |
1141 | efx->type->mem_map_size, efx->membase); | |
8ceee660 BH |
1142 | |
1143 | return 0; | |
1144 | ||
1145 | fail4: | |
dc803df8 | 1146 | pci_release_region(efx->pci_dev, EFX_MEM_BAR); |
8ceee660 | 1147 | fail3: |
2c118e0f | 1148 | efx->membase_phys = 0; |
8ceee660 BH |
1149 | fail2: |
1150 | pci_disable_device(efx->pci_dev); | |
1151 | fail1: | |
1152 | return rc; | |
1153 | } | |
1154 | ||
1155 | static void efx_fini_io(struct efx_nic *efx) | |
1156 | { | |
62776d03 | 1157 | netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n"); |
8ceee660 BH |
1158 | |
1159 | if (efx->membase) { | |
1160 | iounmap(efx->membase); | |
1161 | efx->membase = NULL; | |
1162 | } | |
1163 | ||
1164 | if (efx->membase_phys) { | |
dc803df8 | 1165 | pci_release_region(efx->pci_dev, EFX_MEM_BAR); |
2c118e0f | 1166 | efx->membase_phys = 0; |
8ceee660 BH |
1167 | } |
1168 | ||
1169 | pci_disable_device(efx->pci_dev); | |
1170 | } | |
1171 | ||
a9a52506 | 1172 | static unsigned int efx_wanted_parallelism(struct efx_nic *efx) |
46123d04 | 1173 | { |
cdb08f8f | 1174 | cpumask_var_t thread_mask; |
a16e5b24 | 1175 | unsigned int count; |
46123d04 | 1176 | int cpu; |
5b874e25 BH |
1177 | |
1178 | if (rss_cpus) | |
1179 | return rss_cpus; | |
46123d04 | 1180 | |
cdb08f8f | 1181 | if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) { |
a9a52506 BH |
1182 | netif_warn(efx, probe, efx->net_dev, |
1183 | "RSS disabled due to allocation failure\n"); | |
2f8975fb RR |
1184 | return 1; |
1185 | } | |
1186 | ||
46123d04 BH |
1187 | count = 0; |
1188 | for_each_online_cpu(cpu) { | |
cdb08f8f | 1189 | if (!cpumask_test_cpu(cpu, thread_mask)) { |
46123d04 | 1190 | ++count; |
cdb08f8f BH |
1191 | cpumask_or(thread_mask, thread_mask, |
1192 | topology_thread_cpumask(cpu)); | |
46123d04 BH |
1193 | } |
1194 | } | |
1195 | ||
cdb08f8f | 1196 | free_cpumask_var(thread_mask); |
46123d04 BH |
1197 | return count; |
1198 | } | |
1199 | ||
64d8ad6d BH |
1200 | static int |
1201 | efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries) | |
1202 | { | |
1203 | #ifdef CONFIG_RFS_ACCEL | |
a16e5b24 BH |
1204 | unsigned int i; |
1205 | int rc; | |
64d8ad6d BH |
1206 | |
1207 | efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels); | |
1208 | if (!efx->net_dev->rx_cpu_rmap) | |
1209 | return -ENOMEM; | |
1210 | for (i = 0; i < efx->n_rx_channels; i++) { | |
1211 | rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap, | |
1212 | xentries[i].vector); | |
1213 | if (rc) { | |
1214 | free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap); | |
1215 | efx->net_dev->rx_cpu_rmap = NULL; | |
1216 | return rc; | |
1217 | } | |
1218 | } | |
1219 | #endif | |
1220 | return 0; | |
1221 | } | |
1222 | ||
46123d04 BH |
1223 | /* Probe the number and type of interrupts we are able to obtain, and |
1224 | * the resulting numbers of channels and RX queues. | |
1225 | */ | |
64d8ad6d | 1226 | static int efx_probe_interrupts(struct efx_nic *efx) |
8ceee660 | 1227 | { |
a16e5b24 BH |
1228 | unsigned int max_channels = |
1229 | min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS); | |
7f967c01 BH |
1230 | unsigned int extra_channels = 0; |
1231 | unsigned int i, j; | |
a16e5b24 | 1232 | int rc; |
8ceee660 | 1233 | |
7f967c01 BH |
1234 | for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) |
1235 | if (efx->extra_channel_type[i]) | |
1236 | ++extra_channels; | |
1237 | ||
8ceee660 | 1238 | if (efx->interrupt_mode == EFX_INT_MODE_MSIX) { |
46123d04 | 1239 | struct msix_entry xentries[EFX_MAX_CHANNELS]; |
a16e5b24 | 1240 | unsigned int n_channels; |
aa6ef27e | 1241 | |
a9a52506 | 1242 | n_channels = efx_wanted_parallelism(efx); |
a4900ac9 BH |
1243 | if (separate_tx_channels) |
1244 | n_channels *= 2; | |
7f967c01 | 1245 | n_channels += extra_channels; |
a4900ac9 | 1246 | n_channels = min(n_channels, max_channels); |
8ceee660 | 1247 | |
a4900ac9 | 1248 | for (i = 0; i < n_channels; i++) |
8ceee660 | 1249 | xentries[i].entry = i; |
a4900ac9 | 1250 | rc = pci_enable_msix(efx->pci_dev, xentries, n_channels); |
8ceee660 | 1251 | if (rc > 0) { |
62776d03 BH |
1252 | netif_err(efx, drv, efx->net_dev, |
1253 | "WARNING: Insufficient MSI-X vectors" | |
a16e5b24 | 1254 | " available (%d < %u).\n", rc, n_channels); |
62776d03 BH |
1255 | netif_err(efx, drv, efx->net_dev, |
1256 | "WARNING: Performance may be reduced.\n"); | |
a4900ac9 BH |
1257 | EFX_BUG_ON_PARANOID(rc >= n_channels); |
1258 | n_channels = rc; | |
8ceee660 | 1259 | rc = pci_enable_msix(efx->pci_dev, xentries, |
a4900ac9 | 1260 | n_channels); |
8ceee660 BH |
1261 | } |
1262 | ||
1263 | if (rc == 0) { | |
a4900ac9 | 1264 | efx->n_channels = n_channels; |
7f967c01 BH |
1265 | if (n_channels > extra_channels) |
1266 | n_channels -= extra_channels; | |
a4900ac9 | 1267 | if (separate_tx_channels) { |
7f967c01 BH |
1268 | efx->n_tx_channels = max(n_channels / 2, 1U); |
1269 | efx->n_rx_channels = max(n_channels - | |
1270 | efx->n_tx_channels, | |
1271 | 1U); | |
a4900ac9 | 1272 | } else { |
7f967c01 BH |
1273 | efx->n_tx_channels = n_channels; |
1274 | efx->n_rx_channels = n_channels; | |
a4900ac9 | 1275 | } |
64d8ad6d BH |
1276 | rc = efx_init_rx_cpu_rmap(efx, xentries); |
1277 | if (rc) { | |
1278 | pci_disable_msix(efx->pci_dev); | |
1279 | return rc; | |
1280 | } | |
7f967c01 | 1281 | for (i = 0; i < efx->n_channels; i++) |
f7d12cdc BH |
1282 | efx_get_channel(efx, i)->irq = |
1283 | xentries[i].vector; | |
8ceee660 BH |
1284 | } else { |
1285 | /* Fall back to single channel MSI */ | |
1286 | efx->interrupt_mode = EFX_INT_MODE_MSI; | |
62776d03 BH |
1287 | netif_err(efx, drv, efx->net_dev, |
1288 | "could not enable MSI-X\n"); | |
8ceee660 BH |
1289 | } |
1290 | } | |
1291 | ||
1292 | /* Try single interrupt MSI */ | |
1293 | if (efx->interrupt_mode == EFX_INT_MODE_MSI) { | |
28b581ab | 1294 | efx->n_channels = 1; |
a4900ac9 BH |
1295 | efx->n_rx_channels = 1; |
1296 | efx->n_tx_channels = 1; | |
8ceee660 BH |
1297 | rc = pci_enable_msi(efx->pci_dev); |
1298 | if (rc == 0) { | |
f7d12cdc | 1299 | efx_get_channel(efx, 0)->irq = efx->pci_dev->irq; |
8ceee660 | 1300 | } else { |
62776d03 BH |
1301 | netif_err(efx, drv, efx->net_dev, |
1302 | "could not enable MSI\n"); | |
8ceee660 BH |
1303 | efx->interrupt_mode = EFX_INT_MODE_LEGACY; |
1304 | } | |
1305 | } | |
1306 | ||
1307 | /* Assume legacy interrupts */ | |
1308 | if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) { | |
28b581ab | 1309 | efx->n_channels = 1 + (separate_tx_channels ? 1 : 0); |
a4900ac9 BH |
1310 | efx->n_rx_channels = 1; |
1311 | efx->n_tx_channels = 1; | |
8ceee660 BH |
1312 | efx->legacy_irq = efx->pci_dev->irq; |
1313 | } | |
64d8ad6d | 1314 | |
7f967c01 BH |
1315 | /* Assign extra channels if possible */ |
1316 | j = efx->n_channels; | |
1317 | for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) { | |
1318 | if (!efx->extra_channel_type[i]) | |
1319 | continue; | |
1320 | if (efx->interrupt_mode != EFX_INT_MODE_MSIX || | |
1321 | efx->n_channels <= extra_channels) { | |
1322 | efx->extra_channel_type[i]->handle_no_channel(efx); | |
1323 | } else { | |
1324 | --j; | |
1325 | efx_get_channel(efx, j)->type = | |
1326 | efx->extra_channel_type[i]; | |
1327 | } | |
1328 | } | |
1329 | ||
64d8ad6d | 1330 | return 0; |
8ceee660 BH |
1331 | } |
1332 | ||
9f2cb71c | 1333 | /* Enable interrupts, then probe and start the event queues */ |
7f967c01 | 1334 | static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq) |
9f2cb71c BH |
1335 | { |
1336 | struct efx_channel *channel; | |
1337 | ||
1338 | if (efx->legacy_irq) | |
1339 | efx->legacy_irq_enabled = true; | |
1340 | efx_nic_enable_interrupts(efx); | |
1341 | ||
1342 | efx_for_each_channel(channel, efx) { | |
7f967c01 BH |
1343 | if (!channel->type->keep_eventq || !may_keep_eventq) |
1344 | efx_init_eventq(channel); | |
9f2cb71c BH |
1345 | efx_start_eventq(channel); |
1346 | } | |
1347 | ||
1348 | efx_mcdi_mode_event(efx); | |
1349 | } | |
1350 | ||
7f967c01 | 1351 | static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq) |
9f2cb71c BH |
1352 | { |
1353 | struct efx_channel *channel; | |
1354 | ||
1355 | efx_mcdi_mode_poll(efx); | |
1356 | ||
1357 | efx_nic_disable_interrupts(efx); | |
1358 | if (efx->legacy_irq) { | |
1359 | synchronize_irq(efx->legacy_irq); | |
1360 | efx->legacy_irq_enabled = false; | |
1361 | } | |
1362 | ||
1363 | efx_for_each_channel(channel, efx) { | |
1364 | if (channel->irq) | |
1365 | synchronize_irq(channel->irq); | |
1366 | ||
1367 | efx_stop_eventq(channel); | |
7f967c01 BH |
1368 | if (!channel->type->keep_eventq || !may_keep_eventq) |
1369 | efx_fini_eventq(channel); | |
9f2cb71c BH |
1370 | } |
1371 | } | |
1372 | ||
8ceee660 BH |
1373 | static void efx_remove_interrupts(struct efx_nic *efx) |
1374 | { | |
1375 | struct efx_channel *channel; | |
1376 | ||
1377 | /* Remove MSI/MSI-X interrupts */ | |
64ee3120 | 1378 | efx_for_each_channel(channel, efx) |
8ceee660 BH |
1379 | channel->irq = 0; |
1380 | pci_disable_msi(efx->pci_dev); | |
1381 | pci_disable_msix(efx->pci_dev); | |
1382 | ||
1383 | /* Remove legacy interrupt */ | |
1384 | efx->legacy_irq = 0; | |
1385 | } | |
1386 | ||
8831da7b | 1387 | static void efx_set_channels(struct efx_nic *efx) |
8ceee660 | 1388 | { |
602a5322 BH |
1389 | struct efx_channel *channel; |
1390 | struct efx_tx_queue *tx_queue; | |
1391 | ||
97653431 | 1392 | efx->tx_channel_offset = |
a4900ac9 | 1393 | separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0; |
602a5322 BH |
1394 | |
1395 | /* We need to adjust the TX queue numbers if we have separate | |
1396 | * RX-only and TX-only channels. | |
1397 | */ | |
1398 | efx_for_each_channel(channel, efx) { | |
1399 | efx_for_each_channel_tx_queue(tx_queue, channel) | |
1400 | tx_queue->queue -= (efx->tx_channel_offset * | |
1401 | EFX_TXQ_TYPES); | |
1402 | } | |
8ceee660 BH |
1403 | } |
1404 | ||
1405 | static int efx_probe_nic(struct efx_nic *efx) | |
1406 | { | |
765c9f46 | 1407 | size_t i; |
8ceee660 BH |
1408 | int rc; |
1409 | ||
62776d03 | 1410 | netif_dbg(efx, probe, efx->net_dev, "creating NIC\n"); |
8ceee660 BH |
1411 | |
1412 | /* Carry out hardware-type specific initialisation */ | |
ef2b90ee | 1413 | rc = efx->type->probe(efx); |
8ceee660 BH |
1414 | if (rc) |
1415 | return rc; | |
1416 | ||
a4900ac9 | 1417 | /* Determine the number of channels and queues by trying to hook |
8ceee660 | 1418 | * in MSI-X interrupts. */ |
64d8ad6d BH |
1419 | rc = efx_probe_interrupts(efx); |
1420 | if (rc) | |
1421 | goto fail; | |
8ceee660 | 1422 | |
5d3a6fca BH |
1423 | if (efx->n_channels > 1) |
1424 | get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key)); | |
765c9f46 | 1425 | for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++) |
278bc429 BH |
1426 | efx->rx_indir_table[i] = |
1427 | ethtool_rxfh_indir_default(i, efx->n_rx_channels); | |
5d3a6fca | 1428 | |
8831da7b | 1429 | efx_set_channels(efx); |
c4f4adc7 BH |
1430 | netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels); |
1431 | netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels); | |
8ceee660 BH |
1432 | |
1433 | /* Initialise the interrupt moderation settings */ | |
9e393b30 BH |
1434 | efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true, |
1435 | true); | |
8ceee660 BH |
1436 | |
1437 | return 0; | |
64d8ad6d BH |
1438 | |
1439 | fail: | |
1440 | efx->type->remove(efx); | |
1441 | return rc; | |
8ceee660 BH |
1442 | } |
1443 | ||
1444 | static void efx_remove_nic(struct efx_nic *efx) | |
1445 | { | |
62776d03 | 1446 | netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n"); |
8ceee660 BH |
1447 | |
1448 | efx_remove_interrupts(efx); | |
ef2b90ee | 1449 | efx->type->remove(efx); |
8ceee660 BH |
1450 | } |
1451 | ||
1452 | /************************************************************************** | |
1453 | * | |
1454 | * NIC startup/shutdown | |
1455 | * | |
1456 | *************************************************************************/ | |
1457 | ||
1458 | static int efx_probe_all(struct efx_nic *efx) | |
1459 | { | |
8ceee660 BH |
1460 | int rc; |
1461 | ||
8ceee660 BH |
1462 | rc = efx_probe_nic(efx); |
1463 | if (rc) { | |
62776d03 | 1464 | netif_err(efx, probe, efx->net_dev, "failed to create NIC\n"); |
8ceee660 BH |
1465 | goto fail1; |
1466 | } | |
1467 | ||
8ceee660 BH |
1468 | rc = efx_probe_port(efx); |
1469 | if (rc) { | |
62776d03 | 1470 | netif_err(efx, probe, efx->net_dev, "failed to create port\n"); |
8ceee660 BH |
1471 | goto fail2; |
1472 | } | |
1473 | ||
ecc910f5 | 1474 | efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE; |
8ceee660 | 1475 | |
64eebcfd BH |
1476 | rc = efx_probe_filters(efx); |
1477 | if (rc) { | |
1478 | netif_err(efx, probe, efx->net_dev, | |
1479 | "failed to create filter tables\n"); | |
7f967c01 | 1480 | goto fail3; |
64eebcfd BH |
1481 | } |
1482 | ||
7f967c01 BH |
1483 | rc = efx_probe_channels(efx); |
1484 | if (rc) | |
1485 | goto fail4; | |
1486 | ||
8ceee660 BH |
1487 | return 0; |
1488 | ||
64eebcfd | 1489 | fail4: |
7f967c01 | 1490 | efx_remove_filters(efx); |
8ceee660 | 1491 | fail3: |
8ceee660 BH |
1492 | efx_remove_port(efx); |
1493 | fail2: | |
1494 | efx_remove_nic(efx); | |
1495 | fail1: | |
1496 | return rc; | |
1497 | } | |
1498 | ||
9f2cb71c BH |
1499 | /* Called after previous invocation(s) of efx_stop_all, restarts the port, |
1500 | * kernel transmit queues and NAPI processing, and ensures that the port is | |
1501 | * scheduled to be reconfigured. This function is safe to call multiple | |
1502 | * times when the NIC is in any state. | |
1503 | */ | |
8ceee660 BH |
1504 | static void efx_start_all(struct efx_nic *efx) |
1505 | { | |
8ceee660 BH |
1506 | EFX_ASSERT_RESET_SERIALISED(efx); |
1507 | ||
1508 | /* Check that it is appropriate to restart the interface. All | |
1509 | * of these flags are safe to read under just the rtnl lock */ | |
1510 | if (efx->port_enabled) | |
1511 | return; | |
1512 | if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT)) | |
1513 | return; | |
73ba7b68 | 1514 | if (!netif_running(efx->net_dev)) |
8ceee660 BH |
1515 | return; |
1516 | ||
8ceee660 | 1517 | efx_start_port(efx); |
9f2cb71c | 1518 | efx_start_datapath(efx); |
8880f4ec | 1519 | |
78c1f0a0 SH |
1520 | /* Start the hardware monitor if there is one. Otherwise (we're link |
1521 | * event driven), we have to poll the PHY because after an event queue | |
1522 | * flush, we could have a missed a link state change */ | |
1523 | if (efx->type->monitor != NULL) { | |
8ceee660 BH |
1524 | queue_delayed_work(efx->workqueue, &efx->monitor_work, |
1525 | efx_monitor_interval); | |
78c1f0a0 SH |
1526 | } else { |
1527 | mutex_lock(&efx->mac_lock); | |
1528 | if (efx->phy_op->poll(efx)) | |
1529 | efx_link_status_changed(efx); | |
1530 | mutex_unlock(&efx->mac_lock); | |
1531 | } | |
55edc6e6 | 1532 | |
ef2b90ee | 1533 | efx->type->start_stats(efx); |
8ceee660 BH |
1534 | } |
1535 | ||
1536 | /* Flush all delayed work. Should only be called when no more delayed work | |
1537 | * will be scheduled. This doesn't flush pending online resets (efx_reset), | |
1538 | * since we're holding the rtnl_lock at this point. */ | |
1539 | static void efx_flush_all(struct efx_nic *efx) | |
1540 | { | |
8ceee660 BH |
1541 | /* Make sure the hardware monitor is stopped */ |
1542 | cancel_delayed_work_sync(&efx->monitor_work); | |
8ceee660 | 1543 | /* Stop scheduled port reconfigurations */ |
766ca0fa | 1544 | cancel_work_sync(&efx->mac_work); |
8ceee660 BH |
1545 | } |
1546 | ||
1547 | /* Quiesce hardware and software without bringing the link down. | |
1548 | * Safe to call multiple times, when the nic and interface is in any | |
1549 | * state. The caller is guaranteed to subsequently be in a position | |
1550 | * to modify any hardware and software state they see fit without | |
1551 | * taking locks. */ | |
1552 | static void efx_stop_all(struct efx_nic *efx) | |
1553 | { | |
8ceee660 BH |
1554 | EFX_ASSERT_RESET_SERIALISED(efx); |
1555 | ||
1556 | /* port_enabled can be read safely under the rtnl lock */ | |
1557 | if (!efx->port_enabled) | |
1558 | return; | |
1559 | ||
ef2b90ee | 1560 | efx->type->stop_stats(efx); |
8ceee660 BH |
1561 | efx_stop_port(efx); |
1562 | ||
fdaa9aed | 1563 | /* Flush efx_mac_work(), refill_workqueue, monitor_work */ |
8ceee660 BH |
1564 | efx_flush_all(efx); |
1565 | ||
8ceee660 BH |
1566 | /* Stop the kernel transmit interface late, so the watchdog |
1567 | * timer isn't ticking over the flush */ | |
9f2cb71c BH |
1568 | netif_tx_disable(efx->net_dev); |
1569 | ||
1570 | efx_stop_datapath(efx); | |
8ceee660 BH |
1571 | } |
1572 | ||
1573 | static void efx_remove_all(struct efx_nic *efx) | |
1574 | { | |
4642610c | 1575 | efx_remove_channels(efx); |
7f967c01 | 1576 | efx_remove_filters(efx); |
8ceee660 BH |
1577 | efx_remove_port(efx); |
1578 | efx_remove_nic(efx); | |
1579 | } | |
1580 | ||
8ceee660 BH |
1581 | /************************************************************************** |
1582 | * | |
1583 | * Interrupt moderation | |
1584 | * | |
1585 | **************************************************************************/ | |
1586 | ||
cc180b69 | 1587 | static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns) |
0d86ebd8 | 1588 | { |
b548f976 BH |
1589 | if (usecs == 0) |
1590 | return 0; | |
cc180b69 | 1591 | if (usecs * 1000 < quantum_ns) |
0d86ebd8 | 1592 | return 1; /* never round down to 0 */ |
cc180b69 | 1593 | return usecs * 1000 / quantum_ns; |
0d86ebd8 BH |
1594 | } |
1595 | ||
8ceee660 | 1596 | /* Set interrupt moderation parameters */ |
9e393b30 BH |
1597 | int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs, |
1598 | unsigned int rx_usecs, bool rx_adaptive, | |
1599 | bool rx_may_override_tx) | |
8ceee660 | 1600 | { |
f7d12cdc | 1601 | struct efx_channel *channel; |
cc180b69 BH |
1602 | unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max * |
1603 | efx->timer_quantum_ns, | |
1604 | 1000); | |
1605 | unsigned int tx_ticks; | |
1606 | unsigned int rx_ticks; | |
8ceee660 BH |
1607 | |
1608 | EFX_ASSERT_RESET_SERIALISED(efx); | |
1609 | ||
cc180b69 | 1610 | if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max) |
9e393b30 BH |
1611 | return -EINVAL; |
1612 | ||
cc180b69 BH |
1613 | tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns); |
1614 | rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns); | |
1615 | ||
9e393b30 BH |
1616 | if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 && |
1617 | !rx_may_override_tx) { | |
1618 | netif_err(efx, drv, efx->net_dev, "Channels are shared. " | |
1619 | "RX and TX IRQ moderation must be equal\n"); | |
1620 | return -EINVAL; | |
1621 | } | |
1622 | ||
6fb70fd1 | 1623 | efx->irq_rx_adaptive = rx_adaptive; |
0d86ebd8 | 1624 | efx->irq_rx_moderation = rx_ticks; |
f7d12cdc | 1625 | efx_for_each_channel(channel, efx) { |
525da907 | 1626 | if (efx_channel_has_rx_queue(channel)) |
f7d12cdc | 1627 | channel->irq_moderation = rx_ticks; |
525da907 | 1628 | else if (efx_channel_has_tx_queues(channel)) |
f7d12cdc BH |
1629 | channel->irq_moderation = tx_ticks; |
1630 | } | |
9e393b30 BH |
1631 | |
1632 | return 0; | |
8ceee660 BH |
1633 | } |
1634 | ||
a0c4faf5 BH |
1635 | void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs, |
1636 | unsigned int *rx_usecs, bool *rx_adaptive) | |
1637 | { | |
cc180b69 BH |
1638 | /* We must round up when converting ticks to microseconds |
1639 | * because we round down when converting the other way. | |
1640 | */ | |
1641 | ||
a0c4faf5 | 1642 | *rx_adaptive = efx->irq_rx_adaptive; |
cc180b69 BH |
1643 | *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation * |
1644 | efx->timer_quantum_ns, | |
1645 | 1000); | |
a0c4faf5 BH |
1646 | |
1647 | /* If channels are shared between RX and TX, so is IRQ | |
1648 | * moderation. Otherwise, IRQ moderation is the same for all | |
1649 | * TX channels and is not adaptive. | |
1650 | */ | |
1651 | if (efx->tx_channel_offset == 0) | |
1652 | *tx_usecs = *rx_usecs; | |
1653 | else | |
cc180b69 | 1654 | *tx_usecs = DIV_ROUND_UP( |
a0c4faf5 | 1655 | efx->channel[efx->tx_channel_offset]->irq_moderation * |
cc180b69 BH |
1656 | efx->timer_quantum_ns, |
1657 | 1000); | |
a0c4faf5 BH |
1658 | } |
1659 | ||
8ceee660 BH |
1660 | /************************************************************************** |
1661 | * | |
1662 | * Hardware monitor | |
1663 | * | |
1664 | **************************************************************************/ | |
1665 | ||
e254c274 | 1666 | /* Run periodically off the general workqueue */ |
8ceee660 BH |
1667 | static void efx_monitor(struct work_struct *data) |
1668 | { | |
1669 | struct efx_nic *efx = container_of(data, struct efx_nic, | |
1670 | monitor_work.work); | |
8ceee660 | 1671 | |
62776d03 BH |
1672 | netif_vdbg(efx, timer, efx->net_dev, |
1673 | "hardware monitor executing on CPU %d\n", | |
1674 | raw_smp_processor_id()); | |
ef2b90ee | 1675 | BUG_ON(efx->type->monitor == NULL); |
8ceee660 | 1676 | |
8ceee660 BH |
1677 | /* If the mac_lock is already held then it is likely a port |
1678 | * reconfiguration is already in place, which will likely do | |
e254c274 BH |
1679 | * most of the work of monitor() anyway. */ |
1680 | if (mutex_trylock(&efx->mac_lock)) { | |
1681 | if (efx->port_enabled) | |
1682 | efx->type->monitor(efx); | |
1683 | mutex_unlock(&efx->mac_lock); | |
1684 | } | |
8ceee660 | 1685 | |
8ceee660 BH |
1686 | queue_delayed_work(efx->workqueue, &efx->monitor_work, |
1687 | efx_monitor_interval); | |
1688 | } | |
1689 | ||
1690 | /************************************************************************** | |
1691 | * | |
1692 | * ioctls | |
1693 | * | |
1694 | *************************************************************************/ | |
1695 | ||
1696 | /* Net device ioctl | |
1697 | * Context: process, rtnl_lock() held. | |
1698 | */ | |
1699 | static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd) | |
1700 | { | |
767e468c | 1701 | struct efx_nic *efx = netdev_priv(net_dev); |
68e7f45e | 1702 | struct mii_ioctl_data *data = if_mii(ifr); |
8ceee660 BH |
1703 | |
1704 | EFX_ASSERT_RESET_SERIALISED(efx); | |
1705 | ||
68e7f45e BH |
1706 | /* Convert phy_id from older PRTAD/DEVAD format */ |
1707 | if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) && | |
1708 | (data->phy_id & 0xfc00) == 0x0400) | |
1709 | data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400; | |
1710 | ||
1711 | return mdio_mii_ioctl(&efx->mdio, data, cmd); | |
8ceee660 BH |
1712 | } |
1713 | ||
1714 | /************************************************************************** | |
1715 | * | |
1716 | * NAPI interface | |
1717 | * | |
1718 | **************************************************************************/ | |
1719 | ||
7f967c01 BH |
1720 | static void efx_init_napi_channel(struct efx_channel *channel) |
1721 | { | |
1722 | struct efx_nic *efx = channel->efx; | |
1723 | ||
1724 | channel->napi_dev = efx->net_dev; | |
1725 | netif_napi_add(channel->napi_dev, &channel->napi_str, | |
1726 | efx_poll, napi_weight); | |
1727 | } | |
1728 | ||
e8f14992 | 1729 | static void efx_init_napi(struct efx_nic *efx) |
8ceee660 BH |
1730 | { |
1731 | struct efx_channel *channel; | |
8ceee660 | 1732 | |
7f967c01 BH |
1733 | efx_for_each_channel(channel, efx) |
1734 | efx_init_napi_channel(channel); | |
e8f14992 BH |
1735 | } |
1736 | ||
1737 | static void efx_fini_napi_channel(struct efx_channel *channel) | |
1738 | { | |
1739 | if (channel->napi_dev) | |
1740 | netif_napi_del(&channel->napi_str); | |
1741 | channel->napi_dev = NULL; | |
8ceee660 BH |
1742 | } |
1743 | ||
1744 | static void efx_fini_napi(struct efx_nic *efx) | |
1745 | { | |
1746 | struct efx_channel *channel; | |
1747 | ||
e8f14992 BH |
1748 | efx_for_each_channel(channel, efx) |
1749 | efx_fini_napi_channel(channel); | |
8ceee660 BH |
1750 | } |
1751 | ||
1752 | /************************************************************************** | |
1753 | * | |
1754 | * Kernel netpoll interface | |
1755 | * | |
1756 | *************************************************************************/ | |
1757 | ||
1758 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1759 | ||
1760 | /* Although in the common case interrupts will be disabled, this is not | |
1761 | * guaranteed. However, all our work happens inside the NAPI callback, | |
1762 | * so no locking is required. | |
1763 | */ | |
1764 | static void efx_netpoll(struct net_device *net_dev) | |
1765 | { | |
767e468c | 1766 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 BH |
1767 | struct efx_channel *channel; |
1768 | ||
64ee3120 | 1769 | efx_for_each_channel(channel, efx) |
8ceee660 BH |
1770 | efx_schedule_channel(channel); |
1771 | } | |
1772 | ||
1773 | #endif | |
1774 | ||
1775 | /************************************************************************** | |
1776 | * | |
1777 | * Kernel net device interface | |
1778 | * | |
1779 | *************************************************************************/ | |
1780 | ||
1781 | /* Context: process, rtnl_lock() held. */ | |
1782 | static int efx_net_open(struct net_device *net_dev) | |
1783 | { | |
767e468c | 1784 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 BH |
1785 | EFX_ASSERT_RESET_SERIALISED(efx); |
1786 | ||
62776d03 BH |
1787 | netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n", |
1788 | raw_smp_processor_id()); | |
8ceee660 | 1789 | |
f4bd954e BH |
1790 | if (efx->state == STATE_DISABLED) |
1791 | return -EIO; | |
f8b87c17 BH |
1792 | if (efx->phy_mode & PHY_MODE_SPECIAL) |
1793 | return -EBUSY; | |
8880f4ec BH |
1794 | if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL)) |
1795 | return -EIO; | |
f8b87c17 | 1796 | |
78c1f0a0 SH |
1797 | /* Notify the kernel of the link state polled during driver load, |
1798 | * before the monitor starts running */ | |
1799 | efx_link_status_changed(efx); | |
1800 | ||
8ceee660 BH |
1801 | efx_start_all(efx); |
1802 | return 0; | |
1803 | } | |
1804 | ||
1805 | /* Context: process, rtnl_lock() held. | |
1806 | * Note that the kernel will ignore our return code; this method | |
1807 | * should really be a void. | |
1808 | */ | |
1809 | static int efx_net_stop(struct net_device *net_dev) | |
1810 | { | |
767e468c | 1811 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 | 1812 | |
62776d03 BH |
1813 | netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n", |
1814 | raw_smp_processor_id()); | |
8ceee660 | 1815 | |
f4bd954e BH |
1816 | if (efx->state != STATE_DISABLED) { |
1817 | /* Stop the device and flush all the channels */ | |
1818 | efx_stop_all(efx); | |
f4bd954e | 1819 | } |
8ceee660 BH |
1820 | |
1821 | return 0; | |
1822 | } | |
1823 | ||
5b9e207c | 1824 | /* Context: process, dev_base_lock or RTNL held, non-blocking. */ |
2aa9ef11 BH |
1825 | static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev, |
1826 | struct rtnl_link_stats64 *stats) | |
8ceee660 | 1827 | { |
767e468c | 1828 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 | 1829 | struct efx_mac_stats *mac_stats = &efx->mac_stats; |
8ceee660 | 1830 | |
55edc6e6 | 1831 | spin_lock_bh(&efx->stats_lock); |
1cb34522 | 1832 | |
ef2b90ee | 1833 | efx->type->update_stats(efx); |
8ceee660 BH |
1834 | |
1835 | stats->rx_packets = mac_stats->rx_packets; | |
1836 | stats->tx_packets = mac_stats->tx_packets; | |
1837 | stats->rx_bytes = mac_stats->rx_bytes; | |
1838 | stats->tx_bytes = mac_stats->tx_bytes; | |
80485d34 | 1839 | stats->rx_dropped = efx->n_rx_nodesc_drop_cnt; |
8ceee660 BH |
1840 | stats->multicast = mac_stats->rx_multicast; |
1841 | stats->collisions = mac_stats->tx_collision; | |
1842 | stats->rx_length_errors = (mac_stats->rx_gtjumbo + | |
1843 | mac_stats->rx_length_error); | |
8ceee660 BH |
1844 | stats->rx_crc_errors = mac_stats->rx_bad; |
1845 | stats->rx_frame_errors = mac_stats->rx_align_error; | |
1846 | stats->rx_fifo_errors = mac_stats->rx_overflow; | |
1847 | stats->rx_missed_errors = mac_stats->rx_missed; | |
1848 | stats->tx_window_errors = mac_stats->tx_late_collision; | |
1849 | ||
1850 | stats->rx_errors = (stats->rx_length_errors + | |
8ceee660 BH |
1851 | stats->rx_crc_errors + |
1852 | stats->rx_frame_errors + | |
8ceee660 BH |
1853 | mac_stats->rx_symbol_error); |
1854 | stats->tx_errors = (stats->tx_window_errors + | |
1855 | mac_stats->tx_bad); | |
1856 | ||
1cb34522 BH |
1857 | spin_unlock_bh(&efx->stats_lock); |
1858 | ||
8ceee660 BH |
1859 | return stats; |
1860 | } | |
1861 | ||
1862 | /* Context: netif_tx_lock held, BHs disabled. */ | |
1863 | static void efx_watchdog(struct net_device *net_dev) | |
1864 | { | |
767e468c | 1865 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 | 1866 | |
62776d03 BH |
1867 | netif_err(efx, tx_err, efx->net_dev, |
1868 | "TX stuck with port_enabled=%d: resetting channels\n", | |
1869 | efx->port_enabled); | |
8ceee660 | 1870 | |
739bb23d | 1871 | efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG); |
8ceee660 BH |
1872 | } |
1873 | ||
1874 | ||
1875 | /* Context: process, rtnl_lock() held. */ | |
1876 | static int efx_change_mtu(struct net_device *net_dev, int new_mtu) | |
1877 | { | |
767e468c | 1878 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 BH |
1879 | |
1880 | EFX_ASSERT_RESET_SERIALISED(efx); | |
1881 | ||
1882 | if (new_mtu > EFX_MAX_MTU) | |
1883 | return -EINVAL; | |
1884 | ||
1885 | efx_stop_all(efx); | |
1886 | ||
62776d03 | 1887 | netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu); |
8ceee660 | 1888 | |
d3245b28 BH |
1889 | mutex_lock(&efx->mac_lock); |
1890 | /* Reconfigure the MAC before enabling the dma queues so that | |
1891 | * the RX buffers don't overflow */ | |
8ceee660 | 1892 | net_dev->mtu = new_mtu; |
710b208d | 1893 | efx->type->reconfigure_mac(efx); |
d3245b28 BH |
1894 | mutex_unlock(&efx->mac_lock); |
1895 | ||
8ceee660 | 1896 | efx_start_all(efx); |
6c8eef4a | 1897 | return 0; |
8ceee660 BH |
1898 | } |
1899 | ||
1900 | static int efx_set_mac_address(struct net_device *net_dev, void *data) | |
1901 | { | |
767e468c | 1902 | struct efx_nic *efx = netdev_priv(net_dev); |
8ceee660 BH |
1903 | struct sockaddr *addr = data; |
1904 | char *new_addr = addr->sa_data; | |
1905 | ||
1906 | EFX_ASSERT_RESET_SERIALISED(efx); | |
1907 | ||
1908 | if (!is_valid_ether_addr(new_addr)) { | |
62776d03 BH |
1909 | netif_err(efx, drv, efx->net_dev, |
1910 | "invalid ethernet MAC address requested: %pM\n", | |
1911 | new_addr); | |
8ceee660 BH |
1912 | return -EINVAL; |
1913 | } | |
1914 | ||
1915 | memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len); | |
1916 | ||
1917 | /* Reconfigure the MAC */ | |
d3245b28 | 1918 | mutex_lock(&efx->mac_lock); |
710b208d | 1919 | efx->type->reconfigure_mac(efx); |
d3245b28 | 1920 | mutex_unlock(&efx->mac_lock); |
8ceee660 BH |
1921 | |
1922 | return 0; | |
1923 | } | |
1924 | ||
a816f75a | 1925 | /* Context: netif_addr_lock held, BHs disabled. */ |
0fca8c97 | 1926 | static void efx_set_rx_mode(struct net_device *net_dev) |
8ceee660 | 1927 | { |
767e468c | 1928 | struct efx_nic *efx = netdev_priv(net_dev); |
22bedad3 | 1929 | struct netdev_hw_addr *ha; |
8ceee660 | 1930 | union efx_multicast_hash *mc_hash = &efx->multicast_hash; |
8ceee660 BH |
1931 | u32 crc; |
1932 | int bit; | |
8ceee660 | 1933 | |
8be4f3e6 | 1934 | efx->promiscuous = !!(net_dev->flags & IFF_PROMISC); |
8ceee660 BH |
1935 | |
1936 | /* Build multicast hash table */ | |
8be4f3e6 | 1937 | if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) { |
8ceee660 BH |
1938 | memset(mc_hash, 0xff, sizeof(*mc_hash)); |
1939 | } else { | |
1940 | memset(mc_hash, 0x00, sizeof(*mc_hash)); | |
22bedad3 JP |
1941 | netdev_for_each_mc_addr(ha, net_dev) { |
1942 | crc = ether_crc_le(ETH_ALEN, ha->addr); | |
8ceee660 BH |
1943 | bit = crc & (EFX_MCAST_HASH_ENTRIES - 1); |
1944 | set_bit_le(bit, mc_hash->byte); | |
8ceee660 | 1945 | } |
8ceee660 | 1946 | |
8be4f3e6 BH |
1947 | /* Broadcast packets go through the multicast hash filter. |
1948 | * ether_crc_le() of the broadcast address is 0xbe2612ff | |
1949 | * so we always add bit 0xff to the mask. | |
1950 | */ | |
1951 | set_bit_le(0xff, mc_hash->byte); | |
1952 | } | |
a816f75a | 1953 | |
8be4f3e6 BH |
1954 | if (efx->port_enabled) |
1955 | queue_work(efx->workqueue, &efx->mac_work); | |
1956 | /* Otherwise efx_start_port() will do this */ | |
8ceee660 BH |
1957 | } |
1958 | ||
c8f44aff | 1959 | static int efx_set_features(struct net_device *net_dev, netdev_features_t data) |
abfe9039 BH |
1960 | { |
1961 | struct efx_nic *efx = netdev_priv(net_dev); | |
1962 | ||
1963 | /* If disabling RX n-tuple filtering, clear existing filters */ | |
1964 | if (net_dev->features & ~data & NETIF_F_NTUPLE) | |
1965 | efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL); | |
1966 | ||
1967 | return 0; | |
1968 | } | |
1969 | ||
c3ecb9f3 SH |
1970 | static const struct net_device_ops efx_netdev_ops = { |
1971 | .ndo_open = efx_net_open, | |
1972 | .ndo_stop = efx_net_stop, | |
4472702e | 1973 | .ndo_get_stats64 = efx_net_stats, |
c3ecb9f3 SH |
1974 | .ndo_tx_timeout = efx_watchdog, |
1975 | .ndo_start_xmit = efx_hard_start_xmit, | |
1976 | .ndo_validate_addr = eth_validate_addr, | |
1977 | .ndo_do_ioctl = efx_ioctl, | |
1978 | .ndo_change_mtu = efx_change_mtu, | |
1979 | .ndo_set_mac_address = efx_set_mac_address, | |
0fca8c97 | 1980 | .ndo_set_rx_mode = efx_set_rx_mode, |
abfe9039 | 1981 | .ndo_set_features = efx_set_features, |
c3ecb9f3 SH |
1982 | #ifdef CONFIG_NET_POLL_CONTROLLER |
1983 | .ndo_poll_controller = efx_netpoll, | |
1984 | #endif | |
94b274bf | 1985 | .ndo_setup_tc = efx_setup_tc, |
64d8ad6d BH |
1986 | #ifdef CONFIG_RFS_ACCEL |
1987 | .ndo_rx_flow_steer = efx_filter_rfs, | |
1988 | #endif | |
c3ecb9f3 SH |
1989 | }; |
1990 | ||
7dde596e BH |
1991 | static void efx_update_name(struct efx_nic *efx) |
1992 | { | |
1993 | strcpy(efx->name, efx->net_dev->name); | |
1994 | efx_mtd_rename(efx); | |
1995 | efx_set_channel_names(efx); | |
1996 | } | |
1997 | ||
8ceee660 BH |
1998 | static int efx_netdev_event(struct notifier_block *this, |
1999 | unsigned long event, void *ptr) | |
2000 | { | |
d3208b5e | 2001 | struct net_device *net_dev = ptr; |
8ceee660 | 2002 | |
7dde596e BH |
2003 | if (net_dev->netdev_ops == &efx_netdev_ops && |
2004 | event == NETDEV_CHANGENAME) | |
2005 | efx_update_name(netdev_priv(net_dev)); | |
8ceee660 BH |
2006 | |
2007 | return NOTIFY_DONE; | |
2008 | } | |
2009 | ||
2010 | static struct notifier_block efx_netdev_notifier = { | |
2011 | .notifier_call = efx_netdev_event, | |
2012 | }; | |
2013 | ||
06d5e193 BH |
2014 | static ssize_t |
2015 | show_phy_type(struct device *dev, struct device_attribute *attr, char *buf) | |
2016 | { | |
2017 | struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev)); | |
2018 | return sprintf(buf, "%d\n", efx->phy_type); | |
2019 | } | |
2020 | static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL); | |
2021 | ||
8ceee660 BH |
2022 | static int efx_register_netdev(struct efx_nic *efx) |
2023 | { | |
2024 | struct net_device *net_dev = efx->net_dev; | |
c04bfc6b | 2025 | struct efx_channel *channel; |
8ceee660 BH |
2026 | int rc; |
2027 | ||
2028 | net_dev->watchdog_timeo = 5 * HZ; | |
2029 | net_dev->irq = efx->pci_dev->irq; | |
c3ecb9f3 | 2030 | net_dev->netdev_ops = &efx_netdev_ops; |
8ceee660 BH |
2031 | SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops); |
2032 | ||
7dde596e | 2033 | rtnl_lock(); |
aed0628d BH |
2034 | |
2035 | rc = dev_alloc_name(net_dev, net_dev->name); | |
2036 | if (rc < 0) | |
2037 | goto fail_locked; | |
7dde596e | 2038 | efx_update_name(efx); |
aed0628d BH |
2039 | |
2040 | rc = register_netdevice(net_dev); | |
2041 | if (rc) | |
2042 | goto fail_locked; | |
2043 | ||
c04bfc6b BH |
2044 | efx_for_each_channel(channel, efx) { |
2045 | struct efx_tx_queue *tx_queue; | |
60031fcc BH |
2046 | efx_for_each_channel_tx_queue(tx_queue, channel) |
2047 | efx_init_tx_queue_core_txq(tx_queue); | |
c04bfc6b BH |
2048 | } |
2049 | ||
aed0628d | 2050 | /* Always start with carrier off; PHY events will detect the link */ |
86ee5302 | 2051 | netif_carrier_off(net_dev); |
aed0628d | 2052 | |
7dde596e | 2053 | rtnl_unlock(); |
8ceee660 | 2054 | |
06d5e193 BH |
2055 | rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type); |
2056 | if (rc) { | |
62776d03 BH |
2057 | netif_err(efx, drv, efx->net_dev, |
2058 | "failed to init net dev attributes\n"); | |
06d5e193 BH |
2059 | goto fail_registered; |
2060 | } | |
2061 | ||
8ceee660 | 2062 | return 0; |
06d5e193 | 2063 | |
aed0628d BH |
2064 | fail_locked: |
2065 | rtnl_unlock(); | |
62776d03 | 2066 | netif_err(efx, drv, efx->net_dev, "could not register net dev\n"); |
aed0628d BH |
2067 | return rc; |
2068 | ||
06d5e193 BH |
2069 | fail_registered: |
2070 | unregister_netdev(net_dev); | |
2071 | return rc; | |
8ceee660 BH |
2072 | } |
2073 | ||
2074 | static void efx_unregister_netdev(struct efx_nic *efx) | |
2075 | { | |
f7d12cdc | 2076 | struct efx_channel *channel; |
8ceee660 BH |
2077 | struct efx_tx_queue *tx_queue; |
2078 | ||
2079 | if (!efx->net_dev) | |
2080 | return; | |
2081 | ||
767e468c | 2082 | BUG_ON(netdev_priv(efx->net_dev) != efx); |
8ceee660 BH |
2083 | |
2084 | /* Free up any skbs still remaining. This has to happen before | |
2085 | * we try to unregister the netdev as running their destructors | |
2086 | * may be needed to get the device ref. count to 0. */ | |
f7d12cdc BH |
2087 | efx_for_each_channel(channel, efx) { |
2088 | efx_for_each_channel_tx_queue(tx_queue, channel) | |
2089 | efx_release_tx_buffers(tx_queue); | |
2090 | } | |
8ceee660 | 2091 | |
73ba7b68 BH |
2092 | strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name)); |
2093 | device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type); | |
2094 | unregister_netdev(efx->net_dev); | |
8ceee660 BH |
2095 | } |
2096 | ||
2097 | /************************************************************************** | |
2098 | * | |
2099 | * Device reset and suspend | |
2100 | * | |
2101 | **************************************************************************/ | |
2102 | ||
2467ca46 BH |
2103 | /* Tears down the entire software state and most of the hardware state |
2104 | * before reset. */ | |
d3245b28 | 2105 | void efx_reset_down(struct efx_nic *efx, enum reset_type method) |
8ceee660 | 2106 | { |
8ceee660 BH |
2107 | EFX_ASSERT_RESET_SERIALISED(efx); |
2108 | ||
2467ca46 BH |
2109 | efx_stop_all(efx); |
2110 | mutex_lock(&efx->mac_lock); | |
2111 | ||
7f967c01 | 2112 | efx_stop_interrupts(efx, false); |
4b988280 SH |
2113 | if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) |
2114 | efx->phy_op->fini(efx); | |
ef2b90ee | 2115 | efx->type->fini(efx); |
8ceee660 BH |
2116 | } |
2117 | ||
2467ca46 BH |
2118 | /* This function will always ensure that the locks acquired in |
2119 | * efx_reset_down() are released. A failure return code indicates | |
2120 | * that we were unable to reinitialise the hardware, and the | |
2121 | * driver should be disabled. If ok is false, then the rx and tx | |
2122 | * engines are not restarted, pending a RESET_DISABLE. */ | |
d3245b28 | 2123 | int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok) |
8ceee660 BH |
2124 | { |
2125 | int rc; | |
2126 | ||
2467ca46 | 2127 | EFX_ASSERT_RESET_SERIALISED(efx); |
8ceee660 | 2128 | |
ef2b90ee | 2129 | rc = efx->type->init(efx); |
8ceee660 | 2130 | if (rc) { |
62776d03 | 2131 | netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n"); |
eb9f6744 | 2132 | goto fail; |
8ceee660 BH |
2133 | } |
2134 | ||
eb9f6744 BH |
2135 | if (!ok) |
2136 | goto fail; | |
2137 | ||
4b988280 | 2138 | if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) { |
eb9f6744 BH |
2139 | rc = efx->phy_op->init(efx); |
2140 | if (rc) | |
2141 | goto fail; | |
2142 | if (efx->phy_op->reconfigure(efx)) | |
62776d03 BH |
2143 | netif_err(efx, drv, efx->net_dev, |
2144 | "could not restore PHY settings\n"); | |
4b988280 SH |
2145 | } |
2146 | ||
710b208d | 2147 | efx->type->reconfigure_mac(efx); |
8ceee660 | 2148 | |
7f967c01 | 2149 | efx_start_interrupts(efx, false); |
64eebcfd | 2150 | efx_restore_filters(efx); |
eb9f6744 | 2151 | |
eb9f6744 BH |
2152 | mutex_unlock(&efx->mac_lock); |
2153 | ||
2154 | efx_start_all(efx); | |
2155 | ||
2156 | return 0; | |
2157 | ||
2158 | fail: | |
2159 | efx->port_initialized = false; | |
2467ca46 BH |
2160 | |
2161 | mutex_unlock(&efx->mac_lock); | |
2162 | ||
8ceee660 BH |
2163 | return rc; |
2164 | } | |
2165 | ||
eb9f6744 BH |
2166 | /* Reset the NIC using the specified method. Note that the reset may |
2167 | * fail, in which case the card will be left in an unusable state. | |
8ceee660 | 2168 | * |
eb9f6744 | 2169 | * Caller must hold the rtnl_lock. |
8ceee660 | 2170 | */ |
eb9f6744 | 2171 | int efx_reset(struct efx_nic *efx, enum reset_type method) |
8ceee660 | 2172 | { |
eb9f6744 BH |
2173 | int rc, rc2; |
2174 | bool disabled; | |
8ceee660 | 2175 | |
62776d03 BH |
2176 | netif_info(efx, drv, efx->net_dev, "resetting (%s)\n", |
2177 | RESET_TYPE(method)); | |
8ceee660 | 2178 | |
e4abce85 | 2179 | netif_device_detach(efx->net_dev); |
d3245b28 | 2180 | efx_reset_down(efx, method); |
8ceee660 | 2181 | |
ef2b90ee | 2182 | rc = efx->type->reset(efx, method); |
8ceee660 | 2183 | if (rc) { |
62776d03 | 2184 | netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n"); |
eb9f6744 | 2185 | goto out; |
8ceee660 BH |
2186 | } |
2187 | ||
a7d529ae BH |
2188 | /* Clear flags for the scopes we covered. We assume the NIC and |
2189 | * driver are now quiescent so that there is no race here. | |
2190 | */ | |
2191 | efx->reset_pending &= -(1 << (method + 1)); | |
8ceee660 BH |
2192 | |
2193 | /* Reinitialise bus-mastering, which may have been turned off before | |
2194 | * the reset was scheduled. This is still appropriate, even in the | |
2195 | * RESET_TYPE_DISABLE since this driver generally assumes the hardware | |
2196 | * can respond to requests. */ | |
2197 | pci_set_master(efx->pci_dev); | |
2198 | ||
eb9f6744 | 2199 | out: |
8ceee660 | 2200 | /* Leave device stopped if necessary */ |
eb9f6744 BH |
2201 | disabled = rc || method == RESET_TYPE_DISABLE; |
2202 | rc2 = efx_reset_up(efx, method, !disabled); | |
2203 | if (rc2) { | |
2204 | disabled = true; | |
2205 | if (!rc) | |
2206 | rc = rc2; | |
8ceee660 BH |
2207 | } |
2208 | ||
eb9f6744 | 2209 | if (disabled) { |
f49a4589 | 2210 | dev_close(efx->net_dev); |
62776d03 | 2211 | netif_err(efx, drv, efx->net_dev, "has been disabled\n"); |
f4bd954e | 2212 | efx->state = STATE_DISABLED; |
f4bd954e | 2213 | } else { |
62776d03 | 2214 | netif_dbg(efx, drv, efx->net_dev, "reset complete\n"); |
e4abce85 | 2215 | netif_device_attach(efx->net_dev); |
f4bd954e | 2216 | } |
8ceee660 BH |
2217 | return rc; |
2218 | } | |
2219 | ||
2220 | /* The worker thread exists so that code that cannot sleep can | |
2221 | * schedule a reset for later. | |
2222 | */ | |
2223 | static void efx_reset_work(struct work_struct *data) | |
2224 | { | |
eb9f6744 | 2225 | struct efx_nic *efx = container_of(data, struct efx_nic, reset_work); |
a7d529ae | 2226 | unsigned long pending = ACCESS_ONCE(efx->reset_pending); |
8ceee660 | 2227 | |
a7d529ae | 2228 | if (!pending) |
319ba649 SH |
2229 | return; |
2230 | ||
eb9f6744 | 2231 | /* If we're not RUNNING then don't reset. Leave the reset_pending |
a7d529ae | 2232 | * flags set so that efx_pci_probe_main will be retried */ |
eb9f6744 | 2233 | if (efx->state != STATE_RUNNING) { |
62776d03 BH |
2234 | netif_info(efx, drv, efx->net_dev, |
2235 | "scheduled reset quenched. NIC not RUNNING\n"); | |
eb9f6744 BH |
2236 | return; |
2237 | } | |
2238 | ||
2239 | rtnl_lock(); | |
a7d529ae | 2240 | (void)efx_reset(efx, fls(pending) - 1); |
eb9f6744 | 2241 | rtnl_unlock(); |
8ceee660 BH |
2242 | } |
2243 | ||
2244 | void efx_schedule_reset(struct efx_nic *efx, enum reset_type type) | |
2245 | { | |
2246 | enum reset_type method; | |
2247 | ||
8ceee660 BH |
2248 | switch (type) { |
2249 | case RESET_TYPE_INVISIBLE: | |
2250 | case RESET_TYPE_ALL: | |
2251 | case RESET_TYPE_WORLD: | |
2252 | case RESET_TYPE_DISABLE: | |
2253 | method = type; | |
0e2a9c7c BH |
2254 | netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n", |
2255 | RESET_TYPE(method)); | |
8ceee660 | 2256 | break; |
8ceee660 | 2257 | default: |
0e2a9c7c | 2258 | method = efx->type->map_reset_reason(type); |
62776d03 BH |
2259 | netif_dbg(efx, drv, efx->net_dev, |
2260 | "scheduling %s reset for %s\n", | |
2261 | RESET_TYPE(method), RESET_TYPE(type)); | |
0e2a9c7c BH |
2262 | break; |
2263 | } | |
8ceee660 | 2264 | |
a7d529ae | 2265 | set_bit(method, &efx->reset_pending); |
8ceee660 | 2266 | |
8880f4ec BH |
2267 | /* efx_process_channel() will no longer read events once a |
2268 | * reset is scheduled. So switch back to poll'd MCDI completions. */ | |
2269 | efx_mcdi_mode_poll(efx); | |
2270 | ||
1ab00629 | 2271 | queue_work(reset_workqueue, &efx->reset_work); |
8ceee660 BH |
2272 | } |
2273 | ||
2274 | /************************************************************************** | |
2275 | * | |
2276 | * List of NICs we support | |
2277 | * | |
2278 | **************************************************************************/ | |
2279 | ||
2280 | /* PCI device ID table */ | |
a3aa1884 | 2281 | static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = { |
937383a5 BH |
2282 | {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, |
2283 | PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0), | |
daeda630 | 2284 | .driver_data = (unsigned long) &falcon_a1_nic_type}, |
937383a5 BH |
2285 | {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, |
2286 | PCI_DEVICE_ID_SOLARFLARE_SFC4000B), | |
daeda630 | 2287 | .driver_data = (unsigned long) &falcon_b0_nic_type}, |
547c474f | 2288 | {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */ |
8880f4ec | 2289 | .driver_data = (unsigned long) &siena_a0_nic_type}, |
547c474f | 2290 | {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */ |
8880f4ec | 2291 | .driver_data = (unsigned long) &siena_a0_nic_type}, |
8ceee660 BH |
2292 | {0} /* end of list */ |
2293 | }; | |
2294 | ||
2295 | /************************************************************************** | |
2296 | * | |
3759433d | 2297 | * Dummy PHY/MAC operations |
8ceee660 | 2298 | * |
01aad7b6 | 2299 | * Can be used for some unimplemented operations |
8ceee660 BH |
2300 | * Needed so all function pointers are valid and do not have to be tested |
2301 | * before use | |
2302 | * | |
2303 | **************************************************************************/ | |
2304 | int efx_port_dummy_op_int(struct efx_nic *efx) | |
2305 | { | |
2306 | return 0; | |
2307 | } | |
2308 | void efx_port_dummy_op_void(struct efx_nic *efx) {} | |
d215697f | 2309 | |
2310 | static bool efx_port_dummy_op_poll(struct efx_nic *efx) | |
fdaa9aed SH |
2311 | { |
2312 | return false; | |
2313 | } | |
8ceee660 | 2314 | |
6c8c2513 | 2315 | static const struct efx_phy_operations efx_dummy_phy_operations = { |
8ceee660 | 2316 | .init = efx_port_dummy_op_int, |
d3245b28 | 2317 | .reconfigure = efx_port_dummy_op_int, |
fdaa9aed | 2318 | .poll = efx_port_dummy_op_poll, |
8ceee660 | 2319 | .fini = efx_port_dummy_op_void, |
8ceee660 BH |
2320 | }; |
2321 | ||
8ceee660 BH |
2322 | /************************************************************************** |
2323 | * | |
2324 | * Data housekeeping | |
2325 | * | |
2326 | **************************************************************************/ | |
2327 | ||
2328 | /* This zeroes out and then fills in the invariants in a struct | |
2329 | * efx_nic (including all sub-structures). | |
2330 | */ | |
6c8c2513 | 2331 | static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type, |
8ceee660 BH |
2332 | struct pci_dev *pci_dev, struct net_device *net_dev) |
2333 | { | |
4642610c | 2334 | int i; |
8ceee660 BH |
2335 | |
2336 | /* Initialise common structures */ | |
2337 | memset(efx, 0, sizeof(*efx)); | |
2338 | spin_lock_init(&efx->biu_lock); | |
76884835 BH |
2339 | #ifdef CONFIG_SFC_MTD |
2340 | INIT_LIST_HEAD(&efx->mtd_list); | |
2341 | #endif | |
8ceee660 BH |
2342 | INIT_WORK(&efx->reset_work, efx_reset_work); |
2343 | INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor); | |
2344 | efx->pci_dev = pci_dev; | |
62776d03 | 2345 | efx->msg_enable = debug; |
8ceee660 | 2346 | efx->state = STATE_INIT; |
8ceee660 | 2347 | strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name)); |
8ceee660 BH |
2348 | |
2349 | efx->net_dev = net_dev; | |
8ceee660 BH |
2350 | spin_lock_init(&efx->stats_lock); |
2351 | mutex_init(&efx->mac_lock); | |
2352 | efx->phy_op = &efx_dummy_phy_operations; | |
68e7f45e | 2353 | efx->mdio.dev = net_dev; |
766ca0fa | 2354 | INIT_WORK(&efx->mac_work, efx_mac_work); |
9f2cb71c | 2355 | init_waitqueue_head(&efx->flush_wq); |
8ceee660 BH |
2356 | |
2357 | for (i = 0; i < EFX_MAX_CHANNELS; i++) { | |
4642610c BH |
2358 | efx->channel[i] = efx_alloc_channel(efx, i, NULL); |
2359 | if (!efx->channel[i]) | |
2360 | goto fail; | |
8ceee660 BH |
2361 | } |
2362 | ||
2363 | efx->type = type; | |
2364 | ||
8ceee660 BH |
2365 | EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS); |
2366 | ||
2367 | /* Higher numbered interrupt modes are less capable! */ | |
2368 | efx->interrupt_mode = max(efx->type->max_interrupt_mode, | |
2369 | interrupt_mode); | |
2370 | ||
6977dc63 BH |
2371 | /* Would be good to use the net_dev name, but we're too early */ |
2372 | snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s", | |
2373 | pci_name(pci_dev)); | |
2374 | efx->workqueue = create_singlethread_workqueue(efx->workqueue_name); | |
1ab00629 | 2375 | if (!efx->workqueue) |
4642610c | 2376 | goto fail; |
8d9853d9 | 2377 | |
8ceee660 | 2378 | return 0; |
4642610c BH |
2379 | |
2380 | fail: | |
2381 | efx_fini_struct(efx); | |
2382 | return -ENOMEM; | |
8ceee660 BH |
2383 | } |
2384 | ||
2385 | static void efx_fini_struct(struct efx_nic *efx) | |
2386 | { | |
8313aca3 BH |
2387 | int i; |
2388 | ||
2389 | for (i = 0; i < EFX_MAX_CHANNELS; i++) | |
2390 | kfree(efx->channel[i]); | |
2391 | ||
8ceee660 BH |
2392 | if (efx->workqueue) { |
2393 | destroy_workqueue(efx->workqueue); | |
2394 | efx->workqueue = NULL; | |
2395 | } | |
2396 | } | |
2397 | ||
2398 | /************************************************************************** | |
2399 | * | |
2400 | * PCI interface | |
2401 | * | |
2402 | **************************************************************************/ | |
2403 | ||
2404 | /* Main body of final NIC shutdown code | |
2405 | * This is called only at module unload (or hotplug removal). | |
2406 | */ | |
2407 | static void efx_pci_remove_main(struct efx_nic *efx) | |
2408 | { | |
64d8ad6d BH |
2409 | #ifdef CONFIG_RFS_ACCEL |
2410 | free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap); | |
2411 | efx->net_dev->rx_cpu_rmap = NULL; | |
2412 | #endif | |
7f967c01 | 2413 | efx_stop_interrupts(efx, false); |
152b6a62 | 2414 | efx_nic_fini_interrupt(efx); |
8ceee660 | 2415 | efx_fini_port(efx); |
ef2b90ee | 2416 | efx->type->fini(efx); |
8ceee660 BH |
2417 | efx_fini_napi(efx); |
2418 | efx_remove_all(efx); | |
2419 | } | |
2420 | ||
2421 | /* Final NIC shutdown | |
2422 | * This is called only at module unload (or hotplug removal). | |
2423 | */ | |
2424 | static void efx_pci_remove(struct pci_dev *pci_dev) | |
2425 | { | |
2426 | struct efx_nic *efx; | |
2427 | ||
2428 | efx = pci_get_drvdata(pci_dev); | |
2429 | if (!efx) | |
2430 | return; | |
2431 | ||
2432 | /* Mark the NIC as fini, then stop the interface */ | |
2433 | rtnl_lock(); | |
2434 | efx->state = STATE_FINI; | |
2435 | dev_close(efx->net_dev); | |
2436 | ||
2437 | /* Allow any queued efx_resets() to complete */ | |
2438 | rtnl_unlock(); | |
2439 | ||
7f967c01 | 2440 | efx_stop_interrupts(efx, false); |
8ceee660 BH |
2441 | efx_unregister_netdev(efx); |
2442 | ||
7dde596e BH |
2443 | efx_mtd_remove(efx); |
2444 | ||
8ceee660 BH |
2445 | /* Wait for any scheduled resets to complete. No more will be |
2446 | * scheduled from this point because efx_stop_all() has been | |
2447 | * called, we are no longer registered with driverlink, and | |
2448 | * the net_device's have been removed. */ | |
1ab00629 | 2449 | cancel_work_sync(&efx->reset_work); |
8ceee660 BH |
2450 | |
2451 | efx_pci_remove_main(efx); | |
2452 | ||
8ceee660 | 2453 | efx_fini_io(efx); |
62776d03 | 2454 | netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n"); |
8ceee660 BH |
2455 | |
2456 | pci_set_drvdata(pci_dev, NULL); | |
2457 | efx_fini_struct(efx); | |
2458 | free_netdev(efx->net_dev); | |
2459 | }; | |
2460 | ||
2461 | /* Main body of NIC initialisation | |
2462 | * This is called at module load (or hotplug insertion, theoretically). | |
2463 | */ | |
2464 | static int efx_pci_probe_main(struct efx_nic *efx) | |
2465 | { | |
2466 | int rc; | |
2467 | ||
2468 | /* Do start-of-day initialisation */ | |
2469 | rc = efx_probe_all(efx); | |
2470 | if (rc) | |
2471 | goto fail1; | |
2472 | ||
e8f14992 | 2473 | efx_init_napi(efx); |
8ceee660 | 2474 | |
ef2b90ee | 2475 | rc = efx->type->init(efx); |
8ceee660 | 2476 | if (rc) { |
62776d03 BH |
2477 | netif_err(efx, probe, efx->net_dev, |
2478 | "failed to initialise NIC\n"); | |
278c0621 | 2479 | goto fail3; |
8ceee660 BH |
2480 | } |
2481 | ||
2482 | rc = efx_init_port(efx); | |
2483 | if (rc) { | |
62776d03 BH |
2484 | netif_err(efx, probe, efx->net_dev, |
2485 | "failed to initialise port\n"); | |
278c0621 | 2486 | goto fail4; |
8ceee660 BH |
2487 | } |
2488 | ||
152b6a62 | 2489 | rc = efx_nic_init_interrupt(efx); |
8ceee660 | 2490 | if (rc) |
278c0621 | 2491 | goto fail5; |
7f967c01 | 2492 | efx_start_interrupts(efx, false); |
8ceee660 BH |
2493 | |
2494 | return 0; | |
2495 | ||
278c0621 | 2496 | fail5: |
8ceee660 | 2497 | efx_fini_port(efx); |
8ceee660 | 2498 | fail4: |
ef2b90ee | 2499 | efx->type->fini(efx); |
8ceee660 BH |
2500 | fail3: |
2501 | efx_fini_napi(efx); | |
8ceee660 BH |
2502 | efx_remove_all(efx); |
2503 | fail1: | |
2504 | return rc; | |
2505 | } | |
2506 | ||
2507 | /* NIC initialisation | |
2508 | * | |
2509 | * This is called at module load (or hotplug insertion, | |
73ba7b68 | 2510 | * theoretically). It sets up PCI mappings, resets the NIC, |
8ceee660 BH |
2511 | * sets up and registers the network devices with the kernel and hooks |
2512 | * the interrupt service routine. It does not prepare the device for | |
2513 | * transmission; this is left to the first time one of the network | |
2514 | * interfaces is brought up (i.e. efx_net_open). | |
2515 | */ | |
2516 | static int __devinit efx_pci_probe(struct pci_dev *pci_dev, | |
2517 | const struct pci_device_id *entry) | |
2518 | { | |
6c8c2513 | 2519 | const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data; |
8ceee660 BH |
2520 | struct net_device *net_dev; |
2521 | struct efx_nic *efx; | |
fadac6aa | 2522 | int rc; |
8ceee660 BH |
2523 | |
2524 | /* Allocate and initialise a struct net_device and struct efx_nic */ | |
94b274bf BH |
2525 | net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES, |
2526 | EFX_MAX_RX_QUEUES); | |
8ceee660 BH |
2527 | if (!net_dev) |
2528 | return -ENOMEM; | |
c383b537 | 2529 | net_dev->features |= (type->offload_features | NETIF_F_SG | |
97bc5415 | 2530 | NETIF_F_HIGHDMA | NETIF_F_TSO | |
abfe9039 | 2531 | NETIF_F_RXCSUM); |
738a8f4b BH |
2532 | if (type->offload_features & NETIF_F_V6_CSUM) |
2533 | net_dev->features |= NETIF_F_TSO6; | |
28506563 BH |
2534 | /* Mask for features that also apply to VLAN devices */ |
2535 | net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG | | |
abfe9039 BH |
2536 | NETIF_F_HIGHDMA | NETIF_F_ALL_TSO | |
2537 | NETIF_F_RXCSUM); | |
2538 | /* All offloads can be toggled */ | |
2539 | net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA; | |
767e468c | 2540 | efx = netdev_priv(net_dev); |
8ceee660 | 2541 | pci_set_drvdata(pci_dev, efx); |
62776d03 | 2542 | SET_NETDEV_DEV(net_dev, &pci_dev->dev); |
8ceee660 BH |
2543 | rc = efx_init_struct(efx, type, pci_dev, net_dev); |
2544 | if (rc) | |
2545 | goto fail1; | |
2546 | ||
62776d03 | 2547 | netif_info(efx, probe, efx->net_dev, |
ff79c8ac | 2548 | "Solarflare NIC detected\n"); |
8ceee660 BH |
2549 | |
2550 | /* Set up basic I/O (BAR mappings etc) */ | |
2551 | rc = efx_init_io(efx); | |
2552 | if (rc) | |
2553 | goto fail2; | |
2554 | ||
fadac6aa | 2555 | rc = efx_pci_probe_main(efx); |
fa402b2e | 2556 | |
fadac6aa BH |
2557 | /* Serialise against efx_reset(). No more resets will be |
2558 | * scheduled since efx_stop_all() has been called, and we have | |
2559 | * not and never have been registered. | |
2560 | */ | |
2561 | cancel_work_sync(&efx->reset_work); | |
8ceee660 | 2562 | |
fadac6aa BH |
2563 | if (rc) |
2564 | goto fail3; | |
8ceee660 | 2565 | |
fadac6aa BH |
2566 | /* If there was a scheduled reset during probe, the NIC is |
2567 | * probably hosed anyway. | |
2568 | */ | |
2569 | if (efx->reset_pending) { | |
2570 | rc = -EIO; | |
8ceee660 BH |
2571 | goto fail4; |
2572 | } | |
2573 | ||
55edc6e6 BH |
2574 | /* Switch to the running state before we expose the device to the OS, |
2575 | * so that dev_open()|efx_start_all() will actually start the device */ | |
8ceee660 | 2576 | efx->state = STATE_RUNNING; |
7dde596e | 2577 | |
8ceee660 BH |
2578 | rc = efx_register_netdev(efx); |
2579 | if (rc) | |
fadac6aa | 2580 | goto fail4; |
8ceee660 | 2581 | |
62776d03 | 2582 | netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n"); |
a5211bb5 | 2583 | |
7c43161c | 2584 | /* Try to create MTDs, but allow this to fail */ |
a5211bb5 | 2585 | rtnl_lock(); |
7c43161c | 2586 | rc = efx_mtd_probe(efx); |
a5211bb5 | 2587 | rtnl_unlock(); |
7c43161c BH |
2588 | if (rc) |
2589 | netif_warn(efx, probe, efx->net_dev, | |
2590 | "failed to create MTDs (%d)\n", rc); | |
2591 | ||
8ceee660 BH |
2592 | return 0; |
2593 | ||
8ceee660 | 2594 | fail4: |
fadac6aa | 2595 | efx_pci_remove_main(efx); |
8ceee660 BH |
2596 | fail3: |
2597 | efx_fini_io(efx); | |
2598 | fail2: | |
2599 | efx_fini_struct(efx); | |
2600 | fail1: | |
5e2a911c | 2601 | WARN_ON(rc > 0); |
62776d03 | 2602 | netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc); |
8ceee660 BH |
2603 | free_netdev(net_dev); |
2604 | return rc; | |
2605 | } | |
2606 | ||
89c758fa BH |
2607 | static int efx_pm_freeze(struct device *dev) |
2608 | { | |
2609 | struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev)); | |
2610 | ||
2611 | efx->state = STATE_FINI; | |
2612 | ||
2613 | netif_device_detach(efx->net_dev); | |
2614 | ||
2615 | efx_stop_all(efx); | |
7f967c01 | 2616 | efx_stop_interrupts(efx, false); |
89c758fa BH |
2617 | |
2618 | return 0; | |
2619 | } | |
2620 | ||
2621 | static int efx_pm_thaw(struct device *dev) | |
2622 | { | |
2623 | struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev)); | |
2624 | ||
2625 | efx->state = STATE_INIT; | |
2626 | ||
7f967c01 | 2627 | efx_start_interrupts(efx, false); |
89c758fa BH |
2628 | |
2629 | mutex_lock(&efx->mac_lock); | |
2630 | efx->phy_op->reconfigure(efx); | |
2631 | mutex_unlock(&efx->mac_lock); | |
2632 | ||
2633 | efx_start_all(efx); | |
2634 | ||
2635 | netif_device_attach(efx->net_dev); | |
2636 | ||
2637 | efx->state = STATE_RUNNING; | |
2638 | ||
2639 | efx->type->resume_wol(efx); | |
2640 | ||
319ba649 SH |
2641 | /* Reschedule any quenched resets scheduled during efx_pm_freeze() */ |
2642 | queue_work(reset_workqueue, &efx->reset_work); | |
2643 | ||
89c758fa BH |
2644 | return 0; |
2645 | } | |
2646 | ||
2647 | static int efx_pm_poweroff(struct device *dev) | |
2648 | { | |
2649 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
2650 | struct efx_nic *efx = pci_get_drvdata(pci_dev); | |
2651 | ||
2652 | efx->type->fini(efx); | |
2653 | ||
a7d529ae | 2654 | efx->reset_pending = 0; |
89c758fa BH |
2655 | |
2656 | pci_save_state(pci_dev); | |
2657 | return pci_set_power_state(pci_dev, PCI_D3hot); | |
2658 | } | |
2659 | ||
2660 | /* Used for both resume and restore */ | |
2661 | static int efx_pm_resume(struct device *dev) | |
2662 | { | |
2663 | struct pci_dev *pci_dev = to_pci_dev(dev); | |
2664 | struct efx_nic *efx = pci_get_drvdata(pci_dev); | |
2665 | int rc; | |
2666 | ||
2667 | rc = pci_set_power_state(pci_dev, PCI_D0); | |
2668 | if (rc) | |
2669 | return rc; | |
2670 | pci_restore_state(pci_dev); | |
2671 | rc = pci_enable_device(pci_dev); | |
2672 | if (rc) | |
2673 | return rc; | |
2674 | pci_set_master(efx->pci_dev); | |
2675 | rc = efx->type->reset(efx, RESET_TYPE_ALL); | |
2676 | if (rc) | |
2677 | return rc; | |
2678 | rc = efx->type->init(efx); | |
2679 | if (rc) | |
2680 | return rc; | |
2681 | efx_pm_thaw(dev); | |
2682 | return 0; | |
2683 | } | |
2684 | ||
2685 | static int efx_pm_suspend(struct device *dev) | |
2686 | { | |
2687 | int rc; | |
2688 | ||
2689 | efx_pm_freeze(dev); | |
2690 | rc = efx_pm_poweroff(dev); | |
2691 | if (rc) | |
2692 | efx_pm_resume(dev); | |
2693 | return rc; | |
2694 | } | |
2695 | ||
18e83e4c | 2696 | static const struct dev_pm_ops efx_pm_ops = { |
89c758fa BH |
2697 | .suspend = efx_pm_suspend, |
2698 | .resume = efx_pm_resume, | |
2699 | .freeze = efx_pm_freeze, | |
2700 | .thaw = efx_pm_thaw, | |
2701 | .poweroff = efx_pm_poweroff, | |
2702 | .restore = efx_pm_resume, | |
2703 | }; | |
2704 | ||
8ceee660 | 2705 | static struct pci_driver efx_pci_driver = { |
c5d5f5fd | 2706 | .name = KBUILD_MODNAME, |
8ceee660 BH |
2707 | .id_table = efx_pci_table, |
2708 | .probe = efx_pci_probe, | |
2709 | .remove = efx_pci_remove, | |
89c758fa | 2710 | .driver.pm = &efx_pm_ops, |
8ceee660 BH |
2711 | }; |
2712 | ||
2713 | /************************************************************************** | |
2714 | * | |
2715 | * Kernel module interface | |
2716 | * | |
2717 | *************************************************************************/ | |
2718 | ||
2719 | module_param(interrupt_mode, uint, 0444); | |
2720 | MODULE_PARM_DESC(interrupt_mode, | |
2721 | "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)"); | |
2722 | ||
2723 | static int __init efx_init_module(void) | |
2724 | { | |
2725 | int rc; | |
2726 | ||
2727 | printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n"); | |
2728 | ||
2729 | rc = register_netdevice_notifier(&efx_netdev_notifier); | |
2730 | if (rc) | |
2731 | goto err_notifier; | |
2732 | ||
1ab00629 SH |
2733 | reset_workqueue = create_singlethread_workqueue("sfc_reset"); |
2734 | if (!reset_workqueue) { | |
2735 | rc = -ENOMEM; | |
2736 | goto err_reset; | |
2737 | } | |
8ceee660 BH |
2738 | |
2739 | rc = pci_register_driver(&efx_pci_driver); | |
2740 | if (rc < 0) | |
2741 | goto err_pci; | |
2742 | ||
2743 | return 0; | |
2744 | ||
2745 | err_pci: | |
1ab00629 SH |
2746 | destroy_workqueue(reset_workqueue); |
2747 | err_reset: | |
8ceee660 BH |
2748 | unregister_netdevice_notifier(&efx_netdev_notifier); |
2749 | err_notifier: | |
2750 | return rc; | |
2751 | } | |
2752 | ||
2753 | static void __exit efx_exit_module(void) | |
2754 | { | |
2755 | printk(KERN_INFO "Solarflare NET driver unloading\n"); | |
2756 | ||
2757 | pci_unregister_driver(&efx_pci_driver); | |
1ab00629 | 2758 | destroy_workqueue(reset_workqueue); |
8ceee660 BH |
2759 | unregister_netdevice_notifier(&efx_netdev_notifier); |
2760 | ||
2761 | } | |
2762 | ||
2763 | module_init(efx_init_module); | |
2764 | module_exit(efx_exit_module); | |
2765 | ||
906bb26c BH |
2766 | MODULE_AUTHOR("Solarflare Communications and " |
2767 | "Michael Brown <mbrown@fensystems.co.uk>"); | |
8ceee660 BH |
2768 | MODULE_DESCRIPTION("Solarflare Communications network driver"); |
2769 | MODULE_LICENSE("GPL"); | |
2770 | MODULE_DEVICE_TABLE(pci, efx_pci_table); |