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