blob: 64866ff1aea0d9894f2a296ebed80324e5638f15 [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>
Jiri Pirko01789342011-08-16 06:29:00 +000031#include <linux/if.h>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070032#include <linux/if_ether.h>
33#include <linux/if_vlan.h>
34#include <linux/ethtool.h>
35#include <linux/in.h>
36#include <linux/ip.h>
37#include <linux/ipv6.h>
38#include <linux/tcp.h>
Vasanthy Kolluri29046f92010-06-24 10:52:26 +000039#include <linux/rtnetlink.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040040#include <linux/prefetch.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070041#include <net/ip6_checksum.h>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070042
43#include "cq_enet_desc.h"
44#include "vnic_dev.h"
45#include "vnic_intr.h"
46#include "vnic_stats.h"
Scott Feldmanf8bd9092010-05-17 22:50:19 -070047#include "vnic_vic.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070048#include "enic_res.h"
49#include "enic.h"
Vasanthy Kolluri51987462011-02-04 16:17:05 +000050#include "enic_dev.h"
Roopa Prabhub3abfbd2011-03-29 20:36:07 +000051#include "enic_pp.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070052
53#define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
Scott Feldmanea0d7d92009-09-03 17:02:03 +000054#define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
55#define MAX_TSO (1 << 16)
56#define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
57
58#define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
Scott Feldmanf8bd9092010-05-17 22:50:19 -070059#define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
Roopa Prabhu3a4adef2012-01-18 04:23:55 +000060#define PCI_DEVICE_ID_CISCO_VIC_ENET_VF 0x0071 /* enet SRIOV VF */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070061
62/* Supported devices */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000063static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
Scott Feldmanea0d7d92009-09-03 17:02:03 +000064 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
Scott Feldmanf8bd9092010-05-17 22:50:19 -070065 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
Roopa Prabhu3a4adef2012-01-18 04:23:55 +000066 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
Scott Feldman01f2e4e2008-09-15 09:17:11 -070067 { 0, } /* end of table */
68};
69
70MODULE_DESCRIPTION(DRV_DESCRIPTION);
71MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
72MODULE_LICENSE("GPL");
73MODULE_VERSION(DRV_VERSION);
74MODULE_DEVICE_TABLE(pci, enic_id_table);
75
76struct enic_stat {
77 char name[ETH_GSTRING_LEN];
78 unsigned int offset;
79};
80
81#define ENIC_TX_STAT(stat) \
82 { .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
83#define ENIC_RX_STAT(stat) \
84 { .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
85
86static const struct enic_stat enic_tx_stats[] = {
87 ENIC_TX_STAT(tx_frames_ok),
88 ENIC_TX_STAT(tx_unicast_frames_ok),
89 ENIC_TX_STAT(tx_multicast_frames_ok),
90 ENIC_TX_STAT(tx_broadcast_frames_ok),
91 ENIC_TX_STAT(tx_bytes_ok),
92 ENIC_TX_STAT(tx_unicast_bytes_ok),
93 ENIC_TX_STAT(tx_multicast_bytes_ok),
94 ENIC_TX_STAT(tx_broadcast_bytes_ok),
95 ENIC_TX_STAT(tx_drops),
96 ENIC_TX_STAT(tx_errors),
97 ENIC_TX_STAT(tx_tso),
98};
99
100static const struct enic_stat enic_rx_stats[] = {
101 ENIC_RX_STAT(rx_frames_ok),
102 ENIC_RX_STAT(rx_frames_total),
103 ENIC_RX_STAT(rx_unicast_frames_ok),
104 ENIC_RX_STAT(rx_multicast_frames_ok),
105 ENIC_RX_STAT(rx_broadcast_frames_ok),
106 ENIC_RX_STAT(rx_bytes_ok),
107 ENIC_RX_STAT(rx_unicast_bytes_ok),
108 ENIC_RX_STAT(rx_multicast_bytes_ok),
109 ENIC_RX_STAT(rx_broadcast_bytes_ok),
110 ENIC_RX_STAT(rx_drop),
111 ENIC_RX_STAT(rx_no_bufs),
112 ENIC_RX_STAT(rx_errors),
113 ENIC_RX_STAT(rx_rss),
114 ENIC_RX_STAT(rx_crc_errors),
115 ENIC_RX_STAT(rx_frames_64),
116 ENIC_RX_STAT(rx_frames_127),
117 ENIC_RX_STAT(rx_frames_255),
118 ENIC_RX_STAT(rx_frames_511),
119 ENIC_RX_STAT(rx_frames_1023),
120 ENIC_RX_STAT(rx_frames_1518),
121 ENIC_RX_STAT(rx_frames_to_max),
122};
123
124static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
125static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
126
Roopa Prabhu3f192792011-09-22 03:44:43 +0000127int enic_is_dynamic(struct enic *enic)
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700128{
129 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
130}
131
Roopa Prabhu8749b422011-09-22 03:44:33 +0000132int enic_sriov_enabled(struct enic *enic)
133{
134 return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
135}
136
Roopa Prabhu3a4adef2012-01-18 04:23:55 +0000137static int enic_is_sriov_vf(struct enic *enic)
138{
139 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
140}
141
Roopa Prabhu889d13f2011-09-22 03:44:38 +0000142int enic_is_valid_vf(struct enic *enic, int vf)
143{
144#ifdef CONFIG_PCI_IOV
145 return vf >= 0 && vf < enic->num_vfs;
146#else
147 return 0;
148#endif
149}
150
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000151static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
152{
153 return rq;
154}
155
156static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
157{
158 return enic->rq_count + wq;
159}
160
161static inline unsigned int enic_legacy_io_intr(void)
162{
163 return 0;
164}
165
166static inline unsigned int enic_legacy_err_intr(void)
167{
168 return 1;
169}
170
171static inline unsigned int enic_legacy_notify_intr(void)
172{
173 return 2;
174}
175
176static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
177{
Vasanthy Kolluri7d260ec2011-06-09 10:37:02 +0000178 return enic->cq[enic_cq_rq(enic, rq)].interrupt_offset;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000179}
180
181static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
182{
Vasanthy Kolluri7d260ec2011-06-09 10:37:02 +0000183 return enic->cq[enic_cq_wq(enic, wq)].interrupt_offset;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000184}
185
186static inline unsigned int enic_msix_err_intr(struct enic *enic)
187{
188 return enic->rq_count + enic->wq_count;
189}
190
191static inline unsigned int enic_msix_notify_intr(struct enic *enic)
192{
193 return enic->rq_count + enic->wq_count + 1;
194}
195
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700196static int enic_get_settings(struct net_device *netdev,
197 struct ethtool_cmd *ecmd)
198{
199 struct enic *enic = netdev_priv(netdev);
200
201 ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
202 ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
203 ecmd->port = PORT_FIBRE;
204 ecmd->transceiver = XCVR_EXTERNAL;
205
206 if (netif_carrier_ok(netdev)) {
David Decotigny70739492011-04-27 18:32:40 +0000207 ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700208 ecmd->duplex = DUPLEX_FULL;
209 } else {
David Decotigny70739492011-04-27 18:32:40 +0000210 ethtool_cmd_speed_set(ecmd, -1);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700211 ecmd->duplex = -1;
212 }
213
214 ecmd->autoneg = AUTONEG_DISABLE;
215
216 return 0;
217}
218
219static void enic_get_drvinfo(struct net_device *netdev,
220 struct ethtool_drvinfo *drvinfo)
221{
222 struct enic *enic = netdev_priv(netdev);
223 struct vnic_devcmd_fw_info *fw_info;
224
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000225 enic_dev_fw_info(enic, &fw_info);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700226
Rick Jones612a94d2011-11-14 08:13:25 +0000227 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
228 strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
229 strlcpy(drvinfo->fw_version, fw_info->fw_version,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700230 sizeof(drvinfo->fw_version));
Rick Jones612a94d2011-11-14 08:13:25 +0000231 strlcpy(drvinfo->bus_info, pci_name(enic->pdev),
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700232 sizeof(drvinfo->bus_info));
233}
234
235static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
236{
237 unsigned int i;
238
239 switch (stringset) {
240 case ETH_SS_STATS:
241 for (i = 0; i < enic_n_tx_stats; i++) {
242 memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
243 data += ETH_GSTRING_LEN;
244 }
245 for (i = 0; i < enic_n_rx_stats; i++) {
246 memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
247 data += ETH_GSTRING_LEN;
248 }
249 break;
250 }
251}
252
Scott Feldman25f0a062008-09-24 11:23:32 -0700253static int enic_get_sset_count(struct net_device *netdev, int sset)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700254{
Scott Feldman25f0a062008-09-24 11:23:32 -0700255 switch (sset) {
256 case ETH_SS_STATS:
257 return enic_n_tx_stats + enic_n_rx_stats;
258 default:
259 return -EOPNOTSUPP;
260 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700261}
262
263static void enic_get_ethtool_stats(struct net_device *netdev,
264 struct ethtool_stats *stats, u64 *data)
265{
266 struct enic *enic = netdev_priv(netdev);
267 struct vnic_stats *vstats;
268 unsigned int i;
269
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000270 enic_dev_stats_dump(enic, &vstats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700271
272 for (i = 0; i < enic_n_tx_stats; i++)
273 *(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
274 for (i = 0; i < enic_n_rx_stats; i++)
275 *(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
276}
277
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700278static u32 enic_get_msglevel(struct net_device *netdev)
279{
280 struct enic *enic = netdev_priv(netdev);
281 return enic->msg_enable;
282}
283
284static void enic_set_msglevel(struct net_device *netdev, u32 value)
285{
286 struct enic *enic = netdev_priv(netdev);
287 enic->msg_enable = value;
288}
289
Scott Feldman7c844592009-12-23 13:27:54 +0000290static int enic_get_coalesce(struct net_device *netdev,
291 struct ethtool_coalesce *ecmd)
292{
293 struct enic *enic = netdev_priv(netdev);
294
295 ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
296 ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
297
298 return 0;
299}
300
301static int enic_set_coalesce(struct net_device *netdev,
302 struct ethtool_coalesce *ecmd)
303{
304 struct enic *enic = netdev_priv(netdev);
305 u32 tx_coalesce_usecs;
306 u32 rx_coalesce_usecs;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000307 unsigned int i, intr;
Scott Feldman7c844592009-12-23 13:27:54 +0000308
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000309 tx_coalesce_usecs = min_t(u32, ecmd->tx_coalesce_usecs,
310 vnic_dev_get_intr_coal_timer_max(enic->vdev));
311 rx_coalesce_usecs = min_t(u32, ecmd->rx_coalesce_usecs,
312 vnic_dev_get_intr_coal_timer_max(enic->vdev));
Scott Feldman7c844592009-12-23 13:27:54 +0000313
314 switch (vnic_dev_get_intr_mode(enic->vdev)) {
315 case VNIC_DEV_INTR_MODE_INTX:
316 if (tx_coalesce_usecs != rx_coalesce_usecs)
317 return -EINVAL;
318
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000319 intr = enic_legacy_io_intr();
320 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000321 tx_coalesce_usecs);
Scott Feldman7c844592009-12-23 13:27:54 +0000322 break;
323 case VNIC_DEV_INTR_MODE_MSI:
324 if (tx_coalesce_usecs != rx_coalesce_usecs)
325 return -EINVAL;
326
327 vnic_intr_coalescing_timer_set(&enic->intr[0],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000328 tx_coalesce_usecs);
Scott Feldman7c844592009-12-23 13:27:54 +0000329 break;
330 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000331 for (i = 0; i < enic->wq_count; i++) {
332 intr = enic_msix_wq_intr(enic, i);
333 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000334 tx_coalesce_usecs);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000335 }
336
337 for (i = 0; i < enic->rq_count; i++) {
338 intr = enic_msix_rq_intr(enic, i);
339 vnic_intr_coalescing_timer_set(&enic->intr[intr],
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +0000340 rx_coalesce_usecs);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000341 }
342
Scott Feldman7c844592009-12-23 13:27:54 +0000343 break;
344 default:
345 break;
346 }
347
348 enic->tx_coalesce_usecs = tx_coalesce_usecs;
349 enic->rx_coalesce_usecs = rx_coalesce_usecs;
350
351 return 0;
352}
353
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700354static const struct ethtool_ops enic_ethtool_ops = {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700355 .get_settings = enic_get_settings,
356 .get_drvinfo = enic_get_drvinfo,
357 .get_msglevel = enic_get_msglevel,
358 .set_msglevel = enic_set_msglevel,
359 .get_link = ethtool_op_get_link,
360 .get_strings = enic_get_strings,
Scott Feldman25f0a062008-09-24 11:23:32 -0700361 .get_sset_count = enic_get_sset_count,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700362 .get_ethtool_stats = enic_get_ethtool_stats,
Scott Feldman7c844592009-12-23 13:27:54 +0000363 .get_coalesce = enic_get_coalesce,
364 .set_coalesce = enic_set_coalesce,
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700365};
366
367static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
368{
369 struct enic *enic = vnic_dev_priv(wq->vdev);
370
371 if (buf->sop)
372 pci_unmap_single(enic->pdev, buf->dma_addr,
373 buf->len, PCI_DMA_TODEVICE);
374 else
375 pci_unmap_page(enic->pdev, buf->dma_addr,
376 buf->len, PCI_DMA_TODEVICE);
377
378 if (buf->os_buf)
379 dev_kfree_skb_any(buf->os_buf);
380}
381
382static void enic_wq_free_buf(struct vnic_wq *wq,
383 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
384{
385 enic_free_wq_buf(wq, buf);
386}
387
388static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
389 u8 type, u16 q_number, u16 completed_index, void *opaque)
390{
391 struct enic *enic = vnic_dev_priv(vdev);
392
393 spin_lock(&enic->wq_lock[q_number]);
394
395 vnic_wq_service(&enic->wq[q_number], cq_desc,
396 completed_index, enic_wq_free_buf,
397 opaque);
398
399 if (netif_queue_stopped(enic->netdev) &&
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000400 vnic_wq_desc_avail(&enic->wq[q_number]) >=
401 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700402 netif_wake_queue(enic->netdev);
403
404 spin_unlock(&enic->wq_lock[q_number]);
405
406 return 0;
407}
408
409static void enic_log_q_error(struct enic *enic)
410{
411 unsigned int i;
412 u32 error_status;
413
414 for (i = 0; i < enic->wq_count; i++) {
415 error_status = vnic_wq_error_status(&enic->wq[i]);
416 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000417 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
418 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700419 }
420
421 for (i = 0; i < enic->rq_count; i++) {
422 error_status = vnic_rq_error_status(&enic->rq[i]);
423 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000424 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
425 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700426 }
427}
428
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000429static void enic_msglvl_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700430{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000431 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700432
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000433 if (msg_enable != enic->msg_enable) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000434 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
435 enic->msg_enable, msg_enable);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000436 enic->msg_enable = msg_enable;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700437 }
438}
439
440static void enic_mtu_check(struct enic *enic)
441{
442 u32 mtu = vnic_dev_mtu(enic->vdev);
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000443 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700444
Scott Feldman491598a2009-09-03 17:02:40 +0000445 if (mtu && mtu != enic->port_mtu) {
Scott Feldman7c844592009-12-23 13:27:54 +0000446 enic->port_mtu = mtu;
Roopa Prabhu73359032012-01-18 04:24:02 +0000447 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
Roopa Prabhuc97c8942011-06-03 14:35:17 +0000448 mtu = max_t(int, ENIC_MIN_MTU,
449 min_t(int, ENIC_MAX_MTU, mtu));
450 if (mtu != netdev->mtu)
451 schedule_work(&enic->change_mtu_work);
452 } else {
453 if (mtu < netdev->mtu)
454 netdev_warn(netdev,
455 "interface MTU (%d) set higher "
456 "than switch port MTU (%d)\n",
457 netdev->mtu, mtu);
458 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700459 }
460}
461
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000462static void enic_link_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700463{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000464 int link_status = vnic_dev_link_status(enic->vdev);
465 int carrier_ok = netif_carrier_ok(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700466
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000467 if (link_status && !carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000468 netdev_info(enic->netdev, "Link UP\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000469 netif_carrier_on(enic->netdev);
470 } else if (!link_status && carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000471 netdev_info(enic->netdev, "Link DOWN\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000472 netif_carrier_off(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700473 }
474}
475
476static void enic_notify_check(struct enic *enic)
477{
478 enic_msglvl_check(enic);
479 enic_mtu_check(enic);
480 enic_link_check(enic);
481}
482
483#define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
484
485static irqreturn_t enic_isr_legacy(int irq, void *data)
486{
487 struct net_device *netdev = data;
488 struct enic *enic = netdev_priv(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000489 unsigned int io_intr = enic_legacy_io_intr();
490 unsigned int err_intr = enic_legacy_err_intr();
491 unsigned int notify_intr = enic_legacy_notify_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700492 u32 pba;
493
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000494 vnic_intr_mask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700495
496 pba = vnic_intr_legacy_pba(enic->legacy_pba);
497 if (!pba) {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000498 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700499 return IRQ_NONE; /* not our interrupt */
500 }
501
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000502 if (ENIC_TEST_INTR(pba, notify_intr)) {
503 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700504 enic_notify_check(enic);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800505 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700506
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000507 if (ENIC_TEST_INTR(pba, err_intr)) {
508 vnic_intr_return_all_credits(&enic->intr[err_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700509 enic_log_q_error(enic);
510 /* schedule recovery from WQ/RQ error */
511 schedule_work(&enic->reset);
512 return IRQ_HANDLED;
513 }
514
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000515 if (ENIC_TEST_INTR(pba, io_intr)) {
516 if (napi_schedule_prep(&enic->napi[0]))
517 __napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700518 } else {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000519 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700520 }
521
522 return IRQ_HANDLED;
523}
524
525static irqreturn_t enic_isr_msi(int irq, void *data)
526{
527 struct enic *enic = data;
528
529 /* With MSI, there is no sharing of interrupts, so this is
530 * our interrupt and there is no need to ack it. The device
531 * is not providing per-vector masking, so the OS will not
532 * write to PCI config space to mask/unmask the interrupt.
533 * We're using mask_on_assertion for MSI, so the device
534 * automatically masks the interrupt when the interrupt is
535 * generated. Later, when exiting polling, the interrupt
536 * will be unmasked (see enic_poll).
537 *
538 * Also, the device uses the same PCIe Traffic Class (TC)
539 * for Memory Write data and MSI, so there are no ordering
540 * issues; the MSI will always arrive at the Root Complex
541 * _after_ corresponding Memory Writes (i.e. descriptor
542 * writes).
543 */
544
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000545 napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700546
547 return IRQ_HANDLED;
548}
549
550static irqreturn_t enic_isr_msix_rq(int irq, void *data)
551{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000552 struct napi_struct *napi = data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700553
554 /* schedule NAPI polling for RQ cleanup */
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000555 napi_schedule(napi);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700556
557 return IRQ_HANDLED;
558}
559
560static irqreturn_t enic_isr_msix_wq(int irq, void *data)
561{
562 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000563 unsigned int cq = enic_cq_wq(enic, 0);
564 unsigned int intr = enic_msix_wq_intr(enic, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700565 unsigned int wq_work_to_do = -1; /* no limit */
566 unsigned int wq_work_done;
567
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000568 wq_work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700569 wq_work_to_do, enic_wq_service, NULL);
570
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000571 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700572 wq_work_done,
573 1 /* unmask intr */,
574 1 /* reset intr timer */);
575
576 return IRQ_HANDLED;
577}
578
579static irqreturn_t enic_isr_msix_err(int irq, void *data)
580{
581 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000582 unsigned int intr = enic_msix_err_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700583
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000584 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800585
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700586 enic_log_q_error(enic);
587
588 /* schedule recovery from WQ/RQ error */
589 schedule_work(&enic->reset);
590
591 return IRQ_HANDLED;
592}
593
594static irqreturn_t enic_isr_msix_notify(int irq, void *data)
595{
596 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000597 unsigned int intr = enic_msix_notify_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700598
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000599 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700600 enic_notify_check(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700601
602 return IRQ_HANDLED;
603}
604
605static inline void enic_queue_wq_skb_cont(struct enic *enic,
606 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000607 unsigned int len_left, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700608{
Eric Dumazet9e903e02011-10-18 21:00:24 +0000609 const skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700610
611 /* Queue additional data fragments */
612 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000613 len_left -= skb_frag_size(frag);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700614 enic_queue_wq_desc_cont(wq, skb,
Ian Campbell4bf5adb2011-08-29 23:18:27 +0000615 skb_frag_dma_map(&enic->pdev->dev,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000616 frag, 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100617 DMA_TO_DEVICE),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000618 skb_frag_size(frag),
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000619 (len_left == 0), /* EOP? */
620 loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700621 }
622}
623
624static inline void enic_queue_wq_skb_vlan(struct enic *enic,
625 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000626 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700627{
628 unsigned int head_len = skb_headlen(skb);
629 unsigned int len_left = skb->len - head_len;
630 int eop = (len_left == 0);
631
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000632 /* Queue the main skb fragment. The fragments are no larger
633 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
634 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
635 * per fragment is queued.
636 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700637 enic_queue_wq_desc(wq, skb,
638 pci_map_single(enic->pdev, skb->data,
639 head_len, PCI_DMA_TODEVICE),
640 head_len,
641 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000642 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700643
644 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000645 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700646}
647
648static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
649 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000650 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700651{
652 unsigned int head_len = skb_headlen(skb);
653 unsigned int len_left = skb->len - head_len;
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000654 unsigned int hdr_len = skb_checksum_start_offset(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700655 unsigned int csum_offset = hdr_len + skb->csum_offset;
656 int eop = (len_left == 0);
657
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000658 /* Queue the main skb fragment. The fragments are no larger
659 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
660 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
661 * per fragment is queued.
662 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700663 enic_queue_wq_desc_csum_l4(wq, skb,
664 pci_map_single(enic->pdev, skb->data,
665 head_len, PCI_DMA_TODEVICE),
666 head_len,
667 csum_offset,
668 hdr_len,
669 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000670 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700671
672 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000673 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700674}
675
676static inline void enic_queue_wq_skb_tso(struct enic *enic,
677 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000678 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700679{
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000680 unsigned int frag_len_left = skb_headlen(skb);
681 unsigned int len_left = skb->len - frag_len_left;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700682 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
683 int eop = (len_left == 0);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000684 unsigned int len;
685 dma_addr_t dma_addr;
686 unsigned int offset = 0;
687 skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700688
689 /* Preload TCP csum field with IP pseudo hdr calculated
690 * with IP length set to zero. HW will later add in length
691 * to each TCP segment resulting from the TSO.
692 */
693
Harvey Harrison09640e62009-02-01 00:45:17 -0800694 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700695 ip_hdr(skb)->check = 0;
696 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
697 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
Harvey Harrison09640e62009-02-01 00:45:17 -0800698 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700699 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
700 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
701 }
702
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000703 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
704 * for the main skb fragment
705 */
706 while (frag_len_left) {
707 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
708 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
709 len, PCI_DMA_TODEVICE);
710 enic_queue_wq_desc_tso(wq, skb,
711 dma_addr,
712 len,
713 mss, hdr_len,
714 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000715 eop && (len == frag_len_left), loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000716 frag_len_left -= len;
717 offset += len;
718 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700719
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000720 if (eop)
721 return;
722
723 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
724 * for additional data fragments
725 */
726 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000727 len_left -= skb_frag_size(frag);
728 frag_len_left = skb_frag_size(frag);
Ian Campbell4bf5adb2011-08-29 23:18:27 +0000729 offset = 0;
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000730
731 while (frag_len_left) {
732 len = min(frag_len_left,
733 (unsigned int)WQ_ENET_MAX_DESC_LEN);
Ian Campbell4bf5adb2011-08-29 23:18:27 +0000734 dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
735 offset, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100736 DMA_TO_DEVICE);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000737 enic_queue_wq_desc_cont(wq, skb,
738 dma_addr,
739 len,
740 (len_left == 0) &&
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000741 (len == frag_len_left), /* EOP? */
742 loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000743 frag_len_left -= len;
744 offset += len;
745 }
746 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700747}
748
749static inline void enic_queue_wq_skb(struct enic *enic,
750 struct vnic_wq *wq, struct sk_buff *skb)
751{
752 unsigned int mss = skb_shinfo(skb)->gso_size;
753 unsigned int vlan_tag = 0;
754 int vlan_tag_insert = 0;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000755 int loopback = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700756
Jesse Grosseab6d182010-10-20 13:56:03 +0000757 if (vlan_tx_tag_present(skb)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700758 /* VLAN tag from trunking driver */
759 vlan_tag_insert = 1;
760 vlan_tag = vlan_tx_tag_get(skb);
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000761 } else if (enic->loop_enable) {
762 vlan_tag = enic->loop_tag;
763 loopback = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700764 }
765
766 if (mss)
767 enic_queue_wq_skb_tso(enic, wq, skb, mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000768 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700769 else if (skb->ip_summed == CHECKSUM_PARTIAL)
770 enic_queue_wq_skb_csum_l4(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000771 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700772 else
773 enic_queue_wq_skb_vlan(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000774 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700775}
776
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800777/* netif_tx_lock held, process context with BHs disabled, or BH */
Stephen Hemminger613573252009-08-31 19:50:58 +0000778static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
Scott Feldmand87fd252009-12-23 13:27:59 +0000779 struct net_device *netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700780{
781 struct enic *enic = netdev_priv(netdev);
782 struct vnic_wq *wq = &enic->wq[0];
783 unsigned long flags;
784
785 if (skb->len <= 0) {
786 dev_kfree_skb(skb);
787 return NETDEV_TX_OK;
788 }
789
790 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
791 * which is very likely. In the off chance it's going to take
792 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
793 */
794
795 if (skb_shinfo(skb)->gso_size == 0 &&
796 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
797 skb_linearize(skb)) {
798 dev_kfree_skb(skb);
799 return NETDEV_TX_OK;
800 }
801
802 spin_lock_irqsave(&enic->wq_lock[0], flags);
803
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000804 if (vnic_wq_desc_avail(wq) <
805 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700806 netif_stop_queue(netdev);
807 /* This is a hard error, log it */
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000808 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700809 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
810 return NETDEV_TX_BUSY;
811 }
812
813 enic_queue_wq_skb(enic, wq, skb);
814
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000815 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700816 netif_stop_queue(netdev);
817
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700818 spin_unlock_irqrestore(&enic->wq_lock[0], flags);
819
820 return NETDEV_TX_OK;
821}
822
823/* dev_base_lock rwlock held, nominally process context */
stephen hemmingerf20530b2011-06-08 14:54:02 +0000824static struct rtnl_link_stats64 *enic_get_stats(struct net_device *netdev,
825 struct rtnl_link_stats64 *net_stats)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700826{
827 struct enic *enic = netdev_priv(netdev);
828 struct vnic_stats *stats;
829
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000830 enic_dev_stats_dump(enic, &stats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700831
Scott Feldman25f0a062008-09-24 11:23:32 -0700832 net_stats->tx_packets = stats->tx.tx_frames_ok;
833 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
834 net_stats->tx_errors = stats->tx.tx_errors;
835 net_stats->tx_dropped = stats->tx.tx_drops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700836
Scott Feldman25f0a062008-09-24 11:23:32 -0700837 net_stats->rx_packets = stats->rx.rx_frames_ok;
838 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
839 net_stats->rx_errors = stats->rx.rx_errors;
840 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
Scott Feldman350991e2009-09-03 17:02:19 +0000841 net_stats->rx_over_errors = enic->rq_truncated_pkts;
Scott Feldmanbd9fb1a2009-02-09 23:24:08 -0800842 net_stats->rx_crc_errors = enic->rq_bad_fcs;
Scott Feldman350991e2009-09-03 17:02:19 +0000843 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700844
Scott Feldman25f0a062008-09-24 11:23:32 -0700845 return net_stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700846}
847
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000848void enic_reset_addr_lists(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700849{
850 enic->mc_count = 0;
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000851 enic->uc_count = 0;
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000852 enic->flags = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700853}
854
855static int enic_set_mac_addr(struct net_device *netdev, char *addr)
856{
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700857 struct enic *enic = netdev_priv(netdev);
858
Roopa Prabhu73359032012-01-18 04:24:02 +0000859 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700860 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
861 return -EADDRNOTAVAIL;
862 } else {
863 if (!is_valid_ether_addr(addr))
864 return -EADDRNOTAVAIL;
865 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700866
867 memcpy(netdev->dev_addr, addr, netdev->addr_len);
Danny Kukawkada194312012-02-18 01:50:43 -0500868 netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700869
870 return 0;
871}
872
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700873static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
874{
875 struct enic *enic = netdev_priv(netdev);
876 struct sockaddr *saddr = p;
877 char *addr = saddr->sa_data;
878 int err;
879
880 if (netif_running(enic->netdev)) {
881 err = enic_dev_del_station_addr(enic);
882 if (err)
883 return err;
884 }
885
886 err = enic_set_mac_addr(netdev, addr);
887 if (err)
888 return err;
889
890 if (netif_running(enic->netdev)) {
891 err = enic_dev_add_station_addr(enic);
892 if (err)
893 return err;
894 }
895
896 return err;
897}
898
899static int enic_set_mac_address(struct net_device *netdev, void *p)
900{
Roopa Prabhu294dab22010-08-10 18:54:55 +0000901 struct sockaddr *saddr = p;
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000902 char *addr = saddr->sa_data;
903 struct enic *enic = netdev_priv(netdev);
904 int err;
Roopa Prabhu294dab22010-08-10 18:54:55 +0000905
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000906 err = enic_dev_del_station_addr(enic);
907 if (err)
908 return err;
909
910 err = enic_set_mac_addr(netdev, addr);
911 if (err)
912 return err;
913
914 return enic_dev_add_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700915}
916
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000917static void enic_update_multicast_addr_list(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700918{
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000919 struct net_device *netdev = enic->netdev;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000920 struct netdev_hw_addr *ha;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000921 unsigned int mc_count = netdev_mc_count(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700922 u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700923 unsigned int i, j;
924
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000925 if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
926 netdev_warn(netdev, "Registering only %d out of %d "
927 "multicast addresses\n",
928 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700929 mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
Scott Feldman9959a182009-12-23 13:27:43 +0000930 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700931
932 /* Is there an easier way? Trying to minimize to
933 * calls to add/del multicast addrs. We keep the
934 * addrs from the last call in enic->mc_addr and
935 * look for changes to add/del.
936 */
937
Jiri Pirko48e2f182010-02-22 09:22:26 +0000938 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000939 netdev_for_each_mc_addr(ha, netdev) {
Jiri Pirko48e2f182010-02-22 09:22:26 +0000940 if (i == mc_count)
941 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000942 memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700943 }
944
945 for (i = 0; i < enic->mc_count; i++) {
946 for (j = 0; j < mc_count; j++)
Joe Perches2e42e472012-05-09 17:17:46 +0000947 if (ether_addr_equal(enic->mc_addr[i], mc_addr[j]))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700948 break;
949 if (j == mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000950 enic_dev_del_addr(enic, enic->mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700951 }
952
953 for (i = 0; i < mc_count; i++) {
954 for (j = 0; j < enic->mc_count; j++)
Joe Perches2e42e472012-05-09 17:17:46 +0000955 if (ether_addr_equal(mc_addr[i], enic->mc_addr[j]))
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700956 break;
957 if (j == enic->mc_count)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000958 enic_dev_add_addr(enic, mc_addr[i]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700959 }
960
961 /* Save the list to compare against next time
962 */
963
964 for (i = 0; i < mc_count; i++)
965 memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
966
967 enic->mc_count = mc_count;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700968}
969
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000970static void enic_update_unicast_addr_list(struct enic *enic)
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000971{
972 struct net_device *netdev = enic->netdev;
973 struct netdev_hw_addr *ha;
974 unsigned int uc_count = netdev_uc_count(netdev);
975 u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
976 unsigned int i, j;
977
978 if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
979 netdev_warn(netdev, "Registering only %d out of %d "
980 "unicast addresses\n",
981 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
982 uc_count = ENIC_UNICAST_PERFECT_FILTERS;
983 }
984
985 /* Is there an easier way? Trying to minimize to
986 * calls to add/del unicast addrs. We keep the
987 * addrs from the last call in enic->uc_addr and
988 * look for changes to add/del.
989 */
990
991 i = 0;
992 netdev_for_each_uc_addr(ha, netdev) {
993 if (i == uc_count)
994 break;
995 memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
996 }
997
998 for (i = 0; i < enic->uc_count; i++) {
999 for (j = 0; j < uc_count; j++)
Joe Perches2e42e472012-05-09 17:17:46 +00001000 if (ether_addr_equal(enic->uc_addr[i], uc_addr[j]))
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001001 break;
1002 if (j == uc_count)
1003 enic_dev_del_addr(enic, enic->uc_addr[i]);
1004 }
1005
1006 for (i = 0; i < uc_count; i++) {
1007 for (j = 0; j < enic->uc_count; j++)
Joe Perches2e42e472012-05-09 17:17:46 +00001008 if (ether_addr_equal(uc_addr[i], enic->uc_addr[j]))
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001009 break;
1010 if (j == enic->uc_count)
1011 enic_dev_add_addr(enic, uc_addr[i]);
1012 }
1013
1014 /* Save the list to compare against next time
1015 */
1016
1017 for (i = 0; i < uc_count; i++)
1018 memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1019
1020 enic->uc_count = uc_count;
1021}
1022
1023/* netif_tx_lock held, BHs disabled */
1024static void enic_set_rx_mode(struct net_device *netdev)
1025{
1026 struct enic *enic = netdev_priv(netdev);
1027 int directed = 1;
1028 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1029 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1030 int promisc = (netdev->flags & IFF_PROMISC) ||
1031 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1032 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1033 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1034 unsigned int flags = netdev->flags |
1035 (allmulti ? IFF_ALLMULTI : 0) |
1036 (promisc ? IFF_PROMISC : 0);
1037
1038 if (enic->flags != flags) {
1039 enic->flags = flags;
1040 enic_dev_packet_filter(enic, directed,
1041 multicast, broadcast, promisc, allmulti);
1042 }
1043
1044 if (!promisc) {
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001045 enic_update_unicast_addr_list(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001046 if (!allmulti)
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001047 enic_update_multicast_addr_list(enic);
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001048 }
1049}
1050
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001051/* netif_tx_lock held, BHs disabled */
1052static void enic_tx_timeout(struct net_device *netdev)
1053{
1054 struct enic *enic = netdev_priv(netdev);
1055 schedule_work(&enic->reset);
1056}
1057
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001058static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1059{
1060 struct enic *enic = netdev_priv(netdev);
Roopa Prabhu3f192792011-09-22 03:44:43 +00001061 struct enic_port_profile *pp;
1062 int err;
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001063
Roopa Prabhu3f192792011-09-22 03:44:43 +00001064 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1065 if (err)
1066 return err;
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001067
Roopa Prabhub8622cb2012-03-07 03:50:44 +00001068 if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
Roopa Prabhub4765832012-02-20 00:11:58 +00001069 if (vf == PORT_SELF_VF) {
1070 memcpy(pp->vf_mac, mac, ETH_ALEN);
1071 return 0;
1072 } else {
1073 /*
1074 * For sriov vf's set the mac in hw
1075 */
1076 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
1077 vnic_dev_set_mac_addr, mac);
1078 return enic_dev_status_to_errno(err);
1079 }
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00001080 } else
1081 return -EINVAL;
1082}
1083
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001084static int enic_set_vf_port(struct net_device *netdev, int vf,
1085 struct nlattr *port[])
1086{
1087 struct enic *enic = netdev_priv(netdev);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001088 struct enic_port_profile prev_pp;
Roopa Prabhu3f192792011-09-22 03:44:43 +00001089 struct enic_port_profile *pp;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001090 int err = 0, restore_pp = 1;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001091
Roopa Prabhu3f192792011-09-22 03:44:43 +00001092 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1093 if (err)
1094 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001095
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001096 if (!port[IFLA_PORT_REQUEST])
Scott Feldman08f382e2010-06-01 08:59:33 +00001097 return -EOPNOTSUPP;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001098
Roopa Prabhu3f192792011-09-22 03:44:43 +00001099 memcpy(&prev_pp, pp, sizeof(*enic->pp));
1100 memset(pp, 0, sizeof(*enic->pp));
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001101
Roopa Prabhu3f192792011-09-22 03:44:43 +00001102 pp->set |= ENIC_SET_REQUEST;
1103 pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001104
1105 if (port[IFLA_PORT_PROFILE]) {
Roopa Prabhu3f192792011-09-22 03:44:43 +00001106 pp->set |= ENIC_SET_NAME;
1107 memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001108 PORT_PROFILE_MAX);
1109 }
1110
1111 if (port[IFLA_PORT_INSTANCE_UUID]) {
Roopa Prabhu3f192792011-09-22 03:44:43 +00001112 pp->set |= ENIC_SET_INSTANCE;
1113 memcpy(pp->instance_uuid,
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001114 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1115 }
1116
1117 if (port[IFLA_PORT_HOST_UUID]) {
Roopa Prabhu3f192792011-09-22 03:44:43 +00001118 pp->set |= ENIC_SET_HOST;
1119 memcpy(pp->host_uuid,
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001120 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1121 }
1122
Roopa Prabhub4765832012-02-20 00:11:58 +00001123 if (vf == PORT_SELF_VF) {
1124 /* Special case handling: mac came from IFLA_VF_MAC */
1125 if (!is_zero_ether_addr(prev_pp.vf_mac))
1126 memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
Scott Feldman418c4372010-05-22 17:29:58 +00001127
Roopa Prabhub4765832012-02-20 00:11:58 +00001128 if (is_zero_ether_addr(netdev->dev_addr))
1129 eth_hw_addr_random(netdev);
1130 } else {
1131 /* SR-IOV VF: get mac from adapter */
1132 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
1133 vnic_dev_get_mac_addr, pp->mac_addr);
1134 if (err) {
1135 netdev_err(netdev, "Error getting mac for vf %d\n", vf);
1136 memcpy(pp, &prev_pp, sizeof(*pp));
1137 return enic_dev_status_to_errno(err);
1138 }
1139 }
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001140
Roopa Prabhu3f192792011-09-22 03:44:43 +00001141 err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001142 if (err) {
1143 if (restore_pp) {
1144 /* Things are still the way they were: Implicit
1145 * DISASSOCIATE failed
1146 */
Roopa Prabhu3f192792011-09-22 03:44:43 +00001147 memcpy(pp, &prev_pp, sizeof(*pp));
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001148 } else {
Roopa Prabhu3f192792011-09-22 03:44:43 +00001149 memset(pp, 0, sizeof(*pp));
1150 if (vf == PORT_SELF_VF)
1151 memset(netdev->dev_addr, 0, ETH_ALEN);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001152 }
1153 } else {
1154 /* Set flag to indicate that the port assoc/disassoc
1155 * request has been sent out to fw
1156 */
Roopa Prabhu3f192792011-09-22 03:44:43 +00001157 pp->set |= ENIC_PORT_REQUEST_APPLIED;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001158
1159 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
Roopa Prabhu3f192792011-09-22 03:44:43 +00001160 if (pp->request == PORT_REQUEST_DISASSOCIATE) {
1161 memset(pp->mac_addr, 0, ETH_ALEN);
1162 if (vf == PORT_SELF_VF)
1163 memset(netdev->dev_addr, 0, ETH_ALEN);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001164 }
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001165 }
1166
Roopa Prabhub4765832012-02-20 00:11:58 +00001167 if (vf == PORT_SELF_VF)
1168 memset(pp->vf_mac, 0, ETH_ALEN);
Roopa Prabhu296390592010-12-08 13:54:03 +00001169
Roopa Prabhu296390592010-12-08 13:54:03 +00001170 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001171}
1172
1173static int enic_get_vf_port(struct net_device *netdev, int vf,
1174 struct sk_buff *skb)
1175{
1176 struct enic *enic = netdev_priv(netdev);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001177 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
Roopa Prabhu3f192792011-09-22 03:44:43 +00001178 struct enic_port_profile *pp;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001179 int err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001180
Roopa Prabhu3f192792011-09-22 03:44:43 +00001181 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001182 if (err)
Roopa Prabhub3abfbd2011-03-29 20:36:07 +00001183 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001184
Roopa Prabhu3f192792011-09-22 03:44:43 +00001185 if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
1186 return -ENODATA;
1187
1188 err = enic_process_get_pp_request(enic, vf, pp->request, &response);
1189 if (err)
1190 return err;
1191
David S. Miller1a106de2012-04-01 20:22:22 -04001192 if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
1193 nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
1194 ((pp->set & ENIC_SET_NAME) &&
1195 nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
1196 ((pp->set & ENIC_SET_INSTANCE) &&
1197 nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1198 pp->instance_uuid)) ||
1199 ((pp->set & ENIC_SET_HOST) &&
1200 nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
1201 goto nla_put_failure;
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001202 return 0;
1203
1204nla_put_failure:
1205 return -EMSGSIZE;
1206}
1207
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001208static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1209{
1210 struct enic *enic = vnic_dev_priv(rq->vdev);
1211
1212 if (!buf->os_buf)
1213 return;
1214
1215 pci_unmap_single(enic->pdev, buf->dma_addr,
1216 buf->len, PCI_DMA_FROMDEVICE);
1217 dev_kfree_skb_any(buf->os_buf);
1218}
1219
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001220static int enic_rq_alloc_buf(struct vnic_rq *rq)
1221{
1222 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldmand19e22d2009-09-03 17:02:08 +00001223 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001224 struct sk_buff *skb;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00001225 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001226 unsigned int os_buf_index = 0;
1227 dma_addr_t dma_addr;
1228
Eric Dumazet89d71a62009-10-13 05:34:20 +00001229 skb = netdev_alloc_skb_ip_align(netdev, len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001230 if (!skb)
1231 return -ENOMEM;
1232
1233 dma_addr = pci_map_single(enic->pdev, skb->data,
1234 len, PCI_DMA_FROMDEVICE);
1235
1236 enic_queue_rq_desc(rq, skb, os_buf_index,
1237 dma_addr, len);
1238
1239 return 0;
1240}
1241
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001242static void enic_rq_indicate_buf(struct vnic_rq *rq,
1243 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1244 int skipped, void *opaque)
1245{
1246 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001247 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001248 struct sk_buff *skb;
1249
1250 u8 type, color, eop, sop, ingress_port, vlan_stripped;
1251 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1252 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1253 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1254 u8 packet_error;
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001255 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001256 u32 rss_hash;
1257
1258 if (skipped)
1259 return;
1260
1261 skb = buf->os_buf;
1262 prefetch(skb->data - NET_IP_ALIGN);
1263 pci_unmap_single(enic->pdev, buf->dma_addr,
1264 buf->len, PCI_DMA_FROMDEVICE);
1265
1266 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1267 &type, &color, &q_number, &completed_index,
1268 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1269 &csum_not_calc, &rss_hash, &bytes_written,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001270 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001271 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1272 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1273 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1274 &fcs_ok);
1275
1276 if (packet_error) {
1277
Scott Feldman350991e2009-09-03 17:02:19 +00001278 if (!fcs_ok) {
1279 if (bytes_written > 0)
1280 enic->rq_bad_fcs++;
1281 else if (bytes_written == 0)
1282 enic->rq_truncated_pkts++;
1283 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001284
1285 dev_kfree_skb_any(skb);
1286
1287 return;
1288 }
1289
1290 if (eop && bytes_written > 0) {
1291
1292 /* Good receive
1293 */
1294
1295 skb_put(skb, bytes_written);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001296 skb->protocol = eth_type_trans(skb, netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001297
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00001298 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001299 skb->csum = htons(checksum);
1300 skb->ip_summed = CHECKSUM_COMPLETE;
1301 }
1302
Jiri Pirko6ede7462011-07-20 04:54:18 +00001303 if (vlan_stripped)
1304 __vlan_hwaccel_put_tag(skb, vlan_tci);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001305
Jiri Pirko6ede7462011-07-20 04:54:18 +00001306 if (netdev->features & NETIF_F_GRO)
1307 napi_gro_receive(&enic->napi[q_number], skb);
1308 else
1309 netif_receive_skb(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001310 } else {
1311
1312 /* Buffer overflow
1313 */
1314
1315 dev_kfree_skb_any(skb);
1316 }
1317}
1318
1319static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1320 u8 type, u16 q_number, u16 completed_index, void *opaque)
1321{
1322 struct enic *enic = vnic_dev_priv(vdev);
1323
1324 vnic_rq_service(&enic->rq[q_number], cq_desc,
1325 completed_index, VNIC_RQ_RETURN_DESC,
1326 enic_rq_indicate_buf, opaque);
1327
1328 return 0;
1329}
1330
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001331static int enic_poll(struct napi_struct *napi, int budget)
1332{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001333 struct net_device *netdev = napi->dev;
1334 struct enic *enic = netdev_priv(netdev);
1335 unsigned int cq_rq = enic_cq_rq(enic, 0);
1336 unsigned int cq_wq = enic_cq_wq(enic, 0);
1337 unsigned int intr = enic_legacy_io_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001338 unsigned int rq_work_to_do = budget;
1339 unsigned int wq_work_to_do = -1; /* no limit */
1340 unsigned int work_done, rq_work_done, wq_work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001341 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001342
1343 /* Service RQ (first) and WQ
1344 */
1345
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001346 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001347 rq_work_to_do, enic_rq_service, NULL);
1348
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001349 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001350 wq_work_to_do, enic_wq_service, NULL);
1351
1352 /* Accumulate intr event credits for this polling
1353 * cycle. An intr event is the completion of a
1354 * a WQ or RQ packet.
1355 */
1356
1357 work_done = rq_work_done + wq_work_done;
1358
1359 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001360 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001361 work_done,
1362 0 /* don't unmask intr */,
1363 0 /* don't reset intr timer */);
1364
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001365 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001366
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001367 /* Buffer allocation failed. Stay in polling
1368 * mode so we can try to fill the ring again.
1369 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001370
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001371 if (err)
1372 rq_work_done = rq_work_to_do;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001373
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001374 if (rq_work_done < rq_work_to_do) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001375
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001376 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001377 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001378 */
1379
Ben Hutchings288379f2009-01-19 16:43:59 -08001380 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001381 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001382 }
1383
1384 return rq_work_done;
1385}
1386
1387static int enic_poll_msix(struct napi_struct *napi, int budget)
1388{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001389 struct net_device *netdev = napi->dev;
1390 struct enic *enic = netdev_priv(netdev);
1391 unsigned int rq = (napi - &enic->napi[0]);
1392 unsigned int cq = enic_cq_rq(enic, rq);
1393 unsigned int intr = enic_msix_rq_intr(enic, rq);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001394 unsigned int work_to_do = budget;
1395 unsigned int work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001396 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001397
1398 /* Service RQ
1399 */
1400
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001401 work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001402 work_to_do, enic_rq_service, NULL);
1403
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001404 /* Return intr event credits for this polling
1405 * cycle. An intr event is the completion of a
1406 * RQ packet.
1407 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001408
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001409 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001410 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001411 work_done,
1412 0 /* don't unmask intr */,
1413 0 /* don't reset intr timer */);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001414
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001415 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001416
1417 /* Buffer allocation failed. Stay in polling mode
1418 * so we can try to fill the ring again.
1419 */
1420
1421 if (err)
1422 work_done = work_to_do;
1423
1424 if (work_done < work_to_do) {
1425
1426 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001427 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001428 */
1429
Ben Hutchings288379f2009-01-19 16:43:59 -08001430 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001431 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001432 }
1433
1434 return work_done;
1435}
1436
1437static void enic_notify_timer(unsigned long data)
1438{
1439 struct enic *enic = (struct enic *)data;
1440
1441 enic_notify_check(enic);
1442
Scott Feldman25f0a062008-09-24 11:23:32 -07001443 mod_timer(&enic->notify_timer,
1444 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001445}
1446
1447static void enic_free_intr(struct enic *enic)
1448{
1449 struct net_device *netdev = enic->netdev;
1450 unsigned int i;
1451
1452 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1453 case VNIC_DEV_INTR_MODE_INTX:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001454 free_irq(enic->pdev->irq, netdev);
1455 break;
Scott Feldman8f4d2482008-09-24 11:23:42 -07001456 case VNIC_DEV_INTR_MODE_MSI:
1457 free_irq(enic->pdev->irq, enic);
1458 break;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001459 case VNIC_DEV_INTR_MODE_MSIX:
1460 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1461 if (enic->msix[i].requested)
1462 free_irq(enic->msix_entry[i].vector,
1463 enic->msix[i].devid);
1464 break;
1465 default:
1466 break;
1467 }
1468}
1469
1470static int enic_request_intr(struct enic *enic)
1471{
1472 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001473 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001474 int err = 0;
1475
1476 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1477
1478 case VNIC_DEV_INTR_MODE_INTX:
1479
1480 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1481 IRQF_SHARED, netdev->name, netdev);
1482 break;
1483
1484 case VNIC_DEV_INTR_MODE_MSI:
1485
1486 err = request_irq(enic->pdev->irq, enic_isr_msi,
1487 0, netdev->name, enic);
1488 break;
1489
1490 case VNIC_DEV_INTR_MODE_MSIX:
1491
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001492 for (i = 0; i < enic->rq_count; i++) {
1493 intr = enic_msix_rq_intr(enic, i);
1494 sprintf(enic->msix[intr].devname,
1495 "%.11s-rx-%d", netdev->name, i);
1496 enic->msix[intr].isr = enic_isr_msix_rq;
1497 enic->msix[intr].devid = &enic->napi[i];
1498 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001499
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001500 for (i = 0; i < enic->wq_count; i++) {
1501 intr = enic_msix_wq_intr(enic, i);
1502 sprintf(enic->msix[intr].devname,
1503 "%.11s-tx-%d", netdev->name, i);
1504 enic->msix[intr].isr = enic_isr_msix_wq;
1505 enic->msix[intr].devid = enic;
1506 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001507
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001508 intr = enic_msix_err_intr(enic);
1509 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001510 "%.11s-err", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001511 enic->msix[intr].isr = enic_isr_msix_err;
1512 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001513
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001514 intr = enic_msix_notify_intr(enic);
1515 sprintf(enic->msix[intr].devname,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001516 "%.11s-notify", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001517 enic->msix[intr].isr = enic_isr_msix_notify;
1518 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001519
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001520 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1521 enic->msix[i].requested = 0;
1522
1523 for (i = 0; i < enic->intr_count; i++) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001524 err = request_irq(enic->msix_entry[i].vector,
1525 enic->msix[i].isr, 0,
1526 enic->msix[i].devname,
1527 enic->msix[i].devid);
1528 if (err) {
1529 enic_free_intr(enic);
1530 break;
1531 }
1532 enic->msix[i].requested = 1;
1533 }
1534
1535 break;
1536
1537 default:
1538 break;
1539 }
1540
1541 return err;
1542}
1543
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001544static void enic_synchronize_irqs(struct enic *enic)
1545{
1546 unsigned int i;
1547
1548 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1549 case VNIC_DEV_INTR_MODE_INTX:
1550 case VNIC_DEV_INTR_MODE_MSI:
1551 synchronize_irq(enic->pdev->irq);
1552 break;
1553 case VNIC_DEV_INTR_MODE_MSIX:
1554 for (i = 0; i < enic->intr_count; i++)
1555 synchronize_irq(enic->msix_entry[i].vector);
1556 break;
1557 default:
1558 break;
1559 }
1560}
1561
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001562static int enic_dev_notify_set(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001563{
1564 int err;
1565
Scott Feldman56ac88b2009-09-03 17:02:14 +00001566 spin_lock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001567 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1568 case VNIC_DEV_INTR_MODE_INTX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001569 err = vnic_dev_notify_set(enic->vdev,
1570 enic_legacy_notify_intr());
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001571 break;
1572 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001573 err = vnic_dev_notify_set(enic->vdev,
1574 enic_msix_notify_intr(enic));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001575 break;
1576 default:
1577 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1578 break;
1579 }
Scott Feldman56ac88b2009-09-03 17:02:14 +00001580 spin_unlock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001581
1582 return err;
1583}
1584
1585static void enic_notify_timer_start(struct enic *enic)
1586{
1587 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1588 case VNIC_DEV_INTR_MODE_MSI:
1589 mod_timer(&enic->notify_timer, jiffies);
1590 break;
1591 default:
1592 /* Using intr for notification for INTx/MSI-X */
1593 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001594 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001595}
1596
1597/* rtnl lock is held, process context */
1598static int enic_open(struct net_device *netdev)
1599{
1600 struct enic *enic = netdev_priv(netdev);
1601 unsigned int i;
1602 int err;
1603
Scott Feldman4b75a442008-09-24 11:23:53 -07001604 err = enic_request_intr(enic);
1605 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001606 netdev_err(netdev, "Unable to request irq.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001607 return err;
1608 }
1609
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001610 err = enic_dev_notify_set(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001611 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001612 netdev_err(netdev,
1613 "Failed to alloc notify buffer, aborting.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001614 goto err_out_free_intr;
1615 }
1616
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001617 for (i = 0; i < enic->rq_count; i++) {
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001618 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001619 /* Need at least one buffer on ring to get going */
1620 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001621 netdev_err(netdev, "Unable to alloc receive buffers\n");
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001622 err = -ENOMEM;
Scott Feldman4b75a442008-09-24 11:23:53 -07001623 goto err_out_notify_unset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001624 }
1625 }
1626
1627 for (i = 0; i < enic->wq_count; i++)
1628 vnic_wq_enable(&enic->wq[i]);
1629 for (i = 0; i < enic->rq_count; i++)
1630 vnic_rq_enable(&enic->rq[i]);
1631
Roopa Prabhu73359032012-01-18 04:24:02 +00001632 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
Roopa Prabhu296390592010-12-08 13:54:03 +00001633 enic_dev_add_station_addr(enic);
Roopa Prabhu3f192792011-09-22 03:44:43 +00001634
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001635 enic_set_rx_mode(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001636
1637 netif_wake_queue(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001638
1639 for (i = 0; i < enic->rq_count; i++)
1640 napi_enable(&enic->napi[i]);
1641
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001642 enic_dev_enable(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001643
1644 for (i = 0; i < enic->intr_count; i++)
1645 vnic_intr_unmask(&enic->intr[i]);
1646
1647 enic_notify_timer_start(enic);
1648
1649 return 0;
Scott Feldman4b75a442008-09-24 11:23:53 -07001650
1651err_out_notify_unset:
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001652 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001653err_out_free_intr:
1654 enic_free_intr(enic);
1655
1656 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001657}
1658
1659/* rtnl lock is held, process context */
1660static int enic_stop(struct net_device *netdev)
1661{
1662 struct enic *enic = netdev_priv(netdev);
1663 unsigned int i;
1664 int err;
1665
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001666 for (i = 0; i < enic->intr_count; i++) {
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001667 vnic_intr_mask(&enic->intr[i]);
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001668 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1669 }
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001670
1671 enic_synchronize_irqs(enic);
1672
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001673 del_timer_sync(&enic->notify_timer);
1674
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001675 enic_dev_disable(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001676
1677 for (i = 0; i < enic->rq_count; i++)
1678 napi_disable(&enic->napi[i]);
1679
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001680 netif_carrier_off(netdev);
1681 netif_tx_disable(netdev);
Roopa Prabhu3f192792011-09-22 03:44:43 +00001682
Roopa Prabhu73359032012-01-18 04:24:02 +00001683 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
Roopa Prabhu296390592010-12-08 13:54:03 +00001684 enic_dev_del_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001685
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001686 for (i = 0; i < enic->wq_count; i++) {
1687 err = vnic_wq_disable(&enic->wq[i]);
1688 if (err)
1689 return err;
1690 }
1691 for (i = 0; i < enic->rq_count; i++) {
1692 err = vnic_rq_disable(&enic->rq[i]);
1693 if (err)
1694 return err;
1695 }
1696
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001697 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001698 enic_free_intr(enic);
1699
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001700 for (i = 0; i < enic->wq_count; i++)
1701 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1702 for (i = 0; i < enic->rq_count; i++)
1703 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1704 for (i = 0; i < enic->cq_count; i++)
1705 vnic_cq_clean(&enic->cq[i]);
1706 for (i = 0; i < enic->intr_count; i++)
1707 vnic_intr_clean(&enic->intr[i]);
1708
1709 return 0;
1710}
1711
1712static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1713{
1714 struct enic *enic = netdev_priv(netdev);
1715 int running = netif_running(netdev);
1716
Scott Feldman25f0a062008-09-24 11:23:32 -07001717 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1718 return -EINVAL;
1719
Roopa Prabhu73359032012-01-18 04:24:02 +00001720 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001721 return -EOPNOTSUPP;
1722
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001723 if (running)
1724 enic_stop(netdev);
1725
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001726 netdev->mtu = new_mtu;
1727
1728 if (netdev->mtu > enic->port_mtu)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001729 netdev_warn(netdev,
1730 "interface MTU (%d) set higher than port MTU (%d)\n",
1731 netdev->mtu, enic->port_mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001732
1733 if (running)
1734 enic_open(netdev);
1735
1736 return 0;
1737}
1738
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001739static void enic_change_mtu_work(struct work_struct *work)
1740{
1741 struct enic *enic = container_of(work, struct enic, change_mtu_work);
1742 struct net_device *netdev = enic->netdev;
1743 int new_mtu = vnic_dev_mtu(enic->vdev);
1744 int err;
1745 unsigned int i;
1746
1747 new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1748
1749 rtnl_lock();
1750
1751 /* Stop RQ */
1752 del_timer_sync(&enic->notify_timer);
1753
1754 for (i = 0; i < enic->rq_count; i++)
1755 napi_disable(&enic->napi[i]);
1756
1757 vnic_intr_mask(&enic->intr[0]);
1758 enic_synchronize_irqs(enic);
1759 err = vnic_rq_disable(&enic->rq[0]);
1760 if (err) {
1761 netdev_err(netdev, "Unable to disable RQ.\n");
1762 return;
1763 }
1764 vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1765 vnic_cq_clean(&enic->cq[0]);
1766 vnic_intr_clean(&enic->intr[0]);
1767
1768 /* Fill RQ with new_mtu-sized buffers */
1769 netdev->mtu = new_mtu;
1770 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1771 /* Need at least one buffer on ring to get going */
1772 if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1773 netdev_err(netdev, "Unable to alloc receive buffers.\n");
1774 return;
1775 }
1776
1777 /* Start RQ */
1778 vnic_rq_enable(&enic->rq[0]);
1779 napi_enable(&enic->napi[0]);
1780 vnic_intr_unmask(&enic->intr[0]);
1781 enic_notify_timer_start(enic);
1782
1783 rtnl_unlock();
1784
1785 netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1786}
1787
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001788#ifdef CONFIG_NET_POLL_CONTROLLER
1789static void enic_poll_controller(struct net_device *netdev)
1790{
1791 struct enic *enic = netdev_priv(netdev);
1792 struct vnic_dev *vdev = enic->vdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001793 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001794
1795 switch (vnic_dev_get_intr_mode(vdev)) {
1796 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001797 for (i = 0; i < enic->rq_count; i++) {
1798 intr = enic_msix_rq_intr(enic, i);
Vasanthy Kolluri79aeec52010-12-08 13:05:45 +00001799 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1800 &enic->napi[i]);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001801 }
Vasanthy Kollurib880a952011-06-09 10:37:07 +00001802
1803 for (i = 0; i < enic->wq_count; i++) {
1804 intr = enic_msix_wq_intr(enic, i);
1805 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1806 }
1807
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001808 break;
1809 case VNIC_DEV_INTR_MODE_MSI:
1810 enic_isr_msi(enic->pdev->irq, enic);
1811 break;
1812 case VNIC_DEV_INTR_MODE_INTX:
1813 enic_isr_legacy(enic->pdev->irq, netdev);
1814 break;
1815 default:
1816 break;
1817 }
1818}
1819#endif
1820
1821static int enic_dev_wait(struct vnic_dev *vdev,
1822 int (*start)(struct vnic_dev *, int),
1823 int (*finished)(struct vnic_dev *, int *),
1824 int arg)
1825{
1826 unsigned long time;
1827 int done;
1828 int err;
1829
1830 BUG_ON(in_interrupt());
1831
1832 err = start(vdev, arg);
1833 if (err)
1834 return err;
1835
1836 /* Wait for func to complete...2 seconds max
1837 */
1838
1839 time = jiffies + (HZ * 2);
1840 do {
1841
1842 err = finished(vdev, &done);
1843 if (err)
1844 return err;
1845
1846 if (done)
1847 return 0;
1848
1849 schedule_timeout_uninterruptible(HZ / 10);
1850
1851 } while (time_after(time, jiffies));
1852
1853 return -ETIMEDOUT;
1854}
1855
1856static int enic_dev_open(struct enic *enic)
1857{
1858 int err;
1859
1860 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1861 vnic_dev_open_done, 0);
1862 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001863 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1864 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001865
1866 return err;
1867}
1868
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001869static int enic_dev_hang_reset(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001870{
1871 int err;
1872
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001873 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1874 vnic_dev_hang_reset_done, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001875 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001876 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1877 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001878
1879 return err;
1880}
1881
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001882static int enic_set_rsskey(struct enic *enic)
Scott Feldman68f71702009-02-09 23:24:24 -08001883{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001884 dma_addr_t rss_key_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001885 union vnic_rss_key *rss_key_buf_va = NULL;
1886 union vnic_rss_key rss_key = {
1887 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1888 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1889 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1890 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1891 };
1892 int err;
1893
1894 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1895 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1896 if (!rss_key_buf_va)
1897 return -ENOMEM;
1898
1899 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1900
1901 spin_lock(&enic->devcmd_lock);
1902 err = enic_set_rss_key(enic,
1903 rss_key_buf_pa,
1904 sizeof(union vnic_rss_key));
1905 spin_unlock(&enic->devcmd_lock);
1906
1907 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1908 rss_key_buf_va, rss_key_buf_pa);
1909
1910 return err;
1911}
1912
1913static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1914{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001915 dma_addr_t rss_cpu_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001916 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1917 unsigned int i;
1918 int err;
1919
1920 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1921 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1922 if (!rss_cpu_buf_va)
1923 return -ENOMEM;
1924
1925 for (i = 0; i < (1 << rss_hash_bits); i++)
1926 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1927
1928 spin_lock(&enic->devcmd_lock);
1929 err = enic_set_rss_cpu(enic,
1930 rss_cpu_buf_pa,
1931 sizeof(union vnic_rss_cpu));
1932 spin_unlock(&enic->devcmd_lock);
1933
1934 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1935 rss_cpu_buf_va, rss_cpu_buf_pa);
1936
1937 return err;
1938}
1939
1940static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1941 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1942{
Scott Feldman68f71702009-02-09 23:24:24 -08001943 const u8 tso_ipid_split_en = 0;
1944 const u8 ig_vlan_strip_en = 1;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001945 int err;
Scott Feldman68f71702009-02-09 23:24:24 -08001946
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001947 /* Enable VLAN tag stripping.
1948 */
Scott Feldman68f71702009-02-09 23:24:24 -08001949
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001950 spin_lock(&enic->devcmd_lock);
1951 err = enic_set_nic_cfg(enic,
Scott Feldman68f71702009-02-09 23:24:24 -08001952 rss_default_cpu, rss_hash_type,
1953 rss_hash_bits, rss_base_cpu,
1954 rss_enable, tso_ipid_split_en,
1955 ig_vlan_strip_en);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001956 spin_unlock(&enic->devcmd_lock);
1957
1958 return err;
1959}
1960
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001961static int enic_set_rss_nic_cfg(struct enic *enic)
1962{
1963 struct device *dev = enic_get_dev(enic);
1964 const u8 rss_default_cpu = 0;
1965 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1966 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1967 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1968 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1969 const u8 rss_hash_bits = 7;
1970 const u8 rss_base_cpu = 0;
1971 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1972
1973 if (rss_enable) {
1974 if (!enic_set_rsskey(enic)) {
1975 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1976 rss_enable = 0;
1977 dev_warn(dev, "RSS disabled, "
1978 "Failed to set RSS cpu indirection table.");
1979 }
1980 } else {
1981 rss_enable = 0;
1982 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1983 }
1984 }
1985
1986 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1987 rss_hash_bits, rss_base_cpu, rss_enable);
1988}
1989
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001990static void enic_reset(struct work_struct *work)
1991{
1992 struct enic *enic = container_of(work, struct enic, reset);
1993
1994 if (!netif_running(enic->netdev))
1995 return;
1996
1997 rtnl_lock();
1998
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001999 enic_dev_hang_notify(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002000 enic_stop(enic->netdev);
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00002001 enic_dev_hang_reset(enic);
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00002002 enic_reset_addr_lists(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002003 enic_init_vnic_resources(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002004 enic_set_rss_nic_cfg(enic);
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00002005 enic_dev_set_ig_vlan_rewrite_mode(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002006 enic_open(enic->netdev);
2007
2008 rtnl_unlock();
2009}
2010
2011static int enic_set_intr_mode(struct enic *enic)
2012{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002013 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
Vasanthy Kolluri1cbb1a62011-02-17 13:57:19 +00002014 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002015 unsigned int i;
2016
2017 /* Set interrupt mode (INTx, MSI, MSI-X) depending
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002018 * on system capabilities.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002019 *
2020 * Try MSI-X first
2021 *
2022 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2023 * (the second to last INTR is used for WQ/RQ errors)
2024 * (the last INTR is used for notifications)
2025 */
2026
2027 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2028 for (i = 0; i < n + m + 2; i++)
2029 enic->msix_entry[i].entry = i;
2030
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002031 /* Use multiple RQs if RSS is enabled
2032 */
2033
2034 if (ENIC_SETTING(enic, RSS) &&
2035 enic->config.intr_mode < 1 &&
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002036 enic->rq_count >= n &&
2037 enic->wq_count >= m &&
2038 enic->cq_count >= n + m &&
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002039 enic->intr_count >= n + m + 2) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002040
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002041 if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002042
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002043 enic->rq_count = n;
2044 enic->wq_count = m;
2045 enic->cq_count = n + m;
2046 enic->intr_count = n + m + 2;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002047
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002048 vnic_dev_set_intr_mode(enic->vdev,
2049 VNIC_DEV_INTR_MODE_MSIX);
2050
2051 return 0;
2052 }
2053 }
2054
2055 if (enic->config.intr_mode < 1 &&
2056 enic->rq_count >= 1 &&
2057 enic->wq_count >= m &&
2058 enic->cq_count >= 1 + m &&
2059 enic->intr_count >= 1 + m + 2) {
2060 if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2061
2062 enic->rq_count = 1;
2063 enic->wq_count = m;
2064 enic->cq_count = 1 + m;
2065 enic->intr_count = 1 + m + 2;
2066
2067 vnic_dev_set_intr_mode(enic->vdev,
2068 VNIC_DEV_INTR_MODE_MSIX);
2069
2070 return 0;
2071 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002072 }
2073
2074 /* Next try MSI
2075 *
2076 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2077 */
2078
2079 if (enic->config.intr_mode < 2 &&
2080 enic->rq_count >= 1 &&
2081 enic->wq_count >= 1 &&
2082 enic->cq_count >= 2 &&
2083 enic->intr_count >= 1 &&
2084 !pci_enable_msi(enic->pdev)) {
2085
2086 enic->rq_count = 1;
2087 enic->wq_count = 1;
2088 enic->cq_count = 2;
2089 enic->intr_count = 1;
2090
2091 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2092
2093 return 0;
2094 }
2095
2096 /* Next try INTx
2097 *
2098 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2099 * (the first INTR is used for WQ/RQ)
2100 * (the second INTR is used for WQ/RQ errors)
2101 * (the last INTR is used for notifications)
2102 */
2103
2104 if (enic->config.intr_mode < 3 &&
2105 enic->rq_count >= 1 &&
2106 enic->wq_count >= 1 &&
2107 enic->cq_count >= 2 &&
2108 enic->intr_count >= 3) {
2109
2110 enic->rq_count = 1;
2111 enic->wq_count = 1;
2112 enic->cq_count = 2;
2113 enic->intr_count = 3;
2114
2115 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2116
2117 return 0;
2118 }
2119
2120 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2121
2122 return -EINVAL;
2123}
2124
2125static void enic_clear_intr_mode(struct enic *enic)
2126{
2127 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2128 case VNIC_DEV_INTR_MODE_MSIX:
2129 pci_disable_msix(enic->pdev);
2130 break;
2131 case VNIC_DEV_INTR_MODE_MSI:
2132 pci_disable_msi(enic->pdev);
2133 break;
2134 default:
2135 break;
2136 }
2137
2138 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2139}
2140
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002141static const struct net_device_ops enic_netdev_dynamic_ops = {
2142 .ndo_open = enic_open,
2143 .ndo_stop = enic_stop,
2144 .ndo_start_xmit = enic_hard_start_xmit,
stephen hemmingerf20530b2011-06-08 14:54:02 +00002145 .ndo_get_stats64 = enic_get_stats,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002146 .ndo_validate_addr = eth_validate_addr,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002147 .ndo_set_rx_mode = enic_set_rx_mode,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002148 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2149 .ndo_change_mtu = enic_change_mtu,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002150 .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 .ndo_set_vf_port = enic_set_vf_port,
2154 .ndo_get_vf_port = enic_get_vf_port,
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00002155 .ndo_set_vf_mac = enic_set_vf_mac,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002156#ifdef CONFIG_NET_POLL_CONTROLLER
2157 .ndo_poll_controller = enic_poll_controller,
2158#endif
2159};
2160
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002161static const struct net_device_ops enic_netdev_ops = {
2162 .ndo_open = enic_open,
2163 .ndo_stop = enic_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -08002164 .ndo_start_xmit = enic_hard_start_xmit,
stephen hemmingerf20530b2011-06-08 14:54:02 +00002165 .ndo_get_stats64 = enic_get_stats,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002166 .ndo_validate_addr = eth_validate_addr,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002167 .ndo_set_mac_address = enic_set_mac_address,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002168 .ndo_set_rx_mode = enic_set_rx_mode,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002169 .ndo_change_mtu = enic_change_mtu,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002170 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2171 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2172 .ndo_tx_timeout = enic_tx_timeout,
Roopa Prabhu3f192792011-09-22 03:44:43 +00002173 .ndo_set_vf_port = enic_set_vf_port,
2174 .ndo_get_vf_port = enic_get_vf_port,
2175 .ndo_set_vf_mac = enic_set_vf_mac,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002176#ifdef CONFIG_NET_POLL_CONTROLLER
2177 .ndo_poll_controller = enic_poll_controller,
2178#endif
2179};
2180
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002181static void enic_dev_deinit(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002182{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002183 unsigned int i;
2184
2185 for (i = 0; i < enic->rq_count; i++)
2186 netif_napi_del(&enic->napi[i]);
2187
Scott Feldman6fdfa972009-09-03 17:02:45 +00002188 enic_free_vnic_resources(enic);
2189 enic_clear_intr_mode(enic);
2190}
2191
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002192static int enic_dev_init(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002193{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002194 struct device *dev = enic_get_dev(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002195 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002196 unsigned int i;
Scott Feldman6fdfa972009-09-03 17:02:45 +00002197 int err;
2198
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +00002199 /* Get interrupt coalesce timer info */
2200 err = enic_dev_intr_coal_timer_info(enic);
2201 if (err) {
2202 dev_warn(dev, "Using default conversion factor for "
2203 "interrupt coalesce timer\n");
2204 vnic_dev_intr_coal_timer_info_default(enic->vdev);
2205 }
2206
Scott Feldman6fdfa972009-09-03 17:02:45 +00002207 /* Get vNIC configuration
2208 */
2209
2210 err = enic_get_vnic_config(enic);
2211 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002212 dev_err(dev, "Get vNIC configuration failed, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002213 return err;
2214 }
2215
2216 /* Get available resource counts
2217 */
2218
2219 enic_get_res_counts(enic);
2220
2221 /* Set interrupt mode based on resource counts and system
2222 * capabilities
2223 */
2224
2225 err = enic_set_intr_mode(enic);
2226 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002227 dev_err(dev, "Failed to set intr mode based on resource "
2228 "counts and system capabilities, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002229 return err;
2230 }
2231
2232 /* Allocate and configure vNIC resources
2233 */
2234
2235 err = enic_alloc_vnic_resources(enic);
2236 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002237 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002238 goto err_out_free_vnic_resources;
2239 }
2240
2241 enic_init_vnic_resources(enic);
2242
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002243 err = enic_set_rss_nic_cfg(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002244 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002245 dev_err(dev, "Failed to config nic, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002246 goto err_out_free_vnic_resources;
2247 }
2248
2249 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2250 default:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002251 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002252 break;
2253 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002254 for (i = 0; i < enic->rq_count; i++)
2255 netif_napi_add(netdev, &enic->napi[i],
2256 enic_poll_msix, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002257 break;
2258 }
2259
2260 return 0;
2261
2262err_out_free_vnic_resources:
2263 enic_clear_intr_mode(enic);
2264 enic_free_vnic_resources(enic);
2265
2266 return err;
2267}
2268
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002269static void enic_iounmap(struct enic *enic)
2270{
2271 unsigned int i;
2272
2273 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2274 if (enic->bar[i].vaddr)
2275 iounmap(enic->bar[i].vaddr);
2276}
2277
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002278static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002279{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002280 struct device *dev = &pdev->dev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002281 struct net_device *netdev;
2282 struct enic *enic;
2283 int using_dac = 0;
2284 unsigned int i;
2285 int err;
Roopa Prabhu8749b422011-09-22 03:44:33 +00002286#ifdef CONFIG_PCI_IOV
2287 int pos = 0;
2288#endif
Roopa Prabhub67f2312012-01-19 22:25:36 +00002289 int num_pps = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002290
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002291 /* Allocate net device structure and initialize. Private
2292 * instance data is initialized to zero.
2293 */
2294
2295 netdev = alloc_etherdev(sizeof(struct enic));
Joe Perches41de8d42012-01-29 13:47:52 +00002296 if (!netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002297 return -ENOMEM;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002298
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002299 pci_set_drvdata(pdev, netdev);
2300
2301 SET_NETDEV_DEV(netdev, &pdev->dev);
2302
2303 enic = netdev_priv(netdev);
2304 enic->netdev = netdev;
2305 enic->pdev = pdev;
2306
2307 /* Setup PCI resources
2308 */
2309
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002310 err = pci_enable_device_mem(pdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002311 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002312 dev_err(dev, "Cannot enable PCI device, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002313 goto err_out_free_netdev;
2314 }
2315
2316 err = pci_request_regions(pdev, DRV_NAME);
2317 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002318 dev_err(dev, "Cannot request PCI regions, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002319 goto err_out_disable_device;
2320 }
2321
2322 pci_set_master(pdev);
2323
2324 /* Query PCI controller on system for DMA addressing
2325 * limitation for the device. Try 40-bit first, and
2326 * fail to 32-bit.
2327 */
2328
Yang Hongyang50cf1562009-04-06 19:01:14 -07002329 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002330 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07002331 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002332 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002333 dev_err(dev, "No usable DMA configuration, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002334 goto err_out_release_regions;
2335 }
Yang Hongyang284901a2009-04-06 19:01:15 -07002336 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002337 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002338 dev_err(dev, "Unable to obtain %u-bit DMA "
2339 "for consistent allocations, aborting\n", 32);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002340 goto err_out_release_regions;
2341 }
2342 } else {
Yang Hongyang50cf1562009-04-06 19:01:14 -07002343 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002344 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002345 dev_err(dev, "Unable to obtain %u-bit DMA "
2346 "for consistent allocations, aborting\n", 40);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002347 goto err_out_release_regions;
2348 }
2349 using_dac = 1;
2350 }
2351
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002352 /* Map vNIC resources from BAR0-5
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002353 */
2354
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002355 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2356 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2357 continue;
2358 enic->bar[i].len = pci_resource_len(pdev, i);
2359 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2360 if (!enic->bar[i].vaddr) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002361 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002362 err = -ENODEV;
2363 goto err_out_iounmap;
2364 }
2365 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002366 }
2367
2368 /* Register vNIC device
2369 */
2370
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002371 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2372 ARRAY_SIZE(enic->bar));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002373 if (!enic->vdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002374 dev_err(dev, "vNIC registration failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002375 err = -ENODEV;
2376 goto err_out_iounmap;
2377 }
2378
Roopa Prabhu8749b422011-09-22 03:44:33 +00002379#ifdef CONFIG_PCI_IOV
2380 /* Get number of subvnics */
2381 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
2382 if (pos) {
2383 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
Dan Carpenter413708b2012-02-29 21:19:54 +00002384 &enic->num_vfs);
Roopa Prabhu8749b422011-09-22 03:44:33 +00002385 if (enic->num_vfs) {
2386 err = pci_enable_sriov(pdev, enic->num_vfs);
2387 if (err) {
2388 dev_err(dev, "SRIOV enable failed, aborting."
2389 " pci_enable_sriov() returned %d\n",
2390 err);
2391 goto err_out_vnic_unregister;
2392 }
2393 enic->priv_flags |= ENIC_SRIOV_ENABLED;
Roopa Prabhub67f2312012-01-19 22:25:36 +00002394 num_pps = enic->num_vfs;
Roopa Prabhu8749b422011-09-22 03:44:33 +00002395 }
2396 }
Roopa Prabhu8749b422011-09-22 03:44:33 +00002397#endif
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002398
Roopa Prabhu3f192792011-09-22 03:44:43 +00002399 /* Allocate structure for port profiles */
Thomas Meyera1de2212011-11-29 11:08:00 +00002400 enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
Roopa Prabhu3f192792011-09-22 03:44:43 +00002401 if (!enic->pp) {
Roopa Prabhu3f192792011-09-22 03:44:43 +00002402 err = -ENOMEM;
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002403 goto err_out_disable_sriov_pp;
Roopa Prabhu3f192792011-09-22 03:44:43 +00002404 }
2405
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002406 /* Issue device open to get device in known state
2407 */
2408
2409 err = enic_dev_open(enic);
2410 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002411 dev_err(dev, "vNIC dev open failed, aborting\n");
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002412 goto err_out_disable_sriov;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002413 }
2414
Vasanthy Kolluri69161422011-02-04 16:17:16 +00002415 /* Setup devcmd lock
2416 */
2417
2418 spin_lock_init(&enic->devcmd_lock);
2419
2420 /*
2421 * Set ingress vlan rewrite mode before vnic initialization
2422 */
2423
2424 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2425 if (err) {
2426 dev_err(dev,
2427 "Failed to set ingress vlan rewrite mode, aborting.\n");
2428 goto err_out_dev_close;
2429 }
2430
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002431 /* Issue device init to initialize the vnic-to-switch link.
2432 * We'll start with carrier off and wait for link UP
2433 * notification later to turn on carrier. We don't need
2434 * to wait here for the vnic-to-switch link initialization
2435 * to complete; link UP notification is the indication that
2436 * the process is complete.
2437 */
2438
2439 netif_carrier_off(netdev);
2440
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002441 /* Do not call dev_init for a dynamic vnic.
2442 * For a dynamic vnic, init_prov_info will be
2443 * called later by an upper layer.
2444 */
2445
Roopa Prabhu2b68c182012-02-20 00:12:04 +00002446 if (!enic_is_dynamic(enic)) {
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002447 err = vnic_dev_init(enic->vdev, 0);
2448 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002449 dev_err(dev, "vNIC dev init failed, aborting\n");
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002450 goto err_out_dev_close;
2451 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002452 }
2453
Scott Feldman6fdfa972009-09-03 17:02:45 +00002454 err = enic_dev_init(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002455 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002456 dev_err(dev, "Device initialization failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002457 goto err_out_dev_close;
2458 }
2459
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002460 /* Setup notification timer, HW reset task, and wq locks
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002461 */
2462
2463 init_timer(&enic->notify_timer);
2464 enic->notify_timer.function = enic_notify_timer;
2465 enic->notify_timer.data = (unsigned long)enic;
2466
2467 INIT_WORK(&enic->reset, enic_reset);
Roopa Prabhuc97c8942011-06-03 14:35:17 +00002468 INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002469
2470 for (i = 0; i < enic->wq_count; i++)
2471 spin_lock_init(&enic->wq_lock[i]);
2472
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002473 /* Register net device
2474 */
2475
2476 enic->port_mtu = enic->config.mtu;
2477 (void)enic_change_mtu(netdev, enic->port_mtu);
2478
2479 err = enic_set_mac_addr(netdev, enic->mac_addr);
2480 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002481 dev_err(dev, "Invalid MAC address, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002482 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002483 }
2484
Scott Feldman7c844592009-12-23 13:27:54 +00002485 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2486 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2487
Roopa Prabhu73359032012-01-18 04:24:02 +00002488 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002489 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2490 else
2491 netdev->netdev_ops = &enic_netdev_ops;
2492
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002493 netdev->watchdog_timeo = 2 * HZ;
2494 netdev->ethtool_ops = &enic_ethtool_ops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002495
Vasanthy Kolluri73c1ea92010-03-18 16:19:54 +00002496 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00002497 if (ENIC_SETTING(enic, LOOP)) {
2498 netdev->features &= ~NETIF_F_HW_VLAN_TX;
2499 enic->loop_enable = 1;
2500 enic->loop_tag = enic->config.loop_tag;
2501 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2502 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002503 if (ENIC_SETTING(enic, TXCSUM))
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002504 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002505 if (ENIC_SETTING(enic, TSO))
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002506 netdev->hw_features |= NETIF_F_TSO |
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002507 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002508 if (ENIC_SETTING(enic, RXCSUM))
2509 netdev->hw_features |= NETIF_F_RXCSUM;
2510
2511 netdev->features |= netdev->hw_features;
2512
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002513 if (using_dac)
2514 netdev->features |= NETIF_F_HIGHDMA;
2515
Jiri Pirko01789342011-08-16 06:29:00 +00002516 netdev->priv_flags |= IFF_UNICAST_FLT;
2517
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002518 err = register_netdev(netdev);
2519 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002520 dev_err(dev, "Cannot register net device, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002521 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002522 }
2523
2524 return 0;
2525
Scott Feldman6fdfa972009-09-03 17:02:45 +00002526err_out_dev_deinit:
2527 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002528err_out_dev_close:
2529 vnic_dev_close(enic->vdev);
Roopa Prabhu8749b422011-09-22 03:44:33 +00002530err_out_disable_sriov:
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002531 kfree(enic->pp);
2532err_out_disable_sriov_pp:
Roopa Prabhu8749b422011-09-22 03:44:33 +00002533#ifdef CONFIG_PCI_IOV
2534 if (enic_sriov_enabled(enic)) {
2535 pci_disable_sriov(pdev);
2536 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2537 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002538err_out_vnic_unregister:
Roopa Prabhu8749b422011-09-22 03:44:33 +00002539#endif
Roopa Prabhu35d87e32012-01-18 04:24:12 +00002540 vnic_dev_unregister(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002541err_out_iounmap:
2542 enic_iounmap(enic);
2543err_out_release_regions:
2544 pci_release_regions(pdev);
2545err_out_disable_device:
2546 pci_disable_device(pdev);
2547err_out_free_netdev:
2548 pci_set_drvdata(pdev, NULL);
2549 free_netdev(netdev);
2550
2551 return err;
2552}
2553
Bill Pemberton854de922012-12-03 09:23:05 -05002554static void enic_remove(struct pci_dev *pdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002555{
2556 struct net_device *netdev = pci_get_drvdata(pdev);
2557
2558 if (netdev) {
2559 struct enic *enic = netdev_priv(netdev);
2560
Tejun Heo23f333a2010-12-12 16:45:14 +01002561 cancel_work_sync(&enic->reset);
Roopa Prabhuc97c8942011-06-03 14:35:17 +00002562 cancel_work_sync(&enic->change_mtu_work);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002563 unregister_netdev(netdev);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002564 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002565 vnic_dev_close(enic->vdev);
Roopa Prabhu8749b422011-09-22 03:44:33 +00002566#ifdef CONFIG_PCI_IOV
2567 if (enic_sriov_enabled(enic)) {
2568 pci_disable_sriov(pdev);
2569 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2570 }
2571#endif
Roopa Prabhu3f192792011-09-22 03:44:43 +00002572 kfree(enic->pp);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002573 vnic_dev_unregister(enic->vdev);
2574 enic_iounmap(enic);
2575 pci_release_regions(pdev);
2576 pci_disable_device(pdev);
2577 pci_set_drvdata(pdev, NULL);
2578 free_netdev(netdev);
2579 }
2580}
2581
2582static struct pci_driver enic_driver = {
2583 .name = DRV_NAME,
2584 .id_table = enic_id_table,
2585 .probe = enic_probe,
Bill Pemberton854de922012-12-03 09:23:05 -05002586 .remove = enic_remove,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002587};
2588
2589static int __init enic_init_module(void)
2590{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002591 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002592
2593 return pci_register_driver(&enic_driver);
2594}
2595
2596static void __exit enic_cleanup_module(void)
2597{
2598 pci_unregister_driver(&enic_driver);
2599}
2600
2601module_init(enic_init_module);
2602module_exit(enic_cleanup_module);