blob: 77b4bd9587489b000de9a145de6fcbd31026e8e2 [file] [log] [blame]
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005 *
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>
Jiri Pirko01789342011-08-16 06:29:00 +000044#include <linux/if.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000045#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>
Vipul Pandya01bcca62013-07-04 16:10:46 +053063#include <net/addrconf.h>
David S. Miller1ef80192014-11-10 13:27:49 -050064#include <net/bonding.h>
Anish Bhattb5a02f52015-01-14 15:17:34 -080065#include <net/addrconf.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080066#include <linux/uaccess.h>
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +053067#include <linux/crash_dump.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000068
69#include "cxgb4.h"
Rahul Lakkireddyd57fd6c2016-09-20 17:13:06 +053070#include "cxgb4_filter.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000071#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053072#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000073#include "t4_msg.h"
74#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053075#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070076#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053077#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080078#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000079#include "l2t.h"
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +053080#include "smt.h"
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +053081#include "sched.h"
Rahul Lakkireddyd8931842016-09-20 17:13:09 +053082#include "cxgb4_tc_u32.h"
Kumar Sanghvi6a345b32017-09-21 23:41:13 +053083#include "cxgb4_tc_flower.h"
Atul Guptaa45695042017-07-04 16:46:20 +053084#include "cxgb4_ptp.h"
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +053085#include "cxgb4_cudbg.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000086
Hariprasad Shenai812034f2015-04-06 20:23:23 +053087char cxgb4_driver_name[] = KBUILD_MODNAME;
88
Vipul Pandya01bcca62013-07-04 16:10:46 +053089#ifdef DRV_VERSION
90#undef DRV_VERSION
91#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000092#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053093const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053094#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000095
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000096#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
97 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
98 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
99
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530100/* Macros needed to support the PCI Device ID Table ...
101 */
102#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530103 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530104#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000105
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530106/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
107 * called for both.
108 */
109#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
110
111#define CH_PCI_ID_TABLE_ENTRY(devid) \
112 {PCI_VDEVICE(CHELSIO, (devid)), 4}
113
114#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
115 { 0, } \
116 }
117
118#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000119
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530120#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000121#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530122#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530123#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000124#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530125#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530126#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
127#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
128#define PHY_AQ1202_DEVICEID 0x4409
129#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000130
131MODULE_DESCRIPTION(DRV_DESC);
132MODULE_AUTHOR("Chelsio Communications");
133MODULE_LICENSE("Dual BSD/GPL");
134MODULE_VERSION(DRV_VERSION);
135MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530136MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000137MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530138MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000139
Vipul Pandya636f9d32012-09-26 02:39:39 +0000140/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000141 * The driver uses the best interrupt scheme available on a platform in the
142 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
143 * of these schemes the driver may consider as follows:
144 *
145 * msi = 2: choose from among all three options
146 * msi = 1: only consider MSI and INTx interrupts
147 * msi = 0: force INTx interrupts
148 */
149static int msi = 2;
150
151module_param(msi, int, 0644);
152MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
153
154/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000155 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
156 * offset by 2 bytes in order to have the IP headers line up on 4-byte
157 * boundaries. This is a requirement for many architectures which will throw
158 * a machine check fault if an attempt is made to access one of the 4-byte IP
159 * header fields on a non-4-byte boundary. And it's a major performance issue
160 * even on some architectures which allow it like some implementations of the
161 * x86 ISA. However, some architectures don't mind this and for some very
162 * edge-case performance sensitive applications (like forwarding large volumes
163 * of small packets), setting this DMA offset to 0 will decrease the number of
164 * PCI-E Bus transfers enough to measurably affect performance.
165 */
166static int rx_dma_offset = 2;
167
Anish Bhatt688848b2014-06-19 21:37:13 -0700168/* TX Queue select used to determine what algorithm to use for selecting TX
169 * queue. Select between the kernel provided function (select_queue=0) or user
170 * cxgb_select_queue function (select_queue=1)
171 *
172 * Default: select_queue=0
173 */
174static int select_queue;
175module_param(select_queue, int, 0644);
176MODULE_PARM_DESC(select_queue,
177 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
178
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000179static struct dentry *cxgb4_debugfs_root;
180
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530181LIST_HEAD(adapter_list);
182DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000183
184static void link_report(struct net_device *dev)
185{
186 if (!netif_carrier_ok(dev))
187 netdev_info(dev, "link down\n");
188 else {
189 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
190
Hariprasad Shenai85412252015-10-01 13:48:48 +0530191 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000192 const struct port_info *p = netdev_priv(dev);
193
194 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000195 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000196 s = "100Mbps";
197 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530198 case 1000:
199 s = "1Gbps";
200 break;
201 case 10000:
202 s = "10Gbps";
203 break;
204 case 25000:
205 s = "25Gbps";
206 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000207 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530208 s = "40Gbps";
209 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530210 case 100000:
211 s = "100Gbps";
212 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530213 default:
214 pr_info("%s: unsupported speed: %d\n",
215 dev->name, p->link_cfg.speed);
216 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000217 }
218
219 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
220 fc[p->link_cfg.fc]);
221 }
222}
223
Anish Bhatt688848b2014-06-19 21:37:13 -0700224#ifdef CONFIG_CHELSIO_T4_DCB
225/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
226static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
227{
228 struct port_info *pi = netdev_priv(dev);
229 struct adapter *adap = pi->adapter;
230 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
231 int i;
232
233 /* We use a simple mapping of Port TX Queue Index to DCB
234 * Priority when we're enabling DCB.
235 */
236 for (i = 0; i < pi->nqsets; i++, txq++) {
237 u32 name, value;
238 int err;
239
Hariprasad Shenai51678652014-11-21 12:52:02 +0530240 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
241 FW_PARAMS_PARAM_X_V(
242 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
243 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700244 value = enable ? i : 0xffffffff;
245
246 /* Since we can be called while atomic (from "interrupt
247 * level") we need to issue the Set Parameters Commannd
248 * without sleeping (timeout < 0).
249 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530250 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530251 &name, &value,
252 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700253
254 if (err)
255 dev_err(adap->pdev_dev,
256 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
257 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700258 else
259 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700260 }
261}
Anish Bhatt688848b2014-06-19 21:37:13 -0700262
Baoyou Xie50935852016-09-25 14:10:09 +0800263static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530264{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530265 struct port_info *pi = netdev_priv(dev);
266
267 if (!pi->dcb.enabled)
268 return 0;
269
270 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
271 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530272}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200273#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530274
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000275void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
276{
277 struct net_device *dev = adapter->port[port_id];
278
279 /* Skip changes from disabled ports. */
280 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
281 if (link_stat)
282 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700283 else {
284#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530285 if (cxgb4_dcb_enabled(dev)) {
Ganesh Goudarba581f72017-09-23 16:07:28 +0530286 cxgb4_dcb_reset(dev);
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530287 dcb_tx_queue_prio_enable(dev, false);
288 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700289#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000290 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700291 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000292
293 link_report(dev);
294 }
295}
296
297void t4_os_portmod_changed(const struct adapter *adap, int port_id)
298{
299 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000300 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000301 };
302
303 const struct net_device *dev = adap->port[port_id];
304 const struct port_info *pi = netdev_priv(dev);
305
306 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
307 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000308 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000309 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530310 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
311 netdev_info(dev, "%s: unsupported port module inserted\n",
312 dev->name);
313 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
314 netdev_info(dev, "%s: unknown port module inserted\n",
315 dev->name);
316 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
317 netdev_info(dev, "%s: transceiver module error\n", dev->name);
318 else
319 netdev_info(dev, "%s: unknown module type %d inserted\n",
320 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000321}
322
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530323int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
324module_param(dbfifo_int_thresh, int, 0644);
325MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
326
Vipul Pandya404d9e32012-10-08 02:59:43 +0000327/*
328 * usecs to sleep while draining the dbfifo
329 */
330static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530331module_param(dbfifo_drain_delay, int, 0644);
332MODULE_PARM_DESC(dbfifo_drain_delay,
333 "usecs to sleep while draining the dbfifo");
334
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530335static inline int cxgb4_set_addr_hash(struct port_info *pi)
336{
337 struct adapter *adap = pi->adapter;
338 u64 vec = 0;
339 bool ucast = false;
340 struct hash_mac_addr *entry;
341
342 /* Calculate the hash vector for the updated list and program it */
343 list_for_each_entry(entry, &adap->mac_hlist, list) {
344 ucast |= is_unicast_ether_addr(entry->addr);
345 vec |= (1ULL << hash_mac_addr(entry->addr));
346 }
347 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
348 vec, false);
349}
350
351static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
352{
353 struct port_info *pi = netdev_priv(netdev);
354 struct adapter *adap = pi->adapter;
355 int ret;
356 u64 mhash = 0;
357 u64 uhash = 0;
358 bool free = false;
359 bool ucast = is_unicast_ether_addr(mac_addr);
360 const u8 *maclist[1] = {mac_addr};
361 struct hash_mac_addr *new_entry;
362
363 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
364 NULL, ucast ? &uhash : &mhash, false);
365 if (ret < 0)
366 goto out;
367 /* if hash != 0, then add the addr to hash addr list
368 * so on the end we will calculate the hash for the
369 * list and program it
370 */
371 if (uhash || mhash) {
372 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
373 if (!new_entry)
374 return -ENOMEM;
375 ether_addr_copy(new_entry->addr, mac_addr);
376 list_add_tail(&new_entry->list, &adap->mac_hlist);
377 ret = cxgb4_set_addr_hash(pi);
378 }
379out:
380 return ret < 0 ? ret : 0;
381}
382
383static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
384{
385 struct port_info *pi = netdev_priv(netdev);
386 struct adapter *adap = pi->adapter;
387 int ret;
388 const u8 *maclist[1] = {mac_addr};
389 struct hash_mac_addr *entry, *tmp;
390
391 /* If the MAC address to be removed is in the hash addr
392 * list, delete it from the list and update hash vector
393 */
394 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
395 if (ether_addr_equal(entry->addr, mac_addr)) {
396 list_del(&entry->list);
397 kfree(entry);
398 return cxgb4_set_addr_hash(pi);
399 }
400 }
401
402 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
403 return ret < 0 ? -EINVAL : 0;
404}
405
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000406/*
407 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
408 * If @mtu is -1 it is left unchanged.
409 */
410static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
411{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000412 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530413 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000414
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530415 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
416 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530417
418 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
419 (dev->flags & IFF_PROMISC) ? 1 : 0,
420 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
421 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000422}
423
424/**
425 * link_start - enable a port
426 * @dev: the port to enable
427 *
428 * Performs the MAC and PHY actions needed to enable a port.
429 */
430static int link_start(struct net_device *dev)
431{
432 int ret;
433 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530434 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000435
436 /*
437 * We do not set address filters and promiscuity here, the stack does
438 * that step explicitly.
439 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000440 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000441 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000442 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000443 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000444 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000445 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000446 if (ret >= 0) {
447 pi->xact_addr_filt = ret;
448 ret = 0;
449 }
450 }
451 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530452 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000453 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700454 if (ret == 0) {
455 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700456 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
457 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700458 local_bh_enable();
459 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700460
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000461 return ret;
462}
463
Anish Bhatt688848b2014-06-19 21:37:13 -0700464#ifdef CONFIG_CHELSIO_T4_DCB
465/* Handle a Data Center Bridging update message from the firmware. */
466static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
467{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530468 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530469 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700470 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
471 int new_dcb_enabled;
472
473 cxgb4_dcb_handle_fw_update(adap, pcmd);
474 new_dcb_enabled = cxgb4_dcb_enabled(dev);
475
476 /* If the DCB has become enabled or disabled on the port then we're
477 * going to need to set up/tear down DCB Priority parameters for the
478 * TX Queues associated with the port.
479 */
480 if (new_dcb_enabled != old_dcb_enabled)
481 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
482}
483#endif /* CONFIG_CHELSIO_T4_DCB */
484
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000485/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000486 */
487static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
488 const struct pkt_gl *gl)
489{
490 u8 opcode = ((const struct rss_header *)rsp)->opcode;
491
492 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000493
494 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
495 */
496 if (unlikely(opcode == CPL_FW4_MSG &&
497 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
498 rsp++;
499 opcode = ((const struct rss_header *)rsp)->opcode;
500 rsp++;
501 if (opcode != CPL_SGE_EGR_UPDATE) {
502 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
503 , opcode);
504 goto out;
505 }
506 }
507
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000508 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
509 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800510 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000511 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000512
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000513 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000514 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530515 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000516 struct sge_eth_txq *eq;
517
518 eq = container_of(txq, struct sge_eth_txq, q);
519 netif_tx_wake_queue(eq->txq);
520 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530521 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000522
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530523 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000524 tasklet_schedule(&oq->qresume_tsk);
525 }
526 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
527 const struct cpl_fw6_msg *p = (void *)rsp;
528
Anish Bhatt688848b2014-06-19 21:37:13 -0700529#ifdef CONFIG_CHELSIO_T4_DCB
530 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530531 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700532 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530533 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700534
535 if (cmd == FW_PORT_CMD &&
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530536 (action == FW_PORT_ACTION_GET_PORT_INFO ||
537 action == FW_PORT_ACTION_GET_PORT_INFO32)) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530538 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700539 be32_to_cpu(pcmd->op_to_portid));
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530540 struct net_device *dev;
541 int dcbxdis, state_input;
542
543 dev = q->adap->port[q->adap->chan_map[port]];
544 dcbxdis = (action == FW_PORT_ACTION_GET_PORT_INFO
545 ? !!(pcmd->u.info.dcbxdis_pkd &
546 FW_PORT_CMD_DCBXDIS_F)
547 : !!(pcmd->u.info32.lstatus32_to_cbllen32 &
548 FW_PORT_CMD_DCBXDIS32_F));
549 state_input = (dcbxdis
550 ? CXGB4_DCB_INPUT_FW_DISABLED
551 : CXGB4_DCB_INPUT_FW_ENABLED);
Anish Bhatt688848b2014-06-19 21:37:13 -0700552
553 cxgb4_dcb_state_fsm(dev, state_input);
554 }
555
556 if (cmd == FW_PORT_CMD &&
557 action == FW_PORT_ACTION_L2_DCB_CFG)
558 dcb_rpl(q->adap, pcmd);
559 else
560#endif
561 if (p->type == 0)
562 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000563 } else if (opcode == CPL_L2T_WRITE_RPL) {
564 const struct cpl_l2t_write_rpl *p = (void *)rsp;
565
566 do_l2t_write_rpl(q->adap, p);
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +0530567 } else if (opcode == CPL_SMT_WRITE_RPL) {
568 const struct cpl_smt_write_rpl *p = (void *)rsp;
569
570 do_smt_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000571 } else if (opcode == CPL_SET_TCB_RPL) {
572 const struct cpl_set_tcb_rpl *p = (void *)rsp;
573
574 filter_rpl(q->adap, p);
Kumar Sanghvi12b276f2017-11-01 08:53:01 +0530575 } else if (opcode == CPL_ACT_OPEN_RPL) {
576 const struct cpl_act_open_rpl *p = (void *)rsp;
577
578 hash_filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000579 } else
580 dev_err(q->adap->pdev_dev,
581 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000582out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000583 return 0;
584}
585
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000586static void disable_msi(struct adapter *adapter)
587{
588 if (adapter->flags & USING_MSIX) {
589 pci_disable_msix(adapter->pdev);
590 adapter->flags &= ~USING_MSIX;
591 } else if (adapter->flags & USING_MSI) {
592 pci_disable_msi(adapter->pdev);
593 adapter->flags &= ~USING_MSI;
594 }
595}
596
597/*
598 * Interrupt handler for non-data events used with MSI-X.
599 */
600static irqreturn_t t4_nondata_intr(int irq, void *cookie)
601{
602 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530603 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000604
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530605 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000606 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530607 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000608 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530609 if (adap->flags & MASTER_PF)
610 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000611 return IRQ_HANDLED;
612}
613
614/*
615 * Name the MSI-X interrupts.
616 */
617static void name_msix_vecs(struct adapter *adap)
618{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000619 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000620
621 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000622 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000623
624 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000625 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
626 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000627
628 /* Ethernet queues */
629 for_each_port(adap, j) {
630 struct net_device *d = adap->port[j];
631 const struct port_info *pi = netdev_priv(d);
632
Dimitris Michailidisba278162010-12-14 21:36:50 +0000633 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000634 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
635 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000636 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000637}
638
639static int request_msix_queue_irqs(struct adapter *adap)
640{
641 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530642 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530643 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000644
645 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
646 adap->msix_info[1].desc, &s->fw_evtq);
647 if (err)
648 return err;
649
650 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000651 err = request_irq(adap->msix_info[msi_index].vec,
652 t4_sge_intr_msix, 0,
653 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000654 &s->ethrxq[ethqidx].rspq);
655 if (err)
656 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000657 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000658 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000659 return 0;
660
661unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000662 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000663 free_irq(adap->msix_info[--msi_index].vec,
664 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000665 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
666 return err;
667}
668
669static void free_msix_queue_irqs(struct adapter *adap)
670{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000671 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000672 struct sge *s = &adap->sge;
673
674 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
675 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000676 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000677}
678
679/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530680 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000681 * @pi: the port
682 * @queues: array of queue indices for RSS
683 *
684 * Sets up the portion of the HW RSS table for the port's VI to distribute
685 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530686 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000687 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530688int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000689{
690 u16 *rss;
691 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530692 struct adapter *adapter = pi->adapter;
693 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000694
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530695 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000696 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
697 if (!rss)
698 return -ENOMEM;
699
700 /* map the queue indices to queue ids */
701 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530702 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000703
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530704 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000705 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530706 /* If Tunnel All Lookup isn't specified in the global RSS
707 * Configuration, then we need to specify a default Ingress
708 * Queue for any ingress packets which aren't hashed. We'll
709 * use our first ingress queue ...
710 */
711 if (!err)
712 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
713 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
714 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
715 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
716 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
717 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
718 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000719 kfree(rss);
720 return err;
721}
722
723/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000724 * setup_rss - configure RSS
725 * @adap: the adapter
726 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000727 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000728 */
729static int setup_rss(struct adapter *adap)
730{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530731 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000732
733 for_each_port(adap, i) {
734 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000735
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530736 /* Fill default values with equal distribution */
737 for (j = 0; j < pi->rss_size; j++)
738 pi->rss[j] = j % pi->nqsets;
739
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530740 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000741 if (err)
742 return err;
743 }
744 return 0;
745}
746
747/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000748 * Return the channel of the ingress queue with the given qid.
749 */
750static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
751{
752 qid -= p->ingr_start;
753 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
754}
755
756/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000757 * Wait until all NAPI handlers are descheduled.
758 */
759static void quiesce_rx(struct adapter *adap)
760{
761 int i;
762
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530763 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000764 struct sge_rspq *q = adap->sge.ingr_map[i];
765
Eric Dumazet5226b7912017-02-02 11:44:27 -0800766 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000767 napi_disable(&q->napi);
768 }
769}
770
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530771/* Disable interrupt and napi handler */
772static void disable_interrupts(struct adapter *adap)
773{
774 if (adap->flags & FULL_INIT_DONE) {
775 t4_intr_disable(adap);
776 if (adap->flags & USING_MSIX) {
777 free_msix_queue_irqs(adap);
778 free_irq(adap->msix_info[0].vec, adap);
779 } else {
780 free_irq(adap->pdev->irq, adap);
781 }
782 quiesce_rx(adap);
783 }
784}
785
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000786/*
787 * Enable NAPI scheduling and interrupt generation for all Rx queues.
788 */
789static void enable_rx(struct adapter *adap)
790{
791 int i;
792
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530793 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000794 struct sge_rspq *q = adap->sge.ingr_map[i];
795
796 if (!q)
797 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800798 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000799 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800800
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000801 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530802 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
803 SEINTARM_V(q->intr_params) |
804 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000805 }
806}
807
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530808
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530809static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000810{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000811 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530812 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000813
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530814 bitmap_zero(s->starving_fl, s->egr_sz);
815 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000816
817 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530818 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000819 else {
820 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530821 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000822 if (err)
823 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530824 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000825 }
826
827 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530828 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530829 if (err)
830 t4_free_sge_resources(adap);
831 return err;
832}
833
834/**
835 * setup_sge_queues - configure SGE Tx/Rx/response queues
836 * @adap: the adapter
837 *
838 * Determines how many sets of SGE queues to use and initializes them.
839 * We support multiple queue sets per port if we have MSI-X, otherwise
840 * just one queue set per port.
841 */
842static int setup_sge_queues(struct adapter *adap)
843{
844 int err, i, j;
845 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530846 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530847 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000848
Ganesh Goudard427cae2017-06-16 15:36:09 +0530849 if (is_uld(adap))
850 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
851
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000852 for_each_port(adap, i) {
853 struct net_device *dev = adap->port[i];
854 struct port_info *pi = netdev_priv(dev);
855 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
856 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
857
858 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530859 if (adap->msi_idx > 0)
860 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000861 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530862 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530863 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530864 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530865 t4_get_tp_ch_map(adap,
866 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000867 if (err)
868 goto freeout;
869 q->rspq.idx = j;
870 memset(&q->stats, 0, sizeof(q->stats));
871 }
872 for (j = 0; j < pi->nqsets; j++, t++) {
873 err = t4_sge_alloc_eth_txq(adap, t, dev,
874 netdev_get_tx_queue(dev, j),
875 s->fw_evtq.cntxt_id);
876 if (err)
877 goto freeout;
878 }
879 }
880
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000881 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530882 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000883 * have RDMA queues, and that's the right value.
884 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530885 if (rxq_info)
886 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
887
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000888 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530889 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000890 if (err)
891 goto freeout;
892 }
893
Atul Guptaa45695042017-07-04 16:46:20 +0530894 if (!is_t4(adap->params.chip)) {
895 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
896 netdev_get_tx_queue(adap->port[0], 0)
897 , s->fw_evtq.cntxt_id);
898 if (err)
899 goto freeout;
900 }
901
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530902 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530903 MPS_TRC_RSS_CONTROL_A :
904 MPS_T5_TRC_RSS_CONTROL_A,
905 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
906 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000907 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530908freeout:
909 t4_free_sge_resources(adap);
910 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000911}
912
Anish Bhatt688848b2014-06-19 21:37:13 -0700913static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
914 void *accel_priv, select_queue_fallback_t fallback)
915{
916 int txq;
917
918#ifdef CONFIG_CHELSIO_T4_DCB
919 /* If a Data Center Bridging has been successfully negotiated on this
920 * link then we'll use the skb's priority to map it to a TX Queue.
921 * The skb's priority is determined via the VLAN Tag Priority Code
922 * Point field.
923 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530924 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700925 u16 vlan_tci;
926 int err;
927
928 err = vlan_get_tag(skb, &vlan_tci);
929 if (unlikely(err)) {
930 if (net_ratelimit())
931 netdev_warn(dev,
932 "TX Packet without VLAN Tag on DCB Link\n");
933 txq = 0;
934 } else {
935 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530936#ifdef CONFIG_CHELSIO_T4_FCOE
937 if (skb->protocol == htons(ETH_P_FCOE))
938 txq = skb->priority & 0x7;
939#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700940 }
941 return txq;
942 }
943#endif /* CONFIG_CHELSIO_T4_DCB */
944
945 if (select_queue) {
946 txq = (skb_rx_queue_recorded(skb)
947 ? skb_get_rx_queue(skb)
948 : smp_processor_id());
949
950 while (unlikely(txq >= dev->real_num_tx_queues))
951 txq -= dev->real_num_tx_queues;
952
953 return txq;
954 }
955
956 return fallback(dev, skb) % dev->real_num_tx_queues;
957}
958
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000959static int closest_timer(const struct sge *s, int time)
960{
961 int i, delta, match = 0, min_delta = INT_MAX;
962
963 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
964 delta = time - s->timer_val[i];
965 if (delta < 0)
966 delta = -delta;
967 if (delta < min_delta) {
968 min_delta = delta;
969 match = i;
970 }
971 }
972 return match;
973}
974
975static int closest_thres(const struct sge *s, int thres)
976{
977 int i, delta, match = 0, min_delta = INT_MAX;
978
979 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
980 delta = thres - s->counter_val[i];
981 if (delta < 0)
982 delta = -delta;
983 if (delta < min_delta) {
984 min_delta = delta;
985 match = i;
986 }
987 }
988 return match;
989}
990
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000991/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530992 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000993 * @q: the Rx queue
994 * @us: the hold-off time in us, or 0 to disable timer
995 * @cnt: the hold-off packet count, or 0 to disable counter
996 *
997 * Sets an Rx queue's interrupt hold-off time and packet count. At least
998 * one of the two needs to be enabled for the queue to generate interrupts.
999 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301000int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1001 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001002{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301003 struct adapter *adap = q->adap;
1004
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001005 if ((us | cnt) == 0)
1006 cnt = 1;
1007
1008 if (cnt) {
1009 int err;
1010 u32 v, new_idx;
1011
1012 new_idx = closest_thres(&adap->sge, cnt);
1013 if (q->desc && q->pktcnt_idx != new_idx) {
1014 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301015 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1016 FW_PARAMS_PARAM_X_V(
1017 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1018 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301019 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1020 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001021 if (err)
1022 return err;
1023 }
1024 q->pktcnt_idx = new_idx;
1025 }
1026
1027 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301028 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001029 return 0;
1030}
1031
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001032static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001033{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001034 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001035 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001036 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001037
Patrick McHardyf6469682013-04-19 02:04:27 +00001038 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001039 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001040
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301041 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001042 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001043 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001044 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001045 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001046 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001047}
1048
Bill Pemberton91744942012-12-03 09:23:02 -05001049static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001050{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001051 if (IS_ERR_OR_NULL(adap->debugfs_root))
1052 return -1;
1053
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301054#ifdef CONFIG_DEBUG_FS
1055 t4_setup_debugfs(adap);
1056#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001057 return 0;
1058}
1059
1060/*
1061 * upper-layer driver support
1062 */
1063
1064/*
1065 * Allocate an active-open TID and set it to the supplied value.
1066 */
1067int cxgb4_alloc_atid(struct tid_info *t, void *data)
1068{
1069 int atid = -1;
1070
1071 spin_lock_bh(&t->atid_lock);
1072 if (t->afree) {
1073 union aopen_entry *p = t->afree;
1074
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001075 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001076 t->afree = p->next;
1077 p->data = data;
1078 t->atids_in_use++;
1079 }
1080 spin_unlock_bh(&t->atid_lock);
1081 return atid;
1082}
1083EXPORT_SYMBOL(cxgb4_alloc_atid);
1084
1085/*
1086 * Release an active-open TID.
1087 */
1088void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1089{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001090 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001091
1092 spin_lock_bh(&t->atid_lock);
1093 p->next = t->afree;
1094 t->afree = p;
1095 t->atids_in_use--;
1096 spin_unlock_bh(&t->atid_lock);
1097}
1098EXPORT_SYMBOL(cxgb4_free_atid);
1099
1100/*
1101 * Allocate a server TID and set it to the supplied value.
1102 */
1103int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1104{
1105 int stid;
1106
1107 spin_lock_bh(&t->stid_lock);
1108 if (family == PF_INET) {
1109 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1110 if (stid < t->nstids)
1111 __set_bit(stid, t->stid_bmap);
1112 else
1113 stid = -1;
1114 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301115 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001116 if (stid < 0)
1117 stid = -1;
1118 }
1119 if (stid >= 0) {
1120 t->stid_tab[stid].data = data;
1121 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301122 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1123 * This is equivalent to 4 TIDs. With CLIP enabled it
1124 * needs 2 TIDs.
1125 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301126 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301127 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301128 t->v6_stids_in_use += 2;
1129 } else {
1130 t->stids_in_use++;
1131 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001132 }
1133 spin_unlock_bh(&t->stid_lock);
1134 return stid;
1135}
1136EXPORT_SYMBOL(cxgb4_alloc_stid);
1137
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001138/* Allocate a server filter TID and set it to the supplied value.
1139 */
1140int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1141{
1142 int stid;
1143
1144 spin_lock_bh(&t->stid_lock);
1145 if (family == PF_INET) {
1146 stid = find_next_zero_bit(t->stid_bmap,
1147 t->nstids + t->nsftids, t->nstids);
1148 if (stid < (t->nstids + t->nsftids))
1149 __set_bit(stid, t->stid_bmap);
1150 else
1151 stid = -1;
1152 } else {
1153 stid = -1;
1154 }
1155 if (stid >= 0) {
1156 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301157 stid -= t->nstids;
1158 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301159 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001160 }
1161 spin_unlock_bh(&t->stid_lock);
1162 return stid;
1163}
1164EXPORT_SYMBOL(cxgb4_alloc_sftid);
1165
1166/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001167 */
1168void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1169{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301170 /* Is it a server filter TID? */
1171 if (t->nsftids && (stid >= t->sftid_base)) {
1172 stid -= t->sftid_base;
1173 stid += t->nstids;
1174 } else {
1175 stid -= t->stid_base;
1176 }
1177
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001178 spin_lock_bh(&t->stid_lock);
1179 if (family == PF_INET)
1180 __clear_bit(stid, t->stid_bmap);
1181 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301182 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001183 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301184 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301185 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301186 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301187 t->v6_stids_in_use -= 2;
1188 } else {
1189 t->stids_in_use--;
1190 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301191 } else {
1192 t->sftids_in_use--;
1193 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301194
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001195 spin_unlock_bh(&t->stid_lock);
1196}
1197EXPORT_SYMBOL(cxgb4_free_stid);
1198
1199/*
1200 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1201 */
1202static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1203 unsigned int tid)
1204{
1205 struct cpl_tid_release *req;
1206
1207 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001208 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001209 INIT_TP_WR(req, tid);
1210 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1211}
1212
1213/*
1214 * Queue a TID release request and if necessary schedule a work queue to
1215 * process it.
1216 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001217static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1218 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001219{
1220 void **p = &t->tid_tab[tid];
1221 struct adapter *adap = container_of(t, struct adapter, tids);
1222
1223 spin_lock_bh(&adap->tid_release_lock);
1224 *p = adap->tid_release_head;
1225 /* Low 2 bits encode the Tx channel number */
1226 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1227 if (!adap->tid_release_task_busy) {
1228 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001229 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001230 }
1231 spin_unlock_bh(&adap->tid_release_lock);
1232}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001233
1234/*
1235 * Process the list of pending TID release requests.
1236 */
1237static void process_tid_release_list(struct work_struct *work)
1238{
1239 struct sk_buff *skb;
1240 struct adapter *adap;
1241
1242 adap = container_of(work, struct adapter, tid_release_task);
1243
1244 spin_lock_bh(&adap->tid_release_lock);
1245 while (adap->tid_release_head) {
1246 void **p = adap->tid_release_head;
1247 unsigned int chan = (uintptr_t)p & 3;
1248 p = (void *)p - chan;
1249
1250 adap->tid_release_head = *p;
1251 *p = NULL;
1252 spin_unlock_bh(&adap->tid_release_lock);
1253
1254 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1255 GFP_KERNEL)))
1256 schedule_timeout_uninterruptible(1);
1257
1258 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1259 t4_ofld_send(adap, skb);
1260 spin_lock_bh(&adap->tid_release_lock);
1261 }
1262 adap->tid_release_task_busy = false;
1263 spin_unlock_bh(&adap->tid_release_lock);
1264}
1265
1266/*
1267 * Release a TID and inform HW. If we are unable to allocate the release
1268 * message we defer to a work queue.
1269 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301270void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1271 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001272{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001273 struct sk_buff *skb;
1274 struct adapter *adap = container_of(t, struct adapter, tids);
1275
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301276 WARN_ON(tid >= t->ntids);
1277
1278 if (t->tid_tab[tid]) {
1279 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301280 atomic_dec(&t->conns_in_use);
1281 if (t->hash_base && (tid >= t->hash_base)) {
1282 if (family == AF_INET6)
1283 atomic_sub(2, &t->hash_tids_in_use);
1284 else
1285 atomic_dec(&t->hash_tids_in_use);
1286 } else {
1287 if (family == AF_INET6)
1288 atomic_sub(2, &t->tids_in_use);
1289 else
1290 atomic_dec(&t->tids_in_use);
1291 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301292 }
1293
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001294 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1295 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001296 mk_tid_release(skb, chan, tid);
1297 t4_ofld_send(adap, skb);
1298 } else
1299 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001300}
1301EXPORT_SYMBOL(cxgb4_remove_tid);
1302
1303/*
1304 * Allocate and initialize the TID tables. Returns 0 on success.
1305 */
1306static int tid_init(struct tid_info *t)
1307{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301308 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301309 unsigned int max_ftids = t->nftids + t->nsftids;
1310 unsigned int natids = t->natids;
1311 unsigned int stid_bmap_size;
1312 unsigned int ftid_bmap_size;
1313 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001314
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001315 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301316 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001317 size = t->ntids * sizeof(*t->tid_tab) +
1318 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001319 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001320 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001321 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301322 max_ftids * sizeof(*t->ftid_tab) +
1323 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001324
Michal Hocko752ade62017-05-08 15:57:27 -07001325 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001326 if (!t->tid_tab)
1327 return -ENOMEM;
1328
1329 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1330 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001331 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001332 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301333 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001334 spin_lock_init(&t->stid_lock);
1335 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301336 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001337
1338 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301339 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301340 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001341 t->afree = NULL;
1342 t->atids_in_use = 0;
1343 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301344 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301345 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001346
1347 /* Setup the free list for atid_tab and clear the stid bitmap. */
1348 if (natids) {
1349 while (--natids)
1350 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1351 t->afree = t->atid_tab;
1352 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301353
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301354 if (is_offload(adap)) {
1355 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1356 /* Reserve stid 0 for T4/T5 adapters */
1357 if (!t->stid_base &&
1358 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1359 __set_bit(0, t->stid_bmap);
1360 }
1361
1362 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001363 return 0;
1364}
1365
1366/**
1367 * cxgb4_create_server - create an IP server
1368 * @dev: the device
1369 * @stid: the server TID
1370 * @sip: local IP address to bind server to
1371 * @sport: the server's TCP port
1372 * @queue: queue to direct messages from this server to
1373 *
1374 * Create an IP server for the given port and address.
1375 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1376 */
1377int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001378 __be32 sip, __be16 sport, __be16 vlan,
1379 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001380{
1381 unsigned int chan;
1382 struct sk_buff *skb;
1383 struct adapter *adap;
1384 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301385 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001386
1387 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1388 if (!skb)
1389 return -ENOMEM;
1390
1391 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001392 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001393 INIT_TP_WR(req, 0);
1394 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1395 req->local_port = sport;
1396 req->peer_port = htons(0);
1397 req->local_ip = sip;
1398 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001399 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001400 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001401 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1402 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301403 ret = t4_mgmt_tx(adap, skb);
1404 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001405}
1406EXPORT_SYMBOL(cxgb4_create_server);
1407
Vipul Pandya80f40c12013-07-04 16:10:45 +05301408/* cxgb4_create_server6 - create an IPv6 server
1409 * @dev: the device
1410 * @stid: the server TID
1411 * @sip: local IPv6 address to bind server to
1412 * @sport: the server's TCP port
1413 * @queue: queue to direct messages from this server to
1414 *
1415 * Create an IPv6 server for the given port and address.
1416 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1417 */
1418int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1419 const struct in6_addr *sip, __be16 sport,
1420 unsigned int queue)
1421{
1422 unsigned int chan;
1423 struct sk_buff *skb;
1424 struct adapter *adap;
1425 struct cpl_pass_open_req6 *req;
1426 int ret;
1427
1428 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1429 if (!skb)
1430 return -ENOMEM;
1431
1432 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001433 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301434 INIT_TP_WR(req, 0);
1435 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1436 req->local_port = sport;
1437 req->peer_port = htons(0);
1438 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1439 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1440 req->peer_ip_hi = cpu_to_be64(0);
1441 req->peer_ip_lo = cpu_to_be64(0);
1442 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001443 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001444 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1445 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301446 ret = t4_mgmt_tx(adap, skb);
1447 return net_xmit_eval(ret);
1448}
1449EXPORT_SYMBOL(cxgb4_create_server6);
1450
1451int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1452 unsigned int queue, bool ipv6)
1453{
1454 struct sk_buff *skb;
1455 struct adapter *adap;
1456 struct cpl_close_listsvr_req *req;
1457 int ret;
1458
1459 adap = netdev2adap(dev);
1460
1461 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1462 if (!skb)
1463 return -ENOMEM;
1464
Johannes Berg4df864c2017-06-16 14:29:21 +02001465 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301466 INIT_TP_WR(req, 0);
1467 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001468 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1469 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301470 ret = t4_mgmt_tx(adap, skb);
1471 return net_xmit_eval(ret);
1472}
1473EXPORT_SYMBOL(cxgb4_remove_server);
1474
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001475/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001476 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1477 * @mtus: the HW MTU table
1478 * @mtu: the target MTU
1479 * @idx: index of selected entry in the MTU table
1480 *
1481 * Returns the index and the value in the HW MTU table that is closest to
1482 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1483 * table, in which case that smallest available value is selected.
1484 */
1485unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1486 unsigned int *idx)
1487{
1488 unsigned int i = 0;
1489
1490 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1491 ++i;
1492 if (idx)
1493 *idx = i;
1494 return mtus[i];
1495}
1496EXPORT_SYMBOL(cxgb4_best_mtu);
1497
1498/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301499 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1500 * @mtus: the HW MTU table
1501 * @header_size: Header Size
1502 * @data_size_max: maximum Data Segment Size
1503 * @data_size_align: desired Data Segment Size Alignment (2^N)
1504 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1505 *
1506 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1507 * MTU Table based solely on a Maximum MTU parameter, we break that
1508 * parameter up into a Header Size and Maximum Data Segment Size, and
1509 * provide a desired Data Segment Size Alignment. If we find an MTU in
1510 * the Hardware MTU Table which will result in a Data Segment Size with
1511 * the requested alignment _and_ that MTU isn't "too far" from the
1512 * closest MTU, then we'll return that rather than the closest MTU.
1513 */
1514unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1515 unsigned short header_size,
1516 unsigned short data_size_max,
1517 unsigned short data_size_align,
1518 unsigned int *mtu_idxp)
1519{
1520 unsigned short max_mtu = header_size + data_size_max;
1521 unsigned short data_size_align_mask = data_size_align - 1;
1522 int mtu_idx, aligned_mtu_idx;
1523
1524 /* Scan the MTU Table till we find an MTU which is larger than our
1525 * Maximum MTU or we reach the end of the table. Along the way,
1526 * record the last MTU found, if any, which will result in a Data
1527 * Segment Length matching the requested alignment.
1528 */
1529 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1530 unsigned short data_size = mtus[mtu_idx] - header_size;
1531
1532 /* If this MTU minus the Header Size would result in a
1533 * Data Segment Size of the desired alignment, remember it.
1534 */
1535 if ((data_size & data_size_align_mask) == 0)
1536 aligned_mtu_idx = mtu_idx;
1537
1538 /* If we're not at the end of the Hardware MTU Table and the
1539 * next element is larger than our Maximum MTU, drop out of
1540 * the loop.
1541 */
1542 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1543 break;
1544 }
1545
1546 /* If we fell out of the loop because we ran to the end of the table,
1547 * then we just have to use the last [largest] entry.
1548 */
1549 if (mtu_idx == NMTUS)
1550 mtu_idx--;
1551
1552 /* If we found an MTU which resulted in the requested Data Segment
1553 * Length alignment and that's "not far" from the largest MTU which is
1554 * less than or equal to the maximum MTU, then use that.
1555 */
1556 if (aligned_mtu_idx >= 0 &&
1557 mtu_idx - aligned_mtu_idx <= 1)
1558 mtu_idx = aligned_mtu_idx;
1559
1560 /* If the caller has passed in an MTU Index pointer, pass the
1561 * MTU Index back. Return the MTU value.
1562 */
1563 if (mtu_idxp)
1564 *mtu_idxp = mtu_idx;
1565 return mtus[mtu_idx];
1566}
1567EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1568
1569/**
Hariprasad S27999802015-09-23 17:19:26 +05301570 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1571 * @chip: chip type
1572 * @viid: VI id of the given port
1573 *
1574 * Return the SMT index for this VI.
1575 */
1576unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1577{
1578 /* In T4/T5, SMT contains 256 SMAC entries organized in
1579 * 128 rows of 2 entries each.
1580 * In T6, SMT contains 256 SMAC entries in 256 rows.
1581 * TODO: The below code needs to be updated when we add support
1582 * for 256 VFs.
1583 */
1584 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1585 return ((viid & 0x7f) << 1);
1586 else
1587 return (viid & 0x7f);
1588}
1589EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1590
1591/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001592 * cxgb4_port_chan - get the HW channel of a port
1593 * @dev: the net device for the port
1594 *
1595 * Return the HW Tx channel of the given port.
1596 */
1597unsigned int cxgb4_port_chan(const struct net_device *dev)
1598{
1599 return netdev2pinfo(dev)->tx_chan;
1600}
1601EXPORT_SYMBOL(cxgb4_port_chan);
1602
Vipul Pandya881806b2012-05-18 15:29:24 +05301603unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1604{
1605 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001606 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301607
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301608 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1609 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301610 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301611 lp_count = LP_COUNT_G(v1);
1612 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001613 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301614 lp_count = LP_COUNT_T5_G(v1);
1615 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001616 }
1617 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301618}
1619EXPORT_SYMBOL(cxgb4_dbfifo_count);
1620
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001621/**
1622 * cxgb4_port_viid - get the VI id of a port
1623 * @dev: the net device for the port
1624 *
1625 * Return the VI id of the given port.
1626 */
1627unsigned int cxgb4_port_viid(const struct net_device *dev)
1628{
1629 return netdev2pinfo(dev)->viid;
1630}
1631EXPORT_SYMBOL(cxgb4_port_viid);
1632
1633/**
1634 * cxgb4_port_idx - get the index of a port
1635 * @dev: the net device for the port
1636 *
1637 * Return the index of the given port.
1638 */
1639unsigned int cxgb4_port_idx(const struct net_device *dev)
1640{
1641 return netdev2pinfo(dev)->port_id;
1642}
1643EXPORT_SYMBOL(cxgb4_port_idx);
1644
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001645void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1646 struct tp_tcp_stats *v6)
1647{
1648 struct adapter *adap = pci_get_drvdata(pdev);
1649
1650 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301651 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001652 spin_unlock(&adap->stats_lock);
1653}
1654EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1655
1656void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1657 const unsigned int *pgsz_order)
1658{
1659 struct adapter *adap = netdev2adap(dev);
1660
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301661 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1662 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1663 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1664 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001665}
1666EXPORT_SYMBOL(cxgb4_iscsi_init);
1667
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301668int cxgb4_flush_eq_cache(struct net_device *dev)
1669{
1670 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301671
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301672 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301673}
1674EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1675
1676static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1677{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301678 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301679 __be64 indices;
1680 int ret;
1681
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301682 spin_lock(&adap->win0_lock);
1683 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1684 sizeof(indices), (__be32 *)&indices,
1685 T4_MEMORY_READ);
1686 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301687 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001688 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1689 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301690 }
1691 return ret;
1692}
1693
1694int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1695 u16 size)
1696{
1697 struct adapter *adap = netdev2adap(dev);
1698 u16 hw_pidx, hw_cidx;
1699 int ret;
1700
1701 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1702 if (ret)
1703 goto out;
1704
1705 if (pidx != hw_pidx) {
1706 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301707 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301708
1709 if (pidx >= hw_pidx)
1710 delta = pidx - hw_pidx;
1711 else
1712 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301713
1714 if (is_t4(adap->params.chip))
1715 val = PIDX_V(delta);
1716 else
1717 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301718 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301719 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1720 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301721 }
1722out:
1723 return ret;
1724}
1725EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1726
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301727int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1728{
1729 struct adapter *adap;
1730 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301731 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301732 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1733 int ret;
1734
1735 adap = netdev2adap(dev);
1736
1737 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1738
1739 /* Figure out where the offset lands in the Memory Type/Address scheme.
1740 * This code assumes that the memory is laid out starting at offset 0
1741 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1742 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1743 * MC0, and some have both MC0 and MC1.
1744 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301745 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1746 edc0_size = EDRAM0_SIZE_G(size) << 20;
1747 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1748 edc1_size = EDRAM1_SIZE_G(size) << 20;
1749 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1750 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301751
1752 edc0_end = edc0_size;
1753 edc1_end = edc0_end + edc1_size;
1754 mc0_end = edc1_end + mc0_size;
1755
1756 if (offset < edc0_end) {
1757 memtype = MEM_EDC0;
1758 memaddr = offset;
1759 } else if (offset < edc1_end) {
1760 memtype = MEM_EDC1;
1761 memaddr = offset - edc0_end;
1762 } else {
1763 if (offset < mc0_end) {
1764 memtype = MEM_MC0;
1765 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301766 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301767 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1768 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301769 mc1_end = mc0_end + mc1_size;
1770 if (offset < mc1_end) {
1771 memtype = MEM_MC1;
1772 memaddr = offset - mc0_end;
1773 } else {
1774 /* offset beyond the end of any memory */
1775 goto err;
1776 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301777 } else {
1778 /* T4/T6 only has a single memory channel */
1779 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301780 }
1781 }
1782
1783 spin_lock(&adap->win0_lock);
1784 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1785 spin_unlock(&adap->win0_lock);
1786 return ret;
1787
1788err:
1789 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1790 stag, offset);
1791 return -EINVAL;
1792}
1793EXPORT_SYMBOL(cxgb4_read_tpte);
1794
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301795u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1796{
1797 u32 hi, lo;
1798 struct adapter *adap;
1799
1800 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301801 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1802 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301803
1804 return ((u64)hi << 32) | (u64)lo;
1805}
1806EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1807
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301808int cxgb4_bar2_sge_qregs(struct net_device *dev,
1809 unsigned int qid,
1810 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301811 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301812 u64 *pbar2_qoffset,
1813 unsigned int *pbar2_qid)
1814{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301815 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301816 qid,
1817 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1818 ? T4_BAR2_QTYPE_EGRESS
1819 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301820 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301821 pbar2_qoffset,
1822 pbar2_qid);
1823}
1824EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1825
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001826static struct pci_driver cxgb4_driver;
1827
1828static void check_neigh_update(struct neighbour *neigh)
1829{
1830 const struct device *parent;
1831 const struct net_device *netdev = neigh->dev;
1832
Parav Panditd0d7b102017-02-04 11:00:49 -06001833 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001834 netdev = vlan_dev_real_dev(netdev);
1835 parent = netdev->dev.parent;
1836 if (parent && parent->driver == &cxgb4_driver.driver)
1837 t4_l2t_update(dev_get_drvdata(parent), neigh);
1838}
1839
1840static int netevent_cb(struct notifier_block *nb, unsigned long event,
1841 void *data)
1842{
1843 switch (event) {
1844 case NETEVENT_NEIGH_UPDATE:
1845 check_neigh_update(data);
1846 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001847 case NETEVENT_REDIRECT:
1848 default:
1849 break;
1850 }
1851 return 0;
1852}
1853
1854static bool netevent_registered;
1855static struct notifier_block cxgb4_netevent_nb = {
1856 .notifier_call = netevent_cb
1857};
1858
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301859static void drain_db_fifo(struct adapter *adap, int usecs)
1860{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001861 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301862
1863 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301864 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1865 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301866 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301867 lp_count = LP_COUNT_G(v1);
1868 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001869 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301870 lp_count = LP_COUNT_T5_G(v1);
1871 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001872 }
1873
1874 if (lp_count == 0 && hp_count == 0)
1875 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301876 set_current_state(TASK_UNINTERRUPTIBLE);
1877 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301878 } while (1);
1879}
1880
1881static void disable_txq_db(struct sge_txq *q)
1882{
Steve Wise05eb2382014-03-14 21:52:08 +05301883 unsigned long flags;
1884
1885 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301886 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301887 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301888}
1889
Steve Wise05eb2382014-03-14 21:52:08 +05301890static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301891{
1892 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301893 if (q->db_pidx_inc) {
1894 /* Make sure that all writes to the TX descriptors
1895 * are committed before we tell HW about them.
1896 */
1897 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301898 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1899 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301900 q->db_pidx_inc = 0;
1901 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301902 q->db_disabled = 0;
1903 spin_unlock_irq(&q->db_lock);
1904}
1905
1906static void disable_dbs(struct adapter *adap)
1907{
1908 int i;
1909
1910 for_each_ethrxq(&adap->sge, i)
1911 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301912 if (is_offload(adap)) {
1913 struct sge_uld_txq_info *txq_info =
1914 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1915
1916 if (txq_info) {
1917 for_each_ofldtxq(&adap->sge, i) {
1918 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1919
1920 disable_txq_db(&txq->q);
1921 }
1922 }
1923 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301924 for_each_port(adap, i)
1925 disable_txq_db(&adap->sge.ctrlq[i].q);
1926}
1927
1928static void enable_dbs(struct adapter *adap)
1929{
1930 int i;
1931
1932 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301933 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301934 if (is_offload(adap)) {
1935 struct sge_uld_txq_info *txq_info =
1936 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1937
1938 if (txq_info) {
1939 for_each_ofldtxq(&adap->sge, i) {
1940 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1941
1942 enable_txq_db(adap, &txq->q);
1943 }
1944 }
1945 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301946 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301947 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1948}
1949
1950static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1951{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301952 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1953
1954 if (adap->uld && adap->uld[type].handle)
1955 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301956}
1957
1958static void process_db_full(struct work_struct *work)
1959{
1960 struct adapter *adap;
1961
1962 adap = container_of(work, struct adapter, db_full_task);
1963
1964 drain_db_fifo(adap, dbfifo_drain_delay);
1965 enable_dbs(adap);
1966 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301967 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1968 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1969 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1970 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1971 else
1972 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1973 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301974}
1975
1976static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1977{
1978 u16 hw_pidx, hw_cidx;
1979 int ret;
1980
Steve Wise05eb2382014-03-14 21:52:08 +05301981 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301982 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1983 if (ret)
1984 goto out;
1985 if (q->db_pidx != hw_pidx) {
1986 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301987 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301988
1989 if (q->db_pidx >= hw_pidx)
1990 delta = q->db_pidx - hw_pidx;
1991 else
1992 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301993
1994 if (is_t4(adap->params.chip))
1995 val = PIDX_V(delta);
1996 else
1997 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301998 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301999 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2000 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302001 }
2002out:
2003 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302004 q->db_pidx_inc = 0;
2005 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302006 if (ret)
2007 CH_WARN(adap, "DB drop recovery failed.\n");
2008}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302009
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302010static void recover_all_queues(struct adapter *adap)
2011{
2012 int i;
2013
2014 for_each_ethrxq(&adap->sge, i)
2015 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302016 if (is_offload(adap)) {
2017 struct sge_uld_txq_info *txq_info =
2018 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2019 if (txq_info) {
2020 for_each_ofldtxq(&adap->sge, i) {
2021 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2022
2023 sync_txq_pidx(adap, &txq->q);
2024 }
2025 }
2026 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302027 for_each_port(adap, i)
2028 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2029}
2030
Vipul Pandya881806b2012-05-18 15:29:24 +05302031static void process_db_drop(struct work_struct *work)
2032{
2033 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302034
Vipul Pandya881806b2012-05-18 15:29:24 +05302035 adap = container_of(work, struct adapter, db_drop_task);
2036
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302037 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302038 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002039 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302040 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002041 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302042 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002043 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302044 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302045 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002046 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2047 u16 qid = (dropped_db >> 15) & 0x1ffff;
2048 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302049 u64 bar2_qoffset;
2050 unsigned int bar2_qid;
2051 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002052
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302053 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002054 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302055 if (ret)
2056 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2057 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2058 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302059 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302060 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002061
2062 /* Re-enable BAR2 WC */
2063 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2064 }
2065
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302066 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2067 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302068}
2069
2070void t4_db_full(struct adapter *adap)
2071{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302072 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302073 disable_dbs(adap);
2074 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302075 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2076 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002077 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002078 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302079}
2080
2081void t4_db_dropped(struct adapter *adap)
2082{
Steve Wise05eb2382014-03-14 21:52:08 +05302083 if (is_t4(adap->params.chip)) {
2084 disable_dbs(adap);
2085 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2086 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002087 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302088}
2089
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302090void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002091{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002092 if (!netevent_registered) {
2093 register_netevent_notifier(&cxgb4_netevent_nb);
2094 netevent_registered = true;
2095 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002096}
2097
2098static void detach_ulds(struct adapter *adap)
2099{
2100 unsigned int i;
2101
2102 mutex_lock(&uld_mutex);
2103 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002104
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002105 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002106 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302107 adap->uld[i].state_change(adap->uld[i].handle,
2108 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002109
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002110 if (netevent_registered && list_empty(&adapter_list)) {
2111 unregister_netevent_notifier(&cxgb4_netevent_nb);
2112 netevent_registered = false;
2113 }
2114 mutex_unlock(&uld_mutex);
2115}
2116
2117static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2118{
2119 unsigned int i;
2120
2121 mutex_lock(&uld_mutex);
2122 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302123 if (adap->uld && adap->uld[i].handle)
2124 adap->uld[i].state_change(adap->uld[i].handle,
2125 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002126 mutex_unlock(&uld_mutex);
2127}
2128
Anish Bhatt1bb60372014-10-14 20:07:22 -07002129#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002130static int cxgb4_inet6addr_handler(struct notifier_block *this,
2131 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302132{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002133 struct inet6_ifaddr *ifa = data;
2134 struct net_device *event_dev = ifa->idev->dev;
2135 const struct device *parent = NULL;
2136#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302137 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002138#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002139 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002140 event_dev = vlan_dev_real_dev(event_dev);
2141#if IS_ENABLED(CONFIG_BONDING)
2142 if (event_dev->flags & IFF_MASTER) {
2143 list_for_each_entry(adap, &adapter_list, list_node) {
2144 switch (event) {
2145 case NETDEV_UP:
2146 cxgb4_clip_get(adap->port[0],
2147 (const u32 *)ifa, 1);
2148 break;
2149 case NETDEV_DOWN:
2150 cxgb4_clip_release(adap->port[0],
2151 (const u32 *)ifa, 1);
2152 break;
2153 default:
2154 break;
2155 }
2156 }
2157 return NOTIFY_OK;
2158 }
2159#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302160
Anish Bhattb5a02f52015-01-14 15:17:34 -08002161 if (event_dev)
2162 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302163
Anish Bhattb5a02f52015-01-14 15:17:34 -08002164 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302165 switch (event) {
2166 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002167 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302168 break;
2169 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002170 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302171 break;
2172 default:
2173 break;
2174 }
2175 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002176 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302177}
2178
Anish Bhattb5a02f52015-01-14 15:17:34 -08002179static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302180static struct notifier_block cxgb4_inet6addr_notifier = {
2181 .notifier_call = cxgb4_inet6addr_handler
2182};
2183
Vipul Pandya01bcca62013-07-04 16:10:46 +05302184static void update_clip(const struct adapter *adap)
2185{
2186 int i;
2187 struct net_device *dev;
2188 int ret;
2189
2190 rcu_read_lock();
2191
2192 for (i = 0; i < MAX_NPORTS; i++) {
2193 dev = adap->port[i];
2194 ret = 0;
2195
2196 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002197 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302198
2199 if (ret < 0)
2200 break;
2201 }
2202 rcu_read_unlock();
2203}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002204#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302205
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002206/**
2207 * cxgb_up - enable the adapter
2208 * @adap: adapter being enabled
2209 *
2210 * Called when the first port is enabled, this function performs the
2211 * actions necessary to make an adapter operational, such as completing
2212 * the initialization of HW modules, and enabling interrupts.
2213 *
2214 * Must be called with the rtnl lock held.
2215 */
2216static int cxgb_up(struct adapter *adap)
2217{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002218 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002219
Raju Rangoju91060382017-06-19 17:40:48 +05302220 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002221 err = setup_sge_queues(adap);
2222 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302223 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002224 err = setup_rss(adap);
2225 if (err)
2226 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002227
2228 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002229 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002230 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2231 adap->msix_info[0].desc, adap);
2232 if (err)
2233 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002234 err = request_msix_queue_irqs(adap);
2235 if (err) {
2236 free_irq(adap->msix_info[0].vec, adap);
2237 goto irq_err;
2238 }
2239 } else {
2240 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2241 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002242 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002243 if (err)
2244 goto irq_err;
2245 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302246
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002247 enable_rx(adap);
2248 t4_sge_start(adap);
2249 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002250 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302251 mutex_unlock(&uld_mutex);
2252
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002253 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002254#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302255 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002256#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302257 /* Initialize hash mac addr list*/
2258 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002259 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302260
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002261 irq_err:
2262 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002263 freeq:
2264 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302265 rel_lock:
2266 mutex_unlock(&uld_mutex);
2267 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002268}
2269
2270static void cxgb_down(struct adapter *adapter)
2271{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002272 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302273 cancel_work_sync(&adapter->db_full_task);
2274 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002275 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002276 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002277
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002278 t4_sge_stop(adapter);
2279 t4_free_sge_resources(adapter);
2280 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002281}
2282
2283/*
2284 * net_device operations
2285 */
2286static int cxgb_open(struct net_device *dev)
2287{
2288 int err;
2289 struct port_info *pi = netdev_priv(dev);
2290 struct adapter *adapter = pi->adapter;
2291
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002292 netif_carrier_off(dev);
2293
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002294 if (!(adapter->flags & FULL_INIT_DONE)) {
2295 err = cxgb_up(adapter);
2296 if (err < 0)
2297 return err;
2298 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002299
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302300 /* It's possible that the basic port information could have
2301 * changed since we first read it.
2302 */
2303 err = t4_update_port_info(pi);
2304 if (err < 0)
2305 return err;
2306
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002307 err = link_start(dev);
2308 if (!err)
2309 netif_tx_start_all_queues(dev);
2310 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002311}
2312
2313static int cxgb_close(struct net_device *dev)
2314{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002315 struct port_info *pi = netdev_priv(dev);
2316 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302317 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002318
2319 netif_tx_stop_all_queues(dev);
2320 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302321 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2322#ifdef CONFIG_CHELSIO_T4_DCB
2323 cxgb4_dcb_reset(dev);
2324 dcb_tx_queue_prio_enable(dev, false);
2325#endif
2326 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002327}
2328
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002329int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002330 __be32 sip, __be16 sport, __be16 vlan,
2331 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002332{
2333 int ret;
2334 struct filter_entry *f;
2335 struct adapter *adap;
2336 int i;
2337 u8 *val;
2338
2339 adap = netdev2adap(dev);
2340
Vipul Pandya1cab7752012-12-10 09:30:55 +00002341 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302342 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002343 stid += adap->tids.nftids;
2344
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002345 /* Check to make sure the filter requested is writable ...
2346 */
2347 f = &adap->tids.ftid_tab[stid];
2348 ret = writable_filter(f);
2349 if (ret)
2350 return ret;
2351
2352 /* Clear out any old resources being used by the filter before
2353 * we start constructing the new filter.
2354 */
2355 if (f->valid)
2356 clear_filter(adap, f);
2357
2358 /* Clear out filter specifications */
2359 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2360 f->fs.val.lport = cpu_to_be16(sport);
2361 f->fs.mask.lport = ~0;
2362 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002363 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002364 for (i = 0; i < 4; i++) {
2365 f->fs.val.lip[i] = val[i];
2366 f->fs.mask.lip[i] = ~0;
2367 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302368 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002369 f->fs.val.iport = port;
2370 f->fs.mask.iport = mask;
2371 }
2372 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002373
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302374 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302375 f->fs.val.proto = IPPROTO_TCP;
2376 f->fs.mask.proto = ~0;
2377 }
2378
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002379 f->fs.dirsteer = 1;
2380 f->fs.iq = queue;
2381 /* Mark filter as locked */
2382 f->locked = 1;
2383 f->fs.rpttid = 1;
2384
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302385 /* Save the actual tid. We need this to get the corresponding
2386 * filter entry structure in filter_rpl.
2387 */
2388 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002389 ret = set_filter_wr(adap, stid);
2390 if (ret) {
2391 clear_filter(adap, f);
2392 return ret;
2393 }
2394
2395 return 0;
2396}
2397EXPORT_SYMBOL(cxgb4_create_server_filter);
2398
2399int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2400 unsigned int queue, bool ipv6)
2401{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002402 struct filter_entry *f;
2403 struct adapter *adap;
2404
2405 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002406
2407 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302408 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002409 stid += adap->tids.nftids;
2410
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002411 f = &adap->tids.ftid_tab[stid];
2412 /* Unlock the filter */
2413 f->locked = 0;
2414
Wei Yongjun8c148462016-08-20 15:32:41 +00002415 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002416}
2417EXPORT_SYMBOL(cxgb4_remove_server_filter);
2418
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002419static void cxgb_get_stats(struct net_device *dev,
2420 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002421{
2422 struct port_stats stats;
2423 struct port_info *p = netdev_priv(dev);
2424 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002425
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002426 /* Block retrieving statistics during EEH error
2427 * recovery. Otherwise, the recovery might fail
2428 * and the PCI device will be removed permanently
2429 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002430 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002431 if (!netif_device_present(dev)) {
2432 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002433 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002434 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302435 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2436 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002437 spin_unlock(&adapter->stats_lock);
2438
2439 ns->tx_bytes = stats.tx_octets;
2440 ns->tx_packets = stats.tx_frames;
2441 ns->rx_bytes = stats.rx_octets;
2442 ns->rx_packets = stats.rx_frames;
2443 ns->multicast = stats.rx_mcast_frames;
2444
2445 /* detailed rx_errors */
2446 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2447 stats.rx_runt;
2448 ns->rx_over_errors = 0;
2449 ns->rx_crc_errors = stats.rx_fcs_err;
2450 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302451 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002452 stats.rx_ovflow2 + stats.rx_ovflow3 +
2453 stats.rx_trunc0 + stats.rx_trunc1 +
2454 stats.rx_trunc2 + stats.rx_trunc3;
2455 ns->rx_missed_errors = 0;
2456
2457 /* detailed tx_errors */
2458 ns->tx_aborted_errors = 0;
2459 ns->tx_carrier_errors = 0;
2460 ns->tx_fifo_errors = 0;
2461 ns->tx_heartbeat_errors = 0;
2462 ns->tx_window_errors = 0;
2463
2464 ns->tx_errors = stats.tx_error_frames;
2465 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2466 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002467}
2468
2469static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2470{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002471 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002472 int ret = 0, prtad, devad;
2473 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302474 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002475 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2476
2477 switch (cmd) {
2478 case SIOCGMIIPHY:
2479 if (pi->mdio_addr < 0)
2480 return -EOPNOTSUPP;
2481 data->phy_id = pi->mdio_addr;
2482 break;
2483 case SIOCGMIIREG:
2484 case SIOCSMIIREG:
2485 if (mdio_phy_id_is_c45(data->phy_id)) {
2486 prtad = mdio_phy_id_prtad(data->phy_id);
2487 devad = mdio_phy_id_devad(data->phy_id);
2488 } else if (data->phy_id < 32) {
2489 prtad = data->phy_id;
2490 devad = 0;
2491 data->reg_num &= 0x1f;
2492 } else
2493 return -EINVAL;
2494
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302495 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002496 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002497 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002498 data->reg_num, &data->val_out);
2499 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002500 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002501 data->reg_num, data->val_in);
2502 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302503 case SIOCGHWTSTAMP:
2504 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2505 sizeof(pi->tstamp_config)) ?
2506 -EFAULT : 0;
2507 case SIOCSHWTSTAMP:
2508 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2509 sizeof(pi->tstamp_config)))
2510 return -EFAULT;
2511
Atul Guptaa45695042017-07-04 16:46:20 +05302512 if (!is_t4(adapter->params.chip)) {
2513 switch (pi->tstamp_config.tx_type) {
2514 case HWTSTAMP_TX_OFF:
2515 case HWTSTAMP_TX_ON:
2516 break;
2517 default:
2518 return -ERANGE;
2519 }
2520
2521 switch (pi->tstamp_config.rx_filter) {
2522 case HWTSTAMP_FILTER_NONE:
2523 pi->rxtstamp = false;
2524 break;
2525 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2526 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2527 cxgb4_ptprx_timestamping(pi, pi->port_id,
2528 PTP_TS_L4);
2529 break;
2530 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2531 cxgb4_ptprx_timestamping(pi, pi->port_id,
2532 PTP_TS_L2_L4);
2533 break;
2534 case HWTSTAMP_FILTER_ALL:
2535 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2536 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2537 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2538 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2539 pi->rxtstamp = true;
2540 break;
2541 default:
2542 pi->tstamp_config.rx_filter =
2543 HWTSTAMP_FILTER_NONE;
2544 return -ERANGE;
2545 }
2546
2547 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2548 (pi->tstamp_config.rx_filter ==
2549 HWTSTAMP_FILTER_NONE)) {
2550 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2551 pi->ptp_enable = false;
2552 }
2553
2554 if (pi->tstamp_config.rx_filter !=
2555 HWTSTAMP_FILTER_NONE) {
2556 if (cxgb4_ptp_redirect_rx_packet(adapter,
2557 pi) >= 0)
2558 pi->ptp_enable = true;
2559 }
2560 } else {
2561 /* For T4 Adapters */
2562 switch (pi->tstamp_config.rx_filter) {
2563 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302564 pi->rxtstamp = false;
2565 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302566 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302567 pi->rxtstamp = true;
2568 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302569 default:
2570 pi->tstamp_config.rx_filter =
2571 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302572 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302573 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302574 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302575 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2576 sizeof(pi->tstamp_config)) ?
2577 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002578 default:
2579 return -EOPNOTSUPP;
2580 }
2581 return ret;
2582}
2583
2584static void cxgb_set_rxmode(struct net_device *dev)
2585{
2586 /* unfortunately we can't return errors to the stack */
2587 set_rxmode(dev, -1, false);
2588}
2589
2590static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2591{
2592 int ret;
2593 struct port_info *pi = netdev_priv(dev);
2594
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302595 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002596 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002597 if (!ret)
2598 dev->mtu = new_mtu;
2599 return ret;
2600}
2601
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302602#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302603static int dummy_open(struct net_device *dev)
2604{
2605 /* Turn carrier off since we don't have to transmit anything on this
2606 * interface.
2607 */
2608 netif_carrier_off(dev);
2609 return 0;
2610}
2611
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302612/* Fill MAC address that will be assigned by the FW */
2613static void fill_vf_station_mac_addr(struct adapter *adap)
2614{
2615 unsigned int i;
2616 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2617 int err;
2618 u8 *na;
2619 u16 a, b;
2620
2621 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2622 if (!err) {
2623 na = adap->params.vpd.na;
2624 for (i = 0; i < ETH_ALEN; i++)
2625 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2626 hex2val(na[2 * i + 1]));
2627 a = (hw_addr[0] << 8) | hw_addr[1];
2628 b = (hw_addr[1] << 8) | hw_addr[2];
2629 a ^= b;
2630 a |= 0x0200; /* locally assigned Ethernet MAC address */
2631 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2632 macaddr[0] = a >> 8;
2633 macaddr[1] = a & 0xff;
2634
2635 for (i = 2; i < 5; i++)
2636 macaddr[i] = hw_addr[i + 1];
2637
2638 for (i = 0; i < adap->num_vfs; i++) {
2639 macaddr[5] = adap->pf * 16 + i;
2640 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2641 }
2642 }
2643}
2644
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302645static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2646{
2647 struct port_info *pi = netdev_priv(dev);
2648 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302649 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302650
2651 /* verify MAC addr is valid */
2652 if (!is_valid_ether_addr(mac)) {
2653 dev_err(pi->adapter->pdev_dev,
2654 "Invalid Ethernet address %pM for VF %d\n",
2655 mac, vf);
2656 return -EINVAL;
2657 }
2658
2659 dev_info(pi->adapter->pdev_dev,
2660 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302661 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2662 if (!ret)
2663 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2664 return ret;
2665}
2666
2667static int cxgb_get_vf_config(struct net_device *dev,
2668 int vf, struct ifla_vf_info *ivi)
2669{
2670 struct port_info *pi = netdev_priv(dev);
2671 struct adapter *adap = pi->adapter;
2672
2673 if (vf >= adap->num_vfs)
2674 return -EINVAL;
2675 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302676 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2677 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302678 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2679 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302680}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302681
2682static int cxgb_get_phys_port_id(struct net_device *dev,
2683 struct netdev_phys_item_id *ppid)
2684{
2685 struct port_info *pi = netdev_priv(dev);
2686 unsigned int phy_port_id;
2687
2688 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2689 ppid->id_len = sizeof(phy_port_id);
2690 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2691 return 0;
2692}
2693
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302694static int cxgb_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2695 int max_tx_rate)
2696{
2697 struct port_info *pi = netdev_priv(dev);
2698 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302699 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302700 u32 fw_pfvf, fw_class;
2701 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302702 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302703 u16 pktsize;
2704
2705 if (vf >= adap->num_vfs)
2706 return -EINVAL;
2707
2708 if (min_tx_rate) {
2709 dev_err(adap->pdev_dev,
2710 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2711 min_tx_rate, vf);
2712 return -EINVAL;
2713 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302714
2715 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302716 if (ret != FW_SUCCESS) {
2717 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302718 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302719 return -EINVAL;
2720 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302721
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302722 if (!link_ok) {
2723 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2724 return -EINVAL;
2725 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302726
2727 if (max_tx_rate > speed) {
2728 dev_err(adap->pdev_dev,
2729 "Max tx rate %d for VF %d can't be > link-speed %u",
2730 max_tx_rate, vf, speed);
2731 return -EINVAL;
2732 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302733
2734 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302735 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2736 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2737 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2738 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2739 /* configure Traffic Class for rate-limiting */
2740 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2741 SCHED_CLASS_LEVEL_CL_RL,
2742 SCHED_CLASS_MODE_CLASS,
2743 SCHED_CLASS_RATEUNIT_BITS,
2744 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302745 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302746 max_tx_rate * 1000, 0, pktsize);
2747 if (ret) {
2748 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2749 ret);
2750 return -EINVAL;
2751 }
2752 dev_info(adap->pdev_dev,
2753 "Class %d with MSS %u configured with rate %u\n",
2754 class_id, pktsize, max_tx_rate);
2755
2756 /* bind VF to configured Traffic Class */
2757 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2758 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2759 fw_class = class_id;
2760 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2761 &fw_class);
2762 if (ret) {
2763 dev_err(adap->pdev_dev,
2764 "Err %d in binding VF %d to Traffic Class %d\n",
2765 ret, vf, class_id);
2766 return -EINVAL;
2767 }
2768 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2769 adap->pf, vf, class_id);
2770 adap->vfinfo[vf].tx_rate = max_tx_rate;
2771 return 0;
2772}
2773
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302774#endif
2775
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002776static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2777{
2778 int ret;
2779 struct sockaddr *addr = p;
2780 struct port_info *pi = netdev_priv(dev);
2781
2782 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002783 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002784
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302785 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002786 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002787 if (ret < 0)
2788 return ret;
2789
2790 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2791 pi->xact_addr_filt = ret;
2792 return 0;
2793}
2794
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002795#ifdef CONFIG_NET_POLL_CONTROLLER
2796static void cxgb_netpoll(struct net_device *dev)
2797{
2798 struct port_info *pi = netdev_priv(dev);
2799 struct adapter *adap = pi->adapter;
2800
2801 if (adap->flags & USING_MSIX) {
2802 int i;
2803 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2804
2805 for (i = pi->nqsets; i; i--, rx++)
2806 t4_sge_intr_msix(0, &rx->rspq);
2807 } else
2808 t4_intr_handler(adap)(0, adap);
2809}
2810#endif
2811
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302812static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2813{
2814 struct port_info *pi = netdev_priv(dev);
2815 struct adapter *adap = pi->adapter;
2816 struct sched_class *e;
2817 struct ch_sched_params p;
2818 struct ch_sched_queue qe;
2819 u32 req_rate;
2820 int err = 0;
2821
2822 if (!can_sched(dev))
2823 return -ENOTSUPP;
2824
2825 if (index < 0 || index > pi->nqsets - 1)
2826 return -EINVAL;
2827
2828 if (!(adap->flags & FULL_INIT_DONE)) {
2829 dev_err(adap->pdev_dev,
2830 "Failed to rate limit on queue %d. Link Down?\n",
2831 index);
2832 return -EINVAL;
2833 }
2834
2835 /* Convert from Mbps to Kbps */
2836 req_rate = rate << 10;
2837
2838 /* Max rate is 10 Gbps */
2839 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2840 dev_err(adap->pdev_dev,
2841 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2842 rate, SCHED_MAX_RATE_KBPS);
2843 return -ERANGE;
2844 }
2845
2846 /* First unbind the queue from any existing class */
2847 memset(&qe, 0, sizeof(qe));
2848 qe.queue = index;
2849 qe.class = SCHED_CLS_NONE;
2850
2851 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2852 if (err) {
2853 dev_err(adap->pdev_dev,
2854 "Unbinding Queue %d on port %d fail. Err: %d\n",
2855 index, pi->port_id, err);
2856 return err;
2857 }
2858
2859 /* Queue already unbound */
2860 if (!req_rate)
2861 return 0;
2862
2863 /* Fetch any available unused or matching scheduling class */
2864 memset(&p, 0, sizeof(p));
2865 p.type = SCHED_CLASS_TYPE_PACKET;
2866 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2867 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2868 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2869 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2870 p.u.params.channel = pi->tx_chan;
2871 p.u.params.class = SCHED_CLS_NONE;
2872 p.u.params.minrate = 0;
2873 p.u.params.maxrate = req_rate;
2874 p.u.params.weight = 0;
2875 p.u.params.pktsize = dev->mtu;
2876
2877 e = cxgb4_sched_class_alloc(dev, &p);
2878 if (!e)
2879 return -ENOMEM;
2880
2881 /* Bind the queue to a scheduling class */
2882 memset(&qe, 0, sizeof(qe));
2883 qe.queue = index;
2884 qe.class = e->idx;
2885
2886 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2887 if (err)
2888 dev_err(adap->pdev_dev,
2889 "Queue rate limiting failed. Err: %d\n", err);
2890 return err;
2891}
2892
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302893static int cxgb_setup_tc_flower(struct net_device *dev,
2894 struct tc_cls_flower_offload *cls_flower)
2895{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002896 if (cls_flower->common.chain_index)
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302897 return -EOPNOTSUPP;
2898
2899 switch (cls_flower->command) {
2900 case TC_CLSFLOWER_REPLACE:
2901 return cxgb4_tc_flower_replace(dev, cls_flower);
2902 case TC_CLSFLOWER_DESTROY:
2903 return cxgb4_tc_flower_destroy(dev, cls_flower);
2904 case TC_CLSFLOWER_STATS:
2905 return cxgb4_tc_flower_stats(dev, cls_flower);
2906 default:
2907 return -EOPNOTSUPP;
2908 }
2909}
2910
Jiri Pirkof7323042017-08-07 10:15:20 +02002911static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002912 struct tc_cls_u32_offload *cls_u32)
2913{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002914 if (cls_u32->common.chain_index)
Jiri Pirkof7323042017-08-07 10:15:20 +02002915 return -EOPNOTSUPP;
2916
2917 switch (cls_u32->command) {
2918 case TC_CLSU32_NEW_KNODE:
2919 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002920 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002921 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002922 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002923 default:
2924 return -EOPNOTSUPP;
2925 }
2926}
2927
Jiri Pirkocd019e92017-10-19 15:50:40 +02002928static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2929 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302930{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002931 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302932 struct port_info *pi = netdev2pinfo(dev);
2933 struct adapter *adap = netdev2adap(dev);
2934
2935 if (!(adap->flags & FULL_INIT_DONE)) {
2936 dev_err(adap->pdev_dev,
2937 "Failed to setup tc on port %d. Link Down?\n",
2938 pi->port_id);
2939 return -EINVAL;
2940 }
2941
Jiri Pirkof7323042017-08-07 10:15:20 +02002942 switch (type) {
2943 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002944 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302945 case TC_SETUP_CLSFLOWER:
2946 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002947 default:
2948 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302949 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302950}
2951
Jiri Pirkocd019e92017-10-19 15:50:40 +02002952static int cxgb_setup_tc_block(struct net_device *dev,
2953 struct tc_block_offload *f)
2954{
2955 struct port_info *pi = netdev2pinfo(dev);
2956
2957 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2958 return -EOPNOTSUPP;
2959
2960 switch (f->command) {
2961 case TC_BLOCK_BIND:
2962 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
2963 pi, dev);
2964 case TC_BLOCK_UNBIND:
2965 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
2966 return 0;
2967 default:
2968 return -EOPNOTSUPP;
2969 }
2970}
2971
2972static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
2973 void *type_data)
2974{
2975 switch (type) {
Jiri Pirkocd019e92017-10-19 15:50:40 +02002976 case TC_SETUP_BLOCK:
2977 return cxgb_setup_tc_block(dev, type_data);
2978 default:
2979 return -EOPNOTSUPP;
2980 }
2981}
2982
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302983static netdev_features_t cxgb_fix_features(struct net_device *dev,
2984 netdev_features_t features)
2985{
2986 /* Disable GRO, if RX_CSUM is disabled */
2987 if (!(features & NETIF_F_RXCSUM))
2988 features &= ~NETIF_F_GRO;
2989
2990 return features;
2991}
2992
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002993static const struct net_device_ops cxgb4_netdev_ops = {
2994 .ndo_open = cxgb_open,
2995 .ndo_stop = cxgb_close,
2996 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002997 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002998 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002999 .ndo_set_rx_mode = cxgb_set_rxmode,
3000 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003001 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003002 .ndo_validate_addr = eth_validate_addr,
3003 .ndo_do_ioctl = cxgb_ioctl,
3004 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003005#ifdef CONFIG_NET_POLL_CONTROLLER
3006 .ndo_poll_controller = cxgb_netpoll,
3007#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303008#ifdef CONFIG_CHELSIO_T4_FCOE
3009 .ndo_fcoe_enable = cxgb_fcoe_enable,
3010 .ndo_fcoe_disable = cxgb_fcoe_disable,
3011#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303012 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303013 .ndo_setup_tc = cxgb_setup_tc,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303014 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003015};
3016
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303017#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303018static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
3019 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303020 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05303021 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05303022 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05303023 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303024};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303025#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303026
3027static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3028{
3029 struct adapter *adapter = netdev2adap(dev);
3030
3031 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3032 strlcpy(info->version, cxgb4_driver_version,
3033 sizeof(info->version));
3034 strlcpy(info->bus_info, pci_name(adapter->pdev),
3035 sizeof(info->bus_info));
3036}
3037
3038static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3039 .get_drvinfo = get_drvinfo,
3040};
3041
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003042void t4_fatal_err(struct adapter *adap)
3043{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303044 int port;
3045
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003046 if (pci_channel_offline(adap->pdev))
3047 return;
3048
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303049 /* Disable the SGE since ULDs are going to free resources that
3050 * could be exposed to the adapter. RDMA MWs for example...
3051 */
3052 t4_shutdown_adapter(adap);
3053 for_each_port(adap, port) {
3054 struct net_device *dev = adap->port[port];
3055
3056 /* If we get here in very early initialization the network
3057 * devices may not have been set up yet.
3058 */
3059 if (!dev)
3060 continue;
3061
3062 netif_tx_stop_all_queues(dev);
3063 netif_carrier_off(dev);
3064 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003065 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3066}
3067
3068static void setup_memwin(struct adapter *adap)
3069{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303070 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003071
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303072 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003073}
3074
3075static void setup_memwin_rdma(struct adapter *adap)
3076{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003077 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303078 u32 start;
3079 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003080
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303081 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3082 start &= PCI_BASE_ADDRESS_MEM_MASK;
3083 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003084 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3085 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303086 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3087 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003088 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303089 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003090 adap->vres.ocq.start);
3091 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303092 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003093 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003094}
3095
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003096static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3097{
3098 u32 v;
3099 int ret;
3100
3101 /* get device capabilities */
3102 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303103 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3104 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303105 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303106 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003107 if (ret < 0)
3108 return ret;
3109
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303110 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3111 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303112 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003113 if (ret < 0)
3114 return ret;
3115
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303116 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003117 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303118 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3119 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003120 if (ret < 0)
3121 return ret;
3122
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303123 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303124 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3125 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003126 if (ret < 0)
3127 return ret;
3128
3129 t4_sge_init(adap);
3130
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003131 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303132 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303133 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303134 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3135 v = t4_read_reg(adap, TP_PIO_DATA_A);
3136 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003137
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003138 /* first 4 Tx modulation queues point to consecutive Tx channels */
3139 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303140 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3141 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003142
3143 /* associate each Tx modulation queue with consecutive Tx channels */
3144 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303145 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303146 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303147 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303148 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303149 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303150 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003151
3152#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3153 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303154 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3155 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3156 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3157 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3158 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3159 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3160 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3161 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3162 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3163 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003164 }
3165
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003166 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303167 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003168}
3169
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003170/*
3171 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3172 */
3173#define MAX_ATIDS 8192U
3174
3175/*
3176 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003177 *
3178 * If the firmware we're dealing with has Configuration File support, then
3179 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003180 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003181
3182/*
3183 * Tweak configuration based on module parameters, etc. Most of these have
3184 * defaults assigned to them by Firmware Configuration Files (if we're using
3185 * them) but need to be explicitly set if we're using hard-coded
3186 * initialization. But even in the case of using Firmware Configuration
3187 * Files, we'd like to expose the ability to change these via module
3188 * parameters so these are essentially common tweaks/settings for
3189 * Configuration Files and hard-coded initialization ...
3190 */
3191static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003192{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003193 /*
3194 * Fix up various Host-Dependent Parameters like Page Size, Cache
3195 * Line Size, etc. The firmware default is for a 4KB Page Size and
3196 * 64B Cache Line Size ...
3197 */
3198 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003199
Vipul Pandya636f9d32012-09-26 02:39:39 +00003200 /*
3201 * Process module parameters which affect early initialization.
3202 */
3203 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3204 dev_err(&adapter->pdev->dev,
3205 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3206 rx_dma_offset);
3207 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003208 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303209 t4_set_reg_field(adapter, SGE_CONTROL_A,
3210 PKTSHIFT_V(PKTSHIFT_M),
3211 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003212
Vipul Pandya636f9d32012-09-26 02:39:39 +00003213 /*
3214 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3215 * adds the pseudo header itself.
3216 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303217 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3218 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003219
3220 return 0;
3221}
3222
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303223/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3224 * unto themselves and they contain their own firmware to perform their
3225 * tasks ...
3226 */
3227static int phy_aq1202_version(const u8 *phy_fw_data,
3228 size_t phy_fw_size)
3229{
3230 int offset;
3231
3232 /* At offset 0x8 you're looking for the primary image's
3233 * starting offset which is 3 Bytes wide
3234 *
3235 * At offset 0xa of the primary image, you look for the offset
3236 * of the DRAM segment which is 3 Bytes wide.
3237 *
3238 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3239 * wide
3240 */
3241 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3242 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3243 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3244
3245 offset = le24(phy_fw_data + 0x8) << 12;
3246 offset = le24(phy_fw_data + offset + 0xa);
3247 return be16(phy_fw_data + offset + 0x27e);
3248
3249 #undef be16
3250 #undef le16
3251 #undef le24
3252}
3253
3254static struct info_10gbt_phy_fw {
3255 unsigned int phy_fw_id; /* PCI Device ID */
3256 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3257 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3258 int phy_flash; /* Has FLASH for PHY Firmware */
3259} phy_info_array[] = {
3260 {
3261 PHY_AQ1202_DEVICEID,
3262 PHY_AQ1202_FIRMWARE,
3263 phy_aq1202_version,
3264 1,
3265 },
3266 {
3267 PHY_BCM84834_DEVICEID,
3268 PHY_BCM84834_FIRMWARE,
3269 NULL,
3270 0,
3271 },
3272 { 0, NULL, NULL },
3273};
3274
3275static struct info_10gbt_phy_fw *find_phy_info(int devid)
3276{
3277 int i;
3278
3279 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3280 if (phy_info_array[i].phy_fw_id == devid)
3281 return &phy_info_array[i];
3282 }
3283 return NULL;
3284}
3285
3286/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3287 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3288 * we return a negative error number. If we transfer new firmware we return 1
3289 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3290 */
3291static int adap_init0_phy(struct adapter *adap)
3292{
3293 const struct firmware *phyf;
3294 int ret;
3295 struct info_10gbt_phy_fw *phy_info;
3296
3297 /* Use the device ID to determine which PHY file to flash.
3298 */
3299 phy_info = find_phy_info(adap->pdev->device);
3300 if (!phy_info) {
3301 dev_warn(adap->pdev_dev,
3302 "No PHY Firmware file found for this PHY\n");
3303 return -EOPNOTSUPP;
3304 }
3305
3306 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3307 * use that. The adapter firmware provides us with a memory buffer
3308 * where we can load a PHY firmware file from the host if we want to
3309 * override the PHY firmware File in flash.
3310 */
3311 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3312 adap->pdev_dev);
3313 if (ret < 0) {
3314 /* For adapters without FLASH attached to PHY for their
3315 * firmware, it's obviously a fatal error if we can't get the
3316 * firmware to the adapter. For adapters with PHY firmware
3317 * FLASH storage, it's worth a warning if we can't find the
3318 * PHY Firmware but we'll neuter the error ...
3319 */
3320 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3321 "/lib/firmware/%s, error %d\n",
3322 phy_info->phy_fw_file, -ret);
3323 if (phy_info->phy_flash) {
3324 int cur_phy_fw_ver = 0;
3325
3326 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3327 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3328 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3329 ret = 0;
3330 }
3331
3332 return ret;
3333 }
3334
3335 /* Load PHY Firmware onto adapter.
3336 */
3337 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3338 phy_info->phy_fw_version,
3339 (u8 *)phyf->data, phyf->size);
3340 if (ret < 0)
3341 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3342 -ret);
3343 else if (ret > 0) {
3344 int new_phy_fw_ver = 0;
3345
3346 if (phy_info->phy_fw_version)
3347 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3348 phyf->size);
3349 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3350 "Firmware /lib/firmware/%s, version %#x\n",
3351 phy_info->phy_fw_file, new_phy_fw_ver);
3352 }
3353
3354 release_firmware(phyf);
3355
3356 return ret;
3357}
3358
Vipul Pandya636f9d32012-09-26 02:39:39 +00003359/*
3360 * Attempt to initialize the adapter via a Firmware Configuration File.
3361 */
3362static int adap_init0_config(struct adapter *adapter, int reset)
3363{
3364 struct fw_caps_config_cmd caps_cmd;
3365 const struct firmware *cf;
3366 unsigned long mtype = 0, maddr = 0;
3367 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303368 int ret;
3369 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003370 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303371 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003372
3373 /*
3374 * Reset device if necessary.
3375 */
3376 if (reset) {
3377 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303378 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003379 if (ret < 0)
3380 goto bye;
3381 }
3382
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303383 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3384 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3385 * to be performed after any global adapter RESET above since some
3386 * PHYs only have local RAM copies of the PHY firmware.
3387 */
3388 if (is_10gbt_device(adapter->pdev->device)) {
3389 ret = adap_init0_phy(adapter);
3390 if (ret < 0)
3391 goto bye;
3392 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003393 /*
3394 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3395 * then use that. Otherwise, use the configuration file stored
3396 * in the adapter flash ...
3397 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303398 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003399 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303400 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003401 break;
3402 case CHELSIO_T5:
3403 fw_config_file = FW5_CFNAME;
3404 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303405 case CHELSIO_T6:
3406 fw_config_file = FW6_CFNAME;
3407 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003408 default:
3409 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3410 adapter->pdev->device);
3411 ret = -EINVAL;
3412 goto bye;
3413 }
3414
3415 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003416 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303417 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003418 mtype = FW_MEMTYPE_CF_FLASH;
3419 maddr = t4_flash_cfg_addr(adapter);
3420 } else {
3421 u32 params[7], val[7];
3422
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303423 sprintf(fw_config_file_path,
3424 "/lib/firmware/%s", fw_config_file);
3425 config_name = fw_config_file_path;
3426
Vipul Pandya636f9d32012-09-26 02:39:39 +00003427 if (cf->size >= FLASH_CFG_MAX_SIZE)
3428 ret = -ENOMEM;
3429 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303430 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3431 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003432 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303433 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003434 if (ret == 0) {
3435 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303436 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003437 * sizes have to be in terms of multiples of 4
3438 * bytes. So, if the Configuration File isn't
3439 * a multiple of 4 bytes in length we'll have
3440 * to write that out separately since we can't
3441 * guarantee that the bytes following the
3442 * residual byte in the buffer returned by
3443 * request_firmware() are zeroed out ...
3444 */
3445 size_t resid = cf->size & 0x3;
3446 size_t size = cf->size & ~0x3;
3447 __be32 *data = (__be32 *)cf->data;
3448
Hariprasad Shenai51678652014-11-21 12:52:02 +05303449 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3450 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003451
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303452 spin_lock(&adapter->win0_lock);
3453 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3454 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003455 if (ret == 0 && resid != 0) {
3456 union {
3457 __be32 word;
3458 char buf[4];
3459 } last;
3460 int i;
3461
3462 last.word = data[size >> 2];
3463 for (i = resid; i < 4; i++)
3464 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303465 ret = t4_memory_rw(adapter, 0, mtype,
3466 maddr + size,
3467 4, &last.word,
3468 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003469 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303470 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003471 }
3472 }
3473
3474 release_firmware(cf);
3475 if (ret)
3476 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003477 }
3478
Vipul Pandya636f9d32012-09-26 02:39:39 +00003479 /*
3480 * Issue a Capability Configuration command to the firmware to get it
3481 * to parse the Configuration File. We don't use t4_fw_config_file()
3482 * because we want the ability to modify various features after we've
3483 * processed the configuration file ...
3484 */
3485 memset(&caps_cmd, 0, sizeof(caps_cmd));
3486 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303487 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3488 FW_CMD_REQUEST_F |
3489 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303490 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303491 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3492 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3493 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003494 FW_LEN16(caps_cmd));
3495 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3496 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303497
3498 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3499 * Configuration File in FLASH), our last gasp effort is to use the
3500 * Firmware Configuration File which is embedded in the firmware. A
3501 * very few early versions of the firmware didn't have one embedded
3502 * but we can ignore those.
3503 */
3504 if (ret == -ENOENT) {
3505 memset(&caps_cmd, 0, sizeof(caps_cmd));
3506 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303507 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3508 FW_CMD_REQUEST_F |
3509 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303510 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3511 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3512 sizeof(caps_cmd), &caps_cmd);
3513 config_name = "Firmware Default";
3514 }
3515
3516 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003517 if (ret < 0)
3518 goto bye;
3519
Vipul Pandya636f9d32012-09-26 02:39:39 +00003520 finiver = ntohl(caps_cmd.finiver);
3521 finicsum = ntohl(caps_cmd.finicsum);
3522 cfcsum = ntohl(caps_cmd.cfcsum);
3523 if (finicsum != cfcsum)
3524 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3525 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3526 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003527
Vipul Pandya636f9d32012-09-26 02:39:39 +00003528 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003529 * And now tell the firmware to use the configuration we just loaded.
3530 */
3531 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303532 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3533 FW_CMD_REQUEST_F |
3534 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303535 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003536 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3537 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003538 if (ret < 0)
3539 goto bye;
3540
Vipul Pandya636f9d32012-09-26 02:39:39 +00003541 /*
3542 * Tweak configuration based on system architecture, module
3543 * parameters, etc.
3544 */
3545 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003546 if (ret < 0)
3547 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003548
Vipul Pandya636f9d32012-09-26 02:39:39 +00003549 /*
3550 * And finally tell the firmware to initialize itself using the
3551 * parameters from the Configuration File.
3552 */
3553 ret = t4_fw_initialize(adapter, adapter->mbox);
3554 if (ret < 0)
3555 goto bye;
3556
Hariprasad Shenai06640312015-01-13 15:19:25 +05303557 /* Emit Firmware Configuration File information and return
3558 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003559 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003560 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303561 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3562 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003563 return 0;
3564
3565 /*
3566 * Something bad happened. Return the error ... (If the "error"
3567 * is that there's no Configuration File on the adapter we don't
3568 * want to issue a warning since this is fairly common.)
3569 */
3570bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303571 if (config_issued && ret != -ENOENT)
3572 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3573 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003574 return ret;
3575}
3576
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303577static struct fw_info fw_info_array[] = {
3578 {
3579 .chip = CHELSIO_T4,
3580 .fs_name = FW4_CFNAME,
3581 .fw_mod_name = FW4_FNAME,
3582 .fw_hdr = {
3583 .chip = FW_HDR_CHIP_T4,
3584 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3585 .intfver_nic = FW_INTFVER(T4, NIC),
3586 .intfver_vnic = FW_INTFVER(T4, VNIC),
3587 .intfver_ri = FW_INTFVER(T4, RI),
3588 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3589 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3590 },
3591 }, {
3592 .chip = CHELSIO_T5,
3593 .fs_name = FW5_CFNAME,
3594 .fw_mod_name = FW5_FNAME,
3595 .fw_hdr = {
3596 .chip = FW_HDR_CHIP_T5,
3597 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3598 .intfver_nic = FW_INTFVER(T5, NIC),
3599 .intfver_vnic = FW_INTFVER(T5, VNIC),
3600 .intfver_ri = FW_INTFVER(T5, RI),
3601 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3602 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3603 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303604 }, {
3605 .chip = CHELSIO_T6,
3606 .fs_name = FW6_CFNAME,
3607 .fw_mod_name = FW6_FNAME,
3608 .fw_hdr = {
3609 .chip = FW_HDR_CHIP_T6,
3610 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3611 .intfver_nic = FW_INTFVER(T6, NIC),
3612 .intfver_vnic = FW_INTFVER(T6, VNIC),
3613 .intfver_ofld = FW_INTFVER(T6, OFLD),
3614 .intfver_ri = FW_INTFVER(T6, RI),
3615 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3616 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3617 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3618 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3619 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303620 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303621
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303622};
3623
3624static struct fw_info *find_fw_info(int chip)
3625{
3626 int i;
3627
3628 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3629 if (fw_info_array[i].chip == chip)
3630 return &fw_info_array[i];
3631 }
3632 return NULL;
3633}
3634
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003635/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003636 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003637 */
3638static int adap_init0(struct adapter *adap)
3639{
3640 int ret;
3641 u32 v, port_vec;
3642 enum dev_state state;
3643 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003644 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303645 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003646
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303647 /* Grab Firmware Device Log parameters as early as possible so we have
3648 * access to it for debugging, etc.
3649 */
3650 ret = t4_init_devlog_params(adap);
3651 if (ret < 0)
3652 return ret;
3653
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303654 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303655 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3656 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003657 if (ret < 0) {
3658 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3659 ret);
3660 return ret;
3661 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003662 if (ret == adap->mbox)
3663 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003664
Vipul Pandya636f9d32012-09-26 02:39:39 +00003665 /*
3666 * If we're the Master PF Driver and the device is uninitialized,
3667 * then let's consider upgrading the firmware ... (We always want
3668 * to check the firmware version number in order to A. get it for
3669 * later reporting and B. to warn if the currently loaded firmware
3670 * is excessively mismatched relative to the driver.)
3671 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303672
Ganesh Goudar760446f2017-07-20 18:28:48 +05303673 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303674 ret = t4_check_fw_version(adap);
3675 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303676 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303677 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003678 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303679 struct fw_info *fw_info;
3680 struct fw_hdr *card_fw;
3681 const struct firmware *fw;
3682 const u8 *fw_data = NULL;
3683 unsigned int fw_size = 0;
3684
3685 /* This is the firmware whose headers the driver was compiled
3686 * against
3687 */
3688 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3689 if (fw_info == NULL) {
3690 dev_err(adap->pdev_dev,
3691 "unable to get firmware info for chip %d.\n",
3692 CHELSIO_CHIP_VERSION(adap->params.chip));
3693 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003694 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303695
3696 /* allocate memory to read the header of the firmware on the
3697 * card
3698 */
Michal Hocko752ade62017-05-08 15:57:27 -07003699 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303700
3701 /* Get FW from from /lib/firmware/ */
3702 ret = request_firmware(&fw, fw_info->fw_mod_name,
3703 adap->pdev_dev);
3704 if (ret < 0) {
3705 dev_err(adap->pdev_dev,
3706 "unable to load firmware image %s, error %d\n",
3707 fw_info->fw_mod_name, ret);
3708 } else {
3709 fw_data = fw->data;
3710 fw_size = fw->size;
3711 }
3712
3713 /* upgrade FW logic */
3714 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3715 state, &reset);
3716
3717 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003718 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003719 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303720
Vipul Pandya636f9d32012-09-26 02:39:39 +00003721 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303722 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003723 }
3724
3725 /*
3726 * Grab VPD parameters. This should be done after we establish a
3727 * connection to the firmware since some of the VPD parameters
3728 * (notably the Core Clock frequency) are retrieved via requests to
3729 * the firmware. On the other hand, we need these fairly early on
3730 * so we do this right after getting ahold of the firmware.
3731 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303732 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003733 if (ret < 0)
3734 goto bye;
3735
Vipul Pandya636f9d32012-09-26 02:39:39 +00003736 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003737 * Find out what ports are available to us. Note that we need to do
3738 * this before calling adap_init0_no_config() since it needs nports
3739 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003740 */
3741 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303742 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3743 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303744 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003745 if (ret < 0)
3746 goto bye;
3747
3748 adap->params.nports = hweight32(port_vec);
3749 adap->params.portvec = port_vec;
3750
Hariprasad Shenai06640312015-01-13 15:19:25 +05303751 /* If the firmware is initialized already, emit a simply note to that
3752 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003753 */
3754 if (state == DEV_STATE_INIT) {
3755 dev_info(adap->pdev_dev, "Coming up as %s: "\
3756 "Adapter already initialized\n",
3757 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003758 } else {
3759 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3760 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303761
3762 /* Find out whether we're dealing with a version of the
3763 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003764 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303765 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3766 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303767 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303768 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003769
Hariprasad Shenai06640312015-01-13 15:19:25 +05303770 /* If the firmware doesn't support Configuration Files,
3771 * return an error.
3772 */
3773 if (ret < 0) {
3774 dev_err(adap->pdev_dev, "firmware doesn't support "
3775 "Firmware Configuration Files\n");
3776 goto bye;
3777 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003778
Hariprasad Shenai06640312015-01-13 15:19:25 +05303779 /* The firmware provides us with a memory buffer where we can
3780 * load a Configuration File from the host if we want to
3781 * override the Configuration File in flash.
3782 */
3783 ret = adap_init0_config(adap, reset);
3784 if (ret == -ENOENT) {
3785 dev_err(adap->pdev_dev, "no Configuration File "
3786 "present on adapter.\n");
3787 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003788 }
3789 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303790 dev_err(adap->pdev_dev, "could not initialize "
3791 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003792 goto bye;
3793 }
3794 }
3795
Hariprasad Shenai06640312015-01-13 15:19:25 +05303796 /* Give the SGE code a chance to pull in anything that it needs ...
3797 * Note that this must be called after we retrieve our VPD parameters
3798 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003799 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303800 ret = t4_sge_init(adap);
3801 if (ret < 0)
3802 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003803
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003804 if (is_bypass_device(adap->pdev->device))
3805 adap->params.bypass = 1;
3806
Vipul Pandya636f9d32012-09-26 02:39:39 +00003807 /*
3808 * Grab some of our basic fundamental operating parameters.
3809 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003810#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303811 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3812 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003813
3814#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303815 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3816 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3817 FW_PARAMS_PARAM_Y_V(0) | \
3818 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003819
Vipul Pandya636f9d32012-09-26 02:39:39 +00003820 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003821 params[1] = FW_PARAM_PFVF(L2T_START);
3822 params[2] = FW_PARAM_PFVF(L2T_END);
3823 params[3] = FW_PARAM_PFVF(FILTER_START);
3824 params[4] = FW_PARAM_PFVF(FILTER_END);
3825 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303826 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003827 if (ret < 0)
3828 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003829 adap->sge.egr_start = val[0];
3830 adap->l2t_start = val[1];
3831 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003832 adap->tids.ftid_base = val[3];
3833 adap->tids.nftids = val[4] - val[3] + 1;
3834 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003835
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303836 /* qids (ingress/egress) returned from firmware can be anywhere
3837 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3838 * Hence driver needs to allocate memory for this range to
3839 * store the queue info. Get the highest IQFLINT/EQ index returned
3840 * in FW_EQ_*_CMD.alloc command.
3841 */
3842 params[0] = FW_PARAM_PFVF(EQ_END);
3843 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303844 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303845 if (ret < 0)
3846 goto bye;
3847 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3848 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3849
3850 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3851 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3852 if (!adap->sge.egr_map) {
3853 ret = -ENOMEM;
3854 goto bye;
3855 }
3856
3857 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3858 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3859 if (!adap->sge.ingr_map) {
3860 ret = -ENOMEM;
3861 goto bye;
3862 }
3863
3864 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303865 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303866 */
3867 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3868 sizeof(long), GFP_KERNEL);
3869 if (!adap->sge.starving_fl) {
3870 ret = -ENOMEM;
3871 goto bye;
3872 }
3873
3874 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3875 sizeof(long), GFP_KERNEL);
3876 if (!adap->sge.txq_maperr) {
3877 ret = -ENOMEM;
3878 goto bye;
3879 }
3880
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303881#ifdef CONFIG_DEBUG_FS
3882 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3883 sizeof(long), GFP_KERNEL);
3884 if (!adap->sge.blocked_fl) {
3885 ret = -ENOMEM;
3886 goto bye;
3887 }
3888#endif
3889
Anish Bhattb5a02f52015-01-14 15:17:34 -08003890 params[0] = FW_PARAM_PFVF(CLIP_START);
3891 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303892 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003893 if (ret < 0)
3894 goto bye;
3895 adap->clipt_start = val[0];
3896 adap->clipt_end = val[1];
3897
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303898 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3899 * Classes supported by the hardware/firmware so we hard code it here
3900 * for now.
3901 */
3902 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3903
Vipul Pandya636f9d32012-09-26 02:39:39 +00003904 /* query params related to active filter region */
3905 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3906 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303907 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003908 /* If Active filter size is set we enable establishing
3909 * offload connection through firmware work request
3910 */
3911 if ((val[0] != val[1]) && (ret >= 0)) {
3912 adap->flags |= FW_OFLD_CONN;
3913 adap->tids.aftid_base = val[0];
3914 adap->tids.aftid_end = val[1];
3915 }
3916
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003917 /* If we're running on newer firmware, let it know that we're
3918 * prepared to deal with encapsulated CPL messages. Older
3919 * firmware won't understand this and we'll just get
3920 * unencapsulated messages ...
3921 */
3922 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3923 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303924 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003925
Vipul Pandya636f9d32012-09-26 02:39:39 +00003926 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303927 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3928 * capability. Earlier versions of the firmware didn't have the
3929 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3930 * permission to use ULPTX MEMWRITE DSGL.
3931 */
3932 if (is_t4(adap->params.chip)) {
3933 adap->params.ulptx_memwrite_dsgl = false;
3934 } else {
3935 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303936 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303937 1, params, val);
3938 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3939 }
3940
Steve Wise086de572016-09-16 07:54:49 -07003941 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3942 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3943 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3944 1, params, val);
3945 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3946
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05303947 /* See if FW supports FW_FILTER2 work request */
3948 if (is_t4(adap->params.chip)) {
3949 adap->params.filter2_wr_support = 0;
3950 } else {
3951 params[0] = FW_PARAM_DEV(FILTER2_WR);
3952 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3953 1, params, val);
3954 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
3955 }
3956
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303957 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003958 * Get device capabilities so we can determine what resources we need
3959 * to manage.
3960 */
3961 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303962 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3963 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303964 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003965 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3966 &caps_cmd);
3967 if (ret < 0)
3968 goto bye;
3969
Kumar Sanghvi5c312542017-11-01 08:53:00 +05303970 if (caps_cmd.ofldcaps ||
3971 (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER))) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003972 /* query offload-related parameters */
3973 params[0] = FW_PARAM_DEV(NTID);
3974 params[1] = FW_PARAM_PFVF(SERVER_START);
3975 params[2] = FW_PARAM_PFVF(SERVER_END);
3976 params[3] = FW_PARAM_PFVF(TDDP_START);
3977 params[4] = FW_PARAM_PFVF(TDDP_END);
3978 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303979 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003980 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003981 if (ret < 0)
3982 goto bye;
3983 adap->tids.ntids = val[0];
3984 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3985 adap->tids.stid_base = val[1];
3986 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003987 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003988 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003989 * region into two parts. Regular filters get 1/3rd and server
3990 * filters get 2/3rd part. This is only enabled if workarond
3991 * path is enabled.
3992 * 1. For regular filters.
3993 * 2. Server filter: This are special filters which are used
3994 * to redirect SYN packets to offload queue.
3995 */
3996 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3997 adap->tids.sftid_base = adap->tids.ftid_base +
3998 DIV_ROUND_UP(adap->tids.nftids, 3);
3999 adap->tids.nsftids = adap->tids.nftids -
4000 DIV_ROUND_UP(adap->tids.nftids, 3);
4001 adap->tids.nftids = adap->tids.sftid_base -
4002 adap->tids.ftid_base;
4003 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004004 adap->vres.ddp.start = val[3];
4005 adap->vres.ddp.size = val[4] - val[3] + 1;
4006 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004007
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304008 if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4009 if (init_hash_filter(adap) < 0)
4010 goto bye;
4011 } else {
4012 adap->params.offload = 1;
4013 adap->num_ofld_uld += 1;
4014 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004015 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004016 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004017 params[0] = FW_PARAM_PFVF(STAG_START);
4018 params[1] = FW_PARAM_PFVF(STAG_END);
4019 params[2] = FW_PARAM_PFVF(RQ_START);
4020 params[3] = FW_PARAM_PFVF(RQ_END);
4021 params[4] = FW_PARAM_PFVF(PBL_START);
4022 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304023 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004024 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004025 if (ret < 0)
4026 goto bye;
4027 adap->vres.stag.start = val[0];
4028 adap->vres.stag.size = val[1] - val[0] + 1;
4029 adap->vres.rq.start = val[2];
4030 adap->vres.rq.size = val[3] - val[2] + 1;
4031 adap->vres.pbl.start = val[4];
4032 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004033
4034 params[0] = FW_PARAM_PFVF(SQRQ_START);
4035 params[1] = FW_PARAM_PFVF(SQRQ_END);
4036 params[2] = FW_PARAM_PFVF(CQ_START);
4037 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004038 params[4] = FW_PARAM_PFVF(OCQ_START);
4039 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304040 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304041 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004042 if (ret < 0)
4043 goto bye;
4044 adap->vres.qp.start = val[0];
4045 adap->vres.qp.size = val[1] - val[0] + 1;
4046 adap->vres.cq.start = val[2];
4047 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004048 adap->vres.ocq.start = val[4];
4049 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304050
4051 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4052 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304053 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304054 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304055 if (ret < 0) {
4056 adap->params.max_ordird_qp = 8;
4057 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4058 ret = 0;
4059 } else {
4060 adap->params.max_ordird_qp = val[0];
4061 adap->params.max_ird_adapter = val[1];
4062 }
4063 dev_info(adap->pdev_dev,
4064 "max_ordird_qp %d max_ird_adapter %d\n",
4065 adap->params.max_ordird_qp,
4066 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304067 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004068 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004069 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004070 params[0] = FW_PARAM_PFVF(ISCSI_START);
4071 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304072 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004073 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004074 if (ret < 0)
4075 goto bye;
4076 adap->vres.iscsi.start = val[0];
4077 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304078 /* LIO target and cxgb4i initiaitor */
4079 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004080 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304081 if (caps_cmd.cryptocaps) {
4082 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304083 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4084 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4085 params, val);
4086 if (ret < 0) {
4087 if (ret != -EINVAL)
4088 goto bye;
4089 } else {
4090 adap->vres.ncrypto_fc = val[0];
4091 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304092 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4093 adap->num_uld += 1;
4094 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004095#undef FW_PARAM_PFVF
4096#undef FW_PARAM_DEV
4097
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304098 /* The MTU/MSS Table is initialized by now, so load their values. If
4099 * we're initializing the adapter, then we'll make any modifications
4100 * we want to the MTU/MSS Table and also initialize the congestion
4101 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004102 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004103 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304104 if (state != DEV_STATE_INIT) {
4105 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004106
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304107 /* The default MTU Table contains values 1492 and 1500.
4108 * However, for TCP, it's better to have two values which are
4109 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4110 * This allows us to have a TCP Data Payload which is a
4111 * multiple of 8 regardless of what combination of TCP Options
4112 * are in use (always a multiple of 4 bytes) which is
4113 * important for performance reasons. For instance, if no
4114 * options are in use, then we have a 20-byte IP header and a
4115 * 20-byte TCP header. In this case, a 1500-byte MSS would
4116 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4117 * which is not a multiple of 8. So using an MSS of 1488 in
4118 * this case results in a TCP Data Payload of 1448 bytes which
4119 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4120 * Stamps have been negotiated, then an MTU of 1500 bytes
4121 * results in a TCP Data Payload of 1448 bytes which, as
4122 * above, is a multiple of 8 bytes ...
4123 */
4124 for (i = 0; i < NMTUS; i++)
4125 if (adap->params.mtus[i] == 1492) {
4126 adap->params.mtus[i] = 1488;
4127 break;
4128 }
4129
4130 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4131 adap->params.b_wnd);
4132 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304133 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004134 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304135 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004136 return 0;
4137
4138 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004139 * Something bad happened. If a command timed out or failed with EIO
4140 * FW does not operate within its spec or something catastrophic
4141 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004142 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004143bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304144 kfree(adap->sge.egr_map);
4145 kfree(adap->sge.ingr_map);
4146 kfree(adap->sge.starving_fl);
4147 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304148#ifdef CONFIG_DEBUG_FS
4149 kfree(adap->sge.blocked_fl);
4150#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004151 if (ret != -ETIMEDOUT && ret != -EIO)
4152 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004153 return ret;
4154}
4155
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004156/* EEH callbacks */
4157
4158static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4159 pci_channel_state_t state)
4160{
4161 int i;
4162 struct adapter *adap = pci_get_drvdata(pdev);
4163
4164 if (!adap)
4165 goto out;
4166
4167 rtnl_lock();
4168 adap->flags &= ~FW_OK;
4169 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004170 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004171 for_each_port(adap, i) {
4172 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004173 if (dev) {
4174 netif_device_detach(dev);
4175 netif_carrier_off(dev);
4176 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004177 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004178 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304179 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004180 if (adap->flags & FULL_INIT_DONE)
4181 cxgb_down(adap);
4182 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004183 if ((adap->flags & DEV_ENABLED)) {
4184 pci_disable_device(pdev);
4185 adap->flags &= ~DEV_ENABLED;
4186 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004187out: return state == pci_channel_io_perm_failure ?
4188 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4189}
4190
4191static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4192{
4193 int i, ret;
4194 struct fw_caps_config_cmd c;
4195 struct adapter *adap = pci_get_drvdata(pdev);
4196
4197 if (!adap) {
4198 pci_restore_state(pdev);
4199 pci_save_state(pdev);
4200 return PCI_ERS_RESULT_RECOVERED;
4201 }
4202
Gavin Shan144be3d2014-01-23 12:27:34 +08004203 if (!(adap->flags & DEV_ENABLED)) {
4204 if (pci_enable_device(pdev)) {
4205 dev_err(&pdev->dev, "Cannot reenable PCI "
4206 "device after reset\n");
4207 return PCI_ERS_RESULT_DISCONNECT;
4208 }
4209 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004210 }
4211
4212 pci_set_master(pdev);
4213 pci_restore_state(pdev);
4214 pci_save_state(pdev);
4215 pci_cleanup_aer_uncorrect_error_status(pdev);
4216
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304217 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004218 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304219 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004220 return PCI_ERS_RESULT_DISCONNECT;
4221 adap->flags |= FW_OK;
4222 if (adap_init1(adap, &c))
4223 return PCI_ERS_RESULT_DISCONNECT;
4224
4225 for_each_port(adap, i) {
4226 struct port_info *p = adap2pinfo(adap, i);
4227
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304228 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004229 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004230 if (ret < 0)
4231 return PCI_ERS_RESULT_DISCONNECT;
4232 p->viid = ret;
4233 p->xact_addr_filt = -1;
4234 }
4235
4236 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4237 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004238 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004239 if (cxgb_up(adap))
4240 return PCI_ERS_RESULT_DISCONNECT;
4241 return PCI_ERS_RESULT_RECOVERED;
4242}
4243
4244static void eeh_resume(struct pci_dev *pdev)
4245{
4246 int i;
4247 struct adapter *adap = pci_get_drvdata(pdev);
4248
4249 if (!adap)
4250 return;
4251
4252 rtnl_lock();
4253 for_each_port(adap, i) {
4254 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004255 if (dev) {
4256 if (netif_running(dev)) {
4257 link_start(dev);
4258 cxgb_set_rxmode(dev);
4259 }
4260 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004261 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004262 }
4263 rtnl_unlock();
4264}
4265
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004266static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004267 .error_detected = eeh_err_detected,
4268 .slot_reset = eeh_slot_reset,
4269 .resume = eeh_resume,
4270};
4271
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304272/* Return true if the Link Configuration supports "High Speeds" (those greater
4273 * than 1Gb/s).
4274 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304275static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004276{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304277 unsigned int speeds, high_speeds;
4278
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304279 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4280 high_speeds = speeds &
4281 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304282
4283 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004284}
4285
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004286/*
4287 * Perform default configuration of DMA queues depending on the number and type
4288 * of ports we found and the number of available CPUs. Most settings can be
4289 * modified by the admin prior to actual use.
4290 */
Bill Pemberton91744942012-12-03 09:23:02 -05004291static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004292{
4293 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304294 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004295#ifndef CONFIG_CHELSIO_T4_DCB
4296 int q10g = 0;
4297#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004298
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304299 /* Reduce memory usage in kdump environment, disable all offload.
4300 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304301 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304302 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304303 adap->params.crypto = 0;
4304 }
4305
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304306 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004307#ifdef CONFIG_CHELSIO_T4_DCB
4308 /* For Data Center Bridging support we need to be able to support up
4309 * to 8 Traffic Priorities; each of which will be assigned to its
4310 * own TX Queue in order to prevent Head-Of-Line Blocking.
4311 */
4312 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4313 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4314 MAX_ETH_QSETS, adap->params.nports * 8);
4315 BUG_ON(1);
4316 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004317
Anish Bhatt688848b2014-06-19 21:37:13 -07004318 for_each_port(adap, i) {
4319 struct port_info *pi = adap2pinfo(adap, i);
4320
4321 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304322 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004323 qidx += pi->nqsets;
4324 }
4325#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004326 /*
4327 * We default to 1 queue per non-10G port and up to # of cores queues
4328 * per 10G port.
4329 */
4330 if (n10g)
4331 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004332 if (q10g > netif_get_num_default_rss_queues())
4333 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004334
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304335 if (is_kdump_kernel())
4336 q10g = 1;
4337
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004338 for_each_port(adap, i) {
4339 struct port_info *pi = adap2pinfo(adap, i);
4340
4341 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304342 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004343 qidx += pi->nqsets;
4344 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004345#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004346
4347 s->ethqsets = qidx;
4348 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4349
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304350 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004351 /*
4352 * For offload we use 1 queue/channel if all ports are up to 1G,
4353 * otherwise we divide all available queues amongst the channels
4354 * capped by the number of available cores.
4355 */
4356 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304357 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304358 s->ofldqsets = roundup(i, adap->params.nports);
4359 } else {
4360 s->ofldqsets = adap->params.nports;
4361 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004362 }
4363
4364 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4365 struct sge_eth_rxq *r = &s->ethrxq[i];
4366
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304367 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004368 r->fl.size = 72;
4369 }
4370
4371 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4372 s->ethtxq[i].q.size = 1024;
4373
4374 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4375 s->ctrlq[i].q.size = 512;
4376
Atul Guptaa45695042017-07-04 16:46:20 +05304377 if (!is_t4(adap->params.chip))
4378 s->ptptxq.q.size = 8;
4379
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304380 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304381 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004382}
4383
4384/*
4385 * Reduce the number of Ethernet queues across all ports to at most n.
4386 * n provides at least one queue per port.
4387 */
Bill Pemberton91744942012-12-03 09:23:02 -05004388static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004389{
4390 int i;
4391 struct port_info *pi;
4392
4393 while (n < adap->sge.ethqsets)
4394 for_each_port(adap, i) {
4395 pi = adap2pinfo(adap, i);
4396 if (pi->nqsets > 1) {
4397 pi->nqsets--;
4398 adap->sge.ethqsets--;
4399 if (adap->sge.ethqsets <= n)
4400 break;
4401 }
4402 }
4403
4404 n = 0;
4405 for_each_port(adap, i) {
4406 pi = adap2pinfo(adap, i);
4407 pi->first_qset = n;
4408 n += pi->nqsets;
4409 }
4410}
4411
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304412static int get_msix_info(struct adapter *adap)
4413{
4414 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304415 unsigned int max_ingq = 0;
4416
4417 if (is_offload(adap))
4418 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4419 if (is_pci_uld(adap))
4420 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4421
4422 if (!max_ingq)
4423 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304424
4425 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4426 if (!msix_info)
4427 return -ENOMEM;
4428
4429 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4430 sizeof(long), GFP_KERNEL);
4431 if (!adap->msix_bmap_ulds.msix_bmap) {
4432 kfree(msix_info);
4433 return -ENOMEM;
4434 }
4435 spin_lock_init(&adap->msix_bmap_ulds.lock);
4436 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304437out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304438 return 0;
4439}
4440
4441static void free_msix_info(struct adapter *adap)
4442{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304443 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304444 return;
4445
4446 kfree(adap->msix_info_ulds);
4447 kfree(adap->msix_bmap_ulds.msix_bmap);
4448}
4449
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004450/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4451#define EXTRA_VECS 2
4452
Bill Pemberton91744942012-12-03 09:23:02 -05004453static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004454{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304455 int ofld_need = 0, uld_need = 0;
4456 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004457 struct sge *s = &adap->sge;
4458 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304459 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304460 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004461
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304462 if (is_pci_uld(adap))
4463 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4464 if (is_offload(adap))
4465 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304466 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304467 GFP_KERNEL);
4468 if (!entries)
4469 return -ENOMEM;
4470
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304471 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304472 if (get_msix_info(adap)) {
4473 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304474 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304475 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304476
4477 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004478 entries[i].entry = i;
4479
4480 want = s->max_ethqsets + EXTRA_VECS;
4481 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304482 want += adap->num_ofld_uld * s->ofldqsets;
4483 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004484 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304485 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304486 want += adap->num_uld * s->ofldqsets;
4487 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304488 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004489#ifdef CONFIG_CHELSIO_T4_DCB
4490 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4491 * each port.
4492 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304493 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004494#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304495 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004496#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304497 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4498 if (allocated < 0) {
4499 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4500 " not using MSI-X\n");
4501 kfree(entries);
4502 return allocated;
4503 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004504
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304505 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004506 * Every group gets its minimum requirement and NIC gets top
4507 * priority for leftovers.
4508 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304509 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004510 if (i < s->max_ethqsets) {
4511 s->max_ethqsets = i;
4512 if (i < s->ethqsets)
4513 reduce_ethqs(adap, i);
4514 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304515 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304516 if (allocated < want)
4517 s->nqs_per_uld = nchan;
4518 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304519 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304520 }
4521
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304522 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004523 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304524 if (is_uld(adap)) {
4525 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304526 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304527 adap->msix_info_ulds[j].idx = i;
4528 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304529 adap->msix_bmap_ulds.mapsize = j;
4530 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304531 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304532 "nic %d per uld %d\n",
4533 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004534
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304535 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004536 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004537}
4538
4539#undef EXTRA_VECS
4540
Bill Pemberton91744942012-12-03 09:23:02 -05004541static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004542{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304543 unsigned int i;
4544 int err;
4545
4546 err = t4_init_rss_mode(adap, adap->mbox);
4547 if (err)
4548 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004549
4550 for_each_port(adap, i) {
4551 struct port_info *pi = adap2pinfo(adap, i);
4552
4553 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4554 if (!pi->rss)
4555 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004556 }
4557 return 0;
4558}
4559
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304560static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4561 enum pci_bus_speed *speed,
4562 enum pcie_link_width *width)
4563{
4564 u32 lnkcap1, lnkcap2;
4565 int err1, err2;
4566
4567#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4568
4569 *speed = PCI_SPEED_UNKNOWN;
4570 *width = PCIE_LNK_WIDTH_UNKNOWN;
4571
4572 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4573 &lnkcap1);
4574 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4575 &lnkcap2);
4576 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4577 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4578 *speed = PCIE_SPEED_8_0GT;
4579 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4580 *speed = PCIE_SPEED_5_0GT;
4581 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4582 *speed = PCIE_SPEED_2_5GT;
4583 }
4584 if (!err1) {
4585 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4586 if (!lnkcap2) { /* pre-r3.0 */
4587 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4588 *speed = PCIE_SPEED_5_0GT;
4589 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4590 *speed = PCIE_SPEED_2_5GT;
4591 }
4592 }
4593
4594 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4595 return err1 ? err1 : err2 ? err2 : -EINVAL;
4596 return 0;
4597}
4598
4599static void cxgb4_check_pcie_caps(struct adapter *adap)
4600{
4601 enum pcie_link_width width, width_cap;
4602 enum pci_bus_speed speed, speed_cap;
4603
4604#define PCIE_SPEED_STR(speed) \
4605 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4606 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4607 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4608 "Unknown")
4609
4610 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4611 dev_warn(adap->pdev_dev,
4612 "Unable to determine PCIe device BW capabilities\n");
4613 return;
4614 }
4615
4616 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4617 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4618 dev_warn(adap->pdev_dev,
4619 "Unable to determine PCI Express bandwidth.\n");
4620 return;
4621 }
4622
4623 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4624 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4625 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4626 width, width_cap);
4627 if (speed < speed_cap || width < width_cap)
4628 dev_info(adap->pdev_dev,
4629 "A slot with more lanes and/or higher speed is "
4630 "suggested for optimal performance.\n");
4631}
4632
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304633/* Dump basic information about the adapter */
4634static void print_adapter_info(struct adapter *adapter)
4635{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304636 /* Hardware/Firmware/etc. Version/Revision IDs */
4637 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304638
4639 /* Software/Hardware configuration */
4640 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4641 is_offload(adapter) ? "R" : "",
4642 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4643 (adapter->flags & USING_MSI) ? "MSI" : ""),
4644 is_offload(adapter) ? "Offload" : "non-Offload");
4645}
4646
Bill Pemberton91744942012-12-03 09:23:02 -05004647static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004648{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004649 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004650 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004651 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004652 const struct port_info *pi = netdev_priv(dev);
4653 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004654
4655 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4656 spd = " 2.5 GT/s";
4657 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4658 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004659 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4660 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004661
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304662 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304663 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304664 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304665 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304666 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004667 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304668 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304669 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304670 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304671 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304672 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4673 bufp += sprintf(bufp, "50G/");
4674 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304675 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304676 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4677 bufp += sprintf(bufp, "200G/");
4678 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4679 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004680 if (bufp != buf)
4681 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304682 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004683
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304684 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4685 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004686}
4687
Dimitris Michailidis06546392010-07-11 12:01:16 +00004688/*
4689 * Free the following resources:
4690 * - memory used for tables
4691 * - MSI/MSI-X
4692 * - net devices
4693 * - resources FW is holding for us
4694 */
4695static void free_some_resources(struct adapter *adapter)
4696{
4697 unsigned int i;
4698
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05304699 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07004700 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304701 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004702 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304703 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304704 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304705 kfree(adapter->sge.egr_map);
4706 kfree(adapter->sge.ingr_map);
4707 kfree(adapter->sge.starving_fl);
4708 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304709#ifdef CONFIG_DEBUG_FS
4710 kfree(adapter->sge.blocked_fl);
4711#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004712 disable_msi(adapter);
4713
4714 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004715 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304716 struct port_info *pi = adap2pinfo(adapter, i);
4717
4718 if (pi->viid != 0)
4719 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4720 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004721 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004722 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004723 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004724 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304725 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004726}
4727
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004728#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004729#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004730 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004731#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004732
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304733static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4734{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304735 u16 device_id;
4736
4737 /* Retrieve adapter's device ID */
4738 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004739
4740 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304741 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004742 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304743 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004744 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304745 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004746 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304747 default:
4748 dev_err(&pdev->dev, "Device %d is not supported\n",
4749 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304750 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004751 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304752}
4753
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304754#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304755static void dummy_setup(struct net_device *dev)
4756{
4757 dev->type = ARPHRD_NONE;
4758 dev->mtu = 0;
4759 dev->hard_header_len = 0;
4760 dev->addr_len = 0;
4761 dev->tx_queue_len = 0;
4762 dev->flags |= IFF_NOARP;
4763 dev->priv_flags |= IFF_NO_QUEUE;
4764
4765 /* Initialize the device structure. */
4766 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4767 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004768 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304769}
4770
4771static int config_mgmt_dev(struct pci_dev *pdev)
4772{
4773 struct adapter *adap = pci_get_drvdata(pdev);
4774 struct net_device *netdev;
4775 struct port_info *pi;
4776 char name[IFNAMSIZ];
4777 int err;
4778
4779 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304780 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4781 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304782 if (!netdev)
4783 return -ENOMEM;
4784
4785 pi = netdev_priv(netdev);
4786 pi->adapter = adap;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304787 pi->tx_chan = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304788 SET_NETDEV_DEV(netdev, &pdev->dev);
4789
4790 adap->port[0] = netdev;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304791 pi->port_id = 0;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304792
4793 err = register_netdev(adap->port[0]);
4794 if (err) {
4795 pr_info("Unable to register VF mgmt netdev %s\n", name);
4796 free_netdev(adap->port[0]);
4797 adap->port[0] = NULL;
4798 return err;
4799 }
4800 return 0;
4801}
4802
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304803static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4804{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304805 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304806 int err = 0;
4807 int current_vfs = pci_num_vf(pdev);
4808 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304809
Hariprasad Shenai78294512016-08-11 21:06:23 +05304810 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304811 /* Check if cxgb4 is the MASTER and fw is initialized */
4812 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4813 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4814 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4815 dev_warn(&pdev->dev,
4816 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4817 return -EOPNOTSUPP;
4818 }
4819
4820 /* If any of the VF's is already assigned to Guest OS, then
4821 * SRIOV for the same cannot be modified
4822 */
4823 if (current_vfs && pci_vfs_assigned(pdev)) {
4824 dev_err(&pdev->dev,
4825 "Cannot modify SR-IOV while VFs are assigned\n");
4826 num_vfs = current_vfs;
4827 return num_vfs;
4828 }
4829
4830 /* Disable SRIOV when zero is passed.
4831 * One needs to disable SRIOV before modifying it, else
4832 * stack throws the below warning:
4833 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4834 */
4835 if (!num_vfs) {
4836 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304837 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304838 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304839 adap->port[0] = NULL;
4840 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304841 /* free VF resources */
4842 kfree(adap->vfinfo);
4843 adap->vfinfo = NULL;
4844 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304845 return num_vfs;
4846 }
4847
4848 if (num_vfs != current_vfs) {
4849 err = pci_enable_sriov(pdev, num_vfs);
4850 if (err)
4851 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304852
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304853 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304854 err = config_mgmt_dev(pdev);
4855 if (err)
4856 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304857 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304858
4859 adap->vfinfo = kcalloc(adap->num_vfs,
4860 sizeof(struct vf_info), GFP_KERNEL);
4861 if (adap->vfinfo)
4862 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304863 return num_vfs;
4864}
4865#endif
4866
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004867static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004868{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004869 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004870 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004871 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004872 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304873 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304874 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304875 u32 whoami, pl_rev;
4876 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304877 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004878#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304879 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004880#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004881
4882 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4883
4884 err = pci_request_regions(pdev, KBUILD_MODNAME);
4885 if (err) {
4886 /* Just info, some other driver may have claimed the device. */
4887 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4888 return err;
4889 }
4890
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004891 err = pci_enable_device(pdev);
4892 if (err) {
4893 dev_err(&pdev->dev, "cannot enable PCI device\n");
4894 goto out_release_regions;
4895 }
4896
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304897 regs = pci_ioremap_bar(pdev, 0);
4898 if (!regs) {
4899 dev_err(&pdev->dev, "cannot map device registers\n");
4900 err = -ENOMEM;
4901 goto out_disable_device;
4902 }
4903
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304904 err = t4_wait_dev_ready(regs);
4905 if (err < 0)
4906 goto out_unmap_bar0;
4907
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304908 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304909 whoami = readl(regs + PL_WHOAMI_A);
4910 pl_rev = REV_G(readl(regs + PL_REV_A));
4911 chip = get_chip_type(pdev, pl_rev);
4912 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4913 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304914 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304915#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304916 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304917#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304918 pci_disable_device(pdev);
4919 pci_save_state(pdev); /* to restore SR-IOV later */
4920 goto sriov;
4921 }
4922
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004923 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004924 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004925 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4926 if (err) {
4927 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4928 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304929 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004930 }
4931 } else {
4932 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4933 if (err) {
4934 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304935 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004936 }
4937 }
4938
4939 pci_enable_pcie_error_reporting(pdev);
4940 pci_set_master(pdev);
4941 pci_save_state(pdev);
4942
4943 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4944 if (!adapter) {
4945 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304946 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004947 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304948 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004949
Anish Bhatt29aaee62014-08-20 13:44:06 -07004950 adapter->workq = create_singlethread_workqueue("cxgb4");
4951 if (!adapter->workq) {
4952 err = -ENOMEM;
4953 goto out_free_adapter;
4954 }
4955
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304956 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4957 (sizeof(struct mbox_cmd) *
4958 T4_OS_LOG_MBOX_CMDS),
4959 GFP_KERNEL);
4960 if (!adapter->mbox_log) {
4961 err = -ENOMEM;
4962 goto out_free_adapter;
4963 }
4964 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4965
Gavin Shan144be3d2014-01-23 12:27:34 +08004966 /* PCI device has been enabled */
4967 adapter->flags |= DEV_ENABLED;
4968
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304969 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004970 adapter->pdev = pdev;
4971 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304972 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304973 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304974 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05304975 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004976 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4977
Casey Leedomb0ba9d52017-08-15 11:23:26 +08004978 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
4979 * Ingress Packet Data to Free List Buffers in order to allow for
4980 * chipset performance optimizations between the Root Complex and
4981 * Memory Controllers. (Messages to the associated Ingress Queue
4982 * notifying new Packet Placement in the Free Lists Buffers will be
4983 * send without the Relaxed Ordering Attribute thus guaranteeing that
4984 * all preceding PCIe Transaction Layer Packets will be processed
4985 * first.) But some Root Complexes have various issues with Upstream
4986 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
4987 * The PCIe devices which under the Root Complexes will be cleared the
4988 * Relaxed Ordering bit in the configuration space, So we check our
4989 * PCIe configuration space to see if it's flagged with advice against
4990 * using Relaxed Ordering.
4991 */
4992 if (!pcie_relaxed_ordering_enabled(pdev))
4993 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
4994
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004995 spin_lock_init(&adapter->stats_lock);
4996 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004997 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05304998 spin_lock_init(&adapter->mbox_lock);
4999
5000 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005001
5002 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305003 INIT_WORK(&adapter->db_full_task, process_db_full);
5004 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005005
5006 err = t4_prep_adapter(adapter);
5007 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305008 goto out_free_adapter;
5009
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005010
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305011 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305012 s_qpp = (QUEUESPERPAGEPF0_S +
5013 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305014 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305015 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5016 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005017 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5018
5019 /* Each segment size is 128B. Write coalescing is enabled only
5020 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5021 * queue is less no of segments that can be accommodated in
5022 * a page size.
5023 */
5024 if (qpp > num_seg) {
5025 dev_err(&pdev->dev,
5026 "Incorrect number of egress queues per page\n");
5027 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305028 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005029 }
5030 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5031 pci_resource_len(pdev, 2));
5032 if (!adapter->bar2) {
5033 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5034 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305035 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005036 }
5037 }
5038
Vipul Pandya636f9d32012-09-26 02:39:39 +00005039 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005040 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305041#ifdef CONFIG_DEBUG_FS
5042 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5043#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005044 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005045 if (err)
5046 goto out_unmap_bar;
5047
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305048 /* configure SGE_STAT_CFG_A to read WC stats */
5049 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305050 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5051 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5052 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305053
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005054 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005055 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5056 MAX_ETH_QSETS);
5057 if (!netdev) {
5058 err = -ENOMEM;
5059 goto out_free_dev;
5060 }
5061
5062 SET_NETDEV_DEV(netdev, &pdev->dev);
5063
5064 adapter->port[i] = netdev;
5065 pi = netdev_priv(netdev);
5066 pi->adapter = adapter;
5067 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005068 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005069 netdev->irq = pdev->irq;
5070
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005071 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5072 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5073 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305074 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5075 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005076 if (highdma)
5077 netdev->hw_features |= NETIF_F_HIGHDMA;
5078 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005079 netdev->vlan_features = netdev->features & VLAN_FEAT;
5080
Jiri Pirko01789342011-08-16 06:29:00 +00005081 netdev->priv_flags |= IFF_UNICAST_FLT;
5082
Jarod Wilsond894be52016-10-20 13:55:16 -04005083 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305084 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005085 netdev->max_mtu = MAX_MTU;
5086
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005087 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005088#ifdef CONFIG_CHELSIO_T4_DCB
5089 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5090 cxgb4_dcb_state_init(netdev);
5091#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305092 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005093 }
5094
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305095 cxgb4_init_ethtool_dump(adapter);
5096
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005097 pci_set_drvdata(pdev, adapter);
5098
5099 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005100 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005101 if (err)
5102 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305103 } else if (adapter->params.nports == 1) {
5104 /* If we don't have a connection to the firmware -- possibly
5105 * because of an error -- grab the raw VPD parameters so we
5106 * can set the proper MAC Address on the debug network
5107 * interface that we've created.
5108 */
5109 u8 hw_addr[ETH_ALEN];
5110 u8 *na = adapter->params.vpd.na;
5111
5112 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5113 if (!err) {
5114 for (i = 0; i < ETH_ALEN; i++)
5115 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5116 hex2val(na[2 * i + 1]));
5117 t4_set_hw_addr(adapter, 0, hw_addr);
5118 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005119 }
5120
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305121 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005122 * soon as the first register_netdev completes.
5123 */
5124 cfg_queues(adapter);
5125
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305126 adapter->smt = t4_init_smt();
5127 if (!adapter->smt) {
5128 /* We tolerate a lack of SMT, giving up some functionality */
5129 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5130 }
5131
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305132 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005133 if (!adapter->l2t) {
5134 /* We tolerate a lack of L2T, giving up some functionality */
5135 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5136 adapter->params.offload = 0;
5137 }
5138
Anish Bhattb5a02f52015-01-14 15:17:34 -08005139#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305140 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5141 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5142 /* CLIP functionality is not present in hardware,
5143 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005144 */
5145 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305146 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005147 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305148 } else {
5149 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5150 adapter->clipt_end);
5151 if (!adapter->clipt) {
5152 /* We tolerate a lack of clip_table, giving up
5153 * some functionality
5154 */
5155 dev_warn(&pdev->dev,
5156 "could not allocate Clip table, continuing\n");
5157 adapter->params.offload = 0;
5158 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005159 }
5160#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305161
5162 for_each_port(adapter, i) {
5163 pi = adap2pinfo(adapter, i);
5164 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5165 if (!pi->sched_tbl)
5166 dev_warn(&pdev->dev,
5167 "could not activate scheduling on port %d\n",
5168 i);
5169 }
5170
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305171 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005172 dev_warn(&pdev->dev, "could not allocate TID table, "
5173 "continuing\n");
5174 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305175 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305176 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305177 if (!adapter->tc_u32)
5178 dev_warn(&pdev->dev,
5179 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305180
5181 cxgb4_init_tc_flower(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005182 }
5183
Kumar Sanghvi5c312542017-11-01 08:53:00 +05305184 if (is_offload(adapter) || is_hashfilter(adapter)) {
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305185 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5186 u32 hash_base, hash_reg;
5187
5188 if (chip <= CHELSIO_T5) {
5189 hash_reg = LE_DB_TID_HASHBASE_A;
5190 hash_base = t4_read_reg(adapter, hash_reg);
5191 adapter->tids.hash_base = hash_base / 4;
5192 } else {
5193 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5194 hash_base = t4_read_reg(adapter, hash_reg);
5195 adapter->tids.hash_base = hash_base;
5196 }
5197 }
5198 }
5199
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005200 /* See what interrupts we'll be using */
5201 if (msi > 1 && enable_msix(adapter) == 0)
5202 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305203 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005204 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305205 if (msi > 1)
5206 free_msix_info(adapter);
5207 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005208
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305209 /* check for PCI Express bandwidth capabiltites */
5210 cxgb4_check_pcie_caps(adapter);
5211
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005212 err = init_rss(adapter);
5213 if (err)
5214 goto out_free_dev;
5215
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005216 /*
5217 * The card is now ready to go. If any errors occur during device
5218 * registration we do not fail the whole card but rather proceed only
5219 * with the ports we manage to register successfully. However we must
5220 * register at least one net device.
5221 */
5222 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005223 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305224 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005225 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5226 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5227
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305228 netif_carrier_off(adapter->port[i]);
5229
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005230 err = register_netdev(adapter->port[i]);
5231 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005232 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005233 adapter->chan_map[pi->tx_chan] = i;
5234 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005235 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005236 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005237 dev_err(&pdev->dev, "could not register any net devices\n");
5238 goto out_free_dev;
5239 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005240 if (err) {
5241 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5242 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005243 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005244
5245 if (cxgb4_debugfs_root) {
5246 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5247 cxgb4_debugfs_root);
5248 setup_debugfs(adapter);
5249 }
5250
David S. Miller88c51002011-10-07 13:38:43 -04005251 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5252 pdev->needs_freset = 1;
5253
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305254 if (is_uld(adapter)) {
5255 mutex_lock(&uld_mutex);
5256 list_add_tail(&adapter->list_node, &adapter_list);
5257 mutex_unlock(&uld_mutex);
5258 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005259
Atul Gupta9c33e422017-07-04 16:46:21 +05305260 if (!is_t4(adapter->params.chip))
5261 cxgb4_ptp_init(adapter);
5262
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305263 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305264 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305265 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305266
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305267sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005268#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305269 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5270 if (!adapter) {
5271 err = -ENOMEM;
5272 goto free_pci_region;
5273 }
5274
Hariprasad Shenai78294512016-08-11 21:06:23 +05305275 adapter->pdev = pdev;
5276 adapter->pdev_dev = &pdev->dev;
5277 adapter->name = pci_name(pdev);
5278 adapter->mbox = func;
5279 adapter->pf = func;
5280 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305281 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305282 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5283 (sizeof(struct mbox_cmd) *
5284 T4_OS_LOG_MBOX_CMDS),
5285 GFP_KERNEL);
5286 if (!adapter->mbox_log) {
5287 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305288 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305289 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305290 spin_lock_init(&adapter->mbox_lock);
5291 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305292
5293 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5294 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5295 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5296 &v, &port_vec);
5297 if (err < 0) {
5298 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
Ganesh Goudard0417842017-06-09 19:26:24 +05305299 goto free_mbox_log;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305300 }
5301
5302 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305303 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005304 return 0;
5305
Ganesh Goudard0417842017-06-09 19:26:24 +05305306free_mbox_log:
5307 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305308 free_adapter:
5309 kfree(adapter);
5310 free_pci_region:
5311 iounmap(regs);
5312 pci_disable_sriov(pdev);
5313 pci_release_regions(pdev);
5314 return err;
5315#else
5316 return 0;
5317#endif
5318
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005319 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005320 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305321 if (adapter->flags & USING_MSIX)
5322 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305323 if (adapter->num_uld || adapter->num_ofld_uld)
5324 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005325 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305326 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005327 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005328 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005329 if (adapter->workq)
5330 destroy_workqueue(adapter->workq);
5331
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305332 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005333 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305334 out_unmap_bar0:
5335 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005336 out_disable_device:
5337 pci_disable_pcie_error_reporting(pdev);
5338 pci_disable_device(pdev);
5339 out_release_regions:
5340 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005341 return err;
5342}
5343
Bill Pemberton91744942012-12-03 09:23:02 -05005344static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005345{
5346 struct adapter *adapter = pci_get_drvdata(pdev);
5347
Hariprasad Shenai78294512016-08-11 21:06:23 +05305348 if (!adapter) {
5349 pci_release_regions(pdev);
5350 return;
5351 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005352
Ganesh Goudare1f61982017-09-21 12:50:47 +05305353 adapter->flags |= SHUTTING_DOWN;
5354
Hariprasad Shenai78294512016-08-11 21:06:23 +05305355 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005356 int i;
5357
Anish Bhatt29aaee62014-08-20 13:44:06 -07005358 /* Tear down per-adapter Work Queue first since it can contain
5359 * references to our adapter data structure.
5360 */
5361 destroy_workqueue(adapter->workq);
5362
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005363 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005364 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005365 t4_uld_clean_up(adapter);
5366 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005367
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305368 disable_interrupts(adapter);
5369
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005370 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005371 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005372 unregister_netdev(adapter->port[i]);
5373
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005374 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005375
Atul Gupta9c33e422017-07-04 16:46:21 +05305376 if (!is_t4(adapter->params.chip))
5377 cxgb4_ptp_stop(adapter);
5378
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005379 /* If we allocated filters, free up state associated with any
5380 * valid filters ...
5381 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305382 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005383
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005384 if (adapter->flags & FULL_INIT_DONE)
5385 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005386
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305387 if (adapter->flags & USING_MSIX)
5388 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305389 if (adapter->num_uld || adapter->num_ofld_uld)
5390 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005391 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005392#if IS_ENABLED(CONFIG_IPV6)
5393 t4_cleanup_clip_tbl(adapter);
5394#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005395 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305396 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005397 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005398 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005399 if ((adapter->flags & DEV_ENABLED)) {
5400 pci_disable_device(pdev);
5401 adapter->flags &= ~DEV_ENABLED;
5402 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005403 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305404 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005405 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005406 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305407 }
5408#ifdef CONFIG_PCI_IOV
5409 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305410 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305411 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305412 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305413 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305414 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305415 kfree(adapter);
5416 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005417 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305418 }
5419#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005420}
5421
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305422/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5423 * delivery. This is essentially a stripped down version of the PCI remove()
5424 * function where we do the minimal amount of work necessary to shutdown any
5425 * further activity.
5426 */
5427static void shutdown_one(struct pci_dev *pdev)
5428{
5429 struct adapter *adapter = pci_get_drvdata(pdev);
5430
5431 /* As with remove_one() above (see extended comment), we only want do
5432 * do cleanup on PCI Devices which went all the way through init_one()
5433 * ...
5434 */
5435 if (!adapter) {
5436 pci_release_regions(pdev);
5437 return;
5438 }
5439
Ganesh Goudare1f61982017-09-21 12:50:47 +05305440 adapter->flags |= SHUTTING_DOWN;
5441
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305442 if (adapter->pf == 4) {
5443 int i;
5444
5445 for_each_port(adapter, i)
5446 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5447 cxgb_close(adapter->port[i]);
5448
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005449 if (is_uld(adapter)) {
5450 detach_ulds(adapter);
5451 t4_uld_clean_up(adapter);
5452 }
5453
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305454 disable_interrupts(adapter);
5455 disable_msi(adapter);
5456
5457 t4_sge_stop(adapter);
5458 if (adapter->flags & FW_OK)
5459 t4_fw_bye(adapter, adapter->mbox);
5460 }
5461#ifdef CONFIG_PCI_IOV
5462 else {
5463 if (adapter->port[0])
5464 unregister_netdev(adapter->port[0]);
5465 iounmap(adapter->regs);
5466 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305467 kfree(adapter->mbox_log);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305468 kfree(adapter);
5469 pci_disable_sriov(pdev);
5470 pci_release_regions(pdev);
5471 }
5472#endif
5473}
5474
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005475static struct pci_driver cxgb4_driver = {
5476 .name = KBUILD_MODNAME,
5477 .id_table = cxgb4_pci_tbl,
5478 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005479 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305480 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305481#ifdef CONFIG_PCI_IOV
5482 .sriov_configure = cxgb4_iov_configure,
5483#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005484 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005485};
5486
5487static int __init cxgb4_init_module(void)
5488{
5489 int ret;
5490
5491 /* Debugfs support is optional, just warn if this fails */
5492 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5493 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005494 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005495
5496 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005497 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005498 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305499
Anish Bhatt1bb60372014-10-14 20:07:22 -07005500#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005501 if (!inet6addr_registered) {
5502 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5503 inet6addr_registered = true;
5504 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005505#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305506
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005507 return ret;
5508}
5509
5510static void __exit cxgb4_cleanup_module(void)
5511{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005512#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305513 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005514 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5515 inet6addr_registered = false;
5516 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005517#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005518 pci_unregister_driver(&cxgb4_driver);
5519 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005520}
5521
5522module_init(cxgb4_init_module);
5523module_exit(cxgb4_cleanup_module);