blob: 3893370d95a8bd3f2424f4a46456fa0f2ab1ac66 [file] [log] [blame]
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001/*
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002 * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07003 * 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>
Vasanthy Kolluri29046f92010-06-24 10:52:26 +000037#include <linux/rtnetlink.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070038#include <net/ip6_checksum.h>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070039
40#include "cq_enet_desc.h"
41#include "vnic_dev.h"
42#include "vnic_intr.h"
43#include "vnic_stats.h"
Scott Feldmanf8bd9092010-05-17 22:50:19 -070044#include "vnic_vic.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070045#include "enic_res.h"
46#include "enic.h"
Vasanthy Kolluri51987462011-02-04 16:17:05 +000047#include "enic_dev.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070048
49#define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
Scott Feldmanea0d7d92009-09-03 17:02:03 +000050#define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
51#define MAX_TSO (1 << 16)
52#define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
53
54#define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
Scott Feldmanf8bd9092010-05-17 22:50:19 -070055#define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070056
57/* Supported devices */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000058static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
Scott Feldmanea0d7d92009-09-03 17:02:03 +000059 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
Scott Feldmanf8bd9092010-05-17 22:50:19 -070060 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
Scott Feldman01f2e4e2008-09-15 09:17:11 -070061 { 0, } /* end of table */
62};
63
64MODULE_DESCRIPTION(DRV_DESCRIPTION);
65MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
66MODULE_LICENSE("GPL");
67MODULE_VERSION(DRV_VERSION);
68MODULE_DEVICE_TABLE(pci, enic_id_table);
69
70struct enic_stat {
71 char name[ETH_GSTRING_LEN];
72 unsigned int offset;
73};
74
75#define ENIC_TX_STAT(stat) \
76 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
77#define ENIC_RX_STAT(stat) \
78 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
79
80static const struct enic_stat enic_tx_stats[] = {
81 ENIC_TX_STAT(tx_frames_ok),
82 ENIC_TX_STAT(tx_unicast_frames_ok),
83 ENIC_TX_STAT(tx_multicast_frames_ok),
84 ENIC_TX_STAT(tx_broadcast_frames_ok),
85 ENIC_TX_STAT(tx_bytes_ok),
86 ENIC_TX_STAT(tx_unicast_bytes_ok),
87 ENIC_TX_STAT(tx_multicast_bytes_ok),
88 ENIC_TX_STAT(tx_broadcast_bytes_ok),
89 ENIC_TX_STAT(tx_drops),
90 ENIC_TX_STAT(tx_errors),
91 ENIC_TX_STAT(tx_tso),
92};
93
94static const struct enic_stat enic_rx_stats[] = {
95 ENIC_RX_STAT(rx_frames_ok),
96 ENIC_RX_STAT(rx_frames_total),
97 ENIC_RX_STAT(rx_unicast_frames_ok),
98 ENIC_RX_STAT(rx_multicast_frames_ok),
99 ENIC_RX_STAT(rx_broadcast_frames_ok),
100 ENIC_RX_STAT(rx_bytes_ok),
101 ENIC_RX_STAT(rx_unicast_bytes_ok),
102 ENIC_RX_STAT(rx_multicast_bytes_ok),
103 ENIC_RX_STAT(rx_broadcast_bytes_ok),
104 ENIC_RX_STAT(rx_drop),
105 ENIC_RX_STAT(rx_no_bufs),
106 ENIC_RX_STAT(rx_errors),
107 ENIC_RX_STAT(rx_rss),
108 ENIC_RX_STAT(rx_crc_errors),
109 ENIC_RX_STAT(rx_frames_64),
110 ENIC_RX_STAT(rx_frames_127),
111 ENIC_RX_STAT(rx_frames_255),
112 ENIC_RX_STAT(rx_frames_511),
113 ENIC_RX_STAT(rx_frames_1023),
114 ENIC_RX_STAT(rx_frames_1518),
115 ENIC_RX_STAT(rx_frames_to_max),
116};
117
118static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
119static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
120
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700121static int enic_is_dynamic(struct enic *enic)
122{
123 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
124}
125
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000126static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
127{
128 return rq;
129}
130
131static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
132{
133 return enic->rq_count + wq;
134}
135
136static inline unsigned int enic_legacy_io_intr(void)
137{
138 return 0;
139}
140
141static inline unsigned int enic_legacy_err_intr(void)
142{
143 return 1;
144}
145
146static inline unsigned int enic_legacy_notify_intr(void)
147{
148 return 2;
149}
150
151static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
152{
153 return rq;
154}
155
156static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
157{
158 return enic->rq_count + wq;
159}
160
161static inline unsigned int enic_msix_err_intr(struct enic *enic)
162{
163 return enic->rq_count + enic->wq_count;
164}
165
166static inline unsigned int enic_msix_notify_intr(struct enic *enic)
167{
168 return enic->rq_count + enic->wq_count + 1;
169}
170
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700171static int enic_get_settings(struct net_device *netdev,
172 struct ethtool_cmd *ecmd)
173{
174 struct enic *enic = netdev_priv(netdev);
175
176 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
177 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
178 ecmd->port = PORT_FIBRE;
179 ecmd->transceiver = XCVR_EXTERNAL;
180
181 if (netif_carrier_ok(netdev)) {
182 ecmd->speed = vnic_dev_port_speed(enic->vdev);
183 ecmd->duplex = DUPLEX_FULL;
184 } else {
185 ecmd->speed = -1;
186 ecmd->duplex = -1;
187 }
188
189 ecmd->autoneg = AUTONEG_DISABLE;
190
191 return 0;
192}
193
194static void enic_get_drvinfo(struct net_device *netdev,
195 struct ethtool_drvinfo *drvinfo)
196{
197 struct enic *enic = netdev_priv(netdev);
198 struct vnic_devcmd_fw_info *fw_info;
199
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000200 enic_dev_fw_info(enic, &fw_info);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700201
202 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
203 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
204 strncpy(drvinfo->fw_version, fw_info->fw_version,
205 sizeof(drvinfo->fw_version));
206 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
207 sizeof(drvinfo->bus_info));
208}
209
210static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
211{
212 unsigned int i;
213
214 switch (stringset) {
215 case ETH_SS_STATS:
216 for (i = 0; i < enic_n_tx_stats; i++) {
217 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
218 data += ETH_GSTRING_LEN;
219 }
220 for (i = 0; i < enic_n_rx_stats; i++) {
221 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
222 data += ETH_GSTRING_LEN;
223 }
224 break;
225 }
226}
227
Scott Feldman25f0a062008-09-24 11:23:32 -0700228static int enic_get_sset_count(struct net_device *netdev, int sset)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700229{
Scott Feldman25f0a062008-09-24 11:23:32 -0700230 switch (sset) {
231 case ETH_SS_STATS:
232 return enic_n_tx_stats + enic_n_rx_stats;
233 default:
234 return -EOPNOTSUPP;
235 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700236}
237
238static void enic_get_ethtool_stats(struct net_device *netdev,
239 struct ethtool_stats *stats, u64 *data)
240{
241 struct enic *enic = netdev_priv(netdev);
242 struct vnic_stats *vstats;
243 unsigned int i;
244
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000245 enic_dev_stats_dump(enic, &vstats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700246
247 for (i = 0; i < enic_n_tx_stats; i++)
248 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
249 for (i = 0; i < enic_n_rx_stats; i++)
250 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
251}
252
253static u32 enic_get_rx_csum(struct net_device *netdev)
254{
255 struct enic *enic = netdev_priv(netdev);
256 return enic->csum_rx_enabled;
257}
258
259static int enic_set_rx_csum(struct net_device *netdev, u32 data)
260{
261 struct enic *enic = netdev_priv(netdev);
262
Scott Feldman25f0a062008-09-24 11:23:32 -0700263 if (data && !ENIC_SETTING(enic, RXCSUM))
264 return -EINVAL;
265
266 enic->csum_rx_enabled = !!data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700267
268 return 0;
269}
270
271static int enic_set_tx_csum(struct net_device *netdev, u32 data)
272{
273 struct enic *enic = netdev_priv(netdev);
274
Scott Feldman25f0a062008-09-24 11:23:32 -0700275 if (data && !ENIC_SETTING(enic, TXCSUM))
276 return -EINVAL;
277
278 if (data)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700279 netdev->features |= NETIF_F_HW_CSUM;
280 else
281 netdev->features &= ~NETIF_F_HW_CSUM;
282
283 return 0;
284}
285
286static int enic_set_tso(struct net_device *netdev, u32 data)
287{
288 struct enic *enic = netdev_priv(netdev);
289
Scott Feldman25f0a062008-09-24 11:23:32 -0700290 if (data && !ENIC_SETTING(enic, TSO))
291 return -EINVAL;
292
293 if (data)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700294 netdev->features |=
295 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
296 else
297 netdev->features &=
298 ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
299
300 return 0;
301}
302
303static u32 enic_get_msglevel(struct net_device *netdev)
304{
305 struct enic *enic = netdev_priv(netdev);
306 return enic->msg_enable;
307}
308
309static void enic_set_msglevel(struct net_device *netdev, u32 value)
310{
311 struct enic *enic = netdev_priv(netdev);
312 enic->msg_enable = value;
313}
314
Scott Feldman7c844592009-12-23 13:27:54 +0000315static int enic_get_coalesce(struct net_device *netdev,
316 struct ethtool_coalesce *ecmd)
317{
318 struct enic *enic = netdev_priv(netdev);
319
320 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
321 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
322
323 return 0;
324}
325
326static int enic_set_coalesce(struct net_device *netdev,
327 struct ethtool_coalesce *ecmd)
328{
329 struct enic *enic = netdev_priv(netdev);
330 u32 tx_coalesce_usecs;
331 u32 rx_coalesce_usecs;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000332 unsigned int i, intr;
Scott Feldman7c844592009-12-23 13:27:54 +0000333
334 tx_coalesce_usecs = min_t(u32,
335 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
336 ecmd->tx_coalesce_usecs);
337 rx_coalesce_usecs = min_t(u32,
338 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
339 ecmd->rx_coalesce_usecs);
340
341 switch (vnic_dev_get_intr_mode(enic->vdev)) {
342 case VNIC_DEV_INTR_MODE_INTX:
343 if (tx_coalesce_usecs != rx_coalesce_usecs)
344 return -EINVAL;
345
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000346 intr = enic_legacy_io_intr();
347 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Scott Feldman7c844592009-12-23 13:27:54 +0000348 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
349 break;
350 case VNIC_DEV_INTR_MODE_MSI:
351 if (tx_coalesce_usecs != rx_coalesce_usecs)
352 return -EINVAL;
353
354 vnic_intr_coalescing_timer_set(&enic->intr[0],
355 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
356 break;
357 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000358 for (i = 0; i < enic->wq_count; i++) {
359 intr = enic_msix_wq_intr(enic, i);
360 vnic_intr_coalescing_timer_set(&enic->intr[intr],
361 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
362 }
363
364 for (i = 0; i < enic->rq_count; i++) {
365 intr = enic_msix_rq_intr(enic, i);
366 vnic_intr_coalescing_timer_set(&enic->intr[intr],
367 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
368 }
369
Scott Feldman7c844592009-12-23 13:27:54 +0000370 break;
371 default:
372 break;
373 }
374
375 enic->tx_coalesce_usecs = tx_coalesce_usecs;
376 enic->rx_coalesce_usecs = rx_coalesce_usecs;
377
378 return 0;
379}
380
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700381static const struct ethtool_ops enic_ethtool_ops = {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700382 .get_settings = enic_get_settings,
383 .get_drvinfo = enic_get_drvinfo,
384 .get_msglevel = enic_get_msglevel,
385 .set_msglevel = enic_set_msglevel,
386 .get_link = ethtool_op_get_link,
387 .get_strings = enic_get_strings,
Scott Feldman25f0a062008-09-24 11:23:32 -0700388 .get_sset_count = enic_get_sset_count,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700389 .get_ethtool_stats = enic_get_ethtool_stats,
390 .get_rx_csum = enic_get_rx_csum,
391 .set_rx_csum = enic_set_rx_csum,
392 .get_tx_csum = ethtool_op_get_tx_csum,
393 .set_tx_csum = enic_set_tx_csum,
394 .get_sg = ethtool_op_get_sg,
395 .set_sg = ethtool_op_set_sg,
396 .get_tso = ethtool_op_get_tso,
397 .set_tso = enic_set_tso,
Scott Feldman7c844592009-12-23 13:27:54 +0000398 .get_coalesce = enic_get_coalesce,
399 .set_coalesce = enic_set_coalesce,
Scott Feldman86ca9db2008-11-21 21:26:55 -0800400 .get_flags = ethtool_op_get_flags,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700401};
402
403static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
404{
405 struct enic *enic = vnic_dev_priv(wq->vdev);
406
407 if (buf->sop)
408 pci_unmap_single(enic->pdev, buf->dma_addr,
409 buf->len, PCI_DMA_TODEVICE);
410 else
411 pci_unmap_page(enic->pdev, buf->dma_addr,
412 buf->len, PCI_DMA_TODEVICE);
413
414 if (buf->os_buf)
415 dev_kfree_skb_any(buf->os_buf);
416}
417
418static void enic_wq_free_buf(struct vnic_wq *wq,
419 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
420{
421 enic_free_wq_buf(wq, buf);
422}
423
424static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
425 u8 type, u16 q_number, u16 completed_index, void *opaque)
426{
427 struct enic *enic = vnic_dev_priv(vdev);
428
429 spin_lock(&enic->wq_lock[q_number]);
430
431 vnic_wq_service(&enic->wq[q_number], cq_desc,
432 completed_index, enic_wq_free_buf,
433 opaque);
434
435 if (netif_queue_stopped(enic->netdev) &&
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000436 vnic_wq_desc_avail(&enic->wq[q_number]) >=
437 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700438 netif_wake_queue(enic->netdev);
439
440 spin_unlock(&enic->wq_lock[q_number]);
441
442 return 0;
443}
444
445static void enic_log_q_error(struct enic *enic)
446{
447 unsigned int i;
448 u32 error_status;
449
450 for (i = 0; i < enic->wq_count; i++) {
451 error_status = vnic_wq_error_status(&enic->wq[i]);
452 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000453 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
454 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700455 }
456
457 for (i = 0; i < enic->rq_count; i++) {
458 error_status = vnic_rq_error_status(&enic->rq[i]);
459 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000460 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
461 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700462 }
463}
464
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000465static void enic_msglvl_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700466{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000467 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700468
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000469 if (msg_enable != enic->msg_enable) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000470 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
471 enic->msg_enable, msg_enable);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000472 enic->msg_enable = msg_enable;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700473 }
474}
475
476static void enic_mtu_check(struct enic *enic)
477{
478 u32 mtu = vnic_dev_mtu(enic->vdev);
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000479 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700480
Scott Feldman491598a2009-09-03 17:02:40 +0000481 if (mtu && mtu != enic->port_mtu) {
Scott Feldman7c844592009-12-23 13:27:54 +0000482 enic->port_mtu = mtu;
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000483 if (mtu < netdev->mtu)
484 netdev_warn(netdev,
485 "interface MTU (%d) set higher "
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700486 "than switch port MTU (%d)\n",
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000487 netdev->mtu, mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700488 }
489}
490
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000491static void enic_link_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700492{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000493 int link_status = vnic_dev_link_status(enic->vdev);
494 int carrier_ok = netif_carrier_ok(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700495
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000496 if (link_status && !carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000497 netdev_info(enic->netdev, "Link UP\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000498 netif_carrier_on(enic->netdev);
499 } else if (!link_status && carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000500 netdev_info(enic->netdev, "Link DOWN\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000501 netif_carrier_off(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700502 }
503}
504
505static void enic_notify_check(struct enic *enic)
506{
507 enic_msglvl_check(enic);
508 enic_mtu_check(enic);
509 enic_link_check(enic);
510}
511
512#define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
513
514static irqreturn_t enic_isr_legacy(int irq, void *data)
515{
516 struct net_device *netdev = data;
517 struct enic *enic = netdev_priv(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000518 unsigned int io_intr = enic_legacy_io_intr();
519 unsigned int err_intr = enic_legacy_err_intr();
520 unsigned int notify_intr = enic_legacy_notify_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700521 u32 pba;
522
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000523 vnic_intr_mask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700524
525 pba = vnic_intr_legacy_pba(enic->legacy_pba);
526 if (!pba) {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000527 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700528 return IRQ_NONE; /* not our interrupt */
529 }
530
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000531 if (ENIC_TEST_INTR(pba, notify_intr)) {
532 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700533 enic_notify_check(enic);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800534 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700535
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000536 if (ENIC_TEST_INTR(pba, err_intr)) {
537 vnic_intr_return_all_credits(&enic->intr[err_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700538 enic_log_q_error(enic);
539 /* schedule recovery from WQ/RQ error */
540 schedule_work(&enic->reset);
541 return IRQ_HANDLED;
542 }
543
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000544 if (ENIC_TEST_INTR(pba, io_intr)) {
545 if (napi_schedule_prep(&enic->napi[0]))
546 __napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700547 } else {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000548 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700549 }
550
551 return IRQ_HANDLED;
552}
553
554static irqreturn_t enic_isr_msi(int irq, void *data)
555{
556 struct enic *enic = data;
557
558 /* With MSI, there is no sharing of interrupts, so this is
559 * our interrupt and there is no need to ack it. The device
560 * is not providing per-vector masking, so the OS will not
561 * write to PCI config space to mask/unmask the interrupt.
562 * We're using mask_on_assertion for MSI, so the device
563 * automatically masks the interrupt when the interrupt is
564 * generated. Later, when exiting polling, the interrupt
565 * will be unmasked (see enic_poll).
566 *
567 * Also, the device uses the same PCIe Traffic Class (TC)
568 * for Memory Write data and MSI, so there are no ordering
569 * issues; the MSI will always arrive at the Root Complex
570 * _after_ corresponding Memory Writes (i.e. descriptor
571 * writes).
572 */
573
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000574 napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700575
576 return IRQ_HANDLED;
577}
578
579static irqreturn_t enic_isr_msix_rq(int irq, void *data)
580{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000581 struct napi_struct *napi = data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700582
583 /* schedule NAPI polling for RQ cleanup */
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000584 napi_schedule(napi);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700585
586 return IRQ_HANDLED;
587}
588
589static irqreturn_t enic_isr_msix_wq(int irq, void *data)
590{
591 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000592 unsigned int cq = enic_cq_wq(enic, 0);
593 unsigned int intr = enic_msix_wq_intr(enic, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700594 unsigned int wq_work_to_do = -1; /* no limit */
595 unsigned int wq_work_done;
596
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000597 wq_work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700598 wq_work_to_do, enic_wq_service, NULL);
599
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000600 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700601 wq_work_done,
602 1 /* unmask intr */,
603 1 /* reset intr timer */);
604
605 return IRQ_HANDLED;
606}
607
608static irqreturn_t enic_isr_msix_err(int irq, void *data)
609{
610 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000611 unsigned int intr = enic_msix_err_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700612
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000613 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800614
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700615 enic_log_q_error(enic);
616
617 /* schedule recovery from WQ/RQ error */
618 schedule_work(&enic->reset);
619
620 return IRQ_HANDLED;
621}
622
623static irqreturn_t enic_isr_msix_notify(int irq, void *data)
624{
625 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000626 unsigned int intr = enic_msix_notify_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700627
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000628 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700629 enic_notify_check(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700630
631 return IRQ_HANDLED;
632}
633
634static inline void enic_queue_wq_skb_cont(struct enic *enic,
635 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000636 unsigned int len_left, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700637{
638 skb_frag_t *frag;
639
640 /* Queue additional data fragments */
641 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
642 len_left -= frag->size;
643 enic_queue_wq_desc_cont(wq, skb,
644 pci_map_page(enic->pdev, frag->page,
645 frag->page_offset, frag->size,
646 PCI_DMA_TODEVICE),
647 frag->size,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000648 (len_left == 0), /* EOP? */
649 loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700650 }
651}
652
653static inline void enic_queue_wq_skb_vlan(struct enic *enic,
654 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000655 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700656{
657 unsigned int head_len = skb_headlen(skb);
658 unsigned int len_left = skb->len - head_len;
659 int eop = (len_left == 0);
660
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000661 /* Queue the main skb fragment. The fragments are no larger
662 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
663 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
664 * per fragment is queued.
665 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700666 enic_queue_wq_desc(wq, skb,
667 pci_map_single(enic->pdev, skb->data,
668 head_len, PCI_DMA_TODEVICE),
669 head_len,
670 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000671 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700672
673 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000674 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700675}
676
677static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
678 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000679 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700680{
681 unsigned int head_len = skb_headlen(skb);
682 unsigned int len_left = skb->len - head_len;
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000683 unsigned int hdr_len = skb_checksum_start_offset(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700684 unsigned int csum_offset = hdr_len + skb->csum_offset;
685 int eop = (len_left == 0);
686
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000687 /* Queue the main skb fragment. The fragments are no larger
688 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
689 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
690 * per fragment is queued.
691 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700692 enic_queue_wq_desc_csum_l4(wq, skb,
693 pci_map_single(enic->pdev, skb->data,
694 head_len, PCI_DMA_TODEVICE),
695 head_len,
696 csum_offset,
697 hdr_len,
698 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000699 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700700
701 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000702 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700703}
704
705static inline void enic_queue_wq_skb_tso(struct enic *enic,
706 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000707 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700708{
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000709 unsigned int frag_len_left = skb_headlen(skb);
710 unsigned int len_left = skb->len - frag_len_left;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700711 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
712 int eop = (len_left == 0);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000713 unsigned int len;
714 dma_addr_t dma_addr;
715 unsigned int offset = 0;
716 skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700717
718 /* Preload TCP csum field with IP pseudo hdr calculated
719 * with IP length set to zero. HW will later add in length
720 * to each TCP segment resulting from the TSO.
721 */
722
Harvey Harrison09640e62009-02-01 00:45:17 -0800723 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700724 ip_hdr(skb)->check = 0;
725 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
726 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
Harvey Harrison09640e62009-02-01 00:45:17 -0800727 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700728 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
729 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
730 }
731
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000732 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
733 * for the main skb fragment
734 */
735 while (frag_len_left) {
736 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
737 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
738 len, PCI_DMA_TODEVICE);
739 enic_queue_wq_desc_tso(wq, skb,
740 dma_addr,
741 len,
742 mss, hdr_len,
743 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000744 eop && (len == frag_len_left), loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000745 frag_len_left -= len;
746 offset += len;
747 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700748
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000749 if (eop)
750 return;
751
752 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
753 * for additional data fragments
754 */
755 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
756 len_left -= frag->size;
757 frag_len_left = frag->size;
758 offset = frag->page_offset;
759
760 while (frag_len_left) {
761 len = min(frag_len_left,
762 (unsigned int)WQ_ENET_MAX_DESC_LEN);
763 dma_addr = pci_map_page(enic->pdev, frag->page,
764 offset, len,
765 PCI_DMA_TODEVICE);
766 enic_queue_wq_desc_cont(wq, skb,
767 dma_addr,
768 len,
769 (len_left == 0) &&
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000770 (len == frag_len_left), /* EOP? */
771 loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000772 frag_len_left -= len;
773 offset += len;
774 }
775 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700776}
777
778static inline void enic_queue_wq_skb(struct enic *enic,
779 struct vnic_wq *wq, struct sk_buff *skb)
780{
781 unsigned int mss = skb_shinfo(skb)->gso_size;
782 unsigned int vlan_tag = 0;
783 int vlan_tag_insert = 0;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000784 int loopback = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700785
Jesse Grosseab6d182010-10-20 13:56:03 +0000786 if (vlan_tx_tag_present(skb)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700787 /* VLAN tag from trunking driver */
788 vlan_tag_insert = 1;
789 vlan_tag = vlan_tx_tag_get(skb);
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000790 } else if (enic->loop_enable) {
791 vlan_tag = enic->loop_tag;
792 loopback = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700793 }
794
795 if (mss)
796 enic_queue_wq_skb_tso(enic, wq, skb, mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000797 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700798 else if (skb->ip_summed == CHECKSUM_PARTIAL)
799 enic_queue_wq_skb_csum_l4(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000800 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700801 else
802 enic_queue_wq_skb_vlan(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000803 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700804}
805
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800806/* netif_tx_lock held, process context with BHs disabled, or BH */
Stephen Hemminger613573252009-08-31 19:50:58 +0000807static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
Scott Feldmand87fd252009-12-23 13:27:59 +0000808 struct net_device *netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700809{
810 struct enic *enic = netdev_priv(netdev);
811 struct vnic_wq *wq = &enic->wq[0];
812 unsigned long flags;
813
814 if (skb->len <= 0) {
815 dev_kfree_skb(skb);
816 return NETDEV_TX_OK;
817 }
818
819 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
820 * which is very likely. In the off chance it's going to take
821 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
822 */
823
824 if (skb_shinfo(skb)->gso_size == 0 &&
825 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
826 skb_linearize(skb)) {
827 dev_kfree_skb(skb);
828 return NETDEV_TX_OK;
829 }
830
831 spin_lock_irqsave(&enic->wq_lock[0], flags);
832
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000833 if (vnic_wq_desc_avail(wq) <
834 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700835 netif_stop_queue(netdev);
836 /* This is a hard error, log it */
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000837 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700838 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
839 return NETDEV_TX_BUSY;
840 }
841
842 enic_queue_wq_skb(enic, wq, skb);
843
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000844 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700845 netif_stop_queue(netdev);
846
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700847 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
848
849 return NETDEV_TX_OK;
850}
851
852/* dev_base_lock rwlock held, nominally process context */
853static struct net_device_stats *enic_get_stats(struct net_device *netdev)
854{
855 struct enic *enic = netdev_priv(netdev);
Scott Feldman25f0a062008-09-24 11:23:32 -0700856 struct net_device_stats *net_stats = &netdev->stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700857 struct vnic_stats *stats;
858
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000859 enic_dev_stats_dump(enic, &stats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700860
Scott Feldman25f0a062008-09-24 11:23:32 -0700861 net_stats->tx_packets = stats->tx.tx_frames_ok;
862 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
863 net_stats->tx_errors = stats->tx.tx_errors;
864 net_stats->tx_dropped = stats->tx.tx_drops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700865
Scott Feldman25f0a062008-09-24 11:23:32 -0700866 net_stats->rx_packets = stats->rx.rx_frames_ok;
867 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
868 net_stats->rx_errors = stats->rx.rx_errors;
869 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
Scott Feldman350991e2009-09-03 17:02:19 +0000870 net_stats->rx_over_errors = enic->rq_truncated_pkts;
Scott Feldmanbd9fb1a2009-02-09 23:24:08 -0800871 net_stats->rx_crc_errors = enic->rq_bad_fcs;
Scott Feldman350991e2009-09-03 17:02:19 +0000872 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700873
Scott Feldman25f0a062008-09-24 11:23:32 -0700874 return net_stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700875}
876
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000877static void enic_reset_multicast_list(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700878{
879 enic->mc_count = 0;
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000880 enic->flags = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700881}
882
883static int enic_set_mac_addr(struct net_device *netdev, char *addr)
884{
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700885 struct enic *enic = netdev_priv(netdev);
886
887 if (enic_is_dynamic(enic)) {
888 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
889 return -EADDRNOTAVAIL;
890 } else {
891 if (!is_valid_ether_addr(addr))
892 return -EADDRNOTAVAIL;
893 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700894
895 memcpy(netdev->dev_addr, addr, netdev->addr_len);
896
897 return 0;
898}
899
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700900static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
901{
902 struct enic *enic = netdev_priv(netdev);
903 struct sockaddr *saddr = p;
904 char *addr = saddr->sa_data;
905 int err;
906
907 if (netif_running(enic->netdev)) {
908 err = enic_dev_del_station_addr(enic);
909 if (err)
910 return err;
911 }
912
913 err = enic_set_mac_addr(netdev, addr);
914 if (err)
915 return err;
916
917 if (netif_running(enic->netdev)) {
918 err = enic_dev_add_station_addr(enic);
919 if (err)
920 return err;
921 }
922
923 return err;
924}
925
926static int enic_set_mac_address(struct net_device *netdev, void *p)
927{
Roopa Prabhu294dab22010-08-10 18:54:55 +0000928 struct sockaddr *saddr = p;
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000929 char *addr = saddr->sa_data;
930 struct enic *enic = netdev_priv(netdev);
931 int err;
Roopa Prabhu294dab22010-08-10 18:54:55 +0000932
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000933 err = enic_dev_del_station_addr(enic);
934 if (err)
935 return err;
936
937 err = enic_set_mac_addr(netdev, addr);
938 if (err)
939 return err;
940
941 return enic_dev_add_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700942}
943
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000944static void enic_add_multicast_addr_list(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700945{
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000946 struct net_device *netdev = enic->netdev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000947 struct netdev_hw_addr *ha;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000948 unsigned int mc_count = netdev_mc_count(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700949 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700950 unsigned int i, j;
951
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000952 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
953 netdev_warn(netdev, "Registering only %d out of %d "
954 "multicast addresses\n",
955 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700956 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
Scott Feldman9959a182009-12-23 13:27:43 +0000957 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700958
959 /* Is there an easier way? Trying to minimize to
960 * calls to add/del multicast addrs. We keep the
961 * addrs from the last call in enic->mc_addr and
962 * look for changes to add/del.
963 */
964
Jiri Pirko48e2f182010-02-22 09:22:26 +0000965 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000966 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko48e2f182010-02-22 09:22:26 +0000967 if (i == mc_count)
968 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000969 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700970 }
971
972 for (i = 0; i < enic->mc_count; i++) {
973 for (j = 0; j < mc_count; j++)
974 if (compare_ether_addr(enic->mc_addr[i],
975 mc_addr[j]) == 0)
976 break;
977 if (j == mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000978 enic_dev_del_addr(enic, enic->mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700979 }
980
981 for (i = 0; i < mc_count; i++) {
982 for (j = 0; j < enic->mc_count; j++)
983 if (compare_ether_addr(mc_addr[i],
984 enic->mc_addr[j]) == 0)
985 break;
986 if (j == enic->mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000987 enic_dev_add_addr(enic, mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700988 }
989
990 /* Save the list to compare against next time
991 */
992
993 for (i = 0; i < mc_count; i++)
994 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
995
996 enic->mc_count = mc_count;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700997}
998
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000999static void enic_add_unicast_addr_list(struct enic *enic)
1000{
1001 struct net_device *netdev = enic->netdev;
1002 struct netdev_hw_addr *ha;
1003 unsigned int uc_count = netdev_uc_count(netdev);
1004 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
1005 unsigned int i, j;
1006
1007 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
1008 netdev_warn(netdev, "Registering only %d out of %d "
1009 "unicast addresses\n",
1010 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
1011 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
1012 }
1013
1014 /* Is there an easier way? Trying to minimize to
1015 * calls to add/del unicast addrs. We keep the
1016 * addrs from the last call in enic->uc_addr and
1017 * look for changes to add/del.
1018 */
1019
1020 i = 0;
1021 netdev_for_each_uc_addr(ha, netdev) {
1022 if (i == uc_count)
1023 break;
1024 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
1025 }
1026
1027 for (i = 0; i < enic->uc_count; i++) {
1028 for (j = 0; j < uc_count; j++)
1029 if (compare_ether_addr(enic->uc_addr[i],
1030 uc_addr[j]) == 0)
1031 break;
1032 if (j == uc_count)
1033 enic_dev_del_addr(enic, enic->uc_addr[i]);
1034 }
1035
1036 for (i = 0; i < uc_count; i++) {
1037 for (j = 0; j < enic->uc_count; j++)
1038 if (compare_ether_addr(uc_addr[i],
1039 enic->uc_addr[j]) == 0)
1040 break;
1041 if (j == enic->uc_count)
1042 enic_dev_add_addr(enic, uc_addr[i]);
1043 }
1044
1045 /* Save the list to compare against next time
1046 */
1047
1048 for (i = 0; i < uc_count; i++)
1049 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1050
1051 enic->uc_count = uc_count;
1052}
1053
1054/* netif_tx_lock held, BHs disabled */
1055static void enic_set_rx_mode(struct net_device *netdev)
1056{
1057 struct enic *enic = netdev_priv(netdev);
1058 int directed = 1;
1059 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1060 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1061 int promisc = (netdev->flags & IFF_PROMISC) ||
1062 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1063 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1064 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1065 unsigned int flags = netdev->flags |
1066 (allmulti ? IFF_ALLMULTI : 0) |
1067 (promisc ? IFF_PROMISC : 0);
1068
1069 if (enic->flags != flags) {
1070 enic->flags = flags;
1071 enic_dev_packet_filter(enic, directed,
1072 multicast, broadcast, promisc, allmulti);
1073 }
1074
1075 if (!promisc) {
1076 enic_add_unicast_addr_list(enic);
1077 if (!allmulti)
1078 enic_add_multicast_addr_list(enic);
1079 }
1080}
1081
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001082/* rtnl lock is held */
1083static void enic_vlan_rx_register(struct net_device *netdev,
1084 struct vlan_group *vlan_group)
1085{
1086 struct enic *enic = netdev_priv(netdev);
1087 enic->vlan_group = vlan_group;
1088}
1089
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001090/* netif_tx_lock held, BHs disabled */
1091static void enic_tx_timeout(struct net_device *netdev)
1092{
1093 struct enic *enic = netdev_priv(netdev);
1094 schedule_work(&enic->reset);
1095}
1096
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001097static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1098{
1099 struct enic *enic = netdev_priv(netdev);
1100
1101 if (vf != PORT_SELF_VF)
1102 return -EOPNOTSUPP;
1103
1104 /* Ignore the vf argument for now. We can assume the request
1105 * is coming on a vf.
1106 */
1107 if (is_valid_ether_addr(mac)) {
1108 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1109 return 0;
1110 } else
1111 return -EINVAL;
1112}
1113
Scott Feldman08f382e2010-06-01 08:59:33 +00001114static int enic_set_port_profile(struct enic *enic, u8 *mac)
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001115{
1116 struct vic_provinfo *vp;
1117 u8 oui[3] = VIC_PROVINFO_CISCO_OUI;
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001118 u16 os_type = VIC_GENERIC_PROV_OS_TYPE_LINUX;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001119 char uuid_str[38];
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001120 char client_mac_str[18];
1121 u8 *client_mac;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001122 int err;
1123
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001124 err = enic_vnic_dev_deinit(enic);
1125 if (err)
Scott Feldman08f382e2010-06-01 08:59:33 +00001126 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001127
Scott Feldman08f382e2010-06-01 08:59:33 +00001128 switch (enic->pp.request) {
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001129
Scott Feldman08f382e2010-06-01 08:59:33 +00001130 case PORT_REQUEST_ASSOCIATE:
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001131
Scott Feldman08f382e2010-06-01 08:59:33 +00001132 if (!(enic->pp.set & ENIC_SET_NAME) || !strlen(enic->pp.name))
1133 return -EINVAL;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001134
Scott Feldman08f382e2010-06-01 08:59:33 +00001135 if (!is_valid_ether_addr(mac))
1136 return -EADDRNOTAVAIL;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001137
Scott Feldman08f382e2010-06-01 08:59:33 +00001138 vp = vic_provinfo_alloc(GFP_KERNEL, oui,
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001139 VIC_PROVINFO_GENERIC_TYPE);
Scott Feldman08f382e2010-06-01 08:59:33 +00001140 if (!vp)
1141 return -ENOMEM;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001142
Scott Feldman08f382e2010-06-01 08:59:33 +00001143 vic_provinfo_add_tlv(vp,
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001144 VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR,
Scott Feldman08f382e2010-06-01 08:59:33 +00001145 strlen(enic->pp.name) + 1, enic->pp.name);
1146
Roopa Prabhu29639052010-12-08 13:54:03 +00001147 if (!is_zero_ether_addr(enic->pp.mac_addr))
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001148 client_mac = enic->pp.mac_addr;
Roopa Prabhu29639052010-12-08 13:54:03 +00001149 else
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001150 client_mac = mac;
1151
1152 vic_provinfo_add_tlv(vp,
1153 VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR,
1154 ETH_ALEN, client_mac);
1155
1156 sprintf(client_mac_str, "%pM", client_mac);
1157 vic_provinfo_add_tlv(vp,
1158 VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR,
1159 sizeof(client_mac_str), client_mac_str);
Scott Feldman08f382e2010-06-01 08:59:33 +00001160
1161 if (enic->pp.set & ENIC_SET_INSTANCE) {
Joe Perchesc33788b2010-08-03 09:32:24 +00001162 sprintf(uuid_str, "%pUB", enic->pp.instance_uuid);
Scott Feldman08f382e2010-06-01 08:59:33 +00001163 vic_provinfo_add_tlv(vp,
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001164 VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR,
Scott Feldman08f382e2010-06-01 08:59:33 +00001165 sizeof(uuid_str), uuid_str);
1166 }
1167
1168 if (enic->pp.set & ENIC_SET_HOST) {
Joe Perchesc33788b2010-08-03 09:32:24 +00001169 sprintf(uuid_str, "%pUB", enic->pp.host_uuid);
Scott Feldman08f382e2010-06-01 08:59:33 +00001170 vic_provinfo_add_tlv(vp,
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001171 VIC_GENERIC_PROV_TLV_HOST_UUID_STR,
Scott Feldman08f382e2010-06-01 08:59:33 +00001172 sizeof(uuid_str), uuid_str);
1173 }
1174
Roopa Prabhu6c2c9d92010-12-10 12:02:33 +00001175 os_type = htons(os_type);
1176 vic_provinfo_add_tlv(vp,
1177 VIC_GENERIC_PROV_TLV_OS_TYPE,
1178 sizeof(os_type), &os_type);
1179
Scott Feldman08f382e2010-06-01 08:59:33 +00001180 err = enic_dev_init_prov(enic, vp);
1181 vic_provinfo_free(vp);
1182 if (err)
1183 return err;
1184 break;
1185
1186 case PORT_REQUEST_DISASSOCIATE:
1187 break;
1188
1189 default:
1190 return -EINVAL;
1191 }
1192
Roopa Prabhu4dce2392011-01-20 14:35:54 +00001193 /* Set flag to indicate that the port assoc/disassoc
1194 * request has been sent out to fw
1195 */
1196 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1197
Scott Feldman08f382e2010-06-01 08:59:33 +00001198 return 0;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001199}
1200
1201static int enic_set_vf_port(struct net_device *netdev, int vf,
1202 struct nlattr *port[])
1203{
1204 struct enic *enic = netdev_priv(netdev);
Roopa Prabhu29639052010-12-08 13:54:03 +00001205 struct enic_port_profile new_pp;
1206 int err = 0;
Scott Feldman08f382e2010-06-01 08:59:33 +00001207
Roopa Prabhu29639052010-12-08 13:54:03 +00001208 memset(&new_pp, 0, sizeof(new_pp));
Scott Feldman08f382e2010-06-01 08:59:33 +00001209
1210 if (port[IFLA_PORT_REQUEST]) {
Roopa Prabhu29639052010-12-08 13:54:03 +00001211 new_pp.set |= ENIC_SET_REQUEST;
1212 new_pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
Scott Feldman08f382e2010-06-01 08:59:33 +00001213 }
1214
1215 if (port[IFLA_PORT_PROFILE]) {
Roopa Prabhu29639052010-12-08 13:54:03 +00001216 new_pp.set |= ENIC_SET_NAME;
1217 memcpy(new_pp.name, nla_data(port[IFLA_PORT_PROFILE]),
Scott Feldman08f382e2010-06-01 08:59:33 +00001218 PORT_PROFILE_MAX);
1219 }
1220
1221 if (port[IFLA_PORT_INSTANCE_UUID]) {
Roopa Prabhu29639052010-12-08 13:54:03 +00001222 new_pp.set |= ENIC_SET_INSTANCE;
1223 memcpy(new_pp.instance_uuid,
Scott Feldman08f382e2010-06-01 08:59:33 +00001224 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1225 }
1226
1227 if (port[IFLA_PORT_HOST_UUID]) {
Roopa Prabhu29639052010-12-08 13:54:03 +00001228 new_pp.set |= ENIC_SET_HOST;
1229 memcpy(new_pp.host_uuid,
Scott Feldman08f382e2010-06-01 08:59:33 +00001230 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1231 }
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001232
1233 /* don't support VFs, yet */
1234 if (vf != PORT_SELF_VF)
1235 return -EOPNOTSUPP;
1236
Roopa Prabhu29639052010-12-08 13:54:03 +00001237 if (!(new_pp.set & ENIC_SET_REQUEST))
Scott Feldman08f382e2010-06-01 08:59:33 +00001238 return -EOPNOTSUPP;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001239
Roopa Prabhu29639052010-12-08 13:54:03 +00001240 if (new_pp.request == PORT_REQUEST_ASSOCIATE) {
1241 /* Special case handling */
1242 if (!is_zero_ether_addr(enic->pp.vf_mac))
1243 memcpy(new_pp.mac_addr, enic->pp.vf_mac, ETH_ALEN);
Scott Feldman418c4372010-05-22 17:29:58 +00001244
1245 if (is_zero_ether_addr(netdev->dev_addr))
1246 random_ether_addr(netdev->dev_addr);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001247 }
1248
Roopa Prabhu29639052010-12-08 13:54:03 +00001249 memcpy(&enic->pp, &new_pp, sizeof(struct enic_port_profile));
1250
1251 err = enic_set_port_profile(enic, netdev->dev_addr);
1252 if (err)
1253 goto set_port_profile_cleanup;
1254
Roopa Prabhu29639052010-12-08 13:54:03 +00001255set_port_profile_cleanup:
1256 memset(enic->pp.vf_mac, 0, ETH_ALEN);
1257
1258 if (err || enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1259 memset(netdev->dev_addr, 0, ETH_ALEN);
1260 memset(enic->pp.mac_addr, 0, ETH_ALEN);
1261 }
1262
1263 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001264}
1265
1266static int enic_get_vf_port(struct net_device *netdev, int vf,
1267 struct sk_buff *skb)
1268{
1269 struct enic *enic = netdev_priv(netdev);
1270 int err, error, done;
1271 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1272
Roopa Prabhu4dce2392011-01-20 14:35:54 +00001273 if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
Scott Feldman08f382e2010-06-01 08:59:33 +00001274 return -ENODATA;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001275
1276 err = enic_dev_init_done(enic, &done, &error);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001277 if (err)
Scott Feldman08f382e2010-06-01 08:59:33 +00001278 error = err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001279
1280 switch (error) {
1281 case ERR_SUCCESS:
1282 if (!done)
1283 response = PORT_PROFILE_RESPONSE_INPROGRESS;
1284 break;
1285 case ERR_EINVAL:
1286 response = PORT_PROFILE_RESPONSE_INVALID;
1287 break;
1288 case ERR_EBADSTATE:
1289 response = PORT_PROFILE_RESPONSE_BADSTATE;
1290 break;
1291 case ERR_ENOMEM:
1292 response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
1293 break;
1294 default:
1295 response = PORT_PROFILE_RESPONSE_ERROR;
1296 break;
1297 }
1298
1299 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1300 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
Scott Feldman08f382e2010-06-01 08:59:33 +00001301 if (enic->pp.set & ENIC_SET_NAME)
1302 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1303 enic->pp.name);
1304 if (enic->pp.set & ENIC_SET_INSTANCE)
1305 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1306 enic->pp.instance_uuid);
1307 if (enic->pp.set & ENIC_SET_HOST)
1308 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1309 enic->pp.host_uuid);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001310
1311 return 0;
1312
1313nla_put_failure:
1314 return -EMSGSIZE;
1315}
1316
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001317static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1318{
1319 struct enic *enic = vnic_dev_priv(rq->vdev);
1320
1321 if (!buf->os_buf)
1322 return;
1323
1324 pci_unmap_single(enic->pdev, buf->dma_addr,
1325 buf->len, PCI_DMA_FROMDEVICE);
1326 dev_kfree_skb_any(buf->os_buf);
1327}
1328
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001329static int enic_rq_alloc_buf(struct vnic_rq *rq)
1330{
1331 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldmand19e22d2009-09-03 17:02:08 +00001332 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001333 struct sk_buff *skb;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00001334 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001335 unsigned int os_buf_index = 0;
1336 dma_addr_t dma_addr;
1337
Eric Dumazet89d71a62009-10-13 05:34:20 +00001338 skb = netdev_alloc_skb_ip_align(netdev, len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001339 if (!skb)
1340 return -ENOMEM;
1341
1342 dma_addr = pci_map_single(enic->pdev, skb->data,
1343 len, PCI_DMA_FROMDEVICE);
1344
1345 enic_queue_rq_desc(rq, skb, os_buf_index,
1346 dma_addr, len);
1347
1348 return 0;
1349}
1350
Scott Feldman4badc382009-09-03 17:01:58 +00001351static int enic_rq_alloc_buf_a1(struct vnic_rq *rq)
1352{
1353 struct rq_enet_desc *desc = vnic_rq_next_desc(rq);
1354
1355 if (vnic_rq_posting_soon(rq)) {
1356
1357 /* SW workaround for A0 HW erratum: if we're just about
1358 * to write posted_index, insert a dummy desc
1359 * of type resvd
1360 */
1361
1362 rq_enet_desc_enc(desc, 0, RQ_ENET_TYPE_RESV2, 0);
1363 vnic_rq_post(rq, 0, 0, 0, 0);
1364 } else {
1365 return enic_rq_alloc_buf(rq);
1366 }
1367
1368 return 0;
1369}
1370
1371static int enic_set_rq_alloc_buf(struct enic *enic)
1372{
1373 enum vnic_dev_hw_version hw_ver;
1374 int err;
1375
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001376 err = enic_dev_hw_version(enic, &hw_ver);
Scott Feldman4badc382009-09-03 17:01:58 +00001377 if (err)
1378 return err;
1379
1380 switch (hw_ver) {
1381 case VNIC_DEV_HW_VER_A1:
1382 enic->rq_alloc_buf = enic_rq_alloc_buf_a1;
1383 break;
1384 case VNIC_DEV_HW_VER_A2:
1385 case VNIC_DEV_HW_VER_UNKNOWN:
1386 enic->rq_alloc_buf = enic_rq_alloc_buf;
1387 break;
1388 default:
1389 return -ENODEV;
1390 }
1391
1392 return 0;
1393}
1394
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001395static void enic_rq_indicate_buf(struct vnic_rq *rq,
1396 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1397 int skipped, void *opaque)
1398{
1399 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001400 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001401 struct sk_buff *skb;
1402
1403 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1404 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1405 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1406 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1407 u8 packet_error;
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001408 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001409 u32 rss_hash;
1410
1411 if (skipped)
1412 return;
1413
1414 skb = buf->os_buf;
1415 prefetch(skb->data - NET_IP_ALIGN);
1416 pci_unmap_single(enic->pdev, buf->dma_addr,
1417 buf->len, PCI_DMA_FROMDEVICE);
1418
1419 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1420 &type, &color, &q_number, &completed_index,
1421 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1422 &csum_not_calc, &rss_hash, &bytes_written,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001423 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001424 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1425 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1426 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1427 &fcs_ok);
1428
1429 if (packet_error) {
1430
Scott Feldman350991e2009-09-03 17:02:19 +00001431 if (!fcs_ok) {
1432 if (bytes_written > 0)
1433 enic->rq_bad_fcs++;
1434 else if (bytes_written == 0)
1435 enic->rq_truncated_pkts++;
1436 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001437
1438 dev_kfree_skb_any(skb);
1439
1440 return;
1441 }
1442
1443 if (eop && bytes_written > 0) {
1444
1445 /* Good receive
1446 */
1447
1448 skb_put(skb, bytes_written);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001449 skb->protocol = eth_type_trans(skb, netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001450
1451 if (enic->csum_rx_enabled && !csum_not_calc) {
1452 skb->csum = htons(checksum);
1453 skb->ip_summed = CHECKSUM_COMPLETE;
1454 }
1455
Scott Feldman86ca9db2008-11-21 21:26:55 -08001456 skb->dev = netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001457
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001458 if (enic->vlan_group && vlan_stripped &&
1459 (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001460
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001461 if (netdev->features & NETIF_F_GRO)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001462 vlan_gro_receive(&enic->napi[q_number],
1463 enic->vlan_group, vlan_tci, skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001464 else
1465 vlan_hwaccel_receive_skb(skb,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001466 enic->vlan_group, vlan_tci);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001467
1468 } else {
1469
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001470 if (netdev->features & NETIF_F_GRO)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001471 napi_gro_receive(&enic->napi[q_number], skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001472 else
1473 netif_receive_skb(skb);
1474
1475 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001476 } else {
1477
1478 /* Buffer overflow
1479 */
1480
1481 dev_kfree_skb_any(skb);
1482 }
1483}
1484
1485static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1486 u8 type, u16 q_number, u16 completed_index, void *opaque)
1487{
1488 struct enic *enic = vnic_dev_priv(vdev);
1489
1490 vnic_rq_service(&enic->rq[q_number], cq_desc,
1491 completed_index, VNIC_RQ_RETURN_DESC,
1492 enic_rq_indicate_buf, opaque);
1493
1494 return 0;
1495}
1496
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001497static int enic_poll(struct napi_struct *napi, int budget)
1498{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001499 struct net_device *netdev = napi->dev;
1500 struct enic *enic = netdev_priv(netdev);
1501 unsigned int cq_rq = enic_cq_rq(enic, 0);
1502 unsigned int cq_wq = enic_cq_wq(enic, 0);
1503 unsigned int intr = enic_legacy_io_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001504 unsigned int rq_work_to_do = budget;
1505 unsigned int wq_work_to_do = -1; /* no limit */
1506 unsigned int work_done, rq_work_done, wq_work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001507 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001508
1509 /* Service RQ (first) and WQ
1510 */
1511
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001512 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001513 rq_work_to_do, enic_rq_service, NULL);
1514
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001515 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001516 wq_work_to_do, enic_wq_service, NULL);
1517
1518 /* Accumulate intr event credits for this polling
1519 * cycle. An intr event is the completion of a
1520 * a WQ or RQ packet.
1521 */
1522
1523 work_done = rq_work_done + wq_work_done;
1524
1525 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001526 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001527 work_done,
1528 0 /* don't unmask intr */,
1529 0 /* don't reset intr timer */);
1530
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001531 err = vnic_rq_fill(&enic->rq[0], enic->rq_alloc_buf);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001532
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001533 /* Buffer allocation failed. Stay in polling
1534 * mode so we can try to fill the ring again.
1535 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001536
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001537 if (err)
1538 rq_work_done = rq_work_to_do;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001539
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001540 if (rq_work_done < rq_work_to_do) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001541
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001542 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001543 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001544 */
1545
Ben Hutchings288379f2009-01-19 16:43:59 -08001546 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001547 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001548 }
1549
1550 return rq_work_done;
1551}
1552
1553static int enic_poll_msix(struct napi_struct *napi, int budget)
1554{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001555 struct net_device *netdev = napi->dev;
1556 struct enic *enic = netdev_priv(netdev);
1557 unsigned int rq = (napi - &enic->napi[0]);
1558 unsigned int cq = enic_cq_rq(enic, rq);
1559 unsigned int intr = enic_msix_rq_intr(enic, rq);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001560 unsigned int work_to_do = budget;
1561 unsigned int work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001562 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001563
1564 /* Service RQ
1565 */
1566
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001567 work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001568 work_to_do, enic_rq_service, NULL);
1569
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001570 /* Return intr event credits for this polling
1571 * cycle. An intr event is the completion of a
1572 * RQ packet.
1573 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001574
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001575 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001576 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001577 work_done,
1578 0 /* don't unmask intr */,
1579 0 /* don't reset intr timer */);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001580
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001581 err = vnic_rq_fill(&enic->rq[rq], enic->rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001582
1583 /* Buffer allocation failed. Stay in polling mode
1584 * so we can try to fill the ring again.
1585 */
1586
1587 if (err)
1588 work_done = work_to_do;
1589
1590 if (work_done < work_to_do) {
1591
1592 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001593 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001594 */
1595
Ben Hutchings288379f2009-01-19 16:43:59 -08001596 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001597 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001598 }
1599
1600 return work_done;
1601}
1602
1603static void enic_notify_timer(unsigned long data)
1604{
1605 struct enic *enic = (struct enic *)data;
1606
1607 enic_notify_check(enic);
1608
Scott Feldman25f0a062008-09-24 11:23:32 -07001609 mod_timer(&enic->notify_timer,
1610 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001611}
1612
1613static void enic_free_intr(struct enic *enic)
1614{
1615 struct net_device *netdev = enic->netdev;
1616 unsigned int i;
1617
1618 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1619 case VNIC_DEV_INTR_MODE_INTX:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001620 free_irq(enic->pdev->irq, netdev);
1621 break;
Scott Feldman8f4d2482008-09-24 11:23:42 -07001622 case VNIC_DEV_INTR_MODE_MSI:
1623 free_irq(enic->pdev->irq, enic);
1624 break;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001625 case VNIC_DEV_INTR_MODE_MSIX:
1626 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1627 if (enic->msix[i].requested)
1628 free_irq(enic->msix_entry[i].vector,
1629 enic->msix[i].devid);
1630 break;
1631 default:
1632 break;
1633 }
1634}
1635
1636static int enic_request_intr(struct enic *enic)
1637{
1638 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001639 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001640 int err = 0;
1641
1642 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1643
1644 case VNIC_DEV_INTR_MODE_INTX:
1645
1646 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1647 IRQF_SHARED, netdev->name, netdev);
1648 break;
1649
1650 case VNIC_DEV_INTR_MODE_MSI:
1651
1652 err = request_irq(enic->pdev->irq, enic_isr_msi,
1653 0, netdev->name, enic);
1654 break;
1655
1656 case VNIC_DEV_INTR_MODE_MSIX:
1657
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001658 for (i = 0; i < enic->rq_count; i++) {
1659 intr = enic_msix_rq_intr(enic, i);
1660 sprintf(enic->msix[intr].devname,
1661 "%.11s-rx-%d", netdev->name, i);
1662 enic->msix[intr].isr = enic_isr_msix_rq;
1663 enic->msix[intr].devid = &enic->napi[i];
1664 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001665
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001666 for (i = 0; i < enic->wq_count; i++) {
1667 intr = enic_msix_wq_intr(enic, i);
1668 sprintf(enic->msix[intr].devname,
1669 "%.11s-tx-%d", netdev->name, i);
1670 enic->msix[intr].isr = enic_isr_msix_wq;
1671 enic->msix[intr].devid = enic;
1672 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001673
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001674 intr = enic_msix_err_intr(enic);
1675 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001676 "%.11s-err", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001677 enic->msix[intr].isr = enic_isr_msix_err;
1678 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001679
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001680 intr = enic_msix_notify_intr(enic);
1681 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001682 "%.11s-notify", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001683 enic->msix[intr].isr = enic_isr_msix_notify;
1684 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001685
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001686 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1687 enic->msix[i].requested = 0;
1688
1689 for (i = 0; i < enic->intr_count; i++) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001690 err = request_irq(enic->msix_entry[i].vector,
1691 enic->msix[i].isr, 0,
1692 enic->msix[i].devname,
1693 enic->msix[i].devid);
1694 if (err) {
1695 enic_free_intr(enic);
1696 break;
1697 }
1698 enic->msix[i].requested = 1;
1699 }
1700
1701 break;
1702
1703 default:
1704 break;
1705 }
1706
1707 return err;
1708}
1709
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001710static void enic_synchronize_irqs(struct enic *enic)
1711{
1712 unsigned int i;
1713
1714 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1715 case VNIC_DEV_INTR_MODE_INTX:
1716 case VNIC_DEV_INTR_MODE_MSI:
1717 synchronize_irq(enic->pdev->irq);
1718 break;
1719 case VNIC_DEV_INTR_MODE_MSIX:
1720 for (i = 0; i < enic->intr_count; i++)
1721 synchronize_irq(enic->msix_entry[i].vector);
1722 break;
1723 default:
1724 break;
1725 }
1726}
1727
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001728static int enic_dev_notify_set(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001729{
1730 int err;
1731
Scott Feldman56ac88b2009-09-03 17:02:14 +00001732 spin_lock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001733 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1734 case VNIC_DEV_INTR_MODE_INTX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001735 err = vnic_dev_notify_set(enic->vdev,
1736 enic_legacy_notify_intr());
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001737 break;
1738 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001739 err = vnic_dev_notify_set(enic->vdev,
1740 enic_msix_notify_intr(enic));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001741 break;
1742 default:
1743 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1744 break;
1745 }
Scott Feldman56ac88b2009-09-03 17:02:14 +00001746 spin_unlock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001747
1748 return err;
1749}
1750
1751static void enic_notify_timer_start(struct enic *enic)
1752{
1753 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1754 case VNIC_DEV_INTR_MODE_MSI:
1755 mod_timer(&enic->notify_timer, jiffies);
1756 break;
1757 default:
1758 /* Using intr for notification for INTx/MSI-X */
1759 break;
1760 };
1761}
1762
1763/* rtnl lock is held, process context */
1764static int enic_open(struct net_device *netdev)
1765{
1766 struct enic *enic = netdev_priv(netdev);
1767 unsigned int i;
1768 int err;
1769
Scott Feldman4b75a442008-09-24 11:23:53 -07001770 err = enic_request_intr(enic);
1771 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001772 netdev_err(netdev, "Unable to request irq.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001773 return err;
1774 }
1775
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001776 err = enic_dev_notify_set(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001777 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001778 netdev_err(netdev,
1779 "Failed to alloc notify buffer, aborting.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001780 goto err_out_free_intr;
1781 }
1782
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001783 for (i = 0; i < enic->rq_count; i++) {
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001784 vnic_rq_fill(&enic->rq[i], enic->rq_alloc_buf);
1785 /* Need at least one buffer on ring to get going */
1786 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001787 netdev_err(netdev, "Unable to alloc receive buffers\n");
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001788 err = -ENOMEM;
Scott Feldman4b75a442008-09-24 11:23:53 -07001789 goto err_out_notify_unset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001790 }
1791 }
1792
1793 for (i = 0; i < enic->wq_count; i++)
1794 vnic_wq_enable(&enic->wq[i]);
1795 for (i = 0; i < enic->rq_count; i++)
1796 vnic_rq_enable(&enic->rq[i]);
1797
Roopa Prabhu29639052010-12-08 13:54:03 +00001798 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1799 enic_dev_add_addr(enic, enic->pp.mac_addr);
1800 else
1801 enic_dev_add_station_addr(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001802 enic_set_rx_mode(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001803
1804 netif_wake_queue(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001805
1806 for (i = 0; i < enic->rq_count; i++)
1807 napi_enable(&enic->napi[i]);
1808
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001809 enic_dev_enable(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001810
1811 for (i = 0; i < enic->intr_count; i++)
1812 vnic_intr_unmask(&enic->intr[i]);
1813
1814 enic_notify_timer_start(enic);
1815
1816 return 0;
Scott Feldman4b75a442008-09-24 11:23:53 -07001817
1818err_out_notify_unset:
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001819 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001820err_out_free_intr:
1821 enic_free_intr(enic);
1822
1823 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001824}
1825
1826/* rtnl lock is held, process context */
1827static int enic_stop(struct net_device *netdev)
1828{
1829 struct enic *enic = netdev_priv(netdev);
1830 unsigned int i;
1831 int err;
1832
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001833 for (i = 0; i < enic->intr_count; i++) {
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001834 vnic_intr_mask(&enic->intr[i]);
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001835 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1836 }
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001837
1838 enic_synchronize_irqs(enic);
1839
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001840 del_timer_sync(&enic->notify_timer);
1841
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001842 enic_dev_disable(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001843
1844 for (i = 0; i < enic->rq_count; i++)
1845 napi_disable(&enic->napi[i]);
1846
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001847 netif_carrier_off(netdev);
1848 netif_tx_disable(netdev);
Roopa Prabhu29639052010-12-08 13:54:03 +00001849 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1850 enic_dev_del_addr(enic, enic->pp.mac_addr);
1851 else
1852 enic_dev_del_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001853
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001854 for (i = 0; i < enic->wq_count; i++) {
1855 err = vnic_wq_disable(&enic->wq[i]);
1856 if (err)
1857 return err;
1858 }
1859 for (i = 0; i < enic->rq_count; i++) {
1860 err = vnic_rq_disable(&enic->rq[i]);
1861 if (err)
1862 return err;
1863 }
1864
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001865 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001866 enic_free_intr(enic);
1867
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001868 for (i = 0; i < enic->wq_count; i++)
1869 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1870 for (i = 0; i < enic->rq_count; i++)
1871 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1872 for (i = 0; i < enic->cq_count; i++)
1873 vnic_cq_clean(&enic->cq[i]);
1874 for (i = 0; i < enic->intr_count; i++)
1875 vnic_intr_clean(&enic->intr[i]);
1876
1877 return 0;
1878}
1879
1880static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1881{
1882 struct enic *enic = netdev_priv(netdev);
1883 int running = netif_running(netdev);
1884
Scott Feldman25f0a062008-09-24 11:23:32 -07001885 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1886 return -EINVAL;
1887
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001888 if (running)
1889 enic_stop(netdev);
1890
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001891 netdev->mtu = new_mtu;
1892
1893 if (netdev->mtu > enic->port_mtu)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001894 netdev_warn(netdev,
1895 "interface MTU (%d) set higher than port MTU (%d)\n",
1896 netdev->mtu, enic->port_mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001897
1898 if (running)
1899 enic_open(netdev);
1900
1901 return 0;
1902}
1903
1904#ifdef CONFIG_NET_POLL_CONTROLLER
1905static void enic_poll_controller(struct net_device *netdev)
1906{
1907 struct enic *enic = netdev_priv(netdev);
1908 struct vnic_dev *vdev = enic->vdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001909 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001910
1911 switch (vnic_dev_get_intr_mode(vdev)) {
1912 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001913 for (i = 0; i < enic->rq_count; i++) {
1914 intr = enic_msix_rq_intr(enic, i);
Vasanthy Kolluri79aeec52010-12-08 13:05:45 +00001915 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1916 &enic->napi[i]);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001917 }
1918 intr = enic_msix_wq_intr(enic, i);
1919 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001920 break;
1921 case VNIC_DEV_INTR_MODE_MSI:
1922 enic_isr_msi(enic->pdev->irq, enic);
1923 break;
1924 case VNIC_DEV_INTR_MODE_INTX:
1925 enic_isr_legacy(enic->pdev->irq, netdev);
1926 break;
1927 default:
1928 break;
1929 }
1930}
1931#endif
1932
1933static int enic_dev_wait(struct vnic_dev *vdev,
1934 int (*start)(struct vnic_dev *, int),
1935 int (*finished)(struct vnic_dev *, int *),
1936 int arg)
1937{
1938 unsigned long time;
1939 int done;
1940 int err;
1941
1942 BUG_ON(in_interrupt());
1943
1944 err = start(vdev, arg);
1945 if (err)
1946 return err;
1947
1948 /* Wait for func to complete...2 seconds max
1949 */
1950
1951 time = jiffies + (HZ * 2);
1952 do {
1953
1954 err = finished(vdev, &done);
1955 if (err)
1956 return err;
1957
1958 if (done)
1959 return 0;
1960
1961 schedule_timeout_uninterruptible(HZ / 10);
1962
1963 } while (time_after(time, jiffies));
1964
1965 return -ETIMEDOUT;
1966}
1967
1968static int enic_dev_open(struct enic *enic)
1969{
1970 int err;
1971
1972 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1973 vnic_dev_open_done, 0);
1974 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001975 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1976 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001977
1978 return err;
1979}
1980
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001981static int enic_dev_hang_reset(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001982{
1983 int err;
1984
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001985 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1986 vnic_dev_hang_reset_done, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001987 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001988 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1989 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001990
1991 return err;
1992}
1993
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001994static int enic_set_rsskey(struct enic *enic)
Scott Feldman68f71702009-02-09 23:24:24 -08001995{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001996 dma_addr_t rss_key_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001997 union vnic_rss_key *rss_key_buf_va = NULL;
1998 union vnic_rss_key rss_key = {
1999 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
2000 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
2001 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
2002 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
2003 };
2004 int err;
2005
2006 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
2007 sizeof(union vnic_rss_key), &rss_key_buf_pa);
2008 if (!rss_key_buf_va)
2009 return -ENOMEM;
2010
2011 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
2012
2013 spin_lock(&enic->devcmd_lock);
2014 err = enic_set_rss_key(enic,
2015 rss_key_buf_pa,
2016 sizeof(union vnic_rss_key));
2017 spin_unlock(&enic->devcmd_lock);
2018
2019 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
2020 rss_key_buf_va, rss_key_buf_pa);
2021
2022 return err;
2023}
2024
2025static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
2026{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00002027 dma_addr_t rss_cpu_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002028 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
2029 unsigned int i;
2030 int err;
2031
2032 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
2033 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
2034 if (!rss_cpu_buf_va)
2035 return -ENOMEM;
2036
2037 for (i = 0; i < (1 << rss_hash_bits); i++)
2038 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
2039
2040 spin_lock(&enic->devcmd_lock);
2041 err = enic_set_rss_cpu(enic,
2042 rss_cpu_buf_pa,
2043 sizeof(union vnic_rss_cpu));
2044 spin_unlock(&enic->devcmd_lock);
2045
2046 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
2047 rss_cpu_buf_va, rss_cpu_buf_pa);
2048
2049 return err;
2050}
2051
2052static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
2053 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
2054{
Scott Feldman68f71702009-02-09 23:24:24 -08002055 const u8 tso_ipid_split_en = 0;
2056 const u8 ig_vlan_strip_en = 1;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002057 int err;
Scott Feldman68f71702009-02-09 23:24:24 -08002058
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002059 /* Enable VLAN tag stripping.
2060 */
Scott Feldman68f71702009-02-09 23:24:24 -08002061
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002062 spin_lock(&enic->devcmd_lock);
2063 err = enic_set_nic_cfg(enic,
Scott Feldman68f71702009-02-09 23:24:24 -08002064 rss_default_cpu, rss_hash_type,
2065 rss_hash_bits, rss_base_cpu,
2066 rss_enable, tso_ipid_split_en,
2067 ig_vlan_strip_en);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002068 spin_unlock(&enic->devcmd_lock);
2069
2070 return err;
2071}
2072
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002073static int enic_set_rss_nic_cfg(struct enic *enic)
2074{
2075 struct device *dev = enic_get_dev(enic);
2076 const u8 rss_default_cpu = 0;
2077 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
2078 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
2079 NIC_CFG_RSS_HASH_TYPE_IPV6 |
2080 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
2081 const u8 rss_hash_bits = 7;
2082 const u8 rss_base_cpu = 0;
2083 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
2084
2085 if (rss_enable) {
2086 if (!enic_set_rsskey(enic)) {
2087 if (enic_set_rsscpu(enic, rss_hash_bits)) {
2088 rss_enable = 0;
2089 dev_warn(dev, "RSS disabled, "
2090 "Failed to set RSS cpu indirection table.");
2091 }
2092 } else {
2093 rss_enable = 0;
2094 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
2095 }
2096 }
2097
2098 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
2099 rss_hash_bits, rss_base_cpu, rss_enable);
2100}
2101
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002102static void enic_reset(struct work_struct *work)
2103{
2104 struct enic *enic = container_of(work, struct enic, reset);
2105
2106 if (!netif_running(enic->netdev))
2107 return;
2108
2109 rtnl_lock();
2110
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002111 enic_dev_hang_notify(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002112 enic_stop(enic->netdev);
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00002113 enic_dev_hang_reset(enic);
2114 enic_reset_multicast_list(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002115 enic_init_vnic_resources(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002116 enic_set_rss_nic_cfg(enic);
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00002117 enic_dev_set_ig_vlan_rewrite_mode(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002118 enic_open(enic->netdev);
2119
2120 rtnl_unlock();
2121}
2122
2123static int enic_set_intr_mode(struct enic *enic)
2124{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002125 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
Scott Feldman6ba9cdc2009-09-03 17:02:24 +00002126 unsigned int m = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002127 unsigned int i;
2128
2129 /* Set interrupt mode (INTx, MSI, MSI-X) depending
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002130 * on system capabilities.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002131 *
2132 * Try MSI-X first
2133 *
2134 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2135 * (the second to last INTR is used for WQ/RQ errors)
2136 * (the last INTR is used for notifications)
2137 */
2138
2139 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2140 for (i = 0; i < n + m + 2; i++)
2141 enic->msix_entry[i].entry = i;
2142
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002143 /* Use multiple RQs if RSS is enabled
2144 */
2145
2146 if (ENIC_SETTING(enic, RSS) &&
2147 enic->config.intr_mode < 1 &&
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002148 enic->rq_count >= n &&
2149 enic->wq_count >= m &&
2150 enic->cq_count >= n + m &&
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002151 enic->intr_count >= n + m + 2) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002152
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002153 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002154
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002155 enic->rq_count = n;
2156 enic->wq_count = m;
2157 enic->cq_count = n + m;
2158 enic->intr_count = n + m + 2;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002159
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002160 vnic_dev_set_intr_mode(enic->vdev,
2161 VNIC_DEV_INTR_MODE_MSIX);
2162
2163 return 0;
2164 }
2165 }
2166
2167 if (enic->config.intr_mode < 1 &&
2168 enic->rq_count >= 1 &&
2169 enic->wq_count >= m &&
2170 enic->cq_count >= 1 + m &&
2171 enic->intr_count >= 1 + m + 2) {
2172 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2173
2174 enic->rq_count = 1;
2175 enic->wq_count = m;
2176 enic->cq_count = 1 + m;
2177 enic->intr_count = 1 + m + 2;
2178
2179 vnic_dev_set_intr_mode(enic->vdev,
2180 VNIC_DEV_INTR_MODE_MSIX);
2181
2182 return 0;
2183 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002184 }
2185
2186 /* Next try MSI
2187 *
2188 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2189 */
2190
2191 if (enic->config.intr_mode < 2 &&
2192 enic->rq_count >= 1 &&
2193 enic->wq_count >= 1 &&
2194 enic->cq_count >= 2 &&
2195 enic->intr_count >= 1 &&
2196 !pci_enable_msi(enic->pdev)) {
2197
2198 enic->rq_count = 1;
2199 enic->wq_count = 1;
2200 enic->cq_count = 2;
2201 enic->intr_count = 1;
2202
2203 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2204
2205 return 0;
2206 }
2207
2208 /* Next try INTx
2209 *
2210 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2211 * (the first INTR is used for WQ/RQ)
2212 * (the second INTR is used for WQ/RQ errors)
2213 * (the last INTR is used for notifications)
2214 */
2215
2216 if (enic->config.intr_mode < 3 &&
2217 enic->rq_count >= 1 &&
2218 enic->wq_count >= 1 &&
2219 enic->cq_count >= 2 &&
2220 enic->intr_count >= 3) {
2221
2222 enic->rq_count = 1;
2223 enic->wq_count = 1;
2224 enic->cq_count = 2;
2225 enic->intr_count = 3;
2226
2227 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2228
2229 return 0;
2230 }
2231
2232 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2233
2234 return -EINVAL;
2235}
2236
2237static void enic_clear_intr_mode(struct enic *enic)
2238{
2239 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2240 case VNIC_DEV_INTR_MODE_MSIX:
2241 pci_disable_msix(enic->pdev);
2242 break;
2243 case VNIC_DEV_INTR_MODE_MSI:
2244 pci_disable_msi(enic->pdev);
2245 break;
2246 default:
2247 break;
2248 }
2249
2250 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2251}
2252
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002253static const struct net_device_ops enic_netdev_dynamic_ops = {
2254 .ndo_open = enic_open,
2255 .ndo_stop = enic_stop,
2256 .ndo_start_xmit = enic_hard_start_xmit,
2257 .ndo_get_stats = enic_get_stats,
2258 .ndo_validate_addr = eth_validate_addr,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002259 .ndo_set_rx_mode = enic_set_rx_mode,
2260 .ndo_set_multicast_list = enic_set_rx_mode,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002261 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2262 .ndo_change_mtu = enic_change_mtu,
2263 .ndo_vlan_rx_register = enic_vlan_rx_register,
2264 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2265 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2266 .ndo_tx_timeout = enic_tx_timeout,
2267 .ndo_set_vf_port = enic_set_vf_port,
2268 .ndo_get_vf_port = enic_get_vf_port,
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00002269#ifdef IFLA_VF_MAX
2270 .ndo_set_vf_mac = enic_set_vf_mac,
2271#endif
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002272#ifdef CONFIG_NET_POLL_CONTROLLER
2273 .ndo_poll_controller = enic_poll_controller,
2274#endif
2275};
2276
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002277static const struct net_device_ops enic_netdev_ops = {
2278 .ndo_open = enic_open,
2279 .ndo_stop = enic_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -08002280 .ndo_start_xmit = enic_hard_start_xmit,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002281 .ndo_get_stats = enic_get_stats,
2282 .ndo_validate_addr = eth_validate_addr,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002283 .ndo_set_mac_address = enic_set_mac_address,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002284 .ndo_set_rx_mode = enic_set_rx_mode,
2285 .ndo_set_multicast_list = enic_set_rx_mode,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002286 .ndo_change_mtu = enic_change_mtu,
2287 .ndo_vlan_rx_register = enic_vlan_rx_register,
2288 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2289 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2290 .ndo_tx_timeout = enic_tx_timeout,
2291#ifdef CONFIG_NET_POLL_CONTROLLER
2292 .ndo_poll_controller = enic_poll_controller,
2293#endif
2294};
2295
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002296static void enic_dev_deinit(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002297{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002298 unsigned int i;
2299
2300 for (i = 0; i < enic->rq_count; i++)
2301 netif_napi_del(&enic->napi[i]);
2302
Scott Feldman6fdfa972009-09-03 17:02:45 +00002303 enic_free_vnic_resources(enic);
2304 enic_clear_intr_mode(enic);
2305}
2306
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002307static int enic_dev_init(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002308{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002309 struct device *dev = enic_get_dev(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002310 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002311 unsigned int i;
Scott Feldman6fdfa972009-09-03 17:02:45 +00002312 int err;
2313
2314 /* Get vNIC configuration
2315 */
2316
2317 err = enic_get_vnic_config(enic);
2318 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002319 dev_err(dev, "Get vNIC configuration failed, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002320 return err;
2321 }
2322
2323 /* Get available resource counts
2324 */
2325
2326 enic_get_res_counts(enic);
2327
2328 /* Set interrupt mode based on resource counts and system
2329 * capabilities
2330 */
2331
2332 err = enic_set_intr_mode(enic);
2333 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002334 dev_err(dev, "Failed to set intr mode based on resource "
2335 "counts and system capabilities, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002336 return err;
2337 }
2338
2339 /* Allocate and configure vNIC resources
2340 */
2341
2342 err = enic_alloc_vnic_resources(enic);
2343 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002344 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002345 goto err_out_free_vnic_resources;
2346 }
2347
2348 enic_init_vnic_resources(enic);
2349
2350 err = enic_set_rq_alloc_buf(enic);
2351 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002352 dev_err(dev, "Failed to set RQ buffer allocator, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002353 goto err_out_free_vnic_resources;
2354 }
2355
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002356 err = enic_set_rss_nic_cfg(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002357 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002358 dev_err(dev, "Failed to config nic, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002359 goto err_out_free_vnic_resources;
2360 }
2361
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00002362 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2363 if (err) {
Vasanthy Kolluri53c90532010-10-20 10:17:19 +00002364 dev_err(dev,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00002365 "Failed to set ingress vlan rewrite mode, aborting.\n");
2366 goto err_out_free_vnic_resources;
2367 }
2368
Scott Feldman6fdfa972009-09-03 17:02:45 +00002369 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2370 default:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002371 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002372 break;
2373 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002374 for (i = 0; i < enic->rq_count; i++)
2375 netif_napi_add(netdev, &enic->napi[i],
2376 enic_poll_msix, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002377 break;
2378 }
2379
2380 return 0;
2381
2382err_out_free_vnic_resources:
2383 enic_clear_intr_mode(enic);
2384 enic_free_vnic_resources(enic);
2385
2386 return err;
2387}
2388
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002389static void enic_iounmap(struct enic *enic)
2390{
2391 unsigned int i;
2392
2393 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2394 if (enic->bar[i].vaddr)
2395 iounmap(enic->bar[i].vaddr);
2396}
2397
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002398static int __devinit enic_probe(struct pci_dev *pdev,
2399 const struct pci_device_id *ent)
2400{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002401 struct device *dev = &pdev->dev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002402 struct net_device *netdev;
2403 struct enic *enic;
2404 int using_dac = 0;
2405 unsigned int i;
2406 int err;
2407
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002408 /* Allocate net device structure and initialize. Private
2409 * instance data is initialized to zero.
2410 */
2411
2412 netdev = alloc_etherdev(sizeof(struct enic));
2413 if (!netdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002414 pr_err("Etherdev alloc failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002415 return -ENOMEM;
2416 }
2417
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002418 pci_set_drvdata(pdev, netdev);
2419
2420 SET_NETDEV_DEV(netdev, &pdev->dev);
2421
2422 enic = netdev_priv(netdev);
2423 enic->netdev = netdev;
2424 enic->pdev = pdev;
2425
2426 /* Setup PCI resources
2427 */
2428
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002429 err = pci_enable_device_mem(pdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002430 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002431 dev_err(dev, "Cannot enable PCI device, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002432 goto err_out_free_netdev;
2433 }
2434
2435 err = pci_request_regions(pdev, DRV_NAME);
2436 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002437 dev_err(dev, "Cannot request PCI regions, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002438 goto err_out_disable_device;
2439 }
2440
2441 pci_set_master(pdev);
2442
2443 /* Query PCI controller on system for DMA addressing
2444 * limitation for the device. Try 40-bit first, and
2445 * fail to 32-bit.
2446 */
2447
Yang Hongyang50cf1562009-04-06 19:01:14 -07002448 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002449 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07002450 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002451 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002452 dev_err(dev, "No usable DMA configuration, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002453 goto err_out_release_regions;
2454 }
Yang Hongyang284901a2009-04-06 19:01:15 -07002455 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002456 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002457 dev_err(dev, "Unable to obtain %u-bit DMA "
2458 "for consistent allocations, aborting\n", 32);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002459 goto err_out_release_regions;
2460 }
2461 } else {
Yang Hongyang50cf1562009-04-06 19:01:14 -07002462 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002463 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002464 dev_err(dev, "Unable to obtain %u-bit DMA "
2465 "for consistent allocations, aborting\n", 40);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002466 goto err_out_release_regions;
2467 }
2468 using_dac = 1;
2469 }
2470
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002471 /* Map vNIC resources from BAR0-5
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002472 */
2473
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002474 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2475 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2476 continue;
2477 enic->bar[i].len = pci_resource_len(pdev, i);
2478 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2479 if (!enic->bar[i].vaddr) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002480 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002481 err = -ENODEV;
2482 goto err_out_iounmap;
2483 }
2484 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002485 }
2486
2487 /* Register vNIC device
2488 */
2489
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002490 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2491 ARRAY_SIZE(enic->bar));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002492 if (!enic->vdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002493 dev_err(dev, "vNIC registration failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002494 err = -ENODEV;
2495 goto err_out_iounmap;
2496 }
2497
2498 /* Issue device open to get device in known state
2499 */
2500
2501 err = enic_dev_open(enic);
2502 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002503 dev_err(dev, "vNIC dev open failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002504 goto err_out_vnic_unregister;
2505 }
2506
2507 /* Issue device init to initialize the vnic-to-switch link.
2508 * We'll start with carrier off and wait for link UP
2509 * notification later to turn on carrier. We don't need
2510 * to wait here for the vnic-to-switch link initialization
2511 * to complete; link UP notification is the indication that
2512 * the process is complete.
2513 */
2514
2515 netif_carrier_off(netdev);
2516
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002517 /* Do not call dev_init for a dynamic vnic.
2518 * For a dynamic vnic, init_prov_info will be
2519 * called later by an upper layer.
2520 */
2521
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002522 if (!enic_is_dynamic(enic)) {
2523 err = vnic_dev_init(enic->vdev, 0);
2524 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002525 dev_err(dev, "vNIC dev init failed, aborting\n");
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002526 goto err_out_dev_close;
2527 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002528 }
2529
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002530 /* Setup devcmd lock
2531 */
2532
2533 spin_lock_init(&enic->devcmd_lock);
2534
Scott Feldman6fdfa972009-09-03 17:02:45 +00002535 err = enic_dev_init(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002536 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002537 dev_err(dev, "Device initialization failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002538 goto err_out_dev_close;
2539 }
2540
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002541 /* Setup notification timer, HW reset task, and wq locks
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002542 */
2543
2544 init_timer(&enic->notify_timer);
2545 enic->notify_timer.function = enic_notify_timer;
2546 enic->notify_timer.data = (unsigned long)enic;
2547
2548 INIT_WORK(&enic->reset, enic_reset);
2549
2550 for (i = 0; i < enic->wq_count; i++)
2551 spin_lock_init(&enic->wq_lock[i]);
2552
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002553 /* Register net device
2554 */
2555
2556 enic->port_mtu = enic->config.mtu;
2557 (void)enic_change_mtu(netdev, enic->port_mtu);
2558
2559 err = enic_set_mac_addr(netdev, enic->mac_addr);
2560 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002561 dev_err(dev, "Invalid MAC address, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002562 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002563 }
2564
Scott Feldman7c844592009-12-23 13:27:54 +00002565 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2566 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2567
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002568 if (enic_is_dynamic(enic))
2569 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2570 else
2571 netdev->netdev_ops = &enic_netdev_ops;
2572
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002573 netdev->watchdog_timeo = 2 * HZ;
2574 netdev->ethtool_ops = &enic_ethtool_ops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002575
Vasanthy Kolluri73c1ea92010-03-18 16:19:54 +00002576 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00002577 if (ENIC_SETTING(enic, LOOP)) {
2578 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2579 enic->loop_enable = 1;
2580 enic->loop_tag = enic->config.loop_tag;
2581 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2582 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002583 if (ENIC_SETTING(enic, TXCSUM))
2584 netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2585 if (ENIC_SETTING(enic, TSO))
2586 netdev->features |= NETIF_F_TSO |
2587 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
Scott Feldman86ca9db2008-11-21 21:26:55 -08002588 if (ENIC_SETTING(enic, LRO))
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00002589 netdev->features |= NETIF_F_GRO;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002590 if (using_dac)
2591 netdev->features |= NETIF_F_HIGHDMA;
2592
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002593 enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
2594
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002595 err = register_netdev(netdev);
2596 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002597 dev_err(dev, "Cannot register net device, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002598 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002599 }
2600
2601 return 0;
2602
Scott Feldman6fdfa972009-09-03 17:02:45 +00002603err_out_dev_deinit:
2604 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002605err_out_dev_close:
2606 vnic_dev_close(enic->vdev);
2607err_out_vnic_unregister:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002608 vnic_dev_unregister(enic->vdev);
2609err_out_iounmap:
2610 enic_iounmap(enic);
2611err_out_release_regions:
2612 pci_release_regions(pdev);
2613err_out_disable_device:
2614 pci_disable_device(pdev);
2615err_out_free_netdev:
2616 pci_set_drvdata(pdev, NULL);
2617 free_netdev(netdev);
2618
2619 return err;
2620}
2621
2622static void __devexit enic_remove(struct pci_dev *pdev)
2623{
2624 struct net_device *netdev = pci_get_drvdata(pdev);
2625
2626 if (netdev) {
2627 struct enic *enic = netdev_priv(netdev);
2628
Tejun Heo23f333a2010-12-12 16:45:14 +01002629 cancel_work_sync(&enic->reset);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002630 unregister_netdev(netdev);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002631 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002632 vnic_dev_close(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002633 vnic_dev_unregister(enic->vdev);
2634 enic_iounmap(enic);
2635 pci_release_regions(pdev);
2636 pci_disable_device(pdev);
2637 pci_set_drvdata(pdev, NULL);
2638 free_netdev(netdev);
2639 }
2640}
2641
2642static struct pci_driver enic_driver = {
2643 .name = DRV_NAME,
2644 .id_table = enic_id_table,
2645 .probe = enic_probe,
2646 .remove = __devexit_p(enic_remove),
2647};
2648
2649static int __init enic_init_module(void)
2650{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002651 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002652
2653 return pci_register_driver(&enic_driver);
2654}
2655
2656static void __exit enic_cleanup_module(void)
2657{
2658 pci_unregister_driver(&enic_driver);
2659}
2660
2661module_init(enic_init_module);
2662module_exit(enic_cleanup_module);