blob: 35709c7f7c5b86ab097dc3e0e8654376faea0f1a [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);
Kumar Sanghvi3b0b3be2017-11-01 08:53:02 +0530579 } else if (opcode == CPL_ABORT_RPL_RSS) {
580 const struct cpl_abort_rpl_rss *p = (void *)rsp;
581
582 hash_del_filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000583 } else
584 dev_err(q->adap->pdev_dev,
585 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000586out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000587 return 0;
588}
589
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000590static void disable_msi(struct adapter *adapter)
591{
592 if (adapter->flags & USING_MSIX) {
593 pci_disable_msix(adapter->pdev);
594 adapter->flags &= ~USING_MSIX;
595 } else if (adapter->flags & USING_MSI) {
596 pci_disable_msi(adapter->pdev);
597 adapter->flags &= ~USING_MSI;
598 }
599}
600
601/*
602 * Interrupt handler for non-data events used with MSI-X.
603 */
604static irqreturn_t t4_nondata_intr(int irq, void *cookie)
605{
606 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530607 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000608
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530609 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000610 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530611 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000612 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530613 if (adap->flags & MASTER_PF)
614 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000615 return IRQ_HANDLED;
616}
617
618/*
619 * Name the MSI-X interrupts.
620 */
621static void name_msix_vecs(struct adapter *adap)
622{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000623 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000624
625 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000626 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000627
628 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000629 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
630 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000631
632 /* Ethernet queues */
633 for_each_port(adap, j) {
634 struct net_device *d = adap->port[j];
635 const struct port_info *pi = netdev_priv(d);
636
Dimitris Michailidisba278162010-12-14 21:36:50 +0000637 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000638 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
639 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000640 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000641}
642
643static int request_msix_queue_irqs(struct adapter *adap)
644{
645 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530646 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530647 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000648
649 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
650 adap->msix_info[1].desc, &s->fw_evtq);
651 if (err)
652 return err;
653
654 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000655 err = request_irq(adap->msix_info[msi_index].vec,
656 t4_sge_intr_msix, 0,
657 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000658 &s->ethrxq[ethqidx].rspq);
659 if (err)
660 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000661 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000662 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000663 return 0;
664
665unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000666 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000667 free_irq(adap->msix_info[--msi_index].vec,
668 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000669 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
670 return err;
671}
672
673static void free_msix_queue_irqs(struct adapter *adap)
674{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000675 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000676 struct sge *s = &adap->sge;
677
678 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
679 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000680 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000681}
682
683/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530684 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000685 * @pi: the port
686 * @queues: array of queue indices for RSS
687 *
688 * Sets up the portion of the HW RSS table for the port's VI to distribute
689 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530690 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000691 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530692int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000693{
694 u16 *rss;
695 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530696 struct adapter *adapter = pi->adapter;
697 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000698
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530699 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000700 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
701 if (!rss)
702 return -ENOMEM;
703
704 /* map the queue indices to queue ids */
705 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530706 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000707
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530708 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000709 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530710 /* If Tunnel All Lookup isn't specified in the global RSS
711 * Configuration, then we need to specify a default Ingress
712 * Queue for any ingress packets which aren't hashed. We'll
713 * use our first ingress queue ...
714 */
715 if (!err)
716 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
717 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
718 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
719 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
720 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
721 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
722 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000723 kfree(rss);
724 return err;
725}
726
727/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000728 * setup_rss - configure RSS
729 * @adap: the adapter
730 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000731 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000732 */
733static int setup_rss(struct adapter *adap)
734{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530735 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000736
737 for_each_port(adap, i) {
738 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000739
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530740 /* Fill default values with equal distribution */
741 for (j = 0; j < pi->rss_size; j++)
742 pi->rss[j] = j % pi->nqsets;
743
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530744 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000745 if (err)
746 return err;
747 }
748 return 0;
749}
750
751/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000752 * Return the channel of the ingress queue with the given qid.
753 */
754static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
755{
756 qid -= p->ingr_start;
757 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
758}
759
760/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000761 * Wait until all NAPI handlers are descheduled.
762 */
763static void quiesce_rx(struct adapter *adap)
764{
765 int i;
766
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530767 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000768 struct sge_rspq *q = adap->sge.ingr_map[i];
769
Eric Dumazet5226b7912017-02-02 11:44:27 -0800770 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000771 napi_disable(&q->napi);
772 }
773}
774
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530775/* Disable interrupt and napi handler */
776static void disable_interrupts(struct adapter *adap)
777{
778 if (adap->flags & FULL_INIT_DONE) {
779 t4_intr_disable(adap);
780 if (adap->flags & USING_MSIX) {
781 free_msix_queue_irqs(adap);
782 free_irq(adap->msix_info[0].vec, adap);
783 } else {
784 free_irq(adap->pdev->irq, adap);
785 }
786 quiesce_rx(adap);
787 }
788}
789
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000790/*
791 * Enable NAPI scheduling and interrupt generation for all Rx queues.
792 */
793static void enable_rx(struct adapter *adap)
794{
795 int i;
796
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530797 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000798 struct sge_rspq *q = adap->sge.ingr_map[i];
799
800 if (!q)
801 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800802 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000803 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800804
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000805 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530806 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
807 SEINTARM_V(q->intr_params) |
808 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809 }
810}
811
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530812
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530813static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000814{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530816 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000817
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530818 bitmap_zero(s->starving_fl, s->egr_sz);
819 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000820
821 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530822 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000823 else {
824 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530825 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000826 if (err)
827 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530828 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000829 }
830
831 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530832 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530833 if (err)
834 t4_free_sge_resources(adap);
835 return err;
836}
837
838/**
839 * setup_sge_queues - configure SGE Tx/Rx/response queues
840 * @adap: the adapter
841 *
842 * Determines how many sets of SGE queues to use and initializes them.
843 * We support multiple queue sets per port if we have MSI-X, otherwise
844 * just one queue set per port.
845 */
846static int setup_sge_queues(struct adapter *adap)
847{
848 int err, i, j;
849 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530850 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530851 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000852
Ganesh Goudard427cae2017-06-16 15:36:09 +0530853 if (is_uld(adap))
854 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
855
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000856 for_each_port(adap, i) {
857 struct net_device *dev = adap->port[i];
858 struct port_info *pi = netdev_priv(dev);
859 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
860 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
861
862 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530863 if (adap->msi_idx > 0)
864 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000865 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530866 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530867 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530868 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530869 t4_get_tp_ch_map(adap,
870 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000871 if (err)
872 goto freeout;
873 q->rspq.idx = j;
874 memset(&q->stats, 0, sizeof(q->stats));
875 }
876 for (j = 0; j < pi->nqsets; j++, t++) {
877 err = t4_sge_alloc_eth_txq(adap, t, dev,
878 netdev_get_tx_queue(dev, j),
879 s->fw_evtq.cntxt_id);
880 if (err)
881 goto freeout;
882 }
883 }
884
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000885 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530886 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000887 * have RDMA queues, and that's the right value.
888 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530889 if (rxq_info)
890 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
891
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000892 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530893 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000894 if (err)
895 goto freeout;
896 }
897
Atul Guptaa45695042017-07-04 16:46:20 +0530898 if (!is_t4(adap->params.chip)) {
899 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
900 netdev_get_tx_queue(adap->port[0], 0)
901 , s->fw_evtq.cntxt_id);
902 if (err)
903 goto freeout;
904 }
905
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530906 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530907 MPS_TRC_RSS_CONTROL_A :
908 MPS_T5_TRC_RSS_CONTROL_A,
909 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
910 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000911 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530912freeout:
913 t4_free_sge_resources(adap);
914 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000915}
916
Anish Bhatt688848b2014-06-19 21:37:13 -0700917static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
918 void *accel_priv, select_queue_fallback_t fallback)
919{
920 int txq;
921
922#ifdef CONFIG_CHELSIO_T4_DCB
923 /* If a Data Center Bridging has been successfully negotiated on this
924 * link then we'll use the skb's priority to map it to a TX Queue.
925 * The skb's priority is determined via the VLAN Tag Priority Code
926 * Point field.
927 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530928 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700929 u16 vlan_tci;
930 int err;
931
932 err = vlan_get_tag(skb, &vlan_tci);
933 if (unlikely(err)) {
934 if (net_ratelimit())
935 netdev_warn(dev,
936 "TX Packet without VLAN Tag on DCB Link\n");
937 txq = 0;
938 } else {
939 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530940#ifdef CONFIG_CHELSIO_T4_FCOE
941 if (skb->protocol == htons(ETH_P_FCOE))
942 txq = skb->priority & 0x7;
943#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700944 }
945 return txq;
946 }
947#endif /* CONFIG_CHELSIO_T4_DCB */
948
949 if (select_queue) {
950 txq = (skb_rx_queue_recorded(skb)
951 ? skb_get_rx_queue(skb)
952 : smp_processor_id());
953
954 while (unlikely(txq >= dev->real_num_tx_queues))
955 txq -= dev->real_num_tx_queues;
956
957 return txq;
958 }
959
960 return fallback(dev, skb) % dev->real_num_tx_queues;
961}
962
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000963static int closest_timer(const struct sge *s, int time)
964{
965 int i, delta, match = 0, min_delta = INT_MAX;
966
967 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
968 delta = time - s->timer_val[i];
969 if (delta < 0)
970 delta = -delta;
971 if (delta < min_delta) {
972 min_delta = delta;
973 match = i;
974 }
975 }
976 return match;
977}
978
979static int closest_thres(const struct sge *s, int thres)
980{
981 int i, delta, match = 0, min_delta = INT_MAX;
982
983 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
984 delta = thres - s->counter_val[i];
985 if (delta < 0)
986 delta = -delta;
987 if (delta < min_delta) {
988 min_delta = delta;
989 match = i;
990 }
991 }
992 return match;
993}
994
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000995/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530996 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000997 * @q: the Rx queue
998 * @us: the hold-off time in us, or 0 to disable timer
999 * @cnt: the hold-off packet count, or 0 to disable counter
1000 *
1001 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1002 * one of the two needs to be enabled for the queue to generate interrupts.
1003 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301004int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1005 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001006{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301007 struct adapter *adap = q->adap;
1008
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001009 if ((us | cnt) == 0)
1010 cnt = 1;
1011
1012 if (cnt) {
1013 int err;
1014 u32 v, new_idx;
1015
1016 new_idx = closest_thres(&adap->sge, cnt);
1017 if (q->desc && q->pktcnt_idx != new_idx) {
1018 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301019 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1020 FW_PARAMS_PARAM_X_V(
1021 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1022 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301023 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1024 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001025 if (err)
1026 return err;
1027 }
1028 q->pktcnt_idx = new_idx;
1029 }
1030
1031 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301032 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001033 return 0;
1034}
1035
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001036static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001037{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001038 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001039 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001040 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001041
Patrick McHardyf6469682013-04-19 02:04:27 +00001042 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001043 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001044
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301045 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001046 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001047 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001048 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001049 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001050 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001051}
1052
Bill Pemberton91744942012-12-03 09:23:02 -05001053static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001054{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001055 if (IS_ERR_OR_NULL(adap->debugfs_root))
1056 return -1;
1057
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301058#ifdef CONFIG_DEBUG_FS
1059 t4_setup_debugfs(adap);
1060#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001061 return 0;
1062}
1063
1064/*
1065 * upper-layer driver support
1066 */
1067
1068/*
1069 * Allocate an active-open TID and set it to the supplied value.
1070 */
1071int cxgb4_alloc_atid(struct tid_info *t, void *data)
1072{
1073 int atid = -1;
1074
1075 spin_lock_bh(&t->atid_lock);
1076 if (t->afree) {
1077 union aopen_entry *p = t->afree;
1078
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001079 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001080 t->afree = p->next;
1081 p->data = data;
1082 t->atids_in_use++;
1083 }
1084 spin_unlock_bh(&t->atid_lock);
1085 return atid;
1086}
1087EXPORT_SYMBOL(cxgb4_alloc_atid);
1088
1089/*
1090 * Release an active-open TID.
1091 */
1092void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1093{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001094 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001095
1096 spin_lock_bh(&t->atid_lock);
1097 p->next = t->afree;
1098 t->afree = p;
1099 t->atids_in_use--;
1100 spin_unlock_bh(&t->atid_lock);
1101}
1102EXPORT_SYMBOL(cxgb4_free_atid);
1103
1104/*
1105 * Allocate a server TID and set it to the supplied value.
1106 */
1107int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1108{
1109 int stid;
1110
1111 spin_lock_bh(&t->stid_lock);
1112 if (family == PF_INET) {
1113 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1114 if (stid < t->nstids)
1115 __set_bit(stid, t->stid_bmap);
1116 else
1117 stid = -1;
1118 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301119 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001120 if (stid < 0)
1121 stid = -1;
1122 }
1123 if (stid >= 0) {
1124 t->stid_tab[stid].data = data;
1125 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301126 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1127 * This is equivalent to 4 TIDs. With CLIP enabled it
1128 * needs 2 TIDs.
1129 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301130 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301131 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301132 t->v6_stids_in_use += 2;
1133 } else {
1134 t->stids_in_use++;
1135 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001136 }
1137 spin_unlock_bh(&t->stid_lock);
1138 return stid;
1139}
1140EXPORT_SYMBOL(cxgb4_alloc_stid);
1141
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001142/* Allocate a server filter TID and set it to the supplied value.
1143 */
1144int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1145{
1146 int stid;
1147
1148 spin_lock_bh(&t->stid_lock);
1149 if (family == PF_INET) {
1150 stid = find_next_zero_bit(t->stid_bmap,
1151 t->nstids + t->nsftids, t->nstids);
1152 if (stid < (t->nstids + t->nsftids))
1153 __set_bit(stid, t->stid_bmap);
1154 else
1155 stid = -1;
1156 } else {
1157 stid = -1;
1158 }
1159 if (stid >= 0) {
1160 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301161 stid -= t->nstids;
1162 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301163 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001164 }
1165 spin_unlock_bh(&t->stid_lock);
1166 return stid;
1167}
1168EXPORT_SYMBOL(cxgb4_alloc_sftid);
1169
1170/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001171 */
1172void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1173{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301174 /* Is it a server filter TID? */
1175 if (t->nsftids && (stid >= t->sftid_base)) {
1176 stid -= t->sftid_base;
1177 stid += t->nstids;
1178 } else {
1179 stid -= t->stid_base;
1180 }
1181
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001182 spin_lock_bh(&t->stid_lock);
1183 if (family == PF_INET)
1184 __clear_bit(stid, t->stid_bmap);
1185 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301186 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001187 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301188 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301189 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301190 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301191 t->v6_stids_in_use -= 2;
1192 } else {
1193 t->stids_in_use--;
1194 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301195 } else {
1196 t->sftids_in_use--;
1197 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301198
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001199 spin_unlock_bh(&t->stid_lock);
1200}
1201EXPORT_SYMBOL(cxgb4_free_stid);
1202
1203/*
1204 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1205 */
1206static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1207 unsigned int tid)
1208{
1209 struct cpl_tid_release *req;
1210
1211 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001212 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001213 INIT_TP_WR(req, tid);
1214 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1215}
1216
1217/*
1218 * Queue a TID release request and if necessary schedule a work queue to
1219 * process it.
1220 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001221static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1222 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001223{
1224 void **p = &t->tid_tab[tid];
1225 struct adapter *adap = container_of(t, struct adapter, tids);
1226
1227 spin_lock_bh(&adap->tid_release_lock);
1228 *p = adap->tid_release_head;
1229 /* Low 2 bits encode the Tx channel number */
1230 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1231 if (!adap->tid_release_task_busy) {
1232 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001233 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001234 }
1235 spin_unlock_bh(&adap->tid_release_lock);
1236}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001237
1238/*
1239 * Process the list of pending TID release requests.
1240 */
1241static void process_tid_release_list(struct work_struct *work)
1242{
1243 struct sk_buff *skb;
1244 struct adapter *adap;
1245
1246 adap = container_of(work, struct adapter, tid_release_task);
1247
1248 spin_lock_bh(&adap->tid_release_lock);
1249 while (adap->tid_release_head) {
1250 void **p = adap->tid_release_head;
1251 unsigned int chan = (uintptr_t)p & 3;
1252 p = (void *)p - chan;
1253
1254 adap->tid_release_head = *p;
1255 *p = NULL;
1256 spin_unlock_bh(&adap->tid_release_lock);
1257
1258 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1259 GFP_KERNEL)))
1260 schedule_timeout_uninterruptible(1);
1261
1262 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1263 t4_ofld_send(adap, skb);
1264 spin_lock_bh(&adap->tid_release_lock);
1265 }
1266 adap->tid_release_task_busy = false;
1267 spin_unlock_bh(&adap->tid_release_lock);
1268}
1269
1270/*
1271 * Release a TID and inform HW. If we are unable to allocate the release
1272 * message we defer to a work queue.
1273 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301274void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1275 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001276{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001277 struct sk_buff *skb;
1278 struct adapter *adap = container_of(t, struct adapter, tids);
1279
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301280 WARN_ON(tid >= t->ntids);
1281
1282 if (t->tid_tab[tid]) {
1283 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301284 atomic_dec(&t->conns_in_use);
1285 if (t->hash_base && (tid >= t->hash_base)) {
1286 if (family == AF_INET6)
1287 atomic_sub(2, &t->hash_tids_in_use);
1288 else
1289 atomic_dec(&t->hash_tids_in_use);
1290 } else {
1291 if (family == AF_INET6)
1292 atomic_sub(2, &t->tids_in_use);
1293 else
1294 atomic_dec(&t->tids_in_use);
1295 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301296 }
1297
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001298 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1299 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001300 mk_tid_release(skb, chan, tid);
1301 t4_ofld_send(adap, skb);
1302 } else
1303 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001304}
1305EXPORT_SYMBOL(cxgb4_remove_tid);
1306
1307/*
1308 * Allocate and initialize the TID tables. Returns 0 on success.
1309 */
1310static int tid_init(struct tid_info *t)
1311{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301312 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301313 unsigned int max_ftids = t->nftids + t->nsftids;
1314 unsigned int natids = t->natids;
1315 unsigned int stid_bmap_size;
1316 unsigned int ftid_bmap_size;
1317 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001318
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001319 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301320 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001321 size = t->ntids * sizeof(*t->tid_tab) +
1322 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001323 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001324 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001325 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301326 max_ftids * sizeof(*t->ftid_tab) +
1327 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001328
Michal Hocko752ade62017-05-08 15:57:27 -07001329 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001330 if (!t->tid_tab)
1331 return -ENOMEM;
1332
1333 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1334 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001335 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001336 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301337 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001338 spin_lock_init(&t->stid_lock);
1339 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301340 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001341
1342 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301343 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301344 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001345 t->afree = NULL;
1346 t->atids_in_use = 0;
1347 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301348 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301349 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001350
1351 /* Setup the free list for atid_tab and clear the stid bitmap. */
1352 if (natids) {
1353 while (--natids)
1354 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1355 t->afree = t->atid_tab;
1356 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301357
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301358 if (is_offload(adap)) {
1359 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1360 /* Reserve stid 0 for T4/T5 adapters */
1361 if (!t->stid_base &&
1362 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1363 __set_bit(0, t->stid_bmap);
1364 }
1365
1366 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001367 return 0;
1368}
1369
1370/**
1371 * cxgb4_create_server - create an IP server
1372 * @dev: the device
1373 * @stid: the server TID
1374 * @sip: local IP address to bind server to
1375 * @sport: the server's TCP port
1376 * @queue: queue to direct messages from this server to
1377 *
1378 * Create an IP server for the given port and address.
1379 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1380 */
1381int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001382 __be32 sip, __be16 sport, __be16 vlan,
1383 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001384{
1385 unsigned int chan;
1386 struct sk_buff *skb;
1387 struct adapter *adap;
1388 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301389 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001390
1391 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1392 if (!skb)
1393 return -ENOMEM;
1394
1395 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001396 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001397 INIT_TP_WR(req, 0);
1398 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1399 req->local_port = sport;
1400 req->peer_port = htons(0);
1401 req->local_ip = sip;
1402 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001403 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001404 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001405 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1406 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301407 ret = t4_mgmt_tx(adap, skb);
1408 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001409}
1410EXPORT_SYMBOL(cxgb4_create_server);
1411
Vipul Pandya80f40c12013-07-04 16:10:45 +05301412/* cxgb4_create_server6 - create an IPv6 server
1413 * @dev: the device
1414 * @stid: the server TID
1415 * @sip: local IPv6 address to bind server to
1416 * @sport: the server's TCP port
1417 * @queue: queue to direct messages from this server to
1418 *
1419 * Create an IPv6 server for the given port and address.
1420 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1421 */
1422int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1423 const struct in6_addr *sip, __be16 sport,
1424 unsigned int queue)
1425{
1426 unsigned int chan;
1427 struct sk_buff *skb;
1428 struct adapter *adap;
1429 struct cpl_pass_open_req6 *req;
1430 int ret;
1431
1432 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1433 if (!skb)
1434 return -ENOMEM;
1435
1436 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001437 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301438 INIT_TP_WR(req, 0);
1439 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1440 req->local_port = sport;
1441 req->peer_port = htons(0);
1442 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1443 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1444 req->peer_ip_hi = cpu_to_be64(0);
1445 req->peer_ip_lo = cpu_to_be64(0);
1446 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001447 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001448 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1449 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301450 ret = t4_mgmt_tx(adap, skb);
1451 return net_xmit_eval(ret);
1452}
1453EXPORT_SYMBOL(cxgb4_create_server6);
1454
1455int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1456 unsigned int queue, bool ipv6)
1457{
1458 struct sk_buff *skb;
1459 struct adapter *adap;
1460 struct cpl_close_listsvr_req *req;
1461 int ret;
1462
1463 adap = netdev2adap(dev);
1464
1465 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1466 if (!skb)
1467 return -ENOMEM;
1468
Johannes Berg4df864c2017-06-16 14:29:21 +02001469 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301470 INIT_TP_WR(req, 0);
1471 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001472 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1473 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301474 ret = t4_mgmt_tx(adap, skb);
1475 return net_xmit_eval(ret);
1476}
1477EXPORT_SYMBOL(cxgb4_remove_server);
1478
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001479/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001480 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1481 * @mtus: the HW MTU table
1482 * @mtu: the target MTU
1483 * @idx: index of selected entry in the MTU table
1484 *
1485 * Returns the index and the value in the HW MTU table that is closest to
1486 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1487 * table, in which case that smallest available value is selected.
1488 */
1489unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1490 unsigned int *idx)
1491{
1492 unsigned int i = 0;
1493
1494 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1495 ++i;
1496 if (idx)
1497 *idx = i;
1498 return mtus[i];
1499}
1500EXPORT_SYMBOL(cxgb4_best_mtu);
1501
1502/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301503 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1504 * @mtus: the HW MTU table
1505 * @header_size: Header Size
1506 * @data_size_max: maximum Data Segment Size
1507 * @data_size_align: desired Data Segment Size Alignment (2^N)
1508 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1509 *
1510 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1511 * MTU Table based solely on a Maximum MTU parameter, we break that
1512 * parameter up into a Header Size and Maximum Data Segment Size, and
1513 * provide a desired Data Segment Size Alignment. If we find an MTU in
1514 * the Hardware MTU Table which will result in a Data Segment Size with
1515 * the requested alignment _and_ that MTU isn't "too far" from the
1516 * closest MTU, then we'll return that rather than the closest MTU.
1517 */
1518unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1519 unsigned short header_size,
1520 unsigned short data_size_max,
1521 unsigned short data_size_align,
1522 unsigned int *mtu_idxp)
1523{
1524 unsigned short max_mtu = header_size + data_size_max;
1525 unsigned short data_size_align_mask = data_size_align - 1;
1526 int mtu_idx, aligned_mtu_idx;
1527
1528 /* Scan the MTU Table till we find an MTU which is larger than our
1529 * Maximum MTU or we reach the end of the table. Along the way,
1530 * record the last MTU found, if any, which will result in a Data
1531 * Segment Length matching the requested alignment.
1532 */
1533 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1534 unsigned short data_size = mtus[mtu_idx] - header_size;
1535
1536 /* If this MTU minus the Header Size would result in a
1537 * Data Segment Size of the desired alignment, remember it.
1538 */
1539 if ((data_size & data_size_align_mask) == 0)
1540 aligned_mtu_idx = mtu_idx;
1541
1542 /* If we're not at the end of the Hardware MTU Table and the
1543 * next element is larger than our Maximum MTU, drop out of
1544 * the loop.
1545 */
1546 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1547 break;
1548 }
1549
1550 /* If we fell out of the loop because we ran to the end of the table,
1551 * then we just have to use the last [largest] entry.
1552 */
1553 if (mtu_idx == NMTUS)
1554 mtu_idx--;
1555
1556 /* If we found an MTU which resulted in the requested Data Segment
1557 * Length alignment and that's "not far" from the largest MTU which is
1558 * less than or equal to the maximum MTU, then use that.
1559 */
1560 if (aligned_mtu_idx >= 0 &&
1561 mtu_idx - aligned_mtu_idx <= 1)
1562 mtu_idx = aligned_mtu_idx;
1563
1564 /* If the caller has passed in an MTU Index pointer, pass the
1565 * MTU Index back. Return the MTU value.
1566 */
1567 if (mtu_idxp)
1568 *mtu_idxp = mtu_idx;
1569 return mtus[mtu_idx];
1570}
1571EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1572
1573/**
Hariprasad S27999802015-09-23 17:19:26 +05301574 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1575 * @chip: chip type
1576 * @viid: VI id of the given port
1577 *
1578 * Return the SMT index for this VI.
1579 */
1580unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1581{
1582 /* In T4/T5, SMT contains 256 SMAC entries organized in
1583 * 128 rows of 2 entries each.
1584 * In T6, SMT contains 256 SMAC entries in 256 rows.
1585 * TODO: The below code needs to be updated when we add support
1586 * for 256 VFs.
1587 */
1588 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1589 return ((viid & 0x7f) << 1);
1590 else
1591 return (viid & 0x7f);
1592}
1593EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1594
1595/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001596 * cxgb4_port_chan - get the HW channel of a port
1597 * @dev: the net device for the port
1598 *
1599 * Return the HW Tx channel of the given port.
1600 */
1601unsigned int cxgb4_port_chan(const struct net_device *dev)
1602{
1603 return netdev2pinfo(dev)->tx_chan;
1604}
1605EXPORT_SYMBOL(cxgb4_port_chan);
1606
Vipul Pandya881806b2012-05-18 15:29:24 +05301607unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1608{
1609 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001610 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301611
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301612 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1613 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301614 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301615 lp_count = LP_COUNT_G(v1);
1616 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001617 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301618 lp_count = LP_COUNT_T5_G(v1);
1619 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001620 }
1621 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301622}
1623EXPORT_SYMBOL(cxgb4_dbfifo_count);
1624
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001625/**
1626 * cxgb4_port_viid - get the VI id of a port
1627 * @dev: the net device for the port
1628 *
1629 * Return the VI id of the given port.
1630 */
1631unsigned int cxgb4_port_viid(const struct net_device *dev)
1632{
1633 return netdev2pinfo(dev)->viid;
1634}
1635EXPORT_SYMBOL(cxgb4_port_viid);
1636
1637/**
1638 * cxgb4_port_idx - get the index of a port
1639 * @dev: the net device for the port
1640 *
1641 * Return the index of the given port.
1642 */
1643unsigned int cxgb4_port_idx(const struct net_device *dev)
1644{
1645 return netdev2pinfo(dev)->port_id;
1646}
1647EXPORT_SYMBOL(cxgb4_port_idx);
1648
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001649void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1650 struct tp_tcp_stats *v6)
1651{
1652 struct adapter *adap = pci_get_drvdata(pdev);
1653
1654 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301655 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001656 spin_unlock(&adap->stats_lock);
1657}
1658EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1659
1660void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1661 const unsigned int *pgsz_order)
1662{
1663 struct adapter *adap = netdev2adap(dev);
1664
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301665 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1666 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1667 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1668 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001669}
1670EXPORT_SYMBOL(cxgb4_iscsi_init);
1671
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301672int cxgb4_flush_eq_cache(struct net_device *dev)
1673{
1674 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301675
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301676 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301677}
1678EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1679
1680static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1681{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301682 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301683 __be64 indices;
1684 int ret;
1685
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301686 spin_lock(&adap->win0_lock);
1687 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1688 sizeof(indices), (__be32 *)&indices,
1689 T4_MEMORY_READ);
1690 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301691 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001692 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1693 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301694 }
1695 return ret;
1696}
1697
1698int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1699 u16 size)
1700{
1701 struct adapter *adap = netdev2adap(dev);
1702 u16 hw_pidx, hw_cidx;
1703 int ret;
1704
1705 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1706 if (ret)
1707 goto out;
1708
1709 if (pidx != hw_pidx) {
1710 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301711 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301712
1713 if (pidx >= hw_pidx)
1714 delta = pidx - hw_pidx;
1715 else
1716 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301717
1718 if (is_t4(adap->params.chip))
1719 val = PIDX_V(delta);
1720 else
1721 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301722 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301723 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1724 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301725 }
1726out:
1727 return ret;
1728}
1729EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1730
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301731int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1732{
1733 struct adapter *adap;
1734 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301735 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301736 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1737 int ret;
1738
1739 adap = netdev2adap(dev);
1740
1741 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1742
1743 /* Figure out where the offset lands in the Memory Type/Address scheme.
1744 * This code assumes that the memory is laid out starting at offset 0
1745 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1746 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1747 * MC0, and some have both MC0 and MC1.
1748 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301749 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1750 edc0_size = EDRAM0_SIZE_G(size) << 20;
1751 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1752 edc1_size = EDRAM1_SIZE_G(size) << 20;
1753 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1754 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301755
1756 edc0_end = edc0_size;
1757 edc1_end = edc0_end + edc1_size;
1758 mc0_end = edc1_end + mc0_size;
1759
1760 if (offset < edc0_end) {
1761 memtype = MEM_EDC0;
1762 memaddr = offset;
1763 } else if (offset < edc1_end) {
1764 memtype = MEM_EDC1;
1765 memaddr = offset - edc0_end;
1766 } else {
1767 if (offset < mc0_end) {
1768 memtype = MEM_MC0;
1769 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301770 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301771 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1772 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301773 mc1_end = mc0_end + mc1_size;
1774 if (offset < mc1_end) {
1775 memtype = MEM_MC1;
1776 memaddr = offset - mc0_end;
1777 } else {
1778 /* offset beyond the end of any memory */
1779 goto err;
1780 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301781 } else {
1782 /* T4/T6 only has a single memory channel */
1783 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301784 }
1785 }
1786
1787 spin_lock(&adap->win0_lock);
1788 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1789 spin_unlock(&adap->win0_lock);
1790 return ret;
1791
1792err:
1793 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1794 stag, offset);
1795 return -EINVAL;
1796}
1797EXPORT_SYMBOL(cxgb4_read_tpte);
1798
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301799u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1800{
1801 u32 hi, lo;
1802 struct adapter *adap;
1803
1804 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301805 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1806 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301807
1808 return ((u64)hi << 32) | (u64)lo;
1809}
1810EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1811
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301812int cxgb4_bar2_sge_qregs(struct net_device *dev,
1813 unsigned int qid,
1814 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301815 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301816 u64 *pbar2_qoffset,
1817 unsigned int *pbar2_qid)
1818{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301819 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301820 qid,
1821 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1822 ? T4_BAR2_QTYPE_EGRESS
1823 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301824 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301825 pbar2_qoffset,
1826 pbar2_qid);
1827}
1828EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1829
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001830static struct pci_driver cxgb4_driver;
1831
1832static void check_neigh_update(struct neighbour *neigh)
1833{
1834 const struct device *parent;
1835 const struct net_device *netdev = neigh->dev;
1836
Parav Panditd0d7b102017-02-04 11:00:49 -06001837 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001838 netdev = vlan_dev_real_dev(netdev);
1839 parent = netdev->dev.parent;
1840 if (parent && parent->driver == &cxgb4_driver.driver)
1841 t4_l2t_update(dev_get_drvdata(parent), neigh);
1842}
1843
1844static int netevent_cb(struct notifier_block *nb, unsigned long event,
1845 void *data)
1846{
1847 switch (event) {
1848 case NETEVENT_NEIGH_UPDATE:
1849 check_neigh_update(data);
1850 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001851 case NETEVENT_REDIRECT:
1852 default:
1853 break;
1854 }
1855 return 0;
1856}
1857
1858static bool netevent_registered;
1859static struct notifier_block cxgb4_netevent_nb = {
1860 .notifier_call = netevent_cb
1861};
1862
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301863static void drain_db_fifo(struct adapter *adap, int usecs)
1864{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001865 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301866
1867 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301868 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1869 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301870 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301871 lp_count = LP_COUNT_G(v1);
1872 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001873 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301874 lp_count = LP_COUNT_T5_G(v1);
1875 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001876 }
1877
1878 if (lp_count == 0 && hp_count == 0)
1879 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301880 set_current_state(TASK_UNINTERRUPTIBLE);
1881 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301882 } while (1);
1883}
1884
1885static void disable_txq_db(struct sge_txq *q)
1886{
Steve Wise05eb2382014-03-14 21:52:08 +05301887 unsigned long flags;
1888
1889 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301890 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301891 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301892}
1893
Steve Wise05eb2382014-03-14 21:52:08 +05301894static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301895{
1896 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301897 if (q->db_pidx_inc) {
1898 /* Make sure that all writes to the TX descriptors
1899 * are committed before we tell HW about them.
1900 */
1901 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301902 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1903 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301904 q->db_pidx_inc = 0;
1905 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301906 q->db_disabled = 0;
1907 spin_unlock_irq(&q->db_lock);
1908}
1909
1910static void disable_dbs(struct adapter *adap)
1911{
1912 int i;
1913
1914 for_each_ethrxq(&adap->sge, i)
1915 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301916 if (is_offload(adap)) {
1917 struct sge_uld_txq_info *txq_info =
1918 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1919
1920 if (txq_info) {
1921 for_each_ofldtxq(&adap->sge, i) {
1922 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1923
1924 disable_txq_db(&txq->q);
1925 }
1926 }
1927 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301928 for_each_port(adap, i)
1929 disable_txq_db(&adap->sge.ctrlq[i].q);
1930}
1931
1932static void enable_dbs(struct adapter *adap)
1933{
1934 int i;
1935
1936 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301937 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301938 if (is_offload(adap)) {
1939 struct sge_uld_txq_info *txq_info =
1940 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1941
1942 if (txq_info) {
1943 for_each_ofldtxq(&adap->sge, i) {
1944 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1945
1946 enable_txq_db(adap, &txq->q);
1947 }
1948 }
1949 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301950 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301951 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1952}
1953
1954static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1955{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301956 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1957
1958 if (adap->uld && adap->uld[type].handle)
1959 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301960}
1961
1962static void process_db_full(struct work_struct *work)
1963{
1964 struct adapter *adap;
1965
1966 adap = container_of(work, struct adapter, db_full_task);
1967
1968 drain_db_fifo(adap, dbfifo_drain_delay);
1969 enable_dbs(adap);
1970 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301971 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1972 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1973 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1974 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1975 else
1976 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1977 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301978}
1979
1980static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1981{
1982 u16 hw_pidx, hw_cidx;
1983 int ret;
1984
Steve Wise05eb2382014-03-14 21:52:08 +05301985 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301986 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1987 if (ret)
1988 goto out;
1989 if (q->db_pidx != hw_pidx) {
1990 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301991 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301992
1993 if (q->db_pidx >= hw_pidx)
1994 delta = q->db_pidx - hw_pidx;
1995 else
1996 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301997
1998 if (is_t4(adap->params.chip))
1999 val = PIDX_V(delta);
2000 else
2001 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302002 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302003 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2004 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302005 }
2006out:
2007 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302008 q->db_pidx_inc = 0;
2009 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302010 if (ret)
2011 CH_WARN(adap, "DB drop recovery failed.\n");
2012}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302013
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302014static void recover_all_queues(struct adapter *adap)
2015{
2016 int i;
2017
2018 for_each_ethrxq(&adap->sge, i)
2019 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302020 if (is_offload(adap)) {
2021 struct sge_uld_txq_info *txq_info =
2022 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2023 if (txq_info) {
2024 for_each_ofldtxq(&adap->sge, i) {
2025 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2026
2027 sync_txq_pidx(adap, &txq->q);
2028 }
2029 }
2030 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302031 for_each_port(adap, i)
2032 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2033}
2034
Vipul Pandya881806b2012-05-18 15:29:24 +05302035static void process_db_drop(struct work_struct *work)
2036{
2037 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302038
Vipul Pandya881806b2012-05-18 15:29:24 +05302039 adap = container_of(work, struct adapter, db_drop_task);
2040
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302041 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302042 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002043 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302044 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002045 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302046 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002047 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302048 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302049 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002050 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2051 u16 qid = (dropped_db >> 15) & 0x1ffff;
2052 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302053 u64 bar2_qoffset;
2054 unsigned int bar2_qid;
2055 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002056
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302057 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002058 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302059 if (ret)
2060 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2061 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2062 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302063 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302064 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002065
2066 /* Re-enable BAR2 WC */
2067 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2068 }
2069
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302070 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2071 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302072}
2073
2074void t4_db_full(struct adapter *adap)
2075{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302076 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302077 disable_dbs(adap);
2078 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302079 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2080 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002081 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002082 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302083}
2084
2085void t4_db_dropped(struct adapter *adap)
2086{
Steve Wise05eb2382014-03-14 21:52:08 +05302087 if (is_t4(adap->params.chip)) {
2088 disable_dbs(adap);
2089 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2090 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002091 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302092}
2093
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302094void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002095{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002096 if (!netevent_registered) {
2097 register_netevent_notifier(&cxgb4_netevent_nb);
2098 netevent_registered = true;
2099 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002100}
2101
2102static void detach_ulds(struct adapter *adap)
2103{
2104 unsigned int i;
2105
2106 mutex_lock(&uld_mutex);
2107 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002108
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002109 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002110 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302111 adap->uld[i].state_change(adap->uld[i].handle,
2112 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002113
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002114 if (netevent_registered && list_empty(&adapter_list)) {
2115 unregister_netevent_notifier(&cxgb4_netevent_nb);
2116 netevent_registered = false;
2117 }
2118 mutex_unlock(&uld_mutex);
2119}
2120
2121static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2122{
2123 unsigned int i;
2124
2125 mutex_lock(&uld_mutex);
2126 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302127 if (adap->uld && adap->uld[i].handle)
2128 adap->uld[i].state_change(adap->uld[i].handle,
2129 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002130 mutex_unlock(&uld_mutex);
2131}
2132
Anish Bhatt1bb60372014-10-14 20:07:22 -07002133#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002134static int cxgb4_inet6addr_handler(struct notifier_block *this,
2135 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302136{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002137 struct inet6_ifaddr *ifa = data;
2138 struct net_device *event_dev = ifa->idev->dev;
2139 const struct device *parent = NULL;
2140#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302141 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002142#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002143 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002144 event_dev = vlan_dev_real_dev(event_dev);
2145#if IS_ENABLED(CONFIG_BONDING)
2146 if (event_dev->flags & IFF_MASTER) {
2147 list_for_each_entry(adap, &adapter_list, list_node) {
2148 switch (event) {
2149 case NETDEV_UP:
2150 cxgb4_clip_get(adap->port[0],
2151 (const u32 *)ifa, 1);
2152 break;
2153 case NETDEV_DOWN:
2154 cxgb4_clip_release(adap->port[0],
2155 (const u32 *)ifa, 1);
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161 return NOTIFY_OK;
2162 }
2163#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302164
Anish Bhattb5a02f52015-01-14 15:17:34 -08002165 if (event_dev)
2166 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302167
Anish Bhattb5a02f52015-01-14 15:17:34 -08002168 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302169 switch (event) {
2170 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002171 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302172 break;
2173 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002174 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302175 break;
2176 default:
2177 break;
2178 }
2179 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002180 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302181}
2182
Anish Bhattb5a02f52015-01-14 15:17:34 -08002183static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302184static struct notifier_block cxgb4_inet6addr_notifier = {
2185 .notifier_call = cxgb4_inet6addr_handler
2186};
2187
Vipul Pandya01bcca62013-07-04 16:10:46 +05302188static void update_clip(const struct adapter *adap)
2189{
2190 int i;
2191 struct net_device *dev;
2192 int ret;
2193
2194 rcu_read_lock();
2195
2196 for (i = 0; i < MAX_NPORTS; i++) {
2197 dev = adap->port[i];
2198 ret = 0;
2199
2200 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002201 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302202
2203 if (ret < 0)
2204 break;
2205 }
2206 rcu_read_unlock();
2207}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002208#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302209
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002210/**
2211 * cxgb_up - enable the adapter
2212 * @adap: adapter being enabled
2213 *
2214 * Called when the first port is enabled, this function performs the
2215 * actions necessary to make an adapter operational, such as completing
2216 * the initialization of HW modules, and enabling interrupts.
2217 *
2218 * Must be called with the rtnl lock held.
2219 */
2220static int cxgb_up(struct adapter *adap)
2221{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002222 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002223
Raju Rangoju91060382017-06-19 17:40:48 +05302224 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002225 err = setup_sge_queues(adap);
2226 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302227 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002228 err = setup_rss(adap);
2229 if (err)
2230 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002231
2232 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002233 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002234 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2235 adap->msix_info[0].desc, adap);
2236 if (err)
2237 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002238 err = request_msix_queue_irqs(adap);
2239 if (err) {
2240 free_irq(adap->msix_info[0].vec, adap);
2241 goto irq_err;
2242 }
2243 } else {
2244 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2245 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002246 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002247 if (err)
2248 goto irq_err;
2249 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302250
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002251 enable_rx(adap);
2252 t4_sge_start(adap);
2253 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002254 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302255 mutex_unlock(&uld_mutex);
2256
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002257 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002258#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302259 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002260#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302261 /* Initialize hash mac addr list*/
2262 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002263 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302264
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002265 irq_err:
2266 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002267 freeq:
2268 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302269 rel_lock:
2270 mutex_unlock(&uld_mutex);
2271 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002272}
2273
2274static void cxgb_down(struct adapter *adapter)
2275{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002276 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302277 cancel_work_sync(&adapter->db_full_task);
2278 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002279 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002280 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002281
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002282 t4_sge_stop(adapter);
2283 t4_free_sge_resources(adapter);
2284 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002285}
2286
2287/*
2288 * net_device operations
2289 */
2290static int cxgb_open(struct net_device *dev)
2291{
2292 int err;
2293 struct port_info *pi = netdev_priv(dev);
2294 struct adapter *adapter = pi->adapter;
2295
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002296 netif_carrier_off(dev);
2297
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002298 if (!(adapter->flags & FULL_INIT_DONE)) {
2299 err = cxgb_up(adapter);
2300 if (err < 0)
2301 return err;
2302 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002303
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302304 /* It's possible that the basic port information could have
2305 * changed since we first read it.
2306 */
2307 err = t4_update_port_info(pi);
2308 if (err < 0)
2309 return err;
2310
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002311 err = link_start(dev);
2312 if (!err)
2313 netif_tx_start_all_queues(dev);
2314 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002315}
2316
2317static int cxgb_close(struct net_device *dev)
2318{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002319 struct port_info *pi = netdev_priv(dev);
2320 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302321 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002322
2323 netif_tx_stop_all_queues(dev);
2324 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302325 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2326#ifdef CONFIG_CHELSIO_T4_DCB
2327 cxgb4_dcb_reset(dev);
2328 dcb_tx_queue_prio_enable(dev, false);
2329#endif
2330 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002331}
2332
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002333int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002334 __be32 sip, __be16 sport, __be16 vlan,
2335 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002336{
2337 int ret;
2338 struct filter_entry *f;
2339 struct adapter *adap;
2340 int i;
2341 u8 *val;
2342
2343 adap = netdev2adap(dev);
2344
Vipul Pandya1cab7752012-12-10 09:30:55 +00002345 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302346 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002347 stid += adap->tids.nftids;
2348
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002349 /* Check to make sure the filter requested is writable ...
2350 */
2351 f = &adap->tids.ftid_tab[stid];
2352 ret = writable_filter(f);
2353 if (ret)
2354 return ret;
2355
2356 /* Clear out any old resources being used by the filter before
2357 * we start constructing the new filter.
2358 */
2359 if (f->valid)
2360 clear_filter(adap, f);
2361
2362 /* Clear out filter specifications */
2363 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2364 f->fs.val.lport = cpu_to_be16(sport);
2365 f->fs.mask.lport = ~0;
2366 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002367 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002368 for (i = 0; i < 4; i++) {
2369 f->fs.val.lip[i] = val[i];
2370 f->fs.mask.lip[i] = ~0;
2371 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302372 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002373 f->fs.val.iport = port;
2374 f->fs.mask.iport = mask;
2375 }
2376 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002377
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302378 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302379 f->fs.val.proto = IPPROTO_TCP;
2380 f->fs.mask.proto = ~0;
2381 }
2382
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002383 f->fs.dirsteer = 1;
2384 f->fs.iq = queue;
2385 /* Mark filter as locked */
2386 f->locked = 1;
2387 f->fs.rpttid = 1;
2388
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302389 /* Save the actual tid. We need this to get the corresponding
2390 * filter entry structure in filter_rpl.
2391 */
2392 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002393 ret = set_filter_wr(adap, stid);
2394 if (ret) {
2395 clear_filter(adap, f);
2396 return ret;
2397 }
2398
2399 return 0;
2400}
2401EXPORT_SYMBOL(cxgb4_create_server_filter);
2402
2403int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2404 unsigned int queue, bool ipv6)
2405{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002406 struct filter_entry *f;
2407 struct adapter *adap;
2408
2409 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002410
2411 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302412 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002413 stid += adap->tids.nftids;
2414
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002415 f = &adap->tids.ftid_tab[stid];
2416 /* Unlock the filter */
2417 f->locked = 0;
2418
Wei Yongjun8c148462016-08-20 15:32:41 +00002419 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002420}
2421EXPORT_SYMBOL(cxgb4_remove_server_filter);
2422
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002423static void cxgb_get_stats(struct net_device *dev,
2424 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002425{
2426 struct port_stats stats;
2427 struct port_info *p = netdev_priv(dev);
2428 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002429
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002430 /* Block retrieving statistics during EEH error
2431 * recovery. Otherwise, the recovery might fail
2432 * and the PCI device will be removed permanently
2433 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002434 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002435 if (!netif_device_present(dev)) {
2436 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002437 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002438 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302439 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2440 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002441 spin_unlock(&adapter->stats_lock);
2442
2443 ns->tx_bytes = stats.tx_octets;
2444 ns->tx_packets = stats.tx_frames;
2445 ns->rx_bytes = stats.rx_octets;
2446 ns->rx_packets = stats.rx_frames;
2447 ns->multicast = stats.rx_mcast_frames;
2448
2449 /* detailed rx_errors */
2450 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2451 stats.rx_runt;
2452 ns->rx_over_errors = 0;
2453 ns->rx_crc_errors = stats.rx_fcs_err;
2454 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302455 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002456 stats.rx_ovflow2 + stats.rx_ovflow3 +
2457 stats.rx_trunc0 + stats.rx_trunc1 +
2458 stats.rx_trunc2 + stats.rx_trunc3;
2459 ns->rx_missed_errors = 0;
2460
2461 /* detailed tx_errors */
2462 ns->tx_aborted_errors = 0;
2463 ns->tx_carrier_errors = 0;
2464 ns->tx_fifo_errors = 0;
2465 ns->tx_heartbeat_errors = 0;
2466 ns->tx_window_errors = 0;
2467
2468 ns->tx_errors = stats.tx_error_frames;
2469 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2470 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002471}
2472
2473static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2474{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002475 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002476 int ret = 0, prtad, devad;
2477 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302478 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002479 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2480
2481 switch (cmd) {
2482 case SIOCGMIIPHY:
2483 if (pi->mdio_addr < 0)
2484 return -EOPNOTSUPP;
2485 data->phy_id = pi->mdio_addr;
2486 break;
2487 case SIOCGMIIREG:
2488 case SIOCSMIIREG:
2489 if (mdio_phy_id_is_c45(data->phy_id)) {
2490 prtad = mdio_phy_id_prtad(data->phy_id);
2491 devad = mdio_phy_id_devad(data->phy_id);
2492 } else if (data->phy_id < 32) {
2493 prtad = data->phy_id;
2494 devad = 0;
2495 data->reg_num &= 0x1f;
2496 } else
2497 return -EINVAL;
2498
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302499 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002500 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002501 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002502 data->reg_num, &data->val_out);
2503 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002504 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002505 data->reg_num, data->val_in);
2506 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302507 case SIOCGHWTSTAMP:
2508 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2509 sizeof(pi->tstamp_config)) ?
2510 -EFAULT : 0;
2511 case SIOCSHWTSTAMP:
2512 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2513 sizeof(pi->tstamp_config)))
2514 return -EFAULT;
2515
Atul Guptaa45695042017-07-04 16:46:20 +05302516 if (!is_t4(adapter->params.chip)) {
2517 switch (pi->tstamp_config.tx_type) {
2518 case HWTSTAMP_TX_OFF:
2519 case HWTSTAMP_TX_ON:
2520 break;
2521 default:
2522 return -ERANGE;
2523 }
2524
2525 switch (pi->tstamp_config.rx_filter) {
2526 case HWTSTAMP_FILTER_NONE:
2527 pi->rxtstamp = false;
2528 break;
2529 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2530 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2531 cxgb4_ptprx_timestamping(pi, pi->port_id,
2532 PTP_TS_L4);
2533 break;
2534 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2535 cxgb4_ptprx_timestamping(pi, pi->port_id,
2536 PTP_TS_L2_L4);
2537 break;
2538 case HWTSTAMP_FILTER_ALL:
2539 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2540 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2541 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2542 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2543 pi->rxtstamp = true;
2544 break;
2545 default:
2546 pi->tstamp_config.rx_filter =
2547 HWTSTAMP_FILTER_NONE;
2548 return -ERANGE;
2549 }
2550
2551 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2552 (pi->tstamp_config.rx_filter ==
2553 HWTSTAMP_FILTER_NONE)) {
2554 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2555 pi->ptp_enable = false;
2556 }
2557
2558 if (pi->tstamp_config.rx_filter !=
2559 HWTSTAMP_FILTER_NONE) {
2560 if (cxgb4_ptp_redirect_rx_packet(adapter,
2561 pi) >= 0)
2562 pi->ptp_enable = true;
2563 }
2564 } else {
2565 /* For T4 Adapters */
2566 switch (pi->tstamp_config.rx_filter) {
2567 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302568 pi->rxtstamp = false;
2569 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302570 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302571 pi->rxtstamp = true;
2572 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302573 default:
2574 pi->tstamp_config.rx_filter =
2575 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302576 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302577 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302578 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302579 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2580 sizeof(pi->tstamp_config)) ?
2581 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002582 default:
2583 return -EOPNOTSUPP;
2584 }
2585 return ret;
2586}
2587
2588static void cxgb_set_rxmode(struct net_device *dev)
2589{
2590 /* unfortunately we can't return errors to the stack */
2591 set_rxmode(dev, -1, false);
2592}
2593
2594static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2595{
2596 int ret;
2597 struct port_info *pi = netdev_priv(dev);
2598
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302599 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002600 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002601 if (!ret)
2602 dev->mtu = new_mtu;
2603 return ret;
2604}
2605
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302606#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302607static int dummy_open(struct net_device *dev)
2608{
2609 /* Turn carrier off since we don't have to transmit anything on this
2610 * interface.
2611 */
2612 netif_carrier_off(dev);
2613 return 0;
2614}
2615
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302616/* Fill MAC address that will be assigned by the FW */
2617static void fill_vf_station_mac_addr(struct adapter *adap)
2618{
2619 unsigned int i;
2620 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2621 int err;
2622 u8 *na;
2623 u16 a, b;
2624
2625 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2626 if (!err) {
2627 na = adap->params.vpd.na;
2628 for (i = 0; i < ETH_ALEN; i++)
2629 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2630 hex2val(na[2 * i + 1]));
2631 a = (hw_addr[0] << 8) | hw_addr[1];
2632 b = (hw_addr[1] << 8) | hw_addr[2];
2633 a ^= b;
2634 a |= 0x0200; /* locally assigned Ethernet MAC address */
2635 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2636 macaddr[0] = a >> 8;
2637 macaddr[1] = a & 0xff;
2638
2639 for (i = 2; i < 5; i++)
2640 macaddr[i] = hw_addr[i + 1];
2641
2642 for (i = 0; i < adap->num_vfs; i++) {
2643 macaddr[5] = adap->pf * 16 + i;
2644 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2645 }
2646 }
2647}
2648
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302649static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2650{
2651 struct port_info *pi = netdev_priv(dev);
2652 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302653 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302654
2655 /* verify MAC addr is valid */
2656 if (!is_valid_ether_addr(mac)) {
2657 dev_err(pi->adapter->pdev_dev,
2658 "Invalid Ethernet address %pM for VF %d\n",
2659 mac, vf);
2660 return -EINVAL;
2661 }
2662
2663 dev_info(pi->adapter->pdev_dev,
2664 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302665 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2666 if (!ret)
2667 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2668 return ret;
2669}
2670
2671static int cxgb_get_vf_config(struct net_device *dev,
2672 int vf, struct ifla_vf_info *ivi)
2673{
2674 struct port_info *pi = netdev_priv(dev);
2675 struct adapter *adap = pi->adapter;
2676
2677 if (vf >= adap->num_vfs)
2678 return -EINVAL;
2679 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302680 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2681 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302682 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2683 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302684}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302685
2686static int cxgb_get_phys_port_id(struct net_device *dev,
2687 struct netdev_phys_item_id *ppid)
2688{
2689 struct port_info *pi = netdev_priv(dev);
2690 unsigned int phy_port_id;
2691
2692 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2693 ppid->id_len = sizeof(phy_port_id);
2694 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2695 return 0;
2696}
2697
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302698static int cxgb_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2699 int max_tx_rate)
2700{
2701 struct port_info *pi = netdev_priv(dev);
2702 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302703 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302704 u32 fw_pfvf, fw_class;
2705 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302706 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302707 u16 pktsize;
2708
2709 if (vf >= adap->num_vfs)
2710 return -EINVAL;
2711
2712 if (min_tx_rate) {
2713 dev_err(adap->pdev_dev,
2714 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2715 min_tx_rate, vf);
2716 return -EINVAL;
2717 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302718
2719 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302720 if (ret != FW_SUCCESS) {
2721 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302722 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302723 return -EINVAL;
2724 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302725
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302726 if (!link_ok) {
2727 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2728 return -EINVAL;
2729 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302730
2731 if (max_tx_rate > speed) {
2732 dev_err(adap->pdev_dev,
2733 "Max tx rate %d for VF %d can't be > link-speed %u",
2734 max_tx_rate, vf, speed);
2735 return -EINVAL;
2736 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302737
2738 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302739 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2740 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2741 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2742 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2743 /* configure Traffic Class for rate-limiting */
2744 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2745 SCHED_CLASS_LEVEL_CL_RL,
2746 SCHED_CLASS_MODE_CLASS,
2747 SCHED_CLASS_RATEUNIT_BITS,
2748 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302749 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302750 max_tx_rate * 1000, 0, pktsize);
2751 if (ret) {
2752 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2753 ret);
2754 return -EINVAL;
2755 }
2756 dev_info(adap->pdev_dev,
2757 "Class %d with MSS %u configured with rate %u\n",
2758 class_id, pktsize, max_tx_rate);
2759
2760 /* bind VF to configured Traffic Class */
2761 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2762 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2763 fw_class = class_id;
2764 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2765 &fw_class);
2766 if (ret) {
2767 dev_err(adap->pdev_dev,
2768 "Err %d in binding VF %d to Traffic Class %d\n",
2769 ret, vf, class_id);
2770 return -EINVAL;
2771 }
2772 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2773 adap->pf, vf, class_id);
2774 adap->vfinfo[vf].tx_rate = max_tx_rate;
2775 return 0;
2776}
2777
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302778#endif
2779
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002780static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2781{
2782 int ret;
2783 struct sockaddr *addr = p;
2784 struct port_info *pi = netdev_priv(dev);
2785
2786 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002787 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002788
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302789 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002790 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002791 if (ret < 0)
2792 return ret;
2793
2794 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2795 pi->xact_addr_filt = ret;
2796 return 0;
2797}
2798
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002799#ifdef CONFIG_NET_POLL_CONTROLLER
2800static void cxgb_netpoll(struct net_device *dev)
2801{
2802 struct port_info *pi = netdev_priv(dev);
2803 struct adapter *adap = pi->adapter;
2804
2805 if (adap->flags & USING_MSIX) {
2806 int i;
2807 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2808
2809 for (i = pi->nqsets; i; i--, rx++)
2810 t4_sge_intr_msix(0, &rx->rspq);
2811 } else
2812 t4_intr_handler(adap)(0, adap);
2813}
2814#endif
2815
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302816static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2817{
2818 struct port_info *pi = netdev_priv(dev);
2819 struct adapter *adap = pi->adapter;
2820 struct sched_class *e;
2821 struct ch_sched_params p;
2822 struct ch_sched_queue qe;
2823 u32 req_rate;
2824 int err = 0;
2825
2826 if (!can_sched(dev))
2827 return -ENOTSUPP;
2828
2829 if (index < 0 || index > pi->nqsets - 1)
2830 return -EINVAL;
2831
2832 if (!(adap->flags & FULL_INIT_DONE)) {
2833 dev_err(adap->pdev_dev,
2834 "Failed to rate limit on queue %d. Link Down?\n",
2835 index);
2836 return -EINVAL;
2837 }
2838
2839 /* Convert from Mbps to Kbps */
2840 req_rate = rate << 10;
2841
2842 /* Max rate is 10 Gbps */
2843 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2844 dev_err(adap->pdev_dev,
2845 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2846 rate, SCHED_MAX_RATE_KBPS);
2847 return -ERANGE;
2848 }
2849
2850 /* First unbind the queue from any existing class */
2851 memset(&qe, 0, sizeof(qe));
2852 qe.queue = index;
2853 qe.class = SCHED_CLS_NONE;
2854
2855 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2856 if (err) {
2857 dev_err(adap->pdev_dev,
2858 "Unbinding Queue %d on port %d fail. Err: %d\n",
2859 index, pi->port_id, err);
2860 return err;
2861 }
2862
2863 /* Queue already unbound */
2864 if (!req_rate)
2865 return 0;
2866
2867 /* Fetch any available unused or matching scheduling class */
2868 memset(&p, 0, sizeof(p));
2869 p.type = SCHED_CLASS_TYPE_PACKET;
2870 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2871 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2872 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2873 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2874 p.u.params.channel = pi->tx_chan;
2875 p.u.params.class = SCHED_CLS_NONE;
2876 p.u.params.minrate = 0;
2877 p.u.params.maxrate = req_rate;
2878 p.u.params.weight = 0;
2879 p.u.params.pktsize = dev->mtu;
2880
2881 e = cxgb4_sched_class_alloc(dev, &p);
2882 if (!e)
2883 return -ENOMEM;
2884
2885 /* Bind the queue to a scheduling class */
2886 memset(&qe, 0, sizeof(qe));
2887 qe.queue = index;
2888 qe.class = e->idx;
2889
2890 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2891 if (err)
2892 dev_err(adap->pdev_dev,
2893 "Queue rate limiting failed. Err: %d\n", err);
2894 return err;
2895}
2896
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302897static int cxgb_setup_tc_flower(struct net_device *dev,
2898 struct tc_cls_flower_offload *cls_flower)
2899{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002900 if (cls_flower->common.chain_index)
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302901 return -EOPNOTSUPP;
2902
2903 switch (cls_flower->command) {
2904 case TC_CLSFLOWER_REPLACE:
2905 return cxgb4_tc_flower_replace(dev, cls_flower);
2906 case TC_CLSFLOWER_DESTROY:
2907 return cxgb4_tc_flower_destroy(dev, cls_flower);
2908 case TC_CLSFLOWER_STATS:
2909 return cxgb4_tc_flower_stats(dev, cls_flower);
2910 default:
2911 return -EOPNOTSUPP;
2912 }
2913}
2914
Jiri Pirkof7323042017-08-07 10:15:20 +02002915static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002916 struct tc_cls_u32_offload *cls_u32)
2917{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002918 if (cls_u32->common.chain_index)
Jiri Pirkof7323042017-08-07 10:15:20 +02002919 return -EOPNOTSUPP;
2920
2921 switch (cls_u32->command) {
2922 case TC_CLSU32_NEW_KNODE:
2923 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002924 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002925 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002926 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002927 default:
2928 return -EOPNOTSUPP;
2929 }
2930}
2931
Jiri Pirkocd019e92017-10-19 15:50:40 +02002932static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2933 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302934{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002935 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302936 struct port_info *pi = netdev2pinfo(dev);
2937 struct adapter *adap = netdev2adap(dev);
2938
2939 if (!(adap->flags & FULL_INIT_DONE)) {
2940 dev_err(adap->pdev_dev,
2941 "Failed to setup tc on port %d. Link Down?\n",
2942 pi->port_id);
2943 return -EINVAL;
2944 }
2945
Jiri Pirkof7323042017-08-07 10:15:20 +02002946 switch (type) {
2947 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002948 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302949 case TC_SETUP_CLSFLOWER:
2950 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002951 default:
2952 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302953 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302954}
2955
Jiri Pirkocd019e92017-10-19 15:50:40 +02002956static int cxgb_setup_tc_block(struct net_device *dev,
2957 struct tc_block_offload *f)
2958{
2959 struct port_info *pi = netdev2pinfo(dev);
2960
2961 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2962 return -EOPNOTSUPP;
2963
2964 switch (f->command) {
2965 case TC_BLOCK_BIND:
2966 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
2967 pi, dev);
2968 case TC_BLOCK_UNBIND:
2969 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
2970 return 0;
2971 default:
2972 return -EOPNOTSUPP;
2973 }
2974}
2975
2976static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
2977 void *type_data)
2978{
2979 switch (type) {
Jiri Pirkocd019e92017-10-19 15:50:40 +02002980 case TC_SETUP_BLOCK:
2981 return cxgb_setup_tc_block(dev, type_data);
2982 default:
2983 return -EOPNOTSUPP;
2984 }
2985}
2986
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302987static netdev_features_t cxgb_fix_features(struct net_device *dev,
2988 netdev_features_t features)
2989{
2990 /* Disable GRO, if RX_CSUM is disabled */
2991 if (!(features & NETIF_F_RXCSUM))
2992 features &= ~NETIF_F_GRO;
2993
2994 return features;
2995}
2996
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002997static const struct net_device_ops cxgb4_netdev_ops = {
2998 .ndo_open = cxgb_open,
2999 .ndo_stop = cxgb_close,
3000 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003001 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003002 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003003 .ndo_set_rx_mode = cxgb_set_rxmode,
3004 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003005 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003006 .ndo_validate_addr = eth_validate_addr,
3007 .ndo_do_ioctl = cxgb_ioctl,
3008 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003009#ifdef CONFIG_NET_POLL_CONTROLLER
3010 .ndo_poll_controller = cxgb_netpoll,
3011#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303012#ifdef CONFIG_CHELSIO_T4_FCOE
3013 .ndo_fcoe_enable = cxgb_fcoe_enable,
3014 .ndo_fcoe_disable = cxgb_fcoe_disable,
3015#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303016 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303017 .ndo_setup_tc = cxgb_setup_tc,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303018 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003019};
3020
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303021#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303022static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
3023 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303024 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05303025 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05303026 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05303027 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303028};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303029#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303030
3031static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3032{
3033 struct adapter *adapter = netdev2adap(dev);
3034
3035 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3036 strlcpy(info->version, cxgb4_driver_version,
3037 sizeof(info->version));
3038 strlcpy(info->bus_info, pci_name(adapter->pdev),
3039 sizeof(info->bus_info));
3040}
3041
3042static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3043 .get_drvinfo = get_drvinfo,
3044};
3045
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003046void t4_fatal_err(struct adapter *adap)
3047{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303048 int port;
3049
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003050 if (pci_channel_offline(adap->pdev))
3051 return;
3052
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303053 /* Disable the SGE since ULDs are going to free resources that
3054 * could be exposed to the adapter. RDMA MWs for example...
3055 */
3056 t4_shutdown_adapter(adap);
3057 for_each_port(adap, port) {
3058 struct net_device *dev = adap->port[port];
3059
3060 /* If we get here in very early initialization the network
3061 * devices may not have been set up yet.
3062 */
3063 if (!dev)
3064 continue;
3065
3066 netif_tx_stop_all_queues(dev);
3067 netif_carrier_off(dev);
3068 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003069 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3070}
3071
3072static void setup_memwin(struct adapter *adap)
3073{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303074 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003075
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303076 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003077}
3078
3079static void setup_memwin_rdma(struct adapter *adap)
3080{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003081 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303082 u32 start;
3083 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003084
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303085 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3086 start &= PCI_BASE_ADDRESS_MEM_MASK;
3087 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003088 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3089 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303090 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3091 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003092 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303093 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003094 adap->vres.ocq.start);
3095 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303096 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003097 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003098}
3099
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003100static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3101{
3102 u32 v;
3103 int ret;
3104
3105 /* get device capabilities */
3106 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303107 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3108 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303109 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303110 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003111 if (ret < 0)
3112 return ret;
3113
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303114 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3115 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303116 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003117 if (ret < 0)
3118 return ret;
3119
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303120 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003121 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303122 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3123 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003124 if (ret < 0)
3125 return ret;
3126
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303127 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303128 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3129 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003130 if (ret < 0)
3131 return ret;
3132
3133 t4_sge_init(adap);
3134
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003135 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303136 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303137 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303138 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3139 v = t4_read_reg(adap, TP_PIO_DATA_A);
3140 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003141
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003142 /* first 4 Tx modulation queues point to consecutive Tx channels */
3143 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303144 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3145 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003146
3147 /* associate each Tx modulation queue with consecutive Tx channels */
3148 v = 0x84218421;
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_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303151 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303152 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303153 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303154 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003155
3156#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3157 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303158 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3159 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3160 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3161 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3162 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3163 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3164 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3165 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3166 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3167 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003168 }
3169
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003170 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303171 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003172}
3173
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003174/*
3175 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3176 */
3177#define MAX_ATIDS 8192U
3178
3179/*
3180 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003181 *
3182 * If the firmware we're dealing with has Configuration File support, then
3183 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003184 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003185
3186/*
3187 * Tweak configuration based on module parameters, etc. Most of these have
3188 * defaults assigned to them by Firmware Configuration Files (if we're using
3189 * them) but need to be explicitly set if we're using hard-coded
3190 * initialization. But even in the case of using Firmware Configuration
3191 * Files, we'd like to expose the ability to change these via module
3192 * parameters so these are essentially common tweaks/settings for
3193 * Configuration Files and hard-coded initialization ...
3194 */
3195static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003196{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003197 /*
3198 * Fix up various Host-Dependent Parameters like Page Size, Cache
3199 * Line Size, etc. The firmware default is for a 4KB Page Size and
3200 * 64B Cache Line Size ...
3201 */
3202 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003203
Vipul Pandya636f9d32012-09-26 02:39:39 +00003204 /*
3205 * Process module parameters which affect early initialization.
3206 */
3207 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3208 dev_err(&adapter->pdev->dev,
3209 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3210 rx_dma_offset);
3211 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003212 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303213 t4_set_reg_field(adapter, SGE_CONTROL_A,
3214 PKTSHIFT_V(PKTSHIFT_M),
3215 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003216
Vipul Pandya636f9d32012-09-26 02:39:39 +00003217 /*
3218 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3219 * adds the pseudo header itself.
3220 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303221 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3222 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003223
3224 return 0;
3225}
3226
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303227/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3228 * unto themselves and they contain their own firmware to perform their
3229 * tasks ...
3230 */
3231static int phy_aq1202_version(const u8 *phy_fw_data,
3232 size_t phy_fw_size)
3233{
3234 int offset;
3235
3236 /* At offset 0x8 you're looking for the primary image's
3237 * starting offset which is 3 Bytes wide
3238 *
3239 * At offset 0xa of the primary image, you look for the offset
3240 * of the DRAM segment which is 3 Bytes wide.
3241 *
3242 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3243 * wide
3244 */
3245 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3246 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3247 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3248
3249 offset = le24(phy_fw_data + 0x8) << 12;
3250 offset = le24(phy_fw_data + offset + 0xa);
3251 return be16(phy_fw_data + offset + 0x27e);
3252
3253 #undef be16
3254 #undef le16
3255 #undef le24
3256}
3257
3258static struct info_10gbt_phy_fw {
3259 unsigned int phy_fw_id; /* PCI Device ID */
3260 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3261 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3262 int phy_flash; /* Has FLASH for PHY Firmware */
3263} phy_info_array[] = {
3264 {
3265 PHY_AQ1202_DEVICEID,
3266 PHY_AQ1202_FIRMWARE,
3267 phy_aq1202_version,
3268 1,
3269 },
3270 {
3271 PHY_BCM84834_DEVICEID,
3272 PHY_BCM84834_FIRMWARE,
3273 NULL,
3274 0,
3275 },
3276 { 0, NULL, NULL },
3277};
3278
3279static struct info_10gbt_phy_fw *find_phy_info(int devid)
3280{
3281 int i;
3282
3283 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3284 if (phy_info_array[i].phy_fw_id == devid)
3285 return &phy_info_array[i];
3286 }
3287 return NULL;
3288}
3289
3290/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3291 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3292 * we return a negative error number. If we transfer new firmware we return 1
3293 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3294 */
3295static int adap_init0_phy(struct adapter *adap)
3296{
3297 const struct firmware *phyf;
3298 int ret;
3299 struct info_10gbt_phy_fw *phy_info;
3300
3301 /* Use the device ID to determine which PHY file to flash.
3302 */
3303 phy_info = find_phy_info(adap->pdev->device);
3304 if (!phy_info) {
3305 dev_warn(adap->pdev_dev,
3306 "No PHY Firmware file found for this PHY\n");
3307 return -EOPNOTSUPP;
3308 }
3309
3310 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3311 * use that. The adapter firmware provides us with a memory buffer
3312 * where we can load a PHY firmware file from the host if we want to
3313 * override the PHY firmware File in flash.
3314 */
3315 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3316 adap->pdev_dev);
3317 if (ret < 0) {
3318 /* For adapters without FLASH attached to PHY for their
3319 * firmware, it's obviously a fatal error if we can't get the
3320 * firmware to the adapter. For adapters with PHY firmware
3321 * FLASH storage, it's worth a warning if we can't find the
3322 * PHY Firmware but we'll neuter the error ...
3323 */
3324 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3325 "/lib/firmware/%s, error %d\n",
3326 phy_info->phy_fw_file, -ret);
3327 if (phy_info->phy_flash) {
3328 int cur_phy_fw_ver = 0;
3329
3330 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3331 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3332 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3333 ret = 0;
3334 }
3335
3336 return ret;
3337 }
3338
3339 /* Load PHY Firmware onto adapter.
3340 */
3341 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3342 phy_info->phy_fw_version,
3343 (u8 *)phyf->data, phyf->size);
3344 if (ret < 0)
3345 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3346 -ret);
3347 else if (ret > 0) {
3348 int new_phy_fw_ver = 0;
3349
3350 if (phy_info->phy_fw_version)
3351 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3352 phyf->size);
3353 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3354 "Firmware /lib/firmware/%s, version %#x\n",
3355 phy_info->phy_fw_file, new_phy_fw_ver);
3356 }
3357
3358 release_firmware(phyf);
3359
3360 return ret;
3361}
3362
Vipul Pandya636f9d32012-09-26 02:39:39 +00003363/*
3364 * Attempt to initialize the adapter via a Firmware Configuration File.
3365 */
3366static int adap_init0_config(struct adapter *adapter, int reset)
3367{
3368 struct fw_caps_config_cmd caps_cmd;
3369 const struct firmware *cf;
3370 unsigned long mtype = 0, maddr = 0;
3371 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303372 int ret;
3373 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003374 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303375 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003376
3377 /*
3378 * Reset device if necessary.
3379 */
3380 if (reset) {
3381 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303382 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003383 if (ret < 0)
3384 goto bye;
3385 }
3386
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303387 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3388 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3389 * to be performed after any global adapter RESET above since some
3390 * PHYs only have local RAM copies of the PHY firmware.
3391 */
3392 if (is_10gbt_device(adapter->pdev->device)) {
3393 ret = adap_init0_phy(adapter);
3394 if (ret < 0)
3395 goto bye;
3396 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003397 /*
3398 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3399 * then use that. Otherwise, use the configuration file stored
3400 * in the adapter flash ...
3401 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303402 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003403 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303404 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003405 break;
3406 case CHELSIO_T5:
3407 fw_config_file = FW5_CFNAME;
3408 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303409 case CHELSIO_T6:
3410 fw_config_file = FW6_CFNAME;
3411 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003412 default:
3413 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3414 adapter->pdev->device);
3415 ret = -EINVAL;
3416 goto bye;
3417 }
3418
3419 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003420 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303421 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003422 mtype = FW_MEMTYPE_CF_FLASH;
3423 maddr = t4_flash_cfg_addr(adapter);
3424 } else {
3425 u32 params[7], val[7];
3426
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303427 sprintf(fw_config_file_path,
3428 "/lib/firmware/%s", fw_config_file);
3429 config_name = fw_config_file_path;
3430
Vipul Pandya636f9d32012-09-26 02:39:39 +00003431 if (cf->size >= FLASH_CFG_MAX_SIZE)
3432 ret = -ENOMEM;
3433 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303434 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3435 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003436 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303437 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003438 if (ret == 0) {
3439 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303440 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003441 * sizes have to be in terms of multiples of 4
3442 * bytes. So, if the Configuration File isn't
3443 * a multiple of 4 bytes in length we'll have
3444 * to write that out separately since we can't
3445 * guarantee that the bytes following the
3446 * residual byte in the buffer returned by
3447 * request_firmware() are zeroed out ...
3448 */
3449 size_t resid = cf->size & 0x3;
3450 size_t size = cf->size & ~0x3;
3451 __be32 *data = (__be32 *)cf->data;
3452
Hariprasad Shenai51678652014-11-21 12:52:02 +05303453 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3454 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003455
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303456 spin_lock(&adapter->win0_lock);
3457 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3458 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003459 if (ret == 0 && resid != 0) {
3460 union {
3461 __be32 word;
3462 char buf[4];
3463 } last;
3464 int i;
3465
3466 last.word = data[size >> 2];
3467 for (i = resid; i < 4; i++)
3468 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303469 ret = t4_memory_rw(adapter, 0, mtype,
3470 maddr + size,
3471 4, &last.word,
3472 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003473 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303474 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003475 }
3476 }
3477
3478 release_firmware(cf);
3479 if (ret)
3480 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003481 }
3482
Vipul Pandya636f9d32012-09-26 02:39:39 +00003483 /*
3484 * Issue a Capability Configuration command to the firmware to get it
3485 * to parse the Configuration File. We don't use t4_fw_config_file()
3486 * because we want the ability to modify various features after we've
3487 * processed the configuration file ...
3488 */
3489 memset(&caps_cmd, 0, sizeof(caps_cmd));
3490 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303491 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3492 FW_CMD_REQUEST_F |
3493 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303494 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303495 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3496 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3497 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003498 FW_LEN16(caps_cmd));
3499 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3500 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303501
3502 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3503 * Configuration File in FLASH), our last gasp effort is to use the
3504 * Firmware Configuration File which is embedded in the firmware. A
3505 * very few early versions of the firmware didn't have one embedded
3506 * but we can ignore those.
3507 */
3508 if (ret == -ENOENT) {
3509 memset(&caps_cmd, 0, sizeof(caps_cmd));
3510 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303511 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3512 FW_CMD_REQUEST_F |
3513 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303514 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3515 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3516 sizeof(caps_cmd), &caps_cmd);
3517 config_name = "Firmware Default";
3518 }
3519
3520 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003521 if (ret < 0)
3522 goto bye;
3523
Vipul Pandya636f9d32012-09-26 02:39:39 +00003524 finiver = ntohl(caps_cmd.finiver);
3525 finicsum = ntohl(caps_cmd.finicsum);
3526 cfcsum = ntohl(caps_cmd.cfcsum);
3527 if (finicsum != cfcsum)
3528 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3529 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3530 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003531
Vipul Pandya636f9d32012-09-26 02:39:39 +00003532 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003533 * And now tell the firmware to use the configuration we just loaded.
3534 */
3535 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303536 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3537 FW_CMD_REQUEST_F |
3538 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303539 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003540 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3541 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003542 if (ret < 0)
3543 goto bye;
3544
Vipul Pandya636f9d32012-09-26 02:39:39 +00003545 /*
3546 * Tweak configuration based on system architecture, module
3547 * parameters, etc.
3548 */
3549 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003550 if (ret < 0)
3551 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003552
Vipul Pandya636f9d32012-09-26 02:39:39 +00003553 /*
3554 * And finally tell the firmware to initialize itself using the
3555 * parameters from the Configuration File.
3556 */
3557 ret = t4_fw_initialize(adapter, adapter->mbox);
3558 if (ret < 0)
3559 goto bye;
3560
Hariprasad Shenai06640312015-01-13 15:19:25 +05303561 /* Emit Firmware Configuration File information and return
3562 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003563 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003564 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303565 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3566 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003567 return 0;
3568
3569 /*
3570 * Something bad happened. Return the error ... (If the "error"
3571 * is that there's no Configuration File on the adapter we don't
3572 * want to issue a warning since this is fairly common.)
3573 */
3574bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303575 if (config_issued && ret != -ENOENT)
3576 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3577 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003578 return ret;
3579}
3580
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303581static struct fw_info fw_info_array[] = {
3582 {
3583 .chip = CHELSIO_T4,
3584 .fs_name = FW4_CFNAME,
3585 .fw_mod_name = FW4_FNAME,
3586 .fw_hdr = {
3587 .chip = FW_HDR_CHIP_T4,
3588 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3589 .intfver_nic = FW_INTFVER(T4, NIC),
3590 .intfver_vnic = FW_INTFVER(T4, VNIC),
3591 .intfver_ri = FW_INTFVER(T4, RI),
3592 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3593 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3594 },
3595 }, {
3596 .chip = CHELSIO_T5,
3597 .fs_name = FW5_CFNAME,
3598 .fw_mod_name = FW5_FNAME,
3599 .fw_hdr = {
3600 .chip = FW_HDR_CHIP_T5,
3601 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3602 .intfver_nic = FW_INTFVER(T5, NIC),
3603 .intfver_vnic = FW_INTFVER(T5, VNIC),
3604 .intfver_ri = FW_INTFVER(T5, RI),
3605 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3606 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3607 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303608 }, {
3609 .chip = CHELSIO_T6,
3610 .fs_name = FW6_CFNAME,
3611 .fw_mod_name = FW6_FNAME,
3612 .fw_hdr = {
3613 .chip = FW_HDR_CHIP_T6,
3614 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3615 .intfver_nic = FW_INTFVER(T6, NIC),
3616 .intfver_vnic = FW_INTFVER(T6, VNIC),
3617 .intfver_ofld = FW_INTFVER(T6, OFLD),
3618 .intfver_ri = FW_INTFVER(T6, RI),
3619 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3620 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3621 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3622 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3623 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303624 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303625
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303626};
3627
3628static struct fw_info *find_fw_info(int chip)
3629{
3630 int i;
3631
3632 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3633 if (fw_info_array[i].chip == chip)
3634 return &fw_info_array[i];
3635 }
3636 return NULL;
3637}
3638
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003639/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003640 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003641 */
3642static int adap_init0(struct adapter *adap)
3643{
3644 int ret;
3645 u32 v, port_vec;
3646 enum dev_state state;
3647 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003648 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303649 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003650
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303651 /* Grab Firmware Device Log parameters as early as possible so we have
3652 * access to it for debugging, etc.
3653 */
3654 ret = t4_init_devlog_params(adap);
3655 if (ret < 0)
3656 return ret;
3657
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303658 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303659 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3660 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003661 if (ret < 0) {
3662 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3663 ret);
3664 return ret;
3665 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003666 if (ret == adap->mbox)
3667 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003668
Vipul Pandya636f9d32012-09-26 02:39:39 +00003669 /*
3670 * If we're the Master PF Driver and the device is uninitialized,
3671 * then let's consider upgrading the firmware ... (We always want
3672 * to check the firmware version number in order to A. get it for
3673 * later reporting and B. to warn if the currently loaded firmware
3674 * is excessively mismatched relative to the driver.)
3675 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303676
Ganesh Goudar760446f2017-07-20 18:28:48 +05303677 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303678 ret = t4_check_fw_version(adap);
3679 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303680 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303681 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003682 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303683 struct fw_info *fw_info;
3684 struct fw_hdr *card_fw;
3685 const struct firmware *fw;
3686 const u8 *fw_data = NULL;
3687 unsigned int fw_size = 0;
3688
3689 /* This is the firmware whose headers the driver was compiled
3690 * against
3691 */
3692 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3693 if (fw_info == NULL) {
3694 dev_err(adap->pdev_dev,
3695 "unable to get firmware info for chip %d.\n",
3696 CHELSIO_CHIP_VERSION(adap->params.chip));
3697 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003698 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303699
3700 /* allocate memory to read the header of the firmware on the
3701 * card
3702 */
Michal Hocko752ade62017-05-08 15:57:27 -07003703 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303704
3705 /* Get FW from from /lib/firmware/ */
3706 ret = request_firmware(&fw, fw_info->fw_mod_name,
3707 adap->pdev_dev);
3708 if (ret < 0) {
3709 dev_err(adap->pdev_dev,
3710 "unable to load firmware image %s, error %d\n",
3711 fw_info->fw_mod_name, ret);
3712 } else {
3713 fw_data = fw->data;
3714 fw_size = fw->size;
3715 }
3716
3717 /* upgrade FW logic */
3718 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3719 state, &reset);
3720
3721 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003722 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003723 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303724
Vipul Pandya636f9d32012-09-26 02:39:39 +00003725 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303726 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003727 }
3728
3729 /*
3730 * Grab VPD parameters. This should be done after we establish a
3731 * connection to the firmware since some of the VPD parameters
3732 * (notably the Core Clock frequency) are retrieved via requests to
3733 * the firmware. On the other hand, we need these fairly early on
3734 * so we do this right after getting ahold of the firmware.
3735 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303736 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003737 if (ret < 0)
3738 goto bye;
3739
Vipul Pandya636f9d32012-09-26 02:39:39 +00003740 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003741 * Find out what ports are available to us. Note that we need to do
3742 * this before calling adap_init0_no_config() since it needs nports
3743 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003744 */
3745 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303746 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3747 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303748 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003749 if (ret < 0)
3750 goto bye;
3751
3752 adap->params.nports = hweight32(port_vec);
3753 adap->params.portvec = port_vec;
3754
Hariprasad Shenai06640312015-01-13 15:19:25 +05303755 /* If the firmware is initialized already, emit a simply note to that
3756 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003757 */
3758 if (state == DEV_STATE_INIT) {
3759 dev_info(adap->pdev_dev, "Coming up as %s: "\
3760 "Adapter already initialized\n",
3761 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003762 } else {
3763 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3764 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303765
3766 /* Find out whether we're dealing with a version of the
3767 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003768 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303769 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3770 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303771 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303772 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003773
Hariprasad Shenai06640312015-01-13 15:19:25 +05303774 /* If the firmware doesn't support Configuration Files,
3775 * return an error.
3776 */
3777 if (ret < 0) {
3778 dev_err(adap->pdev_dev, "firmware doesn't support "
3779 "Firmware Configuration Files\n");
3780 goto bye;
3781 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003782
Hariprasad Shenai06640312015-01-13 15:19:25 +05303783 /* The firmware provides us with a memory buffer where we can
3784 * load a Configuration File from the host if we want to
3785 * override the Configuration File in flash.
3786 */
3787 ret = adap_init0_config(adap, reset);
3788 if (ret == -ENOENT) {
3789 dev_err(adap->pdev_dev, "no Configuration File "
3790 "present on adapter.\n");
3791 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003792 }
3793 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303794 dev_err(adap->pdev_dev, "could not initialize "
3795 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003796 goto bye;
3797 }
3798 }
3799
Hariprasad Shenai06640312015-01-13 15:19:25 +05303800 /* Give the SGE code a chance to pull in anything that it needs ...
3801 * Note that this must be called after we retrieve our VPD parameters
3802 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003803 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303804 ret = t4_sge_init(adap);
3805 if (ret < 0)
3806 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003807
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003808 if (is_bypass_device(adap->pdev->device))
3809 adap->params.bypass = 1;
3810
Vipul Pandya636f9d32012-09-26 02:39:39 +00003811 /*
3812 * Grab some of our basic fundamental operating parameters.
3813 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003814#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303815 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3816 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003817
3818#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303819 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3820 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3821 FW_PARAMS_PARAM_Y_V(0) | \
3822 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003823
Vipul Pandya636f9d32012-09-26 02:39:39 +00003824 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003825 params[1] = FW_PARAM_PFVF(L2T_START);
3826 params[2] = FW_PARAM_PFVF(L2T_END);
3827 params[3] = FW_PARAM_PFVF(FILTER_START);
3828 params[4] = FW_PARAM_PFVF(FILTER_END);
3829 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303830 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003831 if (ret < 0)
3832 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003833 adap->sge.egr_start = val[0];
3834 adap->l2t_start = val[1];
3835 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003836 adap->tids.ftid_base = val[3];
3837 adap->tids.nftids = val[4] - val[3] + 1;
3838 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003839
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303840 /* qids (ingress/egress) returned from firmware can be anywhere
3841 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3842 * Hence driver needs to allocate memory for this range to
3843 * store the queue info. Get the highest IQFLINT/EQ index returned
3844 * in FW_EQ_*_CMD.alloc command.
3845 */
3846 params[0] = FW_PARAM_PFVF(EQ_END);
3847 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303848 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303849 if (ret < 0)
3850 goto bye;
3851 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3852 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3853
3854 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3855 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3856 if (!adap->sge.egr_map) {
3857 ret = -ENOMEM;
3858 goto bye;
3859 }
3860
3861 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3862 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3863 if (!adap->sge.ingr_map) {
3864 ret = -ENOMEM;
3865 goto bye;
3866 }
3867
3868 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303869 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303870 */
3871 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3872 sizeof(long), GFP_KERNEL);
3873 if (!adap->sge.starving_fl) {
3874 ret = -ENOMEM;
3875 goto bye;
3876 }
3877
3878 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3879 sizeof(long), GFP_KERNEL);
3880 if (!adap->sge.txq_maperr) {
3881 ret = -ENOMEM;
3882 goto bye;
3883 }
3884
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303885#ifdef CONFIG_DEBUG_FS
3886 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3887 sizeof(long), GFP_KERNEL);
3888 if (!adap->sge.blocked_fl) {
3889 ret = -ENOMEM;
3890 goto bye;
3891 }
3892#endif
3893
Anish Bhattb5a02f52015-01-14 15:17:34 -08003894 params[0] = FW_PARAM_PFVF(CLIP_START);
3895 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303896 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003897 if (ret < 0)
3898 goto bye;
3899 adap->clipt_start = val[0];
3900 adap->clipt_end = val[1];
3901
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303902 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3903 * Classes supported by the hardware/firmware so we hard code it here
3904 * for now.
3905 */
3906 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3907
Vipul Pandya636f9d32012-09-26 02:39:39 +00003908 /* query params related to active filter region */
3909 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3910 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303911 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003912 /* If Active filter size is set we enable establishing
3913 * offload connection through firmware work request
3914 */
3915 if ((val[0] != val[1]) && (ret >= 0)) {
3916 adap->flags |= FW_OFLD_CONN;
3917 adap->tids.aftid_base = val[0];
3918 adap->tids.aftid_end = val[1];
3919 }
3920
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003921 /* If we're running on newer firmware, let it know that we're
3922 * prepared to deal with encapsulated CPL messages. Older
3923 * firmware won't understand this and we'll just get
3924 * unencapsulated messages ...
3925 */
3926 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3927 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303928 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003929
Vipul Pandya636f9d32012-09-26 02:39:39 +00003930 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303931 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3932 * capability. Earlier versions of the firmware didn't have the
3933 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3934 * permission to use ULPTX MEMWRITE DSGL.
3935 */
3936 if (is_t4(adap->params.chip)) {
3937 adap->params.ulptx_memwrite_dsgl = false;
3938 } else {
3939 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303940 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303941 1, params, val);
3942 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3943 }
3944
Steve Wise086de572016-09-16 07:54:49 -07003945 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3946 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3947 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3948 1, params, val);
3949 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3950
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05303951 /* See if FW supports FW_FILTER2 work request */
3952 if (is_t4(adap->params.chip)) {
3953 adap->params.filter2_wr_support = 0;
3954 } else {
3955 params[0] = FW_PARAM_DEV(FILTER2_WR);
3956 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3957 1, params, val);
3958 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
3959 }
3960
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303961 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003962 * Get device capabilities so we can determine what resources we need
3963 * to manage.
3964 */
3965 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303966 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3967 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303968 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003969 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3970 &caps_cmd);
3971 if (ret < 0)
3972 goto bye;
3973
Kumar Sanghvi5c312542017-11-01 08:53:00 +05303974 if (caps_cmd.ofldcaps ||
3975 (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER))) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003976 /* query offload-related parameters */
3977 params[0] = FW_PARAM_DEV(NTID);
3978 params[1] = FW_PARAM_PFVF(SERVER_START);
3979 params[2] = FW_PARAM_PFVF(SERVER_END);
3980 params[3] = FW_PARAM_PFVF(TDDP_START);
3981 params[4] = FW_PARAM_PFVF(TDDP_END);
3982 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303983 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003984 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003985 if (ret < 0)
3986 goto bye;
3987 adap->tids.ntids = val[0];
3988 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3989 adap->tids.stid_base = val[1];
3990 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003991 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003992 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003993 * region into two parts. Regular filters get 1/3rd and server
3994 * filters get 2/3rd part. This is only enabled if workarond
3995 * path is enabled.
3996 * 1. For regular filters.
3997 * 2. Server filter: This are special filters which are used
3998 * to redirect SYN packets to offload queue.
3999 */
4000 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4001 adap->tids.sftid_base = adap->tids.ftid_base +
4002 DIV_ROUND_UP(adap->tids.nftids, 3);
4003 adap->tids.nsftids = adap->tids.nftids -
4004 DIV_ROUND_UP(adap->tids.nftids, 3);
4005 adap->tids.nftids = adap->tids.sftid_base -
4006 adap->tids.ftid_base;
4007 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004008 adap->vres.ddp.start = val[3];
4009 adap->vres.ddp.size = val[4] - val[3] + 1;
4010 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004011
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304012 if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4013 if (init_hash_filter(adap) < 0)
4014 goto bye;
4015 } else {
4016 adap->params.offload = 1;
4017 adap->num_ofld_uld += 1;
4018 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004019 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004020 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004021 params[0] = FW_PARAM_PFVF(STAG_START);
4022 params[1] = FW_PARAM_PFVF(STAG_END);
4023 params[2] = FW_PARAM_PFVF(RQ_START);
4024 params[3] = FW_PARAM_PFVF(RQ_END);
4025 params[4] = FW_PARAM_PFVF(PBL_START);
4026 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304027 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004028 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004029 if (ret < 0)
4030 goto bye;
4031 adap->vres.stag.start = val[0];
4032 adap->vres.stag.size = val[1] - val[0] + 1;
4033 adap->vres.rq.start = val[2];
4034 adap->vres.rq.size = val[3] - val[2] + 1;
4035 adap->vres.pbl.start = val[4];
4036 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004037
4038 params[0] = FW_PARAM_PFVF(SQRQ_START);
4039 params[1] = FW_PARAM_PFVF(SQRQ_END);
4040 params[2] = FW_PARAM_PFVF(CQ_START);
4041 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004042 params[4] = FW_PARAM_PFVF(OCQ_START);
4043 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304044 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304045 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004046 if (ret < 0)
4047 goto bye;
4048 adap->vres.qp.start = val[0];
4049 adap->vres.qp.size = val[1] - val[0] + 1;
4050 adap->vres.cq.start = val[2];
4051 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004052 adap->vres.ocq.start = val[4];
4053 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304054
4055 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4056 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304057 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304058 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304059 if (ret < 0) {
4060 adap->params.max_ordird_qp = 8;
4061 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4062 ret = 0;
4063 } else {
4064 adap->params.max_ordird_qp = val[0];
4065 adap->params.max_ird_adapter = val[1];
4066 }
4067 dev_info(adap->pdev_dev,
4068 "max_ordird_qp %d max_ird_adapter %d\n",
4069 adap->params.max_ordird_qp,
4070 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304071 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004072 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004073 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004074 params[0] = FW_PARAM_PFVF(ISCSI_START);
4075 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304076 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004077 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004078 if (ret < 0)
4079 goto bye;
4080 adap->vres.iscsi.start = val[0];
4081 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304082 /* LIO target and cxgb4i initiaitor */
4083 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004084 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304085 if (caps_cmd.cryptocaps) {
4086 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304087 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4088 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4089 params, val);
4090 if (ret < 0) {
4091 if (ret != -EINVAL)
4092 goto bye;
4093 } else {
4094 adap->vres.ncrypto_fc = val[0];
4095 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304096 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4097 adap->num_uld += 1;
4098 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004099#undef FW_PARAM_PFVF
4100#undef FW_PARAM_DEV
4101
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304102 /* The MTU/MSS Table is initialized by now, so load their values. If
4103 * we're initializing the adapter, then we'll make any modifications
4104 * we want to the MTU/MSS Table and also initialize the congestion
4105 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004106 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004107 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304108 if (state != DEV_STATE_INIT) {
4109 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004110
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304111 /* The default MTU Table contains values 1492 and 1500.
4112 * However, for TCP, it's better to have two values which are
4113 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4114 * This allows us to have a TCP Data Payload which is a
4115 * multiple of 8 regardless of what combination of TCP Options
4116 * are in use (always a multiple of 4 bytes) which is
4117 * important for performance reasons. For instance, if no
4118 * options are in use, then we have a 20-byte IP header and a
4119 * 20-byte TCP header. In this case, a 1500-byte MSS would
4120 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4121 * which is not a multiple of 8. So using an MSS of 1488 in
4122 * this case results in a TCP Data Payload of 1448 bytes which
4123 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4124 * Stamps have been negotiated, then an MTU of 1500 bytes
4125 * results in a TCP Data Payload of 1448 bytes which, as
4126 * above, is a multiple of 8 bytes ...
4127 */
4128 for (i = 0; i < NMTUS; i++)
4129 if (adap->params.mtus[i] == 1492) {
4130 adap->params.mtus[i] = 1488;
4131 break;
4132 }
4133
4134 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4135 adap->params.b_wnd);
4136 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304137 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004138 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304139 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004140 return 0;
4141
4142 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004143 * Something bad happened. If a command timed out or failed with EIO
4144 * FW does not operate within its spec or something catastrophic
4145 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004146 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004147bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304148 kfree(adap->sge.egr_map);
4149 kfree(adap->sge.ingr_map);
4150 kfree(adap->sge.starving_fl);
4151 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304152#ifdef CONFIG_DEBUG_FS
4153 kfree(adap->sge.blocked_fl);
4154#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004155 if (ret != -ETIMEDOUT && ret != -EIO)
4156 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004157 return ret;
4158}
4159
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004160/* EEH callbacks */
4161
4162static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4163 pci_channel_state_t state)
4164{
4165 int i;
4166 struct adapter *adap = pci_get_drvdata(pdev);
4167
4168 if (!adap)
4169 goto out;
4170
4171 rtnl_lock();
4172 adap->flags &= ~FW_OK;
4173 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004174 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004175 for_each_port(adap, i) {
4176 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004177 if (dev) {
4178 netif_device_detach(dev);
4179 netif_carrier_off(dev);
4180 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004181 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004182 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304183 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004184 if (adap->flags & FULL_INIT_DONE)
4185 cxgb_down(adap);
4186 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004187 if ((adap->flags & DEV_ENABLED)) {
4188 pci_disable_device(pdev);
4189 adap->flags &= ~DEV_ENABLED;
4190 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004191out: return state == pci_channel_io_perm_failure ?
4192 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4193}
4194
4195static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4196{
4197 int i, ret;
4198 struct fw_caps_config_cmd c;
4199 struct adapter *adap = pci_get_drvdata(pdev);
4200
4201 if (!adap) {
4202 pci_restore_state(pdev);
4203 pci_save_state(pdev);
4204 return PCI_ERS_RESULT_RECOVERED;
4205 }
4206
Gavin Shan144be3d2014-01-23 12:27:34 +08004207 if (!(adap->flags & DEV_ENABLED)) {
4208 if (pci_enable_device(pdev)) {
4209 dev_err(&pdev->dev, "Cannot reenable PCI "
4210 "device after reset\n");
4211 return PCI_ERS_RESULT_DISCONNECT;
4212 }
4213 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004214 }
4215
4216 pci_set_master(pdev);
4217 pci_restore_state(pdev);
4218 pci_save_state(pdev);
4219 pci_cleanup_aer_uncorrect_error_status(pdev);
4220
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304221 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004222 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304223 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004224 return PCI_ERS_RESULT_DISCONNECT;
4225 adap->flags |= FW_OK;
4226 if (adap_init1(adap, &c))
4227 return PCI_ERS_RESULT_DISCONNECT;
4228
4229 for_each_port(adap, i) {
4230 struct port_info *p = adap2pinfo(adap, i);
4231
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304232 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004233 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004234 if (ret < 0)
4235 return PCI_ERS_RESULT_DISCONNECT;
4236 p->viid = ret;
4237 p->xact_addr_filt = -1;
4238 }
4239
4240 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4241 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004242 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004243 if (cxgb_up(adap))
4244 return PCI_ERS_RESULT_DISCONNECT;
4245 return PCI_ERS_RESULT_RECOVERED;
4246}
4247
4248static void eeh_resume(struct pci_dev *pdev)
4249{
4250 int i;
4251 struct adapter *adap = pci_get_drvdata(pdev);
4252
4253 if (!adap)
4254 return;
4255
4256 rtnl_lock();
4257 for_each_port(adap, i) {
4258 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004259 if (dev) {
4260 if (netif_running(dev)) {
4261 link_start(dev);
4262 cxgb_set_rxmode(dev);
4263 }
4264 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004265 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004266 }
4267 rtnl_unlock();
4268}
4269
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004270static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004271 .error_detected = eeh_err_detected,
4272 .slot_reset = eeh_slot_reset,
4273 .resume = eeh_resume,
4274};
4275
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304276/* Return true if the Link Configuration supports "High Speeds" (those greater
4277 * than 1Gb/s).
4278 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304279static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004280{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304281 unsigned int speeds, high_speeds;
4282
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304283 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4284 high_speeds = speeds &
4285 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304286
4287 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004288}
4289
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004290/*
4291 * Perform default configuration of DMA queues depending on the number and type
4292 * of ports we found and the number of available CPUs. Most settings can be
4293 * modified by the admin prior to actual use.
4294 */
Bill Pemberton91744942012-12-03 09:23:02 -05004295static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004296{
4297 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304298 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004299#ifndef CONFIG_CHELSIO_T4_DCB
4300 int q10g = 0;
4301#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004302
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304303 /* Reduce memory usage in kdump environment, disable all offload.
4304 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304305 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304306 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304307 adap->params.crypto = 0;
4308 }
4309
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304310 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004311#ifdef CONFIG_CHELSIO_T4_DCB
4312 /* For Data Center Bridging support we need to be able to support up
4313 * to 8 Traffic Priorities; each of which will be assigned to its
4314 * own TX Queue in order to prevent Head-Of-Line Blocking.
4315 */
4316 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4317 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4318 MAX_ETH_QSETS, adap->params.nports * 8);
4319 BUG_ON(1);
4320 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004321
Anish Bhatt688848b2014-06-19 21:37:13 -07004322 for_each_port(adap, i) {
4323 struct port_info *pi = adap2pinfo(adap, i);
4324
4325 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304326 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004327 qidx += pi->nqsets;
4328 }
4329#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004330 /*
4331 * We default to 1 queue per non-10G port and up to # of cores queues
4332 * per 10G port.
4333 */
4334 if (n10g)
4335 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004336 if (q10g > netif_get_num_default_rss_queues())
4337 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004338
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304339 if (is_kdump_kernel())
4340 q10g = 1;
4341
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004342 for_each_port(adap, i) {
4343 struct port_info *pi = adap2pinfo(adap, i);
4344
4345 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304346 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004347 qidx += pi->nqsets;
4348 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004349#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004350
4351 s->ethqsets = qidx;
4352 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4353
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304354 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004355 /*
4356 * For offload we use 1 queue/channel if all ports are up to 1G,
4357 * otherwise we divide all available queues amongst the channels
4358 * capped by the number of available cores.
4359 */
4360 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304361 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304362 s->ofldqsets = roundup(i, adap->params.nports);
4363 } else {
4364 s->ofldqsets = adap->params.nports;
4365 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004366 }
4367
4368 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4369 struct sge_eth_rxq *r = &s->ethrxq[i];
4370
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304371 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004372 r->fl.size = 72;
4373 }
4374
4375 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4376 s->ethtxq[i].q.size = 1024;
4377
4378 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4379 s->ctrlq[i].q.size = 512;
4380
Atul Guptaa45695042017-07-04 16:46:20 +05304381 if (!is_t4(adap->params.chip))
4382 s->ptptxq.q.size = 8;
4383
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304384 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304385 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004386}
4387
4388/*
4389 * Reduce the number of Ethernet queues across all ports to at most n.
4390 * n provides at least one queue per port.
4391 */
Bill Pemberton91744942012-12-03 09:23:02 -05004392static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004393{
4394 int i;
4395 struct port_info *pi;
4396
4397 while (n < adap->sge.ethqsets)
4398 for_each_port(adap, i) {
4399 pi = adap2pinfo(adap, i);
4400 if (pi->nqsets > 1) {
4401 pi->nqsets--;
4402 adap->sge.ethqsets--;
4403 if (adap->sge.ethqsets <= n)
4404 break;
4405 }
4406 }
4407
4408 n = 0;
4409 for_each_port(adap, i) {
4410 pi = adap2pinfo(adap, i);
4411 pi->first_qset = n;
4412 n += pi->nqsets;
4413 }
4414}
4415
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304416static int get_msix_info(struct adapter *adap)
4417{
4418 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304419 unsigned int max_ingq = 0;
4420
4421 if (is_offload(adap))
4422 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4423 if (is_pci_uld(adap))
4424 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4425
4426 if (!max_ingq)
4427 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304428
4429 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4430 if (!msix_info)
4431 return -ENOMEM;
4432
4433 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4434 sizeof(long), GFP_KERNEL);
4435 if (!adap->msix_bmap_ulds.msix_bmap) {
4436 kfree(msix_info);
4437 return -ENOMEM;
4438 }
4439 spin_lock_init(&adap->msix_bmap_ulds.lock);
4440 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304441out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304442 return 0;
4443}
4444
4445static void free_msix_info(struct adapter *adap)
4446{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304447 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304448 return;
4449
4450 kfree(adap->msix_info_ulds);
4451 kfree(adap->msix_bmap_ulds.msix_bmap);
4452}
4453
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004454/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4455#define EXTRA_VECS 2
4456
Bill Pemberton91744942012-12-03 09:23:02 -05004457static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004458{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304459 int ofld_need = 0, uld_need = 0;
4460 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004461 struct sge *s = &adap->sge;
4462 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304463 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304464 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004465
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304466 if (is_pci_uld(adap))
4467 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4468 if (is_offload(adap))
4469 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304470 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304471 GFP_KERNEL);
4472 if (!entries)
4473 return -ENOMEM;
4474
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304475 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304476 if (get_msix_info(adap)) {
4477 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304478 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304479 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304480
4481 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004482 entries[i].entry = i;
4483
4484 want = s->max_ethqsets + EXTRA_VECS;
4485 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304486 want += adap->num_ofld_uld * s->ofldqsets;
4487 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004488 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304489 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304490 want += adap->num_uld * s->ofldqsets;
4491 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304492 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004493#ifdef CONFIG_CHELSIO_T4_DCB
4494 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4495 * each port.
4496 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304497 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004498#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304499 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004500#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304501 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4502 if (allocated < 0) {
4503 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4504 " not using MSI-X\n");
4505 kfree(entries);
4506 return allocated;
4507 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004508
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304509 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004510 * Every group gets its minimum requirement and NIC gets top
4511 * priority for leftovers.
4512 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304513 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004514 if (i < s->max_ethqsets) {
4515 s->max_ethqsets = i;
4516 if (i < s->ethqsets)
4517 reduce_ethqs(adap, i);
4518 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304519 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304520 if (allocated < want)
4521 s->nqs_per_uld = nchan;
4522 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304523 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304524 }
4525
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304526 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004527 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304528 if (is_uld(adap)) {
4529 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304530 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304531 adap->msix_info_ulds[j].idx = i;
4532 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304533 adap->msix_bmap_ulds.mapsize = j;
4534 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304535 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304536 "nic %d per uld %d\n",
4537 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004538
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304539 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004540 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004541}
4542
4543#undef EXTRA_VECS
4544
Bill Pemberton91744942012-12-03 09:23:02 -05004545static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004546{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304547 unsigned int i;
4548 int err;
4549
4550 err = t4_init_rss_mode(adap, adap->mbox);
4551 if (err)
4552 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004553
4554 for_each_port(adap, i) {
4555 struct port_info *pi = adap2pinfo(adap, i);
4556
4557 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4558 if (!pi->rss)
4559 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004560 }
4561 return 0;
4562}
4563
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304564static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4565 enum pci_bus_speed *speed,
4566 enum pcie_link_width *width)
4567{
4568 u32 lnkcap1, lnkcap2;
4569 int err1, err2;
4570
4571#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4572
4573 *speed = PCI_SPEED_UNKNOWN;
4574 *width = PCIE_LNK_WIDTH_UNKNOWN;
4575
4576 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4577 &lnkcap1);
4578 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4579 &lnkcap2);
4580 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4581 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4582 *speed = PCIE_SPEED_8_0GT;
4583 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4584 *speed = PCIE_SPEED_5_0GT;
4585 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4586 *speed = PCIE_SPEED_2_5GT;
4587 }
4588 if (!err1) {
4589 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4590 if (!lnkcap2) { /* pre-r3.0 */
4591 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4592 *speed = PCIE_SPEED_5_0GT;
4593 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4594 *speed = PCIE_SPEED_2_5GT;
4595 }
4596 }
4597
4598 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4599 return err1 ? err1 : err2 ? err2 : -EINVAL;
4600 return 0;
4601}
4602
4603static void cxgb4_check_pcie_caps(struct adapter *adap)
4604{
4605 enum pcie_link_width width, width_cap;
4606 enum pci_bus_speed speed, speed_cap;
4607
4608#define PCIE_SPEED_STR(speed) \
4609 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4610 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4611 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4612 "Unknown")
4613
4614 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4615 dev_warn(adap->pdev_dev,
4616 "Unable to determine PCIe device BW capabilities\n");
4617 return;
4618 }
4619
4620 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4621 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4622 dev_warn(adap->pdev_dev,
4623 "Unable to determine PCI Express bandwidth.\n");
4624 return;
4625 }
4626
4627 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4628 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4629 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4630 width, width_cap);
4631 if (speed < speed_cap || width < width_cap)
4632 dev_info(adap->pdev_dev,
4633 "A slot with more lanes and/or higher speed is "
4634 "suggested for optimal performance.\n");
4635}
4636
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304637/* Dump basic information about the adapter */
4638static void print_adapter_info(struct adapter *adapter)
4639{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304640 /* Hardware/Firmware/etc. Version/Revision IDs */
4641 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304642
4643 /* Software/Hardware configuration */
4644 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4645 is_offload(adapter) ? "R" : "",
4646 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4647 (adapter->flags & USING_MSI) ? "MSI" : ""),
4648 is_offload(adapter) ? "Offload" : "non-Offload");
4649}
4650
Bill Pemberton91744942012-12-03 09:23:02 -05004651static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004652{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004653 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004654 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004655 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004656 const struct port_info *pi = netdev_priv(dev);
4657 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004658
4659 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4660 spd = " 2.5 GT/s";
4661 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4662 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004663 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4664 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004665
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304666 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304667 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304668 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304669 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304670 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004671 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304672 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304673 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304674 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304675 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304676 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4677 bufp += sprintf(bufp, "50G/");
4678 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304679 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304680 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4681 bufp += sprintf(bufp, "200G/");
4682 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4683 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004684 if (bufp != buf)
4685 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304686 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004687
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304688 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4689 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004690}
4691
Dimitris Michailidis06546392010-07-11 12:01:16 +00004692/*
4693 * Free the following resources:
4694 * - memory used for tables
4695 * - MSI/MSI-X
4696 * - net devices
4697 * - resources FW is holding for us
4698 */
4699static void free_some_resources(struct adapter *adapter)
4700{
4701 unsigned int i;
4702
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05304703 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07004704 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304705 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004706 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304707 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304708 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304709 kfree(adapter->sge.egr_map);
4710 kfree(adapter->sge.ingr_map);
4711 kfree(adapter->sge.starving_fl);
4712 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304713#ifdef CONFIG_DEBUG_FS
4714 kfree(adapter->sge.blocked_fl);
4715#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004716 disable_msi(adapter);
4717
4718 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004719 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304720 struct port_info *pi = adap2pinfo(adapter, i);
4721
4722 if (pi->viid != 0)
4723 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4724 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004725 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004726 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004727 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004728 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304729 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004730}
4731
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004732#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004733#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004734 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004735#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004736
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304737static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4738{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304739 u16 device_id;
4740
4741 /* Retrieve adapter's device ID */
4742 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004743
4744 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304745 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004746 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304747 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004748 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304749 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004750 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304751 default:
4752 dev_err(&pdev->dev, "Device %d is not supported\n",
4753 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304754 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004755 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304756}
4757
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304758#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304759static void dummy_setup(struct net_device *dev)
4760{
4761 dev->type = ARPHRD_NONE;
4762 dev->mtu = 0;
4763 dev->hard_header_len = 0;
4764 dev->addr_len = 0;
4765 dev->tx_queue_len = 0;
4766 dev->flags |= IFF_NOARP;
4767 dev->priv_flags |= IFF_NO_QUEUE;
4768
4769 /* Initialize the device structure. */
4770 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4771 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004772 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304773}
4774
4775static int config_mgmt_dev(struct pci_dev *pdev)
4776{
4777 struct adapter *adap = pci_get_drvdata(pdev);
4778 struct net_device *netdev;
4779 struct port_info *pi;
4780 char name[IFNAMSIZ];
4781 int err;
4782
4783 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304784 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4785 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304786 if (!netdev)
4787 return -ENOMEM;
4788
4789 pi = netdev_priv(netdev);
4790 pi->adapter = adap;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304791 pi->tx_chan = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304792 SET_NETDEV_DEV(netdev, &pdev->dev);
4793
4794 adap->port[0] = netdev;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304795 pi->port_id = 0;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304796
4797 err = register_netdev(adap->port[0]);
4798 if (err) {
4799 pr_info("Unable to register VF mgmt netdev %s\n", name);
4800 free_netdev(adap->port[0]);
4801 adap->port[0] = NULL;
4802 return err;
4803 }
4804 return 0;
4805}
4806
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304807static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4808{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304809 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304810 int err = 0;
4811 int current_vfs = pci_num_vf(pdev);
4812 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304813
Hariprasad Shenai78294512016-08-11 21:06:23 +05304814 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304815 /* Check if cxgb4 is the MASTER and fw is initialized */
4816 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4817 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4818 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4819 dev_warn(&pdev->dev,
4820 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4821 return -EOPNOTSUPP;
4822 }
4823
4824 /* If any of the VF's is already assigned to Guest OS, then
4825 * SRIOV for the same cannot be modified
4826 */
4827 if (current_vfs && pci_vfs_assigned(pdev)) {
4828 dev_err(&pdev->dev,
4829 "Cannot modify SR-IOV while VFs are assigned\n");
4830 num_vfs = current_vfs;
4831 return num_vfs;
4832 }
4833
4834 /* Disable SRIOV when zero is passed.
4835 * One needs to disable SRIOV before modifying it, else
4836 * stack throws the below warning:
4837 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4838 */
4839 if (!num_vfs) {
4840 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304841 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304842 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304843 adap->port[0] = NULL;
4844 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304845 /* free VF resources */
4846 kfree(adap->vfinfo);
4847 adap->vfinfo = NULL;
4848 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304849 return num_vfs;
4850 }
4851
4852 if (num_vfs != current_vfs) {
4853 err = pci_enable_sriov(pdev, num_vfs);
4854 if (err)
4855 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304856
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304857 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304858 err = config_mgmt_dev(pdev);
4859 if (err)
4860 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304861 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304862
4863 adap->vfinfo = kcalloc(adap->num_vfs,
4864 sizeof(struct vf_info), GFP_KERNEL);
4865 if (adap->vfinfo)
4866 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304867 return num_vfs;
4868}
4869#endif
4870
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004871static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004872{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004873 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004874 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004875 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004876 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304877 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304878 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304879 u32 whoami, pl_rev;
4880 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304881 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004882#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304883 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004884#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004885
4886 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4887
4888 err = pci_request_regions(pdev, KBUILD_MODNAME);
4889 if (err) {
4890 /* Just info, some other driver may have claimed the device. */
4891 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4892 return err;
4893 }
4894
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004895 err = pci_enable_device(pdev);
4896 if (err) {
4897 dev_err(&pdev->dev, "cannot enable PCI device\n");
4898 goto out_release_regions;
4899 }
4900
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304901 regs = pci_ioremap_bar(pdev, 0);
4902 if (!regs) {
4903 dev_err(&pdev->dev, "cannot map device registers\n");
4904 err = -ENOMEM;
4905 goto out_disable_device;
4906 }
4907
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304908 err = t4_wait_dev_ready(regs);
4909 if (err < 0)
4910 goto out_unmap_bar0;
4911
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304912 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304913 whoami = readl(regs + PL_WHOAMI_A);
4914 pl_rev = REV_G(readl(regs + PL_REV_A));
4915 chip = get_chip_type(pdev, pl_rev);
4916 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4917 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304918 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304919#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304920 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304921#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304922 pci_disable_device(pdev);
4923 pci_save_state(pdev); /* to restore SR-IOV later */
4924 goto sriov;
4925 }
4926
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004927 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004928 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004929 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4930 if (err) {
4931 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4932 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304933 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004934 }
4935 } else {
4936 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4937 if (err) {
4938 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304939 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004940 }
4941 }
4942
4943 pci_enable_pcie_error_reporting(pdev);
4944 pci_set_master(pdev);
4945 pci_save_state(pdev);
4946
4947 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4948 if (!adapter) {
4949 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304950 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004951 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304952 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004953
Anish Bhatt29aaee62014-08-20 13:44:06 -07004954 adapter->workq = create_singlethread_workqueue("cxgb4");
4955 if (!adapter->workq) {
4956 err = -ENOMEM;
4957 goto out_free_adapter;
4958 }
4959
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304960 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4961 (sizeof(struct mbox_cmd) *
4962 T4_OS_LOG_MBOX_CMDS),
4963 GFP_KERNEL);
4964 if (!adapter->mbox_log) {
4965 err = -ENOMEM;
4966 goto out_free_adapter;
4967 }
4968 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4969
Gavin Shan144be3d2014-01-23 12:27:34 +08004970 /* PCI device has been enabled */
4971 adapter->flags |= DEV_ENABLED;
4972
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304973 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004974 adapter->pdev = pdev;
4975 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304976 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304977 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304978 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05304979 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004980 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4981
Casey Leedomb0ba9d52017-08-15 11:23:26 +08004982 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
4983 * Ingress Packet Data to Free List Buffers in order to allow for
4984 * chipset performance optimizations between the Root Complex and
4985 * Memory Controllers. (Messages to the associated Ingress Queue
4986 * notifying new Packet Placement in the Free Lists Buffers will be
4987 * send without the Relaxed Ordering Attribute thus guaranteeing that
4988 * all preceding PCIe Transaction Layer Packets will be processed
4989 * first.) But some Root Complexes have various issues with Upstream
4990 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
4991 * The PCIe devices which under the Root Complexes will be cleared the
4992 * Relaxed Ordering bit in the configuration space, So we check our
4993 * PCIe configuration space to see if it's flagged with advice against
4994 * using Relaxed Ordering.
4995 */
4996 if (!pcie_relaxed_ordering_enabled(pdev))
4997 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
4998
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004999 spin_lock_init(&adapter->stats_lock);
5000 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07005001 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05305002 spin_lock_init(&adapter->mbox_lock);
5003
5004 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005005
5006 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305007 INIT_WORK(&adapter->db_full_task, process_db_full);
5008 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005009
5010 err = t4_prep_adapter(adapter);
5011 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305012 goto out_free_adapter;
5013
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005014
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305015 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305016 s_qpp = (QUEUESPERPAGEPF0_S +
5017 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305018 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305019 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5020 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005021 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5022
5023 /* Each segment size is 128B. Write coalescing is enabled only
5024 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5025 * queue is less no of segments that can be accommodated in
5026 * a page size.
5027 */
5028 if (qpp > num_seg) {
5029 dev_err(&pdev->dev,
5030 "Incorrect number of egress queues per page\n");
5031 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305032 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005033 }
5034 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5035 pci_resource_len(pdev, 2));
5036 if (!adapter->bar2) {
5037 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5038 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305039 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005040 }
5041 }
5042
Vipul Pandya636f9d32012-09-26 02:39:39 +00005043 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005044 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305045#ifdef CONFIG_DEBUG_FS
5046 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5047#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005048 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005049 if (err)
5050 goto out_unmap_bar;
5051
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305052 /* configure SGE_STAT_CFG_A to read WC stats */
5053 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305054 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5055 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5056 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305057
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005058 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005059 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5060 MAX_ETH_QSETS);
5061 if (!netdev) {
5062 err = -ENOMEM;
5063 goto out_free_dev;
5064 }
5065
5066 SET_NETDEV_DEV(netdev, &pdev->dev);
5067
5068 adapter->port[i] = netdev;
5069 pi = netdev_priv(netdev);
5070 pi->adapter = adapter;
5071 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005072 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005073 netdev->irq = pdev->irq;
5074
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005075 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5076 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5077 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305078 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5079 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005080 if (highdma)
5081 netdev->hw_features |= NETIF_F_HIGHDMA;
5082 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005083 netdev->vlan_features = netdev->features & VLAN_FEAT;
5084
Jiri Pirko01789342011-08-16 06:29:00 +00005085 netdev->priv_flags |= IFF_UNICAST_FLT;
5086
Jarod Wilsond894be52016-10-20 13:55:16 -04005087 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305088 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005089 netdev->max_mtu = MAX_MTU;
5090
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005091 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005092#ifdef CONFIG_CHELSIO_T4_DCB
5093 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5094 cxgb4_dcb_state_init(netdev);
5095#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305096 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005097 }
5098
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305099 cxgb4_init_ethtool_dump(adapter);
5100
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005101 pci_set_drvdata(pdev, adapter);
5102
5103 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005104 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005105 if (err)
5106 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305107 } else if (adapter->params.nports == 1) {
5108 /* If we don't have a connection to the firmware -- possibly
5109 * because of an error -- grab the raw VPD parameters so we
5110 * can set the proper MAC Address on the debug network
5111 * interface that we've created.
5112 */
5113 u8 hw_addr[ETH_ALEN];
5114 u8 *na = adapter->params.vpd.na;
5115
5116 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5117 if (!err) {
5118 for (i = 0; i < ETH_ALEN; i++)
5119 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5120 hex2val(na[2 * i + 1]));
5121 t4_set_hw_addr(adapter, 0, hw_addr);
5122 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005123 }
5124
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305125 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005126 * soon as the first register_netdev completes.
5127 */
5128 cfg_queues(adapter);
5129
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305130 adapter->smt = t4_init_smt();
5131 if (!adapter->smt) {
5132 /* We tolerate a lack of SMT, giving up some functionality */
5133 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5134 }
5135
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305136 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005137 if (!adapter->l2t) {
5138 /* We tolerate a lack of L2T, giving up some functionality */
5139 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5140 adapter->params.offload = 0;
5141 }
5142
Anish Bhattb5a02f52015-01-14 15:17:34 -08005143#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305144 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5145 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5146 /* CLIP functionality is not present in hardware,
5147 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005148 */
5149 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305150 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005151 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305152 } else {
5153 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5154 adapter->clipt_end);
5155 if (!adapter->clipt) {
5156 /* We tolerate a lack of clip_table, giving up
5157 * some functionality
5158 */
5159 dev_warn(&pdev->dev,
5160 "could not allocate Clip table, continuing\n");
5161 adapter->params.offload = 0;
5162 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005163 }
5164#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305165
5166 for_each_port(adapter, i) {
5167 pi = adap2pinfo(adapter, i);
5168 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5169 if (!pi->sched_tbl)
5170 dev_warn(&pdev->dev,
5171 "could not activate scheduling on port %d\n",
5172 i);
5173 }
5174
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305175 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005176 dev_warn(&pdev->dev, "could not allocate TID table, "
5177 "continuing\n");
5178 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305179 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305180 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305181 if (!adapter->tc_u32)
5182 dev_warn(&pdev->dev,
5183 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305184
5185 cxgb4_init_tc_flower(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005186 }
5187
Kumar Sanghvi5c312542017-11-01 08:53:00 +05305188 if (is_offload(adapter) || is_hashfilter(adapter)) {
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305189 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5190 u32 hash_base, hash_reg;
5191
5192 if (chip <= CHELSIO_T5) {
5193 hash_reg = LE_DB_TID_HASHBASE_A;
5194 hash_base = t4_read_reg(adapter, hash_reg);
5195 adapter->tids.hash_base = hash_base / 4;
5196 } else {
5197 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5198 hash_base = t4_read_reg(adapter, hash_reg);
5199 adapter->tids.hash_base = hash_base;
5200 }
5201 }
5202 }
5203
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005204 /* See what interrupts we'll be using */
5205 if (msi > 1 && enable_msix(adapter) == 0)
5206 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305207 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005208 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305209 if (msi > 1)
5210 free_msix_info(adapter);
5211 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005212
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305213 /* check for PCI Express bandwidth capabiltites */
5214 cxgb4_check_pcie_caps(adapter);
5215
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005216 err = init_rss(adapter);
5217 if (err)
5218 goto out_free_dev;
5219
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005220 /*
5221 * The card is now ready to go. If any errors occur during device
5222 * registration we do not fail the whole card but rather proceed only
5223 * with the ports we manage to register successfully. However we must
5224 * register at least one net device.
5225 */
5226 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005227 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305228 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005229 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5230 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5231
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305232 netif_carrier_off(adapter->port[i]);
5233
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005234 err = register_netdev(adapter->port[i]);
5235 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005236 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005237 adapter->chan_map[pi->tx_chan] = i;
5238 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005239 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005240 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005241 dev_err(&pdev->dev, "could not register any net devices\n");
5242 goto out_free_dev;
5243 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005244 if (err) {
5245 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5246 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005247 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005248
5249 if (cxgb4_debugfs_root) {
5250 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5251 cxgb4_debugfs_root);
5252 setup_debugfs(adapter);
5253 }
5254
David S. Miller88c51002011-10-07 13:38:43 -04005255 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5256 pdev->needs_freset = 1;
5257
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305258 if (is_uld(adapter)) {
5259 mutex_lock(&uld_mutex);
5260 list_add_tail(&adapter->list_node, &adapter_list);
5261 mutex_unlock(&uld_mutex);
5262 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005263
Atul Gupta9c33e422017-07-04 16:46:21 +05305264 if (!is_t4(adapter->params.chip))
5265 cxgb4_ptp_init(adapter);
5266
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305267 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305268 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305269 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305270
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305271sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005272#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305273 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5274 if (!adapter) {
5275 err = -ENOMEM;
5276 goto free_pci_region;
5277 }
5278
Hariprasad Shenai78294512016-08-11 21:06:23 +05305279 adapter->pdev = pdev;
5280 adapter->pdev_dev = &pdev->dev;
5281 adapter->name = pci_name(pdev);
5282 adapter->mbox = func;
5283 adapter->pf = func;
5284 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305285 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305286 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5287 (sizeof(struct mbox_cmd) *
5288 T4_OS_LOG_MBOX_CMDS),
5289 GFP_KERNEL);
5290 if (!adapter->mbox_log) {
5291 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305292 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305293 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305294 spin_lock_init(&adapter->mbox_lock);
5295 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305296
5297 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5298 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5299 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5300 &v, &port_vec);
5301 if (err < 0) {
5302 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
Ganesh Goudard0417842017-06-09 19:26:24 +05305303 goto free_mbox_log;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305304 }
5305
5306 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305307 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005308 return 0;
5309
Ganesh Goudard0417842017-06-09 19:26:24 +05305310free_mbox_log:
5311 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305312 free_adapter:
5313 kfree(adapter);
5314 free_pci_region:
5315 iounmap(regs);
5316 pci_disable_sriov(pdev);
5317 pci_release_regions(pdev);
5318 return err;
5319#else
5320 return 0;
5321#endif
5322
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005323 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005324 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305325 if (adapter->flags & USING_MSIX)
5326 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305327 if (adapter->num_uld || adapter->num_ofld_uld)
5328 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005329 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305330 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005331 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005332 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005333 if (adapter->workq)
5334 destroy_workqueue(adapter->workq);
5335
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305336 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005337 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305338 out_unmap_bar0:
5339 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005340 out_disable_device:
5341 pci_disable_pcie_error_reporting(pdev);
5342 pci_disable_device(pdev);
5343 out_release_regions:
5344 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005345 return err;
5346}
5347
Bill Pemberton91744942012-12-03 09:23:02 -05005348static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005349{
5350 struct adapter *adapter = pci_get_drvdata(pdev);
5351
Hariprasad Shenai78294512016-08-11 21:06:23 +05305352 if (!adapter) {
5353 pci_release_regions(pdev);
5354 return;
5355 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005356
Ganesh Goudare1f61982017-09-21 12:50:47 +05305357 adapter->flags |= SHUTTING_DOWN;
5358
Hariprasad Shenai78294512016-08-11 21:06:23 +05305359 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005360 int i;
5361
Anish Bhatt29aaee62014-08-20 13:44:06 -07005362 /* Tear down per-adapter Work Queue first since it can contain
5363 * references to our adapter data structure.
5364 */
5365 destroy_workqueue(adapter->workq);
5366
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005367 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005368 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005369 t4_uld_clean_up(adapter);
5370 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005371
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305372 disable_interrupts(adapter);
5373
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005374 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005375 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005376 unregister_netdev(adapter->port[i]);
5377
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005378 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005379
Atul Gupta9c33e422017-07-04 16:46:21 +05305380 if (!is_t4(adapter->params.chip))
5381 cxgb4_ptp_stop(adapter);
5382
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005383 /* If we allocated filters, free up state associated with any
5384 * valid filters ...
5385 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305386 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005387
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005388 if (adapter->flags & FULL_INIT_DONE)
5389 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005390
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305391 if (adapter->flags & USING_MSIX)
5392 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305393 if (adapter->num_uld || adapter->num_ofld_uld)
5394 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005395 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005396#if IS_ENABLED(CONFIG_IPV6)
5397 t4_cleanup_clip_tbl(adapter);
5398#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005399 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305400 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005401 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005402 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005403 if ((adapter->flags & DEV_ENABLED)) {
5404 pci_disable_device(pdev);
5405 adapter->flags &= ~DEV_ENABLED;
5406 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005407 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305408 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005409 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005410 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305411 }
5412#ifdef CONFIG_PCI_IOV
5413 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305414 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305415 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305416 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305417 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305418 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305419 kfree(adapter);
5420 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005421 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305422 }
5423#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005424}
5425
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305426/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5427 * delivery. This is essentially a stripped down version of the PCI remove()
5428 * function where we do the minimal amount of work necessary to shutdown any
5429 * further activity.
5430 */
5431static void shutdown_one(struct pci_dev *pdev)
5432{
5433 struct adapter *adapter = pci_get_drvdata(pdev);
5434
5435 /* As with remove_one() above (see extended comment), we only want do
5436 * do cleanup on PCI Devices which went all the way through init_one()
5437 * ...
5438 */
5439 if (!adapter) {
5440 pci_release_regions(pdev);
5441 return;
5442 }
5443
Ganesh Goudare1f61982017-09-21 12:50:47 +05305444 adapter->flags |= SHUTTING_DOWN;
5445
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305446 if (adapter->pf == 4) {
5447 int i;
5448
5449 for_each_port(adapter, i)
5450 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5451 cxgb_close(adapter->port[i]);
5452
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005453 if (is_uld(adapter)) {
5454 detach_ulds(adapter);
5455 t4_uld_clean_up(adapter);
5456 }
5457
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305458 disable_interrupts(adapter);
5459 disable_msi(adapter);
5460
5461 t4_sge_stop(adapter);
5462 if (adapter->flags & FW_OK)
5463 t4_fw_bye(adapter, adapter->mbox);
5464 }
5465#ifdef CONFIG_PCI_IOV
5466 else {
5467 if (adapter->port[0])
5468 unregister_netdev(adapter->port[0]);
5469 iounmap(adapter->regs);
5470 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305471 kfree(adapter->mbox_log);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305472 kfree(adapter);
5473 pci_disable_sriov(pdev);
5474 pci_release_regions(pdev);
5475 }
5476#endif
5477}
5478
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005479static struct pci_driver cxgb4_driver = {
5480 .name = KBUILD_MODNAME,
5481 .id_table = cxgb4_pci_tbl,
5482 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005483 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305484 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305485#ifdef CONFIG_PCI_IOV
5486 .sriov_configure = cxgb4_iov_configure,
5487#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005488 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005489};
5490
5491static int __init cxgb4_init_module(void)
5492{
5493 int ret;
5494
5495 /* Debugfs support is optional, just warn if this fails */
5496 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5497 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005498 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005499
5500 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005501 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005502 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305503
Anish Bhatt1bb60372014-10-14 20:07:22 -07005504#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005505 if (!inet6addr_registered) {
5506 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5507 inet6addr_registered = true;
5508 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005509#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305510
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005511 return ret;
5512}
5513
5514static void __exit cxgb4_cleanup_module(void)
5515{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005516#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305517 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005518 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5519 inet6addr_registered = false;
5520 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005521#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005522 pci_unregister_driver(&cxgb4_driver);
5523 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005524}
5525
5526module_init(cxgb4_init_module);
5527module_exit(cxgb4_cleanup_module);