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