1 /*
2  * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
3  * driver for Linux.
4  *
5  * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35 
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/init.h>
41 #include <linux/pci.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/netdevice.h>
44 #include <linux/etherdevice.h>
45 #include <linux/debugfs.h>
46 #include <linux/ethtool.h>
47 #include <linux/mdio.h>
48 
49 #include "t4vf_common.h"
50 #include "t4vf_defs.h"
51 
52 #include "../cxgb4/t4_regs.h"
53 #include "../cxgb4/t4_msg.h"
54 
55 /*
56  * Generic information about the driver.
57  */
58 #define DRV_VERSION "2.0.0-ko"
59 #define DRV_DESC "Chelsio T4/T5/T6 Virtual Function (VF) Network Driver"
60 
61 /*
62  * Module Parameters.
63  * ==================
64  */
65 
66 /*
67  * Default ethtool "message level" for adapters.
68  */
69 #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
70 			 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
71 			 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
72 
73 /*
74  * The driver uses the best interrupt scheme available on a platform in the
75  * order MSI-X then MSI.  This parameter determines which of these schemes the
76  * driver may consider as follows:
77  *
78  *     msi = 2: choose from among MSI-X and MSI
79  *     msi = 1: only consider MSI interrupts
80  *
81  * Note that unlike the Physical Function driver, this Virtual Function driver
82  * does _not_ support legacy INTx interrupts (this limitation is mandated by
83  * the PCI-E SR-IOV standard).
84  */
85 #define MSI_MSIX	2
86 #define MSI_MSI		1
87 #define MSI_DEFAULT	MSI_MSIX
88 
89 static int msi = MSI_DEFAULT;
90 
91 module_param(msi, int, 0644);
92 MODULE_PARM_DESC(msi, "whether to use MSI-X or MSI");
93 
94 /*
95  * Fundamental constants.
96  * ======================
97  */
98 
99 enum {
100 	MAX_TXQ_ENTRIES		= 16384,
101 	MAX_RSPQ_ENTRIES	= 16384,
102 	MAX_RX_BUFFERS		= 16384,
103 
104 	MIN_TXQ_ENTRIES		= 32,
105 	MIN_RSPQ_ENTRIES	= 128,
106 	MIN_FL_ENTRIES		= 16,
107 
108 	/*
109 	 * For purposes of manipulating the Free List size we need to
110 	 * recognize that Free Lists are actually Egress Queues (the host
111 	 * produces free buffers which the hardware consumes), Egress Queues
112 	 * indices are all in units of Egress Context Units bytes, and free
113 	 * list entries are 64-bit PCI DMA addresses.  And since the state of
114 	 * the Producer Index == the Consumer Index implies an EMPTY list, we
115 	 * always have at least one Egress Unit's worth of Free List entries
116 	 * unused.  See sge.c for more details ...
117 	 */
118 	EQ_UNIT = SGE_EQ_IDXSIZE,
119 	FL_PER_EQ_UNIT = EQ_UNIT / sizeof(__be64),
120 	MIN_FL_RESID = FL_PER_EQ_UNIT,
121 };
122 
123 /*
124  * Global driver state.
125  * ====================
126  */
127 
128 static struct dentry *cxgb4vf_debugfs_root;
129 
130 /*
131  * OS "Callback" functions.
132  * ========================
133  */
134 
135 /*
136  * The link status has changed on the indicated "port" (Virtual Interface).
137  */
138 void t4vf_os_link_changed(struct adapter *adapter, int pidx, int link_ok)
139 {
140 	struct net_device *dev = adapter->port[pidx];
141 
142 	/*
143 	 * If the port is disabled or the current recorded "link up"
144 	 * status matches the new status, just return.
145 	 */
146 	if (!netif_running(dev) || link_ok == netif_carrier_ok(dev))
147 		return;
148 
149 	/*
150 	 * Tell the OS that the link status has changed and print a short
151 	 * informative message on the console about the event.
152 	 */
153 	if (link_ok) {
154 		const char *s;
155 		const char *fc;
156 		const struct port_info *pi = netdev_priv(dev);
157 
158 		netif_carrier_on(dev);
159 
160 		switch (pi->link_cfg.speed) {
161 		case 40000:
162 			s = "40Gbps";
163 			break;
164 
165 		case 10000:
166 			s = "10Gbps";
167 			break;
168 
169 		case 1000:
170 			s = "1000Mbps";
171 			break;
172 
173 		case 100:
174 			s = "100Mbps";
175 			break;
176 
177 		default:
178 			s = "unknown";
179 			break;
180 		}
181 
182 		switch (pi->link_cfg.fc) {
183 		case PAUSE_RX:
184 			fc = "RX";
185 			break;
186 
187 		case PAUSE_TX:
188 			fc = "TX";
189 			break;
190 
191 		case PAUSE_RX|PAUSE_TX:
192 			fc = "RX/TX";
193 			break;
194 
195 		default:
196 			fc = "no";
197 			break;
198 		}
199 
200 		netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s, fc);
201 	} else {
202 		netif_carrier_off(dev);
203 		netdev_info(dev, "link down\n");
204 	}
205 }
206 
207 /*
208  * THe port module type has changed on the indicated "port" (Virtual
209  * Interface).
210  */
211 void t4vf_os_portmod_changed(struct adapter *adapter, int pidx)
212 {
213 	static const char * const mod_str[] = {
214 		NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
215 	};
216 	const struct net_device *dev = adapter->port[pidx];
217 	const struct port_info *pi = netdev_priv(dev);
218 
219 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
220 		dev_info(adapter->pdev_dev, "%s: port module unplugged\n",
221 			 dev->name);
222 	else if (pi->mod_type < ARRAY_SIZE(mod_str))
223 		dev_info(adapter->pdev_dev, "%s: %s port module inserted\n",
224 			 dev->name, mod_str[pi->mod_type]);
225 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
226 		dev_info(adapter->pdev_dev, "%s: unsupported optical port "
227 			 "module inserted\n", dev->name);
228 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
229 		dev_info(adapter->pdev_dev, "%s: unknown port module inserted,"
230 			 "forcing TWINAX\n", dev->name);
231 	else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
232 		dev_info(adapter->pdev_dev, "%s: transceiver module error\n",
233 			 dev->name);
234 	else
235 		dev_info(adapter->pdev_dev, "%s: unknown module type %d "
236 			 "inserted\n", dev->name, pi->mod_type);
237 }
238 
239 /*
240  * Net device operations.
241  * ======================
242  */
243 
244 
245 
246 
247 /*
248  * Perform the MAC and PHY actions needed to enable a "port" (Virtual
249  * Interface).
250  */
251 static int link_start(struct net_device *dev)
252 {
253 	int ret;
254 	struct port_info *pi = netdev_priv(dev);
255 
256 	/*
257 	 * We do not set address filters and promiscuity here, the stack does
258 	 * that step explicitly. Enable vlan accel.
259 	 */
260 	ret = t4vf_set_rxmode(pi->adapter, pi->viid, dev->mtu, -1, -1, -1, 1,
261 			      true);
262 	if (ret == 0) {
263 		ret = t4vf_change_mac(pi->adapter, pi->viid,
264 				      pi->xact_addr_filt, dev->dev_addr, true);
265 		if (ret >= 0) {
266 			pi->xact_addr_filt = ret;
267 			ret = 0;
268 		}
269 	}
270 
271 	/*
272 	 * We don't need to actually "start the link" itself since the
273 	 * firmware will do that for us when the first Virtual Interface
274 	 * is enabled on a port.
275 	 */
276 	if (ret == 0)
277 		ret = t4vf_enable_vi(pi->adapter, pi->viid, true, true);
278 	return ret;
279 }
280 
281 /*
282  * Name the MSI-X interrupts.
283  */
284 static void name_msix_vecs(struct adapter *adapter)
285 {
286 	int namelen = sizeof(adapter->msix_info[0].desc) - 1;
287 	int pidx;
288 
289 	/*
290 	 * Firmware events.
291 	 */
292 	snprintf(adapter->msix_info[MSIX_FW].desc, namelen,
293 		 "%s-FWeventq", adapter->name);
294 	adapter->msix_info[MSIX_FW].desc[namelen] = 0;
295 
296 	/*
297 	 * Ethernet queues.
298 	 */
299 	for_each_port(adapter, pidx) {
300 		struct net_device *dev = adapter->port[pidx];
301 		const struct port_info *pi = netdev_priv(dev);
302 		int qs, msi;
303 
304 		for (qs = 0, msi = MSIX_IQFLINT; qs < pi->nqsets; qs++, msi++) {
305 			snprintf(adapter->msix_info[msi].desc, namelen,
306 				 "%s-%d", dev->name, qs);
307 			adapter->msix_info[msi].desc[namelen] = 0;
308 		}
309 	}
310 }
311 
312 /*
313  * Request all of our MSI-X resources.
314  */
315 static int request_msix_queue_irqs(struct adapter *adapter)
316 {
317 	struct sge *s = &adapter->sge;
318 	int rxq, msi, err;
319 
320 	/*
321 	 * Firmware events.
322 	 */
323 	err = request_irq(adapter->msix_info[MSIX_FW].vec, t4vf_sge_intr_msix,
324 			  0, adapter->msix_info[MSIX_FW].desc, &s->fw_evtq);
325 	if (err)
326 		return err;
327 
328 	/*
329 	 * Ethernet queues.
330 	 */
331 	msi = MSIX_IQFLINT;
332 	for_each_ethrxq(s, rxq) {
333 		err = request_irq(adapter->msix_info[msi].vec,
334 				  t4vf_sge_intr_msix, 0,
335 				  adapter->msix_info[msi].desc,
336 				  &s->ethrxq[rxq].rspq);
337 		if (err)
338 			goto err_free_irqs;
339 		msi++;
340 	}
341 	return 0;
342 
343 err_free_irqs:
344 	while (--rxq >= 0)
345 		free_irq(adapter->msix_info[--msi].vec, &s->ethrxq[rxq].rspq);
346 	free_irq(adapter->msix_info[MSIX_FW].vec, &s->fw_evtq);
347 	return err;
348 }
349 
350 /*
351  * Free our MSI-X resources.
352  */
353 static void free_msix_queue_irqs(struct adapter *adapter)
354 {
355 	struct sge *s = &adapter->sge;
356 	int rxq, msi;
357 
358 	free_irq(adapter->msix_info[MSIX_FW].vec, &s->fw_evtq);
359 	msi = MSIX_IQFLINT;
360 	for_each_ethrxq(s, rxq)
361 		free_irq(adapter->msix_info[msi++].vec,
362 			 &s->ethrxq[rxq].rspq);
363 }
364 
365 /*
366  * Turn on NAPI and start up interrupts on a response queue.
367  */
368 static void qenable(struct sge_rspq *rspq)
369 {
370 	napi_enable(&rspq->napi);
371 
372 	/*
373 	 * 0-increment the Going To Sleep register to start the timer and
374 	 * enable interrupts.
375 	 */
376 	t4_write_reg(rspq->adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
377 		     CIDXINC_V(0) |
378 		     SEINTARM_V(rspq->intr_params) |
379 		     INGRESSQID_V(rspq->cntxt_id));
380 }
381 
382 /*
383  * Enable NAPI scheduling and interrupt generation for all Receive Queues.
384  */
385 static void enable_rx(struct adapter *adapter)
386 {
387 	int rxq;
388 	struct sge *s = &adapter->sge;
389 
390 	for_each_ethrxq(s, rxq)
391 		qenable(&s->ethrxq[rxq].rspq);
392 	qenable(&s->fw_evtq);
393 
394 	/*
395 	 * The interrupt queue doesn't use NAPI so we do the 0-increment of
396 	 * its Going To Sleep register here to get it started.
397 	 */
398 	if (adapter->flags & USING_MSI)
399 		t4_write_reg(adapter, T4VF_SGE_BASE_ADDR + SGE_VF_GTS,
400 			     CIDXINC_V(0) |
401 			     SEINTARM_V(s->intrq.intr_params) |
402 			     INGRESSQID_V(s->intrq.cntxt_id));
403 
404 }
405 
406 /*
407  * Wait until all NAPI handlers are descheduled.
408  */
409 static void quiesce_rx(struct adapter *adapter)
410 {
411 	struct sge *s = &adapter->sge;
412 	int rxq;
413 
414 	for_each_ethrxq(s, rxq)
415 		napi_disable(&s->ethrxq[rxq].rspq.napi);
416 	napi_disable(&s->fw_evtq.napi);
417 }
418 
419 /*
420  * Response queue handler for the firmware event queue.
421  */
422 static int fwevtq_handler(struct sge_rspq *rspq, const __be64 *rsp,
423 			  const struct pkt_gl *gl)
424 {
425 	/*
426 	 * Extract response opcode and get pointer to CPL message body.
427 	 */
428 	struct adapter *adapter = rspq->adapter;
429 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
430 	void *cpl = (void *)(rsp + 1);
431 
432 	switch (opcode) {
433 	case CPL_FW6_MSG: {
434 		/*
435 		 * We've received an asynchronous message from the firmware.
436 		 */
437 		const struct cpl_fw6_msg *fw_msg = cpl;
438 		if (fw_msg->type == FW6_TYPE_CMD_RPL)
439 			t4vf_handle_fw_rpl(adapter, fw_msg->data);
440 		break;
441 	}
442 
443 	case CPL_FW4_MSG: {
444 		/* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
445 		 */
446 		const struct cpl_sge_egr_update *p = (void *)(rsp + 3);
447 		opcode = CPL_OPCODE_G(ntohl(p->opcode_qid));
448 		if (opcode != CPL_SGE_EGR_UPDATE) {
449 			dev_err(adapter->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
450 				, opcode);
451 			break;
452 		}
453 		cpl = (void *)p;
454 		/*FALLTHROUGH*/
455 	}
456 
457 	case CPL_SGE_EGR_UPDATE: {
458 		/*
459 		 * We've received an Egress Queue Status Update message.  We
460 		 * get these, if the SGE is configured to send these when the
461 		 * firmware passes certain points in processing our TX
462 		 * Ethernet Queue or if we make an explicit request for one.
463 		 * We use these updates to determine when we may need to
464 		 * restart a TX Ethernet Queue which was stopped for lack of
465 		 * free TX Queue Descriptors ...
466 		 */
467 		const struct cpl_sge_egr_update *p = cpl;
468 		unsigned int qid = EGR_QID_G(be32_to_cpu(p->opcode_qid));
469 		struct sge *s = &adapter->sge;
470 		struct sge_txq *tq;
471 		struct sge_eth_txq *txq;
472 		unsigned int eq_idx;
473 
474 		/*
475 		 * Perform sanity checking on the Queue ID to make sure it
476 		 * really refers to one of our TX Ethernet Egress Queues which
477 		 * is active and matches the queue's ID.  None of these error
478 		 * conditions should ever happen so we may want to either make
479 		 * them fatal and/or conditionalized under DEBUG.
480 		 */
481 		eq_idx = EQ_IDX(s, qid);
482 		if (unlikely(eq_idx >= MAX_EGRQ)) {
483 			dev_err(adapter->pdev_dev,
484 				"Egress Update QID %d out of range\n", qid);
485 			break;
486 		}
487 		tq = s->egr_map[eq_idx];
488 		if (unlikely(tq == NULL)) {
489 			dev_err(adapter->pdev_dev,
490 				"Egress Update QID %d TXQ=NULL\n", qid);
491 			break;
492 		}
493 		txq = container_of(tq, struct sge_eth_txq, q);
494 		if (unlikely(tq->abs_id != qid)) {
495 			dev_err(adapter->pdev_dev,
496 				"Egress Update QID %d refers to TXQ %d\n",
497 				qid, tq->abs_id);
498 			break;
499 		}
500 
501 		/*
502 		 * Restart a stopped TX Queue which has less than half of its
503 		 * TX ring in use ...
504 		 */
505 		txq->q.restarts++;
506 		netif_tx_wake_queue(txq->txq);
507 		break;
508 	}
509 
510 	default:
511 		dev_err(adapter->pdev_dev,
512 			"unexpected CPL %#x on FW event queue\n", opcode);
513 	}
514 
515 	return 0;
516 }
517 
518 /*
519  * Allocate SGE TX/RX response queues.  Determine how many sets of SGE queues
520  * to use and initializes them.  We support multiple "Queue Sets" per port if
521  * we have MSI-X, otherwise just one queue set per port.
522  */
523 static int setup_sge_queues(struct adapter *adapter)
524 {
525 	struct sge *s = &adapter->sge;
526 	int err, pidx, msix;
527 
528 	/*
529 	 * Clear "Queue Set" Free List Starving and TX Queue Mapping Error
530 	 * state.
531 	 */
532 	bitmap_zero(s->starving_fl, MAX_EGRQ);
533 
534 	/*
535 	 * If we're using MSI interrupt mode we need to set up a "forwarded
536 	 * interrupt" queue which we'll set up with our MSI vector.  The rest
537 	 * of the ingress queues will be set up to forward their interrupts to
538 	 * this queue ...  This must be first since t4vf_sge_alloc_rxq() uses
539 	 * the intrq's queue ID as the interrupt forwarding queue for the
540 	 * subsequent calls ...
541 	 */
542 	if (adapter->flags & USING_MSI) {
543 		err = t4vf_sge_alloc_rxq(adapter, &s->intrq, false,
544 					 adapter->port[0], 0, NULL, NULL);
545 		if (err)
546 			goto err_free_queues;
547 	}
548 
549 	/*
550 	 * Allocate our ingress queue for asynchronous firmware messages.
551 	 */
552 	err = t4vf_sge_alloc_rxq(adapter, &s->fw_evtq, true, adapter->port[0],
553 				 MSIX_FW, NULL, fwevtq_handler);
554 	if (err)
555 		goto err_free_queues;
556 
557 	/*
558 	 * Allocate each "port"'s initial Queue Sets.  These can be changed
559 	 * later on ... up to the point where any interface on the adapter is
560 	 * brought up at which point lots of things get nailed down
561 	 * permanently ...
562 	 */
563 	msix = MSIX_IQFLINT;
564 	for_each_port(adapter, pidx) {
565 		struct net_device *dev = adapter->port[pidx];
566 		struct port_info *pi = netdev_priv(dev);
567 		struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset];
568 		struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset];
569 		int qs;
570 
571 		for (qs = 0; qs < pi->nqsets; qs++, rxq++, txq++) {
572 			err = t4vf_sge_alloc_rxq(adapter, &rxq->rspq, false,
573 						 dev, msix++,
574 						 &rxq->fl, t4vf_ethrx_handler);
575 			if (err)
576 				goto err_free_queues;
577 
578 			err = t4vf_sge_alloc_eth_txq(adapter, txq, dev,
579 					     netdev_get_tx_queue(dev, qs),
580 					     s->fw_evtq.cntxt_id);
581 			if (err)
582 				goto err_free_queues;
583 
584 			rxq->rspq.idx = qs;
585 			memset(&rxq->stats, 0, sizeof(rxq->stats));
586 		}
587 	}
588 
589 	/*
590 	 * Create the reverse mappings for the queues.
591 	 */
592 	s->egr_base = s->ethtxq[0].q.abs_id - s->ethtxq[0].q.cntxt_id;
593 	s->ingr_base = s->ethrxq[0].rspq.abs_id - s->ethrxq[0].rspq.cntxt_id;
594 	IQ_MAP(s, s->fw_evtq.abs_id) = &s->fw_evtq;
595 	for_each_port(adapter, pidx) {
596 		struct net_device *dev = adapter->port[pidx];
597 		struct port_info *pi = netdev_priv(dev);
598 		struct sge_eth_rxq *rxq = &s->ethrxq[pi->first_qset];
599 		struct sge_eth_txq *txq = &s->ethtxq[pi->first_qset];
600 		int qs;
601 
602 		for (qs = 0; qs < pi->nqsets; qs++, rxq++, txq++) {
603 			IQ_MAP(s, rxq->rspq.abs_id) = &rxq->rspq;
604 			EQ_MAP(s, txq->q.abs_id) = &txq->q;
605 
606 			/*
607 			 * The FW_IQ_CMD doesn't return the Absolute Queue IDs
608 			 * for Free Lists but since all of the Egress Queues
609 			 * (including Free Lists) have Relative Queue IDs
610 			 * which are computed as Absolute - Base Queue ID, we
611 			 * can synthesize the Absolute Queue IDs for the Free
612 			 * Lists.  This is useful for debugging purposes when
613 			 * we want to dump Queue Contexts via the PF Driver.
614 			 */
615 			rxq->fl.abs_id = rxq->fl.cntxt_id + s->egr_base;
616 			EQ_MAP(s, rxq->fl.abs_id) = &rxq->fl;
617 		}
618 	}
619 	return 0;
620 
621 err_free_queues:
622 	t4vf_free_sge_resources(adapter);
623 	return err;
624 }
625 
626 /*
627  * Set up Receive Side Scaling (RSS) to distribute packets to multiple receive
628  * queues.  We configure the RSS CPU lookup table to distribute to the number
629  * of HW receive queues, and the response queue lookup table to narrow that
630  * down to the response queues actually configured for each "port" (Virtual
631  * Interface).  We always configure the RSS mapping for all ports since the
632  * mapping table has plenty of entries.
633  */
634 static int setup_rss(struct adapter *adapter)
635 {
636 	int pidx;
637 
638 	for_each_port(adapter, pidx) {
639 		struct port_info *pi = adap2pinfo(adapter, pidx);
640 		struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[pi->first_qset];
641 		u16 rss[MAX_PORT_QSETS];
642 		int qs, err;
643 
644 		for (qs = 0; qs < pi->nqsets; qs++)
645 			rss[qs] = rxq[qs].rspq.abs_id;
646 
647 		err = t4vf_config_rss_range(adapter, pi->viid,
648 					    0, pi->rss_size, rss, pi->nqsets);
649 		if (err)
650 			return err;
651 
652 		/*
653 		 * Perform Global RSS Mode-specific initialization.
654 		 */
655 		switch (adapter->params.rss.mode) {
656 		case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL:
657 			/*
658 			 * If Tunnel All Lookup isn't specified in the global
659 			 * RSS Configuration, then we need to specify a
660 			 * default Ingress Queue for any ingress packets which
661 			 * aren't hashed.  We'll use our first ingress queue
662 			 * ...
663 			 */
664 			if (!adapter->params.rss.u.basicvirtual.tnlalllookup) {
665 				union rss_vi_config config;
666 				err = t4vf_read_rss_vi_config(adapter,
667 							      pi->viid,
668 							      &config);
669 				if (err)
670 					return err;
671 				config.basicvirtual.defaultq =
672 					rxq[0].rspq.abs_id;
673 				err = t4vf_write_rss_vi_config(adapter,
674 							       pi->viid,
675 							       &config);
676 				if (err)
677 					return err;
678 			}
679 			break;
680 		}
681 	}
682 
683 	return 0;
684 }
685 
686 /*
687  * Bring the adapter up.  Called whenever we go from no "ports" open to having
688  * one open.  This function performs the actions necessary to make an adapter
689  * operational, such as completing the initialization of HW modules, and
690  * enabling interrupts.  Must be called with the rtnl lock held.  (Note that
691  * this is called "cxgb_up" in the PF Driver.)
692  */
693 static int adapter_up(struct adapter *adapter)
694 {
695 	int err;
696 
697 	/*
698 	 * If this is the first time we've been called, perform basic
699 	 * adapter setup.  Once we've done this, many of our adapter
700 	 * parameters can no longer be changed ...
701 	 */
702 	if ((adapter->flags & FULL_INIT_DONE) == 0) {
703 		err = setup_sge_queues(adapter);
704 		if (err)
705 			return err;
706 		err = setup_rss(adapter);
707 		if (err) {
708 			t4vf_free_sge_resources(adapter);
709 			return err;
710 		}
711 
712 		if (adapter->flags & USING_MSIX)
713 			name_msix_vecs(adapter);
714 		adapter->flags |= FULL_INIT_DONE;
715 	}
716 
717 	/*
718 	 * Acquire our interrupt resources.  We only support MSI-X and MSI.
719 	 */
720 	BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
721 	if (adapter->flags & USING_MSIX)
722 		err = request_msix_queue_irqs(adapter);
723 	else
724 		err = request_irq(adapter->pdev->irq,
725 				  t4vf_intr_handler(adapter), 0,
726 				  adapter->name, adapter);
727 	if (err) {
728 		dev_err(adapter->pdev_dev, "request_irq failed, err %d\n",
729 			err);
730 		return err;
731 	}
732 
733 	/*
734 	 * Enable NAPI ingress processing and return success.
735 	 */
736 	enable_rx(adapter);
737 	t4vf_sge_start(adapter);
738 
739 	/* Initialize hash mac addr list*/
740 	INIT_LIST_HEAD(&adapter->mac_hlist);
741 	return 0;
742 }
743 
744 /*
745  * Bring the adapter down.  Called whenever the last "port" (Virtual
746  * Interface) closed.  (Note that this routine is called "cxgb_down" in the PF
747  * Driver.)
748  */
749 static void adapter_down(struct adapter *adapter)
750 {
751 	/*
752 	 * Free interrupt resources.
753 	 */
754 	if (adapter->flags & USING_MSIX)
755 		free_msix_queue_irqs(adapter);
756 	else
757 		free_irq(adapter->pdev->irq, adapter);
758 
759 	/*
760 	 * Wait for NAPI handlers to finish.
761 	 */
762 	quiesce_rx(adapter);
763 }
764 
765 /*
766  * Start up a net device.
767  */
768 static int cxgb4vf_open(struct net_device *dev)
769 {
770 	int err;
771 	struct port_info *pi = netdev_priv(dev);
772 	struct adapter *adapter = pi->adapter;
773 
774 	/*
775 	 * If this is the first interface that we're opening on the "adapter",
776 	 * bring the "adapter" up now.
777 	 */
778 	if (adapter->open_device_map == 0) {
779 		err = adapter_up(adapter);
780 		if (err)
781 			return err;
782 	}
783 
784 	/*
785 	 * Note that this interface is up and start everything up ...
786 	 */
787 	err = link_start(dev);
788 	if (err)
789 		goto err_unwind;
790 
791 	netif_tx_start_all_queues(dev);
792 	set_bit(pi->port_id, &adapter->open_device_map);
793 	return 0;
794 
795 err_unwind:
796 	if (adapter->open_device_map == 0)
797 		adapter_down(adapter);
798 	return err;
799 }
800 
801 /*
802  * Shut down a net device.  This routine is called "cxgb_close" in the PF
803  * Driver ...
804  */
805 static int cxgb4vf_stop(struct net_device *dev)
806 {
807 	struct port_info *pi = netdev_priv(dev);
808 	struct adapter *adapter = pi->adapter;
809 
810 	netif_tx_stop_all_queues(dev);
811 	netif_carrier_off(dev);
812 	t4vf_enable_vi(adapter, pi->viid, false, false);
813 	pi->link_cfg.link_ok = 0;
814 
815 	clear_bit(pi->port_id, &adapter->open_device_map);
816 	if (adapter->open_device_map == 0)
817 		adapter_down(adapter);
818 	return 0;
819 }
820 
821 /*
822  * Translate our basic statistics into the standard "ifconfig" statistics.
823  */
824 static struct net_device_stats *cxgb4vf_get_stats(struct net_device *dev)
825 {
826 	struct t4vf_port_stats stats;
827 	struct port_info *pi = netdev2pinfo(dev);
828 	struct adapter *adapter = pi->adapter;
829 	struct net_device_stats *ns = &dev->stats;
830 	int err;
831 
832 	spin_lock(&adapter->stats_lock);
833 	err = t4vf_get_port_stats(adapter, pi->pidx, &stats);
834 	spin_unlock(&adapter->stats_lock);
835 
836 	memset(ns, 0, sizeof(*ns));
837 	if (err)
838 		return ns;
839 
840 	ns->tx_bytes = (stats.tx_bcast_bytes + stats.tx_mcast_bytes +
841 			stats.tx_ucast_bytes + stats.tx_offload_bytes);
842 	ns->tx_packets = (stats.tx_bcast_frames + stats.tx_mcast_frames +
843 			  stats.tx_ucast_frames + stats.tx_offload_frames);
844 	ns->rx_bytes = (stats.rx_bcast_bytes + stats.rx_mcast_bytes +
845 			stats.rx_ucast_bytes);
846 	ns->rx_packets = (stats.rx_bcast_frames + stats.rx_mcast_frames +
847 			  stats.rx_ucast_frames);
848 	ns->multicast = stats.rx_mcast_frames;
849 	ns->tx_errors = stats.tx_drop_frames;
850 	ns->rx_errors = stats.rx_err_frames;
851 
852 	return ns;
853 }
854 
855 static inline int cxgb4vf_set_addr_hash(struct port_info *pi)
856 {
857 	struct adapter *adapter = pi->adapter;
858 	u64 vec = 0;
859 	bool ucast = false;
860 	struct hash_mac_addr *entry;
861 
862 	/* Calculate the hash vector for the updated list and program it */
863 	list_for_each_entry(entry, &adapter->mac_hlist, list) {
864 		ucast |= is_unicast_ether_addr(entry->addr);
865 		vec |= (1ULL << hash_mac_addr(entry->addr));
866 	}
867 	return t4vf_set_addr_hash(adapter, pi->viid, ucast, vec, false);
868 }
869 
870 static int cxgb4vf_mac_sync(struct net_device *netdev, const u8 *mac_addr)
871 {
872 	struct port_info *pi = netdev_priv(netdev);
873 	struct adapter *adapter = pi->adapter;
874 	int ret;
875 	u64 mhash = 0;
876 	u64 uhash = 0;
877 	bool free = false;
878 	bool ucast = is_unicast_ether_addr(mac_addr);
879 	const u8 *maclist[1] = {mac_addr};
880 	struct hash_mac_addr *new_entry;
881 
882 	ret = t4vf_alloc_mac_filt(adapter, pi->viid, free, 1, maclist,
883 				  NULL, ucast ? &uhash : &mhash, false);
884 	if (ret < 0)
885 		goto out;
886 	/* if hash != 0, then add the addr to hash addr list
887 	 * so on the end we will calculate the hash for the
888 	 * list and program it
889 	 */
890 	if (uhash || mhash) {
891 		new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
892 		if (!new_entry)
893 			return -ENOMEM;
894 		ether_addr_copy(new_entry->addr, mac_addr);
895 		list_add_tail(&new_entry->list, &adapter->mac_hlist);
896 		ret = cxgb4vf_set_addr_hash(pi);
897 	}
898 out:
899 	return ret < 0 ? ret : 0;
900 }
901 
902 static int cxgb4vf_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
903 {
904 	struct port_info *pi = netdev_priv(netdev);
905 	struct adapter *adapter = pi->adapter;
906 	int ret;
907 	const u8 *maclist[1] = {mac_addr};
908 	struct hash_mac_addr *entry, *tmp;
909 
910 	/* If the MAC address to be removed is in the hash addr
911 	 * list, delete it from the list and update hash vector
912 	 */
913 	list_for_each_entry_safe(entry, tmp, &adapter->mac_hlist, list) {
914 		if (ether_addr_equal(entry->addr, mac_addr)) {
915 			list_del(&entry->list);
916 			kfree(entry);
917 			return cxgb4vf_set_addr_hash(pi);
918 		}
919 	}
920 
921 	ret = t4vf_free_mac_filt(adapter, pi->viid, 1, maclist, false);
922 	return ret < 0 ? -EINVAL : 0;
923 }
924 
925 /*
926  * Set RX properties of a port, such as promiscruity, address filters, and MTU.
927  * If @mtu is -1 it is left unchanged.
928  */
929 static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
930 {
931 	struct port_info *pi = netdev_priv(dev);
932 
933 	__dev_uc_sync(dev, cxgb4vf_mac_sync, cxgb4vf_mac_unsync);
934 	__dev_mc_sync(dev, cxgb4vf_mac_sync, cxgb4vf_mac_unsync);
935 	return t4vf_set_rxmode(pi->adapter, pi->viid, -1,
936 			       (dev->flags & IFF_PROMISC) != 0,
937 			       (dev->flags & IFF_ALLMULTI) != 0,
938 			       1, -1, sleep_ok);
939 }
940 
941 /*
942  * Set the current receive modes on the device.
943  */
944 static void cxgb4vf_set_rxmode(struct net_device *dev)
945 {
946 	/* unfortunately we can't return errors to the stack */
947 	set_rxmode(dev, -1, false);
948 }
949 
950 /*
951  * Find the entry in the interrupt holdoff timer value array which comes
952  * closest to the specified interrupt holdoff value.
953  */
954 static int closest_timer(const struct sge *s, int us)
955 {
956 	int i, timer_idx = 0, min_delta = INT_MAX;
957 
958 	for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
959 		int delta = us - s->timer_val[i];
960 		if (delta < 0)
961 			delta = -delta;
962 		if (delta < min_delta) {
963 			min_delta = delta;
964 			timer_idx = i;
965 		}
966 	}
967 	return timer_idx;
968 }
969 
970 static int closest_thres(const struct sge *s, int thres)
971 {
972 	int i, delta, pktcnt_idx = 0, min_delta = INT_MAX;
973 
974 	for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
975 		delta = thres - s->counter_val[i];
976 		if (delta < 0)
977 			delta = -delta;
978 		if (delta < min_delta) {
979 			min_delta = delta;
980 			pktcnt_idx = i;
981 		}
982 	}
983 	return pktcnt_idx;
984 }
985 
986 /*
987  * Return a queue's interrupt hold-off time in us.  0 means no timer.
988  */
989 static unsigned int qtimer_val(const struct adapter *adapter,
990 			       const struct sge_rspq *rspq)
991 {
992 	unsigned int timer_idx = QINTR_TIMER_IDX_G(rspq->intr_params);
993 
994 	return timer_idx < SGE_NTIMERS
995 		? adapter->sge.timer_val[timer_idx]
996 		: 0;
997 }
998 
999 /**
1000  *	set_rxq_intr_params - set a queue's interrupt holdoff parameters
1001  *	@adapter: the adapter
1002  *	@rspq: the RX response queue
1003  *	@us: the hold-off time in us, or 0 to disable timer
1004  *	@cnt: the hold-off packet count, or 0 to disable counter
1005  *
1006  *	Sets an RX response queue's interrupt hold-off time and packet count.
1007  *	At least one of the two needs to be enabled for the queue to generate
1008  *	interrupts.
1009  */
1010 static int set_rxq_intr_params(struct adapter *adapter, struct sge_rspq *rspq,
1011 			       unsigned int us, unsigned int cnt)
1012 {
1013 	unsigned int timer_idx;
1014 
1015 	/*
1016 	 * If both the interrupt holdoff timer and count are specified as
1017 	 * zero, default to a holdoff count of 1 ...
1018 	 */
1019 	if ((us | cnt) == 0)
1020 		cnt = 1;
1021 
1022 	/*
1023 	 * If an interrupt holdoff count has been specified, then find the
1024 	 * closest configured holdoff count and use that.  If the response
1025 	 * queue has already been created, then update its queue context
1026 	 * parameters ...
1027 	 */
1028 	if (cnt) {
1029 		int err;
1030 		u32 v, pktcnt_idx;
1031 
1032 		pktcnt_idx = closest_thres(&adapter->sge, cnt);
1033 		if (rspq->desc && rspq->pktcnt_idx != pktcnt_idx) {
1034 			v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1035 			    FW_PARAMS_PARAM_X_V(
1036 					FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1037 			    FW_PARAMS_PARAM_YZ_V(rspq->cntxt_id);
1038 			err = t4vf_set_params(adapter, 1, &v, &pktcnt_idx);
1039 			if (err)
1040 				return err;
1041 		}
1042 		rspq->pktcnt_idx = pktcnt_idx;
1043 	}
1044 
1045 	/*
1046 	 * Compute the closest holdoff timer index from the supplied holdoff
1047 	 * timer value.
1048 	 */
1049 	timer_idx = (us == 0
1050 		     ? SGE_TIMER_RSTRT_CNTR
1051 		     : closest_timer(&adapter->sge, us));
1052 
1053 	/*
1054 	 * Update the response queue's interrupt coalescing parameters and
1055 	 * return success.
1056 	 */
1057 	rspq->intr_params = (QINTR_TIMER_IDX_V(timer_idx) |
1058 			     QINTR_CNT_EN_V(cnt > 0));
1059 	return 0;
1060 }
1061 
1062 /*
1063  * Return a version number to identify the type of adapter.  The scheme is:
1064  * - bits 0..9: chip version
1065  * - bits 10..15: chip revision
1066  */
1067 static inline unsigned int mk_adap_vers(const struct adapter *adapter)
1068 {
1069 	/*
1070 	 * Chip version 4, revision 0x3f (cxgb4vf).
1071 	 */
1072 	return CHELSIO_CHIP_VERSION(adapter->params.chip) | (0x3f << 10);
1073 }
1074 
1075 /*
1076  * Execute the specified ioctl command.
1077  */
1078 static int cxgb4vf_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1079 {
1080 	int ret = 0;
1081 
1082 	switch (cmd) {
1083 	    /*
1084 	     * The VF Driver doesn't have access to any of the other
1085 	     * common Ethernet device ioctl()'s (like reading/writing
1086 	     * PHY registers, etc.
1087 	     */
1088 
1089 	default:
1090 		ret = -EOPNOTSUPP;
1091 		break;
1092 	}
1093 	return ret;
1094 }
1095 
1096 /*
1097  * Change the device's MTU.
1098  */
1099 static int cxgb4vf_change_mtu(struct net_device *dev, int new_mtu)
1100 {
1101 	int ret;
1102 	struct port_info *pi = netdev_priv(dev);
1103 
1104 	ret = t4vf_set_rxmode(pi->adapter, pi->viid, new_mtu,
1105 			      -1, -1, -1, -1, true);
1106 	if (!ret)
1107 		dev->mtu = new_mtu;
1108 	return ret;
1109 }
1110 
1111 static netdev_features_t cxgb4vf_fix_features(struct net_device *dev,
1112 	netdev_features_t features)
1113 {
1114 	/*
1115 	 * Since there is no support for separate rx/tx vlan accel
1116 	 * enable/disable make sure tx flag is always in same state as rx.
1117 	 */
1118 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
1119 		features |= NETIF_F_HW_VLAN_CTAG_TX;
1120 	else
1121 		features &= ~NETIF_F_HW_VLAN_CTAG_TX;
1122 
1123 	return features;
1124 }
1125 
1126 static int cxgb4vf_set_features(struct net_device *dev,
1127 	netdev_features_t features)
1128 {
1129 	struct port_info *pi = netdev_priv(dev);
1130 	netdev_features_t changed = dev->features ^ features;
1131 
1132 	if (changed & NETIF_F_HW_VLAN_CTAG_RX)
1133 		t4vf_set_rxmode(pi->adapter, pi->viid, -1, -1, -1, -1,
1134 				features & NETIF_F_HW_VLAN_CTAG_TX, 0);
1135 
1136 	return 0;
1137 }
1138 
1139 /*
1140  * Change the devices MAC address.
1141  */
1142 static int cxgb4vf_set_mac_addr(struct net_device *dev, void *_addr)
1143 {
1144 	int ret;
1145 	struct sockaddr *addr = _addr;
1146 	struct port_info *pi = netdev_priv(dev);
1147 
1148 	if (!is_valid_ether_addr(addr->sa_data))
1149 		return -EADDRNOTAVAIL;
1150 
1151 	ret = t4vf_change_mac(pi->adapter, pi->viid, pi->xact_addr_filt,
1152 			      addr->sa_data, true);
1153 	if (ret < 0)
1154 		return ret;
1155 
1156 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1157 	pi->xact_addr_filt = ret;
1158 	return 0;
1159 }
1160 
1161 #ifdef CONFIG_NET_POLL_CONTROLLER
1162 /*
1163  * Poll all of our receive queues.  This is called outside of normal interrupt
1164  * context.
1165  */
1166 static void cxgb4vf_poll_controller(struct net_device *dev)
1167 {
1168 	struct port_info *pi = netdev_priv(dev);
1169 	struct adapter *adapter = pi->adapter;
1170 
1171 	if (adapter->flags & USING_MSIX) {
1172 		struct sge_eth_rxq *rxq;
1173 		int nqsets;
1174 
1175 		rxq = &adapter->sge.ethrxq[pi->first_qset];
1176 		for (nqsets = pi->nqsets; nqsets; nqsets--) {
1177 			t4vf_sge_intr_msix(0, &rxq->rspq);
1178 			rxq++;
1179 		}
1180 	} else
1181 		t4vf_intr_handler(adapter)(0, adapter);
1182 }
1183 #endif
1184 
1185 /*
1186  * Ethtool operations.
1187  * ===================
1188  *
1189  * Note that we don't support any ethtool operations which change the physical
1190  * state of the port to which we're linked.
1191  */
1192 
1193 /**
1194  *	from_fw_port_mod_type - translate Firmware Port/Module type to Ethtool
1195  *	@port_type: Firmware Port Type
1196  *	@mod_type: Firmware Module Type
1197  *
1198  *	Translate Firmware Port/Module type to Ethtool Port Type.
1199  */
1200 static int from_fw_port_mod_type(enum fw_port_type port_type,
1201 				 enum fw_port_module_type mod_type)
1202 {
1203 	if (port_type == FW_PORT_TYPE_BT_SGMII ||
1204 	    port_type == FW_PORT_TYPE_BT_XFI ||
1205 	    port_type == FW_PORT_TYPE_BT_XAUI) {
1206 		return PORT_TP;
1207 	} else if (port_type == FW_PORT_TYPE_FIBER_XFI ||
1208 		   port_type == FW_PORT_TYPE_FIBER_XAUI) {
1209 		return PORT_FIBRE;
1210 	} else if (port_type == FW_PORT_TYPE_SFP ||
1211 		   port_type == FW_PORT_TYPE_QSFP_10G ||
1212 		   port_type == FW_PORT_TYPE_QSA ||
1213 		   port_type == FW_PORT_TYPE_QSFP) {
1214 		if (mod_type == FW_PORT_MOD_TYPE_LR ||
1215 		    mod_type == FW_PORT_MOD_TYPE_SR ||
1216 		    mod_type == FW_PORT_MOD_TYPE_ER ||
1217 		    mod_type == FW_PORT_MOD_TYPE_LRM)
1218 			return PORT_FIBRE;
1219 		else if (mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
1220 			 mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
1221 			return PORT_DA;
1222 		else
1223 			return PORT_OTHER;
1224 	}
1225 
1226 	return PORT_OTHER;
1227 }
1228 
1229 /**
1230  *	fw_caps_to_lmm - translate Firmware to ethtool Link Mode Mask
1231  *	@port_type: Firmware Port Type
1232  *	@fw_caps: Firmware Port Capabilities
1233  *	@link_mode_mask: ethtool Link Mode Mask
1234  *
1235  *	Translate a Firmware Port Capabilities specification to an ethtool
1236  *	Link Mode Mask.
1237  */
1238 static void fw_caps_to_lmm(enum fw_port_type port_type,
1239 			   unsigned int fw_caps,
1240 			   unsigned long *link_mode_mask)
1241 {
1242 	#define SET_LMM(__lmm_name) __set_bit(ETHTOOL_LINK_MODE_ ## __lmm_name\
1243 			 ## _BIT, link_mode_mask)
1244 
1245 	#define FW_CAPS_TO_LMM(__fw_name, __lmm_name) \
1246 		do { \
1247 			if (fw_caps & FW_PORT_CAP_ ## __fw_name) \
1248 				SET_LMM(__lmm_name); \
1249 		} while (0)
1250 
1251 	switch (port_type) {
1252 	case FW_PORT_TYPE_BT_SGMII:
1253 	case FW_PORT_TYPE_BT_XFI:
1254 	case FW_PORT_TYPE_BT_XAUI:
1255 		SET_LMM(TP);
1256 		FW_CAPS_TO_LMM(SPEED_100M, 100baseT_Full);
1257 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
1258 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
1259 		break;
1260 
1261 	case FW_PORT_TYPE_KX4:
1262 	case FW_PORT_TYPE_KX:
1263 		SET_LMM(Backplane);
1264 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseKX_Full);
1265 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseKX4_Full);
1266 		break;
1267 
1268 	case FW_PORT_TYPE_KR:
1269 		SET_LMM(Backplane);
1270 		SET_LMM(10000baseKR_Full);
1271 		break;
1272 
1273 	case FW_PORT_TYPE_BP_AP:
1274 		SET_LMM(Backplane);
1275 		SET_LMM(10000baseR_FEC);
1276 		SET_LMM(10000baseKR_Full);
1277 		SET_LMM(1000baseKX_Full);
1278 		break;
1279 
1280 	case FW_PORT_TYPE_BP4_AP:
1281 		SET_LMM(Backplane);
1282 		SET_LMM(10000baseR_FEC);
1283 		SET_LMM(10000baseKR_Full);
1284 		SET_LMM(1000baseKX_Full);
1285 		SET_LMM(10000baseKX4_Full);
1286 		break;
1287 
1288 	case FW_PORT_TYPE_FIBER_XFI:
1289 	case FW_PORT_TYPE_FIBER_XAUI:
1290 	case FW_PORT_TYPE_SFP:
1291 	case FW_PORT_TYPE_QSFP_10G:
1292 	case FW_PORT_TYPE_QSA:
1293 		SET_LMM(FIBRE);
1294 		FW_CAPS_TO_LMM(SPEED_1G, 1000baseT_Full);
1295 		FW_CAPS_TO_LMM(SPEED_10G, 10000baseT_Full);
1296 		break;
1297 
1298 	case FW_PORT_TYPE_BP40_BA:
1299 	case FW_PORT_TYPE_QSFP:
1300 		SET_LMM(FIBRE);
1301 		SET_LMM(40000baseSR4_Full);
1302 		break;
1303 
1304 	case FW_PORT_TYPE_CR_QSFP:
1305 	case FW_PORT_TYPE_SFP28:
1306 		SET_LMM(FIBRE);
1307 		SET_LMM(25000baseCR_Full);
1308 		break;
1309 
1310 	case FW_PORT_TYPE_KR4_100G:
1311 	case FW_PORT_TYPE_CR4_QSFP:
1312 		SET_LMM(FIBRE);
1313 		SET_LMM(100000baseCR4_Full);
1314 		break;
1315 
1316 	default:
1317 		break;
1318 	}
1319 
1320 	FW_CAPS_TO_LMM(ANEG, Autoneg);
1321 	FW_CAPS_TO_LMM(802_3_PAUSE, Pause);
1322 	FW_CAPS_TO_LMM(802_3_ASM_DIR, Asym_Pause);
1323 
1324 	#undef FW_CAPS_TO_LMM
1325 	#undef SET_LMM
1326 }
1327 
1328 static int cxgb4vf_get_link_ksettings(struct net_device *dev,
1329 				      struct ethtool_link_ksettings
1330 							*link_ksettings)
1331 {
1332 	const struct port_info *pi = netdev_priv(dev);
1333 	struct ethtool_link_settings *base = &link_ksettings->base;
1334 
1335 	ethtool_link_ksettings_zero_link_mode(link_ksettings, supported);
1336 	ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
1337 	ethtool_link_ksettings_zero_link_mode(link_ksettings, lp_advertising);
1338 
1339 	base->port = from_fw_port_mod_type(pi->port_type, pi->mod_type);
1340 
1341 	if (pi->mdio_addr >= 0) {
1342 		base->phy_address = pi->mdio_addr;
1343 		base->mdio_support = (pi->port_type == FW_PORT_TYPE_BT_SGMII
1344 				      ? ETH_MDIO_SUPPORTS_C22
1345 				      : ETH_MDIO_SUPPORTS_C45);
1346 	} else {
1347 		base->phy_address = 255;
1348 		base->mdio_support = 0;
1349 	}
1350 
1351 	fw_caps_to_lmm(pi->port_type, pi->link_cfg.supported,
1352 		       link_ksettings->link_modes.supported);
1353 	fw_caps_to_lmm(pi->port_type, pi->link_cfg.advertising,
1354 		       link_ksettings->link_modes.advertising);
1355 	fw_caps_to_lmm(pi->port_type, pi->link_cfg.lp_advertising,
1356 		       link_ksettings->link_modes.lp_advertising);
1357 
1358 	if (netif_carrier_ok(dev)) {
1359 		base->speed = pi->link_cfg.speed;
1360 		base->duplex = DUPLEX_FULL;
1361 	} else {
1362 		base->speed = SPEED_UNKNOWN;
1363 		base->duplex = DUPLEX_UNKNOWN;
1364 	}
1365 
1366 	base->autoneg = pi->link_cfg.autoneg;
1367 	if (pi->link_cfg.supported & FW_PORT_CAP_ANEG)
1368 		ethtool_link_ksettings_add_link_mode(link_ksettings,
1369 						     supported, Autoneg);
1370 	if (pi->link_cfg.autoneg)
1371 		ethtool_link_ksettings_add_link_mode(link_ksettings,
1372 						     advertising, Autoneg);
1373 
1374 	return 0;
1375 }
1376 
1377 /*
1378  * Return our driver information.
1379  */
1380 static void cxgb4vf_get_drvinfo(struct net_device *dev,
1381 				struct ethtool_drvinfo *drvinfo)
1382 {
1383 	struct adapter *adapter = netdev2adap(dev);
1384 
1385 	strlcpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver));
1386 	strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
1387 	strlcpy(drvinfo->bus_info, pci_name(to_pci_dev(dev->dev.parent)),
1388 		sizeof(drvinfo->bus_info));
1389 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1390 		 "%u.%u.%u.%u, TP %u.%u.%u.%u",
1391 		 FW_HDR_FW_VER_MAJOR_G(adapter->params.dev.fwrev),
1392 		 FW_HDR_FW_VER_MINOR_G(adapter->params.dev.fwrev),
1393 		 FW_HDR_FW_VER_MICRO_G(adapter->params.dev.fwrev),
1394 		 FW_HDR_FW_VER_BUILD_G(adapter->params.dev.fwrev),
1395 		 FW_HDR_FW_VER_MAJOR_G(adapter->params.dev.tprev),
1396 		 FW_HDR_FW_VER_MINOR_G(adapter->params.dev.tprev),
1397 		 FW_HDR_FW_VER_MICRO_G(adapter->params.dev.tprev),
1398 		 FW_HDR_FW_VER_BUILD_G(adapter->params.dev.tprev));
1399 }
1400 
1401 /*
1402  * Return current adapter message level.
1403  */
1404 static u32 cxgb4vf_get_msglevel(struct net_device *dev)
1405 {
1406 	return netdev2adap(dev)->msg_enable;
1407 }
1408 
1409 /*
1410  * Set current adapter message level.
1411  */
1412 static void cxgb4vf_set_msglevel(struct net_device *dev, u32 msglevel)
1413 {
1414 	netdev2adap(dev)->msg_enable = msglevel;
1415 }
1416 
1417 /*
1418  * Return the device's current Queue Set ring size parameters along with the
1419  * allowed maximum values.  Since ethtool doesn't understand the concept of
1420  * multi-queue devices, we just return the current values associated with the
1421  * first Queue Set.
1422  */
1423 static void cxgb4vf_get_ringparam(struct net_device *dev,
1424 				  struct ethtool_ringparam *rp)
1425 {
1426 	const struct port_info *pi = netdev_priv(dev);
1427 	const struct sge *s = &pi->adapter->sge;
1428 
1429 	rp->rx_max_pending = MAX_RX_BUFFERS;
1430 	rp->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
1431 	rp->rx_jumbo_max_pending = 0;
1432 	rp->tx_max_pending = MAX_TXQ_ENTRIES;
1433 
1434 	rp->rx_pending = s->ethrxq[pi->first_qset].fl.size - MIN_FL_RESID;
1435 	rp->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
1436 	rp->rx_jumbo_pending = 0;
1437 	rp->tx_pending = s->ethtxq[pi->first_qset].q.size;
1438 }
1439 
1440 /*
1441  * Set the Queue Set ring size parameters for the device.  Again, since
1442  * ethtool doesn't allow for the concept of multiple queues per device, we'll
1443  * apply these new values across all of the Queue Sets associated with the
1444  * device -- after vetting them of course!
1445  */
1446 static int cxgb4vf_set_ringparam(struct net_device *dev,
1447 				 struct ethtool_ringparam *rp)
1448 {
1449 	const struct port_info *pi = netdev_priv(dev);
1450 	struct adapter *adapter = pi->adapter;
1451 	struct sge *s = &adapter->sge;
1452 	int qs;
1453 
1454 	if (rp->rx_pending > MAX_RX_BUFFERS ||
1455 	    rp->rx_jumbo_pending ||
1456 	    rp->tx_pending > MAX_TXQ_ENTRIES ||
1457 	    rp->rx_mini_pending > MAX_RSPQ_ENTRIES ||
1458 	    rp->rx_mini_pending < MIN_RSPQ_ENTRIES ||
1459 	    rp->rx_pending < MIN_FL_ENTRIES ||
1460 	    rp->tx_pending < MIN_TXQ_ENTRIES)
1461 		return -EINVAL;
1462 
1463 	if (adapter->flags & FULL_INIT_DONE)
1464 		return -EBUSY;
1465 
1466 	for (qs = pi->first_qset; qs < pi->first_qset + pi->nqsets; qs++) {
1467 		s->ethrxq[qs].fl.size = rp->rx_pending + MIN_FL_RESID;
1468 		s->ethrxq[qs].rspq.size = rp->rx_mini_pending;
1469 		s->ethtxq[qs].q.size = rp->tx_pending;
1470 	}
1471 	return 0;
1472 }
1473 
1474 /*
1475  * Return the interrupt holdoff timer and count for the first Queue Set on the
1476  * device.  Our extension ioctl() (the cxgbtool interface) allows the
1477  * interrupt holdoff timer to be read on all of the device's Queue Sets.
1478  */
1479 static int cxgb4vf_get_coalesce(struct net_device *dev,
1480 				struct ethtool_coalesce *coalesce)
1481 {
1482 	const struct port_info *pi = netdev_priv(dev);
1483 	const struct adapter *adapter = pi->adapter;
1484 	const struct sge_rspq *rspq = &adapter->sge.ethrxq[pi->first_qset].rspq;
1485 
1486 	coalesce->rx_coalesce_usecs = qtimer_val(adapter, rspq);
1487 	coalesce->rx_max_coalesced_frames =
1488 		((rspq->intr_params & QINTR_CNT_EN_F)
1489 		 ? adapter->sge.counter_val[rspq->pktcnt_idx]
1490 		 : 0);
1491 	return 0;
1492 }
1493 
1494 /*
1495  * Set the RX interrupt holdoff timer and count for the first Queue Set on the
1496  * interface.  Our extension ioctl() (the cxgbtool interface) allows us to set
1497  * the interrupt holdoff timer on any of the device's Queue Sets.
1498  */
1499 static int cxgb4vf_set_coalesce(struct net_device *dev,
1500 				struct ethtool_coalesce *coalesce)
1501 {
1502 	const struct port_info *pi = netdev_priv(dev);
1503 	struct adapter *adapter = pi->adapter;
1504 
1505 	return set_rxq_intr_params(adapter,
1506 				   &adapter->sge.ethrxq[pi->first_qset].rspq,
1507 				   coalesce->rx_coalesce_usecs,
1508 				   coalesce->rx_max_coalesced_frames);
1509 }
1510 
1511 /*
1512  * Report current port link pause parameter settings.
1513  */
1514 static void cxgb4vf_get_pauseparam(struct net_device *dev,
1515 				   struct ethtool_pauseparam *pauseparam)
1516 {
1517 	struct port_info *pi = netdev_priv(dev);
1518 
1519 	pauseparam->autoneg = (pi->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
1520 	pauseparam->rx_pause = (pi->link_cfg.fc & PAUSE_RX) != 0;
1521 	pauseparam->tx_pause = (pi->link_cfg.fc & PAUSE_TX) != 0;
1522 }
1523 
1524 /*
1525  * Identify the port by blinking the port's LED.
1526  */
1527 static int cxgb4vf_phys_id(struct net_device *dev,
1528 			   enum ethtool_phys_id_state state)
1529 {
1530 	unsigned int val;
1531 	struct port_info *pi = netdev_priv(dev);
1532 
1533 	if (state == ETHTOOL_ID_ACTIVE)
1534 		val = 0xffff;
1535 	else if (state == ETHTOOL_ID_INACTIVE)
1536 		val = 0;
1537 	else
1538 		return -EINVAL;
1539 
1540 	return t4vf_identify_port(pi->adapter, pi->viid, val);
1541 }
1542 
1543 /*
1544  * Port stats maintained per queue of the port.
1545  */
1546 struct queue_port_stats {
1547 	u64 tso;
1548 	u64 tx_csum;
1549 	u64 rx_csum;
1550 	u64 vlan_ex;
1551 	u64 vlan_ins;
1552 	u64 lro_pkts;
1553 	u64 lro_merged;
1554 };
1555 
1556 /*
1557  * Strings for the ETH_SS_STATS statistics set ("ethtool -S").  Note that
1558  * these need to match the order of statistics returned by
1559  * t4vf_get_port_stats().
1560  */
1561 static const char stats_strings[][ETH_GSTRING_LEN] = {
1562 	/*
1563 	 * These must match the layout of the t4vf_port_stats structure.
1564 	 */
1565 	"TxBroadcastBytes  ",
1566 	"TxBroadcastFrames ",
1567 	"TxMulticastBytes  ",
1568 	"TxMulticastFrames ",
1569 	"TxUnicastBytes    ",
1570 	"TxUnicastFrames   ",
1571 	"TxDroppedFrames   ",
1572 	"TxOffloadBytes    ",
1573 	"TxOffloadFrames   ",
1574 	"RxBroadcastBytes  ",
1575 	"RxBroadcastFrames ",
1576 	"RxMulticastBytes  ",
1577 	"RxMulticastFrames ",
1578 	"RxUnicastBytes    ",
1579 	"RxUnicastFrames   ",
1580 	"RxErrorFrames     ",
1581 
1582 	/*
1583 	 * These are accumulated per-queue statistics and must match the
1584 	 * order of the fields in the queue_port_stats structure.
1585 	 */
1586 	"TSO               ",
1587 	"TxCsumOffload     ",
1588 	"RxCsumGood        ",
1589 	"VLANextractions   ",
1590 	"VLANinsertions    ",
1591 	"GROPackets        ",
1592 	"GROMerged         ",
1593 };
1594 
1595 /*
1596  * Return the number of statistics in the specified statistics set.
1597  */
1598 static int cxgb4vf_get_sset_count(struct net_device *dev, int sset)
1599 {
1600 	switch (sset) {
1601 	case ETH_SS_STATS:
1602 		return ARRAY_SIZE(stats_strings);
1603 	default:
1604 		return -EOPNOTSUPP;
1605 	}
1606 	/*NOTREACHED*/
1607 }
1608 
1609 /*
1610  * Return the strings for the specified statistics set.
1611  */
1612 static void cxgb4vf_get_strings(struct net_device *dev,
1613 				u32 sset,
1614 				u8 *data)
1615 {
1616 	switch (sset) {
1617 	case ETH_SS_STATS:
1618 		memcpy(data, stats_strings, sizeof(stats_strings));
1619 		break;
1620 	}
1621 }
1622 
1623 /*
1624  * Small utility routine to accumulate queue statistics across the queues of
1625  * a "port".
1626  */
1627 static void collect_sge_port_stats(const struct adapter *adapter,
1628 				   const struct port_info *pi,
1629 				   struct queue_port_stats *stats)
1630 {
1631 	const struct sge_eth_txq *txq = &adapter->sge.ethtxq[pi->first_qset];
1632 	const struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[pi->first_qset];
1633 	int qs;
1634 
1635 	memset(stats, 0, sizeof(*stats));
1636 	for (qs = 0; qs < pi->nqsets; qs++, rxq++, txq++) {
1637 		stats->tso += txq->tso;
1638 		stats->tx_csum += txq->tx_cso;
1639 		stats->rx_csum += rxq->stats.rx_cso;
1640 		stats->vlan_ex += rxq->stats.vlan_ex;
1641 		stats->vlan_ins += txq->vlan_ins;
1642 		stats->lro_pkts += rxq->stats.lro_pkts;
1643 		stats->lro_merged += rxq->stats.lro_merged;
1644 	}
1645 }
1646 
1647 /*
1648  * Return the ETH_SS_STATS statistics set.
1649  */
1650 static void cxgb4vf_get_ethtool_stats(struct net_device *dev,
1651 				      struct ethtool_stats *stats,
1652 				      u64 *data)
1653 {
1654 	struct port_info *pi = netdev2pinfo(dev);
1655 	struct adapter *adapter = pi->adapter;
1656 	int err = t4vf_get_port_stats(adapter, pi->pidx,
1657 				      (struct t4vf_port_stats *)data);
1658 	if (err)
1659 		memset(data, 0, sizeof(struct t4vf_port_stats));
1660 
1661 	data += sizeof(struct t4vf_port_stats) / sizeof(u64);
1662 	collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
1663 }
1664 
1665 /*
1666  * Return the size of our register map.
1667  */
1668 static int cxgb4vf_get_regs_len(struct net_device *dev)
1669 {
1670 	return T4VF_REGMAP_SIZE;
1671 }
1672 
1673 /*
1674  * Dump a block of registers, start to end inclusive, into a buffer.
1675  */
1676 static void reg_block_dump(struct adapter *adapter, void *regbuf,
1677 			   unsigned int start, unsigned int end)
1678 {
1679 	u32 *bp = regbuf + start - T4VF_REGMAP_START;
1680 
1681 	for ( ; start <= end; start += sizeof(u32)) {
1682 		/*
1683 		 * Avoid reading the Mailbox Control register since that
1684 		 * can trigger a Mailbox Ownership Arbitration cycle and
1685 		 * interfere with communication with the firmware.
1686 		 */
1687 		if (start == T4VF_CIM_BASE_ADDR + CIM_VF_EXT_MAILBOX_CTRL)
1688 			*bp++ = 0xffff;
1689 		else
1690 			*bp++ = t4_read_reg(adapter, start);
1691 	}
1692 }
1693 
1694 /*
1695  * Copy our entire register map into the provided buffer.
1696  */
1697 static void cxgb4vf_get_regs(struct net_device *dev,
1698 			     struct ethtool_regs *regs,
1699 			     void *regbuf)
1700 {
1701 	struct adapter *adapter = netdev2adap(dev);
1702 
1703 	regs->version = mk_adap_vers(adapter);
1704 
1705 	/*
1706 	 * Fill in register buffer with our register map.
1707 	 */
1708 	memset(regbuf, 0, T4VF_REGMAP_SIZE);
1709 
1710 	reg_block_dump(adapter, regbuf,
1711 		       T4VF_SGE_BASE_ADDR + T4VF_MOD_MAP_SGE_FIRST,
1712 		       T4VF_SGE_BASE_ADDR + T4VF_MOD_MAP_SGE_LAST);
1713 	reg_block_dump(adapter, regbuf,
1714 		       T4VF_MPS_BASE_ADDR + T4VF_MOD_MAP_MPS_FIRST,
1715 		       T4VF_MPS_BASE_ADDR + T4VF_MOD_MAP_MPS_LAST);
1716 
1717 	/* T5 adds new registers in the PL Register map.
1718 	 */
1719 	reg_block_dump(adapter, regbuf,
1720 		       T4VF_PL_BASE_ADDR + T4VF_MOD_MAP_PL_FIRST,
1721 		       T4VF_PL_BASE_ADDR + (is_t4(adapter->params.chip)
1722 		       ? PL_VF_WHOAMI_A : PL_VF_REVISION_A));
1723 	reg_block_dump(adapter, regbuf,
1724 		       T4VF_CIM_BASE_ADDR + T4VF_MOD_MAP_CIM_FIRST,
1725 		       T4VF_CIM_BASE_ADDR + T4VF_MOD_MAP_CIM_LAST);
1726 
1727 	reg_block_dump(adapter, regbuf,
1728 		       T4VF_MBDATA_BASE_ADDR + T4VF_MBDATA_FIRST,
1729 		       T4VF_MBDATA_BASE_ADDR + T4VF_MBDATA_LAST);
1730 }
1731 
1732 /*
1733  * Report current Wake On LAN settings.
1734  */
1735 static void cxgb4vf_get_wol(struct net_device *dev,
1736 			    struct ethtool_wolinfo *wol)
1737 {
1738 	wol->supported = 0;
1739 	wol->wolopts = 0;
1740 	memset(&wol->sopass, 0, sizeof(wol->sopass));
1741 }
1742 
1743 /*
1744  * TCP Segmentation Offload flags which we support.
1745  */
1746 #define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
1747 
1748 static const struct ethtool_ops cxgb4vf_ethtool_ops = {
1749 	.get_link_ksettings	= cxgb4vf_get_link_ksettings,
1750 	.get_drvinfo		= cxgb4vf_get_drvinfo,
1751 	.get_msglevel		= cxgb4vf_get_msglevel,
1752 	.set_msglevel		= cxgb4vf_set_msglevel,
1753 	.get_ringparam		= cxgb4vf_get_ringparam,
1754 	.set_ringparam		= cxgb4vf_set_ringparam,
1755 	.get_coalesce		= cxgb4vf_get_coalesce,
1756 	.set_coalesce		= cxgb4vf_set_coalesce,
1757 	.get_pauseparam		= cxgb4vf_get_pauseparam,
1758 	.get_link		= ethtool_op_get_link,
1759 	.get_strings		= cxgb4vf_get_strings,
1760 	.set_phys_id		= cxgb4vf_phys_id,
1761 	.get_sset_count		= cxgb4vf_get_sset_count,
1762 	.get_ethtool_stats	= cxgb4vf_get_ethtool_stats,
1763 	.get_regs_len		= cxgb4vf_get_regs_len,
1764 	.get_regs		= cxgb4vf_get_regs,
1765 	.get_wol		= cxgb4vf_get_wol,
1766 };
1767 
1768 /*
1769  * /sys/kernel/debug/cxgb4vf support code and data.
1770  * ================================================
1771  */
1772 
1773 /*
1774  * Show Firmware Mailbox Command/Reply Log
1775  *
1776  * Note that we don't do any locking when dumping the Firmware Mailbox Log so
1777  * it's possible that we can catch things during a log update and therefore
1778  * see partially corrupted log entries.  But i9t's probably Good Enough(tm).
1779  * If we ever decide that we want to make sure that we're dumping a coherent
1780  * log, we'd need to perform locking in the mailbox logging and in
1781  * mboxlog_open() where we'd need to grab the entire mailbox log in one go
1782  * like we do for the Firmware Device Log.  But as stated above, meh ...
1783  */
1784 static int mboxlog_show(struct seq_file *seq, void *v)
1785 {
1786 	struct adapter *adapter = seq->private;
1787 	struct mbox_cmd_log *log = adapter->mbox_log;
1788 	struct mbox_cmd *entry;
1789 	int entry_idx, i;
1790 
1791 	if (v == SEQ_START_TOKEN) {
1792 		seq_printf(seq,
1793 			   "%10s  %15s  %5s  %5s  %s\n",
1794 			   "Seq#", "Tstamp", "Atime", "Etime",
1795 			   "Command/Reply");
1796 		return 0;
1797 	}
1798 
1799 	entry_idx = log->cursor + ((uintptr_t)v - 2);
1800 	if (entry_idx >= log->size)
1801 		entry_idx -= log->size;
1802 	entry = mbox_cmd_log_entry(log, entry_idx);
1803 
1804 	/* skip over unused entries */
1805 	if (entry->timestamp == 0)
1806 		return 0;
1807 
1808 	seq_printf(seq, "%10u  %15llu  %5d  %5d",
1809 		   entry->seqno, entry->timestamp,
1810 		   entry->access, entry->execute);
1811 	for (i = 0; i < MBOX_LEN / 8; i++) {
1812 		u64 flit = entry->cmd[i];
1813 		u32 hi = (u32)(flit >> 32);
1814 		u32 lo = (u32)flit;
1815 
1816 		seq_printf(seq, "  %08x %08x", hi, lo);
1817 	}
1818 	seq_puts(seq, "\n");
1819 	return 0;
1820 }
1821 
1822 static inline void *mboxlog_get_idx(struct seq_file *seq, loff_t pos)
1823 {
1824 	struct adapter *adapter = seq->private;
1825 	struct mbox_cmd_log *log = adapter->mbox_log;
1826 
1827 	return ((pos <= log->size) ? (void *)(uintptr_t)(pos + 1) : NULL);
1828 }
1829 
1830 static void *mboxlog_start(struct seq_file *seq, loff_t *pos)
1831 {
1832 	return *pos ? mboxlog_get_idx(seq, *pos) : SEQ_START_TOKEN;
1833 }
1834 
1835 static void *mboxlog_next(struct seq_file *seq, void *v, loff_t *pos)
1836 {
1837 	++*pos;
1838 	return mboxlog_get_idx(seq, *pos);
1839 }
1840 
1841 static void mboxlog_stop(struct seq_file *seq, void *v)
1842 {
1843 }
1844 
1845 static const struct seq_operations mboxlog_seq_ops = {
1846 	.start = mboxlog_start,
1847 	.next  = mboxlog_next,
1848 	.stop  = mboxlog_stop,
1849 	.show  = mboxlog_show
1850 };
1851 
1852 static int mboxlog_open(struct inode *inode, struct file *file)
1853 {
1854 	int res = seq_open(file, &mboxlog_seq_ops);
1855 
1856 	if (!res) {
1857 		struct seq_file *seq = file->private_data;
1858 
1859 		seq->private = inode->i_private;
1860 	}
1861 	return res;
1862 }
1863 
1864 static const struct file_operations mboxlog_fops = {
1865 	.owner   = THIS_MODULE,
1866 	.open    = mboxlog_open,
1867 	.read    = seq_read,
1868 	.llseek  = seq_lseek,
1869 	.release = seq_release,
1870 };
1871 
1872 /*
1873  * Show SGE Queue Set information.  We display QPL Queues Sets per line.
1874  */
1875 #define QPL	4
1876 
1877 static int sge_qinfo_show(struct seq_file *seq, void *v)
1878 {
1879 	struct adapter *adapter = seq->private;
1880 	int eth_entries = DIV_ROUND_UP(adapter->sge.ethqsets, QPL);
1881 	int qs, r = (uintptr_t)v - 1;
1882 
1883 	if (r)
1884 		seq_putc(seq, '\n');
1885 
1886 	#define S3(fmt_spec, s, v) \
1887 		do {\
1888 			seq_printf(seq, "%-12s", s); \
1889 			for (qs = 0; qs < n; ++qs) \
1890 				seq_printf(seq, " %16" fmt_spec, v); \
1891 			seq_putc(seq, '\n'); \
1892 		} while (0)
1893 	#define S(s, v)		S3("s", s, v)
1894 	#define T(s, v)		S3("u", s, txq[qs].v)
1895 	#define R(s, v)		S3("u", s, rxq[qs].v)
1896 
1897 	if (r < eth_entries) {
1898 		const struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[r * QPL];
1899 		const struct sge_eth_txq *txq = &adapter->sge.ethtxq[r * QPL];
1900 		int n = min(QPL, adapter->sge.ethqsets - QPL * r);
1901 
1902 		S("QType:", "Ethernet");
1903 		S("Interface:",
1904 		  (rxq[qs].rspq.netdev
1905 		   ? rxq[qs].rspq.netdev->name
1906 		   : "N/A"));
1907 		S3("d", "Port:",
1908 		   (rxq[qs].rspq.netdev
1909 		    ? ((struct port_info *)
1910 		       netdev_priv(rxq[qs].rspq.netdev))->port_id
1911 		    : -1));
1912 		T("TxQ ID:", q.abs_id);
1913 		T("TxQ size:", q.size);
1914 		T("TxQ inuse:", q.in_use);
1915 		T("TxQ PIdx:", q.pidx);
1916 		T("TxQ CIdx:", q.cidx);
1917 		R("RspQ ID:", rspq.abs_id);
1918 		R("RspQ size:", rspq.size);
1919 		R("RspQE size:", rspq.iqe_len);
1920 		S3("u", "Intr delay:", qtimer_val(adapter, &rxq[qs].rspq));
1921 		S3("u", "Intr pktcnt:",
1922 		   adapter->sge.counter_val[rxq[qs].rspq.pktcnt_idx]);
1923 		R("RspQ CIdx:", rspq.cidx);
1924 		R("RspQ Gen:", rspq.gen);
1925 		R("FL ID:", fl.abs_id);
1926 		R("FL size:", fl.size - MIN_FL_RESID);
1927 		R("FL avail:", fl.avail);
1928 		R("FL PIdx:", fl.pidx);
1929 		R("FL CIdx:", fl.cidx);
1930 		return 0;
1931 	}
1932 
1933 	r -= eth_entries;
1934 	if (r == 0) {
1935 		const struct sge_rspq *evtq = &adapter->sge.fw_evtq;
1936 
1937 		seq_printf(seq, "%-12s %16s\n", "QType:", "FW event queue");
1938 		seq_printf(seq, "%-12s %16u\n", "RspQ ID:", evtq->abs_id);
1939 		seq_printf(seq, "%-12s %16u\n", "Intr delay:",
1940 			   qtimer_val(adapter, evtq));
1941 		seq_printf(seq, "%-12s %16u\n", "Intr pktcnt:",
1942 			   adapter->sge.counter_val[evtq->pktcnt_idx]);
1943 		seq_printf(seq, "%-12s %16u\n", "RspQ Cidx:", evtq->cidx);
1944 		seq_printf(seq, "%-12s %16u\n", "RspQ Gen:", evtq->gen);
1945 	} else if (r == 1) {
1946 		const struct sge_rspq *intrq = &adapter->sge.intrq;
1947 
1948 		seq_printf(seq, "%-12s %16s\n", "QType:", "Interrupt Queue");
1949 		seq_printf(seq, "%-12s %16u\n", "RspQ ID:", intrq->abs_id);
1950 		seq_printf(seq, "%-12s %16u\n", "Intr delay:",
1951 			   qtimer_val(adapter, intrq));
1952 		seq_printf(seq, "%-12s %16u\n", "Intr pktcnt:",
1953 			   adapter->sge.counter_val[intrq->pktcnt_idx]);
1954 		seq_printf(seq, "%-12s %16u\n", "RspQ Cidx:", intrq->cidx);
1955 		seq_printf(seq, "%-12s %16u\n", "RspQ Gen:", intrq->gen);
1956 	}
1957 
1958 	#undef R
1959 	#undef T
1960 	#undef S
1961 	#undef S3
1962 
1963 	return 0;
1964 }
1965 
1966 /*
1967  * Return the number of "entries" in our "file".  We group the multi-Queue
1968  * sections with QPL Queue Sets per "entry".  The sections of the output are:
1969  *
1970  *     Ethernet RX/TX Queue Sets
1971  *     Firmware Event Queue
1972  *     Forwarded Interrupt Queue (if in MSI mode)
1973  */
1974 static int sge_queue_entries(const struct adapter *adapter)
1975 {
1976 	return DIV_ROUND_UP(adapter->sge.ethqsets, QPL) + 1 +
1977 		((adapter->flags & USING_MSI) != 0);
1978 }
1979 
1980 static void *sge_queue_start(struct seq_file *seq, loff_t *pos)
1981 {
1982 	int entries = sge_queue_entries(seq->private);
1983 
1984 	return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
1985 }
1986 
1987 static void sge_queue_stop(struct seq_file *seq, void *v)
1988 {
1989 }
1990 
1991 static void *sge_queue_next(struct seq_file *seq, void *v, loff_t *pos)
1992 {
1993 	int entries = sge_queue_entries(seq->private);
1994 
1995 	++*pos;
1996 	return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
1997 }
1998 
1999 static const struct seq_operations sge_qinfo_seq_ops = {
2000 	.start = sge_queue_start,
2001 	.next  = sge_queue_next,
2002 	.stop  = sge_queue_stop,
2003 	.show  = sge_qinfo_show
2004 };
2005 
2006 static int sge_qinfo_open(struct inode *inode, struct file *file)
2007 {
2008 	int res = seq_open(file, &sge_qinfo_seq_ops);
2009 
2010 	if (!res) {
2011 		struct seq_file *seq = file->private_data;
2012 		seq->private = inode->i_private;
2013 	}
2014 	return res;
2015 }
2016 
2017 static const struct file_operations sge_qinfo_debugfs_fops = {
2018 	.owner   = THIS_MODULE,
2019 	.open    = sge_qinfo_open,
2020 	.read    = seq_read,
2021 	.llseek  = seq_lseek,
2022 	.release = seq_release,
2023 };
2024 
2025 /*
2026  * Show SGE Queue Set statistics.  We display QPL Queues Sets per line.
2027  */
2028 #define QPL	4
2029 
2030 static int sge_qstats_show(struct seq_file *seq, void *v)
2031 {
2032 	struct adapter *adapter = seq->private;
2033 	int eth_entries = DIV_ROUND_UP(adapter->sge.ethqsets, QPL);
2034 	int qs, r = (uintptr_t)v - 1;
2035 
2036 	if (r)
2037 		seq_putc(seq, '\n');
2038 
2039 	#define S3(fmt, s, v) \
2040 		do { \
2041 			seq_printf(seq, "%-16s", s); \
2042 			for (qs = 0; qs < n; ++qs) \
2043 				seq_printf(seq, " %8" fmt, v); \
2044 			seq_putc(seq, '\n'); \
2045 		} while (0)
2046 	#define S(s, v)		S3("s", s, v)
2047 
2048 	#define T3(fmt, s, v)	S3(fmt, s, txq[qs].v)
2049 	#define T(s, v)		T3("lu", s, v)
2050 
2051 	#define R3(fmt, s, v)	S3(fmt, s, rxq[qs].v)
2052 	#define R(s, v)		R3("lu", s, v)
2053 
2054 	if (r < eth_entries) {
2055 		const struct sge_eth_rxq *rxq = &adapter->sge.ethrxq[r * QPL];
2056 		const struct sge_eth_txq *txq = &adapter->sge.ethtxq[r * QPL];
2057 		int n = min(QPL, adapter->sge.ethqsets - QPL * r);
2058 
2059 		S("QType:", "Ethernet");
2060 		S("Interface:",
2061 		  (rxq[qs].rspq.netdev
2062 		   ? rxq[qs].rspq.netdev->name
2063 		   : "N/A"));
2064 		R3("u", "RspQNullInts:", rspq.unhandled_irqs);
2065 		R("RxPackets:", stats.pkts);
2066 		R("RxCSO:", stats.rx_cso);
2067 		R("VLANxtract:", stats.vlan_ex);
2068 		R("LROmerged:", stats.lro_merged);
2069 		R("LROpackets:", stats.lro_pkts);
2070 		R("RxDrops:", stats.rx_drops);
2071 		T("TSO:", tso);
2072 		T("TxCSO:", tx_cso);
2073 		T("VLANins:", vlan_ins);
2074 		T("TxQFull:", q.stops);
2075 		T("TxQRestarts:", q.restarts);
2076 		T("TxMapErr:", mapping_err);
2077 		R("FLAllocErr:", fl.alloc_failed);
2078 		R("FLLrgAlcErr:", fl.large_alloc_failed);
2079 		R("FLStarving:", fl.starving);
2080 		return 0;
2081 	}
2082 
2083 	r -= eth_entries;
2084 	if (r == 0) {
2085 		const struct sge_rspq *evtq = &adapter->sge.fw_evtq;
2086 
2087 		seq_printf(seq, "%-8s %16s\n", "QType:", "FW event queue");
2088 		seq_printf(seq, "%-16s %8u\n", "RspQNullInts:",
2089 			   evtq->unhandled_irqs);
2090 		seq_printf(seq, "%-16s %8u\n", "RspQ CIdx:", evtq->cidx);
2091 		seq_printf(seq, "%-16s %8u\n", "RspQ Gen:", evtq->gen);
2092 	} else if (r == 1) {
2093 		const struct sge_rspq *intrq = &adapter->sge.intrq;
2094 
2095 		seq_printf(seq, "%-8s %16s\n", "QType:", "Interrupt Queue");
2096 		seq_printf(seq, "%-16s %8u\n", "RspQNullInts:",
2097 			   intrq->unhandled_irqs);
2098 		seq_printf(seq, "%-16s %8u\n", "RspQ CIdx:", intrq->cidx);
2099 		seq_printf(seq, "%-16s %8u\n", "RspQ Gen:", intrq->gen);
2100 	}
2101 
2102 	#undef R
2103 	#undef T
2104 	#undef S
2105 	#undef R3
2106 	#undef T3
2107 	#undef S3
2108 
2109 	return 0;
2110 }
2111 
2112 /*
2113  * Return the number of "entries" in our "file".  We group the multi-Queue
2114  * sections with QPL Queue Sets per "entry".  The sections of the output are:
2115  *
2116  *     Ethernet RX/TX Queue Sets
2117  *     Firmware Event Queue
2118  *     Forwarded Interrupt Queue (if in MSI mode)
2119  */
2120 static int sge_qstats_entries(const struct adapter *adapter)
2121 {
2122 	return DIV_ROUND_UP(adapter->sge.ethqsets, QPL) + 1 +
2123 		((adapter->flags & USING_MSI) != 0);
2124 }
2125 
2126 static void *sge_qstats_start(struct seq_file *seq, loff_t *pos)
2127 {
2128 	int entries = sge_qstats_entries(seq->private);
2129 
2130 	return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
2131 }
2132 
2133 static void sge_qstats_stop(struct seq_file *seq, void *v)
2134 {
2135 }
2136 
2137 static void *sge_qstats_next(struct seq_file *seq, void *v, loff_t *pos)
2138 {
2139 	int entries = sge_qstats_entries(seq->private);
2140 
2141 	(*pos)++;
2142 	return *pos < entries ? (void *)((uintptr_t)*pos + 1) : NULL;
2143 }
2144 
2145 static const struct seq_operations sge_qstats_seq_ops = {
2146 	.start = sge_qstats_start,
2147 	.next  = sge_qstats_next,
2148 	.stop  = sge_qstats_stop,
2149 	.show  = sge_qstats_show
2150 };
2151 
2152 static int sge_qstats_open(struct inode *inode, struct file *file)
2153 {
2154 	int res = seq_open(file, &sge_qstats_seq_ops);
2155 
2156 	if (res == 0) {
2157 		struct seq_file *seq = file->private_data;
2158 		seq->private = inode->i_private;
2159 	}
2160 	return res;
2161 }
2162 
2163 static const struct file_operations sge_qstats_proc_fops = {
2164 	.owner   = THIS_MODULE,
2165 	.open    = sge_qstats_open,
2166 	.read    = seq_read,
2167 	.llseek  = seq_lseek,
2168 	.release = seq_release,
2169 };
2170 
2171 /*
2172  * Show PCI-E SR-IOV Virtual Function Resource Limits.
2173  */
2174 static int resources_show(struct seq_file *seq, void *v)
2175 {
2176 	struct adapter *adapter = seq->private;
2177 	struct vf_resources *vfres = &adapter->params.vfres;
2178 
2179 	#define S(desc, fmt, var) \
2180 		seq_printf(seq, "%-60s " fmt "\n", \
2181 			   desc " (" #var "):", vfres->var)
2182 
2183 	S("Virtual Interfaces", "%d", nvi);
2184 	S("Egress Queues", "%d", neq);
2185 	S("Ethernet Control", "%d", nethctrl);
2186 	S("Ingress Queues/w Free Lists/Interrupts", "%d", niqflint);
2187 	S("Ingress Queues", "%d", niq);
2188 	S("Traffic Class", "%d", tc);
2189 	S("Port Access Rights Mask", "%#x", pmask);
2190 	S("MAC Address Filters", "%d", nexactf);
2191 	S("Firmware Command Read Capabilities", "%#x", r_caps);
2192 	S("Firmware Command Write/Execute Capabilities", "%#x", wx_caps);
2193 
2194 	#undef S
2195 
2196 	return 0;
2197 }
2198 
2199 static int resources_open(struct inode *inode, struct file *file)
2200 {
2201 	return single_open(file, resources_show, inode->i_private);
2202 }
2203 
2204 static const struct file_operations resources_proc_fops = {
2205 	.owner   = THIS_MODULE,
2206 	.open    = resources_open,
2207 	.read    = seq_read,
2208 	.llseek  = seq_lseek,
2209 	.release = single_release,
2210 };
2211 
2212 /*
2213  * Show Virtual Interfaces.
2214  */
2215 static int interfaces_show(struct seq_file *seq, void *v)
2216 {
2217 	if (v == SEQ_START_TOKEN) {
2218 		seq_puts(seq, "Interface  Port   VIID\n");
2219 	} else {
2220 		struct adapter *adapter = seq->private;
2221 		int pidx = (uintptr_t)v - 2;
2222 		struct net_device *dev = adapter->port[pidx];
2223 		struct port_info *pi = netdev_priv(dev);
2224 
2225 		seq_printf(seq, "%9s  %4d  %#5x\n",
2226 			   dev->name, pi->port_id, pi->viid);
2227 	}
2228 	return 0;
2229 }
2230 
2231 static inline void *interfaces_get_idx(struct adapter *adapter, loff_t pos)
2232 {
2233 	return pos <= adapter->params.nports
2234 		? (void *)(uintptr_t)(pos + 1)
2235 		: NULL;
2236 }
2237 
2238 static void *interfaces_start(struct seq_file *seq, loff_t *pos)
2239 {
2240 	return *pos
2241 		? interfaces_get_idx(seq->private, *pos)
2242 		: SEQ_START_TOKEN;
2243 }
2244 
2245 static void *interfaces_next(struct seq_file *seq, void *v, loff_t *pos)
2246 {
2247 	(*pos)++;
2248 	return interfaces_get_idx(seq->private, *pos);
2249 }
2250 
2251 static void interfaces_stop(struct seq_file *seq, void *v)
2252 {
2253 }
2254 
2255 static const struct seq_operations interfaces_seq_ops = {
2256 	.start = interfaces_start,
2257 	.next  = interfaces_next,
2258 	.stop  = interfaces_stop,
2259 	.show  = interfaces_show
2260 };
2261 
2262 static int interfaces_open(struct inode *inode, struct file *file)
2263 {
2264 	int res = seq_open(file, &interfaces_seq_ops);
2265 
2266 	if (res == 0) {
2267 		struct seq_file *seq = file->private_data;
2268 		seq->private = inode->i_private;
2269 	}
2270 	return res;
2271 }
2272 
2273 static const struct file_operations interfaces_proc_fops = {
2274 	.owner   = THIS_MODULE,
2275 	.open    = interfaces_open,
2276 	.read    = seq_read,
2277 	.llseek  = seq_lseek,
2278 	.release = seq_release,
2279 };
2280 
2281 /*
2282  * /sys/kernel/debugfs/cxgb4vf/ files list.
2283  */
2284 struct cxgb4vf_debugfs_entry {
2285 	const char *name;		/* name of debugfs node */
2286 	umode_t mode;			/* file system mode */
2287 	const struct file_operations *fops;
2288 };
2289 
2290 static struct cxgb4vf_debugfs_entry debugfs_files[] = {
2291 	{ "mboxlog",    S_IRUGO, &mboxlog_fops },
2292 	{ "sge_qinfo",  S_IRUGO, &sge_qinfo_debugfs_fops },
2293 	{ "sge_qstats", S_IRUGO, &sge_qstats_proc_fops },
2294 	{ "resources",  S_IRUGO, &resources_proc_fops },
2295 	{ "interfaces", S_IRUGO, &interfaces_proc_fops },
2296 };
2297 
2298 /*
2299  * Module and device initialization and cleanup code.
2300  * ==================================================
2301  */
2302 
2303 /*
2304  * Set up out /sys/kernel/debug/cxgb4vf sub-nodes.  We assume that the
2305  * directory (debugfs_root) has already been set up.
2306  */
2307 static int setup_debugfs(struct adapter *adapter)
2308 {
2309 	int i;
2310 
2311 	BUG_ON(IS_ERR_OR_NULL(adapter->debugfs_root));
2312 
2313 	/*
2314 	 * Debugfs support is best effort.
2315 	 */
2316 	for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
2317 		(void)debugfs_create_file(debugfs_files[i].name,
2318 				  debugfs_files[i].mode,
2319 				  adapter->debugfs_root,
2320 				  (void *)adapter,
2321 				  debugfs_files[i].fops);
2322 
2323 	return 0;
2324 }
2325 
2326 /*
2327  * Tear down the /sys/kernel/debug/cxgb4vf sub-nodes created above.  We leave
2328  * it to our caller to tear down the directory (debugfs_root).
2329  */
2330 static void cleanup_debugfs(struct adapter *adapter)
2331 {
2332 	BUG_ON(IS_ERR_OR_NULL(adapter->debugfs_root));
2333 
2334 	/*
2335 	 * Unlike our sister routine cleanup_proc(), we don't need to remove
2336 	 * individual entries because a call will be made to
2337 	 * debugfs_remove_recursive().  We just need to clean up any ancillary
2338 	 * persistent state.
2339 	 */
2340 	/* nothing to do */
2341 }
2342 
2343 /* Figure out how many Ports and Queue Sets we can support.  This depends on
2344  * knowing our Virtual Function Resources and may be called a second time if
2345  * we fall back from MSI-X to MSI Interrupt Mode.
2346  */
2347 static void size_nports_qsets(struct adapter *adapter)
2348 {
2349 	struct vf_resources *vfres = &adapter->params.vfres;
2350 	unsigned int ethqsets, pmask_nports;
2351 
2352 	/* The number of "ports" which we support is equal to the number of
2353 	 * Virtual Interfaces with which we've been provisioned.
2354 	 */
2355 	adapter->params.nports = vfres->nvi;
2356 	if (adapter->params.nports > MAX_NPORTS) {
2357 		dev_warn(adapter->pdev_dev, "only using %d of %d maximum"
2358 			 " allowed virtual interfaces\n", MAX_NPORTS,
2359 			 adapter->params.nports);
2360 		adapter->params.nports = MAX_NPORTS;
2361 	}
2362 
2363 	/* We may have been provisioned with more VIs than the number of
2364 	 * ports we're allowed to access (our Port Access Rights Mask).
2365 	 * This is obviously a configuration conflict but we don't want to
2366 	 * crash the kernel or anything silly just because of that.
2367 	 */
2368 	pmask_nports = hweight32(adapter->params.vfres.pmask);
2369 	if (pmask_nports < adapter->params.nports) {
2370 		dev_warn(adapter->pdev_dev, "only using %d of %d provisioned"
2371 			 " virtual interfaces; limited by Port Access Rights"
2372 			 " mask %#x\n", pmask_nports, adapter->params.nports,
2373 			 adapter->params.vfres.pmask);
2374 		adapter->params.nports = pmask_nports;
2375 	}
2376 
2377 	/* We need to reserve an Ingress Queue for the Asynchronous Firmware
2378 	 * Event Queue.  And if we're using MSI Interrupts, we'll also need to
2379 	 * reserve an Ingress Queue for a Forwarded Interrupts.
2380 	 *
2381 	 * The rest of the FL/Intr-capable ingress queues will be matched up
2382 	 * one-for-one with Ethernet/Control egress queues in order to form
2383 	 * "Queue Sets" which will be aportioned between the "ports".  For
2384 	 * each Queue Set, we'll need the ability to allocate two Egress
2385 	 * Contexts -- one for the Ingress Queue Free List and one for the TX
2386 	 * Ethernet Queue.
2387 	 *
2388 	 * Note that even if we're currently configured to use MSI-X
2389 	 * Interrupts (module variable msi == MSI_MSIX) we may get downgraded
2390 	 * to MSI Interrupts if we can't get enough MSI-X Interrupts.  If that
2391 	 * happens we'll need to adjust things later.
2392 	 */
2393 	ethqsets = vfres->niqflint - 1 - (msi == MSI_MSI);
2394 	if (vfres->nethctrl != ethqsets)
2395 		ethqsets = min(vfres->nethctrl, ethqsets);
2396 	if (vfres->neq < ethqsets*2)
2397 		ethqsets = vfres->neq/2;
2398 	if (ethqsets > MAX_ETH_QSETS)
2399 		ethqsets = MAX_ETH_QSETS;
2400 	adapter->sge.max_ethqsets = ethqsets;
2401 
2402 	if (adapter->sge.max_ethqsets < adapter->params.nports) {
2403 		dev_warn(adapter->pdev_dev, "only using %d of %d available"
2404 			 " virtual interfaces (too few Queue Sets)\n",
2405 			 adapter->sge.max_ethqsets, adapter->params.nports);
2406 		adapter->params.nports = adapter->sge.max_ethqsets;
2407 	}
2408 }
2409 
2410 /*
2411  * Perform early "adapter" initialization.  This is where we discover what
2412  * adapter parameters we're going to be using and initialize basic adapter
2413  * hardware support.
2414  */
2415 static int adap_init0(struct adapter *adapter)
2416 {
2417 	struct sge_params *sge_params = &adapter->params.sge;
2418 	struct sge *s = &adapter->sge;
2419 	int err;
2420 	u32 param, val = 0;
2421 
2422 	/*
2423 	 * Some environments do not properly handle PCIE FLRs -- e.g. in Linux
2424 	 * 2.6.31 and later we can't call pci_reset_function() in order to
2425 	 * issue an FLR because of a self- deadlock on the device semaphore.
2426 	 * Meanwhile, the OS infrastructure doesn't issue FLRs in all the
2427 	 * cases where they're needed -- for instance, some versions of KVM
2428 	 * fail to reset "Assigned Devices" when the VM reboots.  Therefore we
2429 	 * use the firmware based reset in order to reset any per function
2430 	 * state.
2431 	 */
2432 	err = t4vf_fw_reset(adapter);
2433 	if (err < 0) {
2434 		dev_err(adapter->pdev_dev, "FW reset failed: err=%d\n", err);
2435 		return err;
2436 	}
2437 
2438 	/*
2439 	 * Grab basic operational parameters.  These will predominantly have
2440 	 * been set up by the Physical Function Driver or will be hard coded
2441 	 * into the adapter.  We just have to live with them ...  Note that
2442 	 * we _must_ get our VPD parameters before our SGE parameters because
2443 	 * we need to know the adapter's core clock from the VPD in order to
2444 	 * properly decode the SGE Timer Values.
2445 	 */
2446 	err = t4vf_get_dev_params(adapter);
2447 	if (err) {
2448 		dev_err(adapter->pdev_dev, "unable to retrieve adapter"
2449 			" device parameters: err=%d\n", err);
2450 		return err;
2451 	}
2452 	err = t4vf_get_vpd_params(adapter);
2453 	if (err) {
2454 		dev_err(adapter->pdev_dev, "unable to retrieve adapter"
2455 			" VPD parameters: err=%d\n", err);
2456 		return err;
2457 	}
2458 	err = t4vf_get_sge_params(adapter);
2459 	if (err) {
2460 		dev_err(adapter->pdev_dev, "unable to retrieve adapter"
2461 			" SGE parameters: err=%d\n", err);
2462 		return err;
2463 	}
2464 	err = t4vf_get_rss_glb_config(adapter);
2465 	if (err) {
2466 		dev_err(adapter->pdev_dev, "unable to retrieve adapter"
2467 			" RSS parameters: err=%d\n", err);
2468 		return err;
2469 	}
2470 	if (adapter->params.rss.mode !=
2471 	    FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL) {
2472 		dev_err(adapter->pdev_dev, "unable to operate with global RSS"
2473 			" mode %d\n", adapter->params.rss.mode);
2474 		return -EINVAL;
2475 	}
2476 	err = t4vf_sge_init(adapter);
2477 	if (err) {
2478 		dev_err(adapter->pdev_dev, "unable to use adapter parameters:"
2479 			" err=%d\n", err);
2480 		return err;
2481 	}
2482 
2483 	/* If we're running on newer firmware, let it know that we're
2484 	 * prepared to deal with encapsulated CPL messages.  Older
2485 	 * firmware won't understand this and we'll just get
2486 	 * unencapsulated messages ...
2487 	 */
2488 	param = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2489 		FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_CPLFW4MSG_ENCAP);
2490 	val = 1;
2491 	(void) t4vf_set_params(adapter, 1, &param, &val);
2492 
2493 	/*
2494 	 * Retrieve our RX interrupt holdoff timer values and counter
2495 	 * threshold values from the SGE parameters.
2496 	 */
2497 	s->timer_val[0] = core_ticks_to_us(adapter,
2498 		TIMERVALUE0_G(sge_params->sge_timer_value_0_and_1));
2499 	s->timer_val[1] = core_ticks_to_us(adapter,
2500 		TIMERVALUE1_G(sge_params->sge_timer_value_0_and_1));
2501 	s->timer_val[2] = core_ticks_to_us(adapter,
2502 		TIMERVALUE0_G(sge_params->sge_timer_value_2_and_3));
2503 	s->timer_val[3] = core_ticks_to_us(adapter,
2504 		TIMERVALUE1_G(sge_params->sge_timer_value_2_and_3));
2505 	s->timer_val[4] = core_ticks_to_us(adapter,
2506 		TIMERVALUE0_G(sge_params->sge_timer_value_4_and_5));
2507 	s->timer_val[5] = core_ticks_to_us(adapter,
2508 		TIMERVALUE1_G(sge_params->sge_timer_value_4_and_5));
2509 
2510 	s->counter_val[0] = THRESHOLD_0_G(sge_params->sge_ingress_rx_threshold);
2511 	s->counter_val[1] = THRESHOLD_1_G(sge_params->sge_ingress_rx_threshold);
2512 	s->counter_val[2] = THRESHOLD_2_G(sge_params->sge_ingress_rx_threshold);
2513 	s->counter_val[3] = THRESHOLD_3_G(sge_params->sge_ingress_rx_threshold);
2514 
2515 	/*
2516 	 * Grab our Virtual Interface resource allocation, extract the
2517 	 * features that we're interested in and do a bit of sanity testing on
2518 	 * what we discover.
2519 	 */
2520 	err = t4vf_get_vfres(adapter);
2521 	if (err) {
2522 		dev_err(adapter->pdev_dev, "unable to get virtual interface"
2523 			" resources: err=%d\n", err);
2524 		return err;
2525 	}
2526 
2527 	/* Check for various parameter sanity issues */
2528 	if (adapter->params.vfres.pmask == 0) {
2529 		dev_err(adapter->pdev_dev, "no port access configured\n"
2530 			"usable!\n");
2531 		return -EINVAL;
2532 	}
2533 	if (adapter->params.vfres.nvi == 0) {
2534 		dev_err(adapter->pdev_dev, "no virtual interfaces configured/"
2535 			"usable!\n");
2536 		return -EINVAL;
2537 	}
2538 
2539 	/* Initialize nports and max_ethqsets now that we have our Virtual
2540 	 * Function Resources.
2541 	 */
2542 	size_nports_qsets(adapter);
2543 
2544 	return 0;
2545 }
2546 
2547 static inline void init_rspq(struct sge_rspq *rspq, u8 timer_idx,
2548 			     u8 pkt_cnt_idx, unsigned int size,
2549 			     unsigned int iqe_size)
2550 {
2551 	rspq->intr_params = (QINTR_TIMER_IDX_V(timer_idx) |
2552 			     (pkt_cnt_idx < SGE_NCOUNTERS ?
2553 			      QINTR_CNT_EN_F : 0));
2554 	rspq->pktcnt_idx = (pkt_cnt_idx < SGE_NCOUNTERS
2555 			    ? pkt_cnt_idx
2556 			    : 0);
2557 	rspq->iqe_len = iqe_size;
2558 	rspq->size = size;
2559 }
2560 
2561 /*
2562  * Perform default configuration of DMA queues depending on the number and
2563  * type of ports we found and the number of available CPUs.  Most settings can
2564  * be modified by the admin via ethtool and cxgbtool prior to the adapter
2565  * being brought up for the first time.
2566  */
2567 static void cfg_queues(struct adapter *adapter)
2568 {
2569 	struct sge *s = &adapter->sge;
2570 	int q10g, n10g, qidx, pidx, qs;
2571 	size_t iqe_size;
2572 
2573 	/*
2574 	 * We should not be called till we know how many Queue Sets we can
2575 	 * support.  In particular, this means that we need to know what kind
2576 	 * of interrupts we'll be using ...
2577 	 */
2578 	BUG_ON((adapter->flags & (USING_MSIX|USING_MSI)) == 0);
2579 
2580 	/*
2581 	 * Count the number of 10GbE Virtual Interfaces that we have.
2582 	 */
2583 	n10g = 0;
2584 	for_each_port(adapter, pidx)
2585 		n10g += is_x_10g_port(&adap2pinfo(adapter, pidx)->link_cfg);
2586 
2587 	/*
2588 	 * We default to 1 queue per non-10G port and up to # of cores queues
2589 	 * per 10G port.
2590 	 */
2591 	if (n10g == 0)
2592 		q10g = 0;
2593 	else {
2594 		int n1g = (adapter->params.nports - n10g);
2595 		q10g = (adapter->sge.max_ethqsets - n1g) / n10g;
2596 		if (q10g > num_online_cpus())
2597 			q10g = num_online_cpus();
2598 	}
2599 
2600 	/*
2601 	 * Allocate the "Queue Sets" to the various Virtual Interfaces.
2602 	 * The layout will be established in setup_sge_queues() when the
2603 	 * adapter is brough up for the first time.
2604 	 */
2605 	qidx = 0;
2606 	for_each_port(adapter, pidx) {
2607 		struct port_info *pi = adap2pinfo(adapter, pidx);
2608 
2609 		pi->first_qset = qidx;
2610 		pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
2611 		qidx += pi->nqsets;
2612 	}
2613 	s->ethqsets = qidx;
2614 
2615 	/*
2616 	 * The Ingress Queue Entry Size for our various Response Queues needs
2617 	 * to be big enough to accommodate the largest message we can receive
2618 	 * from the chip/firmware; which is 64 bytes ...
2619 	 */
2620 	iqe_size = 64;
2621 
2622 	/*
2623 	 * Set up default Queue Set parameters ...  Start off with the
2624 	 * shortest interrupt holdoff timer.
2625 	 */
2626 	for (qs = 0; qs < s->max_ethqsets; qs++) {
2627 		struct sge_eth_rxq *rxq = &s->ethrxq[qs];
2628 		struct sge_eth_txq *txq = &s->ethtxq[qs];
2629 
2630 		init_rspq(&rxq->rspq, 0, 0, 1024, iqe_size);
2631 		rxq->fl.size = 72;
2632 		txq->q.size = 1024;
2633 	}
2634 
2635 	/*
2636 	 * The firmware event queue is used for link state changes and
2637 	 * notifications of TX DMA completions.
2638 	 */
2639 	init_rspq(&s->fw_evtq, SGE_TIMER_RSTRT_CNTR, 0, 512, iqe_size);
2640 
2641 	/*
2642 	 * The forwarded interrupt queue is used when we're in MSI interrupt
2643 	 * mode.  In this mode all interrupts associated with RX queues will
2644 	 * be forwarded to a single queue which we'll associate with our MSI
2645 	 * interrupt vector.  The messages dropped in the forwarded interrupt
2646 	 * queue will indicate which ingress queue needs servicing ...  This
2647 	 * queue needs to be large enough to accommodate all of the ingress
2648 	 * queues which are forwarding their interrupt (+1 to prevent the PIDX
2649 	 * from equalling the CIDX if every ingress queue has an outstanding
2650 	 * interrupt).  The queue doesn't need to be any larger because no
2651 	 * ingress queue will ever have more than one outstanding interrupt at
2652 	 * any time ...
2653 	 */
2654 	init_rspq(&s->intrq, SGE_TIMER_RSTRT_CNTR, 0, MSIX_ENTRIES + 1,
2655 		  iqe_size);
2656 }
2657 
2658 /*
2659  * Reduce the number of Ethernet queues across all ports to at most n.
2660  * n provides at least one queue per port.
2661  */
2662 static void reduce_ethqs(struct adapter *adapter, int n)
2663 {
2664 	int i;
2665 	struct port_info *pi;
2666 
2667 	/*
2668 	 * While we have too many active Ether Queue Sets, interate across the
2669 	 * "ports" and reduce their individual Queue Set allocations.
2670 	 */
2671 	BUG_ON(n < adapter->params.nports);
2672 	while (n < adapter->sge.ethqsets)
2673 		for_each_port(adapter, i) {
2674 			pi = adap2pinfo(adapter, i);
2675 			if (pi->nqsets > 1) {
2676 				pi->nqsets--;
2677 				adapter->sge.ethqsets--;
2678 				if (adapter->sge.ethqsets <= n)
2679 					break;
2680 			}
2681 		}
2682 
2683 	/*
2684 	 * Reassign the starting Queue Sets for each of the "ports" ...
2685 	 */
2686 	n = 0;
2687 	for_each_port(adapter, i) {
2688 		pi = adap2pinfo(adapter, i);
2689 		pi->first_qset = n;
2690 		n += pi->nqsets;
2691 	}
2692 }
2693 
2694 /*
2695  * We need to grab enough MSI-X vectors to cover our interrupt needs.  Ideally
2696  * we get a separate MSI-X vector for every "Queue Set" plus any extras we
2697  * need.  Minimally we need one for every Virtual Interface plus those needed
2698  * for our "extras".  Note that this process may lower the maximum number of
2699  * allowed Queue Sets ...
2700  */
2701 static int enable_msix(struct adapter *adapter)
2702 {
2703 	int i, want, need, nqsets;
2704 	struct msix_entry entries[MSIX_ENTRIES];
2705 	struct sge *s = &adapter->sge;
2706 
2707 	for (i = 0; i < MSIX_ENTRIES; ++i)
2708 		entries[i].entry = i;
2709 
2710 	/*
2711 	 * We _want_ enough MSI-X interrupts to cover all of our "Queue Sets"
2712 	 * plus those needed for our "extras" (for example, the firmware
2713 	 * message queue).  We _need_ at least one "Queue Set" per Virtual
2714 	 * Interface plus those needed for our "extras".  So now we get to see
2715 	 * if the song is right ...
2716 	 */
2717 	want = s->max_ethqsets + MSIX_EXTRAS;
2718 	need = adapter->params.nports + MSIX_EXTRAS;
2719 
2720 	want = pci_enable_msix_range(adapter->pdev, entries, need, want);
2721 	if (want < 0)
2722 		return want;
2723 
2724 	nqsets = want - MSIX_EXTRAS;
2725 	if (nqsets < s->max_ethqsets) {
2726 		dev_warn(adapter->pdev_dev, "only enough MSI-X vectors"
2727 			 " for %d Queue Sets\n", nqsets);
2728 		s->max_ethqsets = nqsets;
2729 		if (nqsets < s->ethqsets)
2730 			reduce_ethqs(adapter, nqsets);
2731 	}
2732 	for (i = 0; i < want; ++i)
2733 		adapter->msix_info[i].vec = entries[i].vector;
2734 
2735 	return 0;
2736 }
2737 
2738 static const struct net_device_ops cxgb4vf_netdev_ops	= {
2739 	.ndo_open		= cxgb4vf_open,
2740 	.ndo_stop		= cxgb4vf_stop,
2741 	.ndo_start_xmit		= t4vf_eth_xmit,
2742 	.ndo_get_stats		= cxgb4vf_get_stats,
2743 	.ndo_set_rx_mode	= cxgb4vf_set_rxmode,
2744 	.ndo_set_mac_address	= cxgb4vf_set_mac_addr,
2745 	.ndo_validate_addr	= eth_validate_addr,
2746 	.ndo_do_ioctl		= cxgb4vf_do_ioctl,
2747 	.ndo_change_mtu		= cxgb4vf_change_mtu,
2748 	.ndo_fix_features	= cxgb4vf_fix_features,
2749 	.ndo_set_features	= cxgb4vf_set_features,
2750 #ifdef CONFIG_NET_POLL_CONTROLLER
2751 	.ndo_poll_controller	= cxgb4vf_poll_controller,
2752 #endif
2753 };
2754 
2755 /*
2756  * "Probe" a device: initialize a device and construct all kernel and driver
2757  * state needed to manage the device.  This routine is called "init_one" in
2758  * the PF Driver ...
2759  */
2760 static int cxgb4vf_pci_probe(struct pci_dev *pdev,
2761 			     const struct pci_device_id *ent)
2762 {
2763 	int pci_using_dac;
2764 	int err, pidx;
2765 	unsigned int pmask;
2766 	struct adapter *adapter;
2767 	struct port_info *pi;
2768 	struct net_device *netdev;
2769 	unsigned int pf;
2770 
2771 	/*
2772 	 * Print our driver banner the first time we're called to initialize a
2773 	 * device.
2774 	 */
2775 	pr_info_once("%s - version %s\n", DRV_DESC, DRV_VERSION);
2776 
2777 	/*
2778 	 * Initialize generic PCI device state.
2779 	 */
2780 	err = pci_enable_device(pdev);
2781 	if (err) {
2782 		dev_err(&pdev->dev, "cannot enable PCI device\n");
2783 		return err;
2784 	}
2785 
2786 	/*
2787 	 * Reserve PCI resources for the device.  If we can't get them some
2788 	 * other driver may have already claimed the device ...
2789 	 */
2790 	err = pci_request_regions(pdev, KBUILD_MODNAME);
2791 	if (err) {
2792 		dev_err(&pdev->dev, "cannot obtain PCI resources\n");
2793 		goto err_disable_device;
2794 	}
2795 
2796 	/*
2797 	 * Set up our DMA mask: try for 64-bit address masking first and
2798 	 * fall back to 32-bit if we can't get 64 bits ...
2799 	 */
2800 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2801 	if (err == 0) {
2802 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2803 		if (err) {
2804 			dev_err(&pdev->dev, "unable to obtain 64-bit DMA for"
2805 				" coherent allocations\n");
2806 			goto err_release_regions;
2807 		}
2808 		pci_using_dac = 1;
2809 	} else {
2810 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2811 		if (err != 0) {
2812 			dev_err(&pdev->dev, "no usable DMA configuration\n");
2813 			goto err_release_regions;
2814 		}
2815 		pci_using_dac = 0;
2816 	}
2817 
2818 	/*
2819 	 * Enable bus mastering for the device ...
2820 	 */
2821 	pci_set_master(pdev);
2822 
2823 	/*
2824 	 * Allocate our adapter data structure and attach it to the device.
2825 	 */
2826 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
2827 	if (!adapter) {
2828 		err = -ENOMEM;
2829 		goto err_release_regions;
2830 	}
2831 	pci_set_drvdata(pdev, adapter);
2832 	adapter->pdev = pdev;
2833 	adapter->pdev_dev = &pdev->dev;
2834 
2835 	adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
2836 				    (sizeof(struct mbox_cmd) *
2837 				     T4VF_OS_LOG_MBOX_CMDS),
2838 				    GFP_KERNEL);
2839 	if (!adapter->mbox_log) {
2840 		err = -ENOMEM;
2841 		goto err_free_adapter;
2842 	}
2843 	adapter->mbox_log->size = T4VF_OS_LOG_MBOX_CMDS;
2844 
2845 	/*
2846 	 * Initialize SMP data synchronization resources.
2847 	 */
2848 	spin_lock_init(&adapter->stats_lock);
2849 	spin_lock_init(&adapter->mbox_lock);
2850 	INIT_LIST_HEAD(&adapter->mlist.list);
2851 
2852 	/*
2853 	 * Map our I/O registers in BAR0.
2854 	 */
2855 	adapter->regs = pci_ioremap_bar(pdev, 0);
2856 	if (!adapter->regs) {
2857 		dev_err(&pdev->dev, "cannot map device registers\n");
2858 		err = -ENOMEM;
2859 		goto err_free_adapter;
2860 	}
2861 
2862 	/* Wait for the device to become ready before proceeding ...
2863 	 */
2864 	err = t4vf_prep_adapter(adapter);
2865 	if (err) {
2866 		dev_err(adapter->pdev_dev, "device didn't become ready:"
2867 			" err=%d\n", err);
2868 		goto err_unmap_bar0;
2869 	}
2870 
2871 	/* For T5 and later we want to use the new BAR-based User Doorbells,
2872 	 * so we need to map BAR2 here ...
2873 	 */
2874 	if (!is_t4(adapter->params.chip)) {
2875 		adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
2876 					   pci_resource_len(pdev, 2));
2877 		if (!adapter->bar2) {
2878 			dev_err(adapter->pdev_dev, "cannot map BAR2 doorbells\n");
2879 			err = -ENOMEM;
2880 			goto err_unmap_bar0;
2881 		}
2882 	}
2883 	/*
2884 	 * Initialize adapter level features.
2885 	 */
2886 	adapter->name = pci_name(pdev);
2887 	adapter->msg_enable = DFLT_MSG_ENABLE;
2888 	err = adap_init0(adapter);
2889 	if (err)
2890 		goto err_unmap_bar;
2891 
2892 	/*
2893 	 * Allocate our "adapter ports" and stitch everything together.
2894 	 */
2895 	pmask = adapter->params.vfres.pmask;
2896 	pf = t4vf_get_pf_from_vf(adapter);
2897 	for_each_port(adapter, pidx) {
2898 		int port_id, viid;
2899 		u8 mac[ETH_ALEN];
2900 		unsigned int naddr = 1;
2901 
2902 		/*
2903 		 * We simplistically allocate our virtual interfaces
2904 		 * sequentially across the port numbers to which we have
2905 		 * access rights.  This should be configurable in some manner
2906 		 * ...
2907 		 */
2908 		if (pmask == 0)
2909 			break;
2910 		port_id = ffs(pmask) - 1;
2911 		pmask &= ~(1 << port_id);
2912 		viid = t4vf_alloc_vi(adapter, port_id);
2913 		if (viid < 0) {
2914 			dev_err(&pdev->dev, "cannot allocate VI for port %d:"
2915 				" err=%d\n", port_id, viid);
2916 			err = viid;
2917 			goto err_free_dev;
2918 		}
2919 
2920 		/*
2921 		 * Allocate our network device and stitch things together.
2922 		 */
2923 		netdev = alloc_etherdev_mq(sizeof(struct port_info),
2924 					   MAX_PORT_QSETS);
2925 		if (netdev == NULL) {
2926 			t4vf_free_vi(adapter, viid);
2927 			err = -ENOMEM;
2928 			goto err_free_dev;
2929 		}
2930 		adapter->port[pidx] = netdev;
2931 		SET_NETDEV_DEV(netdev, &pdev->dev);
2932 		pi = netdev_priv(netdev);
2933 		pi->adapter = adapter;
2934 		pi->pidx = pidx;
2935 		pi->port_id = port_id;
2936 		pi->viid = viid;
2937 
2938 		/*
2939 		 * Initialize the starting state of our "port" and register
2940 		 * it.
2941 		 */
2942 		pi->xact_addr_filt = -1;
2943 		netif_carrier_off(netdev);
2944 		netdev->irq = pdev->irq;
2945 
2946 		netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
2947 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2948 			NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_RXCSUM;
2949 		netdev->vlan_features = NETIF_F_SG | TSO_FLAGS |
2950 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2951 			NETIF_F_HIGHDMA;
2952 		netdev->features = netdev->hw_features |
2953 				   NETIF_F_HW_VLAN_CTAG_TX;
2954 		if (pci_using_dac)
2955 			netdev->features |= NETIF_F_HIGHDMA;
2956 
2957 		netdev->priv_flags |= IFF_UNICAST_FLT;
2958 		netdev->min_mtu = 81;
2959 		netdev->max_mtu = ETH_MAX_MTU;
2960 
2961 		netdev->netdev_ops = &cxgb4vf_netdev_ops;
2962 		netdev->ethtool_ops = &cxgb4vf_ethtool_ops;
2963 		netdev->dev_port = pi->port_id;
2964 
2965 		/*
2966 		 * Initialize the hardware/software state for the port.
2967 		 */
2968 		err = t4vf_port_init(adapter, pidx);
2969 		if (err) {
2970 			dev_err(&pdev->dev, "cannot initialize port %d\n",
2971 				pidx);
2972 			goto err_free_dev;
2973 		}
2974 
2975 		err = t4vf_get_vf_mac_acl(adapter, pf, &naddr, mac);
2976 		if (err) {
2977 			dev_err(&pdev->dev,
2978 				"unable to determine MAC ACL address, "
2979 				"continuing anyway.. (status %d)\n", err);
2980 		} else if (naddr && adapter->params.vfres.nvi == 1) {
2981 			struct sockaddr addr;
2982 
2983 			ether_addr_copy(addr.sa_data, mac);
2984 			err = cxgb4vf_set_mac_addr(netdev, &addr);
2985 			if (err) {
2986 				dev_err(&pdev->dev,
2987 					"unable to set MAC address %pM\n",
2988 					mac);
2989 				goto err_free_dev;
2990 			}
2991 			dev_info(&pdev->dev,
2992 				 "Using assigned MAC ACL: %pM\n", mac);
2993 		}
2994 	}
2995 
2996 	/* See what interrupts we'll be using.  If we've been configured to
2997 	 * use MSI-X interrupts, try to enable them but fall back to using
2998 	 * MSI interrupts if we can't enable MSI-X interrupts.  If we can't
2999 	 * get MSI interrupts we bail with the error.
3000 	 */
3001 	if (msi == MSI_MSIX && enable_msix(adapter) == 0)
3002 		adapter->flags |= USING_MSIX;
3003 	else {
3004 		if (msi == MSI_MSIX) {
3005 			dev_info(adapter->pdev_dev,
3006 				 "Unable to use MSI-X Interrupts; falling "
3007 				 "back to MSI Interrupts\n");
3008 
3009 			/* We're going to need a Forwarded Interrupt Queue so
3010 			 * that may cut into how many Queue Sets we can
3011 			 * support.
3012 			 */
3013 			msi = MSI_MSI;
3014 			size_nports_qsets(adapter);
3015 		}
3016 		err = pci_enable_msi(pdev);
3017 		if (err) {
3018 			dev_err(&pdev->dev, "Unable to allocate MSI Interrupts;"
3019 				" err=%d\n", err);
3020 			goto err_free_dev;
3021 		}
3022 		adapter->flags |= USING_MSI;
3023 	}
3024 
3025 	/* Now that we know how many "ports" we have and what interrupt
3026 	 * mechanism we're going to use, we can configure our queue resources.
3027 	 */
3028 	cfg_queues(adapter);
3029 
3030 	/*
3031 	 * The "card" is now ready to go.  If any errors occur during device
3032 	 * registration we do not fail the whole "card" but rather proceed
3033 	 * only with the ports we manage to register successfully.  However we
3034 	 * must register at least one net device.
3035 	 */
3036 	for_each_port(adapter, pidx) {
3037 		struct port_info *pi = netdev_priv(adapter->port[pidx]);
3038 		netdev = adapter->port[pidx];
3039 		if (netdev == NULL)
3040 			continue;
3041 
3042 		netif_set_real_num_tx_queues(netdev, pi->nqsets);
3043 		netif_set_real_num_rx_queues(netdev, pi->nqsets);
3044 
3045 		err = register_netdev(netdev);
3046 		if (err) {
3047 			dev_warn(&pdev->dev, "cannot register net device %s,"
3048 				 " skipping\n", netdev->name);
3049 			continue;
3050 		}
3051 
3052 		set_bit(pidx, &adapter->registered_device_map);
3053 	}
3054 	if (adapter->registered_device_map == 0) {
3055 		dev_err(&pdev->dev, "could not register any net devices\n");
3056 		goto err_disable_interrupts;
3057 	}
3058 
3059 	/*
3060 	 * Set up our debugfs entries.
3061 	 */
3062 	if (!IS_ERR_OR_NULL(cxgb4vf_debugfs_root)) {
3063 		adapter->debugfs_root =
3064 			debugfs_create_dir(pci_name(pdev),
3065 					   cxgb4vf_debugfs_root);
3066 		if (IS_ERR_OR_NULL(adapter->debugfs_root))
3067 			dev_warn(&pdev->dev, "could not create debugfs"
3068 				 " directory");
3069 		else
3070 			setup_debugfs(adapter);
3071 	}
3072 
3073 	/*
3074 	 * Print a short notice on the existence and configuration of the new
3075 	 * VF network device ...
3076 	 */
3077 	for_each_port(adapter, pidx) {
3078 		dev_info(adapter->pdev_dev, "%s: Chelsio VF NIC PCIe %s\n",
3079 			 adapter->port[pidx]->name,
3080 			 (adapter->flags & USING_MSIX) ? "MSI-X" :
3081 			 (adapter->flags & USING_MSI)  ? "MSI" : "");
3082 	}
3083 
3084 	/*
3085 	 * Return success!
3086 	 */
3087 	return 0;
3088 
3089 	/*
3090 	 * Error recovery and exit code.  Unwind state that's been created
3091 	 * so far and return the error.
3092 	 */
3093 err_disable_interrupts:
3094 	if (adapter->flags & USING_MSIX) {
3095 		pci_disable_msix(adapter->pdev);
3096 		adapter->flags &= ~USING_MSIX;
3097 	} else if (adapter->flags & USING_MSI) {
3098 		pci_disable_msi(adapter->pdev);
3099 		adapter->flags &= ~USING_MSI;
3100 	}
3101 
3102 err_free_dev:
3103 	for_each_port(adapter, pidx) {
3104 		netdev = adapter->port[pidx];
3105 		if (netdev == NULL)
3106 			continue;
3107 		pi = netdev_priv(netdev);
3108 		t4vf_free_vi(adapter, pi->viid);
3109 		if (test_bit(pidx, &adapter->registered_device_map))
3110 			unregister_netdev(netdev);
3111 		free_netdev(netdev);
3112 	}
3113 
3114 err_unmap_bar:
3115 	if (!is_t4(adapter->params.chip))
3116 		iounmap(adapter->bar2);
3117 
3118 err_unmap_bar0:
3119 	iounmap(adapter->regs);
3120 
3121 err_free_adapter:
3122 	kfree(adapter->mbox_log);
3123 	kfree(adapter);
3124 
3125 err_release_regions:
3126 	pci_release_regions(pdev);
3127 	pci_clear_master(pdev);
3128 
3129 err_disable_device:
3130 	pci_disable_device(pdev);
3131 
3132 	return err;
3133 }
3134 
3135 /*
3136  * "Remove" a device: tear down all kernel and driver state created in the
3137  * "probe" routine and quiesce the device (disable interrupts, etc.).  (Note
3138  * that this is called "remove_one" in the PF Driver.)
3139  */
3140 static void cxgb4vf_pci_remove(struct pci_dev *pdev)
3141 {
3142 	struct adapter *adapter = pci_get_drvdata(pdev);
3143 
3144 	/*
3145 	 * Tear down driver state associated with device.
3146 	 */
3147 	if (adapter) {
3148 		int pidx;
3149 
3150 		/*
3151 		 * Stop all of our activity.  Unregister network port,
3152 		 * disable interrupts, etc.
3153 		 */
3154 		for_each_port(adapter, pidx)
3155 			if (test_bit(pidx, &adapter->registered_device_map))
3156 				unregister_netdev(adapter->port[pidx]);
3157 		t4vf_sge_stop(adapter);
3158 		if (adapter->flags & USING_MSIX) {
3159 			pci_disable_msix(adapter->pdev);
3160 			adapter->flags &= ~USING_MSIX;
3161 		} else if (adapter->flags & USING_MSI) {
3162 			pci_disable_msi(adapter->pdev);
3163 			adapter->flags &= ~USING_MSI;
3164 		}
3165 
3166 		/*
3167 		 * Tear down our debugfs entries.
3168 		 */
3169 		if (!IS_ERR_OR_NULL(adapter->debugfs_root)) {
3170 			cleanup_debugfs(adapter);
3171 			debugfs_remove_recursive(adapter->debugfs_root);
3172 		}
3173 
3174 		/*
3175 		 * Free all of the various resources which we've acquired ...
3176 		 */
3177 		t4vf_free_sge_resources(adapter);
3178 		for_each_port(adapter, pidx) {
3179 			struct net_device *netdev = adapter->port[pidx];
3180 			struct port_info *pi;
3181 
3182 			if (netdev == NULL)
3183 				continue;
3184 
3185 			pi = netdev_priv(netdev);
3186 			t4vf_free_vi(adapter, pi->viid);
3187 			free_netdev(netdev);
3188 		}
3189 		iounmap(adapter->regs);
3190 		if (!is_t4(adapter->params.chip))
3191 			iounmap(adapter->bar2);
3192 		kfree(adapter->mbox_log);
3193 		kfree(adapter);
3194 	}
3195 
3196 	/*
3197 	 * Disable the device and release its PCI resources.
3198 	 */
3199 	pci_disable_device(pdev);
3200 	pci_clear_master(pdev);
3201 	pci_release_regions(pdev);
3202 }
3203 
3204 /*
3205  * "Shutdown" quiesce the device, stopping Ingress Packet and Interrupt
3206  * delivery.
3207  */
3208 static void cxgb4vf_pci_shutdown(struct pci_dev *pdev)
3209 {
3210 	struct adapter *adapter;
3211 	int pidx;
3212 
3213 	adapter = pci_get_drvdata(pdev);
3214 	if (!adapter)
3215 		return;
3216 
3217 	/* Disable all Virtual Interfaces.  This will shut down the
3218 	 * delivery of all ingress packets into the chip for these
3219 	 * Virtual Interfaces.
3220 	 */
3221 	for_each_port(adapter, pidx)
3222 		if (test_bit(pidx, &adapter->registered_device_map))
3223 			unregister_netdev(adapter->port[pidx]);
3224 
3225 	/* Free up all Queues which will prevent further DMA and
3226 	 * Interrupts allowing various internal pathways to drain.
3227 	 */
3228 	t4vf_sge_stop(adapter);
3229 	if (adapter->flags & USING_MSIX) {
3230 		pci_disable_msix(adapter->pdev);
3231 		adapter->flags &= ~USING_MSIX;
3232 	} else if (adapter->flags & USING_MSI) {
3233 		pci_disable_msi(adapter->pdev);
3234 		adapter->flags &= ~USING_MSI;
3235 	}
3236 
3237 	/*
3238 	 * Free up all Queues which will prevent further DMA and
3239 	 * Interrupts allowing various internal pathways to drain.
3240 	 */
3241 	t4vf_free_sge_resources(adapter);
3242 	pci_set_drvdata(pdev, NULL);
3243 }
3244 
3245 /* Macros needed to support the PCI Device ID Table ...
3246  */
3247 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
3248 	static const struct pci_device_id cxgb4vf_pci_tbl[] = {
3249 #define CH_PCI_DEVICE_ID_FUNCTION	0x8
3250 
3251 #define CH_PCI_ID_TABLE_ENTRY(devid) \
3252 		{ PCI_VDEVICE(CHELSIO, (devid)), 0 }
3253 
3254 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
3255 
3256 #include "../cxgb4/t4_pci_id_tbl.h"
3257 
3258 MODULE_DESCRIPTION(DRV_DESC);
3259 MODULE_AUTHOR("Chelsio Communications");
3260 MODULE_LICENSE("Dual BSD/GPL");
3261 MODULE_VERSION(DRV_VERSION);
3262 MODULE_DEVICE_TABLE(pci, cxgb4vf_pci_tbl);
3263 
3264 static struct pci_driver cxgb4vf_driver = {
3265 	.name		= KBUILD_MODNAME,
3266 	.id_table	= cxgb4vf_pci_tbl,
3267 	.probe		= cxgb4vf_pci_probe,
3268 	.remove		= cxgb4vf_pci_remove,
3269 	.shutdown	= cxgb4vf_pci_shutdown,
3270 };
3271 
3272 /*
3273  * Initialize global driver state.
3274  */
3275 static int __init cxgb4vf_module_init(void)
3276 {
3277 	int ret;
3278 
3279 	/*
3280 	 * Vet our module parameters.
3281 	 */
3282 	if (msi != MSI_MSIX && msi != MSI_MSI) {
3283 		pr_warn("bad module parameter msi=%d; must be %d (MSI-X or MSI) or %d (MSI)\n",
3284 			msi, MSI_MSIX, MSI_MSI);
3285 		return -EINVAL;
3286 	}
3287 
3288 	/* Debugfs support is optional, just warn if this fails */
3289 	cxgb4vf_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
3290 	if (IS_ERR_OR_NULL(cxgb4vf_debugfs_root))
3291 		pr_warn("could not create debugfs entry, continuing\n");
3292 
3293 	ret = pci_register_driver(&cxgb4vf_driver);
3294 	if (ret < 0 && !IS_ERR_OR_NULL(cxgb4vf_debugfs_root))
3295 		debugfs_remove(cxgb4vf_debugfs_root);
3296 	return ret;
3297 }
3298 
3299 /*
3300  * Tear down global driver state.
3301  */
3302 static void __exit cxgb4vf_module_exit(void)
3303 {
3304 	pci_unregister_driver(&cxgb4vf_driver);
3305 	debugfs_remove(cxgb4vf_debugfs_root);
3306 }
3307 
3308 module_init(cxgb4vf_module_init);
3309 module_exit(cxgb4vf_module_exit);
3310