xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c (revision c77cd47cee041bc1664b8e5fcd23036e5aab8e2a)
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 	if (tid_out_of_range(&adap->tids, tid)) {
1803 		dev_err(adap->pdev_dev, "tid %d out of range\n", tid);
1804 		return;
1805 	}
1806 
1807 	if (t->tid_tab[tid - adap->tids.tid_base]) {
1808 		t->tid_tab[tid - adap->tids.tid_base] = NULL;
1809 		atomic_dec(&t->conns_in_use);
1810 		if (t->hash_base && (tid >= t->hash_base)) {
1811 			if (family == AF_INET6)
1812 				atomic_sub(2, &t->hash_tids_in_use);
1813 			else
1814 				atomic_dec(&t->hash_tids_in_use);
1815 		} else {
1816 			if (family == AF_INET6)
1817 				atomic_sub(2, &t->tids_in_use);
1818 			else
1819 				atomic_dec(&t->tids_in_use);
1820 		}
1821 	}
1822 
1823 	skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1824 	if (likely(skb)) {
1825 		mk_tid_release(skb, chan, tid);
1826 		t4_ofld_send(adap, skb);
1827 	} else
1828 		cxgb4_queue_tid_release(t, chan, tid);
1829 }
1830 EXPORT_SYMBOL(cxgb4_remove_tid);
1831 
1832 /*
1833  * Allocate and initialize the TID tables.  Returns 0 on success.
1834  */
tid_init(struct tid_info * t)1835 static int tid_init(struct tid_info *t)
1836 {
1837 	struct adapter *adap = container_of(t, struct adapter, tids);
1838 	unsigned int max_ftids = t->nftids + t->nsftids;
1839 	unsigned int natids = t->natids;
1840 	unsigned int hpftid_bmap_size;
1841 	unsigned int eotid_bmap_size;
1842 	unsigned int stid_bmap_size;
1843 	unsigned int ftid_bmap_size;
1844 	size_t size;
1845 
1846 	stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
1847 	ftid_bmap_size = BITS_TO_LONGS(t->nftids);
1848 	hpftid_bmap_size = BITS_TO_LONGS(t->nhpftids);
1849 	eotid_bmap_size = BITS_TO_LONGS(t->neotids);
1850 	size = t->ntids * sizeof(*t->tid_tab) +
1851 	       natids * sizeof(*t->atid_tab) +
1852 	       t->nstids * sizeof(*t->stid_tab) +
1853 	       t->nsftids * sizeof(*t->stid_tab) +
1854 	       stid_bmap_size * sizeof(long) +
1855 	       t->nhpftids * sizeof(*t->hpftid_tab) +
1856 	       hpftid_bmap_size * sizeof(long) +
1857 	       max_ftids * sizeof(*t->ftid_tab) +
1858 	       ftid_bmap_size * sizeof(long) +
1859 	       t->neotids * sizeof(*t->eotid_tab) +
1860 	       eotid_bmap_size * sizeof(long);
1861 
1862 	t->tid_tab = kvzalloc(size, GFP_KERNEL);
1863 	if (!t->tid_tab)
1864 		return -ENOMEM;
1865 
1866 	t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1867 	t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
1868 	t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
1869 	t->hpftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
1870 	t->hpftid_bmap = (unsigned long *)&t->hpftid_tab[t->nhpftids];
1871 	t->ftid_tab = (struct filter_entry *)&t->hpftid_bmap[hpftid_bmap_size];
1872 	t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
1873 	t->eotid_tab = (struct eotid_entry *)&t->ftid_bmap[ftid_bmap_size];
1874 	t->eotid_bmap = (unsigned long *)&t->eotid_tab[t->neotids];
1875 	spin_lock_init(&t->stid_lock);
1876 	spin_lock_init(&t->atid_lock);
1877 	spin_lock_init(&t->ftid_lock);
1878 
1879 	t->stids_in_use = 0;
1880 	t->v6_stids_in_use = 0;
1881 	t->sftids_in_use = 0;
1882 	t->afree = NULL;
1883 	t->atids_in_use = 0;
1884 	atomic_set(&t->tids_in_use, 0);
1885 	atomic_set(&t->conns_in_use, 0);
1886 	atomic_set(&t->hash_tids_in_use, 0);
1887 	atomic_set(&t->eotids_in_use, 0);
1888 
1889 	/* Setup the free list for atid_tab and clear the stid bitmap. */
1890 	if (natids) {
1891 		while (--natids)
1892 			t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1893 		t->afree = t->atid_tab;
1894 	}
1895 
1896 	if (is_offload(adap)) {
1897 		bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1898 		/* Reserve stid 0 for T4/T5 adapters */
1899 		if (!t->stid_base &&
1900 		    CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1901 			__set_bit(0, t->stid_bmap);
1902 
1903 		if (t->neotids)
1904 			bitmap_zero(t->eotid_bmap, t->neotids);
1905 	}
1906 
1907 	if (t->nhpftids)
1908 		bitmap_zero(t->hpftid_bmap, t->nhpftids);
1909 	bitmap_zero(t->ftid_bmap, t->nftids);
1910 	return 0;
1911 }
1912 
1913 /**
1914  *	cxgb4_create_server - create an IP server
1915  *	@dev: the device
1916  *	@stid: the server TID
1917  *	@sip: local IP address to bind server to
1918  *	@sport: the server's TCP port
1919  *	@vlan: the VLAN header information
1920  *	@queue: queue to direct messages from this server to
1921  *
1922  *	Create an IP server for the given port and address.
1923  *	Returns <0 on error and one of the %NET_XMIT_* values on success.
1924  */
cxgb4_create_server(const struct net_device * dev,unsigned int stid,__be32 sip,__be16 sport,__be16 vlan,unsigned int queue)1925 int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
1926 			__be32 sip, __be16 sport, __be16 vlan,
1927 			unsigned int queue)
1928 {
1929 	unsigned int chan;
1930 	struct sk_buff *skb;
1931 	struct adapter *adap;
1932 	struct cpl_pass_open_req *req;
1933 	int ret;
1934 
1935 	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1936 	if (!skb)
1937 		return -ENOMEM;
1938 
1939 	adap = netdev2adap(dev);
1940 	req = __skb_put(skb, sizeof(*req));
1941 	INIT_TP_WR(req, 0);
1942 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1943 	req->local_port = sport;
1944 	req->peer_port = htons(0);
1945 	req->local_ip = sip;
1946 	req->peer_ip = htonl(0);
1947 	chan = rxq_to_chan(&adap->sge, queue);
1948 	req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
1949 	req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1950 				SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
1951 	ret = t4_mgmt_tx(adap, skb);
1952 	return net_xmit_eval(ret);
1953 }
1954 EXPORT_SYMBOL(cxgb4_create_server);
1955 
1956 /*	cxgb4_create_server6 - create an IPv6 server
1957  *	@dev: the device
1958  *	@stid: the server TID
1959  *	@sip: local IPv6 address to bind server to
1960  *	@sport: the server's TCP port
1961  *	@queue: queue to direct messages from this server to
1962  *
1963  *	Create an IPv6 server for the given port and address.
1964  *	Returns <0 on error and one of the %NET_XMIT_* values on success.
1965  */
cxgb4_create_server6(const struct net_device * dev,unsigned int stid,const struct in6_addr * sip,__be16 sport,unsigned int queue)1966 int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1967 			 const struct in6_addr *sip, __be16 sport,
1968 			 unsigned int queue)
1969 {
1970 	unsigned int chan;
1971 	struct sk_buff *skb;
1972 	struct adapter *adap;
1973 	struct cpl_pass_open_req6 *req;
1974 	int ret;
1975 
1976 	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1977 	if (!skb)
1978 		return -ENOMEM;
1979 
1980 	adap = netdev2adap(dev);
1981 	req = __skb_put(skb, sizeof(*req));
1982 	INIT_TP_WR(req, 0);
1983 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1984 	req->local_port = sport;
1985 	req->peer_port = htons(0);
1986 	req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1987 	req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1988 	req->peer_ip_hi = cpu_to_be64(0);
1989 	req->peer_ip_lo = cpu_to_be64(0);
1990 	chan = rxq_to_chan(&adap->sge, queue);
1991 	req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
1992 	req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1993 				SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
1994 	ret = t4_mgmt_tx(adap, skb);
1995 	return net_xmit_eval(ret);
1996 }
1997 EXPORT_SYMBOL(cxgb4_create_server6);
1998 
cxgb4_remove_server(const struct net_device * dev,unsigned int stid,unsigned int queue,bool ipv6)1999 int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
2000 			unsigned int queue, bool ipv6)
2001 {
2002 	struct sk_buff *skb;
2003 	struct adapter *adap;
2004 	struct cpl_close_listsvr_req *req;
2005 	int ret;
2006 
2007 	adap = netdev2adap(dev);
2008 
2009 	skb = alloc_skb(sizeof(*req), GFP_KERNEL);
2010 	if (!skb)
2011 		return -ENOMEM;
2012 
2013 	req = __skb_put(skb, sizeof(*req));
2014 	INIT_TP_WR(req, 0);
2015 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
2016 	req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
2017 				LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
2018 	ret = t4_mgmt_tx(adap, skb);
2019 	return net_xmit_eval(ret);
2020 }
2021 EXPORT_SYMBOL(cxgb4_remove_server);
2022 
2023 /**
2024  *	cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
2025  *	@mtus: the HW MTU table
2026  *	@mtu: the target MTU
2027  *	@idx: index of selected entry in the MTU table
2028  *
2029  *	Returns the index and the value in the HW MTU table that is closest to
2030  *	but does not exceed @mtu, unless @mtu is smaller than any value in the
2031  *	table, in which case that smallest available value is selected.
2032  */
cxgb4_best_mtu(const unsigned short * mtus,unsigned short mtu,unsigned int * idx)2033 unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
2034 			    unsigned int *idx)
2035 {
2036 	unsigned int i = 0;
2037 
2038 	while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
2039 		++i;
2040 	if (idx)
2041 		*idx = i;
2042 	return mtus[i];
2043 }
2044 EXPORT_SYMBOL(cxgb4_best_mtu);
2045 
2046 /**
2047  *     cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
2048  *     @mtus: the HW MTU table
2049  *     @header_size: Header Size
2050  *     @data_size_max: maximum Data Segment Size
2051  *     @data_size_align: desired Data Segment Size Alignment (2^N)
2052  *     @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
2053  *
2054  *     Similar to cxgb4_best_mtu() but instead of searching the Hardware
2055  *     MTU Table based solely on a Maximum MTU parameter, we break that
2056  *     parameter up into a Header Size and Maximum Data Segment Size, and
2057  *     provide a desired Data Segment Size Alignment.  If we find an MTU in
2058  *     the Hardware MTU Table which will result in a Data Segment Size with
2059  *     the requested alignment _and_ that MTU isn't "too far" from the
2060  *     closest MTU, then we'll return that rather than the closest MTU.
2061  */
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)2062 unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
2063 				    unsigned short header_size,
2064 				    unsigned short data_size_max,
2065 				    unsigned short data_size_align,
2066 				    unsigned int *mtu_idxp)
2067 {
2068 	unsigned short max_mtu = header_size + data_size_max;
2069 	unsigned short data_size_align_mask = data_size_align - 1;
2070 	int mtu_idx, aligned_mtu_idx;
2071 
2072 	/* Scan the MTU Table till we find an MTU which is larger than our
2073 	 * Maximum MTU or we reach the end of the table.  Along the way,
2074 	 * record the last MTU found, if any, which will result in a Data
2075 	 * Segment Length matching the requested alignment.
2076 	 */
2077 	for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
2078 		unsigned short data_size = mtus[mtu_idx] - header_size;
2079 
2080 		/* If this MTU minus the Header Size would result in a
2081 		 * Data Segment Size of the desired alignment, remember it.
2082 		 */
2083 		if ((data_size & data_size_align_mask) == 0)
2084 			aligned_mtu_idx = mtu_idx;
2085 
2086 		/* If we're not at the end of the Hardware MTU Table and the
2087 		 * next element is larger than our Maximum MTU, drop out of
2088 		 * the loop.
2089 		 */
2090 		if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
2091 			break;
2092 	}
2093 
2094 	/* If we fell out of the loop because we ran to the end of the table,
2095 	 * then we just have to use the last [largest] entry.
2096 	 */
2097 	if (mtu_idx == NMTUS)
2098 		mtu_idx--;
2099 
2100 	/* If we found an MTU which resulted in the requested Data Segment
2101 	 * Length alignment and that's "not far" from the largest MTU which is
2102 	 * less than or equal to the maximum MTU, then use that.
2103 	 */
2104 	if (aligned_mtu_idx >= 0 &&
2105 	    mtu_idx - aligned_mtu_idx <= 1)
2106 		mtu_idx = aligned_mtu_idx;
2107 
2108 	/* If the caller has passed in an MTU Index pointer, pass the
2109 	 * MTU Index back.  Return the MTU value.
2110 	 */
2111 	if (mtu_idxp)
2112 		*mtu_idxp = mtu_idx;
2113 	return mtus[mtu_idx];
2114 }
2115 EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
2116 
2117 /**
2118  *	cxgb4_port_chan - get the HW channel of a port
2119  *	@dev: the net device for the port
2120  *
2121  *	Return the HW Tx channel of the given port.
2122  */
cxgb4_port_chan(const struct net_device * dev)2123 unsigned int cxgb4_port_chan(const struct net_device *dev)
2124 {
2125 	return netdev2pinfo(dev)->tx_chan;
2126 }
2127 EXPORT_SYMBOL(cxgb4_port_chan);
2128 
2129 /**
2130  *      cxgb4_port_e2cchan - get the HW c-channel of a port
2131  *      @dev: the net device for the port
2132  *
2133  *      Return the HW RX c-channel of the given port.
2134  */
cxgb4_port_e2cchan(const struct net_device * dev)2135 unsigned int cxgb4_port_e2cchan(const struct net_device *dev)
2136 {
2137 	return netdev2pinfo(dev)->rx_cchan;
2138 }
2139 EXPORT_SYMBOL(cxgb4_port_e2cchan);
2140 
cxgb4_dbfifo_count(const struct net_device * dev,int lpfifo)2141 unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
2142 {
2143 	struct adapter *adap = netdev2adap(dev);
2144 	u32 v1, v2, lp_count, hp_count;
2145 
2146 	v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2147 	v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
2148 	if (is_t4(adap->params.chip)) {
2149 		lp_count = LP_COUNT_G(v1);
2150 		hp_count = HP_COUNT_G(v1);
2151 	} else {
2152 		lp_count = LP_COUNT_T5_G(v1);
2153 		hp_count = HP_COUNT_T5_G(v2);
2154 	}
2155 	return lpfifo ? lp_count : hp_count;
2156 }
2157 EXPORT_SYMBOL(cxgb4_dbfifo_count);
2158 
2159 /**
2160  *	cxgb4_port_viid - get the VI id of a port
2161  *	@dev: the net device for the port
2162  *
2163  *	Return the VI id of the given port.
2164  */
cxgb4_port_viid(const struct net_device * dev)2165 unsigned int cxgb4_port_viid(const struct net_device *dev)
2166 {
2167 	return netdev2pinfo(dev)->viid;
2168 }
2169 EXPORT_SYMBOL(cxgb4_port_viid);
2170 
2171 /**
2172  *	cxgb4_port_idx - get the index of a port
2173  *	@dev: the net device for the port
2174  *
2175  *	Return the index of the given port.
2176  */
cxgb4_port_idx(const struct net_device * dev)2177 unsigned int cxgb4_port_idx(const struct net_device *dev)
2178 {
2179 	return netdev2pinfo(dev)->port_id;
2180 }
2181 EXPORT_SYMBOL(cxgb4_port_idx);
2182 
cxgb4_get_tcp_stats(struct pci_dev * pdev,struct tp_tcp_stats * v4,struct tp_tcp_stats * v6)2183 void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
2184 			 struct tp_tcp_stats *v6)
2185 {
2186 	struct adapter *adap = pci_get_drvdata(pdev);
2187 
2188 	spin_lock(&adap->stats_lock);
2189 	t4_tp_get_tcp_stats(adap, v4, v6, false);
2190 	spin_unlock(&adap->stats_lock);
2191 }
2192 EXPORT_SYMBOL(cxgb4_get_tcp_stats);
2193 
cxgb4_flush_eq_cache(struct net_device * dev)2194 int cxgb4_flush_eq_cache(struct net_device *dev)
2195 {
2196 	struct adapter *adap = netdev2adap(dev);
2197 
2198 	return t4_sge_ctxt_flush(adap, adap->mbox, CTXT_EGRESS);
2199 }
2200 EXPORT_SYMBOL(cxgb4_flush_eq_cache);
2201 
read_eq_indices(struct adapter * adap,u16 qid,u16 * pidx,u16 * cidx)2202 static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
2203 {
2204 	u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
2205 	__be64 indices;
2206 	int ret;
2207 
2208 	spin_lock(&adap->win0_lock);
2209 	ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
2210 			   sizeof(indices), (__be32 *)&indices,
2211 			   T4_MEMORY_READ);
2212 	spin_unlock(&adap->win0_lock);
2213 	if (!ret) {
2214 		*cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
2215 		*pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
2216 	}
2217 	return ret;
2218 }
2219 
cxgb4_sync_txq_pidx(struct net_device * dev,u16 qid,u16 pidx,u16 size)2220 int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
2221 			u16 size)
2222 {
2223 	struct adapter *adap = netdev2adap(dev);
2224 	u16 hw_pidx, hw_cidx;
2225 	int ret;
2226 
2227 	ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
2228 	if (ret)
2229 		goto out;
2230 
2231 	if (pidx != hw_pidx) {
2232 		u16 delta;
2233 		u32 val;
2234 
2235 		if (pidx >= hw_pidx)
2236 			delta = pidx - hw_pidx;
2237 		else
2238 			delta = size - hw_pidx + pidx;
2239 
2240 		if (is_t4(adap->params.chip))
2241 			val = PIDX_V(delta);
2242 		else
2243 			val = PIDX_T5_V(delta);
2244 		wmb();
2245 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2246 			     QID_V(qid) | val);
2247 	}
2248 out:
2249 	return ret;
2250 }
2251 EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
2252 
cxgb4_read_tpte(struct net_device * dev,u32 stag,__be32 * tpte)2253 int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
2254 {
2255 	u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
2256 	u32 edc0_end, edc1_end, mc0_end, mc1_end;
2257 	u32 offset, memtype, memaddr;
2258 	struct adapter *adap;
2259 	u32 hma_size = 0;
2260 	int ret;
2261 
2262 	adap = netdev2adap(dev);
2263 
2264 	offset = ((stag >> 8) * 32) + adap->vres.stag.start;
2265 
2266 	/* Figure out where the offset lands in the Memory Type/Address scheme.
2267 	 * This code assumes that the memory is laid out starting at offset 0
2268 	 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
2269 	 * and EDC1.  Some cards will have neither MC0 nor MC1, most cards have
2270 	 * MC0, and some have both MC0 and MC1.
2271 	 */
2272 	size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
2273 	edc0_size = EDRAM0_SIZE_G(size) << 20;
2274 	size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
2275 	edc1_size = EDRAM1_SIZE_G(size) << 20;
2276 	size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
2277 	mc0_size = EXT_MEM0_SIZE_G(size) << 20;
2278 
2279 	if (t4_read_reg(adap, MA_TARGET_MEM_ENABLE_A) & HMA_MUX_F) {
2280 		size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2281 		hma_size = EXT_MEM1_SIZE_G(size) << 20;
2282 	}
2283 	edc0_end = edc0_size;
2284 	edc1_end = edc0_end + edc1_size;
2285 	mc0_end = edc1_end + mc0_size;
2286 
2287 	if (offset < edc0_end) {
2288 		memtype = MEM_EDC0;
2289 		memaddr = offset;
2290 	} else if (offset < edc1_end) {
2291 		memtype = MEM_EDC1;
2292 		memaddr = offset - edc0_end;
2293 	} else {
2294 		if (hma_size && (offset < (edc1_end + hma_size))) {
2295 			memtype = MEM_HMA;
2296 			memaddr = offset - edc1_end;
2297 		} else if (offset < mc0_end) {
2298 			memtype = MEM_MC0;
2299 			memaddr = offset - edc1_end;
2300 		} else if (is_t5(adap->params.chip)) {
2301 			size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2302 			mc1_size = EXT_MEM1_SIZE_G(size) << 20;
2303 			mc1_end = mc0_end + mc1_size;
2304 			if (offset < mc1_end) {
2305 				memtype = MEM_MC1;
2306 				memaddr = offset - mc0_end;
2307 			} else {
2308 				/* offset beyond the end of any memory */
2309 				goto err;
2310 			}
2311 		} else {
2312 			/* T4/T6 only has a single memory channel */
2313 			goto err;
2314 		}
2315 	}
2316 
2317 	spin_lock(&adap->win0_lock);
2318 	ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
2319 	spin_unlock(&adap->win0_lock);
2320 	return ret;
2321 
2322 err:
2323 	dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
2324 		stag, offset);
2325 	return -EINVAL;
2326 }
2327 EXPORT_SYMBOL(cxgb4_read_tpte);
2328 
cxgb4_read_sge_timestamp(struct net_device * dev)2329 u64 cxgb4_read_sge_timestamp(struct net_device *dev)
2330 {
2331 	u32 hi, lo;
2332 	struct adapter *adap;
2333 
2334 	adap = netdev2adap(dev);
2335 	lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
2336 	hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
2337 
2338 	return ((u64)hi << 32) | (u64)lo;
2339 }
2340 EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
2341 
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)2342 int cxgb4_bar2_sge_qregs(struct net_device *dev,
2343 			 unsigned int qid,
2344 			 enum cxgb4_bar2_qtype qtype,
2345 			 int user,
2346 			 u64 *pbar2_qoffset,
2347 			 unsigned int *pbar2_qid)
2348 {
2349 	return t4_bar2_sge_qregs(netdev2adap(dev),
2350 				 qid,
2351 				 (qtype == CXGB4_BAR2_QTYPE_EGRESS
2352 				  ? T4_BAR2_QTYPE_EGRESS
2353 				  : T4_BAR2_QTYPE_INGRESS),
2354 				 user,
2355 				 pbar2_qoffset,
2356 				 pbar2_qid);
2357 }
2358 EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
2359 
2360 static struct pci_driver cxgb4_driver;
2361 
check_neigh_update(struct neighbour * neigh)2362 static void check_neigh_update(struct neighbour *neigh)
2363 {
2364 	const struct device *parent;
2365 	const struct net_device *netdev = neigh->dev;
2366 
2367 	if (is_vlan_dev(netdev))
2368 		netdev = vlan_dev_real_dev(netdev);
2369 	parent = netdev->dev.parent;
2370 	if (parent && parent->driver == &cxgb4_driver.driver)
2371 		t4_l2t_update(dev_get_drvdata(parent), neigh);
2372 }
2373 
netevent_cb(struct notifier_block * nb,unsigned long event,void * data)2374 static int netevent_cb(struct notifier_block *nb, unsigned long event,
2375 		       void *data)
2376 {
2377 	switch (event) {
2378 	case NETEVENT_NEIGH_UPDATE:
2379 		check_neigh_update(data);
2380 		break;
2381 	case NETEVENT_REDIRECT:
2382 	default:
2383 		break;
2384 	}
2385 	return 0;
2386 }
2387 
2388 static bool netevent_registered;
2389 static struct notifier_block cxgb4_netevent_nb = {
2390 	.notifier_call = netevent_cb
2391 };
2392 
drain_db_fifo(struct adapter * adap,int usecs)2393 static void drain_db_fifo(struct adapter *adap, int usecs)
2394 {
2395 	u32 v1, v2, lp_count, hp_count;
2396 
2397 	do {
2398 		v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2399 		v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
2400 		if (is_t4(adap->params.chip)) {
2401 			lp_count = LP_COUNT_G(v1);
2402 			hp_count = HP_COUNT_G(v1);
2403 		} else {
2404 			lp_count = LP_COUNT_T5_G(v1);
2405 			hp_count = HP_COUNT_T5_G(v2);
2406 		}
2407 
2408 		if (lp_count == 0 && hp_count == 0)
2409 			break;
2410 		set_current_state(TASK_UNINTERRUPTIBLE);
2411 		schedule_timeout(usecs_to_jiffies(usecs));
2412 	} while (1);
2413 }
2414 
disable_txq_db(struct sge_txq * q)2415 static void disable_txq_db(struct sge_txq *q)
2416 {
2417 	unsigned long flags;
2418 
2419 	spin_lock_irqsave(&q->db_lock, flags);
2420 	q->db_disabled = 1;
2421 	spin_unlock_irqrestore(&q->db_lock, flags);
2422 }
2423 
enable_txq_db(struct adapter * adap,struct sge_txq * q)2424 static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
2425 {
2426 	spin_lock_irq(&q->db_lock);
2427 	if (q->db_pidx_inc) {
2428 		/* Make sure that all writes to the TX descriptors
2429 		 * are committed before we tell HW about them.
2430 		 */
2431 		wmb();
2432 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2433 			     QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
2434 		q->db_pidx_inc = 0;
2435 	}
2436 	q->db_disabled = 0;
2437 	spin_unlock_irq(&q->db_lock);
2438 }
2439 
disable_dbs(struct adapter * adap)2440 static void disable_dbs(struct adapter *adap)
2441 {
2442 	int i;
2443 
2444 	for_each_ethrxq(&adap->sge, i)
2445 		disable_txq_db(&adap->sge.ethtxq[i].q);
2446 	if (is_offload(adap)) {
2447 		struct sge_uld_txq_info *txq_info =
2448 			adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2449 
2450 		if (txq_info) {
2451 			for_each_ofldtxq(&adap->sge, i) {
2452 				struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2453 
2454 				disable_txq_db(&txq->q);
2455 			}
2456 		}
2457 	}
2458 	for_each_port(adap, i)
2459 		disable_txq_db(&adap->sge.ctrlq[i].q);
2460 }
2461 
enable_dbs(struct adapter * adap)2462 static void enable_dbs(struct adapter *adap)
2463 {
2464 	int i;
2465 
2466 	for_each_ethrxq(&adap->sge, i)
2467 		enable_txq_db(adap, &adap->sge.ethtxq[i].q);
2468 	if (is_offload(adap)) {
2469 		struct sge_uld_txq_info *txq_info =
2470 			adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2471 
2472 		if (txq_info) {
2473 			for_each_ofldtxq(&adap->sge, i) {
2474 				struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2475 
2476 				enable_txq_db(adap, &txq->q);
2477 			}
2478 		}
2479 	}
2480 	for_each_port(adap, i)
2481 		enable_txq_db(adap, &adap->sge.ctrlq[i].q);
2482 }
2483 
notify_rdma_uld(struct adapter * adap,enum cxgb4_control cmd)2484 static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
2485 {
2486 	enum cxgb4_uld type = CXGB4_ULD_RDMA;
2487 
2488 	if (adap->uld && adap->uld[type].handle)
2489 		adap->uld[type].control(adap->uld[type].handle, cmd);
2490 }
2491 
process_db_full(struct work_struct * work)2492 static void process_db_full(struct work_struct *work)
2493 {
2494 	struct adapter *adap;
2495 
2496 	adap = container_of(work, struct adapter, db_full_task);
2497 
2498 	drain_db_fifo(adap, dbfifo_drain_delay);
2499 	enable_dbs(adap);
2500 	notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
2501 	if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2502 		t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2503 				 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
2504 				 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
2505 	else
2506 		t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2507 				 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
2508 }
2509 
sync_txq_pidx(struct adapter * adap,struct sge_txq * q)2510 static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
2511 {
2512 	u16 hw_pidx, hw_cidx;
2513 	int ret;
2514 
2515 	spin_lock_irq(&q->db_lock);
2516 	ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2517 	if (ret)
2518 		goto out;
2519 	if (q->db_pidx != hw_pidx) {
2520 		u16 delta;
2521 		u32 val;
2522 
2523 		if (q->db_pidx >= hw_pidx)
2524 			delta = q->db_pidx - hw_pidx;
2525 		else
2526 			delta = q->size - hw_pidx + q->db_pidx;
2527 
2528 		if (is_t4(adap->params.chip))
2529 			val = PIDX_V(delta);
2530 		else
2531 			val = PIDX_T5_V(delta);
2532 		wmb();
2533 		t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2534 			     QID_V(q->cntxt_id) | val);
2535 	}
2536 out:
2537 	q->db_disabled = 0;
2538 	q->db_pidx_inc = 0;
2539 	spin_unlock_irq(&q->db_lock);
2540 	if (ret)
2541 		CH_WARN(adap, "DB drop recovery failed.\n");
2542 }
2543 
recover_all_queues(struct adapter * adap)2544 static void recover_all_queues(struct adapter *adap)
2545 {
2546 	int i;
2547 
2548 	for_each_ethrxq(&adap->sge, i)
2549 		sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
2550 	if (is_offload(adap)) {
2551 		struct sge_uld_txq_info *txq_info =
2552 			adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2553 		if (txq_info) {
2554 			for_each_ofldtxq(&adap->sge, i) {
2555 				struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2556 
2557 				sync_txq_pidx(adap, &txq->q);
2558 			}
2559 		}
2560 	}
2561 	for_each_port(adap, i)
2562 		sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2563 }
2564 
process_db_drop(struct work_struct * work)2565 static void process_db_drop(struct work_struct *work)
2566 {
2567 	struct adapter *adap;
2568 
2569 	adap = container_of(work, struct adapter, db_drop_task);
2570 
2571 	if (is_t4(adap->params.chip)) {
2572 		drain_db_fifo(adap, dbfifo_drain_delay);
2573 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
2574 		drain_db_fifo(adap, dbfifo_drain_delay);
2575 		recover_all_queues(adap);
2576 		drain_db_fifo(adap, dbfifo_drain_delay);
2577 		enable_dbs(adap);
2578 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
2579 	} else if (is_t5(adap->params.chip)) {
2580 		u32 dropped_db = t4_read_reg(adap, 0x010ac);
2581 		u16 qid = (dropped_db >> 15) & 0x1ffff;
2582 		u16 pidx_inc = dropped_db & 0x1fff;
2583 		u64 bar2_qoffset;
2584 		unsigned int bar2_qid;
2585 		int ret;
2586 
2587 		ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
2588 					0, &bar2_qoffset, &bar2_qid);
2589 		if (ret)
2590 			dev_err(adap->pdev_dev, "doorbell drop recovery: "
2591 				"qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2592 		else
2593 			writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
2594 			       adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
2595 
2596 		/* Re-enable BAR2 WC */
2597 		t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2598 	}
2599 
2600 	if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2601 		t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
2602 }
2603 
t4_db_full(struct adapter * adap)2604 void t4_db_full(struct adapter *adap)
2605 {
2606 	if (is_t4(adap->params.chip)) {
2607 		disable_dbs(adap);
2608 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2609 		t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2610 				 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
2611 		queue_work(adap->workq, &adap->db_full_task);
2612 	}
2613 }
2614 
t4_db_dropped(struct adapter * adap)2615 void t4_db_dropped(struct adapter *adap)
2616 {
2617 	if (is_t4(adap->params.chip)) {
2618 		disable_dbs(adap);
2619 		notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2620 	}
2621 	queue_work(adap->workq, &adap->db_drop_task);
2622 }
2623 
t4_register_netevent_notifier(void)2624 void t4_register_netevent_notifier(void)
2625 {
2626 	if (!netevent_registered) {
2627 		register_netevent_notifier(&cxgb4_netevent_nb);
2628 		netevent_registered = true;
2629 	}
2630 }
2631 
detach_ulds(struct adapter * adap)2632 static void detach_ulds(struct adapter *adap)
2633 {
2634 	unsigned int i;
2635 
2636 	if (!is_uld(adap))
2637 		return;
2638 
2639 	mutex_lock(&uld_mutex);
2640 	list_del(&adap->list_node);
2641 
2642 	for (i = 0; i < CXGB4_ULD_MAX; i++)
2643 		if (adap->uld && adap->uld[i].handle)
2644 			adap->uld[i].state_change(adap->uld[i].handle,
2645 					     CXGB4_STATE_DETACH);
2646 
2647 	if (netevent_registered && list_empty(&adapter_list)) {
2648 		unregister_netevent_notifier(&cxgb4_netevent_nb);
2649 		netevent_registered = false;
2650 	}
2651 	mutex_unlock(&uld_mutex);
2652 }
2653 
notify_ulds(struct adapter * adap,enum cxgb4_state new_state)2654 static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2655 {
2656 	unsigned int i;
2657 
2658 	mutex_lock(&uld_mutex);
2659 	for (i = 0; i < CXGB4_ULD_MAX; i++)
2660 		if (adap->uld && adap->uld[i].handle)
2661 			adap->uld[i].state_change(adap->uld[i].handle,
2662 						  new_state);
2663 	mutex_unlock(&uld_mutex);
2664 }
2665 
2666 #if IS_ENABLED(CONFIG_IPV6)
cxgb4_inet6addr_handler(struct notifier_block * this,unsigned long event,void * data)2667 static int cxgb4_inet6addr_handler(struct notifier_block *this,
2668 				   unsigned long event, void *data)
2669 {
2670 	struct inet6_ifaddr *ifa = data;
2671 	struct net_device *event_dev = ifa->idev->dev;
2672 	const struct device *parent = NULL;
2673 #if IS_ENABLED(CONFIG_BONDING)
2674 	struct adapter *adap;
2675 #endif
2676 	if (is_vlan_dev(event_dev))
2677 		event_dev = vlan_dev_real_dev(event_dev);
2678 #if IS_ENABLED(CONFIG_BONDING)
2679 	if (event_dev->flags & IFF_MASTER) {
2680 		list_for_each_entry(adap, &adapter_list, list_node) {
2681 			switch (event) {
2682 			case NETDEV_UP:
2683 				cxgb4_clip_get(adap->port[0],
2684 					       (const u32 *)ifa, 1);
2685 				break;
2686 			case NETDEV_DOWN:
2687 				cxgb4_clip_release(adap->port[0],
2688 						   (const u32 *)ifa, 1);
2689 				break;
2690 			default:
2691 				break;
2692 			}
2693 		}
2694 		return NOTIFY_OK;
2695 	}
2696 #endif
2697 
2698 	if (event_dev)
2699 		parent = event_dev->dev.parent;
2700 
2701 	if (parent && parent->driver == &cxgb4_driver.driver) {
2702 		switch (event) {
2703 		case NETDEV_UP:
2704 			cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
2705 			break;
2706 		case NETDEV_DOWN:
2707 			cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
2708 			break;
2709 		default:
2710 			break;
2711 		}
2712 	}
2713 	return NOTIFY_OK;
2714 }
2715 
2716 static bool inet6addr_registered;
2717 static struct notifier_block cxgb4_inet6addr_notifier = {
2718 	.notifier_call = cxgb4_inet6addr_handler
2719 };
2720 
update_clip(const struct adapter * adap)2721 static void update_clip(const struct adapter *adap)
2722 {
2723 	int i;
2724 	struct net_device *dev;
2725 	int ret;
2726 
2727 	rcu_read_lock();
2728 
2729 	for (i = 0; i < MAX_NPORTS; i++) {
2730 		dev = adap->port[i];
2731 		ret = 0;
2732 
2733 		if (dev)
2734 			ret = cxgb4_update_root_dev_clip(dev);
2735 
2736 		if (ret < 0)
2737 			break;
2738 	}
2739 	rcu_read_unlock();
2740 }
2741 #endif /* IS_ENABLED(CONFIG_IPV6) */
2742 
2743 /**
2744  *	cxgb_up - enable the adapter
2745  *	@adap: adapter being enabled
2746  *
2747  *	Called when the first port is enabled, this function performs the
2748  *	actions necessary to make an adapter operational, such as completing
2749  *	the initialization of HW modules, and enabling interrupts.
2750  *
2751  *	Must be called with the rtnl lock held.
2752  */
cxgb_up(struct adapter * adap)2753 static int cxgb_up(struct adapter *adap)
2754 {
2755 	struct sge *s = &adap->sge;
2756 	int err;
2757 
2758 	mutex_lock(&uld_mutex);
2759 	err = setup_sge_queues(adap);
2760 	if (err)
2761 		goto rel_lock;
2762 	err = setup_rss(adap);
2763 	if (err)
2764 		goto freeq;
2765 
2766 	if (adap->flags & CXGB4_USING_MSIX) {
2767 		if (s->nd_msix_idx < 0) {
2768 			err = -ENOMEM;
2769 			goto irq_err;
2770 		}
2771 
2772 		err = request_irq(adap->msix_info[s->nd_msix_idx].vec,
2773 				  t4_nondata_intr, 0,
2774 				  adap->msix_info[s->nd_msix_idx].desc, adap);
2775 		if (err)
2776 			goto irq_err;
2777 
2778 		err = request_msix_queue_irqs(adap);
2779 		if (err)
2780 			goto irq_err_free_nd_msix;
2781 	} else {
2782 		err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2783 				  (adap->flags & CXGB4_USING_MSI) ? 0
2784 								  : IRQF_SHARED,
2785 				  adap->port[0]->name, adap);
2786 		if (err)
2787 			goto irq_err;
2788 	}
2789 
2790 	enable_rx(adap);
2791 	t4_sge_start(adap);
2792 	t4_intr_enable(adap);
2793 	adap->flags |= CXGB4_FULL_INIT_DONE;
2794 	mutex_unlock(&uld_mutex);
2795 
2796 	notify_ulds(adap, CXGB4_STATE_UP);
2797 #if IS_ENABLED(CONFIG_IPV6)
2798 	update_clip(adap);
2799 #endif
2800 	return err;
2801 
2802 irq_err_free_nd_msix:
2803 	free_irq(adap->msix_info[s->nd_msix_idx].vec, adap);
2804 irq_err:
2805 	dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
2806 freeq:
2807 	t4_free_sge_resources(adap);
2808 rel_lock:
2809 	mutex_unlock(&uld_mutex);
2810 	return err;
2811 }
2812 
cxgb_down(struct adapter * adapter)2813 static void cxgb_down(struct adapter *adapter)
2814 {
2815 	cancel_work_sync(&adapter->tid_release_task);
2816 	cancel_work_sync(&adapter->db_full_task);
2817 	cancel_work_sync(&adapter->db_drop_task);
2818 	adapter->tid_release_task_busy = false;
2819 	adapter->tid_release_head = NULL;
2820 
2821 	t4_sge_stop(adapter);
2822 	t4_free_sge_resources(adapter);
2823 
2824 	adapter->flags &= ~CXGB4_FULL_INIT_DONE;
2825 }
2826 
2827 /*
2828  * net_device operations
2829  */
cxgb_open(struct net_device * dev)2830 static int cxgb_open(struct net_device *dev)
2831 {
2832 	struct port_info *pi = netdev_priv(dev);
2833 	struct adapter *adapter = pi->adapter;
2834 	int err;
2835 
2836 	netif_carrier_off(dev);
2837 
2838 	if (!(adapter->flags & CXGB4_FULL_INIT_DONE)) {
2839 		err = cxgb_up(adapter);
2840 		if (err < 0)
2841 			return err;
2842 	}
2843 
2844 	/* It's possible that the basic port information could have
2845 	 * changed since we first read it.
2846 	 */
2847 	err = t4_update_port_info(pi);
2848 	if (err < 0)
2849 		return err;
2850 
2851 	err = link_start(dev);
2852 	if (err)
2853 		return err;
2854 
2855 	if (pi->nmirrorqsets) {
2856 		mutex_lock(&pi->vi_mirror_mutex);
2857 		err = cxgb4_port_mirror_alloc_queues(dev);
2858 		if (err)
2859 			goto out_unlock;
2860 
2861 		err = cxgb4_port_mirror_start(dev);
2862 		if (err)
2863 			goto out_free_queues;
2864 		mutex_unlock(&pi->vi_mirror_mutex);
2865 	}
2866 
2867 	netif_tx_start_all_queues(dev);
2868 	return 0;
2869 
2870 out_free_queues:
2871 	cxgb4_port_mirror_free_queues(dev);
2872 
2873 out_unlock:
2874 	mutex_unlock(&pi->vi_mirror_mutex);
2875 	return err;
2876 }
2877 
cxgb_close(struct net_device * dev)2878 static int cxgb_close(struct net_device *dev)
2879 {
2880 	struct port_info *pi = netdev_priv(dev);
2881 	struct adapter *adapter = pi->adapter;
2882 	int ret;
2883 
2884 	netif_tx_stop_all_queues(dev);
2885 	netif_carrier_off(dev);
2886 	ret = t4_enable_pi_params(adapter, adapter->pf, pi,
2887 				  false, false, false);
2888 #ifdef CONFIG_CHELSIO_T4_DCB
2889 	cxgb4_dcb_reset(dev);
2890 	dcb_tx_queue_prio_enable(dev, false);
2891 #endif
2892 	if (ret)
2893 		return ret;
2894 
2895 	if (pi->nmirrorqsets) {
2896 		mutex_lock(&pi->vi_mirror_mutex);
2897 		cxgb4_port_mirror_stop(dev);
2898 		cxgb4_port_mirror_free_queues(dev);
2899 		mutex_unlock(&pi->vi_mirror_mutex);
2900 	}
2901 
2902 	return 0;
2903 }
2904 
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)2905 int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
2906 		__be32 sip, __be16 sport, __be16 vlan,
2907 		unsigned int queue, unsigned char port, unsigned char mask)
2908 {
2909 	int ret;
2910 	struct filter_entry *f;
2911 	struct adapter *adap;
2912 	int i;
2913 	u8 *val;
2914 
2915 	adap = netdev2adap(dev);
2916 
2917 	/* Adjust stid to correct filter index */
2918 	stid -= adap->tids.sftid_base;
2919 	stid += adap->tids.nftids;
2920 
2921 	/* Check to make sure the filter requested is writable ...
2922 	 */
2923 	f = &adap->tids.ftid_tab[stid];
2924 	ret = writable_filter(f);
2925 	if (ret)
2926 		return ret;
2927 
2928 	/* Clear out any old resources being used by the filter before
2929 	 * we start constructing the new filter.
2930 	 */
2931 	if (f->valid)
2932 		clear_filter(adap, f);
2933 
2934 	/* Clear out filter specifications */
2935 	memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2936 	f->fs.val.lport = be16_to_cpu(sport);
2937 	f->fs.mask.lport  = ~0;
2938 	val = (u8 *)&sip;
2939 	if ((val[0] | val[1] | val[2] | val[3]) != 0) {
2940 		for (i = 0; i < 4; i++) {
2941 			f->fs.val.lip[i] = val[i];
2942 			f->fs.mask.lip[i] = ~0;
2943 		}
2944 		if (adap->params.tp.vlan_pri_map & PORT_F) {
2945 			f->fs.val.iport = port;
2946 			f->fs.mask.iport = mask;
2947 		}
2948 	}
2949 
2950 	if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
2951 		f->fs.val.proto = IPPROTO_TCP;
2952 		f->fs.mask.proto = ~0;
2953 	}
2954 
2955 	f->fs.dirsteer = 1;
2956 	f->fs.iq = queue;
2957 	/* Mark filter as locked */
2958 	f->locked = 1;
2959 	f->fs.rpttid = 1;
2960 
2961 	/* Save the actual tid. We need this to get the corresponding
2962 	 * filter entry structure in filter_rpl.
2963 	 */
2964 	f->tid = stid + adap->tids.ftid_base;
2965 	ret = set_filter_wr(adap, stid);
2966 	if (ret) {
2967 		clear_filter(adap, f);
2968 		return ret;
2969 	}
2970 
2971 	return 0;
2972 }
2973 EXPORT_SYMBOL(cxgb4_create_server_filter);
2974 
cxgb4_remove_server_filter(const struct net_device * dev,unsigned int stid,unsigned int queue,bool ipv6)2975 int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2976 		unsigned int queue, bool ipv6)
2977 {
2978 	struct filter_entry *f;
2979 	struct adapter *adap;
2980 
2981 	adap = netdev2adap(dev);
2982 
2983 	/* Adjust stid to correct filter index */
2984 	stid -= adap->tids.sftid_base;
2985 	stid += adap->tids.nftids;
2986 
2987 	f = &adap->tids.ftid_tab[stid];
2988 	/* Unlock the filter */
2989 	f->locked = 0;
2990 
2991 	return delete_filter(adap, stid);
2992 }
2993 EXPORT_SYMBOL(cxgb4_remove_server_filter);
2994 
cxgb_get_stats(struct net_device * dev,struct rtnl_link_stats64 * ns)2995 static void cxgb_get_stats(struct net_device *dev,
2996 			   struct rtnl_link_stats64 *ns)
2997 {
2998 	struct port_stats stats;
2999 	struct port_info *p = netdev_priv(dev);
3000 	struct adapter *adapter = p->adapter;
3001 
3002 	/* Block retrieving statistics during EEH error
3003 	 * recovery. Otherwise, the recovery might fail
3004 	 * and the PCI device will be removed permanently
3005 	 */
3006 	spin_lock(&adapter->stats_lock);
3007 	if (!netif_device_present(dev)) {
3008 		spin_unlock(&adapter->stats_lock);
3009 		return;
3010 	}
3011 	t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
3012 				 &p->stats_base);
3013 	spin_unlock(&adapter->stats_lock);
3014 
3015 	ns->tx_bytes   = stats.tx_octets;
3016 	ns->tx_packets = stats.tx_frames;
3017 	ns->rx_bytes   = stats.rx_octets;
3018 	ns->rx_packets = stats.rx_frames;
3019 	ns->multicast  = stats.rx_mcast_frames;
3020 
3021 	/* detailed rx_errors */
3022 	ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
3023 			       stats.rx_runt;
3024 	ns->rx_over_errors   = 0;
3025 	ns->rx_crc_errors    = stats.rx_fcs_err;
3026 	ns->rx_frame_errors  = stats.rx_symbol_err;
3027 	ns->rx_dropped	     = stats.rx_ovflow0 + stats.rx_ovflow1 +
3028 			       stats.rx_ovflow2 + stats.rx_ovflow3 +
3029 			       stats.rx_trunc0 + stats.rx_trunc1 +
3030 			       stats.rx_trunc2 + stats.rx_trunc3;
3031 	ns->rx_missed_errors = 0;
3032 
3033 	/* detailed tx_errors */
3034 	ns->tx_aborted_errors   = 0;
3035 	ns->tx_carrier_errors   = 0;
3036 	ns->tx_fifo_errors      = 0;
3037 	ns->tx_heartbeat_errors = 0;
3038 	ns->tx_window_errors    = 0;
3039 
3040 	ns->tx_errors = stats.tx_error_frames;
3041 	ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
3042 		ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
3043 }
3044 
cxgb_ioctl(struct net_device * dev,struct ifreq * req,int cmd)3045 static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
3046 {
3047 	unsigned int mbox;
3048 	int ret = 0, prtad, devad;
3049 	struct port_info *pi = netdev_priv(dev);
3050 	struct adapter *adapter = pi->adapter;
3051 	struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
3052 
3053 	switch (cmd) {
3054 	case SIOCGMIIPHY:
3055 		if (pi->mdio_addr < 0)
3056 			return -EOPNOTSUPP;
3057 		data->phy_id = pi->mdio_addr;
3058 		break;
3059 	case SIOCGMIIREG:
3060 	case SIOCSMIIREG:
3061 		if (mdio_phy_id_is_c45(data->phy_id)) {
3062 			prtad = mdio_phy_id_prtad(data->phy_id);
3063 			devad = mdio_phy_id_devad(data->phy_id);
3064 		} else if (data->phy_id < 32) {
3065 			prtad = data->phy_id;
3066 			devad = 0;
3067 			data->reg_num &= 0x1f;
3068 		} else
3069 			return -EINVAL;
3070 
3071 		mbox = pi->adapter->pf;
3072 		if (cmd == SIOCGMIIREG)
3073 			ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
3074 					 data->reg_num, &data->val_out);
3075 		else
3076 			ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
3077 					 data->reg_num, data->val_in);
3078 		break;
3079 	case SIOCGHWTSTAMP:
3080 		return copy_to_user(req->ifr_data, &pi->tstamp_config,
3081 				    sizeof(pi->tstamp_config)) ?
3082 			-EFAULT : 0;
3083 	case SIOCSHWTSTAMP:
3084 		if (copy_from_user(&pi->tstamp_config, req->ifr_data,
3085 				   sizeof(pi->tstamp_config)))
3086 			return -EFAULT;
3087 
3088 		if (!is_t4(adapter->params.chip)) {
3089 			switch (pi->tstamp_config.tx_type) {
3090 			case HWTSTAMP_TX_OFF:
3091 			case HWTSTAMP_TX_ON:
3092 				break;
3093 			default:
3094 				return -ERANGE;
3095 			}
3096 
3097 			switch (pi->tstamp_config.rx_filter) {
3098 			case HWTSTAMP_FILTER_NONE:
3099 				pi->rxtstamp = false;
3100 				break;
3101 			case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3102 			case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3103 				cxgb4_ptprx_timestamping(pi, pi->port_id,
3104 							 PTP_TS_L4);
3105 				break;
3106 			case HWTSTAMP_FILTER_PTP_V2_EVENT:
3107 				cxgb4_ptprx_timestamping(pi, pi->port_id,
3108 							 PTP_TS_L2_L4);
3109 				break;
3110 			case HWTSTAMP_FILTER_ALL:
3111 			case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3112 			case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3113 			case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3114 			case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3115 				pi->rxtstamp = true;
3116 				break;
3117 			default:
3118 				pi->tstamp_config.rx_filter =
3119 					HWTSTAMP_FILTER_NONE;
3120 				return -ERANGE;
3121 			}
3122 
3123 			if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
3124 			    (pi->tstamp_config.rx_filter ==
3125 				HWTSTAMP_FILTER_NONE)) {
3126 				if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
3127 					pi->ptp_enable = false;
3128 			}
3129 
3130 			if (pi->tstamp_config.rx_filter !=
3131 				HWTSTAMP_FILTER_NONE) {
3132 				if (cxgb4_ptp_redirect_rx_packet(adapter,
3133 								 pi) >= 0)
3134 					pi->ptp_enable = true;
3135 			}
3136 		} else {
3137 			/* For T4 Adapters */
3138 			switch (pi->tstamp_config.rx_filter) {
3139 			case HWTSTAMP_FILTER_NONE:
3140 			pi->rxtstamp = false;
3141 			break;
3142 			case HWTSTAMP_FILTER_ALL:
3143 			pi->rxtstamp = true;
3144 			break;
3145 			default:
3146 			pi->tstamp_config.rx_filter =
3147 			HWTSTAMP_FILTER_NONE;
3148 			return -ERANGE;
3149 			}
3150 		}
3151 		return copy_to_user(req->ifr_data, &pi->tstamp_config,
3152 				    sizeof(pi->tstamp_config)) ?
3153 			-EFAULT : 0;
3154 	default:
3155 		return -EOPNOTSUPP;
3156 	}
3157 	return ret;
3158 }
3159 
cxgb_set_rxmode(struct net_device * dev)3160 static void cxgb_set_rxmode(struct net_device *dev)
3161 {
3162 	/* unfortunately we can't return errors to the stack */
3163 	set_rxmode(dev, -1, false);
3164 }
3165 
cxgb_change_mtu(struct net_device * dev,int new_mtu)3166 static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
3167 {
3168 	struct port_info *pi = netdev_priv(dev);
3169 	int ret;
3170 
3171 	ret = t4_set_rxmode(pi->adapter, pi->adapter->mbox, pi->viid,
3172 			    pi->viid_mirror, new_mtu, -1, -1, -1, -1, true);
3173 	if (!ret)
3174 		WRITE_ONCE(dev->mtu, new_mtu);
3175 	return ret;
3176 }
3177 
3178 #ifdef CONFIG_PCI_IOV
cxgb4_mgmt_open(struct net_device * dev)3179 static int cxgb4_mgmt_open(struct net_device *dev)
3180 {
3181 	/* Turn carrier off since we don't have to transmit anything on this
3182 	 * interface.
3183 	 */
3184 	netif_carrier_off(dev);
3185 	return 0;
3186 }
3187 
3188 /* Fill MAC address that will be assigned by the FW */
cxgb4_mgmt_fill_vf_station_mac_addr(struct adapter * adap)3189 static void cxgb4_mgmt_fill_vf_station_mac_addr(struct adapter *adap)
3190 {
3191 	u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
3192 	unsigned int i, vf, nvfs;
3193 	u16 a, b;
3194 	int err;
3195 	u8 *na;
3196 
3197 	err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
3198 	if (err)
3199 		return;
3200 
3201 	na = adap->params.vpd.na;
3202 	for (i = 0; i < ETH_ALEN; i++)
3203 		hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
3204 			      hex2val(na[2 * i + 1]));
3205 
3206 	a = (hw_addr[0] << 8) | hw_addr[1];
3207 	b = (hw_addr[1] << 8) | hw_addr[2];
3208 	a ^= b;
3209 	a |= 0x0200;    /* locally assigned Ethernet MAC address */
3210 	a &= ~0x0100;   /* not a multicast Ethernet MAC address */
3211 	macaddr[0] = a >> 8;
3212 	macaddr[1] = a & 0xff;
3213 
3214 	for (i = 2; i < 5; i++)
3215 		macaddr[i] = hw_addr[i + 1];
3216 
3217 	for (vf = 0, nvfs = pci_sriov_get_totalvfs(adap->pdev);
3218 		vf < nvfs; vf++) {
3219 		macaddr[5] = adap->pf * nvfs + vf;
3220 		ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, macaddr);
3221 	}
3222 }
3223 
cxgb4_mgmt_set_vf_mac(struct net_device * dev,int vf,u8 * mac)3224 static int cxgb4_mgmt_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
3225 {
3226 	struct port_info *pi = netdev_priv(dev);
3227 	struct adapter *adap = pi->adapter;
3228 	int ret;
3229 
3230 	/* verify MAC addr is valid */
3231 	if (!is_valid_ether_addr(mac)) {
3232 		dev_err(pi->adapter->pdev_dev,
3233 			"Invalid Ethernet address %pM for VF %d\n",
3234 			mac, vf);
3235 		return -EINVAL;
3236 	}
3237 
3238 	dev_info(pi->adapter->pdev_dev,
3239 		 "Setting MAC %pM on VF %d\n", mac, vf);
3240 	ret = t4_set_vf_mac_acl(adap, vf + 1, pi->lport, 1, mac);
3241 	if (!ret)
3242 		ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
3243 	return ret;
3244 }
3245 
cxgb4_mgmt_get_vf_config(struct net_device * dev,int vf,struct ifla_vf_info * ivi)3246 static int cxgb4_mgmt_get_vf_config(struct net_device *dev,
3247 				    int vf, struct ifla_vf_info *ivi)
3248 {
3249 	struct port_info *pi = netdev_priv(dev);
3250 	struct adapter *adap = pi->adapter;
3251 	struct vf_info *vfinfo;
3252 
3253 	if (vf >= adap->num_vfs)
3254 		return -EINVAL;
3255 	vfinfo = &adap->vfinfo[vf];
3256 
3257 	ivi->vf = vf;
3258 	ivi->max_tx_rate = vfinfo->tx_rate;
3259 	ivi->min_tx_rate = 0;
3260 	ether_addr_copy(ivi->mac, vfinfo->vf_mac_addr);
3261 	ivi->vlan = vfinfo->vlan;
3262 	ivi->linkstate = vfinfo->link_state;
3263 	return 0;
3264 }
3265 
cxgb4_mgmt_get_phys_port_id(struct net_device * dev,struct netdev_phys_item_id * ppid)3266 static int cxgb4_mgmt_get_phys_port_id(struct net_device *dev,
3267 				       struct netdev_phys_item_id *ppid)
3268 {
3269 	struct port_info *pi = netdev_priv(dev);
3270 	unsigned int phy_port_id;
3271 
3272 	phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
3273 	ppid->id_len = sizeof(phy_port_id);
3274 	memcpy(ppid->id, &phy_port_id, ppid->id_len);
3275 	return 0;
3276 }
3277 
cxgb4_mgmt_set_vf_rate(struct net_device * dev,int vf,int min_tx_rate,int max_tx_rate)3278 static int cxgb4_mgmt_set_vf_rate(struct net_device *dev, int vf,
3279 				  int min_tx_rate, int max_tx_rate)
3280 {
3281 	struct port_info *pi = netdev_priv(dev);
3282 	struct adapter *adap = pi->adapter;
3283 	unsigned int link_ok, speed, mtu;
3284 	u32 fw_pfvf, fw_class;
3285 	int class_id = vf;
3286 	int ret;
3287 	u16 pktsize;
3288 
3289 	if (vf >= adap->num_vfs)
3290 		return -EINVAL;
3291 
3292 	if (min_tx_rate) {
3293 		dev_err(adap->pdev_dev,
3294 			"Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
3295 			min_tx_rate, vf);
3296 		return -EINVAL;
3297 	}
3298 
3299 	if (max_tx_rate == 0) {
3300 		/* unbind VF to to any Traffic Class */
3301 		fw_pfvf =
3302 		    (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
3303 		     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
3304 		fw_class = 0xffffffff;
3305 		ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1,
3306 				    &fw_pfvf, &fw_class);
3307 		if (ret) {
3308 			dev_err(adap->pdev_dev,
3309 				"Err %d in unbinding PF %d VF %d from TX Rate Limiting\n",
3310 				ret, adap->pf, vf);
3311 			return -EINVAL;
3312 		}
3313 		dev_info(adap->pdev_dev,
3314 			 "PF %d VF %d is unbound from TX Rate Limiting\n",
3315 			 adap->pf, vf);
3316 		adap->vfinfo[vf].tx_rate = 0;
3317 		return 0;
3318 	}
3319 
3320 	ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
3321 	if (ret != FW_SUCCESS) {
3322 		dev_err(adap->pdev_dev,
3323 			"Failed to get link information for VF %d\n", vf);
3324 		return -EINVAL;
3325 	}
3326 
3327 	if (!link_ok) {
3328 		dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
3329 		return -EINVAL;
3330 	}
3331 
3332 	if (max_tx_rate > speed) {
3333 		dev_err(adap->pdev_dev,
3334 			"Max tx rate %d for VF %d can't be > link-speed %u",
3335 			max_tx_rate, vf, speed);
3336 		return -EINVAL;
3337 	}
3338 
3339 	pktsize = mtu;
3340 	/* subtract ethhdr size and 4 bytes crc since, f/w appends it */
3341 	pktsize = pktsize - sizeof(struct ethhdr) - 4;
3342 	/* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
3343 	pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
3344 	/* configure Traffic Class for rate-limiting */
3345 	ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
3346 			      SCHED_CLASS_LEVEL_CL_RL,
3347 			      SCHED_CLASS_MODE_CLASS,
3348 			      SCHED_CLASS_RATEUNIT_BITS,
3349 			      SCHED_CLASS_RATEMODE_ABS,
3350 			      pi->tx_chan, class_id, 0,
3351 			      max_tx_rate * 1000, 0, pktsize, 0);
3352 	if (ret) {
3353 		dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
3354 			ret);
3355 		return -EINVAL;
3356 	}
3357 	dev_info(adap->pdev_dev,
3358 		 "Class %d with MSS %u configured with rate %u\n",
3359 		 class_id, pktsize, max_tx_rate);
3360 
3361 	/* bind VF to configured Traffic Class */
3362 	fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
3363 		   FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
3364 	fw_class = class_id;
3365 	ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
3366 			    &fw_class);
3367 	if (ret) {
3368 		dev_err(adap->pdev_dev,
3369 			"Err %d in binding PF %d VF %d to Traffic Class %d\n",
3370 			ret, adap->pf, vf, class_id);
3371 		return -EINVAL;
3372 	}
3373 	dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
3374 		 adap->pf, vf, class_id);
3375 	adap->vfinfo[vf].tx_rate = max_tx_rate;
3376 	return 0;
3377 }
3378 
cxgb4_mgmt_set_vf_vlan(struct net_device * dev,int vf,u16 vlan,u8 qos,__be16 vlan_proto)3379 static int cxgb4_mgmt_set_vf_vlan(struct net_device *dev, int vf,
3380 				  u16 vlan, u8 qos, __be16 vlan_proto)
3381 {
3382 	struct port_info *pi = netdev_priv(dev);
3383 	struct adapter *adap = pi->adapter;
3384 	int ret;
3385 
3386 	if (vf >= adap->num_vfs || vlan > 4095 || qos > 7)
3387 		return -EINVAL;
3388 
3389 	if (vlan_proto != htons(ETH_P_8021Q) || qos != 0)
3390 		return -EPROTONOSUPPORT;
3391 
3392 	ret = t4_set_vlan_acl(adap, adap->mbox, vf + 1, vlan);
3393 	if (!ret) {
3394 		adap->vfinfo[vf].vlan = vlan;
3395 		return 0;
3396 	}
3397 
3398 	dev_err(adap->pdev_dev, "Err %d %s VLAN ACL for PF/VF %d/%d\n",
3399 		ret, (vlan ? "setting" : "clearing"), adap->pf, vf);
3400 	return ret;
3401 }
3402 
cxgb4_mgmt_set_vf_link_state(struct net_device * dev,int vf,int link)3403 static int cxgb4_mgmt_set_vf_link_state(struct net_device *dev, int vf,
3404 					int link)
3405 {
3406 	struct port_info *pi = netdev_priv(dev);
3407 	struct adapter *adap = pi->adapter;
3408 	u32 param, val;
3409 	int ret = 0;
3410 
3411 	if (vf >= adap->num_vfs)
3412 		return -EINVAL;
3413 
3414 	switch (link) {
3415 	case IFLA_VF_LINK_STATE_AUTO:
3416 		val = FW_VF_LINK_STATE_AUTO;
3417 		break;
3418 
3419 	case IFLA_VF_LINK_STATE_ENABLE:
3420 		val = FW_VF_LINK_STATE_ENABLE;
3421 		break;
3422 
3423 	case IFLA_VF_LINK_STATE_DISABLE:
3424 		val = FW_VF_LINK_STATE_DISABLE;
3425 		break;
3426 
3427 	default:
3428 		return -EINVAL;
3429 	}
3430 
3431 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
3432 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_LINK_STATE));
3433 	ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1,
3434 			    &param, &val);
3435 	if (ret) {
3436 		dev_err(adap->pdev_dev,
3437 			"Error %d in setting PF %d VF %d link state\n",
3438 			ret, adap->pf, vf);
3439 		return -EINVAL;
3440 	}
3441 
3442 	adap->vfinfo[vf].link_state = link;
3443 	return ret;
3444 }
3445 #endif /* CONFIG_PCI_IOV */
3446 
cxgb_set_mac_addr(struct net_device * dev,void * p)3447 static int cxgb_set_mac_addr(struct net_device *dev, void *p)
3448 {
3449 	int ret;
3450 	struct sockaddr *addr = p;
3451 	struct port_info *pi = netdev_priv(dev);
3452 
3453 	if (!is_valid_ether_addr(addr->sa_data))
3454 		return -EADDRNOTAVAIL;
3455 
3456 	ret = cxgb4_update_mac_filt(pi, pi->viid, &pi->xact_addr_filt,
3457 				    addr->sa_data, true, &pi->smt_idx);
3458 	if (ret < 0)
3459 		return ret;
3460 
3461 	eth_hw_addr_set(dev, addr->sa_data);
3462 	return 0;
3463 }
3464 
3465 #ifdef CONFIG_NET_POLL_CONTROLLER
cxgb_netpoll(struct net_device * dev)3466 static void cxgb_netpoll(struct net_device *dev)
3467 {
3468 	struct port_info *pi = netdev_priv(dev);
3469 	struct adapter *adap = pi->adapter;
3470 
3471 	if (adap->flags & CXGB4_USING_MSIX) {
3472 		int i;
3473 		struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
3474 
3475 		for (i = pi->nqsets; i; i--, rx++)
3476 			t4_sge_intr_msix(0, &rx->rspq);
3477 	} else
3478 		t4_intr_handler(adap)(0, adap);
3479 }
3480 #endif
3481 
cxgb_set_tx_maxrate(struct net_device * dev,int index,u32 rate)3482 static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
3483 {
3484 	struct port_info *pi = netdev_priv(dev);
3485 	struct adapter *adap = pi->adapter;
3486 	struct ch_sched_queue qe = { 0 };
3487 	struct ch_sched_params p = { 0 };
3488 	struct sched_class *e;
3489 	u32 req_rate;
3490 	int err = 0;
3491 
3492 	if (!can_sched(dev))
3493 		return -ENOTSUPP;
3494 
3495 	if (index < 0 || index > pi->nqsets - 1)
3496 		return -EINVAL;
3497 
3498 	if (!(adap->flags & CXGB4_FULL_INIT_DONE)) {
3499 		dev_err(adap->pdev_dev,
3500 			"Failed to rate limit on queue %d. Link Down?\n",
3501 			index);
3502 		return -EINVAL;
3503 	}
3504 
3505 	qe.queue = index;
3506 	e = cxgb4_sched_queue_lookup(dev, &qe);
3507 	if (e && e->info.u.params.level != SCHED_CLASS_LEVEL_CL_RL) {
3508 		dev_err(adap->pdev_dev,
3509 			"Queue %u already bound to class %u of type: %u\n",
3510 			index, e->idx, e->info.u.params.level);
3511 		return -EBUSY;
3512 	}
3513 
3514 	/* Convert from Mbps to Kbps */
3515 	req_rate = rate * 1000;
3516 
3517 	/* Max rate is 100 Gbps */
3518 	if (req_rate > SCHED_MAX_RATE_KBPS) {
3519 		dev_err(adap->pdev_dev,
3520 			"Invalid rate %u Mbps, Max rate is %u Mbps\n",
3521 			rate, SCHED_MAX_RATE_KBPS / 1000);
3522 		return -ERANGE;
3523 	}
3524 
3525 	/* First unbind the queue from any existing class */
3526 	memset(&qe, 0, sizeof(qe));
3527 	qe.queue = index;
3528 	qe.class = SCHED_CLS_NONE;
3529 
3530 	err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
3531 	if (err) {
3532 		dev_err(adap->pdev_dev,
3533 			"Unbinding Queue %d on port %d fail. Err: %d\n",
3534 			index, pi->port_id, err);
3535 		return err;
3536 	}
3537 
3538 	/* Queue already unbound */
3539 	if (!req_rate)
3540 		return 0;
3541 
3542 	/* Fetch any available unused or matching scheduling class */
3543 	p.type = SCHED_CLASS_TYPE_PACKET;
3544 	p.u.params.level    = SCHED_CLASS_LEVEL_CL_RL;
3545 	p.u.params.mode     = SCHED_CLASS_MODE_CLASS;
3546 	p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
3547 	p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
3548 	p.u.params.channel  = pi->tx_chan;
3549 	p.u.params.class    = SCHED_CLS_NONE;
3550 	p.u.params.minrate  = 0;
3551 	p.u.params.maxrate  = req_rate;
3552 	p.u.params.weight   = 0;
3553 	p.u.params.pktsize  = dev->mtu;
3554 
3555 	e = cxgb4_sched_class_alloc(dev, &p);
3556 	if (!e)
3557 		return -ENOMEM;
3558 
3559 	/* Bind the queue to a scheduling class */
3560 	memset(&qe, 0, sizeof(qe));
3561 	qe.queue = index;
3562 	qe.class = e->idx;
3563 
3564 	err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
3565 	if (err)
3566 		dev_err(adap->pdev_dev,
3567 			"Queue rate limiting failed. Err: %d\n", err);
3568 	return err;
3569 }
3570 
cxgb_setup_tc_flower(struct net_device * dev,struct flow_cls_offload * cls_flower)3571 static int cxgb_setup_tc_flower(struct net_device *dev,
3572 				struct flow_cls_offload *cls_flower)
3573 {
3574 	switch (cls_flower->command) {
3575 	case FLOW_CLS_REPLACE:
3576 		return cxgb4_tc_flower_replace(dev, cls_flower);
3577 	case FLOW_CLS_DESTROY:
3578 		return cxgb4_tc_flower_destroy(dev, cls_flower);
3579 	case FLOW_CLS_STATS:
3580 		return cxgb4_tc_flower_stats(dev, cls_flower);
3581 	default:
3582 		return -EOPNOTSUPP;
3583 	}
3584 }
3585 
cxgb_setup_tc_cls_u32(struct net_device * dev,struct tc_cls_u32_offload * cls_u32)3586 static int cxgb_setup_tc_cls_u32(struct net_device *dev,
3587 				 struct tc_cls_u32_offload *cls_u32)
3588 {
3589 	switch (cls_u32->command) {
3590 	case TC_CLSU32_NEW_KNODE:
3591 	case TC_CLSU32_REPLACE_KNODE:
3592 		return cxgb4_config_knode(dev, cls_u32);
3593 	case TC_CLSU32_DELETE_KNODE:
3594 		return cxgb4_delete_knode(dev, cls_u32);
3595 	default:
3596 		return -EOPNOTSUPP;
3597 	}
3598 }
3599 
cxgb_setup_tc_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls_matchall,bool ingress)3600 static int cxgb_setup_tc_matchall(struct net_device *dev,
3601 				  struct tc_cls_matchall_offload *cls_matchall,
3602 				  bool ingress)
3603 {
3604 	struct adapter *adap = netdev2adap(dev);
3605 
3606 	if (!adap->tc_matchall)
3607 		return -ENOMEM;
3608 
3609 	switch (cls_matchall->command) {
3610 	case TC_CLSMATCHALL_REPLACE:
3611 		return cxgb4_tc_matchall_replace(dev, cls_matchall, ingress);
3612 	case TC_CLSMATCHALL_DESTROY:
3613 		return cxgb4_tc_matchall_destroy(dev, cls_matchall, ingress);
3614 	case TC_CLSMATCHALL_STATS:
3615 		if (ingress)
3616 			return cxgb4_tc_matchall_stats(dev, cls_matchall);
3617 		break;
3618 	default:
3619 		break;
3620 	}
3621 
3622 	return -EOPNOTSUPP;
3623 }
3624 
cxgb_setup_tc_block_ingress_cb(enum tc_setup_type type,void * type_data,void * cb_priv)3625 static int cxgb_setup_tc_block_ingress_cb(enum tc_setup_type type,
3626 					  void *type_data, void *cb_priv)
3627 {
3628 	struct net_device *dev = cb_priv;
3629 	struct port_info *pi = netdev2pinfo(dev);
3630 	struct adapter *adap = netdev2adap(dev);
3631 
3632 	if (!(adap->flags & CXGB4_FULL_INIT_DONE)) {
3633 		dev_err(adap->pdev_dev,
3634 			"Failed to setup tc on port %d. Link Down?\n",
3635 			pi->port_id);
3636 		return -EINVAL;
3637 	}
3638 
3639 	if (!tc_cls_can_offload_and_chain0(dev, type_data))
3640 		return -EOPNOTSUPP;
3641 
3642 	switch (type) {
3643 	case TC_SETUP_CLSU32:
3644 		return cxgb_setup_tc_cls_u32(dev, type_data);
3645 	case TC_SETUP_CLSFLOWER:
3646 		return cxgb_setup_tc_flower(dev, type_data);
3647 	case TC_SETUP_CLSMATCHALL:
3648 		return cxgb_setup_tc_matchall(dev, type_data, true);
3649 	default:
3650 		return -EOPNOTSUPP;
3651 	}
3652 }
3653 
cxgb_setup_tc_block_egress_cb(enum tc_setup_type type,void * type_data,void * cb_priv)3654 static int cxgb_setup_tc_block_egress_cb(enum tc_setup_type type,
3655 					 void *type_data, void *cb_priv)
3656 {
3657 	struct net_device *dev = cb_priv;
3658 	struct port_info *pi = netdev2pinfo(dev);
3659 	struct adapter *adap = netdev2adap(dev);
3660 
3661 	if (!(adap->flags & CXGB4_FULL_INIT_DONE)) {
3662 		dev_err(adap->pdev_dev,
3663 			"Failed to setup tc on port %d. Link Down?\n",
3664 			pi->port_id);
3665 		return -EINVAL;
3666 	}
3667 
3668 	if (!tc_cls_can_offload_and_chain0(dev, type_data))
3669 		return -EOPNOTSUPP;
3670 
3671 	switch (type) {
3672 	case TC_SETUP_CLSMATCHALL:
3673 		return cxgb_setup_tc_matchall(dev, type_data, false);
3674 	default:
3675 		break;
3676 	}
3677 
3678 	return -EOPNOTSUPP;
3679 }
3680 
cxgb_setup_tc_mqprio(struct net_device * dev,struct tc_mqprio_qopt_offload * mqprio)3681 static int cxgb_setup_tc_mqprio(struct net_device *dev,
3682 				struct tc_mqprio_qopt_offload *mqprio)
3683 {
3684 	struct adapter *adap = netdev2adap(dev);
3685 
3686 	if (!is_ethofld(adap) || !adap->tc_mqprio)
3687 		return -ENOMEM;
3688 
3689 	return cxgb4_setup_tc_mqprio(dev, mqprio);
3690 }
3691 
3692 static LIST_HEAD(cxgb_block_cb_list);
3693 
cxgb_setup_tc_block(struct net_device * dev,struct flow_block_offload * f)3694 static int cxgb_setup_tc_block(struct net_device *dev,
3695 			       struct flow_block_offload *f)
3696 {
3697 	struct port_info *pi = netdev_priv(dev);
3698 	flow_setup_cb_t *cb;
3699 	bool ingress_only;
3700 
3701 	pi->tc_block_shared = f->block_shared;
3702 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
3703 		cb = cxgb_setup_tc_block_egress_cb;
3704 		ingress_only = false;
3705 	} else {
3706 		cb = cxgb_setup_tc_block_ingress_cb;
3707 		ingress_only = true;
3708 	}
3709 
3710 	return flow_block_cb_setup_simple(f, &cxgb_block_cb_list,
3711 					  cb, pi, dev, ingress_only);
3712 }
3713 
cxgb_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)3714 static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
3715 			 void *type_data)
3716 {
3717 	switch (type) {
3718 	case TC_SETUP_QDISC_MQPRIO:
3719 		return cxgb_setup_tc_mqprio(dev, type_data);
3720 	case TC_SETUP_BLOCK:
3721 		return cxgb_setup_tc_block(dev, type_data);
3722 	default:
3723 		return -EOPNOTSUPP;
3724 	}
3725 }
3726 
cxgb_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)3727 static int cxgb_udp_tunnel_unset_port(struct net_device *netdev,
3728 				      unsigned int table, unsigned int entry,
3729 				      struct udp_tunnel_info *ti)
3730 {
3731 	struct port_info *pi = netdev_priv(netdev);
3732 	struct adapter *adapter = pi->adapter;
3733 	u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3734 	int ret = 0, i;
3735 
3736 	switch (ti->type) {
3737 	case UDP_TUNNEL_TYPE_VXLAN:
3738 		adapter->vxlan_port = 0;
3739 		t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A, 0);
3740 		break;
3741 	case UDP_TUNNEL_TYPE_GENEVE:
3742 		adapter->geneve_port = 0;
3743 		t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A, 0);
3744 		break;
3745 	default:
3746 		return -EINVAL;
3747 	}
3748 
3749 	/* Matchall mac entries can be deleted only after all tunnel ports
3750 	 * are brought down or removed.
3751 	 */
3752 	if (!adapter->rawf_cnt)
3753 		return 0;
3754 	for_each_port(adapter, i) {
3755 		pi = adap2pinfo(adapter, i);
3756 		ret = t4_free_raw_mac_filt(adapter, pi->viid,
3757 					   match_all_mac, match_all_mac,
3758 					   adapter->rawf_start + pi->port_id,
3759 					   1, pi->port_id, false);
3760 		if (ret < 0) {
3761 			netdev_info(netdev, "Failed to free mac filter entry, for port %d\n",
3762 				    i);
3763 			return ret;
3764 		}
3765 	}
3766 
3767 	return 0;
3768 }
3769 
cxgb_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)3770 static int cxgb_udp_tunnel_set_port(struct net_device *netdev,
3771 				    unsigned int table, unsigned int entry,
3772 				    struct udp_tunnel_info *ti)
3773 {
3774 	struct port_info *pi = netdev_priv(netdev);
3775 	struct adapter *adapter = pi->adapter;
3776 	u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3777 	int i, ret;
3778 
3779 	switch (ti->type) {
3780 	case UDP_TUNNEL_TYPE_VXLAN:
3781 		adapter->vxlan_port = ti->port;
3782 		t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A,
3783 			     VXLAN_V(be16_to_cpu(ti->port)) | VXLAN_EN_F);
3784 		break;
3785 	case UDP_TUNNEL_TYPE_GENEVE:
3786 		adapter->geneve_port = ti->port;
3787 		t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A,
3788 			     GENEVE_V(be16_to_cpu(ti->port)) | GENEVE_EN_F);
3789 		break;
3790 	default:
3791 		return -EINVAL;
3792 	}
3793 
3794 	/* Create a 'match all' mac filter entry for inner mac,
3795 	 * if raw mac interface is supported. Once the linux kernel provides
3796 	 * driver entry points for adding/deleting the inner mac addresses,
3797 	 * we will remove this 'match all' entry and fallback to adding
3798 	 * exact match filters.
3799 	 */
3800 	for_each_port(adapter, i) {
3801 		pi = adap2pinfo(adapter, i);
3802 
3803 		ret = t4_alloc_raw_mac_filt(adapter, pi->viid,
3804 					    match_all_mac,
3805 					    match_all_mac,
3806 					    adapter->rawf_start + pi->port_id,
3807 					    1, pi->port_id, false);
3808 		if (ret < 0) {
3809 			netdev_info(netdev, "Failed to allocate a mac filter entry, not adding port %d\n",
3810 				    be16_to_cpu(ti->port));
3811 			return ret;
3812 		}
3813 	}
3814 
3815 	return 0;
3816 }
3817 
3818 static const struct udp_tunnel_nic_info cxgb_udp_tunnels = {
3819 	.set_port	= cxgb_udp_tunnel_set_port,
3820 	.unset_port	= cxgb_udp_tunnel_unset_port,
3821 	.tables		= {
3822 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
3823 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
3824 	},
3825 };
3826 
cxgb_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)3827 static netdev_features_t cxgb_features_check(struct sk_buff *skb,
3828 					     struct net_device *dev,
3829 					     netdev_features_t features)
3830 {
3831 	struct port_info *pi = netdev_priv(dev);
3832 	struct adapter *adapter = pi->adapter;
3833 
3834 	if (CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3835 		return features;
3836 
3837 	/* Check if hw supports offload for this packet */
3838 	if (!skb->encapsulation || cxgb_encap_offload_supported(skb))
3839 		return features;
3840 
3841 	/* Offload is not supported for this encapsulated packet */
3842 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3843 }
3844 
cxgb_fix_features(struct net_device * dev,netdev_features_t features)3845 static netdev_features_t cxgb_fix_features(struct net_device *dev,
3846 					   netdev_features_t features)
3847 {
3848 	/* Disable GRO, if RX_CSUM is disabled */
3849 	if (!(features & NETIF_F_RXCSUM))
3850 		features &= ~NETIF_F_GRO;
3851 
3852 	return features;
3853 }
3854 
3855 static const struct net_device_ops cxgb4_netdev_ops = {
3856 	.ndo_open             = cxgb_open,
3857 	.ndo_stop             = cxgb_close,
3858 	.ndo_start_xmit       = t4_start_xmit,
3859 	.ndo_select_queue     =	cxgb_select_queue,
3860 	.ndo_get_stats64      = cxgb_get_stats,
3861 	.ndo_set_rx_mode      = cxgb_set_rxmode,
3862 	.ndo_set_mac_address  = cxgb_set_mac_addr,
3863 	.ndo_set_features     = cxgb_set_features,
3864 	.ndo_validate_addr    = eth_validate_addr,
3865 	.ndo_eth_ioctl         = cxgb_ioctl,
3866 	.ndo_change_mtu       = cxgb_change_mtu,
3867 #ifdef CONFIG_NET_POLL_CONTROLLER
3868 	.ndo_poll_controller  = cxgb_netpoll,
3869 #endif
3870 #ifdef CONFIG_CHELSIO_T4_FCOE
3871 	.ndo_fcoe_enable      = cxgb_fcoe_enable,
3872 	.ndo_fcoe_disable     = cxgb_fcoe_disable,
3873 #endif /* CONFIG_CHELSIO_T4_FCOE */
3874 	.ndo_set_tx_maxrate   = cxgb_set_tx_maxrate,
3875 	.ndo_setup_tc         = cxgb_setup_tc,
3876 	.ndo_features_check   = cxgb_features_check,
3877 	.ndo_fix_features     = cxgb_fix_features,
3878 };
3879 
3880 #ifdef CONFIG_PCI_IOV
3881 static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
3882 	.ndo_open               = cxgb4_mgmt_open,
3883 	.ndo_set_vf_mac         = cxgb4_mgmt_set_vf_mac,
3884 	.ndo_get_vf_config      = cxgb4_mgmt_get_vf_config,
3885 	.ndo_set_vf_rate        = cxgb4_mgmt_set_vf_rate,
3886 	.ndo_get_phys_port_id   = cxgb4_mgmt_get_phys_port_id,
3887 	.ndo_set_vf_vlan        = cxgb4_mgmt_set_vf_vlan,
3888 	.ndo_set_vf_link_state	= cxgb4_mgmt_set_vf_link_state,
3889 };
3890 
cxgb4_mgmt_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)3891 static void cxgb4_mgmt_get_drvinfo(struct net_device *dev,
3892 				   struct ethtool_drvinfo *info)
3893 {
3894 	struct adapter *adapter = netdev2adap(dev);
3895 
3896 	strscpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3897 	strscpy(info->bus_info, pci_name(adapter->pdev),
3898 		sizeof(info->bus_info));
3899 }
3900 
3901 static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3902 	.get_drvinfo       = cxgb4_mgmt_get_drvinfo,
3903 };
3904 #endif
3905 
notify_fatal_err(struct work_struct * work)3906 static void notify_fatal_err(struct work_struct *work)
3907 {
3908 	struct adapter *adap;
3909 
3910 	adap = container_of(work, struct adapter, fatal_err_notify_task);
3911 	notify_ulds(adap, CXGB4_STATE_FATAL_ERROR);
3912 }
3913 
t4_fatal_err(struct adapter * adap)3914 void t4_fatal_err(struct adapter *adap)
3915 {
3916 	int port;
3917 
3918 	if (pci_channel_offline(adap->pdev))
3919 		return;
3920 
3921 	/* Disable the SGE since ULDs are going to free resources that
3922 	 * could be exposed to the adapter.  RDMA MWs for example...
3923 	 */
3924 	t4_shutdown_adapter(adap);
3925 	for_each_port(adap, port) {
3926 		struct net_device *dev = adap->port[port];
3927 
3928 		/* If we get here in very early initialization the network
3929 		 * devices may not have been set up yet.
3930 		 */
3931 		if (!dev)
3932 			continue;
3933 
3934 		netif_tx_stop_all_queues(dev);
3935 		netif_carrier_off(dev);
3936 	}
3937 	dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3938 	queue_work(adap->workq, &adap->fatal_err_notify_task);
3939 }
3940 
setup_memwin(struct adapter * adap)3941 static void setup_memwin(struct adapter *adap)
3942 {
3943 	u32 nic_win_base = t4_get_util_window(adap);
3944 
3945 	t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
3946 }
3947 
setup_memwin_rdma(struct adapter * adap)3948 static void setup_memwin_rdma(struct adapter *adap)
3949 {
3950 	if (adap->vres.ocq.size) {
3951 		u32 start;
3952 		unsigned int sz_kb;
3953 
3954 		start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3955 		start &= PCI_BASE_ADDRESS_MEM_MASK;
3956 		start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
3957 		sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3958 		t4_write_reg(adap,
3959 			     PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3960 			     start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
3961 		t4_write_reg(adap,
3962 			     PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
3963 			     adap->vres.ocq.start);
3964 		t4_read_reg(adap,
3965 			    PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
3966 	}
3967 }
3968 
3969 /* HMA Definitions */
3970 
3971 /* The maximum number of address that can be send in a single FW cmd */
3972 #define HMA_MAX_ADDR_IN_CMD	5
3973 
3974 #define HMA_PAGE_SIZE		PAGE_SIZE
3975 
3976 #define HMA_MAX_NO_FW_ADDRESS	(16 << 10)  /* FW supports 16K addresses */
3977 
3978 #define HMA_PAGE_ORDER					\
3979 	((HMA_PAGE_SIZE < HMA_MAX_NO_FW_ADDRESS) ?	\
3980 	ilog2(HMA_MAX_NO_FW_ADDRESS / HMA_PAGE_SIZE) : 0)
3981 
3982 /* The minimum and maximum possible HMA sizes that can be specified in the FW
3983  * configuration(in units of MB).
3984  */
3985 #define HMA_MIN_TOTAL_SIZE	1
3986 #define HMA_MAX_TOTAL_SIZE				\
3987 	(((HMA_PAGE_SIZE << HMA_PAGE_ORDER) *		\
3988 	  HMA_MAX_NO_FW_ADDRESS) >> 20)
3989 
adap_free_hma_mem(struct adapter * adapter)3990 static void adap_free_hma_mem(struct adapter *adapter)
3991 {
3992 	struct scatterlist *iter;
3993 	struct page *page;
3994 	int i;
3995 
3996 	if (!adapter->hma.sgt)
3997 		return;
3998 
3999 	if (adapter->hma.flags & HMA_DMA_MAPPED_FLAG) {
4000 		dma_unmap_sg(adapter->pdev_dev, adapter->hma.sgt->sgl,
4001 			     adapter->hma.sgt->nents, DMA_BIDIRECTIONAL);
4002 		adapter->hma.flags &= ~HMA_DMA_MAPPED_FLAG;
4003 	}
4004 
4005 	for_each_sg(adapter->hma.sgt->sgl, iter,
4006 		    adapter->hma.sgt->orig_nents, i) {
4007 		page = sg_page(iter);
4008 		if (page)
4009 			__free_pages(page, HMA_PAGE_ORDER);
4010 	}
4011 
4012 	kfree(adapter->hma.phy_addr);
4013 	sg_free_table(adapter->hma.sgt);
4014 	kfree(adapter->hma.sgt);
4015 	adapter->hma.sgt = NULL;
4016 }
4017 
adap_config_hma(struct adapter * adapter)4018 static int adap_config_hma(struct adapter *adapter)
4019 {
4020 	struct scatterlist *sgl, *iter;
4021 	struct sg_table *sgt;
4022 	struct page *newpage;
4023 	unsigned int i, j, k;
4024 	u32 param, hma_size;
4025 	unsigned int ncmds;
4026 	size_t page_size;
4027 	u32 page_order;
4028 	int node, ret;
4029 
4030 	/* HMA is supported only for T6+ cards.
4031 	 * Avoid initializing HMA in kdump kernels.
4032 	 */
4033 	if (is_kdump_kernel() ||
4034 	    CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
4035 		return 0;
4036 
4037 	/* Get the HMA region size required by fw */
4038 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4039 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_HMA_SIZE));
4040 	ret = t4_query_params(adapter, adapter->mbox, adapter->pf, 0,
4041 			      1, &param, &hma_size);
4042 	/* An error means card has its own memory or HMA is not supported by
4043 	 * the firmware. Return without any errors.
4044 	 */
4045 	if (ret || !hma_size)
4046 		return 0;
4047 
4048 	if (hma_size < HMA_MIN_TOTAL_SIZE ||
4049 	    hma_size > HMA_MAX_TOTAL_SIZE) {
4050 		dev_err(adapter->pdev_dev,
4051 			"HMA size %uMB beyond bounds(%u-%lu)MB\n",
4052 			hma_size, HMA_MIN_TOTAL_SIZE, HMA_MAX_TOTAL_SIZE);
4053 		return -EINVAL;
4054 	}
4055 
4056 	page_size = HMA_PAGE_SIZE;
4057 	page_order = HMA_PAGE_ORDER;
4058 	adapter->hma.sgt = kzalloc(sizeof(*adapter->hma.sgt), GFP_KERNEL);
4059 	if (unlikely(!adapter->hma.sgt)) {
4060 		dev_err(adapter->pdev_dev, "HMA SG table allocation failed\n");
4061 		return -ENOMEM;
4062 	}
4063 	sgt = adapter->hma.sgt;
4064 	/* FW returned value will be in MB's
4065 	 */
4066 	sgt->orig_nents = (hma_size << 20) / (page_size << page_order);
4067 	if (sg_alloc_table(sgt, sgt->orig_nents, GFP_KERNEL)) {
4068 		dev_err(adapter->pdev_dev, "HMA SGL allocation failed\n");
4069 		kfree(adapter->hma.sgt);
4070 		adapter->hma.sgt = NULL;
4071 		return -ENOMEM;
4072 	}
4073 
4074 	sgl = adapter->hma.sgt->sgl;
4075 	node = dev_to_node(adapter->pdev_dev);
4076 	for_each_sg(sgl, iter, sgt->orig_nents, i) {
4077 		newpage = alloc_pages_node(node, __GFP_NOWARN | GFP_KERNEL |
4078 					   __GFP_ZERO, page_order);
4079 		if (!newpage) {
4080 			dev_err(adapter->pdev_dev,
4081 				"Not enough memory for HMA page allocation\n");
4082 			ret = -ENOMEM;
4083 			goto free_hma;
4084 		}
4085 		sg_set_page(iter, newpage, page_size << page_order, 0);
4086 	}
4087 
4088 	sgt->nents = dma_map_sg(adapter->pdev_dev, sgl, sgt->orig_nents,
4089 				DMA_BIDIRECTIONAL);
4090 	if (!sgt->nents) {
4091 		dev_err(adapter->pdev_dev,
4092 			"Not enough memory for HMA DMA mapping");
4093 		ret = -ENOMEM;
4094 		goto free_hma;
4095 	}
4096 	adapter->hma.flags |= HMA_DMA_MAPPED_FLAG;
4097 
4098 	adapter->hma.phy_addr = kcalloc(sgt->nents, sizeof(dma_addr_t),
4099 					GFP_KERNEL);
4100 	if (unlikely(!adapter->hma.phy_addr))
4101 		goto free_hma;
4102 
4103 	for_each_sg(sgl, iter, sgt->nents, i) {
4104 		newpage = sg_page(iter);
4105 		adapter->hma.phy_addr[i] = sg_dma_address(iter);
4106 	}
4107 
4108 	ncmds = DIV_ROUND_UP(sgt->nents, HMA_MAX_ADDR_IN_CMD);
4109 	/* Pass on the addresses to firmware */
4110 	for (i = 0, k = 0; i < ncmds; i++, k += HMA_MAX_ADDR_IN_CMD) {
4111 		struct fw_hma_cmd hma_cmd;
4112 		u8 naddr = HMA_MAX_ADDR_IN_CMD;
4113 		u8 soc = 0, eoc = 0;
4114 		u8 hma_mode = 1; /* Presently we support only Page table mode */
4115 
4116 		soc = (i == 0) ? 1 : 0;
4117 		eoc = (i == ncmds - 1) ? 1 : 0;
4118 
4119 		/* For last cmd, set naddr corresponding to remaining
4120 		 * addresses
4121 		 */
4122 		if (i == ncmds - 1) {
4123 			naddr = sgt->nents % HMA_MAX_ADDR_IN_CMD;
4124 			naddr = naddr ? naddr : HMA_MAX_ADDR_IN_CMD;
4125 		}
4126 		memset(&hma_cmd, 0, sizeof(hma_cmd));
4127 		hma_cmd.op_pkd = htonl(FW_CMD_OP_V(FW_HMA_CMD) |
4128 				       FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
4129 		hma_cmd.retval_len16 = htonl(FW_LEN16(hma_cmd));
4130 
4131 		hma_cmd.mode_to_pcie_params =
4132 			htonl(FW_HMA_CMD_MODE_V(hma_mode) |
4133 			      FW_HMA_CMD_SOC_V(soc) | FW_HMA_CMD_EOC_V(eoc));
4134 
4135 		/* HMA cmd size specified in MB's */
4136 		hma_cmd.naddr_size =
4137 			htonl(FW_HMA_CMD_SIZE_V(hma_size) |
4138 			      FW_HMA_CMD_NADDR_V(naddr));
4139 
4140 		/* Total Page size specified in units of 4K */
4141 		hma_cmd.addr_size_pkd =
4142 			htonl(FW_HMA_CMD_ADDR_SIZE_V
4143 				((page_size << page_order) >> 12));
4144 
4145 		/* Fill the 5 addresses */
4146 		for (j = 0; j < naddr; j++) {
4147 			hma_cmd.phy_address[j] =
4148 				cpu_to_be64(adapter->hma.phy_addr[j + k]);
4149 		}
4150 		ret = t4_wr_mbox(adapter, adapter->mbox, &hma_cmd,
4151 				 sizeof(hma_cmd), &hma_cmd);
4152 		if (ret) {
4153 			dev_err(adapter->pdev_dev,
4154 				"HMA FW command failed with err %d\n", ret);
4155 			goto free_hma;
4156 		}
4157 	}
4158 
4159 	if (!ret)
4160 		dev_info(adapter->pdev_dev,
4161 			 "Reserved %uMB host memory for HMA\n", hma_size);
4162 	return ret;
4163 
4164 free_hma:
4165 	adap_free_hma_mem(adapter);
4166 	return ret;
4167 }
4168 
adap_init1(struct adapter * adap,struct fw_caps_config_cmd * c)4169 static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
4170 {
4171 	u32 v;
4172 	int ret;
4173 
4174 	/* Now that we've successfully configured and initialized the adapter
4175 	 * can ask the Firmware what resources it has provisioned for us.
4176 	 */
4177 	ret = t4_get_pfres(adap);
4178 	if (ret) {
4179 		dev_err(adap->pdev_dev,
4180 			"Unable to retrieve resource provisioning information\n");
4181 		return ret;
4182 	}
4183 
4184 	/* get device capabilities */
4185 	memset(c, 0, sizeof(*c));
4186 	c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4187 			       FW_CMD_REQUEST_F | FW_CMD_READ_F);
4188 	c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
4189 	ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
4190 	if (ret < 0)
4191 		return ret;
4192 
4193 	c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4194 			       FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
4195 	ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
4196 	if (ret < 0)
4197 		return ret;
4198 
4199 	ret = t4_config_glbl_rss(adap, adap->pf,
4200 				 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
4201 				 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
4202 				 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
4203 	if (ret < 0)
4204 		return ret;
4205 
4206 	ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
4207 			  MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
4208 			  FW_CMD_CAP_PF);
4209 	if (ret < 0)
4210 		return ret;
4211 
4212 	t4_sge_init(adap);
4213 
4214 	/* tweak some settings */
4215 	t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
4216 	t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
4217 	t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
4218 	v = t4_read_reg(adap, TP_PIO_DATA_A);
4219 	t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
4220 
4221 	/* first 4 Tx modulation queues point to consecutive Tx channels */
4222 	adap->params.tp.tx_modq_map = 0xE4;
4223 	t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
4224 		     TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
4225 
4226 	/* associate each Tx modulation queue with consecutive Tx channels */
4227 	v = 0x84218421;
4228 	t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
4229 			  &v, 1, TP_TX_SCHED_HDR_A);
4230 	t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
4231 			  &v, 1, TP_TX_SCHED_FIFO_A);
4232 	t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
4233 			  &v, 1, TP_TX_SCHED_PCMD_A);
4234 
4235 #define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
4236 	if (is_offload(adap)) {
4237 		t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
4238 			     TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
4239 			     TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
4240 			     TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
4241 			     TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
4242 		t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
4243 			     TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
4244 			     TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
4245 			     TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
4246 			     TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
4247 	}
4248 
4249 	/* get basic stuff going */
4250 	return t4_early_init(adap, adap->pf);
4251 }
4252 
4253 /*
4254  * Max # of ATIDs.  The absolute HW max is 16K but we keep it lower.
4255  */
4256 #define MAX_ATIDS 8192U
4257 
4258 /*
4259  * Phase 0 of initialization: contact FW, obtain config, perform basic init.
4260  *
4261  * If the firmware we're dealing with has Configuration File support, then
4262  * we use that to perform all configuration
4263  */
4264 
4265 /*
4266  * Tweak configuration based on module parameters, etc.  Most of these have
4267  * defaults assigned to them by Firmware Configuration Files (if we're using
4268  * them) but need to be explicitly set if we're using hard-coded
4269  * initialization.  But even in the case of using Firmware Configuration
4270  * Files, we'd like to expose the ability to change these via module
4271  * parameters so these are essentially common tweaks/settings for
4272  * Configuration Files and hard-coded initialization ...
4273  */
adap_init0_tweaks(struct adapter * adapter)4274 static int adap_init0_tweaks(struct adapter *adapter)
4275 {
4276 	/*
4277 	 * Fix up various Host-Dependent Parameters like Page Size, Cache
4278 	 * Line Size, etc.  The firmware default is for a 4KB Page Size and
4279 	 * 64B Cache Line Size ...
4280 	 */
4281 	t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
4282 
4283 	/*
4284 	 * Process module parameters which affect early initialization.
4285 	 */
4286 	if (rx_dma_offset != 2 && rx_dma_offset != 0) {
4287 		dev_err(&adapter->pdev->dev,
4288 			"Ignoring illegal rx_dma_offset=%d, using 2\n",
4289 			rx_dma_offset);
4290 		rx_dma_offset = 2;
4291 	}
4292 	t4_set_reg_field(adapter, SGE_CONTROL_A,
4293 			 PKTSHIFT_V(PKTSHIFT_M),
4294 			 PKTSHIFT_V(rx_dma_offset));
4295 
4296 	/*
4297 	 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
4298 	 * adds the pseudo header itself.
4299 	 */
4300 	t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
4301 			       CSUM_HAS_PSEUDO_HDR_F, 0);
4302 
4303 	return 0;
4304 }
4305 
4306 /* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
4307  * unto themselves and they contain their own firmware to perform their
4308  * tasks ...
4309  */
phy_aq1202_version(const u8 * phy_fw_data,size_t phy_fw_size)4310 static int phy_aq1202_version(const u8 *phy_fw_data,
4311 			      size_t phy_fw_size)
4312 {
4313 	int offset;
4314 
4315 	/* At offset 0x8 you're looking for the primary image's
4316 	 * starting offset which is 3 Bytes wide
4317 	 *
4318 	 * At offset 0xa of the primary image, you look for the offset
4319 	 * of the DRAM segment which is 3 Bytes wide.
4320 	 *
4321 	 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
4322 	 * wide
4323 	 */
4324 	#define be16(__p) (((__p)[0] << 8) | (__p)[1])
4325 	#define le16(__p) ((__p)[0] | ((__p)[1] << 8))
4326 	#define le24(__p) (le16(__p) | ((__p)[2] << 16))
4327 
4328 	offset = le24(phy_fw_data + 0x8) << 12;
4329 	offset = le24(phy_fw_data + offset + 0xa);
4330 	return be16(phy_fw_data + offset + 0x27e);
4331 
4332 	#undef be16
4333 	#undef le16
4334 	#undef le24
4335 }
4336 
4337 static struct info_10gbt_phy_fw {
4338 	unsigned int phy_fw_id;		/* PCI Device ID */
4339 	char *phy_fw_file;		/* /lib/firmware/ PHY Firmware file */
4340 	int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
4341 	int phy_flash;			/* Has FLASH for PHY Firmware */
4342 } phy_info_array[] = {
4343 	{
4344 		PHY_AQ1202_DEVICEID,
4345 		PHY_AQ1202_FIRMWARE,
4346 		phy_aq1202_version,
4347 		1,
4348 	},
4349 	{
4350 		PHY_BCM84834_DEVICEID,
4351 		PHY_BCM84834_FIRMWARE,
4352 		NULL,
4353 		0,
4354 	},
4355 	{ 0, NULL, NULL },
4356 };
4357 
find_phy_info(int devid)4358 static struct info_10gbt_phy_fw *find_phy_info(int devid)
4359 {
4360 	int i;
4361 
4362 	for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
4363 		if (phy_info_array[i].phy_fw_id == devid)
4364 			return &phy_info_array[i];
4365 	}
4366 	return NULL;
4367 }
4368 
4369 /* Handle updating of chip-external 10Gb/s-BT PHY firmware.  This needs to
4370  * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD.  On error
4371  * we return a negative error number.  If we transfer new firmware we return 1
4372  * (from t4_load_phy_fw()).  If we don't do anything we return 0.
4373  */
adap_init0_phy(struct adapter * adap)4374 static int adap_init0_phy(struct adapter *adap)
4375 {
4376 	const struct firmware *phyf;
4377 	int ret;
4378 	struct info_10gbt_phy_fw *phy_info;
4379 
4380 	/* Use the device ID to determine which PHY file to flash.
4381 	 */
4382 	phy_info = find_phy_info(adap->pdev->device);
4383 	if (!phy_info) {
4384 		dev_warn(adap->pdev_dev,
4385 			 "No PHY Firmware file found for this PHY\n");
4386 		return -EOPNOTSUPP;
4387 	}
4388 
4389 	/* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
4390 	 * use that. The adapter firmware provides us with a memory buffer
4391 	 * where we can load a PHY firmware file from the host if we want to
4392 	 * override the PHY firmware File in flash.
4393 	 */
4394 	ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
4395 				      adap->pdev_dev);
4396 	if (ret < 0) {
4397 		/* For adapters without FLASH attached to PHY for their
4398 		 * firmware, it's obviously a fatal error if we can't get the
4399 		 * firmware to the adapter.  For adapters with PHY firmware
4400 		 * FLASH storage, it's worth a warning if we can't find the
4401 		 * PHY Firmware but we'll neuter the error ...
4402 		 */
4403 		dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
4404 			"/lib/firmware/%s, error %d\n",
4405 			phy_info->phy_fw_file, -ret);
4406 		if (phy_info->phy_flash) {
4407 			int cur_phy_fw_ver = 0;
4408 
4409 			t4_phy_fw_ver(adap, &cur_phy_fw_ver);
4410 			dev_warn(adap->pdev_dev, "continuing with, on-adapter "
4411 				 "FLASH copy, version %#x\n", cur_phy_fw_ver);
4412 			ret = 0;
4413 		}
4414 
4415 		return ret;
4416 	}
4417 
4418 	/* Load PHY Firmware onto adapter.
4419 	 */
4420 	ret = t4_load_phy_fw(adap, MEMWIN_NIC, phy_info->phy_fw_version,
4421 			     (u8 *)phyf->data, phyf->size);
4422 	if (ret < 0)
4423 		dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
4424 			-ret);
4425 	else if (ret > 0) {
4426 		int new_phy_fw_ver = 0;
4427 
4428 		if (phy_info->phy_fw_version)
4429 			new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
4430 								  phyf->size);
4431 		dev_info(adap->pdev_dev, "Successfully transferred PHY "
4432 			 "Firmware /lib/firmware/%s, version %#x\n",
4433 			 phy_info->phy_fw_file, new_phy_fw_ver);
4434 	}
4435 
4436 	release_firmware(phyf);
4437 
4438 	return ret;
4439 }
4440 
4441 /*
4442  * Attempt to initialize the adapter via a Firmware Configuration File.
4443  */
adap_init0_config(struct adapter * adapter,int reset)4444 static int adap_init0_config(struct adapter *adapter, int reset)
4445 {
4446 	char *fw_config_file, fw_config_file_path[256];
4447 	u32 finiver, finicsum, cfcsum, param, val;
4448 	struct fw_caps_config_cmd caps_cmd;
4449 	unsigned long mtype = 0, maddr = 0;
4450 	const struct firmware *cf;
4451 	char *config_name = NULL;
4452 	int config_issued = 0;
4453 	int ret;
4454 
4455 	/*
4456 	 * Reset device if necessary.
4457 	 */
4458 	if (reset) {
4459 		ret = t4_fw_reset(adapter, adapter->mbox,
4460 				  PIORSTMODE_F | PIORST_F);
4461 		if (ret < 0)
4462 			goto bye;
4463 	}
4464 
4465 	/* If this is a 10Gb/s-BT adapter make sure the chip-external
4466 	 * 10Gb/s-BT PHYs have up-to-date firmware.  Note that this step needs
4467 	 * to be performed after any global adapter RESET above since some
4468 	 * PHYs only have local RAM copies of the PHY firmware.
4469 	 */
4470 	if (is_10gbt_device(adapter->pdev->device)) {
4471 		ret = adap_init0_phy(adapter);
4472 		if (ret < 0)
4473 			goto bye;
4474 	}
4475 	/*
4476 	 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
4477 	 * then use that.  Otherwise, use the configuration file stored
4478 	 * in the adapter flash ...
4479 	 */
4480 	switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
4481 	case CHELSIO_T4:
4482 		fw_config_file = FW4_CFNAME;
4483 		break;
4484 	case CHELSIO_T5:
4485 		fw_config_file = FW5_CFNAME;
4486 		break;
4487 	case CHELSIO_T6:
4488 		fw_config_file = FW6_CFNAME;
4489 		break;
4490 	default:
4491 		dev_err(adapter->pdev_dev, "Device %d is not supported\n",
4492 		       adapter->pdev->device);
4493 		ret = -EINVAL;
4494 		goto bye;
4495 	}
4496 
4497 	ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
4498 	if (ret < 0) {
4499 		config_name = "On FLASH";
4500 		mtype = FW_MEMTYPE_CF_FLASH;
4501 		maddr = t4_flash_cfg_addr(adapter);
4502 	} else {
4503 		u32 params[7], val[7];
4504 
4505 		sprintf(fw_config_file_path,
4506 			"/lib/firmware/%s", fw_config_file);
4507 		config_name = fw_config_file_path;
4508 
4509 		if (cf->size >= FLASH_CFG_MAX_SIZE)
4510 			ret = -ENOMEM;
4511 		else {
4512 			params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4513 			     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
4514 			ret = t4_query_params(adapter, adapter->mbox,
4515 					      adapter->pf, 0, 1, params, val);
4516 			if (ret == 0) {
4517 				/*
4518 				 * For t4_memory_rw() below addresses and
4519 				 * sizes have to be in terms of multiples of 4
4520 				 * bytes.  So, if the Configuration File isn't
4521 				 * a multiple of 4 bytes in length we'll have
4522 				 * to write that out separately since we can't
4523 				 * guarantee that the bytes following the
4524 				 * residual byte in the buffer returned by
4525 				 * request_firmware() are zeroed out ...
4526 				 */
4527 				size_t resid = cf->size & 0x3;
4528 				size_t size = cf->size & ~0x3;
4529 				__be32 *data = (__be32 *)cf->data;
4530 
4531 				mtype = FW_PARAMS_PARAM_Y_G(val[0]);
4532 				maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
4533 
4534 				spin_lock(&adapter->win0_lock);
4535 				ret = t4_memory_rw(adapter, 0, mtype, maddr,
4536 						   size, data, T4_MEMORY_WRITE);
4537 				if (ret == 0 && resid != 0) {
4538 					union {
4539 						__be32 word;
4540 						char buf[4];
4541 					} last;
4542 					int i;
4543 
4544 					last.word = data[size >> 2];
4545 					for (i = resid; i < 4; i++)
4546 						last.buf[i] = 0;
4547 					ret = t4_memory_rw(adapter, 0, mtype,
4548 							   maddr + size,
4549 							   4, &last.word,
4550 							   T4_MEMORY_WRITE);
4551 				}
4552 				spin_unlock(&adapter->win0_lock);
4553 			}
4554 		}
4555 
4556 		release_firmware(cf);
4557 		if (ret)
4558 			goto bye;
4559 	}
4560 
4561 	val = 0;
4562 
4563 	/* Ofld + Hash filter is supported. Older fw will fail this request and
4564 	 * it is fine.
4565 	 */
4566 	param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4567 		 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_HASHFILTER_WITH_OFLD));
4568 	ret = t4_set_params(adapter, adapter->mbox, adapter->pf, 0,
4569 			    1, &param, &val);
4570 
4571 	/* FW doesn't know about Hash filter + ofld support,
4572 	 * it's not a problem, don't return an error.
4573 	 */
4574 	if (ret < 0) {
4575 		dev_warn(adapter->pdev_dev,
4576 			 "Hash filter with ofld is not supported by FW\n");
4577 	}
4578 
4579 	/*
4580 	 * Issue a Capability Configuration command to the firmware to get it
4581 	 * to parse the Configuration File.  We don't use t4_fw_config_file()
4582 	 * because we want the ability to modify various features after we've
4583 	 * processed the configuration file ...
4584 	 */
4585 	memset(&caps_cmd, 0, sizeof(caps_cmd));
4586 	caps_cmd.op_to_write =
4587 		htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4588 		      FW_CMD_REQUEST_F |
4589 		      FW_CMD_READ_F);
4590 	caps_cmd.cfvalid_to_len16 =
4591 		htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
4592 		      FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
4593 		      FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
4594 		      FW_LEN16(caps_cmd));
4595 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
4596 			 &caps_cmd);
4597 
4598 	/* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
4599 	 * Configuration File in FLASH), our last gasp effort is to use the
4600 	 * Firmware Configuration File which is embedded in the firmware.  A
4601 	 * very few early versions of the firmware didn't have one embedded
4602 	 * but we can ignore those.
4603 	 */
4604 	if (ret == -ENOENT) {
4605 		memset(&caps_cmd, 0, sizeof(caps_cmd));
4606 		caps_cmd.op_to_write =
4607 			htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4608 					FW_CMD_REQUEST_F |
4609 					FW_CMD_READ_F);
4610 		caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
4611 		ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
4612 				sizeof(caps_cmd), &caps_cmd);
4613 		config_name = "Firmware Default";
4614 	}
4615 
4616 	config_issued = 1;
4617 	if (ret < 0)
4618 		goto bye;
4619 
4620 	finiver = ntohl(caps_cmd.finiver);
4621 	finicsum = ntohl(caps_cmd.finicsum);
4622 	cfcsum = ntohl(caps_cmd.cfcsum);
4623 	if (finicsum != cfcsum)
4624 		dev_warn(adapter->pdev_dev, "Configuration File checksum "\
4625 			 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
4626 			 finicsum, cfcsum);
4627 
4628 	/*
4629 	 * And now tell the firmware to use the configuration we just loaded.
4630 	 */
4631 	caps_cmd.op_to_write =
4632 		htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4633 		      FW_CMD_REQUEST_F |
4634 		      FW_CMD_WRITE_F);
4635 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
4636 	ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
4637 			 NULL);
4638 	if (ret < 0)
4639 		goto bye;
4640 
4641 	/*
4642 	 * Tweak configuration based on system architecture, module
4643 	 * parameters, etc.
4644 	 */
4645 	ret = adap_init0_tweaks(adapter);
4646 	if (ret < 0)
4647 		goto bye;
4648 
4649 	/* We will proceed even if HMA init fails. */
4650 	ret = adap_config_hma(adapter);
4651 	if (ret)
4652 		dev_err(adapter->pdev_dev,
4653 			"HMA configuration failed with error %d\n", ret);
4654 
4655 	if (is_t6(adapter->params.chip)) {
4656 		adap_config_hpfilter(adapter);
4657 		ret = setup_ppod_edram(adapter);
4658 		if (!ret)
4659 			dev_info(adapter->pdev_dev, "Successfully enabled "
4660 				 "ppod edram feature\n");
4661 	}
4662 
4663 	/*
4664 	 * And finally tell the firmware to initialize itself using the
4665 	 * parameters from the Configuration File.
4666 	 */
4667 	ret = t4_fw_initialize(adapter, adapter->mbox);
4668 	if (ret < 0)
4669 		goto bye;
4670 
4671 	/* Emit Firmware Configuration File information and return
4672 	 * successfully.
4673 	 */
4674 	dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
4675 		 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
4676 		 config_name, finiver, cfcsum);
4677 	return 0;
4678 
4679 	/*
4680 	 * Something bad happened.  Return the error ...  (If the "error"
4681 	 * is that there's no Configuration File on the adapter we don't
4682 	 * want to issue a warning since this is fairly common.)
4683 	 */
4684 bye:
4685 	if (config_issued && ret != -ENOENT)
4686 		dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
4687 			 config_name, -ret);
4688 	return ret;
4689 }
4690 
4691 static struct fw_info fw_info_array[] = {
4692 	{
4693 		.chip = CHELSIO_T4,
4694 		.fs_name = FW4_CFNAME,
4695 		.fw_mod_name = FW4_FNAME,
4696 		.fw_hdr = {
4697 			.chip = FW_HDR_CHIP_T4,
4698 			.fw_ver = __cpu_to_be32(FW_VERSION(T4)),
4699 			.intfver_nic = FW_INTFVER(T4, NIC),
4700 			.intfver_vnic = FW_INTFVER(T4, VNIC),
4701 			.intfver_ri = FW_INTFVER(T4, RI),
4702 			.intfver_iscsi = FW_INTFVER(T4, ISCSI),
4703 			.intfver_fcoe = FW_INTFVER(T4, FCOE),
4704 		},
4705 	}, {
4706 		.chip = CHELSIO_T5,
4707 		.fs_name = FW5_CFNAME,
4708 		.fw_mod_name = FW5_FNAME,
4709 		.fw_hdr = {
4710 			.chip = FW_HDR_CHIP_T5,
4711 			.fw_ver = __cpu_to_be32(FW_VERSION(T5)),
4712 			.intfver_nic = FW_INTFVER(T5, NIC),
4713 			.intfver_vnic = FW_INTFVER(T5, VNIC),
4714 			.intfver_ri = FW_INTFVER(T5, RI),
4715 			.intfver_iscsi = FW_INTFVER(T5, ISCSI),
4716 			.intfver_fcoe = FW_INTFVER(T5, FCOE),
4717 		},
4718 	}, {
4719 		.chip = CHELSIO_T6,
4720 		.fs_name = FW6_CFNAME,
4721 		.fw_mod_name = FW6_FNAME,
4722 		.fw_hdr = {
4723 			.chip = FW_HDR_CHIP_T6,
4724 			.fw_ver = __cpu_to_be32(FW_VERSION(T6)),
4725 			.intfver_nic = FW_INTFVER(T6, NIC),
4726 			.intfver_vnic = FW_INTFVER(T6, VNIC),
4727 			.intfver_ofld = FW_INTFVER(T6, OFLD),
4728 			.intfver_ri = FW_INTFVER(T6, RI),
4729 			.intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4730 			.intfver_iscsi = FW_INTFVER(T6, ISCSI),
4731 			.intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4732 			.intfver_fcoe = FW_INTFVER(T6, FCOE),
4733 		},
4734 	}
4735 
4736 };
4737 
find_fw_info(int chip)4738 static struct fw_info *find_fw_info(int chip)
4739 {
4740 	int i;
4741 
4742 	for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
4743 		if (fw_info_array[i].chip == chip)
4744 			return &fw_info_array[i];
4745 	}
4746 	return NULL;
4747 }
4748 
4749 /*
4750  * Phase 0 of initialization: contact FW, obtain config, perform basic init.
4751  */
adap_init0(struct adapter * adap,int vpd_skip)4752 static int adap_init0(struct adapter *adap, int vpd_skip)
4753 {
4754 	struct fw_caps_config_cmd caps_cmd;
4755 	u32 params[7], val[7];
4756 	enum dev_state state;
4757 	u32 v, port_vec;
4758 	int reset = 1;
4759 	int ret;
4760 
4761 	/* Grab Firmware Device Log parameters as early as possible so we have
4762 	 * access to it for debugging, etc.
4763 	 */
4764 	ret = t4_init_devlog_params(adap);
4765 	if (ret < 0)
4766 		return ret;
4767 
4768 	/* Contact FW, advertising Master capability */
4769 	ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
4770 			  is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
4771 	if (ret < 0) {
4772 		dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
4773 			ret);
4774 		return ret;
4775 	}
4776 	if (ret == adap->mbox)
4777 		adap->flags |= CXGB4_MASTER_PF;
4778 
4779 	/*
4780 	 * If we're the Master PF Driver and the device is uninitialized,
4781 	 * then let's consider upgrading the firmware ...  (We always want
4782 	 * to check the firmware version number in order to A. get it for
4783 	 * later reporting and B. to warn if the currently loaded firmware
4784 	 * is excessively mismatched relative to the driver.)
4785 	 */
4786 
4787 	t4_get_version_info(adap);
4788 	ret = t4_check_fw_version(adap);
4789 	/* If firmware is too old (not supported by driver) force an update. */
4790 	if (ret)
4791 		state = DEV_STATE_UNINIT;
4792 	if ((adap->flags & CXGB4_MASTER_PF) && state != DEV_STATE_INIT) {
4793 		struct fw_info *fw_info;
4794 		struct fw_hdr *card_fw;
4795 		const struct firmware *fw;
4796 		const u8 *fw_data = NULL;
4797 		unsigned int fw_size = 0;
4798 
4799 		/* This is the firmware whose headers the driver was compiled
4800 		 * against
4801 		 */
4802 		fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
4803 		if (fw_info == NULL) {
4804 			dev_err(adap->pdev_dev,
4805 				"unable to get firmware info for chip %d.\n",
4806 				CHELSIO_CHIP_VERSION(adap->params.chip));
4807 			return -EINVAL;
4808 		}
4809 
4810 		/* allocate memory to read the header of the firmware on the
4811 		 * card
4812 		 */
4813 		card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
4814 		if (!card_fw) {
4815 			ret = -ENOMEM;
4816 			goto bye;
4817 		}
4818 
4819 		/* Get FW from from /lib/firmware/ */
4820 		ret = request_firmware(&fw, fw_info->fw_mod_name,
4821 				       adap->pdev_dev);
4822 		if (ret < 0) {
4823 			dev_err(adap->pdev_dev,
4824 				"unable to load firmware image %s, error %d\n",
4825 				fw_info->fw_mod_name, ret);
4826 		} else {
4827 			fw_data = fw->data;
4828 			fw_size = fw->size;
4829 		}
4830 
4831 		/* upgrade FW logic */
4832 		ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
4833 				 state, &reset);
4834 
4835 		/* Cleaning up */
4836 		release_firmware(fw);
4837 		kvfree(card_fw);
4838 
4839 		if (ret < 0)
4840 			goto bye;
4841 	}
4842 
4843 	/* If the firmware is initialized already, emit a simply note to that
4844 	 * effect. Otherwise, it's time to try initializing the adapter.
4845 	 */
4846 	if (state == DEV_STATE_INIT) {
4847 		ret = adap_config_hma(adap);
4848 		if (ret)
4849 			dev_err(adap->pdev_dev,
4850 				"HMA configuration failed with error %d\n",
4851 				ret);
4852 		dev_info(adap->pdev_dev, "Coming up as %s: "\
4853 			 "Adapter already initialized\n",
4854 			 adap->flags & CXGB4_MASTER_PF ? "MASTER" : "SLAVE");
4855 	} else {
4856 		dev_info(adap->pdev_dev, "Coming up as MASTER: "\
4857 			 "Initializing adapter\n");
4858 
4859 		/* Find out whether we're dealing with a version of the
4860 		 * firmware which has configuration file support.
4861 		 */
4862 		params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4863 			     FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
4864 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
4865 				      params, val);
4866 
4867 		/* If the firmware doesn't support Configuration Files,
4868 		 * return an error.
4869 		 */
4870 		if (ret < 0) {
4871 			dev_err(adap->pdev_dev, "firmware doesn't support "
4872 				"Firmware Configuration Files\n");
4873 			goto bye;
4874 		}
4875 
4876 		/* The firmware provides us with a memory buffer where we can
4877 		 * load a Configuration File from the host if we want to
4878 		 * override the Configuration File in flash.
4879 		 */
4880 		ret = adap_init0_config(adap, reset);
4881 		if (ret == -ENOENT) {
4882 			dev_err(adap->pdev_dev, "no Configuration File "
4883 				"present on adapter.\n");
4884 			goto bye;
4885 		}
4886 		if (ret < 0) {
4887 			dev_err(adap->pdev_dev, "could not initialize "
4888 				"adapter, error %d\n", -ret);
4889 			goto bye;
4890 		}
4891 	}
4892 
4893 	/* Now that we've successfully configured and initialized the adapter
4894 	 * (or found it already initialized), we can ask the Firmware what
4895 	 * resources it has provisioned for us.
4896 	 */
4897 	ret = t4_get_pfres(adap);
4898 	if (ret) {
4899 		dev_err(adap->pdev_dev,
4900 			"Unable to retrieve resource provisioning information\n");
4901 		goto bye;
4902 	}
4903 
4904 	/* Grab VPD parameters.  This should be done after we establish a
4905 	 * connection to the firmware since some of the VPD parameters
4906 	 * (notably the Core Clock frequency) are retrieved via requests to
4907 	 * the firmware.  On the other hand, we need these fairly early on
4908 	 * so we do this right after getting ahold of the firmware.
4909 	 *
4910 	 * We need to do this after initializing the adapter because someone
4911 	 * could have FLASHed a new VPD which won't be read by the firmware
4912 	 * until we do the RESET ...
4913 	 */
4914 	if (!vpd_skip) {
4915 		ret = t4_get_vpd_params(adap, &adap->params.vpd);
4916 		if (ret < 0)
4917 			goto bye;
4918 	}
4919 
4920 	/* Find out what ports are available to us.  Note that we need to do
4921 	 * this before calling adap_init0_no_config() since it needs nports
4922 	 * and portvec ...
4923 	 */
4924 	v =
4925 	    FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4926 	    FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
4927 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
4928 	if (ret < 0)
4929 		goto bye;
4930 
4931 	adap->params.nports = hweight32(port_vec);
4932 	adap->params.portvec = port_vec;
4933 
4934 	/* Give the SGE code a chance to pull in anything that it needs ...
4935 	 * Note that this must be called after we retrieve our VPD parameters
4936 	 * in order to know how to convert core ticks to seconds, etc.
4937 	 */
4938 	ret = t4_sge_init(adap);
4939 	if (ret < 0)
4940 		goto bye;
4941 
4942 	/* Grab the SGE Doorbell Queue Timer values.  If successful, that
4943 	 * indicates that the Firmware and Hardware support this.
4944 	 */
4945 	params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4946 		    FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_DBQ_TIMERTICK));
4947 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4948 			      1, params, val);
4949 
4950 	if (!ret) {
4951 		adap->sge.dbqtimer_tick = val[0];
4952 		ret = t4_read_sge_dbqtimers(adap,
4953 					    ARRAY_SIZE(adap->sge.dbqtimer_val),
4954 					    adap->sge.dbqtimer_val);
4955 	}
4956 
4957 	if (!ret)
4958 		adap->flags |= CXGB4_SGE_DBQ_TIMER;
4959 
4960 	if (is_bypass_device(adap->pdev->device))
4961 		adap->params.bypass = 1;
4962 
4963 	/*
4964 	 * Grab some of our basic fundamental operating parameters.
4965 	 */
4966 	params[0] = FW_PARAM_PFVF(EQ_START);
4967 	params[1] = FW_PARAM_PFVF(L2T_START);
4968 	params[2] = FW_PARAM_PFVF(L2T_END);
4969 	params[3] = FW_PARAM_PFVF(FILTER_START);
4970 	params[4] = FW_PARAM_PFVF(FILTER_END);
4971 	params[5] = FW_PARAM_PFVF(IQFLINT_START);
4972 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
4973 	if (ret < 0)
4974 		goto bye;
4975 	adap->sge.egr_start = val[0];
4976 	adap->l2t_start = val[1];
4977 	adap->l2t_end = val[2];
4978 	adap->tids.ftid_base = val[3];
4979 	adap->tids.nftids = val[4] - val[3] + 1;
4980 	adap->sge.ingr_start = val[5];
4981 
4982 	if (CHELSIO_CHIP_VERSION(adap->params.chip) > CHELSIO_T5) {
4983 		params[0] = FW_PARAM_PFVF(HPFILTER_START);
4984 		params[1] = FW_PARAM_PFVF(HPFILTER_END);
4985 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4986 				      params, val);
4987 		if (ret < 0)
4988 			goto bye;
4989 
4990 		adap->tids.hpftid_base = val[0];
4991 		adap->tids.nhpftids = val[1] - val[0] + 1;
4992 
4993 		/* Read the raw mps entries. In T6, the last 2 tcam entries
4994 		 * are reserved for raw mac addresses (rawf = 2, one per port).
4995 		 */
4996 		params[0] = FW_PARAM_PFVF(RAWF_START);
4997 		params[1] = FW_PARAM_PFVF(RAWF_END);
4998 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4999 				      params, val);
5000 		if (ret == 0) {
5001 			adap->rawf_start = val[0];
5002 			adap->rawf_cnt = val[1] - val[0] + 1;
5003 		}
5004 
5005 		adap->tids.tid_base =
5006 			t4_read_reg(adap, LE_DB_ACTIVE_TABLE_START_INDEX_A);
5007 	}
5008 
5009 	/* qids (ingress/egress) returned from firmware can be anywhere
5010 	 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
5011 	 * Hence driver needs to allocate memory for this range to
5012 	 * store the queue info. Get the highest IQFLINT/EQ index returned
5013 	 * in FW_EQ_*_CMD.alloc command.
5014 	 */
5015 	params[0] = FW_PARAM_PFVF(EQ_END);
5016 	params[1] = FW_PARAM_PFVF(IQFLINT_END);
5017 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
5018 	if (ret < 0)
5019 		goto bye;
5020 	adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
5021 	adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
5022 
5023 	adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
5024 				    sizeof(*adap->sge.egr_map), GFP_KERNEL);
5025 	if (!adap->sge.egr_map) {
5026 		ret = -ENOMEM;
5027 		goto bye;
5028 	}
5029 
5030 	adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
5031 				     sizeof(*adap->sge.ingr_map), GFP_KERNEL);
5032 	if (!adap->sge.ingr_map) {
5033 		ret = -ENOMEM;
5034 		goto bye;
5035 	}
5036 
5037 	/* Allocate the memory for the vaious egress queue bitmaps
5038 	 * ie starving_fl, txq_maperr and blocked_fl.
5039 	 */
5040 	adap->sge.starving_fl = bitmap_zalloc(adap->sge.egr_sz, GFP_KERNEL);
5041 	if (!adap->sge.starving_fl) {
5042 		ret = -ENOMEM;
5043 		goto bye;
5044 	}
5045 
5046 	adap->sge.txq_maperr = bitmap_zalloc(adap->sge.egr_sz, GFP_KERNEL);
5047 	if (!adap->sge.txq_maperr) {
5048 		ret = -ENOMEM;
5049 		goto bye;
5050 	}
5051 
5052 #ifdef CONFIG_DEBUG_FS
5053 	adap->sge.blocked_fl = bitmap_zalloc(adap->sge.egr_sz, GFP_KERNEL);
5054 	if (!adap->sge.blocked_fl) {
5055 		ret = -ENOMEM;
5056 		goto bye;
5057 	}
5058 #endif
5059 
5060 	params[0] = FW_PARAM_PFVF(CLIP_START);
5061 	params[1] = FW_PARAM_PFVF(CLIP_END);
5062 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
5063 	if (ret < 0)
5064 		goto bye;
5065 	adap->clipt_start = val[0];
5066 	adap->clipt_end = val[1];
5067 
5068 	/* Get the supported number of traffic classes */
5069 	params[0] = FW_PARAM_DEV(NUM_TM_CLASS);
5070 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
5071 	if (ret < 0) {
5072 		/* We couldn't retrieve the number of Traffic Classes
5073 		 * supported by the hardware/firmware. So we hard
5074 		 * code it here.
5075 		 */
5076 		adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
5077 	} else {
5078 		adap->params.nsched_cls = val[0];
5079 	}
5080 
5081 	/* query params related to active filter region */
5082 	params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
5083 	params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
5084 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
5085 	/* If Active filter size is set we enable establishing
5086 	 * offload connection through firmware work request
5087 	 */
5088 	if ((val[0] != val[1]) && (ret >= 0)) {
5089 		adap->flags |= CXGB4_FW_OFLD_CONN;
5090 		adap->tids.aftid_base = val[0];
5091 		adap->tids.aftid_end = val[1];
5092 	}
5093 
5094 	/* If we're running on newer firmware, let it know that we're
5095 	 * prepared to deal with encapsulated CPL messages.  Older
5096 	 * firmware won't understand this and we'll just get
5097 	 * unencapsulated messages ...
5098 	 */
5099 	params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
5100 	val[0] = 1;
5101 	(void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
5102 
5103 	/*
5104 	 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
5105 	 * capability.  Earlier versions of the firmware didn't have the
5106 	 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
5107 	 * permission to use ULPTX MEMWRITE DSGL.
5108 	 */
5109 	if (is_t4(adap->params.chip)) {
5110 		adap->params.ulptx_memwrite_dsgl = false;
5111 	} else {
5112 		params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
5113 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
5114 				      1, params, val);
5115 		adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
5116 	}
5117 
5118 	/* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
5119 	params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
5120 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
5121 			      1, params, val);
5122 	adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
5123 
5124 	/* See if FW supports FW_FILTER2 work request */
5125 	if (is_t4(adap->params.chip)) {
5126 		adap->params.filter2_wr_support = false;
5127 	} else {
5128 		params[0] = FW_PARAM_DEV(FILTER2_WR);
5129 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
5130 				      1, params, val);
5131 		adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
5132 	}
5133 
5134 	/* Check if FW supports returning vin and smt index.
5135 	 * If this is not supported, driver will interpret
5136 	 * these values from viid.
5137 	 */
5138 	params[0] = FW_PARAM_DEV(OPAQUE_VIID_SMT_EXTN);
5139 	ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
5140 			      1, params, val);
5141 	adap->params.viid_smt_extn_support = (ret == 0 && val[0] != 0);
5142 
5143 	/*
5144 	 * Get device capabilities so we can determine what resources we need
5145 	 * to manage.
5146 	 */
5147 	memset(&caps_cmd, 0, sizeof(caps_cmd));
5148 	caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
5149 				     FW_CMD_REQUEST_F | FW_CMD_READ_F);
5150 	caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
5151 	ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
5152 			 &caps_cmd);
5153 	if (ret < 0)
5154 		goto bye;
5155 
5156 	/* hash filter has some mandatory register settings to be tested and for
5157 	 * that it needs to test whether offload is enabled or not, hence
5158 	 * checking and setting it here.
5159 	 */
5160 	if (caps_cmd.ofldcaps)
5161 		adap->params.offload = 1;
5162 
5163 	if (caps_cmd.ofldcaps ||
5164 	    (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) ||
5165 	    (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_ETHOFLD))) {
5166 		/* query offload-related parameters */
5167 		params[0] = FW_PARAM_DEV(NTID);
5168 		params[1] = FW_PARAM_PFVF(SERVER_START);
5169 		params[2] = FW_PARAM_PFVF(SERVER_END);
5170 		params[3] = FW_PARAM_PFVF(TDDP_START);
5171 		params[4] = FW_PARAM_PFVF(TDDP_END);
5172 		params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
5173 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
5174 				      params, val);
5175 		if (ret < 0)
5176 			goto bye;
5177 		adap->tids.ntids = val[0];
5178 		adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
5179 		adap->tids.stid_base = val[1];
5180 		adap->tids.nstids = val[2] - val[1] + 1;
5181 		/*
5182 		 * Setup server filter region. Divide the available filter
5183 		 * region into two parts. Regular filters get 1/3rd and server
5184 		 * filters get 2/3rd part. This is only enabled if workarond
5185 		 * path is enabled.
5186 		 * 1. For regular filters.
5187 		 * 2. Server filter: This are special filters which are used
5188 		 * to redirect SYN packets to offload queue.
5189 		 */
5190 		if (adap->flags & CXGB4_FW_OFLD_CONN && !is_bypass(adap)) {
5191 			adap->tids.sftid_base = adap->tids.ftid_base +
5192 					DIV_ROUND_UP(adap->tids.nftids, 3);
5193 			adap->tids.nsftids = adap->tids.nftids -
5194 					 DIV_ROUND_UP(adap->tids.nftids, 3);
5195 			adap->tids.nftids = adap->tids.sftid_base -
5196 						adap->tids.ftid_base;
5197 		}
5198 		adap->vres.ddp.start = val[3];
5199 		adap->vres.ddp.size = val[4] - val[3] + 1;
5200 		adap->params.ofldq_wr_cred = val[5];
5201 
5202 		if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
5203 			init_hash_filter(adap);
5204 		} else {
5205 			adap->num_ofld_uld += 1;
5206 		}
5207 
5208 		if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_ETHOFLD)) {
5209 			params[0] = FW_PARAM_PFVF(ETHOFLD_START);
5210 			params[1] = FW_PARAM_PFVF(ETHOFLD_END);
5211 			ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
5212 					      params, val);
5213 			if (!ret) {
5214 				adap->tids.eotid_base = val[0];
5215 				adap->tids.neotids = min_t(u32, MAX_ATIDS,
5216 							   val[1] - val[0] + 1);
5217 				adap->params.ethofld = 1;
5218 			}
5219 		}
5220 	}
5221 	if (caps_cmd.rdmacaps) {
5222 		params[0] = FW_PARAM_PFVF(STAG_START);
5223 		params[1] = FW_PARAM_PFVF(STAG_END);
5224 		params[2] = FW_PARAM_PFVF(RQ_START);
5225 		params[3] = FW_PARAM_PFVF(RQ_END);
5226 		params[4] = FW_PARAM_PFVF(PBL_START);
5227 		params[5] = FW_PARAM_PFVF(PBL_END);
5228 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
5229 				      params, val);
5230 		if (ret < 0)
5231 			goto bye;
5232 		adap->vres.stag.start = val[0];
5233 		adap->vres.stag.size = val[1] - val[0] + 1;
5234 		adap->vres.rq.start = val[2];
5235 		adap->vres.rq.size = val[3] - val[2] + 1;
5236 		adap->vres.pbl.start = val[4];
5237 		adap->vres.pbl.size = val[5] - val[4] + 1;
5238 
5239 		params[0] = FW_PARAM_PFVF(SRQ_START);
5240 		params[1] = FW_PARAM_PFVF(SRQ_END);
5241 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
5242 				      params, val);
5243 		if (!ret) {
5244 			adap->vres.srq.start = val[0];
5245 			adap->vres.srq.size = val[1] - val[0] + 1;
5246 		}
5247 		if (adap->vres.srq.size) {
5248 			adap->srq = t4_init_srq(adap->vres.srq.size);
5249 			if (!adap->srq)
5250 				dev_warn(&adap->pdev->dev, "could not allocate SRQ, continuing\n");
5251 		}
5252 
5253 		params[0] = FW_PARAM_PFVF(SQRQ_START);
5254 		params[1] = FW_PARAM_PFVF(SQRQ_END);
5255 		params[2] = FW_PARAM_PFVF(CQ_START);
5256 		params[3] = FW_PARAM_PFVF(CQ_END);
5257 		params[4] = FW_PARAM_PFVF(OCQ_START);
5258 		params[5] = FW_PARAM_PFVF(OCQ_END);
5259 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
5260 				      val);
5261 		if (ret < 0)
5262 			goto bye;
5263 		adap->vres.qp.start = val[0];
5264 		adap->vres.qp.size = val[1] - val[0] + 1;
5265 		adap->vres.cq.start = val[2];
5266 		adap->vres.cq.size = val[3] - val[2] + 1;
5267 		adap->vres.ocq.start = val[4];
5268 		adap->vres.ocq.size = val[5] - val[4] + 1;
5269 
5270 		params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
5271 		params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
5272 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
5273 				      val);
5274 		if (ret < 0) {
5275 			adap->params.max_ordird_qp = 8;
5276 			adap->params.max_ird_adapter = 32 * adap->tids.ntids;
5277 			ret = 0;
5278 		} else {
5279 			adap->params.max_ordird_qp = val[0];
5280 			adap->params.max_ird_adapter = val[1];
5281 		}
5282 		dev_info(adap->pdev_dev,
5283 			 "max_ordird_qp %d max_ird_adapter %d\n",
5284 			 adap->params.max_ordird_qp,
5285 			 adap->params.max_ird_adapter);
5286 
5287 		/* Enable write_with_immediate if FW supports it */
5288 		params[0] = FW_PARAM_DEV(RDMA_WRITE_WITH_IMM);
5289 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params,
5290 				      val);
5291 		adap->params.write_w_imm_support = (ret == 0 && val[0] != 0);
5292 
5293 		/* Enable write_cmpl if FW supports it */
5294 		params[0] = FW_PARAM_DEV(RI_WRITE_CMPL_WR);
5295 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params,
5296 				      val);
5297 		adap->params.write_cmpl_support = (ret == 0 && val[0] != 0);
5298 		adap->num_ofld_uld += 2;
5299 	}
5300 	if (caps_cmd.iscsicaps) {
5301 		params[0] = FW_PARAM_PFVF(ISCSI_START);
5302 		params[1] = FW_PARAM_PFVF(ISCSI_END);
5303 		ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
5304 				      params, val);
5305 		if (ret < 0)
5306 			goto bye;
5307 		adap->vres.iscsi.start = val[0];
5308 		adap->vres.iscsi.size = val[1] - val[0] + 1;
5309 		if (is_t6(adap->params.chip)) {
5310 			params[0] = FW_PARAM_PFVF(PPOD_EDRAM_START);
5311 			params[1] = FW_PARAM_PFVF(PPOD_EDRAM_END);
5312 			ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
5313 					      params, val);
5314 			if (!ret) {
5315 				adap->vres.ppod_edram.start = val[0];
5316 				adap->vres.ppod_edram.size =
5317 					val[1] - val[0] + 1;
5318 
5319 				dev_info(adap->pdev_dev,
5320 					 "ppod edram start 0x%x end 0x%x size 0x%x\n",
5321 					 val[0], val[1],
5322 					 adap->vres.ppod_edram.size);
5323 			}
5324 		}
5325 		/* LIO target and cxgb4i initiaitor */
5326 		adap->num_ofld_uld += 2;
5327 	}
5328 	if (caps_cmd.cryptocaps) {
5329 		if (ntohs(caps_cmd.cryptocaps) &
5330 		    FW_CAPS_CONFIG_CRYPTO_LOOKASIDE) {
5331 			params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
5332 			ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
5333 					      2, params, val);
5334 			if (ret < 0) {
5335 				if (ret != -EINVAL)
5336 					goto bye;
5337 			} else {
5338 				adap->vres.ncrypto_fc = val[0];
5339 			}
5340 			adap->num_ofld_uld += 1;
5341 		}
5342 		if (ntohs(caps_cmd.cryptocaps) &
5343 		    FW_CAPS_CONFIG_TLS_INLINE) {
5344 			params[0] = FW_PARAM_PFVF(TLS_START);
5345 			params[1] = FW_PARAM_PFVF(TLS_END);
5346 			ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
5347 					      2, params, val);
5348 			if (ret < 0)
5349 				goto bye;
5350 			adap->vres.key.start = val[0];
5351 			adap->vres.key.size = val[1] - val[0] + 1;
5352 			adap->num_uld += 1;
5353 		}
5354 		adap->params.crypto = ntohs(caps_cmd.cryptocaps);
5355 	}
5356 
5357 	/* The MTU/MSS Table is initialized by now, so load their values.  If
5358 	 * we're initializing the adapter, then we'll make any modifications
5359 	 * we want to the MTU/MSS Table and also initialize the congestion
5360 	 * parameters.
5361 	 */
5362 	t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
5363 	if (state != DEV_STATE_INIT) {
5364 		int i;
5365 
5366 		/* The default MTU Table contains values 1492 and 1500.
5367 		 * However, for TCP, it's better to have two values which are
5368 		 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
5369 		 * This allows us to have a TCP Data Payload which is a
5370 		 * multiple of 8 regardless of what combination of TCP Options
5371 		 * are in use (always a multiple of 4 bytes) which is
5372 		 * important for performance reasons.  For instance, if no
5373 		 * options are in use, then we have a 20-byte IP header and a
5374 		 * 20-byte TCP header.  In this case, a 1500-byte MSS would
5375 		 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
5376 		 * which is not a multiple of 8.  So using an MSS of 1488 in
5377 		 * this case results in a TCP Data Payload of 1448 bytes which
5378 		 * is a multiple of 8.  On the other hand, if 12-byte TCP Time
5379 		 * Stamps have been negotiated, then an MTU of 1500 bytes
5380 		 * results in a TCP Data Payload of 1448 bytes which, as
5381 		 * above, is a multiple of 8 bytes ...
5382 		 */
5383 		for (i = 0; i < NMTUS; i++)
5384 			if (adap->params.mtus[i] == 1492) {
5385 				adap->params.mtus[i] = 1488;
5386 				break;
5387 			}
5388 
5389 		t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
5390 			     adap->params.b_wnd);
5391 	}
5392 	t4_init_sge_params(adap);
5393 	adap->flags |= CXGB4_FW_OK;
5394 	t4_init_tp_params(adap, true);
5395 	return 0;
5396 
5397 	/*
5398 	 * Something bad happened.  If a command timed out or failed with EIO
5399 	 * FW does not operate within its spec or something catastrophic
5400 	 * happened to HW/FW, stop issuing commands.
5401 	 */
5402 bye:
5403 	adap_free_hma_mem(adap);
5404 	kfree(adap->sge.egr_map);
5405 	kfree(adap->sge.ingr_map);
5406 	bitmap_free(adap->sge.starving_fl);
5407 	bitmap_free(adap->sge.txq_maperr);
5408 #ifdef CONFIG_DEBUG_FS
5409 	bitmap_free(adap->sge.blocked_fl);
5410 #endif
5411 	if (ret != -ETIMEDOUT && ret != -EIO)
5412 		t4_fw_bye(adap, adap->mbox);
5413 	return ret;
5414 }
5415 
5416 /* EEH callbacks */
5417 
eeh_err_detected(struct pci_dev * pdev,pci_channel_state_t state)5418 static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
5419 					 pci_channel_state_t state)
5420 {
5421 	int i;
5422 	struct adapter *adap = pci_get_drvdata(pdev);
5423 
5424 	if (!adap)
5425 		goto out;
5426 
5427 	rtnl_lock();
5428 	adap->flags &= ~CXGB4_FW_OK;
5429 	notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
5430 	spin_lock(&adap->stats_lock);
5431 	for_each_port(adap, i) {
5432 		struct net_device *dev = adap->port[i];
5433 		if (dev) {
5434 			netif_device_detach(dev);
5435 			netif_carrier_off(dev);
5436 		}
5437 	}
5438 	spin_unlock(&adap->stats_lock);
5439 	disable_interrupts(adap);
5440 	if (adap->flags & CXGB4_FULL_INIT_DONE)
5441 		cxgb_down(adap);
5442 	rtnl_unlock();
5443 	if ((adap->flags & CXGB4_DEV_ENABLED)) {
5444 		pci_disable_device(pdev);
5445 		adap->flags &= ~CXGB4_DEV_ENABLED;
5446 	}
5447 out:	return state == pci_channel_io_perm_failure ?
5448 		PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
5449 }
5450 
eeh_slot_reset(struct pci_dev * pdev)5451 static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
5452 {
5453 	int i, ret;
5454 	struct fw_caps_config_cmd c;
5455 	struct adapter *adap = pci_get_drvdata(pdev);
5456 
5457 	if (!adap) {
5458 		pci_restore_state(pdev);
5459 		pci_save_state(pdev);
5460 		return PCI_ERS_RESULT_RECOVERED;
5461 	}
5462 
5463 	if (!(adap->flags & CXGB4_DEV_ENABLED)) {
5464 		if (pci_enable_device(pdev)) {
5465 			dev_err(&pdev->dev, "Cannot reenable PCI "
5466 					    "device after reset\n");
5467 			return PCI_ERS_RESULT_DISCONNECT;
5468 		}
5469 		adap->flags |= CXGB4_DEV_ENABLED;
5470 	}
5471 
5472 	pci_set_master(pdev);
5473 	pci_restore_state(pdev);
5474 	pci_save_state(pdev);
5475 
5476 	if (t4_wait_dev_ready(adap->regs) < 0)
5477 		return PCI_ERS_RESULT_DISCONNECT;
5478 	if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
5479 		return PCI_ERS_RESULT_DISCONNECT;
5480 	adap->flags |= CXGB4_FW_OK;
5481 	if (adap_init1(adap, &c))
5482 		return PCI_ERS_RESULT_DISCONNECT;
5483 
5484 	for_each_port(adap, i) {
5485 		struct port_info *pi = adap2pinfo(adap, i);
5486 		u8 vivld = 0, vin = 0;
5487 
5488 		ret = t4_alloc_vi(adap, adap->mbox, pi->tx_chan, adap->pf, 0, 1,
5489 				  NULL, NULL, &vivld, &vin);
5490 		if (ret < 0)
5491 			return PCI_ERS_RESULT_DISCONNECT;
5492 		pi->viid = ret;
5493 		pi->xact_addr_filt = -1;
5494 		/* If fw supports returning the VIN as part of FW_VI_CMD,
5495 		 * save the returned values.
5496 		 */
5497 		if (adap->params.viid_smt_extn_support) {
5498 			pi->vivld = vivld;
5499 			pi->vin = vin;
5500 		} else {
5501 			/* Retrieve the values from VIID */
5502 			pi->vivld = FW_VIID_VIVLD_G(pi->viid);
5503 			pi->vin = FW_VIID_VIN_G(pi->viid);
5504 		}
5505 	}
5506 
5507 	t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
5508 		     adap->params.b_wnd);
5509 	setup_memwin(adap);
5510 	if (cxgb_up(adap))
5511 		return PCI_ERS_RESULT_DISCONNECT;
5512 	return PCI_ERS_RESULT_RECOVERED;
5513 }
5514 
eeh_resume(struct pci_dev * pdev)5515 static void eeh_resume(struct pci_dev *pdev)
5516 {
5517 	int i;
5518 	struct adapter *adap = pci_get_drvdata(pdev);
5519 
5520 	if (!adap)
5521 		return;
5522 
5523 	rtnl_lock();
5524 	for_each_port(adap, i) {
5525 		struct net_device *dev = adap->port[i];
5526 		if (dev) {
5527 			if (netif_running(dev)) {
5528 				link_start(dev);
5529 				cxgb_set_rxmode(dev);
5530 			}
5531 			netif_device_attach(dev);
5532 		}
5533 	}
5534 	rtnl_unlock();
5535 }
5536 
eeh_reset_prepare(struct pci_dev * pdev)5537 static void eeh_reset_prepare(struct pci_dev *pdev)
5538 {
5539 	struct adapter *adapter = pci_get_drvdata(pdev);
5540 	int i;
5541 
5542 	if (adapter->pf != 4)
5543 		return;
5544 
5545 	adapter->flags &= ~CXGB4_FW_OK;
5546 
5547 	notify_ulds(adapter, CXGB4_STATE_DOWN);
5548 
5549 	for_each_port(adapter, i)
5550 		if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5551 			cxgb_close(adapter->port[i]);
5552 
5553 	disable_interrupts(adapter);
5554 	cxgb4_free_mps_ref_entries(adapter);
5555 
5556 	adap_free_hma_mem(adapter);
5557 
5558 	if (adapter->flags & CXGB4_FULL_INIT_DONE)
5559 		cxgb_down(adapter);
5560 }
5561 
eeh_reset_done(struct pci_dev * pdev)5562 static void eeh_reset_done(struct pci_dev *pdev)
5563 {
5564 	struct adapter *adapter = pci_get_drvdata(pdev);
5565 	int err, i;
5566 
5567 	if (adapter->pf != 4)
5568 		return;
5569 
5570 	err = t4_wait_dev_ready(adapter->regs);
5571 	if (err < 0) {
5572 		dev_err(adapter->pdev_dev,
5573 			"Device not ready, err %d", err);
5574 		return;
5575 	}
5576 
5577 	setup_memwin(adapter);
5578 
5579 	err = adap_init0(adapter, 1);
5580 	if (err) {
5581 		dev_err(adapter->pdev_dev,
5582 			"Adapter init failed, err %d", err);
5583 		return;
5584 	}
5585 
5586 	setup_memwin_rdma(adapter);
5587 
5588 	if (adapter->flags & CXGB4_FW_OK) {
5589 		err = t4_port_init(adapter, adapter->pf, adapter->pf, 0);
5590 		if (err) {
5591 			dev_err(adapter->pdev_dev,
5592 				"Port init failed, err %d", err);
5593 			return;
5594 		}
5595 	}
5596 
5597 	err = cfg_queues(adapter);
5598 	if (err) {
5599 		dev_err(adapter->pdev_dev,
5600 			"Config queues failed, err %d", err);
5601 		return;
5602 	}
5603 
5604 	cxgb4_init_mps_ref_entries(adapter);
5605 
5606 	err = setup_fw_sge_queues(adapter);
5607 	if (err) {
5608 		dev_err(adapter->pdev_dev,
5609 			"FW sge queue allocation failed, err %d", err);
5610 		return;
5611 	}
5612 
5613 	for_each_port(adapter, i)
5614 		if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5615 			cxgb_open(adapter->port[i]);
5616 }
5617 
5618 static const struct pci_error_handlers cxgb4_eeh = {
5619 	.error_detected = eeh_err_detected,
5620 	.slot_reset     = eeh_slot_reset,
5621 	.resume         = eeh_resume,
5622 	.reset_prepare  = eeh_reset_prepare,
5623 	.reset_done     = eeh_reset_done,
5624 };
5625 
5626 /* Return true if the Link Configuration supports "High Speeds" (those greater
5627  * than 1Gb/s).
5628  */
is_x_10g_port(const struct link_config * lc)5629 static inline bool is_x_10g_port(const struct link_config *lc)
5630 {
5631 	unsigned int speeds, high_speeds;
5632 
5633 	speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
5634 	high_speeds = speeds &
5635 			~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
5636 
5637 	return high_speeds != 0;
5638 }
5639 
5640 /* Perform default configuration of DMA queues depending on the number and type
5641  * of ports we found and the number of available CPUs.  Most settings can be
5642  * modified by the admin prior to actual use.
5643  */
cfg_queues(struct adapter * adap)5644 static int cfg_queues(struct adapter *adap)
5645 {
5646 	u32 avail_qsets, avail_eth_qsets, avail_uld_qsets;
5647 	u32 ncpus = num_online_cpus();
5648 	u32 niqflint, neq, num_ulds;
5649 	struct sge *s = &adap->sge;
5650 	u32 i, n10g = 0, qidx = 0;
5651 	u32 q10g = 0, q1g;
5652 
5653 	/* Reduce memory usage in kdump environment, disable all offload. */
5654 	if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
5655 		adap->params.offload = 0;
5656 		adap->params.crypto = 0;
5657 		adap->params.ethofld = 0;
5658 	}
5659 
5660 	/* Calculate the number of Ethernet Queue Sets available based on
5661 	 * resources provisioned for us.  We always have an Asynchronous
5662 	 * Firmware Event Ingress Queue.  If we're operating in MSI or Legacy
5663 	 * IRQ Pin Interrupt mode, then we'll also have a Forwarded Interrupt
5664 	 * Ingress Queue.  Meanwhile, we need two Egress Queues for each
5665 	 * Queue Set: one for the Free List and one for the Ethernet TX Queue.
5666 	 *
5667 	 * Note that we should also take into account all of the various
5668 	 * Offload Queues.  But, in any situation where we're operating in
5669 	 * a Resource Constrained Provisioning environment, doing any Offload
5670 	 * at all is problematic ...
5671 	 */
5672 	niqflint = adap->params.pfres.niqflint - 1;
5673 	if (!(adap->flags & CXGB4_USING_MSIX))
5674 		niqflint--;
5675 	neq = adap->params.pfres.neq / 2;
5676 	avail_qsets = min(niqflint, neq);
5677 
5678 	if (avail_qsets < adap->params.nports) {
5679 		dev_err(adap->pdev_dev, "avail_eth_qsets=%d < nports=%d\n",
5680 			avail_qsets, adap->params.nports);
5681 		return -ENOMEM;
5682 	}
5683 
5684 	/* Count the number of 10Gb/s or better ports */
5685 	for_each_port(adap, i)
5686 		n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
5687 
5688 	avail_eth_qsets = min_t(u32, avail_qsets, MAX_ETH_QSETS);
5689 
5690 	/* We default to 1 queue per non-10G port and up to # of cores queues
5691 	 * per 10G port.
5692 	 */
5693 	if (n10g)
5694 		q10g = (avail_eth_qsets - (adap->params.nports - n10g)) / n10g;
5695 
5696 #ifdef CONFIG_CHELSIO_T4_DCB
5697 	/* For Data Center Bridging support we need to be able to support up
5698 	 * to 8 Traffic Priorities; each of which will be assigned to its
5699 	 * own TX Queue in order to prevent Head-Of-Line Blocking.
5700 	 */
5701 	q1g = 8;
5702 	if (adap->params.nports * 8 > avail_eth_qsets) {
5703 		dev_err(adap->pdev_dev, "DCB avail_eth_qsets=%d < %d!\n",
5704 			avail_eth_qsets, adap->params.nports * 8);
5705 		return -ENOMEM;
5706 	}
5707 
5708 	if (adap->params.nports * ncpus < avail_eth_qsets)
5709 		q10g = max(8U, ncpus);
5710 	else
5711 		q10g = max(8U, q10g);
5712 
5713 	while ((q10g * n10g) >
5714 	       (avail_eth_qsets - (adap->params.nports - n10g) * q1g))
5715 		q10g--;
5716 
5717 #else /* !CONFIG_CHELSIO_T4_DCB */
5718 	q1g = 1;
5719 	q10g = min(q10g, ncpus);
5720 #endif /* !CONFIG_CHELSIO_T4_DCB */
5721 	if (is_kdump_kernel()) {
5722 		q10g = 1;
5723 		q1g = 1;
5724 	}
5725 
5726 	for_each_port(adap, i) {
5727 		struct port_info *pi = adap2pinfo(adap, i);
5728 
5729 		pi->first_qset = qidx;
5730 		pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : q1g;
5731 		qidx += pi->nqsets;
5732 	}
5733 
5734 	s->ethqsets = qidx;
5735 	s->max_ethqsets = qidx;   /* MSI-X may lower it later */
5736 	avail_qsets -= qidx;
5737 
5738 	if (is_uld(adap)) {
5739 		/* For offload we use 1 queue/channel if all ports are up to 1G,
5740 		 * otherwise we divide all available queues amongst the channels
5741 		 * capped by the number of available cores.
5742 		 */
5743 		num_ulds = adap->num_uld + adap->num_ofld_uld;
5744 		i = min_t(u32, MAX_OFLD_QSETS, ncpus);
5745 		avail_uld_qsets = roundup(i, adap->params.nports);
5746 		if (avail_qsets < num_ulds * adap->params.nports) {
5747 			adap->params.offload = 0;
5748 			adap->params.crypto = 0;
5749 			s->ofldqsets = 0;
5750 		} else if (avail_qsets < num_ulds * avail_uld_qsets || !n10g) {
5751 			s->ofldqsets = adap->params.nports;
5752 		} else {
5753 			s->ofldqsets = avail_uld_qsets;
5754 		}
5755 
5756 		avail_qsets -= num_ulds * s->ofldqsets;
5757 	}
5758 
5759 	/* ETHOFLD Queues used for QoS offload should follow same
5760 	 * allocation scheme as normal Ethernet Queues.
5761 	 */
5762 	if (is_ethofld(adap)) {
5763 		if (avail_qsets < s->max_ethqsets) {
5764 			adap->params.ethofld = 0;
5765 			s->eoqsets = 0;
5766 		} else {
5767 			s->eoqsets = s->max_ethqsets;
5768 		}
5769 		avail_qsets -= s->eoqsets;
5770 	}
5771 
5772 	/* Mirror queues must follow same scheme as normal Ethernet
5773 	 * Queues, when there are enough queues available. Otherwise,
5774 	 * allocate at least 1 queue per port. If even 1 queue is not
5775 	 * available, then disable mirror queues support.
5776 	 */
5777 	if (avail_qsets >= s->max_ethqsets)
5778 		s->mirrorqsets = s->max_ethqsets;
5779 	else if (avail_qsets >= adap->params.nports)
5780 		s->mirrorqsets = adap->params.nports;
5781 	else
5782 		s->mirrorqsets = 0;
5783 	avail_qsets -= s->mirrorqsets;
5784 
5785 	for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
5786 		struct sge_eth_rxq *r = &s->ethrxq[i];
5787 
5788 		init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
5789 		r->fl.size = 72;
5790 	}
5791 
5792 	for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
5793 		s->ethtxq[i].q.size = 1024;
5794 
5795 	for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
5796 		s->ctrlq[i].q.size = 512;
5797 
5798 	if (!is_t4(adap->params.chip))
5799 		s->ptptxq.q.size = 8;
5800 
5801 	init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
5802 	init_rspq(adap, &s->intrq, 0, 1, 512, 64);
5803 
5804 	return 0;
5805 }
5806 
5807 /*
5808  * Reduce the number of Ethernet queues across all ports to at most n.
5809  * n provides at least one queue per port.
5810  */
reduce_ethqs(struct adapter * adap,int n)5811 static void reduce_ethqs(struct adapter *adap, int n)
5812 {
5813 	int i;
5814 	struct port_info *pi;
5815 
5816 	while (n < adap->sge.ethqsets)
5817 		for_each_port(adap, i) {
5818 			pi = adap2pinfo(adap, i);
5819 			if (pi->nqsets > 1) {
5820 				pi->nqsets--;
5821 				adap->sge.ethqsets--;
5822 				if (adap->sge.ethqsets <= n)
5823 					break;
5824 			}
5825 		}
5826 
5827 	n = 0;
5828 	for_each_port(adap, i) {
5829 		pi = adap2pinfo(adap, i);
5830 		pi->first_qset = n;
5831 		n += pi->nqsets;
5832 	}
5833 }
5834 
alloc_msix_info(struct adapter * adap,u32 num_vec)5835 static int alloc_msix_info(struct adapter *adap, u32 num_vec)
5836 {
5837 	struct msix_info *msix_info;
5838 
5839 	msix_info = kcalloc(num_vec, sizeof(*msix_info), GFP_KERNEL);
5840 	if (!msix_info)
5841 		return -ENOMEM;
5842 
5843 	adap->msix_bmap.msix_bmap = bitmap_zalloc(num_vec, GFP_KERNEL);
5844 	if (!adap->msix_bmap.msix_bmap) {
5845 		kfree(msix_info);
5846 		return -ENOMEM;
5847 	}
5848 
5849 	spin_lock_init(&adap->msix_bmap.lock);
5850 	adap->msix_bmap.mapsize = num_vec;
5851 
5852 	adap->msix_info = msix_info;
5853 	return 0;
5854 }
5855 
free_msix_info(struct adapter * adap)5856 static void free_msix_info(struct adapter *adap)
5857 {
5858 	bitmap_free(adap->msix_bmap.msix_bmap);
5859 	kfree(adap->msix_info);
5860 }
5861 
cxgb4_get_msix_idx_from_bmap(struct adapter * adap)5862 int cxgb4_get_msix_idx_from_bmap(struct adapter *adap)
5863 {
5864 	struct msix_bmap *bmap = &adap->msix_bmap;
5865 	unsigned int msix_idx;
5866 	unsigned long flags;
5867 
5868 	spin_lock_irqsave(&bmap->lock, flags);
5869 	msix_idx = find_first_zero_bit(bmap->msix_bmap, bmap->mapsize);
5870 	if (msix_idx < bmap->mapsize) {
5871 		__set_bit(msix_idx, bmap->msix_bmap);
5872 	} else {
5873 		spin_unlock_irqrestore(&bmap->lock, flags);
5874 		return -ENOSPC;
5875 	}
5876 
5877 	spin_unlock_irqrestore(&bmap->lock, flags);
5878 	return msix_idx;
5879 }
5880 
cxgb4_free_msix_idx_in_bmap(struct adapter * adap,unsigned int msix_idx)5881 void cxgb4_free_msix_idx_in_bmap(struct adapter *adap,
5882 				 unsigned int msix_idx)
5883 {
5884 	struct msix_bmap *bmap = &adap->msix_bmap;
5885 	unsigned long flags;
5886 
5887 	spin_lock_irqsave(&bmap->lock, flags);
5888 	__clear_bit(msix_idx, bmap->msix_bmap);
5889 	spin_unlock_irqrestore(&bmap->lock, flags);
5890 }
5891 
5892 /* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
5893 #define EXTRA_VECS 2
5894 
enable_msix(struct adapter * adap)5895 static int enable_msix(struct adapter *adap)
5896 {
5897 	u32 eth_need, uld_need = 0, ethofld_need = 0, mirror_need = 0;
5898 	u32 ethqsets = 0, ofldqsets = 0, eoqsets = 0, mirrorqsets = 0;
5899 	u8 num_uld = 0, nchan = adap->params.nports;
5900 	u32 i, want, need, num_vec;
5901 	struct sge *s = &adap->sge;
5902 	struct msix_entry *entries;
5903 	struct port_info *pi;
5904 	int allocated, ret;
5905 
5906 	want = s->max_ethqsets;
5907 #ifdef CONFIG_CHELSIO_T4_DCB
5908 	/* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
5909 	 * each port.
5910 	 */
5911 	need = 8 * nchan;
5912 #else
5913 	need = nchan;
5914 #endif
5915 	eth_need = need;
5916 	if (is_uld(adap)) {
5917 		num_uld = adap->num_ofld_uld + adap->num_uld;
5918 		want += num_uld * s->ofldqsets;
5919 		uld_need = num_uld * nchan;
5920 		need += uld_need;
5921 	}
5922 
5923 	if (is_ethofld(adap)) {
5924 		want += s->eoqsets;
5925 		ethofld_need = eth_need;
5926 		need += ethofld_need;
5927 	}
5928 
5929 	if (s->mirrorqsets) {
5930 		want += s->mirrorqsets;
5931 		mirror_need = nchan;
5932 		need += mirror_need;
5933 	}
5934 
5935 	want += EXTRA_VECS;
5936 	need += EXTRA_VECS;
5937 
5938 	entries = kmalloc_array(want, sizeof(*entries), GFP_KERNEL);
5939 	if (!entries)
5940 		return -ENOMEM;
5941 
5942 	for (i = 0; i < want; i++)
5943 		entries[i].entry = i;
5944 
5945 	allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
5946 	if (allocated < 0) {
5947 		/* Disable offload and attempt to get vectors for NIC
5948 		 * only mode.
5949 		 */
5950 		want = s->max_ethqsets + EXTRA_VECS;
5951 		need = eth_need + EXTRA_VECS;
5952 		allocated = pci_enable_msix_range(adap->pdev, entries,
5953 						  need, want);
5954 		if (allocated < 0) {
5955 			dev_info(adap->pdev_dev,
5956 				 "Disabling MSI-X due to insufficient MSI-X vectors\n");
5957 			ret = allocated;
5958 			goto out_free;
5959 		}
5960 
5961 		dev_info(adap->pdev_dev,
5962 			 "Disabling offload due to insufficient MSI-X vectors\n");
5963 		adap->params.offload = 0;
5964 		adap->params.crypto = 0;
5965 		adap->params.ethofld = 0;
5966 		s->ofldqsets = 0;
5967 		s->eoqsets = 0;
5968 		s->mirrorqsets = 0;
5969 		uld_need = 0;
5970 		ethofld_need = 0;
5971 		mirror_need = 0;
5972 	}
5973 
5974 	num_vec = allocated;
5975 	if (num_vec < want) {
5976 		/* Distribute available vectors to the various queue groups.
5977 		 * Every group gets its minimum requirement and NIC gets top
5978 		 * priority for leftovers.
5979 		 */
5980 		ethqsets = eth_need;
5981 		if (is_uld(adap))
5982 			ofldqsets = nchan;
5983 		if (is_ethofld(adap))
5984 			eoqsets = ethofld_need;
5985 		if (s->mirrorqsets)
5986 			mirrorqsets = mirror_need;
5987 
5988 		num_vec -= need;
5989 		while (num_vec) {
5990 			if (num_vec < eth_need + ethofld_need ||
5991 			    ethqsets > s->max_ethqsets)
5992 				break;
5993 
5994 			for_each_port(adap, i) {
5995 				pi = adap2pinfo(adap, i);
5996 				if (pi->nqsets < 2)
5997 					continue;
5998 
5999 				ethqsets++;
6000 				num_vec--;
6001 				if (ethofld_need) {
6002 					eoqsets++;
6003 					num_vec--;
6004 				}
6005 			}
6006 		}
6007 
6008 		if (is_uld(adap)) {
6009 			while (num_vec) {
6010 				if (num_vec < uld_need ||
6011 				    ofldqsets > s->ofldqsets)
6012 					break;
6013 
6014 				ofldqsets++;
6015 				num_vec -= uld_need;
6016 			}
6017 		}
6018 
6019 		if (s->mirrorqsets) {
6020 			while (num_vec) {
6021 				if (num_vec < mirror_need ||
6022 				    mirrorqsets > s->mirrorqsets)
6023 					break;
6024 
6025 				mirrorqsets++;
6026 				num_vec -= mirror_need;
6027 			}
6028 		}
6029 	} else {
6030 		ethqsets = s->max_ethqsets;
6031 		if (is_uld(adap))
6032 			ofldqsets = s->ofldqsets;
6033 		if (is_ethofld(adap))
6034 			eoqsets = s->eoqsets;
6035 		if (s->mirrorqsets)
6036 			mirrorqsets = s->mirrorqsets;
6037 	}
6038 
6039 	if (ethqsets < s->max_ethqsets) {
6040 		s->max_ethqsets = ethqsets;
6041 		reduce_ethqs(adap, ethqsets);
6042 	}
6043 
6044 	if (is_uld(adap)) {
6045 		s->ofldqsets = ofldqsets;
6046 		s->nqs_per_uld = s->ofldqsets;
6047 	}
6048 
6049 	if (is_ethofld(adap))
6050 		s->eoqsets = eoqsets;
6051 
6052 	if (s->mirrorqsets) {
6053 		s->mirrorqsets = mirrorqsets;
6054 		for_each_port(adap, i) {
6055 			pi = adap2pinfo(adap, i);
6056 			pi->nmirrorqsets = s->mirrorqsets / nchan;
6057 			mutex_init(&pi->vi_mirror_mutex);
6058 		}
6059 	}
6060 
6061 	/* map for msix */
6062 	ret = alloc_msix_info(adap, allocated);
6063 	if (ret)
6064 		goto out_disable_msix;
6065 
6066 	for (i = 0; i < allocated; i++) {
6067 		adap->msix_info[i].vec = entries[i].vector;
6068 		adap->msix_info[i].idx = i;
6069 	}
6070 
6071 	dev_info(adap->pdev_dev,
6072 		 "%d MSI-X vectors allocated, nic %d eoqsets %d per uld %d mirrorqsets %d\n",
6073 		 allocated, s->max_ethqsets, s->eoqsets, s->nqs_per_uld,
6074 		 s->mirrorqsets);
6075 
6076 	kfree(entries);
6077 	return 0;
6078 
6079 out_disable_msix:
6080 	pci_disable_msix(adap->pdev);
6081 
6082 out_free:
6083 	kfree(entries);
6084 	return ret;
6085 }
6086 
6087 #undef EXTRA_VECS
6088 
init_rss(struct adapter * adap)6089 static int init_rss(struct adapter *adap)
6090 {
6091 	unsigned int i;
6092 	int err;
6093 
6094 	err = t4_init_rss_mode(adap, adap->mbox);
6095 	if (err)
6096 		return err;
6097 
6098 	for_each_port(adap, i) {
6099 		struct port_info *pi = adap2pinfo(adap, i);
6100 
6101 		pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
6102 		if (!pi->rss)
6103 			return -ENOMEM;
6104 	}
6105 	return 0;
6106 }
6107 
6108 /* Dump basic information about the adapter */
print_adapter_info(struct adapter * adapter)6109 static void print_adapter_info(struct adapter *adapter)
6110 {
6111 	/* Hardware/Firmware/etc. Version/Revision IDs */
6112 	t4_dump_version_info(adapter);
6113 
6114 	/* Software/Hardware configuration */
6115 	dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
6116 		 is_offload(adapter) ? "R" : "",
6117 		 ((adapter->flags & CXGB4_USING_MSIX) ? "MSI-X" :
6118 		  (adapter->flags & CXGB4_USING_MSI) ? "MSI" : ""),
6119 		 is_offload(adapter) ? "Offload" : "non-Offload");
6120 }
6121 
print_port_info(const struct net_device * dev)6122 static void print_port_info(const struct net_device *dev)
6123 {
6124 	char buf[80];
6125 	char *bufp = buf;
6126 	const struct port_info *pi = netdev_priv(dev);
6127 	const struct adapter *adap = pi->adapter;
6128 
6129 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
6130 		bufp += sprintf(bufp, "100M/");
6131 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
6132 		bufp += sprintf(bufp, "1G/");
6133 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
6134 		bufp += sprintf(bufp, "10G/");
6135 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
6136 		bufp += sprintf(bufp, "25G/");
6137 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
6138 		bufp += sprintf(bufp, "40G/");
6139 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
6140 		bufp += sprintf(bufp, "50G/");
6141 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
6142 		bufp += sprintf(bufp, "100G/");
6143 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
6144 		bufp += sprintf(bufp, "200G/");
6145 	if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
6146 		bufp += sprintf(bufp, "400G/");
6147 	if (bufp != buf)
6148 		--bufp;
6149 	sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
6150 
6151 	netdev_info(dev, "Chelsio %s %s\n", adap->params.vpd.id, buf);
6152 }
6153 
6154 /*
6155  * Free the following resources:
6156  * - memory used for tables
6157  * - MSI/MSI-X
6158  * - net devices
6159  * - resources FW is holding for us
6160  */
free_some_resources(struct adapter * adapter)6161 static void free_some_resources(struct adapter *adapter)
6162 {
6163 	unsigned int i;
6164 
6165 	kvfree(adapter->smt);
6166 	kvfree(adapter->l2t);
6167 	kvfree(adapter->srq);
6168 	t4_cleanup_sched(adapter);
6169 	kvfree(adapter->tids.tid_tab);
6170 	cxgb4_cleanup_tc_matchall(adapter);
6171 	cxgb4_cleanup_tc_mqprio(adapter);
6172 	cxgb4_cleanup_tc_flower(adapter);
6173 	cxgb4_cleanup_tc_u32(adapter);
6174 	cxgb4_cleanup_ethtool_filters(adapter);
6175 	kfree(adapter->sge.egr_map);
6176 	kfree(adapter->sge.ingr_map);
6177 	bitmap_free(adapter->sge.starving_fl);
6178 	bitmap_free(adapter->sge.txq_maperr);
6179 #ifdef CONFIG_DEBUG_FS
6180 	bitmap_free(adapter->sge.blocked_fl);
6181 #endif
6182 	disable_msi(adapter);
6183 
6184 	for_each_port(adapter, i)
6185 		if (adapter->port[i]) {
6186 			struct port_info *pi = adap2pinfo(adapter, i);
6187 
6188 			if (pi->viid != 0)
6189 				t4_free_vi(adapter, adapter->mbox, adapter->pf,
6190 					   0, pi->viid);
6191 			kfree(adap2pinfo(adapter, i)->rss);
6192 			free_netdev(adapter->port[i]);
6193 		}
6194 	if (adapter->flags & CXGB4_FW_OK)
6195 		t4_fw_bye(adapter, adapter->pf);
6196 }
6197 
6198 #define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN | \
6199 		   NETIF_F_GSO_UDP_L4)
6200 #define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
6201 		   NETIF_F_GRO | NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
6202 #define SEGMENT_SIZE 128
6203 
t4_get_chip_type(struct adapter * adap,int ver)6204 static int t4_get_chip_type(struct adapter *adap, int ver)
6205 {
6206 	u32 pl_rev = REV_G(t4_read_reg(adap, PL_REV_A));
6207 
6208 	switch (ver) {
6209 	case CHELSIO_T4:
6210 		return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
6211 	case CHELSIO_T5:
6212 		return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
6213 	case CHELSIO_T6:
6214 		return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
6215 	default:
6216 		break;
6217 	}
6218 	return -EINVAL;
6219 }
6220 
6221 #ifdef CONFIG_PCI_IOV
cxgb4_mgmt_setup(struct net_device * dev)6222 static void cxgb4_mgmt_setup(struct net_device *dev)
6223 {
6224 	dev->type = ARPHRD_NONE;
6225 	dev->mtu = 0;
6226 	dev->hard_header_len = 0;
6227 	dev->addr_len = 0;
6228 	dev->tx_queue_len = 0;
6229 	dev->flags |= IFF_NOARP;
6230 	dev->priv_flags |= IFF_NO_QUEUE;
6231 
6232 	/* Initialize the device structure. */
6233 	dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
6234 	dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
6235 }
6236 
cxgb4_iov_configure(struct pci_dev * pdev,int num_vfs)6237 static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
6238 {
6239 	struct adapter *adap = pci_get_drvdata(pdev);
6240 	int err = 0;
6241 	int current_vfs = pci_num_vf(pdev);
6242 	u32 pcie_fw;
6243 
6244 	pcie_fw = readl(adap->regs + PCIE_FW_A);
6245 	/* Check if fw is initialized */
6246 	if (!(pcie_fw & PCIE_FW_INIT_F)) {
6247 		dev_warn(&pdev->dev, "Device not initialized\n");
6248 		return -EOPNOTSUPP;
6249 	}
6250 
6251 	/* If any of the VF's is already assigned to Guest OS, then
6252 	 * SRIOV for the same cannot be modified
6253 	 */
6254 	if (current_vfs && pci_vfs_assigned(pdev)) {
6255 		dev_err(&pdev->dev,
6256 			"Cannot modify SR-IOV while VFs are assigned\n");
6257 		return current_vfs;
6258 	}
6259 	/* Note that the upper-level code ensures that we're never called with
6260 	 * a non-zero "num_vfs" when we already have VFs instantiated.  But
6261 	 * it never hurts to code defensively.
6262 	 */
6263 	if (num_vfs != 0 && current_vfs != 0)
6264 		return -EBUSY;
6265 
6266 	/* Nothing to do for no change. */
6267 	if (num_vfs == current_vfs)
6268 		return num_vfs;
6269 
6270 	/* Disable SRIOV when zero is passed. */
6271 	if (!num_vfs) {
6272 		pci_disable_sriov(pdev);
6273 		/* free VF Management Interface */
6274 		unregister_netdev(adap->port[0]);
6275 		free_netdev(adap->port[0]);
6276 		adap->port[0] = NULL;
6277 
6278 		/* free VF resources */
6279 		adap->num_vfs = 0;
6280 		kfree(adap->vfinfo);
6281 		adap->vfinfo = NULL;
6282 		return 0;
6283 	}
6284 
6285 	if (!current_vfs) {
6286 		struct fw_pfvf_cmd port_cmd, port_rpl;
6287 		struct net_device *netdev;
6288 		unsigned int pmask, port;
6289 		struct pci_dev *pbridge;
6290 		struct port_info *pi;
6291 		char name[IFNAMSIZ];
6292 		u32 devcap2;
6293 		u16 flags;
6294 
6295 		/* If we want to instantiate Virtual Functions, then our
6296 		 * parent bridge's PCI-E needs to support Alternative Routing
6297 		 * ID (ARI) because our VFs will show up at function offset 8
6298 		 * and above.
6299 		 */
6300 		pbridge = pdev->bus->self;
6301 		pcie_capability_read_word(pbridge, PCI_EXP_FLAGS, &flags);
6302 		pcie_capability_read_dword(pbridge, PCI_EXP_DEVCAP2, &devcap2);
6303 
6304 		if ((flags & PCI_EXP_FLAGS_VERS) < 2 ||
6305 		    !(devcap2 & PCI_EXP_DEVCAP2_ARI)) {
6306 			/* Our parent bridge does not support ARI so issue a
6307 			 * warning and skip instantiating the VFs.  They
6308 			 * won't be reachable.
6309 			 */
6310 			dev_warn(&pdev->dev, "Parent bridge %02x:%02x.%x doesn't support ARI; can't instantiate Virtual Functions\n",
6311 				 pbridge->bus->number, PCI_SLOT(pbridge->devfn),
6312 				 PCI_FUNC(pbridge->devfn));
6313 			return -ENOTSUPP;
6314 		}
6315 		memset(&port_cmd, 0, sizeof(port_cmd));
6316 		port_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
6317 						 FW_CMD_REQUEST_F |
6318 						 FW_CMD_READ_F |
6319 						 FW_PFVF_CMD_PFN_V(adap->pf) |
6320 						 FW_PFVF_CMD_VFN_V(0));
6321 		port_cmd.retval_len16 = cpu_to_be32(FW_LEN16(port_cmd));
6322 		err = t4_wr_mbox(adap, adap->mbox, &port_cmd, sizeof(port_cmd),
6323 				 &port_rpl);
6324 		if (err)
6325 			return err;
6326 		pmask = FW_PFVF_CMD_PMASK_G(be32_to_cpu(port_rpl.type_to_neq));
6327 		port = ffs(pmask) - 1;
6328 		/* Allocate VF Management Interface. */
6329 		snprintf(name, IFNAMSIZ, "mgmtpf%d,%d", adap->adap_idx,
6330 			 adap->pf);
6331 		netdev = alloc_netdev(sizeof(struct port_info),
6332 				      name, NET_NAME_UNKNOWN, cxgb4_mgmt_setup);
6333 		if (!netdev)
6334 			return -ENOMEM;
6335 
6336 		pi = netdev_priv(netdev);
6337 		pi->adapter = adap;
6338 		pi->lport = port;
6339 		pi->tx_chan = port;
6340 		SET_NETDEV_DEV(netdev, &pdev->dev);
6341 
6342 		adap->port[0] = netdev;
6343 		pi->port_id = 0;
6344 
6345 		err = register_netdev(adap->port[0]);
6346 		if (err) {
6347 			pr_info("Unable to register VF mgmt netdev %s\n", name);
6348 			free_netdev(adap->port[0]);
6349 			adap->port[0] = NULL;
6350 			return err;
6351 		}
6352 		/* Allocate and set up VF Information. */
6353 		adap->vfinfo = kcalloc(pci_sriov_get_totalvfs(pdev),
6354 				       sizeof(struct vf_info), GFP_KERNEL);
6355 		if (!adap->vfinfo) {
6356 			unregister_netdev(adap->port[0]);
6357 			free_netdev(adap->port[0]);
6358 			adap->port[0] = NULL;
6359 			return -ENOMEM;
6360 		}
6361 		cxgb4_mgmt_fill_vf_station_mac_addr(adap);
6362 	}
6363 	/* Instantiate the requested number of VFs. */
6364 	err = pci_enable_sriov(pdev, num_vfs);
6365 	if (err) {
6366 		pr_info("Unable to instantiate %d VFs\n", num_vfs);
6367 		if (!current_vfs) {
6368 			unregister_netdev(adap->port[0]);
6369 			free_netdev(adap->port[0]);
6370 			adap->port[0] = NULL;
6371 			kfree(adap->vfinfo);
6372 			adap->vfinfo = NULL;
6373 		}
6374 		return err;
6375 	}
6376 
6377 	adap->num_vfs = num_vfs;
6378 	return num_vfs;
6379 }
6380 #endif /* CONFIG_PCI_IOV */
6381 
6382 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE) || IS_ENABLED(CONFIG_CHELSIO_IPSEC_INLINE)
6383 
chcr_offload_state(struct adapter * adap,enum cxgb4_netdev_tls_ops op_val)6384 static int chcr_offload_state(struct adapter *adap,
6385 			      enum cxgb4_netdev_tls_ops op_val)
6386 {
6387 	switch (op_val) {
6388 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
6389 	case CXGB4_TLSDEV_OPS:
6390 		if (!adap->uld[CXGB4_ULD_KTLS].handle) {
6391 			dev_dbg(adap->pdev_dev, "ch_ktls driver is not loaded\n");
6392 			return -EOPNOTSUPP;
6393 		}
6394 		if (!adap->uld[CXGB4_ULD_KTLS].tlsdev_ops) {
6395 			dev_dbg(adap->pdev_dev,
6396 				"ch_ktls driver has no registered tlsdev_ops\n");
6397 			return -EOPNOTSUPP;
6398 		}
6399 		break;
6400 #endif /* CONFIG_CHELSIO_TLS_DEVICE */
6401 #if IS_ENABLED(CONFIG_CHELSIO_IPSEC_INLINE)
6402 	case CXGB4_XFRMDEV_OPS:
6403 		if (!adap->uld[CXGB4_ULD_IPSEC].handle) {
6404 			dev_dbg(adap->pdev_dev, "chipsec driver is not loaded\n");
6405 			return -EOPNOTSUPP;
6406 		}
6407 		if (!adap->uld[CXGB4_ULD_IPSEC].xfrmdev_ops) {
6408 			dev_dbg(adap->pdev_dev,
6409 				"chipsec driver has no registered xfrmdev_ops\n");
6410 			return -EOPNOTSUPP;
6411 		}
6412 		break;
6413 #endif /* CONFIG_CHELSIO_IPSEC_INLINE */
6414 	default:
6415 		dev_dbg(adap->pdev_dev,
6416 			"driver has no support for offload %d\n", op_val);
6417 		return -EOPNOTSUPP;
6418 	}
6419 
6420 	return 0;
6421 }
6422 
6423 #endif /* CONFIG_CHELSIO_TLS_DEVICE || CONFIG_CHELSIO_IPSEC_INLINE */
6424 
6425 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
6426 
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)6427 static int cxgb4_ktls_dev_add(struct net_device *netdev, struct sock *sk,
6428 			      enum tls_offload_ctx_dir direction,
6429 			      struct tls_crypto_info *crypto_info,
6430 			      u32 tcp_sn)
6431 {
6432 	struct adapter *adap = netdev2adap(netdev);
6433 	int ret;
6434 
6435 	mutex_lock(&uld_mutex);
6436 	ret = chcr_offload_state(adap, CXGB4_TLSDEV_OPS);
6437 	if (ret)
6438 		goto out_unlock;
6439 
6440 	ret = cxgb4_set_ktls_feature(adap, FW_PARAMS_PARAM_DEV_KTLS_HW_ENABLE);
6441 	if (ret)
6442 		goto out_unlock;
6443 
6444 	ret = adap->uld[CXGB4_ULD_KTLS].tlsdev_ops->tls_dev_add(netdev, sk,
6445 								direction,
6446 								crypto_info,
6447 								tcp_sn);
6448 	/* if there is a failure, clear the refcount */
6449 	if (ret)
6450 		cxgb4_set_ktls_feature(adap,
6451 				       FW_PARAMS_PARAM_DEV_KTLS_HW_DISABLE);
6452 out_unlock:
6453 	mutex_unlock(&uld_mutex);
6454 	return ret;
6455 }
6456 
cxgb4_ktls_dev_del(struct net_device * netdev,struct tls_context * tls_ctx,enum tls_offload_ctx_dir direction)6457 static void cxgb4_ktls_dev_del(struct net_device *netdev,
6458 			       struct tls_context *tls_ctx,
6459 			       enum tls_offload_ctx_dir direction)
6460 {
6461 	struct adapter *adap = netdev2adap(netdev);
6462 
6463 	mutex_lock(&uld_mutex);
6464 	if (chcr_offload_state(adap, CXGB4_TLSDEV_OPS))
6465 		goto out_unlock;
6466 
6467 	adap->uld[CXGB4_ULD_KTLS].tlsdev_ops->tls_dev_del(netdev, tls_ctx,
6468 							  direction);
6469 
6470 out_unlock:
6471 	cxgb4_set_ktls_feature(adap, FW_PARAMS_PARAM_DEV_KTLS_HW_DISABLE);
6472 	mutex_unlock(&uld_mutex);
6473 }
6474 
6475 static const struct tlsdev_ops cxgb4_ktls_ops = {
6476 	.tls_dev_add = cxgb4_ktls_dev_add,
6477 	.tls_dev_del = cxgb4_ktls_dev_del,
6478 };
6479 #endif /* CONFIG_CHELSIO_TLS_DEVICE */
6480 
6481 #if IS_ENABLED(CONFIG_CHELSIO_IPSEC_INLINE)
6482 
cxgb4_xfrm_add_state(struct xfrm_state * x,struct netlink_ext_ack * extack)6483 static int cxgb4_xfrm_add_state(struct xfrm_state *x,
6484 				struct netlink_ext_ack *extack)
6485 {
6486 	struct adapter *adap = netdev2adap(x->xso.dev);
6487 	int ret;
6488 
6489 	if (!mutex_trylock(&uld_mutex)) {
6490 		NL_SET_ERR_MSG_MOD(extack, "crypto uld critical resource is under use");
6491 		return -EBUSY;
6492 	}
6493 	ret = chcr_offload_state(adap, CXGB4_XFRMDEV_OPS);
6494 	if (ret)
6495 		goto out_unlock;
6496 
6497 	ret = adap->uld[CXGB4_ULD_IPSEC].xfrmdev_ops->xdo_dev_state_add(x, extack);
6498 
6499 out_unlock:
6500 	mutex_unlock(&uld_mutex);
6501 
6502 	return ret;
6503 }
6504 
cxgb4_xfrm_del_state(struct xfrm_state * x)6505 static void cxgb4_xfrm_del_state(struct xfrm_state *x)
6506 {
6507 	struct adapter *adap = netdev2adap(x->xso.dev);
6508 
6509 	if (!mutex_trylock(&uld_mutex)) {
6510 		dev_dbg(adap->pdev_dev,
6511 			"crypto uld critical resource is under use\n");
6512 		return;
6513 	}
6514 	if (chcr_offload_state(adap, CXGB4_XFRMDEV_OPS))
6515 		goto out_unlock;
6516 
6517 	adap->uld[CXGB4_ULD_IPSEC].xfrmdev_ops->xdo_dev_state_delete(x);
6518 
6519 out_unlock:
6520 	mutex_unlock(&uld_mutex);
6521 }
6522 
cxgb4_xfrm_free_state(struct xfrm_state * x)6523 static void cxgb4_xfrm_free_state(struct xfrm_state *x)
6524 {
6525 	struct adapter *adap = netdev2adap(x->xso.dev);
6526 
6527 	if (!mutex_trylock(&uld_mutex)) {
6528 		dev_dbg(adap->pdev_dev,
6529 			"crypto uld critical resource is under use\n");
6530 		return;
6531 	}
6532 	if (chcr_offload_state(adap, CXGB4_XFRMDEV_OPS))
6533 		goto out_unlock;
6534 
6535 	adap->uld[CXGB4_ULD_IPSEC].xfrmdev_ops->xdo_dev_state_free(x);
6536 
6537 out_unlock:
6538 	mutex_unlock(&uld_mutex);
6539 }
6540 
cxgb4_ipsec_offload_ok(struct sk_buff * skb,struct xfrm_state * x)6541 static bool cxgb4_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
6542 {
6543 	struct adapter *adap = netdev2adap(x->xso.dev);
6544 	bool ret = false;
6545 
6546 	if (!mutex_trylock(&uld_mutex)) {
6547 		dev_dbg(adap->pdev_dev,
6548 			"crypto uld critical resource is under use\n");
6549 		return ret;
6550 	}
6551 	if (chcr_offload_state(adap, CXGB4_XFRMDEV_OPS))
6552 		goto out_unlock;
6553 
6554 	ret = adap->uld[CXGB4_ULD_IPSEC].xfrmdev_ops->xdo_dev_offload_ok(skb, x);
6555 
6556 out_unlock:
6557 	mutex_unlock(&uld_mutex);
6558 	return ret;
6559 }
6560 
cxgb4_advance_esn_state(struct xfrm_state * x)6561 static void cxgb4_advance_esn_state(struct xfrm_state *x)
6562 {
6563 	struct adapter *adap = netdev2adap(x->xso.dev);
6564 
6565 	if (!mutex_trylock(&uld_mutex)) {
6566 		dev_dbg(adap->pdev_dev,
6567 			"crypto uld critical resource is under use\n");
6568 		return;
6569 	}
6570 	if (chcr_offload_state(adap, CXGB4_XFRMDEV_OPS))
6571 		goto out_unlock;
6572 
6573 	adap->uld[CXGB4_ULD_IPSEC].xfrmdev_ops->xdo_dev_state_advance_esn(x);
6574 
6575 out_unlock:
6576 	mutex_unlock(&uld_mutex);
6577 }
6578 
6579 static const struct xfrmdev_ops cxgb4_xfrmdev_ops = {
6580 	.xdo_dev_state_add      = cxgb4_xfrm_add_state,
6581 	.xdo_dev_state_delete   = cxgb4_xfrm_del_state,
6582 	.xdo_dev_state_free     = cxgb4_xfrm_free_state,
6583 	.xdo_dev_offload_ok     = cxgb4_ipsec_offload_ok,
6584 	.xdo_dev_state_advance_esn = cxgb4_advance_esn_state,
6585 };
6586 
6587 #endif /* CONFIG_CHELSIO_IPSEC_INLINE */
6588 
init_one(struct pci_dev * pdev,const struct pci_device_id * ent)6589 static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6590 {
6591 	struct net_device *netdev;
6592 	struct adapter *adapter;
6593 	static int adap_idx = 1;
6594 	int s_qpp, qpp, num_seg;
6595 	struct port_info *pi;
6596 	enum chip_type chip;
6597 	void __iomem *regs;
6598 	int func, chip_ver;
6599 	u16 device_id;
6600 	int i, err;
6601 	u32 whoami;
6602 
6603 	err = pci_request_regions(pdev, KBUILD_MODNAME);
6604 	if (err) {
6605 		/* Just info, some other driver may have claimed the device. */
6606 		dev_info(&pdev->dev, "cannot obtain PCI resources\n");
6607 		return err;
6608 	}
6609 
6610 	err = pci_enable_device(pdev);
6611 	if (err) {
6612 		dev_err(&pdev->dev, "cannot enable PCI device\n");
6613 		goto out_release_regions;
6614 	}
6615 
6616 	regs = pci_ioremap_bar(pdev, 0);
6617 	if (!regs) {
6618 		dev_err(&pdev->dev, "cannot map device registers\n");
6619 		err = -ENOMEM;
6620 		goto out_disable_device;
6621 	}
6622 
6623 	adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
6624 	if (!adapter) {
6625 		err = -ENOMEM;
6626 		goto out_unmap_bar0;
6627 	}
6628 
6629 	adapter->regs = regs;
6630 	err = t4_wait_dev_ready(regs);
6631 	if (err < 0)
6632 		goto out_free_adapter;
6633 
6634 	/* We control everything through one PF */
6635 	whoami = t4_read_reg(adapter, PL_WHOAMI_A);
6636 	pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
6637 	chip = t4_get_chip_type(adapter, CHELSIO_PCI_ID_VER(device_id));
6638 	if ((int)chip < 0) {
6639 		dev_err(&pdev->dev, "Device %d is not supported\n", device_id);
6640 		err = chip;
6641 		goto out_free_adapter;
6642 	}
6643 	chip_ver = CHELSIO_CHIP_VERSION(chip);
6644 	func = chip_ver <= CHELSIO_T5 ?
6645 	       SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
6646 
6647 	adapter->pdev = pdev;
6648 	adapter->pdev_dev = &pdev->dev;
6649 	adapter->name = pci_name(pdev);
6650 	adapter->mbox = func;
6651 	adapter->pf = func;
6652 	adapter->params.chip = chip;
6653 	adapter->adap_idx = adap_idx;
6654 	adapter->msg_enable = DFLT_MSG_ENABLE;
6655 	adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
6656 				    (sizeof(struct mbox_cmd) *
6657 				     T4_OS_LOG_MBOX_CMDS),
6658 				    GFP_KERNEL);
6659 	if (!adapter->mbox_log) {
6660 		err = -ENOMEM;
6661 		goto out_free_adapter;
6662 	}
6663 	spin_lock_init(&adapter->mbox_lock);
6664 	INIT_LIST_HEAD(&adapter->mlist.list);
6665 	adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
6666 	pci_set_drvdata(pdev, adapter);
6667 
6668 	if (func != ent->driver_data) {
6669 		pci_disable_device(pdev);
6670 		pci_save_state(pdev);        /* to restore SR-IOV later */
6671 		return 0;
6672 	}
6673 
6674 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6675 	if (err) {
6676 		dev_err(&pdev->dev, "no usable DMA configuration\n");
6677 		goto out_free_adapter;
6678 	}
6679 
6680 	pci_set_master(pdev);
6681 	pci_save_state(pdev);
6682 	adap_idx++;
6683 	adapter->workq = create_singlethread_workqueue("cxgb4");
6684 	if (!adapter->workq) {
6685 		err = -ENOMEM;
6686 		goto out_free_adapter;
6687 	}
6688 
6689 	/* PCI device has been enabled */
6690 	adapter->flags |= CXGB4_DEV_ENABLED;
6691 	memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
6692 
6693 	/* If possible, we use PCIe Relaxed Ordering Attribute to deliver
6694 	 * Ingress Packet Data to Free List Buffers in order to allow for
6695 	 * chipset performance optimizations between the Root Complex and
6696 	 * Memory Controllers.  (Messages to the associated Ingress Queue
6697 	 * notifying new Packet Placement in the Free Lists Buffers will be
6698 	 * send without the Relaxed Ordering Attribute thus guaranteeing that
6699 	 * all preceding PCIe Transaction Layer Packets will be processed
6700 	 * first.)  But some Root Complexes have various issues with Upstream
6701 	 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
6702 	 * The PCIe devices which under the Root Complexes will be cleared the
6703 	 * Relaxed Ordering bit in the configuration space, So we check our
6704 	 * PCIe configuration space to see if it's flagged with advice against
6705 	 * using Relaxed Ordering.
6706 	 */
6707 	if (!pcie_relaxed_ordering_enabled(pdev))
6708 		adapter->flags |= CXGB4_ROOT_NO_RELAXED_ORDERING;
6709 
6710 	spin_lock_init(&adapter->stats_lock);
6711 	spin_lock_init(&adapter->tid_release_lock);
6712 	spin_lock_init(&adapter->win0_lock);
6713 
6714 	INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
6715 	INIT_WORK(&adapter->db_full_task, process_db_full);
6716 	INIT_WORK(&adapter->db_drop_task, process_db_drop);
6717 	INIT_WORK(&adapter->fatal_err_notify_task, notify_fatal_err);
6718 
6719 	err = t4_prep_adapter(adapter);
6720 	if (err)
6721 		goto out_free_adapter;
6722 
6723 	if (is_kdump_kernel()) {
6724 		/* Collect hardware state and append to /proc/vmcore */
6725 		err = cxgb4_cudbg_vmcore_add_dump(adapter);
6726 		if (err) {
6727 			dev_warn(adapter->pdev_dev,
6728 				 "Fail collecting vmcore device dump, err: %d. Continuing\n",
6729 				 err);
6730 			err = 0;
6731 		}
6732 	}
6733 
6734 	if (!is_t4(adapter->params.chip)) {
6735 		s_qpp = (QUEUESPERPAGEPF0_S +
6736 			(QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
6737 			adapter->pf);
6738 		qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
6739 		      SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
6740 		num_seg = PAGE_SIZE / SEGMENT_SIZE;
6741 
6742 		/* Each segment size is 128B. Write coalescing is enabled only
6743 		 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
6744 		 * queue is less no of segments that can be accommodated in
6745 		 * a page size.
6746 		 */
6747 		if (qpp > num_seg) {
6748 			dev_err(&pdev->dev,
6749 				"Incorrect number of egress queues per page\n");
6750 			err = -EINVAL;
6751 			goto out_free_adapter;
6752 		}
6753 		adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
6754 		pci_resource_len(pdev, 2));
6755 		if (!adapter->bar2) {
6756 			dev_err(&pdev->dev, "cannot map device bar2 region\n");
6757 			err = -ENOMEM;
6758 			goto out_free_adapter;
6759 		}
6760 	}
6761 
6762 	setup_memwin(adapter);
6763 	err = adap_init0(adapter, 0);
6764 	if (err)
6765 		goto out_unmap_bar;
6766 
6767 	setup_memwin_rdma(adapter);
6768 
6769 	/* configure SGE_STAT_CFG_A to read WC stats */
6770 	if (!is_t4(adapter->params.chip))
6771 		t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
6772 			     (is_t5(adapter->params.chip) ? STATMODE_V(0) :
6773 			      T6_STATMODE_V(0)));
6774 
6775 	/* Initialize hash mac addr list */
6776 	INIT_LIST_HEAD(&adapter->mac_hlist);
6777 
6778 	for_each_port(adapter, i) {
6779 		/* For supporting MQPRIO Offload, need some extra
6780 		 * queues for each ETHOFLD TIDs. Keep it equal to
6781 		 * MAX_ATIDs for now. Once we connect to firmware
6782 		 * later and query the EOTID params, we'll come to
6783 		 * know the actual # of EOTIDs supported.
6784 		 */
6785 		netdev = alloc_etherdev_mq(sizeof(struct port_info),
6786 					   MAX_ETH_QSETS + MAX_ATIDS);
6787 		if (!netdev) {
6788 			err = -ENOMEM;
6789 			goto out_free_dev;
6790 		}
6791 
6792 		SET_NETDEV_DEV(netdev, &pdev->dev);
6793 
6794 		adapter->port[i] = netdev;
6795 		pi = netdev_priv(netdev);
6796 		pi->adapter = adapter;
6797 		pi->xact_addr_filt = -1;
6798 		pi->port_id = i;
6799 		netdev->irq = pdev->irq;
6800 
6801 		netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
6802 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
6803 			NETIF_F_RXCSUM | NETIF_F_RXHASH | NETIF_F_GRO |
6804 			NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
6805 			NETIF_F_HW_TC | NETIF_F_NTUPLE | NETIF_F_HIGHDMA;
6806 
6807 		if (chip_ver > CHELSIO_T5) {
6808 			netdev->hw_enc_features |= NETIF_F_IP_CSUM |
6809 						   NETIF_F_IPV6_CSUM |
6810 						   NETIF_F_RXCSUM |
6811 						   NETIF_F_GSO_UDP_TUNNEL |
6812 						   NETIF_F_GSO_UDP_TUNNEL_CSUM |
6813 						   NETIF_F_TSO | NETIF_F_TSO6;
6814 
6815 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
6816 					       NETIF_F_GSO_UDP_TUNNEL_CSUM |
6817 					       NETIF_F_HW_TLS_RECORD;
6818 
6819 			if (adapter->rawf_cnt)
6820 				netdev->udp_tunnel_nic_info = &cxgb_udp_tunnels;
6821 		}
6822 
6823 		netdev->features |= netdev->hw_features;
6824 		netdev->vlan_features = netdev->features & VLAN_FEAT;
6825 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
6826 		if (pi->adapter->params.crypto & FW_CAPS_CONFIG_TLS_HW) {
6827 			netdev->hw_features |= NETIF_F_HW_TLS_TX;
6828 			netdev->tlsdev_ops = &cxgb4_ktls_ops;
6829 			/* initialize the refcount */
6830 			refcount_set(&pi->adapter->chcr_ktls.ktls_refcount, 0);
6831 		}
6832 #endif /* CONFIG_CHELSIO_TLS_DEVICE */
6833 #if IS_ENABLED(CONFIG_CHELSIO_IPSEC_INLINE)
6834 		if (pi->adapter->params.crypto & FW_CAPS_CONFIG_IPSEC_INLINE) {
6835 			netdev->hw_enc_features |= NETIF_F_HW_ESP;
6836 			netdev->features |= NETIF_F_HW_ESP;
6837 			netdev->xfrmdev_ops = &cxgb4_xfrmdev_ops;
6838 		}
6839 #endif /* CONFIG_CHELSIO_IPSEC_INLINE */
6840 
6841 		netdev->priv_flags |= IFF_UNICAST_FLT;
6842 
6843 		/* MTU range: 81 - 9600 */
6844 		netdev->min_mtu = 81;              /* accommodate SACK */
6845 		netdev->max_mtu = MAX_MTU;
6846 
6847 		netdev->netdev_ops = &cxgb4_netdev_ops;
6848 #ifdef CONFIG_CHELSIO_T4_DCB
6849 		netdev->dcbnl_ops = &cxgb4_dcb_ops;
6850 		cxgb4_dcb_state_init(netdev);
6851 		cxgb4_dcb_version_init(netdev);
6852 #endif
6853 		cxgb4_set_ethtool_ops(netdev);
6854 	}
6855 
6856 	cxgb4_init_ethtool_dump(adapter);
6857 
6858 	pci_set_drvdata(pdev, adapter);
6859 
6860 	if (adapter->flags & CXGB4_FW_OK) {
6861 		err = t4_port_init(adapter, func, func, 0);
6862 		if (err)
6863 			goto out_free_dev;
6864 	} else if (adapter->params.nports == 1) {
6865 		/* If we don't have a connection to the firmware -- possibly
6866 		 * because of an error -- grab the raw VPD parameters so we
6867 		 * can set the proper MAC Address on the debug network
6868 		 * interface that we've created.
6869 		 */
6870 		u8 hw_addr[ETH_ALEN];
6871 		u8 *na = adapter->params.vpd.na;
6872 
6873 		err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
6874 		if (!err) {
6875 			for (i = 0; i < ETH_ALEN; i++)
6876 				hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
6877 					      hex2val(na[2 * i + 1]));
6878 			t4_set_hw_addr(adapter, 0, hw_addr);
6879 		}
6880 	}
6881 
6882 	if (!(adapter->flags & CXGB4_FW_OK))
6883 		goto fw_attach_fail;
6884 
6885 	/* Configure queues and allocate tables now, they can be needed as
6886 	 * soon as the first register_netdev completes.
6887 	 */
6888 	err = cfg_queues(adapter);
6889 	if (err)
6890 		goto out_free_dev;
6891 
6892 	adapter->smt = t4_init_smt();
6893 	if (!adapter->smt) {
6894 		/* We tolerate a lack of SMT, giving up some functionality */
6895 		dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
6896 	}
6897 
6898 	adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
6899 	if (!adapter->l2t) {
6900 		/* We tolerate a lack of L2T, giving up some functionality */
6901 		dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
6902 		adapter->params.offload = 0;
6903 	}
6904 
6905 #if IS_ENABLED(CONFIG_IPV6)
6906 	if (chip_ver <= CHELSIO_T5 &&
6907 	    (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
6908 		/* CLIP functionality is not present in hardware,
6909 		 * hence disable all offload features
6910 		 */
6911 		dev_warn(&pdev->dev,
6912 			 "CLIP not enabled in hardware, continuing\n");
6913 		adapter->params.offload = 0;
6914 	} else {
6915 		adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
6916 						  adapter->clipt_end);
6917 		if (!adapter->clipt) {
6918 			/* We tolerate a lack of clip_table, giving up
6919 			 * some functionality
6920 			 */
6921 			dev_warn(&pdev->dev,
6922 				 "could not allocate Clip table, continuing\n");
6923 			adapter->params.offload = 0;
6924 		}
6925 	}
6926 #endif
6927 
6928 	for_each_port(adapter, i) {
6929 		pi = adap2pinfo(adapter, i);
6930 		pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
6931 		if (!pi->sched_tbl)
6932 			dev_warn(&pdev->dev,
6933 				 "could not activate scheduling on port %d\n",
6934 				 i);
6935 	}
6936 
6937 	if (is_offload(adapter) || is_hashfilter(adapter)) {
6938 		if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
6939 			u32 v;
6940 
6941 			v = t4_read_reg(adapter, LE_DB_HASH_CONFIG_A);
6942 			if (chip_ver <= CHELSIO_T5) {
6943 				adapter->tids.nhash = 1 << HASHTIDSIZE_G(v);
6944 				v = t4_read_reg(adapter, LE_DB_TID_HASHBASE_A);
6945 				adapter->tids.hash_base = v / 4;
6946 			} else {
6947 				adapter->tids.nhash = HASHTBLSIZE_G(v) << 3;
6948 				v = t4_read_reg(adapter,
6949 						T6_LE_DB_HASH_TID_BASE_A);
6950 				adapter->tids.hash_base = v;
6951 			}
6952 		}
6953 	}
6954 
6955 	if (tid_init(&adapter->tids) < 0) {
6956 		dev_warn(&pdev->dev, "could not allocate TID table, "
6957 			 "continuing\n");
6958 		adapter->params.offload = 0;
6959 	} else {
6960 		adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
6961 		if (!adapter->tc_u32)
6962 			dev_warn(&pdev->dev,
6963 				 "could not offload tc u32, continuing\n");
6964 
6965 		if (cxgb4_init_tc_flower(adapter))
6966 			dev_warn(&pdev->dev,
6967 				 "could not offload tc flower, continuing\n");
6968 
6969 		if (cxgb4_init_tc_mqprio(adapter))
6970 			dev_warn(&pdev->dev,
6971 				 "could not offload tc mqprio, continuing\n");
6972 
6973 		if (cxgb4_init_tc_matchall(adapter))
6974 			dev_warn(&pdev->dev,
6975 				 "could not offload tc matchall, continuing\n");
6976 		if (cxgb4_init_ethtool_filters(adapter))
6977 			dev_warn(&pdev->dev,
6978 				 "could not initialize ethtool filters, continuing\n");
6979 	}
6980 
6981 	/* See what interrupts we'll be using */
6982 	if (msi > 1 && enable_msix(adapter) == 0)
6983 		adapter->flags |= CXGB4_USING_MSIX;
6984 	else if (msi > 0 && pci_enable_msi(pdev) == 0) {
6985 		adapter->flags |= CXGB4_USING_MSI;
6986 		if (msi > 1)
6987 			free_msix_info(adapter);
6988 	}
6989 
6990 	/* check for PCI Express bandwidth capabiltites */
6991 	pcie_print_link_status(pdev);
6992 
6993 	cxgb4_init_mps_ref_entries(adapter);
6994 
6995 	err = init_rss(adapter);
6996 	if (err)
6997 		goto out_free_dev;
6998 
6999 	err = setup_non_data_intr(adapter);
7000 	if (err) {
7001 		dev_err(adapter->pdev_dev,
7002 			"Non Data interrupt allocation failed, err: %d\n", err);
7003 		goto out_free_dev;
7004 	}
7005 
7006 	err = setup_fw_sge_queues(adapter);
7007 	if (err) {
7008 		dev_err(adapter->pdev_dev,
7009 			"FW sge queue allocation failed, err %d", err);
7010 		goto out_free_dev;
7011 	}
7012 
7013 fw_attach_fail:
7014 	/*
7015 	 * The card is now ready to go.  If any errors occur during device
7016 	 * registration we do not fail the whole card but rather proceed only
7017 	 * with the ports we manage to register successfully.  However we must
7018 	 * register at least one net device.
7019 	 */
7020 	for_each_port(adapter, i) {
7021 		pi = adap2pinfo(adapter, i);
7022 		adapter->port[i]->dev_port = pi->lport;
7023 		netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
7024 		netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
7025 
7026 		netif_carrier_off(adapter->port[i]);
7027 
7028 		err = register_netdev(adapter->port[i]);
7029 		if (err)
7030 			break;
7031 		adapter->chan_map[pi->tx_chan] = i;
7032 		print_port_info(adapter->port[i]);
7033 	}
7034 	if (i == 0) {
7035 		dev_err(&pdev->dev, "could not register any net devices\n");
7036 		goto out_free_dev;
7037 	}
7038 	if (err) {
7039 		dev_warn(&pdev->dev, "only %d net devices registered\n", i);
7040 		err = 0;
7041 	}
7042 
7043 	if (cxgb4_debugfs_root) {
7044 		adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
7045 							   cxgb4_debugfs_root);
7046 		setup_debugfs(adapter);
7047 	}
7048 
7049 	/* PCIe EEH recovery on powerpc platforms needs fundamental reset */
7050 	pdev->needs_freset = 1;
7051 
7052 	if (is_uld(adapter))
7053 		cxgb4_uld_enable(adapter);
7054 
7055 	if (!is_t4(adapter->params.chip))
7056 		cxgb4_ptp_init(adapter);
7057 
7058 	if (IS_REACHABLE(CONFIG_THERMAL) &&
7059 	    !is_t4(adapter->params.chip) && (adapter->flags & CXGB4_FW_OK))
7060 		cxgb4_thermal_init(adapter);
7061 
7062 	print_adapter_info(adapter);
7063 	return 0;
7064 
7065  out_free_dev:
7066 	t4_free_sge_resources(adapter);
7067 	free_some_resources(adapter);
7068 	if (adapter->flags & CXGB4_USING_MSIX)
7069 		free_msix_info(adapter);
7070 	if (adapter->num_uld || adapter->num_ofld_uld)
7071 		t4_uld_mem_free(adapter);
7072  out_unmap_bar:
7073 	if (!is_t4(adapter->params.chip))
7074 		iounmap(adapter->bar2);
7075  out_free_adapter:
7076 	if (adapter->workq)
7077 		destroy_workqueue(adapter->workq);
7078 
7079 	kfree(adapter->mbox_log);
7080 	kfree(adapter);
7081  out_unmap_bar0:
7082 	iounmap(regs);
7083  out_disable_device:
7084 	pci_disable_device(pdev);
7085  out_release_regions:
7086 	pci_release_regions(pdev);
7087 	return err;
7088 }
7089 
remove_one(struct pci_dev * pdev)7090 static void remove_one(struct pci_dev *pdev)
7091 {
7092 	struct adapter *adapter = pci_get_drvdata(pdev);
7093 	struct hash_mac_addr *entry, *tmp;
7094 
7095 	if (!adapter) {
7096 		pci_release_regions(pdev);
7097 		return;
7098 	}
7099 
7100 	/* If we allocated filters, free up state associated with any
7101 	 * valid filters ...
7102 	 */
7103 	clear_all_filters(adapter);
7104 
7105 	adapter->flags |= CXGB4_SHUTTING_DOWN;
7106 
7107 	if (adapter->pf == 4) {
7108 		int i;
7109 
7110 		/* Tear down per-adapter Work Queue first since it can contain
7111 		 * references to our adapter data structure.
7112 		 */
7113 		destroy_workqueue(adapter->workq);
7114 
7115 		detach_ulds(adapter);
7116 
7117 		for_each_port(adapter, i)
7118 			if (adapter->port[i]->reg_state == NETREG_REGISTERED)
7119 				unregister_netdev(adapter->port[i]);
7120 
7121 		t4_uld_clean_up(adapter);
7122 
7123 		adap_free_hma_mem(adapter);
7124 
7125 		disable_interrupts(adapter);
7126 
7127 		cxgb4_free_mps_ref_entries(adapter);
7128 
7129 		debugfs_remove_recursive(adapter->debugfs_root);
7130 
7131 		if (!is_t4(adapter->params.chip))
7132 			cxgb4_ptp_stop(adapter);
7133 		if (IS_REACHABLE(CONFIG_THERMAL))
7134 			cxgb4_thermal_remove(adapter);
7135 
7136 		if (adapter->flags & CXGB4_FULL_INIT_DONE)
7137 			cxgb_down(adapter);
7138 
7139 		if (adapter->flags & CXGB4_USING_MSIX)
7140 			free_msix_info(adapter);
7141 		if (adapter->num_uld || adapter->num_ofld_uld)
7142 			t4_uld_mem_free(adapter);
7143 		free_some_resources(adapter);
7144 		list_for_each_entry_safe(entry, tmp, &adapter->mac_hlist,
7145 					 list) {
7146 			list_del(&entry->list);
7147 			kfree(entry);
7148 		}
7149 
7150 #if IS_ENABLED(CONFIG_IPV6)
7151 		t4_cleanup_clip_tbl(adapter);
7152 #endif
7153 		if (!is_t4(adapter->params.chip))
7154 			iounmap(adapter->bar2);
7155 	}
7156 #ifdef CONFIG_PCI_IOV
7157 	else {
7158 		cxgb4_iov_configure(adapter->pdev, 0);
7159 	}
7160 #endif
7161 	iounmap(adapter->regs);
7162 	if ((adapter->flags & CXGB4_DEV_ENABLED)) {
7163 		pci_disable_device(pdev);
7164 		adapter->flags &= ~CXGB4_DEV_ENABLED;
7165 	}
7166 	pci_release_regions(pdev);
7167 	kfree(adapter->mbox_log);
7168 	synchronize_rcu();
7169 	kfree(adapter);
7170 }
7171 
7172 /* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
7173  * delivery.  This is essentially a stripped down version of the PCI remove()
7174  * function where we do the minimal amount of work necessary to shutdown any
7175  * further activity.
7176  */
shutdown_one(struct pci_dev * pdev)7177 static void shutdown_one(struct pci_dev *pdev)
7178 {
7179 	struct adapter *adapter = pci_get_drvdata(pdev);
7180 
7181 	/* As with remove_one() above (see extended comment), we only want do
7182 	 * do cleanup on PCI Devices which went all the way through init_one()
7183 	 * ...
7184 	 */
7185 	if (!adapter) {
7186 		pci_release_regions(pdev);
7187 		return;
7188 	}
7189 
7190 	adapter->flags |= CXGB4_SHUTTING_DOWN;
7191 
7192 	if (adapter->pf == 4) {
7193 		int i;
7194 
7195 		for_each_port(adapter, i)
7196 			if (adapter->port[i]->reg_state == NETREG_REGISTERED)
7197 				cxgb_close(adapter->port[i]);
7198 
7199 		rtnl_lock();
7200 		cxgb4_mqprio_stop_offload(adapter);
7201 		rtnl_unlock();
7202 
7203 		if (is_uld(adapter)) {
7204 			detach_ulds(adapter);
7205 			t4_uld_clean_up(adapter);
7206 		}
7207 
7208 		disable_interrupts(adapter);
7209 		disable_msi(adapter);
7210 
7211 		t4_sge_stop(adapter);
7212 		if (adapter->flags & CXGB4_FW_OK)
7213 			t4_fw_bye(adapter, adapter->mbox);
7214 	}
7215 }
7216 
7217 static struct pci_driver cxgb4_driver = {
7218 	.name     = KBUILD_MODNAME,
7219 	.id_table = cxgb4_pci_tbl,
7220 	.probe    = init_one,
7221 	.remove   = remove_one,
7222 	.shutdown = shutdown_one,
7223 #ifdef CONFIG_PCI_IOV
7224 	.sriov_configure = cxgb4_iov_configure,
7225 #endif
7226 	.err_handler = &cxgb4_eeh,
7227 };
7228 
cxgb4_init_module(void)7229 static int __init cxgb4_init_module(void)
7230 {
7231 	int ret;
7232 
7233 	cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
7234 
7235 	ret = pci_register_driver(&cxgb4_driver);
7236 	if (ret < 0)
7237 		goto err_pci;
7238 
7239 #if IS_ENABLED(CONFIG_IPV6)
7240 	if (!inet6addr_registered) {
7241 		ret = register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
7242 		if (ret)
7243 			pci_unregister_driver(&cxgb4_driver);
7244 		else
7245 			inet6addr_registered = true;
7246 	}
7247 #endif
7248 
7249 	if (ret == 0)
7250 		return ret;
7251 
7252 err_pci:
7253 	debugfs_remove(cxgb4_debugfs_root);
7254 
7255 	return ret;
7256 }
7257 
cxgb4_cleanup_module(void)7258 static void __exit cxgb4_cleanup_module(void)
7259 {
7260 #if IS_ENABLED(CONFIG_IPV6)
7261 	if (inet6addr_registered) {
7262 		unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
7263 		inet6addr_registered = false;
7264 	}
7265 #endif
7266 	pci_unregister_driver(&cxgb4_driver);
7267 	debugfs_remove(cxgb4_debugfs_root);  /* NULL ok */
7268 }
7269 
7270 module_init(cxgb4_init_module);
7271 module_exit(cxgb4_cleanup_module);
7272