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