]>
Commit | Line | Data |
---|---|---|
01f2e4ea SF |
1 | /* |
2 | * Copyright 2008 Cisco Systems, Inc. All rights reserved. | |
3 | * Copyright 2007 Nuova Systems, Inc. All rights reserved. | |
4 | * | |
5 | * This program is free software; you may redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; version 2 of the License. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
10 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
11 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
12 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
13 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
15 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
16 | * SOFTWARE. | |
17 | * | |
18 | */ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/errno.h> | |
24 | #include <linux/types.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/workqueue.h> | |
27 | #include <linux/pci.h> | |
28 | #include <linux/netdevice.h> | |
29 | #include <linux/etherdevice.h> | |
30 | #include <linux/if_ether.h> | |
31 | #include <linux/if_vlan.h> | |
32 | #include <linux/ethtool.h> | |
33 | #include <linux/in.h> | |
34 | #include <linux/ip.h> | |
35 | #include <linux/ipv6.h> | |
36 | #include <linux/tcp.h> | |
b7c6bfb7 | 37 | #include <net/ip6_checksum.h> |
01f2e4ea SF |
38 | |
39 | #include "cq_enet_desc.h" | |
40 | #include "vnic_dev.h" | |
41 | #include "vnic_intr.h" | |
42 | #include "vnic_stats.h" | |
43 | #include "enic_res.h" | |
44 | #include "enic.h" | |
45 | ||
46 | #define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ) | |
01f2e4ea SF |
47 | |
48 | /* Supported devices */ | |
49 | static struct pci_device_id enic_id_table[] = { | |
50 | { PCI_VDEVICE(CISCO, 0x0043) }, | |
51 | { 0, } /* end of table */ | |
52 | }; | |
53 | ||
54 | MODULE_DESCRIPTION(DRV_DESCRIPTION); | |
55 | MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>"); | |
56 | MODULE_LICENSE("GPL"); | |
57 | MODULE_VERSION(DRV_VERSION); | |
58 | MODULE_DEVICE_TABLE(pci, enic_id_table); | |
59 | ||
60 | struct enic_stat { | |
61 | char name[ETH_GSTRING_LEN]; | |
62 | unsigned int offset; | |
63 | }; | |
64 | ||
65 | #define ENIC_TX_STAT(stat) \ | |
66 | { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 } | |
67 | #define ENIC_RX_STAT(stat) \ | |
68 | { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 } | |
69 | ||
70 | static const struct enic_stat enic_tx_stats[] = { | |
71 | ENIC_TX_STAT(tx_frames_ok), | |
72 | ENIC_TX_STAT(tx_unicast_frames_ok), | |
73 | ENIC_TX_STAT(tx_multicast_frames_ok), | |
74 | ENIC_TX_STAT(tx_broadcast_frames_ok), | |
75 | ENIC_TX_STAT(tx_bytes_ok), | |
76 | ENIC_TX_STAT(tx_unicast_bytes_ok), | |
77 | ENIC_TX_STAT(tx_multicast_bytes_ok), | |
78 | ENIC_TX_STAT(tx_broadcast_bytes_ok), | |
79 | ENIC_TX_STAT(tx_drops), | |
80 | ENIC_TX_STAT(tx_errors), | |
81 | ENIC_TX_STAT(tx_tso), | |
82 | }; | |
83 | ||
84 | static const struct enic_stat enic_rx_stats[] = { | |
85 | ENIC_RX_STAT(rx_frames_ok), | |
86 | ENIC_RX_STAT(rx_frames_total), | |
87 | ENIC_RX_STAT(rx_unicast_frames_ok), | |
88 | ENIC_RX_STAT(rx_multicast_frames_ok), | |
89 | ENIC_RX_STAT(rx_broadcast_frames_ok), | |
90 | ENIC_RX_STAT(rx_bytes_ok), | |
91 | ENIC_RX_STAT(rx_unicast_bytes_ok), | |
92 | ENIC_RX_STAT(rx_multicast_bytes_ok), | |
93 | ENIC_RX_STAT(rx_broadcast_bytes_ok), | |
94 | ENIC_RX_STAT(rx_drop), | |
95 | ENIC_RX_STAT(rx_no_bufs), | |
96 | ENIC_RX_STAT(rx_errors), | |
97 | ENIC_RX_STAT(rx_rss), | |
98 | ENIC_RX_STAT(rx_crc_errors), | |
99 | ENIC_RX_STAT(rx_frames_64), | |
100 | ENIC_RX_STAT(rx_frames_127), | |
101 | ENIC_RX_STAT(rx_frames_255), | |
102 | ENIC_RX_STAT(rx_frames_511), | |
103 | ENIC_RX_STAT(rx_frames_1023), | |
104 | ENIC_RX_STAT(rx_frames_1518), | |
105 | ENIC_RX_STAT(rx_frames_to_max), | |
106 | }; | |
107 | ||
108 | static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats); | |
109 | static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats); | |
110 | ||
111 | static int enic_get_settings(struct net_device *netdev, | |
112 | struct ethtool_cmd *ecmd) | |
113 | { | |
114 | struct enic *enic = netdev_priv(netdev); | |
115 | ||
116 | ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE); | |
117 | ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE); | |
118 | ecmd->port = PORT_FIBRE; | |
119 | ecmd->transceiver = XCVR_EXTERNAL; | |
120 | ||
121 | if (netif_carrier_ok(netdev)) { | |
122 | ecmd->speed = vnic_dev_port_speed(enic->vdev); | |
123 | ecmd->duplex = DUPLEX_FULL; | |
124 | } else { | |
125 | ecmd->speed = -1; | |
126 | ecmd->duplex = -1; | |
127 | } | |
128 | ||
129 | ecmd->autoneg = AUTONEG_DISABLE; | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
134 | static void enic_get_drvinfo(struct net_device *netdev, | |
135 | struct ethtool_drvinfo *drvinfo) | |
136 | { | |
137 | struct enic *enic = netdev_priv(netdev); | |
138 | struct vnic_devcmd_fw_info *fw_info; | |
139 | ||
140 | spin_lock(&enic->devcmd_lock); | |
141 | vnic_dev_fw_info(enic->vdev, &fw_info); | |
142 | spin_unlock(&enic->devcmd_lock); | |
143 | ||
144 | strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver)); | |
145 | strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version)); | |
146 | strncpy(drvinfo->fw_version, fw_info->fw_version, | |
147 | sizeof(drvinfo->fw_version)); | |
148 | strncpy(drvinfo->bus_info, pci_name(enic->pdev), | |
149 | sizeof(drvinfo->bus_info)); | |
150 | } | |
151 | ||
152 | static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data) | |
153 | { | |
154 | unsigned int i; | |
155 | ||
156 | switch (stringset) { | |
157 | case ETH_SS_STATS: | |
158 | for (i = 0; i < enic_n_tx_stats; i++) { | |
159 | memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN); | |
160 | data += ETH_GSTRING_LEN; | |
161 | } | |
162 | for (i = 0; i < enic_n_rx_stats; i++) { | |
163 | memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN); | |
164 | data += ETH_GSTRING_LEN; | |
165 | } | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
25f0a061 | 170 | static int enic_get_sset_count(struct net_device *netdev, int sset) |
01f2e4ea | 171 | { |
25f0a061 SF |
172 | switch (sset) { |
173 | case ETH_SS_STATS: | |
174 | return enic_n_tx_stats + enic_n_rx_stats; | |
175 | default: | |
176 | return -EOPNOTSUPP; | |
177 | } | |
01f2e4ea SF |
178 | } |
179 | ||
180 | static void enic_get_ethtool_stats(struct net_device *netdev, | |
181 | struct ethtool_stats *stats, u64 *data) | |
182 | { | |
183 | struct enic *enic = netdev_priv(netdev); | |
184 | struct vnic_stats *vstats; | |
185 | unsigned int i; | |
186 | ||
187 | spin_lock(&enic->devcmd_lock); | |
188 | vnic_dev_stats_dump(enic->vdev, &vstats); | |
189 | spin_unlock(&enic->devcmd_lock); | |
190 | ||
191 | for (i = 0; i < enic_n_tx_stats; i++) | |
192 | *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset]; | |
193 | for (i = 0; i < enic_n_rx_stats; i++) | |
194 | *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset]; | |
195 | } | |
196 | ||
197 | static u32 enic_get_rx_csum(struct net_device *netdev) | |
198 | { | |
199 | struct enic *enic = netdev_priv(netdev); | |
200 | return enic->csum_rx_enabled; | |
201 | } | |
202 | ||
203 | static int enic_set_rx_csum(struct net_device *netdev, u32 data) | |
204 | { | |
205 | struct enic *enic = netdev_priv(netdev); | |
206 | ||
25f0a061 SF |
207 | if (data && !ENIC_SETTING(enic, RXCSUM)) |
208 | return -EINVAL; | |
209 | ||
210 | enic->csum_rx_enabled = !!data; | |
01f2e4ea SF |
211 | |
212 | return 0; | |
213 | } | |
214 | ||
215 | static int enic_set_tx_csum(struct net_device *netdev, u32 data) | |
216 | { | |
217 | struct enic *enic = netdev_priv(netdev); | |
218 | ||
25f0a061 SF |
219 | if (data && !ENIC_SETTING(enic, TXCSUM)) |
220 | return -EINVAL; | |
221 | ||
222 | if (data) | |
01f2e4ea SF |
223 | netdev->features |= NETIF_F_HW_CSUM; |
224 | else | |
225 | netdev->features &= ~NETIF_F_HW_CSUM; | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | static int enic_set_tso(struct net_device *netdev, u32 data) | |
231 | { | |
232 | struct enic *enic = netdev_priv(netdev); | |
233 | ||
25f0a061 SF |
234 | if (data && !ENIC_SETTING(enic, TSO)) |
235 | return -EINVAL; | |
236 | ||
237 | if (data) | |
01f2e4ea SF |
238 | netdev->features |= |
239 | NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN; | |
240 | else | |
241 | netdev->features &= | |
242 | ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN); | |
243 | ||
244 | return 0; | |
245 | } | |
246 | ||
247 | static u32 enic_get_msglevel(struct net_device *netdev) | |
248 | { | |
249 | struct enic *enic = netdev_priv(netdev); | |
250 | return enic->msg_enable; | |
251 | } | |
252 | ||
253 | static void enic_set_msglevel(struct net_device *netdev, u32 value) | |
254 | { | |
255 | struct enic *enic = netdev_priv(netdev); | |
256 | enic->msg_enable = value; | |
257 | } | |
258 | ||
259 | static struct ethtool_ops enic_ethtool_ops = { | |
260 | .get_settings = enic_get_settings, | |
261 | .get_drvinfo = enic_get_drvinfo, | |
262 | .get_msglevel = enic_get_msglevel, | |
263 | .set_msglevel = enic_set_msglevel, | |
264 | .get_link = ethtool_op_get_link, | |
265 | .get_strings = enic_get_strings, | |
25f0a061 | 266 | .get_sset_count = enic_get_sset_count, |
01f2e4ea SF |
267 | .get_ethtool_stats = enic_get_ethtool_stats, |
268 | .get_rx_csum = enic_get_rx_csum, | |
269 | .set_rx_csum = enic_set_rx_csum, | |
270 | .get_tx_csum = ethtool_op_get_tx_csum, | |
271 | .set_tx_csum = enic_set_tx_csum, | |
272 | .get_sg = ethtool_op_get_sg, | |
273 | .set_sg = ethtool_op_set_sg, | |
274 | .get_tso = ethtool_op_get_tso, | |
275 | .set_tso = enic_set_tso, | |
86ca9db7 SF |
276 | .get_flags = ethtool_op_get_flags, |
277 | .set_flags = ethtool_op_set_flags, | |
01f2e4ea SF |
278 | }; |
279 | ||
280 | static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf) | |
281 | { | |
282 | struct enic *enic = vnic_dev_priv(wq->vdev); | |
283 | ||
284 | if (buf->sop) | |
285 | pci_unmap_single(enic->pdev, buf->dma_addr, | |
286 | buf->len, PCI_DMA_TODEVICE); | |
287 | else | |
288 | pci_unmap_page(enic->pdev, buf->dma_addr, | |
289 | buf->len, PCI_DMA_TODEVICE); | |
290 | ||
291 | if (buf->os_buf) | |
292 | dev_kfree_skb_any(buf->os_buf); | |
293 | } | |
294 | ||
295 | static void enic_wq_free_buf(struct vnic_wq *wq, | |
296 | struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque) | |
297 | { | |
298 | enic_free_wq_buf(wq, buf); | |
299 | } | |
300 | ||
301 | static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc, | |
302 | u8 type, u16 q_number, u16 completed_index, void *opaque) | |
303 | { | |
304 | struct enic *enic = vnic_dev_priv(vdev); | |
305 | ||
306 | spin_lock(&enic->wq_lock[q_number]); | |
307 | ||
308 | vnic_wq_service(&enic->wq[q_number], cq_desc, | |
309 | completed_index, enic_wq_free_buf, | |
310 | opaque); | |
311 | ||
312 | if (netif_queue_stopped(enic->netdev) && | |
313 | vnic_wq_desc_avail(&enic->wq[q_number]) >= MAX_SKB_FRAGS + 1) | |
314 | netif_wake_queue(enic->netdev); | |
315 | ||
316 | spin_unlock(&enic->wq_lock[q_number]); | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static void enic_log_q_error(struct enic *enic) | |
322 | { | |
323 | unsigned int i; | |
324 | u32 error_status; | |
325 | ||
326 | for (i = 0; i < enic->wq_count; i++) { | |
327 | error_status = vnic_wq_error_status(&enic->wq[i]); | |
328 | if (error_status) | |
329 | printk(KERN_ERR PFX "%s: WQ[%d] error_status %d\n", | |
330 | enic->netdev->name, i, error_status); | |
331 | } | |
332 | ||
333 | for (i = 0; i < enic->rq_count; i++) { | |
334 | error_status = vnic_rq_error_status(&enic->rq[i]); | |
335 | if (error_status) | |
336 | printk(KERN_ERR PFX "%s: RQ[%d] error_status %d\n", | |
337 | enic->netdev->name, i, error_status); | |
338 | } | |
339 | } | |
340 | ||
341 | static void enic_link_check(struct enic *enic) | |
342 | { | |
343 | int link_status = vnic_dev_link_status(enic->vdev); | |
344 | int carrier_ok = netif_carrier_ok(enic->netdev); | |
345 | ||
346 | if (link_status && !carrier_ok) { | |
347 | printk(KERN_INFO PFX "%s: Link UP\n", enic->netdev->name); | |
348 | netif_carrier_on(enic->netdev); | |
349 | } else if (!link_status && carrier_ok) { | |
350 | printk(KERN_INFO PFX "%s: Link DOWN\n", enic->netdev->name); | |
351 | netif_carrier_off(enic->netdev); | |
352 | } | |
353 | } | |
354 | ||
355 | static void enic_mtu_check(struct enic *enic) | |
356 | { | |
357 | u32 mtu = vnic_dev_mtu(enic->vdev); | |
358 | ||
359 | if (mtu != enic->port_mtu) { | |
360 | if (mtu < enic->netdev->mtu) | |
361 | printk(KERN_WARNING PFX | |
362 | "%s: interface MTU (%d) set higher " | |
363 | "than switch port MTU (%d)\n", | |
364 | enic->netdev->name, enic->netdev->mtu, mtu); | |
365 | enic->port_mtu = mtu; | |
366 | } | |
367 | } | |
368 | ||
369 | static void enic_msglvl_check(struct enic *enic) | |
370 | { | |
371 | u32 msg_enable = vnic_dev_msg_lvl(enic->vdev); | |
372 | ||
373 | if (msg_enable != enic->msg_enable) { | |
374 | printk(KERN_INFO PFX "%s: msg lvl changed from 0x%x to 0x%x\n", | |
375 | enic->netdev->name, enic->msg_enable, msg_enable); | |
376 | enic->msg_enable = msg_enable; | |
377 | } | |
378 | } | |
379 | ||
380 | static void enic_notify_check(struct enic *enic) | |
381 | { | |
382 | enic_msglvl_check(enic); | |
383 | enic_mtu_check(enic); | |
384 | enic_link_check(enic); | |
385 | } | |
386 | ||
387 | #define ENIC_TEST_INTR(pba, i) (pba & (1 << i)) | |
388 | ||
389 | static irqreturn_t enic_isr_legacy(int irq, void *data) | |
390 | { | |
391 | struct net_device *netdev = data; | |
392 | struct enic *enic = netdev_priv(netdev); | |
393 | u32 pba; | |
394 | ||
395 | vnic_intr_mask(&enic->intr[ENIC_INTX_WQ_RQ]); | |
396 | ||
397 | pba = vnic_intr_legacy_pba(enic->legacy_pba); | |
398 | if (!pba) { | |
399 | vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]); | |
400 | return IRQ_NONE; /* not our interrupt */ | |
401 | } | |
402 | ||
ed8af6b2 SF |
403 | if (ENIC_TEST_INTR(pba, ENIC_INTX_NOTIFY)) { |
404 | vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_NOTIFY]); | |
01f2e4ea | 405 | enic_notify_check(enic); |
ed8af6b2 | 406 | } |
01f2e4ea SF |
407 | |
408 | if (ENIC_TEST_INTR(pba, ENIC_INTX_ERR)) { | |
ed8af6b2 | 409 | vnic_intr_return_all_credits(&enic->intr[ENIC_INTX_ERR]); |
01f2e4ea SF |
410 | enic_log_q_error(enic); |
411 | /* schedule recovery from WQ/RQ error */ | |
412 | schedule_work(&enic->reset); | |
413 | return IRQ_HANDLED; | |
414 | } | |
415 | ||
416 | if (ENIC_TEST_INTR(pba, ENIC_INTX_WQ_RQ)) { | |
288379f0 BH |
417 | if (napi_schedule_prep(&enic->napi)) |
418 | __napi_schedule(&enic->napi); | |
01f2e4ea SF |
419 | } else { |
420 | vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]); | |
421 | } | |
422 | ||
423 | return IRQ_HANDLED; | |
424 | } | |
425 | ||
426 | static irqreturn_t enic_isr_msi(int irq, void *data) | |
427 | { | |
428 | struct enic *enic = data; | |
429 | ||
430 | /* With MSI, there is no sharing of interrupts, so this is | |
431 | * our interrupt and there is no need to ack it. The device | |
432 | * is not providing per-vector masking, so the OS will not | |
433 | * write to PCI config space to mask/unmask the interrupt. | |
434 | * We're using mask_on_assertion for MSI, so the device | |
435 | * automatically masks the interrupt when the interrupt is | |
436 | * generated. Later, when exiting polling, the interrupt | |
437 | * will be unmasked (see enic_poll). | |
438 | * | |
439 | * Also, the device uses the same PCIe Traffic Class (TC) | |
440 | * for Memory Write data and MSI, so there are no ordering | |
441 | * issues; the MSI will always arrive at the Root Complex | |
442 | * _after_ corresponding Memory Writes (i.e. descriptor | |
443 | * writes). | |
444 | */ | |
445 | ||
288379f0 | 446 | napi_schedule(&enic->napi); |
01f2e4ea SF |
447 | |
448 | return IRQ_HANDLED; | |
449 | } | |
450 | ||
451 | static irqreturn_t enic_isr_msix_rq(int irq, void *data) | |
452 | { | |
453 | struct enic *enic = data; | |
454 | ||
455 | /* schedule NAPI polling for RQ cleanup */ | |
288379f0 | 456 | napi_schedule(&enic->napi); |
01f2e4ea SF |
457 | |
458 | return IRQ_HANDLED; | |
459 | } | |
460 | ||
461 | static irqreturn_t enic_isr_msix_wq(int irq, void *data) | |
462 | { | |
463 | struct enic *enic = data; | |
464 | unsigned int wq_work_to_do = -1; /* no limit */ | |
465 | unsigned int wq_work_done; | |
466 | ||
467 | wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ], | |
468 | wq_work_to_do, enic_wq_service, NULL); | |
469 | ||
470 | vnic_intr_return_credits(&enic->intr[ENIC_MSIX_WQ], | |
471 | wq_work_done, | |
472 | 1 /* unmask intr */, | |
473 | 1 /* reset intr timer */); | |
474 | ||
475 | return IRQ_HANDLED; | |
476 | } | |
477 | ||
478 | static irqreturn_t enic_isr_msix_err(int irq, void *data) | |
479 | { | |
480 | struct enic *enic = data; | |
481 | ||
ed8af6b2 SF |
482 | vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_ERR]); |
483 | ||
01f2e4ea SF |
484 | enic_log_q_error(enic); |
485 | ||
486 | /* schedule recovery from WQ/RQ error */ | |
487 | schedule_work(&enic->reset); | |
488 | ||
489 | return IRQ_HANDLED; | |
490 | } | |
491 | ||
492 | static irqreturn_t enic_isr_msix_notify(int irq, void *data) | |
493 | { | |
494 | struct enic *enic = data; | |
495 | ||
ed8af6b2 | 496 | vnic_intr_return_all_credits(&enic->intr[ENIC_MSIX_NOTIFY]); |
01f2e4ea | 497 | enic_notify_check(enic); |
01f2e4ea SF |
498 | |
499 | return IRQ_HANDLED; | |
500 | } | |
501 | ||
502 | static inline void enic_queue_wq_skb_cont(struct enic *enic, | |
503 | struct vnic_wq *wq, struct sk_buff *skb, | |
504 | unsigned int len_left) | |
505 | { | |
506 | skb_frag_t *frag; | |
507 | ||
508 | /* Queue additional data fragments */ | |
509 | for (frag = skb_shinfo(skb)->frags; len_left; frag++) { | |
510 | len_left -= frag->size; | |
511 | enic_queue_wq_desc_cont(wq, skb, | |
512 | pci_map_page(enic->pdev, frag->page, | |
513 | frag->page_offset, frag->size, | |
514 | PCI_DMA_TODEVICE), | |
515 | frag->size, | |
516 | (len_left == 0)); /* EOP? */ | |
517 | } | |
518 | } | |
519 | ||
520 | static inline void enic_queue_wq_skb_vlan(struct enic *enic, | |
521 | struct vnic_wq *wq, struct sk_buff *skb, | |
522 | int vlan_tag_insert, unsigned int vlan_tag) | |
523 | { | |
524 | unsigned int head_len = skb_headlen(skb); | |
525 | unsigned int len_left = skb->len - head_len; | |
526 | int eop = (len_left == 0); | |
527 | ||
528 | /* Queue the main skb fragment */ | |
529 | enic_queue_wq_desc(wq, skb, | |
530 | pci_map_single(enic->pdev, skb->data, | |
531 | head_len, PCI_DMA_TODEVICE), | |
532 | head_len, | |
533 | vlan_tag_insert, vlan_tag, | |
534 | eop); | |
535 | ||
536 | if (!eop) | |
537 | enic_queue_wq_skb_cont(enic, wq, skb, len_left); | |
538 | } | |
539 | ||
540 | static inline void enic_queue_wq_skb_csum_l4(struct enic *enic, | |
541 | struct vnic_wq *wq, struct sk_buff *skb, | |
542 | int vlan_tag_insert, unsigned int vlan_tag) | |
543 | { | |
544 | unsigned int head_len = skb_headlen(skb); | |
545 | unsigned int len_left = skb->len - head_len; | |
546 | unsigned int hdr_len = skb_transport_offset(skb); | |
547 | unsigned int csum_offset = hdr_len + skb->csum_offset; | |
548 | int eop = (len_left == 0); | |
549 | ||
550 | /* Queue the main skb fragment */ | |
551 | enic_queue_wq_desc_csum_l4(wq, skb, | |
552 | pci_map_single(enic->pdev, skb->data, | |
553 | head_len, PCI_DMA_TODEVICE), | |
554 | head_len, | |
555 | csum_offset, | |
556 | hdr_len, | |
557 | vlan_tag_insert, vlan_tag, | |
558 | eop); | |
559 | ||
560 | if (!eop) | |
561 | enic_queue_wq_skb_cont(enic, wq, skb, len_left); | |
562 | } | |
563 | ||
564 | static inline void enic_queue_wq_skb_tso(struct enic *enic, | |
565 | struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss, | |
566 | int vlan_tag_insert, unsigned int vlan_tag) | |
567 | { | |
568 | unsigned int head_len = skb_headlen(skb); | |
569 | unsigned int len_left = skb->len - head_len; | |
570 | unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); | |
571 | int eop = (len_left == 0); | |
572 | ||
573 | /* Preload TCP csum field with IP pseudo hdr calculated | |
574 | * with IP length set to zero. HW will later add in length | |
575 | * to each TCP segment resulting from the TSO. | |
576 | */ | |
577 | ||
09640e63 | 578 | if (skb->protocol == cpu_to_be16(ETH_P_IP)) { |
01f2e4ea SF |
579 | ip_hdr(skb)->check = 0; |
580 | tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, | |
581 | ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); | |
09640e63 | 582 | } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) { |
01f2e4ea SF |
583 | tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, |
584 | &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0); | |
585 | } | |
586 | ||
587 | /* Queue the main skb fragment */ | |
588 | enic_queue_wq_desc_tso(wq, skb, | |
589 | pci_map_single(enic->pdev, skb->data, | |
590 | head_len, PCI_DMA_TODEVICE), | |
591 | head_len, | |
592 | mss, hdr_len, | |
593 | vlan_tag_insert, vlan_tag, | |
594 | eop); | |
595 | ||
596 | if (!eop) | |
597 | enic_queue_wq_skb_cont(enic, wq, skb, len_left); | |
598 | } | |
599 | ||
600 | static inline void enic_queue_wq_skb(struct enic *enic, | |
601 | struct vnic_wq *wq, struct sk_buff *skb) | |
602 | { | |
603 | unsigned int mss = skb_shinfo(skb)->gso_size; | |
604 | unsigned int vlan_tag = 0; | |
605 | int vlan_tag_insert = 0; | |
606 | ||
607 | if (enic->vlan_group && vlan_tx_tag_present(skb)) { | |
608 | /* VLAN tag from trunking driver */ | |
609 | vlan_tag_insert = 1; | |
610 | vlan_tag = vlan_tx_tag_get(skb); | |
611 | } | |
612 | ||
613 | if (mss) | |
614 | enic_queue_wq_skb_tso(enic, wq, skb, mss, | |
615 | vlan_tag_insert, vlan_tag); | |
616 | else if (skb->ip_summed == CHECKSUM_PARTIAL) | |
617 | enic_queue_wq_skb_csum_l4(enic, wq, skb, | |
618 | vlan_tag_insert, vlan_tag); | |
619 | else | |
620 | enic_queue_wq_skb_vlan(enic, wq, skb, | |
621 | vlan_tag_insert, vlan_tag); | |
622 | } | |
623 | ||
ed8af6b2 | 624 | /* netif_tx_lock held, process context with BHs disabled, or BH */ |
01f2e4ea SF |
625 | static int enic_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev) |
626 | { | |
627 | struct enic *enic = netdev_priv(netdev); | |
628 | struct vnic_wq *wq = &enic->wq[0]; | |
629 | unsigned long flags; | |
630 | ||
631 | if (skb->len <= 0) { | |
632 | dev_kfree_skb(skb); | |
633 | return NETDEV_TX_OK; | |
634 | } | |
635 | ||
636 | /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs, | |
637 | * which is very likely. In the off chance it's going to take | |
638 | * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb. | |
639 | */ | |
640 | ||
641 | if (skb_shinfo(skb)->gso_size == 0 && | |
642 | skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC && | |
643 | skb_linearize(skb)) { | |
644 | dev_kfree_skb(skb); | |
645 | return NETDEV_TX_OK; | |
646 | } | |
647 | ||
648 | spin_lock_irqsave(&enic->wq_lock[0], flags); | |
649 | ||
650 | if (vnic_wq_desc_avail(wq) < skb_shinfo(skb)->nr_frags + 1) { | |
651 | netif_stop_queue(netdev); | |
652 | /* This is a hard error, log it */ | |
653 | printk(KERN_ERR PFX "%s: BUG! Tx ring full when " | |
654 | "queue awake!\n", netdev->name); | |
655 | spin_unlock_irqrestore(&enic->wq_lock[0], flags); | |
656 | return NETDEV_TX_BUSY; | |
657 | } | |
658 | ||
659 | enic_queue_wq_skb(enic, wq, skb); | |
660 | ||
661 | if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + 1) | |
662 | netif_stop_queue(netdev); | |
663 | ||
664 | netdev->trans_start = jiffies; | |
665 | ||
666 | spin_unlock_irqrestore(&enic->wq_lock[0], flags); | |
667 | ||
668 | return NETDEV_TX_OK; | |
669 | } | |
670 | ||
671 | /* dev_base_lock rwlock held, nominally process context */ | |
672 | static struct net_device_stats *enic_get_stats(struct net_device *netdev) | |
673 | { | |
674 | struct enic *enic = netdev_priv(netdev); | |
25f0a061 | 675 | struct net_device_stats *net_stats = &netdev->stats; |
01f2e4ea SF |
676 | struct vnic_stats *stats; |
677 | ||
678 | spin_lock(&enic->devcmd_lock); | |
679 | vnic_dev_stats_dump(enic->vdev, &stats); | |
680 | spin_unlock(&enic->devcmd_lock); | |
681 | ||
25f0a061 SF |
682 | net_stats->tx_packets = stats->tx.tx_frames_ok; |
683 | net_stats->tx_bytes = stats->tx.tx_bytes_ok; | |
684 | net_stats->tx_errors = stats->tx.tx_errors; | |
685 | net_stats->tx_dropped = stats->tx.tx_drops; | |
01f2e4ea | 686 | |
25f0a061 SF |
687 | net_stats->rx_packets = stats->rx.rx_frames_ok; |
688 | net_stats->rx_bytes = stats->rx.rx_bytes_ok; | |
689 | net_stats->rx_errors = stats->rx.rx_errors; | |
690 | net_stats->multicast = stats->rx.rx_multicast_frames_ok; | |
bd9fb1a4 | 691 | net_stats->rx_crc_errors = enic->rq_bad_fcs; |
25f0a061 | 692 | net_stats->rx_dropped = stats->rx.rx_no_bufs; |
01f2e4ea | 693 | |
25f0a061 | 694 | return net_stats; |
01f2e4ea SF |
695 | } |
696 | ||
697 | static void enic_reset_mcaddrs(struct enic *enic) | |
698 | { | |
699 | enic->mc_count = 0; | |
700 | } | |
701 | ||
702 | static int enic_set_mac_addr(struct net_device *netdev, char *addr) | |
703 | { | |
704 | if (!is_valid_ether_addr(addr)) | |
705 | return -EADDRNOTAVAIL; | |
706 | ||
707 | memcpy(netdev->dev_addr, addr, netdev->addr_len); | |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
712 | /* netif_tx_lock held, BHs disabled */ | |
713 | static void enic_set_multicast_list(struct net_device *netdev) | |
714 | { | |
715 | struct enic *enic = netdev_priv(netdev); | |
716 | struct dev_mc_list *list = netdev->mc_list; | |
717 | int directed = 1; | |
718 | int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0; | |
719 | int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0; | |
720 | int promisc = (netdev->flags & IFF_PROMISC) ? 1 : 0; | |
721 | int allmulti = (netdev->flags & IFF_ALLMULTI) || | |
722 | (netdev->mc_count > ENIC_MULTICAST_PERFECT_FILTERS); | |
723 | u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN]; | |
724 | unsigned int mc_count = netdev->mc_count; | |
725 | unsigned int i, j; | |
726 | ||
727 | if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) | |
728 | mc_count = ENIC_MULTICAST_PERFECT_FILTERS; | |
729 | ||
730 | spin_lock(&enic->devcmd_lock); | |
731 | ||
732 | vnic_dev_packet_filter(enic->vdev, directed, | |
733 | multicast, broadcast, promisc, allmulti); | |
734 | ||
735 | /* Is there an easier way? Trying to minimize to | |
736 | * calls to add/del multicast addrs. We keep the | |
737 | * addrs from the last call in enic->mc_addr and | |
738 | * look for changes to add/del. | |
739 | */ | |
740 | ||
741 | for (i = 0; list && i < mc_count; i++) { | |
742 | memcpy(mc_addr[i], list->dmi_addr, ETH_ALEN); | |
743 | list = list->next; | |
744 | } | |
745 | ||
746 | for (i = 0; i < enic->mc_count; i++) { | |
747 | for (j = 0; j < mc_count; j++) | |
748 | if (compare_ether_addr(enic->mc_addr[i], | |
749 | mc_addr[j]) == 0) | |
750 | break; | |
751 | if (j == mc_count) | |
752 | enic_del_multicast_addr(enic, enic->mc_addr[i]); | |
753 | } | |
754 | ||
755 | for (i = 0; i < mc_count; i++) { | |
756 | for (j = 0; j < enic->mc_count; j++) | |
757 | if (compare_ether_addr(mc_addr[i], | |
758 | enic->mc_addr[j]) == 0) | |
759 | break; | |
760 | if (j == enic->mc_count) | |
761 | enic_add_multicast_addr(enic, mc_addr[i]); | |
762 | } | |
763 | ||
764 | /* Save the list to compare against next time | |
765 | */ | |
766 | ||
767 | for (i = 0; i < mc_count; i++) | |
768 | memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN); | |
769 | ||
770 | enic->mc_count = mc_count; | |
771 | ||
772 | spin_unlock(&enic->devcmd_lock); | |
773 | } | |
774 | ||
775 | /* rtnl lock is held */ | |
776 | static void enic_vlan_rx_register(struct net_device *netdev, | |
777 | struct vlan_group *vlan_group) | |
778 | { | |
779 | struct enic *enic = netdev_priv(netdev); | |
780 | enic->vlan_group = vlan_group; | |
781 | } | |
782 | ||
783 | /* rtnl lock is held */ | |
784 | static void enic_vlan_rx_add_vid(struct net_device *netdev, u16 vid) | |
785 | { | |
786 | struct enic *enic = netdev_priv(netdev); | |
787 | ||
788 | spin_lock(&enic->devcmd_lock); | |
789 | enic_add_vlan(enic, vid); | |
790 | spin_unlock(&enic->devcmd_lock); | |
791 | } | |
792 | ||
793 | /* rtnl lock is held */ | |
794 | static void enic_vlan_rx_kill_vid(struct net_device *netdev, u16 vid) | |
795 | { | |
796 | struct enic *enic = netdev_priv(netdev); | |
797 | ||
798 | spin_lock(&enic->devcmd_lock); | |
799 | enic_del_vlan(enic, vid); | |
800 | spin_unlock(&enic->devcmd_lock); | |
801 | } | |
802 | ||
803 | /* netif_tx_lock held, BHs disabled */ | |
804 | static void enic_tx_timeout(struct net_device *netdev) | |
805 | { | |
806 | struct enic *enic = netdev_priv(netdev); | |
807 | schedule_work(&enic->reset); | |
808 | } | |
809 | ||
810 | static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf) | |
811 | { | |
812 | struct enic *enic = vnic_dev_priv(rq->vdev); | |
813 | ||
814 | if (!buf->os_buf) | |
815 | return; | |
816 | ||
817 | pci_unmap_single(enic->pdev, buf->dma_addr, | |
818 | buf->len, PCI_DMA_FROMDEVICE); | |
819 | dev_kfree_skb_any(buf->os_buf); | |
820 | } | |
821 | ||
822 | static inline struct sk_buff *enic_rq_alloc_skb(unsigned int size) | |
823 | { | |
824 | struct sk_buff *skb; | |
825 | ||
826 | skb = dev_alloc_skb(size + NET_IP_ALIGN); | |
827 | ||
828 | if (skb) | |
829 | skb_reserve(skb, NET_IP_ALIGN); | |
830 | ||
831 | return skb; | |
832 | } | |
833 | ||
834 | static int enic_rq_alloc_buf(struct vnic_rq *rq) | |
835 | { | |
836 | struct enic *enic = vnic_dev_priv(rq->vdev); | |
837 | struct sk_buff *skb; | |
838 | unsigned int len = enic->netdev->mtu + ETH_HLEN; | |
839 | unsigned int os_buf_index = 0; | |
840 | dma_addr_t dma_addr; | |
841 | ||
842 | skb = enic_rq_alloc_skb(len); | |
843 | if (!skb) | |
844 | return -ENOMEM; | |
845 | ||
846 | dma_addr = pci_map_single(enic->pdev, skb->data, | |
847 | len, PCI_DMA_FROMDEVICE); | |
848 | ||
849 | enic_queue_rq_desc(rq, skb, os_buf_index, | |
850 | dma_addr, len); | |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | static int enic_get_skb_header(struct sk_buff *skb, void **iphdr, | |
856 | void **tcph, u64 *hdr_flags, void *priv) | |
857 | { | |
858 | struct cq_enet_rq_desc *cq_desc = priv; | |
859 | unsigned int ip_len; | |
860 | struct iphdr *iph; | |
861 | ||
862 | u8 type, color, eop, sop, ingress_port, vlan_stripped; | |
863 | u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof; | |
864 | u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok; | |
865 | u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc; | |
866 | u8 packet_error; | |
867 | u16 q_number, completed_index, bytes_written, vlan, checksum; | |
868 | u32 rss_hash; | |
869 | ||
870 | cq_enet_rq_desc_dec(cq_desc, | |
871 | &type, &color, &q_number, &completed_index, | |
872 | &ingress_port, &fcoe, &eop, &sop, &rss_type, | |
873 | &csum_not_calc, &rss_hash, &bytes_written, | |
874 | &packet_error, &vlan_stripped, &vlan, &checksum, | |
875 | &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error, | |
876 | &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp, | |
877 | &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment, | |
878 | &fcs_ok); | |
879 | ||
880 | if (!(ipv4 && tcp && !ipv4_fragment)) | |
881 | return -1; | |
882 | ||
883 | skb_reset_network_header(skb); | |
884 | iph = ip_hdr(skb); | |
885 | ||
886 | ip_len = ip_hdrlen(skb); | |
887 | skb_set_transport_header(skb, ip_len); | |
888 | ||
889 | /* check if ip header and tcp header are complete */ | |
890 | if (ntohs(iph->tot_len) < ip_len + tcp_hdrlen(skb)) | |
891 | return -1; | |
892 | ||
893 | *hdr_flags = LRO_IPV4 | LRO_TCP; | |
894 | *tcph = tcp_hdr(skb); | |
895 | *iphdr = iph; | |
896 | ||
897 | return 0; | |
898 | } | |
899 | ||
900 | static void enic_rq_indicate_buf(struct vnic_rq *rq, | |
901 | struct cq_desc *cq_desc, struct vnic_rq_buf *buf, | |
902 | int skipped, void *opaque) | |
903 | { | |
904 | struct enic *enic = vnic_dev_priv(rq->vdev); | |
86ca9db7 | 905 | struct net_device *netdev = enic->netdev; |
01f2e4ea SF |
906 | struct sk_buff *skb; |
907 | ||
908 | u8 type, color, eop, sop, ingress_port, vlan_stripped; | |
909 | u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof; | |
910 | u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok; | |
911 | u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc; | |
912 | u8 packet_error; | |
913 | u16 q_number, completed_index, bytes_written, vlan, checksum; | |
914 | u32 rss_hash; | |
915 | ||
916 | if (skipped) | |
917 | return; | |
918 | ||
919 | skb = buf->os_buf; | |
920 | prefetch(skb->data - NET_IP_ALIGN); | |
921 | pci_unmap_single(enic->pdev, buf->dma_addr, | |
922 | buf->len, PCI_DMA_FROMDEVICE); | |
923 | ||
924 | cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc, | |
925 | &type, &color, &q_number, &completed_index, | |
926 | &ingress_port, &fcoe, &eop, &sop, &rss_type, | |
927 | &csum_not_calc, &rss_hash, &bytes_written, | |
928 | &packet_error, &vlan_stripped, &vlan, &checksum, | |
929 | &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error, | |
930 | &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp, | |
931 | &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment, | |
932 | &fcs_ok); | |
933 | ||
934 | if (packet_error) { | |
935 | ||
bd9fb1a4 SF |
936 | if (bytes_written > 0 && !fcs_ok) |
937 | enic->rq_bad_fcs++; | |
01f2e4ea SF |
938 | |
939 | dev_kfree_skb_any(skb); | |
940 | ||
941 | return; | |
942 | } | |
943 | ||
944 | if (eop && bytes_written > 0) { | |
945 | ||
946 | /* Good receive | |
947 | */ | |
948 | ||
949 | skb_put(skb, bytes_written); | |
86ca9db7 | 950 | skb->protocol = eth_type_trans(skb, netdev); |
01f2e4ea SF |
951 | |
952 | if (enic->csum_rx_enabled && !csum_not_calc) { | |
953 | skb->csum = htons(checksum); | |
954 | skb->ip_summed = CHECKSUM_COMPLETE; | |
955 | } | |
956 | ||
86ca9db7 | 957 | skb->dev = netdev; |
01f2e4ea SF |
958 | |
959 | if (enic->vlan_group && vlan_stripped) { | |
960 | ||
86ca9db7 | 961 | if ((netdev->features & NETIF_F_LRO) && ipv4) |
01f2e4ea SF |
962 | lro_vlan_hwaccel_receive_skb(&enic->lro_mgr, |
963 | skb, enic->vlan_group, | |
964 | vlan, cq_desc); | |
965 | else | |
966 | vlan_hwaccel_receive_skb(skb, | |
967 | enic->vlan_group, vlan); | |
968 | ||
969 | } else { | |
970 | ||
86ca9db7 | 971 | if ((netdev->features & NETIF_F_LRO) && ipv4) |
01f2e4ea SF |
972 | lro_receive_skb(&enic->lro_mgr, skb, cq_desc); |
973 | else | |
974 | netif_receive_skb(skb); | |
975 | ||
976 | } | |
977 | ||
978 | } else { | |
979 | ||
980 | /* Buffer overflow | |
981 | */ | |
982 | ||
983 | dev_kfree_skb_any(skb); | |
984 | } | |
985 | } | |
986 | ||
987 | static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc, | |
988 | u8 type, u16 q_number, u16 completed_index, void *opaque) | |
989 | { | |
990 | struct enic *enic = vnic_dev_priv(vdev); | |
991 | ||
992 | vnic_rq_service(&enic->rq[q_number], cq_desc, | |
993 | completed_index, VNIC_RQ_RETURN_DESC, | |
994 | enic_rq_indicate_buf, opaque); | |
995 | ||
996 | return 0; | |
997 | } | |
998 | ||
999 | static void enic_rq_drop_buf(struct vnic_rq *rq, | |
1000 | struct cq_desc *cq_desc, struct vnic_rq_buf *buf, | |
1001 | int skipped, void *opaque) | |
1002 | { | |
1003 | struct enic *enic = vnic_dev_priv(rq->vdev); | |
1004 | struct sk_buff *skb = buf->os_buf; | |
1005 | ||
1006 | if (skipped) | |
1007 | return; | |
1008 | ||
1009 | pci_unmap_single(enic->pdev, buf->dma_addr, | |
1010 | buf->len, PCI_DMA_FROMDEVICE); | |
1011 | ||
1012 | dev_kfree_skb_any(skb); | |
1013 | } | |
1014 | ||
1015 | static int enic_rq_service_drop(struct vnic_dev *vdev, struct cq_desc *cq_desc, | |
1016 | u8 type, u16 q_number, u16 completed_index, void *opaque) | |
1017 | { | |
1018 | struct enic *enic = vnic_dev_priv(vdev); | |
1019 | ||
1020 | vnic_rq_service(&enic->rq[q_number], cq_desc, | |
1021 | completed_index, VNIC_RQ_RETURN_DESC, | |
1022 | enic_rq_drop_buf, opaque); | |
1023 | ||
1024 | return 0; | |
1025 | } | |
1026 | ||
1027 | static int enic_poll(struct napi_struct *napi, int budget) | |
1028 | { | |
1029 | struct enic *enic = container_of(napi, struct enic, napi); | |
1030 | struct net_device *netdev = enic->netdev; | |
1031 | unsigned int rq_work_to_do = budget; | |
1032 | unsigned int wq_work_to_do = -1; /* no limit */ | |
1033 | unsigned int work_done, rq_work_done, wq_work_done; | |
1034 | ||
1035 | /* Service RQ (first) and WQ | |
1036 | */ | |
1037 | ||
1038 | rq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ], | |
1039 | rq_work_to_do, enic_rq_service, NULL); | |
1040 | ||
1041 | wq_work_done = vnic_cq_service(&enic->cq[ENIC_CQ_WQ], | |
1042 | wq_work_to_do, enic_wq_service, NULL); | |
1043 | ||
1044 | /* Accumulate intr event credits for this polling | |
1045 | * cycle. An intr event is the completion of a | |
1046 | * a WQ or RQ packet. | |
1047 | */ | |
1048 | ||
1049 | work_done = rq_work_done + wq_work_done; | |
1050 | ||
1051 | if (work_done > 0) | |
1052 | vnic_intr_return_credits(&enic->intr[ENIC_INTX_WQ_RQ], | |
1053 | work_done, | |
1054 | 0 /* don't unmask intr */, | |
1055 | 0 /* don't reset intr timer */); | |
1056 | ||
1057 | if (rq_work_done > 0) { | |
1058 | ||
1059 | /* Replenish RQ | |
1060 | */ | |
1061 | ||
1062 | vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf); | |
1063 | ||
1064 | } else { | |
1065 | ||
1066 | /* If no work done, flush all LROs and exit polling | |
1067 | */ | |
1068 | ||
86ca9db7 | 1069 | if (netdev->features & NETIF_F_LRO) |
01f2e4ea SF |
1070 | lro_flush_all(&enic->lro_mgr); |
1071 | ||
288379f0 | 1072 | napi_complete(napi); |
ed8af6b2 | 1073 | vnic_intr_unmask(&enic->intr[ENIC_INTX_WQ_RQ]); |
01f2e4ea SF |
1074 | } |
1075 | ||
1076 | return rq_work_done; | |
1077 | } | |
1078 | ||
1079 | static int enic_poll_msix(struct napi_struct *napi, int budget) | |
1080 | { | |
1081 | struct enic *enic = container_of(napi, struct enic, napi); | |
1082 | struct net_device *netdev = enic->netdev; | |
1083 | unsigned int work_to_do = budget; | |
1084 | unsigned int work_done; | |
1085 | ||
1086 | /* Service RQ | |
1087 | */ | |
1088 | ||
1089 | work_done = vnic_cq_service(&enic->cq[ENIC_CQ_RQ], | |
1090 | work_to_do, enic_rq_service, NULL); | |
1091 | ||
1092 | if (work_done > 0) { | |
1093 | ||
1094 | /* Replenish RQ | |
1095 | */ | |
1096 | ||
1097 | vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf); | |
1098 | ||
ed8af6b2 | 1099 | /* Return intr event credits for this polling |
01f2e4ea | 1100 | * cycle. An intr event is the completion of a |
ed8af6b2 | 1101 | * RQ packet. |
01f2e4ea SF |
1102 | */ |
1103 | ||
1104 | vnic_intr_return_credits(&enic->intr[ENIC_MSIX_RQ], | |
1105 | work_done, | |
1106 | 0 /* don't unmask intr */, | |
1107 | 0 /* don't reset intr timer */); | |
1108 | } else { | |
1109 | ||
1110 | /* If no work done, flush all LROs and exit polling | |
1111 | */ | |
1112 | ||
86ca9db7 | 1113 | if (netdev->features & NETIF_F_LRO) |
01f2e4ea SF |
1114 | lro_flush_all(&enic->lro_mgr); |
1115 | ||
288379f0 | 1116 | napi_complete(napi); |
01f2e4ea SF |
1117 | vnic_intr_unmask(&enic->intr[ENIC_MSIX_RQ]); |
1118 | } | |
1119 | ||
1120 | return work_done; | |
1121 | } | |
1122 | ||
1123 | static void enic_notify_timer(unsigned long data) | |
1124 | { | |
1125 | struct enic *enic = (struct enic *)data; | |
1126 | ||
1127 | enic_notify_check(enic); | |
1128 | ||
25f0a061 SF |
1129 | mod_timer(&enic->notify_timer, |
1130 | round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD)); | |
01f2e4ea SF |
1131 | } |
1132 | ||
1133 | static void enic_free_intr(struct enic *enic) | |
1134 | { | |
1135 | struct net_device *netdev = enic->netdev; | |
1136 | unsigned int i; | |
1137 | ||
1138 | switch (vnic_dev_get_intr_mode(enic->vdev)) { | |
1139 | case VNIC_DEV_INTR_MODE_INTX: | |
01f2e4ea SF |
1140 | free_irq(enic->pdev->irq, netdev); |
1141 | break; | |
8f4d248c SF |
1142 | case VNIC_DEV_INTR_MODE_MSI: |
1143 | free_irq(enic->pdev->irq, enic); | |
1144 | break; | |
01f2e4ea SF |
1145 | case VNIC_DEV_INTR_MODE_MSIX: |
1146 | for (i = 0; i < ARRAY_SIZE(enic->msix); i++) | |
1147 | if (enic->msix[i].requested) | |
1148 | free_irq(enic->msix_entry[i].vector, | |
1149 | enic->msix[i].devid); | |
1150 | break; | |
1151 | default: | |
1152 | break; | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | static int enic_request_intr(struct enic *enic) | |
1157 | { | |
1158 | struct net_device *netdev = enic->netdev; | |
1159 | unsigned int i; | |
1160 | int err = 0; | |
1161 | ||
1162 | switch (vnic_dev_get_intr_mode(enic->vdev)) { | |
1163 | ||
1164 | case VNIC_DEV_INTR_MODE_INTX: | |
1165 | ||
1166 | err = request_irq(enic->pdev->irq, enic_isr_legacy, | |
1167 | IRQF_SHARED, netdev->name, netdev); | |
1168 | break; | |
1169 | ||
1170 | case VNIC_DEV_INTR_MODE_MSI: | |
1171 | ||
1172 | err = request_irq(enic->pdev->irq, enic_isr_msi, | |
1173 | 0, netdev->name, enic); | |
1174 | break; | |
1175 | ||
1176 | case VNIC_DEV_INTR_MODE_MSIX: | |
1177 | ||
1178 | sprintf(enic->msix[ENIC_MSIX_RQ].devname, | |
8f4d248c | 1179 | "%.11s-rx-0", netdev->name); |
01f2e4ea SF |
1180 | enic->msix[ENIC_MSIX_RQ].isr = enic_isr_msix_rq; |
1181 | enic->msix[ENIC_MSIX_RQ].devid = enic; | |
1182 | ||
1183 | sprintf(enic->msix[ENIC_MSIX_WQ].devname, | |
8f4d248c | 1184 | "%.11s-tx-0", netdev->name); |
01f2e4ea SF |
1185 | enic->msix[ENIC_MSIX_WQ].isr = enic_isr_msix_wq; |
1186 | enic->msix[ENIC_MSIX_WQ].devid = enic; | |
1187 | ||
1188 | sprintf(enic->msix[ENIC_MSIX_ERR].devname, | |
1189 | "%.11s-err", netdev->name); | |
1190 | enic->msix[ENIC_MSIX_ERR].isr = enic_isr_msix_err; | |
1191 | enic->msix[ENIC_MSIX_ERR].devid = enic; | |
1192 | ||
1193 | sprintf(enic->msix[ENIC_MSIX_NOTIFY].devname, | |
1194 | "%.11s-notify", netdev->name); | |
1195 | enic->msix[ENIC_MSIX_NOTIFY].isr = enic_isr_msix_notify; | |
1196 | enic->msix[ENIC_MSIX_NOTIFY].devid = enic; | |
1197 | ||
1198 | for (i = 0; i < ARRAY_SIZE(enic->msix); i++) { | |
1199 | err = request_irq(enic->msix_entry[i].vector, | |
1200 | enic->msix[i].isr, 0, | |
1201 | enic->msix[i].devname, | |
1202 | enic->msix[i].devid); | |
1203 | if (err) { | |
1204 | enic_free_intr(enic); | |
1205 | break; | |
1206 | } | |
1207 | enic->msix[i].requested = 1; | |
1208 | } | |
1209 | ||
1210 | break; | |
1211 | ||
1212 | default: | |
1213 | break; | |
1214 | } | |
1215 | ||
1216 | return err; | |
1217 | } | |
1218 | ||
1219 | static int enic_notify_set(struct enic *enic) | |
1220 | { | |
1221 | int err; | |
1222 | ||
1223 | switch (vnic_dev_get_intr_mode(enic->vdev)) { | |
1224 | case VNIC_DEV_INTR_MODE_INTX: | |
1225 | err = vnic_dev_notify_set(enic->vdev, ENIC_INTX_NOTIFY); | |
1226 | break; | |
1227 | case VNIC_DEV_INTR_MODE_MSIX: | |
1228 | err = vnic_dev_notify_set(enic->vdev, ENIC_MSIX_NOTIFY); | |
1229 | break; | |
1230 | default: | |
1231 | err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */); | |
1232 | break; | |
1233 | } | |
1234 | ||
1235 | return err; | |
1236 | } | |
1237 | ||
1238 | static void enic_notify_timer_start(struct enic *enic) | |
1239 | { | |
1240 | switch (vnic_dev_get_intr_mode(enic->vdev)) { | |
1241 | case VNIC_DEV_INTR_MODE_MSI: | |
1242 | mod_timer(&enic->notify_timer, jiffies); | |
1243 | break; | |
1244 | default: | |
1245 | /* Using intr for notification for INTx/MSI-X */ | |
1246 | break; | |
1247 | }; | |
1248 | } | |
1249 | ||
1250 | /* rtnl lock is held, process context */ | |
1251 | static int enic_open(struct net_device *netdev) | |
1252 | { | |
1253 | struct enic *enic = netdev_priv(netdev); | |
1254 | unsigned int i; | |
1255 | int err; | |
1256 | ||
4b75a442 SF |
1257 | err = enic_request_intr(enic); |
1258 | if (err) { | |
1259 | printk(KERN_ERR PFX "%s: Unable to request irq.\n", | |
1260 | netdev->name); | |
1261 | return err; | |
1262 | } | |
1263 | ||
1264 | err = enic_notify_set(enic); | |
1265 | if (err) { | |
1266 | printk(KERN_ERR PFX | |
1267 | "%s: Failed to alloc notify buffer, aborting.\n", | |
1268 | netdev->name); | |
1269 | goto err_out_free_intr; | |
1270 | } | |
1271 | ||
01f2e4ea SF |
1272 | for (i = 0; i < enic->rq_count; i++) { |
1273 | err = vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf); | |
1274 | if (err) { | |
1275 | printk(KERN_ERR PFX | |
1276 | "%s: Unable to alloc receive buffers.\n", | |
1277 | netdev->name); | |
4b75a442 | 1278 | goto err_out_notify_unset; |
01f2e4ea SF |
1279 | } |
1280 | } | |
1281 | ||
1282 | for (i = 0; i < enic->wq_count; i++) | |
1283 | vnic_wq_enable(&enic->wq[i]); | |
1284 | for (i = 0; i < enic->rq_count; i++) | |
1285 | vnic_rq_enable(&enic->rq[i]); | |
1286 | ||
1287 | enic_add_station_addr(enic); | |
1288 | enic_set_multicast_list(netdev); | |
1289 | ||
1290 | netif_wake_queue(netdev); | |
1291 | napi_enable(&enic->napi); | |
1292 | vnic_dev_enable(enic->vdev); | |
1293 | ||
1294 | for (i = 0; i < enic->intr_count; i++) | |
1295 | vnic_intr_unmask(&enic->intr[i]); | |
1296 | ||
1297 | enic_notify_timer_start(enic); | |
1298 | ||
1299 | return 0; | |
4b75a442 SF |
1300 | |
1301 | err_out_notify_unset: | |
1302 | vnic_dev_notify_unset(enic->vdev); | |
1303 | err_out_free_intr: | |
1304 | enic_free_intr(enic); | |
1305 | ||
1306 | return err; | |
01f2e4ea SF |
1307 | } |
1308 | ||
1309 | /* rtnl lock is held, process context */ | |
1310 | static int enic_stop(struct net_device *netdev) | |
1311 | { | |
1312 | struct enic *enic = netdev_priv(netdev); | |
1313 | unsigned int i; | |
1314 | int err; | |
1315 | ||
1316 | del_timer_sync(&enic->notify_timer); | |
1317 | ||
1318 | vnic_dev_disable(enic->vdev); | |
1319 | napi_disable(&enic->napi); | |
1320 | netif_stop_queue(netdev); | |
1321 | ||
1322 | for (i = 0; i < enic->intr_count; i++) | |
1323 | vnic_intr_mask(&enic->intr[i]); | |
1324 | ||
1325 | for (i = 0; i < enic->wq_count; i++) { | |
1326 | err = vnic_wq_disable(&enic->wq[i]); | |
1327 | if (err) | |
1328 | return err; | |
1329 | } | |
1330 | for (i = 0; i < enic->rq_count; i++) { | |
1331 | err = vnic_rq_disable(&enic->rq[i]); | |
1332 | if (err) | |
1333 | return err; | |
1334 | } | |
1335 | ||
4b75a442 SF |
1336 | vnic_dev_notify_unset(enic->vdev); |
1337 | enic_free_intr(enic); | |
1338 | ||
01f2e4ea SF |
1339 | (void)vnic_cq_service(&enic->cq[ENIC_CQ_RQ], |
1340 | -1, enic_rq_service_drop, NULL); | |
1341 | (void)vnic_cq_service(&enic->cq[ENIC_CQ_WQ], | |
1342 | -1, enic_wq_service, NULL); | |
1343 | ||
1344 | for (i = 0; i < enic->wq_count; i++) | |
1345 | vnic_wq_clean(&enic->wq[i], enic_free_wq_buf); | |
1346 | for (i = 0; i < enic->rq_count; i++) | |
1347 | vnic_rq_clean(&enic->rq[i], enic_free_rq_buf); | |
1348 | for (i = 0; i < enic->cq_count; i++) | |
1349 | vnic_cq_clean(&enic->cq[i]); | |
1350 | for (i = 0; i < enic->intr_count; i++) | |
1351 | vnic_intr_clean(&enic->intr[i]); | |
1352 | ||
1353 | return 0; | |
1354 | } | |
1355 | ||
1356 | static int enic_change_mtu(struct net_device *netdev, int new_mtu) | |
1357 | { | |
1358 | struct enic *enic = netdev_priv(netdev); | |
1359 | int running = netif_running(netdev); | |
1360 | ||
25f0a061 SF |
1361 | if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU) |
1362 | return -EINVAL; | |
1363 | ||
01f2e4ea SF |
1364 | if (running) |
1365 | enic_stop(netdev); | |
1366 | ||
01f2e4ea SF |
1367 | netdev->mtu = new_mtu; |
1368 | ||
1369 | if (netdev->mtu > enic->port_mtu) | |
1370 | printk(KERN_WARNING PFX | |
1371 | "%s: interface MTU (%d) set higher " | |
1372 | "than port MTU (%d)\n", | |
1373 | netdev->name, netdev->mtu, enic->port_mtu); | |
1374 | ||
1375 | if (running) | |
1376 | enic_open(netdev); | |
1377 | ||
1378 | return 0; | |
1379 | } | |
1380 | ||
1381 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1382 | static void enic_poll_controller(struct net_device *netdev) | |
1383 | { | |
1384 | struct enic *enic = netdev_priv(netdev); | |
1385 | struct vnic_dev *vdev = enic->vdev; | |
1386 | ||
1387 | switch (vnic_dev_get_intr_mode(vdev)) { | |
1388 | case VNIC_DEV_INTR_MODE_MSIX: | |
1389 | enic_isr_msix_rq(enic->pdev->irq, enic); | |
1390 | enic_isr_msix_wq(enic->pdev->irq, enic); | |
1391 | break; | |
1392 | case VNIC_DEV_INTR_MODE_MSI: | |
1393 | enic_isr_msi(enic->pdev->irq, enic); | |
1394 | break; | |
1395 | case VNIC_DEV_INTR_MODE_INTX: | |
1396 | enic_isr_legacy(enic->pdev->irq, netdev); | |
1397 | break; | |
1398 | default: | |
1399 | break; | |
1400 | } | |
1401 | } | |
1402 | #endif | |
1403 | ||
1404 | static int enic_dev_wait(struct vnic_dev *vdev, | |
1405 | int (*start)(struct vnic_dev *, int), | |
1406 | int (*finished)(struct vnic_dev *, int *), | |
1407 | int arg) | |
1408 | { | |
1409 | unsigned long time; | |
1410 | int done; | |
1411 | int err; | |
1412 | ||
1413 | BUG_ON(in_interrupt()); | |
1414 | ||
1415 | err = start(vdev, arg); | |
1416 | if (err) | |
1417 | return err; | |
1418 | ||
1419 | /* Wait for func to complete...2 seconds max | |
1420 | */ | |
1421 | ||
1422 | time = jiffies + (HZ * 2); | |
1423 | do { | |
1424 | ||
1425 | err = finished(vdev, &done); | |
1426 | if (err) | |
1427 | return err; | |
1428 | ||
1429 | if (done) | |
1430 | return 0; | |
1431 | ||
1432 | schedule_timeout_uninterruptible(HZ / 10); | |
1433 | ||
1434 | } while (time_after(time, jiffies)); | |
1435 | ||
1436 | return -ETIMEDOUT; | |
1437 | } | |
1438 | ||
1439 | static int enic_dev_open(struct enic *enic) | |
1440 | { | |
1441 | int err; | |
1442 | ||
1443 | err = enic_dev_wait(enic->vdev, vnic_dev_open, | |
1444 | vnic_dev_open_done, 0); | |
1445 | if (err) | |
1446 | printk(KERN_ERR PFX | |
1447 | "vNIC device open failed, err %d.\n", err); | |
1448 | ||
1449 | return err; | |
1450 | } | |
1451 | ||
1452 | static int enic_dev_soft_reset(struct enic *enic) | |
1453 | { | |
1454 | int err; | |
1455 | ||
1456 | err = enic_dev_wait(enic->vdev, vnic_dev_soft_reset, | |
1457 | vnic_dev_soft_reset_done, 0); | |
1458 | if (err) | |
1459 | printk(KERN_ERR PFX | |
1460 | "vNIC soft reset failed, err %d.\n", err); | |
1461 | ||
1462 | return err; | |
1463 | } | |
1464 | ||
1465 | static void enic_reset(struct work_struct *work) | |
1466 | { | |
1467 | struct enic *enic = container_of(work, struct enic, reset); | |
1468 | ||
1469 | if (!netif_running(enic->netdev)) | |
1470 | return; | |
1471 | ||
1472 | rtnl_lock(); | |
1473 | ||
1474 | spin_lock(&enic->devcmd_lock); | |
1475 | vnic_dev_hang_notify(enic->vdev); | |
1476 | spin_unlock(&enic->devcmd_lock); | |
1477 | ||
1478 | enic_stop(enic->netdev); | |
1479 | enic_dev_soft_reset(enic); | |
1480 | enic_reset_mcaddrs(enic); | |
1481 | enic_init_vnic_resources(enic); | |
1482 | enic_open(enic->netdev); | |
1483 | ||
1484 | rtnl_unlock(); | |
1485 | } | |
1486 | ||
1487 | static int enic_set_intr_mode(struct enic *enic) | |
1488 | { | |
1489 | unsigned int n = ARRAY_SIZE(enic->rq); | |
1490 | unsigned int m = ARRAY_SIZE(enic->wq); | |
1491 | unsigned int i; | |
1492 | ||
1493 | /* Set interrupt mode (INTx, MSI, MSI-X) depending | |
1494 | * system capabilities. | |
1495 | * | |
1496 | * Try MSI-X first | |
1497 | * | |
1498 | * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs | |
1499 | * (the second to last INTR is used for WQ/RQ errors) | |
1500 | * (the last INTR is used for notifications) | |
1501 | */ | |
1502 | ||
1503 | BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2); | |
1504 | for (i = 0; i < n + m + 2; i++) | |
1505 | enic->msix_entry[i].entry = i; | |
1506 | ||
1507 | if (enic->config.intr_mode < 1 && | |
1508 | enic->rq_count >= n && | |
1509 | enic->wq_count >= m && | |
1510 | enic->cq_count >= n + m && | |
1511 | enic->intr_count >= n + m + 2 && | |
1512 | !pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) { | |
1513 | ||
1514 | enic->rq_count = n; | |
1515 | enic->wq_count = m; | |
1516 | enic->cq_count = n + m; | |
1517 | enic->intr_count = n + m + 2; | |
1518 | ||
1519 | vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSIX); | |
1520 | ||
1521 | return 0; | |
1522 | } | |
1523 | ||
1524 | /* Next try MSI | |
1525 | * | |
1526 | * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR | |
1527 | */ | |
1528 | ||
1529 | if (enic->config.intr_mode < 2 && | |
1530 | enic->rq_count >= 1 && | |
1531 | enic->wq_count >= 1 && | |
1532 | enic->cq_count >= 2 && | |
1533 | enic->intr_count >= 1 && | |
1534 | !pci_enable_msi(enic->pdev)) { | |
1535 | ||
1536 | enic->rq_count = 1; | |
1537 | enic->wq_count = 1; | |
1538 | enic->cq_count = 2; | |
1539 | enic->intr_count = 1; | |
1540 | ||
1541 | vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI); | |
1542 | ||
1543 | return 0; | |
1544 | } | |
1545 | ||
1546 | /* Next try INTx | |
1547 | * | |
1548 | * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs | |
1549 | * (the first INTR is used for WQ/RQ) | |
1550 | * (the second INTR is used for WQ/RQ errors) | |
1551 | * (the last INTR is used for notifications) | |
1552 | */ | |
1553 | ||
1554 | if (enic->config.intr_mode < 3 && | |
1555 | enic->rq_count >= 1 && | |
1556 | enic->wq_count >= 1 && | |
1557 | enic->cq_count >= 2 && | |
1558 | enic->intr_count >= 3) { | |
1559 | ||
1560 | enic->rq_count = 1; | |
1561 | enic->wq_count = 1; | |
1562 | enic->cq_count = 2; | |
1563 | enic->intr_count = 3; | |
1564 | ||
1565 | vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX); | |
1566 | ||
1567 | return 0; | |
1568 | } | |
1569 | ||
1570 | vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN); | |
1571 | ||
1572 | return -EINVAL; | |
1573 | } | |
1574 | ||
1575 | static void enic_clear_intr_mode(struct enic *enic) | |
1576 | { | |
1577 | switch (vnic_dev_get_intr_mode(enic->vdev)) { | |
1578 | case VNIC_DEV_INTR_MODE_MSIX: | |
1579 | pci_disable_msix(enic->pdev); | |
1580 | break; | |
1581 | case VNIC_DEV_INTR_MODE_MSI: | |
1582 | pci_disable_msi(enic->pdev); | |
1583 | break; | |
1584 | default: | |
1585 | break; | |
1586 | } | |
1587 | ||
1588 | vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN); | |
1589 | } | |
1590 | ||
1591 | static void enic_iounmap(struct enic *enic) | |
1592 | { | |
1593 | if (enic->bar0.vaddr) | |
1594 | iounmap(enic->bar0.vaddr); | |
1595 | } | |
1596 | ||
afe29f7a SH |
1597 | static const struct net_device_ops enic_netdev_ops = { |
1598 | .ndo_open = enic_open, | |
1599 | .ndo_stop = enic_stop, | |
00829823 | 1600 | .ndo_start_xmit = enic_hard_start_xmit, |
afe29f7a SH |
1601 | .ndo_get_stats = enic_get_stats, |
1602 | .ndo_validate_addr = eth_validate_addr, | |
fe96aaa1 | 1603 | .ndo_set_mac_address = eth_mac_addr, |
afe29f7a SH |
1604 | .ndo_set_multicast_list = enic_set_multicast_list, |
1605 | .ndo_change_mtu = enic_change_mtu, | |
1606 | .ndo_vlan_rx_register = enic_vlan_rx_register, | |
1607 | .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid, | |
1608 | .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid, | |
1609 | .ndo_tx_timeout = enic_tx_timeout, | |
1610 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1611 | .ndo_poll_controller = enic_poll_controller, | |
1612 | #endif | |
1613 | }; | |
1614 | ||
01f2e4ea SF |
1615 | static int __devinit enic_probe(struct pci_dev *pdev, |
1616 | const struct pci_device_id *ent) | |
1617 | { | |
1618 | struct net_device *netdev; | |
1619 | struct enic *enic; | |
1620 | int using_dac = 0; | |
1621 | unsigned int i; | |
1622 | int err; | |
1623 | ||
1624 | const u8 rss_default_cpu = 0; | |
1625 | const u8 rss_hash_type = 0; | |
1626 | const u8 rss_hash_bits = 0; | |
1627 | const u8 rss_base_cpu = 0; | |
1628 | const u8 rss_enable = 0; | |
1629 | const u8 tso_ipid_split_en = 0; | |
1630 | const u8 ig_vlan_strip_en = 1; | |
1631 | ||
1632 | /* Allocate net device structure and initialize. Private | |
1633 | * instance data is initialized to zero. | |
1634 | */ | |
1635 | ||
1636 | netdev = alloc_etherdev(sizeof(struct enic)); | |
1637 | if (!netdev) { | |
1638 | printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n"); | |
1639 | return -ENOMEM; | |
1640 | } | |
1641 | ||
01f2e4ea SF |
1642 | pci_set_drvdata(pdev, netdev); |
1643 | ||
1644 | SET_NETDEV_DEV(netdev, &pdev->dev); | |
1645 | ||
1646 | enic = netdev_priv(netdev); | |
1647 | enic->netdev = netdev; | |
1648 | enic->pdev = pdev; | |
1649 | ||
1650 | /* Setup PCI resources | |
1651 | */ | |
1652 | ||
1653 | err = pci_enable_device(pdev); | |
1654 | if (err) { | |
1655 | printk(KERN_ERR PFX | |
4b75a442 | 1656 | "Cannot enable PCI device, aborting.\n"); |
01f2e4ea SF |
1657 | goto err_out_free_netdev; |
1658 | } | |
1659 | ||
1660 | err = pci_request_regions(pdev, DRV_NAME); | |
1661 | if (err) { | |
1662 | printk(KERN_ERR PFX | |
4b75a442 | 1663 | "Cannot request PCI regions, aborting.\n"); |
01f2e4ea SF |
1664 | goto err_out_disable_device; |
1665 | } | |
1666 | ||
1667 | pci_set_master(pdev); | |
1668 | ||
1669 | /* Query PCI controller on system for DMA addressing | |
1670 | * limitation for the device. Try 40-bit first, and | |
1671 | * fail to 32-bit. | |
1672 | */ | |
1673 | ||
1674 | err = pci_set_dma_mask(pdev, DMA_40BIT_MASK); | |
1675 | if (err) { | |
1676 | err = pci_set_dma_mask(pdev, DMA_32BIT_MASK); | |
1677 | if (err) { | |
1678 | printk(KERN_ERR PFX | |
4b75a442 | 1679 | "No usable DMA configuration, aborting.\n"); |
01f2e4ea SF |
1680 | goto err_out_release_regions; |
1681 | } | |
1682 | err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK); | |
1683 | if (err) { | |
1684 | printk(KERN_ERR PFX | |
4b75a442 SF |
1685 | "Unable to obtain 32-bit DMA " |
1686 | "for consistent allocations, aborting.\n"); | |
01f2e4ea SF |
1687 | goto err_out_release_regions; |
1688 | } | |
1689 | } else { | |
1690 | err = pci_set_consistent_dma_mask(pdev, DMA_40BIT_MASK); | |
1691 | if (err) { | |
1692 | printk(KERN_ERR PFX | |
4b75a442 SF |
1693 | "Unable to obtain 40-bit DMA " |
1694 | "for consistent allocations, aborting.\n"); | |
01f2e4ea SF |
1695 | goto err_out_release_regions; |
1696 | } | |
1697 | using_dac = 1; | |
1698 | } | |
1699 | ||
1700 | /* Map vNIC resources from BAR0 | |
1701 | */ | |
1702 | ||
1703 | if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { | |
1704 | printk(KERN_ERR PFX | |
4b75a442 | 1705 | "BAR0 not memory-map'able, aborting.\n"); |
01f2e4ea SF |
1706 | err = -ENODEV; |
1707 | goto err_out_release_regions; | |
1708 | } | |
1709 | ||
1710 | enic->bar0.vaddr = pci_iomap(pdev, 0, enic->bar0.len); | |
1711 | enic->bar0.bus_addr = pci_resource_start(pdev, 0); | |
1712 | enic->bar0.len = pci_resource_len(pdev, 0); | |
1713 | ||
1714 | if (!enic->bar0.vaddr) { | |
1715 | printk(KERN_ERR PFX | |
4b75a442 | 1716 | "Cannot memory-map BAR0 res hdr, aborting.\n"); |
01f2e4ea SF |
1717 | err = -ENODEV; |
1718 | goto err_out_release_regions; | |
1719 | } | |
1720 | ||
1721 | /* Register vNIC device | |
1722 | */ | |
1723 | ||
1724 | enic->vdev = vnic_dev_register(NULL, enic, pdev, &enic->bar0); | |
1725 | if (!enic->vdev) { | |
1726 | printk(KERN_ERR PFX | |
4b75a442 | 1727 | "vNIC registration failed, aborting.\n"); |
01f2e4ea SF |
1728 | err = -ENODEV; |
1729 | goto err_out_iounmap; | |
1730 | } | |
1731 | ||
1732 | /* Issue device open to get device in known state | |
1733 | */ | |
1734 | ||
1735 | err = enic_dev_open(enic); | |
1736 | if (err) { | |
1737 | printk(KERN_ERR PFX | |
4b75a442 | 1738 | "vNIC dev open failed, aborting.\n"); |
01f2e4ea SF |
1739 | goto err_out_vnic_unregister; |
1740 | } | |
1741 | ||
1742 | /* Issue device init to initialize the vnic-to-switch link. | |
1743 | * We'll start with carrier off and wait for link UP | |
1744 | * notification later to turn on carrier. We don't need | |
1745 | * to wait here for the vnic-to-switch link initialization | |
1746 | * to complete; link UP notification is the indication that | |
1747 | * the process is complete. | |
1748 | */ | |
1749 | ||
1750 | netif_carrier_off(netdev); | |
1751 | ||
1752 | err = vnic_dev_init(enic->vdev, 0); | |
1753 | if (err) { | |
1754 | printk(KERN_ERR PFX | |
4b75a442 | 1755 | "vNIC dev init failed, aborting.\n"); |
01f2e4ea SF |
1756 | goto err_out_dev_close; |
1757 | } | |
1758 | ||
1759 | /* Get vNIC configuration | |
1760 | */ | |
1761 | ||
1762 | err = enic_get_vnic_config(enic); | |
1763 | if (err) { | |
1764 | printk(KERN_ERR PFX | |
4b75a442 | 1765 | "Get vNIC configuration failed, aborting.\n"); |
01f2e4ea SF |
1766 | goto err_out_dev_close; |
1767 | } | |
1768 | ||
1769 | /* Get available resource counts | |
86ca9db7 | 1770 | */ |
01f2e4ea SF |
1771 | |
1772 | enic_get_res_counts(enic); | |
1773 | ||
1774 | /* Set interrupt mode based on resource counts and system | |
1775 | * capabilities | |
86ca9db7 | 1776 | */ |
01f2e4ea SF |
1777 | |
1778 | err = enic_set_intr_mode(enic); | |
1779 | if (err) { | |
1780 | printk(KERN_ERR PFX | |
4b75a442 | 1781 | "Failed to set intr mode, aborting.\n"); |
01f2e4ea SF |
1782 | goto err_out_dev_close; |
1783 | } | |
1784 | ||
1785 | /* Allocate and configure vNIC resources | |
1786 | */ | |
1787 | ||
1788 | err = enic_alloc_vnic_resources(enic); | |
1789 | if (err) { | |
1790 | printk(KERN_ERR PFX | |
4b75a442 | 1791 | "Failed to alloc vNIC resources, aborting.\n"); |
01f2e4ea SF |
1792 | goto err_out_free_vnic_resources; |
1793 | } | |
1794 | ||
1795 | enic_init_vnic_resources(enic); | |
1796 | ||
1797 | /* Enable VLAN tag stripping. RSS not enabled (yet). | |
1798 | */ | |
1799 | ||
1800 | err = enic_set_nic_cfg(enic, | |
1801 | rss_default_cpu, rss_hash_type, | |
1802 | rss_hash_bits, rss_base_cpu, | |
1803 | rss_enable, tso_ipid_split_en, | |
1804 | ig_vlan_strip_en); | |
1805 | if (err) { | |
1806 | printk(KERN_ERR PFX | |
4b75a442 | 1807 | "Failed to config nic, aborting.\n"); |
01f2e4ea SF |
1808 | goto err_out_free_vnic_resources; |
1809 | } | |
1810 | ||
1811 | /* Setup notification timer, HW reset task, and locks | |
1812 | */ | |
1813 | ||
1814 | init_timer(&enic->notify_timer); | |
1815 | enic->notify_timer.function = enic_notify_timer; | |
1816 | enic->notify_timer.data = (unsigned long)enic; | |
1817 | ||
1818 | INIT_WORK(&enic->reset, enic_reset); | |
1819 | ||
1820 | for (i = 0; i < enic->wq_count; i++) | |
1821 | spin_lock_init(&enic->wq_lock[i]); | |
1822 | ||
1823 | spin_lock_init(&enic->devcmd_lock); | |
1824 | ||
1825 | /* Register net device | |
1826 | */ | |
1827 | ||
1828 | enic->port_mtu = enic->config.mtu; | |
1829 | (void)enic_change_mtu(netdev, enic->port_mtu); | |
1830 | ||
1831 | err = enic_set_mac_addr(netdev, enic->mac_addr); | |
1832 | if (err) { | |
1833 | printk(KERN_ERR PFX | |
4b75a442 SF |
1834 | "Invalid MAC address, aborting.\n"); |
1835 | goto err_out_free_vnic_resources; | |
01f2e4ea SF |
1836 | } |
1837 | ||
afe29f7a | 1838 | netdev->netdev_ops = &enic_netdev_ops; |
01f2e4ea SF |
1839 | netdev->watchdog_timeo = 2 * HZ; |
1840 | netdev->ethtool_ops = &enic_ethtool_ops; | |
01f2e4ea SF |
1841 | |
1842 | switch (vnic_dev_get_intr_mode(enic->vdev)) { | |
1843 | default: | |
1844 | netif_napi_add(netdev, &enic->napi, enic_poll, 64); | |
1845 | break; | |
1846 | case VNIC_DEV_INTR_MODE_MSIX: | |
1847 | netif_napi_add(netdev, &enic->napi, enic_poll_msix, 64); | |
1848 | break; | |
1849 | } | |
1850 | ||
1851 | netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX; | |
1852 | if (ENIC_SETTING(enic, TXCSUM)) | |
1853 | netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM; | |
1854 | if (ENIC_SETTING(enic, TSO)) | |
1855 | netdev->features |= NETIF_F_TSO | | |
1856 | NETIF_F_TSO6 | NETIF_F_TSO_ECN; | |
86ca9db7 SF |
1857 | if (ENIC_SETTING(enic, LRO)) |
1858 | netdev->features |= NETIF_F_LRO; | |
01f2e4ea SF |
1859 | if (using_dac) |
1860 | netdev->features |= NETIF_F_HIGHDMA; | |
1861 | ||
1862 | ||
1863 | enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM); | |
1864 | ||
86ca9db7 SF |
1865 | enic->lro_mgr.max_aggr = ENIC_LRO_MAX_AGGR; |
1866 | enic->lro_mgr.max_desc = ENIC_LRO_MAX_DESC; | |
1867 | enic->lro_mgr.lro_arr = enic->lro_desc; | |
1868 | enic->lro_mgr.get_skb_header = enic_get_skb_header; | |
1869 | enic->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID; | |
1870 | enic->lro_mgr.dev = netdev; | |
1871 | enic->lro_mgr.ip_summed = CHECKSUM_COMPLETE; | |
1872 | enic->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY; | |
1873 | ||
01f2e4ea SF |
1874 | |
1875 | err = register_netdev(netdev); | |
1876 | if (err) { | |
1877 | printk(KERN_ERR PFX | |
4b75a442 SF |
1878 | "Cannot register net device, aborting.\n"); |
1879 | goto err_out_free_vnic_resources; | |
01f2e4ea SF |
1880 | } |
1881 | ||
1882 | return 0; | |
1883 | ||
01f2e4ea SF |
1884 | err_out_free_vnic_resources: |
1885 | enic_free_vnic_resources(enic); | |
01f2e4ea SF |
1886 | err_out_dev_close: |
1887 | vnic_dev_close(enic->vdev); | |
1888 | err_out_vnic_unregister: | |
1889 | enic_clear_intr_mode(enic); | |
1890 | vnic_dev_unregister(enic->vdev); | |
1891 | err_out_iounmap: | |
1892 | enic_iounmap(enic); | |
1893 | err_out_release_regions: | |
1894 | pci_release_regions(pdev); | |
1895 | err_out_disable_device: | |
1896 | pci_disable_device(pdev); | |
1897 | err_out_free_netdev: | |
1898 | pci_set_drvdata(pdev, NULL); | |
1899 | free_netdev(netdev); | |
1900 | ||
1901 | return err; | |
1902 | } | |
1903 | ||
1904 | static void __devexit enic_remove(struct pci_dev *pdev) | |
1905 | { | |
1906 | struct net_device *netdev = pci_get_drvdata(pdev); | |
1907 | ||
1908 | if (netdev) { | |
1909 | struct enic *enic = netdev_priv(netdev); | |
1910 | ||
1911 | flush_scheduled_work(); | |
1912 | unregister_netdev(netdev); | |
01f2e4ea | 1913 | enic_free_vnic_resources(enic); |
01f2e4ea SF |
1914 | vnic_dev_close(enic->vdev); |
1915 | enic_clear_intr_mode(enic); | |
1916 | vnic_dev_unregister(enic->vdev); | |
1917 | enic_iounmap(enic); | |
1918 | pci_release_regions(pdev); | |
1919 | pci_disable_device(pdev); | |
1920 | pci_set_drvdata(pdev, NULL); | |
1921 | free_netdev(netdev); | |
1922 | } | |
1923 | } | |
1924 | ||
1925 | static struct pci_driver enic_driver = { | |
1926 | .name = DRV_NAME, | |
1927 | .id_table = enic_id_table, | |
1928 | .probe = enic_probe, | |
1929 | .remove = __devexit_p(enic_remove), | |
1930 | }; | |
1931 | ||
1932 | static int __init enic_init_module(void) | |
1933 | { | |
1934 | printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION); | |
1935 | ||
1936 | return pci_register_driver(&enic_driver); | |
1937 | } | |
1938 | ||
1939 | static void __exit enic_cleanup_module(void) | |
1940 | { | |
1941 | pci_unregister_driver(&enic_driver); | |
1942 | } | |
1943 | ||
1944 | module_init(enic_init_module); | |
1945 | module_exit(enic_cleanup_module); |