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