blob: e25800fa96cab80388b24cd74f11765fb5bc7d8e [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000026#include <linux/interrupt.h>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070027#include <linux/workqueue.h>
28#include <linux/pci.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/if_ether.h>
32#include <linux/if_vlan.h>
33#include <linux/ethtool.h>
34#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/ipv6.h>
37#include <linux/tcp.h>
Vasanthy Kolluri29046f92010-06-24 10:52:26 +000038#include <linux/rtnetlink.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040039#include <linux/prefetch.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070040#include <net/ip6_checksum.h>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070041
42#include "cq_enet_desc.h"
43#include "vnic_dev.h"
44#include "vnic_intr.h"
45#include "vnic_stats.h"
Scott Feldmanf8bd9092010-05-17 22:50:19 -070046#include "vnic_vic.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070047#include "enic_res.h"
48#include "enic.h"
Vasanthy Kolluri51987462011-02-04 16:17:05 +000049#include "enic_dev.h"
Roopa Prabhub3abfbd2011-03-29 20:36:07 +000050#include "enic_pp.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070051
52#define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
Scott Feldmanea0d7d92009-09-03 17:02:03 +000053#define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
54#define MAX_TSO (1 << 16)
55#define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
56
57#define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
Scott Feldmanf8bd9092010-05-17 22:50:19 -070058#define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070059
60/* Supported devices */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000061static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
Scott Feldmanea0d7d92009-09-03 17:02:03 +000062 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
Scott Feldmanf8bd9092010-05-17 22:50:19 -070063 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
Scott Feldman01f2e4e2008-09-15 09:17:11 -070064 { 0, } /* end of table */
65};
66
67MODULE_DESCRIPTION(DRV_DESCRIPTION);
68MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
69MODULE_LICENSE("GPL");
70MODULE_VERSION(DRV_VERSION);
71MODULE_DEVICE_TABLE(pci, enic_id_table);
72
73struct enic_stat {
74 char name[ETH_GSTRING_LEN];
75 unsigned int offset;
76};
77
78#define ENIC_TX_STAT(stat) \
79 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
80#define ENIC_RX_STAT(stat) \
81 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
82
83static const struct enic_stat enic_tx_stats[] = {
84 ENIC_TX_STAT(tx_frames_ok),
85 ENIC_TX_STAT(tx_unicast_frames_ok),
86 ENIC_TX_STAT(tx_multicast_frames_ok),
87 ENIC_TX_STAT(tx_broadcast_frames_ok),
88 ENIC_TX_STAT(tx_bytes_ok),
89 ENIC_TX_STAT(tx_unicast_bytes_ok),
90 ENIC_TX_STAT(tx_multicast_bytes_ok),
91 ENIC_TX_STAT(tx_broadcast_bytes_ok),
92 ENIC_TX_STAT(tx_drops),
93 ENIC_TX_STAT(tx_errors),
94 ENIC_TX_STAT(tx_tso),
95};
96
97static const struct enic_stat enic_rx_stats[] = {
98 ENIC_RX_STAT(rx_frames_ok),
99 ENIC_RX_STAT(rx_frames_total),
100 ENIC_RX_STAT(rx_unicast_frames_ok),
101 ENIC_RX_STAT(rx_multicast_frames_ok),
102 ENIC_RX_STAT(rx_broadcast_frames_ok),
103 ENIC_RX_STAT(rx_bytes_ok),
104 ENIC_RX_STAT(rx_unicast_bytes_ok),
105 ENIC_RX_STAT(rx_multicast_bytes_ok),
106 ENIC_RX_STAT(rx_broadcast_bytes_ok),
107 ENIC_RX_STAT(rx_drop),
108 ENIC_RX_STAT(rx_no_bufs),
109 ENIC_RX_STAT(rx_errors),
110 ENIC_RX_STAT(rx_rss),
111 ENIC_RX_STAT(rx_crc_errors),
112 ENIC_RX_STAT(rx_frames_64),
113 ENIC_RX_STAT(rx_frames_127),
114 ENIC_RX_STAT(rx_frames_255),
115 ENIC_RX_STAT(rx_frames_511),
116 ENIC_RX_STAT(rx_frames_1023),
117 ENIC_RX_STAT(rx_frames_1518),
118 ENIC_RX_STAT(rx_frames_to_max),
119};
120
121static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
122static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
123
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700124static int enic_is_dynamic(struct enic *enic)
125{
126 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
127}
128
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000129static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
130{
131 return rq;
132}
133
134static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
135{
136 return enic->rq_count + wq;
137}
138
139static inline unsigned int enic_legacy_io_intr(void)
140{
141 return 0;
142}
143
144static inline unsigned int enic_legacy_err_intr(void)
145{
146 return 1;
147}
148
149static inline unsigned int enic_legacy_notify_intr(void)
150{
151 return 2;
152}
153
154static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
155{
Vasanthy Kolluri7d260ec2011-06-09 10:37:02 +0000156 return enic->cq[enic_cq_rq(enic, rq)].interrupt_offset;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000157}
158
159static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
160{
Vasanthy Kolluri7d260ec2011-06-09 10:37:02 +0000161 return enic->cq[enic_cq_wq(enic, wq)].interrupt_offset;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000162}
163
164static inline unsigned int enic_msix_err_intr(struct enic *enic)
165{
166 return enic->rq_count + enic->wq_count;
167}
168
169static inline unsigned int enic_msix_notify_intr(struct enic *enic)
170{
171 return enic->rq_count + enic->wq_count + 1;
172}
173
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700174static int enic_get_settings(struct net_device *netdev,
175 struct ethtool_cmd *ecmd)
176{
177 struct enic *enic = netdev_priv(netdev);
178
179 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
180 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
181 ecmd->port = PORT_FIBRE;
182 ecmd->transceiver = XCVR_EXTERNAL;
183
184 if (netif_carrier_ok(netdev)) {
David Decotigny70739492011-04-27 18:32:40 +0000185 ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700186 ecmd->duplex = DUPLEX_FULL;
187 } else {
David Decotigny70739492011-04-27 18:32:40 +0000188 ethtool_cmd_speed_set(ecmd, -1);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700189 ecmd->duplex = -1;
190 }
191
192 ecmd->autoneg = AUTONEG_DISABLE;
193
194 return 0;
195}
196
197static void enic_get_drvinfo(struct net_device *netdev,
198 struct ethtool_drvinfo *drvinfo)
199{
200 struct enic *enic = netdev_priv(netdev);
201 struct vnic_devcmd_fw_info *fw_info;
202
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000203 enic_dev_fw_info(enic, &fw_info);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700204
205 strncpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
206 strncpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
207 strncpy(drvinfo->fw_version, fw_info->fw_version,
208 sizeof(drvinfo->fw_version));
209 strncpy(drvinfo->bus_info, pci_name(enic->pdev),
210 sizeof(drvinfo->bus_info));
211}
212
213static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
214{
215 unsigned int i;
216
217 switch (stringset) {
218 case ETH_SS_STATS:
219 for (i = 0; i < enic_n_tx_stats; i++) {
220 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
221 data += ETH_GSTRING_LEN;
222 }
223 for (i = 0; i < enic_n_rx_stats; i++) {
224 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
225 data += ETH_GSTRING_LEN;
226 }
227 break;
228 }
229}
230
Scott Feldman25f0a062008-09-24 11:23:32 -0700231static int enic_get_sset_count(struct net_device *netdev, int sset)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700232{
Scott Feldman25f0a062008-09-24 11:23:32 -0700233 switch (sset) {
234 case ETH_SS_STATS:
235 return enic_n_tx_stats + enic_n_rx_stats;
236 default:
237 return -EOPNOTSUPP;
238 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700239}
240
241static void enic_get_ethtool_stats(struct net_device *netdev,
242 struct ethtool_stats *stats, u64 *data)
243{
244 struct enic *enic = netdev_priv(netdev);
245 struct vnic_stats *vstats;
246 unsigned int i;
247
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000248 enic_dev_stats_dump(enic, &vstats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700249
250 for (i = 0; i < enic_n_tx_stats; i++)
251 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
252 for (i = 0; i < enic_n_rx_stats; i++)
253 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
254}
255
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700256static u32 enic_get_msglevel(struct net_device *netdev)
257{
258 struct enic *enic = netdev_priv(netdev);
259 return enic->msg_enable;
260}
261
262static void enic_set_msglevel(struct net_device *netdev, u32 value)
263{
264 struct enic *enic = netdev_priv(netdev);
265 enic->msg_enable = value;
266}
267
Scott Feldman7c844592009-12-23 13:27:54 +0000268static int enic_get_coalesce(struct net_device *netdev,
269 struct ethtool_coalesce *ecmd)
270{
271 struct enic *enic = netdev_priv(netdev);
272
273 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
274 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
275
276 return 0;
277}
278
279static int enic_set_coalesce(struct net_device *netdev,
280 struct ethtool_coalesce *ecmd)
281{
282 struct enic *enic = netdev_priv(netdev);
283 u32 tx_coalesce_usecs;
284 u32 rx_coalesce_usecs;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000285 unsigned int i, intr;
Scott Feldman7c844592009-12-23 13:27:54 +0000286
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000287 tx_coalesce_usecs = min_t(u32, ecmd->tx_coalesce_usecs,
288 vnic_dev_get_intr_coal_timer_max(enic->vdev));
289 rx_coalesce_usecs = min_t(u32, ecmd->rx_coalesce_usecs,
290 vnic_dev_get_intr_coal_timer_max(enic->vdev));
Scott Feldman7c844592009-12-23 13:27:54 +0000291
292 switch (vnic_dev_get_intr_mode(enic->vdev)) {
293 case VNIC_DEV_INTR_MODE_INTX:
294 if (tx_coalesce_usecs != rx_coalesce_usecs)
295 return -EINVAL;
296
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000297 intr = enic_legacy_io_intr();
298 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000299 tx_coalesce_usecs);
Scott Feldman7c844592009-12-23 13:27:54 +0000300 break;
301 case VNIC_DEV_INTR_MODE_MSI:
302 if (tx_coalesce_usecs != rx_coalesce_usecs)
303 return -EINVAL;
304
305 vnic_intr_coalescing_timer_set(&enic->intr[0],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000306 tx_coalesce_usecs);
Scott Feldman7c844592009-12-23 13:27:54 +0000307 break;
308 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000309 for (i = 0; i < enic->wq_count; i++) {
310 intr = enic_msix_wq_intr(enic, i);
311 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000312 tx_coalesce_usecs);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000313 }
314
315 for (i = 0; i < enic->rq_count; i++) {
316 intr = enic_msix_rq_intr(enic, i);
317 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000318 rx_coalesce_usecs);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000319 }
320
Scott Feldman7c844592009-12-23 13:27:54 +0000321 break;
322 default:
323 break;
324 }
325
326 enic->tx_coalesce_usecs = tx_coalesce_usecs;
327 enic->rx_coalesce_usecs = rx_coalesce_usecs;
328
329 return 0;
330}
331
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700332static const struct ethtool_ops enic_ethtool_ops = {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700333 .get_settings = enic_get_settings,
334 .get_drvinfo = enic_get_drvinfo,
335 .get_msglevel = enic_get_msglevel,
336 .set_msglevel = enic_set_msglevel,
337 .get_link = ethtool_op_get_link,
338 .get_strings = enic_get_strings,
Scott Feldman25f0a062008-09-24 11:23:32 -0700339 .get_sset_count = enic_get_sset_count,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700340 .get_ethtool_stats = enic_get_ethtool_stats,
Scott Feldman7c844592009-12-23 13:27:54 +0000341 .get_coalesce = enic_get_coalesce,
342 .set_coalesce = enic_set_coalesce,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700343};
344
345static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
346{
347 struct enic *enic = vnic_dev_priv(wq->vdev);
348
349 if (buf->sop)
350 pci_unmap_single(enic->pdev, buf->dma_addr,
351 buf->len, PCI_DMA_TODEVICE);
352 else
353 pci_unmap_page(enic->pdev, buf->dma_addr,
354 buf->len, PCI_DMA_TODEVICE);
355
356 if (buf->os_buf)
357 dev_kfree_skb_any(buf->os_buf);
358}
359
360static void enic_wq_free_buf(struct vnic_wq *wq,
361 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
362{
363 enic_free_wq_buf(wq, buf);
364}
365
366static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
367 u8 type, u16 q_number, u16 completed_index, void *opaque)
368{
369 struct enic *enic = vnic_dev_priv(vdev);
370
371 spin_lock(&enic->wq_lock[q_number]);
372
373 vnic_wq_service(&enic->wq[q_number], cq_desc,
374 completed_index, enic_wq_free_buf,
375 opaque);
376
377 if (netif_queue_stopped(enic->netdev) &&
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000378 vnic_wq_desc_avail(&enic->wq[q_number]) >=
379 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700380 netif_wake_queue(enic->netdev);
381
382 spin_unlock(&enic->wq_lock[q_number]);
383
384 return 0;
385}
386
387static void enic_log_q_error(struct enic *enic)
388{
389 unsigned int i;
390 u32 error_status;
391
392 for (i = 0; i < enic->wq_count; i++) {
393 error_status = vnic_wq_error_status(&enic->wq[i]);
394 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000395 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
396 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700397 }
398
399 for (i = 0; i < enic->rq_count; i++) {
400 error_status = vnic_rq_error_status(&enic->rq[i]);
401 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000402 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
403 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700404 }
405}
406
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000407static void enic_msglvl_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700408{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000409 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700410
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000411 if (msg_enable != enic->msg_enable) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000412 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
413 enic->msg_enable, msg_enable);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000414 enic->msg_enable = msg_enable;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700415 }
416}
417
418static void enic_mtu_check(struct enic *enic)
419{
420 u32 mtu = vnic_dev_mtu(enic->vdev);
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000421 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700422
Scott Feldman491598a2009-09-03 17:02:40 +0000423 if (mtu && mtu != enic->port_mtu) {
Scott Feldman7c844592009-12-23 13:27:54 +0000424 enic->port_mtu = mtu;
Roopa Prabhuc97c8942011-06-03 14:35:17 +0000425 if (enic_is_dynamic(enic)) {
426 mtu = max_t(int, ENIC_MIN_MTU,
427 min_t(int, ENIC_MAX_MTU, mtu));
428 if (mtu != netdev->mtu)
429 schedule_work(&enic->change_mtu_work);
430 } else {
431 if (mtu < netdev->mtu)
432 netdev_warn(netdev,
433 "interface MTU (%d) set higher "
434 "than switch port MTU (%d)\n",
435 netdev->mtu, mtu);
436 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700437 }
438}
439
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000440static void enic_link_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700441{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000442 int link_status = vnic_dev_link_status(enic->vdev);
443 int carrier_ok = netif_carrier_ok(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700444
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000445 if (link_status && !carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000446 netdev_info(enic->netdev, "Link UP\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000447 netif_carrier_on(enic->netdev);
448 } else if (!link_status && carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000449 netdev_info(enic->netdev, "Link DOWN\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000450 netif_carrier_off(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700451 }
452}
453
454static void enic_notify_check(struct enic *enic)
455{
456 enic_msglvl_check(enic);
457 enic_mtu_check(enic);
458 enic_link_check(enic);
459}
460
461#define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
462
463static irqreturn_t enic_isr_legacy(int irq, void *data)
464{
465 struct net_device *netdev = data;
466 struct enic *enic = netdev_priv(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000467 unsigned int io_intr = enic_legacy_io_intr();
468 unsigned int err_intr = enic_legacy_err_intr();
469 unsigned int notify_intr = enic_legacy_notify_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700470 u32 pba;
471
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000472 vnic_intr_mask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700473
474 pba = vnic_intr_legacy_pba(enic->legacy_pba);
475 if (!pba) {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000476 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700477 return IRQ_NONE; /* not our interrupt */
478 }
479
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000480 if (ENIC_TEST_INTR(pba, notify_intr)) {
481 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700482 enic_notify_check(enic);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800483 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700484
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000485 if (ENIC_TEST_INTR(pba, err_intr)) {
486 vnic_intr_return_all_credits(&enic->intr[err_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700487 enic_log_q_error(enic);
488 /* schedule recovery from WQ/RQ error */
489 schedule_work(&enic->reset);
490 return IRQ_HANDLED;
491 }
492
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000493 if (ENIC_TEST_INTR(pba, io_intr)) {
494 if (napi_schedule_prep(&enic->napi[0]))
495 __napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700496 } else {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000497 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700498 }
499
500 return IRQ_HANDLED;
501}
502
503static irqreturn_t enic_isr_msi(int irq, void *data)
504{
505 struct enic *enic = data;
506
507 /* With MSI, there is no sharing of interrupts, so this is
508 * our interrupt and there is no need to ack it. The device
509 * is not providing per-vector masking, so the OS will not
510 * write to PCI config space to mask/unmask the interrupt.
511 * We're using mask_on_assertion for MSI, so the device
512 * automatically masks the interrupt when the interrupt is
513 * generated. Later, when exiting polling, the interrupt
514 * will be unmasked (see enic_poll).
515 *
516 * Also, the device uses the same PCIe Traffic Class (TC)
517 * for Memory Write data and MSI, so there are no ordering
518 * issues; the MSI will always arrive at the Root Complex
519 * _after_ corresponding Memory Writes (i.e. descriptor
520 * writes).
521 */
522
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000523 napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700524
525 return IRQ_HANDLED;
526}
527
528static irqreturn_t enic_isr_msix_rq(int irq, void *data)
529{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000530 struct napi_struct *napi = data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700531
532 /* schedule NAPI polling for RQ cleanup */
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000533 napi_schedule(napi);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700534
535 return IRQ_HANDLED;
536}
537
538static irqreturn_t enic_isr_msix_wq(int irq, void *data)
539{
540 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000541 unsigned int cq = enic_cq_wq(enic, 0);
542 unsigned int intr = enic_msix_wq_intr(enic, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700543 unsigned int wq_work_to_do = -1; /* no limit */
544 unsigned int wq_work_done;
545
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000546 wq_work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700547 wq_work_to_do, enic_wq_service, NULL);
548
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000549 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700550 wq_work_done,
551 1 /* unmask intr */,
552 1 /* reset intr timer */);
553
554 return IRQ_HANDLED;
555}
556
557static irqreturn_t enic_isr_msix_err(int irq, void *data)
558{
559 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000560 unsigned int intr = enic_msix_err_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700561
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000562 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800563
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700564 enic_log_q_error(enic);
565
566 /* schedule recovery from WQ/RQ error */
567 schedule_work(&enic->reset);
568
569 return IRQ_HANDLED;
570}
571
572static irqreturn_t enic_isr_msix_notify(int irq, void *data)
573{
574 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000575 unsigned int intr = enic_msix_notify_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700576
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000577 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700578 enic_notify_check(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700579
580 return IRQ_HANDLED;
581}
582
583static inline void enic_queue_wq_skb_cont(struct enic *enic,
584 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000585 unsigned int len_left, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700586{
587 skb_frag_t *frag;
588
589 /* Queue additional data fragments */
590 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
591 len_left -= frag->size;
592 enic_queue_wq_desc_cont(wq, skb,
593 pci_map_page(enic->pdev, frag->page,
594 frag->page_offset, frag->size,
595 PCI_DMA_TODEVICE),
596 frag->size,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000597 (len_left == 0), /* EOP? */
598 loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700599 }
600}
601
602static inline void enic_queue_wq_skb_vlan(struct enic *enic,
603 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000604 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700605{
606 unsigned int head_len = skb_headlen(skb);
607 unsigned int len_left = skb->len - head_len;
608 int eop = (len_left == 0);
609
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000610 /* Queue the main skb fragment. The fragments are no larger
611 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
612 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
613 * per fragment is queued.
614 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700615 enic_queue_wq_desc(wq, skb,
616 pci_map_single(enic->pdev, skb->data,
617 head_len, PCI_DMA_TODEVICE),
618 head_len,
619 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000620 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700621
622 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000623 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700624}
625
626static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
627 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000628 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700629{
630 unsigned int head_len = skb_headlen(skb);
631 unsigned int len_left = skb->len - head_len;
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000632 unsigned int hdr_len = skb_checksum_start_offset(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700633 unsigned int csum_offset = hdr_len + skb->csum_offset;
634 int eop = (len_left == 0);
635
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000636 /* Queue the main skb fragment. The fragments are no larger
637 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
638 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
639 * per fragment is queued.
640 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700641 enic_queue_wq_desc_csum_l4(wq, skb,
642 pci_map_single(enic->pdev, skb->data,
643 head_len, PCI_DMA_TODEVICE),
644 head_len,
645 csum_offset,
646 hdr_len,
647 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000648 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700649
650 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000651 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700652}
653
654static inline void enic_queue_wq_skb_tso(struct enic *enic,
655 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
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{
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000658 unsigned int frag_len_left = skb_headlen(skb);
659 unsigned int len_left = skb->len - frag_len_left;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700660 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
661 int eop = (len_left == 0);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000662 unsigned int len;
663 dma_addr_t dma_addr;
664 unsigned int offset = 0;
665 skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700666
667 /* Preload TCP csum field with IP pseudo hdr calculated
668 * with IP length set to zero. HW will later add in length
669 * to each TCP segment resulting from the TSO.
670 */
671
Harvey Harrison09640e62009-02-01 00:45:17 -0800672 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700673 ip_hdr(skb)->check = 0;
674 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
675 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
Harvey Harrison09640e62009-02-01 00:45:17 -0800676 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700677 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
678 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
679 }
680
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000681 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
682 * for the main skb fragment
683 */
684 while (frag_len_left) {
685 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
686 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
687 len, PCI_DMA_TODEVICE);
688 enic_queue_wq_desc_tso(wq, skb,
689 dma_addr,
690 len,
691 mss, hdr_len,
692 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000693 eop && (len == frag_len_left), loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000694 frag_len_left -= len;
695 offset += len;
696 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700697
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000698 if (eop)
699 return;
700
701 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
702 * for additional data fragments
703 */
704 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
705 len_left -= frag->size;
706 frag_len_left = frag->size;
707 offset = frag->page_offset;
708
709 while (frag_len_left) {
710 len = min(frag_len_left,
711 (unsigned int)WQ_ENET_MAX_DESC_LEN);
712 dma_addr = pci_map_page(enic->pdev, frag->page,
713 offset, len,
714 PCI_DMA_TODEVICE);
715 enic_queue_wq_desc_cont(wq, skb,
716 dma_addr,
717 len,
718 (len_left == 0) &&
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000719 (len == frag_len_left), /* EOP? */
720 loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000721 frag_len_left -= len;
722 offset += len;
723 }
724 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700725}
726
727static inline void enic_queue_wq_skb(struct enic *enic,
728 struct vnic_wq *wq, struct sk_buff *skb)
729{
730 unsigned int mss = skb_shinfo(skb)->gso_size;
731 unsigned int vlan_tag = 0;
732 int vlan_tag_insert = 0;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000733 int loopback = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700734
Jesse Grosseab6d182010-10-20 13:56:03 +0000735 if (vlan_tx_tag_present(skb)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700736 /* VLAN tag from trunking driver */
737 vlan_tag_insert = 1;
738 vlan_tag = vlan_tx_tag_get(skb);
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000739 } else if (enic->loop_enable) {
740 vlan_tag = enic->loop_tag;
741 loopback = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700742 }
743
744 if (mss)
745 enic_queue_wq_skb_tso(enic, wq, skb, mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000746 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700747 else if (skb->ip_summed == CHECKSUM_PARTIAL)
748 enic_queue_wq_skb_csum_l4(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000749 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700750 else
751 enic_queue_wq_skb_vlan(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000752 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700753}
754
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800755/* netif_tx_lock held, process context with BHs disabled, or BH */
Stephen Hemminger613573252009-08-31 19:50:58 +0000756static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
Scott Feldmand87fd252009-12-23 13:27:59 +0000757 struct net_device *netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700758{
759 struct enic *enic = netdev_priv(netdev);
760 struct vnic_wq *wq = &enic->wq[0];
761 unsigned long flags;
762
763 if (skb->len <= 0) {
764 dev_kfree_skb(skb);
765 return NETDEV_TX_OK;
766 }
767
768 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
769 * which is very likely. In the off chance it's going to take
770 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
771 */
772
773 if (skb_shinfo(skb)->gso_size == 0 &&
774 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
775 skb_linearize(skb)) {
776 dev_kfree_skb(skb);
777 return NETDEV_TX_OK;
778 }
779
780 spin_lock_irqsave(&enic->wq_lock[0], flags);
781
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000782 if (vnic_wq_desc_avail(wq) <
783 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700784 netif_stop_queue(netdev);
785 /* This is a hard error, log it */
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000786 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700787 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
788 return NETDEV_TX_BUSY;
789 }
790
791 enic_queue_wq_skb(enic, wq, skb);
792
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000793 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700794 netif_stop_queue(netdev);
795
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700796 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
797
798 return NETDEV_TX_OK;
799}
800
801/* dev_base_lock rwlock held, nominally process context */
stephen hemmingerf20530b2011-06-08 14:54:02 +0000802static struct rtnl_link_stats64 *enic_get_stats(struct net_device *netdev,
803 struct rtnl_link_stats64 *net_stats)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700804{
805 struct enic *enic = netdev_priv(netdev);
806 struct vnic_stats *stats;
807
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000808 enic_dev_stats_dump(enic, &stats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700809
Scott Feldman25f0a062008-09-24 11:23:32 -0700810 net_stats->tx_packets = stats->tx.tx_frames_ok;
811 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
812 net_stats->tx_errors = stats->tx.tx_errors;
813 net_stats->tx_dropped = stats->tx.tx_drops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700814
Scott Feldman25f0a062008-09-24 11:23:32 -0700815 net_stats->rx_packets = stats->rx.rx_frames_ok;
816 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
817 net_stats->rx_errors = stats->rx.rx_errors;
818 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
Scott Feldman350991e2009-09-03 17:02:19 +0000819 net_stats->rx_over_errors = enic->rq_truncated_pkts;
Scott Feldmanbd9fb1a2009-02-09 23:24:08 -0800820 net_stats->rx_crc_errors = enic->rq_bad_fcs;
Scott Feldman350991e2009-09-03 17:02:19 +0000821 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700822
Scott Feldman25f0a062008-09-24 11:23:32 -0700823 return net_stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700824}
825
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000826void enic_reset_addr_lists(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700827{
828 enic->mc_count = 0;
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000829 enic->uc_count = 0;
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000830 enic->flags = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700831}
832
833static int enic_set_mac_addr(struct net_device *netdev, char *addr)
834{
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700835 struct enic *enic = netdev_priv(netdev);
836
837 if (enic_is_dynamic(enic)) {
838 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
839 return -EADDRNOTAVAIL;
840 } else {
841 if (!is_valid_ether_addr(addr))
842 return -EADDRNOTAVAIL;
843 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700844
845 memcpy(netdev->dev_addr, addr, netdev->addr_len);
846
847 return 0;
848}
849
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700850static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
851{
852 struct enic *enic = netdev_priv(netdev);
853 struct sockaddr *saddr = p;
854 char *addr = saddr->sa_data;
855 int err;
856
857 if (netif_running(enic->netdev)) {
858 err = enic_dev_del_station_addr(enic);
859 if (err)
860 return err;
861 }
862
863 err = enic_set_mac_addr(netdev, addr);
864 if (err)
865 return err;
866
867 if (netif_running(enic->netdev)) {
868 err = enic_dev_add_station_addr(enic);
869 if (err)
870 return err;
871 }
872
873 return err;
874}
875
876static int enic_set_mac_address(struct net_device *netdev, void *p)
877{
Roopa Prabhu294dab22010-08-10 18:54:55 +0000878 struct sockaddr *saddr = p;
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000879 char *addr = saddr->sa_data;
880 struct enic *enic = netdev_priv(netdev);
881 int err;
Roopa Prabhu294dab22010-08-10 18:54:55 +0000882
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000883 err = enic_dev_del_station_addr(enic);
884 if (err)
885 return err;
886
887 err = enic_set_mac_addr(netdev, addr);
888 if (err)
889 return err;
890
891 return enic_dev_add_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700892}
893
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000894static void enic_update_multicast_addr_list(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700895{
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000896 struct net_device *netdev = enic->netdev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000897 struct netdev_hw_addr *ha;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000898 unsigned int mc_count = netdev_mc_count(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700899 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700900 unsigned int i, j;
901
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000902 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
903 netdev_warn(netdev, "Registering only %d out of %d "
904 "multicast addresses\n",
905 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700906 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
Scott Feldman9959a182009-12-23 13:27:43 +0000907 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700908
909 /* Is there an easier way? Trying to minimize to
910 * calls to add/del multicast addrs. We keep the
911 * addrs from the last call in enic->mc_addr and
912 * look for changes to add/del.
913 */
914
Jiri Pirko48e2f182010-02-22 09:22:26 +0000915 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000916 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko48e2f182010-02-22 09:22:26 +0000917 if (i == mc_count)
918 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000919 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700920 }
921
922 for (i = 0; i < enic->mc_count; i++) {
923 for (j = 0; j < mc_count; j++)
924 if (compare_ether_addr(enic->mc_addr[i],
925 mc_addr[j]) == 0)
926 break;
927 if (j == mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000928 enic_dev_del_addr(enic, enic->mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700929 }
930
931 for (i = 0; i < mc_count; i++) {
932 for (j = 0; j < enic->mc_count; j++)
933 if (compare_ether_addr(mc_addr[i],
934 enic->mc_addr[j]) == 0)
935 break;
936 if (j == enic->mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000937 enic_dev_add_addr(enic, mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700938 }
939
940 /* Save the list to compare against next time
941 */
942
943 for (i = 0; i < mc_count; i++)
944 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
945
946 enic->mc_count = mc_count;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700947}
948
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000949static void enic_update_unicast_addr_list(struct enic *enic)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000950{
951 struct net_device *netdev = enic->netdev;
952 struct netdev_hw_addr *ha;
953 unsigned int uc_count = netdev_uc_count(netdev);
954 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
955 unsigned int i, j;
956
957 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
958 netdev_warn(netdev, "Registering only %d out of %d "
959 "unicast addresses\n",
960 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
961 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
962 }
963
964 /* Is there an easier way? Trying to minimize to
965 * calls to add/del unicast addrs. We keep the
966 * addrs from the last call in enic->uc_addr and
967 * look for changes to add/del.
968 */
969
970 i = 0;
971 netdev_for_each_uc_addr(ha, netdev) {
972 if (i == uc_count)
973 break;
974 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
975 }
976
977 for (i = 0; i < enic->uc_count; i++) {
978 for (j = 0; j < uc_count; j++)
979 if (compare_ether_addr(enic->uc_addr[i],
980 uc_addr[j]) == 0)
981 break;
982 if (j == uc_count)
983 enic_dev_del_addr(enic, enic->uc_addr[i]);
984 }
985
986 for (i = 0; i < uc_count; i++) {
987 for (j = 0; j < enic->uc_count; j++)
988 if (compare_ether_addr(uc_addr[i],
989 enic->uc_addr[j]) == 0)
990 break;
991 if (j == enic->uc_count)
992 enic_dev_add_addr(enic, uc_addr[i]);
993 }
994
995 /* Save the list to compare against next time
996 */
997
998 for (i = 0; i < uc_count; i++)
999 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1000
1001 enic->uc_count = uc_count;
1002}
1003
1004/* netif_tx_lock held, BHs disabled */
1005static void enic_set_rx_mode(struct net_device *netdev)
1006{
1007 struct enic *enic = netdev_priv(netdev);
1008 int directed = 1;
1009 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1010 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1011 int promisc = (netdev->flags & IFF_PROMISC) ||
1012 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1013 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1014 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1015 unsigned int flags = netdev->flags |
1016 (allmulti ? IFF_ALLMULTI : 0) |
1017 (promisc ? IFF_PROMISC : 0);
1018
1019 if (enic->flags != flags) {
1020 enic->flags = flags;
1021 enic_dev_packet_filter(enic, directed,
1022 multicast, broadcast, promisc, allmulti);
1023 }
1024
1025 if (!promisc) {
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001026 enic_update_unicast_addr_list(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001027 if (!allmulti)
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001028 enic_update_multicast_addr_list(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001029 }
1030}
1031
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001032/* rtnl lock is held */
1033static void enic_vlan_rx_register(struct net_device *netdev,
1034 struct vlan_group *vlan_group)
1035{
1036 struct enic *enic = netdev_priv(netdev);
1037 enic->vlan_group = vlan_group;
1038}
1039
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001040/* netif_tx_lock held, BHs disabled */
1041static void enic_tx_timeout(struct net_device *netdev)
1042{
1043 struct enic *enic = netdev_priv(netdev);
1044 schedule_work(&enic->reset);
1045}
1046
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001047static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1048{
1049 struct enic *enic = netdev_priv(netdev);
1050
1051 if (vf != PORT_SELF_VF)
1052 return -EOPNOTSUPP;
1053
1054 /* Ignore the vf argument for now. We can assume the request
1055 * is coming on a vf.
1056 */
1057 if (is_valid_ether_addr(mac)) {
1058 memcpy(enic->pp.vf_mac, mac, ETH_ALEN);
1059 return 0;
1060 } else
1061 return -EINVAL;
1062}
1063
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001064static int enic_set_vf_port(struct net_device *netdev, int vf,
1065 struct nlattr *port[])
1066{
1067 struct enic *enic = netdev_priv(netdev);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001068 struct enic_port_profile prev_pp;
1069 int err = 0, restore_pp = 1;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001070
1071 /* don't support VFs, yet */
1072 if (vf != PORT_SELF_VF)
1073 return -EOPNOTSUPP;
1074
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001075 if (!port[IFLA_PORT_REQUEST])
Scott Feldman08f382e2010-06-01 08:59:33 +00001076 return -EOPNOTSUPP;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001077
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001078 memcpy(&prev_pp, &enic->pp, sizeof(enic->pp));
1079 memset(&enic->pp, 0, sizeof(enic->pp));
1080
1081 enic->pp.set |= ENIC_SET_REQUEST;
1082 enic->pp.request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1083
1084 if (port[IFLA_PORT_PROFILE]) {
1085 enic->pp.set |= ENIC_SET_NAME;
1086 memcpy(enic->pp.name, nla_data(port[IFLA_PORT_PROFILE]),
1087 PORT_PROFILE_MAX);
1088 }
1089
1090 if (port[IFLA_PORT_INSTANCE_UUID]) {
1091 enic->pp.set |= ENIC_SET_INSTANCE;
1092 memcpy(enic->pp.instance_uuid,
1093 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1094 }
1095
1096 if (port[IFLA_PORT_HOST_UUID]) {
1097 enic->pp.set |= ENIC_SET_HOST;
1098 memcpy(enic->pp.host_uuid,
1099 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1100 }
1101
1102 /* Special case handling: mac came from IFLA_VF_MAC */
1103 if (!is_zero_ether_addr(prev_pp.vf_mac))
1104 memcpy(enic->pp.mac_addr, prev_pp.vf_mac, ETH_ALEN);
Scott Feldman418c4372010-05-22 17:29:58 +00001105
1106 if (is_zero_ether_addr(netdev->dev_addr))
1107 random_ether_addr(netdev->dev_addr);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001108
1109 err = enic_process_set_pp_request(enic, &prev_pp, &restore_pp);
1110 if (err) {
1111 if (restore_pp) {
1112 /* Things are still the way they were: Implicit
1113 * DISASSOCIATE failed
1114 */
1115 memcpy(&enic->pp, &prev_pp, sizeof(enic->pp));
1116 } else {
1117 memset(&enic->pp, 0, sizeof(enic->pp));
1118 memset(netdev->dev_addr, 0, ETH_ALEN);
1119 }
1120 } else {
1121 /* Set flag to indicate that the port assoc/disassoc
1122 * request has been sent out to fw
1123 */
1124 enic->pp.set |= ENIC_PORT_REQUEST_APPLIED;
1125
1126 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1127 if (enic->pp.request == PORT_REQUEST_DISASSOCIATE) {
1128 memset(enic->pp.mac_addr, 0, ETH_ALEN);
1129 memset(netdev->dev_addr, 0, ETH_ALEN);
1130 }
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001131 }
1132
Roopa Prabhu296390592010-12-08 13:54:03 +00001133 memset(enic->pp.vf_mac, 0, ETH_ALEN);
1134
Roopa Prabhu296390592010-12-08 13:54:03 +00001135 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001136}
1137
1138static int enic_get_vf_port(struct net_device *netdev, int vf,
1139 struct sk_buff *skb)
1140{
1141 struct enic *enic = netdev_priv(netdev);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001142 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001143 int err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001144
Roopa Prabhu4dce2392011-01-20 14:35:54 +00001145 if (!(enic->pp.set & ENIC_PORT_REQUEST_APPLIED))
Scott Feldman08f382e2010-06-01 08:59:33 +00001146 return -ENODATA;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001147
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001148 err = enic_process_get_pp_request(enic, enic->pp.request, &response);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001149 if (err)
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001150 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001151
1152 NLA_PUT_U16(skb, IFLA_PORT_REQUEST, enic->pp.request);
1153 NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
Scott Feldman08f382e2010-06-01 08:59:33 +00001154 if (enic->pp.set & ENIC_SET_NAME)
1155 NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1156 enic->pp.name);
1157 if (enic->pp.set & ENIC_SET_INSTANCE)
1158 NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1159 enic->pp.instance_uuid);
1160 if (enic->pp.set & ENIC_SET_HOST)
1161 NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1162 enic->pp.host_uuid);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001163
1164 return 0;
1165
1166nla_put_failure:
1167 return -EMSGSIZE;
1168}
1169
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001170static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1171{
1172 struct enic *enic = vnic_dev_priv(rq->vdev);
1173
1174 if (!buf->os_buf)
1175 return;
1176
1177 pci_unmap_single(enic->pdev, buf->dma_addr,
1178 buf->len, PCI_DMA_FROMDEVICE);
1179 dev_kfree_skb_any(buf->os_buf);
1180}
1181
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001182static int enic_rq_alloc_buf(struct vnic_rq *rq)
1183{
1184 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldmand19e22d2009-09-03 17:02:08 +00001185 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001186 struct sk_buff *skb;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00001187 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001188 unsigned int os_buf_index = 0;
1189 dma_addr_t dma_addr;
1190
Eric Dumazet89d71a62009-10-13 05:34:20 +00001191 skb = netdev_alloc_skb_ip_align(netdev, len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001192 if (!skb)
1193 return -ENOMEM;
1194
1195 dma_addr = pci_map_single(enic->pdev, skb->data,
1196 len, PCI_DMA_FROMDEVICE);
1197
1198 enic_queue_rq_desc(rq, skb, os_buf_index,
1199 dma_addr, len);
1200
1201 return 0;
1202}
1203
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001204static void enic_rq_indicate_buf(struct vnic_rq *rq,
1205 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1206 int skipped, void *opaque)
1207{
1208 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001209 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001210 struct sk_buff *skb;
1211
1212 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1213 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1214 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1215 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1216 u8 packet_error;
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001217 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001218 u32 rss_hash;
1219
1220 if (skipped)
1221 return;
1222
1223 skb = buf->os_buf;
1224 prefetch(skb->data - NET_IP_ALIGN);
1225 pci_unmap_single(enic->pdev, buf->dma_addr,
1226 buf->len, PCI_DMA_FROMDEVICE);
1227
1228 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1229 &type, &color, &q_number, &completed_index,
1230 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1231 &csum_not_calc, &rss_hash, &bytes_written,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001232 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001233 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1234 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1235 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1236 &fcs_ok);
1237
1238 if (packet_error) {
1239
Scott Feldman350991e2009-09-03 17:02:19 +00001240 if (!fcs_ok) {
1241 if (bytes_written > 0)
1242 enic->rq_bad_fcs++;
1243 else if (bytes_written == 0)
1244 enic->rq_truncated_pkts++;
1245 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001246
1247 dev_kfree_skb_any(skb);
1248
1249 return;
1250 }
1251
1252 if (eop && bytes_written > 0) {
1253
1254 /* Good receive
1255 */
1256
1257 skb_put(skb, bytes_written);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001258 skb->protocol = eth_type_trans(skb, netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001259
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00001260 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001261 skb->csum = htons(checksum);
1262 skb->ip_summed = CHECKSUM_COMPLETE;
1263 }
1264
Scott Feldman86ca9db2008-11-21 21:26:55 -08001265 skb->dev = netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001266
Vasanthy Kolluri87574462011-06-09 10:36:52 +00001267 if (vlan_stripped) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001268
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001269 if (netdev->features & NETIF_F_GRO)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001270 vlan_gro_receive(&enic->napi[q_number],
1271 enic->vlan_group, vlan_tci, skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001272 else
1273 vlan_hwaccel_receive_skb(skb,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001274 enic->vlan_group, vlan_tci);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001275
1276 } else {
1277
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001278 if (netdev->features & NETIF_F_GRO)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001279 napi_gro_receive(&enic->napi[q_number], skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001280 else
1281 netif_receive_skb(skb);
1282
1283 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001284 } else {
1285
1286 /* Buffer overflow
1287 */
1288
1289 dev_kfree_skb_any(skb);
1290 }
1291}
1292
1293static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1294 u8 type, u16 q_number, u16 completed_index, void *opaque)
1295{
1296 struct enic *enic = vnic_dev_priv(vdev);
1297
1298 vnic_rq_service(&enic->rq[q_number], cq_desc,
1299 completed_index, VNIC_RQ_RETURN_DESC,
1300 enic_rq_indicate_buf, opaque);
1301
1302 return 0;
1303}
1304
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001305static int enic_poll(struct napi_struct *napi, int budget)
1306{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001307 struct net_device *netdev = napi->dev;
1308 struct enic *enic = netdev_priv(netdev);
1309 unsigned int cq_rq = enic_cq_rq(enic, 0);
1310 unsigned int cq_wq = enic_cq_wq(enic, 0);
1311 unsigned int intr = enic_legacy_io_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001312 unsigned int rq_work_to_do = budget;
1313 unsigned int wq_work_to_do = -1; /* no limit */
1314 unsigned int work_done, rq_work_done, wq_work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001315 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001316
1317 /* Service RQ (first) and WQ
1318 */
1319
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001320 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001321 rq_work_to_do, enic_rq_service, NULL);
1322
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001323 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001324 wq_work_to_do, enic_wq_service, NULL);
1325
1326 /* Accumulate intr event credits for this polling
1327 * cycle. An intr event is the completion of a
1328 * a WQ or RQ packet.
1329 */
1330
1331 work_done = rq_work_done + wq_work_done;
1332
1333 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001334 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001335 work_done,
1336 0 /* don't unmask intr */,
1337 0 /* don't reset intr timer */);
1338
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001339 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001340
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001341 /* Buffer allocation failed. Stay in polling
1342 * mode so we can try to fill the ring again.
1343 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001344
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001345 if (err)
1346 rq_work_done = rq_work_to_do;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001347
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001348 if (rq_work_done < rq_work_to_do) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001349
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001350 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001351 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001352 */
1353
Ben Hutchings288379f2009-01-19 16:43:59 -08001354 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001355 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001356 }
1357
1358 return rq_work_done;
1359}
1360
1361static int enic_poll_msix(struct napi_struct *napi, int budget)
1362{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001363 struct net_device *netdev = napi->dev;
1364 struct enic *enic = netdev_priv(netdev);
1365 unsigned int rq = (napi - &enic->napi[0]);
1366 unsigned int cq = enic_cq_rq(enic, rq);
1367 unsigned int intr = enic_msix_rq_intr(enic, rq);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001368 unsigned int work_to_do = budget;
1369 unsigned int work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001370 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001371
1372 /* Service RQ
1373 */
1374
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001375 work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001376 work_to_do, enic_rq_service, NULL);
1377
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001378 /* Return intr event credits for this polling
1379 * cycle. An intr event is the completion of a
1380 * RQ packet.
1381 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001382
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001383 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001384 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001385 work_done,
1386 0 /* don't unmask intr */,
1387 0 /* don't reset intr timer */);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001388
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001389 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001390
1391 /* Buffer allocation failed. Stay in polling mode
1392 * so we can try to fill the ring again.
1393 */
1394
1395 if (err)
1396 work_done = work_to_do;
1397
1398 if (work_done < work_to_do) {
1399
1400 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001401 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001402 */
1403
Ben Hutchings288379f2009-01-19 16:43:59 -08001404 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001405 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001406 }
1407
1408 return work_done;
1409}
1410
1411static void enic_notify_timer(unsigned long data)
1412{
1413 struct enic *enic = (struct enic *)data;
1414
1415 enic_notify_check(enic);
1416
Scott Feldman25f0a062008-09-24 11:23:32 -07001417 mod_timer(&enic->notify_timer,
1418 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001419}
1420
1421static void enic_free_intr(struct enic *enic)
1422{
1423 struct net_device *netdev = enic->netdev;
1424 unsigned int i;
1425
1426 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1427 case VNIC_DEV_INTR_MODE_INTX:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001428 free_irq(enic->pdev->irq, netdev);
1429 break;
Scott Feldman8f4d2482008-09-24 11:23:42 -07001430 case VNIC_DEV_INTR_MODE_MSI:
1431 free_irq(enic->pdev->irq, enic);
1432 break;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001433 case VNIC_DEV_INTR_MODE_MSIX:
1434 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1435 if (enic->msix[i].requested)
1436 free_irq(enic->msix_entry[i].vector,
1437 enic->msix[i].devid);
1438 break;
1439 default:
1440 break;
1441 }
1442}
1443
1444static int enic_request_intr(struct enic *enic)
1445{
1446 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001447 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001448 int err = 0;
1449
1450 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1451
1452 case VNIC_DEV_INTR_MODE_INTX:
1453
1454 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1455 IRQF_SHARED, netdev->name, netdev);
1456 break;
1457
1458 case VNIC_DEV_INTR_MODE_MSI:
1459
1460 err = request_irq(enic->pdev->irq, enic_isr_msi,
1461 0, netdev->name, enic);
1462 break;
1463
1464 case VNIC_DEV_INTR_MODE_MSIX:
1465
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001466 for (i = 0; i < enic->rq_count; i++) {
1467 intr = enic_msix_rq_intr(enic, i);
1468 sprintf(enic->msix[intr].devname,
1469 "%.11s-rx-%d", netdev->name, i);
1470 enic->msix[intr].isr = enic_isr_msix_rq;
1471 enic->msix[intr].devid = &enic->napi[i];
1472 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001473
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001474 for (i = 0; i < enic->wq_count; i++) {
1475 intr = enic_msix_wq_intr(enic, i);
1476 sprintf(enic->msix[intr].devname,
1477 "%.11s-tx-%d", netdev->name, i);
1478 enic->msix[intr].isr = enic_isr_msix_wq;
1479 enic->msix[intr].devid = enic;
1480 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001481
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001482 intr = enic_msix_err_intr(enic);
1483 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001484 "%.11s-err", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001485 enic->msix[intr].isr = enic_isr_msix_err;
1486 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001487
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001488 intr = enic_msix_notify_intr(enic);
1489 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001490 "%.11s-notify", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001491 enic->msix[intr].isr = enic_isr_msix_notify;
1492 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001493
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001494 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1495 enic->msix[i].requested = 0;
1496
1497 for (i = 0; i < enic->intr_count; i++) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001498 err = request_irq(enic->msix_entry[i].vector,
1499 enic->msix[i].isr, 0,
1500 enic->msix[i].devname,
1501 enic->msix[i].devid);
1502 if (err) {
1503 enic_free_intr(enic);
1504 break;
1505 }
1506 enic->msix[i].requested = 1;
1507 }
1508
1509 break;
1510
1511 default:
1512 break;
1513 }
1514
1515 return err;
1516}
1517
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001518static void enic_synchronize_irqs(struct enic *enic)
1519{
1520 unsigned int i;
1521
1522 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1523 case VNIC_DEV_INTR_MODE_INTX:
1524 case VNIC_DEV_INTR_MODE_MSI:
1525 synchronize_irq(enic->pdev->irq);
1526 break;
1527 case VNIC_DEV_INTR_MODE_MSIX:
1528 for (i = 0; i < enic->intr_count; i++)
1529 synchronize_irq(enic->msix_entry[i].vector);
1530 break;
1531 default:
1532 break;
1533 }
1534}
1535
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001536static int enic_dev_notify_set(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001537{
1538 int err;
1539
Scott Feldman56ac88b2009-09-03 17:02:14 +00001540 spin_lock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001541 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1542 case VNIC_DEV_INTR_MODE_INTX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001543 err = vnic_dev_notify_set(enic->vdev,
1544 enic_legacy_notify_intr());
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001545 break;
1546 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001547 err = vnic_dev_notify_set(enic->vdev,
1548 enic_msix_notify_intr(enic));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001549 break;
1550 default:
1551 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1552 break;
1553 }
Scott Feldman56ac88b2009-09-03 17:02:14 +00001554 spin_unlock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001555
1556 return err;
1557}
1558
1559static void enic_notify_timer_start(struct enic *enic)
1560{
1561 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1562 case VNIC_DEV_INTR_MODE_MSI:
1563 mod_timer(&enic->notify_timer, jiffies);
1564 break;
1565 default:
1566 /* Using intr for notification for INTx/MSI-X */
1567 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001568 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001569}
1570
1571/* rtnl lock is held, process context */
1572static int enic_open(struct net_device *netdev)
1573{
1574 struct enic *enic = netdev_priv(netdev);
1575 unsigned int i;
1576 int err;
1577
Scott Feldman4b75a442008-09-24 11:23:53 -07001578 err = enic_request_intr(enic);
1579 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001580 netdev_err(netdev, "Unable to request irq.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001581 return err;
1582 }
1583
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001584 err = enic_dev_notify_set(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001585 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001586 netdev_err(netdev,
1587 "Failed to alloc notify buffer, aborting.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001588 goto err_out_free_intr;
1589 }
1590
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001591 for (i = 0; i < enic->rq_count; i++) {
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001592 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001593 /* Need at least one buffer on ring to get going */
1594 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001595 netdev_err(netdev, "Unable to alloc receive buffers\n");
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001596 err = -ENOMEM;
Scott Feldman4b75a442008-09-24 11:23:53 -07001597 goto err_out_notify_unset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001598 }
1599 }
1600
1601 for (i = 0; i < enic->wq_count; i++)
1602 vnic_wq_enable(&enic->wq[i]);
1603 for (i = 0; i < enic->rq_count; i++)
1604 vnic_rq_enable(&enic->rq[i]);
1605
Roopa Prabhu296390592010-12-08 13:54:03 +00001606 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1607 enic_dev_add_addr(enic, enic->pp.mac_addr);
1608 else
1609 enic_dev_add_station_addr(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001610 enic_set_rx_mode(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001611
1612 netif_wake_queue(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001613
1614 for (i = 0; i < enic->rq_count; i++)
1615 napi_enable(&enic->napi[i]);
1616
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001617 enic_dev_enable(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001618
1619 for (i = 0; i < enic->intr_count; i++)
1620 vnic_intr_unmask(&enic->intr[i]);
1621
1622 enic_notify_timer_start(enic);
1623
1624 return 0;
Scott Feldman4b75a442008-09-24 11:23:53 -07001625
1626err_out_notify_unset:
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001627 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001628err_out_free_intr:
1629 enic_free_intr(enic);
1630
1631 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001632}
1633
1634/* rtnl lock is held, process context */
1635static int enic_stop(struct net_device *netdev)
1636{
1637 struct enic *enic = netdev_priv(netdev);
1638 unsigned int i;
1639 int err;
1640
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001641 for (i = 0; i < enic->intr_count; i++) {
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001642 vnic_intr_mask(&enic->intr[i]);
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001643 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1644 }
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001645
1646 enic_synchronize_irqs(enic);
1647
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001648 del_timer_sync(&enic->notify_timer);
1649
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001650 enic_dev_disable(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001651
1652 for (i = 0; i < enic->rq_count; i++)
1653 napi_disable(&enic->napi[i]);
1654
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001655 netif_carrier_off(netdev);
1656 netif_tx_disable(netdev);
Roopa Prabhu296390592010-12-08 13:54:03 +00001657 if (enic_is_dynamic(enic) && !is_zero_ether_addr(enic->pp.mac_addr))
1658 enic_dev_del_addr(enic, enic->pp.mac_addr);
1659 else
1660 enic_dev_del_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001661
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001662 for (i = 0; i < enic->wq_count; i++) {
1663 err = vnic_wq_disable(&enic->wq[i]);
1664 if (err)
1665 return err;
1666 }
1667 for (i = 0; i < enic->rq_count; i++) {
1668 err = vnic_rq_disable(&enic->rq[i]);
1669 if (err)
1670 return err;
1671 }
1672
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001673 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001674 enic_free_intr(enic);
1675
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001676 for (i = 0; i < enic->wq_count; i++)
1677 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1678 for (i = 0; i < enic->rq_count; i++)
1679 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1680 for (i = 0; i < enic->cq_count; i++)
1681 vnic_cq_clean(&enic->cq[i]);
1682 for (i = 0; i < enic->intr_count; i++)
1683 vnic_intr_clean(&enic->intr[i]);
1684
1685 return 0;
1686}
1687
1688static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1689{
1690 struct enic *enic = netdev_priv(netdev);
1691 int running = netif_running(netdev);
1692
Scott Feldman25f0a062008-09-24 11:23:32 -07001693 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1694 return -EINVAL;
1695
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001696 if (enic_is_dynamic(enic))
1697 return -EOPNOTSUPP;
1698
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001699 if (running)
1700 enic_stop(netdev);
1701
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001702 netdev->mtu = new_mtu;
1703
1704 if (netdev->mtu > enic->port_mtu)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001705 netdev_warn(netdev,
1706 "interface MTU (%d) set higher than port MTU (%d)\n",
1707 netdev->mtu, enic->port_mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001708
1709 if (running)
1710 enic_open(netdev);
1711
1712 return 0;
1713}
1714
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001715static void enic_change_mtu_work(struct work_struct *work)
1716{
1717 struct enic *enic = container_of(work, struct enic, change_mtu_work);
1718 struct net_device *netdev = enic->netdev;
1719 int new_mtu = vnic_dev_mtu(enic->vdev);
1720 int err;
1721 unsigned int i;
1722
1723 new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1724
1725 rtnl_lock();
1726
1727 /* Stop RQ */
1728 del_timer_sync(&enic->notify_timer);
1729
1730 for (i = 0; i < enic->rq_count; i++)
1731 napi_disable(&enic->napi[i]);
1732
1733 vnic_intr_mask(&enic->intr[0]);
1734 enic_synchronize_irqs(enic);
1735 err = vnic_rq_disable(&enic->rq[0]);
1736 if (err) {
1737 netdev_err(netdev, "Unable to disable RQ.\n");
1738 return;
1739 }
1740 vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1741 vnic_cq_clean(&enic->cq[0]);
1742 vnic_intr_clean(&enic->intr[0]);
1743
1744 /* Fill RQ with new_mtu-sized buffers */
1745 netdev->mtu = new_mtu;
1746 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1747 /* Need at least one buffer on ring to get going */
1748 if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1749 netdev_err(netdev, "Unable to alloc receive buffers.\n");
1750 return;
1751 }
1752
1753 /* Start RQ */
1754 vnic_rq_enable(&enic->rq[0]);
1755 napi_enable(&enic->napi[0]);
1756 vnic_intr_unmask(&enic->intr[0]);
1757 enic_notify_timer_start(enic);
1758
1759 rtnl_unlock();
1760
1761 netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1762}
1763
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001764#ifdef CONFIG_NET_POLL_CONTROLLER
1765static void enic_poll_controller(struct net_device *netdev)
1766{
1767 struct enic *enic = netdev_priv(netdev);
1768 struct vnic_dev *vdev = enic->vdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001769 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001770
1771 switch (vnic_dev_get_intr_mode(vdev)) {
1772 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001773 for (i = 0; i < enic->rq_count; i++) {
1774 intr = enic_msix_rq_intr(enic, i);
Vasanthy Kolluri79aeec52010-12-08 13:05:45 +00001775 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1776 &enic->napi[i]);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001777 }
Vasanthy Kollurib880a952011-06-09 10:37:07 +00001778
1779 for (i = 0; i < enic->wq_count; i++) {
1780 intr = enic_msix_wq_intr(enic, i);
1781 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1782 }
1783
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001784 break;
1785 case VNIC_DEV_INTR_MODE_MSI:
1786 enic_isr_msi(enic->pdev->irq, enic);
1787 break;
1788 case VNIC_DEV_INTR_MODE_INTX:
1789 enic_isr_legacy(enic->pdev->irq, netdev);
1790 break;
1791 default:
1792 break;
1793 }
1794}
1795#endif
1796
1797static int enic_dev_wait(struct vnic_dev *vdev,
1798 int (*start)(struct vnic_dev *, int),
1799 int (*finished)(struct vnic_dev *, int *),
1800 int arg)
1801{
1802 unsigned long time;
1803 int done;
1804 int err;
1805
1806 BUG_ON(in_interrupt());
1807
1808 err = start(vdev, arg);
1809 if (err)
1810 return err;
1811
1812 /* Wait for func to complete...2 seconds max
1813 */
1814
1815 time = jiffies + (HZ * 2);
1816 do {
1817
1818 err = finished(vdev, &done);
1819 if (err)
1820 return err;
1821
1822 if (done)
1823 return 0;
1824
1825 schedule_timeout_uninterruptible(HZ / 10);
1826
1827 } while (time_after(time, jiffies));
1828
1829 return -ETIMEDOUT;
1830}
1831
1832static int enic_dev_open(struct enic *enic)
1833{
1834 int err;
1835
1836 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1837 vnic_dev_open_done, 0);
1838 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001839 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1840 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001841
1842 return err;
1843}
1844
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001845static int enic_dev_hang_reset(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001846{
1847 int err;
1848
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001849 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1850 vnic_dev_hang_reset_done, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001851 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001852 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1853 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001854
1855 return err;
1856}
1857
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001858static int enic_set_rsskey(struct enic *enic)
Scott Feldman68f71702009-02-09 23:24:24 -08001859{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001860 dma_addr_t rss_key_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001861 union vnic_rss_key *rss_key_buf_va = NULL;
1862 union vnic_rss_key rss_key = {
1863 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1864 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1865 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1866 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1867 };
1868 int err;
1869
1870 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1871 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1872 if (!rss_key_buf_va)
1873 return -ENOMEM;
1874
1875 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1876
1877 spin_lock(&enic->devcmd_lock);
1878 err = enic_set_rss_key(enic,
1879 rss_key_buf_pa,
1880 sizeof(union vnic_rss_key));
1881 spin_unlock(&enic->devcmd_lock);
1882
1883 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1884 rss_key_buf_va, rss_key_buf_pa);
1885
1886 return err;
1887}
1888
1889static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1890{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001891 dma_addr_t rss_cpu_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001892 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1893 unsigned int i;
1894 int err;
1895
1896 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1897 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1898 if (!rss_cpu_buf_va)
1899 return -ENOMEM;
1900
1901 for (i = 0; i < (1 << rss_hash_bits); i++)
1902 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1903
1904 spin_lock(&enic->devcmd_lock);
1905 err = enic_set_rss_cpu(enic,
1906 rss_cpu_buf_pa,
1907 sizeof(union vnic_rss_cpu));
1908 spin_unlock(&enic->devcmd_lock);
1909
1910 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1911 rss_cpu_buf_va, rss_cpu_buf_pa);
1912
1913 return err;
1914}
1915
1916static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1917 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1918{
Scott Feldman68f71702009-02-09 23:24:24 -08001919 const u8 tso_ipid_split_en = 0;
1920 const u8 ig_vlan_strip_en = 1;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001921 int err;
Scott Feldman68f71702009-02-09 23:24:24 -08001922
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001923 /* Enable VLAN tag stripping.
1924 */
Scott Feldman68f71702009-02-09 23:24:24 -08001925
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001926 spin_lock(&enic->devcmd_lock);
1927 err = enic_set_nic_cfg(enic,
Scott Feldman68f71702009-02-09 23:24:24 -08001928 rss_default_cpu, rss_hash_type,
1929 rss_hash_bits, rss_base_cpu,
1930 rss_enable, tso_ipid_split_en,
1931 ig_vlan_strip_en);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001932 spin_unlock(&enic->devcmd_lock);
1933
1934 return err;
1935}
1936
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001937static int enic_set_rss_nic_cfg(struct enic *enic)
1938{
1939 struct device *dev = enic_get_dev(enic);
1940 const u8 rss_default_cpu = 0;
1941 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1942 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1943 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1944 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1945 const u8 rss_hash_bits = 7;
1946 const u8 rss_base_cpu = 0;
1947 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1948
1949 if (rss_enable) {
1950 if (!enic_set_rsskey(enic)) {
1951 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1952 rss_enable = 0;
1953 dev_warn(dev, "RSS disabled, "
1954 "Failed to set RSS cpu indirection table.");
1955 }
1956 } else {
1957 rss_enable = 0;
1958 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1959 }
1960 }
1961
1962 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1963 rss_hash_bits, rss_base_cpu, rss_enable);
1964}
1965
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001966static void enic_reset(struct work_struct *work)
1967{
1968 struct enic *enic = container_of(work, struct enic, reset);
1969
1970 if (!netif_running(enic->netdev))
1971 return;
1972
1973 rtnl_lock();
1974
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001975 enic_dev_hang_notify(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001976 enic_stop(enic->netdev);
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001977 enic_dev_hang_reset(enic);
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001978 enic_reset_addr_lists(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001979 enic_init_vnic_resources(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001980 enic_set_rss_nic_cfg(enic);
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001981 enic_dev_set_ig_vlan_rewrite_mode(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001982 enic_open(enic->netdev);
1983
1984 rtnl_unlock();
1985}
1986
1987static int enic_set_intr_mode(struct enic *enic)
1988{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001989 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
Vasanthy Kolluri1cbb1a62011-02-17 13:57:19 +00001990 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001991 unsigned int i;
1992
1993 /* Set interrupt mode (INTx, MSI, MSI-X) depending
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001994 * on system capabilities.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001995 *
1996 * Try MSI-X first
1997 *
1998 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1999 * (the second to last INTR is used for WQ/RQ errors)
2000 * (the last INTR is used for notifications)
2001 */
2002
2003 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2004 for (i = 0; i < n + m + 2; i++)
2005 enic->msix_entry[i].entry = i;
2006
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002007 /* Use multiple RQs if RSS is enabled
2008 */
2009
2010 if (ENIC_SETTING(enic, RSS) &&
2011 enic->config.intr_mode < 1 &&
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002012 enic->rq_count >= n &&
2013 enic->wq_count >= m &&
2014 enic->cq_count >= n + m &&
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002015 enic->intr_count >= n + m + 2) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002016
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002017 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002018
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002019 enic->rq_count = n;
2020 enic->wq_count = m;
2021 enic->cq_count = n + m;
2022 enic->intr_count = n + m + 2;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002023
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002024 vnic_dev_set_intr_mode(enic->vdev,
2025 VNIC_DEV_INTR_MODE_MSIX);
2026
2027 return 0;
2028 }
2029 }
2030
2031 if (enic->config.intr_mode < 1 &&
2032 enic->rq_count >= 1 &&
2033 enic->wq_count >= m &&
2034 enic->cq_count >= 1 + m &&
2035 enic->intr_count >= 1 + m + 2) {
2036 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2037
2038 enic->rq_count = 1;
2039 enic->wq_count = m;
2040 enic->cq_count = 1 + m;
2041 enic->intr_count = 1 + m + 2;
2042
2043 vnic_dev_set_intr_mode(enic->vdev,
2044 VNIC_DEV_INTR_MODE_MSIX);
2045
2046 return 0;
2047 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002048 }
2049
2050 /* Next try MSI
2051 *
2052 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2053 */
2054
2055 if (enic->config.intr_mode < 2 &&
2056 enic->rq_count >= 1 &&
2057 enic->wq_count >= 1 &&
2058 enic->cq_count >= 2 &&
2059 enic->intr_count >= 1 &&
2060 !pci_enable_msi(enic->pdev)) {
2061
2062 enic->rq_count = 1;
2063 enic->wq_count = 1;
2064 enic->cq_count = 2;
2065 enic->intr_count = 1;
2066
2067 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2068
2069 return 0;
2070 }
2071
2072 /* Next try INTx
2073 *
2074 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2075 * (the first INTR is used for WQ/RQ)
2076 * (the second INTR is used for WQ/RQ errors)
2077 * (the last INTR is used for notifications)
2078 */
2079
2080 if (enic->config.intr_mode < 3 &&
2081 enic->rq_count >= 1 &&
2082 enic->wq_count >= 1 &&
2083 enic->cq_count >= 2 &&
2084 enic->intr_count >= 3) {
2085
2086 enic->rq_count = 1;
2087 enic->wq_count = 1;
2088 enic->cq_count = 2;
2089 enic->intr_count = 3;
2090
2091 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2092
2093 return 0;
2094 }
2095
2096 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2097
2098 return -EINVAL;
2099}
2100
2101static void enic_clear_intr_mode(struct enic *enic)
2102{
2103 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2104 case VNIC_DEV_INTR_MODE_MSIX:
2105 pci_disable_msix(enic->pdev);
2106 break;
2107 case VNIC_DEV_INTR_MODE_MSI:
2108 pci_disable_msi(enic->pdev);
2109 break;
2110 default:
2111 break;
2112 }
2113
2114 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2115}
2116
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002117static const struct net_device_ops enic_netdev_dynamic_ops = {
2118 .ndo_open = enic_open,
2119 .ndo_stop = enic_stop,
2120 .ndo_start_xmit = enic_hard_start_xmit,
stephen hemmingerf20530b2011-06-08 14:54:02 +00002121 .ndo_get_stats64 = enic_get_stats,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002122 .ndo_validate_addr = eth_validate_addr,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002123 .ndo_set_rx_mode = enic_set_rx_mode,
2124 .ndo_set_multicast_list = enic_set_rx_mode,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002125 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2126 .ndo_change_mtu = enic_change_mtu,
2127 .ndo_vlan_rx_register = enic_vlan_rx_register,
2128 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2129 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2130 .ndo_tx_timeout = enic_tx_timeout,
2131 .ndo_set_vf_port = enic_set_vf_port,
2132 .ndo_get_vf_port = enic_get_vf_port,
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00002133 .ndo_set_vf_mac = enic_set_vf_mac,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002134#ifdef CONFIG_NET_POLL_CONTROLLER
2135 .ndo_poll_controller = enic_poll_controller,
2136#endif
2137};
2138
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002139static const struct net_device_ops enic_netdev_ops = {
2140 .ndo_open = enic_open,
2141 .ndo_stop = enic_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -08002142 .ndo_start_xmit = enic_hard_start_xmit,
stephen hemmingerf20530b2011-06-08 14:54:02 +00002143 .ndo_get_stats64 = enic_get_stats,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002144 .ndo_validate_addr = eth_validate_addr,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002145 .ndo_set_mac_address = enic_set_mac_address,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002146 .ndo_set_rx_mode = enic_set_rx_mode,
2147 .ndo_set_multicast_list = enic_set_rx_mode,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002148 .ndo_change_mtu = enic_change_mtu,
2149 .ndo_vlan_rx_register = enic_vlan_rx_register,
2150 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2151 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2152 .ndo_tx_timeout = enic_tx_timeout,
2153#ifdef CONFIG_NET_POLL_CONTROLLER
2154 .ndo_poll_controller = enic_poll_controller,
2155#endif
2156};
2157
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002158static void enic_dev_deinit(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002159{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002160 unsigned int i;
2161
2162 for (i = 0; i < enic->rq_count; i++)
2163 netif_napi_del(&enic->napi[i]);
2164
Scott Feldman6fdfa972009-09-03 17:02:45 +00002165 enic_free_vnic_resources(enic);
2166 enic_clear_intr_mode(enic);
2167}
2168
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002169static int enic_dev_init(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002170{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002171 struct device *dev = enic_get_dev(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002172 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002173 unsigned int i;
Scott Feldman6fdfa972009-09-03 17:02:45 +00002174 int err;
2175
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +00002176 /* Get interrupt coalesce timer info */
2177 err = enic_dev_intr_coal_timer_info(enic);
2178 if (err) {
2179 dev_warn(dev, "Using default conversion factor for "
2180 "interrupt coalesce timer\n");
2181 vnic_dev_intr_coal_timer_info_default(enic->vdev);
2182 }
2183
Scott Feldman6fdfa972009-09-03 17:02:45 +00002184 /* Get vNIC configuration
2185 */
2186
2187 err = enic_get_vnic_config(enic);
2188 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002189 dev_err(dev, "Get vNIC configuration failed, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002190 return err;
2191 }
2192
2193 /* Get available resource counts
2194 */
2195
2196 enic_get_res_counts(enic);
2197
2198 /* Set interrupt mode based on resource counts and system
2199 * capabilities
2200 */
2201
2202 err = enic_set_intr_mode(enic);
2203 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002204 dev_err(dev, "Failed to set intr mode based on resource "
2205 "counts and system capabilities, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002206 return err;
2207 }
2208
2209 /* Allocate and configure vNIC resources
2210 */
2211
2212 err = enic_alloc_vnic_resources(enic);
2213 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002214 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002215 goto err_out_free_vnic_resources;
2216 }
2217
2218 enic_init_vnic_resources(enic);
2219
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002220 err = enic_set_rss_nic_cfg(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002221 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002222 dev_err(dev, "Failed to config nic, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002223 goto err_out_free_vnic_resources;
2224 }
2225
2226 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2227 default:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002228 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002229 break;
2230 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002231 for (i = 0; i < enic->rq_count; i++)
2232 netif_napi_add(netdev, &enic->napi[i],
2233 enic_poll_msix, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002234 break;
2235 }
2236
2237 return 0;
2238
2239err_out_free_vnic_resources:
2240 enic_clear_intr_mode(enic);
2241 enic_free_vnic_resources(enic);
2242
2243 return err;
2244}
2245
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002246static void enic_iounmap(struct enic *enic)
2247{
2248 unsigned int i;
2249
2250 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2251 if (enic->bar[i].vaddr)
2252 iounmap(enic->bar[i].vaddr);
2253}
2254
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002255static int __devinit enic_probe(struct pci_dev *pdev,
2256 const struct pci_device_id *ent)
2257{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002258 struct device *dev = &pdev->dev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002259 struct net_device *netdev;
2260 struct enic *enic;
2261 int using_dac = 0;
2262 unsigned int i;
2263 int err;
2264
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002265 /* Allocate net device structure and initialize. Private
2266 * instance data is initialized to zero.
2267 */
2268
2269 netdev = alloc_etherdev(sizeof(struct enic));
2270 if (!netdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002271 pr_err("Etherdev alloc failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002272 return -ENOMEM;
2273 }
2274
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002275 pci_set_drvdata(pdev, netdev);
2276
2277 SET_NETDEV_DEV(netdev, &pdev->dev);
2278
2279 enic = netdev_priv(netdev);
2280 enic->netdev = netdev;
2281 enic->pdev = pdev;
2282
2283 /* Setup PCI resources
2284 */
2285
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002286 err = pci_enable_device_mem(pdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002287 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002288 dev_err(dev, "Cannot enable PCI device, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002289 goto err_out_free_netdev;
2290 }
2291
2292 err = pci_request_regions(pdev, DRV_NAME);
2293 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002294 dev_err(dev, "Cannot request PCI regions, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002295 goto err_out_disable_device;
2296 }
2297
2298 pci_set_master(pdev);
2299
2300 /* Query PCI controller on system for DMA addressing
2301 * limitation for the device. Try 40-bit first, and
2302 * fail to 32-bit.
2303 */
2304
Yang Hongyang50cf1562009-04-06 19:01:14 -07002305 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002306 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07002307 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002308 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002309 dev_err(dev, "No usable DMA configuration, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002310 goto err_out_release_regions;
2311 }
Yang Hongyang284901a2009-04-06 19:01:15 -07002312 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002313 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002314 dev_err(dev, "Unable to obtain %u-bit DMA "
2315 "for consistent allocations, aborting\n", 32);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002316 goto err_out_release_regions;
2317 }
2318 } else {
Yang Hongyang50cf1562009-04-06 19:01:14 -07002319 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002320 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002321 dev_err(dev, "Unable to obtain %u-bit DMA "
2322 "for consistent allocations, aborting\n", 40);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002323 goto err_out_release_regions;
2324 }
2325 using_dac = 1;
2326 }
2327
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002328 /* Map vNIC resources from BAR0-5
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002329 */
2330
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002331 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2332 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2333 continue;
2334 enic->bar[i].len = pci_resource_len(pdev, i);
2335 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2336 if (!enic->bar[i].vaddr) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002337 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002338 err = -ENODEV;
2339 goto err_out_iounmap;
2340 }
2341 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002342 }
2343
2344 /* Register vNIC device
2345 */
2346
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002347 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2348 ARRAY_SIZE(enic->bar));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002349 if (!enic->vdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002350 dev_err(dev, "vNIC registration failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002351 err = -ENODEV;
2352 goto err_out_iounmap;
2353 }
2354
2355 /* Issue device open to get device in known state
2356 */
2357
2358 err = enic_dev_open(enic);
2359 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002360 dev_err(dev, "vNIC dev open failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002361 goto err_out_vnic_unregister;
2362 }
2363
Vasanthy Kolluri69161422011-02-04 16:17:16 +00002364 /* Setup devcmd lock
2365 */
2366
2367 spin_lock_init(&enic->devcmd_lock);
2368
2369 /*
2370 * Set ingress vlan rewrite mode before vnic initialization
2371 */
2372
2373 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2374 if (err) {
2375 dev_err(dev,
2376 "Failed to set ingress vlan rewrite mode, aborting.\n");
2377 goto err_out_dev_close;
2378 }
2379
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002380 /* Issue device init to initialize the vnic-to-switch link.
2381 * We'll start with carrier off and wait for link UP
2382 * notification later to turn on carrier. We don't need
2383 * to wait here for the vnic-to-switch link initialization
2384 * to complete; link UP notification is the indication that
2385 * the process is complete.
2386 */
2387
2388 netif_carrier_off(netdev);
2389
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002390 /* Do not call dev_init for a dynamic vnic.
2391 * For a dynamic vnic, init_prov_info will be
2392 * called later by an upper layer.
2393 */
2394
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002395 if (!enic_is_dynamic(enic)) {
2396 err = vnic_dev_init(enic->vdev, 0);
2397 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002398 dev_err(dev, "vNIC dev init failed, aborting\n");
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002399 goto err_out_dev_close;
2400 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002401 }
2402
Scott Feldman6fdfa972009-09-03 17:02:45 +00002403 err = enic_dev_init(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002404 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002405 dev_err(dev, "Device initialization failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002406 goto err_out_dev_close;
2407 }
2408
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002409 /* Setup notification timer, HW reset task, and wq locks
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002410 */
2411
2412 init_timer(&enic->notify_timer);
2413 enic->notify_timer.function = enic_notify_timer;
2414 enic->notify_timer.data = (unsigned long)enic;
2415
2416 INIT_WORK(&enic->reset, enic_reset);
Roopa Prabhuc97c8942011-06-03 14:35:17 +00002417 INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002418
2419 for (i = 0; i < enic->wq_count; i++)
2420 spin_lock_init(&enic->wq_lock[i]);
2421
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002422 /* Register net device
2423 */
2424
2425 enic->port_mtu = enic->config.mtu;
2426 (void)enic_change_mtu(netdev, enic->port_mtu);
2427
2428 err = enic_set_mac_addr(netdev, enic->mac_addr);
2429 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002430 dev_err(dev, "Invalid MAC address, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002431 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002432 }
2433
Scott Feldman7c844592009-12-23 13:27:54 +00002434 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2435 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2436
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002437 if (enic_is_dynamic(enic))
2438 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2439 else
2440 netdev->netdev_ops = &enic_netdev_ops;
2441
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002442 netdev->watchdog_timeo = 2 * HZ;
2443 netdev->ethtool_ops = &enic_ethtool_ops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002444
Vasanthy Kolluri73c1ea92010-03-18 16:19:54 +00002445 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00002446 if (ENIC_SETTING(enic, LOOP)) {
2447 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2448 enic->loop_enable = 1;
2449 enic->loop_tag = enic->config.loop_tag;
2450 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2451 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002452 if (ENIC_SETTING(enic, TXCSUM))
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002453 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002454 if (ENIC_SETTING(enic, TSO))
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002455 netdev->hw_features |= NETIF_F_TSO |
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002456 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002457 if (ENIC_SETTING(enic, RXCSUM))
2458 netdev->hw_features |= NETIF_F_RXCSUM;
2459
2460 netdev->features |= netdev->hw_features;
2461
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002462 if (using_dac)
2463 netdev->features |= NETIF_F_HIGHDMA;
2464
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002465 err = register_netdev(netdev);
2466 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002467 dev_err(dev, "Cannot register net device, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002468 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002469 }
2470
2471 return 0;
2472
Scott Feldman6fdfa972009-09-03 17:02:45 +00002473err_out_dev_deinit:
2474 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002475err_out_dev_close:
2476 vnic_dev_close(enic->vdev);
2477err_out_vnic_unregister:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002478 vnic_dev_unregister(enic->vdev);
2479err_out_iounmap:
2480 enic_iounmap(enic);
2481err_out_release_regions:
2482 pci_release_regions(pdev);
2483err_out_disable_device:
2484 pci_disable_device(pdev);
2485err_out_free_netdev:
2486 pci_set_drvdata(pdev, NULL);
2487 free_netdev(netdev);
2488
2489 return err;
2490}
2491
2492static void __devexit enic_remove(struct pci_dev *pdev)
2493{
2494 struct net_device *netdev = pci_get_drvdata(pdev);
2495
2496 if (netdev) {
2497 struct enic *enic = netdev_priv(netdev);
2498
Tejun Heo23f333a2010-12-12 16:45:14 +01002499 cancel_work_sync(&enic->reset);
Roopa Prabhuc97c8942011-06-03 14:35:17 +00002500 cancel_work_sync(&enic->change_mtu_work);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002501 unregister_netdev(netdev);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002502 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002503 vnic_dev_close(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002504 vnic_dev_unregister(enic->vdev);
2505 enic_iounmap(enic);
2506 pci_release_regions(pdev);
2507 pci_disable_device(pdev);
2508 pci_set_drvdata(pdev, NULL);
2509 free_netdev(netdev);
2510 }
2511}
2512
2513static struct pci_driver enic_driver = {
2514 .name = DRV_NAME,
2515 .id_table = enic_id_table,
2516 .probe = enic_probe,
2517 .remove = __devexit_p(enic_remove),
2518};
2519
2520static int __init enic_init_module(void)
2521{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002522 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002523
2524 return pci_register_driver(&enic_driver);
2525}
2526
2527static void __exit enic_cleanup_module(void)
2528{
2529 pci_unregister_driver(&enic_driver);
2530}
2531
2532module_init(enic_init_module);
2533module_exit(enic_cleanup_module);