xref: /openbmc/linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c (revision c51d39010a1bccc9c1294e2d7c00005aefeb2b5c)
1 /*
2  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 
37 #include <linux/bitmap.h>
38 #include <linux/crc32.h>
39 #include <linux/ctype.h>
40 #include <linux/debugfs.h>
41 #include <linux/err.h>
42 #include <linux/etherdevice.h>
43 #include <linux/firmware.h>
44 #include <linux/if.h>
45 #include <linux/if_vlan.h>
46 #include <linux/init.h>
47 #include <linux/log2.h>
48 #include <linux/mdio.h>
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/mutex.h>
52 #include <linux/netdevice.h>
53 #include <linux/pci.h>
54 #include <linux/aer.h>
55 #include <linux/rtnetlink.h>
56 #include <linux/sched.h>
57 #include <linux/seq_file.h>
58 #include <linux/sockios.h>
59 #include <linux/vmalloc.h>
60 #include <linux/workqueue.h>
61 #include <net/neighbour.h>
62 #include <net/netevent.h>
63 #include <net/addrconf.h>
64 #include <net/bonding.h>
65 #include <net/addrconf.h>
66 #include <asm/uaccess.h>
67 #include <linux/crash_dump.h>
68 
69 #include "cxgb4.h"
70 #include "cxgb4_filter.h"
71 #include "t4_regs.h"
72 #include "t4_values.h"
73 #include "t4_msg.h"
74 #include "t4fw_api.h"
75 #include "t4fw_version.h"
76 #include "cxgb4_dcb.h"
77 #include "cxgb4_debugfs.h"
78 #include "clip_tbl.h"
79 #include "l2t.h"
80 #include "sched.h"
81 #include "cxgb4_tc_u32.h"
82 
83 char cxgb4_driver_name[] = KBUILD_MODNAME;
84 
85 #ifdef DRV_VERSION
86 #undef DRV_VERSION
87 #endif
88 #define DRV_VERSION "2.0.0-ko"
89 const char cxgb4_driver_version[] = DRV_VERSION;
90 #define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
91 
92 #define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
93 			 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
94 			 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
95 
96 /* Macros needed to support the PCI Device ID Table ...
97  */
98 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
99 	static const struct pci_device_id cxgb4_pci_tbl[] = {
100 #define CH_PCI_DEVICE_ID_FUNCTION 0x4
101 
102 /* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
103  * called for both.
104  */
105 #define CH_PCI_DEVICE_ID_FUNCTION2 0x0
106 
107 #define CH_PCI_ID_TABLE_ENTRY(devid) \
108 		{PCI_VDEVICE(CHELSIO, (devid)), 4}
109 
110 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
111 		{ 0, } \
112 	}
113 
114 #include "t4_pci_id_tbl.h"
115 
116 #define FW4_FNAME "cxgb4/t4fw.bin"
117 #define FW5_FNAME "cxgb4/t5fw.bin"
118 #define FW6_FNAME "cxgb4/t6fw.bin"
119 #define FW4_CFNAME "cxgb4/t4-config.txt"
120 #define FW5_CFNAME "cxgb4/t5-config.txt"
121 #define FW6_CFNAME "cxgb4/t6-config.txt"
122 #define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
123 #define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
124 #define PHY_AQ1202_DEVICEID 0x4409
125 #define PHY_BCM84834_DEVICEID 0x4486
126 
127 MODULE_DESCRIPTION(DRV_DESC);
128 MODULE_AUTHOR("Chelsio Communications");
129 MODULE_LICENSE("Dual BSD/GPL");
130 MODULE_VERSION(DRV_VERSION);
131 MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
132 MODULE_FIRMWARE(FW4_FNAME);
133 MODULE_FIRMWARE(FW5_FNAME);
134 MODULE_FIRMWARE(FW6_FNAME);
135 
136 /*
137  * Normally we're willing to become the firmware's Master PF but will be happy
138  * if another PF has already become the Master and initialized the adapter.
139  * Setting "force_init" will cause this driver to forcibly establish itself as
140  * the Master PF and initialize the adapter.
141  */
142 static uint force_init;
143 
144 module_param(force_init, uint, 0644);
145 MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
146 		 "deprecated parameter");
147 
148 static int dflt_msg_enable = DFLT_MSG_ENABLE;
149 
150 module_param(dflt_msg_enable, int, 0644);
151 MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
152 		 "deprecated parameter");
153 
154 /*
155  * The driver uses the best interrupt scheme available on a platform in the
156  * order MSI-X, MSI, legacy INTx interrupts.  This parameter determines which
157  * of these schemes the driver may consider as follows:
158  *
159  * msi = 2: choose from among all three options
160  * msi = 1: only consider MSI and INTx interrupts
161  * msi = 0: force INTx interrupts
162  */
163 static int msi = 2;
164 
165 module_param(msi, int, 0644);
166 MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
167 
168 /*
169  * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
170  * offset by 2 bytes in order to have the IP headers line up on 4-byte
171  * boundaries.  This is a requirement for many architectures which will throw
172  * a machine check fault if an attempt is made to access one of the 4-byte IP
173  * header fields on a non-4-byte boundary.  And it's a major performance issue
174  * even on some architectures which allow it like some implementations of the
175  * x86 ISA.  However, some architectures don't mind this and for some very
176  * edge-case performance sensitive applications (like forwarding large volumes
177  * of small packets), setting this DMA offset to 0 will decrease the number of
178  * PCI-E Bus transfers enough to measurably affect performance.
179  */
180 static int rx_dma_offset = 2;
181 
182 #ifdef CONFIG_PCI_IOV
183 /* Configure the number of PCI-E Virtual Function which are to be instantiated
184  * on SR-IOV Capable Physical Functions.
185  */
186 static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
187 
188 module_param_array(num_vf, uint, NULL, 0644);
189 MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3, deprecated parameter - please use the pci sysfs interface.");
190 #endif
191 
192 /* TX Queue select used to determine what algorithm to use for selecting TX
193  * queue. Select between the kernel provided function (select_queue=0) or user
194  * cxgb_select_queue function (select_queue=1)
195  *
196  * Default: select_queue=0
197  */
198 static int select_queue;
199 module_param(select_queue, int, 0644);
200 MODULE_PARM_DESC(select_queue,
201 		 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
202 
203 static struct dentry *cxgb4_debugfs_root;
204 
205 LIST_HEAD(adapter_list);
206 DEFINE_MUTEX(uld_mutex);
207 
208 static void link_report(struct net_device *dev)
209 {
210 	if (!netif_carrier_ok(dev))
211 		netdev_info(dev, "link down\n");
212 	else {
213 		static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
214 
215 		const char *s;
216 		const struct port_info *p = netdev_priv(dev);
217 
218 		switch (p->link_cfg.speed) {
219 		case 10000:
220 			s = "10Gbps";
221 			break;
222 		case 1000:
223 			s = "1000Mbps";
224 			break;
225 		case 100:
226 			s = "100Mbps";
227 			break;
228 		case 40000:
229 			s = "40Gbps";
230 			break;
231 		default:
232 			pr_info("%s: unsupported speed: %d\n",
233 				dev->name, p->link_cfg.speed);
234 			return;
235 		}
236 
237 		netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
238 			    fc[p->link_cfg.fc]);
239 	}
240 }
241 
242 #ifdef CONFIG_CHELSIO_T4_DCB
243 /* Set up/tear down Data Center Bridging Priority mapping for a net device. */
244 static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
245 {
246 	struct port_info *pi = netdev_priv(dev);
247 	struct adapter *adap = pi->adapter;
248 	struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
249 	int i;
250 
251 	/* We use a simple mapping of Port TX Queue Index to DCB
252 	 * Priority when we're enabling DCB.
253 	 */
254 	for (i = 0; i < pi->nqsets; i++, txq++) {
255 		u32 name, value;
256 		int err;
257 
258 		name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
259 			FW_PARAMS_PARAM_X_V(
260 				FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
261 			FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
262 		value = enable ? i : 0xffffffff;
263 
264 		/* Since we can be called while atomic (from "interrupt
265 		 * level") we need to issue the Set Parameters Commannd
266 		 * without sleeping (timeout < 0).
267 		 */
268 		err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
269 					    &name, &value,
270 					    -FW_CMD_MAX_TIMEOUT);
271 
272 		if (err)
273 			dev_err(adap->pdev_dev,
274 				"Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
275 				enable ? "set" : "unset", pi->port_id, i, -err);
276 		else
277 			txq->dcb_prio = value;
278 	}
279 }
280 
281 static int cxgb4_dcb_enabled(const struct net_device *dev)
282 {
283 	struct port_info *pi = netdev_priv(dev);
284 
285 	if (!pi->dcb.enabled)
286 		return 0;
287 
288 	return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
289 		(pi->dcb.state == CXGB4_DCB_STATE_HOST));
290 }
291 #endif /* CONFIG_CHELSIO_T4_DCB */
292 
293 void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
294 {
295 	struct net_device *dev = adapter->port[port_id];
296 
297 	/* Skip changes from disabled ports. */
298 	if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
299 		if (link_stat)
300 			netif_carrier_on(dev);
301 		else {
302 #ifdef CONFIG_CHELSIO_T4_DCB
303 			if (cxgb4_dcb_enabled(dev)) {
304 				cxgb4_dcb_state_init(dev);
305 				dcb_tx_queue_prio_enable(dev, false);
306 			}
307 #endif /* CONFIG_CHELSIO_T4_DCB */
308 			netif_carrier_off(dev);
309 		}
310 
311 		link_report(dev);
312 	}
313 }
314 
315 void t4_os_portmod_changed(const struct adapter *adap, int port_id)
316 {
317 	static const char *mod_str[] = {
318 		NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
319 	};
320 
321 	const struct net_device *dev = adap->port[port_id];
322 	const struct port_info *pi = netdev_priv(dev);
323 
324 	if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
325 		netdev_info(dev, "port module unplugged\n");
326 	else if (pi->mod_type < ARRAY_SIZE(mod_str))
327 		netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
328 	else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
329 		netdev_info(dev, "%s: unsupported port module inserted\n",
330 			    dev->name);
331 	else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
332 		netdev_info(dev, "%s: unknown port module inserted\n",
333 			    dev->name);
334 	else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
335 		netdev_info(dev, "%s: transceiver module error\n", dev->name);
336 	else
337 		netdev_info(dev, "%s: unknown module type %d inserted\n",
338 			    dev->name, pi->mod_type);
339 }
340 
341 int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
342 module_param(dbfifo_int_thresh, int, 0644);
343 MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
344 
345 /*
346  * usecs to sleep while draining the dbfifo
347  */
348 static int dbfifo_drain_delay = 1000;
349 module_param(dbfifo_drain_delay, int, 0644);
350 MODULE_PARM_DESC(dbfifo_drain_delay,
351 		 "usecs to sleep while draining the dbfifo");
352 
353 static inline int cxgb4_set_addr_hash(struct port_info *pi)
354 {
355 	struct adapter *adap = pi->adapter;
356 	u64 vec = 0;
357 	bool ucast = false;
358 	struct hash_mac_addr *entry;
359 
360 	/* Calculate the hash vector for the updated list and program it */
361 	list_for_each_entry(entry, &adap->mac_hlist, list) {
362 		ucast |= is_unicast_ether_addr(entry->addr);
363 		vec |= (1ULL << hash_mac_addr(entry->addr));
364 	}
365 	return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
366 				vec, false);
367 }
368 
369 static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
370 {
371 	struct port_info *pi = netdev_priv(netdev);
372 	struct adapter *adap = pi->adapter;
373 	int ret;
374 	u64 mhash = 0;
375 	u64 uhash = 0;
376 	bool free = false;
377 	bool ucast = is_unicast_ether_addr(mac_addr);
378 	const u8 *maclist[1] = {mac_addr};
379 	struct hash_mac_addr *new_entry;
380 
381 	ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
382 				NULL, ucast ? &uhash : &mhash, false);
383 	if (ret < 0)
384 		goto out;
385 	/* if hash != 0, then add the addr to hash addr list
386 	 * so on the end we will calculate the hash for the
387 	 * list and program it
388 	 */
389 	if (uhash || mhash) {
390 		new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
391 		if (!new_entry)
392 			return -ENOMEM;
393 		ether_addr_copy(new_entry->addr, mac_addr);
394 		list_add_tail(&new_entry->list, &adap->mac_hlist);
395 		ret = cxgb4_set_addr_hash(pi);
396 	}
397 out:
398 	return ret < 0 ? ret : 0;
399 }
400 
401 static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
402 {
403 	struct port_info *pi = netdev_priv(netdev);
404 	struct adapter *adap = pi->adapter;
405 	int ret;
406 	const u8 *maclist[1] = {mac_addr};
407 	struct hash_mac_addr *entry, *tmp;
408 
409 	/* If the MAC address to be removed is in the hash addr
410 	 * list, delete it from the list and update hash vector
411 	 */
412 	list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
413 		if (ether_addr_equal(entry->addr, mac_addr)) {
414 			list_del(&entry->list);
415 			kfree(entry);
416 			return cxgb4_set_addr_hash(pi);
417 		}
418 	}
419 
420 	ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
421 	return ret < 0 ? -EINVAL : 0;
422 }
423 
424 /*
425  * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
426  * If @mtu is -1 it is left unchanged.
427  */
428 static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
429 {
430 	struct port_info *pi = netdev_priv(dev);
431 	struct adapter *adapter = pi->adapter;
432 
433 	__dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
434 	__dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
435 
436 	return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
437 			     (dev->flags & IFF_PROMISC) ? 1 : 0,
438 			     (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
439 			     sleep_ok);
440 }
441 
442 /**
443  *	link_start - enable a port
444  *	@dev: the port to enable
445  *
446  *	Performs the MAC and PHY actions needed to enable a port.
447  */
448 static int link_start(struct net_device *dev)
449 {
450 	int ret;
451 	struct port_info *pi = netdev_priv(dev);
452 	unsigned int mb = pi->adapter->pf;
453 
454 	/*
455 	 * We do not set address filters and promiscuity here, the stack does
456 	 * that step explicitly.
457 	 */
458 	ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
459 			    !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
460 	if (ret == 0) {
461 		ret = t4_change_mac(pi->adapter, mb, pi->viid,
462 				    pi->xact_addr_filt, dev->dev_addr, true,
463 				    true);
464 		if (ret >= 0) {
465 			pi->xact_addr_filt = ret;
466 			ret = 0;
467 		}
468 	}
469 	if (ret == 0)
470 		ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
471 				    &pi->link_cfg);
472 	if (ret == 0) {
473 		local_bh_disable();
474 		ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
475 					  true, CXGB4_DCB_ENABLED);
476 		local_bh_enable();
477 	}
478 
479 	return ret;
480 }
481 
482 #ifdef CONFIG_CHELSIO_T4_DCB
483 /* Handle a Data Center Bridging update message from the firmware. */
484 static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
485 {
486 	int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
487 	struct net_device *dev = adap->port[adap->chan_map[port]];
488 	int old_dcb_enabled = cxgb4_dcb_enabled(dev);
489 	int new_dcb_enabled;
490 
491 	cxgb4_dcb_handle_fw_update(adap, pcmd);
492 	new_dcb_enabled = cxgb4_dcb_enabled(dev);
493 
494 	/* If the DCB has become enabled or disabled on the port then we're
495 	 * going to need to set up/tear down DCB Priority parameters for the
496 	 * TX Queues associated with the port.
497 	 */
498 	if (new_dcb_enabled != old_dcb_enabled)
499 		dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
500 }
501 #endif /* CONFIG_CHELSIO_T4_DCB */
502 
503 /* Response queue handler for the FW event queue.
504  */
505 static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
506 			  const struct pkt_gl *gl)
507 {
508 	u8 opcode = ((const struct rss_header *)rsp)->opcode;
509 
510 	rsp++;                                          /* skip RSS header */
511 
512 	/* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
513 	 */
514 	if (unlikely(opcode == CPL_FW4_MSG &&
515 	   ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
516 		rsp++;
517 		opcode = ((const struct rss_header *)rsp)->opcode;
518 		rsp++;
519 		if (opcode != CPL_SGE_EGR_UPDATE) {
520 			dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
521 				, opcode);
522 			goto out;
523 		}
524 	}
525 
526 	if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
527 		const struct cpl_sge_egr_update *p = (void *)rsp;
528 		unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
529 		struct sge_txq *txq;
530 
531 		txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
532 		txq->restarts++;
533 		if (txq->q_type == CXGB4_TXQ_ETH) {
534 			struct sge_eth_txq *eq;
535 
536 			eq = container_of(txq, struct sge_eth_txq, q);
537 			netif_tx_wake_queue(eq->txq);
538 		} else {
539 			struct sge_uld_txq *oq;
540 
541 			oq = container_of(txq, struct sge_uld_txq, q);
542 			tasklet_schedule(&oq->qresume_tsk);
543 		}
544 	} else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
545 		const struct cpl_fw6_msg *p = (void *)rsp;
546 
547 #ifdef CONFIG_CHELSIO_T4_DCB
548 		const struct fw_port_cmd *pcmd = (const void *)p->data;
549 		unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
550 		unsigned int action =
551 			FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
552 
553 		if (cmd == FW_PORT_CMD &&
554 		    action == FW_PORT_ACTION_GET_PORT_INFO) {
555 			int port = FW_PORT_CMD_PORTID_G(
556 					be32_to_cpu(pcmd->op_to_portid));
557 			struct net_device *dev =
558 				q->adap->port[q->adap->chan_map[port]];
559 			int state_input = ((pcmd->u.info.dcbxdis_pkd &
560 					    FW_PORT_CMD_DCBXDIS_F)
561 					   ? CXGB4_DCB_INPUT_FW_DISABLED
562 					   : CXGB4_DCB_INPUT_FW_ENABLED);
563 
564 			cxgb4_dcb_state_fsm(dev, state_input);
565 		}
566 
567 		if (cmd == FW_PORT_CMD &&
568 		    action == FW_PORT_ACTION_L2_DCB_CFG)
569 			dcb_rpl(q->adap, pcmd);
570 		else
571 #endif
572 			if (p->type == 0)
573 				t4_handle_fw_rpl(q->adap, p->data);
574 	} else if (opcode == CPL_L2T_WRITE_RPL) {
575 		const struct cpl_l2t_write_rpl *p = (void *)rsp;
576 
577 		do_l2t_write_rpl(q->adap, p);
578 	} else if (opcode == CPL_SET_TCB_RPL) {
579 		const struct cpl_set_tcb_rpl *p = (void *)rsp;
580 
581 		filter_rpl(q->adap, p);
582 	} else
583 		dev_err(q->adap->pdev_dev,
584 			"unexpected CPL %#x on FW event queue\n", opcode);
585 out:
586 	return 0;
587 }
588 
589 static void disable_msi(struct adapter *adapter)
590 {
591 	if (adapter->flags & USING_MSIX) {
592 		pci_disable_msix(adapter->pdev);
593 		adapter->flags &= ~USING_MSIX;
594 	} else if (adapter->flags & USING_MSI) {
595 		pci_disable_msi(adapter->pdev);
596 		adapter->flags &= ~USING_MSI;
597 	}
598 }
599 
600 /*
601  * Interrupt handler for non-data events used with MSI-X.
602  */
603 static irqreturn_t t4_nondata_intr(int irq, void *cookie)
604 {
605 	struct adapter *adap = cookie;
606 	u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
607 
608 	if (v & PFSW_F) {
609 		adap->swintr = 1;
610 		t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
611 	}
612 	if (adap->flags & MASTER_PF)
613 		t4_slow_intr_handler(adap);
614 	return IRQ_HANDLED;
615 }
616 
617 /*
618  * Name the MSI-X interrupts.
619  */
620 static void name_msix_vecs(struct adapter *adap)
621 {
622 	int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
623 
624 	/* non-data interrupts */
625 	snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
626 
627 	/* FW events */
628 	snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
629 		 adap->port[0]->name);
630 
631 	/* Ethernet queues */
632 	for_each_port(adap, j) {
633 		struct net_device *d = adap->port[j];
634 		const struct port_info *pi = netdev_priv(d);
635 
636 		for (i = 0; i < pi->nqsets; i++, msi_idx++)
637 			snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
638 				 d->name, i);
639 	}
640 }
641 
642 static int request_msix_queue_irqs(struct adapter *adap)
643 {
644 	struct sge *s = &adap->sge;
645 	int err, ethqidx;
646 	int msi_index = 2;
647 
648 	err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
649 			  adap->msix_info[1].desc, &s->fw_evtq);
650 	if (err)
651 		return err;
652 
653 	for_each_ethrxq(s, ethqidx) {
654 		err = request_irq(adap->msix_info[msi_index].vec,
655 				  t4_sge_intr_msix, 0,
656 				  adap->msix_info[msi_index].desc,
657 				  &s->ethrxq[ethqidx].rspq);
658 		if (err)
659 			goto unwind;
660 		msi_index++;
661 	}
662 	return 0;
663 
664 unwind:
665 	while (--ethqidx >= 0)
666 		free_irq(adap->msix_info[--msi_index].vec,
667 			 &s->ethrxq[ethqidx].rspq);
668 	free_irq(adap->msix_info[1].vec, &s->fw_evtq);
669 	return err;
670 }
671 
672 static void free_msix_queue_irqs(struct adapter *adap)
673 {
674 	int i, msi_index = 2;
675 	struct sge *s = &adap->sge;
676 
677 	free_irq(adap->msix_info[1].vec, &s->fw_evtq);
678 	for_each_ethrxq(s, i)
679 		free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
680 }
681 
682 /**
683  *	cxgb4_write_rss - write the RSS table for a given port
684  *	@pi: the port
685  *	@queues: array of queue indices for RSS
686  *
687  *	Sets up the portion of the HW RSS table for the port's VI to distribute
688  *	packets to the Rx queues in @queues.
689  *	Should never be called before setting up sge eth rx queues
690  */
691 int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
692 {
693 	u16 *rss;
694 	int i, err;
695 	struct adapter *adapter = pi->adapter;
696 	const struct sge_eth_rxq *rxq;
697 
698 	rxq = &adapter->sge.ethrxq[pi->first_qset];
699 	rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
700 	if (!rss)
701 		return -ENOMEM;
702 
703 	/* map the queue indices to queue ids */
704 	for (i = 0; i < pi->rss_size; i++, queues++)
705 		rss[i] = rxq[*queues].rspq.abs_id;
706 
707 	err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
708 				  pi->rss_size, rss, pi->rss_size);
709 	/* If Tunnel All Lookup isn't specified in the global RSS
710 	 * Configuration, then we need to specify a default Ingress
711 	 * Queue for any ingress packets which aren't hashed.  We'll
712 	 * use our first ingress queue ...
713 	 */
714 	if (!err)
715 		err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
716 				       FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
717 				       FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
718 				       FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
719 				       FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
720 				       FW_RSS_VI_CONFIG_CMD_UDPEN_F,
721 				       rss[0]);
722 	kfree(rss);
723 	return err;
724 }
725 
726 /**
727  *	setup_rss - configure RSS
728  *	@adap: the adapter
729  *
730  *	Sets up RSS for each port.
731  */
732 static int setup_rss(struct adapter *adap)
733 {
734 	int i, j, err;
735 
736 	for_each_port(adap, i) {
737 		const struct port_info *pi = adap2pinfo(adap, i);
738 
739 		/* Fill default values with equal distribution */
740 		for (j = 0; j < pi->rss_size; j++)
741 			pi->rss[j] = j % pi->nqsets;
742 
743 		err = cxgb4_write_rss(pi, pi->rss);
744 		if (err)
745 			return err;
746 	}
747 	return 0;
748 }
749 
750 /*
751  * Return the channel of the ingress queue with the given qid.
752  */
753 static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
754 {
755 	qid -= p->ingr_start;
756 	return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
757 }
758 
759 /*
760  * Wait until all NAPI handlers are descheduled.
761  */
762 static void quiesce_rx(struct adapter *adap)
763 {
764 	int i;
765 
766 	for (i = 0; i < adap->sge.ingr_sz; i++) {
767 		struct sge_rspq *q = adap->sge.ingr_map[i];
768 
769 		if (q && q->handler) {
770 			napi_disable(&q->napi);
771 			local_bh_disable();
772 			while (!cxgb_poll_lock_napi(q))
773 				mdelay(1);
774 			local_bh_enable();
775 		}
776 
777 	}
778 }
779 
780 /* Disable interrupt and napi handler */
781 static void disable_interrupts(struct adapter *adap)
782 {
783 	if (adap->flags & FULL_INIT_DONE) {
784 		t4_intr_disable(adap);
785 		if (adap->flags & USING_MSIX) {
786 			free_msix_queue_irqs(adap);
787 			free_irq(adap->msix_info[0].vec, adap);
788 		} else {
789 			free_irq(adap->pdev->irq, adap);
790 		}
791 		quiesce_rx(adap);
792 	}
793 }
794 
795 /*
796  * Enable NAPI scheduling and interrupt generation for all Rx queues.
797  */
798 static void enable_rx(struct adapter *adap)
799 {
800 	int i;
801 
802 	for (i = 0; i < adap->sge.ingr_sz; i++) {
803 		struct sge_rspq *q = adap->sge.ingr_map[i];
804 
805 		if (!q)
806 			continue;
807 		if (q->handler) {
808 			cxgb_busy_poll_init_lock(q);
809 			napi_enable(&q->napi);
810 		}
811 		/* 0-increment GTS to start the timer and enable interrupts */
812 		t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
813 			     SEINTARM_V(q->intr_params) |
814 			     INGRESSQID_V(q->cntxt_id));
815 	}
816 }
817 
818 
819 static int setup_fw_sge_queues(struct adapter *adap)
820 {
821 	struct sge *s = &adap->sge;
822 	int err = 0;
823 
824 	bitmap_zero(s->starving_fl, s->egr_sz);
825 	bitmap_zero(s->txq_maperr, s->egr_sz);
826 
827 	if (adap->flags & USING_MSIX)
828 		adap->msi_idx = 1;         /* vector 0 is for non-queue interrupts */
829 	else {
830 		err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
831 				       NULL, NULL, NULL, -1);
832 		if (err)
833 			return err;
834 		adap->msi_idx = -((int)s->intrq.abs_id + 1);
835 	}
836 
837 	err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
838 			       adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
839 	if (err)
840 		t4_free_sge_resources(adap);
841 	return err;
842 }
843 
844 /**
845  *	setup_sge_queues - configure SGE Tx/Rx/response queues
846  *	@adap: the adapter
847  *
848  *	Determines how many sets of SGE queues to use and initializes them.
849  *	We support multiple queue sets per port if we have MSI-X, otherwise
850  *	just one queue set per port.
851  */
852 static int setup_sge_queues(struct adapter *adap)
853 {
854 	int err, i, j;
855 	struct sge *s = &adap->sge;
856 	struct sge_uld_rxq_info *rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
857 	unsigned int cmplqid = 0;
858 
859 	for_each_port(adap, i) {
860 		struct net_device *dev = adap->port[i];
861 		struct port_info *pi = netdev_priv(dev);
862 		struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
863 		struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
864 
865 		for (j = 0; j < pi->nqsets; j++, q++) {
866 			if (adap->msi_idx > 0)
867 				adap->msi_idx++;
868 			err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
869 					       adap->msi_idx, &q->fl,
870 					       t4_ethrx_handler,
871 					       NULL,
872 					       t4_get_mps_bg_map(adap,
873 								 pi->tx_chan));
874 			if (err)
875 				goto freeout;
876 			q->rspq.idx = j;
877 			memset(&q->stats, 0, sizeof(q->stats));
878 		}
879 		for (j = 0; j < pi->nqsets; j++, t++) {
880 			err = t4_sge_alloc_eth_txq(adap, t, dev,
881 					netdev_get_tx_queue(dev, j),
882 					s->fw_evtq.cntxt_id);
883 			if (err)
884 				goto freeout;
885 		}
886 	}
887 
888 	for_each_port(adap, i) {
889 		/* Note that cmplqid below is 0 if we don't
890 		 * have RDMA queues, and that's the right value.
891 		 */
892 		if (rxq_info)
893 			cmplqid	= rxq_info->uldrxq[i].rspq.cntxt_id;
894 
895 		err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
896 					    s->fw_evtq.cntxt_id, cmplqid);
897 		if (err)
898 			goto freeout;
899 	}
900 
901 	t4_write_reg(adap, is_t4(adap->params.chip) ?
902 				MPS_TRC_RSS_CONTROL_A :
903 				MPS_T5_TRC_RSS_CONTROL_A,
904 		     RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
905 		     QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
906 	return 0;
907 freeout:
908 	t4_free_sge_resources(adap);
909 	return err;
910 }
911 
912 /*
913  * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
914  * The allocated memory is cleared.
915  */
916 void *t4_alloc_mem(size_t size)
917 {
918 	void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
919 
920 	if (!p)
921 		p = vzalloc(size);
922 	return p;
923 }
924 
925 /*
926  * Free memory allocated through alloc_mem().
927  */
928 void t4_free_mem(void *addr)
929 {
930 	kvfree(addr);
931 }
932 
933 static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
934 			     void *accel_priv, select_queue_fallback_t fallback)
935 {
936 	int txq;
937 
938 #ifdef CONFIG_CHELSIO_T4_DCB
939 	/* If a Data Center Bridging has been successfully negotiated on this
940 	 * link then we'll use the skb's priority to map it to a TX Queue.
941 	 * The skb's priority is determined via the VLAN Tag Priority Code
942 	 * Point field.
943 	 */
944 	if (cxgb4_dcb_enabled(dev)) {
945 		u16 vlan_tci;
946 		int err;
947 
948 		err = vlan_get_tag(skb, &vlan_tci);
949 		if (unlikely(err)) {
950 			if (net_ratelimit())
951 				netdev_warn(dev,
952 					    "TX Packet without VLAN Tag on DCB Link\n");
953 			txq = 0;
954 		} else {
955 			txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
956 #ifdef CONFIG_CHELSIO_T4_FCOE
957 			if (skb->protocol == htons(ETH_P_FCOE))
958 				txq = skb->priority & 0x7;
959 #endif /* CONFIG_CHELSIO_T4_FCOE */
960 		}
961 		return txq;
962 	}
963 #endif /* CONFIG_CHELSIO_T4_DCB */
964 
965 	if (select_queue) {
966 		txq = (skb_rx_queue_recorded(skb)
967 			? skb_get_rx_queue(skb)
968 			: smp_processor_id());
969 
970 		while (unlikely(txq >= dev->real_num_tx_queues))
971 			txq -= dev->real_num_tx_queues;
972 
973 		return txq;
974 	}
975 
976 	return fallback(dev, skb) % dev->real_num_tx_queues;
977 }
978 
979 static int closest_timer(const struct sge *s, int time)
980 {
981 	int i, delta, match = 0, min_delta = INT_MAX;
982 
983 	for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
984 		delta = time - s->timer_val[i];
985 		if (delta < 0)
986 			delta = -delta;
987 		if (delta < min_delta) {
988 			min_delta = delta;
989 			match = i;
990 		}
991 	}
992 	return match;
993 }
994 
995 static int closest_thres(const struct sge *s, int thres)
996 {
997 	int i, delta, match = 0, min_delta = INT_MAX;
998 
999 	for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1000 		delta = thres - s->counter_val[i];
1001 		if (delta < 0)
1002 			delta = -delta;
1003 		if (delta < min_delta) {
1004 			min_delta = delta;
1005 			match = i;
1006 		}
1007 	}
1008 	return match;
1009 }
1010 
1011 /**
1012  *	cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
1013  *	@q: the Rx queue
1014  *	@us: the hold-off time in us, or 0 to disable timer
1015  *	@cnt: the hold-off packet count, or 0 to disable counter
1016  *
1017  *	Sets an Rx queue's interrupt hold-off time and packet count.  At least
1018  *	one of the two needs to be enabled for the queue to generate interrupts.
1019  */
1020 int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1021 			       unsigned int us, unsigned int cnt)
1022 {
1023 	struct adapter *adap = q->adap;
1024 
1025 	if ((us | cnt) == 0)
1026 		cnt = 1;
1027 
1028 	if (cnt) {
1029 		int err;
1030 		u32 v, new_idx;
1031 
1032 		new_idx = closest_thres(&adap->sge, cnt);
1033 		if (q->desc && q->pktcnt_idx != new_idx) {
1034 			/* the queue has already been created, update it */
1035 			v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1036 			    FW_PARAMS_PARAM_X_V(
1037 					FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1038 			    FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
1039 			err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1040 					    &v, &new_idx);
1041 			if (err)
1042 				return err;
1043 		}
1044 		q->pktcnt_idx = new_idx;
1045 	}
1046 
1047 	us = us == 0 ? 6 : closest_timer(&adap->sge, us);
1048 	q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
1049 	return 0;
1050 }
1051 
1052 static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
1053 {
1054 	const struct port_info *pi = netdev_priv(dev);
1055 	netdev_features_t changed = dev->features ^ features;
1056 	int err;
1057 
1058 	if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
1059 		return 0;
1060 
1061 	err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
1062 			    -1, -1, -1,
1063 			    !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
1064 	if (unlikely(err))
1065 		dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
1066 	return err;
1067 }
1068 
1069 static int setup_debugfs(struct adapter *adap)
1070 {
1071 	if (IS_ERR_OR_NULL(adap->debugfs_root))
1072 		return -1;
1073 
1074 #ifdef CONFIG_DEBUG_FS
1075 	t4_setup_debugfs(adap);
1076 #endif
1077 	return 0;
1078 }
1079 
1080 /*
1081  * upper-layer driver support
1082  */
1083 
1084 /*
1085  * Allocate an active-open TID and set it to the supplied value.
1086  */
1087 int cxgb4_alloc_atid(struct tid_info *t, void *data)
1088 {
1089 	int atid = -1;
1090 
1091 	spin_lock_bh(&t->atid_lock);
1092 	if (t->afree) {
1093 		union aopen_entry *p = t->afree;
1094 
1095 		atid = (p - t->atid_tab) + t->atid_base;
1096 		t->afree = p->next;
1097 		p->data = data;
1098 		t->atids_in_use++;
1099 	}
1100 	spin_unlock_bh(&t->atid_lock);
1101 	return atid;
1102 }
1103 EXPORT_SYMBOL(cxgb4_alloc_atid);
1104 
1105 /*
1106  * Release an active-open TID.
1107  */
1108 void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1109 {
1110 	union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
1111 
1112 	spin_lock_bh(&t->atid_lock);
1113 	p->next = t->afree;
1114 	t->afree = p;
1115 	t->atids_in_use--;
1116 	spin_unlock_bh(&t->atid_lock);
1117 }
1118 EXPORT_SYMBOL(cxgb4_free_atid);
1119 
1120 /*
1121  * Allocate a server TID and set it to the supplied value.
1122  */
1123 int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1124 {
1125 	int stid;
1126 
1127 	spin_lock_bh(&t->stid_lock);
1128 	if (family == PF_INET) {
1129 		stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1130 		if (stid < t->nstids)
1131 			__set_bit(stid, t->stid_bmap);
1132 		else
1133 			stid = -1;
1134 	} else {
1135 		stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
1136 		if (stid < 0)
1137 			stid = -1;
1138 	}
1139 	if (stid >= 0) {
1140 		t->stid_tab[stid].data = data;
1141 		stid += t->stid_base;
1142 		/* IPv6 requires max of 520 bits or 16 cells in TCAM
1143 		 * This is equivalent to 4 TIDs. With CLIP enabled it
1144 		 * needs 2 TIDs.
1145 		 */
1146 		if (family == PF_INET)
1147 			t->stids_in_use++;
1148 		else
1149 			t->stids_in_use += 2;
1150 	}
1151 	spin_unlock_bh(&t->stid_lock);
1152 	return stid;
1153 }
1154 EXPORT_SYMBOL(cxgb4_alloc_stid);
1155 
1156 /* Allocate a server filter TID and set it to the supplied value.
1157  */
1158 int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1159 {
1160 	int stid;
1161 
1162 	spin_lock_bh(&t->stid_lock);
1163 	if (family == PF_INET) {
1164 		stid = find_next_zero_bit(t->stid_bmap,
1165 				t->nstids + t->nsftids, t->nstids);
1166 		if (stid < (t->nstids + t->nsftids))
1167 			__set_bit(stid, t->stid_bmap);
1168 		else
1169 			stid = -1;
1170 	} else {
1171 		stid = -1;
1172 	}
1173 	if (stid >= 0) {
1174 		t->stid_tab[stid].data = data;
1175 		stid -= t->nstids;
1176 		stid += t->sftid_base;
1177 		t->sftids_in_use++;
1178 	}
1179 	spin_unlock_bh(&t->stid_lock);
1180 	return stid;
1181 }
1182 EXPORT_SYMBOL(cxgb4_alloc_sftid);
1183 
1184 /* Release a server TID.
1185  */
1186 void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1187 {
1188 	/* Is it a server filter TID? */
1189 	if (t->nsftids && (stid >= t->sftid_base)) {
1190 		stid -= t->sftid_base;
1191 		stid += t->nstids;
1192 	} else {
1193 		stid -= t->stid_base;
1194 	}
1195 
1196 	spin_lock_bh(&t->stid_lock);
1197 	if (family == PF_INET)
1198 		__clear_bit(stid, t->stid_bmap);
1199 	else
1200 		bitmap_release_region(t->stid_bmap, stid, 1);
1201 	t->stid_tab[stid].data = NULL;
1202 	if (stid < t->nstids) {
1203 		if (family == PF_INET)
1204 			t->stids_in_use--;
1205 		else
1206 			t->stids_in_use -= 2;
1207 	} else {
1208 		t->sftids_in_use--;
1209 	}
1210 	spin_unlock_bh(&t->stid_lock);
1211 }
1212 EXPORT_SYMBOL(cxgb4_free_stid);
1213 
1214 /*
1215  * Populate a TID_RELEASE WR.  Caller must properly size the skb.
1216  */
1217 static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1218 			   unsigned int tid)
1219 {
1220 	struct cpl_tid_release *req;
1221 
1222 	set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1223 	req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1224 	INIT_TP_WR(req, tid);
1225 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1226 }
1227 
1228 /*
1229  * Queue a TID release request and if necessary schedule a work queue to
1230  * process it.
1231  */
1232 static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1233 				    unsigned int tid)
1234 {
1235 	void **p = &t->tid_tab[tid];
1236 	struct adapter *adap = container_of(t, struct adapter, tids);
1237 
1238 	spin_lock_bh(&adap->tid_release_lock);
1239 	*p = adap->tid_release_head;
1240 	/* Low 2 bits encode the Tx channel number */
1241 	adap->tid_release_head = (void **)((uintptr_t)p | chan);
1242 	if (!adap->tid_release_task_busy) {
1243 		adap->tid_release_task_busy = true;
1244 		queue_work(adap->workq, &adap->tid_release_task);
1245 	}
1246 	spin_unlock_bh(&adap->tid_release_lock);
1247 }
1248 
1249 /*
1250  * Process the list of pending TID release requests.
1251  */
1252 static void process_tid_release_list(struct work_struct *work)
1253 {
1254 	struct sk_buff *skb;
1255 	struct adapter *adap;
1256 
1257 	adap = container_of(work, struct adapter, tid_release_task);
1258 
1259 	spin_lock_bh(&adap->tid_release_lock);
1260 	while (adap->tid_release_head) {
1261 		void **p = adap->tid_release_head;
1262 		unsigned int chan = (uintptr_t)p & 3;
1263 		p = (void *)p - chan;
1264 
1265 		adap->tid_release_head = *p;
1266 		*p = NULL;
1267 		spin_unlock_bh(&adap->tid_release_lock);
1268 
1269 		while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1270 					 GFP_KERNEL)))
1271 			schedule_timeout_uninterruptible(1);
1272 
1273 		mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1274 		t4_ofld_send(adap, skb);
1275 		spin_lock_bh(&adap->tid_release_lock);
1276 	}
1277 	adap->tid_release_task_busy = false;
1278 	spin_unlock_bh(&adap->tid_release_lock);
1279 }
1280 
1281 /*
1282  * Release a TID and inform HW.  If we are unable to allocate the release
1283  * message we defer to a work queue.
1284  */
1285 void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1286 {
1287 	struct sk_buff *skb;
1288 	struct adapter *adap = container_of(t, struct adapter, tids);
1289 
1290 	WARN_ON(tid >= t->ntids);
1291 
1292 	if (t->tid_tab[tid]) {
1293 		t->tid_tab[tid] = NULL;
1294 		if (t->hash_base && (tid >= t->hash_base))
1295 			atomic_dec(&t->hash_tids_in_use);
1296 		else
1297 			atomic_dec(&t->tids_in_use);
1298 	}
1299 
1300 	skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1301 	if (likely(skb)) {
1302 		mk_tid_release(skb, chan, tid);
1303 		t4_ofld_send(adap, skb);
1304 	} else
1305 		cxgb4_queue_tid_release(t, chan, tid);
1306 }
1307 EXPORT_SYMBOL(cxgb4_remove_tid);
1308 
1309 /*
1310  * Allocate and initialize the TID tables.  Returns 0 on success.
1311  */
1312 static int tid_init(struct tid_info *t)
1313 {
1314 	struct adapter *adap = container_of(t, struct adapter, tids);
1315 	unsigned int max_ftids = t->nftids + t->nsftids;
1316 	unsigned int natids = t->natids;
1317 	unsigned int stid_bmap_size;
1318 	unsigned int ftid_bmap_size;
1319 	size_t size;
1320 
1321 	stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
1322 	ftid_bmap_size = BITS_TO_LONGS(t->nftids);
1323 	size = t->ntids * sizeof(*t->tid_tab) +
1324 	       natids * sizeof(*t->atid_tab) +
1325 	       t->nstids * sizeof(*t->stid_tab) +
1326 	       t->nsftids * sizeof(*t->stid_tab) +
1327 	       stid_bmap_size * sizeof(long) +
1328 	       max_ftids * sizeof(*t->ftid_tab) +
1329 	       ftid_bmap_size * sizeof(long);
1330 
1331 	t->tid_tab = t4_alloc_mem(size);
1332 	if (!t->tid_tab)
1333 		return -ENOMEM;
1334 
1335 	t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1336 	t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
1337 	t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
1338 	t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
1339 	t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
1340 	spin_lock_init(&t->stid_lock);
1341 	spin_lock_init(&t->atid_lock);
1342 	spin_lock_init(&t->ftid_lock);
1343 
1344 	t->stids_in_use = 0;
1345 	t->sftids_in_use = 0;
1346 	t->afree = NULL;
1347 	t->atids_in_use = 0;
1348 	atomic_set(&t->tids_in_use, 0);
1349 	atomic_set(&t->hash_tids_in_use, 0);
1350 
1351 	/* Setup the free list for atid_tab and clear the stid bitmap. */
1352 	if (natids) {
1353 		while (--natids)
1354 			t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1355 		t->afree = t->atid_tab;
1356 	}
1357 
1358 	if (is_offload(adap)) {
1359 		bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1360 		/* Reserve stid 0 for T4/T5 adapters */
1361 		if (!t->stid_base &&
1362 		    CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1363 			__set_bit(0, t->stid_bmap);
1364 	}
1365 
1366 	bitmap_zero(t->ftid_bmap, t->nftids);
1367 	return 0;
1368 }
1369 
1370 /**
1371  *	cxgb4_create_server - create an IP server
1372  *	@dev: the device
1373  *	@stid: the server TID
1374  *	@sip: local IP address to bind server to
1375  *	@sport: the server's TCP port
1376  *	@queue: queue to direct messages from this server to
1377  *
1378  *	Create an IP server for the given port and address.
1379  *	Returns <0 on error and one of the %NET_XMIT_* values on success.
1380  */
1381 int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
1382 			__be32 sip, __be16 sport, __be16 vlan,
1383 			unsigned int queue)
1384 {
1385 	unsigned int chan;
1386 	struct sk_buff *skb;
1387 	struct adapter *adap;
1388 	struct cpl_pass_open_req *req;
1389 	int ret;
1390 
1391 	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1392 	if (!skb)
1393 		return -ENOMEM;
1394 
1395 	adap = netdev2adap(dev);
1396 	req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1397 	INIT_TP_WR(req, 0);
1398 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1399 	req->local_port = sport;
1400 	req->peer_port = htons(0);
1401 	req->local_ip = sip;
1402 	req->peer_ip = htonl(0);
1403 	chan = rxq_to_chan(&adap->sge, queue);
1404 	req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
1405 	req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1406 				SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
1407 	ret = t4_mgmt_tx(adap, skb);
1408 	return net_xmit_eval(ret);
1409 }
1410 EXPORT_SYMBOL(cxgb4_create_server);
1411 
1412 /*	cxgb4_create_server6 - create an IPv6 server
1413  *	@dev: the device
1414  *	@stid: the server TID
1415  *	@sip: local IPv6 address to bind server to
1416  *	@sport: the server's TCP port
1417  *	@queue: queue to direct messages from this server to
1418  *
1419  *	Create an IPv6 server for the given port and address.
1420  *	Returns <0 on error and one of the %NET_XMIT_* values on success.
1421  */
1422 int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1423 			 const struct in6_addr *sip, __be16 sport,
1424 			 unsigned int queue)
1425 {
1426 	unsigned int chan;
1427 	struct sk_buff *skb;
1428 	struct adapter *adap;
1429 	struct cpl_pass_open_req6 *req;
1430 	int ret;
1431 
1432 	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1433 	if (!skb)
1434 		return -ENOMEM;
1435 
1436 	adap = netdev2adap(dev);
1437 	req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1438 	INIT_TP_WR(req, 0);
1439 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1440 	req->local_port = sport;
1441 	req->peer_port = htons(0);
1442 	req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1443 	req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1444 	req->peer_ip_hi = cpu_to_be64(0);
1445 	req->peer_ip_lo = cpu_to_be64(0);
1446 	chan = rxq_to_chan(&adap->sge, queue);
1447 	req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
1448 	req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1449 				SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
1450 	ret = t4_mgmt_tx(adap, skb);
1451 	return net_xmit_eval(ret);
1452 }
1453 EXPORT_SYMBOL(cxgb4_create_server6);
1454 
1455 int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1456 			unsigned int queue, bool ipv6)
1457 {
1458 	struct sk_buff *skb;
1459 	struct adapter *adap;
1460 	struct cpl_close_listsvr_req *req;
1461 	int ret;
1462 
1463 	adap = netdev2adap(dev);
1464 
1465 	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1466 	if (!skb)
1467 		return -ENOMEM;
1468 
1469 	req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1470 	INIT_TP_WR(req, 0);
1471 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
1472 	req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1473 				LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
1474 	ret = t4_mgmt_tx(adap, skb);
1475 	return net_xmit_eval(ret);
1476 }
1477 EXPORT_SYMBOL(cxgb4_remove_server);
1478 
1479 /**
1480  *	cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1481  *	@mtus: the HW MTU table
1482  *	@mtu: the target MTU
1483  *	@idx: index of selected entry in the MTU table
1484  *
1485  *	Returns the index and the value in the HW MTU table that is closest to
1486  *	but does not exceed @mtu, unless @mtu is smaller than any value in the
1487  *	table, in which case that smallest available value is selected.
1488  */
1489 unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1490 			    unsigned int *idx)
1491 {
1492 	unsigned int i = 0;
1493 
1494 	while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1495 		++i;
1496 	if (idx)
1497 		*idx = i;
1498 	return mtus[i];
1499 }
1500 EXPORT_SYMBOL(cxgb4_best_mtu);
1501 
1502 /**
1503  *     cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1504  *     @mtus: the HW MTU table
1505  *     @header_size: Header Size
1506  *     @data_size_max: maximum Data Segment Size
1507  *     @data_size_align: desired Data Segment Size Alignment (2^N)
1508  *     @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1509  *
1510  *     Similar to cxgb4_best_mtu() but instead of searching the Hardware
1511  *     MTU Table based solely on a Maximum MTU parameter, we break that
1512  *     parameter up into a Header Size and Maximum Data Segment Size, and
1513  *     provide a desired Data Segment Size Alignment.  If we find an MTU in
1514  *     the Hardware MTU Table which will result in a Data Segment Size with
1515  *     the requested alignment _and_ that MTU isn't "too far" from the
1516  *     closest MTU, then we'll return that rather than the closest MTU.
1517  */
1518 unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1519 				    unsigned short header_size,
1520 				    unsigned short data_size_max,
1521 				    unsigned short data_size_align,
1522 				    unsigned int *mtu_idxp)
1523 {
1524 	unsigned short max_mtu = header_size + data_size_max;
1525 	unsigned short data_size_align_mask = data_size_align - 1;
1526 	int mtu_idx, aligned_mtu_idx;
1527 
1528 	/* Scan the MTU Table till we find an MTU which is larger than our
1529 	 * Maximum MTU or we reach the end of the table.  Along the way,
1530 	 * record the last MTU found, if any, which will result in a Data
1531 	 * Segment Length matching the requested alignment.
1532 	 */
1533 	for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1534 		unsigned short data_size = mtus[mtu_idx] - header_size;
1535 
1536 		/* If this MTU minus the Header Size would result in a
1537 		 * Data Segment Size of the desired alignment, remember it.
1538 		 */
1539 		if ((data_size & data_size_align_mask) == 0)
1540 			aligned_mtu_idx = mtu_idx;
1541 
1542 		/* If we're not at the end of the Hardware MTU Table and the
1543 		 * next element is larger than our Maximum MTU, drop out of
1544 		 * the loop.
1545 		 */
1546 		if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1547 			break;
1548 	}
1549 
1550 	/* If we fell out of the loop because we ran to the end of the table,
1551 	 * then we just have to use the last [largest] entry.
1552 	 */
1553 	if (mtu_idx == NMTUS)
1554 		mtu_idx--;
1555 
1556 	/* If we found an MTU which resulted in the requested Data Segment
1557 	 * Length alignment and that's "not far" from the largest MTU which is
1558 	 * less than or equal to the maximum MTU, then use that.
1559 	 */
1560 	if (aligned_mtu_idx >= 0 &&
1561 	    mtu_idx - aligned_mtu_idx <= 1)
1562 		mtu_idx = aligned_mtu_idx;
1563 
1564 	/* If the caller has passed in an MTU Index pointer, pass the
1565 	 * MTU Index back.  Return the MTU value.
1566 	 */
1567 	if (mtu_idxp)
1568 		*mtu_idxp = mtu_idx;
1569 	return mtus[mtu_idx];
1570 }
1571 EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1572 
1573 /**
1574  *	cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1575  *	@chip: chip type
1576  *	@viid: VI id of the given port
1577  *
1578  *	Return the SMT index for this VI.
1579  */
1580 unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1581 {
1582 	/* In T4/T5, SMT contains 256 SMAC entries organized in
1583 	 * 128 rows of 2 entries each.
1584 	 * In T6, SMT contains 256 SMAC entries in 256 rows.
1585 	 * TODO: The below code needs to be updated when we add support
1586 	 * for 256 VFs.
1587 	 */
1588 	if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1589 		return ((viid & 0x7f) << 1);
1590 	else
1591 		return (viid & 0x7f);
1592 }
1593 EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1594 
1595 /**
1596  *	cxgb4_port_chan - get the HW channel of a port
1597  *	@dev: the net device for the port
1598  *
1599  *	Return the HW Tx channel of the given port.
1600  */
1601 unsigned int cxgb4_port_chan(const struct net_device *dev)
1602 {
1603 	return netdev2pinfo(dev)->tx_chan;
1604 }
1605 EXPORT_SYMBOL(cxgb4_port_chan);
1606 
1607 unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1608 {
1609 	struct adapter *adap = netdev2adap(dev);
1610 	u32 v1, v2, lp_count, hp_count;
1611 
1612 	v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1613 	v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
1614 	if (is_t4(adap->params.chip)) {
1615 		lp_count = LP_COUNT_G(v1);
1616 		hp_count = HP_COUNT_G(v1);
1617 	} else {
1618 		lp_count = LP_COUNT_T5_G(v1);
1619 		hp_count = HP_COUNT_T5_G(v2);
1620 	}
1621 	return lpfifo ? lp_count : hp_count;
1622 }
1623 EXPORT_SYMBOL(cxgb4_dbfifo_count);
1624 
1625 /**
1626  *	cxgb4_port_viid - get the VI id of a port
1627  *	@dev: the net device for the port
1628  *
1629  *	Return the VI id of the given port.
1630  */
1631 unsigned int cxgb4_port_viid(const struct net_device *dev)
1632 {
1633 	return netdev2pinfo(dev)->viid;
1634 }
1635 EXPORT_SYMBOL(cxgb4_port_viid);
1636 
1637 /**
1638  *	cxgb4_port_idx - get the index of a port
1639  *	@dev: the net device for the port
1640  *
1641  *	Return the index of the given port.
1642  */
1643 unsigned int cxgb4_port_idx(const struct net_device *dev)
1644 {
1645 	return netdev2pinfo(dev)->port_id;
1646 }
1647 EXPORT_SYMBOL(cxgb4_port_idx);
1648 
1649 void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1650 			 struct tp_tcp_stats *v6)
1651 {
1652 	struct adapter *adap = pci_get_drvdata(pdev);
1653 
1654 	spin_lock(&adap->stats_lock);
1655 	t4_tp_get_tcp_stats(adap, v4, v6);
1656 	spin_unlock(&adap->stats_lock);
1657 }
1658 EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1659 
1660 void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1661 		      const unsigned int *pgsz_order)
1662 {
1663 	struct adapter *adap = netdev2adap(dev);
1664 
1665 	t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1666 	t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1667 		     HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1668 		     HPZ3_V(pgsz_order[3]));
1669 }
1670 EXPORT_SYMBOL(cxgb4_iscsi_init);
1671 
1672 int cxgb4_flush_eq_cache(struct net_device *dev)
1673 {
1674 	struct adapter *adap = netdev2adap(dev);
1675 
1676 	return t4_sge_ctxt_flush(adap, adap->mbox);
1677 }
1678 EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1679 
1680 static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1681 {
1682 	u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
1683 	__be64 indices;
1684 	int ret;
1685 
1686 	spin_lock(&adap->win0_lock);
1687 	ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1688 			   sizeof(indices), (__be32 *)&indices,
1689 			   T4_MEMORY_READ);
1690 	spin_unlock(&adap->win0_lock);
1691 	if (!ret) {
1692 		*cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1693 		*pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
1694 	}
1695 	return ret;
1696 }
1697 
1698 int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1699 			u16 size)
1700 {
1701 	struct adapter *adap = netdev2adap(dev);
1702 	u16 hw_pidx, hw_cidx;
1703 	int ret;
1704 
1705 	ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1706 	if (ret)
1707 		goto out;
1708 
1709 	if (pidx != hw_pidx) {
1710 		u16 delta;
1711 		u32 val;
1712 
1713 		if (pidx >= hw_pidx)
1714 			delta = pidx - hw_pidx;
1715 		else
1716 			delta = size - hw_pidx + pidx;
1717 
1718 		if (is_t4(adap->params.chip))
1719 			val = PIDX_V(delta);
1720 		else
1721 			val = PIDX_T5_V(delta);
1722 		wmb();
1723 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1724 			     QID_V(qid) | val);
1725 	}
1726 out:
1727 	return ret;
1728 }
1729 EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1730 
1731 int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1732 {
1733 	struct adapter *adap;
1734 	u32 offset, memtype, memaddr;
1735 	u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
1736 	u32 edc0_end, edc1_end, mc0_end, mc1_end;
1737 	int ret;
1738 
1739 	adap = netdev2adap(dev);
1740 
1741 	offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1742 
1743 	/* Figure out where the offset lands in the Memory Type/Address scheme.
1744 	 * This code assumes that the memory is laid out starting at offset 0
1745 	 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1746 	 * and EDC1.  Some cards will have neither MC0 nor MC1, most cards have
1747 	 * MC0, and some have both MC0 and MC1.
1748 	 */
1749 	size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1750 	edc0_size = EDRAM0_SIZE_G(size) << 20;
1751 	size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1752 	edc1_size = EDRAM1_SIZE_G(size) << 20;
1753 	size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1754 	mc0_size = EXT_MEM0_SIZE_G(size) << 20;
1755 
1756 	edc0_end = edc0_size;
1757 	edc1_end = edc0_end + edc1_size;
1758 	mc0_end = edc1_end + mc0_size;
1759 
1760 	if (offset < edc0_end) {
1761 		memtype = MEM_EDC0;
1762 		memaddr = offset;
1763 	} else if (offset < edc1_end) {
1764 		memtype = MEM_EDC1;
1765 		memaddr = offset - edc0_end;
1766 	} else {
1767 		if (offset < mc0_end) {
1768 			memtype = MEM_MC0;
1769 			memaddr = offset - edc1_end;
1770 		} else if (is_t5(adap->params.chip)) {
1771 			size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1772 			mc1_size = EXT_MEM1_SIZE_G(size) << 20;
1773 			mc1_end = mc0_end + mc1_size;
1774 			if (offset < mc1_end) {
1775 				memtype = MEM_MC1;
1776 				memaddr = offset - mc0_end;
1777 			} else {
1778 				/* offset beyond the end of any memory */
1779 				goto err;
1780 			}
1781 		} else {
1782 			/* T4/T6 only has a single memory channel */
1783 			goto err;
1784 		}
1785 	}
1786 
1787 	spin_lock(&adap->win0_lock);
1788 	ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1789 	spin_unlock(&adap->win0_lock);
1790 	return ret;
1791 
1792 err:
1793 	dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1794 		stag, offset);
1795 	return -EINVAL;
1796 }
1797 EXPORT_SYMBOL(cxgb4_read_tpte);
1798 
1799 u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1800 {
1801 	u32 hi, lo;
1802 	struct adapter *adap;
1803 
1804 	adap = netdev2adap(dev);
1805 	lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1806 	hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
1807 
1808 	return ((u64)hi << 32) | (u64)lo;
1809 }
1810 EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1811 
1812 int cxgb4_bar2_sge_qregs(struct net_device *dev,
1813 			 unsigned int qid,
1814 			 enum cxgb4_bar2_qtype qtype,
1815 			 int user,
1816 			 u64 *pbar2_qoffset,
1817 			 unsigned int *pbar2_qid)
1818 {
1819 	return t4_bar2_sge_qregs(netdev2adap(dev),
1820 				 qid,
1821 				 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1822 				  ? T4_BAR2_QTYPE_EGRESS
1823 				  : T4_BAR2_QTYPE_INGRESS),
1824 				 user,
1825 				 pbar2_qoffset,
1826 				 pbar2_qid);
1827 }
1828 EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1829 
1830 static struct pci_driver cxgb4_driver;
1831 
1832 static void check_neigh_update(struct neighbour *neigh)
1833 {
1834 	const struct device *parent;
1835 	const struct net_device *netdev = neigh->dev;
1836 
1837 	if (netdev->priv_flags & IFF_802_1Q_VLAN)
1838 		netdev = vlan_dev_real_dev(netdev);
1839 	parent = netdev->dev.parent;
1840 	if (parent && parent->driver == &cxgb4_driver.driver)
1841 		t4_l2t_update(dev_get_drvdata(parent), neigh);
1842 }
1843 
1844 static int netevent_cb(struct notifier_block *nb, unsigned long event,
1845 		       void *data)
1846 {
1847 	switch (event) {
1848 	case NETEVENT_NEIGH_UPDATE:
1849 		check_neigh_update(data);
1850 		break;
1851 	case NETEVENT_REDIRECT:
1852 	default:
1853 		break;
1854 	}
1855 	return 0;
1856 }
1857 
1858 static bool netevent_registered;
1859 static struct notifier_block cxgb4_netevent_nb = {
1860 	.notifier_call = netevent_cb
1861 };
1862 
1863 static void drain_db_fifo(struct adapter *adap, int usecs)
1864 {
1865 	u32 v1, v2, lp_count, hp_count;
1866 
1867 	do {
1868 		v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1869 		v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
1870 		if (is_t4(adap->params.chip)) {
1871 			lp_count = LP_COUNT_G(v1);
1872 			hp_count = HP_COUNT_G(v1);
1873 		} else {
1874 			lp_count = LP_COUNT_T5_G(v1);
1875 			hp_count = HP_COUNT_T5_G(v2);
1876 		}
1877 
1878 		if (lp_count == 0 && hp_count == 0)
1879 			break;
1880 		set_current_state(TASK_UNINTERRUPTIBLE);
1881 		schedule_timeout(usecs_to_jiffies(usecs));
1882 	} while (1);
1883 }
1884 
1885 static void disable_txq_db(struct sge_txq *q)
1886 {
1887 	unsigned long flags;
1888 
1889 	spin_lock_irqsave(&q->db_lock, flags);
1890 	q->db_disabled = 1;
1891 	spin_unlock_irqrestore(&q->db_lock, flags);
1892 }
1893 
1894 static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
1895 {
1896 	spin_lock_irq(&q->db_lock);
1897 	if (q->db_pidx_inc) {
1898 		/* Make sure that all writes to the TX descriptors
1899 		 * are committed before we tell HW about them.
1900 		 */
1901 		wmb();
1902 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1903 			     QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
1904 		q->db_pidx_inc = 0;
1905 	}
1906 	q->db_disabled = 0;
1907 	spin_unlock_irq(&q->db_lock);
1908 }
1909 
1910 static void disable_dbs(struct adapter *adap)
1911 {
1912 	int i;
1913 
1914 	for_each_ethrxq(&adap->sge, i)
1915 		disable_txq_db(&adap->sge.ethtxq[i].q);
1916 	if (is_offload(adap)) {
1917 		struct sge_uld_txq_info *txq_info =
1918 			adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1919 
1920 		if (txq_info) {
1921 			for_each_ofldtxq(&adap->sge, i) {
1922 				struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1923 
1924 				disable_txq_db(&txq->q);
1925 			}
1926 		}
1927 	}
1928 	for_each_port(adap, i)
1929 		disable_txq_db(&adap->sge.ctrlq[i].q);
1930 }
1931 
1932 static void enable_dbs(struct adapter *adap)
1933 {
1934 	int i;
1935 
1936 	for_each_ethrxq(&adap->sge, i)
1937 		enable_txq_db(adap, &adap->sge.ethtxq[i].q);
1938 	if (is_offload(adap)) {
1939 		struct sge_uld_txq_info *txq_info =
1940 			adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1941 
1942 		if (txq_info) {
1943 			for_each_ofldtxq(&adap->sge, i) {
1944 				struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1945 
1946 				enable_txq_db(adap, &txq->q);
1947 			}
1948 		}
1949 	}
1950 	for_each_port(adap, i)
1951 		enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1952 }
1953 
1954 static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1955 {
1956 	enum cxgb4_uld type = CXGB4_ULD_RDMA;
1957 
1958 	if (adap->uld && adap->uld[type].handle)
1959 		adap->uld[type].control(adap->uld[type].handle, cmd);
1960 }
1961 
1962 static void process_db_full(struct work_struct *work)
1963 {
1964 	struct adapter *adap;
1965 
1966 	adap = container_of(work, struct adapter, db_full_task);
1967 
1968 	drain_db_fifo(adap, dbfifo_drain_delay);
1969 	enable_dbs(adap);
1970 	notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
1971 	if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1972 		t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1973 				 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1974 				 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1975 	else
1976 		t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1977 				 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
1978 }
1979 
1980 static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1981 {
1982 	u16 hw_pidx, hw_cidx;
1983 	int ret;
1984 
1985 	spin_lock_irq(&q->db_lock);
1986 	ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1987 	if (ret)
1988 		goto out;
1989 	if (q->db_pidx != hw_pidx) {
1990 		u16 delta;
1991 		u32 val;
1992 
1993 		if (q->db_pidx >= hw_pidx)
1994 			delta = q->db_pidx - hw_pidx;
1995 		else
1996 			delta = q->size - hw_pidx + q->db_pidx;
1997 
1998 		if (is_t4(adap->params.chip))
1999 			val = PIDX_V(delta);
2000 		else
2001 			val = PIDX_T5_V(delta);
2002 		wmb();
2003 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2004 			     QID_V(q->cntxt_id) | val);
2005 	}
2006 out:
2007 	q->db_disabled = 0;
2008 	q->db_pidx_inc = 0;
2009 	spin_unlock_irq(&q->db_lock);
2010 	if (ret)
2011 		CH_WARN(adap, "DB drop recovery failed.\n");
2012 }
2013 
2014 static void recover_all_queues(struct adapter *adap)
2015 {
2016 	int i;
2017 
2018 	for_each_ethrxq(&adap->sge, i)
2019 		sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
2020 	if (is_offload(adap)) {
2021 		struct sge_uld_txq_info *txq_info =
2022 			adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2023 		if (txq_info) {
2024 			for_each_ofldtxq(&adap->sge, i) {
2025 				struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2026 
2027 				sync_txq_pidx(adap, &txq->q);
2028 			}
2029 		}
2030 	}
2031 	for_each_port(adap, i)
2032 		sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2033 }
2034 
2035 static void process_db_drop(struct work_struct *work)
2036 {
2037 	struct adapter *adap;
2038 
2039 	adap = container_of(work, struct adapter, db_drop_task);
2040 
2041 	if (is_t4(adap->params.chip)) {
2042 		drain_db_fifo(adap, dbfifo_drain_delay);
2043 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
2044 		drain_db_fifo(adap, dbfifo_drain_delay);
2045 		recover_all_queues(adap);
2046 		drain_db_fifo(adap, dbfifo_drain_delay);
2047 		enable_dbs(adap);
2048 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
2049 	} else if (is_t5(adap->params.chip)) {
2050 		u32 dropped_db = t4_read_reg(adap, 0x010ac);
2051 		u16 qid = (dropped_db >> 15) & 0x1ffff;
2052 		u16 pidx_inc = dropped_db & 0x1fff;
2053 		u64 bar2_qoffset;
2054 		unsigned int bar2_qid;
2055 		int ret;
2056 
2057 		ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
2058 					0, &bar2_qoffset, &bar2_qid);
2059 		if (ret)
2060 			dev_err(adap->pdev_dev, "doorbell drop recovery: "
2061 				"qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2062 		else
2063 			writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
2064 			       adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
2065 
2066 		/* Re-enable BAR2 WC */
2067 		t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2068 	}
2069 
2070 	if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2071 		t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
2072 }
2073 
2074 void t4_db_full(struct adapter *adap)
2075 {
2076 	if (is_t4(adap->params.chip)) {
2077 		disable_dbs(adap);
2078 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2079 		t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2080 				 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
2081 		queue_work(adap->workq, &adap->db_full_task);
2082 	}
2083 }
2084 
2085 void t4_db_dropped(struct adapter *adap)
2086 {
2087 	if (is_t4(adap->params.chip)) {
2088 		disable_dbs(adap);
2089 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2090 	}
2091 	queue_work(adap->workq, &adap->db_drop_task);
2092 }
2093 
2094 void t4_register_netevent_notifier(void)
2095 {
2096 	if (!netevent_registered) {
2097 		register_netevent_notifier(&cxgb4_netevent_nb);
2098 		netevent_registered = true;
2099 	}
2100 }
2101 
2102 static void detach_ulds(struct adapter *adap)
2103 {
2104 	unsigned int i;
2105 
2106 	mutex_lock(&uld_mutex);
2107 	list_del(&adap->list_node);
2108 	for (i = 0; i < CXGB4_ULD_MAX; i++)
2109 		if (adap->uld && adap->uld[i].handle) {
2110 			adap->uld[i].state_change(adap->uld[i].handle,
2111 					     CXGB4_STATE_DETACH);
2112 			adap->uld[i].handle = NULL;
2113 		}
2114 	if (netevent_registered && list_empty(&adapter_list)) {
2115 		unregister_netevent_notifier(&cxgb4_netevent_nb);
2116 		netevent_registered = false;
2117 	}
2118 	mutex_unlock(&uld_mutex);
2119 }
2120 
2121 static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2122 {
2123 	unsigned int i;
2124 
2125 	mutex_lock(&uld_mutex);
2126 	for (i = 0; i < CXGB4_ULD_MAX; i++)
2127 		if (adap->uld && adap->uld[i].handle)
2128 			adap->uld[i].state_change(adap->uld[i].handle,
2129 						  new_state);
2130 	mutex_unlock(&uld_mutex);
2131 }
2132 
2133 #if IS_ENABLED(CONFIG_IPV6)
2134 static int cxgb4_inet6addr_handler(struct notifier_block *this,
2135 				   unsigned long event, void *data)
2136 {
2137 	struct inet6_ifaddr *ifa = data;
2138 	struct net_device *event_dev = ifa->idev->dev;
2139 	const struct device *parent = NULL;
2140 #if IS_ENABLED(CONFIG_BONDING)
2141 	struct adapter *adap;
2142 #endif
2143 	if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2144 		event_dev = vlan_dev_real_dev(event_dev);
2145 #if IS_ENABLED(CONFIG_BONDING)
2146 	if (event_dev->flags & IFF_MASTER) {
2147 		list_for_each_entry(adap, &adapter_list, list_node) {
2148 			switch (event) {
2149 			case NETDEV_UP:
2150 				cxgb4_clip_get(adap->port[0],
2151 					       (const u32 *)ifa, 1);
2152 				break;
2153 			case NETDEV_DOWN:
2154 				cxgb4_clip_release(adap->port[0],
2155 						   (const u32 *)ifa, 1);
2156 				break;
2157 			default:
2158 				break;
2159 			}
2160 		}
2161 		return NOTIFY_OK;
2162 	}
2163 #endif
2164 
2165 	if (event_dev)
2166 		parent = event_dev->dev.parent;
2167 
2168 	if (parent && parent->driver == &cxgb4_driver.driver) {
2169 		switch (event) {
2170 		case NETDEV_UP:
2171 			cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
2172 			break;
2173 		case NETDEV_DOWN:
2174 			cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
2175 			break;
2176 		default:
2177 			break;
2178 		}
2179 	}
2180 	return NOTIFY_OK;
2181 }
2182 
2183 static bool inet6addr_registered;
2184 static struct notifier_block cxgb4_inet6addr_notifier = {
2185 	.notifier_call = cxgb4_inet6addr_handler
2186 };
2187 
2188 static void update_clip(const struct adapter *adap)
2189 {
2190 	int i;
2191 	struct net_device *dev;
2192 	int ret;
2193 
2194 	rcu_read_lock();
2195 
2196 	for (i = 0; i < MAX_NPORTS; i++) {
2197 		dev = adap->port[i];
2198 		ret = 0;
2199 
2200 		if (dev)
2201 			ret = cxgb4_update_root_dev_clip(dev);
2202 
2203 		if (ret < 0)
2204 			break;
2205 	}
2206 	rcu_read_unlock();
2207 }
2208 #endif /* IS_ENABLED(CONFIG_IPV6) */
2209 
2210 /**
2211  *	cxgb_up - enable the adapter
2212  *	@adap: adapter being enabled
2213  *
2214  *	Called when the first port is enabled, this function performs the
2215  *	actions necessary to make an adapter operational, such as completing
2216  *	the initialization of HW modules, and enabling interrupts.
2217  *
2218  *	Must be called with the rtnl lock held.
2219  */
2220 static int cxgb_up(struct adapter *adap)
2221 {
2222 	int err;
2223 
2224 	err = setup_sge_queues(adap);
2225 	if (err)
2226 		goto out;
2227 	err = setup_rss(adap);
2228 	if (err)
2229 		goto freeq;
2230 
2231 	if (adap->flags & USING_MSIX) {
2232 		name_msix_vecs(adap);
2233 		err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2234 				  adap->msix_info[0].desc, adap);
2235 		if (err)
2236 			goto irq_err;
2237 		err = request_msix_queue_irqs(adap);
2238 		if (err) {
2239 			free_irq(adap->msix_info[0].vec, adap);
2240 			goto irq_err;
2241 		}
2242 	} else {
2243 		err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2244 				  (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
2245 				  adap->port[0]->name, adap);
2246 		if (err)
2247 			goto irq_err;
2248 	}
2249 	enable_rx(adap);
2250 	t4_sge_start(adap);
2251 	t4_intr_enable(adap);
2252 	adap->flags |= FULL_INIT_DONE;
2253 	notify_ulds(adap, CXGB4_STATE_UP);
2254 #if IS_ENABLED(CONFIG_IPV6)
2255 	update_clip(adap);
2256 #endif
2257 	/* Initialize hash mac addr list*/
2258 	INIT_LIST_HEAD(&adap->mac_hlist);
2259  out:
2260 	return err;
2261  irq_err:
2262 	dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
2263  freeq:
2264 	t4_free_sge_resources(adap);
2265 	goto out;
2266 }
2267 
2268 static void cxgb_down(struct adapter *adapter)
2269 {
2270 	cancel_work_sync(&adapter->tid_release_task);
2271 	cancel_work_sync(&adapter->db_full_task);
2272 	cancel_work_sync(&adapter->db_drop_task);
2273 	adapter->tid_release_task_busy = false;
2274 	adapter->tid_release_head = NULL;
2275 
2276 	t4_sge_stop(adapter);
2277 	t4_free_sge_resources(adapter);
2278 	adapter->flags &= ~FULL_INIT_DONE;
2279 }
2280 
2281 /*
2282  * net_device operations
2283  */
2284 static int cxgb_open(struct net_device *dev)
2285 {
2286 	int err;
2287 	struct port_info *pi = netdev_priv(dev);
2288 	struct adapter *adapter = pi->adapter;
2289 
2290 	netif_carrier_off(dev);
2291 
2292 	if (!(adapter->flags & FULL_INIT_DONE)) {
2293 		err = cxgb_up(adapter);
2294 		if (err < 0)
2295 			return err;
2296 	}
2297 
2298 	err = link_start(dev);
2299 	if (!err)
2300 		netif_tx_start_all_queues(dev);
2301 	return err;
2302 }
2303 
2304 static int cxgb_close(struct net_device *dev)
2305 {
2306 	struct port_info *pi = netdev_priv(dev);
2307 	struct adapter *adapter = pi->adapter;
2308 
2309 	netif_tx_stop_all_queues(dev);
2310 	netif_carrier_off(dev);
2311 	return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2312 }
2313 
2314 int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
2315 		__be32 sip, __be16 sport, __be16 vlan,
2316 		unsigned int queue, unsigned char port, unsigned char mask)
2317 {
2318 	int ret;
2319 	struct filter_entry *f;
2320 	struct adapter *adap;
2321 	int i;
2322 	u8 *val;
2323 
2324 	adap = netdev2adap(dev);
2325 
2326 	/* Adjust stid to correct filter index */
2327 	stid -= adap->tids.sftid_base;
2328 	stid += adap->tids.nftids;
2329 
2330 	/* Check to make sure the filter requested is writable ...
2331 	 */
2332 	f = &adap->tids.ftid_tab[stid];
2333 	ret = writable_filter(f);
2334 	if (ret)
2335 		return ret;
2336 
2337 	/* Clear out any old resources being used by the filter before
2338 	 * we start constructing the new filter.
2339 	 */
2340 	if (f->valid)
2341 		clear_filter(adap, f);
2342 
2343 	/* Clear out filter specifications */
2344 	memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2345 	f->fs.val.lport = cpu_to_be16(sport);
2346 	f->fs.mask.lport  = ~0;
2347 	val = (u8 *)&sip;
2348 	if ((val[0] | val[1] | val[2] | val[3]) != 0) {
2349 		for (i = 0; i < 4; i++) {
2350 			f->fs.val.lip[i] = val[i];
2351 			f->fs.mask.lip[i] = ~0;
2352 		}
2353 		if (adap->params.tp.vlan_pri_map & PORT_F) {
2354 			f->fs.val.iport = port;
2355 			f->fs.mask.iport = mask;
2356 		}
2357 	}
2358 
2359 	if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
2360 		f->fs.val.proto = IPPROTO_TCP;
2361 		f->fs.mask.proto = ~0;
2362 	}
2363 
2364 	f->fs.dirsteer = 1;
2365 	f->fs.iq = queue;
2366 	/* Mark filter as locked */
2367 	f->locked = 1;
2368 	f->fs.rpttid = 1;
2369 
2370 	ret = set_filter_wr(adap, stid);
2371 	if (ret) {
2372 		clear_filter(adap, f);
2373 		return ret;
2374 	}
2375 
2376 	return 0;
2377 }
2378 EXPORT_SYMBOL(cxgb4_create_server_filter);
2379 
2380 int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2381 		unsigned int queue, bool ipv6)
2382 {
2383 	struct filter_entry *f;
2384 	struct adapter *adap;
2385 
2386 	adap = netdev2adap(dev);
2387 
2388 	/* Adjust stid to correct filter index */
2389 	stid -= adap->tids.sftid_base;
2390 	stid += adap->tids.nftids;
2391 
2392 	f = &adap->tids.ftid_tab[stid];
2393 	/* Unlock the filter */
2394 	f->locked = 0;
2395 
2396 	return delete_filter(adap, stid);
2397 }
2398 EXPORT_SYMBOL(cxgb4_remove_server_filter);
2399 
2400 static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2401 						struct rtnl_link_stats64 *ns)
2402 {
2403 	struct port_stats stats;
2404 	struct port_info *p = netdev_priv(dev);
2405 	struct adapter *adapter = p->adapter;
2406 
2407 	/* Block retrieving statistics during EEH error
2408 	 * recovery. Otherwise, the recovery might fail
2409 	 * and the PCI device will be removed permanently
2410 	 */
2411 	spin_lock(&adapter->stats_lock);
2412 	if (!netif_device_present(dev)) {
2413 		spin_unlock(&adapter->stats_lock);
2414 		return ns;
2415 	}
2416 	t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2417 				 &p->stats_base);
2418 	spin_unlock(&adapter->stats_lock);
2419 
2420 	ns->tx_bytes   = stats.tx_octets;
2421 	ns->tx_packets = stats.tx_frames;
2422 	ns->rx_bytes   = stats.rx_octets;
2423 	ns->rx_packets = stats.rx_frames;
2424 	ns->multicast  = stats.rx_mcast_frames;
2425 
2426 	/* detailed rx_errors */
2427 	ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2428 			       stats.rx_runt;
2429 	ns->rx_over_errors   = 0;
2430 	ns->rx_crc_errors    = stats.rx_fcs_err;
2431 	ns->rx_frame_errors  = stats.rx_symbol_err;
2432 	ns->rx_fifo_errors   = stats.rx_ovflow0 + stats.rx_ovflow1 +
2433 			       stats.rx_ovflow2 + stats.rx_ovflow3 +
2434 			       stats.rx_trunc0 + stats.rx_trunc1 +
2435 			       stats.rx_trunc2 + stats.rx_trunc3;
2436 	ns->rx_missed_errors = 0;
2437 
2438 	/* detailed tx_errors */
2439 	ns->tx_aborted_errors   = 0;
2440 	ns->tx_carrier_errors   = 0;
2441 	ns->tx_fifo_errors      = 0;
2442 	ns->tx_heartbeat_errors = 0;
2443 	ns->tx_window_errors    = 0;
2444 
2445 	ns->tx_errors = stats.tx_error_frames;
2446 	ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2447 		ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2448 	return ns;
2449 }
2450 
2451 static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2452 {
2453 	unsigned int mbox;
2454 	int ret = 0, prtad, devad;
2455 	struct port_info *pi = netdev_priv(dev);
2456 	struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2457 
2458 	switch (cmd) {
2459 	case SIOCGMIIPHY:
2460 		if (pi->mdio_addr < 0)
2461 			return -EOPNOTSUPP;
2462 		data->phy_id = pi->mdio_addr;
2463 		break;
2464 	case SIOCGMIIREG:
2465 	case SIOCSMIIREG:
2466 		if (mdio_phy_id_is_c45(data->phy_id)) {
2467 			prtad = mdio_phy_id_prtad(data->phy_id);
2468 			devad = mdio_phy_id_devad(data->phy_id);
2469 		} else if (data->phy_id < 32) {
2470 			prtad = data->phy_id;
2471 			devad = 0;
2472 			data->reg_num &= 0x1f;
2473 		} else
2474 			return -EINVAL;
2475 
2476 		mbox = pi->adapter->pf;
2477 		if (cmd == SIOCGMIIREG)
2478 			ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
2479 					 data->reg_num, &data->val_out);
2480 		else
2481 			ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
2482 					 data->reg_num, data->val_in);
2483 		break;
2484 	case SIOCGHWTSTAMP:
2485 		return copy_to_user(req->ifr_data, &pi->tstamp_config,
2486 				    sizeof(pi->tstamp_config)) ?
2487 			-EFAULT : 0;
2488 	case SIOCSHWTSTAMP:
2489 		if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2490 				   sizeof(pi->tstamp_config)))
2491 			return -EFAULT;
2492 
2493 		switch (pi->tstamp_config.rx_filter) {
2494 		case HWTSTAMP_FILTER_NONE:
2495 			pi->rxtstamp = false;
2496 			break;
2497 		case HWTSTAMP_FILTER_ALL:
2498 			pi->rxtstamp = true;
2499 			break;
2500 		default:
2501 			pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2502 			return -ERANGE;
2503 		}
2504 
2505 		return copy_to_user(req->ifr_data, &pi->tstamp_config,
2506 				    sizeof(pi->tstamp_config)) ?
2507 			-EFAULT : 0;
2508 	default:
2509 		return -EOPNOTSUPP;
2510 	}
2511 	return ret;
2512 }
2513 
2514 static void cxgb_set_rxmode(struct net_device *dev)
2515 {
2516 	/* unfortunately we can't return errors to the stack */
2517 	set_rxmode(dev, -1, false);
2518 }
2519 
2520 static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2521 {
2522 	int ret;
2523 	struct port_info *pi = netdev_priv(dev);
2524 
2525 	ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
2526 			    -1, -1, -1, true);
2527 	if (!ret)
2528 		dev->mtu = new_mtu;
2529 	return ret;
2530 }
2531 
2532 #ifdef CONFIG_PCI_IOV
2533 static int dummy_open(struct net_device *dev)
2534 {
2535 	/* Turn carrier off since we don't have to transmit anything on this
2536 	 * interface.
2537 	 */
2538 	netif_carrier_off(dev);
2539 	return 0;
2540 }
2541 
2542 /* Fill MAC address that will be assigned by the FW */
2543 static void fill_vf_station_mac_addr(struct adapter *adap)
2544 {
2545 	unsigned int i;
2546 	u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2547 	int err;
2548 	u8 *na;
2549 	u16 a, b;
2550 
2551 	err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2552 	if (!err) {
2553 		na = adap->params.vpd.na;
2554 		for (i = 0; i < ETH_ALEN; i++)
2555 			hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2556 				      hex2val(na[2 * i + 1]));
2557 		a = (hw_addr[0] << 8) | hw_addr[1];
2558 		b = (hw_addr[1] << 8) | hw_addr[2];
2559 		a ^= b;
2560 		a |= 0x0200;    /* locally assigned Ethernet MAC address */
2561 		a &= ~0x0100;   /* not a multicast Ethernet MAC address */
2562 		macaddr[0] = a >> 8;
2563 		macaddr[1] = a & 0xff;
2564 
2565 		for (i = 2; i < 5; i++)
2566 			macaddr[i] = hw_addr[i + 1];
2567 
2568 		for (i = 0; i < adap->num_vfs; i++) {
2569 			macaddr[5] = adap->pf * 16 + i;
2570 			ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2571 		}
2572 	}
2573 }
2574 
2575 static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2576 {
2577 	struct port_info *pi = netdev_priv(dev);
2578 	struct adapter *adap = pi->adapter;
2579 	int ret;
2580 
2581 	/* verify MAC addr is valid */
2582 	if (!is_valid_ether_addr(mac)) {
2583 		dev_err(pi->adapter->pdev_dev,
2584 			"Invalid Ethernet address %pM for VF %d\n",
2585 			mac, vf);
2586 		return -EINVAL;
2587 	}
2588 
2589 	dev_info(pi->adapter->pdev_dev,
2590 		 "Setting MAC %pM on VF %d\n", mac, vf);
2591 	ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2592 	if (!ret)
2593 		ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2594 	return ret;
2595 }
2596 
2597 static int cxgb_get_vf_config(struct net_device *dev,
2598 			      int vf, struct ifla_vf_info *ivi)
2599 {
2600 	struct port_info *pi = netdev_priv(dev);
2601 	struct adapter *adap = pi->adapter;
2602 
2603 	if (vf >= adap->num_vfs)
2604 		return -EINVAL;
2605 	ivi->vf = vf;
2606 	ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2607 	return 0;
2608 }
2609 #endif
2610 
2611 static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2612 {
2613 	int ret;
2614 	struct sockaddr *addr = p;
2615 	struct port_info *pi = netdev_priv(dev);
2616 
2617 	if (!is_valid_ether_addr(addr->sa_data))
2618 		return -EADDRNOTAVAIL;
2619 
2620 	ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
2621 			    pi->xact_addr_filt, addr->sa_data, true, true);
2622 	if (ret < 0)
2623 		return ret;
2624 
2625 	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2626 	pi->xact_addr_filt = ret;
2627 	return 0;
2628 }
2629 
2630 #ifdef CONFIG_NET_POLL_CONTROLLER
2631 static void cxgb_netpoll(struct net_device *dev)
2632 {
2633 	struct port_info *pi = netdev_priv(dev);
2634 	struct adapter *adap = pi->adapter;
2635 
2636 	if (adap->flags & USING_MSIX) {
2637 		int i;
2638 		struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2639 
2640 		for (i = pi->nqsets; i; i--, rx++)
2641 			t4_sge_intr_msix(0, &rx->rspq);
2642 	} else
2643 		t4_intr_handler(adap)(0, adap);
2644 }
2645 #endif
2646 
2647 static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2648 {
2649 	struct port_info *pi = netdev_priv(dev);
2650 	struct adapter *adap = pi->adapter;
2651 	struct sched_class *e;
2652 	struct ch_sched_params p;
2653 	struct ch_sched_queue qe;
2654 	u32 req_rate;
2655 	int err = 0;
2656 
2657 	if (!can_sched(dev))
2658 		return -ENOTSUPP;
2659 
2660 	if (index < 0 || index > pi->nqsets - 1)
2661 		return -EINVAL;
2662 
2663 	if (!(adap->flags & FULL_INIT_DONE)) {
2664 		dev_err(adap->pdev_dev,
2665 			"Failed to rate limit on queue %d. Link Down?\n",
2666 			index);
2667 		return -EINVAL;
2668 	}
2669 
2670 	/* Convert from Mbps to Kbps */
2671 	req_rate = rate << 10;
2672 
2673 	/* Max rate is 10 Gbps */
2674 	if (req_rate >= SCHED_MAX_RATE_KBPS) {
2675 		dev_err(adap->pdev_dev,
2676 			"Invalid rate %u Mbps, Max rate is %u Gbps\n",
2677 			rate, SCHED_MAX_RATE_KBPS);
2678 		return -ERANGE;
2679 	}
2680 
2681 	/* First unbind the queue from any existing class */
2682 	memset(&qe, 0, sizeof(qe));
2683 	qe.queue = index;
2684 	qe.class = SCHED_CLS_NONE;
2685 
2686 	err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2687 	if (err) {
2688 		dev_err(adap->pdev_dev,
2689 			"Unbinding Queue %d on port %d fail. Err: %d\n",
2690 			index, pi->port_id, err);
2691 		return err;
2692 	}
2693 
2694 	/* Queue already unbound */
2695 	if (!req_rate)
2696 		return 0;
2697 
2698 	/* Fetch any available unused or matching scheduling class */
2699 	memset(&p, 0, sizeof(p));
2700 	p.type = SCHED_CLASS_TYPE_PACKET;
2701 	p.u.params.level    = SCHED_CLASS_LEVEL_CL_RL;
2702 	p.u.params.mode     = SCHED_CLASS_MODE_CLASS;
2703 	p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2704 	p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2705 	p.u.params.channel  = pi->tx_chan;
2706 	p.u.params.class    = SCHED_CLS_NONE;
2707 	p.u.params.minrate  = 0;
2708 	p.u.params.maxrate  = req_rate;
2709 	p.u.params.weight   = 0;
2710 	p.u.params.pktsize  = dev->mtu;
2711 
2712 	e = cxgb4_sched_class_alloc(dev, &p);
2713 	if (!e)
2714 		return -ENOMEM;
2715 
2716 	/* Bind the queue to a scheduling class */
2717 	memset(&qe, 0, sizeof(qe));
2718 	qe.queue = index;
2719 	qe.class = e->idx;
2720 
2721 	err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2722 	if (err)
2723 		dev_err(adap->pdev_dev,
2724 			"Queue rate limiting failed. Err: %d\n", err);
2725 	return err;
2726 }
2727 
2728 static int cxgb_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
2729 			 struct tc_to_netdev *tc)
2730 {
2731 	struct port_info *pi = netdev2pinfo(dev);
2732 	struct adapter *adap = netdev2adap(dev);
2733 
2734 	if (!(adap->flags & FULL_INIT_DONE)) {
2735 		dev_err(adap->pdev_dev,
2736 			"Failed to setup tc on port %d. Link Down?\n",
2737 			pi->port_id);
2738 		return -EINVAL;
2739 	}
2740 
2741 	if (TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS) &&
2742 	    tc->type == TC_SETUP_CLSU32) {
2743 		switch (tc->cls_u32->command) {
2744 		case TC_CLSU32_NEW_KNODE:
2745 		case TC_CLSU32_REPLACE_KNODE:
2746 			return cxgb4_config_knode(dev, proto, tc->cls_u32);
2747 		case TC_CLSU32_DELETE_KNODE:
2748 			return cxgb4_delete_knode(dev, proto, tc->cls_u32);
2749 		default:
2750 			return -EOPNOTSUPP;
2751 		}
2752 	}
2753 
2754 	return -EOPNOTSUPP;
2755 }
2756 
2757 static const struct net_device_ops cxgb4_netdev_ops = {
2758 	.ndo_open             = cxgb_open,
2759 	.ndo_stop             = cxgb_close,
2760 	.ndo_start_xmit       = t4_eth_xmit,
2761 	.ndo_select_queue     =	cxgb_select_queue,
2762 	.ndo_get_stats64      = cxgb_get_stats,
2763 	.ndo_set_rx_mode      = cxgb_set_rxmode,
2764 	.ndo_set_mac_address  = cxgb_set_mac_addr,
2765 	.ndo_set_features     = cxgb_set_features,
2766 	.ndo_validate_addr    = eth_validate_addr,
2767 	.ndo_do_ioctl         = cxgb_ioctl,
2768 	.ndo_change_mtu       = cxgb_change_mtu,
2769 #ifdef CONFIG_NET_POLL_CONTROLLER
2770 	.ndo_poll_controller  = cxgb_netpoll,
2771 #endif
2772 #ifdef CONFIG_CHELSIO_T4_FCOE
2773 	.ndo_fcoe_enable      = cxgb_fcoe_enable,
2774 	.ndo_fcoe_disable     = cxgb_fcoe_disable,
2775 #endif /* CONFIG_CHELSIO_T4_FCOE */
2776 #ifdef CONFIG_NET_RX_BUSY_POLL
2777 	.ndo_busy_poll        = cxgb_busy_poll,
2778 #endif
2779 	.ndo_set_tx_maxrate   = cxgb_set_tx_maxrate,
2780 	.ndo_setup_tc         = cxgb_setup_tc,
2781 };
2782 
2783 #ifdef CONFIG_PCI_IOV
2784 static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2785 	.ndo_open             = dummy_open,
2786 	.ndo_set_vf_mac       = cxgb_set_vf_mac,
2787 	.ndo_get_vf_config    = cxgb_get_vf_config,
2788 };
2789 #endif
2790 
2791 static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2792 {
2793 	struct adapter *adapter = netdev2adap(dev);
2794 
2795 	strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2796 	strlcpy(info->version, cxgb4_driver_version,
2797 		sizeof(info->version));
2798 	strlcpy(info->bus_info, pci_name(adapter->pdev),
2799 		sizeof(info->bus_info));
2800 }
2801 
2802 static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
2803 	.get_drvinfo       = get_drvinfo,
2804 };
2805 
2806 void t4_fatal_err(struct adapter *adap)
2807 {
2808 	t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
2809 	t4_intr_disable(adap);
2810 	dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
2811 }
2812 
2813 static void setup_memwin(struct adapter *adap)
2814 {
2815 	u32 nic_win_base = t4_get_util_window(adap);
2816 
2817 	t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
2818 }
2819 
2820 static void setup_memwin_rdma(struct adapter *adap)
2821 {
2822 	if (adap->vres.ocq.size) {
2823 		u32 start;
2824 		unsigned int sz_kb;
2825 
2826 		start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
2827 		start &= PCI_BASE_ADDRESS_MEM_MASK;
2828 		start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
2829 		sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
2830 		t4_write_reg(adap,
2831 			     PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
2832 			     start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
2833 		t4_write_reg(adap,
2834 			     PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
2835 			     adap->vres.ocq.start);
2836 		t4_read_reg(adap,
2837 			    PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
2838 	}
2839 }
2840 
2841 static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
2842 {
2843 	u32 v;
2844 	int ret;
2845 
2846 	/* get device capabilities */
2847 	memset(c, 0, sizeof(*c));
2848 	c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2849 			       FW_CMD_REQUEST_F | FW_CMD_READ_F);
2850 	c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
2851 	ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
2852 	if (ret < 0)
2853 		return ret;
2854 
2855 	c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2856 			       FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
2857 	ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
2858 	if (ret < 0)
2859 		return ret;
2860 
2861 	ret = t4_config_glbl_rss(adap, adap->pf,
2862 				 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
2863 				 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
2864 				 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
2865 	if (ret < 0)
2866 		return ret;
2867 
2868 	ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
2869 			  MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
2870 			  FW_CMD_CAP_PF);
2871 	if (ret < 0)
2872 		return ret;
2873 
2874 	t4_sge_init(adap);
2875 
2876 	/* tweak some settings */
2877 	t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
2878 	t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
2879 	t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
2880 	v = t4_read_reg(adap, TP_PIO_DATA_A);
2881 	t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
2882 
2883 	/* first 4 Tx modulation queues point to consecutive Tx channels */
2884 	adap->params.tp.tx_modq_map = 0xE4;
2885 	t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
2886 		     TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
2887 
2888 	/* associate each Tx modulation queue with consecutive Tx channels */
2889 	v = 0x84218421;
2890 	t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
2891 			  &v, 1, TP_TX_SCHED_HDR_A);
2892 	t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
2893 			  &v, 1, TP_TX_SCHED_FIFO_A);
2894 	t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
2895 			  &v, 1, TP_TX_SCHED_PCMD_A);
2896 
2897 #define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
2898 	if (is_offload(adap)) {
2899 		t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
2900 			     TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2901 			     TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2902 			     TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2903 			     TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
2904 		t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
2905 			     TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2906 			     TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2907 			     TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2908 			     TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
2909 	}
2910 
2911 	/* get basic stuff going */
2912 	return t4_early_init(adap, adap->pf);
2913 }
2914 
2915 /*
2916  * Max # of ATIDs.  The absolute HW max is 16K but we keep it lower.
2917  */
2918 #define MAX_ATIDS 8192U
2919 
2920 /*
2921  * Phase 0 of initialization: contact FW, obtain config, perform basic init.
2922  *
2923  * If the firmware we're dealing with has Configuration File support, then
2924  * we use that to perform all configuration
2925  */
2926 
2927 /*
2928  * Tweak configuration based on module parameters, etc.  Most of these have
2929  * defaults assigned to them by Firmware Configuration Files (if we're using
2930  * them) but need to be explicitly set if we're using hard-coded
2931  * initialization.  But even in the case of using Firmware Configuration
2932  * Files, we'd like to expose the ability to change these via module
2933  * parameters so these are essentially common tweaks/settings for
2934  * Configuration Files and hard-coded initialization ...
2935  */
2936 static int adap_init0_tweaks(struct adapter *adapter)
2937 {
2938 	/*
2939 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
2940 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
2941 	 * 64B Cache Line Size ...
2942 	 */
2943 	t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
2944 
2945 	/*
2946 	 * Process module parameters which affect early initialization.
2947 	 */
2948 	if (rx_dma_offset != 2 && rx_dma_offset != 0) {
2949 		dev_err(&adapter->pdev->dev,
2950 			"Ignoring illegal rx_dma_offset=%d, using 2\n",
2951 			rx_dma_offset);
2952 		rx_dma_offset = 2;
2953 	}
2954 	t4_set_reg_field(adapter, SGE_CONTROL_A,
2955 			 PKTSHIFT_V(PKTSHIFT_M),
2956 			 PKTSHIFT_V(rx_dma_offset));
2957 
2958 	/*
2959 	 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
2960 	 * adds the pseudo header itself.
2961 	 */
2962 	t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
2963 			       CSUM_HAS_PSEUDO_HDR_F, 0);
2964 
2965 	return 0;
2966 }
2967 
2968 /* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
2969  * unto themselves and they contain their own firmware to perform their
2970  * tasks ...
2971  */
2972 static int phy_aq1202_version(const u8 *phy_fw_data,
2973 			      size_t phy_fw_size)
2974 {
2975 	int offset;
2976 
2977 	/* At offset 0x8 you're looking for the primary image's
2978 	 * starting offset which is 3 Bytes wide
2979 	 *
2980 	 * At offset 0xa of the primary image, you look for the offset
2981 	 * of the DRAM segment which is 3 Bytes wide.
2982 	 *
2983 	 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
2984 	 * wide
2985 	 */
2986 	#define be16(__p) (((__p)[0] << 8) | (__p)[1])
2987 	#define le16(__p) ((__p)[0] | ((__p)[1] << 8))
2988 	#define le24(__p) (le16(__p) | ((__p)[2] << 16))
2989 
2990 	offset = le24(phy_fw_data + 0x8) << 12;
2991 	offset = le24(phy_fw_data + offset + 0xa);
2992 	return be16(phy_fw_data + offset + 0x27e);
2993 
2994 	#undef be16
2995 	#undef le16
2996 	#undef le24
2997 }
2998 
2999 static struct info_10gbt_phy_fw {
3000 	unsigned int phy_fw_id;		/* PCI Device ID */
3001 	char *phy_fw_file;		/* /lib/firmware/ PHY Firmware file */
3002 	int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3003 	int phy_flash;			/* Has FLASH for PHY Firmware */
3004 } phy_info_array[] = {
3005 	{
3006 		PHY_AQ1202_DEVICEID,
3007 		PHY_AQ1202_FIRMWARE,
3008 		phy_aq1202_version,
3009 		1,
3010 	},
3011 	{
3012 		PHY_BCM84834_DEVICEID,
3013 		PHY_BCM84834_FIRMWARE,
3014 		NULL,
3015 		0,
3016 	},
3017 	{ 0, NULL, NULL },
3018 };
3019 
3020 static struct info_10gbt_phy_fw *find_phy_info(int devid)
3021 {
3022 	int i;
3023 
3024 	for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3025 		if (phy_info_array[i].phy_fw_id == devid)
3026 			return &phy_info_array[i];
3027 	}
3028 	return NULL;
3029 }
3030 
3031 /* Handle updating of chip-external 10Gb/s-BT PHY firmware.  This needs to
3032  * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD.  On error
3033  * we return a negative error number.  If we transfer new firmware we return 1
3034  * (from t4_load_phy_fw()).  If we don't do anything we return 0.
3035  */
3036 static int adap_init0_phy(struct adapter *adap)
3037 {
3038 	const struct firmware *phyf;
3039 	int ret;
3040 	struct info_10gbt_phy_fw *phy_info;
3041 
3042 	/* Use the device ID to determine which PHY file to flash.
3043 	 */
3044 	phy_info = find_phy_info(adap->pdev->device);
3045 	if (!phy_info) {
3046 		dev_warn(adap->pdev_dev,
3047 			 "No PHY Firmware file found for this PHY\n");
3048 		return -EOPNOTSUPP;
3049 	}
3050 
3051 	/* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3052 	 * use that. The adapter firmware provides us with a memory buffer
3053 	 * where we can load a PHY firmware file from the host if we want to
3054 	 * override the PHY firmware File in flash.
3055 	 */
3056 	ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3057 				      adap->pdev_dev);
3058 	if (ret < 0) {
3059 		/* For adapters without FLASH attached to PHY for their
3060 		 * firmware, it's obviously a fatal error if we can't get the
3061 		 * firmware to the adapter.  For adapters with PHY firmware
3062 		 * FLASH storage, it's worth a warning if we can't find the
3063 		 * PHY Firmware but we'll neuter the error ...
3064 		 */
3065 		dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3066 			"/lib/firmware/%s, error %d\n",
3067 			phy_info->phy_fw_file, -ret);
3068 		if (phy_info->phy_flash) {
3069 			int cur_phy_fw_ver = 0;
3070 
3071 			t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3072 			dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3073 				 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3074 			ret = 0;
3075 		}
3076 
3077 		return ret;
3078 	}
3079 
3080 	/* Load PHY Firmware onto adapter.
3081 	 */
3082 	ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3083 			     phy_info->phy_fw_version,
3084 			     (u8 *)phyf->data, phyf->size);
3085 	if (ret < 0)
3086 		dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3087 			-ret);
3088 	else if (ret > 0) {
3089 		int new_phy_fw_ver = 0;
3090 
3091 		if (phy_info->phy_fw_version)
3092 			new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3093 								  phyf->size);
3094 		dev_info(adap->pdev_dev, "Successfully transferred PHY "
3095 			 "Firmware /lib/firmware/%s, version %#x\n",
3096 			 phy_info->phy_fw_file, new_phy_fw_ver);
3097 	}
3098 
3099 	release_firmware(phyf);
3100 
3101 	return ret;
3102 }
3103 
3104 /*
3105  * Attempt to initialize the adapter via a Firmware Configuration File.
3106  */
3107 static int adap_init0_config(struct adapter *adapter, int reset)
3108 {
3109 	struct fw_caps_config_cmd caps_cmd;
3110 	const struct firmware *cf;
3111 	unsigned long mtype = 0, maddr = 0;
3112 	u32 finiver, finicsum, cfcsum;
3113 	int ret;
3114 	int config_issued = 0;
3115 	char *fw_config_file, fw_config_file_path[256];
3116 	char *config_name = NULL;
3117 
3118 	/*
3119 	 * Reset device if necessary.
3120 	 */
3121 	if (reset) {
3122 		ret = t4_fw_reset(adapter, adapter->mbox,
3123 				  PIORSTMODE_F | PIORST_F);
3124 		if (ret < 0)
3125 			goto bye;
3126 	}
3127 
3128 	/* If this is a 10Gb/s-BT adapter make sure the chip-external
3129 	 * 10Gb/s-BT PHYs have up-to-date firmware.  Note that this step needs
3130 	 * to be performed after any global adapter RESET above since some
3131 	 * PHYs only have local RAM copies of the PHY firmware.
3132 	 */
3133 	if (is_10gbt_device(adapter->pdev->device)) {
3134 		ret = adap_init0_phy(adapter);
3135 		if (ret < 0)
3136 			goto bye;
3137 	}
3138 	/*
3139 	 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3140 	 * then use that.  Otherwise, use the configuration file stored
3141 	 * in the adapter flash ...
3142 	 */
3143 	switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
3144 	case CHELSIO_T4:
3145 		fw_config_file = FW4_CFNAME;
3146 		break;
3147 	case CHELSIO_T5:
3148 		fw_config_file = FW5_CFNAME;
3149 		break;
3150 	case CHELSIO_T6:
3151 		fw_config_file = FW6_CFNAME;
3152 		break;
3153 	default:
3154 		dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3155 		       adapter->pdev->device);
3156 		ret = -EINVAL;
3157 		goto bye;
3158 	}
3159 
3160 	ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
3161 	if (ret < 0) {
3162 		config_name = "On FLASH";
3163 		mtype = FW_MEMTYPE_CF_FLASH;
3164 		maddr = t4_flash_cfg_addr(adapter);
3165 	} else {
3166 		u32 params[7], val[7];
3167 
3168 		sprintf(fw_config_file_path,
3169 			"/lib/firmware/%s", fw_config_file);
3170 		config_name = fw_config_file_path;
3171 
3172 		if (cf->size >= FLASH_CFG_MAX_SIZE)
3173 			ret = -ENOMEM;
3174 		else {
3175 			params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3176 			     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
3177 			ret = t4_query_params(adapter, adapter->mbox,
3178 					      adapter->pf, 0, 1, params, val);
3179 			if (ret == 0) {
3180 				/*
3181 				 * For t4_memory_rw() below addresses and
3182 				 * sizes have to be in terms of multiples of 4
3183 				 * bytes.  So, if the Configuration File isn't
3184 				 * a multiple of 4 bytes in length we'll have
3185 				 * to write that out separately since we can't
3186 				 * guarantee that the bytes following the
3187 				 * residual byte in the buffer returned by
3188 				 * request_firmware() are zeroed out ...
3189 				 */
3190 				size_t resid = cf->size & 0x3;
3191 				size_t size = cf->size & ~0x3;
3192 				__be32 *data = (__be32 *)cf->data;
3193 
3194 				mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3195 				maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
3196 
3197 				spin_lock(&adapter->win0_lock);
3198 				ret = t4_memory_rw(adapter, 0, mtype, maddr,
3199 						   size, data, T4_MEMORY_WRITE);
3200 				if (ret == 0 && resid != 0) {
3201 					union {
3202 						__be32 word;
3203 						char buf[4];
3204 					} last;
3205 					int i;
3206 
3207 					last.word = data[size >> 2];
3208 					for (i = resid; i < 4; i++)
3209 						last.buf[i] = 0;
3210 					ret = t4_memory_rw(adapter, 0, mtype,
3211 							   maddr + size,
3212 							   4, &last.word,
3213 							   T4_MEMORY_WRITE);
3214 				}
3215 				spin_unlock(&adapter->win0_lock);
3216 			}
3217 		}
3218 
3219 		release_firmware(cf);
3220 		if (ret)
3221 			goto bye;
3222 	}
3223 
3224 	/*
3225 	 * Issue a Capability Configuration command to the firmware to get it
3226 	 * to parse the Configuration File.  We don't use t4_fw_config_file()
3227 	 * because we want the ability to modify various features after we've
3228 	 * processed the configuration file ...
3229 	 */
3230 	memset(&caps_cmd, 0, sizeof(caps_cmd));
3231 	caps_cmd.op_to_write =
3232 		htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3233 		      FW_CMD_REQUEST_F |
3234 		      FW_CMD_READ_F);
3235 	caps_cmd.cfvalid_to_len16 =
3236 		htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3237 		      FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3238 		      FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
3239 		      FW_LEN16(caps_cmd));
3240 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3241 			 &caps_cmd);
3242 
3243 	/* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3244 	 * Configuration File in FLASH), our last gasp effort is to use the
3245 	 * Firmware Configuration File which is embedded in the firmware.  A
3246 	 * very few early versions of the firmware didn't have one embedded
3247 	 * but we can ignore those.
3248 	 */
3249 	if (ret == -ENOENT) {
3250 		memset(&caps_cmd, 0, sizeof(caps_cmd));
3251 		caps_cmd.op_to_write =
3252 			htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3253 					FW_CMD_REQUEST_F |
3254 					FW_CMD_READ_F);
3255 		caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3256 		ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3257 				sizeof(caps_cmd), &caps_cmd);
3258 		config_name = "Firmware Default";
3259 	}
3260 
3261 	config_issued = 1;
3262 	if (ret < 0)
3263 		goto bye;
3264 
3265 	finiver = ntohl(caps_cmd.finiver);
3266 	finicsum = ntohl(caps_cmd.finicsum);
3267 	cfcsum = ntohl(caps_cmd.cfcsum);
3268 	if (finicsum != cfcsum)
3269 		dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3270 			 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3271 			 finicsum, cfcsum);
3272 
3273 	/*
3274 	 * And now tell the firmware to use the configuration we just loaded.
3275 	 */
3276 	caps_cmd.op_to_write =
3277 		htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3278 		      FW_CMD_REQUEST_F |
3279 		      FW_CMD_WRITE_F);
3280 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3281 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3282 			 NULL);
3283 	if (ret < 0)
3284 		goto bye;
3285 
3286 	/*
3287 	 * Tweak configuration based on system architecture, module
3288 	 * parameters, etc.
3289 	 */
3290 	ret = adap_init0_tweaks(adapter);
3291 	if (ret < 0)
3292 		goto bye;
3293 
3294 	/*
3295 	 * And finally tell the firmware to initialize itself using the
3296 	 * parameters from the Configuration File.
3297 	 */
3298 	ret = t4_fw_initialize(adapter, adapter->mbox);
3299 	if (ret < 0)
3300 		goto bye;
3301 
3302 	/* Emit Firmware Configuration File information and return
3303 	 * successfully.
3304 	 */
3305 	dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
3306 		 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3307 		 config_name, finiver, cfcsum);
3308 	return 0;
3309 
3310 	/*
3311 	 * Something bad happened.  Return the error ...  (If the "error"
3312 	 * is that there's no Configuration File on the adapter we don't
3313 	 * want to issue a warning since this is fairly common.)
3314 	 */
3315 bye:
3316 	if (config_issued && ret != -ENOENT)
3317 		dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3318 			 config_name, -ret);
3319 	return ret;
3320 }
3321 
3322 static struct fw_info fw_info_array[] = {
3323 	{
3324 		.chip = CHELSIO_T4,
3325 		.fs_name = FW4_CFNAME,
3326 		.fw_mod_name = FW4_FNAME,
3327 		.fw_hdr = {
3328 			.chip = FW_HDR_CHIP_T4,
3329 			.fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3330 			.intfver_nic = FW_INTFVER(T4, NIC),
3331 			.intfver_vnic = FW_INTFVER(T4, VNIC),
3332 			.intfver_ri = FW_INTFVER(T4, RI),
3333 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
3334 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
3335 		},
3336 	}, {
3337 		.chip = CHELSIO_T5,
3338 		.fs_name = FW5_CFNAME,
3339 		.fw_mod_name = FW5_FNAME,
3340 		.fw_hdr = {
3341 			.chip = FW_HDR_CHIP_T5,
3342 			.fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3343 			.intfver_nic = FW_INTFVER(T5, NIC),
3344 			.intfver_vnic = FW_INTFVER(T5, VNIC),
3345 			.intfver_ri = FW_INTFVER(T5, RI),
3346 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
3347 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
3348 		},
3349 	}, {
3350 		.chip = CHELSIO_T6,
3351 		.fs_name = FW6_CFNAME,
3352 		.fw_mod_name = FW6_FNAME,
3353 		.fw_hdr = {
3354 			.chip = FW_HDR_CHIP_T6,
3355 			.fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3356 			.intfver_nic = FW_INTFVER(T6, NIC),
3357 			.intfver_vnic = FW_INTFVER(T6, VNIC),
3358 			.intfver_ofld = FW_INTFVER(T6, OFLD),
3359 			.intfver_ri = FW_INTFVER(T6, RI),
3360 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3361 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
3362 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3363 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
3364 		},
3365 	}
3366 
3367 };
3368 
3369 static struct fw_info *find_fw_info(int chip)
3370 {
3371 	int i;
3372 
3373 	for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3374 		if (fw_info_array[i].chip == chip)
3375 			return &fw_info_array[i];
3376 	}
3377 	return NULL;
3378 }
3379 
3380 /*
3381  * Phase 0 of initialization: contact FW, obtain config, perform basic init.
3382  */
3383 static int adap_init0(struct adapter *adap)
3384 {
3385 	int ret;
3386 	u32 v, port_vec;
3387 	enum dev_state state;
3388 	u32 params[7], val[7];
3389 	struct fw_caps_config_cmd caps_cmd;
3390 	int reset = 1;
3391 
3392 	/* Grab Firmware Device Log parameters as early as possible so we have
3393 	 * access to it for debugging, etc.
3394 	 */
3395 	ret = t4_init_devlog_params(adap);
3396 	if (ret < 0)
3397 		return ret;
3398 
3399 	/* Contact FW, advertising Master capability */
3400 	ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3401 			  is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
3402 	if (ret < 0) {
3403 		dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3404 			ret);
3405 		return ret;
3406 	}
3407 	if (ret == adap->mbox)
3408 		adap->flags |= MASTER_PF;
3409 
3410 	/*
3411 	 * If we're the Master PF Driver and the device is uninitialized,
3412 	 * then let's consider upgrading the firmware ...  (We always want
3413 	 * to check the firmware version number in order to A. get it for
3414 	 * later reporting and B. to warn if the currently loaded firmware
3415 	 * is excessively mismatched relative to the driver.)
3416 	 */
3417 	t4_get_fw_version(adap, &adap->params.fw_vers);
3418 	t4_get_bs_version(adap, &adap->params.bs_vers);
3419 	t4_get_tp_version(adap, &adap->params.tp_vers);
3420 	t4_get_exprom_version(adap, &adap->params.er_vers);
3421 
3422 	ret = t4_check_fw_version(adap);
3423 	/* If firmware is too old (not supported by driver) force an update. */
3424 	if (ret)
3425 		state = DEV_STATE_UNINIT;
3426 	if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
3427 		struct fw_info *fw_info;
3428 		struct fw_hdr *card_fw;
3429 		const struct firmware *fw;
3430 		const u8 *fw_data = NULL;
3431 		unsigned int fw_size = 0;
3432 
3433 		/* This is the firmware whose headers the driver was compiled
3434 		 * against
3435 		 */
3436 		fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3437 		if (fw_info == NULL) {
3438 			dev_err(adap->pdev_dev,
3439 				"unable to get firmware info for chip %d.\n",
3440 				CHELSIO_CHIP_VERSION(adap->params.chip));
3441 			return -EINVAL;
3442 		}
3443 
3444 		/* allocate memory to read the header of the firmware on the
3445 		 * card
3446 		 */
3447 		card_fw = t4_alloc_mem(sizeof(*card_fw));
3448 
3449 		/* Get FW from from /lib/firmware/ */
3450 		ret = request_firmware(&fw, fw_info->fw_mod_name,
3451 				       adap->pdev_dev);
3452 		if (ret < 0) {
3453 			dev_err(adap->pdev_dev,
3454 				"unable to load firmware image %s, error %d\n",
3455 				fw_info->fw_mod_name, ret);
3456 		} else {
3457 			fw_data = fw->data;
3458 			fw_size = fw->size;
3459 		}
3460 
3461 		/* upgrade FW logic */
3462 		ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3463 				 state, &reset);
3464 
3465 		/* Cleaning up */
3466 		release_firmware(fw);
3467 		t4_free_mem(card_fw);
3468 
3469 		if (ret < 0)
3470 			goto bye;
3471 	}
3472 
3473 	/*
3474 	 * Grab VPD parameters.  This should be done after we establish a
3475 	 * connection to the firmware since some of the VPD parameters
3476 	 * (notably the Core Clock frequency) are retrieved via requests to
3477 	 * the firmware.  On the other hand, we need these fairly early on
3478 	 * so we do this right after getting ahold of the firmware.
3479 	 */
3480 	ret = t4_get_vpd_params(adap, &adap->params.vpd);
3481 	if (ret < 0)
3482 		goto bye;
3483 
3484 	/*
3485 	 * Find out what ports are available to us.  Note that we need to do
3486 	 * this before calling adap_init0_no_config() since it needs nports
3487 	 * and portvec ...
3488 	 */
3489 	v =
3490 	    FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3491 	    FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
3492 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
3493 	if (ret < 0)
3494 		goto bye;
3495 
3496 	adap->params.nports = hweight32(port_vec);
3497 	adap->params.portvec = port_vec;
3498 
3499 	/* If the firmware is initialized already, emit a simply note to that
3500 	 * effect. Otherwise, it's time to try initializing the adapter.
3501 	 */
3502 	if (state == DEV_STATE_INIT) {
3503 		dev_info(adap->pdev_dev, "Coming up as %s: "\
3504 			 "Adapter already initialized\n",
3505 			 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
3506 	} else {
3507 		dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3508 			 "Initializing adapter\n");
3509 
3510 		/* Find out whether we're dealing with a version of the
3511 		 * firmware which has configuration file support.
3512 		 */
3513 		params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3514 			     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
3515 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
3516 				      params, val);
3517 
3518 		/* If the firmware doesn't support Configuration Files,
3519 		 * return an error.
3520 		 */
3521 		if (ret < 0) {
3522 			dev_err(adap->pdev_dev, "firmware doesn't support "
3523 				"Firmware Configuration Files\n");
3524 			goto bye;
3525 		}
3526 
3527 		/* The firmware provides us with a memory buffer where we can
3528 		 * load a Configuration File from the host if we want to
3529 		 * override the Configuration File in flash.
3530 		 */
3531 		ret = adap_init0_config(adap, reset);
3532 		if (ret == -ENOENT) {
3533 			dev_err(adap->pdev_dev, "no Configuration File "
3534 				"present on adapter.\n");
3535 			goto bye;
3536 		}
3537 		if (ret < 0) {
3538 			dev_err(adap->pdev_dev, "could not initialize "
3539 				"adapter, error %d\n", -ret);
3540 			goto bye;
3541 		}
3542 	}
3543 
3544 	/* Give the SGE code a chance to pull in anything that it needs ...
3545 	 * Note that this must be called after we retrieve our VPD parameters
3546 	 * in order to know how to convert core ticks to seconds, etc.
3547 	 */
3548 	ret = t4_sge_init(adap);
3549 	if (ret < 0)
3550 		goto bye;
3551 
3552 	if (is_bypass_device(adap->pdev->device))
3553 		adap->params.bypass = 1;
3554 
3555 	/*
3556 	 * Grab some of our basic fundamental operating parameters.
3557 	 */
3558 #define FW_PARAM_DEV(param) \
3559 	(FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3560 	FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
3561 
3562 #define FW_PARAM_PFVF(param) \
3563 	FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3564 	FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)|  \
3565 	FW_PARAMS_PARAM_Y_V(0) | \
3566 	FW_PARAMS_PARAM_Z_V(0)
3567 
3568 	params[0] = FW_PARAM_PFVF(EQ_START);
3569 	params[1] = FW_PARAM_PFVF(L2T_START);
3570 	params[2] = FW_PARAM_PFVF(L2T_END);
3571 	params[3] = FW_PARAM_PFVF(FILTER_START);
3572 	params[4] = FW_PARAM_PFVF(FILTER_END);
3573 	params[5] = FW_PARAM_PFVF(IQFLINT_START);
3574 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
3575 	if (ret < 0)
3576 		goto bye;
3577 	adap->sge.egr_start = val[0];
3578 	adap->l2t_start = val[1];
3579 	adap->l2t_end = val[2];
3580 	adap->tids.ftid_base = val[3];
3581 	adap->tids.nftids = val[4] - val[3] + 1;
3582 	adap->sge.ingr_start = val[5];
3583 
3584 	/* qids (ingress/egress) returned from firmware can be anywhere
3585 	 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3586 	 * Hence driver needs to allocate memory for this range to
3587 	 * store the queue info. Get the highest IQFLINT/EQ index returned
3588 	 * in FW_EQ_*_CMD.alloc command.
3589 	 */
3590 	params[0] = FW_PARAM_PFVF(EQ_END);
3591 	params[1] = FW_PARAM_PFVF(IQFLINT_END);
3592 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
3593 	if (ret < 0)
3594 		goto bye;
3595 	adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3596 	adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3597 
3598 	adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3599 				    sizeof(*adap->sge.egr_map), GFP_KERNEL);
3600 	if (!adap->sge.egr_map) {
3601 		ret = -ENOMEM;
3602 		goto bye;
3603 	}
3604 
3605 	adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3606 				     sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3607 	if (!adap->sge.ingr_map) {
3608 		ret = -ENOMEM;
3609 		goto bye;
3610 	}
3611 
3612 	/* Allocate the memory for the vaious egress queue bitmaps
3613 	 * ie starving_fl, txq_maperr and blocked_fl.
3614 	 */
3615 	adap->sge.starving_fl =	kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3616 					sizeof(long), GFP_KERNEL);
3617 	if (!adap->sge.starving_fl) {
3618 		ret = -ENOMEM;
3619 		goto bye;
3620 	}
3621 
3622 	adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3623 				       sizeof(long), GFP_KERNEL);
3624 	if (!adap->sge.txq_maperr) {
3625 		ret = -ENOMEM;
3626 		goto bye;
3627 	}
3628 
3629 #ifdef CONFIG_DEBUG_FS
3630 	adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3631 				       sizeof(long), GFP_KERNEL);
3632 	if (!adap->sge.blocked_fl) {
3633 		ret = -ENOMEM;
3634 		goto bye;
3635 	}
3636 #endif
3637 
3638 	params[0] = FW_PARAM_PFVF(CLIP_START);
3639 	params[1] = FW_PARAM_PFVF(CLIP_END);
3640 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
3641 	if (ret < 0)
3642 		goto bye;
3643 	adap->clipt_start = val[0];
3644 	adap->clipt_end = val[1];
3645 
3646 	/* We don't yet have a PARAMs calls to retrieve the number of Traffic
3647 	 * Classes supported by the hardware/firmware so we hard code it here
3648 	 * for now.
3649 	 */
3650 	adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3651 
3652 	/* query params related to active filter region */
3653 	params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3654 	params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
3655 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
3656 	/* If Active filter size is set we enable establishing
3657 	 * offload connection through firmware work request
3658 	 */
3659 	if ((val[0] != val[1]) && (ret >= 0)) {
3660 		adap->flags |= FW_OFLD_CONN;
3661 		adap->tids.aftid_base = val[0];
3662 		adap->tids.aftid_end = val[1];
3663 	}
3664 
3665 	/* If we're running on newer firmware, let it know that we're
3666 	 * prepared to deal with encapsulated CPL messages.  Older
3667 	 * firmware won't understand this and we'll just get
3668 	 * unencapsulated messages ...
3669 	 */
3670 	params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3671 	val[0] = 1;
3672 	(void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
3673 
3674 	/*
3675 	 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3676 	 * capability.  Earlier versions of the firmware didn't have the
3677 	 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3678 	 * permission to use ULPTX MEMWRITE DSGL.
3679 	 */
3680 	if (is_t4(adap->params.chip)) {
3681 		adap->params.ulptx_memwrite_dsgl = false;
3682 	} else {
3683 		params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
3684 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3685 				      1, params, val);
3686 		adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3687 	}
3688 
3689 	/* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3690 	params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3691 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3692 			      1, params, val);
3693 	adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3694 
3695 	/*
3696 	 * Get device capabilities so we can determine what resources we need
3697 	 * to manage.
3698 	 */
3699 	memset(&caps_cmd, 0, sizeof(caps_cmd));
3700 	caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3701 				     FW_CMD_REQUEST_F | FW_CMD_READ_F);
3702 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3703 	ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3704 			 &caps_cmd);
3705 	if (ret < 0)
3706 		goto bye;
3707 
3708 	if (caps_cmd.ofldcaps) {
3709 		/* query offload-related parameters */
3710 		params[0] = FW_PARAM_DEV(NTID);
3711 		params[1] = FW_PARAM_PFVF(SERVER_START);
3712 		params[2] = FW_PARAM_PFVF(SERVER_END);
3713 		params[3] = FW_PARAM_PFVF(TDDP_START);
3714 		params[4] = FW_PARAM_PFVF(TDDP_END);
3715 		params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
3716 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
3717 				      params, val);
3718 		if (ret < 0)
3719 			goto bye;
3720 		adap->tids.ntids = val[0];
3721 		adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3722 		adap->tids.stid_base = val[1];
3723 		adap->tids.nstids = val[2] - val[1] + 1;
3724 		/*
3725 		 * Setup server filter region. Divide the available filter
3726 		 * region into two parts. Regular filters get 1/3rd and server
3727 		 * filters get 2/3rd part. This is only enabled if workarond
3728 		 * path is enabled.
3729 		 * 1. For regular filters.
3730 		 * 2. Server filter: This are special filters which are used
3731 		 * to redirect SYN packets to offload queue.
3732 		 */
3733 		if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3734 			adap->tids.sftid_base = adap->tids.ftid_base +
3735 					DIV_ROUND_UP(adap->tids.nftids, 3);
3736 			adap->tids.nsftids = adap->tids.nftids -
3737 					 DIV_ROUND_UP(adap->tids.nftids, 3);
3738 			adap->tids.nftids = adap->tids.sftid_base -
3739 						adap->tids.ftid_base;
3740 		}
3741 		adap->vres.ddp.start = val[3];
3742 		adap->vres.ddp.size = val[4] - val[3] + 1;
3743 		adap->params.ofldq_wr_cred = val[5];
3744 
3745 		adap->params.offload = 1;
3746 		adap->num_ofld_uld += 1;
3747 	}
3748 	if (caps_cmd.rdmacaps) {
3749 		params[0] = FW_PARAM_PFVF(STAG_START);
3750 		params[1] = FW_PARAM_PFVF(STAG_END);
3751 		params[2] = FW_PARAM_PFVF(RQ_START);
3752 		params[3] = FW_PARAM_PFVF(RQ_END);
3753 		params[4] = FW_PARAM_PFVF(PBL_START);
3754 		params[5] = FW_PARAM_PFVF(PBL_END);
3755 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
3756 				      params, val);
3757 		if (ret < 0)
3758 			goto bye;
3759 		adap->vres.stag.start = val[0];
3760 		adap->vres.stag.size = val[1] - val[0] + 1;
3761 		adap->vres.rq.start = val[2];
3762 		adap->vres.rq.size = val[3] - val[2] + 1;
3763 		adap->vres.pbl.start = val[4];
3764 		adap->vres.pbl.size = val[5] - val[4] + 1;
3765 
3766 		params[0] = FW_PARAM_PFVF(SQRQ_START);
3767 		params[1] = FW_PARAM_PFVF(SQRQ_END);
3768 		params[2] = FW_PARAM_PFVF(CQ_START);
3769 		params[3] = FW_PARAM_PFVF(CQ_END);
3770 		params[4] = FW_PARAM_PFVF(OCQ_START);
3771 		params[5] = FW_PARAM_PFVF(OCQ_END);
3772 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
3773 				      val);
3774 		if (ret < 0)
3775 			goto bye;
3776 		adap->vres.qp.start = val[0];
3777 		adap->vres.qp.size = val[1] - val[0] + 1;
3778 		adap->vres.cq.start = val[2];
3779 		adap->vres.cq.size = val[3] - val[2] + 1;
3780 		adap->vres.ocq.start = val[4];
3781 		adap->vres.ocq.size = val[5] - val[4] + 1;
3782 
3783 		params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3784 		params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
3785 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
3786 				      val);
3787 		if (ret < 0) {
3788 			adap->params.max_ordird_qp = 8;
3789 			adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3790 			ret = 0;
3791 		} else {
3792 			adap->params.max_ordird_qp = val[0];
3793 			adap->params.max_ird_adapter = val[1];
3794 		}
3795 		dev_info(adap->pdev_dev,
3796 			 "max_ordird_qp %d max_ird_adapter %d\n",
3797 			 adap->params.max_ordird_qp,
3798 			 adap->params.max_ird_adapter);
3799 		adap->num_ofld_uld += 2;
3800 	}
3801 	if (caps_cmd.iscsicaps) {
3802 		params[0] = FW_PARAM_PFVF(ISCSI_START);
3803 		params[1] = FW_PARAM_PFVF(ISCSI_END);
3804 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
3805 				      params, val);
3806 		if (ret < 0)
3807 			goto bye;
3808 		adap->vres.iscsi.start = val[0];
3809 		adap->vres.iscsi.size = val[1] - val[0] + 1;
3810 		/* LIO target and cxgb4i initiaitor */
3811 		adap->num_ofld_uld += 2;
3812 	}
3813 	if (caps_cmd.cryptocaps) {
3814 		/* Should query params here...TODO */
3815 		adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
3816 		adap->num_uld += 1;
3817 	}
3818 #undef FW_PARAM_PFVF
3819 #undef FW_PARAM_DEV
3820 
3821 	/* The MTU/MSS Table is initialized by now, so load their values.  If
3822 	 * we're initializing the adapter, then we'll make any modifications
3823 	 * we want to the MTU/MSS Table and also initialize the congestion
3824 	 * parameters.
3825 	 */
3826 	t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
3827 	if (state != DEV_STATE_INIT) {
3828 		int i;
3829 
3830 		/* The default MTU Table contains values 1492 and 1500.
3831 		 * However, for TCP, it's better to have two values which are
3832 		 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
3833 		 * This allows us to have a TCP Data Payload which is a
3834 		 * multiple of 8 regardless of what combination of TCP Options
3835 		 * are in use (always a multiple of 4 bytes) which is
3836 		 * important for performance reasons.  For instance, if no
3837 		 * options are in use, then we have a 20-byte IP header and a
3838 		 * 20-byte TCP header.  In this case, a 1500-byte MSS would
3839 		 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
3840 		 * which is not a multiple of 8.  So using an MSS of 1488 in
3841 		 * this case results in a TCP Data Payload of 1448 bytes which
3842 		 * is a multiple of 8.  On the other hand, if 12-byte TCP Time
3843 		 * Stamps have been negotiated, then an MTU of 1500 bytes
3844 		 * results in a TCP Data Payload of 1448 bytes which, as
3845 		 * above, is a multiple of 8 bytes ...
3846 		 */
3847 		for (i = 0; i < NMTUS; i++)
3848 			if (adap->params.mtus[i] == 1492) {
3849 				adap->params.mtus[i] = 1488;
3850 				break;
3851 			}
3852 
3853 		t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3854 			     adap->params.b_wnd);
3855 	}
3856 	t4_init_sge_params(adap);
3857 	adap->flags |= FW_OK;
3858 	t4_init_tp_params(adap);
3859 	return 0;
3860 
3861 	/*
3862 	 * Something bad happened.  If a command timed out or failed with EIO
3863 	 * FW does not operate within its spec or something catastrophic
3864 	 * happened to HW/FW, stop issuing commands.
3865 	 */
3866 bye:
3867 	kfree(adap->sge.egr_map);
3868 	kfree(adap->sge.ingr_map);
3869 	kfree(adap->sge.starving_fl);
3870 	kfree(adap->sge.txq_maperr);
3871 #ifdef CONFIG_DEBUG_FS
3872 	kfree(adap->sge.blocked_fl);
3873 #endif
3874 	if (ret != -ETIMEDOUT && ret != -EIO)
3875 		t4_fw_bye(adap, adap->mbox);
3876 	return ret;
3877 }
3878 
3879 /* EEH callbacks */
3880 
3881 static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
3882 					 pci_channel_state_t state)
3883 {
3884 	int i;
3885 	struct adapter *adap = pci_get_drvdata(pdev);
3886 
3887 	if (!adap)
3888 		goto out;
3889 
3890 	rtnl_lock();
3891 	adap->flags &= ~FW_OK;
3892 	notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
3893 	spin_lock(&adap->stats_lock);
3894 	for_each_port(adap, i) {
3895 		struct net_device *dev = adap->port[i];
3896 
3897 		netif_device_detach(dev);
3898 		netif_carrier_off(dev);
3899 	}
3900 	spin_unlock(&adap->stats_lock);
3901 	disable_interrupts(adap);
3902 	if (adap->flags & FULL_INIT_DONE)
3903 		cxgb_down(adap);
3904 	rtnl_unlock();
3905 	if ((adap->flags & DEV_ENABLED)) {
3906 		pci_disable_device(pdev);
3907 		adap->flags &= ~DEV_ENABLED;
3908 	}
3909 out:	return state == pci_channel_io_perm_failure ?
3910 		PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
3911 }
3912 
3913 static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
3914 {
3915 	int i, ret;
3916 	struct fw_caps_config_cmd c;
3917 	struct adapter *adap = pci_get_drvdata(pdev);
3918 
3919 	if (!adap) {
3920 		pci_restore_state(pdev);
3921 		pci_save_state(pdev);
3922 		return PCI_ERS_RESULT_RECOVERED;
3923 	}
3924 
3925 	if (!(adap->flags & DEV_ENABLED)) {
3926 		if (pci_enable_device(pdev)) {
3927 			dev_err(&pdev->dev, "Cannot reenable PCI "
3928 					    "device after reset\n");
3929 			return PCI_ERS_RESULT_DISCONNECT;
3930 		}
3931 		adap->flags |= DEV_ENABLED;
3932 	}
3933 
3934 	pci_set_master(pdev);
3935 	pci_restore_state(pdev);
3936 	pci_save_state(pdev);
3937 	pci_cleanup_aer_uncorrect_error_status(pdev);
3938 
3939 	if (t4_wait_dev_ready(adap->regs) < 0)
3940 		return PCI_ERS_RESULT_DISCONNECT;
3941 	if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
3942 		return PCI_ERS_RESULT_DISCONNECT;
3943 	adap->flags |= FW_OK;
3944 	if (adap_init1(adap, &c))
3945 		return PCI_ERS_RESULT_DISCONNECT;
3946 
3947 	for_each_port(adap, i) {
3948 		struct port_info *p = adap2pinfo(adap, i);
3949 
3950 		ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
3951 				  NULL, NULL);
3952 		if (ret < 0)
3953 			return PCI_ERS_RESULT_DISCONNECT;
3954 		p->viid = ret;
3955 		p->xact_addr_filt = -1;
3956 	}
3957 
3958 	t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3959 		     adap->params.b_wnd);
3960 	setup_memwin(adap);
3961 	if (cxgb_up(adap))
3962 		return PCI_ERS_RESULT_DISCONNECT;
3963 	return PCI_ERS_RESULT_RECOVERED;
3964 }
3965 
3966 static void eeh_resume(struct pci_dev *pdev)
3967 {
3968 	int i;
3969 	struct adapter *adap = pci_get_drvdata(pdev);
3970 
3971 	if (!adap)
3972 		return;
3973 
3974 	rtnl_lock();
3975 	for_each_port(adap, i) {
3976 		struct net_device *dev = adap->port[i];
3977 
3978 		if (netif_running(dev)) {
3979 			link_start(dev);
3980 			cxgb_set_rxmode(dev);
3981 		}
3982 		netif_device_attach(dev);
3983 	}
3984 	rtnl_unlock();
3985 }
3986 
3987 static const struct pci_error_handlers cxgb4_eeh = {
3988 	.error_detected = eeh_err_detected,
3989 	.slot_reset     = eeh_slot_reset,
3990 	.resume         = eeh_resume,
3991 };
3992 
3993 /* Return true if the Link Configuration supports "High Speeds" (those greater
3994  * than 1Gb/s).
3995  */
3996 static inline bool is_x_10g_port(const struct link_config *lc)
3997 {
3998 	unsigned int speeds, high_speeds;
3999 
4000 	speeds = FW_PORT_CAP_SPEED_V(FW_PORT_CAP_SPEED_G(lc->supported));
4001 	high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
4002 
4003 	return high_speeds != 0;
4004 }
4005 
4006 /*
4007  * Perform default configuration of DMA queues depending on the number and type
4008  * of ports we found and the number of available CPUs.  Most settings can be
4009  * modified by the admin prior to actual use.
4010  */
4011 static void cfg_queues(struct adapter *adap)
4012 {
4013 	struct sge *s = &adap->sge;
4014 	int i = 0, n10g = 0, qidx = 0;
4015 #ifndef CONFIG_CHELSIO_T4_DCB
4016 	int q10g = 0;
4017 #endif
4018 
4019 	/* Reduce memory usage in kdump environment, disable all offload.
4020 	 */
4021 	if (is_kdump_kernel()) {
4022 		adap->params.offload = 0;
4023 		adap->params.crypto = 0;
4024 	} else if (is_uld(adap) && t4_uld_mem_alloc(adap)) {
4025 		adap->params.offload = 0;
4026 		adap->params.crypto = 0;
4027 	}
4028 
4029 	n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
4030 #ifdef CONFIG_CHELSIO_T4_DCB
4031 	/* For Data Center Bridging support we need to be able to support up
4032 	 * to 8 Traffic Priorities; each of which will be assigned to its
4033 	 * own TX Queue in order to prevent Head-Of-Line Blocking.
4034 	 */
4035 	if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4036 		dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4037 			MAX_ETH_QSETS, adap->params.nports * 8);
4038 		BUG_ON(1);
4039 	}
4040 
4041 	for_each_port(adap, i) {
4042 		struct port_info *pi = adap2pinfo(adap, i);
4043 
4044 		pi->first_qset = qidx;
4045 		pi->nqsets = 8;
4046 		qidx += pi->nqsets;
4047 	}
4048 #else /* !CONFIG_CHELSIO_T4_DCB */
4049 	/*
4050 	 * We default to 1 queue per non-10G port and up to # of cores queues
4051 	 * per 10G port.
4052 	 */
4053 	if (n10g)
4054 		q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
4055 	if (q10g > netif_get_num_default_rss_queues())
4056 		q10g = netif_get_num_default_rss_queues();
4057 
4058 	for_each_port(adap, i) {
4059 		struct port_info *pi = adap2pinfo(adap, i);
4060 
4061 		pi->first_qset = qidx;
4062 		pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
4063 		qidx += pi->nqsets;
4064 	}
4065 #endif /* !CONFIG_CHELSIO_T4_DCB */
4066 
4067 	s->ethqsets = qidx;
4068 	s->max_ethqsets = qidx;   /* MSI-X may lower it later */
4069 
4070 	if (is_uld(adap)) {
4071 		/*
4072 		 * For offload we use 1 queue/channel if all ports are up to 1G,
4073 		 * otherwise we divide all available queues amongst the channels
4074 		 * capped by the number of available cores.
4075 		 */
4076 		if (n10g) {
4077 			i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
4078 			s->ofldqsets = roundup(i, adap->params.nports);
4079 		} else {
4080 			s->ofldqsets = adap->params.nports;
4081 		}
4082 	}
4083 
4084 	for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4085 		struct sge_eth_rxq *r = &s->ethrxq[i];
4086 
4087 		init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
4088 		r->fl.size = 72;
4089 	}
4090 
4091 	for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4092 		s->ethtxq[i].q.size = 1024;
4093 
4094 	for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4095 		s->ctrlq[i].q.size = 512;
4096 
4097 	init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
4098 	init_rspq(adap, &s->intrq, 0, 1, 512, 64);
4099 }
4100 
4101 /*
4102  * Reduce the number of Ethernet queues across all ports to at most n.
4103  * n provides at least one queue per port.
4104  */
4105 static void reduce_ethqs(struct adapter *adap, int n)
4106 {
4107 	int i;
4108 	struct port_info *pi;
4109 
4110 	while (n < adap->sge.ethqsets)
4111 		for_each_port(adap, i) {
4112 			pi = adap2pinfo(adap, i);
4113 			if (pi->nqsets > 1) {
4114 				pi->nqsets--;
4115 				adap->sge.ethqsets--;
4116 				if (adap->sge.ethqsets <= n)
4117 					break;
4118 			}
4119 		}
4120 
4121 	n = 0;
4122 	for_each_port(adap, i) {
4123 		pi = adap2pinfo(adap, i);
4124 		pi->first_qset = n;
4125 		n += pi->nqsets;
4126 	}
4127 }
4128 
4129 static int get_msix_info(struct adapter *adap)
4130 {
4131 	struct uld_msix_info *msix_info;
4132 	unsigned int max_ingq = 0;
4133 
4134 	if (is_offload(adap))
4135 		max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4136 	if (is_pci_uld(adap))
4137 		max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4138 
4139 	if (!max_ingq)
4140 		goto out;
4141 
4142 	msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4143 	if (!msix_info)
4144 		return -ENOMEM;
4145 
4146 	adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4147 						 sizeof(long), GFP_KERNEL);
4148 	if (!adap->msix_bmap_ulds.msix_bmap) {
4149 		kfree(msix_info);
4150 		return -ENOMEM;
4151 	}
4152 	spin_lock_init(&adap->msix_bmap_ulds.lock);
4153 	adap->msix_info_ulds = msix_info;
4154 out:
4155 	return 0;
4156 }
4157 
4158 static void free_msix_info(struct adapter *adap)
4159 {
4160 	if (!(adap->num_uld && adap->num_ofld_uld))
4161 		return;
4162 
4163 	kfree(adap->msix_info_ulds);
4164 	kfree(adap->msix_bmap_ulds.msix_bmap);
4165 }
4166 
4167 /* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4168 #define EXTRA_VECS 2
4169 
4170 static int enable_msix(struct adapter *adap)
4171 {
4172 	int ofld_need = 0, uld_need = 0;
4173 	int i, j, want, need, allocated;
4174 	struct sge *s = &adap->sge;
4175 	unsigned int nchan = adap->params.nports;
4176 	struct msix_entry *entries;
4177 	int max_ingq = MAX_INGQ;
4178 
4179 	if (is_pci_uld(adap))
4180 		max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4181 	if (is_offload(adap))
4182 		max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
4183 	entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
4184 			  GFP_KERNEL);
4185 	if (!entries)
4186 		return -ENOMEM;
4187 
4188 	/* map for msix */
4189 	if (get_msix_info(adap)) {
4190 		adap->params.offload = 0;
4191 		adap->params.crypto = 0;
4192 	}
4193 
4194 	for (i = 0; i < max_ingq + 1; ++i)
4195 		entries[i].entry = i;
4196 
4197 	want = s->max_ethqsets + EXTRA_VECS;
4198 	if (is_offload(adap)) {
4199 		want += adap->num_ofld_uld * s->ofldqsets;
4200 		ofld_need = adap->num_ofld_uld * nchan;
4201 	}
4202 	if (is_pci_uld(adap)) {
4203 		want += adap->num_uld * s->ofldqsets;
4204 		uld_need = adap->num_uld * nchan;
4205 	}
4206 #ifdef CONFIG_CHELSIO_T4_DCB
4207 	/* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4208 	 * each port.
4209 	 */
4210 	need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
4211 #else
4212 	need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
4213 #endif
4214 	allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4215 	if (allocated < 0) {
4216 		dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4217 			 " not using MSI-X\n");
4218 		kfree(entries);
4219 		return allocated;
4220 	}
4221 
4222 	/* Distribute available vectors to the various queue groups.
4223 	 * Every group gets its minimum requirement and NIC gets top
4224 	 * priority for leftovers.
4225 	 */
4226 	i = allocated - EXTRA_VECS - ofld_need - uld_need;
4227 	if (i < s->max_ethqsets) {
4228 		s->max_ethqsets = i;
4229 		if (i < s->ethqsets)
4230 			reduce_ethqs(adap, i);
4231 	}
4232 	if (is_uld(adap)) {
4233 		if (allocated < want)
4234 			s->nqs_per_uld = nchan;
4235 		else
4236 			s->nqs_per_uld = s->ofldqsets;
4237 	}
4238 
4239 	for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
4240 		adap->msix_info[i].vec = entries[i].vector;
4241 	if (is_uld(adap)) {
4242 		for (j = 0 ; i < allocated; ++i, j++) {
4243 			adap->msix_info_ulds[j].vec = entries[i].vector;
4244 			adap->msix_info_ulds[j].idx = i;
4245 		}
4246 		adap->msix_bmap_ulds.mapsize = j;
4247 	}
4248 	dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
4249 		 "nic %d per uld %d\n",
4250 		 allocated, s->max_ethqsets, s->nqs_per_uld);
4251 
4252 	kfree(entries);
4253 	return 0;
4254 }
4255 
4256 #undef EXTRA_VECS
4257 
4258 static int init_rss(struct adapter *adap)
4259 {
4260 	unsigned int i;
4261 	int err;
4262 
4263 	err = t4_init_rss_mode(adap, adap->mbox);
4264 	if (err)
4265 		return err;
4266 
4267 	for_each_port(adap, i) {
4268 		struct port_info *pi = adap2pinfo(adap, i);
4269 
4270 		pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4271 		if (!pi->rss)
4272 			return -ENOMEM;
4273 	}
4274 	return 0;
4275 }
4276 
4277 static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4278 					enum pci_bus_speed *speed,
4279 					enum pcie_link_width *width)
4280 {
4281 	u32 lnkcap1, lnkcap2;
4282 	int err1, err2;
4283 
4284 #define  PCIE_MLW_CAP_SHIFT 4   /* start of MLW mask in link capabilities */
4285 
4286 	*speed = PCI_SPEED_UNKNOWN;
4287 	*width = PCIE_LNK_WIDTH_UNKNOWN;
4288 
4289 	err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4290 					  &lnkcap1);
4291 	err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4292 					  &lnkcap2);
4293 	if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4294 		if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4295 			*speed = PCIE_SPEED_8_0GT;
4296 		else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4297 			*speed = PCIE_SPEED_5_0GT;
4298 		else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4299 			*speed = PCIE_SPEED_2_5GT;
4300 	}
4301 	if (!err1) {
4302 		*width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4303 		if (!lnkcap2) { /* pre-r3.0 */
4304 			if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4305 				*speed = PCIE_SPEED_5_0GT;
4306 			else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4307 				*speed = PCIE_SPEED_2_5GT;
4308 		}
4309 	}
4310 
4311 	if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4312 		return err1 ? err1 : err2 ? err2 : -EINVAL;
4313 	return 0;
4314 }
4315 
4316 static void cxgb4_check_pcie_caps(struct adapter *adap)
4317 {
4318 	enum pcie_link_width width, width_cap;
4319 	enum pci_bus_speed speed, speed_cap;
4320 
4321 #define PCIE_SPEED_STR(speed) \
4322 	(speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4323 	 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4324 	 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4325 	 "Unknown")
4326 
4327 	if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4328 		dev_warn(adap->pdev_dev,
4329 			 "Unable to determine PCIe device BW capabilities\n");
4330 		return;
4331 	}
4332 
4333 	if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4334 	    speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4335 		dev_warn(adap->pdev_dev,
4336 			 "Unable to determine PCI Express bandwidth.\n");
4337 		return;
4338 	}
4339 
4340 	dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4341 		 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4342 	dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4343 		 width, width_cap);
4344 	if (speed < speed_cap || width < width_cap)
4345 		dev_info(adap->pdev_dev,
4346 			 "A slot with more lanes and/or higher speed is "
4347 			 "suggested for optimal performance.\n");
4348 }
4349 
4350 /* Dump basic information about the adapter */
4351 static void print_adapter_info(struct adapter *adapter)
4352 {
4353 	/* Device information */
4354 	dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4355 		 adapter->params.vpd.id,
4356 		 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4357 	dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4358 		 adapter->params.vpd.sn, adapter->params.vpd.pn);
4359 
4360 	/* Firmware Version */
4361 	if (!adapter->params.fw_vers)
4362 		dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4363 	else
4364 		dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4365 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4366 			 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4367 			 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4368 			 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4369 
4370 	/* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4371 	 * Firmware, so dev_info() is more appropriate here.)
4372 	 */
4373 	if (!adapter->params.bs_vers)
4374 		dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4375 	else
4376 		dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4377 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4378 			 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4379 			 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4380 			 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4381 
4382 	/* TP Microcode Version */
4383 	if (!adapter->params.tp_vers)
4384 		dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4385 	else
4386 		dev_info(adapter->pdev_dev,
4387 			 "TP Microcode version: %u.%u.%u.%u\n",
4388 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4389 			 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4390 			 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4391 			 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4392 
4393 	/* Expansion ROM version */
4394 	if (!adapter->params.er_vers)
4395 		dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4396 	else
4397 		dev_info(adapter->pdev_dev,
4398 			 "Expansion ROM version: %u.%u.%u.%u\n",
4399 			 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4400 			 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4401 			 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4402 			 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4403 
4404 	/* Software/Hardware configuration */
4405 	dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4406 		 is_offload(adapter) ? "R" : "",
4407 		 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4408 		  (adapter->flags & USING_MSI) ? "MSI" : ""),
4409 		 is_offload(adapter) ? "Offload" : "non-Offload");
4410 }
4411 
4412 static void print_port_info(const struct net_device *dev)
4413 {
4414 	char buf[80];
4415 	char *bufp = buf;
4416 	const char *spd = "";
4417 	const struct port_info *pi = netdev_priv(dev);
4418 	const struct adapter *adap = pi->adapter;
4419 
4420 	if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4421 		spd = " 2.5 GT/s";
4422 	else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4423 		spd = " 5 GT/s";
4424 	else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4425 		spd = " 8 GT/s";
4426 
4427 	if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4428 		bufp += sprintf(bufp, "100/");
4429 	if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4430 		bufp += sprintf(bufp, "1000/");
4431 	if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4432 		bufp += sprintf(bufp, "10G/");
4433 	if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
4434 		bufp += sprintf(bufp, "25G/");
4435 	if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4436 		bufp += sprintf(bufp, "40G/");
4437 	if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
4438 		bufp += sprintf(bufp, "100G/");
4439 	if (bufp != buf)
4440 		--bufp;
4441 	sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
4442 
4443 	netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4444 		    dev->name, adap->params.vpd.id, adap->name, buf);
4445 }
4446 
4447 static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
4448 {
4449 	pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
4450 }
4451 
4452 /*
4453  * Free the following resources:
4454  * - memory used for tables
4455  * - MSI/MSI-X
4456  * - net devices
4457  * - resources FW is holding for us
4458  */
4459 static void free_some_resources(struct adapter *adapter)
4460 {
4461 	unsigned int i;
4462 
4463 	t4_free_mem(adapter->l2t);
4464 	t4_cleanup_sched(adapter);
4465 	t4_free_mem(adapter->tids.tid_tab);
4466 	cxgb4_cleanup_tc_u32(adapter);
4467 	kfree(adapter->sge.egr_map);
4468 	kfree(adapter->sge.ingr_map);
4469 	kfree(adapter->sge.starving_fl);
4470 	kfree(adapter->sge.txq_maperr);
4471 #ifdef CONFIG_DEBUG_FS
4472 	kfree(adapter->sge.blocked_fl);
4473 #endif
4474 	disable_msi(adapter);
4475 
4476 	for_each_port(adapter, i)
4477 		if (adapter->port[i]) {
4478 			struct port_info *pi = adap2pinfo(adapter, i);
4479 
4480 			if (pi->viid != 0)
4481 				t4_free_vi(adapter, adapter->mbox, adapter->pf,
4482 					   0, pi->viid);
4483 			kfree(adap2pinfo(adapter, i)->rss);
4484 			free_netdev(adapter->port[i]);
4485 		}
4486 	if (adapter->flags & FW_OK)
4487 		t4_fw_bye(adapter, adapter->pf);
4488 }
4489 
4490 #define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
4491 #define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
4492 		   NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
4493 #define SEGMENT_SIZE 128
4494 
4495 static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4496 {
4497 	u16 device_id;
4498 
4499 	/* Retrieve adapter's device ID */
4500 	pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
4501 
4502 	switch (device_id >> 12) {
4503 	case CHELSIO_T4:
4504 		return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
4505 	case CHELSIO_T5:
4506 		return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
4507 	case CHELSIO_T6:
4508 		return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
4509 	default:
4510 		dev_err(&pdev->dev, "Device %d is not supported\n",
4511 			device_id);
4512 	}
4513 	return -EINVAL;
4514 }
4515 
4516 #ifdef CONFIG_PCI_IOV
4517 static void dummy_setup(struct net_device *dev)
4518 {
4519 	dev->type = ARPHRD_NONE;
4520 	dev->mtu = 0;
4521 	dev->hard_header_len = 0;
4522 	dev->addr_len = 0;
4523 	dev->tx_queue_len = 0;
4524 	dev->flags |= IFF_NOARP;
4525 	dev->priv_flags |= IFF_NO_QUEUE;
4526 
4527 	/* Initialize the device structure. */
4528 	dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4529 	dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
4530 	dev->destructor = free_netdev;
4531 }
4532 
4533 static int config_mgmt_dev(struct pci_dev *pdev)
4534 {
4535 	struct adapter *adap = pci_get_drvdata(pdev);
4536 	struct net_device *netdev;
4537 	struct port_info *pi;
4538 	char name[IFNAMSIZ];
4539 	int err;
4540 
4541 	snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
4542 	netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, dummy_setup);
4543 	if (!netdev)
4544 		return -ENOMEM;
4545 
4546 	pi = netdev_priv(netdev);
4547 	pi->adapter = adap;
4548 	SET_NETDEV_DEV(netdev, &pdev->dev);
4549 
4550 	adap->port[0] = netdev;
4551 
4552 	err = register_netdev(adap->port[0]);
4553 	if (err) {
4554 		pr_info("Unable to register VF mgmt netdev %s\n", name);
4555 		free_netdev(adap->port[0]);
4556 		adap->port[0] = NULL;
4557 		return err;
4558 	}
4559 	return 0;
4560 }
4561 
4562 static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4563 {
4564 	struct adapter *adap = pci_get_drvdata(pdev);
4565 	int err = 0;
4566 	int current_vfs = pci_num_vf(pdev);
4567 	u32 pcie_fw;
4568 
4569 	pcie_fw = readl(adap->regs + PCIE_FW_A);
4570 	/* Check if cxgb4 is the MASTER and fw is initialized */
4571 	if (!(pcie_fw & PCIE_FW_INIT_F) ||
4572 	    !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4573 	    PCIE_FW_MASTER_G(pcie_fw) != 4) {
4574 		dev_warn(&pdev->dev,
4575 			 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4576 		return -EOPNOTSUPP;
4577 	}
4578 
4579 	/* If any of the VF's is already assigned to Guest OS, then
4580 	 * SRIOV for the same cannot be modified
4581 	 */
4582 	if (current_vfs && pci_vfs_assigned(pdev)) {
4583 		dev_err(&pdev->dev,
4584 			"Cannot modify SR-IOV while VFs are assigned\n");
4585 		num_vfs = current_vfs;
4586 		return num_vfs;
4587 	}
4588 
4589 	/* Disable SRIOV when zero is passed.
4590 	 * One needs to disable SRIOV before modifying it, else
4591 	 * stack throws the below warning:
4592 	 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4593 	 */
4594 	if (!num_vfs) {
4595 		pci_disable_sriov(pdev);
4596 		if (adap->port[0]) {
4597 			unregister_netdev(adap->port[0]);
4598 			adap->port[0] = NULL;
4599 		}
4600 		/* free VF resources */
4601 		kfree(adap->vfinfo);
4602 		adap->vfinfo = NULL;
4603 		adap->num_vfs = 0;
4604 		return num_vfs;
4605 	}
4606 
4607 	if (num_vfs != current_vfs) {
4608 		err = pci_enable_sriov(pdev, num_vfs);
4609 		if (err)
4610 			return err;
4611 
4612 		adap->num_vfs = num_vfs;
4613 		err = config_mgmt_dev(pdev);
4614 		if (err)
4615 			return err;
4616 	}
4617 
4618 	adap->vfinfo = kcalloc(adap->num_vfs,
4619 			       sizeof(struct vf_info), GFP_KERNEL);
4620 	if (adap->vfinfo)
4621 		fill_vf_station_mac_addr(adap);
4622 	return num_vfs;
4623 }
4624 #endif
4625 
4626 static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
4627 {
4628 	int func, i, err, s_qpp, qpp, num_seg;
4629 	struct port_info *pi;
4630 	bool highdma = false;
4631 	struct adapter *adapter = NULL;
4632 	struct net_device *netdev;
4633 	void __iomem *regs;
4634 	u32 whoami, pl_rev;
4635 	enum chip_type chip;
4636 	static int adap_idx = 1;
4637 
4638 	printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4639 
4640 	err = pci_request_regions(pdev, KBUILD_MODNAME);
4641 	if (err) {
4642 		/* Just info, some other driver may have claimed the device. */
4643 		dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4644 		return err;
4645 	}
4646 
4647 	err = pci_enable_device(pdev);
4648 	if (err) {
4649 		dev_err(&pdev->dev, "cannot enable PCI device\n");
4650 		goto out_release_regions;
4651 	}
4652 
4653 	regs = pci_ioremap_bar(pdev, 0);
4654 	if (!regs) {
4655 		dev_err(&pdev->dev, "cannot map device registers\n");
4656 		err = -ENOMEM;
4657 		goto out_disable_device;
4658 	}
4659 
4660 	err = t4_wait_dev_ready(regs);
4661 	if (err < 0)
4662 		goto out_unmap_bar0;
4663 
4664 	/* We control everything through one PF */
4665 	whoami = readl(regs + PL_WHOAMI_A);
4666 	pl_rev = REV_G(readl(regs + PL_REV_A));
4667 	chip = get_chip_type(pdev, pl_rev);
4668 	func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4669 		SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
4670 	if (func != ent->driver_data) {
4671 #ifndef CONFIG_PCI_IOV
4672 		iounmap(regs);
4673 #endif
4674 		pci_disable_device(pdev);
4675 		pci_save_state(pdev);        /* to restore SR-IOV later */
4676 		goto sriov;
4677 	}
4678 
4679 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4680 		highdma = true;
4681 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4682 		if (err) {
4683 			dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4684 				"coherent allocations\n");
4685 			goto out_unmap_bar0;
4686 		}
4687 	} else {
4688 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4689 		if (err) {
4690 			dev_err(&pdev->dev, "no usable DMA configuration\n");
4691 			goto out_unmap_bar0;
4692 		}
4693 	}
4694 
4695 	pci_enable_pcie_error_reporting(pdev);
4696 	enable_pcie_relaxed_ordering(pdev);
4697 	pci_set_master(pdev);
4698 	pci_save_state(pdev);
4699 
4700 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4701 	if (!adapter) {
4702 		err = -ENOMEM;
4703 		goto out_unmap_bar0;
4704 	}
4705 	adap_idx++;
4706 
4707 	adapter->workq = create_singlethread_workqueue("cxgb4");
4708 	if (!adapter->workq) {
4709 		err = -ENOMEM;
4710 		goto out_free_adapter;
4711 	}
4712 
4713 	adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4714 				    (sizeof(struct mbox_cmd) *
4715 				     T4_OS_LOG_MBOX_CMDS),
4716 				    GFP_KERNEL);
4717 	if (!adapter->mbox_log) {
4718 		err = -ENOMEM;
4719 		goto out_free_adapter;
4720 	}
4721 	adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4722 
4723 	/* PCI device has been enabled */
4724 	adapter->flags |= DEV_ENABLED;
4725 
4726 	adapter->regs = regs;
4727 	adapter->pdev = pdev;
4728 	adapter->pdev_dev = &pdev->dev;
4729 	adapter->name = pci_name(pdev);
4730 	adapter->mbox = func;
4731 	adapter->pf = func;
4732 	adapter->msg_enable = dflt_msg_enable;
4733 	memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4734 
4735 	spin_lock_init(&adapter->stats_lock);
4736 	spin_lock_init(&adapter->tid_release_lock);
4737 	spin_lock_init(&adapter->win0_lock);
4738 
4739 	INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
4740 	INIT_WORK(&adapter->db_full_task, process_db_full);
4741 	INIT_WORK(&adapter->db_drop_task, process_db_drop);
4742 
4743 	err = t4_prep_adapter(adapter);
4744 	if (err)
4745 		goto out_free_adapter;
4746 
4747 
4748 	if (!is_t4(adapter->params.chip)) {
4749 		s_qpp = (QUEUESPERPAGEPF0_S +
4750 			(QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
4751 			adapter->pf);
4752 		qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4753 		      SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
4754 		num_seg = PAGE_SIZE / SEGMENT_SIZE;
4755 
4756 		/* Each segment size is 128B. Write coalescing is enabled only
4757 		 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4758 		 * queue is less no of segments that can be accommodated in
4759 		 * a page size.
4760 		 */
4761 		if (qpp > num_seg) {
4762 			dev_err(&pdev->dev,
4763 				"Incorrect number of egress queues per page\n");
4764 			err = -EINVAL;
4765 			goto out_free_adapter;
4766 		}
4767 		adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4768 		pci_resource_len(pdev, 2));
4769 		if (!adapter->bar2) {
4770 			dev_err(&pdev->dev, "cannot map device bar2 region\n");
4771 			err = -ENOMEM;
4772 			goto out_free_adapter;
4773 		}
4774 	}
4775 
4776 	setup_memwin(adapter);
4777 	err = adap_init0(adapter);
4778 #ifdef CONFIG_DEBUG_FS
4779 	bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4780 #endif
4781 	setup_memwin_rdma(adapter);
4782 	if (err)
4783 		goto out_unmap_bar;
4784 
4785 	/* configure SGE_STAT_CFG_A to read WC stats */
4786 	if (!is_t4(adapter->params.chip))
4787 		t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4788 			     (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4789 			      T6_STATMODE_V(0)));
4790 
4791 	for_each_port(adapter, i) {
4792 		netdev = alloc_etherdev_mq(sizeof(struct port_info),
4793 					   MAX_ETH_QSETS);
4794 		if (!netdev) {
4795 			err = -ENOMEM;
4796 			goto out_free_dev;
4797 		}
4798 
4799 		SET_NETDEV_DEV(netdev, &pdev->dev);
4800 
4801 		adapter->port[i] = netdev;
4802 		pi = netdev_priv(netdev);
4803 		pi->adapter = adapter;
4804 		pi->xact_addr_filt = -1;
4805 		pi->port_id = i;
4806 		netdev->irq = pdev->irq;
4807 
4808 		netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4809 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4810 			NETIF_F_RXCSUM | NETIF_F_RXHASH |
4811 			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
4812 			NETIF_F_HW_TC;
4813 		if (highdma)
4814 			netdev->hw_features |= NETIF_F_HIGHDMA;
4815 		netdev->features |= netdev->hw_features;
4816 		netdev->vlan_features = netdev->features & VLAN_FEAT;
4817 
4818 		netdev->priv_flags |= IFF_UNICAST_FLT;
4819 
4820 		/* MTU range: 81 - 9600 */
4821 		netdev->min_mtu = 81;
4822 		netdev->max_mtu = MAX_MTU;
4823 
4824 		netdev->netdev_ops = &cxgb4_netdev_ops;
4825 #ifdef CONFIG_CHELSIO_T4_DCB
4826 		netdev->dcbnl_ops = &cxgb4_dcb_ops;
4827 		cxgb4_dcb_state_init(netdev);
4828 #endif
4829 		cxgb4_set_ethtool_ops(netdev);
4830 	}
4831 
4832 	pci_set_drvdata(pdev, adapter);
4833 
4834 	if (adapter->flags & FW_OK) {
4835 		err = t4_port_init(adapter, func, func, 0);
4836 		if (err)
4837 			goto out_free_dev;
4838 	} else if (adapter->params.nports == 1) {
4839 		/* If we don't have a connection to the firmware -- possibly
4840 		 * because of an error -- grab the raw VPD parameters so we
4841 		 * can set the proper MAC Address on the debug network
4842 		 * interface that we've created.
4843 		 */
4844 		u8 hw_addr[ETH_ALEN];
4845 		u8 *na = adapter->params.vpd.na;
4846 
4847 		err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
4848 		if (!err) {
4849 			for (i = 0; i < ETH_ALEN; i++)
4850 				hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
4851 					      hex2val(na[2 * i + 1]));
4852 			t4_set_hw_addr(adapter, 0, hw_addr);
4853 		}
4854 	}
4855 
4856 	/* Configure queues and allocate tables now, they can be needed as
4857 	 * soon as the first register_netdev completes.
4858 	 */
4859 	cfg_queues(adapter);
4860 
4861 	adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
4862 	if (!adapter->l2t) {
4863 		/* We tolerate a lack of L2T, giving up some functionality */
4864 		dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
4865 		adapter->params.offload = 0;
4866 	}
4867 
4868 #if IS_ENABLED(CONFIG_IPV6)
4869 	if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
4870 	    (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
4871 		/* CLIP functionality is not present in hardware,
4872 		 * hence disable all offload features
4873 		 */
4874 		dev_warn(&pdev->dev,
4875 			 "CLIP not enabled in hardware, continuing\n");
4876 		adapter->params.offload = 0;
4877 	} else {
4878 		adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
4879 						  adapter->clipt_end);
4880 		if (!adapter->clipt) {
4881 			/* We tolerate a lack of clip_table, giving up
4882 			 * some functionality
4883 			 */
4884 			dev_warn(&pdev->dev,
4885 				 "could not allocate Clip table, continuing\n");
4886 			adapter->params.offload = 0;
4887 		}
4888 	}
4889 #endif
4890 
4891 	for_each_port(adapter, i) {
4892 		pi = adap2pinfo(adapter, i);
4893 		pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
4894 		if (!pi->sched_tbl)
4895 			dev_warn(&pdev->dev,
4896 				 "could not activate scheduling on port %d\n",
4897 				 i);
4898 	}
4899 
4900 	if (tid_init(&adapter->tids) < 0) {
4901 		dev_warn(&pdev->dev, "could not allocate TID table, "
4902 			 "continuing\n");
4903 		adapter->params.offload = 0;
4904 	} else {
4905 		adapter->tc_u32 = cxgb4_init_tc_u32(adapter,
4906 						    CXGB4_MAX_LINK_HANDLE);
4907 		if (!adapter->tc_u32)
4908 			dev_warn(&pdev->dev,
4909 				 "could not offload tc u32, continuing\n");
4910 	}
4911 
4912 	if (is_offload(adapter)) {
4913 		if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
4914 			u32 hash_base, hash_reg;
4915 
4916 			if (chip <= CHELSIO_T5) {
4917 				hash_reg = LE_DB_TID_HASHBASE_A;
4918 				hash_base = t4_read_reg(adapter, hash_reg);
4919 				adapter->tids.hash_base = hash_base / 4;
4920 			} else {
4921 				hash_reg = T6_LE_DB_HASH_TID_BASE_A;
4922 				hash_base = t4_read_reg(adapter, hash_reg);
4923 				adapter->tids.hash_base = hash_base;
4924 			}
4925 		}
4926 	}
4927 
4928 	/* See what interrupts we'll be using */
4929 	if (msi > 1 && enable_msix(adapter) == 0)
4930 		adapter->flags |= USING_MSIX;
4931 	else if (msi > 0 && pci_enable_msi(pdev) == 0) {
4932 		adapter->flags |= USING_MSI;
4933 		if (msi > 1)
4934 			free_msix_info(adapter);
4935 	}
4936 
4937 	/* check for PCI Express bandwidth capabiltites */
4938 	cxgb4_check_pcie_caps(adapter);
4939 
4940 	err = init_rss(adapter);
4941 	if (err)
4942 		goto out_free_dev;
4943 
4944 	/*
4945 	 * The card is now ready to go.  If any errors occur during device
4946 	 * registration we do not fail the whole card but rather proceed only
4947 	 * with the ports we manage to register successfully.  However we must
4948 	 * register at least one net device.
4949 	 */
4950 	for_each_port(adapter, i) {
4951 		pi = adap2pinfo(adapter, i);
4952 		netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
4953 		netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
4954 
4955 		err = register_netdev(adapter->port[i]);
4956 		if (err)
4957 			break;
4958 		adapter->chan_map[pi->tx_chan] = i;
4959 		print_port_info(adapter->port[i]);
4960 	}
4961 	if (i == 0) {
4962 		dev_err(&pdev->dev, "could not register any net devices\n");
4963 		goto out_free_dev;
4964 	}
4965 	if (err) {
4966 		dev_warn(&pdev->dev, "only %d net devices registered\n", i);
4967 		err = 0;
4968 	}
4969 
4970 	if (cxgb4_debugfs_root) {
4971 		adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
4972 							   cxgb4_debugfs_root);
4973 		setup_debugfs(adapter);
4974 	}
4975 
4976 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4977 	pdev->needs_freset = 1;
4978 
4979 	if (is_uld(adapter)) {
4980 		mutex_lock(&uld_mutex);
4981 		list_add_tail(&adapter->list_node, &adapter_list);
4982 		mutex_unlock(&uld_mutex);
4983 	}
4984 
4985 	print_adapter_info(adapter);
4986 	setup_fw_sge_queues(adapter);
4987 	return 0;
4988 
4989 sriov:
4990 #ifdef CONFIG_PCI_IOV
4991 	if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0) {
4992 		dev_warn(&pdev->dev,
4993 			 "Enabling SR-IOV VFs using the num_vf module "
4994 			 "parameter is deprecated - please use the pci sysfs "
4995 			 "interface instead.\n");
4996 		if (pci_enable_sriov(pdev, num_vf[func]) == 0)
4997 			dev_info(&pdev->dev,
4998 				 "instantiated %u virtual functions\n",
4999 				 num_vf[func]);
5000 	}
5001 
5002 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5003 	if (!adapter) {
5004 		err = -ENOMEM;
5005 		goto free_pci_region;
5006 	}
5007 
5008 	adapter->pdev = pdev;
5009 	adapter->pdev_dev = &pdev->dev;
5010 	adapter->name = pci_name(pdev);
5011 	adapter->mbox = func;
5012 	adapter->pf = func;
5013 	adapter->regs = regs;
5014 	adapter->adap_idx = adap_idx;
5015 	adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5016 				    (sizeof(struct mbox_cmd) *
5017 				     T4_OS_LOG_MBOX_CMDS),
5018 				    GFP_KERNEL);
5019 	if (!adapter->mbox_log) {
5020 		err = -ENOMEM;
5021 		goto free_adapter;
5022 	}
5023 	pci_set_drvdata(pdev, adapter);
5024 	return 0;
5025 
5026  free_adapter:
5027 	kfree(adapter);
5028  free_pci_region:
5029 	iounmap(regs);
5030 	pci_disable_sriov(pdev);
5031 	pci_release_regions(pdev);
5032 	return err;
5033 #else
5034 	return 0;
5035 #endif
5036 
5037  out_free_dev:
5038 	free_some_resources(adapter);
5039 	if (adapter->flags & USING_MSIX)
5040 		free_msix_info(adapter);
5041 	if (adapter->num_uld || adapter->num_ofld_uld)
5042 		t4_uld_mem_free(adapter);
5043  out_unmap_bar:
5044 	if (!is_t4(adapter->params.chip))
5045 		iounmap(adapter->bar2);
5046  out_free_adapter:
5047 	if (adapter->workq)
5048 		destroy_workqueue(adapter->workq);
5049 
5050 	kfree(adapter->mbox_log);
5051 	kfree(adapter);
5052  out_unmap_bar0:
5053 	iounmap(regs);
5054  out_disable_device:
5055 	pci_disable_pcie_error_reporting(pdev);
5056 	pci_disable_device(pdev);
5057  out_release_regions:
5058 	pci_release_regions(pdev);
5059 	return err;
5060 }
5061 
5062 static void remove_one(struct pci_dev *pdev)
5063 {
5064 	struct adapter *adapter = pci_get_drvdata(pdev);
5065 
5066 	if (!adapter) {
5067 		pci_release_regions(pdev);
5068 		return;
5069 	}
5070 
5071 	if (adapter->pf == 4) {
5072 		int i;
5073 
5074 		/* Tear down per-adapter Work Queue first since it can contain
5075 		 * references to our adapter data structure.
5076 		 */
5077 		destroy_workqueue(adapter->workq);
5078 
5079 		if (is_uld(adapter))
5080 			detach_ulds(adapter);
5081 
5082 		disable_interrupts(adapter);
5083 
5084 		for_each_port(adapter, i)
5085 			if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5086 				unregister_netdev(adapter->port[i]);
5087 
5088 		debugfs_remove_recursive(adapter->debugfs_root);
5089 
5090 		/* If we allocated filters, free up state associated with any
5091 		 * valid filters ...
5092 		 */
5093 		clear_all_filters(adapter);
5094 
5095 		if (adapter->flags & FULL_INIT_DONE)
5096 			cxgb_down(adapter);
5097 
5098 		if (adapter->flags & USING_MSIX)
5099 			free_msix_info(adapter);
5100 		if (adapter->num_uld || adapter->num_ofld_uld)
5101 			t4_uld_mem_free(adapter);
5102 		free_some_resources(adapter);
5103 #if IS_ENABLED(CONFIG_IPV6)
5104 		t4_cleanup_clip_tbl(adapter);
5105 #endif
5106 		iounmap(adapter->regs);
5107 		if (!is_t4(adapter->params.chip))
5108 			iounmap(adapter->bar2);
5109 		pci_disable_pcie_error_reporting(pdev);
5110 		if ((adapter->flags & DEV_ENABLED)) {
5111 			pci_disable_device(pdev);
5112 			adapter->flags &= ~DEV_ENABLED;
5113 		}
5114 		pci_release_regions(pdev);
5115 		kfree(adapter->mbox_log);
5116 		synchronize_rcu();
5117 		kfree(adapter);
5118 	}
5119 #ifdef CONFIG_PCI_IOV
5120 	else {
5121 		if (adapter->port[0])
5122 			unregister_netdev(adapter->port[0]);
5123 		iounmap(adapter->regs);
5124 		kfree(adapter->vfinfo);
5125 		kfree(adapter);
5126 		pci_disable_sriov(pdev);
5127 		pci_release_regions(pdev);
5128 	}
5129 #endif
5130 }
5131 
5132 /* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5133  * delivery.  This is essentially a stripped down version of the PCI remove()
5134  * function where we do the minimal amount of work necessary to shutdown any
5135  * further activity.
5136  */
5137 static void shutdown_one(struct pci_dev *pdev)
5138 {
5139 	struct adapter *adapter = pci_get_drvdata(pdev);
5140 
5141 	/* As with remove_one() above (see extended comment), we only want do
5142 	 * do cleanup on PCI Devices which went all the way through init_one()
5143 	 * ...
5144 	 */
5145 	if (!adapter) {
5146 		pci_release_regions(pdev);
5147 		return;
5148 	}
5149 
5150 	if (adapter->pf == 4) {
5151 		int i;
5152 
5153 		for_each_port(adapter, i)
5154 			if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5155 				cxgb_close(adapter->port[i]);
5156 
5157 		t4_uld_clean_up(adapter);
5158 		disable_interrupts(adapter);
5159 		disable_msi(adapter);
5160 
5161 		t4_sge_stop(adapter);
5162 		if (adapter->flags & FW_OK)
5163 			t4_fw_bye(adapter, adapter->mbox);
5164 	}
5165 #ifdef CONFIG_PCI_IOV
5166 	else {
5167 		if (adapter->port[0])
5168 			unregister_netdev(adapter->port[0]);
5169 		iounmap(adapter->regs);
5170 		kfree(adapter->vfinfo);
5171 		kfree(adapter);
5172 		pci_disable_sriov(pdev);
5173 		pci_release_regions(pdev);
5174 	}
5175 #endif
5176 }
5177 
5178 static struct pci_driver cxgb4_driver = {
5179 	.name     = KBUILD_MODNAME,
5180 	.id_table = cxgb4_pci_tbl,
5181 	.probe    = init_one,
5182 	.remove   = remove_one,
5183 	.shutdown = shutdown_one,
5184 #ifdef CONFIG_PCI_IOV
5185 	.sriov_configure = cxgb4_iov_configure,
5186 #endif
5187 	.err_handler = &cxgb4_eeh,
5188 };
5189 
5190 static int __init cxgb4_init_module(void)
5191 {
5192 	int ret;
5193 
5194 	/* Debugfs support is optional, just warn if this fails */
5195 	cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5196 	if (!cxgb4_debugfs_root)
5197 		pr_warn("could not create debugfs entry, continuing\n");
5198 
5199 	ret = pci_register_driver(&cxgb4_driver);
5200 	if (ret < 0)
5201 		debugfs_remove(cxgb4_debugfs_root);
5202 
5203 #if IS_ENABLED(CONFIG_IPV6)
5204 	if (!inet6addr_registered) {
5205 		register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5206 		inet6addr_registered = true;
5207 	}
5208 #endif
5209 
5210 	return ret;
5211 }
5212 
5213 static void __exit cxgb4_cleanup_module(void)
5214 {
5215 #if IS_ENABLED(CONFIG_IPV6)
5216 	if (inet6addr_registered) {
5217 		unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5218 		inet6addr_registered = false;
5219 	}
5220 #endif
5221 	pci_unregister_driver(&cxgb4_driver);
5222 	debugfs_remove(cxgb4_debugfs_root);  /* NULL ok */
5223 }
5224 
5225 module_init(cxgb4_init_module);
5226 module_exit(cxgb4_cleanup_module);
5227