blob: 151b375337a95ae2fe3d24dd9e13393c9f79e66a [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>
Scott Feldman01f2e4e2008-09-15 09:17:11 -070034#include <linux/in.h>
35#include <linux/ip.h>
36#include <linux/ipv6.h>
37#include <linux/tcp.h>
Vasanthy Kolluri29046f92010-06-24 10:52:26 +000038#include <linux/rtnetlink.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040039#include <linux/prefetch.h>
Kamalesh Babulalb7c6bfb2008-10-13 18:41:01 -070040#include <net/ip6_checksum.h>
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +053041#include <linux/ktime.h>
Govindarajulu Varadarajanb6e97c12014-06-23 16:08:01 +053042#ifdef CONFIG_RFS_ACCEL
43#include <linux/cpu_rmap.h>
44#endif
Scott Feldman01f2e4e2008-09-15 09:17:11 -070045
46#include "cq_enet_desc.h"
47#include "vnic_dev.h"
48#include "vnic_intr.h"
49#include "vnic_stats.h"
Scott Feldmanf8bd9092010-05-17 22:50:19 -070050#include "vnic_vic.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070051#include "enic_res.h"
52#include "enic.h"
Vasanthy Kolluri51987462011-02-04 16:17:05 +000053#include "enic_dev.h"
Roopa Prabhub3abfbd2011-03-29 20:36:07 +000054#include "enic_pp.h"
Scott Feldman01f2e4e2008-09-15 09:17:11 -070055
56#define ENIC_NOTIFY_TIMER_PERIOD (2 * HZ)
Scott Feldmanea0d7d92009-09-03 17:02:03 +000057#define WQ_ENET_MAX_DESC_LEN (1 << WQ_ENET_LEN_BITS)
58#define MAX_TSO (1 << 16)
59#define ENIC_DESC_MAX_SPLITS (MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
60
61#define PCI_DEVICE_ID_CISCO_VIC_ENET 0x0043 /* ethernet vnic */
Scott Feldmanf8bd9092010-05-17 22:50:19 -070062#define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN 0x0044 /* enet dynamic vnic */
Roopa Prabhu3a4adef2012-01-18 04:23:55 +000063#define PCI_DEVICE_ID_CISCO_VIC_ENET_VF 0x0071 /* enet SRIOV VF */
Scott Feldman01f2e4e2008-09-15 09:17:11 -070064
65/* Supported devices */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000066static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
Scott Feldmanea0d7d92009-09-03 17:02:03 +000067 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
Scott Feldmanf8bd9092010-05-17 22:50:19 -070068 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
Roopa Prabhu3a4adef2012-01-18 04:23:55 +000069 { PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
Scott Feldman01f2e4e2008-09-15 09:17:11 -070070 { 0, } /* end of table */
71};
72
73MODULE_DESCRIPTION(DRV_DESCRIPTION);
74MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
75MODULE_LICENSE("GPL");
76MODULE_VERSION(DRV_VERSION);
77MODULE_DEVICE_TABLE(pci, enic_id_table);
78
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +053079#define ENIC_LARGE_PKT_THRESHOLD 1000
80#define ENIC_MAX_COALESCE_TIMERS 10
81/* Interrupt moderation table, which will be used to decide the
82 * coalescing timer values
83 * {rx_rate in Mbps, mapping percentage of the range}
84 */
85struct enic_intr_mod_table mod_table[ENIC_MAX_COALESCE_TIMERS + 1] = {
86 {4000, 0},
87 {4400, 10},
88 {5060, 20},
89 {5230, 30},
90 {5540, 40},
91 {5820, 50},
92 {6120, 60},
93 {6435, 70},
94 {6745, 80},
95 {7000, 90},
96 {0xFFFFFFFF, 100}
97};
98
99/* This table helps the driver to pick different ranges for rx coalescing
100 * timer depending on the link speed.
101 */
102struct enic_intr_mod_range mod_range[ENIC_MAX_LINK_SPEEDS] = {
103 {0, 0}, /* 0 - 4 Gbps */
104 {0, 3}, /* 4 - 10 Gbps */
105 {3, 6}, /* 10 - 40 Gbps */
106};
107
Roopa Prabhu3f192792011-09-22 03:44:43 +0000108int enic_is_dynamic(struct enic *enic)
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700109{
110 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
111}
112
Roopa Prabhu8749b422011-09-22 03:44:33 +0000113int enic_sriov_enabled(struct enic *enic)
114{
115 return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
116}
117
Roopa Prabhu3a4adef2012-01-18 04:23:55 +0000118static int enic_is_sriov_vf(struct enic *enic)
119{
120 return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
121}
122
Roopa Prabhu889d13f2011-09-22 03:44:38 +0000123int enic_is_valid_vf(struct enic *enic, int vf)
124{
125#ifdef CONFIG_PCI_IOV
126 return vf >= 0 && vf < enic->num_vfs;
127#else
128 return 0;
129#endif
130}
131
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700132static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
133{
134 struct enic *enic = vnic_dev_priv(wq->vdev);
135
136 if (buf->sop)
137 pci_unmap_single(enic->pdev, buf->dma_addr,
138 buf->len, PCI_DMA_TODEVICE);
139 else
140 pci_unmap_page(enic->pdev, buf->dma_addr,
141 buf->len, PCI_DMA_TODEVICE);
142
143 if (buf->os_buf)
144 dev_kfree_skb_any(buf->os_buf);
145}
146
147static void enic_wq_free_buf(struct vnic_wq *wq,
148 struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
149{
150 enic_free_wq_buf(wq, buf);
151}
152
153static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
154 u8 type, u16 q_number, u16 completed_index, void *opaque)
155{
156 struct enic *enic = vnic_dev_priv(vdev);
157
158 spin_lock(&enic->wq_lock[q_number]);
159
160 vnic_wq_service(&enic->wq[q_number], cq_desc,
161 completed_index, enic_wq_free_buf,
162 opaque);
163
govindarajulu.v822473b2013-09-04 11:17:14 +0530164 if (netif_tx_queue_stopped(netdev_get_tx_queue(enic->netdev, q_number)) &&
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000165 vnic_wq_desc_avail(&enic->wq[q_number]) >=
166 (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
govindarajulu.v822473b2013-09-04 11:17:14 +0530167 netif_wake_subqueue(enic->netdev, q_number);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700168
169 spin_unlock(&enic->wq_lock[q_number]);
170
171 return 0;
172}
173
174static void enic_log_q_error(struct enic *enic)
175{
176 unsigned int i;
177 u32 error_status;
178
179 for (i = 0; i < enic->wq_count; i++) {
180 error_status = vnic_wq_error_status(&enic->wq[i]);
181 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000182 netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
183 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700184 }
185
186 for (i = 0; i < enic->rq_count; i++) {
187 error_status = vnic_rq_error_status(&enic->rq[i]);
188 if (error_status)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000189 netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
190 i, error_status);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700191 }
192}
193
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000194static void enic_msglvl_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700195{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000196 u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700197
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000198 if (msg_enable != enic->msg_enable) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000199 netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
200 enic->msg_enable, msg_enable);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000201 enic->msg_enable = msg_enable;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700202 }
203}
204
205static void enic_mtu_check(struct enic *enic)
206{
207 u32 mtu = vnic_dev_mtu(enic->vdev);
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000208 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700209
Scott Feldman491598a2009-09-03 17:02:40 +0000210 if (mtu && mtu != enic->port_mtu) {
Scott Feldman7c844592009-12-23 13:27:54 +0000211 enic->port_mtu = mtu;
Roopa Prabhu73359032012-01-18 04:24:02 +0000212 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
Roopa Prabhuc97c8942011-06-03 14:35:17 +0000213 mtu = max_t(int, ENIC_MIN_MTU,
214 min_t(int, ENIC_MAX_MTU, mtu));
215 if (mtu != netdev->mtu)
216 schedule_work(&enic->change_mtu_work);
217 } else {
218 if (mtu < netdev->mtu)
219 netdev_warn(netdev,
220 "interface MTU (%d) set higher "
221 "than switch port MTU (%d)\n",
222 netdev->mtu, mtu);
223 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700224 }
225}
226
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000227static void enic_link_check(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700228{
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000229 int link_status = vnic_dev_link_status(enic->vdev);
230 int carrier_ok = netif_carrier_ok(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700231
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000232 if (link_status && !carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000233 netdev_info(enic->netdev, "Link UP\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000234 netif_carrier_on(enic->netdev);
235 } else if (!link_status && carrier_ok) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000236 netdev_info(enic->netdev, "Link DOWN\n");
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000237 netif_carrier_off(enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700238 }
239}
240
241static void enic_notify_check(struct enic *enic)
242{
243 enic_msglvl_check(enic);
244 enic_mtu_check(enic);
245 enic_link_check(enic);
246}
247
248#define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
249
250static irqreturn_t enic_isr_legacy(int irq, void *data)
251{
252 struct net_device *netdev = data;
253 struct enic *enic = netdev_priv(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000254 unsigned int io_intr = enic_legacy_io_intr();
255 unsigned int err_intr = enic_legacy_err_intr();
256 unsigned int notify_intr = enic_legacy_notify_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700257 u32 pba;
258
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000259 vnic_intr_mask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700260
261 pba = vnic_intr_legacy_pba(enic->legacy_pba);
262 if (!pba) {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000263 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700264 return IRQ_NONE; /* not our interrupt */
265 }
266
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000267 if (ENIC_TEST_INTR(pba, notify_intr)) {
268 vnic_intr_return_all_credits(&enic->intr[notify_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700269 enic_notify_check(enic);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800270 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700271
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000272 if (ENIC_TEST_INTR(pba, err_intr)) {
273 vnic_intr_return_all_credits(&enic->intr[err_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700274 enic_log_q_error(enic);
275 /* schedule recovery from WQ/RQ error */
276 schedule_work(&enic->reset);
277 return IRQ_HANDLED;
278 }
279
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000280 if (ENIC_TEST_INTR(pba, io_intr)) {
281 if (napi_schedule_prep(&enic->napi[0]))
282 __napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700283 } else {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000284 vnic_intr_unmask(&enic->intr[io_intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700285 }
286
287 return IRQ_HANDLED;
288}
289
290static irqreturn_t enic_isr_msi(int irq, void *data)
291{
292 struct enic *enic = data;
293
294 /* With MSI, there is no sharing of interrupts, so this is
295 * our interrupt and there is no need to ack it. The device
296 * is not providing per-vector masking, so the OS will not
297 * write to PCI config space to mask/unmask the interrupt.
298 * We're using mask_on_assertion for MSI, so the device
299 * automatically masks the interrupt when the interrupt is
300 * generated. Later, when exiting polling, the interrupt
301 * will be unmasked (see enic_poll).
302 *
303 * Also, the device uses the same PCIe Traffic Class (TC)
304 * for Memory Write data and MSI, so there are no ordering
305 * issues; the MSI will always arrive at the Root Complex
306 * _after_ corresponding Memory Writes (i.e. descriptor
307 * writes).
308 */
309
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000310 napi_schedule(&enic->napi[0]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700311
312 return IRQ_HANDLED;
313}
314
315static irqreturn_t enic_isr_msix_rq(int irq, void *data)
316{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000317 struct napi_struct *napi = data;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700318
319 /* schedule NAPI polling for RQ cleanup */
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000320 napi_schedule(napi);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700321
322 return IRQ_HANDLED;
323}
324
325static irqreturn_t enic_isr_msix_wq(int irq, void *data)
326{
327 struct enic *enic = data;
govindarajulu.v822473b2013-09-04 11:17:14 +0530328 unsigned int cq;
329 unsigned int intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700330 unsigned int wq_work_to_do = -1; /* no limit */
331 unsigned int wq_work_done;
govindarajulu.v822473b2013-09-04 11:17:14 +0530332 unsigned int wq_irq;
333
334 wq_irq = (u32)irq - enic->msix_entry[enic_msix_wq_intr(enic, 0)].vector;
335 cq = enic_cq_wq(enic, wq_irq);
336 intr = enic_msix_wq_intr(enic, wq_irq);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700337
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000338 wq_work_done = vnic_cq_service(&enic->cq[cq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700339 wq_work_to_do, enic_wq_service, NULL);
340
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000341 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700342 wq_work_done,
343 1 /* unmask intr */,
344 1 /* reset intr timer */);
345
346 return IRQ_HANDLED;
347}
348
349static irqreturn_t enic_isr_msix_err(int irq, void *data)
350{
351 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000352 unsigned int intr = enic_msix_err_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700353
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000354 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800355
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700356 enic_log_q_error(enic);
357
358 /* schedule recovery from WQ/RQ error */
359 schedule_work(&enic->reset);
360
361 return IRQ_HANDLED;
362}
363
364static irqreturn_t enic_isr_msix_notify(int irq, void *data)
365{
366 struct enic *enic = data;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000367 unsigned int intr = enic_msix_notify_intr(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700368
Vasanthy Kolluri717258b2010-10-20 10:16:59 +0000369 vnic_intr_return_all_credits(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700370 enic_notify_check(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700371
372 return IRQ_HANDLED;
373}
374
375static inline void enic_queue_wq_skb_cont(struct enic *enic,
376 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000377 unsigned int len_left, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700378{
Eric Dumazet9e903e02011-10-18 21:00:24 +0000379 const skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700380
381 /* Queue additional data fragments */
382 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000383 len_left -= skb_frag_size(frag);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700384 enic_queue_wq_desc_cont(wq, skb,
Ian Campbell4bf5adb2011-08-29 23:18:27 +0000385 skb_frag_dma_map(&enic->pdev->dev,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000386 frag, 0, skb_frag_size(frag),
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100387 DMA_TO_DEVICE),
Eric Dumazet9e903e02011-10-18 21:00:24 +0000388 skb_frag_size(frag),
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000389 (len_left == 0), /* EOP? */
390 loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700391 }
392}
393
394static inline void enic_queue_wq_skb_vlan(struct enic *enic,
395 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000396 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700397{
398 unsigned int head_len = skb_headlen(skb);
399 unsigned int len_left = skb->len - head_len;
400 int eop = (len_left == 0);
401
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000402 /* Queue the main skb fragment. The fragments are no larger
403 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
404 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
405 * per fragment is queued.
406 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700407 enic_queue_wq_desc(wq, skb,
408 pci_map_single(enic->pdev, skb->data,
409 head_len, PCI_DMA_TODEVICE),
410 head_len,
411 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000412 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700413
414 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000415 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700416}
417
418static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
419 struct vnic_wq *wq, struct sk_buff *skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000420 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700421{
422 unsigned int head_len = skb_headlen(skb);
423 unsigned int len_left = skb->len - head_len;
Michał Mirosław0d0b1672010-12-14 15:24:08 +0000424 unsigned int hdr_len = skb_checksum_start_offset(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700425 unsigned int csum_offset = hdr_len + skb->csum_offset;
426 int eop = (len_left == 0);
427
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000428 /* Queue the main skb fragment. The fragments are no larger
429 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
430 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
431 * per fragment is queued.
432 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700433 enic_queue_wq_desc_csum_l4(wq, skb,
434 pci_map_single(enic->pdev, skb->data,
435 head_len, PCI_DMA_TODEVICE),
436 head_len,
437 csum_offset,
438 hdr_len,
439 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000440 eop, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700441
442 if (!eop)
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000443 enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700444}
445
446static inline void enic_queue_wq_skb_tso(struct enic *enic,
447 struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000448 int vlan_tag_insert, unsigned int vlan_tag, int loopback)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700449{
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000450 unsigned int frag_len_left = skb_headlen(skb);
451 unsigned int len_left = skb->len - frag_len_left;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700452 unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
453 int eop = (len_left == 0);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000454 unsigned int len;
455 dma_addr_t dma_addr;
456 unsigned int offset = 0;
457 skb_frag_t *frag;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700458
459 /* Preload TCP csum field with IP pseudo hdr calculated
460 * with IP length set to zero. HW will later add in length
461 * to each TCP segment resulting from the TSO.
462 */
463
Harvey Harrison09640e62009-02-01 00:45:17 -0800464 if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700465 ip_hdr(skb)->check = 0;
466 tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
467 ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
Harvey Harrison09640e62009-02-01 00:45:17 -0800468 } else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700469 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
470 &ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
471 }
472
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000473 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
474 * for the main skb fragment
475 */
476 while (frag_len_left) {
477 len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
478 dma_addr = pci_map_single(enic->pdev, skb->data + offset,
479 len, PCI_DMA_TODEVICE);
480 enic_queue_wq_desc_tso(wq, skb,
481 dma_addr,
482 len,
483 mss, hdr_len,
484 vlan_tag_insert, vlan_tag,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000485 eop && (len == frag_len_left), loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000486 frag_len_left -= len;
487 offset += len;
488 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700489
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000490 if (eop)
491 return;
492
493 /* Queue WQ_ENET_MAX_DESC_LEN length descriptors
494 * for additional data fragments
495 */
496 for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000497 len_left -= skb_frag_size(frag);
498 frag_len_left = skb_frag_size(frag);
Ian Campbell4bf5adb2011-08-29 23:18:27 +0000499 offset = 0;
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000500
501 while (frag_len_left) {
502 len = min(frag_len_left,
503 (unsigned int)WQ_ENET_MAX_DESC_LEN);
Ian Campbell4bf5adb2011-08-29 23:18:27 +0000504 dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
505 offset, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100506 DMA_TO_DEVICE);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000507 enic_queue_wq_desc_cont(wq, skb,
508 dma_addr,
509 len,
510 (len_left == 0) &&
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000511 (len == frag_len_left), /* EOP? */
512 loopback);
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000513 frag_len_left -= len;
514 offset += len;
515 }
516 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700517}
518
519static inline void enic_queue_wq_skb(struct enic *enic,
520 struct vnic_wq *wq, struct sk_buff *skb)
521{
522 unsigned int mss = skb_shinfo(skb)->gso_size;
523 unsigned int vlan_tag = 0;
524 int vlan_tag_insert = 0;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000525 int loopback = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700526
Jesse Grosseab6d182010-10-20 13:56:03 +0000527 if (vlan_tx_tag_present(skb)) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700528 /* VLAN tag from trunking driver */
529 vlan_tag_insert = 1;
530 vlan_tag = vlan_tx_tag_get(skb);
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000531 } else if (enic->loop_enable) {
532 vlan_tag = enic->loop_tag;
533 loopback = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700534 }
535
536 if (mss)
537 enic_queue_wq_skb_tso(enic, wq, skb, mss,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000538 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700539 else if (skb->ip_summed == CHECKSUM_PARTIAL)
540 enic_queue_wq_skb_csum_l4(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000541 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700542 else
543 enic_queue_wq_skb_vlan(enic, wq, skb,
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000544 vlan_tag_insert, vlan_tag, loopback);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700545}
546
Scott Feldmaned8af6b2009-02-09 23:23:50 -0800547/* netif_tx_lock held, process context with BHs disabled, or BH */
Stephen Hemminger613573252009-08-31 19:50:58 +0000548static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
Scott Feldmand87fd252009-12-23 13:27:59 +0000549 struct net_device *netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700550{
551 struct enic *enic = netdev_priv(netdev);
govindarajulu.v822473b2013-09-04 11:17:14 +0530552 struct vnic_wq *wq;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700553 unsigned long flags;
govindarajulu.v822473b2013-09-04 11:17:14 +0530554 unsigned int txq_map;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700555
556 if (skb->len <= 0) {
Eric W. Biederman98d8a652014-03-15 16:49:05 -0700557 dev_kfree_skb_any(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700558 return NETDEV_TX_OK;
559 }
560
govindarajulu.v822473b2013-09-04 11:17:14 +0530561 txq_map = skb_get_queue_mapping(skb) % enic->wq_count;
562 wq = &enic->wq[txq_map];
563
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700564 /* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
565 * which is very likely. In the off chance it's going to take
566 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
567 */
568
569 if (skb_shinfo(skb)->gso_size == 0 &&
570 skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
571 skb_linearize(skb)) {
Eric W. Biederman98d8a652014-03-15 16:49:05 -0700572 dev_kfree_skb_any(skb);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700573 return NETDEV_TX_OK;
574 }
575
govindarajulu.v822473b2013-09-04 11:17:14 +0530576 spin_lock_irqsave(&enic->wq_lock[txq_map], flags);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700577
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000578 if (vnic_wq_desc_avail(wq) <
579 skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
govindarajulu.v822473b2013-09-04 11:17:14 +0530580 netif_tx_stop_queue(netdev_get_tx_queue(netdev, txq_map));
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700581 /* This is a hard error, log it */
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +0000582 netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
govindarajulu.v822473b2013-09-04 11:17:14 +0530583 spin_unlock_irqrestore(&enic->wq_lock[txq_map], flags);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700584 return NETDEV_TX_BUSY;
585 }
586
587 enic_queue_wq_skb(enic, wq, skb);
588
Scott Feldmanea0d7d92009-09-03 17:02:03 +0000589 if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
govindarajulu.v822473b2013-09-04 11:17:14 +0530590 netif_tx_stop_queue(netdev_get_tx_queue(netdev, txq_map));
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700591
govindarajulu.v822473b2013-09-04 11:17:14 +0530592 spin_unlock_irqrestore(&enic->wq_lock[txq_map], flags);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700593
594 return NETDEV_TX_OK;
595}
596
597/* dev_base_lock rwlock held, nominally process context */
stephen hemmingerf20530b2011-06-08 14:54:02 +0000598static struct rtnl_link_stats64 *enic_get_stats(struct net_device *netdev,
599 struct rtnl_link_stats64 *net_stats)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700600{
601 struct enic *enic = netdev_priv(netdev);
602 struct vnic_stats *stats;
603
Vasanthy Kolluri383ab922010-06-24 10:50:12 +0000604 enic_dev_stats_dump(enic, &stats);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700605
Scott Feldman25f0a062008-09-24 11:23:32 -0700606 net_stats->tx_packets = stats->tx.tx_frames_ok;
607 net_stats->tx_bytes = stats->tx.tx_bytes_ok;
608 net_stats->tx_errors = stats->tx.tx_errors;
609 net_stats->tx_dropped = stats->tx.tx_drops;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700610
Scott Feldman25f0a062008-09-24 11:23:32 -0700611 net_stats->rx_packets = stats->rx.rx_frames_ok;
612 net_stats->rx_bytes = stats->rx.rx_bytes_ok;
613 net_stats->rx_errors = stats->rx.rx_errors;
614 net_stats->multicast = stats->rx.rx_multicast_frames_ok;
Scott Feldman350991e2009-09-03 17:02:19 +0000615 net_stats->rx_over_errors = enic->rq_truncated_pkts;
Scott Feldmanbd9fb1a2009-02-09 23:24:08 -0800616 net_stats->rx_crc_errors = enic->rq_bad_fcs;
Scott Feldman350991e2009-09-03 17:02:19 +0000617 net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700618
Scott Feldman25f0a062008-09-24 11:23:32 -0700619 return net_stats;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700620}
621
Alexander Duyckf0096182014-05-28 18:44:52 -0700622static int enic_mc_sync(struct net_device *netdev, const u8 *mc_addr)
623{
624 struct enic *enic = netdev_priv(netdev);
625
626 if (enic->mc_count == ENIC_MULTICAST_PERFECT_FILTERS) {
627 unsigned int mc_count = netdev_mc_count(netdev);
628
629 netdev_warn(netdev, "Registering only %d out of %d multicast addresses\n",
630 ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
631
632 return -ENOSPC;
633 }
634
635 enic_dev_add_addr(enic, mc_addr);
636 enic->mc_count++;
637
638 return 0;
639}
640
641static int enic_mc_unsync(struct net_device *netdev, const u8 *mc_addr)
642{
643 struct enic *enic = netdev_priv(netdev);
644
645 enic_dev_del_addr(enic, mc_addr);
646 enic->mc_count--;
647
648 return 0;
649}
650
651static int enic_uc_sync(struct net_device *netdev, const u8 *uc_addr)
652{
653 struct enic *enic = netdev_priv(netdev);
654
655 if (enic->uc_count == ENIC_UNICAST_PERFECT_FILTERS) {
656 unsigned int uc_count = netdev_uc_count(netdev);
657
658 netdev_warn(netdev, "Registering only %d out of %d unicast addresses\n",
659 ENIC_UNICAST_PERFECT_FILTERS, uc_count);
660
661 return -ENOSPC;
662 }
663
664 enic_dev_add_addr(enic, uc_addr);
665 enic->uc_count++;
666
667 return 0;
668}
669
670static int enic_uc_unsync(struct net_device *netdev, const u8 *uc_addr)
671{
672 struct enic *enic = netdev_priv(netdev);
673
674 enic_dev_del_addr(enic, uc_addr);
675 enic->uc_count--;
676
677 return 0;
678}
679
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000680void enic_reset_addr_lists(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700681{
Alexander Duyckf0096182014-05-28 18:44:52 -0700682 struct net_device *netdev = enic->netdev;
683
684 __dev_uc_unsync(netdev, NULL);
685 __dev_mc_unsync(netdev, NULL);
686
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700687 enic->mc_count = 0;
Vasanthy Kollurie0afe532011-02-17 08:53:12 +0000688 enic->uc_count = 0;
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +0000689 enic->flags = 0;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700690}
691
692static int enic_set_mac_addr(struct net_device *netdev, char *addr)
693{
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700694 struct enic *enic = netdev_priv(netdev);
695
Roopa Prabhu73359032012-01-18 04:24:02 +0000696 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700697 if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
698 return -EADDRNOTAVAIL;
699 } else {
700 if (!is_valid_ether_addr(addr))
701 return -EADDRNOTAVAIL;
702 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700703
704 memcpy(netdev->dev_addr, addr, netdev->addr_len);
705
706 return 0;
707}
708
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700709static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
710{
711 struct enic *enic = netdev_priv(netdev);
712 struct sockaddr *saddr = p;
713 char *addr = saddr->sa_data;
714 int err;
715
716 if (netif_running(enic->netdev)) {
717 err = enic_dev_del_station_addr(enic);
718 if (err)
719 return err;
720 }
721
722 err = enic_set_mac_addr(netdev, addr);
723 if (err)
724 return err;
725
726 if (netif_running(enic->netdev)) {
727 err = enic_dev_add_station_addr(enic);
728 if (err)
729 return err;
730 }
731
732 return err;
733}
734
735static int enic_set_mac_address(struct net_device *netdev, void *p)
736{
Roopa Prabhu294dab22010-08-10 18:54:55 +0000737 struct sockaddr *saddr = p;
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000738 char *addr = saddr->sa_data;
739 struct enic *enic = netdev_priv(netdev);
740 int err;
Roopa Prabhu294dab22010-08-10 18:54:55 +0000741
Vasanthy Kolluric76fd322010-10-20 10:17:04 +0000742 err = enic_dev_del_station_addr(enic);
743 if (err)
744 return err;
745
746 err = enic_set_mac_addr(netdev, addr);
747 if (err)
748 return err;
749
750 return enic_dev_add_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700751}
752
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000753/* netif_tx_lock held, BHs disabled */
754static void enic_set_rx_mode(struct net_device *netdev)
755{
756 struct enic *enic = netdev_priv(netdev);
757 int directed = 1;
758 int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
759 int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
760 int promisc = (netdev->flags & IFF_PROMISC) ||
761 netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
762 int allmulti = (netdev->flags & IFF_ALLMULTI) ||
763 netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
764 unsigned int flags = netdev->flags |
765 (allmulti ? IFF_ALLMULTI : 0) |
766 (promisc ? IFF_PROMISC : 0);
767
768 if (enic->flags != flags) {
769 enic->flags = flags;
770 enic_dev_packet_filter(enic, directed,
771 multicast, broadcast, promisc, allmulti);
772 }
773
774 if (!promisc) {
Alexander Duyckf0096182014-05-28 18:44:52 -0700775 __dev_uc_sync(netdev, enic_uc_sync, enic_uc_unsync);
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000776 if (!allmulti)
Alexander Duyckf0096182014-05-28 18:44:52 -0700777 __dev_mc_sync(netdev, enic_mc_sync, enic_mc_unsync);
Roopa Prabhu319d7e82010-12-08 13:19:58 +0000778 }
779}
780
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700781/* netif_tx_lock held, BHs disabled */
782static void enic_tx_timeout(struct net_device *netdev)
783{
784 struct enic *enic = netdev_priv(netdev);
785 schedule_work(&enic->reset);
786}
787
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +0000788static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
789{
790 struct enic *enic = netdev_priv(netdev);
Roopa Prabhu3f192792011-09-22 03:44:43 +0000791 struct enic_port_profile *pp;
792 int err;
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +0000793
Roopa Prabhu3f192792011-09-22 03:44:43 +0000794 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
795 if (err)
796 return err;
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +0000797
Roopa Prabhub8622cb2012-03-07 03:50:44 +0000798 if (is_valid_ether_addr(mac) || is_zero_ether_addr(mac)) {
Roopa Prabhub4765832012-02-20 00:11:58 +0000799 if (vf == PORT_SELF_VF) {
800 memcpy(pp->vf_mac, mac, ETH_ALEN);
801 return 0;
802 } else {
803 /*
804 * For sriov vf's set the mac in hw
805 */
806 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
807 vnic_dev_set_mac_addr, mac);
808 return enic_dev_status_to_errno(err);
809 }
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +0000810 } else
811 return -EINVAL;
812}
813
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700814static int enic_set_vf_port(struct net_device *netdev, int vf,
815 struct nlattr *port[])
816{
817 struct enic *enic = netdev_priv(netdev);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000818 struct enic_port_profile prev_pp;
Roopa Prabhu3f192792011-09-22 03:44:43 +0000819 struct enic_port_profile *pp;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000820 int err = 0, restore_pp = 1;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700821
Roopa Prabhu3f192792011-09-22 03:44:43 +0000822 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
823 if (err)
824 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700825
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000826 if (!port[IFLA_PORT_REQUEST])
Scott Feldman08f382e2010-06-01 08:59:33 +0000827 return -EOPNOTSUPP;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700828
Roopa Prabhu3f192792011-09-22 03:44:43 +0000829 memcpy(&prev_pp, pp, sizeof(*enic->pp));
830 memset(pp, 0, sizeof(*enic->pp));
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000831
Roopa Prabhu3f192792011-09-22 03:44:43 +0000832 pp->set |= ENIC_SET_REQUEST;
833 pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000834
835 if (port[IFLA_PORT_PROFILE]) {
Roopa Prabhu3f192792011-09-22 03:44:43 +0000836 pp->set |= ENIC_SET_NAME;
837 memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000838 PORT_PROFILE_MAX);
839 }
840
841 if (port[IFLA_PORT_INSTANCE_UUID]) {
Roopa Prabhu3f192792011-09-22 03:44:43 +0000842 pp->set |= ENIC_SET_INSTANCE;
843 memcpy(pp->instance_uuid,
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000844 nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
845 }
846
847 if (port[IFLA_PORT_HOST_UUID]) {
Roopa Prabhu3f192792011-09-22 03:44:43 +0000848 pp->set |= ENIC_SET_HOST;
849 memcpy(pp->host_uuid,
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000850 nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
851 }
852
Roopa Prabhub4765832012-02-20 00:11:58 +0000853 if (vf == PORT_SELF_VF) {
854 /* Special case handling: mac came from IFLA_VF_MAC */
855 if (!is_zero_ether_addr(prev_pp.vf_mac))
856 memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
Scott Feldman418c4372010-05-22 17:29:58 +0000857
Roopa Prabhub4765832012-02-20 00:11:58 +0000858 if (is_zero_ether_addr(netdev->dev_addr))
859 eth_hw_addr_random(netdev);
860 } else {
861 /* SR-IOV VF: get mac from adapter */
862 ENIC_DEVCMD_PROXY_BY_INDEX(vf, err, enic,
863 vnic_dev_get_mac_addr, pp->mac_addr);
864 if (err) {
865 netdev_err(netdev, "Error getting mac for vf %d\n", vf);
866 memcpy(pp, &prev_pp, sizeof(*pp));
867 return enic_dev_status_to_errno(err);
868 }
869 }
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000870
Roopa Prabhu3f192792011-09-22 03:44:43 +0000871 err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000872 if (err) {
873 if (restore_pp) {
874 /* Things are still the way they were: Implicit
875 * DISASSOCIATE failed
876 */
Roopa Prabhu3f192792011-09-22 03:44:43 +0000877 memcpy(pp, &prev_pp, sizeof(*pp));
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000878 } else {
Roopa Prabhu3f192792011-09-22 03:44:43 +0000879 memset(pp, 0, sizeof(*pp));
880 if (vf == PORT_SELF_VF)
881 memset(netdev->dev_addr, 0, ETH_ALEN);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000882 }
883 } else {
884 /* Set flag to indicate that the port assoc/disassoc
885 * request has been sent out to fw
886 */
Roopa Prabhu3f192792011-09-22 03:44:43 +0000887 pp->set |= ENIC_PORT_REQUEST_APPLIED;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000888
889 /* If DISASSOCIATE, clean up all assigned/saved macaddresses */
Roopa Prabhu3f192792011-09-22 03:44:43 +0000890 if (pp->request == PORT_REQUEST_DISASSOCIATE) {
891 memset(pp->mac_addr, 0, ETH_ALEN);
892 if (vf == PORT_SELF_VF)
893 memset(netdev->dev_addr, 0, ETH_ALEN);
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000894 }
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700895 }
896
Roopa Prabhub4765832012-02-20 00:11:58 +0000897 if (vf == PORT_SELF_VF)
898 memset(pp->vf_mac, 0, ETH_ALEN);
Roopa Prabhu296390592010-12-08 13:54:03 +0000899
Roopa Prabhu296390592010-12-08 13:54:03 +0000900 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700901}
902
903static int enic_get_vf_port(struct net_device *netdev, int vf,
904 struct sk_buff *skb)
905{
906 struct enic *enic = netdev_priv(netdev);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700907 u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
Roopa Prabhu3f192792011-09-22 03:44:43 +0000908 struct enic_port_profile *pp;
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000909 int err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700910
Roopa Prabhu3f192792011-09-22 03:44:43 +0000911 ENIC_PP_BY_INDEX(enic, vf, pp, &err);
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700912 if (err)
Roopa Prabhub3abfbd2011-03-29 20:36:07 +0000913 return err;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700914
Roopa Prabhu3f192792011-09-22 03:44:43 +0000915 if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
916 return -ENODATA;
917
918 err = enic_process_get_pp_request(enic, vf, pp->request, &response);
919 if (err)
920 return err;
921
David S. Miller1a106de2012-04-01 20:22:22 -0400922 if (nla_put_u16(skb, IFLA_PORT_REQUEST, pp->request) ||
923 nla_put_u16(skb, IFLA_PORT_RESPONSE, response) ||
924 ((pp->set & ENIC_SET_NAME) &&
925 nla_put(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX, pp->name)) ||
926 ((pp->set & ENIC_SET_INSTANCE) &&
927 nla_put(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
928 pp->instance_uuid)) ||
929 ((pp->set & ENIC_SET_HOST) &&
930 nla_put(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX, pp->host_uuid)))
931 goto nla_put_failure;
Scott Feldmanf8bd9092010-05-17 22:50:19 -0700932 return 0;
933
934nla_put_failure:
935 return -EMSGSIZE;
936}
937
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700938static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
939{
940 struct enic *enic = vnic_dev_priv(rq->vdev);
941
942 if (!buf->os_buf)
943 return;
944
945 pci_unmap_single(enic->pdev, buf->dma_addr,
946 buf->len, PCI_DMA_FROMDEVICE);
947 dev_kfree_skb_any(buf->os_buf);
948}
949
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700950static int enic_rq_alloc_buf(struct vnic_rq *rq)
951{
952 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldmand19e22d2009-09-03 17:02:08 +0000953 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700954 struct sk_buff *skb;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +0000955 unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700956 unsigned int os_buf_index = 0;
957 dma_addr_t dma_addr;
958
Eric Dumazet89d71a62009-10-13 05:34:20 +0000959 skb = netdev_alloc_skb_ip_align(netdev, len);
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700960 if (!skb)
961 return -ENOMEM;
962
963 dma_addr = pci_map_single(enic->pdev, skb->data,
964 len, PCI_DMA_FROMDEVICE);
965
966 enic_queue_rq_desc(rq, skb, os_buf_index,
967 dma_addr, len);
968
969 return 0;
970}
971
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +0530972static void enic_intr_update_pkt_size(struct vnic_rx_bytes_counter *pkt_size,
973 u32 pkt_len)
974{
975 if (ENIC_LARGE_PKT_THRESHOLD <= pkt_len)
976 pkt_size->large_pkt_bytes_cnt += pkt_len;
977 else
978 pkt_size->small_pkt_bytes_cnt += pkt_len;
979}
980
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700981static void enic_rq_indicate_buf(struct vnic_rq *rq,
982 struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
983 int skipped, void *opaque)
984{
985 struct enic *enic = vnic_dev_priv(rq->vdev);
Scott Feldman86ca9db2008-11-21 21:26:55 -0800986 struct net_device *netdev = enic->netdev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700987 struct sk_buff *skb;
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +0530988 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700989
990 u8 type, color, eop, sop, ingress_port, vlan_stripped;
991 u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
992 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
993 u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
994 u8 packet_error;
Vasanthy Kollurif8cac142010-06-24 10:49:51 +0000995 u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
Scott Feldman01f2e4e2008-09-15 09:17:11 -0700996 u32 rss_hash;
997
998 if (skipped)
999 return;
1000
1001 skb = buf->os_buf;
1002 prefetch(skb->data - NET_IP_ALIGN);
1003 pci_unmap_single(enic->pdev, buf->dma_addr,
1004 buf->len, PCI_DMA_FROMDEVICE);
1005
1006 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1007 &type, &color, &q_number, &completed_index,
1008 &ingress_port, &fcoe, &eop, &sop, &rss_type,
1009 &csum_not_calc, &rss_hash, &bytes_written,
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001010 &packet_error, &vlan_stripped, &vlan_tci, &checksum,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001011 &fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1012 &fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1013 &ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1014 &fcs_ok);
1015
1016 if (packet_error) {
1017
Scott Feldman350991e2009-09-03 17:02:19 +00001018 if (!fcs_ok) {
1019 if (bytes_written > 0)
1020 enic->rq_bad_fcs++;
1021 else if (bytes_written == 0)
1022 enic->rq_truncated_pkts++;
1023 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001024
1025 dev_kfree_skb_any(skb);
1026
1027 return;
1028 }
1029
1030 if (eop && bytes_written > 0) {
1031
1032 /* Good receive
1033 */
1034
1035 skb_put(skb, bytes_written);
Scott Feldman86ca9db2008-11-21 21:26:55 -08001036 skb->protocol = eth_type_trans(skb, netdev);
govindarajulu.vbf751ba2013-09-04 11:17:15 +05301037 skb_record_rx_queue(skb, q_number);
1038 if (netdev->features & NETIF_F_RXHASH) {
Tom Herbert3739acd2013-12-17 23:23:42 -08001039 skb_set_hash(skb, rss_hash,
1040 (rss_type &
1041 (NIC_CFG_RSS_HASH_TYPE_TCP_IPV6_EX |
1042 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6 |
1043 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4)) ?
1044 PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3);
govindarajulu.vbf751ba2013-09-04 11:17:15 +05301045 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001046
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00001047 if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001048 skb->csum = htons(checksum);
1049 skb->ip_summed = CHECKSUM_COMPLETE;
1050 }
1051
Jiri Pirko6ede7462011-07-20 04:54:18 +00001052 if (vlan_stripped)
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001053 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tci);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001054
Jiri Pirko6ede7462011-07-20 04:54:18 +00001055 if (netdev->features & NETIF_F_GRO)
1056 napi_gro_receive(&enic->napi[q_number], skb);
1057 else
1058 netif_receive_skb(skb);
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05301059 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1060 enic_intr_update_pkt_size(&cq->pkt_size_counter,
1061 bytes_written);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001062 } else {
1063
1064 /* Buffer overflow
1065 */
1066
1067 dev_kfree_skb_any(skb);
1068 }
1069}
1070
1071static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1072 u8 type, u16 q_number, u16 completed_index, void *opaque)
1073{
1074 struct enic *enic = vnic_dev_priv(vdev);
1075
1076 vnic_rq_service(&enic->rq[q_number], cq_desc,
1077 completed_index, VNIC_RQ_RETURN_DESC,
1078 enic_rq_indicate_buf, opaque);
1079
1080 return 0;
1081}
1082
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001083static int enic_poll(struct napi_struct *napi, int budget)
1084{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001085 struct net_device *netdev = napi->dev;
1086 struct enic *enic = netdev_priv(netdev);
1087 unsigned int cq_rq = enic_cq_rq(enic, 0);
1088 unsigned int cq_wq = enic_cq_wq(enic, 0);
1089 unsigned int intr = enic_legacy_io_intr();
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001090 unsigned int rq_work_to_do = budget;
1091 unsigned int wq_work_to_do = -1; /* no limit */
Eric W. Biederman4c502542014-03-14 18:02:08 -07001092 unsigned int work_done, rq_work_done = 0, wq_work_done;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001093 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001094
1095 /* Service RQ (first) and WQ
1096 */
1097
Eric W. Biederman4c502542014-03-14 18:02:08 -07001098 if (budget > 0)
1099 rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1100 rq_work_to_do, enic_rq_service, NULL);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001101
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001102 wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001103 wq_work_to_do, enic_wq_service, NULL);
1104
1105 /* Accumulate intr event credits for this polling
1106 * cycle. An intr event is the completion of a
1107 * a WQ or RQ packet.
1108 */
1109
1110 work_done = rq_work_done + wq_work_done;
1111
1112 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001113 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001114 work_done,
1115 0 /* don't unmask intr */,
1116 0 /* don't reset intr timer */);
1117
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001118 err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001119
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001120 /* Buffer allocation failed. Stay in polling
1121 * mode so we can try to fill the ring again.
1122 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001123
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001124 if (err)
1125 rq_work_done = rq_work_to_do;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001126
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001127 if (rq_work_done < rq_work_to_do) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001128
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001129 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001130 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001131 */
1132
Ben Hutchings288379f2009-01-19 16:43:59 -08001133 napi_complete(napi);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001134 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001135 }
1136
1137 return rq_work_done;
1138}
1139
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05301140static void enic_set_int_moderation(struct enic *enic, struct vnic_rq *rq)
1141{
1142 unsigned int intr = enic_msix_rq_intr(enic, rq->index);
1143 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1144 u32 timer = cq->tobe_rx_coal_timeval;
1145
1146 if (cq->tobe_rx_coal_timeval != cq->cur_rx_coal_timeval) {
1147 vnic_intr_coalescing_timer_set(&enic->intr[intr], timer);
1148 cq->cur_rx_coal_timeval = cq->tobe_rx_coal_timeval;
1149 }
1150}
1151
1152static void enic_calc_int_moderation(struct enic *enic, struct vnic_rq *rq)
1153{
1154 struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1155 struct vnic_cq *cq = &enic->cq[enic_cq_rq(enic, rq->index)];
1156 struct vnic_rx_bytes_counter *pkt_size_counter = &cq->pkt_size_counter;
1157 int index;
1158 u32 timer;
1159 u32 range_start;
1160 u32 traffic;
1161 u64 delta;
1162 ktime_t now = ktime_get();
1163
1164 delta = ktime_us_delta(now, cq->prev_ts);
1165 if (delta < ENIC_AIC_TS_BREAK)
1166 return;
1167 cq->prev_ts = now;
1168
1169 traffic = pkt_size_counter->large_pkt_bytes_cnt +
1170 pkt_size_counter->small_pkt_bytes_cnt;
1171 /* The table takes Mbps
1172 * traffic *= 8 => bits
1173 * traffic *= (10^6 / delta) => bps
1174 * traffic /= 10^6 => Mbps
1175 *
1176 * Combining, traffic *= (8 / delta)
1177 */
1178
1179 traffic <<= 3;
Govindarajulu Varadarajan958c4922014-05-26 15:52:43 +05301180 traffic = delta > UINT_MAX ? 0 : traffic / (u32)delta;
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05301181
1182 for (index = 0; index < ENIC_MAX_COALESCE_TIMERS; index++)
1183 if (traffic < mod_table[index].rx_rate)
1184 break;
1185 range_start = (pkt_size_counter->small_pkt_bytes_cnt >
1186 pkt_size_counter->large_pkt_bytes_cnt << 1) ?
1187 rx_coal->small_pkt_range_start :
1188 rx_coal->large_pkt_range_start;
1189 timer = range_start + ((rx_coal->range_end - range_start) *
1190 mod_table[index].range_percent / 100);
1191 /* Damping */
1192 cq->tobe_rx_coal_timeval = (timer + cq->tobe_rx_coal_timeval) >> 1;
1193
1194 pkt_size_counter->large_pkt_bytes_cnt = 0;
1195 pkt_size_counter->small_pkt_bytes_cnt = 0;
1196}
1197
Govindarajulu Varadarajanb6e97c12014-06-23 16:08:01 +05301198#ifdef CONFIG_RFS_ACCEL
1199static void enic_free_rx_cpu_rmap(struct enic *enic)
1200{
1201 free_irq_cpu_rmap(enic->netdev->rx_cpu_rmap);
1202 enic->netdev->rx_cpu_rmap = NULL;
1203}
1204
1205static void enic_set_rx_cpu_rmap(struct enic *enic)
1206{
1207 int i, res;
1208
1209 if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) {
1210 enic->netdev->rx_cpu_rmap = alloc_irq_cpu_rmap(enic->rq_count);
1211 if (unlikely(!enic->netdev->rx_cpu_rmap))
1212 return;
1213 for (i = 0; i < enic->rq_count; i++) {
1214 res = irq_cpu_rmap_add(enic->netdev->rx_cpu_rmap,
1215 enic->msix_entry[i].vector);
1216 if (unlikely(res)) {
1217 enic_free_rx_cpu_rmap(enic);
1218 return;
1219 }
1220 }
1221 }
1222}
1223
1224#else
1225
1226static void enic_free_rx_cpu_rmap(struct enic *enic)
1227{
1228}
1229
1230static void enic_set_rx_cpu_rmap(struct enic *enic)
1231{
1232}
1233
1234#endif /* CONFIG_RFS_ACCEL */
1235
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001236static int enic_poll_msix(struct napi_struct *napi, int budget)
1237{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001238 struct net_device *netdev = napi->dev;
1239 struct enic *enic = netdev_priv(netdev);
1240 unsigned int rq = (napi - &enic->napi[0]);
1241 unsigned int cq = enic_cq_rq(enic, rq);
1242 unsigned int intr = enic_msix_rq_intr(enic, rq);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001243 unsigned int work_to_do = budget;
Eric W. Biederman4c502542014-03-14 18:02:08 -07001244 unsigned int work_done = 0;
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001245 int err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001246
1247 /* Service RQ
1248 */
1249
Eric W. Biederman4c502542014-03-14 18:02:08 -07001250 if (budget > 0)
1251 work_done = vnic_cq_service(&enic->cq[cq],
1252 work_to_do, enic_rq_service, NULL);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001253
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001254 /* Return intr event credits for this polling
1255 * cycle. An intr event is the completion of a
1256 * RQ packet.
1257 */
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001258
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001259 if (work_done > 0)
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001260 vnic_intr_return_credits(&enic->intr[intr],
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001261 work_done,
1262 0 /* don't unmask intr */,
1263 0 /* don't reset intr timer */);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001264
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001265 err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001266
1267 /* Buffer allocation failed. Stay in polling mode
1268 * so we can try to fill the ring again.
1269 */
1270
1271 if (err)
1272 work_done = work_to_do;
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05301273 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1274 /* Call the function which refreshes
1275 * the intr coalescing timer value based on
1276 * the traffic. This is supported only in
1277 * the case of MSI-x mode
1278 */
1279 enic_calc_int_moderation(enic, &enic->rq[rq]);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001280
1281 if (work_done < work_to_do) {
1282
1283 /* Some work done, but not enough to stay in polling,
Vasanthy Kolluri88132f52010-06-24 10:49:25 +00001284 * exit polling
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001285 */
1286
Ben Hutchings288379f2009-01-19 16:43:59 -08001287 napi_complete(napi);
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05301288 if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce)
1289 enic_set_int_moderation(enic, &enic->rq[rq]);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001290 vnic_intr_unmask(&enic->intr[intr]);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001291 }
1292
1293 return work_done;
1294}
1295
1296static void enic_notify_timer(unsigned long data)
1297{
1298 struct enic *enic = (struct enic *)data;
1299
1300 enic_notify_check(enic);
1301
Scott Feldman25f0a062008-09-24 11:23:32 -07001302 mod_timer(&enic->notify_timer,
1303 round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001304}
1305
1306static void enic_free_intr(struct enic *enic)
1307{
1308 struct net_device *netdev = enic->netdev;
1309 unsigned int i;
1310
Govindarajulu Varadarajanb6e97c12014-06-23 16:08:01 +05301311 enic_free_rx_cpu_rmap(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001312 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1313 case VNIC_DEV_INTR_MODE_INTX:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001314 free_irq(enic->pdev->irq, netdev);
1315 break;
Scott Feldman8f4d2482008-09-24 11:23:42 -07001316 case VNIC_DEV_INTR_MODE_MSI:
1317 free_irq(enic->pdev->irq, enic);
1318 break;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001319 case VNIC_DEV_INTR_MODE_MSIX:
1320 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1321 if (enic->msix[i].requested)
1322 free_irq(enic->msix_entry[i].vector,
1323 enic->msix[i].devid);
1324 break;
1325 default:
1326 break;
1327 }
1328}
1329
1330static int enic_request_intr(struct enic *enic)
1331{
1332 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001333 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001334 int err = 0;
1335
Govindarajulu Varadarajanb6e97c12014-06-23 16:08:01 +05301336 enic_set_rx_cpu_rmap(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001337 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1338
1339 case VNIC_DEV_INTR_MODE_INTX:
1340
1341 err = request_irq(enic->pdev->irq, enic_isr_legacy,
1342 IRQF_SHARED, netdev->name, netdev);
1343 break;
1344
1345 case VNIC_DEV_INTR_MODE_MSI:
1346
1347 err = request_irq(enic->pdev->irq, enic_isr_msi,
1348 0, netdev->name, enic);
1349 break;
1350
1351 case VNIC_DEV_INTR_MODE_MSIX:
1352
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001353 for (i = 0; i < enic->rq_count; i++) {
1354 intr = enic_msix_rq_intr(enic, i);
Dan Carpenter4505f402013-01-17 21:46:18 +00001355 snprintf(enic->msix[intr].devname,
1356 sizeof(enic->msix[intr].devname),
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001357 "%.11s-rx-%d", netdev->name, i);
1358 enic->msix[intr].isr = enic_isr_msix_rq;
1359 enic->msix[intr].devid = &enic->napi[i];
1360 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001361
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001362 for (i = 0; i < enic->wq_count; i++) {
1363 intr = enic_msix_wq_intr(enic, i);
Dan Carpenter4505f402013-01-17 21:46:18 +00001364 snprintf(enic->msix[intr].devname,
1365 sizeof(enic->msix[intr].devname),
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001366 "%.11s-tx-%d", netdev->name, i);
1367 enic->msix[intr].isr = enic_isr_msix_wq;
1368 enic->msix[intr].devid = enic;
1369 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001370
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001371 intr = enic_msix_err_intr(enic);
Dan Carpenter4505f402013-01-17 21:46:18 +00001372 snprintf(enic->msix[intr].devname,
1373 sizeof(enic->msix[intr].devname),
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001374 "%.11s-err", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001375 enic->msix[intr].isr = enic_isr_msix_err;
1376 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001377
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001378 intr = enic_msix_notify_intr(enic);
Dan Carpenter4505f402013-01-17 21:46:18 +00001379 snprintf(enic->msix[intr].devname,
1380 sizeof(enic->msix[intr].devname),
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001381 "%.11s-notify", netdev->name);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001382 enic->msix[intr].isr = enic_isr_msix_notify;
1383 enic->msix[intr].devid = enic;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001384
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001385 for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1386 enic->msix[i].requested = 0;
1387
1388 for (i = 0; i < enic->intr_count; i++) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001389 err = request_irq(enic->msix_entry[i].vector,
1390 enic->msix[i].isr, 0,
1391 enic->msix[i].devname,
1392 enic->msix[i].devid);
1393 if (err) {
1394 enic_free_intr(enic);
1395 break;
1396 }
1397 enic->msix[i].requested = 1;
1398 }
1399
1400 break;
1401
1402 default:
1403 break;
1404 }
1405
1406 return err;
1407}
1408
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001409static void enic_synchronize_irqs(struct enic *enic)
1410{
1411 unsigned int i;
1412
1413 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1414 case VNIC_DEV_INTR_MODE_INTX:
1415 case VNIC_DEV_INTR_MODE_MSI:
1416 synchronize_irq(enic->pdev->irq);
1417 break;
1418 case VNIC_DEV_INTR_MODE_MSIX:
1419 for (i = 0; i < enic->intr_count; i++)
1420 synchronize_irq(enic->msix_entry[i].vector);
1421 break;
1422 default:
1423 break;
1424 }
1425}
1426
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05301427static void enic_set_rx_coal_setting(struct enic *enic)
1428{
1429 unsigned int speed;
1430 int index = -1;
1431 struct enic_rx_coal *rx_coal = &enic->rx_coalesce_setting;
1432
1433 /* If intr mode is not MSIX, do not do adaptive coalescing */
1434 if (VNIC_DEV_INTR_MODE_MSIX != vnic_dev_get_intr_mode(enic->vdev)) {
1435 netdev_info(enic->netdev, "INTR mode is not MSIX, Not initializing adaptive coalescing");
1436 return;
1437 }
1438
1439 /* 1. Read the link speed from fw
1440 * 2. Pick the default range for the speed
1441 * 3. Update it in enic->rx_coalesce_setting
1442 */
1443 speed = vnic_dev_port_speed(enic->vdev);
1444 if (ENIC_LINK_SPEED_10G < speed)
1445 index = ENIC_LINK_40G_INDEX;
1446 else if (ENIC_LINK_SPEED_4G < speed)
1447 index = ENIC_LINK_10G_INDEX;
1448 else
1449 index = ENIC_LINK_4G_INDEX;
1450
1451 rx_coal->small_pkt_range_start = mod_range[index].small_pkt_range_start;
1452 rx_coal->large_pkt_range_start = mod_range[index].large_pkt_range_start;
1453 rx_coal->range_end = ENIC_RX_COALESCE_RANGE_END;
1454
1455 /* Start with the value provided by UCSM */
1456 for (index = 0; index < enic->rq_count; index++)
1457 enic->cq[index].cur_rx_coal_timeval =
1458 enic->config.intr_timer_usec;
1459
1460 rx_coal->use_adaptive_rx_coalesce = 1;
1461}
1462
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001463static int enic_dev_notify_set(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001464{
1465 int err;
1466
Scott Feldman56ac88b2009-09-03 17:02:14 +00001467 spin_lock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001468 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1469 case VNIC_DEV_INTR_MODE_INTX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001470 err = vnic_dev_notify_set(enic->vdev,
1471 enic_legacy_notify_intr());
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001472 break;
1473 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001474 err = vnic_dev_notify_set(enic->vdev,
1475 enic_msix_notify_intr(enic));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001476 break;
1477 default:
1478 err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1479 break;
1480 }
Scott Feldman56ac88b2009-09-03 17:02:14 +00001481 spin_unlock(&enic->devcmd_lock);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001482
1483 return err;
1484}
1485
1486static void enic_notify_timer_start(struct enic *enic)
1487{
1488 switch (vnic_dev_get_intr_mode(enic->vdev)) {
1489 case VNIC_DEV_INTR_MODE_MSI:
1490 mod_timer(&enic->notify_timer, jiffies);
1491 break;
1492 default:
1493 /* Using intr for notification for INTx/MSI-X */
1494 break;
Joe Perches6403eab2011-06-03 11:51:20 +00001495 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001496}
1497
1498/* rtnl lock is held, process context */
1499static int enic_open(struct net_device *netdev)
1500{
1501 struct enic *enic = netdev_priv(netdev);
1502 unsigned int i;
1503 int err;
1504
Scott Feldman4b75a442008-09-24 11:23:53 -07001505 err = enic_request_intr(enic);
1506 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001507 netdev_err(netdev, "Unable to request irq.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001508 return err;
1509 }
1510
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001511 err = enic_dev_notify_set(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001512 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001513 netdev_err(netdev,
1514 "Failed to alloc notify buffer, aborting.\n");
Scott Feldman4b75a442008-09-24 11:23:53 -07001515 goto err_out_free_intr;
1516 }
1517
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001518 for (i = 0; i < enic->rq_count; i++) {
Vasanthy Kolluri0eb26022011-02-04 16:17:21 +00001519 vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001520 /* Need at least one buffer on ring to get going */
1521 if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001522 netdev_err(netdev, "Unable to alloc receive buffers\n");
Scott Feldman2d6ddce2009-12-23 13:27:38 +00001523 err = -ENOMEM;
Scott Feldman4b75a442008-09-24 11:23:53 -07001524 goto err_out_notify_unset;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001525 }
1526 }
1527
1528 for (i = 0; i < enic->wq_count; i++)
1529 vnic_wq_enable(&enic->wq[i]);
1530 for (i = 0; i < enic->rq_count; i++)
1531 vnic_rq_enable(&enic->rq[i]);
1532
Roopa Prabhu73359032012-01-18 04:24:02 +00001533 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
Roopa Prabhu296390592010-12-08 13:54:03 +00001534 enic_dev_add_station_addr(enic);
Roopa Prabhu3f192792011-09-22 03:44:43 +00001535
Roopa Prabhu319d7e82010-12-08 13:19:58 +00001536 enic_set_rx_mode(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001537
govindarajulu.v822473b2013-09-04 11:17:14 +05301538 netif_tx_wake_all_queues(netdev);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001539
1540 for (i = 0; i < enic->rq_count; i++)
1541 napi_enable(&enic->napi[i]);
1542
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001543 enic_dev_enable(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001544
1545 for (i = 0; i < enic->intr_count; i++)
1546 vnic_intr_unmask(&enic->intr[i]);
1547
1548 enic_notify_timer_start(enic);
1549
1550 return 0;
Scott Feldman4b75a442008-09-24 11:23:53 -07001551
1552err_out_notify_unset:
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001553 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001554err_out_free_intr:
1555 enic_free_intr(enic);
1556
1557 return err;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001558}
1559
1560/* rtnl lock is held, process context */
1561static int enic_stop(struct net_device *netdev)
1562{
1563 struct enic *enic = netdev_priv(netdev);
1564 unsigned int i;
1565 int err;
1566
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001567 for (i = 0; i < enic->intr_count; i++) {
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001568 vnic_intr_mask(&enic->intr[i]);
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00001569 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1570 }
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001571
1572 enic_synchronize_irqs(enic);
1573
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001574 del_timer_sync(&enic->notify_timer);
1575
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001576 enic_dev_disable(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001577
1578 for (i = 0; i < enic->rq_count; i++)
1579 napi_disable(&enic->napi[i]);
1580
Scott Feldmanb3d18d12009-12-23 13:27:30 +00001581 netif_carrier_off(netdev);
1582 netif_tx_disable(netdev);
Roopa Prabhu3f192792011-09-22 03:44:43 +00001583
Roopa Prabhu73359032012-01-18 04:24:02 +00001584 if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
Roopa Prabhu296390592010-12-08 13:54:03 +00001585 enic_dev_del_station_addr(enic);
Scott Feldmanf8bd9092010-05-17 22:50:19 -07001586
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001587 for (i = 0; i < enic->wq_count; i++) {
1588 err = vnic_wq_disable(&enic->wq[i]);
1589 if (err)
1590 return err;
1591 }
1592 for (i = 0; i < enic->rq_count; i++) {
1593 err = vnic_rq_disable(&enic->rq[i]);
1594 if (err)
1595 return err;
1596 }
1597
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001598 enic_dev_notify_unset(enic);
Scott Feldman4b75a442008-09-24 11:23:53 -07001599 enic_free_intr(enic);
1600
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001601 for (i = 0; i < enic->wq_count; i++)
1602 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1603 for (i = 0; i < enic->rq_count; i++)
1604 vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1605 for (i = 0; i < enic->cq_count; i++)
1606 vnic_cq_clean(&enic->cq[i]);
1607 for (i = 0; i < enic->intr_count; i++)
1608 vnic_intr_clean(&enic->intr[i]);
1609
1610 return 0;
1611}
1612
1613static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1614{
1615 struct enic *enic = netdev_priv(netdev);
1616 int running = netif_running(netdev);
1617
Scott Feldman25f0a062008-09-24 11:23:32 -07001618 if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1619 return -EINVAL;
1620
Roopa Prabhu73359032012-01-18 04:24:02 +00001621 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001622 return -EOPNOTSUPP;
1623
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001624 if (running)
1625 enic_stop(netdev);
1626
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001627 netdev->mtu = new_mtu;
1628
1629 if (netdev->mtu > enic->port_mtu)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001630 netdev_warn(netdev,
1631 "interface MTU (%d) set higher than port MTU (%d)\n",
1632 netdev->mtu, enic->port_mtu);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001633
1634 if (running)
1635 enic_open(netdev);
1636
1637 return 0;
1638}
1639
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001640static void enic_change_mtu_work(struct work_struct *work)
1641{
1642 struct enic *enic = container_of(work, struct enic, change_mtu_work);
1643 struct net_device *netdev = enic->netdev;
1644 int new_mtu = vnic_dev_mtu(enic->vdev);
1645 int err;
1646 unsigned int i;
1647
1648 new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1649
1650 rtnl_lock();
1651
1652 /* Stop RQ */
1653 del_timer_sync(&enic->notify_timer);
1654
1655 for (i = 0; i < enic->rq_count; i++)
1656 napi_disable(&enic->napi[i]);
1657
1658 vnic_intr_mask(&enic->intr[0]);
1659 enic_synchronize_irqs(enic);
1660 err = vnic_rq_disable(&enic->rq[0]);
1661 if (err) {
Konstantin Khlebnikove0575902013-07-08 11:22:51 +04001662 rtnl_unlock();
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001663 netdev_err(netdev, "Unable to disable RQ.\n");
1664 return;
1665 }
1666 vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1667 vnic_cq_clean(&enic->cq[0]);
1668 vnic_intr_clean(&enic->intr[0]);
1669
1670 /* Fill RQ with new_mtu-sized buffers */
1671 netdev->mtu = new_mtu;
1672 vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1673 /* Need at least one buffer on ring to get going */
1674 if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
Konstantin Khlebnikove0575902013-07-08 11:22:51 +04001675 rtnl_unlock();
Roopa Prabhuc97c8942011-06-03 14:35:17 +00001676 netdev_err(netdev, "Unable to alloc receive buffers.\n");
1677 return;
1678 }
1679
1680 /* Start RQ */
1681 vnic_rq_enable(&enic->rq[0]);
1682 napi_enable(&enic->napi[0]);
1683 vnic_intr_unmask(&enic->intr[0]);
1684 enic_notify_timer_start(enic);
1685
1686 rtnl_unlock();
1687
1688 netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1689}
1690
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001691#ifdef CONFIG_NET_POLL_CONTROLLER
1692static void enic_poll_controller(struct net_device *netdev)
1693{
1694 struct enic *enic = netdev_priv(netdev);
1695 struct vnic_dev *vdev = enic->vdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001696 unsigned int i, intr;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001697
1698 switch (vnic_dev_get_intr_mode(vdev)) {
1699 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001700 for (i = 0; i < enic->rq_count; i++) {
1701 intr = enic_msix_rq_intr(enic, i);
Vasanthy Kolluri79aeec52010-12-08 13:05:45 +00001702 enic_isr_msix_rq(enic->msix_entry[intr].vector,
1703 &enic->napi[i]);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001704 }
Vasanthy Kollurib880a952011-06-09 10:37:07 +00001705
1706 for (i = 0; i < enic->wq_count; i++) {
1707 intr = enic_msix_wq_intr(enic, i);
1708 enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1709 }
1710
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001711 break;
1712 case VNIC_DEV_INTR_MODE_MSI:
1713 enic_isr_msi(enic->pdev->irq, enic);
1714 break;
1715 case VNIC_DEV_INTR_MODE_INTX:
1716 enic_isr_legacy(enic->pdev->irq, netdev);
1717 break;
1718 default:
1719 break;
1720 }
1721}
1722#endif
1723
1724static int enic_dev_wait(struct vnic_dev *vdev,
1725 int (*start)(struct vnic_dev *, int),
1726 int (*finished)(struct vnic_dev *, int *),
1727 int arg)
1728{
1729 unsigned long time;
1730 int done;
1731 int err;
1732
1733 BUG_ON(in_interrupt());
1734
1735 err = start(vdev, arg);
1736 if (err)
1737 return err;
1738
1739 /* Wait for func to complete...2 seconds max
1740 */
1741
1742 time = jiffies + (HZ * 2);
1743 do {
1744
1745 err = finished(vdev, &done);
1746 if (err)
1747 return err;
1748
1749 if (done)
1750 return 0;
1751
1752 schedule_timeout_uninterruptible(HZ / 10);
1753
1754 } while (time_after(time, jiffies));
1755
1756 return -ETIMEDOUT;
1757}
1758
1759static int enic_dev_open(struct enic *enic)
1760{
1761 int err;
1762
1763 err = enic_dev_wait(enic->vdev, vnic_dev_open,
1764 vnic_dev_open_done, 0);
1765 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001766 dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1767 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001768
1769 return err;
1770}
1771
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001772static int enic_dev_hang_reset(struct enic *enic)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001773{
1774 int err;
1775
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001776 err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1777 vnic_dev_hang_reset_done, 0);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001778 if (err)
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00001779 netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1780 err);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001781
1782 return err;
1783}
1784
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001785static int enic_set_rsskey(struct enic *enic)
Scott Feldman68f71702009-02-09 23:24:24 -08001786{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001787 dma_addr_t rss_key_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001788 union vnic_rss_key *rss_key_buf_va = NULL;
1789 union vnic_rss_key rss_key = {
1790 .key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1791 .key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1792 .key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1793 .key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1794 };
1795 int err;
1796
1797 rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1798 sizeof(union vnic_rss_key), &rss_key_buf_pa);
1799 if (!rss_key_buf_va)
1800 return -ENOMEM;
1801
1802 memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1803
1804 spin_lock(&enic->devcmd_lock);
1805 err = enic_set_rss_key(enic,
1806 rss_key_buf_pa,
1807 sizeof(union vnic_rss_key));
1808 spin_unlock(&enic->devcmd_lock);
1809
1810 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1811 rss_key_buf_va, rss_key_buf_pa);
1812
1813 return err;
1814}
1815
1816static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1817{
Vasanthy Kolluri1f4f0672010-11-15 08:09:55 +00001818 dma_addr_t rss_cpu_buf_pa;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001819 union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1820 unsigned int i;
1821 int err;
1822
1823 rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1824 sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1825 if (!rss_cpu_buf_va)
1826 return -ENOMEM;
1827
1828 for (i = 0; i < (1 << rss_hash_bits); i++)
1829 (*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1830
1831 spin_lock(&enic->devcmd_lock);
1832 err = enic_set_rss_cpu(enic,
1833 rss_cpu_buf_pa,
1834 sizeof(union vnic_rss_cpu));
1835 spin_unlock(&enic->devcmd_lock);
1836
1837 pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1838 rss_cpu_buf_va, rss_cpu_buf_pa);
1839
1840 return err;
1841}
1842
1843static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1844 u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1845{
Scott Feldman68f71702009-02-09 23:24:24 -08001846 const u8 tso_ipid_split_en = 0;
1847 const u8 ig_vlan_strip_en = 1;
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001848 int err;
Scott Feldman68f71702009-02-09 23:24:24 -08001849
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001850 /* Enable VLAN tag stripping.
1851 */
Scott Feldman68f71702009-02-09 23:24:24 -08001852
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001853 spin_lock(&enic->devcmd_lock);
1854 err = enic_set_nic_cfg(enic,
Scott Feldman68f71702009-02-09 23:24:24 -08001855 rss_default_cpu, rss_hash_type,
1856 rss_hash_bits, rss_base_cpu,
1857 rss_enable, tso_ipid_split_en,
1858 ig_vlan_strip_en);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001859 spin_unlock(&enic->devcmd_lock);
1860
1861 return err;
1862}
1863
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001864static int enic_set_rss_nic_cfg(struct enic *enic)
1865{
1866 struct device *dev = enic_get_dev(enic);
1867 const u8 rss_default_cpu = 0;
1868 const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1869 NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1870 NIC_CFG_RSS_HASH_TYPE_IPV6 |
1871 NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1872 const u8 rss_hash_bits = 7;
1873 const u8 rss_base_cpu = 0;
1874 u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1875
1876 if (rss_enable) {
1877 if (!enic_set_rsskey(enic)) {
1878 if (enic_set_rsscpu(enic, rss_hash_bits)) {
1879 rss_enable = 0;
1880 dev_warn(dev, "RSS disabled, "
1881 "Failed to set RSS cpu indirection table.");
1882 }
1883 } else {
1884 rss_enable = 0;
1885 dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1886 }
1887 }
1888
1889 return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1890 rss_hash_bits, rss_base_cpu, rss_enable);
1891}
1892
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001893static void enic_reset(struct work_struct *work)
1894{
1895 struct enic *enic = container_of(work, struct enic, reset);
1896
1897 if (!netif_running(enic->netdev))
1898 return;
1899
1900 rtnl_lock();
1901
Neel Patel0b038562013-08-16 15:47:40 -07001902 spin_lock(&enic->enic_api_lock);
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00001903 enic_dev_hang_notify(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001904 enic_stop(enic->netdev);
Vasanthy Kolluri99ef5632010-06-24 10:50:00 +00001905 enic_dev_hang_reset(enic);
Vasanthy Kollurie0afe532011-02-17 08:53:12 +00001906 enic_reset_addr_lists(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001907 enic_init_vnic_resources(enic);
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001908 enic_set_rss_nic_cfg(enic);
Vasanthy Kollurif8cac142010-06-24 10:49:51 +00001909 enic_dev_set_ig_vlan_rewrite_mode(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001910 enic_open(enic->netdev);
Neel Patel0b038562013-08-16 15:47:40 -07001911 spin_unlock(&enic->enic_api_lock);
Neel Pateld765bb42013-08-16 15:47:41 -07001912 call_netdevice_notifiers(NETDEV_REBOOT, enic->netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001913
1914 rtnl_unlock();
1915}
1916
1917static int enic_set_intr_mode(struct enic *enic)
1918{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001919 unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
Vasanthy Kolluri1cbb1a62011-02-17 13:57:19 +00001920 unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001921 unsigned int i;
1922
1923 /* Set interrupt mode (INTx, MSI, MSI-X) depending
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001924 * on system capabilities.
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001925 *
1926 * Try MSI-X first
1927 *
1928 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
1929 * (the second to last INTR is used for WQ/RQ errors)
1930 * (the last INTR is used for notifications)
1931 */
1932
1933 BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
1934 for (i = 0; i < n + m + 2; i++)
1935 enic->msix_entry[i].entry = i;
1936
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001937 /* Use multiple RQs if RSS is enabled
1938 */
1939
1940 if (ENIC_SETTING(enic, RSS) &&
1941 enic->config.intr_mode < 1 &&
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001942 enic->rq_count >= n &&
1943 enic->wq_count >= m &&
1944 enic->cq_count >= n + m &&
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001945 enic->intr_count >= n + m + 2) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001946
Alexander Gordeevabbb6a32014-02-18 11:08:02 +01001947 if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
1948 n + m + 2, n + m + 2) > 0) {
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001949
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001950 enic->rq_count = n;
1951 enic->wq_count = m;
1952 enic->cq_count = n + m;
1953 enic->intr_count = n + m + 2;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001954
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001955 vnic_dev_set_intr_mode(enic->vdev,
1956 VNIC_DEV_INTR_MODE_MSIX);
1957
1958 return 0;
1959 }
1960 }
1961
1962 if (enic->config.intr_mode < 1 &&
1963 enic->rq_count >= 1 &&
1964 enic->wq_count >= m &&
1965 enic->cq_count >= 1 + m &&
1966 enic->intr_count >= 1 + m + 2) {
Alexander Gordeevabbb6a32014-02-18 11:08:02 +01001967 if (pci_enable_msix_range(enic->pdev, enic->msix_entry,
1968 1 + m + 2, 1 + m + 2) > 0) {
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00001969
1970 enic->rq_count = 1;
1971 enic->wq_count = m;
1972 enic->cq_count = 1 + m;
1973 enic->intr_count = 1 + m + 2;
1974
1975 vnic_dev_set_intr_mode(enic->vdev,
1976 VNIC_DEV_INTR_MODE_MSIX);
1977
1978 return 0;
1979 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07001980 }
1981
1982 /* Next try MSI
1983 *
1984 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
1985 */
1986
1987 if (enic->config.intr_mode < 2 &&
1988 enic->rq_count >= 1 &&
1989 enic->wq_count >= 1 &&
1990 enic->cq_count >= 2 &&
1991 enic->intr_count >= 1 &&
1992 !pci_enable_msi(enic->pdev)) {
1993
1994 enic->rq_count = 1;
1995 enic->wq_count = 1;
1996 enic->cq_count = 2;
1997 enic->intr_count = 1;
1998
1999 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2000
2001 return 0;
2002 }
2003
2004 /* Next try INTx
2005 *
2006 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2007 * (the first INTR is used for WQ/RQ)
2008 * (the second INTR is used for WQ/RQ errors)
2009 * (the last INTR is used for notifications)
2010 */
2011
2012 if (enic->config.intr_mode < 3 &&
2013 enic->rq_count >= 1 &&
2014 enic->wq_count >= 1 &&
2015 enic->cq_count >= 2 &&
2016 enic->intr_count >= 3) {
2017
2018 enic->rq_count = 1;
2019 enic->wq_count = 1;
2020 enic->cq_count = 2;
2021 enic->intr_count = 3;
2022
2023 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2024
2025 return 0;
2026 }
2027
2028 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2029
2030 return -EINVAL;
2031}
2032
2033static void enic_clear_intr_mode(struct enic *enic)
2034{
2035 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2036 case VNIC_DEV_INTR_MODE_MSIX:
2037 pci_disable_msix(enic->pdev);
2038 break;
2039 case VNIC_DEV_INTR_MODE_MSI:
2040 pci_disable_msi(enic->pdev);
2041 break;
2042 default:
2043 break;
2044 }
2045
2046 vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2047}
2048
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002049static const struct net_device_ops enic_netdev_dynamic_ops = {
2050 .ndo_open = enic_open,
2051 .ndo_stop = enic_stop,
2052 .ndo_start_xmit = enic_hard_start_xmit,
stephen hemmingerf20530b2011-06-08 14:54:02 +00002053 .ndo_get_stats64 = enic_get_stats,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002054 .ndo_validate_addr = eth_validate_addr,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002055 .ndo_set_rx_mode = enic_set_rx_mode,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002056 .ndo_set_mac_address = enic_set_mac_address_dynamic,
2057 .ndo_change_mtu = enic_change_mtu,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002058 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2059 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2060 .ndo_tx_timeout = enic_tx_timeout,
2061 .ndo_set_vf_port = enic_set_vf_port,
2062 .ndo_get_vf_port = enic_get_vf_port,
Roopa Prabhu0b1c00f2010-12-08 13:53:58 +00002063 .ndo_set_vf_mac = enic_set_vf_mac,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002064#ifdef CONFIG_NET_POLL_CONTROLLER
2065 .ndo_poll_controller = enic_poll_controller,
2066#endif
2067};
2068
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002069static const struct net_device_ops enic_netdev_ops = {
2070 .ndo_open = enic_open,
2071 .ndo_stop = enic_stop,
Stephen Hemminger00829822008-11-20 20:14:53 -08002072 .ndo_start_xmit = enic_hard_start_xmit,
stephen hemmingerf20530b2011-06-08 14:54:02 +00002073 .ndo_get_stats64 = enic_get_stats,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002074 .ndo_validate_addr = eth_validate_addr,
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002075 .ndo_set_mac_address = enic_set_mac_address,
Roopa Prabhu319d7e82010-12-08 13:19:58 +00002076 .ndo_set_rx_mode = enic_set_rx_mode,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002077 .ndo_change_mtu = enic_change_mtu,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002078 .ndo_vlan_rx_add_vid = enic_vlan_rx_add_vid,
2079 .ndo_vlan_rx_kill_vid = enic_vlan_rx_kill_vid,
2080 .ndo_tx_timeout = enic_tx_timeout,
Roopa Prabhu3f192792011-09-22 03:44:43 +00002081 .ndo_set_vf_port = enic_set_vf_port,
2082 .ndo_get_vf_port = enic_get_vf_port,
2083 .ndo_set_vf_mac = enic_set_vf_mac,
Stephen Hemmingerafe29f72008-11-19 22:23:26 -08002084#ifdef CONFIG_NET_POLL_CONTROLLER
2085 .ndo_poll_controller = enic_poll_controller,
2086#endif
2087};
2088
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002089static void enic_dev_deinit(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002090{
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002091 unsigned int i;
2092
2093 for (i = 0; i < enic->rq_count; i++)
2094 netif_napi_del(&enic->napi[i]);
2095
Scott Feldman6fdfa972009-09-03 17:02:45 +00002096 enic_free_vnic_resources(enic);
2097 enic_clear_intr_mode(enic);
2098}
2099
Vasanthy Kolluri2fdba382010-09-30 13:35:45 +00002100static int enic_dev_init(struct enic *enic)
Scott Feldman6fdfa972009-09-03 17:02:45 +00002101{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002102 struct device *dev = enic_get_dev(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002103 struct net_device *netdev = enic->netdev;
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002104 unsigned int i;
Scott Feldman6fdfa972009-09-03 17:02:45 +00002105 int err;
2106
Vasanthy Kolluriea7ea652011-06-17 07:56:48 +00002107 /* Get interrupt coalesce timer info */
2108 err = enic_dev_intr_coal_timer_info(enic);
2109 if (err) {
2110 dev_warn(dev, "Using default conversion factor for "
2111 "interrupt coalesce timer\n");
2112 vnic_dev_intr_coal_timer_info_default(enic->vdev);
2113 }
2114
Scott Feldman6fdfa972009-09-03 17:02:45 +00002115 /* Get vNIC configuration
2116 */
2117
2118 err = enic_get_vnic_config(enic);
2119 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002120 dev_err(dev, "Get vNIC configuration failed, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002121 return err;
2122 }
2123
2124 /* Get available resource counts
2125 */
2126
2127 enic_get_res_counts(enic);
2128
2129 /* Set interrupt mode based on resource counts and system
2130 * capabilities
2131 */
2132
2133 err = enic_set_intr_mode(enic);
2134 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002135 dev_err(dev, "Failed to set intr mode based on resource "
2136 "counts and system capabilities, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002137 return err;
2138 }
2139
2140 /* Allocate and configure vNIC resources
2141 */
2142
2143 err = enic_alloc_vnic_resources(enic);
2144 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002145 dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002146 goto err_out_free_vnic_resources;
2147 }
2148
2149 enic_init_vnic_resources(enic);
2150
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002151 err = enic_set_rss_nic_cfg(enic);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002152 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002153 dev_err(dev, "Failed to config nic, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002154 goto err_out_free_vnic_resources;
2155 }
2156
2157 switch (vnic_dev_get_intr_mode(enic->vdev)) {
2158 default:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002159 netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002160 break;
2161 case VNIC_DEV_INTR_MODE_MSIX:
Vasanthy Kolluri717258b2010-10-20 10:16:59 +00002162 for (i = 0; i < enic->rq_count; i++)
2163 netif_napi_add(netdev, &enic->napi[i],
2164 enic_poll_msix, 64);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002165 break;
2166 }
2167
2168 return 0;
2169
2170err_out_free_vnic_resources:
2171 enic_clear_intr_mode(enic);
2172 enic_free_vnic_resources(enic);
2173
2174 return err;
2175}
2176
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002177static void enic_iounmap(struct enic *enic)
2178{
2179 unsigned int i;
2180
2181 for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2182 if (enic->bar[i].vaddr)
2183 iounmap(enic->bar[i].vaddr);
2184}
2185
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002186static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002187{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002188 struct device *dev = &pdev->dev;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002189 struct net_device *netdev;
2190 struct enic *enic;
2191 int using_dac = 0;
2192 unsigned int i;
2193 int err;
Roopa Prabhu8749b422011-09-22 03:44:33 +00002194#ifdef CONFIG_PCI_IOV
2195 int pos = 0;
2196#endif
Roopa Prabhub67f2312012-01-19 22:25:36 +00002197 int num_pps = 1;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002198
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002199 /* Allocate net device structure and initialize. Private
2200 * instance data is initialized to zero.
2201 */
2202
govindarajulu.v822473b2013-09-04 11:17:14 +05302203 netdev = alloc_etherdev_mqs(sizeof(struct enic),
2204 ENIC_RQ_MAX, ENIC_WQ_MAX);
Joe Perches41de8d42012-01-29 13:47:52 +00002205 if (!netdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002206 return -ENOMEM;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002207
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002208 pci_set_drvdata(pdev, netdev);
2209
2210 SET_NETDEV_DEV(netdev, &pdev->dev);
2211
2212 enic = netdev_priv(netdev);
2213 enic->netdev = netdev;
2214 enic->pdev = pdev;
2215
2216 /* Setup PCI resources
2217 */
2218
Vasanthy Kolluri29046f92010-06-24 10:52:26 +00002219 err = pci_enable_device_mem(pdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002220 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002221 dev_err(dev, "Cannot enable PCI device, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002222 goto err_out_free_netdev;
2223 }
2224
2225 err = pci_request_regions(pdev, DRV_NAME);
2226 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002227 dev_err(dev, "Cannot request PCI regions, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002228 goto err_out_disable_device;
2229 }
2230
2231 pci_set_master(pdev);
2232
2233 /* Query PCI controller on system for DMA addressing
govindarajulu.v624dbf52013-09-04 11:17:16 +05302234 * limitation for the device. Try 64-bit first, and
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002235 * fail to 32-bit.
2236 */
2237
govindarajulu.v624dbf52013-09-04 11:17:16 +05302238 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002239 if (err) {
Yang Hongyang284901a2009-04-06 19:01:15 -07002240 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002241 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002242 dev_err(dev, "No usable DMA configuration, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002243 goto err_out_release_regions;
2244 }
Yang Hongyang284901a2009-04-06 19:01:15 -07002245 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002246 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002247 dev_err(dev, "Unable to obtain %u-bit DMA "
2248 "for consistent allocations, aborting\n", 32);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002249 goto err_out_release_regions;
2250 }
2251 } else {
govindarajulu.v624dbf52013-09-04 11:17:16 +05302252 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002253 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002254 dev_err(dev, "Unable to obtain %u-bit DMA "
govindarajulu.v624dbf52013-09-04 11:17:16 +05302255 "for consistent allocations, aborting\n", 64);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002256 goto err_out_release_regions;
2257 }
2258 using_dac = 1;
2259 }
2260
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002261 /* Map vNIC resources from BAR0-5
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002262 */
2263
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002264 for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2265 if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2266 continue;
2267 enic->bar[i].len = pci_resource_len(pdev, i);
2268 enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2269 if (!enic->bar[i].vaddr) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002270 dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002271 err = -ENODEV;
2272 goto err_out_iounmap;
2273 }
2274 enic->bar[i].bus_addr = pci_resource_start(pdev, i);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002275 }
2276
2277 /* Register vNIC device
2278 */
2279
Scott Feldman27e6c7d2009-09-03 17:01:53 +00002280 enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2281 ARRAY_SIZE(enic->bar));
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002282 if (!enic->vdev) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002283 dev_err(dev, "vNIC registration failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002284 err = -ENODEV;
2285 goto err_out_iounmap;
2286 }
2287
Roopa Prabhu8749b422011-09-22 03:44:33 +00002288#ifdef CONFIG_PCI_IOV
2289 /* Get number of subvnics */
2290 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
2291 if (pos) {
2292 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
Dan Carpenter413708b2012-02-29 21:19:54 +00002293 &enic->num_vfs);
Roopa Prabhu8749b422011-09-22 03:44:33 +00002294 if (enic->num_vfs) {
2295 err = pci_enable_sriov(pdev, enic->num_vfs);
2296 if (err) {
2297 dev_err(dev, "SRIOV enable failed, aborting."
2298 " pci_enable_sriov() returned %d\n",
2299 err);
2300 goto err_out_vnic_unregister;
2301 }
2302 enic->priv_flags |= ENIC_SRIOV_ENABLED;
Roopa Prabhub67f2312012-01-19 22:25:36 +00002303 num_pps = enic->num_vfs;
Roopa Prabhu8749b422011-09-22 03:44:33 +00002304 }
2305 }
Roopa Prabhu8749b422011-09-22 03:44:33 +00002306#endif
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002307
Roopa Prabhu3f192792011-09-22 03:44:43 +00002308 /* Allocate structure for port profiles */
Thomas Meyera1de2212011-11-29 11:08:00 +00002309 enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
Roopa Prabhu3f192792011-09-22 03:44:43 +00002310 if (!enic->pp) {
Roopa Prabhu3f192792011-09-22 03:44:43 +00002311 err = -ENOMEM;
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002312 goto err_out_disable_sriov_pp;
Roopa Prabhu3f192792011-09-22 03:44:43 +00002313 }
2314
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002315 /* Issue device open to get device in known state
2316 */
2317
2318 err = enic_dev_open(enic);
2319 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002320 dev_err(dev, "vNIC dev open failed, aborting\n");
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002321 goto err_out_disable_sriov;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002322 }
2323
Vasanthy Kolluri69161422011-02-04 16:17:16 +00002324 /* Setup devcmd lock
2325 */
2326
2327 spin_lock_init(&enic->devcmd_lock);
Neel Patel0b038562013-08-16 15:47:40 -07002328 spin_lock_init(&enic->enic_api_lock);
Vasanthy Kolluri69161422011-02-04 16:17:16 +00002329
2330 /*
2331 * Set ingress vlan rewrite mode before vnic initialization
2332 */
2333
2334 err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2335 if (err) {
2336 dev_err(dev,
2337 "Failed to set ingress vlan rewrite mode, aborting.\n");
2338 goto err_out_dev_close;
2339 }
2340
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002341 /* Issue device init to initialize the vnic-to-switch link.
2342 * We'll start with carrier off and wait for link UP
2343 * notification later to turn on carrier. We don't need
2344 * to wait here for the vnic-to-switch link initialization
2345 * to complete; link UP notification is the indication that
2346 * the process is complete.
2347 */
2348
2349 netif_carrier_off(netdev);
2350
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002351 /* Do not call dev_init for a dynamic vnic.
2352 * For a dynamic vnic, init_prov_info will be
2353 * called later by an upper layer.
2354 */
2355
Roopa Prabhu2b68c182012-02-20 00:12:04 +00002356 if (!enic_is_dynamic(enic)) {
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002357 err = vnic_dev_init(enic->vdev, 0);
2358 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002359 dev_err(dev, "vNIC dev init failed, aborting\n");
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002360 goto err_out_dev_close;
2361 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002362 }
2363
Scott Feldman6fdfa972009-09-03 17:02:45 +00002364 err = enic_dev_init(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002365 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002366 dev_err(dev, "Device initialization failed, aborting\n");
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002367 goto err_out_dev_close;
2368 }
2369
govindarajulu.v822473b2013-09-04 11:17:14 +05302370 netif_set_real_num_tx_queues(netdev, enic->wq_count);
govindarajulu.vbf751ba2013-09-04 11:17:15 +05302371 netif_set_real_num_rx_queues(netdev, enic->rq_count);
govindarajulu.v822473b2013-09-04 11:17:14 +05302372
Vasanthy Kolluri383ab922010-06-24 10:50:12 +00002373 /* Setup notification timer, HW reset task, and wq locks
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002374 */
2375
2376 init_timer(&enic->notify_timer);
2377 enic->notify_timer.function = enic_notify_timer;
2378 enic->notify_timer.data = (unsigned long)enic;
2379
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05302380 enic_set_rx_coal_setting(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002381 INIT_WORK(&enic->reset, enic_reset);
Roopa Prabhuc97c8942011-06-03 14:35:17 +00002382 INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002383
2384 for (i = 0; i < enic->wq_count; i++)
2385 spin_lock_init(&enic->wq_lock[i]);
2386
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002387 /* Register net device
2388 */
2389
2390 enic->port_mtu = enic->config.mtu;
2391 (void)enic_change_mtu(netdev, enic->port_mtu);
2392
2393 err = enic_set_mac_addr(netdev, enic->mac_addr);
2394 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002395 dev_err(dev, "Invalid MAC address, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002396 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002397 }
2398
Scott Feldman7c844592009-12-23 13:27:54 +00002399 enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
Sujith Sankar7c2ce6e2014-05-20 03:14:05 +05302400 /* rx coalesce time already got initialized. This gets used
2401 * if adaptive coal is turned off
2402 */
Scott Feldman7c844592009-12-23 13:27:54 +00002403 enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2404
Roopa Prabhu73359032012-01-18 04:24:02 +00002405 if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
Scott Feldmanf8bd9092010-05-17 22:50:19 -07002406 netdev->netdev_ops = &enic_netdev_dynamic_ops;
2407 else
2408 netdev->netdev_ops = &enic_netdev_ops;
2409
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002410 netdev->watchdog_timeo = 2 * HZ;
Neel Patelf13bbc22013-07-22 09:59:18 -07002411 enic_set_ethtool_ops(netdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002412
Patrick McHardyf6469682013-04-19 02:04:27 +00002413 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00002414 if (ENIC_SETTING(enic, LOOP)) {
Patrick McHardyf6469682013-04-19 02:04:27 +00002415 netdev->features &= ~NETIF_F_HW_VLAN_CTAG_TX;
Vasanthy Kolluri1825aca2010-06-24 10:51:59 +00002416 enic->loop_enable = 1;
2417 enic->loop_tag = enic->config.loop_tag;
2418 dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2419 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002420 if (ENIC_SETTING(enic, TXCSUM))
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002421 netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002422 if (ENIC_SETTING(enic, TSO))
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002423 netdev->hw_features |= NETIF_F_TSO |
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002424 NETIF_F_TSO6 | NETIF_F_TSO_ECN;
govindarajulu.vbf751ba2013-09-04 11:17:15 +05302425 if (ENIC_SETTING(enic, RSS))
2426 netdev->hw_features |= NETIF_F_RXHASH;
Michał Mirosław5ec8f9b2011-04-07 02:43:48 +00002427 if (ENIC_SETTING(enic, RXCSUM))
2428 netdev->hw_features |= NETIF_F_RXCSUM;
2429
2430 netdev->features |= netdev->hw_features;
2431
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002432 if (using_dac)
2433 netdev->features |= NETIF_F_HIGHDMA;
2434
Jiri Pirko01789342011-08-16 06:29:00 +00002435 netdev->priv_flags |= IFF_UNICAST_FLT;
2436
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002437 err = register_netdev(netdev);
2438 if (err) {
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002439 dev_err(dev, "Cannot register net device, aborting\n");
Scott Feldman6fdfa972009-09-03 17:02:45 +00002440 goto err_out_dev_deinit;
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002441 }
2442
2443 return 0;
2444
Scott Feldman6fdfa972009-09-03 17:02:45 +00002445err_out_dev_deinit:
2446 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002447err_out_dev_close:
2448 vnic_dev_close(enic->vdev);
Roopa Prabhu8749b422011-09-22 03:44:33 +00002449err_out_disable_sriov:
Roopa Prabhuca2b7212012-01-18 04:24:07 +00002450 kfree(enic->pp);
2451err_out_disable_sriov_pp:
Roopa Prabhu8749b422011-09-22 03:44:33 +00002452#ifdef CONFIG_PCI_IOV
2453 if (enic_sriov_enabled(enic)) {
2454 pci_disable_sriov(pdev);
2455 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2456 }
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002457err_out_vnic_unregister:
Roopa Prabhu8749b422011-09-22 03:44:33 +00002458#endif
Roopa Prabhu35d87e32012-01-18 04:24:12 +00002459 vnic_dev_unregister(enic->vdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002460err_out_iounmap:
2461 enic_iounmap(enic);
2462err_out_release_regions:
2463 pci_release_regions(pdev);
2464err_out_disable_device:
2465 pci_disable_device(pdev);
2466err_out_free_netdev:
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002467 free_netdev(netdev);
2468
2469 return err;
2470}
2471
Bill Pemberton854de922012-12-03 09:23:05 -05002472static void enic_remove(struct pci_dev *pdev)
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002473{
2474 struct net_device *netdev = pci_get_drvdata(pdev);
2475
2476 if (netdev) {
2477 struct enic *enic = netdev_priv(netdev);
2478
Tejun Heo23f333a2010-12-12 16:45:14 +01002479 cancel_work_sync(&enic->reset);
Roopa Prabhuc97c8942011-06-03 14:35:17 +00002480 cancel_work_sync(&enic->change_mtu_work);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002481 unregister_netdev(netdev);
Scott Feldman6fdfa972009-09-03 17:02:45 +00002482 enic_dev_deinit(enic);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002483 vnic_dev_close(enic->vdev);
Roopa Prabhu8749b422011-09-22 03:44:33 +00002484#ifdef CONFIG_PCI_IOV
2485 if (enic_sriov_enabled(enic)) {
2486 pci_disable_sriov(pdev);
2487 enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2488 }
2489#endif
Roopa Prabhu3f192792011-09-22 03:44:43 +00002490 kfree(enic->pp);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002491 vnic_dev_unregister(enic->vdev);
2492 enic_iounmap(enic);
2493 pci_release_regions(pdev);
2494 pci_disable_device(pdev);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002495 free_netdev(netdev);
2496 }
2497}
2498
2499static struct pci_driver enic_driver = {
2500 .name = DRV_NAME,
2501 .id_table = enic_id_table,
2502 .probe = enic_probe,
Bill Pemberton854de922012-12-03 09:23:05 -05002503 .remove = enic_remove,
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002504};
2505
2506static int __init enic_init_module(void)
2507{
Vasanthy Kolluria7a79de2010-06-24 10:50:56 +00002508 pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
Scott Feldman01f2e4e2008-09-15 09:17:11 -07002509
2510 return pci_register_driver(&enic_driver);
2511}
2512
2513static void __exit enic_cleanup_module(void)
2514{
2515 pci_unregister_driver(&enic_driver);
2516}
2517
2518module_init(enic_init_module);
2519module_exit(enic_cleanup_module);