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