1 /*
2  * Copyright 2008-2010 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  *
18  */
19 
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/pci.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if.h>
32 #include <linux/if_ether.h>
33 #include <linux/if_vlan.h>
34 #include <linux/ethtool.h>
35 #include <linux/in.h>
36 #include <linux/ip.h>
37 #include <linux/ipv6.h>
38 #include <linux/tcp.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/prefetch.h>
41 #include <net/ip6_checksum.h>
42 
43 #include "cq_enet_desc.h"
44 #include "vnic_dev.h"
45 #include "vnic_intr.h"
46 #include "vnic_stats.h"
47 #include "vnic_vic.h"
48 #include "enic_res.h"
49 #include "enic.h"
50 #include "enic_dev.h"
51 #include "enic_pp.h"
52 
53 #define ENIC_NOTIFY_TIMER_PERIOD	(2 * HZ)
54 #define WQ_ENET_MAX_DESC_LEN		(1 << WQ_ENET_LEN_BITS)
55 #define MAX_TSO				(1 << 16)
56 #define ENIC_DESC_MAX_SPLITS		(MAX_TSO / WQ_ENET_MAX_DESC_LEN + 1)
57 
58 #define PCI_DEVICE_ID_CISCO_VIC_ENET         0x0043  /* ethernet vnic */
59 #define PCI_DEVICE_ID_CISCO_VIC_ENET_DYN     0x0044  /* enet dynamic vnic */
60 #define PCI_DEVICE_ID_CISCO_VIC_ENET_VF      0x0071  /* enet SRIOV VF */
61 
62 /* Supported devices */
63 static DEFINE_PCI_DEVICE_TABLE(enic_id_table) = {
64 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET) },
65 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_DYN) },
66 	{ PCI_VDEVICE(CISCO, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
67 	{ 0, }	/* end of table */
68 };
69 
70 MODULE_DESCRIPTION(DRV_DESCRIPTION);
71 MODULE_AUTHOR("Scott Feldman <scofeldm@cisco.com>");
72 MODULE_LICENSE("GPL");
73 MODULE_VERSION(DRV_VERSION);
74 MODULE_DEVICE_TABLE(pci, enic_id_table);
75 
76 struct enic_stat {
77 	char name[ETH_GSTRING_LEN];
78 	unsigned int offset;
79 };
80 
81 #define ENIC_TX_STAT(stat)	\
82 	{ .name = #stat, .offset = offsetof(struct vnic_tx_stats, stat) / 8 }
83 #define ENIC_RX_STAT(stat)	\
84 	{ .name = #stat, .offset = offsetof(struct vnic_rx_stats, stat) / 8 }
85 
86 static const struct enic_stat enic_tx_stats[] = {
87 	ENIC_TX_STAT(tx_frames_ok),
88 	ENIC_TX_STAT(tx_unicast_frames_ok),
89 	ENIC_TX_STAT(tx_multicast_frames_ok),
90 	ENIC_TX_STAT(tx_broadcast_frames_ok),
91 	ENIC_TX_STAT(tx_bytes_ok),
92 	ENIC_TX_STAT(tx_unicast_bytes_ok),
93 	ENIC_TX_STAT(tx_multicast_bytes_ok),
94 	ENIC_TX_STAT(tx_broadcast_bytes_ok),
95 	ENIC_TX_STAT(tx_drops),
96 	ENIC_TX_STAT(tx_errors),
97 	ENIC_TX_STAT(tx_tso),
98 };
99 
100 static const struct enic_stat enic_rx_stats[] = {
101 	ENIC_RX_STAT(rx_frames_ok),
102 	ENIC_RX_STAT(rx_frames_total),
103 	ENIC_RX_STAT(rx_unicast_frames_ok),
104 	ENIC_RX_STAT(rx_multicast_frames_ok),
105 	ENIC_RX_STAT(rx_broadcast_frames_ok),
106 	ENIC_RX_STAT(rx_bytes_ok),
107 	ENIC_RX_STAT(rx_unicast_bytes_ok),
108 	ENIC_RX_STAT(rx_multicast_bytes_ok),
109 	ENIC_RX_STAT(rx_broadcast_bytes_ok),
110 	ENIC_RX_STAT(rx_drop),
111 	ENIC_RX_STAT(rx_no_bufs),
112 	ENIC_RX_STAT(rx_errors),
113 	ENIC_RX_STAT(rx_rss),
114 	ENIC_RX_STAT(rx_crc_errors),
115 	ENIC_RX_STAT(rx_frames_64),
116 	ENIC_RX_STAT(rx_frames_127),
117 	ENIC_RX_STAT(rx_frames_255),
118 	ENIC_RX_STAT(rx_frames_511),
119 	ENIC_RX_STAT(rx_frames_1023),
120 	ENIC_RX_STAT(rx_frames_1518),
121 	ENIC_RX_STAT(rx_frames_to_max),
122 };
123 
124 static const unsigned int enic_n_tx_stats = ARRAY_SIZE(enic_tx_stats);
125 static const unsigned int enic_n_rx_stats = ARRAY_SIZE(enic_rx_stats);
126 
enic_is_dynamic(struct enic * enic)127 int enic_is_dynamic(struct enic *enic)
128 {
129 	return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_DYN;
130 }
131 
enic_sriov_enabled(struct enic * enic)132 int enic_sriov_enabled(struct enic *enic)
133 {
134 	return (enic->priv_flags & ENIC_SRIOV_ENABLED) ? 1 : 0;
135 }
136 
enic_is_sriov_vf(struct enic * enic)137 static int enic_is_sriov_vf(struct enic *enic)
138 {
139 	return enic->pdev->device == PCI_DEVICE_ID_CISCO_VIC_ENET_VF;
140 }
141 
enic_is_valid_vf(struct enic * enic,int vf)142 int enic_is_valid_vf(struct enic *enic, int vf)
143 {
144 #ifdef CONFIG_PCI_IOV
145 	return vf >= 0 && vf < enic->num_vfs;
146 #else
147 	return 0;
148 #endif
149 }
150 
enic_cq_rq(struct enic * enic,unsigned int rq)151 static inline unsigned int enic_cq_rq(struct enic *enic, unsigned int rq)
152 {
153 	return rq;
154 }
155 
enic_cq_wq(struct enic * enic,unsigned int wq)156 static inline unsigned int enic_cq_wq(struct enic *enic, unsigned int wq)
157 {
158 	return enic->rq_count + wq;
159 }
160 
enic_legacy_io_intr(void)161 static inline unsigned int enic_legacy_io_intr(void)
162 {
163 	return 0;
164 }
165 
enic_legacy_err_intr(void)166 static inline unsigned int enic_legacy_err_intr(void)
167 {
168 	return 1;
169 }
170 
enic_legacy_notify_intr(void)171 static inline unsigned int enic_legacy_notify_intr(void)
172 {
173 	return 2;
174 }
175 
enic_msix_rq_intr(struct enic * enic,unsigned int rq)176 static inline unsigned int enic_msix_rq_intr(struct enic *enic, unsigned int rq)
177 {
178 	return enic->cq[enic_cq_rq(enic, rq)].interrupt_offset;
179 }
180 
enic_msix_wq_intr(struct enic * enic,unsigned int wq)181 static inline unsigned int enic_msix_wq_intr(struct enic *enic, unsigned int wq)
182 {
183 	return enic->cq[enic_cq_wq(enic, wq)].interrupt_offset;
184 }
185 
enic_msix_err_intr(struct enic * enic)186 static inline unsigned int enic_msix_err_intr(struct enic *enic)
187 {
188 	return enic->rq_count + enic->wq_count;
189 }
190 
enic_msix_notify_intr(struct enic * enic)191 static inline unsigned int enic_msix_notify_intr(struct enic *enic)
192 {
193 	return enic->rq_count + enic->wq_count + 1;
194 }
195 
enic_get_settings(struct net_device * netdev,struct ethtool_cmd * ecmd)196 static int enic_get_settings(struct net_device *netdev,
197 	struct ethtool_cmd *ecmd)
198 {
199 	struct enic *enic = netdev_priv(netdev);
200 
201 	ecmd->supported = (SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE);
202 	ecmd->advertising = (ADVERTISED_10000baseT_Full | ADVERTISED_FIBRE);
203 	ecmd->port = PORT_FIBRE;
204 	ecmd->transceiver = XCVR_EXTERNAL;
205 
206 	if (netif_carrier_ok(netdev)) {
207 		ethtool_cmd_speed_set(ecmd, vnic_dev_port_speed(enic->vdev));
208 		ecmd->duplex = DUPLEX_FULL;
209 	} else {
210 		ethtool_cmd_speed_set(ecmd, -1);
211 		ecmd->duplex = -1;
212 	}
213 
214 	ecmd->autoneg = AUTONEG_DISABLE;
215 
216 	return 0;
217 }
218 
enic_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)219 static void enic_get_drvinfo(struct net_device *netdev,
220 	struct ethtool_drvinfo *drvinfo)
221 {
222 	struct enic *enic = netdev_priv(netdev);
223 	struct vnic_devcmd_fw_info *fw_info;
224 
225 	enic_dev_fw_info(enic, &fw_info);
226 
227 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
228 	strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
229 	strlcpy(drvinfo->fw_version, fw_info->fw_version,
230 		sizeof(drvinfo->fw_version));
231 	strlcpy(drvinfo->bus_info, pci_name(enic->pdev),
232 		sizeof(drvinfo->bus_info));
233 }
234 
enic_get_strings(struct net_device * netdev,u32 stringset,u8 * data)235 static void enic_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
236 {
237 	unsigned int i;
238 
239 	switch (stringset) {
240 	case ETH_SS_STATS:
241 		for (i = 0; i < enic_n_tx_stats; i++) {
242 			memcpy(data, enic_tx_stats[i].name, ETH_GSTRING_LEN);
243 			data += ETH_GSTRING_LEN;
244 		}
245 		for (i = 0; i < enic_n_rx_stats; i++) {
246 			memcpy(data, enic_rx_stats[i].name, ETH_GSTRING_LEN);
247 			data += ETH_GSTRING_LEN;
248 		}
249 		break;
250 	}
251 }
252 
enic_get_sset_count(struct net_device * netdev,int sset)253 static int enic_get_sset_count(struct net_device *netdev, int sset)
254 {
255 	switch (sset) {
256 	case ETH_SS_STATS:
257 		return enic_n_tx_stats + enic_n_rx_stats;
258 	default:
259 		return -EOPNOTSUPP;
260 	}
261 }
262 
enic_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)263 static void enic_get_ethtool_stats(struct net_device *netdev,
264 	struct ethtool_stats *stats, u64 *data)
265 {
266 	struct enic *enic = netdev_priv(netdev);
267 	struct vnic_stats *vstats;
268 	unsigned int i;
269 
270 	enic_dev_stats_dump(enic, &vstats);
271 
272 	for (i = 0; i < enic_n_tx_stats; i++)
273 		*(data++) = ((u64 *)&vstats->tx)[enic_tx_stats[i].offset];
274 	for (i = 0; i < enic_n_rx_stats; i++)
275 		*(data++) = ((u64 *)&vstats->rx)[enic_rx_stats[i].offset];
276 }
277 
enic_get_msglevel(struct net_device * netdev)278 static u32 enic_get_msglevel(struct net_device *netdev)
279 {
280 	struct enic *enic = netdev_priv(netdev);
281 	return enic->msg_enable;
282 }
283 
enic_set_msglevel(struct net_device * netdev,u32 value)284 static void enic_set_msglevel(struct net_device *netdev, u32 value)
285 {
286 	struct enic *enic = netdev_priv(netdev);
287 	enic->msg_enable = value;
288 }
289 
enic_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ecmd)290 static int enic_get_coalesce(struct net_device *netdev,
291 	struct ethtool_coalesce *ecmd)
292 {
293 	struct enic *enic = netdev_priv(netdev);
294 
295 	ecmd->tx_coalesce_usecs = enic->tx_coalesce_usecs;
296 	ecmd->rx_coalesce_usecs = enic->rx_coalesce_usecs;
297 
298 	return 0;
299 }
300 
enic_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ecmd)301 static int enic_set_coalesce(struct net_device *netdev,
302 	struct ethtool_coalesce *ecmd)
303 {
304 	struct enic *enic = netdev_priv(netdev);
305 	u32 tx_coalesce_usecs;
306 	u32 rx_coalesce_usecs;
307 	unsigned int i, intr;
308 
309 	tx_coalesce_usecs = min_t(u32, ecmd->tx_coalesce_usecs,
310 		vnic_dev_get_intr_coal_timer_max(enic->vdev));
311 	rx_coalesce_usecs = min_t(u32, ecmd->rx_coalesce_usecs,
312 		vnic_dev_get_intr_coal_timer_max(enic->vdev));
313 
314 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
315 	case VNIC_DEV_INTR_MODE_INTX:
316 		if (tx_coalesce_usecs != rx_coalesce_usecs)
317 			return -EINVAL;
318 
319 		intr = enic_legacy_io_intr();
320 		vnic_intr_coalescing_timer_set(&enic->intr[intr],
321 			tx_coalesce_usecs);
322 		break;
323 	case VNIC_DEV_INTR_MODE_MSI:
324 		if (tx_coalesce_usecs != rx_coalesce_usecs)
325 			return -EINVAL;
326 
327 		vnic_intr_coalescing_timer_set(&enic->intr[0],
328 			tx_coalesce_usecs);
329 		break;
330 	case VNIC_DEV_INTR_MODE_MSIX:
331 		for (i = 0; i < enic->wq_count; i++) {
332 			intr = enic_msix_wq_intr(enic, i);
333 			vnic_intr_coalescing_timer_set(&enic->intr[intr],
334 				tx_coalesce_usecs);
335 		}
336 
337 		for (i = 0; i < enic->rq_count; i++) {
338 			intr = enic_msix_rq_intr(enic, i);
339 			vnic_intr_coalescing_timer_set(&enic->intr[intr],
340 				rx_coalesce_usecs);
341 		}
342 
343 		break;
344 	default:
345 		break;
346 	}
347 
348 	enic->tx_coalesce_usecs = tx_coalesce_usecs;
349 	enic->rx_coalesce_usecs = rx_coalesce_usecs;
350 
351 	return 0;
352 }
353 
354 static const struct ethtool_ops enic_ethtool_ops = {
355 	.get_settings = enic_get_settings,
356 	.get_drvinfo = enic_get_drvinfo,
357 	.get_msglevel = enic_get_msglevel,
358 	.set_msglevel = enic_set_msglevel,
359 	.get_link = ethtool_op_get_link,
360 	.get_strings = enic_get_strings,
361 	.get_sset_count = enic_get_sset_count,
362 	.get_ethtool_stats = enic_get_ethtool_stats,
363 	.get_coalesce = enic_get_coalesce,
364 	.set_coalesce = enic_set_coalesce,
365 };
366 
enic_free_wq_buf(struct vnic_wq * wq,struct vnic_wq_buf * buf)367 static void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
368 {
369 	struct enic *enic = vnic_dev_priv(wq->vdev);
370 
371 	if (buf->sop)
372 		pci_unmap_single(enic->pdev, buf->dma_addr,
373 			buf->len, PCI_DMA_TODEVICE);
374 	else
375 		pci_unmap_page(enic->pdev, buf->dma_addr,
376 			buf->len, PCI_DMA_TODEVICE);
377 
378 	if (buf->os_buf)
379 		dev_kfree_skb_any(buf->os_buf);
380 }
381 
enic_wq_free_buf(struct vnic_wq * wq,struct cq_desc * cq_desc,struct vnic_wq_buf * buf,void * opaque)382 static void enic_wq_free_buf(struct vnic_wq *wq,
383 	struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque)
384 {
385 	enic_free_wq_buf(wq, buf);
386 }
387 
enic_wq_service(struct vnic_dev * vdev,struct cq_desc * cq_desc,u8 type,u16 q_number,u16 completed_index,void * opaque)388 static int enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
389 	u8 type, u16 q_number, u16 completed_index, void *opaque)
390 {
391 	struct enic *enic = vnic_dev_priv(vdev);
392 
393 	spin_lock(&enic->wq_lock[q_number]);
394 
395 	vnic_wq_service(&enic->wq[q_number], cq_desc,
396 		completed_index, enic_wq_free_buf,
397 		opaque);
398 
399 	if (netif_queue_stopped(enic->netdev) &&
400 	    vnic_wq_desc_avail(&enic->wq[q_number]) >=
401 	    (MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS))
402 		netif_wake_queue(enic->netdev);
403 
404 	spin_unlock(&enic->wq_lock[q_number]);
405 
406 	return 0;
407 }
408 
enic_log_q_error(struct enic * enic)409 static void enic_log_q_error(struct enic *enic)
410 {
411 	unsigned int i;
412 	u32 error_status;
413 
414 	for (i = 0; i < enic->wq_count; i++) {
415 		error_status = vnic_wq_error_status(&enic->wq[i]);
416 		if (error_status)
417 			netdev_err(enic->netdev, "WQ[%d] error_status %d\n",
418 				i, error_status);
419 	}
420 
421 	for (i = 0; i < enic->rq_count; i++) {
422 		error_status = vnic_rq_error_status(&enic->rq[i]);
423 		if (error_status)
424 			netdev_err(enic->netdev, "RQ[%d] error_status %d\n",
425 				i, error_status);
426 	}
427 }
428 
enic_msglvl_check(struct enic * enic)429 static void enic_msglvl_check(struct enic *enic)
430 {
431 	u32 msg_enable = vnic_dev_msg_lvl(enic->vdev);
432 
433 	if (msg_enable != enic->msg_enable) {
434 		netdev_info(enic->netdev, "msg lvl changed from 0x%x to 0x%x\n",
435 			enic->msg_enable, msg_enable);
436 		enic->msg_enable = msg_enable;
437 	}
438 }
439 
enic_mtu_check(struct enic * enic)440 static void enic_mtu_check(struct enic *enic)
441 {
442 	u32 mtu = vnic_dev_mtu(enic->vdev);
443 	struct net_device *netdev = enic->netdev;
444 
445 	if (mtu && mtu != enic->port_mtu) {
446 		enic->port_mtu = mtu;
447 		if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
448 			mtu = max_t(int, ENIC_MIN_MTU,
449 				min_t(int, ENIC_MAX_MTU, mtu));
450 			if (mtu != netdev->mtu)
451 				schedule_work(&enic->change_mtu_work);
452 		} else {
453 			if (mtu < netdev->mtu)
454 				netdev_warn(netdev,
455 					"interface MTU (%d) set higher "
456 					"than switch port MTU (%d)\n",
457 					netdev->mtu, mtu);
458 		}
459 	}
460 }
461 
enic_link_check(struct enic * enic)462 static void enic_link_check(struct enic *enic)
463 {
464 	int link_status = vnic_dev_link_status(enic->vdev);
465 	int carrier_ok = netif_carrier_ok(enic->netdev);
466 
467 	if (link_status && !carrier_ok) {
468 		netdev_info(enic->netdev, "Link UP\n");
469 		netif_carrier_on(enic->netdev);
470 	} else if (!link_status && carrier_ok) {
471 		netdev_info(enic->netdev, "Link DOWN\n");
472 		netif_carrier_off(enic->netdev);
473 	}
474 }
475 
enic_notify_check(struct enic * enic)476 static void enic_notify_check(struct enic *enic)
477 {
478 	enic_msglvl_check(enic);
479 	enic_mtu_check(enic);
480 	enic_link_check(enic);
481 }
482 
483 #define ENIC_TEST_INTR(pba, i) (pba & (1 << i))
484 
enic_isr_legacy(int irq,void * data)485 static irqreturn_t enic_isr_legacy(int irq, void *data)
486 {
487 	struct net_device *netdev = data;
488 	struct enic *enic = netdev_priv(netdev);
489 	unsigned int io_intr = enic_legacy_io_intr();
490 	unsigned int err_intr = enic_legacy_err_intr();
491 	unsigned int notify_intr = enic_legacy_notify_intr();
492 	u32 pba;
493 
494 	vnic_intr_mask(&enic->intr[io_intr]);
495 
496 	pba = vnic_intr_legacy_pba(enic->legacy_pba);
497 	if (!pba) {
498 		vnic_intr_unmask(&enic->intr[io_intr]);
499 		return IRQ_NONE;	/* not our interrupt */
500 	}
501 
502 	if (ENIC_TEST_INTR(pba, notify_intr)) {
503 		vnic_intr_return_all_credits(&enic->intr[notify_intr]);
504 		enic_notify_check(enic);
505 	}
506 
507 	if (ENIC_TEST_INTR(pba, err_intr)) {
508 		vnic_intr_return_all_credits(&enic->intr[err_intr]);
509 		enic_log_q_error(enic);
510 		/* schedule recovery from WQ/RQ error */
511 		schedule_work(&enic->reset);
512 		return IRQ_HANDLED;
513 	}
514 
515 	if (ENIC_TEST_INTR(pba, io_intr)) {
516 		if (napi_schedule_prep(&enic->napi[0]))
517 			__napi_schedule(&enic->napi[0]);
518 	} else {
519 		vnic_intr_unmask(&enic->intr[io_intr]);
520 	}
521 
522 	return IRQ_HANDLED;
523 }
524 
enic_isr_msi(int irq,void * data)525 static irqreturn_t enic_isr_msi(int irq, void *data)
526 {
527 	struct enic *enic = data;
528 
529 	/* With MSI, there is no sharing of interrupts, so this is
530 	 * our interrupt and there is no need to ack it.  The device
531 	 * is not providing per-vector masking, so the OS will not
532 	 * write to PCI config space to mask/unmask the interrupt.
533 	 * We're using mask_on_assertion for MSI, so the device
534 	 * automatically masks the interrupt when the interrupt is
535 	 * generated.  Later, when exiting polling, the interrupt
536 	 * will be unmasked (see enic_poll).
537 	 *
538 	 * Also, the device uses the same PCIe Traffic Class (TC)
539 	 * for Memory Write data and MSI, so there are no ordering
540 	 * issues; the MSI will always arrive at the Root Complex
541 	 * _after_ corresponding Memory Writes (i.e. descriptor
542 	 * writes).
543 	 */
544 
545 	napi_schedule(&enic->napi[0]);
546 
547 	return IRQ_HANDLED;
548 }
549 
enic_isr_msix_rq(int irq,void * data)550 static irqreturn_t enic_isr_msix_rq(int irq, void *data)
551 {
552 	struct napi_struct *napi = data;
553 
554 	/* schedule NAPI polling for RQ cleanup */
555 	napi_schedule(napi);
556 
557 	return IRQ_HANDLED;
558 }
559 
enic_isr_msix_wq(int irq,void * data)560 static irqreturn_t enic_isr_msix_wq(int irq, void *data)
561 {
562 	struct enic *enic = data;
563 	unsigned int cq = enic_cq_wq(enic, 0);
564 	unsigned int intr = enic_msix_wq_intr(enic, 0);
565 	unsigned int wq_work_to_do = -1; /* no limit */
566 	unsigned int wq_work_done;
567 
568 	wq_work_done = vnic_cq_service(&enic->cq[cq],
569 		wq_work_to_do, enic_wq_service, NULL);
570 
571 	vnic_intr_return_credits(&enic->intr[intr],
572 		wq_work_done,
573 		1 /* unmask intr */,
574 		1 /* reset intr timer */);
575 
576 	return IRQ_HANDLED;
577 }
578 
enic_isr_msix_err(int irq,void * data)579 static irqreturn_t enic_isr_msix_err(int irq, void *data)
580 {
581 	struct enic *enic = data;
582 	unsigned int intr = enic_msix_err_intr(enic);
583 
584 	vnic_intr_return_all_credits(&enic->intr[intr]);
585 
586 	enic_log_q_error(enic);
587 
588 	/* schedule recovery from WQ/RQ error */
589 	schedule_work(&enic->reset);
590 
591 	return IRQ_HANDLED;
592 }
593 
enic_isr_msix_notify(int irq,void * data)594 static irqreturn_t enic_isr_msix_notify(int irq, void *data)
595 {
596 	struct enic *enic = data;
597 	unsigned int intr = enic_msix_notify_intr(enic);
598 
599 	vnic_intr_return_all_credits(&enic->intr[intr]);
600 	enic_notify_check(enic);
601 
602 	return IRQ_HANDLED;
603 }
604 
enic_queue_wq_skb_cont(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,unsigned int len_left,int loopback)605 static inline void enic_queue_wq_skb_cont(struct enic *enic,
606 	struct vnic_wq *wq, struct sk_buff *skb,
607 	unsigned int len_left, int loopback)
608 {
609 	const skb_frag_t *frag;
610 
611 	/* Queue additional data fragments */
612 	for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
613 		len_left -= skb_frag_size(frag);
614 		enic_queue_wq_desc_cont(wq, skb,
615 			skb_frag_dma_map(&enic->pdev->dev,
616 					 frag, 0, skb_frag_size(frag),
617 					 DMA_TO_DEVICE),
618 			skb_frag_size(frag),
619 			(len_left == 0),	/* EOP? */
620 			loopback);
621 	}
622 }
623 
enic_queue_wq_skb_vlan(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,int vlan_tag_insert,unsigned int vlan_tag,int loopback)624 static inline void enic_queue_wq_skb_vlan(struct enic *enic,
625 	struct vnic_wq *wq, struct sk_buff *skb,
626 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
627 {
628 	unsigned int head_len = skb_headlen(skb);
629 	unsigned int len_left = skb->len - head_len;
630 	int eop = (len_left == 0);
631 
632 	/* Queue the main skb fragment. The fragments are no larger
633 	 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
634 	 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
635 	 * per fragment is queued.
636 	 */
637 	enic_queue_wq_desc(wq, skb,
638 		pci_map_single(enic->pdev, skb->data,
639 			head_len, PCI_DMA_TODEVICE),
640 		head_len,
641 		vlan_tag_insert, vlan_tag,
642 		eop, loopback);
643 
644 	if (!eop)
645 		enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
646 }
647 
enic_queue_wq_skb_csum_l4(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,int vlan_tag_insert,unsigned int vlan_tag,int loopback)648 static inline void enic_queue_wq_skb_csum_l4(struct enic *enic,
649 	struct vnic_wq *wq, struct sk_buff *skb,
650 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
651 {
652 	unsigned int head_len = skb_headlen(skb);
653 	unsigned int len_left = skb->len - head_len;
654 	unsigned int hdr_len = skb_checksum_start_offset(skb);
655 	unsigned int csum_offset = hdr_len + skb->csum_offset;
656 	int eop = (len_left == 0);
657 
658 	/* Queue the main skb fragment. The fragments are no larger
659 	 * than max MTU(9000)+ETH_HDR_LEN(14) bytes, which is less
660 	 * than WQ_ENET_MAX_DESC_LEN length. So only one descriptor
661 	 * per fragment is queued.
662 	 */
663 	enic_queue_wq_desc_csum_l4(wq, skb,
664 		pci_map_single(enic->pdev, skb->data,
665 			head_len, PCI_DMA_TODEVICE),
666 		head_len,
667 		csum_offset,
668 		hdr_len,
669 		vlan_tag_insert, vlan_tag,
670 		eop, loopback);
671 
672 	if (!eop)
673 		enic_queue_wq_skb_cont(enic, wq, skb, len_left, loopback);
674 }
675 
enic_queue_wq_skb_tso(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb,unsigned int mss,int vlan_tag_insert,unsigned int vlan_tag,int loopback)676 static inline void enic_queue_wq_skb_tso(struct enic *enic,
677 	struct vnic_wq *wq, struct sk_buff *skb, unsigned int mss,
678 	int vlan_tag_insert, unsigned int vlan_tag, int loopback)
679 {
680 	unsigned int frag_len_left = skb_headlen(skb);
681 	unsigned int len_left = skb->len - frag_len_left;
682 	unsigned int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
683 	int eop = (len_left == 0);
684 	unsigned int len;
685 	dma_addr_t dma_addr;
686 	unsigned int offset = 0;
687 	skb_frag_t *frag;
688 
689 	/* Preload TCP csum field with IP pseudo hdr calculated
690 	 * with IP length set to zero.  HW will later add in length
691 	 * to each TCP segment resulting from the TSO.
692 	 */
693 
694 	if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
695 		ip_hdr(skb)->check = 0;
696 		tcp_hdr(skb)->check = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
697 			ip_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
698 	} else if (skb->protocol == cpu_to_be16(ETH_P_IPV6)) {
699 		tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
700 			&ipv6_hdr(skb)->daddr, 0, IPPROTO_TCP, 0);
701 	}
702 
703 	/* Queue WQ_ENET_MAX_DESC_LEN length descriptors
704 	 * for the main skb fragment
705 	 */
706 	while (frag_len_left) {
707 		len = min(frag_len_left, (unsigned int)WQ_ENET_MAX_DESC_LEN);
708 		dma_addr = pci_map_single(enic->pdev, skb->data + offset,
709 				len, PCI_DMA_TODEVICE);
710 		enic_queue_wq_desc_tso(wq, skb,
711 			dma_addr,
712 			len,
713 			mss, hdr_len,
714 			vlan_tag_insert, vlan_tag,
715 			eop && (len == frag_len_left), loopback);
716 		frag_len_left -= len;
717 		offset += len;
718 	}
719 
720 	if (eop)
721 		return;
722 
723 	/* Queue WQ_ENET_MAX_DESC_LEN length descriptors
724 	 * for additional data fragments
725 	 */
726 	for (frag = skb_shinfo(skb)->frags; len_left; frag++) {
727 		len_left -= skb_frag_size(frag);
728 		frag_len_left = skb_frag_size(frag);
729 		offset = 0;
730 
731 		while (frag_len_left) {
732 			len = min(frag_len_left,
733 				(unsigned int)WQ_ENET_MAX_DESC_LEN);
734 			dma_addr = skb_frag_dma_map(&enic->pdev->dev, frag,
735 						    offset, len,
736 						    DMA_TO_DEVICE);
737 			enic_queue_wq_desc_cont(wq, skb,
738 				dma_addr,
739 				len,
740 				(len_left == 0) &&
741 				(len == frag_len_left),		/* EOP? */
742 				loopback);
743 			frag_len_left -= len;
744 			offset += len;
745 		}
746 	}
747 }
748 
enic_queue_wq_skb(struct enic * enic,struct vnic_wq * wq,struct sk_buff * skb)749 static inline void enic_queue_wq_skb(struct enic *enic,
750 	struct vnic_wq *wq, struct sk_buff *skb)
751 {
752 	unsigned int mss = skb_shinfo(skb)->gso_size;
753 	unsigned int vlan_tag = 0;
754 	int vlan_tag_insert = 0;
755 	int loopback = 0;
756 
757 	if (vlan_tx_tag_present(skb)) {
758 		/* VLAN tag from trunking driver */
759 		vlan_tag_insert = 1;
760 		vlan_tag = vlan_tx_tag_get(skb);
761 	} else if (enic->loop_enable) {
762 		vlan_tag = enic->loop_tag;
763 		loopback = 1;
764 	}
765 
766 	if (mss)
767 		enic_queue_wq_skb_tso(enic, wq, skb, mss,
768 			vlan_tag_insert, vlan_tag, loopback);
769 	else if	(skb->ip_summed == CHECKSUM_PARTIAL)
770 		enic_queue_wq_skb_csum_l4(enic, wq, skb,
771 			vlan_tag_insert, vlan_tag, loopback);
772 	else
773 		enic_queue_wq_skb_vlan(enic, wq, skb,
774 			vlan_tag_insert, vlan_tag, loopback);
775 }
776 
777 /* netif_tx_lock held, process context with BHs disabled, or BH */
enic_hard_start_xmit(struct sk_buff * skb,struct net_device * netdev)778 static netdev_tx_t enic_hard_start_xmit(struct sk_buff *skb,
779 	struct net_device *netdev)
780 {
781 	struct enic *enic = netdev_priv(netdev);
782 	struct vnic_wq *wq = &enic->wq[0];
783 	unsigned long flags;
784 
785 	if (skb->len <= 0) {
786 		dev_kfree_skb(skb);
787 		return NETDEV_TX_OK;
788 	}
789 
790 	/* Non-TSO sends must fit within ENIC_NON_TSO_MAX_DESC descs,
791 	 * which is very likely.  In the off chance it's going to take
792 	 * more than * ENIC_NON_TSO_MAX_DESC, linearize the skb.
793 	 */
794 
795 	if (skb_shinfo(skb)->gso_size == 0 &&
796 	    skb_shinfo(skb)->nr_frags + 1 > ENIC_NON_TSO_MAX_DESC &&
797 	    skb_linearize(skb)) {
798 		dev_kfree_skb(skb);
799 		return NETDEV_TX_OK;
800 	}
801 
802 	spin_lock_irqsave(&enic->wq_lock[0], flags);
803 
804 	if (vnic_wq_desc_avail(wq) <
805 	    skb_shinfo(skb)->nr_frags + ENIC_DESC_MAX_SPLITS) {
806 		netif_stop_queue(netdev);
807 		/* This is a hard error, log it */
808 		netdev_err(netdev, "BUG! Tx ring full when queue awake!\n");
809 		spin_unlock_irqrestore(&enic->wq_lock[0], flags);
810 		return NETDEV_TX_BUSY;
811 	}
812 
813 	enic_queue_wq_skb(enic, wq, skb);
814 
815 	if (vnic_wq_desc_avail(wq) < MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)
816 		netif_stop_queue(netdev);
817 
818 	spin_unlock_irqrestore(&enic->wq_lock[0], flags);
819 
820 	return NETDEV_TX_OK;
821 }
822 
823 /* dev_base_lock rwlock held, nominally process context */
enic_get_stats(struct net_device * netdev,struct rtnl_link_stats64 * net_stats)824 static struct rtnl_link_stats64 *enic_get_stats(struct net_device *netdev,
825 						struct rtnl_link_stats64 *net_stats)
826 {
827 	struct enic *enic = netdev_priv(netdev);
828 	struct vnic_stats *stats;
829 
830 	enic_dev_stats_dump(enic, &stats);
831 
832 	net_stats->tx_packets = stats->tx.tx_frames_ok;
833 	net_stats->tx_bytes = stats->tx.tx_bytes_ok;
834 	net_stats->tx_errors = stats->tx.tx_errors;
835 	net_stats->tx_dropped = stats->tx.tx_drops;
836 
837 	net_stats->rx_packets = stats->rx.rx_frames_ok;
838 	net_stats->rx_bytes = stats->rx.rx_bytes_ok;
839 	net_stats->rx_errors = stats->rx.rx_errors;
840 	net_stats->multicast = stats->rx.rx_multicast_frames_ok;
841 	net_stats->rx_over_errors = enic->rq_truncated_pkts;
842 	net_stats->rx_crc_errors = enic->rq_bad_fcs;
843 	net_stats->rx_dropped = stats->rx.rx_no_bufs + stats->rx.rx_drop;
844 
845 	return net_stats;
846 }
847 
enic_reset_addr_lists(struct enic * enic)848 void enic_reset_addr_lists(struct enic *enic)
849 {
850 	enic->mc_count = 0;
851 	enic->uc_count = 0;
852 	enic->flags = 0;
853 }
854 
enic_set_mac_addr(struct net_device * netdev,char * addr)855 static int enic_set_mac_addr(struct net_device *netdev, char *addr)
856 {
857 	struct enic *enic = netdev_priv(netdev);
858 
859 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic)) {
860 		if (!is_valid_ether_addr(addr) && !is_zero_ether_addr(addr))
861 			return -EADDRNOTAVAIL;
862 	} else {
863 		if (!is_valid_ether_addr(addr))
864 			return -EADDRNOTAVAIL;
865 	}
866 
867 	memcpy(netdev->dev_addr, addr, netdev->addr_len);
868 
869 	return 0;
870 }
871 
enic_set_mac_address_dynamic(struct net_device * netdev,void * p)872 static int enic_set_mac_address_dynamic(struct net_device *netdev, void *p)
873 {
874 	struct enic *enic = netdev_priv(netdev);
875 	struct sockaddr *saddr = p;
876 	char *addr = saddr->sa_data;
877 	int err;
878 
879 	if (netif_running(enic->netdev)) {
880 		err = enic_dev_del_station_addr(enic);
881 		if (err)
882 			return err;
883 	}
884 
885 	err = enic_set_mac_addr(netdev, addr);
886 	if (err)
887 		return err;
888 
889 	if (netif_running(enic->netdev)) {
890 		err = enic_dev_add_station_addr(enic);
891 		if (err)
892 			return err;
893 	}
894 
895 	return err;
896 }
897 
enic_set_mac_address(struct net_device * netdev,void * p)898 static int enic_set_mac_address(struct net_device *netdev, void *p)
899 {
900 	struct sockaddr *saddr = p;
901 	char *addr = saddr->sa_data;
902 	struct enic *enic = netdev_priv(netdev);
903 	int err;
904 
905 	err = enic_dev_del_station_addr(enic);
906 	if (err)
907 		return err;
908 
909 	err = enic_set_mac_addr(netdev, addr);
910 	if (err)
911 		return err;
912 
913 	return enic_dev_add_station_addr(enic);
914 }
915 
enic_update_multicast_addr_list(struct enic * enic)916 static void enic_update_multicast_addr_list(struct enic *enic)
917 {
918 	struct net_device *netdev = enic->netdev;
919 	struct netdev_hw_addr *ha;
920 	unsigned int mc_count = netdev_mc_count(netdev);
921 	u8 mc_addr[ENIC_MULTICAST_PERFECT_FILTERS][ETH_ALEN];
922 	unsigned int i, j;
923 
924 	if (mc_count > ENIC_MULTICAST_PERFECT_FILTERS) {
925 		netdev_warn(netdev, "Registering only %d out of %d "
926 			"multicast addresses\n",
927 			ENIC_MULTICAST_PERFECT_FILTERS, mc_count);
928 		mc_count = ENIC_MULTICAST_PERFECT_FILTERS;
929 	}
930 
931 	/* Is there an easier way?  Trying to minimize to
932 	 * calls to add/del multicast addrs.  We keep the
933 	 * addrs from the last call in enic->mc_addr and
934 	 * look for changes to add/del.
935 	 */
936 
937 	i = 0;
938 	netdev_for_each_mc_addr(ha, netdev) {
939 		if (i == mc_count)
940 			break;
941 		memcpy(mc_addr[i++], ha->addr, ETH_ALEN);
942 	}
943 
944 	for (i = 0; i < enic->mc_count; i++) {
945 		for (j = 0; j < mc_count; j++)
946 			if (compare_ether_addr(enic->mc_addr[i],
947 				mc_addr[j]) == 0)
948 				break;
949 		if (j == mc_count)
950 			enic_dev_del_addr(enic, enic->mc_addr[i]);
951 	}
952 
953 	for (i = 0; i < mc_count; i++) {
954 		for (j = 0; j < enic->mc_count; j++)
955 			if (compare_ether_addr(mc_addr[i],
956 				enic->mc_addr[j]) == 0)
957 				break;
958 		if (j == enic->mc_count)
959 			enic_dev_add_addr(enic, mc_addr[i]);
960 	}
961 
962 	/* Save the list to compare against next time
963 	 */
964 
965 	for (i = 0; i < mc_count; i++)
966 		memcpy(enic->mc_addr[i], mc_addr[i], ETH_ALEN);
967 
968 	enic->mc_count = mc_count;
969 }
970 
enic_update_unicast_addr_list(struct enic * enic)971 static void enic_update_unicast_addr_list(struct enic *enic)
972 {
973 	struct net_device *netdev = enic->netdev;
974 	struct netdev_hw_addr *ha;
975 	unsigned int uc_count = netdev_uc_count(netdev);
976 	u8 uc_addr[ENIC_UNICAST_PERFECT_FILTERS][ETH_ALEN];
977 	unsigned int i, j;
978 
979 	if (uc_count > ENIC_UNICAST_PERFECT_FILTERS) {
980 		netdev_warn(netdev, "Registering only %d out of %d "
981 			"unicast addresses\n",
982 			ENIC_UNICAST_PERFECT_FILTERS, uc_count);
983 		uc_count = ENIC_UNICAST_PERFECT_FILTERS;
984 	}
985 
986 	/* Is there an easier way?  Trying to minimize to
987 	 * calls to add/del unicast addrs.  We keep the
988 	 * addrs from the last call in enic->uc_addr and
989 	 * look for changes to add/del.
990 	 */
991 
992 	i = 0;
993 	netdev_for_each_uc_addr(ha, netdev) {
994 		if (i == uc_count)
995 			break;
996 		memcpy(uc_addr[i++], ha->addr, ETH_ALEN);
997 	}
998 
999 	for (i = 0; i < enic->uc_count; i++) {
1000 		for (j = 0; j < uc_count; j++)
1001 			if (compare_ether_addr(enic->uc_addr[i],
1002 				uc_addr[j]) == 0)
1003 				break;
1004 		if (j == uc_count)
1005 			enic_dev_del_addr(enic, enic->uc_addr[i]);
1006 	}
1007 
1008 	for (i = 0; i < uc_count; i++) {
1009 		for (j = 0; j < enic->uc_count; j++)
1010 			if (compare_ether_addr(uc_addr[i],
1011 				enic->uc_addr[j]) == 0)
1012 				break;
1013 		if (j == enic->uc_count)
1014 			enic_dev_add_addr(enic, uc_addr[i]);
1015 	}
1016 
1017 	/* Save the list to compare against next time
1018 	 */
1019 
1020 	for (i = 0; i < uc_count; i++)
1021 		memcpy(enic->uc_addr[i], uc_addr[i], ETH_ALEN);
1022 
1023 	enic->uc_count = uc_count;
1024 }
1025 
1026 /* netif_tx_lock held, BHs disabled */
enic_set_rx_mode(struct net_device * netdev)1027 static void enic_set_rx_mode(struct net_device *netdev)
1028 {
1029 	struct enic *enic = netdev_priv(netdev);
1030 	int directed = 1;
1031 	int multicast = (netdev->flags & IFF_MULTICAST) ? 1 : 0;
1032 	int broadcast = (netdev->flags & IFF_BROADCAST) ? 1 : 0;
1033 	int promisc = (netdev->flags & IFF_PROMISC) ||
1034 		netdev_uc_count(netdev) > ENIC_UNICAST_PERFECT_FILTERS;
1035 	int allmulti = (netdev->flags & IFF_ALLMULTI) ||
1036 		netdev_mc_count(netdev) > ENIC_MULTICAST_PERFECT_FILTERS;
1037 	unsigned int flags = netdev->flags |
1038 		(allmulti ? IFF_ALLMULTI : 0) |
1039 		(promisc ? IFF_PROMISC : 0);
1040 
1041 	if (enic->flags != flags) {
1042 		enic->flags = flags;
1043 		enic_dev_packet_filter(enic, directed,
1044 			multicast, broadcast, promisc, allmulti);
1045 	}
1046 
1047 	if (!promisc) {
1048 		enic_update_unicast_addr_list(enic);
1049 		if (!allmulti)
1050 			enic_update_multicast_addr_list(enic);
1051 	}
1052 }
1053 
1054 /* netif_tx_lock held, BHs disabled */
enic_tx_timeout(struct net_device * netdev)1055 static void enic_tx_timeout(struct net_device *netdev)
1056 {
1057 	struct enic *enic = netdev_priv(netdev);
1058 	schedule_work(&enic->reset);
1059 }
1060 
enic_set_vf_mac(struct net_device * netdev,int vf,u8 * mac)1061 static int enic_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
1062 {
1063 	struct enic *enic = netdev_priv(netdev);
1064 	struct enic_port_profile *pp;
1065 	int err;
1066 
1067 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1068 	if (err)
1069 		return err;
1070 
1071 	if (is_valid_ether_addr(mac)) {
1072 		memcpy(pp->vf_mac, mac, ETH_ALEN);
1073 		return 0;
1074 	} else
1075 		return -EINVAL;
1076 }
1077 
enic_set_vf_port(struct net_device * netdev,int vf,struct nlattr * port[])1078 static int enic_set_vf_port(struct net_device *netdev, int vf,
1079 	struct nlattr *port[])
1080 {
1081 	struct enic *enic = netdev_priv(netdev);
1082 	struct enic_port_profile prev_pp;
1083 	struct enic_port_profile *pp;
1084 	int err = 0, restore_pp = 1;
1085 
1086 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1087 	if (err)
1088 		return err;
1089 
1090 	if (!port[IFLA_PORT_REQUEST])
1091 		return -EOPNOTSUPP;
1092 
1093 	memcpy(&prev_pp, pp, sizeof(*enic->pp));
1094 	memset(pp, 0, sizeof(*enic->pp));
1095 
1096 	pp->set |= ENIC_SET_REQUEST;
1097 	pp->request = nla_get_u8(port[IFLA_PORT_REQUEST]);
1098 
1099 	if (port[IFLA_PORT_PROFILE]) {
1100 		pp->set |= ENIC_SET_NAME;
1101 		memcpy(pp->name, nla_data(port[IFLA_PORT_PROFILE]),
1102 			PORT_PROFILE_MAX);
1103 	}
1104 
1105 	if (port[IFLA_PORT_INSTANCE_UUID]) {
1106 		pp->set |= ENIC_SET_INSTANCE;
1107 		memcpy(pp->instance_uuid,
1108 			nla_data(port[IFLA_PORT_INSTANCE_UUID]), PORT_UUID_MAX);
1109 	}
1110 
1111 	if (port[IFLA_PORT_HOST_UUID]) {
1112 		pp->set |= ENIC_SET_HOST;
1113 		memcpy(pp->host_uuid,
1114 			nla_data(port[IFLA_PORT_HOST_UUID]), PORT_UUID_MAX);
1115 	}
1116 
1117 	/* Special case handling: mac came from IFLA_VF_MAC */
1118 	if (!is_zero_ether_addr(prev_pp.vf_mac))
1119 		memcpy(pp->mac_addr, prev_pp.vf_mac, ETH_ALEN);
1120 
1121 	if (vf == PORT_SELF_VF && is_zero_ether_addr(netdev->dev_addr))
1122 		random_ether_addr(netdev->dev_addr);
1123 
1124 	err = enic_process_set_pp_request(enic, vf, &prev_pp, &restore_pp);
1125 	if (err) {
1126 		if (restore_pp) {
1127 			/* Things are still the way they were: Implicit
1128 			 * DISASSOCIATE failed
1129 			 */
1130 			memcpy(pp, &prev_pp, sizeof(*pp));
1131 		} else {
1132 			memset(pp, 0, sizeof(*pp));
1133 			if (vf == PORT_SELF_VF)
1134 				memset(netdev->dev_addr, 0, ETH_ALEN);
1135 		}
1136 	} else {
1137 		/* Set flag to indicate that the port assoc/disassoc
1138 		 * request has been sent out to fw
1139 		 */
1140 		pp->set |= ENIC_PORT_REQUEST_APPLIED;
1141 
1142 		/* If DISASSOCIATE, clean up all assigned/saved macaddresses */
1143 		if (pp->request == PORT_REQUEST_DISASSOCIATE) {
1144 			memset(pp->mac_addr, 0, ETH_ALEN);
1145 			if (vf == PORT_SELF_VF)
1146 				memset(netdev->dev_addr, 0, ETH_ALEN);
1147 		}
1148 	}
1149 
1150 	memset(pp->vf_mac, 0, ETH_ALEN);
1151 
1152 	return err;
1153 }
1154 
enic_get_vf_port(struct net_device * netdev,int vf,struct sk_buff * skb)1155 static int enic_get_vf_port(struct net_device *netdev, int vf,
1156 	struct sk_buff *skb)
1157 {
1158 	struct enic *enic = netdev_priv(netdev);
1159 	u16 response = PORT_PROFILE_RESPONSE_SUCCESS;
1160 	struct enic_port_profile *pp;
1161 	int err;
1162 
1163 	ENIC_PP_BY_INDEX(enic, vf, pp, &err);
1164 	if (err)
1165 		return err;
1166 
1167 	if (!(pp->set & ENIC_PORT_REQUEST_APPLIED))
1168 		return -ENODATA;
1169 
1170 	err = enic_process_get_pp_request(enic, vf, pp->request, &response);
1171 	if (err)
1172 		return err;
1173 
1174 	NLA_PUT_U16(skb, IFLA_PORT_REQUEST, pp->request);
1175 	NLA_PUT_U16(skb, IFLA_PORT_RESPONSE, response);
1176 	if (pp->set & ENIC_SET_NAME)
1177 		NLA_PUT(skb, IFLA_PORT_PROFILE, PORT_PROFILE_MAX,
1178 			pp->name);
1179 	if (pp->set & ENIC_SET_INSTANCE)
1180 		NLA_PUT(skb, IFLA_PORT_INSTANCE_UUID, PORT_UUID_MAX,
1181 			pp->instance_uuid);
1182 	if (pp->set & ENIC_SET_HOST)
1183 		NLA_PUT(skb, IFLA_PORT_HOST_UUID, PORT_UUID_MAX,
1184 			pp->host_uuid);
1185 
1186 	return 0;
1187 
1188 nla_put_failure:
1189 	return -EMSGSIZE;
1190 }
1191 
enic_free_rq_buf(struct vnic_rq * rq,struct vnic_rq_buf * buf)1192 static void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
1193 {
1194 	struct enic *enic = vnic_dev_priv(rq->vdev);
1195 
1196 	if (!buf->os_buf)
1197 		return;
1198 
1199 	pci_unmap_single(enic->pdev, buf->dma_addr,
1200 		buf->len, PCI_DMA_FROMDEVICE);
1201 	dev_kfree_skb_any(buf->os_buf);
1202 }
1203 
enic_rq_alloc_buf(struct vnic_rq * rq)1204 static int enic_rq_alloc_buf(struct vnic_rq *rq)
1205 {
1206 	struct enic *enic = vnic_dev_priv(rq->vdev);
1207 	struct net_device *netdev = enic->netdev;
1208 	struct sk_buff *skb;
1209 	unsigned int len = netdev->mtu + VLAN_ETH_HLEN;
1210 	unsigned int os_buf_index = 0;
1211 	dma_addr_t dma_addr;
1212 
1213 	skb = netdev_alloc_skb_ip_align(netdev, len);
1214 	if (!skb)
1215 		return -ENOMEM;
1216 
1217 	dma_addr = pci_map_single(enic->pdev, skb->data,
1218 		len, PCI_DMA_FROMDEVICE);
1219 
1220 	enic_queue_rq_desc(rq, skb, os_buf_index,
1221 		dma_addr, len);
1222 
1223 	return 0;
1224 }
1225 
enic_rq_indicate_buf(struct vnic_rq * rq,struct cq_desc * cq_desc,struct vnic_rq_buf * buf,int skipped,void * opaque)1226 static void enic_rq_indicate_buf(struct vnic_rq *rq,
1227 	struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
1228 	int skipped, void *opaque)
1229 {
1230 	struct enic *enic = vnic_dev_priv(rq->vdev);
1231 	struct net_device *netdev = enic->netdev;
1232 	struct sk_buff *skb;
1233 
1234 	u8 type, color, eop, sop, ingress_port, vlan_stripped;
1235 	u8 fcoe, fcoe_sof, fcoe_fc_crc_ok, fcoe_enc_error, fcoe_eof;
1236 	u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
1237 	u8 ipv6, ipv4, ipv4_fragment, fcs_ok, rss_type, csum_not_calc;
1238 	u8 packet_error;
1239 	u16 q_number, completed_index, bytes_written, vlan_tci, checksum;
1240 	u32 rss_hash;
1241 
1242 	if (skipped)
1243 		return;
1244 
1245 	skb = buf->os_buf;
1246 	prefetch(skb->data - NET_IP_ALIGN);
1247 	pci_unmap_single(enic->pdev, buf->dma_addr,
1248 		buf->len, PCI_DMA_FROMDEVICE);
1249 
1250 	cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
1251 		&type, &color, &q_number, &completed_index,
1252 		&ingress_port, &fcoe, &eop, &sop, &rss_type,
1253 		&csum_not_calc, &rss_hash, &bytes_written,
1254 		&packet_error, &vlan_stripped, &vlan_tci, &checksum,
1255 		&fcoe_sof, &fcoe_fc_crc_ok, &fcoe_enc_error,
1256 		&fcoe_eof, &tcp_udp_csum_ok, &udp, &tcp,
1257 		&ipv4_csum_ok, &ipv6, &ipv4, &ipv4_fragment,
1258 		&fcs_ok);
1259 
1260 	if (packet_error) {
1261 
1262 		if (!fcs_ok) {
1263 			if (bytes_written > 0)
1264 				enic->rq_bad_fcs++;
1265 			else if (bytes_written == 0)
1266 				enic->rq_truncated_pkts++;
1267 		}
1268 
1269 		dev_kfree_skb_any(skb);
1270 
1271 		return;
1272 	}
1273 
1274 	if (eop && bytes_written > 0) {
1275 
1276 		/* Good receive
1277 		 */
1278 
1279 		skb_put(skb, bytes_written);
1280 		skb->protocol = eth_type_trans(skb, netdev);
1281 
1282 		if ((netdev->features & NETIF_F_RXCSUM) && !csum_not_calc) {
1283 			skb->csum = htons(checksum);
1284 			skb->ip_summed = CHECKSUM_COMPLETE;
1285 		}
1286 
1287 		skb->dev = netdev;
1288 
1289 		if (vlan_stripped)
1290 			__vlan_hwaccel_put_tag(skb, vlan_tci);
1291 
1292 		if (netdev->features & NETIF_F_GRO)
1293 			napi_gro_receive(&enic->napi[q_number], skb);
1294 		else
1295 			netif_receive_skb(skb);
1296 	} else {
1297 
1298 		/* Buffer overflow
1299 		 */
1300 
1301 		dev_kfree_skb_any(skb);
1302 	}
1303 }
1304 
enic_rq_service(struct vnic_dev * vdev,struct cq_desc * cq_desc,u8 type,u16 q_number,u16 completed_index,void * opaque)1305 static int enic_rq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc,
1306 	u8 type, u16 q_number, u16 completed_index, void *opaque)
1307 {
1308 	struct enic *enic = vnic_dev_priv(vdev);
1309 
1310 	vnic_rq_service(&enic->rq[q_number], cq_desc,
1311 		completed_index, VNIC_RQ_RETURN_DESC,
1312 		enic_rq_indicate_buf, opaque);
1313 
1314 	return 0;
1315 }
1316 
enic_poll(struct napi_struct * napi,int budget)1317 static int enic_poll(struct napi_struct *napi, int budget)
1318 {
1319 	struct net_device *netdev = napi->dev;
1320 	struct enic *enic = netdev_priv(netdev);
1321 	unsigned int cq_rq = enic_cq_rq(enic, 0);
1322 	unsigned int cq_wq = enic_cq_wq(enic, 0);
1323 	unsigned int intr = enic_legacy_io_intr();
1324 	unsigned int rq_work_to_do = budget;
1325 	unsigned int wq_work_to_do = -1; /* no limit */
1326 	unsigned int  work_done, rq_work_done, wq_work_done;
1327 	int err;
1328 
1329 	/* Service RQ (first) and WQ
1330 	 */
1331 
1332 	rq_work_done = vnic_cq_service(&enic->cq[cq_rq],
1333 		rq_work_to_do, enic_rq_service, NULL);
1334 
1335 	wq_work_done = vnic_cq_service(&enic->cq[cq_wq],
1336 		wq_work_to_do, enic_wq_service, NULL);
1337 
1338 	/* Accumulate intr event credits for this polling
1339 	 * cycle.  An intr event is the completion of a
1340 	 * a WQ or RQ packet.
1341 	 */
1342 
1343 	work_done = rq_work_done + wq_work_done;
1344 
1345 	if (work_done > 0)
1346 		vnic_intr_return_credits(&enic->intr[intr],
1347 			work_done,
1348 			0 /* don't unmask intr */,
1349 			0 /* don't reset intr timer */);
1350 
1351 	err = vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1352 
1353 	/* Buffer allocation failed. Stay in polling
1354 	 * mode so we can try to fill the ring again.
1355 	 */
1356 
1357 	if (err)
1358 		rq_work_done = rq_work_to_do;
1359 
1360 	if (rq_work_done < rq_work_to_do) {
1361 
1362 		/* Some work done, but not enough to stay in polling,
1363 		 * exit polling
1364 		 */
1365 
1366 		napi_complete(napi);
1367 		vnic_intr_unmask(&enic->intr[intr]);
1368 	}
1369 
1370 	return rq_work_done;
1371 }
1372 
enic_poll_msix(struct napi_struct * napi,int budget)1373 static int enic_poll_msix(struct napi_struct *napi, int budget)
1374 {
1375 	struct net_device *netdev = napi->dev;
1376 	struct enic *enic = netdev_priv(netdev);
1377 	unsigned int rq = (napi - &enic->napi[0]);
1378 	unsigned int cq = enic_cq_rq(enic, rq);
1379 	unsigned int intr = enic_msix_rq_intr(enic, rq);
1380 	unsigned int work_to_do = budget;
1381 	unsigned int work_done;
1382 	int err;
1383 
1384 	/* Service RQ
1385 	 */
1386 
1387 	work_done = vnic_cq_service(&enic->cq[cq],
1388 		work_to_do, enic_rq_service, NULL);
1389 
1390 	/* Return intr event credits for this polling
1391 	 * cycle.  An intr event is the completion of a
1392 	 * RQ packet.
1393 	 */
1394 
1395 	if (work_done > 0)
1396 		vnic_intr_return_credits(&enic->intr[intr],
1397 			work_done,
1398 			0 /* don't unmask intr */,
1399 			0 /* don't reset intr timer */);
1400 
1401 	err = vnic_rq_fill(&enic->rq[rq], enic_rq_alloc_buf);
1402 
1403 	/* Buffer allocation failed. Stay in polling mode
1404 	 * so we can try to fill the ring again.
1405 	 */
1406 
1407 	if (err)
1408 		work_done = work_to_do;
1409 
1410 	if (work_done < work_to_do) {
1411 
1412 		/* Some work done, but not enough to stay in polling,
1413 		 * exit polling
1414 		 */
1415 
1416 		napi_complete(napi);
1417 		vnic_intr_unmask(&enic->intr[intr]);
1418 	}
1419 
1420 	return work_done;
1421 }
1422 
enic_notify_timer(unsigned long data)1423 static void enic_notify_timer(unsigned long data)
1424 {
1425 	struct enic *enic = (struct enic *)data;
1426 
1427 	enic_notify_check(enic);
1428 
1429 	mod_timer(&enic->notify_timer,
1430 		round_jiffies(jiffies + ENIC_NOTIFY_TIMER_PERIOD));
1431 }
1432 
enic_free_intr(struct enic * enic)1433 static void enic_free_intr(struct enic *enic)
1434 {
1435 	struct net_device *netdev = enic->netdev;
1436 	unsigned int i;
1437 
1438 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1439 	case VNIC_DEV_INTR_MODE_INTX:
1440 		free_irq(enic->pdev->irq, netdev);
1441 		break;
1442 	case VNIC_DEV_INTR_MODE_MSI:
1443 		free_irq(enic->pdev->irq, enic);
1444 		break;
1445 	case VNIC_DEV_INTR_MODE_MSIX:
1446 		for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1447 			if (enic->msix[i].requested)
1448 				free_irq(enic->msix_entry[i].vector,
1449 					enic->msix[i].devid);
1450 		break;
1451 	default:
1452 		break;
1453 	}
1454 }
1455 
enic_request_intr(struct enic * enic)1456 static int enic_request_intr(struct enic *enic)
1457 {
1458 	struct net_device *netdev = enic->netdev;
1459 	unsigned int i, intr;
1460 	int err = 0;
1461 
1462 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1463 
1464 	case VNIC_DEV_INTR_MODE_INTX:
1465 
1466 		err = request_irq(enic->pdev->irq, enic_isr_legacy,
1467 			IRQF_SHARED, netdev->name, netdev);
1468 		break;
1469 
1470 	case VNIC_DEV_INTR_MODE_MSI:
1471 
1472 		err = request_irq(enic->pdev->irq, enic_isr_msi,
1473 			0, netdev->name, enic);
1474 		break;
1475 
1476 	case VNIC_DEV_INTR_MODE_MSIX:
1477 
1478 		for (i = 0; i < enic->rq_count; i++) {
1479 			intr = enic_msix_rq_intr(enic, i);
1480 			sprintf(enic->msix[intr].devname,
1481 				"%.11s-rx-%d", netdev->name, i);
1482 			enic->msix[intr].isr = enic_isr_msix_rq;
1483 			enic->msix[intr].devid = &enic->napi[i];
1484 		}
1485 
1486 		for (i = 0; i < enic->wq_count; i++) {
1487 			intr = enic_msix_wq_intr(enic, i);
1488 			sprintf(enic->msix[intr].devname,
1489 				"%.11s-tx-%d", netdev->name, i);
1490 			enic->msix[intr].isr = enic_isr_msix_wq;
1491 			enic->msix[intr].devid = enic;
1492 		}
1493 
1494 		intr = enic_msix_err_intr(enic);
1495 		sprintf(enic->msix[intr].devname,
1496 			"%.11s-err", netdev->name);
1497 		enic->msix[intr].isr = enic_isr_msix_err;
1498 		enic->msix[intr].devid = enic;
1499 
1500 		intr = enic_msix_notify_intr(enic);
1501 		sprintf(enic->msix[intr].devname,
1502 			"%.11s-notify", netdev->name);
1503 		enic->msix[intr].isr = enic_isr_msix_notify;
1504 		enic->msix[intr].devid = enic;
1505 
1506 		for (i = 0; i < ARRAY_SIZE(enic->msix); i++)
1507 			enic->msix[i].requested = 0;
1508 
1509 		for (i = 0; i < enic->intr_count; i++) {
1510 			err = request_irq(enic->msix_entry[i].vector,
1511 				enic->msix[i].isr, 0,
1512 				enic->msix[i].devname,
1513 				enic->msix[i].devid);
1514 			if (err) {
1515 				enic_free_intr(enic);
1516 				break;
1517 			}
1518 			enic->msix[i].requested = 1;
1519 		}
1520 
1521 		break;
1522 
1523 	default:
1524 		break;
1525 	}
1526 
1527 	return err;
1528 }
1529 
enic_synchronize_irqs(struct enic * enic)1530 static void enic_synchronize_irqs(struct enic *enic)
1531 {
1532 	unsigned int i;
1533 
1534 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1535 	case VNIC_DEV_INTR_MODE_INTX:
1536 	case VNIC_DEV_INTR_MODE_MSI:
1537 		synchronize_irq(enic->pdev->irq);
1538 		break;
1539 	case VNIC_DEV_INTR_MODE_MSIX:
1540 		for (i = 0; i < enic->intr_count; i++)
1541 			synchronize_irq(enic->msix_entry[i].vector);
1542 		break;
1543 	default:
1544 		break;
1545 	}
1546 }
1547 
enic_dev_notify_set(struct enic * enic)1548 static int enic_dev_notify_set(struct enic *enic)
1549 {
1550 	int err;
1551 
1552 	spin_lock(&enic->devcmd_lock);
1553 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1554 	case VNIC_DEV_INTR_MODE_INTX:
1555 		err = vnic_dev_notify_set(enic->vdev,
1556 			enic_legacy_notify_intr());
1557 		break;
1558 	case VNIC_DEV_INTR_MODE_MSIX:
1559 		err = vnic_dev_notify_set(enic->vdev,
1560 			enic_msix_notify_intr(enic));
1561 		break;
1562 	default:
1563 		err = vnic_dev_notify_set(enic->vdev, -1 /* no intr */);
1564 		break;
1565 	}
1566 	spin_unlock(&enic->devcmd_lock);
1567 
1568 	return err;
1569 }
1570 
enic_notify_timer_start(struct enic * enic)1571 static void enic_notify_timer_start(struct enic *enic)
1572 {
1573 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
1574 	case VNIC_DEV_INTR_MODE_MSI:
1575 		mod_timer(&enic->notify_timer, jiffies);
1576 		break;
1577 	default:
1578 		/* Using intr for notification for INTx/MSI-X */
1579 		break;
1580 	}
1581 }
1582 
1583 /* rtnl lock is held, process context */
enic_open(struct net_device * netdev)1584 static int enic_open(struct net_device *netdev)
1585 {
1586 	struct enic *enic = netdev_priv(netdev);
1587 	unsigned int i;
1588 	int err;
1589 
1590 	err = enic_request_intr(enic);
1591 	if (err) {
1592 		netdev_err(netdev, "Unable to request irq.\n");
1593 		return err;
1594 	}
1595 
1596 	err = enic_dev_notify_set(enic);
1597 	if (err) {
1598 		netdev_err(netdev,
1599 			"Failed to alloc notify buffer, aborting.\n");
1600 		goto err_out_free_intr;
1601 	}
1602 
1603 	for (i = 0; i < enic->rq_count; i++) {
1604 		vnic_rq_fill(&enic->rq[i], enic_rq_alloc_buf);
1605 		/* Need at least one buffer on ring to get going */
1606 		if (vnic_rq_desc_used(&enic->rq[i]) == 0) {
1607 			netdev_err(netdev, "Unable to alloc receive buffers\n");
1608 			err = -ENOMEM;
1609 			goto err_out_notify_unset;
1610 		}
1611 	}
1612 
1613 	for (i = 0; i < enic->wq_count; i++)
1614 		vnic_wq_enable(&enic->wq[i]);
1615 	for (i = 0; i < enic->rq_count; i++)
1616 		vnic_rq_enable(&enic->rq[i]);
1617 
1618 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1619 		enic_dev_add_station_addr(enic);
1620 
1621 	enic_set_rx_mode(netdev);
1622 
1623 	netif_wake_queue(netdev);
1624 
1625 	for (i = 0; i < enic->rq_count; i++)
1626 		napi_enable(&enic->napi[i]);
1627 
1628 	enic_dev_enable(enic);
1629 
1630 	for (i = 0; i < enic->intr_count; i++)
1631 		vnic_intr_unmask(&enic->intr[i]);
1632 
1633 	enic_notify_timer_start(enic);
1634 
1635 	return 0;
1636 
1637 err_out_notify_unset:
1638 	enic_dev_notify_unset(enic);
1639 err_out_free_intr:
1640 	enic_free_intr(enic);
1641 
1642 	return err;
1643 }
1644 
1645 /* rtnl lock is held, process context */
enic_stop(struct net_device * netdev)1646 static int enic_stop(struct net_device *netdev)
1647 {
1648 	struct enic *enic = netdev_priv(netdev);
1649 	unsigned int i;
1650 	int err;
1651 
1652 	for (i = 0; i < enic->intr_count; i++) {
1653 		vnic_intr_mask(&enic->intr[i]);
1654 		(void)vnic_intr_masked(&enic->intr[i]); /* flush write */
1655 	}
1656 
1657 	enic_synchronize_irqs(enic);
1658 
1659 	del_timer_sync(&enic->notify_timer);
1660 
1661 	enic_dev_disable(enic);
1662 
1663 	for (i = 0; i < enic->rq_count; i++)
1664 		napi_disable(&enic->napi[i]);
1665 
1666 	netif_carrier_off(netdev);
1667 	netif_tx_disable(netdev);
1668 
1669 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
1670 		enic_dev_del_station_addr(enic);
1671 
1672 	for (i = 0; i < enic->wq_count; i++) {
1673 		err = vnic_wq_disable(&enic->wq[i]);
1674 		if (err)
1675 			return err;
1676 	}
1677 	for (i = 0; i < enic->rq_count; i++) {
1678 		err = vnic_rq_disable(&enic->rq[i]);
1679 		if (err)
1680 			return err;
1681 	}
1682 
1683 	enic_dev_notify_unset(enic);
1684 	enic_free_intr(enic);
1685 
1686 	for (i = 0; i < enic->wq_count; i++)
1687 		vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
1688 	for (i = 0; i < enic->rq_count; i++)
1689 		vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
1690 	for (i = 0; i < enic->cq_count; i++)
1691 		vnic_cq_clean(&enic->cq[i]);
1692 	for (i = 0; i < enic->intr_count; i++)
1693 		vnic_intr_clean(&enic->intr[i]);
1694 
1695 	return 0;
1696 }
1697 
enic_change_mtu(struct net_device * netdev,int new_mtu)1698 static int enic_change_mtu(struct net_device *netdev, int new_mtu)
1699 {
1700 	struct enic *enic = netdev_priv(netdev);
1701 	int running = netif_running(netdev);
1702 
1703 	if (new_mtu < ENIC_MIN_MTU || new_mtu > ENIC_MAX_MTU)
1704 		return -EINVAL;
1705 
1706 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
1707 		return -EOPNOTSUPP;
1708 
1709 	if (running)
1710 		enic_stop(netdev);
1711 
1712 	netdev->mtu = new_mtu;
1713 
1714 	if (netdev->mtu > enic->port_mtu)
1715 		netdev_warn(netdev,
1716 			"interface MTU (%d) set higher than port MTU (%d)\n",
1717 			netdev->mtu, enic->port_mtu);
1718 
1719 	if (running)
1720 		enic_open(netdev);
1721 
1722 	return 0;
1723 }
1724 
enic_change_mtu_work(struct work_struct * work)1725 static void enic_change_mtu_work(struct work_struct *work)
1726 {
1727 	struct enic *enic = container_of(work, struct enic, change_mtu_work);
1728 	struct net_device *netdev = enic->netdev;
1729 	int new_mtu = vnic_dev_mtu(enic->vdev);
1730 	int err;
1731 	unsigned int i;
1732 
1733 	new_mtu = max_t(int, ENIC_MIN_MTU, min_t(int, ENIC_MAX_MTU, new_mtu));
1734 
1735 	rtnl_lock();
1736 
1737 	/* Stop RQ */
1738 	del_timer_sync(&enic->notify_timer);
1739 
1740 	for (i = 0; i < enic->rq_count; i++)
1741 		napi_disable(&enic->napi[i]);
1742 
1743 	vnic_intr_mask(&enic->intr[0]);
1744 	enic_synchronize_irqs(enic);
1745 	err = vnic_rq_disable(&enic->rq[0]);
1746 	if (err) {
1747 		netdev_err(netdev, "Unable to disable RQ.\n");
1748 		return;
1749 	}
1750 	vnic_rq_clean(&enic->rq[0], enic_free_rq_buf);
1751 	vnic_cq_clean(&enic->cq[0]);
1752 	vnic_intr_clean(&enic->intr[0]);
1753 
1754 	/* Fill RQ with new_mtu-sized buffers */
1755 	netdev->mtu = new_mtu;
1756 	vnic_rq_fill(&enic->rq[0], enic_rq_alloc_buf);
1757 	/* Need at least one buffer on ring to get going */
1758 	if (vnic_rq_desc_used(&enic->rq[0]) == 0) {
1759 		netdev_err(netdev, "Unable to alloc receive buffers.\n");
1760 		return;
1761 	}
1762 
1763 	/* Start RQ */
1764 	vnic_rq_enable(&enic->rq[0]);
1765 	napi_enable(&enic->napi[0]);
1766 	vnic_intr_unmask(&enic->intr[0]);
1767 	enic_notify_timer_start(enic);
1768 
1769 	rtnl_unlock();
1770 
1771 	netdev_info(netdev, "interface MTU set as %d\n", netdev->mtu);
1772 }
1773 
1774 #ifdef CONFIG_NET_POLL_CONTROLLER
enic_poll_controller(struct net_device * netdev)1775 static void enic_poll_controller(struct net_device *netdev)
1776 {
1777 	struct enic *enic = netdev_priv(netdev);
1778 	struct vnic_dev *vdev = enic->vdev;
1779 	unsigned int i, intr;
1780 
1781 	switch (vnic_dev_get_intr_mode(vdev)) {
1782 	case VNIC_DEV_INTR_MODE_MSIX:
1783 		for (i = 0; i < enic->rq_count; i++) {
1784 			intr = enic_msix_rq_intr(enic, i);
1785 			enic_isr_msix_rq(enic->msix_entry[intr].vector,
1786 				&enic->napi[i]);
1787 		}
1788 
1789 		for (i = 0; i < enic->wq_count; i++) {
1790 			intr = enic_msix_wq_intr(enic, i);
1791 			enic_isr_msix_wq(enic->msix_entry[intr].vector, enic);
1792 		}
1793 
1794 		break;
1795 	case VNIC_DEV_INTR_MODE_MSI:
1796 		enic_isr_msi(enic->pdev->irq, enic);
1797 		break;
1798 	case VNIC_DEV_INTR_MODE_INTX:
1799 		enic_isr_legacy(enic->pdev->irq, netdev);
1800 		break;
1801 	default:
1802 		break;
1803 	}
1804 }
1805 #endif
1806 
enic_dev_wait(struct vnic_dev * vdev,int (* start)(struct vnic_dev *,int),int (* finished)(struct vnic_dev *,int *),int arg)1807 static int enic_dev_wait(struct vnic_dev *vdev,
1808 	int (*start)(struct vnic_dev *, int),
1809 	int (*finished)(struct vnic_dev *, int *),
1810 	int arg)
1811 {
1812 	unsigned long time;
1813 	int done;
1814 	int err;
1815 
1816 	BUG_ON(in_interrupt());
1817 
1818 	err = start(vdev, arg);
1819 	if (err)
1820 		return err;
1821 
1822 	/* Wait for func to complete...2 seconds max
1823 	 */
1824 
1825 	time = jiffies + (HZ * 2);
1826 	do {
1827 
1828 		err = finished(vdev, &done);
1829 		if (err)
1830 			return err;
1831 
1832 		if (done)
1833 			return 0;
1834 
1835 		schedule_timeout_uninterruptible(HZ / 10);
1836 
1837 	} while (time_after(time, jiffies));
1838 
1839 	return -ETIMEDOUT;
1840 }
1841 
enic_dev_open(struct enic * enic)1842 static int enic_dev_open(struct enic *enic)
1843 {
1844 	int err;
1845 
1846 	err = enic_dev_wait(enic->vdev, vnic_dev_open,
1847 		vnic_dev_open_done, 0);
1848 	if (err)
1849 		dev_err(enic_get_dev(enic), "vNIC device open failed, err %d\n",
1850 			err);
1851 
1852 	return err;
1853 }
1854 
enic_dev_hang_reset(struct enic * enic)1855 static int enic_dev_hang_reset(struct enic *enic)
1856 {
1857 	int err;
1858 
1859 	err = enic_dev_wait(enic->vdev, vnic_dev_hang_reset,
1860 		vnic_dev_hang_reset_done, 0);
1861 	if (err)
1862 		netdev_err(enic->netdev, "vNIC hang reset failed, err %d\n",
1863 			err);
1864 
1865 	return err;
1866 }
1867 
enic_set_rsskey(struct enic * enic)1868 static int enic_set_rsskey(struct enic *enic)
1869 {
1870 	dma_addr_t rss_key_buf_pa;
1871 	union vnic_rss_key *rss_key_buf_va = NULL;
1872 	union vnic_rss_key rss_key = {
1873 		.key[0].b = {85, 67, 83, 97, 119, 101, 115, 111, 109, 101},
1874 		.key[1].b = {80, 65, 76, 79, 117, 110, 105, 113, 117, 101},
1875 		.key[2].b = {76, 73, 78, 85, 88, 114, 111, 99, 107, 115},
1876 		.key[3].b = {69, 78, 73, 67, 105, 115, 99, 111, 111, 108},
1877 	};
1878 	int err;
1879 
1880 	rss_key_buf_va = pci_alloc_consistent(enic->pdev,
1881 		sizeof(union vnic_rss_key), &rss_key_buf_pa);
1882 	if (!rss_key_buf_va)
1883 		return -ENOMEM;
1884 
1885 	memcpy(rss_key_buf_va, &rss_key, sizeof(union vnic_rss_key));
1886 
1887 	spin_lock(&enic->devcmd_lock);
1888 	err = enic_set_rss_key(enic,
1889 		rss_key_buf_pa,
1890 		sizeof(union vnic_rss_key));
1891 	spin_unlock(&enic->devcmd_lock);
1892 
1893 	pci_free_consistent(enic->pdev, sizeof(union vnic_rss_key),
1894 		rss_key_buf_va, rss_key_buf_pa);
1895 
1896 	return err;
1897 }
1898 
enic_set_rsscpu(struct enic * enic,u8 rss_hash_bits)1899 static int enic_set_rsscpu(struct enic *enic, u8 rss_hash_bits)
1900 {
1901 	dma_addr_t rss_cpu_buf_pa;
1902 	union vnic_rss_cpu *rss_cpu_buf_va = NULL;
1903 	unsigned int i;
1904 	int err;
1905 
1906 	rss_cpu_buf_va = pci_alloc_consistent(enic->pdev,
1907 		sizeof(union vnic_rss_cpu), &rss_cpu_buf_pa);
1908 	if (!rss_cpu_buf_va)
1909 		return -ENOMEM;
1910 
1911 	for (i = 0; i < (1 << rss_hash_bits); i++)
1912 		(*rss_cpu_buf_va).cpu[i/4].b[i%4] = i % enic->rq_count;
1913 
1914 	spin_lock(&enic->devcmd_lock);
1915 	err = enic_set_rss_cpu(enic,
1916 		rss_cpu_buf_pa,
1917 		sizeof(union vnic_rss_cpu));
1918 	spin_unlock(&enic->devcmd_lock);
1919 
1920 	pci_free_consistent(enic->pdev, sizeof(union vnic_rss_cpu),
1921 		rss_cpu_buf_va, rss_cpu_buf_pa);
1922 
1923 	return err;
1924 }
1925 
enic_set_niccfg(struct enic * enic,u8 rss_default_cpu,u8 rss_hash_type,u8 rss_hash_bits,u8 rss_base_cpu,u8 rss_enable)1926 static int enic_set_niccfg(struct enic *enic, u8 rss_default_cpu,
1927 	u8 rss_hash_type, u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable)
1928 {
1929 	const u8 tso_ipid_split_en = 0;
1930 	const u8 ig_vlan_strip_en = 1;
1931 	int err;
1932 
1933 	/* Enable VLAN tag stripping.
1934 	*/
1935 
1936 	spin_lock(&enic->devcmd_lock);
1937 	err = enic_set_nic_cfg(enic,
1938 		rss_default_cpu, rss_hash_type,
1939 		rss_hash_bits, rss_base_cpu,
1940 		rss_enable, tso_ipid_split_en,
1941 		ig_vlan_strip_en);
1942 	spin_unlock(&enic->devcmd_lock);
1943 
1944 	return err;
1945 }
1946 
enic_set_rss_nic_cfg(struct enic * enic)1947 static int enic_set_rss_nic_cfg(struct enic *enic)
1948 {
1949 	struct device *dev = enic_get_dev(enic);
1950 	const u8 rss_default_cpu = 0;
1951 	const u8 rss_hash_type = NIC_CFG_RSS_HASH_TYPE_IPV4 |
1952 		NIC_CFG_RSS_HASH_TYPE_TCP_IPV4 |
1953 		NIC_CFG_RSS_HASH_TYPE_IPV6 |
1954 		NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
1955 	const u8 rss_hash_bits = 7;
1956 	const u8 rss_base_cpu = 0;
1957 	u8 rss_enable = ENIC_SETTING(enic, RSS) && (enic->rq_count > 1);
1958 
1959 	if (rss_enable) {
1960 		if (!enic_set_rsskey(enic)) {
1961 			if (enic_set_rsscpu(enic, rss_hash_bits)) {
1962 				rss_enable = 0;
1963 				dev_warn(dev, "RSS disabled, "
1964 					"Failed to set RSS cpu indirection table.");
1965 			}
1966 		} else {
1967 			rss_enable = 0;
1968 			dev_warn(dev, "RSS disabled, Failed to set RSS key.\n");
1969 		}
1970 	}
1971 
1972 	return enic_set_niccfg(enic, rss_default_cpu, rss_hash_type,
1973 		rss_hash_bits, rss_base_cpu, rss_enable);
1974 }
1975 
enic_reset(struct work_struct * work)1976 static void enic_reset(struct work_struct *work)
1977 {
1978 	struct enic *enic = container_of(work, struct enic, reset);
1979 
1980 	if (!netif_running(enic->netdev))
1981 		return;
1982 
1983 	rtnl_lock();
1984 
1985 	enic_dev_hang_notify(enic);
1986 	enic_stop(enic->netdev);
1987 	enic_dev_hang_reset(enic);
1988 	enic_reset_addr_lists(enic);
1989 	enic_init_vnic_resources(enic);
1990 	enic_set_rss_nic_cfg(enic);
1991 	enic_dev_set_ig_vlan_rewrite_mode(enic);
1992 	enic_open(enic->netdev);
1993 
1994 	rtnl_unlock();
1995 }
1996 
enic_set_intr_mode(struct enic * enic)1997 static int enic_set_intr_mode(struct enic *enic)
1998 {
1999 	unsigned int n = min_t(unsigned int, enic->rq_count, ENIC_RQ_MAX);
2000 	unsigned int m = min_t(unsigned int, enic->wq_count, ENIC_WQ_MAX);
2001 	unsigned int i;
2002 
2003 	/* Set interrupt mode (INTx, MSI, MSI-X) depending
2004 	 * on system capabilities.
2005 	 *
2006 	 * Try MSI-X first
2007 	 *
2008 	 * We need n RQs, m WQs, n+m CQs, and n+m+2 INTRs
2009 	 * (the second to last INTR is used for WQ/RQ errors)
2010 	 * (the last INTR is used for notifications)
2011 	 */
2012 
2013 	BUG_ON(ARRAY_SIZE(enic->msix_entry) < n + m + 2);
2014 	for (i = 0; i < n + m + 2; i++)
2015 		enic->msix_entry[i].entry = i;
2016 
2017 	/* Use multiple RQs if RSS is enabled
2018 	 */
2019 
2020 	if (ENIC_SETTING(enic, RSS) &&
2021 	    enic->config.intr_mode < 1 &&
2022 	    enic->rq_count >= n &&
2023 	    enic->wq_count >= m &&
2024 	    enic->cq_count >= n + m &&
2025 	    enic->intr_count >= n + m + 2) {
2026 
2027 		if (!pci_enable_msix(enic->pdev, enic->msix_entry, n + m + 2)) {
2028 
2029 			enic->rq_count = n;
2030 			enic->wq_count = m;
2031 			enic->cq_count = n + m;
2032 			enic->intr_count = n + m + 2;
2033 
2034 			vnic_dev_set_intr_mode(enic->vdev,
2035 				VNIC_DEV_INTR_MODE_MSIX);
2036 
2037 			return 0;
2038 		}
2039 	}
2040 
2041 	if (enic->config.intr_mode < 1 &&
2042 	    enic->rq_count >= 1 &&
2043 	    enic->wq_count >= m &&
2044 	    enic->cq_count >= 1 + m &&
2045 	    enic->intr_count >= 1 + m + 2) {
2046 		if (!pci_enable_msix(enic->pdev, enic->msix_entry, 1 + m + 2)) {
2047 
2048 			enic->rq_count = 1;
2049 			enic->wq_count = m;
2050 			enic->cq_count = 1 + m;
2051 			enic->intr_count = 1 + m + 2;
2052 
2053 			vnic_dev_set_intr_mode(enic->vdev,
2054 				VNIC_DEV_INTR_MODE_MSIX);
2055 
2056 			return 0;
2057 		}
2058 	}
2059 
2060 	/* Next try MSI
2061 	 *
2062 	 * We need 1 RQ, 1 WQ, 2 CQs, and 1 INTR
2063 	 */
2064 
2065 	if (enic->config.intr_mode < 2 &&
2066 	    enic->rq_count >= 1 &&
2067 	    enic->wq_count >= 1 &&
2068 	    enic->cq_count >= 2 &&
2069 	    enic->intr_count >= 1 &&
2070 	    !pci_enable_msi(enic->pdev)) {
2071 
2072 		enic->rq_count = 1;
2073 		enic->wq_count = 1;
2074 		enic->cq_count = 2;
2075 		enic->intr_count = 1;
2076 
2077 		vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_MSI);
2078 
2079 		return 0;
2080 	}
2081 
2082 	/* Next try INTx
2083 	 *
2084 	 * We need 1 RQ, 1 WQ, 2 CQs, and 3 INTRs
2085 	 * (the first INTR is used for WQ/RQ)
2086 	 * (the second INTR is used for WQ/RQ errors)
2087 	 * (the last INTR is used for notifications)
2088 	 */
2089 
2090 	if (enic->config.intr_mode < 3 &&
2091 	    enic->rq_count >= 1 &&
2092 	    enic->wq_count >= 1 &&
2093 	    enic->cq_count >= 2 &&
2094 	    enic->intr_count >= 3) {
2095 
2096 		enic->rq_count = 1;
2097 		enic->wq_count = 1;
2098 		enic->cq_count = 2;
2099 		enic->intr_count = 3;
2100 
2101 		vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_INTX);
2102 
2103 		return 0;
2104 	}
2105 
2106 	vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2107 
2108 	return -EINVAL;
2109 }
2110 
enic_clear_intr_mode(struct enic * enic)2111 static void enic_clear_intr_mode(struct enic *enic)
2112 {
2113 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
2114 	case VNIC_DEV_INTR_MODE_MSIX:
2115 		pci_disable_msix(enic->pdev);
2116 		break;
2117 	case VNIC_DEV_INTR_MODE_MSI:
2118 		pci_disable_msi(enic->pdev);
2119 		break;
2120 	default:
2121 		break;
2122 	}
2123 
2124 	vnic_dev_set_intr_mode(enic->vdev, VNIC_DEV_INTR_MODE_UNKNOWN);
2125 }
2126 
2127 static const struct net_device_ops enic_netdev_dynamic_ops = {
2128 	.ndo_open		= enic_open,
2129 	.ndo_stop		= enic_stop,
2130 	.ndo_start_xmit		= enic_hard_start_xmit,
2131 	.ndo_get_stats64	= enic_get_stats,
2132 	.ndo_validate_addr	= eth_validate_addr,
2133 	.ndo_set_rx_mode	= enic_set_rx_mode,
2134 	.ndo_set_mac_address	= enic_set_mac_address_dynamic,
2135 	.ndo_change_mtu		= enic_change_mtu,
2136 	.ndo_vlan_rx_add_vid	= enic_vlan_rx_add_vid,
2137 	.ndo_vlan_rx_kill_vid	= enic_vlan_rx_kill_vid,
2138 	.ndo_tx_timeout		= enic_tx_timeout,
2139 	.ndo_set_vf_port	= enic_set_vf_port,
2140 	.ndo_get_vf_port	= enic_get_vf_port,
2141 	.ndo_set_vf_mac		= enic_set_vf_mac,
2142 #ifdef CONFIG_NET_POLL_CONTROLLER
2143 	.ndo_poll_controller	= enic_poll_controller,
2144 #endif
2145 };
2146 
2147 static const struct net_device_ops enic_netdev_ops = {
2148 	.ndo_open		= enic_open,
2149 	.ndo_stop		= enic_stop,
2150 	.ndo_start_xmit		= enic_hard_start_xmit,
2151 	.ndo_get_stats64	= enic_get_stats,
2152 	.ndo_validate_addr	= eth_validate_addr,
2153 	.ndo_set_mac_address	= enic_set_mac_address,
2154 	.ndo_set_rx_mode	= enic_set_rx_mode,
2155 	.ndo_change_mtu		= enic_change_mtu,
2156 	.ndo_vlan_rx_add_vid	= enic_vlan_rx_add_vid,
2157 	.ndo_vlan_rx_kill_vid	= enic_vlan_rx_kill_vid,
2158 	.ndo_tx_timeout		= enic_tx_timeout,
2159 	.ndo_set_vf_port	= enic_set_vf_port,
2160 	.ndo_get_vf_port	= enic_get_vf_port,
2161 	.ndo_set_vf_mac		= enic_set_vf_mac,
2162 #ifdef CONFIG_NET_POLL_CONTROLLER
2163 	.ndo_poll_controller	= enic_poll_controller,
2164 #endif
2165 };
2166 
enic_dev_deinit(struct enic * enic)2167 static void enic_dev_deinit(struct enic *enic)
2168 {
2169 	unsigned int i;
2170 
2171 	for (i = 0; i < enic->rq_count; i++)
2172 		netif_napi_del(&enic->napi[i]);
2173 
2174 	enic_free_vnic_resources(enic);
2175 	enic_clear_intr_mode(enic);
2176 }
2177 
enic_dev_init(struct enic * enic)2178 static int enic_dev_init(struct enic *enic)
2179 {
2180 	struct device *dev = enic_get_dev(enic);
2181 	struct net_device *netdev = enic->netdev;
2182 	unsigned int i;
2183 	int err;
2184 
2185 	/* Get interrupt coalesce timer info */
2186 	err = enic_dev_intr_coal_timer_info(enic);
2187 	if (err) {
2188 		dev_warn(dev, "Using default conversion factor for "
2189 			"interrupt coalesce timer\n");
2190 		vnic_dev_intr_coal_timer_info_default(enic->vdev);
2191 	}
2192 
2193 	/* Get vNIC configuration
2194 	 */
2195 
2196 	err = enic_get_vnic_config(enic);
2197 	if (err) {
2198 		dev_err(dev, "Get vNIC configuration failed, aborting\n");
2199 		return err;
2200 	}
2201 
2202 	/* Get available resource counts
2203 	 */
2204 
2205 	enic_get_res_counts(enic);
2206 
2207 	/* Set interrupt mode based on resource counts and system
2208 	 * capabilities
2209 	 */
2210 
2211 	err = enic_set_intr_mode(enic);
2212 	if (err) {
2213 		dev_err(dev, "Failed to set intr mode based on resource "
2214 			"counts and system capabilities, aborting\n");
2215 		return err;
2216 	}
2217 
2218 	/* Allocate and configure vNIC resources
2219 	 */
2220 
2221 	err = enic_alloc_vnic_resources(enic);
2222 	if (err) {
2223 		dev_err(dev, "Failed to alloc vNIC resources, aborting\n");
2224 		goto err_out_free_vnic_resources;
2225 	}
2226 
2227 	enic_init_vnic_resources(enic);
2228 
2229 	err = enic_set_rss_nic_cfg(enic);
2230 	if (err) {
2231 		dev_err(dev, "Failed to config nic, aborting\n");
2232 		goto err_out_free_vnic_resources;
2233 	}
2234 
2235 	switch (vnic_dev_get_intr_mode(enic->vdev)) {
2236 	default:
2237 		netif_napi_add(netdev, &enic->napi[0], enic_poll, 64);
2238 		break;
2239 	case VNIC_DEV_INTR_MODE_MSIX:
2240 		for (i = 0; i < enic->rq_count; i++)
2241 			netif_napi_add(netdev, &enic->napi[i],
2242 				enic_poll_msix, 64);
2243 		break;
2244 	}
2245 
2246 	return 0;
2247 
2248 err_out_free_vnic_resources:
2249 	enic_clear_intr_mode(enic);
2250 	enic_free_vnic_resources(enic);
2251 
2252 	return err;
2253 }
2254 
enic_iounmap(struct enic * enic)2255 static void enic_iounmap(struct enic *enic)
2256 {
2257 	unsigned int i;
2258 
2259 	for (i = 0; i < ARRAY_SIZE(enic->bar); i++)
2260 		if (enic->bar[i].vaddr)
2261 			iounmap(enic->bar[i].vaddr);
2262 }
2263 
enic_probe(struct pci_dev * pdev,const struct pci_device_id * ent)2264 static int __devinit enic_probe(struct pci_dev *pdev,
2265 	const struct pci_device_id *ent)
2266 {
2267 	struct device *dev = &pdev->dev;
2268 	struct net_device *netdev;
2269 	struct enic *enic;
2270 	int using_dac = 0;
2271 	unsigned int i;
2272 	int err;
2273 #ifdef CONFIG_PCI_IOV
2274 	int pos = 0;
2275 #endif
2276 	int num_pps = 1;
2277 
2278 	/* Allocate net device structure and initialize.  Private
2279 	 * instance data is initialized to zero.
2280 	 */
2281 
2282 	netdev = alloc_etherdev(sizeof(struct enic));
2283 	if (!netdev) {
2284 		pr_err("Etherdev alloc failed, aborting\n");
2285 		return -ENOMEM;
2286 	}
2287 
2288 	pci_set_drvdata(pdev, netdev);
2289 
2290 	SET_NETDEV_DEV(netdev, &pdev->dev);
2291 
2292 	enic = netdev_priv(netdev);
2293 	enic->netdev = netdev;
2294 	enic->pdev = pdev;
2295 
2296 	/* Setup PCI resources
2297 	 */
2298 
2299 	err = pci_enable_device_mem(pdev);
2300 	if (err) {
2301 		dev_err(dev, "Cannot enable PCI device, aborting\n");
2302 		goto err_out_free_netdev;
2303 	}
2304 
2305 	err = pci_request_regions(pdev, DRV_NAME);
2306 	if (err) {
2307 		dev_err(dev, "Cannot request PCI regions, aborting\n");
2308 		goto err_out_disable_device;
2309 	}
2310 
2311 	pci_set_master(pdev);
2312 
2313 	/* Query PCI controller on system for DMA addressing
2314 	 * limitation for the device.  Try 40-bit first, and
2315 	 * fail to 32-bit.
2316 	 */
2317 
2318 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
2319 	if (err) {
2320 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2321 		if (err) {
2322 			dev_err(dev, "No usable DMA configuration, aborting\n");
2323 			goto err_out_release_regions;
2324 		}
2325 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2326 		if (err) {
2327 			dev_err(dev, "Unable to obtain %u-bit DMA "
2328 				"for consistent allocations, aborting\n", 32);
2329 			goto err_out_release_regions;
2330 		}
2331 	} else {
2332 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
2333 		if (err) {
2334 			dev_err(dev, "Unable to obtain %u-bit DMA "
2335 				"for consistent allocations, aborting\n", 40);
2336 			goto err_out_release_regions;
2337 		}
2338 		using_dac = 1;
2339 	}
2340 
2341 	/* Map vNIC resources from BAR0-5
2342 	 */
2343 
2344 	for (i = 0; i < ARRAY_SIZE(enic->bar); i++) {
2345 		if (!(pci_resource_flags(pdev, i) & IORESOURCE_MEM))
2346 			continue;
2347 		enic->bar[i].len = pci_resource_len(pdev, i);
2348 		enic->bar[i].vaddr = pci_iomap(pdev, i, enic->bar[i].len);
2349 		if (!enic->bar[i].vaddr) {
2350 			dev_err(dev, "Cannot memory-map BAR %d, aborting\n", i);
2351 			err = -ENODEV;
2352 			goto err_out_iounmap;
2353 		}
2354 		enic->bar[i].bus_addr = pci_resource_start(pdev, i);
2355 	}
2356 
2357 	/* Register vNIC device
2358 	 */
2359 
2360 	enic->vdev = vnic_dev_register(NULL, enic, pdev, enic->bar,
2361 		ARRAY_SIZE(enic->bar));
2362 	if (!enic->vdev) {
2363 		dev_err(dev, "vNIC registration failed, aborting\n");
2364 		err = -ENODEV;
2365 		goto err_out_iounmap;
2366 	}
2367 
2368 #ifdef CONFIG_PCI_IOV
2369 	/* Get number of subvnics */
2370 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
2371 	if (pos) {
2372 		pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF,
2373 			&enic->num_vfs);
2374 		if (enic->num_vfs) {
2375 			err = pci_enable_sriov(pdev, enic->num_vfs);
2376 			if (err) {
2377 				dev_err(dev, "SRIOV enable failed, aborting."
2378 					" pci_enable_sriov() returned %d\n",
2379 					err);
2380 				goto err_out_vnic_unregister;
2381 			}
2382 			enic->priv_flags |= ENIC_SRIOV_ENABLED;
2383 			num_pps = enic->num_vfs;
2384 		}
2385 	}
2386 #endif
2387 
2388 	/* Allocate structure for port profiles */
2389 	enic->pp = kcalloc(num_pps, sizeof(*enic->pp), GFP_KERNEL);
2390 	if (!enic->pp) {
2391 		pr_err("port profile alloc failed, aborting\n");
2392 		err = -ENOMEM;
2393 		goto err_out_disable_sriov_pp;
2394 	}
2395 
2396 	/* Issue device open to get device in known state
2397 	 */
2398 
2399 	err = enic_dev_open(enic);
2400 	if (err) {
2401 		dev_err(dev, "vNIC dev open failed, aborting\n");
2402 		goto err_out_disable_sriov;
2403 	}
2404 
2405 	/* Setup devcmd lock
2406 	 */
2407 
2408 	spin_lock_init(&enic->devcmd_lock);
2409 
2410 	/*
2411 	 * Set ingress vlan rewrite mode before vnic initialization
2412 	 */
2413 
2414 	err = enic_dev_set_ig_vlan_rewrite_mode(enic);
2415 	if (err) {
2416 		dev_err(dev,
2417 			"Failed to set ingress vlan rewrite mode, aborting.\n");
2418 		goto err_out_dev_close;
2419 	}
2420 
2421 	/* Issue device init to initialize the vnic-to-switch link.
2422 	 * We'll start with carrier off and wait for link UP
2423 	 * notification later to turn on carrier.  We don't need
2424 	 * to wait here for the vnic-to-switch link initialization
2425 	 * to complete; link UP notification is the indication that
2426 	 * the process is complete.
2427 	 */
2428 
2429 	netif_carrier_off(netdev);
2430 
2431 	/* Do not call dev_init for a dynamic vnic.
2432 	 * For a dynamic vnic, init_prov_info will be
2433 	 * called later by an upper layer.
2434 	 */
2435 
2436 	if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic)) {
2437 		err = vnic_dev_init(enic->vdev, 0);
2438 		if (err) {
2439 			dev_err(dev, "vNIC dev init failed, aborting\n");
2440 			goto err_out_dev_close;
2441 		}
2442 	}
2443 
2444 	err = enic_dev_init(enic);
2445 	if (err) {
2446 		dev_err(dev, "Device initialization failed, aborting\n");
2447 		goto err_out_dev_close;
2448 	}
2449 
2450 	/* Setup notification timer, HW reset task, and wq locks
2451 	 */
2452 
2453 	init_timer(&enic->notify_timer);
2454 	enic->notify_timer.function = enic_notify_timer;
2455 	enic->notify_timer.data = (unsigned long)enic;
2456 
2457 	INIT_WORK(&enic->reset, enic_reset);
2458 	INIT_WORK(&enic->change_mtu_work, enic_change_mtu_work);
2459 
2460 	for (i = 0; i < enic->wq_count; i++)
2461 		spin_lock_init(&enic->wq_lock[i]);
2462 
2463 	/* Register net device
2464 	 */
2465 
2466 	enic->port_mtu = enic->config.mtu;
2467 	(void)enic_change_mtu(netdev, enic->port_mtu);
2468 
2469 #ifdef CONFIG_PCI_IOV
2470 	if (enic_is_sriov_vf(enic) && is_zero_ether_addr(enic->mac_addr))
2471 		random_ether_addr(enic->mac_addr);
2472 #endif
2473 
2474 	err = enic_set_mac_addr(netdev, enic->mac_addr);
2475 	if (err) {
2476 		dev_err(dev, "Invalid MAC address, aborting\n");
2477 		goto err_out_dev_deinit;
2478 	}
2479 
2480 	enic->tx_coalesce_usecs = enic->config.intr_timer_usec;
2481 	enic->rx_coalesce_usecs = enic->tx_coalesce_usecs;
2482 
2483 	if (enic_is_dynamic(enic) || enic_is_sriov_vf(enic))
2484 		netdev->netdev_ops = &enic_netdev_dynamic_ops;
2485 	else
2486 		netdev->netdev_ops = &enic_netdev_ops;
2487 
2488 	netdev->watchdog_timeo = 2 * HZ;
2489 	netdev->ethtool_ops = &enic_ethtool_ops;
2490 
2491 	netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
2492 	if (ENIC_SETTING(enic, LOOP)) {
2493 		netdev->features &= ~NETIF_F_HW_VLAN_TX;
2494 		enic->loop_enable = 1;
2495 		enic->loop_tag = enic->config.loop_tag;
2496 		dev_info(dev, "loopback tag=0x%04x\n", enic->loop_tag);
2497 	}
2498 	if (ENIC_SETTING(enic, TXCSUM))
2499 		netdev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
2500 	if (ENIC_SETTING(enic, TSO))
2501 		netdev->hw_features |= NETIF_F_TSO |
2502 			NETIF_F_TSO6 | NETIF_F_TSO_ECN;
2503 	if (ENIC_SETTING(enic, RXCSUM))
2504 		netdev->hw_features |= NETIF_F_RXCSUM;
2505 
2506 	netdev->features |= netdev->hw_features;
2507 
2508 	if (using_dac)
2509 		netdev->features |= NETIF_F_HIGHDMA;
2510 
2511 	netdev->priv_flags |= IFF_UNICAST_FLT;
2512 
2513 	err = register_netdev(netdev);
2514 	if (err) {
2515 		dev_err(dev, "Cannot register net device, aborting\n");
2516 		goto err_out_dev_deinit;
2517 	}
2518 
2519 	return 0;
2520 
2521 err_out_dev_deinit:
2522 	enic_dev_deinit(enic);
2523 err_out_dev_close:
2524 	vnic_dev_close(enic->vdev);
2525 err_out_disable_sriov:
2526 	kfree(enic->pp);
2527 err_out_disable_sriov_pp:
2528 #ifdef CONFIG_PCI_IOV
2529 	if (enic_sriov_enabled(enic)) {
2530 		pci_disable_sriov(pdev);
2531 		enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2532 	}
2533 err_out_vnic_unregister:
2534 #endif
2535 	vnic_dev_unregister(enic->vdev);
2536 err_out_iounmap:
2537 	enic_iounmap(enic);
2538 err_out_release_regions:
2539 	pci_release_regions(pdev);
2540 err_out_disable_device:
2541 	pci_disable_device(pdev);
2542 err_out_free_netdev:
2543 	pci_set_drvdata(pdev, NULL);
2544 	free_netdev(netdev);
2545 
2546 	return err;
2547 }
2548 
enic_remove(struct pci_dev * pdev)2549 static void __devexit enic_remove(struct pci_dev *pdev)
2550 {
2551 	struct net_device *netdev = pci_get_drvdata(pdev);
2552 
2553 	if (netdev) {
2554 		struct enic *enic = netdev_priv(netdev);
2555 
2556 		cancel_work_sync(&enic->reset);
2557 		cancel_work_sync(&enic->change_mtu_work);
2558 		unregister_netdev(netdev);
2559 		enic_dev_deinit(enic);
2560 		vnic_dev_close(enic->vdev);
2561 #ifdef CONFIG_PCI_IOV
2562 		if (enic_sriov_enabled(enic)) {
2563 			pci_disable_sriov(pdev);
2564 			enic->priv_flags &= ~ENIC_SRIOV_ENABLED;
2565 		}
2566 #endif
2567 		kfree(enic->pp);
2568 		vnic_dev_unregister(enic->vdev);
2569 		enic_iounmap(enic);
2570 		pci_release_regions(pdev);
2571 		pci_disable_device(pdev);
2572 		pci_set_drvdata(pdev, NULL);
2573 		free_netdev(netdev);
2574 	}
2575 }
2576 
2577 static struct pci_driver enic_driver = {
2578 	.name = DRV_NAME,
2579 	.id_table = enic_id_table,
2580 	.probe = enic_probe,
2581 	.remove = __devexit_p(enic_remove),
2582 };
2583 
enic_init_module(void)2584 static int __init enic_init_module(void)
2585 {
2586 	pr_info("%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
2587 
2588 	return pci_register_driver(&enic_driver);
2589 }
2590 
enic_cleanup_module(void)2591 static void __exit enic_cleanup_module(void)
2592 {
2593 	pci_unregister_driver(&enic_driver);
2594 }
2595 
2596 module_init(enic_init_module);
2597 module_exit(enic_cleanup_module);
2598