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