enic: Add support for MTU change via port profile on a dynamic vnic
[linux-2.6-block.git] / drivers / net / enic / enic_main.c
CommitLineData
01f2e4ea 1/*
29046f9b 2 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
01f2e4ea
SF
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>
29046f9b 37#include <linux/rtnetlink.h>
70c71606 38#include <linux/prefetch.h>
b7c6bfb7 39#include <net/ip6_checksum.h>
01f2e4ea
SF
40
41#include "cq_enet_desc.h"
42#include "vnic_dev.h"
43#include "vnic_intr.h"
44#include "vnic_stats.h"
f8bd9091 45#include "vnic_vic.h"
01f2e4ea
SF
46#include "enic_res.h"
47#include "enic.h"
51987461 48#include "enic_dev.h"
b3abfbd2 49#include "enic_pp.h"
01f2e4ea
SF
50
51#define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
ea0d7d91
SF
52#define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
53#define MAX_TSO (1 << 16)
54#define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
55
56#define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
f8bd9091 57#define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
01f2e4ea
SF
58
59/* Supported devices */
a3aa1884 60static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
ea0d7d91 61 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
f8bd9091 62 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
01f2e4ea
SF
63 { 0, } /* end of table */
64};
65
66MODULE_DESCRIPTION(DRV_DESCRIPTION);
67MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
68MODULE_LICENSE("GPL");
69MODULE_VERSION(DRV_VERSION);
70MODULE_DEVICE_TABLE(pci, enic_id_table);
71
72struct enic_stat {
73 char name[ETH_GSTRING_LEN];
74 unsigned int offset;
75};
76
77#define ENIC_TX_STAT(stat) \
78 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
79#define ENIC_RX_STAT(stat) \
80 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
81
82static const struct enic_stat enic_tx_stats[] = {
83 ENIC_TX_STAT(tx_frames_ok),
84 ENIC_TX_STAT(tx_unicast_frames_ok),
85 ENIC_TX_STAT(tx_multicast_frames_ok),
86 ENIC_TX_STAT(tx_broadcast_frames_ok),
87 ENIC_TX_STAT(tx_bytes_ok),
88 ENIC_TX_STAT(tx_unicast_bytes_ok),
89 ENIC_TX_STAT(tx_multicast_bytes_ok),
90 ENIC_TX_STAT(tx_broadcast_bytes_ok),
91 ENIC_TX_STAT(tx_drops),
92 ENIC_TX_STAT(tx_errors),
93 ENIC_TX_STAT(tx_tso),
94};
95
96static const struct enic_stat enic_rx_stats[] = {
97 ENIC_RX_STAT(rx_frames_ok),
98 ENIC_RX_STAT(rx_frames_total),
99 ENIC_RX_STAT(rx_unicast_frames_ok),
100 ENIC_RX_STAT(rx_multicast_frames_ok),
101 ENIC_RX_STAT(rx_broadcast_frames_ok),
102 ENIC_RX_STAT(rx_bytes_ok),
103 ENIC_RX_STAT(rx_unicast_bytes_ok),
104 ENIC_RX_STAT(rx_multicast_bytes_ok),
105 ENIC_RX_STAT(rx_broadcast_bytes_ok),
106 ENIC_RX_STAT(rx_drop),
107 ENIC_RX_STAT(rx_no_bufs),
108 ENIC_RX_STAT(rx_errors),
109 ENIC_RX_STAT(rx_rss),
110 ENIC_RX_STAT(rx_crc_errors),
111 ENIC_RX_STAT(rx_frames_64),
112 ENIC_RX_STAT(rx_frames_127),
113 ENIC_RX_STAT(rx_frames_255),
114 ENIC_RX_STAT(rx_frames_511),
115 ENIC_RX_STAT(rx_frames_1023),
116 ENIC_RX_STAT(rx_frames_1518),
117 ENIC_RX_STAT(rx_frames_to_max),
118};
119
120static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
121static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
122
f8bd9091
SF
123static int enic_is_dynamic(struct enic *enic)
124{
125 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
126}
127
717258ba
VK
128static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
129{
130 return rq;
131}
132
133static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
134{
135 return enic->rq_count + wq;
136}
137
138static inline unsigned int enic_legacy_io_intr(void)
139{
140 return 0;
141}
142
143static inline unsigned int enic_legacy_err_intr(void)
144{
145 return 1;
146}
147
148static inline unsigned int enic_legacy_notify_intr(void)
149{
150 return 2;
151}
152
153static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
154{
155 return rq;
156}
157
158static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
159{
160 return enic->rq_count + wq;
161}
162
163static inline unsigned int enic_msix_err_intr(struct enic *enic)
164{
165 return enic->rq_count + enic->wq_count;
166}
167
168static inline unsigned int enic_msix_notify_intr(struct enic *enic)
169{
170 return enic->rq_count + enic->wq_count + 1;
171}
172
01f2e4ea
SF
173static int enic_get_settings(struct net_device *netdev,
174 struct ethtool_cmd *ecmd)
175{
176 struct enic *enic = netdev_priv(netdev);
177
178 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
179 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
180 ecmd->port = PORT_FIBRE;
181 ecmd->transceiver = XCVR_EXTERNAL;
182
183 if (netif_carrier_ok(netdev)) {
70739497 184 ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
01f2e4ea
SF
185 ecmd->duplex = DUPLEX_FULL;
186 } else {
70739497 187 ethtool_cmd_speed_set(ecmd, -1);
01f2e4ea
SF
188 ecmd->duplex = -1;
189 }
190
191 ecmd->autoneg = AUTONEG_DISABLE;
192
193 return 0;
194}
195
196static void enic_get_drvinfo(struct net_device *netdev,
197 struct ethtool_drvinfo *drvinfo)
198{
199 struct enic *enic = netdev_priv(netdev);
200 struct vnic_devcmd_fw_info *fw_info;
201
383ab92f 202 enic_dev_fw_info(enic, &fw_info);
01f2e4ea
SF
203
204 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
205 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
206 strncpy(drvinfo->fw_version, fw_info->fw_version,
207 sizeof(drvinfo->fw_version));
208 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
209 sizeof(drvinfo->bus_info));
210}
211
212static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
213{
214 unsigned int i;
215
216 switch (stringset) {
217 case ETH_SS_STATS:
218 for (i = 0; i < enic_n_tx_stats; i++) {
219 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
220 data += ETH_GSTRING_LEN;
221 }
222 for (i = 0; i < enic_n_rx_stats; i++) {
223 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
224 data += ETH_GSTRING_LEN;
225 }
226 break;
227 }
228}
229
25f0a061 230static int enic_get_sset_count(struct net_device *netdev, int sset)
01f2e4ea 231{
25f0a061
SF
232 switch (sset) {
233 case ETH_SS_STATS:
234 return enic_n_tx_stats + enic_n_rx_stats;
235 default:
236 return -EOPNOTSUPP;
237 }
01f2e4ea
SF
238}
239
240static void enic_get_ethtool_stats(struct net_device *netdev,
241 struct ethtool_stats *stats, u64 *data)
242{
243 struct enic *enic = netdev_priv(netdev);
244 struct vnic_stats *vstats;
245 unsigned int i;
246
383ab92f 247 enic_dev_stats_dump(enic, &vstats);
01f2e4ea
SF
248
249 for (i = 0; i < enic_n_tx_stats; i++)
250 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
251 for (i = 0; i < enic_n_rx_stats; i++)
252 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
253}
254
01f2e4ea
SF
255static u32 enic_get_msglevel(struct net_device *netdev)
256{
257 struct enic *enic = netdev_priv(netdev);
258 return enic->msg_enable;
259}
260
261static void enic_set_msglevel(struct net_device *netdev, u32 value)
262{
263 struct enic *enic = netdev_priv(netdev);
264 enic->msg_enable = value;
265}
266
7c844599
SF
267static int enic_get_coalesce(struct net_device *netdev,
268 struct ethtool_coalesce *ecmd)
269{
270 struct enic *enic = netdev_priv(netdev);
271
272 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
273 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
274
275 return 0;
276}
277
278static int enic_set_coalesce(struct net_device *netdev,
279 struct ethtool_coalesce *ecmd)
280{
281 struct enic *enic = netdev_priv(netdev);
282 u32 tx_coalesce_usecs;
283 u32 rx_coalesce_usecs;
717258ba 284 unsigned int i, intr;
7c844599
SF
285
286 tx_coalesce_usecs = min_t(u32,
287 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
288 ecmd->tx_coalesce_usecs);
289 rx_coalesce_usecs = min_t(u32,
290 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
291 ecmd->rx_coalesce_usecs);
292
293 switch (vnic_dev_get_intr_mode(enic->vdev)) {
294 case VNIC_DEV_INTR_MODE_INTX:
295 if (tx_coalesce_usecs != rx_coalesce_usecs)
296 return -EINVAL;
297
717258ba
VK
298 intr = enic_legacy_io_intr();
299 vnic_intr_coalescing_timer_set(&enic->intr[intr],
7c844599
SF
300 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
301 break;
302 case VNIC_DEV_INTR_MODE_MSI:
303 if (tx_coalesce_usecs != rx_coalesce_usecs)
304 return -EINVAL;
305
306 vnic_intr_coalescing_timer_set(&enic->intr[0],
307 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
308 break;
309 case VNIC_DEV_INTR_MODE_MSIX:
717258ba
VK
310 for (i = 0; i < enic->wq_count; i++) {
311 intr = enic_msix_wq_intr(enic, i);
312 vnic_intr_coalescing_timer_set(&enic->intr[intr],
313 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
314 }
315
316 for (i = 0; i < enic->rq_count; i++) {
317 intr = enic_msix_rq_intr(enic, i);
318 vnic_intr_coalescing_timer_set(&enic->intr[intr],
319 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
320 }
321
7c844599
SF
322 break;
323 default:
324 break;
325 }
326
327 enic->tx_coalesce_usecs = tx_coalesce_usecs;
328 enic->rx_coalesce_usecs = rx_coalesce_usecs;
329
330 return 0;
331}
332
0fc0b732 333static const struct ethtool_ops enic_ethtool_ops = {
01f2e4ea
SF
334 .get_settings = enic_get_settings,
335 .get_drvinfo = enic_get_drvinfo,
336 .get_msglevel = enic_get_msglevel,
337 .set_msglevel = enic_set_msglevel,
338 .get_link = ethtool_op_get_link,
339 .get_strings = enic_get_strings,
25f0a061 340 .get_sset_count = enic_get_sset_count,
01f2e4ea 341 .get_ethtool_stats = enic_get_ethtool_stats,
7c844599
SF
342 .get_coalesce = enic_get_coalesce,
343 .set_coalesce = enic_set_coalesce,
01f2e4ea
SF
344};
345
346static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
347{
348 struct enic *enic = vnic_dev_priv(wq->vdev);
349
350 if (buf->sop)
351 pci_unmap_single(enic->pdev, buf->dma_addr,
352 buf->len, PCI_DMA_TODEVICE);
353 else
354 pci_unmap_page(enic->pdev, buf->dma_addr,
355 buf->len, PCI_DMA_TODEVICE);
356
357 if (buf->os_buf)
358 dev_kfree_skb_any(buf->os_buf);
359}
360
361static void enic_wq_free_buf(struct vnic_wq *wq,
362 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
363{
364 enic_free_wq_buf(wq, buf);
365}
366
367static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
368 u8 type, u16 q_number, u16 completed_index, void *opaque)
369{
370 struct enic *enic = vnic_dev_priv(vdev);
371
372 spin_lock(&enic->wq_lock[q_number]);
373
374 vnic_wq_service(&enic->wq[q_number], cq_desc,
375 completed_index, enic_wq_free_buf,
376 opaque);
377
378 if (netif_queue_stopped(enic->netdev) &&
ea0d7d91
SF
379 vnic_wq_desc_avail(&enic->wq[q_number]) >=
380 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
01f2e4ea
SF
381 netif_wake_queue(enic->netdev);
382
383 spin_unlock(&enic->wq_lock[q_number]);
384
385 return 0;
386}
387
388static void enic_log_q_error(struct enic *enic)
389{
390 unsigned int i;
391 u32 error_status;
392
393 for (i = 0; i < enic->wq_count; i++) {
394 error_status = vnic_wq_error_status(&enic->wq[i]);
395 if (error_status)
a7a79deb
VK
396 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
397 i, error_status);
01f2e4ea
SF
398 }
399
400 for (i = 0; i < enic->rq_count; i++) {
401 error_status = vnic_rq_error_status(&enic->rq[i]);
402 if (error_status)
a7a79deb
VK
403 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
404 i, error_status);
01f2e4ea
SF
405 }
406}
407
383ab92f 408static void enic_msglvl_check(struct enic *enic)
01f2e4ea 409{
383ab92f 410 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
01f2e4ea 411
383ab92f 412 if (msg_enable != enic->msg_enable) {
a7a79deb
VK
413 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
414 enic->msg_enable, msg_enable);
383ab92f 415 enic->msg_enable = msg_enable;
01f2e4ea
SF
416 }
417}
418
419static void enic_mtu_check(struct enic *enic)
420{
421 u32 mtu = vnic_dev_mtu(enic->vdev);
a7a79deb 422 struct net_device *netdev = enic->netdev;
01f2e4ea 423
491598a4 424 if (mtu && mtu != enic->port_mtu) {
7c844599 425 enic->port_mtu = mtu;
c97c894d
RP
426 if (enic_is_dynamic(enic)) {
427 mtu = max_t(int, ENIC_MIN_MTU,
428 min_t(int, ENIC_MAX_MTU, mtu));
429 if (mtu != netdev->mtu)
430 schedule_work(&enic->change_mtu_work);
431 } else {
432 if (mtu < netdev->mtu)
433 netdev_warn(netdev,
434 "interface MTU (%d) set higher "
435 "than switch port MTU (%d)\n",
436 netdev->mtu, mtu);
437 }
01f2e4ea
SF
438 }
439}
440
383ab92f 441static void enic_link_check(struct enic *enic)
01f2e4ea 442{
383ab92f
VK
443 int link_status = vnic_dev_link_status(enic->vdev);
444 int carrier_ok = netif_carrier_ok(enic->netdev);
01f2e4ea 445
383ab92f 446 if (link_status && !carrier_ok) {
a7a79deb 447 netdev_info(enic->netdev, "Link UP\n");
383ab92f
VK
448 netif_carrier_on(enic->netdev);
449 } else if (!link_status && carrier_ok) {
a7a79deb 450 netdev_info(enic->netdev, "Link DOWN\n");
383ab92f 451 netif_carrier_off(enic->netdev);
01f2e4ea
SF
452 }
453}
454
455static void enic_notify_check(struct enic *enic)
456{
457 enic_msglvl_check(enic);
458 enic_mtu_check(enic);
459 enic_link_check(enic);
460}
461
462#define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
463
464static irqreturn_t enic_isr_legacy(int irq, void *data)
465{
466 struct net_device *netdev = data;
467 struct enic *enic = netdev_priv(netdev);
717258ba
VK
468 unsigned int io_intr = enic_legacy_io_intr();
469 unsigned int err_intr = enic_legacy_err_intr();
470 unsigned int notify_intr = enic_legacy_notify_intr();
01f2e4ea
SF
471 u32 pba;
472
717258ba 473 vnic_intr_mask(&enic->intr[io_intr]);
01f2e4ea
SF
474
475 pba = vnic_intr_legacy_pba(enic->legacy_pba);
476 if (!pba) {
717258ba 477 vnic_intr_unmask(&enic->intr[io_intr]);
01f2e4ea
SF
478 return IRQ_NONE; /* not our interrupt */
479 }
480
717258ba
VK
481 if (ENIC_TEST_INTR(pba, notify_intr)) {
482 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
01f2e4ea 483 enic_notify_check(enic);
ed8af6b2 484 }
01f2e4ea 485
717258ba
VK
486 if (ENIC_TEST_INTR(pba, err_intr)) {
487 vnic_intr_return_all_credits(&enic->intr[err_intr]);
01f2e4ea
SF
488 enic_log_q_error(enic);
489 /* schedule recovery from WQ/RQ error */
490 schedule_work(&enic->reset);
491 return IRQ_HANDLED;
492 }
493
717258ba
VK
494 if (ENIC_TEST_INTR(pba, io_intr)) {
495 if (napi_schedule_prep(&enic->napi[0]))
496 __napi_schedule(&enic->napi[0]);
01f2e4ea 497 } else {
717258ba 498 vnic_intr_unmask(&enic->intr[io_intr]);
01f2e4ea
SF
499 }
500
501 return IRQ_HANDLED;
502}
503
504static irqreturn_t enic_isr_msi(int irq, void *data)
505{
506 struct enic *enic = data;
507
508 /* With MSI, there is no sharing of interrupts, so this is
509 * our interrupt and there is no need to ack it. The device
510 * is not providing per-vector masking, so the OS will not
511 * write to PCI config space to mask/unmask the interrupt.
512 * We're using mask_on_assertion for MSI, so the device
513 * automatically masks the interrupt when the interrupt is
514 * generated. Later, when exiting polling, the interrupt
515 * will be unmasked (see enic_poll).
516 *
517 * Also, the device uses the same PCIe Traffic Class (TC)
518 * for Memory Write data and MSI, so there are no ordering
519 * issues; the MSI will always arrive at the Root Complex
520 * _after_ corresponding Memory Writes (i.e. descriptor
521 * writes).
522 */
523
717258ba 524 napi_schedule(&enic->napi[0]);
01f2e4ea
SF
525
526 return IRQ_HANDLED;
527}
528
529static irqreturn_t enic_isr_msix_rq(int irq, void *data)
530{
717258ba 531 struct napi_struct *napi = data;
01f2e4ea
SF
532
533 /* schedule NAPI polling for RQ cleanup */
717258ba 534 napi_schedule(napi);
01f2e4ea
SF
535
536 return IRQ_HANDLED;
537}
538
539static irqreturn_t enic_isr_msix_wq(int irq, void *data)
540{
541 struct enic *enic = data;
717258ba
VK
542 unsigned int cq = enic_cq_wq(enic, 0);
543 unsigned int intr = enic_msix_wq_intr(enic, 0);
01f2e4ea
SF
544 unsigned int wq_work_to_do = -1; /* no limit */
545 unsigned int wq_work_done;
546
717258ba 547 wq_work_done = vnic_cq_service(&enic->cq[cq],
01f2e4ea
SF
548 wq_work_to_do, enic_wq_service, NULL);
549
717258ba 550 vnic_intr_return_credits(&enic->intr[intr],
01f2e4ea
SF
551 wq_work_done,
552 1 /* unmask intr */,
553 1 /* reset intr timer */);
554
555 return IRQ_HANDLED;
556}
557
558static irqreturn_t enic_isr_msix_err(int irq, void *data)
559{
560 struct enic *enic = data;
717258ba 561 unsigned int intr = enic_msix_err_intr(enic);
01f2e4ea 562
717258ba 563 vnic_intr_return_all_credits(&enic->intr[intr]);
ed8af6b2 564
01f2e4ea
SF
565 enic_log_q_error(enic);
566
567 /* schedule recovery from WQ/RQ error */
568 schedule_work(&enic->reset);
569
570 return IRQ_HANDLED;
571}
572
573static irqreturn_t enic_isr_msix_notify(int irq, void *data)
574{
575 struct enic *enic = data;
717258ba 576 unsigned int intr = enic_msix_notify_intr(enic);
01f2e4ea 577
717258ba 578 vnic_intr_return_all_credits(&enic->intr[intr]);
01f2e4ea 579 enic_notify_check(enic);
01f2e4ea
SF
580
581 return IRQ_HANDLED;
582}
583
584static inline void enic_queue_wq_skb_cont(struct enic *enic,
585 struct vnic_wq *wq, struct sk_buff *skb,
1825aca6 586 unsigned int len_left, int loopback)
01f2e4ea
SF
587{
588 skb_frag_t *frag;
589
590 /* Queue additional data fragments */
591 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
592 len_left -= frag->size;
593 enic_queue_wq_desc_cont(wq, skb,
594 pci_map_page(enic->pdev, frag->page,
595 frag->page_offset, frag->size,
596 PCI_DMA_TODEVICE),
597 frag->size,
1825aca6
VK
598 (len_left == 0), /* EOP? */
599 loopback);
01f2e4ea
SF
600 }
601}
602
603static inline void enic_queue_wq_skb_vlan(struct enic *enic,
604 struct vnic_wq *wq, struct sk_buff *skb,
1825aca6 605 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
01f2e4ea
SF
606{
607 unsigned int head_len = skb_headlen(skb);
608 unsigned int len_left = skb->len - head_len;
609 int eop = (len_left == 0);
610
ea0d7d91
SF
611 /* Queue the main skb fragment. The fragments are no larger
612 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
613 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
614 * per fragment is queued.
615 */
01f2e4ea
SF
616 enic_queue_wq_desc(wq, skb,
617 pci_map_single(enic->pdev, skb->data,
618 head_len, PCI_DMA_TODEVICE),
619 head_len,
620 vlan_tag_insert, vlan_tag,
1825aca6 621 eop, loopback);
01f2e4ea
SF
622
623 if (!eop)
1825aca6 624 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
01f2e4ea
SF
625}
626
627static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
628 struct vnic_wq *wq, struct sk_buff *skb,
1825aca6 629 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
01f2e4ea
SF
630{
631 unsigned int head_len = skb_headlen(skb);
632 unsigned int len_left = skb->len - head_len;
0d0b1672 633 unsigned int hdr_len = skb_checksum_start_offset(skb);
01f2e4ea
SF
634 unsigned int csum_offset = hdr_len + skb->csum_offset;
635 int eop = (len_left == 0);
636
ea0d7d91
SF
637 /* Queue the main skb fragment. The fragments are no larger
638 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
639 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
640 * per fragment is queued.
641 */
01f2e4ea
SF
642 enic_queue_wq_desc_csum_l4(wq, skb,
643 pci_map_single(enic->pdev, skb->data,
644 head_len, PCI_DMA_TODEVICE),
645 head_len,
646 csum_offset,
647 hdr_len,
648 vlan_tag_insert, vlan_tag,
1825aca6 649 eop, loopback);
01f2e4ea
SF
650
651 if (!eop)
1825aca6 652 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
01f2e4ea
SF
653}
654
655static inline void enic_queue_wq_skb_tso(struct enic *enic,
656 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
1825aca6 657 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
01f2e4ea 658{
ea0d7d91
SF
659 unsigned int frag_len_left = skb_headlen(skb);
660 unsigned int len_left = skb->len - frag_len_left;
01f2e4ea
SF
661 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
662 int eop = (len_left == 0);
ea0d7d91
SF
663 unsigned int len;
664 dma_addr_t dma_addr;
665 unsigned int offset = 0;
666 skb_frag_t *frag;
01f2e4ea
SF
667
668 /* Preload TCP csum field with IP pseudo hdr calculated
669 * with IP length set to zero. HW will later add in length
670 * to each TCP segment resulting from the TSO.
671 */
672
09640e63 673 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
01f2e4ea
SF
674 ip_hdr(skb)->check = 0;
675 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
676 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
09640e63 677 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
01f2e4ea
SF
678 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
679 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
680 }
681
ea0d7d91
SF
682 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
683 * for the main skb fragment
684 */
685 while (frag_len_left) {
686 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
687 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
688 len, PCI_DMA_TODEVICE);
689 enic_queue_wq_desc_tso(wq, skb,
690 dma_addr,
691 len,
692 mss, hdr_len,
693 vlan_tag_insert, vlan_tag,
1825aca6 694 eop && (len == frag_len_left), loopback);
ea0d7d91
SF
695 frag_len_left -= len;
696 offset += len;
697 }
01f2e4ea 698
ea0d7d91
SF
699 if (eop)
700 return;
701
702 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
703 * for additional data fragments
704 */
705 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
706 len_left -= frag->size;
707 frag_len_left = frag->size;
708 offset = frag->page_offset;
709
710 while (frag_len_left) {
711 len = min(frag_len_left,
712 (unsigned int)WQ_ENET_MAX_DESC_LEN);
713 dma_addr = pci_map_page(enic->pdev, frag->page,
714 offset, len,
715 PCI_DMA_TODEVICE);
716 enic_queue_wq_desc_cont(wq, skb,
717 dma_addr,
718 len,
719 (len_left == 0) &&
1825aca6
VK
720 (len == frag_len_left), /* EOP? */
721 loopback);
ea0d7d91
SF
722 frag_len_left -= len;
723 offset += len;
724 }
725 }
01f2e4ea
SF
726}
727
728static inline void enic_queue_wq_skb(struct enic *enic,
729 struct vnic_wq *wq, struct sk_buff *skb)
730{
731 unsigned int mss = skb_shinfo(skb)->gso_size;
732 unsigned int vlan_tag = 0;
733 int vlan_tag_insert = 0;
1825aca6 734 int loopback = 0;
01f2e4ea 735
eab6d18d 736 if (vlan_tx_tag_present(skb)) {
01f2e4ea
SF
737 /* VLAN tag from trunking driver */
738 vlan_tag_insert = 1;
739 vlan_tag = vlan_tx_tag_get(skb);
1825aca6
VK
740 } else if (enic->loop_enable) {
741 vlan_tag = enic->loop_tag;
742 loopback = 1;
01f2e4ea
SF
743 }
744
745 if (mss)
746 enic_queue_wq_skb_tso(enic, wq, skb, mss,
1825aca6 747 vlan_tag_insert, vlan_tag, loopback);
01f2e4ea
SF
748 else if (skb->ip_summed == CHECKSUM_PARTIAL)
749 enic_queue_wq_skb_csum_l4(enic, wq, skb,
1825aca6 750 vlan_tag_insert, vlan_tag, loopback);
01f2e4ea
SF
751 else
752 enic_queue_wq_skb_vlan(enic, wq, skb,
1825aca6 753 vlan_tag_insert, vlan_tag, loopback);
01f2e4ea
SF
754}
755
ed8af6b2 756/* netif_tx_lock held, process context with BHs disabled, or BH */
61357325 757static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
d87fd25d 758 struct net_device *netdev)
01f2e4ea
SF
759{
760 struct enic *enic = netdev_priv(netdev);
761 struct vnic_wq *wq = &enic->wq[0];
762 unsigned long flags;
763
764 if (skb->len <= 0) {
765 dev_kfree_skb(skb);
766 return NETDEV_TX_OK;
767 }
768
769 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
770 * which is very likely. In the off chance it's going to take
771 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
772 */
773
774 if (skb_shinfo(skb)->gso_size == 0 &&
775 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
776 skb_linearize(skb)) {
777 dev_kfree_skb(skb);
778 return NETDEV_TX_OK;
779 }
780
781 spin_lock_irqsave(&enic->wq_lock[0], flags);
782
ea0d7d91
SF
783 if (vnic_wq_desc_avail(wq) <
784 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
01f2e4ea
SF
785 netif_stop_queue(netdev);
786 /* This is a hard error, log it */
a7a79deb 787 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
01f2e4ea
SF
788 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
789 return NETDEV_TX_BUSY;
790 }
791
792 enic_queue_wq_skb(enic, wq, skb);
793
ea0d7d91 794 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
01f2e4ea
SF
795 netif_stop_queue(netdev);
796
01f2e4ea
SF
797 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
798
799 return NETDEV_TX_OK;
800}
801
802/* dev_base_lock rwlock held, nominally process context */
803static struct net_device_stats *enic_get_stats(struct net_device *netdev)
804{
805 struct enic *enic = netdev_priv(netdev);
25f0a061 806 struct net_device_stats *net_stats = &netdev->stats;
01f2e4ea
SF
807 struct vnic_stats *stats;
808
383ab92f 809 enic_dev_stats_dump(enic, &stats);
01f2e4ea 810
25f0a061
SF
811 net_stats->tx_packets = stats->tx.tx_frames_ok;
812 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
813 net_stats->tx_errors = stats->tx.tx_errors;
814 net_stats->tx_dropped = stats->tx.tx_drops;
01f2e4ea 815
25f0a061
SF
816 net_stats->rx_packets = stats->rx.rx_frames_ok;
817 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
818 net_stats->rx_errors = stats->rx.rx_errors;
819 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
350991e1 820 net_stats->rx_over_errors = enic->rq_truncated_pkts;
bd9fb1a4 821 net_stats->rx_crc_errors = enic->rq_bad_fcs;
350991e1 822 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
01f2e4ea 823
25f0a061 824 return net_stats;
01f2e4ea
SF
825}
826
b3abfbd2 827void enic_reset_addr_lists(struct enic *enic)
01f2e4ea
SF
828{
829 enic->mc_count = 0;
e0afe53f 830 enic->uc_count = 0;
99ef5639 831 enic->flags = 0;
01f2e4ea
SF
832}
833
834static int enic_set_mac_addr(struct net_device *netdev, char *addr)
835{
f8bd9091
SF
836 struct enic *enic = netdev_priv(netdev);
837
838 if (enic_is_dynamic(enic)) {
839 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
840 return -EADDRNOTAVAIL;
841 } else {
842 if (!is_valid_ether_addr(addr))
843 return -EADDRNOTAVAIL;
844 }
01f2e4ea
SF
845
846 memcpy(netdev->dev_addr, addr, netdev->addr_len);
847
848 return 0;
849}
850
f8bd9091
SF
851static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
852{
853 struct enic *enic = netdev_priv(netdev);
854 struct sockaddr *saddr = p;
855 char *addr = saddr->sa_data;
856 int err;
857
858 if (netif_running(enic->netdev)) {
859 err = enic_dev_del_station_addr(enic);
860 if (err)
861 return err;
862 }
863
864 err = enic_set_mac_addr(netdev, addr);
865 if (err)
866 return err;
867
868 if (netif_running(enic->netdev)) {
869 err = enic_dev_add_station_addr(enic);
870 if (err)
871 return err;
872 }
873
874 return err;
875}
876
877static int enic_set_mac_address(struct net_device *netdev, void *p)
878{
294dab25 879 struct sockaddr *saddr = p;
c76fd32d
VK
880 char *addr = saddr->sa_data;
881 struct enic *enic = netdev_priv(netdev);
882 int err;
883
884 err = enic_dev_del_station_addr(enic);
885 if (err)
886 return err;
887
888 err = enic_set_mac_addr(netdev, addr);
889 if (err)
890 return err;
294dab25 891
c76fd32d 892 return enic_dev_add_station_addr(enic);
f8bd9091
SF
893}
894
e0afe53f 895static void enic_update_multicast_addr_list(struct enic *enic)
01f2e4ea 896{
319d7e84 897 struct net_device *netdev = enic->netdev;
22bedad3 898 struct netdev_hw_addr *ha;
4cd24eaf 899 unsigned int mc_count = netdev_mc_count(netdev);
01f2e4ea 900 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
01f2e4ea
SF
901 unsigned int i, j;
902
319d7e84
RP
903 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
904 netdev_warn(netdev, "Registering only %d out of %d "
905 "multicast addresses\n",
906 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
01f2e4ea 907 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
9959a185 908 }
01f2e4ea
SF
909
910 /* Is there an easier way? Trying to minimize to
911 * calls to add/del multicast addrs. We keep the
912 * addrs from the last call in enic->mc_addr and
913 * look for changes to add/del.
914 */
915
48e2f183 916 i = 0;
22bedad3 917 netdev_for_each_mc_addr(ha, netdev) {
48e2f183
JP
918 if (i == mc_count)
919 break;
22bedad3 920 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
01f2e4ea
SF
921 }
922
923 for (i = 0; i < enic->mc_count; i++) {
924 for (j = 0; j < mc_count; j++)
925 if (compare_ether_addr(enic->mc_addr[i],
926 mc_addr[j]) == 0)
927 break;
928 if (j == mc_count)
319d7e84 929 enic_dev_del_addr(enic, enic->mc_addr[i]);
01f2e4ea
SF
930 }
931
932 for (i = 0; i < mc_count; i++) {
933 for (j = 0; j < enic->mc_count; j++)
934 if (compare_ether_addr(mc_addr[i],
935 enic->mc_addr[j]) == 0)
936 break;
937 if (j == enic->mc_count)
319d7e84 938 enic_dev_add_addr(enic, mc_addr[i]);
01f2e4ea
SF
939 }
940
941 /* Save the list to compare against next time
942 */
943
944 for (i = 0; i < mc_count; i++)
945 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
946
947 enic->mc_count = mc_count;
01f2e4ea
SF
948}
949
e0afe53f 950static void enic_update_unicast_addr_list(struct enic *enic)
319d7e84
RP
951{
952 struct net_device *netdev = enic->netdev;
953 struct netdev_hw_addr *ha;
954 unsigned int uc_count = netdev_uc_count(netdev);
955 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
956 unsigned int i, j;
957
958 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
959 netdev_warn(netdev, "Registering only %d out of %d "
960 "unicast addresses\n",
961 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
962 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
963 }
964
965 /* Is there an easier way? Trying to minimize to
966 * calls to add/del unicast addrs. We keep the
967 * addrs from the last call in enic->uc_addr and
968 * look for changes to add/del.
969 */
970
971 i = 0;
972 netdev_for_each_uc_addr(ha, netdev) {
973 if (i == uc_count)
974 break;
975 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
976 }
977
978 for (i = 0; i < enic->uc_count; i++) {
979 for (j = 0; j < uc_count; j++)
980 if (compare_ether_addr(enic->uc_addr[i],
981 uc_addr[j]) == 0)
982 break;
983 if (j == uc_count)
984 enic_dev_del_addr(enic, enic->uc_addr[i]);
985 }
986
987 for (i = 0; i < uc_count; i++) {
988 for (j = 0; j < enic->uc_count; j++)
989 if (compare_ether_addr(uc_addr[i],
990 enic->uc_addr[j]) == 0)
991 break;
992 if (j == enic->uc_count)
993 enic_dev_add_addr(enic, uc_addr[i]);
994 }
995
996 /* Save the list to compare against next time
997 */
998
999 for (i = 0; i < uc_count; i++)
1000 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1001
1002 enic->uc_count = uc_count;
1003}
1004
1005/* netif_tx_lock held, BHs disabled */
1006static void enic_set_rx_mode(struct net_device *netdev)
1007{
1008 struct enic *enic = netdev_priv(netdev);
1009 int directed = 1;
1010 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1011 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1012 int promisc = (netdev->flags & IFF_PROMISC) ||
1013 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1014 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1015 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1016 unsigned int flags = netdev->flags |
1017 (allmulti ? IFF_ALLMULTI : 0) |
1018 (promisc ? IFF_PROMISC : 0);
1019
1020 if (enic->flags != flags) {
1021 enic->flags = flags;
1022 enic_dev_packet_filter(enic, directed,
1023 multicast, broadcast, promisc, allmulti);
1024 }
1025
1026 if (!promisc) {
e0afe53f 1027 enic_update_unicast_addr_list(enic);
319d7e84 1028 if (!allmulti)
e0afe53f 1029 enic_update_multicast_addr_list(enic);
319d7e84
RP
1030 }
1031}
1032
01f2e4ea
SF
1033/* rtnl lock is held */
1034static void enic_vlan_rx_register(struct net_device *netdev,
1035 struct vlan_group *vlan_group)
1036{
1037 struct enic *enic = netdev_priv(netdev);
1038 enic->vlan_group = vlan_group;
1039}
1040
01f2e4ea
SF
1041/* netif_tx_lock held, BHs disabled */
1042static void enic_tx_timeout(struct net_device *netdev)
1043{
1044 struct enic *enic = netdev_priv(netdev);
1045 schedule_work(&enic->reset);
1046}
1047
0b1c00fc
RP
1048static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1049{
1050 struct enic *enic = netdev_priv(netdev);
1051
1052 if (vf != PORT_SELF_VF)
1053 return -EOPNOTSUPP;
1054
1055 /* Ignore the vf argument for now. We can assume the request
1056 * is coming on a vf.
1057 */
1058 if (is_valid_ether_addr(mac)) {
1059 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1060 return 0;
1061 } else
1062 return -EINVAL;
1063}
1064
f8bd9091
SF
1065static int enic_set_vf_port(struct net_device *netdev, int vf,
1066 struct nlattr *port[])
1067{
1068 struct enic *enic = netdev_priv(netdev);
b3abfbd2
RP
1069 struct enic_port_profile prev_pp;
1070 int err = 0, restore_pp = 1;
08f382eb 1071
b3abfbd2
RP
1072 /* don't support VFs, yet */
1073 if (vf != PORT_SELF_VF)
1074 return -EOPNOTSUPP;
08f382eb 1075
b3abfbd2
RP
1076 if (!port[IFLA_PORT_REQUEST])
1077 return -EOPNOTSUPP;
1078
1079 memcpy(&prev_pp, &enic->pp, sizeof(enic->pp));
1080 memset(&enic->pp, 0, sizeof(enic->pp));
1081
1082 enic->pp.set |= ENIC_SET_REQUEST;
1083 enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
08f382eb
SF
1084
1085 if (port[IFLA_PORT_PROFILE]) {
b3abfbd2
RP
1086 enic->pp.set |= ENIC_SET_NAME;
1087 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
08f382eb
SF
1088 PORT_PROFILE_MAX);
1089 }
1090
1091 if (port[IFLA_PORT_INSTANCE_UUID]) {
b3abfbd2
RP
1092 enic->pp.set |= ENIC_SET_INSTANCE;
1093 memcpy(enic->pp.instance_uuid,
08f382eb
SF
1094 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1095 }
1096
1097 if (port[IFLA_PORT_HOST_UUID]) {
b3abfbd2
RP
1098 enic->pp.set |= ENIC_SET_HOST;
1099 memcpy(enic->pp.host_uuid,
08f382eb
SF
1100 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1101 }
f8bd9091 1102
b3abfbd2
RP
1103 /* Special case handling: mac came from IFLA_VF_MAC */
1104 if (!is_zero_ether_addr(prev_pp.vf_mac))
1105 memcpy(enic->pp.mac_addr, prev_pp.vf_mac, ETH_ALEN);
418c437d
SF
1106
1107 if (is_zero_ether_addr(netdev->dev_addr))
1108 random_ether_addr(netdev->dev_addr);
f8bd9091 1109
b3abfbd2
RP
1110 err = enic_process_set_pp_request(enic, &prev_pp, &restore_pp);
1111 if (err) {
1112 if (restore_pp) {
1113 /* Things are still the way they were: Implicit
1114 * DISASSOCIATE failed
1115 */
1116 memcpy(&enic->pp, &prev_pp, sizeof(enic->pp));
1117 } else {
1118 memset(&enic->pp, 0, sizeof(enic->pp));
1119 memset(netdev->dev_addr, 0, ETH_ALEN);
1120 }
1121 } else {
1122 /* Set flag to indicate that the port assoc/disassoc
1123 * request has been sent out to fw
1124 */
1125 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1126
1127 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1128 if (enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1129 memset(enic->pp.mac_addr, 0, ETH_ALEN);
1130 memset(netdev->dev_addr, 0, ETH_ALEN);
1131 }
1132 }
29639059 1133
29639059
RP
1134 memset(enic->pp.vf_mac, 0, ETH_ALEN);
1135
29639059 1136 return err;
f8bd9091
SF
1137}
1138
1139static int enic_get_vf_port(struct net_device *netdev, int vf,
1140 struct sk_buff *skb)
1141{
1142 struct enic *enic = netdev_priv(netdev);
f8bd9091 1143 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
b3abfbd2 1144 int err;
f8bd9091 1145
4dce2396 1146 if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
08f382eb 1147 return -ENODATA;
f8bd9091 1148
b3abfbd2 1149 err = enic_process_get_pp_request(enic, enic->pp.request, &response);
f8bd9091 1150 if (err)
b3abfbd2 1151 return err;
f8bd9091
SF
1152
1153 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1154 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
08f382eb
SF
1155 if (enic->pp.set & ENIC_SET_NAME)
1156 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1157 enic->pp.name);
1158 if (enic->pp.set & ENIC_SET_INSTANCE)
1159 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1160 enic->pp.instance_uuid);
1161 if (enic->pp.set & ENIC_SET_HOST)
1162 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1163 enic->pp.host_uuid);
f8bd9091
SF
1164
1165 return 0;
1166
1167nla_put_failure:
1168 return -EMSGSIZE;
1169}
1170
01f2e4ea
SF
1171static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1172{
1173 struct enic *enic = vnic_dev_priv(rq->vdev);
1174
1175 if (!buf->os_buf)
1176 return;
1177
1178 pci_unmap_single(enic->pdev, buf->dma_addr,
1179 buf->len, PCI_DMA_FROMDEVICE);
1180 dev_kfree_skb_any(buf->os_buf);
1181}
1182
01f2e4ea
SF
1183static int enic_rq_alloc_buf(struct vnic_rq *rq)
1184{
1185 struct enic *enic = vnic_dev_priv(rq->vdev);
d19e22dc 1186 struct net_device *netdev = enic->netdev;
01f2e4ea 1187 struct sk_buff *skb;
1825aca6 1188 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
01f2e4ea
SF
1189 unsigned int os_buf_index = 0;
1190 dma_addr_t dma_addr;
1191
89d71a66 1192 skb = netdev_alloc_skb_ip_align(netdev, len);
01f2e4ea
SF
1193 if (!skb)
1194 return -ENOMEM;
1195
1196 dma_addr = pci_map_single(enic->pdev, skb->data,
1197 len, PCI_DMA_FROMDEVICE);
1198
1199 enic_queue_rq_desc(rq, skb, os_buf_index,
1200 dma_addr, len);
1201
1202 return 0;
1203}
1204
01f2e4ea
SF
1205static void enic_rq_indicate_buf(struct vnic_rq *rq,
1206 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1207 int skipped, void *opaque)
1208{
1209 struct enic *enic = vnic_dev_priv(rq->vdev);
86ca9db7 1210 struct net_device *netdev = enic->netdev;
01f2e4ea
SF
1211 struct sk_buff *skb;
1212
1213 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1214 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1215 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1216 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1217 u8 packet_error;
f8cac14a 1218 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
01f2e4ea
SF
1219 u32 rss_hash;
1220
1221 if (skipped)
1222 return;
1223
1224 skb = buf->os_buf;
1225 prefetch(skb->data - NET_IP_ALIGN);
1226 pci_unmap_single(enic->pdev, buf->dma_addr,
1227 buf->len, PCI_DMA_FROMDEVICE);
1228
1229 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1230 &type, &color, &q_number, &completed_index,
1231 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1232 &csum_not_calc, &rss_hash, &bytes_written,
f8cac14a 1233 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
01f2e4ea
SF
1234 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1235 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1236 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1237 &fcs_ok);
1238
1239 if (packet_error) {
1240
350991e1
SF
1241 if (!fcs_ok) {
1242 if (bytes_written > 0)
1243 enic->rq_bad_fcs++;
1244 else if (bytes_written == 0)
1245 enic->rq_truncated_pkts++;
1246 }
01f2e4ea
SF
1247
1248 dev_kfree_skb_any(skb);
1249
1250 return;
1251 }
1252
1253 if (eop && bytes_written > 0) {
1254
1255 /* Good receive
1256 */
1257
1258 skb_put(skb, bytes_written);
86ca9db7 1259 skb->protocol = eth_type_trans(skb, netdev);
01f2e4ea 1260
5ec8f9b8 1261 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
01f2e4ea
SF
1262 skb->csum = htons(checksum);
1263 skb->ip_summed = CHECKSUM_COMPLETE;
1264 }
1265
86ca9db7 1266 skb->dev = netdev;
01f2e4ea 1267
f8cac14a
VK
1268 if (enic->vlan_group && vlan_stripped &&
1269 (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
01f2e4ea 1270
88132f55 1271 if (netdev->features & NETIF_F_GRO)
717258ba
VK
1272 vlan_gro_receive(&enic->napi[q_number],
1273 enic->vlan_group, vlan_tci, skb);
01f2e4ea
SF
1274 else
1275 vlan_hwaccel_receive_skb(skb,
f8cac14a 1276 enic->vlan_group, vlan_tci);
01f2e4ea
SF
1277
1278 } else {
1279
88132f55 1280 if (netdev->features & NETIF_F_GRO)
717258ba 1281 napi_gro_receive(&enic->napi[q_number], skb);
01f2e4ea
SF
1282 else
1283 netif_receive_skb(skb);
1284
1285 }
01f2e4ea
SF
1286 } else {
1287
1288 /* Buffer overflow
1289 */
1290
1291 dev_kfree_skb_any(skb);
1292 }
1293}
1294
1295static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1296 u8 type, u16 q_number, u16 completed_index, void *opaque)
1297{
1298 struct enic *enic = vnic_dev_priv(vdev);
1299
1300 vnic_rq_service(&enic->rq[q_number], cq_desc,
1301 completed_index, VNIC_RQ_RETURN_DESC,
1302 enic_rq_indicate_buf, opaque);
1303
1304 return 0;
1305}
1306
01f2e4ea
SF
1307static int enic_poll(struct napi_struct *napi, int budget)
1308{
717258ba
VK
1309 struct net_device *netdev = napi->dev;
1310 struct enic *enic = netdev_priv(netdev);
1311 unsigned int cq_rq = enic_cq_rq(enic, 0);
1312 unsigned int cq_wq = enic_cq_wq(enic, 0);
1313 unsigned int intr = enic_legacy_io_intr();
01f2e4ea
SF
1314 unsigned int rq_work_to_do = budget;
1315 unsigned int wq_work_to_do = -1; /* no limit */
1316 unsigned int work_done, rq_work_done, wq_work_done;
2d6ddced 1317 int err;
01f2e4ea
SF
1318
1319 /* Service RQ (first) and WQ
1320 */
1321
717258ba 1322 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
01f2e4ea
SF
1323 rq_work_to_do, enic_rq_service, NULL);
1324
717258ba 1325 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
01f2e4ea
SF
1326 wq_work_to_do, enic_wq_service, NULL);
1327
1328 /* Accumulate intr event credits for this polling
1329 * cycle. An intr event is the completion of a
1330 * a WQ or RQ packet.
1331 */
1332
1333 work_done = rq_work_done + wq_work_done;
1334
1335 if (work_done > 0)
717258ba 1336 vnic_intr_return_credits(&enic->intr[intr],
01f2e4ea
SF
1337 work_done,
1338 0 /* don't unmask intr */,
1339 0 /* don't reset intr timer */);
1340
0eb26022 1341 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
01f2e4ea 1342
2d6ddced
SF
1343 /* Buffer allocation failed. Stay in polling
1344 * mode so we can try to fill the ring again.
1345 */
01f2e4ea 1346
2d6ddced
SF
1347 if (err)
1348 rq_work_done = rq_work_to_do;
01f2e4ea 1349
2d6ddced 1350 if (rq_work_done < rq_work_to_do) {
01f2e4ea 1351
2d6ddced 1352 /* Some work done, but not enough to stay in polling,
88132f55 1353 * exit polling
01f2e4ea
SF
1354 */
1355
288379f0 1356 napi_complete(napi);
717258ba 1357 vnic_intr_unmask(&enic->intr[intr]);
01f2e4ea
SF
1358 }
1359
1360 return rq_work_done;
1361}
1362
1363static int enic_poll_msix(struct napi_struct *napi, int budget)
1364{
717258ba
VK
1365 struct net_device *netdev = napi->dev;
1366 struct enic *enic = netdev_priv(netdev);
1367 unsigned int rq = (napi - &enic->napi[0]);
1368 unsigned int cq = enic_cq_rq(enic, rq);
1369 unsigned int intr = enic_msix_rq_intr(enic, rq);
01f2e4ea
SF
1370 unsigned int work_to_do = budget;
1371 unsigned int work_done;
2d6ddced 1372 int err;
01f2e4ea
SF
1373
1374 /* Service RQ
1375 */
1376
717258ba 1377 work_done = vnic_cq_service(&enic->cq[cq],
01f2e4ea
SF
1378 work_to_do, enic_rq_service, NULL);
1379
2d6ddced
SF
1380 /* Return intr event credits for this polling
1381 * cycle. An intr event is the completion of a
1382 * RQ packet.
1383 */
01f2e4ea 1384
2d6ddced 1385 if (work_done > 0)
717258ba 1386 vnic_intr_return_credits(&enic->intr[intr],
01f2e4ea
SF
1387 work_done,
1388 0 /* don't unmask intr */,
1389 0 /* don't reset intr timer */);
01f2e4ea 1390
0eb26022 1391 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
2d6ddced
SF
1392
1393 /* Buffer allocation failed. Stay in polling mode
1394 * so we can try to fill the ring again.
1395 */
1396
1397 if (err)
1398 work_done = work_to_do;
1399
1400 if (work_done < work_to_do) {
1401
1402 /* Some work done, but not enough to stay in polling,
88132f55 1403 * exit polling
01f2e4ea
SF
1404 */
1405
288379f0 1406 napi_complete(napi);
717258ba 1407 vnic_intr_unmask(&enic->intr[intr]);
01f2e4ea
SF
1408 }
1409
1410 return work_done;
1411}
1412
1413static void enic_notify_timer(unsigned long data)
1414{
1415 struct enic *enic = (struct enic *)data;
1416
1417 enic_notify_check(enic);
1418
25f0a061
SF
1419 mod_timer(&enic->notify_timer,
1420 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
01f2e4ea
SF
1421}
1422
1423static void enic_free_intr(struct enic *enic)
1424{
1425 struct net_device *netdev = enic->netdev;
1426 unsigned int i;
1427
1428 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1429 case VNIC_DEV_INTR_MODE_INTX:
01f2e4ea
SF
1430 free_irq(enic->pdev->irq, netdev);
1431 break;
8f4d248c
SF
1432 case VNIC_DEV_INTR_MODE_MSI:
1433 free_irq(enic->pdev->irq, enic);
1434 break;
01f2e4ea
SF
1435 case VNIC_DEV_INTR_MODE_MSIX:
1436 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1437 if (enic->msix[i].requested)
1438 free_irq(enic->msix_entry[i].vector,
1439 enic->msix[i].devid);
1440 break;
1441 default:
1442 break;
1443 }
1444}
1445
1446static int enic_request_intr(struct enic *enic)
1447{
1448 struct net_device *netdev = enic->netdev;
717258ba 1449 unsigned int i, intr;
01f2e4ea
SF
1450 int err = 0;
1451
1452 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1453
1454 case VNIC_DEV_INTR_MODE_INTX:
1455
1456 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1457 IRQF_SHARED, netdev->name, netdev);
1458 break;
1459
1460 case VNIC_DEV_INTR_MODE_MSI:
1461
1462 err = request_irq(enic->pdev->irq, enic_isr_msi,
1463 0, netdev->name, enic);
1464 break;
1465
1466 case VNIC_DEV_INTR_MODE_MSIX:
1467
717258ba
VK
1468 for (i = 0; i < enic->rq_count; i++) {
1469 intr = enic_msix_rq_intr(enic, i);
1470 sprintf(enic->msix[intr].devname,
1471 "%.11s-rx-%d", netdev->name, i);
1472 enic->msix[intr].isr = enic_isr_msix_rq;
1473 enic->msix[intr].devid = &enic->napi[i];
1474 }
01f2e4ea 1475
717258ba
VK
1476 for (i = 0; i < enic->wq_count; i++) {
1477 intr = enic_msix_wq_intr(enic, i);
1478 sprintf(enic->msix[intr].devname,
1479 "%.11s-tx-%d", netdev->name, i);
1480 enic->msix[intr].isr = enic_isr_msix_wq;
1481 enic->msix[intr].devid = enic;
1482 }
01f2e4ea 1483
717258ba
VK
1484 intr = enic_msix_err_intr(enic);
1485 sprintf(enic->msix[intr].devname,
01f2e4ea 1486 "%.11s-err", netdev->name);
717258ba
VK
1487 enic->msix[intr].isr = enic_isr_msix_err;
1488 enic->msix[intr].devid = enic;
01f2e4ea 1489
717258ba
VK
1490 intr = enic_msix_notify_intr(enic);
1491 sprintf(enic->msix[intr].devname,
01f2e4ea 1492 "%.11s-notify", netdev->name);
717258ba
VK
1493 enic->msix[intr].isr = enic_isr_msix_notify;
1494 enic->msix[intr].devid = enic;
1495
1496 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1497 enic->msix[i].requested = 0;
01f2e4ea 1498
717258ba 1499 for (i = 0; i < enic->intr_count; i++) {
01f2e4ea
SF
1500 err = request_irq(enic->msix_entry[i].vector,
1501 enic->msix[i].isr, 0,
1502 enic->msix[i].devname,
1503 enic->msix[i].devid);
1504 if (err) {
1505 enic_free_intr(enic);
1506 break;
1507 }
1508 enic->msix[i].requested = 1;
1509 }
1510
1511 break;
1512
1513 default:
1514 break;
1515 }
1516
1517 return err;
1518}
1519
b3d18d19
SF
1520static void enic_synchronize_irqs(struct enic *enic)
1521{
1522 unsigned int i;
1523
1524 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1525 case VNIC_DEV_INTR_MODE_INTX:
1526 case VNIC_DEV_INTR_MODE_MSI:
1527 synchronize_irq(enic->pdev->irq);
1528 break;
1529 case VNIC_DEV_INTR_MODE_MSIX:
1530 for (i = 0; i < enic->intr_count; i++)
1531 synchronize_irq(enic->msix_entry[i].vector);
1532 break;
1533 default:
1534 break;
1535 }
1536}
1537
383ab92f 1538static int enic_dev_notify_set(struct enic *enic)
01f2e4ea
SF
1539{
1540 int err;
1541
56ac88b3 1542 spin_lock(&enic->devcmd_lock);
01f2e4ea
SF
1543 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1544 case VNIC_DEV_INTR_MODE_INTX:
717258ba
VK
1545 err = vnic_dev_notify_set(enic->vdev,
1546 enic_legacy_notify_intr());
01f2e4ea
SF
1547 break;
1548 case VNIC_DEV_INTR_MODE_MSIX:
717258ba
VK
1549 err = vnic_dev_notify_set(enic->vdev,
1550 enic_msix_notify_intr(enic));
01f2e4ea
SF
1551 break;
1552 default:
1553 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1554 break;
1555 }
56ac88b3 1556 spin_unlock(&enic->devcmd_lock);
01f2e4ea
SF
1557
1558 return err;
1559}
1560
1561static void enic_notify_timer_start(struct enic *enic)
1562{
1563 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1564 case VNIC_DEV_INTR_MODE_MSI:
1565 mod_timer(&enic->notify_timer, jiffies);
1566 break;
1567 default:
1568 /* Using intr for notification for INTx/MSI-X */
1569 break;
6403eab1 1570 }
01f2e4ea
SF
1571}
1572
1573/* rtnl lock is held, process context */
1574static int enic_open(struct net_device *netdev)
1575{
1576 struct enic *enic = netdev_priv(netdev);
1577 unsigned int i;
1578 int err;
1579
4b75a442
SF
1580 err = enic_request_intr(enic);
1581 if (err) {
a7a79deb 1582 netdev_err(netdev, "Unable to request irq.\n");
4b75a442
SF
1583 return err;
1584 }
1585
383ab92f 1586 err = enic_dev_notify_set(enic);
4b75a442 1587 if (err) {
a7a79deb
VK
1588 netdev_err(netdev,
1589 "Failed to alloc notify buffer, aborting.\n");
4b75a442
SF
1590 goto err_out_free_intr;
1591 }
1592
01f2e4ea 1593 for (i = 0; i < enic->rq_count; i++) {
0eb26022 1594 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
2d6ddced
SF
1595 /* Need at least one buffer on ring to get going */
1596 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
a7a79deb 1597 netdev_err(netdev, "Unable to alloc receive buffers\n");
2d6ddced 1598 err = -ENOMEM;
4b75a442 1599 goto err_out_notify_unset;
01f2e4ea
SF
1600 }
1601 }
1602
1603 for (i = 0; i < enic->wq_count; i++)
1604 vnic_wq_enable(&enic->wq[i]);
1605 for (i = 0; i < enic->rq_count; i++)
1606 vnic_rq_enable(&enic->rq[i]);
1607
29639059
RP
1608 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1609 enic_dev_add_addr(enic, enic->pp.mac_addr);
1610 else
1611 enic_dev_add_station_addr(enic);
319d7e84 1612 enic_set_rx_mode(netdev);
01f2e4ea
SF
1613
1614 netif_wake_queue(netdev);
717258ba
VK
1615
1616 for (i = 0; i < enic->rq_count; i++)
1617 napi_enable(&enic->napi[i]);
1618
383ab92f 1619 enic_dev_enable(enic);
01f2e4ea
SF
1620
1621 for (i = 0; i < enic->intr_count; i++)
1622 vnic_intr_unmask(&enic->intr[i]);
1623
1624 enic_notify_timer_start(enic);
1625
1626 return 0;
4b75a442
SF
1627
1628err_out_notify_unset:
383ab92f 1629 enic_dev_notify_unset(enic);
4b75a442
SF
1630err_out_free_intr:
1631 enic_free_intr(enic);
1632
1633 return err;
01f2e4ea
SF
1634}
1635
1636/* rtnl lock is held, process context */
1637static int enic_stop(struct net_device *netdev)
1638{
1639 struct enic *enic = netdev_priv(netdev);
1640 unsigned int i;
1641 int err;
1642
29046f9b 1643 for (i = 0; i < enic->intr_count; i++) {
b3d18d19 1644 vnic_intr_mask(&enic->intr[i]);
29046f9b
VK
1645 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1646 }
b3d18d19
SF
1647
1648 enic_synchronize_irqs(enic);
1649
01f2e4ea
SF
1650 del_timer_sync(&enic->notify_timer);
1651
383ab92f 1652 enic_dev_disable(enic);
717258ba
VK
1653
1654 for (i = 0; i < enic->rq_count; i++)
1655 napi_disable(&enic->napi[i]);
1656
b3d18d19
SF
1657 netif_carrier_off(netdev);
1658 netif_tx_disable(netdev);
29639059
RP
1659 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1660 enic_dev_del_addr(enic, enic->pp.mac_addr);
1661 else
1662 enic_dev_del_station_addr(enic);
f8bd9091 1663
01f2e4ea
SF
1664 for (i = 0; i < enic->wq_count; i++) {
1665 err = vnic_wq_disable(&enic->wq[i]);
1666 if (err)
1667 return err;
1668 }
1669 for (i = 0; i < enic->rq_count; i++) {
1670 err = vnic_rq_disable(&enic->rq[i]);
1671 if (err)
1672 return err;
1673 }
1674
383ab92f 1675 enic_dev_notify_unset(enic);
4b75a442
SF
1676 enic_free_intr(enic);
1677
01f2e4ea
SF
1678 for (i = 0; i < enic->wq_count; i++)
1679 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1680 for (i = 0; i < enic->rq_count; i++)
1681 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1682 for (i = 0; i < enic->cq_count; i++)
1683 vnic_cq_clean(&enic->cq[i]);
1684 for (i = 0; i < enic->intr_count; i++)
1685 vnic_intr_clean(&enic->intr[i]);
1686
1687 return 0;
1688}
1689
1690static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1691{
1692 struct enic *enic = netdev_priv(netdev);
1693 int running = netif_running(netdev);
1694
25f0a061
SF
1695 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1696 return -EINVAL;
1697
c97c894d
RP
1698 if (enic_is_dynamic(enic))
1699 return -EOPNOTSUPP;
1700
01f2e4ea
SF
1701 if (running)
1702 enic_stop(netdev);
1703
01f2e4ea
SF
1704 netdev->mtu = new_mtu;
1705
1706 if (netdev->mtu > enic->port_mtu)
a7a79deb
VK
1707 netdev_warn(netdev,
1708 "interface MTU (%d) set higher than port MTU (%d)\n",
1709 netdev->mtu, enic->port_mtu);
01f2e4ea
SF
1710
1711 if (running)
1712 enic_open(netdev);
1713
1714 return 0;
1715}
1716
c97c894d
RP
1717static void enic_change_mtu_work(struct work_struct *work)
1718{
1719 struct enic *enic = container_of(work, struct enic, change_mtu_work);
1720 struct net_device *netdev = enic->netdev;
1721 int new_mtu = vnic_dev_mtu(enic->vdev);
1722 int err;
1723 unsigned int i;
1724
1725 new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1726
1727 rtnl_lock();
1728
1729 /* Stop RQ */
1730 del_timer_sync(&enic->notify_timer);
1731
1732 for (i = 0; i < enic->rq_count; i++)
1733 napi_disable(&enic->napi[i]);
1734
1735 vnic_intr_mask(&enic->intr[0]);
1736 enic_synchronize_irqs(enic);
1737 err = vnic_rq_disable(&enic->rq[0]);
1738 if (err) {
1739 netdev_err(netdev, "Unable to disable RQ.\n");
1740 return;
1741 }
1742 vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1743 vnic_cq_clean(&enic->cq[0]);
1744 vnic_intr_clean(&enic->intr[0]);
1745
1746 /* Fill RQ with new_mtu-sized buffers */
1747 netdev->mtu = new_mtu;
1748 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1749 /* Need at least one buffer on ring to get going */
1750 if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1751 netdev_err(netdev, "Unable to alloc receive buffers.\n");
1752 return;
1753 }
1754
1755 /* Start RQ */
1756 vnic_rq_enable(&enic->rq[0]);
1757 napi_enable(&enic->napi[0]);
1758 vnic_intr_unmask(&enic->intr[0]);
1759 enic_notify_timer_start(enic);
1760
1761 rtnl_unlock();
1762
1763 netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1764}
1765
01f2e4ea
SF
1766#ifdef CONFIG_NET_POLL_CONTROLLER
1767static void enic_poll_controller(struct net_device *netdev)
1768{
1769 struct enic *enic = netdev_priv(netdev);
1770 struct vnic_dev *vdev = enic->vdev;
717258ba 1771 unsigned int i, intr;
01f2e4ea
SF
1772
1773 switch (vnic_dev_get_intr_mode(vdev)) {
1774 case VNIC_DEV_INTR_MODE_MSIX:
717258ba
VK
1775 for (i = 0; i < enic->rq_count; i++) {
1776 intr = enic_msix_rq_intr(enic, i);
79aeec58
VK
1777 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1778 &enic->napi[i]);
717258ba
VK
1779 }
1780 intr = enic_msix_wq_intr(enic, i);
1781 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
01f2e4ea
SF
1782 break;
1783 case VNIC_DEV_INTR_MODE_MSI:
1784 enic_isr_msi(enic->pdev->irq, enic);
1785 break;
1786 case VNIC_DEV_INTR_MODE_INTX:
1787 enic_isr_legacy(enic->pdev->irq, netdev);
1788 break;
1789 default:
1790 break;
1791 }
1792}
1793#endif
1794
1795static int enic_dev_wait(struct vnic_dev *vdev,
1796 int (*start)(struct vnic_dev *, int),
1797 int (*finished)(struct vnic_dev *, int *),
1798 int arg)
1799{
1800 unsigned long time;
1801 int done;
1802 int err;
1803
1804 BUG_ON(in_interrupt());
1805
1806 err = start(vdev, arg);
1807 if (err)
1808 return err;
1809
1810 /* Wait for func to complete...2 seconds max
1811 */
1812
1813 time = jiffies + (HZ * 2);
1814 do {
1815
1816 err = finished(vdev, &done);
1817 if (err)
1818 return err;
1819
1820 if (done)
1821 return 0;
1822
1823 schedule_timeout_uninterruptible(HZ / 10);
1824
1825 } while (time_after(time, jiffies));
1826
1827 return -ETIMEDOUT;
1828}
1829
1830static int enic_dev_open(struct enic *enic)
1831{
1832 int err;
1833
1834 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1835 vnic_dev_open_done, 0);
1836 if (err)
a7a79deb
VK
1837 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1838 err);
01f2e4ea
SF
1839
1840 return err;
1841}
1842
99ef5639 1843static int enic_dev_hang_reset(struct enic *enic)
01f2e4ea
SF
1844{
1845 int err;
1846
99ef5639
VK
1847 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1848 vnic_dev_hang_reset_done, 0);
01f2e4ea 1849 if (err)
a7a79deb
VK
1850 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1851 err);
01f2e4ea
SF
1852
1853 return err;
1854}
1855
717258ba
VK
1856static int enic_set_rsskey(struct enic *enic)
1857{
1f4f067f 1858 dma_addr_t rss_key_buf_pa;
717258ba
VK
1859 union vnic_rss_key *rss_key_buf_va = NULL;
1860 union vnic_rss_key rss_key = {
1861 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1862 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1863 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1864 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1865 };
1866 int err;
1867
1868 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1869 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1870 if (!rss_key_buf_va)
1871 return -ENOMEM;
1872
1873 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1874
1875 spin_lock(&enic->devcmd_lock);
1876 err = enic_set_rss_key(enic,
1877 rss_key_buf_pa,
1878 sizeof(union vnic_rss_key));
1879 spin_unlock(&enic->devcmd_lock);
1880
1881 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1882 rss_key_buf_va, rss_key_buf_pa);
1883
1884 return err;
1885}
1886
1887static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1888{
1f4f067f 1889 dma_addr_t rss_cpu_buf_pa;
717258ba
VK
1890 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1891 unsigned int i;
1892 int err;
1893
1894 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1895 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1896 if (!rss_cpu_buf_va)
1897 return -ENOMEM;
1898
1899 for (i = 0; i < (1 << rss_hash_bits); i++)
1900 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1901
1902 spin_lock(&enic->devcmd_lock);
1903 err = enic_set_rss_cpu(enic,
1904 rss_cpu_buf_pa,
1905 sizeof(union vnic_rss_cpu));
1906 spin_unlock(&enic->devcmd_lock);
1907
1908 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1909 rss_cpu_buf_va, rss_cpu_buf_pa);
1910
1911 return err;
1912}
1913
1914static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1915 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
68f71708 1916{
68f71708
SF
1917 const u8 tso_ipid_split_en = 0;
1918 const u8 ig_vlan_strip_en = 1;
383ab92f 1919 int err;
68f71708 1920
717258ba
VK
1921 /* Enable VLAN tag stripping.
1922 */
68f71708 1923
383ab92f
VK
1924 spin_lock(&enic->devcmd_lock);
1925 err = enic_set_nic_cfg(enic,
68f71708
SF
1926 rss_default_cpu, rss_hash_type,
1927 rss_hash_bits, rss_base_cpu,
1928 rss_enable, tso_ipid_split_en,
1929 ig_vlan_strip_en);
383ab92f
VK
1930 spin_unlock(&enic->devcmd_lock);
1931
1932 return err;
1933}
1934
717258ba
VK
1935static int enic_set_rss_nic_cfg(struct enic *enic)
1936{
1937 struct device *dev = enic_get_dev(enic);
1938 const u8 rss_default_cpu = 0;
1939 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1940 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1941 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1942 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1943 const u8 rss_hash_bits = 7;
1944 const u8 rss_base_cpu = 0;
1945 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1946
1947 if (rss_enable) {
1948 if (!enic_set_rsskey(enic)) {
1949 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1950 rss_enable = 0;
1951 dev_warn(dev, "RSS disabled, "
1952 "Failed to set RSS cpu indirection table.");
1953 }
1954 } else {
1955 rss_enable = 0;
1956 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1957 }
1958 }
1959
1960 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1961 rss_hash_bits, rss_base_cpu, rss_enable);
f8cac14a
VK
1962}
1963
01f2e4ea
SF
1964static void enic_reset(struct work_struct *work)
1965{
1966 struct enic *enic = container_of(work, struct enic, reset);
1967
1968 if (!netif_running(enic->netdev))
1969 return;
1970
1971 rtnl_lock();
1972
383ab92f 1973 enic_dev_hang_notify(enic);
01f2e4ea 1974 enic_stop(enic->netdev);
99ef5639 1975 enic_dev_hang_reset(enic);
e0afe53f 1976 enic_reset_addr_lists(enic);
01f2e4ea 1977 enic_init_vnic_resources(enic);
717258ba 1978 enic_set_rss_nic_cfg(enic);
f8cac14a 1979 enic_dev_set_ig_vlan_rewrite_mode(enic);
01f2e4ea
SF
1980 enic_open(enic->netdev);
1981
1982 rtnl_unlock();
1983}
1984
1985static int enic_set_intr_mode(struct enic *enic)
1986{
717258ba 1987 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
1cbb1a61 1988 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
01f2e4ea
SF
1989 unsigned int i;
1990
1991 /* Set interrupt mode (INTx, MSI, MSI-X) depending
717258ba 1992 * on system capabilities.
01f2e4ea
SF
1993 *
1994 * Try MSI-X first
1995 *
1996 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1997 * (the second to last INTR is used for WQ/RQ errors)
1998 * (the last INTR is used for notifications)
1999 */
2000
2001 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2002 for (i = 0; i < n + m + 2; i++)
2003 enic->msix_entry[i].entry = i;
2004
717258ba
VK
2005 /* Use multiple RQs if RSS is enabled
2006 */
2007
2008 if (ENIC_SETTING(enic, RSS) &&
2009 enic->config.intr_mode < 1 &&
01f2e4ea
SF
2010 enic->rq_count >= n &&
2011 enic->wq_count >= m &&
2012 enic->cq_count >= n + m &&
717258ba 2013 enic->intr_count >= n + m + 2) {
01f2e4ea 2014
717258ba 2015 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
01f2e4ea 2016
717258ba
VK
2017 enic->rq_count = n;
2018 enic->wq_count = m;
2019 enic->cq_count = n + m;
2020 enic->intr_count = n + m + 2;
01f2e4ea 2021
717258ba
VK
2022 vnic_dev_set_intr_mode(enic->vdev,
2023 VNIC_DEV_INTR_MODE_MSIX);
2024
2025 return 0;
2026 }
2027 }
2028
2029 if (enic->config.intr_mode < 1 &&
2030 enic->rq_count >= 1 &&
2031 enic->wq_count >= m &&
2032 enic->cq_count >= 1 + m &&
2033 enic->intr_count >= 1 + m + 2) {
2034 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2035
2036 enic->rq_count = 1;
2037 enic->wq_count = m;
2038 enic->cq_count = 1 + m;
2039 enic->intr_count = 1 + m + 2;
2040
2041 vnic_dev_set_intr_mode(enic->vdev,
2042 VNIC_DEV_INTR_MODE_MSIX);
2043
2044 return 0;
2045 }
01f2e4ea
SF
2046 }
2047
2048 /* Next try MSI
2049 *
2050 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2051 */
2052
2053 if (enic->config.intr_mode < 2 &&
2054 enic->rq_count >= 1 &&
2055 enic->wq_count >= 1 &&
2056 enic->cq_count >= 2 &&
2057 enic->intr_count >= 1 &&
2058 !pci_enable_msi(enic->pdev)) {
2059
2060 enic->rq_count = 1;
2061 enic->wq_count = 1;
2062 enic->cq_count = 2;
2063 enic->intr_count = 1;
2064
2065 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2066
2067 return 0;
2068 }
2069
2070 /* Next try INTx
2071 *
2072 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2073 * (the first INTR is used for WQ/RQ)
2074 * (the second INTR is used for WQ/RQ errors)
2075 * (the last INTR is used for notifications)
2076 */
2077
2078 if (enic->config.intr_mode < 3 &&
2079 enic->rq_count >= 1 &&
2080 enic->wq_count >= 1 &&
2081 enic->cq_count >= 2 &&
2082 enic->intr_count >= 3) {
2083
2084 enic->rq_count = 1;
2085 enic->wq_count = 1;
2086 enic->cq_count = 2;
2087 enic->intr_count = 3;
2088
2089 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2090
2091 return 0;
2092 }
2093
2094 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2095
2096 return -EINVAL;
2097}
2098
2099static void enic_clear_intr_mode(struct enic *enic)
2100{
2101 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2102 case VNIC_DEV_INTR_MODE_MSIX:
2103 pci_disable_msix(enic->pdev);
2104 break;
2105 case VNIC_DEV_INTR_MODE_MSI:
2106 pci_disable_msi(enic->pdev);
2107 break;
2108 default:
2109 break;
2110 }
2111
2112 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2113}
2114
f8bd9091
SF
2115static const struct net_device_ops enic_netdev_dynamic_ops = {
2116 .ndo_open = enic_open,
2117 .ndo_stop = enic_stop,
2118 .ndo_start_xmit = enic_hard_start_xmit,
2119 .ndo_get_stats = enic_get_stats,
2120 .ndo_validate_addr = eth_validate_addr,
319d7e84
RP
2121 .ndo_set_rx_mode = enic_set_rx_mode,
2122 .ndo_set_multicast_list = enic_set_rx_mode,
f8bd9091
SF
2123 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2124 .ndo_change_mtu = enic_change_mtu,
2125 .ndo_vlan_rx_register = enic_vlan_rx_register,
2126 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2127 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2128 .ndo_tx_timeout = enic_tx_timeout,
2129 .ndo_set_vf_port = enic_set_vf_port,
2130 .ndo_get_vf_port = enic_get_vf_port,
0b1c00fc 2131 .ndo_set_vf_mac = enic_set_vf_mac,
f8bd9091
SF
2132#ifdef CONFIG_NET_POLL_CONTROLLER
2133 .ndo_poll_controller = enic_poll_controller,
2134#endif
2135};
2136
afe29f7a
SH
2137static const struct net_device_ops enic_netdev_ops = {
2138 .ndo_open = enic_open,
2139 .ndo_stop = enic_stop,
00829823 2140 .ndo_start_xmit = enic_hard_start_xmit,
afe29f7a
SH
2141 .ndo_get_stats = enic_get_stats,
2142 .ndo_validate_addr = eth_validate_addr,
f8bd9091 2143 .ndo_set_mac_address = enic_set_mac_address,
319d7e84
RP
2144 .ndo_set_rx_mode = enic_set_rx_mode,
2145 .ndo_set_multicast_list = enic_set_rx_mode,
afe29f7a
SH
2146 .ndo_change_mtu = enic_change_mtu,
2147 .ndo_vlan_rx_register = enic_vlan_rx_register,
2148 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2149 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2150 .ndo_tx_timeout = enic_tx_timeout,
2151#ifdef CONFIG_NET_POLL_CONTROLLER
2152 .ndo_poll_controller = enic_poll_controller,
2153#endif
2154};
2155
2fdba388 2156static void enic_dev_deinit(struct enic *enic)
6fdfa970 2157{
717258ba
VK
2158 unsigned int i;
2159
2160 for (i = 0; i < enic->rq_count; i++)
2161 netif_napi_del(&enic->napi[i]);
2162
6fdfa970
SF
2163 enic_free_vnic_resources(enic);
2164 enic_clear_intr_mode(enic);
2165}
2166
2fdba388 2167static int enic_dev_init(struct enic *enic)
6fdfa970 2168{
a7a79deb 2169 struct device *dev = enic_get_dev(enic);
6fdfa970 2170 struct net_device *netdev = enic->netdev;
717258ba 2171 unsigned int i;
6fdfa970
SF
2172 int err;
2173
2174 /* Get vNIC configuration
2175 */
2176
2177 err = enic_get_vnic_config(enic);
2178 if (err) {
a7a79deb 2179 dev_err(dev, "Get vNIC configuration failed, aborting\n");
6fdfa970
SF
2180 return err;
2181 }
2182
2183 /* Get available resource counts
2184 */
2185
2186 enic_get_res_counts(enic);
2187
2188 /* Set interrupt mode based on resource counts and system
2189 * capabilities
2190 */
2191
2192 err = enic_set_intr_mode(enic);
2193 if (err) {
a7a79deb
VK
2194 dev_err(dev, "Failed to set intr mode based on resource "
2195 "counts and system capabilities, aborting\n");
6fdfa970
SF
2196 return err;
2197 }
2198
2199 /* Allocate and configure vNIC resources
2200 */
2201
2202 err = enic_alloc_vnic_resources(enic);
2203 if (err) {
a7a79deb 2204 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
6fdfa970
SF
2205 goto err_out_free_vnic_resources;
2206 }
2207
2208 enic_init_vnic_resources(enic);
2209
717258ba 2210 err = enic_set_rss_nic_cfg(enic);
6fdfa970 2211 if (err) {
a7a79deb 2212 dev_err(dev, "Failed to config nic, aborting\n");
6fdfa970
SF
2213 goto err_out_free_vnic_resources;
2214 }
2215
2216 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2217 default:
717258ba 2218 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
6fdfa970
SF
2219 break;
2220 case VNIC_DEV_INTR_MODE_MSIX:
717258ba
VK
2221 for (i = 0; i < enic->rq_count; i++)
2222 netif_napi_add(netdev, &enic->napi[i],
2223 enic_poll_msix, 64);
6fdfa970
SF
2224 break;
2225 }
2226
2227 return 0;
2228
2229err_out_free_vnic_resources:
2230 enic_clear_intr_mode(enic);
2231 enic_free_vnic_resources(enic);
2232
2233 return err;
2234}
2235
27e6c7d3
SF
2236static void enic_iounmap(struct enic *enic)
2237{
2238 unsigned int i;
2239
2240 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2241 if (enic->bar[i].vaddr)
2242 iounmap(enic->bar[i].vaddr);
2243}
2244
01f2e4ea
SF
2245static int __devinit enic_probe(struct pci_dev *pdev,
2246 const struct pci_device_id *ent)
2247{
a7a79deb 2248 struct device *dev = &pdev->dev;
01f2e4ea
SF
2249 struct net_device *netdev;
2250 struct enic *enic;
2251 int using_dac = 0;
2252 unsigned int i;
2253 int err;
2254
01f2e4ea
SF
2255 /* Allocate net device structure and initialize. Private
2256 * instance data is initialized to zero.
2257 */
2258
2259 netdev = alloc_etherdev(sizeof(struct enic));
2260 if (!netdev) {
a7a79deb 2261 pr_err("Etherdev alloc failed, aborting\n");
01f2e4ea
SF
2262 return -ENOMEM;
2263 }
2264
01f2e4ea
SF
2265 pci_set_drvdata(pdev, netdev);
2266
2267 SET_NETDEV_DEV(netdev, &pdev->dev);
2268
2269 enic = netdev_priv(netdev);
2270 enic->netdev = netdev;
2271 enic->pdev = pdev;
2272
2273 /* Setup PCI resources
2274 */
2275
29046f9b 2276 err = pci_enable_device_mem(pdev);
01f2e4ea 2277 if (err) {
a7a79deb 2278 dev_err(dev, "Cannot enable PCI device, aborting\n");
01f2e4ea
SF
2279 goto err_out_free_netdev;
2280 }
2281
2282 err = pci_request_regions(pdev, DRV_NAME);
2283 if (err) {
a7a79deb 2284 dev_err(dev, "Cannot request PCI regions, aborting\n");
01f2e4ea
SF
2285 goto err_out_disable_device;
2286 }
2287
2288 pci_set_master(pdev);
2289
2290 /* Query PCI controller on system for DMA addressing
2291 * limitation for the device. Try 40-bit first, and
2292 * fail to 32-bit.
2293 */
2294
50cf156a 2295 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
01f2e4ea 2296 if (err) {
284901a9 2297 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
01f2e4ea 2298 if (err) {
a7a79deb 2299 dev_err(dev, "No usable DMA configuration, aborting\n");
01f2e4ea
SF
2300 goto err_out_release_regions;
2301 }
284901a9 2302 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
01f2e4ea 2303 if (err) {
a7a79deb
VK
2304 dev_err(dev, "Unable to obtain %u-bit DMA "
2305 "for consistent allocations, aborting\n", 32);
01f2e4ea
SF
2306 goto err_out_release_regions;
2307 }
2308 } else {
50cf156a 2309 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
01f2e4ea 2310 if (err) {
a7a79deb
VK
2311 dev_err(dev, "Unable to obtain %u-bit DMA "
2312 "for consistent allocations, aborting\n", 40);
01f2e4ea
SF
2313 goto err_out_release_regions;
2314 }
2315 using_dac = 1;
2316 }
2317
27e6c7d3 2318 /* Map vNIC resources from BAR0-5
01f2e4ea
SF
2319 */
2320
27e6c7d3
SF
2321 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2322 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2323 continue;
2324 enic->bar[i].len = pci_resource_len(pdev, i);
2325 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2326 if (!enic->bar[i].vaddr) {
a7a79deb 2327 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
27e6c7d3
SF
2328 err = -ENODEV;
2329 goto err_out_iounmap;
2330 }
2331 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
01f2e4ea
SF
2332 }
2333
2334 /* Register vNIC device
2335 */
2336
27e6c7d3
SF
2337 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2338 ARRAY_SIZE(enic->bar));
01f2e4ea 2339 if (!enic->vdev) {
a7a79deb 2340 dev_err(dev, "vNIC registration failed, aborting\n");
01f2e4ea
SF
2341 err = -ENODEV;
2342 goto err_out_iounmap;
2343 }
2344
2345 /* Issue device open to get device in known state
2346 */
2347
2348 err = enic_dev_open(enic);
2349 if (err) {
a7a79deb 2350 dev_err(dev, "vNIC dev open failed, aborting\n");
01f2e4ea
SF
2351 goto err_out_vnic_unregister;
2352 }
2353
69161425
VK
2354 /* Setup devcmd lock
2355 */
2356
2357 spin_lock_init(&enic->devcmd_lock);
2358
2359 /*
2360 * Set ingress vlan rewrite mode before vnic initialization
2361 */
2362
2363 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2364 if (err) {
2365 dev_err(dev,
2366 "Failed to set ingress vlan rewrite mode, aborting.\n");
2367 goto err_out_dev_close;
2368 }
2369
01f2e4ea
SF
2370 /* Issue device init to initialize the vnic-to-switch link.
2371 * We'll start with carrier off and wait for link UP
2372 * notification later to turn on carrier. We don't need
2373 * to wait here for the vnic-to-switch link initialization
2374 * to complete; link UP notification is the indication that
2375 * the process is complete.
2376 */
2377
2378 netif_carrier_off(netdev);
2379
a7a79deb
VK
2380 /* Do not call dev_init for a dynamic vnic.
2381 * For a dynamic vnic, init_prov_info will be
2382 * called later by an upper layer.
2383 */
2384
f8bd9091
SF
2385 if (!enic_is_dynamic(enic)) {
2386 err = vnic_dev_init(enic->vdev, 0);
2387 if (err) {
a7a79deb 2388 dev_err(dev, "vNIC dev init failed, aborting\n");
f8bd9091
SF
2389 goto err_out_dev_close;
2390 }
01f2e4ea
SF
2391 }
2392
6fdfa970 2393 err = enic_dev_init(enic);
01f2e4ea 2394 if (err) {
a7a79deb 2395 dev_err(dev, "Device initialization failed, aborting\n");
01f2e4ea
SF
2396 goto err_out_dev_close;
2397 }
2398
383ab92f 2399 /* Setup notification timer, HW reset task, and wq locks
01f2e4ea
SF
2400 */
2401
2402 init_timer(&enic->notify_timer);
2403 enic->notify_timer.function = enic_notify_timer;
2404 enic->notify_timer.data = (unsigned long)enic;
2405
2406 INIT_WORK(&enic->reset, enic_reset);
c97c894d 2407 INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
01f2e4ea
SF
2408
2409 for (i = 0; i < enic->wq_count; i++)
2410 spin_lock_init(&enic->wq_lock[i]);
2411
01f2e4ea
SF
2412 /* Register net device
2413 */
2414
2415 enic->port_mtu = enic->config.mtu;
2416 (void)enic_change_mtu(netdev, enic->port_mtu);
2417
2418 err = enic_set_mac_addr(netdev, enic->mac_addr);
2419 if (err) {
a7a79deb 2420 dev_err(dev, "Invalid MAC address, aborting\n");
6fdfa970 2421 goto err_out_dev_deinit;
01f2e4ea
SF
2422 }
2423
7c844599
SF
2424 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2425 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2426
f8bd9091
SF
2427 if (enic_is_dynamic(enic))
2428 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2429 else
2430 netdev->netdev_ops = &enic_netdev_ops;
2431
01f2e4ea
SF
2432 netdev->watchdog_timeo = 2 * HZ;
2433 netdev->ethtool_ops = &enic_ethtool_ops;
01f2e4ea 2434
73c1ea9b 2435 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1825aca6
VK
2436 if (ENIC_SETTING(enic, LOOP)) {
2437 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2438 enic->loop_enable = 1;
2439 enic->loop_tag = enic->config.loop_tag;
2440 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2441 }
01f2e4ea 2442 if (ENIC_SETTING(enic, TXCSUM))
5ec8f9b8 2443 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
01f2e4ea 2444 if (ENIC_SETTING(enic, TSO))
5ec8f9b8 2445 netdev->hw_features |= NETIF_F_TSO |
01f2e4ea 2446 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
5ec8f9b8
MM
2447 if (ENIC_SETTING(enic, RXCSUM))
2448 netdev->hw_features |= NETIF_F_RXCSUM;
2449
2450 netdev->features |= netdev->hw_features;
2451
01f2e4ea
SF
2452 if (using_dac)
2453 netdev->features |= NETIF_F_HIGHDMA;
2454
01f2e4ea
SF
2455 err = register_netdev(netdev);
2456 if (err) {
a7a79deb 2457 dev_err(dev, "Cannot register net device, aborting\n");
6fdfa970 2458 goto err_out_dev_deinit;
01f2e4ea
SF
2459 }
2460
2461 return 0;
2462
6fdfa970
SF
2463err_out_dev_deinit:
2464 enic_dev_deinit(enic);
01f2e4ea
SF
2465err_out_dev_close:
2466 vnic_dev_close(enic->vdev);
2467err_out_vnic_unregister:
01f2e4ea
SF
2468 vnic_dev_unregister(enic->vdev);
2469err_out_iounmap:
2470 enic_iounmap(enic);
2471err_out_release_regions:
2472 pci_release_regions(pdev);
2473err_out_disable_device:
2474 pci_disable_device(pdev);
2475err_out_free_netdev:
2476 pci_set_drvdata(pdev, NULL);
2477 free_netdev(netdev);
2478
2479 return err;
2480}
2481
2482static void __devexit enic_remove(struct pci_dev *pdev)
2483{
2484 struct net_device *netdev = pci_get_drvdata(pdev);
2485
2486 if (netdev) {
2487 struct enic *enic = netdev_priv(netdev);
2488
23f333a2 2489 cancel_work_sync(&enic->reset);
c97c894d 2490 cancel_work_sync(&enic->change_mtu_work);
01f2e4ea 2491 unregister_netdev(netdev);
6fdfa970 2492 enic_dev_deinit(enic);
01f2e4ea 2493 vnic_dev_close(enic->vdev);
01f2e4ea
SF
2494 vnic_dev_unregister(enic->vdev);
2495 enic_iounmap(enic);
2496 pci_release_regions(pdev);
2497 pci_disable_device(pdev);
2498 pci_set_drvdata(pdev, NULL);
2499 free_netdev(netdev);
2500 }
2501}
2502
2503static struct pci_driver enic_driver = {
2504 .name = DRV_NAME,
2505 .id_table = enic_id_table,
2506 .probe = enic_probe,
2507 .remove = __devexit_p(enic_remove),
2508};
2509
2510static int __init enic_init_module(void)
2511{
a7a79deb 2512 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
01f2e4ea
SF
2513
2514 return pci_register_driver(&enic_driver);
2515}
2516
2517static void __exit enic_cleanup_module(void)
2518{
2519 pci_unregister_driver(&enic_driver);
2520}
2521
2522module_init(enic_init_module);
2523module_exit(enic_cleanup_module);