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