blob: 9a3a0277bf218b48b323137d65880b8e12e0db5c [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"
Roopa Prabhub3abfbd2011-03-29 20:36:07 +000048#include "enic_pp.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070049
50#define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
Scott Feldmanea0d7d92009-09-03 17:02:03 +000051#define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
52#define MAX_TSO (1 << 16)
53#define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
54
55#define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
Scott Feldmanf8bd9092010-05-17 22:50:19 -070056#define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070057
58/* Supported devices */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000059static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
Scott Feldmanea0d7d92009-09-03 17:02:03 +000060 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
Scott Feldmanf8bd9092010-05-17 22:50:19 -070061 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
Scott Feldman01f2e4e2008-09-15 09:17:11 -070062 { 0, } /* end of table */
63};
64
65MODULE_DESCRIPTION(DRV_DESCRIPTION);
66MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
67MODULE_LICENSE("GPL");
68MODULE_VERSION(DRV_VERSION);
69MODULE_DEVICE_TABLE(pci, enic_id_table);
70
71struct enic_stat {
72 char name[ETH_GSTRING_LEN];
73 unsigned int offset;
74};
75
76#define ENIC_TX_STAT(stat) \
77 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
78#define ENIC_RX_STAT(stat) \
79 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
80
81static const struct enic_stat enic_tx_stats[] = {
82 ENIC_TX_STAT(tx_frames_ok),
83 ENIC_TX_STAT(tx_unicast_frames_ok),
84 ENIC_TX_STAT(tx_multicast_frames_ok),
85 ENIC_TX_STAT(tx_broadcast_frames_ok),
86 ENIC_TX_STAT(tx_bytes_ok),
87 ENIC_TX_STAT(tx_unicast_bytes_ok),
88 ENIC_TX_STAT(tx_multicast_bytes_ok),
89 ENIC_TX_STAT(tx_broadcast_bytes_ok),
90 ENIC_TX_STAT(tx_drops),
91 ENIC_TX_STAT(tx_errors),
92 ENIC_TX_STAT(tx_tso),
93};
94
95static const struct enic_stat enic_rx_stats[] = {
96 ENIC_RX_STAT(rx_frames_ok),
97 ENIC_RX_STAT(rx_frames_total),
98 ENIC_RX_STAT(rx_unicast_frames_ok),
99 ENIC_RX_STAT(rx_multicast_frames_ok),
100 ENIC_RX_STAT(rx_broadcast_frames_ok),
101 ENIC_RX_STAT(rx_bytes_ok),
102 ENIC_RX_STAT(rx_unicast_bytes_ok),
103 ENIC_RX_STAT(rx_multicast_bytes_ok),
104 ENIC_RX_STAT(rx_broadcast_bytes_ok),
105 ENIC_RX_STAT(rx_drop),
106 ENIC_RX_STAT(rx_no_bufs),
107 ENIC_RX_STAT(rx_errors),
108 ENIC_RX_STAT(rx_rss),
109 ENIC_RX_STAT(rx_crc_errors),
110 ENIC_RX_STAT(rx_frames_64),
111 ENIC_RX_STAT(rx_frames_127),
112 ENIC_RX_STAT(rx_frames_255),
113 ENIC_RX_STAT(rx_frames_511),
114 ENIC_RX_STAT(rx_frames_1023),
115 ENIC_RX_STAT(rx_frames_1518),
116 ENIC_RX_STAT(rx_frames_to_max),
117};
118
119static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
120static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
121
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700122static int enic_is_dynamic(struct enic *enic)
123{
124 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
125}
126
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000127static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
128{
129 return rq;
130}
131
132static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
133{
134 return enic->rq_count + wq;
135}
136
137static inline unsigned int enic_legacy_io_intr(void)
138{
139 return 0;
140}
141
142static inline unsigned int enic_legacy_err_intr(void)
143{
144 return 1;
145}
146
147static inline unsigned int enic_legacy_notify_intr(void)
148{
149 return 2;
150}
151
152static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
153{
154 return rq;
155}
156
157static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
158{
159 return enic->rq_count + wq;
160}
161
162static inline unsigned int enic_msix_err_intr(struct enic *enic)
163{
164 return enic->rq_count + enic->wq_count;
165}
166
167static inline unsigned int enic_msix_notify_intr(struct enic *enic)
168{
169 return enic->rq_count + enic->wq_count + 1;
170}
171
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700172static int enic_get_settings(struct net_device *netdev,
173 struct ethtool_cmd *ecmd)
174{
175 struct enic *enic = netdev_priv(netdev);
176
177 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
178 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
179 ecmd->port = PORT_FIBRE;
180 ecmd->transceiver = XCVR_EXTERNAL;
181
182 if (netif_carrier_ok(netdev)) {
183 ecmd->speed = vnic_dev_port_speed(enic->vdev);
184 ecmd->duplex = DUPLEX_FULL;
185 } else {
186 ecmd->speed = -1;
187 ecmd->duplex = -1;
188 }
189
190 ecmd->autoneg = AUTONEG_DISABLE;
191
192 return 0;
193}
194
195static void enic_get_drvinfo(struct net_device *netdev,
196 struct ethtool_drvinfo *drvinfo)
197{
198 struct enic *enic = netdev_priv(netdev);
199 struct vnic_devcmd_fw_info *fw_info;
200
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000201 enic_dev_fw_info(enic, &fw_info);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700202
203 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
204 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
205 strncpy(drvinfo->fw_version, fw_info->fw_version,
206 sizeof(drvinfo->fw_version));
207 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
208 sizeof(drvinfo->bus_info));
209}
210
211static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
212{
213 unsigned int i;
214
215 switch (stringset) {
216 case ETH_SS_STATS:
217 for (i = 0; i < enic_n_tx_stats; i++) {
218 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
219 data += ETH_GSTRING_LEN;
220 }
221 for (i = 0; i < enic_n_rx_stats; i++) {
222 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
223 data += ETH_GSTRING_LEN;
224 }
225 break;
226 }
227}
228
Scott Feldman25f0a062008-09-24 11:23:32 -0700229static int enic_get_sset_count(struct net_device *netdev, int sset)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700230{
Scott Feldman25f0a062008-09-24 11:23:32 -0700231 switch (sset) {
232 case ETH_SS_STATS:
233 return enic_n_tx_stats + enic_n_rx_stats;
234 default:
235 return -EOPNOTSUPP;
236 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700237}
238
239static void enic_get_ethtool_stats(struct net_device *netdev,
240 struct ethtool_stats *stats, u64 *data)
241{
242 struct enic *enic = netdev_priv(netdev);
243 struct vnic_stats *vstats;
244 unsigned int i;
245
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000246 enic_dev_stats_dump(enic, &vstats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700247
248 for (i = 0; i < enic_n_tx_stats; i++)
249 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
250 for (i = 0; i < enic_n_rx_stats; i++)
251 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
252}
253
254static u32 enic_get_rx_csum(struct net_device *netdev)
255{
256 struct enic *enic = netdev_priv(netdev);
257 return enic->csum_rx_enabled;
258}
259
260static int enic_set_rx_csum(struct net_device *netdev, u32 data)
261{
262 struct enic *enic = netdev_priv(netdev);
263
Scott Feldman25f0a062008-09-24 11:23:32 -0700264 if (data && !ENIC_SETTING(enic, RXCSUM))
265 return -EINVAL;
266
267 enic->csum_rx_enabled = !!data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700268
269 return 0;
270}
271
272static int enic_set_tx_csum(struct net_device *netdev, u32 data)
273{
274 struct enic *enic = netdev_priv(netdev);
275
Scott Feldman25f0a062008-09-24 11:23:32 -0700276 if (data && !ENIC_SETTING(enic, TXCSUM))
277 return -EINVAL;
278
279 if (data)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700280 netdev->features |= NETIF_F_HW_CSUM;
281 else
282 netdev->features &= ~NETIF_F_HW_CSUM;
283
284 return 0;
285}
286
287static int enic_set_tso(struct net_device *netdev, u32 data)
288{
289 struct enic *enic = netdev_priv(netdev);
290
Scott Feldman25f0a062008-09-24 11:23:32 -0700291 if (data && !ENIC_SETTING(enic, TSO))
292 return -EINVAL;
293
294 if (data)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700295 netdev->features |=
296 NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN;
297 else
298 netdev->features &=
299 ~(NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN);
300
301 return 0;
302}
303
304static u32 enic_get_msglevel(struct net_device *netdev)
305{
306 struct enic *enic = netdev_priv(netdev);
307 return enic->msg_enable;
308}
309
310static void enic_set_msglevel(struct net_device *netdev, u32 value)
311{
312 struct enic *enic = netdev_priv(netdev);
313 enic->msg_enable = value;
314}
315
Scott Feldman7c844592009-12-23 13:27:54 +0000316static int enic_get_coalesce(struct net_device *netdev,
317 struct ethtool_coalesce *ecmd)
318{
319 struct enic *enic = netdev_priv(netdev);
320
321 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
322 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
323
324 return 0;
325}
326
327static int enic_set_coalesce(struct net_device *netdev,
328 struct ethtool_coalesce *ecmd)
329{
330 struct enic *enic = netdev_priv(netdev);
331 u32 tx_coalesce_usecs;
332 u32 rx_coalesce_usecs;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000333 unsigned int i, intr;
Scott Feldman7c844592009-12-23 13:27:54 +0000334
335 tx_coalesce_usecs = min_t(u32,
336 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
337 ecmd->tx_coalesce_usecs);
338 rx_coalesce_usecs = min_t(u32,
339 INTR_COALESCE_HW_TO_USEC(VNIC_INTR_TIMER_MAX),
340 ecmd->rx_coalesce_usecs);
341
342 switch (vnic_dev_get_intr_mode(enic->vdev)) {
343 case VNIC_DEV_INTR_MODE_INTX:
344 if (tx_coalesce_usecs != rx_coalesce_usecs)
345 return -EINVAL;
346
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000347 intr = enic_legacy_io_intr();
348 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Scott Feldman7c844592009-12-23 13:27:54 +0000349 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
350 break;
351 case VNIC_DEV_INTR_MODE_MSI:
352 if (tx_coalesce_usecs != rx_coalesce_usecs)
353 return -EINVAL;
354
355 vnic_intr_coalescing_timer_set(&enic->intr[0],
356 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
357 break;
358 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000359 for (i = 0; i < enic->wq_count; i++) {
360 intr = enic_msix_wq_intr(enic, i);
361 vnic_intr_coalescing_timer_set(&enic->intr[intr],
362 INTR_COALESCE_USEC_TO_HW(tx_coalesce_usecs));
363 }
364
365 for (i = 0; i < enic->rq_count; i++) {
366 intr = enic_msix_rq_intr(enic, i);
367 vnic_intr_coalescing_timer_set(&enic->intr[intr],
368 INTR_COALESCE_USEC_TO_HW(rx_coalesce_usecs));
369 }
370
Scott Feldman7c844592009-12-23 13:27:54 +0000371 break;
372 default:
373 break;
374 }
375
376 enic->tx_coalesce_usecs = tx_coalesce_usecs;
377 enic->rx_coalesce_usecs = rx_coalesce_usecs;
378
379 return 0;
380}
381
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700382static const struct ethtool_ops enic_ethtool_ops = {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700383 .get_settings = enic_get_settings,
384 .get_drvinfo = enic_get_drvinfo,
385 .get_msglevel = enic_get_msglevel,
386 .set_msglevel = enic_set_msglevel,
387 .get_link = ethtool_op_get_link,
388 .get_strings = enic_get_strings,
Scott Feldman25f0a062008-09-24 11:23:32 -0700389 .get_sset_count = enic_get_sset_count,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700390 .get_ethtool_stats = enic_get_ethtool_stats,
391 .get_rx_csum = enic_get_rx_csum,
392 .set_rx_csum = enic_set_rx_csum,
393 .get_tx_csum = ethtool_op_get_tx_csum,
394 .set_tx_csum = enic_set_tx_csum,
395 .get_sg = ethtool_op_get_sg,
396 .set_sg = ethtool_op_set_sg,
397 .get_tso = ethtool_op_get_tso,
398 .set_tso = enic_set_tso,
Scott Feldman7c844592009-12-23 13:27:54 +0000399 .get_coalesce = enic_get_coalesce,
400 .set_coalesce = enic_set_coalesce,
Scott Feldman86ca9db2008-11-21 21:26:55 -0800401 .get_flags = ethtool_op_get_flags,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700402};
403
404static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
405{
406 struct enic *enic = vnic_dev_priv(wq->vdev);
407
408 if (buf->sop)
409 pci_unmap_single(enic->pdev, buf->dma_addr,
410 buf->len, PCI_DMA_TODEVICE);
411 else
412 pci_unmap_page(enic->pdev, buf->dma_addr,
413 buf->len, PCI_DMA_TODEVICE);
414
415 if (buf->os_buf)
416 dev_kfree_skb_any(buf->os_buf);
417}
418
419static void enic_wq_free_buf(struct vnic_wq *wq,
420 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
421{
422 enic_free_wq_buf(wq, buf);
423}
424
425static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
426 u8 type, u16 q_number, u16 completed_index, void *opaque)
427{
428 struct enic *enic = vnic_dev_priv(vdev);
429
430 spin_lock(&enic->wq_lock[q_number]);
431
432 vnic_wq_service(&enic->wq[q_number], cq_desc,
433 completed_index, enic_wq_free_buf,
434 opaque);
435
436 if (netif_queue_stopped(enic->netdev) &&
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000437 vnic_wq_desc_avail(&enic->wq[q_number]) >=
438 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700439 netif_wake_queue(enic->netdev);
440
441 spin_unlock(&enic->wq_lock[q_number]);
442
443 return 0;
444}
445
446static void enic_log_q_error(struct enic *enic)
447{
448 unsigned int i;
449 u32 error_status;
450
451 for (i = 0; i < enic->wq_count; i++) {
452 error_status = vnic_wq_error_status(&enic->wq[i]);
453 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000454 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
455 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700456 }
457
458 for (i = 0; i < enic->rq_count; i++) {
459 error_status = vnic_rq_error_status(&enic->rq[i]);
460 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000461 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
462 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700463 }
464}
465
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000466static void enic_msglvl_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700467{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000468 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700469
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000470 if (msg_enable != enic->msg_enable) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000471 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
472 enic->msg_enable, msg_enable);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000473 enic->msg_enable = msg_enable;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700474 }
475}
476
477static void enic_mtu_check(struct enic *enic)
478{
479 u32 mtu = vnic_dev_mtu(enic->vdev);
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000480 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700481
Scott Feldman491598a2009-09-03 17:02:40 +0000482 if (mtu && mtu != enic->port_mtu) {
Scott Feldman7c844592009-12-23 13:27:54 +0000483 enic->port_mtu = mtu;
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000484 if (mtu < netdev->mtu)
485 netdev_warn(netdev,
486 "interface MTU (%d) set higher "
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700487 "than switch port MTU (%d)\n",
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000488 netdev->mtu, mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700489 }
490}
491
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000492static void enic_link_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700493{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000494 int link_status = vnic_dev_link_status(enic->vdev);
495 int carrier_ok = netif_carrier_ok(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700496
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000497 if (link_status && !carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000498 netdev_info(enic->netdev, "Link UP\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000499 netif_carrier_on(enic->netdev);
500 } else if (!link_status && carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000501 netdev_info(enic->netdev, "Link DOWN\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000502 netif_carrier_off(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700503 }
504}
505
506static void enic_notify_check(struct enic *enic)
507{
508 enic_msglvl_check(enic);
509 enic_mtu_check(enic);
510 enic_link_check(enic);
511}
512
513#define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
514
515static irqreturn_t enic_isr_legacy(int irq, void *data)
516{
517 struct net_device *netdev = data;
518 struct enic *enic = netdev_priv(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000519 unsigned int io_intr = enic_legacy_io_intr();
520 unsigned int err_intr = enic_legacy_err_intr();
521 unsigned int notify_intr = enic_legacy_notify_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700522 u32 pba;
523
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000524 vnic_intr_mask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700525
526 pba = vnic_intr_legacy_pba(enic->legacy_pba);
527 if (!pba) {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000528 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700529 return IRQ_NONE; /* not our interrupt */
530 }
531
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000532 if (ENIC_TEST_INTR(pba, notify_intr)) {
533 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700534 enic_notify_check(enic);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800535 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700536
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000537 if (ENIC_TEST_INTR(pba, err_intr)) {
538 vnic_intr_return_all_credits(&enic->intr[err_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700539 enic_log_q_error(enic);
540 /* schedule recovery from WQ/RQ error */
541 schedule_work(&enic->reset);
542 return IRQ_HANDLED;
543 }
544
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000545 if (ENIC_TEST_INTR(pba, io_intr)) {
546 if (napi_schedule_prep(&enic->napi[0]))
547 __napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700548 } else {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000549 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700550 }
551
552 return IRQ_HANDLED;
553}
554
555static irqreturn_t enic_isr_msi(int irq, void *data)
556{
557 struct enic *enic = data;
558
559 /* With MSI, there is no sharing of interrupts, so this is
560 * our interrupt and there is no need to ack it. The device
561 * is not providing per-vector masking, so the OS will not
562 * write to PCI config space to mask/unmask the interrupt.
563 * We're using mask_on_assertion for MSI, so the device
564 * automatically masks the interrupt when the interrupt is
565 * generated. Later, when exiting polling, the interrupt
566 * will be unmasked (see enic_poll).
567 *
568 * Also, the device uses the same PCIe Traffic Class (TC)
569 * for Memory Write data and MSI, so there are no ordering
570 * issues; the MSI will always arrive at the Root Complex
571 * _after_ corresponding Memory Writes (i.e. descriptor
572 * writes).
573 */
574
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000575 napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700576
577 return IRQ_HANDLED;
578}
579
580static irqreturn_t enic_isr_msix_rq(int irq, void *data)
581{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000582 struct napi_struct *napi = data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700583
584 /* schedule NAPI polling for RQ cleanup */
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000585 napi_schedule(napi);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700586
587 return IRQ_HANDLED;
588}
589
590static irqreturn_t enic_isr_msix_wq(int irq, void *data)
591{
592 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000593 unsigned int cq = enic_cq_wq(enic, 0);
594 unsigned int intr = enic_msix_wq_intr(enic, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700595 unsigned int wq_work_to_do = -1; /* no limit */
596 unsigned int wq_work_done;
597
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000598 wq_work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700599 wq_work_to_do, enic_wq_service, NULL);
600
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000601 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700602 wq_work_done,
603 1 /* unmask intr */,
604 1 /* reset intr timer */);
605
606 return IRQ_HANDLED;
607}
608
609static irqreturn_t enic_isr_msix_err(int irq, void *data)
610{
611 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000612 unsigned int intr = enic_msix_err_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700613
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000614 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800615
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700616 enic_log_q_error(enic);
617
618 /* schedule recovery from WQ/RQ error */
619 schedule_work(&enic->reset);
620
621 return IRQ_HANDLED;
622}
623
624static irqreturn_t enic_isr_msix_notify(int irq, void *data)
625{
626 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000627 unsigned int intr = enic_msix_notify_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700628
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000629 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700630 enic_notify_check(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700631
632 return IRQ_HANDLED;
633}
634
635static inline void enic_queue_wq_skb_cont(struct enic *enic,
636 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000637 unsigned int len_left, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700638{
639 skb_frag_t *frag;
640
641 /* Queue additional data fragments */
642 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
643 len_left -= frag->size;
644 enic_queue_wq_desc_cont(wq, skb,
645 pci_map_page(enic->pdev, frag->page,
646 frag->page_offset, frag->size,
647 PCI_DMA_TODEVICE),
648 frag->size,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000649 (len_left == 0), /* EOP? */
650 loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700651 }
652}
653
654static inline void enic_queue_wq_skb_vlan(struct enic *enic,
655 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000656 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700657{
658 unsigned int head_len = skb_headlen(skb);
659 unsigned int len_left = skb->len - head_len;
660 int eop = (len_left == 0);
661
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000662 /* Queue the main skb fragment. The fragments are no larger
663 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
664 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
665 * per fragment is queued.
666 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700667 enic_queue_wq_desc(wq, skb,
668 pci_map_single(enic->pdev, skb->data,
669 head_len, PCI_DMA_TODEVICE),
670 head_len,
671 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000672 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700673
674 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000675 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700676}
677
678static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
679 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000680 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700681{
682 unsigned int head_len = skb_headlen(skb);
683 unsigned int len_left = skb->len - head_len;
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000684 unsigned int hdr_len = skb_checksum_start_offset(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700685 unsigned int csum_offset = hdr_len + skb->csum_offset;
686 int eop = (len_left == 0);
687
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000688 /* Queue the main skb fragment. The fragments are no larger
689 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
690 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
691 * per fragment is queued.
692 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700693 enic_queue_wq_desc_csum_l4(wq, skb,
694 pci_map_single(enic->pdev, skb->data,
695 head_len, PCI_DMA_TODEVICE),
696 head_len,
697 csum_offset,
698 hdr_len,
699 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000700 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700701
702 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000703 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700704}
705
706static inline void enic_queue_wq_skb_tso(struct enic *enic,
707 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000708 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700709{
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000710 unsigned int frag_len_left = skb_headlen(skb);
711 unsigned int len_left = skb->len - frag_len_left;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700712 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
713 int eop = (len_left == 0);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000714 unsigned int len;
715 dma_addr_t dma_addr;
716 unsigned int offset = 0;
717 skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700718
719 /* Preload TCP csum field with IP pseudo hdr calculated
720 * with IP length set to zero. HW will later add in length
721 * to each TCP segment resulting from the TSO.
722 */
723
Harvey Harrison09640e62009-02-01 00:45:17 -0800724 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700725 ip_hdr(skb)->check = 0;
726 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
727 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
Harvey Harrison09640e62009-02-01 00:45:17 -0800728 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700729 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
730 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
731 }
732
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000733 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
734 * for the main skb fragment
735 */
736 while (frag_len_left) {
737 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
738 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
739 len, PCI_DMA_TODEVICE);
740 enic_queue_wq_desc_tso(wq, skb,
741 dma_addr,
742 len,
743 mss, hdr_len,
744 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000745 eop && (len == frag_len_left), loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000746 frag_len_left -= len;
747 offset += len;
748 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700749
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000750 if (eop)
751 return;
752
753 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
754 * for additional data fragments
755 */
756 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
757 len_left -= frag->size;
758 frag_len_left = frag->size;
759 offset = frag->page_offset;
760
761 while (frag_len_left) {
762 len = min(frag_len_left,
763 (unsigned int)WQ_ENET_MAX_DESC_LEN);
764 dma_addr = pci_map_page(enic->pdev, frag->page,
765 offset, len,
766 PCI_DMA_TODEVICE);
767 enic_queue_wq_desc_cont(wq, skb,
768 dma_addr,
769 len,
770 (len_left == 0) &&
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000771 (len == frag_len_left), /* EOP? */
772 loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000773 frag_len_left -= len;
774 offset += len;
775 }
776 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700777}
778
779static inline void enic_queue_wq_skb(struct enic *enic,
780 struct vnic_wq *wq, struct sk_buff *skb)
781{
782 unsigned int mss = skb_shinfo(skb)->gso_size;
783 unsigned int vlan_tag = 0;
784 int vlan_tag_insert = 0;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000785 int loopback = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700786
Jesse Grosseab6d182010-10-20 13:56:03 +0000787 if (vlan_tx_tag_present(skb)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700788 /* VLAN tag from trunking driver */
789 vlan_tag_insert = 1;
790 vlan_tag = vlan_tx_tag_get(skb);
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000791 } else if (enic->loop_enable) {
792 vlan_tag = enic->loop_tag;
793 loopback = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700794 }
795
796 if (mss)
797 enic_queue_wq_skb_tso(enic, wq, skb, mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000798 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700799 else if (skb->ip_summed == CHECKSUM_PARTIAL)
800 enic_queue_wq_skb_csum_l4(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000801 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700802 else
803 enic_queue_wq_skb_vlan(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000804 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700805}
806
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800807/* netif_tx_lock held, process context with BHs disabled, or BH */
Stephen Hemminger613573252009-08-31 19:50:58 +0000808static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
Scott Feldmand87fd252009-12-23 13:27:59 +0000809 struct net_device *netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700810{
811 struct enic *enic = netdev_priv(netdev);
812 struct vnic_wq *wq = &enic->wq[0];
813 unsigned long flags;
814
815 if (skb->len <= 0) {
816 dev_kfree_skb(skb);
817 return NETDEV_TX_OK;
818 }
819
820 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
821 * which is very likely. In the off chance it's going to take
822 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
823 */
824
825 if (skb_shinfo(skb)->gso_size == 0 &&
826 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
827 skb_linearize(skb)) {
828 dev_kfree_skb(skb);
829 return NETDEV_TX_OK;
830 }
831
832 spin_lock_irqsave(&enic->wq_lock[0], flags);
833
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000834 if (vnic_wq_desc_avail(wq) <
835 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700836 netif_stop_queue(netdev);
837 /* This is a hard error, log it */
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000838 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700839 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
840 return NETDEV_TX_BUSY;
841 }
842
843 enic_queue_wq_skb(enic, wq, skb);
844
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000845 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700846 netif_stop_queue(netdev);
847
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700848 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
849
850 return NETDEV_TX_OK;
851}
852
853/* dev_base_lock rwlock held, nominally process context */
854static struct net_device_stats *enic_get_stats(struct net_device *netdev)
855{
856 struct enic *enic = netdev_priv(netdev);
Scott Feldman25f0a062008-09-24 11:23:32 -0700857 struct net_device_stats *net_stats = &netdev->stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700858 struct vnic_stats *stats;
859
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000860 enic_dev_stats_dump(enic, &stats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700861
Scott Feldman25f0a062008-09-24 11:23:32 -0700862 net_stats->tx_packets = stats->tx.tx_frames_ok;
863 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
864 net_stats->tx_errors = stats->tx.tx_errors;
865 net_stats->tx_dropped = stats->tx.tx_drops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700866
Scott Feldman25f0a062008-09-24 11:23:32 -0700867 net_stats->rx_packets = stats->rx.rx_frames_ok;
868 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
869 net_stats->rx_errors = stats->rx.rx_errors;
870 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
Scott Feldman350991e2009-09-03 17:02:19 +0000871 net_stats->rx_over_errors = enic->rq_truncated_pkts;
Scott Feldmanbd9fb1a2009-02-09 23:24:08 -0800872 net_stats->rx_crc_errors = enic->rq_bad_fcs;
Scott Feldman350991e2009-09-03 17:02:19 +0000873 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700874
Scott Feldman25f0a062008-09-24 11:23:32 -0700875 return net_stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700876}
877
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000878void enic_reset_addr_lists(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700879{
880 enic->mc_count = 0;
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000881 enic->uc_count = 0;
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000882 enic->flags = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700883}
884
885static int enic_set_mac_addr(struct net_device *netdev, char *addr)
886{
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700887 struct enic *enic = netdev_priv(netdev);
888
889 if (enic_is_dynamic(enic)) {
890 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
891 return -EADDRNOTAVAIL;
892 } else {
893 if (!is_valid_ether_addr(addr))
894 return -EADDRNOTAVAIL;
895 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700896
897 memcpy(netdev->dev_addr, addr, netdev->addr_len);
898
899 return 0;
900}
901
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700902static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
903{
904 struct enic *enic = netdev_priv(netdev);
905 struct sockaddr *saddr = p;
906 char *addr = saddr->sa_data;
907 int err;
908
909 if (netif_running(enic->netdev)) {
910 err = enic_dev_del_station_addr(enic);
911 if (err)
912 return err;
913 }
914
915 err = enic_set_mac_addr(netdev, addr);
916 if (err)
917 return err;
918
919 if (netif_running(enic->netdev)) {
920 err = enic_dev_add_station_addr(enic);
921 if (err)
922 return err;
923 }
924
925 return err;
926}
927
928static int enic_set_mac_address(struct net_device *netdev, void *p)
929{
Roopa Prabhu294dab22010-08-10 18:54:55 +0000930 struct sockaddr *saddr = p;
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000931 char *addr = saddr->sa_data;
932 struct enic *enic = netdev_priv(netdev);
933 int err;
Roopa Prabhu294dab22010-08-10 18:54:55 +0000934
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000935 err = enic_dev_del_station_addr(enic);
936 if (err)
937 return err;
938
939 err = enic_set_mac_addr(netdev, addr);
940 if (err)
941 return err;
942
943 return enic_dev_add_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700944}
945
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000946static void enic_update_multicast_addr_list(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700947{
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000948 struct net_device *netdev = enic->netdev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000949 struct netdev_hw_addr *ha;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000950 unsigned int mc_count = netdev_mc_count(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700951 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700952 unsigned int i, j;
953
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000954 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
955 netdev_warn(netdev, "Registering only %d out of %d "
956 "multicast addresses\n",
957 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700958 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
Scott Feldman9959a182009-12-23 13:27:43 +0000959 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700960
961 /* Is there an easier way? Trying to minimize to
962 * calls to add/del multicast addrs. We keep the
963 * addrs from the last call in enic->mc_addr and
964 * look for changes to add/del.
965 */
966
Jiri Pirko48e2f182010-02-22 09:22:26 +0000967 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000968 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko48e2f182010-02-22 09:22:26 +0000969 if (i == mc_count)
970 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000971 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700972 }
973
974 for (i = 0; i < enic->mc_count; i++) {
975 for (j = 0; j < mc_count; j++)
976 if (compare_ether_addr(enic->mc_addr[i],
977 mc_addr[j]) == 0)
978 break;
979 if (j == mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000980 enic_dev_del_addr(enic, enic->mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700981 }
982
983 for (i = 0; i < mc_count; i++) {
984 for (j = 0; j < enic->mc_count; j++)
985 if (compare_ether_addr(mc_addr[i],
986 enic->mc_addr[j]) == 0)
987 break;
988 if (j == enic->mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000989 enic_dev_add_addr(enic, mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700990 }
991
992 /* Save the list to compare against next time
993 */
994
995 for (i = 0; i < mc_count; i++)
996 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
997
998 enic->mc_count = mc_count;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700999}
1000
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001001static void enic_update_unicast_addr_list(struct enic *enic)
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001002{
1003 struct net_device *netdev = enic->netdev;
1004 struct netdev_hw_addr *ha;
1005 unsigned int uc_count = netdev_uc_count(netdev);
1006 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
1007 unsigned int i, j;
1008
1009 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
1010 netdev_warn(netdev, "Registering only %d out of %d "
1011 "unicast addresses\n",
1012 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
1013 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
1014 }
1015
1016 /* Is there an easier way? Trying to minimize to
1017 * calls to add/del unicast addrs. We keep the
1018 * addrs from the last call in enic->uc_addr and
1019 * look for changes to add/del.
1020 */
1021
1022 i = 0;
1023 netdev_for_each_uc_addr(ha, netdev) {
1024 if (i == uc_count)
1025 break;
1026 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
1027 }
1028
1029 for (i = 0; i < enic->uc_count; i++) {
1030 for (j = 0; j < uc_count; j++)
1031 if (compare_ether_addr(enic->uc_addr[i],
1032 uc_addr[j]) == 0)
1033 break;
1034 if (j == uc_count)
1035 enic_dev_del_addr(enic, enic->uc_addr[i]);
1036 }
1037
1038 for (i = 0; i < uc_count; i++) {
1039 for (j = 0; j < enic->uc_count; j++)
1040 if (compare_ether_addr(uc_addr[i],
1041 enic->uc_addr[j]) == 0)
1042 break;
1043 if (j == enic->uc_count)
1044 enic_dev_add_addr(enic, uc_addr[i]);
1045 }
1046
1047 /* Save the list to compare against next time
1048 */
1049
1050 for (i = 0; i < uc_count; i++)
1051 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1052
1053 enic->uc_count = uc_count;
1054}
1055
1056/* netif_tx_lock held, BHs disabled */
1057static void enic_set_rx_mode(struct net_device *netdev)
1058{
1059 struct enic *enic = netdev_priv(netdev);
1060 int directed = 1;
1061 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1062 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1063 int promisc = (netdev->flags & IFF_PROMISC) ||
1064 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1065 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1066 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1067 unsigned int flags = netdev->flags |
1068 (allmulti ? IFF_ALLMULTI : 0) |
1069 (promisc ? IFF_PROMISC : 0);
1070
1071 if (enic->flags != flags) {
1072 enic->flags = flags;
1073 enic_dev_packet_filter(enic, directed,
1074 multicast, broadcast, promisc, allmulti);
1075 }
1076
1077 if (!promisc) {
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001078 enic_update_unicast_addr_list(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001079 if (!allmulti)
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001080 enic_update_multicast_addr_list(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001081 }
1082}
1083
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001084/* rtnl lock is held */
1085static void enic_vlan_rx_register(struct net_device *netdev,
1086 struct vlan_group *vlan_group)
1087{
1088 struct enic *enic = netdev_priv(netdev);
1089 enic->vlan_group = vlan_group;
1090}
1091
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001092/* netif_tx_lock held, BHs disabled */
1093static void enic_tx_timeout(struct net_device *netdev)
1094{
1095 struct enic *enic = netdev_priv(netdev);
1096 schedule_work(&enic->reset);
1097}
1098
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001099static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1100{
1101 struct enic *enic = netdev_priv(netdev);
1102
1103 if (vf != PORT_SELF_VF)
1104 return -EOPNOTSUPP;
1105
1106 /* Ignore the vf argument for now. We can assume the request
1107 * is coming on a vf.
1108 */
1109 if (is_valid_ether_addr(mac)) {
1110 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1111 return 0;
1112 } else
1113 return -EINVAL;
1114}
1115
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001116static int enic_set_vf_port(struct net_device *netdev, int vf,
1117 struct nlattr *port[])
1118{
1119 struct enic *enic = netdev_priv(netdev);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001120 struct enic_port_profile prev_pp;
1121 int err = 0, restore_pp = 1;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001122
1123 /* don't support VFs, yet */
1124 if (vf != PORT_SELF_VF)
1125 return -EOPNOTSUPP;
1126
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001127 if (!port[IFLA_PORT_REQUEST])
Scott Feldman08f382e2010-06-01 08:59:33 +00001128 return -EOPNOTSUPP;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001129
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001130 memcpy(&prev_pp, &enic->pp, sizeof(enic->pp));
1131 memset(&enic->pp, 0, sizeof(enic->pp));
1132
1133 enic->pp.set |= ENIC_SET_REQUEST;
1134 enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1135
1136 if (port[IFLA_PORT_PROFILE]) {
1137 enic->pp.set |= ENIC_SET_NAME;
1138 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
1139 PORT_PROFILE_MAX);
1140 }
1141
1142 if (port[IFLA_PORT_INSTANCE_UUID]) {
1143 enic->pp.set |= ENIC_SET_INSTANCE;
1144 memcpy(enic->pp.instance_uuid,
1145 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1146 }
1147
1148 if (port[IFLA_PORT_HOST_UUID]) {
1149 enic->pp.set |= ENIC_SET_HOST;
1150 memcpy(enic->pp.host_uuid,
1151 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1152 }
1153
1154 /* Special case handling: mac came from IFLA_VF_MAC */
1155 if (!is_zero_ether_addr(prev_pp.vf_mac))
1156 memcpy(enic->pp.mac_addr, prev_pp.vf_mac, ETH_ALEN);
Scott Feldman418c4372010-05-22 17:29:58 +00001157
1158 if (is_zero_ether_addr(netdev->dev_addr))
1159 random_ether_addr(netdev->dev_addr);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001160
1161 err = enic_process_set_pp_request(enic, &prev_pp, &restore_pp);
1162 if (err) {
1163 if (restore_pp) {
1164 /* Things are still the way they were: Implicit
1165 * DISASSOCIATE failed
1166 */
1167 memcpy(&enic->pp, &prev_pp, sizeof(enic->pp));
1168 } else {
1169 memset(&enic->pp, 0, sizeof(enic->pp));
1170 memset(netdev->dev_addr, 0, ETH_ALEN);
1171 }
1172 } else {
1173 /* Set flag to indicate that the port assoc/disassoc
1174 * request has been sent out to fw
1175 */
1176 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1177
1178 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1179 if (enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1180 memset(enic->pp.mac_addr, 0, ETH_ALEN);
1181 memset(netdev->dev_addr, 0, ETH_ALEN);
1182 }
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001183 }
1184
Roopa Prabhu296390592010-12-08 13:54:03 +00001185 memset(enic->pp.vf_mac, 0, ETH_ALEN);
1186
Roopa Prabhu296390592010-12-08 13:54:03 +00001187 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001188}
1189
1190static int enic_get_vf_port(struct net_device *netdev, int vf,
1191 struct sk_buff *skb)
1192{
1193 struct enic *enic = netdev_priv(netdev);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001194 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001195 int err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001196
Roopa Prabhu4dce2392011-01-20 14:35:54 +00001197 if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
Scott Feldman08f382e2010-06-01 08:59:33 +00001198 return -ENODATA;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001199
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001200 err = enic_process_get_pp_request(enic, enic->pp.request, &response);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001201 if (err)
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001202 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001203
1204 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1205 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
Scott Feldman08f382e2010-06-01 08:59:33 +00001206 if (enic->pp.set & ENIC_SET_NAME)
1207 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1208 enic->pp.name);
1209 if (enic->pp.set & ENIC_SET_INSTANCE)
1210 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1211 enic->pp.instance_uuid);
1212 if (enic->pp.set & ENIC_SET_HOST)
1213 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1214 enic->pp.host_uuid);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001215
1216 return 0;
1217
1218nla_put_failure:
1219 return -EMSGSIZE;
1220}
1221
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001222static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1223{
1224 struct enic *enic = vnic_dev_priv(rq->vdev);
1225
1226 if (!buf->os_buf)
1227 return;
1228
1229 pci_unmap_single(enic->pdev, buf->dma_addr,
1230 buf->len, PCI_DMA_FROMDEVICE);
1231 dev_kfree_skb_any(buf->os_buf);
1232}
1233
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001234static int enic_rq_alloc_buf(struct vnic_rq *rq)
1235{
1236 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldmand19e22d2009-09-03 17:02:08 +00001237 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001238 struct sk_buff *skb;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00001239 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001240 unsigned int os_buf_index = 0;
1241 dma_addr_t dma_addr;
1242
Eric Dumazet89d71a62009-10-13 05:34:20 +00001243 skb = netdev_alloc_skb_ip_align(netdev, len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001244 if (!skb)
1245 return -ENOMEM;
1246
1247 dma_addr = pci_map_single(enic->pdev, skb->data,
1248 len, PCI_DMA_FROMDEVICE);
1249
1250 enic_queue_rq_desc(rq, skb, os_buf_index,
1251 dma_addr, len);
1252
1253 return 0;
1254}
1255
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001256static void enic_rq_indicate_buf(struct vnic_rq *rq,
1257 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1258 int skipped, void *opaque)
1259{
1260 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001261 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001262 struct sk_buff *skb;
1263
1264 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1265 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1266 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1267 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1268 u8 packet_error;
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001269 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001270 u32 rss_hash;
1271
1272 if (skipped)
1273 return;
1274
1275 skb = buf->os_buf;
1276 prefetch(skb->data - NET_IP_ALIGN);
1277 pci_unmap_single(enic->pdev, buf->dma_addr,
1278 buf->len, PCI_DMA_FROMDEVICE);
1279
1280 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1281 &type, &color, &q_number, &completed_index,
1282 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1283 &csum_not_calc, &rss_hash, &bytes_written,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001284 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001285 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1286 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1287 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1288 &fcs_ok);
1289
1290 if (packet_error) {
1291
Scott Feldman350991e2009-09-03 17:02:19 +00001292 if (!fcs_ok) {
1293 if (bytes_written > 0)
1294 enic->rq_bad_fcs++;
1295 else if (bytes_written == 0)
1296 enic->rq_truncated_pkts++;
1297 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001298
1299 dev_kfree_skb_any(skb);
1300
1301 return;
1302 }
1303
1304 if (eop && bytes_written > 0) {
1305
1306 /* Good receive
1307 */
1308
1309 skb_put(skb, bytes_written);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001310 skb->protocol = eth_type_trans(skb, netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001311
1312 if (enic->csum_rx_enabled && !csum_not_calc) {
1313 skb->csum = htons(checksum);
1314 skb->ip_summed = CHECKSUM_COMPLETE;
1315 }
1316
Scott Feldman86ca9db2008-11-21 21:26:55 -08001317 skb->dev = netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001318
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001319 if (enic->vlan_group && vlan_stripped &&
1320 (vlan_tci & CQ_ENET_RQ_DESC_VLAN_TCI_VLAN_MASK)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001321
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001322 if (netdev->features & NETIF_F_GRO)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001323 vlan_gro_receive(&enic->napi[q_number],
1324 enic->vlan_group, vlan_tci, skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001325 else
1326 vlan_hwaccel_receive_skb(skb,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001327 enic->vlan_group, vlan_tci);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001328
1329 } else {
1330
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001331 if (netdev->features & NETIF_F_GRO)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001332 napi_gro_receive(&enic->napi[q_number], skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001333 else
1334 netif_receive_skb(skb);
1335
1336 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001337 } else {
1338
1339 /* Buffer overflow
1340 */
1341
1342 dev_kfree_skb_any(skb);
1343 }
1344}
1345
1346static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1347 u8 type, u16 q_number, u16 completed_index, void *opaque)
1348{
1349 struct enic *enic = vnic_dev_priv(vdev);
1350
1351 vnic_rq_service(&enic->rq[q_number], cq_desc,
1352 completed_index, VNIC_RQ_RETURN_DESC,
1353 enic_rq_indicate_buf, opaque);
1354
1355 return 0;
1356}
1357
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001358static int enic_poll(struct napi_struct *napi, int budget)
1359{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001360 struct net_device *netdev = napi->dev;
1361 struct enic *enic = netdev_priv(netdev);
1362 unsigned int cq_rq = enic_cq_rq(enic, 0);
1363 unsigned int cq_wq = enic_cq_wq(enic, 0);
1364 unsigned int intr = enic_legacy_io_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001365 unsigned int rq_work_to_do = budget;
1366 unsigned int wq_work_to_do = -1; /* no limit */
1367 unsigned int work_done, rq_work_done, wq_work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001368 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001369
1370 /* Service RQ (first) and WQ
1371 */
1372
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001373 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001374 rq_work_to_do, enic_rq_service, NULL);
1375
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001376 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001377 wq_work_to_do, enic_wq_service, NULL);
1378
1379 /* Accumulate intr event credits for this polling
1380 * cycle. An intr event is the completion of a
1381 * a WQ or RQ packet.
1382 */
1383
1384 work_done = rq_work_done + wq_work_done;
1385
1386 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001387 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001388 work_done,
1389 0 /* don't unmask intr */,
1390 0 /* don't reset intr timer */);
1391
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001392 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001393
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001394 /* Buffer allocation failed. Stay in polling
1395 * mode so we can try to fill the ring again.
1396 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001397
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001398 if (err)
1399 rq_work_done = rq_work_to_do;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001400
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001401 if (rq_work_done < rq_work_to_do) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001402
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001403 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001404 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001405 */
1406
Ben Hutchings288379f2009-01-19 16:43:59 -08001407 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001408 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001409 }
1410
1411 return rq_work_done;
1412}
1413
1414static int enic_poll_msix(struct napi_struct *napi, int budget)
1415{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001416 struct net_device *netdev = napi->dev;
1417 struct enic *enic = netdev_priv(netdev);
1418 unsigned int rq = (napi - &enic->napi[0]);
1419 unsigned int cq = enic_cq_rq(enic, rq);
1420 unsigned int intr = enic_msix_rq_intr(enic, rq);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001421 unsigned int work_to_do = budget;
1422 unsigned int work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001423 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001424
1425 /* Service RQ
1426 */
1427
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001428 work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001429 work_to_do, enic_rq_service, NULL);
1430
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001431 /* Return intr event credits for this polling
1432 * cycle. An intr event is the completion of a
1433 * RQ packet.
1434 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001435
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001436 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001437 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001438 work_done,
1439 0 /* don't unmask intr */,
1440 0 /* don't reset intr timer */);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001441
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001442 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001443
1444 /* Buffer allocation failed. Stay in polling mode
1445 * so we can try to fill the ring again.
1446 */
1447
1448 if (err)
1449 work_done = work_to_do;
1450
1451 if (work_done < work_to_do) {
1452
1453 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001454 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001455 */
1456
Ben Hutchings288379f2009-01-19 16:43:59 -08001457 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001458 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001459 }
1460
1461 return work_done;
1462}
1463
1464static void enic_notify_timer(unsigned long data)
1465{
1466 struct enic *enic = (struct enic *)data;
1467
1468 enic_notify_check(enic);
1469
Scott Feldman25f0a062008-09-24 11:23:32 -07001470 mod_timer(&enic->notify_timer,
1471 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001472}
1473
1474static void enic_free_intr(struct enic *enic)
1475{
1476 struct net_device *netdev = enic->netdev;
1477 unsigned int i;
1478
1479 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1480 case VNIC_DEV_INTR_MODE_INTX:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001481 free_irq(enic->pdev->irq, netdev);
1482 break;
Scott Feldman8f4d2482008-09-24 11:23:42 -07001483 case VNIC_DEV_INTR_MODE_MSI:
1484 free_irq(enic->pdev->irq, enic);
1485 break;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001486 case VNIC_DEV_INTR_MODE_MSIX:
1487 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1488 if (enic->msix[i].requested)
1489 free_irq(enic->msix_entry[i].vector,
1490 enic->msix[i].devid);
1491 break;
1492 default:
1493 break;
1494 }
1495}
1496
1497static int enic_request_intr(struct enic *enic)
1498{
1499 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001500 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001501 int err = 0;
1502
1503 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1504
1505 case VNIC_DEV_INTR_MODE_INTX:
1506
1507 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1508 IRQF_SHARED, netdev->name, netdev);
1509 break;
1510
1511 case VNIC_DEV_INTR_MODE_MSI:
1512
1513 err = request_irq(enic->pdev->irq, enic_isr_msi,
1514 0, netdev->name, enic);
1515 break;
1516
1517 case VNIC_DEV_INTR_MODE_MSIX:
1518
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001519 for (i = 0; i < enic->rq_count; i++) {
1520 intr = enic_msix_rq_intr(enic, i);
1521 sprintf(enic->msix[intr].devname,
1522 "%.11s-rx-%d", netdev->name, i);
1523 enic->msix[intr].isr = enic_isr_msix_rq;
1524 enic->msix[intr].devid = &enic->napi[i];
1525 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001526
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001527 for (i = 0; i < enic->wq_count; i++) {
1528 intr = enic_msix_wq_intr(enic, i);
1529 sprintf(enic->msix[intr].devname,
1530 "%.11s-tx-%d", netdev->name, i);
1531 enic->msix[intr].isr = enic_isr_msix_wq;
1532 enic->msix[intr].devid = enic;
1533 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001534
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001535 intr = enic_msix_err_intr(enic);
1536 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001537 "%.11s-err", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001538 enic->msix[intr].isr = enic_isr_msix_err;
1539 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001540
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001541 intr = enic_msix_notify_intr(enic);
1542 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001543 "%.11s-notify", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001544 enic->msix[intr].isr = enic_isr_msix_notify;
1545 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001546
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001547 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1548 enic->msix[i].requested = 0;
1549
1550 for (i = 0; i < enic->intr_count; i++) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001551 err = request_irq(enic->msix_entry[i].vector,
1552 enic->msix[i].isr, 0,
1553 enic->msix[i].devname,
1554 enic->msix[i].devid);
1555 if (err) {
1556 enic_free_intr(enic);
1557 break;
1558 }
1559 enic->msix[i].requested = 1;
1560 }
1561
1562 break;
1563
1564 default:
1565 break;
1566 }
1567
1568 return err;
1569}
1570
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001571static void enic_synchronize_irqs(struct enic *enic)
1572{
1573 unsigned int i;
1574
1575 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1576 case VNIC_DEV_INTR_MODE_INTX:
1577 case VNIC_DEV_INTR_MODE_MSI:
1578 synchronize_irq(enic->pdev->irq);
1579 break;
1580 case VNIC_DEV_INTR_MODE_MSIX:
1581 for (i = 0; i < enic->intr_count; i++)
1582 synchronize_irq(enic->msix_entry[i].vector);
1583 break;
1584 default:
1585 break;
1586 }
1587}
1588
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001589static int enic_dev_notify_set(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001590{
1591 int err;
1592
Scott Feldman56ac88b2009-09-03 17:02:14 +00001593 spin_lock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001594 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1595 case VNIC_DEV_INTR_MODE_INTX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001596 err = vnic_dev_notify_set(enic->vdev,
1597 enic_legacy_notify_intr());
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001598 break;
1599 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001600 err = vnic_dev_notify_set(enic->vdev,
1601 enic_msix_notify_intr(enic));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001602 break;
1603 default:
1604 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1605 break;
1606 }
Scott Feldman56ac88b2009-09-03 17:02:14 +00001607 spin_unlock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001608
1609 return err;
1610}
1611
1612static void enic_notify_timer_start(struct enic *enic)
1613{
1614 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1615 case VNIC_DEV_INTR_MODE_MSI:
1616 mod_timer(&enic->notify_timer, jiffies);
1617 break;
1618 default:
1619 /* Using intr for notification for INTx/MSI-X */
1620 break;
1621 };
1622}
1623
1624/* rtnl lock is held, process context */
1625static int enic_open(struct net_device *netdev)
1626{
1627 struct enic *enic = netdev_priv(netdev);
1628 unsigned int i;
1629 int err;
1630
Scott Feldman4b75a442008-09-24 11:23:53 -07001631 err = enic_request_intr(enic);
1632 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001633 netdev_err(netdev, "Unable to request irq.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001634 return err;
1635 }
1636
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001637 err = enic_dev_notify_set(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001638 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001639 netdev_err(netdev,
1640 "Failed to alloc notify buffer, aborting.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001641 goto err_out_free_intr;
1642 }
1643
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001644 for (i = 0; i < enic->rq_count; i++) {
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001645 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001646 /* Need at least one buffer on ring to get going */
1647 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001648 netdev_err(netdev, "Unable to alloc receive buffers\n");
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001649 err = -ENOMEM;
Scott Feldman4b75a442008-09-24 11:23:53 -07001650 goto err_out_notify_unset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001651 }
1652 }
1653
1654 for (i = 0; i < enic->wq_count; i++)
1655 vnic_wq_enable(&enic->wq[i]);
1656 for (i = 0; i < enic->rq_count; i++)
1657 vnic_rq_enable(&enic->rq[i]);
1658
Roopa Prabhu296390592010-12-08 13:54:03 +00001659 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1660 enic_dev_add_addr(enic, enic->pp.mac_addr);
1661 else
1662 enic_dev_add_station_addr(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001663 enic_set_rx_mode(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001664
1665 netif_wake_queue(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001666
1667 for (i = 0; i < enic->rq_count; i++)
1668 napi_enable(&enic->napi[i]);
1669
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001670 enic_dev_enable(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001671
1672 for (i = 0; i < enic->intr_count; i++)
1673 vnic_intr_unmask(&enic->intr[i]);
1674
1675 enic_notify_timer_start(enic);
1676
1677 return 0;
Scott Feldman4b75a442008-09-24 11:23:53 -07001678
1679err_out_notify_unset:
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001680 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001681err_out_free_intr:
1682 enic_free_intr(enic);
1683
1684 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001685}
1686
1687/* rtnl lock is held, process context */
1688static int enic_stop(struct net_device *netdev)
1689{
1690 struct enic *enic = netdev_priv(netdev);
1691 unsigned int i;
1692 int err;
1693
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001694 for (i = 0; i < enic->intr_count; i++) {
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001695 vnic_intr_mask(&enic->intr[i]);
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001696 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1697 }
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001698
1699 enic_synchronize_irqs(enic);
1700
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001701 del_timer_sync(&enic->notify_timer);
1702
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001703 enic_dev_disable(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001704
1705 for (i = 0; i < enic->rq_count; i++)
1706 napi_disable(&enic->napi[i]);
1707
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001708 netif_carrier_off(netdev);
1709 netif_tx_disable(netdev);
Roopa Prabhu296390592010-12-08 13:54:03 +00001710 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1711 enic_dev_del_addr(enic, enic->pp.mac_addr);
1712 else
1713 enic_dev_del_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001714
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001715 for (i = 0; i < enic->wq_count; i++) {
1716 err = vnic_wq_disable(&enic->wq[i]);
1717 if (err)
1718 return err;
1719 }
1720 for (i = 0; i < enic->rq_count; i++) {
1721 err = vnic_rq_disable(&enic->rq[i]);
1722 if (err)
1723 return err;
1724 }
1725
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001726 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001727 enic_free_intr(enic);
1728
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001729 for (i = 0; i < enic->wq_count; i++)
1730 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1731 for (i = 0; i < enic->rq_count; i++)
1732 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1733 for (i = 0; i < enic->cq_count; i++)
1734 vnic_cq_clean(&enic->cq[i]);
1735 for (i = 0; i < enic->intr_count; i++)
1736 vnic_intr_clean(&enic->intr[i]);
1737
1738 return 0;
1739}
1740
1741static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1742{
1743 struct enic *enic = netdev_priv(netdev);
1744 int running = netif_running(netdev);
1745
Scott Feldman25f0a062008-09-24 11:23:32 -07001746 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1747 return -EINVAL;
1748
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001749 if (running)
1750 enic_stop(netdev);
1751
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001752 netdev->mtu = new_mtu;
1753
1754 if (netdev->mtu > enic->port_mtu)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001755 netdev_warn(netdev,
1756 "interface MTU (%d) set higher than port MTU (%d)\n",
1757 netdev->mtu, enic->port_mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001758
1759 if (running)
1760 enic_open(netdev);
1761
1762 return 0;
1763}
1764
1765#ifdef CONFIG_NET_POLL_CONTROLLER
1766static void enic_poll_controller(struct net_device *netdev)
1767{
1768 struct enic *enic = netdev_priv(netdev);
1769 struct vnic_dev *vdev = enic->vdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001770 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001771
1772 switch (vnic_dev_get_intr_mode(vdev)) {
1773 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001774 for (i = 0; i < enic->rq_count; i++) {
1775 intr = enic_msix_rq_intr(enic, i);
Vasanthy Kolluri79aeec52010-12-08 13:05:45 +00001776 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1777 &enic->napi[i]);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001778 }
1779 intr = enic_msix_wq_intr(enic, i);
1780 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001781 break;
1782 case VNIC_DEV_INTR_MODE_MSI:
1783 enic_isr_msi(enic->pdev->irq, enic);
1784 break;
1785 case VNIC_DEV_INTR_MODE_INTX:
1786 enic_isr_legacy(enic->pdev->irq, netdev);
1787 break;
1788 default:
1789 break;
1790 }
1791}
1792#endif
1793
1794static int enic_dev_wait(struct vnic_dev *vdev,
1795 int (*start)(struct vnic_dev *, int),
1796 int (*finished)(struct vnic_dev *, int *),
1797 int arg)
1798{
1799 unsigned long time;
1800 int done;
1801 int err;
1802
1803 BUG_ON(in_interrupt());
1804
1805 err = start(vdev, arg);
1806 if (err)
1807 return err;
1808
1809 /* Wait for func to complete...2 seconds max
1810 */
1811
1812 time = jiffies + (HZ * 2);
1813 do {
1814
1815 err = finished(vdev, &done);
1816 if (err)
1817 return err;
1818
1819 if (done)
1820 return 0;
1821
1822 schedule_timeout_uninterruptible(HZ / 10);
1823
1824 } while (time_after(time, jiffies));
1825
1826 return -ETIMEDOUT;
1827}
1828
1829static int enic_dev_open(struct enic *enic)
1830{
1831 int err;
1832
1833 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1834 vnic_dev_open_done, 0);
1835 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001836 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1837 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001838
1839 return err;
1840}
1841
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001842static int enic_dev_hang_reset(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001843{
1844 int err;
1845
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001846 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1847 vnic_dev_hang_reset_done, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001848 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001849 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1850 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001851
1852 return err;
1853}
1854
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001855static int enic_set_rsskey(struct enic *enic)
Scott Feldman68f71702009-02-09 23:24:24 -08001856{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001857 dma_addr_t rss_key_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001858 union vnic_rss_key *rss_key_buf_va = NULL;
1859 union vnic_rss_key rss_key = {
1860 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1861 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1862 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1863 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1864 };
1865 int err;
1866
1867 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1868 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1869 if (!rss_key_buf_va)
1870 return -ENOMEM;
1871
1872 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1873
1874 spin_lock(&enic->devcmd_lock);
1875 err = enic_set_rss_key(enic,
1876 rss_key_buf_pa,
1877 sizeof(union vnic_rss_key));
1878 spin_unlock(&enic->devcmd_lock);
1879
1880 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1881 rss_key_buf_va, rss_key_buf_pa);
1882
1883 return err;
1884}
1885
1886static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1887{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001888 dma_addr_t rss_cpu_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001889 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1890 unsigned int i;
1891 int err;
1892
1893 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1894 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1895 if (!rss_cpu_buf_va)
1896 return -ENOMEM;
1897
1898 for (i = 0; i < (1 << rss_hash_bits); i++)
1899 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1900
1901 spin_lock(&enic->devcmd_lock);
1902 err = enic_set_rss_cpu(enic,
1903 rss_cpu_buf_pa,
1904 sizeof(union vnic_rss_cpu));
1905 spin_unlock(&enic->devcmd_lock);
1906
1907 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1908 rss_cpu_buf_va, rss_cpu_buf_pa);
1909
1910 return err;
1911}
1912
1913static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1914 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1915{
Scott Feldman68f71702009-02-09 23:24:24 -08001916 const u8 tso_ipid_split_en = 0;
1917 const u8 ig_vlan_strip_en = 1;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001918 int err;
Scott Feldman68f71702009-02-09 23:24:24 -08001919
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001920 /* Enable VLAN tag stripping.
1921 */
Scott Feldman68f71702009-02-09 23:24:24 -08001922
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001923 spin_lock(&enic->devcmd_lock);
1924 err = enic_set_nic_cfg(enic,
Scott Feldman68f71702009-02-09 23:24:24 -08001925 rss_default_cpu, rss_hash_type,
1926 rss_hash_bits, rss_base_cpu,
1927 rss_enable, tso_ipid_split_en,
1928 ig_vlan_strip_en);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001929 spin_unlock(&enic->devcmd_lock);
1930
1931 return err;
1932}
1933
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001934static int enic_set_rss_nic_cfg(struct enic *enic)
1935{
1936 struct device *dev = enic_get_dev(enic);
1937 const u8 rss_default_cpu = 0;
1938 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1939 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1940 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1941 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1942 const u8 rss_hash_bits = 7;
1943 const u8 rss_base_cpu = 0;
1944 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1945
1946 if (rss_enable) {
1947 if (!enic_set_rsskey(enic)) {
1948 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1949 rss_enable = 0;
1950 dev_warn(dev, "RSS disabled, "
1951 "Failed to set RSS cpu indirection table.");
1952 }
1953 } else {
1954 rss_enable = 0;
1955 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1956 }
1957 }
1958
1959 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1960 rss_hash_bits, rss_base_cpu, rss_enable);
1961}
1962
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001963static void enic_reset(struct work_struct *work)
1964{
1965 struct enic *enic = container_of(work, struct enic, reset);
1966
1967 if (!netif_running(enic->netdev))
1968 return;
1969
1970 rtnl_lock();
1971
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001972 enic_dev_hang_notify(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001973 enic_stop(enic->netdev);
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001974 enic_dev_hang_reset(enic);
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001975 enic_reset_addr_lists(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001976 enic_init_vnic_resources(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001977 enic_set_rss_nic_cfg(enic);
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001978 enic_dev_set_ig_vlan_rewrite_mode(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001979 enic_open(enic->netdev);
1980
1981 rtnl_unlock();
1982}
1983
1984static int enic_set_intr_mode(struct enic *enic)
1985{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001986 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
Vasanthy Kolluri1cbb1a62011-02-17 13:57:19 +00001987 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001988 unsigned int i;
1989
1990 /* Set interrupt mode (INTx, MSI, MSI-X) depending
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001991 * on system capabilities.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001992 *
1993 * Try MSI-X first
1994 *
1995 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1996 * (the second to last INTR is used for WQ/RQ errors)
1997 * (the last INTR is used for notifications)
1998 */
1999
2000 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2001 for (i = 0; i < n + m + 2; i++)
2002 enic->msix_entry[i].entry = i;
2003
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002004 /* Use multiple RQs if RSS is enabled
2005 */
2006
2007 if (ENIC_SETTING(enic, RSS) &&
2008 enic->config.intr_mode < 1 &&
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002009 enic->rq_count >= n &&
2010 enic->wq_count >= m &&
2011 enic->cq_count >= n + m &&
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002012 enic->intr_count >= n + m + 2) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002013
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002014 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002015
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002016 enic->rq_count = n;
2017 enic->wq_count = m;
2018 enic->cq_count = n + m;
2019 enic->intr_count = n + m + 2;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002020
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002021 vnic_dev_set_intr_mode(enic->vdev,
2022 VNIC_DEV_INTR_MODE_MSIX);
2023
2024 return 0;
2025 }
2026 }
2027
2028 if (enic->config.intr_mode < 1 &&
2029 enic->rq_count >= 1 &&
2030 enic->wq_count >= m &&
2031 enic->cq_count >= 1 + m &&
2032 enic->intr_count >= 1 + m + 2) {
2033 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2034
2035 enic->rq_count = 1;
2036 enic->wq_count = m;
2037 enic->cq_count = 1 + m;
2038 enic->intr_count = 1 + m + 2;
2039
2040 vnic_dev_set_intr_mode(enic->vdev,
2041 VNIC_DEV_INTR_MODE_MSIX);
2042
2043 return 0;
2044 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002045 }
2046
2047 /* Next try MSI
2048 *
2049 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2050 */
2051
2052 if (enic->config.intr_mode < 2 &&
2053 enic->rq_count >= 1 &&
2054 enic->wq_count >= 1 &&
2055 enic->cq_count >= 2 &&
2056 enic->intr_count >= 1 &&
2057 !pci_enable_msi(enic->pdev)) {
2058
2059 enic->rq_count = 1;
2060 enic->wq_count = 1;
2061 enic->cq_count = 2;
2062 enic->intr_count = 1;
2063
2064 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2065
2066 return 0;
2067 }
2068
2069 /* Next try INTx
2070 *
2071 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2072 * (the first INTR is used for WQ/RQ)
2073 * (the second INTR is used for WQ/RQ errors)
2074 * (the last INTR is used for notifications)
2075 */
2076
2077 if (enic->config.intr_mode < 3 &&
2078 enic->rq_count >= 1 &&
2079 enic->wq_count >= 1 &&
2080 enic->cq_count >= 2 &&
2081 enic->intr_count >= 3) {
2082
2083 enic->rq_count = 1;
2084 enic->wq_count = 1;
2085 enic->cq_count = 2;
2086 enic->intr_count = 3;
2087
2088 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2089
2090 return 0;
2091 }
2092
2093 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2094
2095 return -EINVAL;
2096}
2097
2098static void enic_clear_intr_mode(struct enic *enic)
2099{
2100 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2101 case VNIC_DEV_INTR_MODE_MSIX:
2102 pci_disable_msix(enic->pdev);
2103 break;
2104 case VNIC_DEV_INTR_MODE_MSI:
2105 pci_disable_msi(enic->pdev);
2106 break;
2107 default:
2108 break;
2109 }
2110
2111 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2112}
2113
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002114static const struct net_device_ops enic_netdev_dynamic_ops = {
2115 .ndo_open = enic_open,
2116 .ndo_stop = enic_stop,
2117 .ndo_start_xmit = enic_hard_start_xmit,
2118 .ndo_get_stats = enic_get_stats,
2119 .ndo_validate_addr = eth_validate_addr,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002120 .ndo_set_rx_mode = enic_set_rx_mode,
2121 .ndo_set_multicast_list = enic_set_rx_mode,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002122 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2123 .ndo_change_mtu = enic_change_mtu,
2124 .ndo_vlan_rx_register = enic_vlan_rx_register,
2125 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2126 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2127 .ndo_tx_timeout = enic_tx_timeout,
2128 .ndo_set_vf_port = enic_set_vf_port,
2129 .ndo_get_vf_port = enic_get_vf_port,
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00002130 .ndo_set_vf_mac = enic_set_vf_mac,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002131#ifdef CONFIG_NET_POLL_CONTROLLER
2132 .ndo_poll_controller = enic_poll_controller,
2133#endif
2134};
2135
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002136static const struct net_device_ops enic_netdev_ops = {
2137 .ndo_open = enic_open,
2138 .ndo_stop = enic_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -08002139 .ndo_start_xmit = enic_hard_start_xmit,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002140 .ndo_get_stats = enic_get_stats,
2141 .ndo_validate_addr = eth_validate_addr,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002142 .ndo_set_mac_address = enic_set_mac_address,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002143 .ndo_set_rx_mode = enic_set_rx_mode,
2144 .ndo_set_multicast_list = enic_set_rx_mode,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002145 .ndo_change_mtu = enic_change_mtu,
2146 .ndo_vlan_rx_register = enic_vlan_rx_register,
2147 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2148 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2149 .ndo_tx_timeout = enic_tx_timeout,
2150#ifdef CONFIG_NET_POLL_CONTROLLER
2151 .ndo_poll_controller = enic_poll_controller,
2152#endif
2153};
2154
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002155static void enic_dev_deinit(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002156{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002157 unsigned int i;
2158
2159 for (i = 0; i < enic->rq_count; i++)
2160 netif_napi_del(&enic->napi[i]);
2161
Scott Feldman6fdfa972009-09-03 17:02:45 +00002162 enic_free_vnic_resources(enic);
2163 enic_clear_intr_mode(enic);
2164}
2165
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002166static int enic_dev_init(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002167{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002168 struct device *dev = enic_get_dev(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002169 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002170 unsigned int i;
Scott Feldman6fdfa972009-09-03 17:02:45 +00002171 int err;
2172
2173 /* Get vNIC configuration
2174 */
2175
2176 err = enic_get_vnic_config(enic);
2177 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002178 dev_err(dev, "Get vNIC configuration failed, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002179 return err;
2180 }
2181
2182 /* Get available resource counts
2183 */
2184
2185 enic_get_res_counts(enic);
2186
2187 /* Set interrupt mode based on resource counts and system
2188 * capabilities
2189 */
2190
2191 err = enic_set_intr_mode(enic);
2192 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002193 dev_err(dev, "Failed to set intr mode based on resource "
2194 "counts and system capabilities, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002195 return err;
2196 }
2197
2198 /* Allocate and configure vNIC resources
2199 */
2200
2201 err = enic_alloc_vnic_resources(enic);
2202 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002203 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002204 goto err_out_free_vnic_resources;
2205 }
2206
2207 enic_init_vnic_resources(enic);
2208
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002209 err = enic_set_rss_nic_cfg(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002210 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002211 dev_err(dev, "Failed to config nic, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002212 goto err_out_free_vnic_resources;
2213 }
2214
2215 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2216 default:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002217 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002218 break;
2219 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002220 for (i = 0; i < enic->rq_count; i++)
2221 netif_napi_add(netdev, &enic->napi[i],
2222 enic_poll_msix, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002223 break;
2224 }
2225
2226 return 0;
2227
2228err_out_free_vnic_resources:
2229 enic_clear_intr_mode(enic);
2230 enic_free_vnic_resources(enic);
2231
2232 return err;
2233}
2234
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002235static void enic_iounmap(struct enic *enic)
2236{
2237 unsigned int i;
2238
2239 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2240 if (enic->bar[i].vaddr)
2241 iounmap(enic->bar[i].vaddr);
2242}
2243
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002244static int __devinit enic_probe(struct pci_dev *pdev,
2245 const struct pci_device_id *ent)
2246{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002247 struct device *dev = &pdev->dev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002248 struct net_device *netdev;
2249 struct enic *enic;
2250 int using_dac = 0;
2251 unsigned int i;
2252 int err;
2253
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002254 /* Allocate net device structure and initialize. Private
2255 * instance data is initialized to zero.
2256 */
2257
2258 netdev = alloc_etherdev(sizeof(struct enic));
2259 if (!netdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002260 pr_err("Etherdev alloc failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002261 return -ENOMEM;
2262 }
2263
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002264 pci_set_drvdata(pdev, netdev);
2265
2266 SET_NETDEV_DEV(netdev, &pdev->dev);
2267
2268 enic = netdev_priv(netdev);
2269 enic->netdev = netdev;
2270 enic->pdev = pdev;
2271
2272 /* Setup PCI resources
2273 */
2274
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002275 err = pci_enable_device_mem(pdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002276 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002277 dev_err(dev, "Cannot enable PCI device, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002278 goto err_out_free_netdev;
2279 }
2280
2281 err = pci_request_regions(pdev, DRV_NAME);
2282 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002283 dev_err(dev, "Cannot request PCI regions, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002284 goto err_out_disable_device;
2285 }
2286
2287 pci_set_master(pdev);
2288
2289 /* Query PCI controller on system for DMA addressing
2290 * limitation for the device. Try 40-bit first, and
2291 * fail to 32-bit.
2292 */
2293
Yang Hongyang50cf1562009-04-06 19:01:14 -07002294 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002295 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07002296 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002297 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002298 dev_err(dev, "No usable DMA configuration, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002299 goto err_out_release_regions;
2300 }
Yang Hongyang284901a2009-04-06 19:01:15 -07002301 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002302 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002303 dev_err(dev, "Unable to obtain %u-bit DMA "
2304 "for consistent allocations, aborting\n", 32);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002305 goto err_out_release_regions;
2306 }
2307 } else {
Yang Hongyang50cf1562009-04-06 19:01:14 -07002308 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002309 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002310 dev_err(dev, "Unable to obtain %u-bit DMA "
2311 "for consistent allocations, aborting\n", 40);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002312 goto err_out_release_regions;
2313 }
2314 using_dac = 1;
2315 }
2316
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002317 /* Map vNIC resources from BAR0-5
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002318 */
2319
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002320 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2321 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2322 continue;
2323 enic->bar[i].len = pci_resource_len(pdev, i);
2324 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2325 if (!enic->bar[i].vaddr) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002326 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002327 err = -ENODEV;
2328 goto err_out_iounmap;
2329 }
2330 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002331 }
2332
2333 /* Register vNIC device
2334 */
2335
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002336 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2337 ARRAY_SIZE(enic->bar));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002338 if (!enic->vdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002339 dev_err(dev, "vNIC registration failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002340 err = -ENODEV;
2341 goto err_out_iounmap;
2342 }
2343
2344 /* Issue device open to get device in known state
2345 */
2346
2347 err = enic_dev_open(enic);
2348 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002349 dev_err(dev, "vNIC dev open failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002350 goto err_out_vnic_unregister;
2351 }
2352
Vasanthy Kolluri69161422011-02-04 16:17:16 +00002353 /* Setup devcmd lock
2354 */
2355
2356 spin_lock_init(&enic->devcmd_lock);
2357
2358 /*
2359 * Set ingress vlan rewrite mode before vnic initialization
2360 */
2361
2362 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2363 if (err) {
2364 dev_err(dev,
2365 "Failed to set ingress vlan rewrite mode, aborting.\n");
2366 goto err_out_dev_close;
2367 }
2368
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002369 /* Issue device init to initialize the vnic-to-switch link.
2370 * We'll start with carrier off and wait for link UP
2371 * notification later to turn on carrier. We don't need
2372 * to wait here for the vnic-to-switch link initialization
2373 * to complete; link UP notification is the indication that
2374 * the process is complete.
2375 */
2376
2377 netif_carrier_off(netdev);
2378
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002379 /* Do not call dev_init for a dynamic vnic.
2380 * For a dynamic vnic, init_prov_info will be
2381 * called later by an upper layer.
2382 */
2383
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002384 if (!enic_is_dynamic(enic)) {
2385 err = vnic_dev_init(enic->vdev, 0);
2386 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002387 dev_err(dev, "vNIC dev init failed, aborting\n");
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002388 goto err_out_dev_close;
2389 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002390 }
2391
Scott Feldman6fdfa972009-09-03 17:02:45 +00002392 err = enic_dev_init(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002393 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002394 dev_err(dev, "Device initialization failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002395 goto err_out_dev_close;
2396 }
2397
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002398 /* Setup notification timer, HW reset task, and wq locks
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002399 */
2400
2401 init_timer(&enic->notify_timer);
2402 enic->notify_timer.function = enic_notify_timer;
2403 enic->notify_timer.data = (unsigned long)enic;
2404
2405 INIT_WORK(&enic->reset, enic_reset);
2406
2407 for (i = 0; i < enic->wq_count; i++)
2408 spin_lock_init(&enic->wq_lock[i]);
2409
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002410 /* Register net device
2411 */
2412
2413 enic->port_mtu = enic->config.mtu;
2414 (void)enic_change_mtu(netdev, enic->port_mtu);
2415
2416 err = enic_set_mac_addr(netdev, enic->mac_addr);
2417 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002418 dev_err(dev, "Invalid MAC address, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002419 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002420 }
2421
Scott Feldman7c844592009-12-23 13:27:54 +00002422 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2423 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2424
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002425 if (enic_is_dynamic(enic))
2426 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2427 else
2428 netdev->netdev_ops = &enic_netdev_ops;
2429
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002430 netdev->watchdog_timeo = 2 * HZ;
2431 netdev->ethtool_ops = &enic_ethtool_ops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002432
Vasanthy Kolluri73c1ea92010-03-18 16:19:54 +00002433 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00002434 if (ENIC_SETTING(enic, LOOP)) {
2435 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2436 enic->loop_enable = 1;
2437 enic->loop_tag = enic->config.loop_tag;
2438 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2439 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002440 if (ENIC_SETTING(enic, TXCSUM))
2441 netdev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2442 if (ENIC_SETTING(enic, TSO))
2443 netdev->features |= NETIF_F_TSO |
2444 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
Scott Feldman86ca9db2008-11-21 21:26:55 -08002445 if (ENIC_SETTING(enic, LRO))
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00002446 netdev->features |= NETIF_F_GRO;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002447 if (using_dac)
2448 netdev->features |= NETIF_F_HIGHDMA;
2449
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002450 enic->csum_rx_enabled = ENIC_SETTING(enic, RXCSUM);
2451
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002452 err = register_netdev(netdev);
2453 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002454 dev_err(dev, "Cannot register net device, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002455 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002456 }
2457
2458 return 0;
2459
Scott Feldman6fdfa972009-09-03 17:02:45 +00002460err_out_dev_deinit:
2461 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002462err_out_dev_close:
2463 vnic_dev_close(enic->vdev);
2464err_out_vnic_unregister:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002465 vnic_dev_unregister(enic->vdev);
2466err_out_iounmap:
2467 enic_iounmap(enic);
2468err_out_release_regions:
2469 pci_release_regions(pdev);
2470err_out_disable_device:
2471 pci_disable_device(pdev);
2472err_out_free_netdev:
2473 pci_set_drvdata(pdev, NULL);
2474 free_netdev(netdev);
2475
2476 return err;
2477}
2478
2479static void __devexit enic_remove(struct pci_dev *pdev)
2480{
2481 struct net_device *netdev = pci_get_drvdata(pdev);
2482
2483 if (netdev) {
2484 struct enic *enic = netdev_priv(netdev);
2485
Tejun Heo23f333a2010-12-12 16:45:14 +01002486 cancel_work_sync(&enic->reset);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002487 unregister_netdev(netdev);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002488 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002489 vnic_dev_close(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002490 vnic_dev_unregister(enic->vdev);
2491 enic_iounmap(enic);
2492 pci_release_regions(pdev);
2493 pci_disable_device(pdev);
2494 pci_set_drvdata(pdev, NULL);
2495 free_netdev(netdev);
2496 }
2497}
2498
2499static struct pci_driver enic_driver = {
2500 .name = DRV_NAME,
2501 .id_table = enic_id_table,
2502 .probe = enic_probe,
2503 .remove = __devexit_p(enic_remove),
2504};
2505
2506static int __init enic_init_module(void)
2507{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002508 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002509
2510 return pci_register_driver(&enic_driver);
2511}
2512
2513static void __exit enic_cleanup_module(void)
2514{
2515 pci_unregister_driver(&enic_driver);
2516}
2517
2518module_init(enic_init_module);
2519module_exit(enic_cleanup_module);