blob: 3cd82aea3bb68fee300c6ef2597e51adc1e9eb04 [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);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000575 } else
576 dev_err(q->adap->pdev_dev,
577 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000578out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000579 return 0;
580}
581
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000582static void disable_msi(struct adapter *adapter)
583{
584 if (adapter->flags & USING_MSIX) {
585 pci_disable_msix(adapter->pdev);
586 adapter->flags &= ~USING_MSIX;
587 } else if (adapter->flags & USING_MSI) {
588 pci_disable_msi(adapter->pdev);
589 adapter->flags &= ~USING_MSI;
590 }
591}
592
593/*
594 * Interrupt handler for non-data events used with MSI-X.
595 */
596static irqreturn_t t4_nondata_intr(int irq, void *cookie)
597{
598 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530599 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000600
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530601 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000602 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530603 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000604 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530605 if (adap->flags & MASTER_PF)
606 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000607 return IRQ_HANDLED;
608}
609
610/*
611 * Name the MSI-X interrupts.
612 */
613static void name_msix_vecs(struct adapter *adap)
614{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000615 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000616
617 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000618 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000619
620 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000621 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
622 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000623
624 /* Ethernet queues */
625 for_each_port(adap, j) {
626 struct net_device *d = adap->port[j];
627 const struct port_info *pi = netdev_priv(d);
628
Dimitris Michailidisba278162010-12-14 21:36:50 +0000629 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000630 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
631 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000632 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000633}
634
635static int request_msix_queue_irqs(struct adapter *adap)
636{
637 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530638 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530639 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000640
641 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
642 adap->msix_info[1].desc, &s->fw_evtq);
643 if (err)
644 return err;
645
646 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000647 err = request_irq(adap->msix_info[msi_index].vec,
648 t4_sge_intr_msix, 0,
649 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000650 &s->ethrxq[ethqidx].rspq);
651 if (err)
652 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000653 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000654 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000655 return 0;
656
657unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000658 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000659 free_irq(adap->msix_info[--msi_index].vec,
660 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000661 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
662 return err;
663}
664
665static void free_msix_queue_irqs(struct adapter *adap)
666{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000667 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000668 struct sge *s = &adap->sge;
669
670 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
671 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000672 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000673}
674
675/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530676 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000677 * @pi: the port
678 * @queues: array of queue indices for RSS
679 *
680 * Sets up the portion of the HW RSS table for the port's VI to distribute
681 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530682 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000683 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530684int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000685{
686 u16 *rss;
687 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530688 struct adapter *adapter = pi->adapter;
689 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000690
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530691 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000692 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
693 if (!rss)
694 return -ENOMEM;
695
696 /* map the queue indices to queue ids */
697 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530698 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000699
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530700 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000701 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530702 /* If Tunnel All Lookup isn't specified in the global RSS
703 * Configuration, then we need to specify a default Ingress
704 * Queue for any ingress packets which aren't hashed. We'll
705 * use our first ingress queue ...
706 */
707 if (!err)
708 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
709 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
710 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
711 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
712 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
713 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
714 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000715 kfree(rss);
716 return err;
717}
718
719/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000720 * setup_rss - configure RSS
721 * @adap: the adapter
722 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000723 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000724 */
725static int setup_rss(struct adapter *adap)
726{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530727 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000728
729 for_each_port(adap, i) {
730 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000731
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530732 /* Fill default values with equal distribution */
733 for (j = 0; j < pi->rss_size; j++)
734 pi->rss[j] = j % pi->nqsets;
735
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530736 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000737 if (err)
738 return err;
739 }
740 return 0;
741}
742
743/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000744 * Return the channel of the ingress queue with the given qid.
745 */
746static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
747{
748 qid -= p->ingr_start;
749 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
750}
751
752/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000753 * Wait until all NAPI handlers are descheduled.
754 */
755static void quiesce_rx(struct adapter *adap)
756{
757 int i;
758
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530759 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000760 struct sge_rspq *q = adap->sge.ingr_map[i];
761
Eric Dumazet5226b7912017-02-02 11:44:27 -0800762 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000763 napi_disable(&q->napi);
764 }
765}
766
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530767/* Disable interrupt and napi handler */
768static void disable_interrupts(struct adapter *adap)
769{
770 if (adap->flags & FULL_INIT_DONE) {
771 t4_intr_disable(adap);
772 if (adap->flags & USING_MSIX) {
773 free_msix_queue_irqs(adap);
774 free_irq(adap->msix_info[0].vec, adap);
775 } else {
776 free_irq(adap->pdev->irq, adap);
777 }
778 quiesce_rx(adap);
779 }
780}
781
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000782/*
783 * Enable NAPI scheduling and interrupt generation for all Rx queues.
784 */
785static void enable_rx(struct adapter *adap)
786{
787 int i;
788
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530789 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000790 struct sge_rspq *q = adap->sge.ingr_map[i];
791
792 if (!q)
793 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800794 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000795 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800796
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000797 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530798 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
799 SEINTARM_V(q->intr_params) |
800 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000801 }
802}
803
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530804
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530805static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000806{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000807 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530808 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530810 bitmap_zero(s->starving_fl, s->egr_sz);
811 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000812
813 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530814 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815 else {
816 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530817 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000818 if (err)
819 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530820 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000821 }
822
823 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530824 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530825 if (err)
826 t4_free_sge_resources(adap);
827 return err;
828}
829
830/**
831 * setup_sge_queues - configure SGE Tx/Rx/response queues
832 * @adap: the adapter
833 *
834 * Determines how many sets of SGE queues to use and initializes them.
835 * We support multiple queue sets per port if we have MSI-X, otherwise
836 * just one queue set per port.
837 */
838static int setup_sge_queues(struct adapter *adap)
839{
840 int err, i, j;
841 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530842 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530843 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000844
Ganesh Goudard427cae2017-06-16 15:36:09 +0530845 if (is_uld(adap))
846 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
847
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000848 for_each_port(adap, i) {
849 struct net_device *dev = adap->port[i];
850 struct port_info *pi = netdev_priv(dev);
851 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
852 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
853
854 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530855 if (adap->msi_idx > 0)
856 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000857 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530858 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530859 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530860 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530861 t4_get_tp_ch_map(adap,
862 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000863 if (err)
864 goto freeout;
865 q->rspq.idx = j;
866 memset(&q->stats, 0, sizeof(q->stats));
867 }
868 for (j = 0; j < pi->nqsets; j++, t++) {
869 err = t4_sge_alloc_eth_txq(adap, t, dev,
870 netdev_get_tx_queue(dev, j),
871 s->fw_evtq.cntxt_id);
872 if (err)
873 goto freeout;
874 }
875 }
876
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000877 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530878 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000879 * have RDMA queues, and that's the right value.
880 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530881 if (rxq_info)
882 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
883
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000884 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530885 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000886 if (err)
887 goto freeout;
888 }
889
Atul Guptaa45695042017-07-04 16:46:20 +0530890 if (!is_t4(adap->params.chip)) {
891 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
892 netdev_get_tx_queue(adap->port[0], 0)
893 , s->fw_evtq.cntxt_id);
894 if (err)
895 goto freeout;
896 }
897
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530898 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530899 MPS_TRC_RSS_CONTROL_A :
900 MPS_T5_TRC_RSS_CONTROL_A,
901 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
902 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000903 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530904freeout:
905 t4_free_sge_resources(adap);
906 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000907}
908
Anish Bhatt688848b2014-06-19 21:37:13 -0700909static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
910 void *accel_priv, select_queue_fallback_t fallback)
911{
912 int txq;
913
914#ifdef CONFIG_CHELSIO_T4_DCB
915 /* If a Data Center Bridging has been successfully negotiated on this
916 * link then we'll use the skb's priority to map it to a TX Queue.
917 * The skb's priority is determined via the VLAN Tag Priority Code
918 * Point field.
919 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530920 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700921 u16 vlan_tci;
922 int err;
923
924 err = vlan_get_tag(skb, &vlan_tci);
925 if (unlikely(err)) {
926 if (net_ratelimit())
927 netdev_warn(dev,
928 "TX Packet without VLAN Tag on DCB Link\n");
929 txq = 0;
930 } else {
931 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530932#ifdef CONFIG_CHELSIO_T4_FCOE
933 if (skb->protocol == htons(ETH_P_FCOE))
934 txq = skb->priority & 0x7;
935#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700936 }
937 return txq;
938 }
939#endif /* CONFIG_CHELSIO_T4_DCB */
940
941 if (select_queue) {
942 txq = (skb_rx_queue_recorded(skb)
943 ? skb_get_rx_queue(skb)
944 : smp_processor_id());
945
946 while (unlikely(txq >= dev->real_num_tx_queues))
947 txq -= dev->real_num_tx_queues;
948
949 return txq;
950 }
951
952 return fallback(dev, skb) % dev->real_num_tx_queues;
953}
954
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000955static int closest_timer(const struct sge *s, int time)
956{
957 int i, delta, match = 0, min_delta = INT_MAX;
958
959 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
960 delta = time - s->timer_val[i];
961 if (delta < 0)
962 delta = -delta;
963 if (delta < min_delta) {
964 min_delta = delta;
965 match = i;
966 }
967 }
968 return match;
969}
970
971static int closest_thres(const struct sge *s, int thres)
972{
973 int i, delta, match = 0, min_delta = INT_MAX;
974
975 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
976 delta = thres - s->counter_val[i];
977 if (delta < 0)
978 delta = -delta;
979 if (delta < min_delta) {
980 min_delta = delta;
981 match = i;
982 }
983 }
984 return match;
985}
986
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000987/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530988 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000989 * @q: the Rx queue
990 * @us: the hold-off time in us, or 0 to disable timer
991 * @cnt: the hold-off packet count, or 0 to disable counter
992 *
993 * Sets an Rx queue's interrupt hold-off time and packet count. At least
994 * one of the two needs to be enabled for the queue to generate interrupts.
995 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530996int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
997 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000998{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +0530999 struct adapter *adap = q->adap;
1000
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001001 if ((us | cnt) == 0)
1002 cnt = 1;
1003
1004 if (cnt) {
1005 int err;
1006 u32 v, new_idx;
1007
1008 new_idx = closest_thres(&adap->sge, cnt);
1009 if (q->desc && q->pktcnt_idx != new_idx) {
1010 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301011 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1012 FW_PARAMS_PARAM_X_V(
1013 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1014 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301015 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1016 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001017 if (err)
1018 return err;
1019 }
1020 q->pktcnt_idx = new_idx;
1021 }
1022
1023 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301024 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001025 return 0;
1026}
1027
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001028static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001029{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001030 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001031 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001032 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001033
Patrick McHardyf6469682013-04-19 02:04:27 +00001034 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001035 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001036
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301037 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001038 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001039 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001040 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001041 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001042 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001043}
1044
Bill Pemberton91744942012-12-03 09:23:02 -05001045static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001046{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001047 if (IS_ERR_OR_NULL(adap->debugfs_root))
1048 return -1;
1049
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301050#ifdef CONFIG_DEBUG_FS
1051 t4_setup_debugfs(adap);
1052#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001053 return 0;
1054}
1055
1056/*
1057 * upper-layer driver support
1058 */
1059
1060/*
1061 * Allocate an active-open TID and set it to the supplied value.
1062 */
1063int cxgb4_alloc_atid(struct tid_info *t, void *data)
1064{
1065 int atid = -1;
1066
1067 spin_lock_bh(&t->atid_lock);
1068 if (t->afree) {
1069 union aopen_entry *p = t->afree;
1070
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001071 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001072 t->afree = p->next;
1073 p->data = data;
1074 t->atids_in_use++;
1075 }
1076 spin_unlock_bh(&t->atid_lock);
1077 return atid;
1078}
1079EXPORT_SYMBOL(cxgb4_alloc_atid);
1080
1081/*
1082 * Release an active-open TID.
1083 */
1084void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1085{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001086 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001087
1088 spin_lock_bh(&t->atid_lock);
1089 p->next = t->afree;
1090 t->afree = p;
1091 t->atids_in_use--;
1092 spin_unlock_bh(&t->atid_lock);
1093}
1094EXPORT_SYMBOL(cxgb4_free_atid);
1095
1096/*
1097 * Allocate a server TID and set it to the supplied value.
1098 */
1099int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1100{
1101 int stid;
1102
1103 spin_lock_bh(&t->stid_lock);
1104 if (family == PF_INET) {
1105 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1106 if (stid < t->nstids)
1107 __set_bit(stid, t->stid_bmap);
1108 else
1109 stid = -1;
1110 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301111 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001112 if (stid < 0)
1113 stid = -1;
1114 }
1115 if (stid >= 0) {
1116 t->stid_tab[stid].data = data;
1117 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301118 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1119 * This is equivalent to 4 TIDs. With CLIP enabled it
1120 * needs 2 TIDs.
1121 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301122 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301123 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301124 t->v6_stids_in_use += 2;
1125 } else {
1126 t->stids_in_use++;
1127 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001128 }
1129 spin_unlock_bh(&t->stid_lock);
1130 return stid;
1131}
1132EXPORT_SYMBOL(cxgb4_alloc_stid);
1133
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001134/* Allocate a server filter TID and set it to the supplied value.
1135 */
1136int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1137{
1138 int stid;
1139
1140 spin_lock_bh(&t->stid_lock);
1141 if (family == PF_INET) {
1142 stid = find_next_zero_bit(t->stid_bmap,
1143 t->nstids + t->nsftids, t->nstids);
1144 if (stid < (t->nstids + t->nsftids))
1145 __set_bit(stid, t->stid_bmap);
1146 else
1147 stid = -1;
1148 } else {
1149 stid = -1;
1150 }
1151 if (stid >= 0) {
1152 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301153 stid -= t->nstids;
1154 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301155 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001156 }
1157 spin_unlock_bh(&t->stid_lock);
1158 return stid;
1159}
1160EXPORT_SYMBOL(cxgb4_alloc_sftid);
1161
1162/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001163 */
1164void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1165{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301166 /* Is it a server filter TID? */
1167 if (t->nsftids && (stid >= t->sftid_base)) {
1168 stid -= t->sftid_base;
1169 stid += t->nstids;
1170 } else {
1171 stid -= t->stid_base;
1172 }
1173
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001174 spin_lock_bh(&t->stid_lock);
1175 if (family == PF_INET)
1176 __clear_bit(stid, t->stid_bmap);
1177 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301178 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001179 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301180 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301181 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301182 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301183 t->v6_stids_in_use -= 2;
1184 } else {
1185 t->stids_in_use--;
1186 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301187 } else {
1188 t->sftids_in_use--;
1189 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301190
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001191 spin_unlock_bh(&t->stid_lock);
1192}
1193EXPORT_SYMBOL(cxgb4_free_stid);
1194
1195/*
1196 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1197 */
1198static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1199 unsigned int tid)
1200{
1201 struct cpl_tid_release *req;
1202
1203 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001204 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001205 INIT_TP_WR(req, tid);
1206 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1207}
1208
1209/*
1210 * Queue a TID release request and if necessary schedule a work queue to
1211 * process it.
1212 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001213static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1214 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001215{
1216 void **p = &t->tid_tab[tid];
1217 struct adapter *adap = container_of(t, struct adapter, tids);
1218
1219 spin_lock_bh(&adap->tid_release_lock);
1220 *p = adap->tid_release_head;
1221 /* Low 2 bits encode the Tx channel number */
1222 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1223 if (!adap->tid_release_task_busy) {
1224 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001225 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001226 }
1227 spin_unlock_bh(&adap->tid_release_lock);
1228}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001229
1230/*
1231 * Process the list of pending TID release requests.
1232 */
1233static void process_tid_release_list(struct work_struct *work)
1234{
1235 struct sk_buff *skb;
1236 struct adapter *adap;
1237
1238 adap = container_of(work, struct adapter, tid_release_task);
1239
1240 spin_lock_bh(&adap->tid_release_lock);
1241 while (adap->tid_release_head) {
1242 void **p = adap->tid_release_head;
1243 unsigned int chan = (uintptr_t)p & 3;
1244 p = (void *)p - chan;
1245
1246 adap->tid_release_head = *p;
1247 *p = NULL;
1248 spin_unlock_bh(&adap->tid_release_lock);
1249
1250 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1251 GFP_KERNEL)))
1252 schedule_timeout_uninterruptible(1);
1253
1254 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1255 t4_ofld_send(adap, skb);
1256 spin_lock_bh(&adap->tid_release_lock);
1257 }
1258 adap->tid_release_task_busy = false;
1259 spin_unlock_bh(&adap->tid_release_lock);
1260}
1261
1262/*
1263 * Release a TID and inform HW. If we are unable to allocate the release
1264 * message we defer to a work queue.
1265 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301266void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1267 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001268{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001269 struct sk_buff *skb;
1270 struct adapter *adap = container_of(t, struct adapter, tids);
1271
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301272 WARN_ON(tid >= t->ntids);
1273
1274 if (t->tid_tab[tid]) {
1275 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301276 atomic_dec(&t->conns_in_use);
1277 if (t->hash_base && (tid >= t->hash_base)) {
1278 if (family == AF_INET6)
1279 atomic_sub(2, &t->hash_tids_in_use);
1280 else
1281 atomic_dec(&t->hash_tids_in_use);
1282 } else {
1283 if (family == AF_INET6)
1284 atomic_sub(2, &t->tids_in_use);
1285 else
1286 atomic_dec(&t->tids_in_use);
1287 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301288 }
1289
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001290 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1291 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001292 mk_tid_release(skb, chan, tid);
1293 t4_ofld_send(adap, skb);
1294 } else
1295 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001296}
1297EXPORT_SYMBOL(cxgb4_remove_tid);
1298
1299/*
1300 * Allocate and initialize the TID tables. Returns 0 on success.
1301 */
1302static int tid_init(struct tid_info *t)
1303{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301304 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301305 unsigned int max_ftids = t->nftids + t->nsftids;
1306 unsigned int natids = t->natids;
1307 unsigned int stid_bmap_size;
1308 unsigned int ftid_bmap_size;
1309 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001310
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001311 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301312 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001313 size = t->ntids * sizeof(*t->tid_tab) +
1314 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001315 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001316 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001317 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301318 max_ftids * sizeof(*t->ftid_tab) +
1319 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001320
Michal Hocko752ade62017-05-08 15:57:27 -07001321 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001322 if (!t->tid_tab)
1323 return -ENOMEM;
1324
1325 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1326 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001327 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001328 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301329 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001330 spin_lock_init(&t->stid_lock);
1331 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301332 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001333
1334 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301335 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301336 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001337 t->afree = NULL;
1338 t->atids_in_use = 0;
1339 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301340 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301341 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001342
1343 /* Setup the free list for atid_tab and clear the stid bitmap. */
1344 if (natids) {
1345 while (--natids)
1346 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1347 t->afree = t->atid_tab;
1348 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301349
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301350 if (is_offload(adap)) {
1351 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1352 /* Reserve stid 0 for T4/T5 adapters */
1353 if (!t->stid_base &&
1354 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1355 __set_bit(0, t->stid_bmap);
1356 }
1357
1358 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001359 return 0;
1360}
1361
1362/**
1363 * cxgb4_create_server - create an IP server
1364 * @dev: the device
1365 * @stid: the server TID
1366 * @sip: local IP address to bind server to
1367 * @sport: the server's TCP port
1368 * @queue: queue to direct messages from this server to
1369 *
1370 * Create an IP server for the given port and address.
1371 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1372 */
1373int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001374 __be32 sip, __be16 sport, __be16 vlan,
1375 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001376{
1377 unsigned int chan;
1378 struct sk_buff *skb;
1379 struct adapter *adap;
1380 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301381 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001382
1383 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1384 if (!skb)
1385 return -ENOMEM;
1386
1387 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001388 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001389 INIT_TP_WR(req, 0);
1390 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1391 req->local_port = sport;
1392 req->peer_port = htons(0);
1393 req->local_ip = sip;
1394 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001395 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001396 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001397 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1398 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301399 ret = t4_mgmt_tx(adap, skb);
1400 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001401}
1402EXPORT_SYMBOL(cxgb4_create_server);
1403
Vipul Pandya80f40c12013-07-04 16:10:45 +05301404/* cxgb4_create_server6 - create an IPv6 server
1405 * @dev: the device
1406 * @stid: the server TID
1407 * @sip: local IPv6 address to bind server to
1408 * @sport: the server's TCP port
1409 * @queue: queue to direct messages from this server to
1410 *
1411 * Create an IPv6 server for the given port and address.
1412 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1413 */
1414int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1415 const struct in6_addr *sip, __be16 sport,
1416 unsigned int queue)
1417{
1418 unsigned int chan;
1419 struct sk_buff *skb;
1420 struct adapter *adap;
1421 struct cpl_pass_open_req6 *req;
1422 int ret;
1423
1424 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1425 if (!skb)
1426 return -ENOMEM;
1427
1428 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001429 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301430 INIT_TP_WR(req, 0);
1431 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1432 req->local_port = sport;
1433 req->peer_port = htons(0);
1434 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1435 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1436 req->peer_ip_hi = cpu_to_be64(0);
1437 req->peer_ip_lo = cpu_to_be64(0);
1438 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001439 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001440 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1441 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301442 ret = t4_mgmt_tx(adap, skb);
1443 return net_xmit_eval(ret);
1444}
1445EXPORT_SYMBOL(cxgb4_create_server6);
1446
1447int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1448 unsigned int queue, bool ipv6)
1449{
1450 struct sk_buff *skb;
1451 struct adapter *adap;
1452 struct cpl_close_listsvr_req *req;
1453 int ret;
1454
1455 adap = netdev2adap(dev);
1456
1457 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1458 if (!skb)
1459 return -ENOMEM;
1460
Johannes Berg4df864c2017-06-16 14:29:21 +02001461 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301462 INIT_TP_WR(req, 0);
1463 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001464 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1465 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301466 ret = t4_mgmt_tx(adap, skb);
1467 return net_xmit_eval(ret);
1468}
1469EXPORT_SYMBOL(cxgb4_remove_server);
1470
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001471/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001472 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1473 * @mtus: the HW MTU table
1474 * @mtu: the target MTU
1475 * @idx: index of selected entry in the MTU table
1476 *
1477 * Returns the index and the value in the HW MTU table that is closest to
1478 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1479 * table, in which case that smallest available value is selected.
1480 */
1481unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1482 unsigned int *idx)
1483{
1484 unsigned int i = 0;
1485
1486 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1487 ++i;
1488 if (idx)
1489 *idx = i;
1490 return mtus[i];
1491}
1492EXPORT_SYMBOL(cxgb4_best_mtu);
1493
1494/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301495 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1496 * @mtus: the HW MTU table
1497 * @header_size: Header Size
1498 * @data_size_max: maximum Data Segment Size
1499 * @data_size_align: desired Data Segment Size Alignment (2^N)
1500 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1501 *
1502 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1503 * MTU Table based solely on a Maximum MTU parameter, we break that
1504 * parameter up into a Header Size and Maximum Data Segment Size, and
1505 * provide a desired Data Segment Size Alignment. If we find an MTU in
1506 * the Hardware MTU Table which will result in a Data Segment Size with
1507 * the requested alignment _and_ that MTU isn't "too far" from the
1508 * closest MTU, then we'll return that rather than the closest MTU.
1509 */
1510unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1511 unsigned short header_size,
1512 unsigned short data_size_max,
1513 unsigned short data_size_align,
1514 unsigned int *mtu_idxp)
1515{
1516 unsigned short max_mtu = header_size + data_size_max;
1517 unsigned short data_size_align_mask = data_size_align - 1;
1518 int mtu_idx, aligned_mtu_idx;
1519
1520 /* Scan the MTU Table till we find an MTU which is larger than our
1521 * Maximum MTU or we reach the end of the table. Along the way,
1522 * record the last MTU found, if any, which will result in a Data
1523 * Segment Length matching the requested alignment.
1524 */
1525 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1526 unsigned short data_size = mtus[mtu_idx] - header_size;
1527
1528 /* If this MTU minus the Header Size would result in a
1529 * Data Segment Size of the desired alignment, remember it.
1530 */
1531 if ((data_size & data_size_align_mask) == 0)
1532 aligned_mtu_idx = mtu_idx;
1533
1534 /* If we're not at the end of the Hardware MTU Table and the
1535 * next element is larger than our Maximum MTU, drop out of
1536 * the loop.
1537 */
1538 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1539 break;
1540 }
1541
1542 /* If we fell out of the loop because we ran to the end of the table,
1543 * then we just have to use the last [largest] entry.
1544 */
1545 if (mtu_idx == NMTUS)
1546 mtu_idx--;
1547
1548 /* If we found an MTU which resulted in the requested Data Segment
1549 * Length alignment and that's "not far" from the largest MTU which is
1550 * less than or equal to the maximum MTU, then use that.
1551 */
1552 if (aligned_mtu_idx >= 0 &&
1553 mtu_idx - aligned_mtu_idx <= 1)
1554 mtu_idx = aligned_mtu_idx;
1555
1556 /* If the caller has passed in an MTU Index pointer, pass the
1557 * MTU Index back. Return the MTU value.
1558 */
1559 if (mtu_idxp)
1560 *mtu_idxp = mtu_idx;
1561 return mtus[mtu_idx];
1562}
1563EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1564
1565/**
Hariprasad S27999802015-09-23 17:19:26 +05301566 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1567 * @chip: chip type
1568 * @viid: VI id of the given port
1569 *
1570 * Return the SMT index for this VI.
1571 */
1572unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1573{
1574 /* In T4/T5, SMT contains 256 SMAC entries organized in
1575 * 128 rows of 2 entries each.
1576 * In T6, SMT contains 256 SMAC entries in 256 rows.
1577 * TODO: The below code needs to be updated when we add support
1578 * for 256 VFs.
1579 */
1580 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1581 return ((viid & 0x7f) << 1);
1582 else
1583 return (viid & 0x7f);
1584}
1585EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1586
1587/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001588 * cxgb4_port_chan - get the HW channel of a port
1589 * @dev: the net device for the port
1590 *
1591 * Return the HW Tx channel of the given port.
1592 */
1593unsigned int cxgb4_port_chan(const struct net_device *dev)
1594{
1595 return netdev2pinfo(dev)->tx_chan;
1596}
1597EXPORT_SYMBOL(cxgb4_port_chan);
1598
Vipul Pandya881806b2012-05-18 15:29:24 +05301599unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1600{
1601 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001602 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301603
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301604 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1605 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301606 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301607 lp_count = LP_COUNT_G(v1);
1608 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001609 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301610 lp_count = LP_COUNT_T5_G(v1);
1611 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001612 }
1613 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301614}
1615EXPORT_SYMBOL(cxgb4_dbfifo_count);
1616
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001617/**
1618 * cxgb4_port_viid - get the VI id of a port
1619 * @dev: the net device for the port
1620 *
1621 * Return the VI id of the given port.
1622 */
1623unsigned int cxgb4_port_viid(const struct net_device *dev)
1624{
1625 return netdev2pinfo(dev)->viid;
1626}
1627EXPORT_SYMBOL(cxgb4_port_viid);
1628
1629/**
1630 * cxgb4_port_idx - get the index of a port
1631 * @dev: the net device for the port
1632 *
1633 * Return the index of the given port.
1634 */
1635unsigned int cxgb4_port_idx(const struct net_device *dev)
1636{
1637 return netdev2pinfo(dev)->port_id;
1638}
1639EXPORT_SYMBOL(cxgb4_port_idx);
1640
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001641void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1642 struct tp_tcp_stats *v6)
1643{
1644 struct adapter *adap = pci_get_drvdata(pdev);
1645
1646 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301647 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001648 spin_unlock(&adap->stats_lock);
1649}
1650EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1651
1652void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1653 const unsigned int *pgsz_order)
1654{
1655 struct adapter *adap = netdev2adap(dev);
1656
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301657 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1658 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1659 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1660 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001661}
1662EXPORT_SYMBOL(cxgb4_iscsi_init);
1663
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301664int cxgb4_flush_eq_cache(struct net_device *dev)
1665{
1666 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301667
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301668 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301669}
1670EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1671
1672static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1673{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301674 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301675 __be64 indices;
1676 int ret;
1677
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301678 spin_lock(&adap->win0_lock);
1679 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1680 sizeof(indices), (__be32 *)&indices,
1681 T4_MEMORY_READ);
1682 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301683 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001684 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1685 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301686 }
1687 return ret;
1688}
1689
1690int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1691 u16 size)
1692{
1693 struct adapter *adap = netdev2adap(dev);
1694 u16 hw_pidx, hw_cidx;
1695 int ret;
1696
1697 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1698 if (ret)
1699 goto out;
1700
1701 if (pidx != hw_pidx) {
1702 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301703 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301704
1705 if (pidx >= hw_pidx)
1706 delta = pidx - hw_pidx;
1707 else
1708 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301709
1710 if (is_t4(adap->params.chip))
1711 val = PIDX_V(delta);
1712 else
1713 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301714 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301715 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1716 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301717 }
1718out:
1719 return ret;
1720}
1721EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1722
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301723int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1724{
1725 struct adapter *adap;
1726 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301727 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301728 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1729 int ret;
1730
1731 adap = netdev2adap(dev);
1732
1733 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1734
1735 /* Figure out where the offset lands in the Memory Type/Address scheme.
1736 * This code assumes that the memory is laid out starting at offset 0
1737 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1738 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1739 * MC0, and some have both MC0 and MC1.
1740 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301741 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1742 edc0_size = EDRAM0_SIZE_G(size) << 20;
1743 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1744 edc1_size = EDRAM1_SIZE_G(size) << 20;
1745 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1746 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301747
1748 edc0_end = edc0_size;
1749 edc1_end = edc0_end + edc1_size;
1750 mc0_end = edc1_end + mc0_size;
1751
1752 if (offset < edc0_end) {
1753 memtype = MEM_EDC0;
1754 memaddr = offset;
1755 } else if (offset < edc1_end) {
1756 memtype = MEM_EDC1;
1757 memaddr = offset - edc0_end;
1758 } else {
1759 if (offset < mc0_end) {
1760 memtype = MEM_MC0;
1761 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301762 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301763 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1764 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301765 mc1_end = mc0_end + mc1_size;
1766 if (offset < mc1_end) {
1767 memtype = MEM_MC1;
1768 memaddr = offset - mc0_end;
1769 } else {
1770 /* offset beyond the end of any memory */
1771 goto err;
1772 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301773 } else {
1774 /* T4/T6 only has a single memory channel */
1775 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301776 }
1777 }
1778
1779 spin_lock(&adap->win0_lock);
1780 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1781 spin_unlock(&adap->win0_lock);
1782 return ret;
1783
1784err:
1785 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1786 stag, offset);
1787 return -EINVAL;
1788}
1789EXPORT_SYMBOL(cxgb4_read_tpte);
1790
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301791u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1792{
1793 u32 hi, lo;
1794 struct adapter *adap;
1795
1796 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301797 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1798 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301799
1800 return ((u64)hi << 32) | (u64)lo;
1801}
1802EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1803
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301804int cxgb4_bar2_sge_qregs(struct net_device *dev,
1805 unsigned int qid,
1806 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301807 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301808 u64 *pbar2_qoffset,
1809 unsigned int *pbar2_qid)
1810{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301811 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301812 qid,
1813 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1814 ? T4_BAR2_QTYPE_EGRESS
1815 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301816 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301817 pbar2_qoffset,
1818 pbar2_qid);
1819}
1820EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1821
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001822static struct pci_driver cxgb4_driver;
1823
1824static void check_neigh_update(struct neighbour *neigh)
1825{
1826 const struct device *parent;
1827 const struct net_device *netdev = neigh->dev;
1828
Parav Panditd0d7b102017-02-04 11:00:49 -06001829 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001830 netdev = vlan_dev_real_dev(netdev);
1831 parent = netdev->dev.parent;
1832 if (parent && parent->driver == &cxgb4_driver.driver)
1833 t4_l2t_update(dev_get_drvdata(parent), neigh);
1834}
1835
1836static int netevent_cb(struct notifier_block *nb, unsigned long event,
1837 void *data)
1838{
1839 switch (event) {
1840 case NETEVENT_NEIGH_UPDATE:
1841 check_neigh_update(data);
1842 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001843 case NETEVENT_REDIRECT:
1844 default:
1845 break;
1846 }
1847 return 0;
1848}
1849
1850static bool netevent_registered;
1851static struct notifier_block cxgb4_netevent_nb = {
1852 .notifier_call = netevent_cb
1853};
1854
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301855static void drain_db_fifo(struct adapter *adap, int usecs)
1856{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001857 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301858
1859 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301860 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1861 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301862 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301863 lp_count = LP_COUNT_G(v1);
1864 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001865 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301866 lp_count = LP_COUNT_T5_G(v1);
1867 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001868 }
1869
1870 if (lp_count == 0 && hp_count == 0)
1871 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301872 set_current_state(TASK_UNINTERRUPTIBLE);
1873 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301874 } while (1);
1875}
1876
1877static void disable_txq_db(struct sge_txq *q)
1878{
Steve Wise05eb2382014-03-14 21:52:08 +05301879 unsigned long flags;
1880
1881 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301882 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301883 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301884}
1885
Steve Wise05eb2382014-03-14 21:52:08 +05301886static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301887{
1888 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301889 if (q->db_pidx_inc) {
1890 /* Make sure that all writes to the TX descriptors
1891 * are committed before we tell HW about them.
1892 */
1893 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301894 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1895 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301896 q->db_pidx_inc = 0;
1897 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301898 q->db_disabled = 0;
1899 spin_unlock_irq(&q->db_lock);
1900}
1901
1902static void disable_dbs(struct adapter *adap)
1903{
1904 int i;
1905
1906 for_each_ethrxq(&adap->sge, i)
1907 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301908 if (is_offload(adap)) {
1909 struct sge_uld_txq_info *txq_info =
1910 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1911
1912 if (txq_info) {
1913 for_each_ofldtxq(&adap->sge, i) {
1914 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1915
1916 disable_txq_db(&txq->q);
1917 }
1918 }
1919 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301920 for_each_port(adap, i)
1921 disable_txq_db(&adap->sge.ctrlq[i].q);
1922}
1923
1924static void enable_dbs(struct adapter *adap)
1925{
1926 int i;
1927
1928 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301929 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301930 if (is_offload(adap)) {
1931 struct sge_uld_txq_info *txq_info =
1932 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1933
1934 if (txq_info) {
1935 for_each_ofldtxq(&adap->sge, i) {
1936 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1937
1938 enable_txq_db(adap, &txq->q);
1939 }
1940 }
1941 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301942 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301943 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1944}
1945
1946static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1947{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301948 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1949
1950 if (adap->uld && adap->uld[type].handle)
1951 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301952}
1953
1954static void process_db_full(struct work_struct *work)
1955{
1956 struct adapter *adap;
1957
1958 adap = container_of(work, struct adapter, db_full_task);
1959
1960 drain_db_fifo(adap, dbfifo_drain_delay);
1961 enable_dbs(adap);
1962 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301963 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1964 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1965 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1966 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1967 else
1968 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1969 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301970}
1971
1972static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1973{
1974 u16 hw_pidx, hw_cidx;
1975 int ret;
1976
Steve Wise05eb2382014-03-14 21:52:08 +05301977 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301978 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1979 if (ret)
1980 goto out;
1981 if (q->db_pidx != hw_pidx) {
1982 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301983 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301984
1985 if (q->db_pidx >= hw_pidx)
1986 delta = q->db_pidx - hw_pidx;
1987 else
1988 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301989
1990 if (is_t4(adap->params.chip))
1991 val = PIDX_V(delta);
1992 else
1993 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301994 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301995 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1996 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301997 }
1998out:
1999 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302000 q->db_pidx_inc = 0;
2001 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302002 if (ret)
2003 CH_WARN(adap, "DB drop recovery failed.\n");
2004}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302005
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302006static void recover_all_queues(struct adapter *adap)
2007{
2008 int i;
2009
2010 for_each_ethrxq(&adap->sge, i)
2011 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302012 if (is_offload(adap)) {
2013 struct sge_uld_txq_info *txq_info =
2014 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2015 if (txq_info) {
2016 for_each_ofldtxq(&adap->sge, i) {
2017 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2018
2019 sync_txq_pidx(adap, &txq->q);
2020 }
2021 }
2022 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302023 for_each_port(adap, i)
2024 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2025}
2026
Vipul Pandya881806b2012-05-18 15:29:24 +05302027static void process_db_drop(struct work_struct *work)
2028{
2029 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302030
Vipul Pandya881806b2012-05-18 15:29:24 +05302031 adap = container_of(work, struct adapter, db_drop_task);
2032
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302033 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302034 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002035 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302036 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002037 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302038 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002039 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302040 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302041 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002042 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2043 u16 qid = (dropped_db >> 15) & 0x1ffff;
2044 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302045 u64 bar2_qoffset;
2046 unsigned int bar2_qid;
2047 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002048
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302049 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002050 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302051 if (ret)
2052 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2053 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2054 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302055 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302056 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002057
2058 /* Re-enable BAR2 WC */
2059 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2060 }
2061
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302062 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2063 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302064}
2065
2066void t4_db_full(struct adapter *adap)
2067{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302068 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302069 disable_dbs(adap);
2070 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302071 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2072 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002073 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002074 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302075}
2076
2077void t4_db_dropped(struct adapter *adap)
2078{
Steve Wise05eb2382014-03-14 21:52:08 +05302079 if (is_t4(adap->params.chip)) {
2080 disable_dbs(adap);
2081 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2082 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002083 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302084}
2085
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302086void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002087{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002088 if (!netevent_registered) {
2089 register_netevent_notifier(&cxgb4_netevent_nb);
2090 netevent_registered = true;
2091 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002092}
2093
2094static void detach_ulds(struct adapter *adap)
2095{
2096 unsigned int i;
2097
2098 mutex_lock(&uld_mutex);
2099 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002100
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002101 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002102 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302103 adap->uld[i].state_change(adap->uld[i].handle,
2104 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002105
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002106 if (netevent_registered && list_empty(&adapter_list)) {
2107 unregister_netevent_notifier(&cxgb4_netevent_nb);
2108 netevent_registered = false;
2109 }
2110 mutex_unlock(&uld_mutex);
2111}
2112
2113static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2114{
2115 unsigned int i;
2116
2117 mutex_lock(&uld_mutex);
2118 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302119 if (adap->uld && adap->uld[i].handle)
2120 adap->uld[i].state_change(adap->uld[i].handle,
2121 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002122 mutex_unlock(&uld_mutex);
2123}
2124
Anish Bhatt1bb60372014-10-14 20:07:22 -07002125#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002126static int cxgb4_inet6addr_handler(struct notifier_block *this,
2127 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302128{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002129 struct inet6_ifaddr *ifa = data;
2130 struct net_device *event_dev = ifa->idev->dev;
2131 const struct device *parent = NULL;
2132#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302133 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002134#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002135 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002136 event_dev = vlan_dev_real_dev(event_dev);
2137#if IS_ENABLED(CONFIG_BONDING)
2138 if (event_dev->flags & IFF_MASTER) {
2139 list_for_each_entry(adap, &adapter_list, list_node) {
2140 switch (event) {
2141 case NETDEV_UP:
2142 cxgb4_clip_get(adap->port[0],
2143 (const u32 *)ifa, 1);
2144 break;
2145 case NETDEV_DOWN:
2146 cxgb4_clip_release(adap->port[0],
2147 (const u32 *)ifa, 1);
2148 break;
2149 default:
2150 break;
2151 }
2152 }
2153 return NOTIFY_OK;
2154 }
2155#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302156
Anish Bhattb5a02f52015-01-14 15:17:34 -08002157 if (event_dev)
2158 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302159
Anish Bhattb5a02f52015-01-14 15:17:34 -08002160 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302161 switch (event) {
2162 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002163 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302164 break;
2165 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002166 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302167 break;
2168 default:
2169 break;
2170 }
2171 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002172 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302173}
2174
Anish Bhattb5a02f52015-01-14 15:17:34 -08002175static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302176static struct notifier_block cxgb4_inet6addr_notifier = {
2177 .notifier_call = cxgb4_inet6addr_handler
2178};
2179
Vipul Pandya01bcca62013-07-04 16:10:46 +05302180static void update_clip(const struct adapter *adap)
2181{
2182 int i;
2183 struct net_device *dev;
2184 int ret;
2185
2186 rcu_read_lock();
2187
2188 for (i = 0; i < MAX_NPORTS; i++) {
2189 dev = adap->port[i];
2190 ret = 0;
2191
2192 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002193 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302194
2195 if (ret < 0)
2196 break;
2197 }
2198 rcu_read_unlock();
2199}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002200#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302201
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002202/**
2203 * cxgb_up - enable the adapter
2204 * @adap: adapter being enabled
2205 *
2206 * Called when the first port is enabled, this function performs the
2207 * actions necessary to make an adapter operational, such as completing
2208 * the initialization of HW modules, and enabling interrupts.
2209 *
2210 * Must be called with the rtnl lock held.
2211 */
2212static int cxgb_up(struct adapter *adap)
2213{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002214 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002215
Raju Rangoju91060382017-06-19 17:40:48 +05302216 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002217 err = setup_sge_queues(adap);
2218 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302219 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002220 err = setup_rss(adap);
2221 if (err)
2222 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002223
2224 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002225 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002226 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2227 adap->msix_info[0].desc, adap);
2228 if (err)
2229 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002230 err = request_msix_queue_irqs(adap);
2231 if (err) {
2232 free_irq(adap->msix_info[0].vec, adap);
2233 goto irq_err;
2234 }
2235 } else {
2236 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2237 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002238 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002239 if (err)
2240 goto irq_err;
2241 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302242
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002243 enable_rx(adap);
2244 t4_sge_start(adap);
2245 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002246 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302247 mutex_unlock(&uld_mutex);
2248
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002249 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002250#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302251 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002252#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302253 /* Initialize hash mac addr list*/
2254 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002255 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302256
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002257 irq_err:
2258 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002259 freeq:
2260 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302261 rel_lock:
2262 mutex_unlock(&uld_mutex);
2263 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002264}
2265
2266static void cxgb_down(struct adapter *adapter)
2267{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002268 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302269 cancel_work_sync(&adapter->db_full_task);
2270 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002271 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002272 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002273
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002274 t4_sge_stop(adapter);
2275 t4_free_sge_resources(adapter);
2276 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002277}
2278
2279/*
2280 * net_device operations
2281 */
2282static int cxgb_open(struct net_device *dev)
2283{
2284 int err;
2285 struct port_info *pi = netdev_priv(dev);
2286 struct adapter *adapter = pi->adapter;
2287
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002288 netif_carrier_off(dev);
2289
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002290 if (!(adapter->flags & FULL_INIT_DONE)) {
2291 err = cxgb_up(adapter);
2292 if (err < 0)
2293 return err;
2294 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002295
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302296 /* It's possible that the basic port information could have
2297 * changed since we first read it.
2298 */
2299 err = t4_update_port_info(pi);
2300 if (err < 0)
2301 return err;
2302
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002303 err = link_start(dev);
2304 if (!err)
2305 netif_tx_start_all_queues(dev);
2306 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002307}
2308
2309static int cxgb_close(struct net_device *dev)
2310{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002311 struct port_info *pi = netdev_priv(dev);
2312 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302313 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002314
2315 netif_tx_stop_all_queues(dev);
2316 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302317 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2318#ifdef CONFIG_CHELSIO_T4_DCB
2319 cxgb4_dcb_reset(dev);
2320 dcb_tx_queue_prio_enable(dev, false);
2321#endif
2322 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002323}
2324
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002325int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002326 __be32 sip, __be16 sport, __be16 vlan,
2327 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002328{
2329 int ret;
2330 struct filter_entry *f;
2331 struct adapter *adap;
2332 int i;
2333 u8 *val;
2334
2335 adap = netdev2adap(dev);
2336
Vipul Pandya1cab7752012-12-10 09:30:55 +00002337 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302338 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002339 stid += adap->tids.nftids;
2340
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002341 /* Check to make sure the filter requested is writable ...
2342 */
2343 f = &adap->tids.ftid_tab[stid];
2344 ret = writable_filter(f);
2345 if (ret)
2346 return ret;
2347
2348 /* Clear out any old resources being used by the filter before
2349 * we start constructing the new filter.
2350 */
2351 if (f->valid)
2352 clear_filter(adap, f);
2353
2354 /* Clear out filter specifications */
2355 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2356 f->fs.val.lport = cpu_to_be16(sport);
2357 f->fs.mask.lport = ~0;
2358 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002359 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002360 for (i = 0; i < 4; i++) {
2361 f->fs.val.lip[i] = val[i];
2362 f->fs.mask.lip[i] = ~0;
2363 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302364 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002365 f->fs.val.iport = port;
2366 f->fs.mask.iport = mask;
2367 }
2368 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002369
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302370 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302371 f->fs.val.proto = IPPROTO_TCP;
2372 f->fs.mask.proto = ~0;
2373 }
2374
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002375 f->fs.dirsteer = 1;
2376 f->fs.iq = queue;
2377 /* Mark filter as locked */
2378 f->locked = 1;
2379 f->fs.rpttid = 1;
2380
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302381 /* Save the actual tid. We need this to get the corresponding
2382 * filter entry structure in filter_rpl.
2383 */
2384 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002385 ret = set_filter_wr(adap, stid);
2386 if (ret) {
2387 clear_filter(adap, f);
2388 return ret;
2389 }
2390
2391 return 0;
2392}
2393EXPORT_SYMBOL(cxgb4_create_server_filter);
2394
2395int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2396 unsigned int queue, bool ipv6)
2397{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002398 struct filter_entry *f;
2399 struct adapter *adap;
2400
2401 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002402
2403 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302404 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002405 stid += adap->tids.nftids;
2406
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002407 f = &adap->tids.ftid_tab[stid];
2408 /* Unlock the filter */
2409 f->locked = 0;
2410
Wei Yongjun8c148462016-08-20 15:32:41 +00002411 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002412}
2413EXPORT_SYMBOL(cxgb4_remove_server_filter);
2414
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002415static void cxgb_get_stats(struct net_device *dev,
2416 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002417{
2418 struct port_stats stats;
2419 struct port_info *p = netdev_priv(dev);
2420 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002421
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002422 /* Block retrieving statistics during EEH error
2423 * recovery. Otherwise, the recovery might fail
2424 * and the PCI device will be removed permanently
2425 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002426 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002427 if (!netif_device_present(dev)) {
2428 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002429 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002430 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302431 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2432 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002433 spin_unlock(&adapter->stats_lock);
2434
2435 ns->tx_bytes = stats.tx_octets;
2436 ns->tx_packets = stats.tx_frames;
2437 ns->rx_bytes = stats.rx_octets;
2438 ns->rx_packets = stats.rx_frames;
2439 ns->multicast = stats.rx_mcast_frames;
2440
2441 /* detailed rx_errors */
2442 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2443 stats.rx_runt;
2444 ns->rx_over_errors = 0;
2445 ns->rx_crc_errors = stats.rx_fcs_err;
2446 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302447 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002448 stats.rx_ovflow2 + stats.rx_ovflow3 +
2449 stats.rx_trunc0 + stats.rx_trunc1 +
2450 stats.rx_trunc2 + stats.rx_trunc3;
2451 ns->rx_missed_errors = 0;
2452
2453 /* detailed tx_errors */
2454 ns->tx_aborted_errors = 0;
2455 ns->tx_carrier_errors = 0;
2456 ns->tx_fifo_errors = 0;
2457 ns->tx_heartbeat_errors = 0;
2458 ns->tx_window_errors = 0;
2459
2460 ns->tx_errors = stats.tx_error_frames;
2461 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2462 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002463}
2464
2465static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2466{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002467 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002468 int ret = 0, prtad, devad;
2469 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302470 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002471 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2472
2473 switch (cmd) {
2474 case SIOCGMIIPHY:
2475 if (pi->mdio_addr < 0)
2476 return -EOPNOTSUPP;
2477 data->phy_id = pi->mdio_addr;
2478 break;
2479 case SIOCGMIIREG:
2480 case SIOCSMIIREG:
2481 if (mdio_phy_id_is_c45(data->phy_id)) {
2482 prtad = mdio_phy_id_prtad(data->phy_id);
2483 devad = mdio_phy_id_devad(data->phy_id);
2484 } else if (data->phy_id < 32) {
2485 prtad = data->phy_id;
2486 devad = 0;
2487 data->reg_num &= 0x1f;
2488 } else
2489 return -EINVAL;
2490
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302491 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002492 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002493 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002494 data->reg_num, &data->val_out);
2495 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002496 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002497 data->reg_num, data->val_in);
2498 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302499 case SIOCGHWTSTAMP:
2500 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2501 sizeof(pi->tstamp_config)) ?
2502 -EFAULT : 0;
2503 case SIOCSHWTSTAMP:
2504 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2505 sizeof(pi->tstamp_config)))
2506 return -EFAULT;
2507
Atul Guptaa45695042017-07-04 16:46:20 +05302508 if (!is_t4(adapter->params.chip)) {
2509 switch (pi->tstamp_config.tx_type) {
2510 case HWTSTAMP_TX_OFF:
2511 case HWTSTAMP_TX_ON:
2512 break;
2513 default:
2514 return -ERANGE;
2515 }
2516
2517 switch (pi->tstamp_config.rx_filter) {
2518 case HWTSTAMP_FILTER_NONE:
2519 pi->rxtstamp = false;
2520 break;
2521 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2522 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2523 cxgb4_ptprx_timestamping(pi, pi->port_id,
2524 PTP_TS_L4);
2525 break;
2526 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2527 cxgb4_ptprx_timestamping(pi, pi->port_id,
2528 PTP_TS_L2_L4);
2529 break;
2530 case HWTSTAMP_FILTER_ALL:
2531 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2532 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2533 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2534 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2535 pi->rxtstamp = true;
2536 break;
2537 default:
2538 pi->tstamp_config.rx_filter =
2539 HWTSTAMP_FILTER_NONE;
2540 return -ERANGE;
2541 }
2542
2543 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2544 (pi->tstamp_config.rx_filter ==
2545 HWTSTAMP_FILTER_NONE)) {
2546 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2547 pi->ptp_enable = false;
2548 }
2549
2550 if (pi->tstamp_config.rx_filter !=
2551 HWTSTAMP_FILTER_NONE) {
2552 if (cxgb4_ptp_redirect_rx_packet(adapter,
2553 pi) >= 0)
2554 pi->ptp_enable = true;
2555 }
2556 } else {
2557 /* For T4 Adapters */
2558 switch (pi->tstamp_config.rx_filter) {
2559 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302560 pi->rxtstamp = false;
2561 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302562 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302563 pi->rxtstamp = true;
2564 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302565 default:
2566 pi->tstamp_config.rx_filter =
2567 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302568 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302569 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302570 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302571 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2572 sizeof(pi->tstamp_config)) ?
2573 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002574 default:
2575 return -EOPNOTSUPP;
2576 }
2577 return ret;
2578}
2579
2580static void cxgb_set_rxmode(struct net_device *dev)
2581{
2582 /* unfortunately we can't return errors to the stack */
2583 set_rxmode(dev, -1, false);
2584}
2585
2586static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2587{
2588 int ret;
2589 struct port_info *pi = netdev_priv(dev);
2590
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302591 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002592 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002593 if (!ret)
2594 dev->mtu = new_mtu;
2595 return ret;
2596}
2597
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302598#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302599static int dummy_open(struct net_device *dev)
2600{
2601 /* Turn carrier off since we don't have to transmit anything on this
2602 * interface.
2603 */
2604 netif_carrier_off(dev);
2605 return 0;
2606}
2607
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302608/* Fill MAC address that will be assigned by the FW */
2609static void fill_vf_station_mac_addr(struct adapter *adap)
2610{
2611 unsigned int i;
2612 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2613 int err;
2614 u8 *na;
2615 u16 a, b;
2616
2617 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2618 if (!err) {
2619 na = adap->params.vpd.na;
2620 for (i = 0; i < ETH_ALEN; i++)
2621 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2622 hex2val(na[2 * i + 1]));
2623 a = (hw_addr[0] << 8) | hw_addr[1];
2624 b = (hw_addr[1] << 8) | hw_addr[2];
2625 a ^= b;
2626 a |= 0x0200; /* locally assigned Ethernet MAC address */
2627 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2628 macaddr[0] = a >> 8;
2629 macaddr[1] = a & 0xff;
2630
2631 for (i = 2; i < 5; i++)
2632 macaddr[i] = hw_addr[i + 1];
2633
2634 for (i = 0; i < adap->num_vfs; i++) {
2635 macaddr[5] = adap->pf * 16 + i;
2636 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2637 }
2638 }
2639}
2640
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302641static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2642{
2643 struct port_info *pi = netdev_priv(dev);
2644 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302645 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302646
2647 /* verify MAC addr is valid */
2648 if (!is_valid_ether_addr(mac)) {
2649 dev_err(pi->adapter->pdev_dev,
2650 "Invalid Ethernet address %pM for VF %d\n",
2651 mac, vf);
2652 return -EINVAL;
2653 }
2654
2655 dev_info(pi->adapter->pdev_dev,
2656 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302657 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2658 if (!ret)
2659 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2660 return ret;
2661}
2662
2663static int cxgb_get_vf_config(struct net_device *dev,
2664 int vf, struct ifla_vf_info *ivi)
2665{
2666 struct port_info *pi = netdev_priv(dev);
2667 struct adapter *adap = pi->adapter;
2668
2669 if (vf >= adap->num_vfs)
2670 return -EINVAL;
2671 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302672 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2673 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302674 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2675 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302676}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302677
2678static int cxgb_get_phys_port_id(struct net_device *dev,
2679 struct netdev_phys_item_id *ppid)
2680{
2681 struct port_info *pi = netdev_priv(dev);
2682 unsigned int phy_port_id;
2683
2684 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2685 ppid->id_len = sizeof(phy_port_id);
2686 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2687 return 0;
2688}
2689
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302690static int cxgb_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2691 int max_tx_rate)
2692{
2693 struct port_info *pi = netdev_priv(dev);
2694 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302695 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302696 u32 fw_pfvf, fw_class;
2697 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302698 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302699 u16 pktsize;
2700
2701 if (vf >= adap->num_vfs)
2702 return -EINVAL;
2703
2704 if (min_tx_rate) {
2705 dev_err(adap->pdev_dev,
2706 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2707 min_tx_rate, vf);
2708 return -EINVAL;
2709 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302710
2711 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302712 if (ret != FW_SUCCESS) {
2713 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302714 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302715 return -EINVAL;
2716 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302717
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302718 if (!link_ok) {
2719 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2720 return -EINVAL;
2721 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302722
2723 if (max_tx_rate > speed) {
2724 dev_err(adap->pdev_dev,
2725 "Max tx rate %d for VF %d can't be > link-speed %u",
2726 max_tx_rate, vf, speed);
2727 return -EINVAL;
2728 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302729
2730 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302731 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2732 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2733 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2734 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2735 /* configure Traffic Class for rate-limiting */
2736 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2737 SCHED_CLASS_LEVEL_CL_RL,
2738 SCHED_CLASS_MODE_CLASS,
2739 SCHED_CLASS_RATEUNIT_BITS,
2740 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302741 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302742 max_tx_rate * 1000, 0, pktsize);
2743 if (ret) {
2744 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2745 ret);
2746 return -EINVAL;
2747 }
2748 dev_info(adap->pdev_dev,
2749 "Class %d with MSS %u configured with rate %u\n",
2750 class_id, pktsize, max_tx_rate);
2751
2752 /* bind VF to configured Traffic Class */
2753 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2754 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2755 fw_class = class_id;
2756 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2757 &fw_class);
2758 if (ret) {
2759 dev_err(adap->pdev_dev,
2760 "Err %d in binding VF %d to Traffic Class %d\n",
2761 ret, vf, class_id);
2762 return -EINVAL;
2763 }
2764 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2765 adap->pf, vf, class_id);
2766 adap->vfinfo[vf].tx_rate = max_tx_rate;
2767 return 0;
2768}
2769
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302770#endif
2771
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002772static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2773{
2774 int ret;
2775 struct sockaddr *addr = p;
2776 struct port_info *pi = netdev_priv(dev);
2777
2778 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002779 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002780
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302781 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002782 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002783 if (ret < 0)
2784 return ret;
2785
2786 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2787 pi->xact_addr_filt = ret;
2788 return 0;
2789}
2790
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002791#ifdef CONFIG_NET_POLL_CONTROLLER
2792static void cxgb_netpoll(struct net_device *dev)
2793{
2794 struct port_info *pi = netdev_priv(dev);
2795 struct adapter *adap = pi->adapter;
2796
2797 if (adap->flags & USING_MSIX) {
2798 int i;
2799 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2800
2801 for (i = pi->nqsets; i; i--, rx++)
2802 t4_sge_intr_msix(0, &rx->rspq);
2803 } else
2804 t4_intr_handler(adap)(0, adap);
2805}
2806#endif
2807
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302808static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2809{
2810 struct port_info *pi = netdev_priv(dev);
2811 struct adapter *adap = pi->adapter;
2812 struct sched_class *e;
2813 struct ch_sched_params p;
2814 struct ch_sched_queue qe;
2815 u32 req_rate;
2816 int err = 0;
2817
2818 if (!can_sched(dev))
2819 return -ENOTSUPP;
2820
2821 if (index < 0 || index > pi->nqsets - 1)
2822 return -EINVAL;
2823
2824 if (!(adap->flags & FULL_INIT_DONE)) {
2825 dev_err(adap->pdev_dev,
2826 "Failed to rate limit on queue %d. Link Down?\n",
2827 index);
2828 return -EINVAL;
2829 }
2830
2831 /* Convert from Mbps to Kbps */
2832 req_rate = rate << 10;
2833
2834 /* Max rate is 10 Gbps */
2835 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2836 dev_err(adap->pdev_dev,
2837 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2838 rate, SCHED_MAX_RATE_KBPS);
2839 return -ERANGE;
2840 }
2841
2842 /* First unbind the queue from any existing class */
2843 memset(&qe, 0, sizeof(qe));
2844 qe.queue = index;
2845 qe.class = SCHED_CLS_NONE;
2846
2847 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2848 if (err) {
2849 dev_err(adap->pdev_dev,
2850 "Unbinding Queue %d on port %d fail. Err: %d\n",
2851 index, pi->port_id, err);
2852 return err;
2853 }
2854
2855 /* Queue already unbound */
2856 if (!req_rate)
2857 return 0;
2858
2859 /* Fetch any available unused or matching scheduling class */
2860 memset(&p, 0, sizeof(p));
2861 p.type = SCHED_CLASS_TYPE_PACKET;
2862 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2863 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2864 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2865 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2866 p.u.params.channel = pi->tx_chan;
2867 p.u.params.class = SCHED_CLS_NONE;
2868 p.u.params.minrate = 0;
2869 p.u.params.maxrate = req_rate;
2870 p.u.params.weight = 0;
2871 p.u.params.pktsize = dev->mtu;
2872
2873 e = cxgb4_sched_class_alloc(dev, &p);
2874 if (!e)
2875 return -ENOMEM;
2876
2877 /* Bind the queue to a scheduling class */
2878 memset(&qe, 0, sizeof(qe));
2879 qe.queue = index;
2880 qe.class = e->idx;
2881
2882 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2883 if (err)
2884 dev_err(adap->pdev_dev,
2885 "Queue rate limiting failed. Err: %d\n", err);
2886 return err;
2887}
2888
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302889static int cxgb_setup_tc_flower(struct net_device *dev,
2890 struct tc_cls_flower_offload *cls_flower)
2891{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002892 if (cls_flower->common.chain_index)
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302893 return -EOPNOTSUPP;
2894
2895 switch (cls_flower->command) {
2896 case TC_CLSFLOWER_REPLACE:
2897 return cxgb4_tc_flower_replace(dev, cls_flower);
2898 case TC_CLSFLOWER_DESTROY:
2899 return cxgb4_tc_flower_destroy(dev, cls_flower);
2900 case TC_CLSFLOWER_STATS:
2901 return cxgb4_tc_flower_stats(dev, cls_flower);
2902 default:
2903 return -EOPNOTSUPP;
2904 }
2905}
2906
Jiri Pirkof7323042017-08-07 10:15:20 +02002907static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002908 struct tc_cls_u32_offload *cls_u32)
2909{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002910 if (cls_u32->common.chain_index)
Jiri Pirkof7323042017-08-07 10:15:20 +02002911 return -EOPNOTSUPP;
2912
2913 switch (cls_u32->command) {
2914 case TC_CLSU32_NEW_KNODE:
2915 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002916 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002917 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002918 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002919 default:
2920 return -EOPNOTSUPP;
2921 }
2922}
2923
Jiri Pirkocd019e92017-10-19 15:50:40 +02002924static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2925 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302926{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002927 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302928 struct port_info *pi = netdev2pinfo(dev);
2929 struct adapter *adap = netdev2adap(dev);
2930
2931 if (!(adap->flags & FULL_INIT_DONE)) {
2932 dev_err(adap->pdev_dev,
2933 "Failed to setup tc on port %d. Link Down?\n",
2934 pi->port_id);
2935 return -EINVAL;
2936 }
2937
Jiri Pirkof7323042017-08-07 10:15:20 +02002938 switch (type) {
2939 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002940 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302941 case TC_SETUP_CLSFLOWER:
2942 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002943 default:
2944 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302945 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302946}
2947
Jiri Pirkocd019e92017-10-19 15:50:40 +02002948static int cxgb_setup_tc_block(struct net_device *dev,
2949 struct tc_block_offload *f)
2950{
2951 struct port_info *pi = netdev2pinfo(dev);
2952
2953 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2954 return -EOPNOTSUPP;
2955
2956 switch (f->command) {
2957 case TC_BLOCK_BIND:
2958 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
2959 pi, dev);
2960 case TC_BLOCK_UNBIND:
2961 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
2962 return 0;
2963 default:
2964 return -EOPNOTSUPP;
2965 }
2966}
2967
2968static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
2969 void *type_data)
2970{
2971 switch (type) {
2972 case TC_SETUP_CLSU32:
2973 case TC_SETUP_CLSFLOWER:
2974 return 0; /* will be removed after conversion from ndo */
2975 case TC_SETUP_BLOCK:
2976 return cxgb_setup_tc_block(dev, type_data);
2977 default:
2978 return -EOPNOTSUPP;
2979 }
2980}
2981
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302982static netdev_features_t cxgb_fix_features(struct net_device *dev,
2983 netdev_features_t features)
2984{
2985 /* Disable GRO, if RX_CSUM is disabled */
2986 if (!(features & NETIF_F_RXCSUM))
2987 features &= ~NETIF_F_GRO;
2988
2989 return features;
2990}
2991
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002992static const struct net_device_ops cxgb4_netdev_ops = {
2993 .ndo_open = cxgb_open,
2994 .ndo_stop = cxgb_close,
2995 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002996 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002997 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002998 .ndo_set_rx_mode = cxgb_set_rxmode,
2999 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003000 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003001 .ndo_validate_addr = eth_validate_addr,
3002 .ndo_do_ioctl = cxgb_ioctl,
3003 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003004#ifdef CONFIG_NET_POLL_CONTROLLER
3005 .ndo_poll_controller = cxgb_netpoll,
3006#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303007#ifdef CONFIG_CHELSIO_T4_FCOE
3008 .ndo_fcoe_enable = cxgb_fcoe_enable,
3009 .ndo_fcoe_disable = cxgb_fcoe_disable,
3010#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303011 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303012 .ndo_setup_tc = cxgb_setup_tc,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303013 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003014};
3015
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303016#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303017static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
3018 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303019 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05303020 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05303021 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05303022 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303023};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303024#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303025
3026static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3027{
3028 struct adapter *adapter = netdev2adap(dev);
3029
3030 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3031 strlcpy(info->version, cxgb4_driver_version,
3032 sizeof(info->version));
3033 strlcpy(info->bus_info, pci_name(adapter->pdev),
3034 sizeof(info->bus_info));
3035}
3036
3037static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3038 .get_drvinfo = get_drvinfo,
3039};
3040
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003041void t4_fatal_err(struct adapter *adap)
3042{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303043 int port;
3044
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003045 if (pci_channel_offline(adap->pdev))
3046 return;
3047
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303048 /* Disable the SGE since ULDs are going to free resources that
3049 * could be exposed to the adapter. RDMA MWs for example...
3050 */
3051 t4_shutdown_adapter(adap);
3052 for_each_port(adap, port) {
3053 struct net_device *dev = adap->port[port];
3054
3055 /* If we get here in very early initialization the network
3056 * devices may not have been set up yet.
3057 */
3058 if (!dev)
3059 continue;
3060
3061 netif_tx_stop_all_queues(dev);
3062 netif_carrier_off(dev);
3063 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003064 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3065}
3066
3067static void setup_memwin(struct adapter *adap)
3068{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303069 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003070
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303071 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003072}
3073
3074static void setup_memwin_rdma(struct adapter *adap)
3075{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003076 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303077 u32 start;
3078 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003079
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303080 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3081 start &= PCI_BASE_ADDRESS_MEM_MASK;
3082 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003083 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3084 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303085 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3086 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003087 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303088 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003089 adap->vres.ocq.start);
3090 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303091 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003092 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003093}
3094
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003095static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3096{
3097 u32 v;
3098 int ret;
3099
3100 /* get device capabilities */
3101 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303102 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3103 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303104 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303105 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003106 if (ret < 0)
3107 return ret;
3108
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303109 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3110 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303111 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003112 if (ret < 0)
3113 return ret;
3114
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303115 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003116 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303117 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3118 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003119 if (ret < 0)
3120 return ret;
3121
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303122 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303123 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3124 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003125 if (ret < 0)
3126 return ret;
3127
3128 t4_sge_init(adap);
3129
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003130 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303131 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303132 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303133 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3134 v = t4_read_reg(adap, TP_PIO_DATA_A);
3135 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003136
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003137 /* first 4 Tx modulation queues point to consecutive Tx channels */
3138 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303139 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3140 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003141
3142 /* associate each Tx modulation queue with consecutive Tx channels */
3143 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303144 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303145 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303146 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303147 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303148 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303149 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003150
3151#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3152 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303153 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3154 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3155 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3156 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3157 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3158 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_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));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003163 }
3164
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003165 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303166 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003167}
3168
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003169/*
3170 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3171 */
3172#define MAX_ATIDS 8192U
3173
3174/*
3175 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003176 *
3177 * If the firmware we're dealing with has Configuration File support, then
3178 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003179 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003180
3181/*
3182 * Tweak configuration based on module parameters, etc. Most of these have
3183 * defaults assigned to them by Firmware Configuration Files (if we're using
3184 * them) but need to be explicitly set if we're using hard-coded
3185 * initialization. But even in the case of using Firmware Configuration
3186 * Files, we'd like to expose the ability to change these via module
3187 * parameters so these are essentially common tweaks/settings for
3188 * Configuration Files and hard-coded initialization ...
3189 */
3190static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003191{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003192 /*
3193 * Fix up various Host-Dependent Parameters like Page Size, Cache
3194 * Line Size, etc. The firmware default is for a 4KB Page Size and
3195 * 64B Cache Line Size ...
3196 */
3197 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003198
Vipul Pandya636f9d32012-09-26 02:39:39 +00003199 /*
3200 * Process module parameters which affect early initialization.
3201 */
3202 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3203 dev_err(&adapter->pdev->dev,
3204 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3205 rx_dma_offset);
3206 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003207 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303208 t4_set_reg_field(adapter, SGE_CONTROL_A,
3209 PKTSHIFT_V(PKTSHIFT_M),
3210 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003211
Vipul Pandya636f9d32012-09-26 02:39:39 +00003212 /*
3213 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3214 * adds the pseudo header itself.
3215 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303216 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3217 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003218
3219 return 0;
3220}
3221
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303222/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3223 * unto themselves and they contain their own firmware to perform their
3224 * tasks ...
3225 */
3226static int phy_aq1202_version(const u8 *phy_fw_data,
3227 size_t phy_fw_size)
3228{
3229 int offset;
3230
3231 /* At offset 0x8 you're looking for the primary image's
3232 * starting offset which is 3 Bytes wide
3233 *
3234 * At offset 0xa of the primary image, you look for the offset
3235 * of the DRAM segment which is 3 Bytes wide.
3236 *
3237 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3238 * wide
3239 */
3240 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3241 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3242 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3243
3244 offset = le24(phy_fw_data + 0x8) << 12;
3245 offset = le24(phy_fw_data + offset + 0xa);
3246 return be16(phy_fw_data + offset + 0x27e);
3247
3248 #undef be16
3249 #undef le16
3250 #undef le24
3251}
3252
3253static struct info_10gbt_phy_fw {
3254 unsigned int phy_fw_id; /* PCI Device ID */
3255 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3256 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3257 int phy_flash; /* Has FLASH for PHY Firmware */
3258} phy_info_array[] = {
3259 {
3260 PHY_AQ1202_DEVICEID,
3261 PHY_AQ1202_FIRMWARE,
3262 phy_aq1202_version,
3263 1,
3264 },
3265 {
3266 PHY_BCM84834_DEVICEID,
3267 PHY_BCM84834_FIRMWARE,
3268 NULL,
3269 0,
3270 },
3271 { 0, NULL, NULL },
3272};
3273
3274static struct info_10gbt_phy_fw *find_phy_info(int devid)
3275{
3276 int i;
3277
3278 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3279 if (phy_info_array[i].phy_fw_id == devid)
3280 return &phy_info_array[i];
3281 }
3282 return NULL;
3283}
3284
3285/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3286 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3287 * we return a negative error number. If we transfer new firmware we return 1
3288 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3289 */
3290static int adap_init0_phy(struct adapter *adap)
3291{
3292 const struct firmware *phyf;
3293 int ret;
3294 struct info_10gbt_phy_fw *phy_info;
3295
3296 /* Use the device ID to determine which PHY file to flash.
3297 */
3298 phy_info = find_phy_info(adap->pdev->device);
3299 if (!phy_info) {
3300 dev_warn(adap->pdev_dev,
3301 "No PHY Firmware file found for this PHY\n");
3302 return -EOPNOTSUPP;
3303 }
3304
3305 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3306 * use that. The adapter firmware provides us with a memory buffer
3307 * where we can load a PHY firmware file from the host if we want to
3308 * override the PHY firmware File in flash.
3309 */
3310 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3311 adap->pdev_dev);
3312 if (ret < 0) {
3313 /* For adapters without FLASH attached to PHY for their
3314 * firmware, it's obviously a fatal error if we can't get the
3315 * firmware to the adapter. For adapters with PHY firmware
3316 * FLASH storage, it's worth a warning if we can't find the
3317 * PHY Firmware but we'll neuter the error ...
3318 */
3319 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3320 "/lib/firmware/%s, error %d\n",
3321 phy_info->phy_fw_file, -ret);
3322 if (phy_info->phy_flash) {
3323 int cur_phy_fw_ver = 0;
3324
3325 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3326 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3327 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3328 ret = 0;
3329 }
3330
3331 return ret;
3332 }
3333
3334 /* Load PHY Firmware onto adapter.
3335 */
3336 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3337 phy_info->phy_fw_version,
3338 (u8 *)phyf->data, phyf->size);
3339 if (ret < 0)
3340 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3341 -ret);
3342 else if (ret > 0) {
3343 int new_phy_fw_ver = 0;
3344
3345 if (phy_info->phy_fw_version)
3346 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3347 phyf->size);
3348 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3349 "Firmware /lib/firmware/%s, version %#x\n",
3350 phy_info->phy_fw_file, new_phy_fw_ver);
3351 }
3352
3353 release_firmware(phyf);
3354
3355 return ret;
3356}
3357
Vipul Pandya636f9d32012-09-26 02:39:39 +00003358/*
3359 * Attempt to initialize the adapter via a Firmware Configuration File.
3360 */
3361static int adap_init0_config(struct adapter *adapter, int reset)
3362{
3363 struct fw_caps_config_cmd caps_cmd;
3364 const struct firmware *cf;
3365 unsigned long mtype = 0, maddr = 0;
3366 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303367 int ret;
3368 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003369 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303370 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003371
3372 /*
3373 * Reset device if necessary.
3374 */
3375 if (reset) {
3376 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303377 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003378 if (ret < 0)
3379 goto bye;
3380 }
3381
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303382 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3383 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3384 * to be performed after any global adapter RESET above since some
3385 * PHYs only have local RAM copies of the PHY firmware.
3386 */
3387 if (is_10gbt_device(adapter->pdev->device)) {
3388 ret = adap_init0_phy(adapter);
3389 if (ret < 0)
3390 goto bye;
3391 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003392 /*
3393 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3394 * then use that. Otherwise, use the configuration file stored
3395 * in the adapter flash ...
3396 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303397 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003398 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303399 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003400 break;
3401 case CHELSIO_T5:
3402 fw_config_file = FW5_CFNAME;
3403 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303404 case CHELSIO_T6:
3405 fw_config_file = FW6_CFNAME;
3406 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003407 default:
3408 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3409 adapter->pdev->device);
3410 ret = -EINVAL;
3411 goto bye;
3412 }
3413
3414 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003415 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303416 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003417 mtype = FW_MEMTYPE_CF_FLASH;
3418 maddr = t4_flash_cfg_addr(adapter);
3419 } else {
3420 u32 params[7], val[7];
3421
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303422 sprintf(fw_config_file_path,
3423 "/lib/firmware/%s", fw_config_file);
3424 config_name = fw_config_file_path;
3425
Vipul Pandya636f9d32012-09-26 02:39:39 +00003426 if (cf->size >= FLASH_CFG_MAX_SIZE)
3427 ret = -ENOMEM;
3428 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303429 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3430 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003431 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303432 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003433 if (ret == 0) {
3434 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303435 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003436 * sizes have to be in terms of multiples of 4
3437 * bytes. So, if the Configuration File isn't
3438 * a multiple of 4 bytes in length we'll have
3439 * to write that out separately since we can't
3440 * guarantee that the bytes following the
3441 * residual byte in the buffer returned by
3442 * request_firmware() are zeroed out ...
3443 */
3444 size_t resid = cf->size & 0x3;
3445 size_t size = cf->size & ~0x3;
3446 __be32 *data = (__be32 *)cf->data;
3447
Hariprasad Shenai51678652014-11-21 12:52:02 +05303448 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3449 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003450
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303451 spin_lock(&adapter->win0_lock);
3452 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3453 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003454 if (ret == 0 && resid != 0) {
3455 union {
3456 __be32 word;
3457 char buf[4];
3458 } last;
3459 int i;
3460
3461 last.word = data[size >> 2];
3462 for (i = resid; i < 4; i++)
3463 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303464 ret = t4_memory_rw(adapter, 0, mtype,
3465 maddr + size,
3466 4, &last.word,
3467 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003468 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303469 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003470 }
3471 }
3472
3473 release_firmware(cf);
3474 if (ret)
3475 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003476 }
3477
Vipul Pandya636f9d32012-09-26 02:39:39 +00003478 /*
3479 * Issue a Capability Configuration command to the firmware to get it
3480 * to parse the Configuration File. We don't use t4_fw_config_file()
3481 * because we want the ability to modify various features after we've
3482 * processed the configuration file ...
3483 */
3484 memset(&caps_cmd, 0, sizeof(caps_cmd));
3485 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303486 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3487 FW_CMD_REQUEST_F |
3488 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303489 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303490 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3491 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3492 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003493 FW_LEN16(caps_cmd));
3494 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3495 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303496
3497 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3498 * Configuration File in FLASH), our last gasp effort is to use the
3499 * Firmware Configuration File which is embedded in the firmware. A
3500 * very few early versions of the firmware didn't have one embedded
3501 * but we can ignore those.
3502 */
3503 if (ret == -ENOENT) {
3504 memset(&caps_cmd, 0, sizeof(caps_cmd));
3505 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303506 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3507 FW_CMD_REQUEST_F |
3508 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303509 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3510 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3511 sizeof(caps_cmd), &caps_cmd);
3512 config_name = "Firmware Default";
3513 }
3514
3515 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003516 if (ret < 0)
3517 goto bye;
3518
Vipul Pandya636f9d32012-09-26 02:39:39 +00003519 finiver = ntohl(caps_cmd.finiver);
3520 finicsum = ntohl(caps_cmd.finicsum);
3521 cfcsum = ntohl(caps_cmd.cfcsum);
3522 if (finicsum != cfcsum)
3523 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3524 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3525 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003526
Vipul Pandya636f9d32012-09-26 02:39:39 +00003527 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003528 * And now tell the firmware to use the configuration we just loaded.
3529 */
3530 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303531 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3532 FW_CMD_REQUEST_F |
3533 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303534 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003535 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3536 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003537 if (ret < 0)
3538 goto bye;
3539
Vipul Pandya636f9d32012-09-26 02:39:39 +00003540 /*
3541 * Tweak configuration based on system architecture, module
3542 * parameters, etc.
3543 */
3544 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003545 if (ret < 0)
3546 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003547
Vipul Pandya636f9d32012-09-26 02:39:39 +00003548 /*
3549 * And finally tell the firmware to initialize itself using the
3550 * parameters from the Configuration File.
3551 */
3552 ret = t4_fw_initialize(adapter, adapter->mbox);
3553 if (ret < 0)
3554 goto bye;
3555
Hariprasad Shenai06640312015-01-13 15:19:25 +05303556 /* Emit Firmware Configuration File information and return
3557 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003558 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003559 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303560 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3561 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003562 return 0;
3563
3564 /*
3565 * Something bad happened. Return the error ... (If the "error"
3566 * is that there's no Configuration File on the adapter we don't
3567 * want to issue a warning since this is fairly common.)
3568 */
3569bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303570 if (config_issued && ret != -ENOENT)
3571 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3572 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003573 return ret;
3574}
3575
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303576static struct fw_info fw_info_array[] = {
3577 {
3578 .chip = CHELSIO_T4,
3579 .fs_name = FW4_CFNAME,
3580 .fw_mod_name = FW4_FNAME,
3581 .fw_hdr = {
3582 .chip = FW_HDR_CHIP_T4,
3583 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3584 .intfver_nic = FW_INTFVER(T4, NIC),
3585 .intfver_vnic = FW_INTFVER(T4, VNIC),
3586 .intfver_ri = FW_INTFVER(T4, RI),
3587 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3588 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3589 },
3590 }, {
3591 .chip = CHELSIO_T5,
3592 .fs_name = FW5_CFNAME,
3593 .fw_mod_name = FW5_FNAME,
3594 .fw_hdr = {
3595 .chip = FW_HDR_CHIP_T5,
3596 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3597 .intfver_nic = FW_INTFVER(T5, NIC),
3598 .intfver_vnic = FW_INTFVER(T5, VNIC),
3599 .intfver_ri = FW_INTFVER(T5, RI),
3600 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3601 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3602 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303603 }, {
3604 .chip = CHELSIO_T6,
3605 .fs_name = FW6_CFNAME,
3606 .fw_mod_name = FW6_FNAME,
3607 .fw_hdr = {
3608 .chip = FW_HDR_CHIP_T6,
3609 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3610 .intfver_nic = FW_INTFVER(T6, NIC),
3611 .intfver_vnic = FW_INTFVER(T6, VNIC),
3612 .intfver_ofld = FW_INTFVER(T6, OFLD),
3613 .intfver_ri = FW_INTFVER(T6, RI),
3614 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3615 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3616 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3617 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3618 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303619 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303620
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303621};
3622
3623static struct fw_info *find_fw_info(int chip)
3624{
3625 int i;
3626
3627 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3628 if (fw_info_array[i].chip == chip)
3629 return &fw_info_array[i];
3630 }
3631 return NULL;
3632}
3633
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003634/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003635 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003636 */
3637static int adap_init0(struct adapter *adap)
3638{
3639 int ret;
3640 u32 v, port_vec;
3641 enum dev_state state;
3642 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003643 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303644 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003645
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303646 /* Grab Firmware Device Log parameters as early as possible so we have
3647 * access to it for debugging, etc.
3648 */
3649 ret = t4_init_devlog_params(adap);
3650 if (ret < 0)
3651 return ret;
3652
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303653 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303654 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3655 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003656 if (ret < 0) {
3657 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3658 ret);
3659 return ret;
3660 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003661 if (ret == adap->mbox)
3662 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003663
Vipul Pandya636f9d32012-09-26 02:39:39 +00003664 /*
3665 * If we're the Master PF Driver and the device is uninitialized,
3666 * then let's consider upgrading the firmware ... (We always want
3667 * to check the firmware version number in order to A. get it for
3668 * later reporting and B. to warn if the currently loaded firmware
3669 * is excessively mismatched relative to the driver.)
3670 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303671
Ganesh Goudar760446f2017-07-20 18:28:48 +05303672 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303673 ret = t4_check_fw_version(adap);
3674 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303675 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303676 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003677 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303678 struct fw_info *fw_info;
3679 struct fw_hdr *card_fw;
3680 const struct firmware *fw;
3681 const u8 *fw_data = NULL;
3682 unsigned int fw_size = 0;
3683
3684 /* This is the firmware whose headers the driver was compiled
3685 * against
3686 */
3687 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3688 if (fw_info == NULL) {
3689 dev_err(adap->pdev_dev,
3690 "unable to get firmware info for chip %d.\n",
3691 CHELSIO_CHIP_VERSION(adap->params.chip));
3692 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003693 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303694
3695 /* allocate memory to read the header of the firmware on the
3696 * card
3697 */
Michal Hocko752ade62017-05-08 15:57:27 -07003698 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303699
3700 /* Get FW from from /lib/firmware/ */
3701 ret = request_firmware(&fw, fw_info->fw_mod_name,
3702 adap->pdev_dev);
3703 if (ret < 0) {
3704 dev_err(adap->pdev_dev,
3705 "unable to load firmware image %s, error %d\n",
3706 fw_info->fw_mod_name, ret);
3707 } else {
3708 fw_data = fw->data;
3709 fw_size = fw->size;
3710 }
3711
3712 /* upgrade FW logic */
3713 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3714 state, &reset);
3715
3716 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003717 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003718 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303719
Vipul Pandya636f9d32012-09-26 02:39:39 +00003720 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303721 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003722 }
3723
3724 /*
3725 * Grab VPD parameters. This should be done after we establish a
3726 * connection to the firmware since some of the VPD parameters
3727 * (notably the Core Clock frequency) are retrieved via requests to
3728 * the firmware. On the other hand, we need these fairly early on
3729 * so we do this right after getting ahold of the firmware.
3730 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303731 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003732 if (ret < 0)
3733 goto bye;
3734
Vipul Pandya636f9d32012-09-26 02:39:39 +00003735 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003736 * Find out what ports are available to us. Note that we need to do
3737 * this before calling adap_init0_no_config() since it needs nports
3738 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003739 */
3740 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303741 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3742 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303743 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003744 if (ret < 0)
3745 goto bye;
3746
3747 adap->params.nports = hweight32(port_vec);
3748 adap->params.portvec = port_vec;
3749
Hariprasad Shenai06640312015-01-13 15:19:25 +05303750 /* If the firmware is initialized already, emit a simply note to that
3751 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003752 */
3753 if (state == DEV_STATE_INIT) {
3754 dev_info(adap->pdev_dev, "Coming up as %s: "\
3755 "Adapter already initialized\n",
3756 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003757 } else {
3758 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3759 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303760
3761 /* Find out whether we're dealing with a version of the
3762 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003763 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303764 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3765 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303766 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303767 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003768
Hariprasad Shenai06640312015-01-13 15:19:25 +05303769 /* If the firmware doesn't support Configuration Files,
3770 * return an error.
3771 */
3772 if (ret < 0) {
3773 dev_err(adap->pdev_dev, "firmware doesn't support "
3774 "Firmware Configuration Files\n");
3775 goto bye;
3776 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003777
Hariprasad Shenai06640312015-01-13 15:19:25 +05303778 /* The firmware provides us with a memory buffer where we can
3779 * load a Configuration File from the host if we want to
3780 * override the Configuration File in flash.
3781 */
3782 ret = adap_init0_config(adap, reset);
3783 if (ret == -ENOENT) {
3784 dev_err(adap->pdev_dev, "no Configuration File "
3785 "present on adapter.\n");
3786 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003787 }
3788 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303789 dev_err(adap->pdev_dev, "could not initialize "
3790 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003791 goto bye;
3792 }
3793 }
3794
Hariprasad Shenai06640312015-01-13 15:19:25 +05303795 /* Give the SGE code a chance to pull in anything that it needs ...
3796 * Note that this must be called after we retrieve our VPD parameters
3797 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003798 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303799 ret = t4_sge_init(adap);
3800 if (ret < 0)
3801 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003802
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003803 if (is_bypass_device(adap->pdev->device))
3804 adap->params.bypass = 1;
3805
Vipul Pandya636f9d32012-09-26 02:39:39 +00003806 /*
3807 * Grab some of our basic fundamental operating parameters.
3808 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003809#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303810 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3811 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003812
3813#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303814 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3815 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3816 FW_PARAMS_PARAM_Y_V(0) | \
3817 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003818
Vipul Pandya636f9d32012-09-26 02:39:39 +00003819 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003820 params[1] = FW_PARAM_PFVF(L2T_START);
3821 params[2] = FW_PARAM_PFVF(L2T_END);
3822 params[3] = FW_PARAM_PFVF(FILTER_START);
3823 params[4] = FW_PARAM_PFVF(FILTER_END);
3824 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303825 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003826 if (ret < 0)
3827 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003828 adap->sge.egr_start = val[0];
3829 adap->l2t_start = val[1];
3830 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003831 adap->tids.ftid_base = val[3];
3832 adap->tids.nftids = val[4] - val[3] + 1;
3833 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003834
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303835 /* qids (ingress/egress) returned from firmware can be anywhere
3836 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3837 * Hence driver needs to allocate memory for this range to
3838 * store the queue info. Get the highest IQFLINT/EQ index returned
3839 * in FW_EQ_*_CMD.alloc command.
3840 */
3841 params[0] = FW_PARAM_PFVF(EQ_END);
3842 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303843 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303844 if (ret < 0)
3845 goto bye;
3846 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3847 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3848
3849 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3850 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3851 if (!adap->sge.egr_map) {
3852 ret = -ENOMEM;
3853 goto bye;
3854 }
3855
3856 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3857 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3858 if (!adap->sge.ingr_map) {
3859 ret = -ENOMEM;
3860 goto bye;
3861 }
3862
3863 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303864 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303865 */
3866 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3867 sizeof(long), GFP_KERNEL);
3868 if (!adap->sge.starving_fl) {
3869 ret = -ENOMEM;
3870 goto bye;
3871 }
3872
3873 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3874 sizeof(long), GFP_KERNEL);
3875 if (!adap->sge.txq_maperr) {
3876 ret = -ENOMEM;
3877 goto bye;
3878 }
3879
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303880#ifdef CONFIG_DEBUG_FS
3881 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3882 sizeof(long), GFP_KERNEL);
3883 if (!adap->sge.blocked_fl) {
3884 ret = -ENOMEM;
3885 goto bye;
3886 }
3887#endif
3888
Anish Bhattb5a02f52015-01-14 15:17:34 -08003889 params[0] = FW_PARAM_PFVF(CLIP_START);
3890 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303891 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003892 if (ret < 0)
3893 goto bye;
3894 adap->clipt_start = val[0];
3895 adap->clipt_end = val[1];
3896
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303897 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3898 * Classes supported by the hardware/firmware so we hard code it here
3899 * for now.
3900 */
3901 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3902
Vipul Pandya636f9d32012-09-26 02:39:39 +00003903 /* query params related to active filter region */
3904 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3905 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303906 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003907 /* If Active filter size is set we enable establishing
3908 * offload connection through firmware work request
3909 */
3910 if ((val[0] != val[1]) && (ret >= 0)) {
3911 adap->flags |= FW_OFLD_CONN;
3912 adap->tids.aftid_base = val[0];
3913 adap->tids.aftid_end = val[1];
3914 }
3915
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003916 /* If we're running on newer firmware, let it know that we're
3917 * prepared to deal with encapsulated CPL messages. Older
3918 * firmware won't understand this and we'll just get
3919 * unencapsulated messages ...
3920 */
3921 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3922 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303923 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003924
Vipul Pandya636f9d32012-09-26 02:39:39 +00003925 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303926 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3927 * capability. Earlier versions of the firmware didn't have the
3928 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3929 * permission to use ULPTX MEMWRITE DSGL.
3930 */
3931 if (is_t4(adap->params.chip)) {
3932 adap->params.ulptx_memwrite_dsgl = false;
3933 } else {
3934 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303935 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303936 1, params, val);
3937 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3938 }
3939
Steve Wise086de572016-09-16 07:54:49 -07003940 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3941 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3942 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3943 1, params, val);
3944 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3945
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05303946 /* See if FW supports FW_FILTER2 work request */
3947 if (is_t4(adap->params.chip)) {
3948 adap->params.filter2_wr_support = 0;
3949 } else {
3950 params[0] = FW_PARAM_DEV(FILTER2_WR);
3951 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3952 1, params, val);
3953 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
3954 }
3955
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303956 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003957 * Get device capabilities so we can determine what resources we need
3958 * to manage.
3959 */
3960 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303961 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3962 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303963 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003964 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3965 &caps_cmd);
3966 if (ret < 0)
3967 goto bye;
3968
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003969 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003970 /* query offload-related parameters */
3971 params[0] = FW_PARAM_DEV(NTID);
3972 params[1] = FW_PARAM_PFVF(SERVER_START);
3973 params[2] = FW_PARAM_PFVF(SERVER_END);
3974 params[3] = FW_PARAM_PFVF(TDDP_START);
3975 params[4] = FW_PARAM_PFVF(TDDP_END);
3976 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303977 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003978 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003979 if (ret < 0)
3980 goto bye;
3981 adap->tids.ntids = val[0];
3982 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3983 adap->tids.stid_base = val[1];
3984 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003985 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003986 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003987 * region into two parts. Regular filters get 1/3rd and server
3988 * filters get 2/3rd part. This is only enabled if workarond
3989 * path is enabled.
3990 * 1. For regular filters.
3991 * 2. Server filter: This are special filters which are used
3992 * to redirect SYN packets to offload queue.
3993 */
3994 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3995 adap->tids.sftid_base = adap->tids.ftid_base +
3996 DIV_ROUND_UP(adap->tids.nftids, 3);
3997 adap->tids.nsftids = adap->tids.nftids -
3998 DIV_ROUND_UP(adap->tids.nftids, 3);
3999 adap->tids.nftids = adap->tids.sftid_base -
4000 adap->tids.ftid_base;
4001 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004002 adap->vres.ddp.start = val[3];
4003 adap->vres.ddp.size = val[4] - val[3] + 1;
4004 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004005
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004006 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304007 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004008 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004009 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004010 params[0] = FW_PARAM_PFVF(STAG_START);
4011 params[1] = FW_PARAM_PFVF(STAG_END);
4012 params[2] = FW_PARAM_PFVF(RQ_START);
4013 params[3] = FW_PARAM_PFVF(RQ_END);
4014 params[4] = FW_PARAM_PFVF(PBL_START);
4015 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304016 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004017 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004018 if (ret < 0)
4019 goto bye;
4020 adap->vres.stag.start = val[0];
4021 adap->vres.stag.size = val[1] - val[0] + 1;
4022 adap->vres.rq.start = val[2];
4023 adap->vres.rq.size = val[3] - val[2] + 1;
4024 adap->vres.pbl.start = val[4];
4025 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004026
4027 params[0] = FW_PARAM_PFVF(SQRQ_START);
4028 params[1] = FW_PARAM_PFVF(SQRQ_END);
4029 params[2] = FW_PARAM_PFVF(CQ_START);
4030 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004031 params[4] = FW_PARAM_PFVF(OCQ_START);
4032 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304033 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304034 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004035 if (ret < 0)
4036 goto bye;
4037 adap->vres.qp.start = val[0];
4038 adap->vres.qp.size = val[1] - val[0] + 1;
4039 adap->vres.cq.start = val[2];
4040 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004041 adap->vres.ocq.start = val[4];
4042 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304043
4044 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4045 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304046 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304047 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304048 if (ret < 0) {
4049 adap->params.max_ordird_qp = 8;
4050 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4051 ret = 0;
4052 } else {
4053 adap->params.max_ordird_qp = val[0];
4054 adap->params.max_ird_adapter = val[1];
4055 }
4056 dev_info(adap->pdev_dev,
4057 "max_ordird_qp %d max_ird_adapter %d\n",
4058 adap->params.max_ordird_qp,
4059 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304060 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004061 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004062 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004063 params[0] = FW_PARAM_PFVF(ISCSI_START);
4064 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304065 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004066 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004067 if (ret < 0)
4068 goto bye;
4069 adap->vres.iscsi.start = val[0];
4070 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304071 /* LIO target and cxgb4i initiaitor */
4072 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004073 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304074 if (caps_cmd.cryptocaps) {
4075 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304076 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4077 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4078 params, val);
4079 if (ret < 0) {
4080 if (ret != -EINVAL)
4081 goto bye;
4082 } else {
4083 adap->vres.ncrypto_fc = val[0];
4084 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304085 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4086 adap->num_uld += 1;
4087 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004088#undef FW_PARAM_PFVF
4089#undef FW_PARAM_DEV
4090
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304091 /* The MTU/MSS Table is initialized by now, so load their values. If
4092 * we're initializing the adapter, then we'll make any modifications
4093 * we want to the MTU/MSS Table and also initialize the congestion
4094 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004095 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004096 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304097 if (state != DEV_STATE_INIT) {
4098 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004099
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304100 /* The default MTU Table contains values 1492 and 1500.
4101 * However, for TCP, it's better to have two values which are
4102 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4103 * This allows us to have a TCP Data Payload which is a
4104 * multiple of 8 regardless of what combination of TCP Options
4105 * are in use (always a multiple of 4 bytes) which is
4106 * important for performance reasons. For instance, if no
4107 * options are in use, then we have a 20-byte IP header and a
4108 * 20-byte TCP header. In this case, a 1500-byte MSS would
4109 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4110 * which is not a multiple of 8. So using an MSS of 1488 in
4111 * this case results in a TCP Data Payload of 1448 bytes which
4112 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4113 * Stamps have been negotiated, then an MTU of 1500 bytes
4114 * results in a TCP Data Payload of 1448 bytes which, as
4115 * above, is a multiple of 8 bytes ...
4116 */
4117 for (i = 0; i < NMTUS; i++)
4118 if (adap->params.mtus[i] == 1492) {
4119 adap->params.mtus[i] = 1488;
4120 break;
4121 }
4122
4123 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4124 adap->params.b_wnd);
4125 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304126 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004127 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304128 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004129 return 0;
4130
4131 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004132 * Something bad happened. If a command timed out or failed with EIO
4133 * FW does not operate within its spec or something catastrophic
4134 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004135 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004136bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304137 kfree(adap->sge.egr_map);
4138 kfree(adap->sge.ingr_map);
4139 kfree(adap->sge.starving_fl);
4140 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304141#ifdef CONFIG_DEBUG_FS
4142 kfree(adap->sge.blocked_fl);
4143#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004144 if (ret != -ETIMEDOUT && ret != -EIO)
4145 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004146 return ret;
4147}
4148
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004149/* EEH callbacks */
4150
4151static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4152 pci_channel_state_t state)
4153{
4154 int i;
4155 struct adapter *adap = pci_get_drvdata(pdev);
4156
4157 if (!adap)
4158 goto out;
4159
4160 rtnl_lock();
4161 adap->flags &= ~FW_OK;
4162 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004163 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004164 for_each_port(adap, i) {
4165 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004166 if (dev) {
4167 netif_device_detach(dev);
4168 netif_carrier_off(dev);
4169 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004170 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004171 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304172 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004173 if (adap->flags & FULL_INIT_DONE)
4174 cxgb_down(adap);
4175 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004176 if ((adap->flags & DEV_ENABLED)) {
4177 pci_disable_device(pdev);
4178 adap->flags &= ~DEV_ENABLED;
4179 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004180out: return state == pci_channel_io_perm_failure ?
4181 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4182}
4183
4184static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4185{
4186 int i, ret;
4187 struct fw_caps_config_cmd c;
4188 struct adapter *adap = pci_get_drvdata(pdev);
4189
4190 if (!adap) {
4191 pci_restore_state(pdev);
4192 pci_save_state(pdev);
4193 return PCI_ERS_RESULT_RECOVERED;
4194 }
4195
Gavin Shan144be3d2014-01-23 12:27:34 +08004196 if (!(adap->flags & DEV_ENABLED)) {
4197 if (pci_enable_device(pdev)) {
4198 dev_err(&pdev->dev, "Cannot reenable PCI "
4199 "device after reset\n");
4200 return PCI_ERS_RESULT_DISCONNECT;
4201 }
4202 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004203 }
4204
4205 pci_set_master(pdev);
4206 pci_restore_state(pdev);
4207 pci_save_state(pdev);
4208 pci_cleanup_aer_uncorrect_error_status(pdev);
4209
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304210 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004211 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304212 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004213 return PCI_ERS_RESULT_DISCONNECT;
4214 adap->flags |= FW_OK;
4215 if (adap_init1(adap, &c))
4216 return PCI_ERS_RESULT_DISCONNECT;
4217
4218 for_each_port(adap, i) {
4219 struct port_info *p = adap2pinfo(adap, i);
4220
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304221 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004222 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004223 if (ret < 0)
4224 return PCI_ERS_RESULT_DISCONNECT;
4225 p->viid = ret;
4226 p->xact_addr_filt = -1;
4227 }
4228
4229 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4230 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004231 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004232 if (cxgb_up(adap))
4233 return PCI_ERS_RESULT_DISCONNECT;
4234 return PCI_ERS_RESULT_RECOVERED;
4235}
4236
4237static void eeh_resume(struct pci_dev *pdev)
4238{
4239 int i;
4240 struct adapter *adap = pci_get_drvdata(pdev);
4241
4242 if (!adap)
4243 return;
4244
4245 rtnl_lock();
4246 for_each_port(adap, i) {
4247 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004248 if (dev) {
4249 if (netif_running(dev)) {
4250 link_start(dev);
4251 cxgb_set_rxmode(dev);
4252 }
4253 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004254 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004255 }
4256 rtnl_unlock();
4257}
4258
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004259static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004260 .error_detected = eeh_err_detected,
4261 .slot_reset = eeh_slot_reset,
4262 .resume = eeh_resume,
4263};
4264
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304265/* Return true if the Link Configuration supports "High Speeds" (those greater
4266 * than 1Gb/s).
4267 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304268static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004269{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304270 unsigned int speeds, high_speeds;
4271
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304272 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4273 high_speeds = speeds &
4274 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304275
4276 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004277}
4278
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004279/*
4280 * Perform default configuration of DMA queues depending on the number and type
4281 * of ports we found and the number of available CPUs. Most settings can be
4282 * modified by the admin prior to actual use.
4283 */
Bill Pemberton91744942012-12-03 09:23:02 -05004284static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004285{
4286 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304287 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004288#ifndef CONFIG_CHELSIO_T4_DCB
4289 int q10g = 0;
4290#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004291
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304292 /* Reduce memory usage in kdump environment, disable all offload.
4293 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304294 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304295 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304296 adap->params.crypto = 0;
4297 }
4298
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304299 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004300#ifdef CONFIG_CHELSIO_T4_DCB
4301 /* For Data Center Bridging support we need to be able to support up
4302 * to 8 Traffic Priorities; each of which will be assigned to its
4303 * own TX Queue in order to prevent Head-Of-Line Blocking.
4304 */
4305 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4306 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4307 MAX_ETH_QSETS, adap->params.nports * 8);
4308 BUG_ON(1);
4309 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004310
Anish Bhatt688848b2014-06-19 21:37:13 -07004311 for_each_port(adap, i) {
4312 struct port_info *pi = adap2pinfo(adap, i);
4313
4314 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304315 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004316 qidx += pi->nqsets;
4317 }
4318#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004319 /*
4320 * We default to 1 queue per non-10G port and up to # of cores queues
4321 * per 10G port.
4322 */
4323 if (n10g)
4324 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004325 if (q10g > netif_get_num_default_rss_queues())
4326 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004327
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304328 if (is_kdump_kernel())
4329 q10g = 1;
4330
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004331 for_each_port(adap, i) {
4332 struct port_info *pi = adap2pinfo(adap, i);
4333
4334 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304335 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004336 qidx += pi->nqsets;
4337 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004338#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004339
4340 s->ethqsets = qidx;
4341 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4342
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304343 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004344 /*
4345 * For offload we use 1 queue/channel if all ports are up to 1G,
4346 * otherwise we divide all available queues amongst the channels
4347 * capped by the number of available cores.
4348 */
4349 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304350 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304351 s->ofldqsets = roundup(i, adap->params.nports);
4352 } else {
4353 s->ofldqsets = adap->params.nports;
4354 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004355 }
4356
4357 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4358 struct sge_eth_rxq *r = &s->ethrxq[i];
4359
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304360 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004361 r->fl.size = 72;
4362 }
4363
4364 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4365 s->ethtxq[i].q.size = 1024;
4366
4367 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4368 s->ctrlq[i].q.size = 512;
4369
Atul Guptaa45695042017-07-04 16:46:20 +05304370 if (!is_t4(adap->params.chip))
4371 s->ptptxq.q.size = 8;
4372
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304373 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304374 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004375}
4376
4377/*
4378 * Reduce the number of Ethernet queues across all ports to at most n.
4379 * n provides at least one queue per port.
4380 */
Bill Pemberton91744942012-12-03 09:23:02 -05004381static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004382{
4383 int i;
4384 struct port_info *pi;
4385
4386 while (n < adap->sge.ethqsets)
4387 for_each_port(adap, i) {
4388 pi = adap2pinfo(adap, i);
4389 if (pi->nqsets > 1) {
4390 pi->nqsets--;
4391 adap->sge.ethqsets--;
4392 if (adap->sge.ethqsets <= n)
4393 break;
4394 }
4395 }
4396
4397 n = 0;
4398 for_each_port(adap, i) {
4399 pi = adap2pinfo(adap, i);
4400 pi->first_qset = n;
4401 n += pi->nqsets;
4402 }
4403}
4404
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304405static int get_msix_info(struct adapter *adap)
4406{
4407 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304408 unsigned int max_ingq = 0;
4409
4410 if (is_offload(adap))
4411 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4412 if (is_pci_uld(adap))
4413 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4414
4415 if (!max_ingq)
4416 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304417
4418 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4419 if (!msix_info)
4420 return -ENOMEM;
4421
4422 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4423 sizeof(long), GFP_KERNEL);
4424 if (!adap->msix_bmap_ulds.msix_bmap) {
4425 kfree(msix_info);
4426 return -ENOMEM;
4427 }
4428 spin_lock_init(&adap->msix_bmap_ulds.lock);
4429 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304430out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304431 return 0;
4432}
4433
4434static void free_msix_info(struct adapter *adap)
4435{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304436 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304437 return;
4438
4439 kfree(adap->msix_info_ulds);
4440 kfree(adap->msix_bmap_ulds.msix_bmap);
4441}
4442
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004443/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4444#define EXTRA_VECS 2
4445
Bill Pemberton91744942012-12-03 09:23:02 -05004446static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004447{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304448 int ofld_need = 0, uld_need = 0;
4449 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004450 struct sge *s = &adap->sge;
4451 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304452 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304453 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004454
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304455 if (is_pci_uld(adap))
4456 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4457 if (is_offload(adap))
4458 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304459 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304460 GFP_KERNEL);
4461 if (!entries)
4462 return -ENOMEM;
4463
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304464 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304465 if (get_msix_info(adap)) {
4466 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304467 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304468 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304469
4470 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004471 entries[i].entry = i;
4472
4473 want = s->max_ethqsets + EXTRA_VECS;
4474 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304475 want += adap->num_ofld_uld * s->ofldqsets;
4476 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004477 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304478 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304479 want += adap->num_uld * s->ofldqsets;
4480 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304481 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004482#ifdef CONFIG_CHELSIO_T4_DCB
4483 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4484 * each port.
4485 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304486 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004487#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304488 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004489#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304490 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4491 if (allocated < 0) {
4492 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4493 " not using MSI-X\n");
4494 kfree(entries);
4495 return allocated;
4496 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004497
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304498 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004499 * Every group gets its minimum requirement and NIC gets top
4500 * priority for leftovers.
4501 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304502 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004503 if (i < s->max_ethqsets) {
4504 s->max_ethqsets = i;
4505 if (i < s->ethqsets)
4506 reduce_ethqs(adap, i);
4507 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304508 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304509 if (allocated < want)
4510 s->nqs_per_uld = nchan;
4511 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304512 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304513 }
4514
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304515 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004516 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304517 if (is_uld(adap)) {
4518 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304519 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304520 adap->msix_info_ulds[j].idx = i;
4521 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304522 adap->msix_bmap_ulds.mapsize = j;
4523 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304524 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304525 "nic %d per uld %d\n",
4526 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004527
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304528 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004529 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004530}
4531
4532#undef EXTRA_VECS
4533
Bill Pemberton91744942012-12-03 09:23:02 -05004534static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004535{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304536 unsigned int i;
4537 int err;
4538
4539 err = t4_init_rss_mode(adap, adap->mbox);
4540 if (err)
4541 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004542
4543 for_each_port(adap, i) {
4544 struct port_info *pi = adap2pinfo(adap, i);
4545
4546 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4547 if (!pi->rss)
4548 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004549 }
4550 return 0;
4551}
4552
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304553static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4554 enum pci_bus_speed *speed,
4555 enum pcie_link_width *width)
4556{
4557 u32 lnkcap1, lnkcap2;
4558 int err1, err2;
4559
4560#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4561
4562 *speed = PCI_SPEED_UNKNOWN;
4563 *width = PCIE_LNK_WIDTH_UNKNOWN;
4564
4565 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4566 &lnkcap1);
4567 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4568 &lnkcap2);
4569 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4570 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4571 *speed = PCIE_SPEED_8_0GT;
4572 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4573 *speed = PCIE_SPEED_5_0GT;
4574 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4575 *speed = PCIE_SPEED_2_5GT;
4576 }
4577 if (!err1) {
4578 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4579 if (!lnkcap2) { /* pre-r3.0 */
4580 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4581 *speed = PCIE_SPEED_5_0GT;
4582 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4583 *speed = PCIE_SPEED_2_5GT;
4584 }
4585 }
4586
4587 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4588 return err1 ? err1 : err2 ? err2 : -EINVAL;
4589 return 0;
4590}
4591
4592static void cxgb4_check_pcie_caps(struct adapter *adap)
4593{
4594 enum pcie_link_width width, width_cap;
4595 enum pci_bus_speed speed, speed_cap;
4596
4597#define PCIE_SPEED_STR(speed) \
4598 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4599 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4600 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4601 "Unknown")
4602
4603 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4604 dev_warn(adap->pdev_dev,
4605 "Unable to determine PCIe device BW capabilities\n");
4606 return;
4607 }
4608
4609 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4610 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4611 dev_warn(adap->pdev_dev,
4612 "Unable to determine PCI Express bandwidth.\n");
4613 return;
4614 }
4615
4616 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4617 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4618 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4619 width, width_cap);
4620 if (speed < speed_cap || width < width_cap)
4621 dev_info(adap->pdev_dev,
4622 "A slot with more lanes and/or higher speed is "
4623 "suggested for optimal performance.\n");
4624}
4625
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304626/* Dump basic information about the adapter */
4627static void print_adapter_info(struct adapter *adapter)
4628{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304629 /* Hardware/Firmware/etc. Version/Revision IDs */
4630 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304631
4632 /* Software/Hardware configuration */
4633 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4634 is_offload(adapter) ? "R" : "",
4635 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4636 (adapter->flags & USING_MSI) ? "MSI" : ""),
4637 is_offload(adapter) ? "Offload" : "non-Offload");
4638}
4639
Bill Pemberton91744942012-12-03 09:23:02 -05004640static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004641{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004642 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004643 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004644 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004645 const struct port_info *pi = netdev_priv(dev);
4646 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004647
4648 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4649 spd = " 2.5 GT/s";
4650 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4651 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004652 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4653 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004654
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304655 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304656 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304657 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304658 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304659 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004660 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304661 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304662 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304663 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304664 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304665 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4666 bufp += sprintf(bufp, "50G/");
4667 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304668 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304669 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4670 bufp += sprintf(bufp, "200G/");
4671 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4672 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004673 if (bufp != buf)
4674 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304675 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004676
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304677 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4678 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004679}
4680
Dimitris Michailidis06546392010-07-11 12:01:16 +00004681/*
4682 * Free the following resources:
4683 * - memory used for tables
4684 * - MSI/MSI-X
4685 * - net devices
4686 * - resources FW is holding for us
4687 */
4688static void free_some_resources(struct adapter *adapter)
4689{
4690 unsigned int i;
4691
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05304692 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07004693 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304694 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004695 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304696 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304697 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304698 kfree(adapter->sge.egr_map);
4699 kfree(adapter->sge.ingr_map);
4700 kfree(adapter->sge.starving_fl);
4701 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304702#ifdef CONFIG_DEBUG_FS
4703 kfree(adapter->sge.blocked_fl);
4704#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004705 disable_msi(adapter);
4706
4707 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004708 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304709 struct port_info *pi = adap2pinfo(adapter, i);
4710
4711 if (pi->viid != 0)
4712 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4713 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004714 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004715 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004716 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004717 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304718 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004719}
4720
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004721#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004722#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004723 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004724#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004725
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304726static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4727{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304728 u16 device_id;
4729
4730 /* Retrieve adapter's device ID */
4731 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004732
4733 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304734 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004735 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304736 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004737 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304738 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004739 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304740 default:
4741 dev_err(&pdev->dev, "Device %d is not supported\n",
4742 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304743 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004744 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304745}
4746
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304747#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304748static void dummy_setup(struct net_device *dev)
4749{
4750 dev->type = ARPHRD_NONE;
4751 dev->mtu = 0;
4752 dev->hard_header_len = 0;
4753 dev->addr_len = 0;
4754 dev->tx_queue_len = 0;
4755 dev->flags |= IFF_NOARP;
4756 dev->priv_flags |= IFF_NO_QUEUE;
4757
4758 /* Initialize the device structure. */
4759 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4760 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004761 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304762}
4763
4764static int config_mgmt_dev(struct pci_dev *pdev)
4765{
4766 struct adapter *adap = pci_get_drvdata(pdev);
4767 struct net_device *netdev;
4768 struct port_info *pi;
4769 char name[IFNAMSIZ];
4770 int err;
4771
4772 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304773 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4774 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304775 if (!netdev)
4776 return -ENOMEM;
4777
4778 pi = netdev_priv(netdev);
4779 pi->adapter = adap;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304780 pi->tx_chan = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304781 SET_NETDEV_DEV(netdev, &pdev->dev);
4782
4783 adap->port[0] = netdev;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304784 pi->port_id = 0;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304785
4786 err = register_netdev(adap->port[0]);
4787 if (err) {
4788 pr_info("Unable to register VF mgmt netdev %s\n", name);
4789 free_netdev(adap->port[0]);
4790 adap->port[0] = NULL;
4791 return err;
4792 }
4793 return 0;
4794}
4795
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304796static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4797{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304798 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304799 int err = 0;
4800 int current_vfs = pci_num_vf(pdev);
4801 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304802
Hariprasad Shenai78294512016-08-11 21:06:23 +05304803 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304804 /* Check if cxgb4 is the MASTER and fw is initialized */
4805 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4806 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4807 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4808 dev_warn(&pdev->dev,
4809 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4810 return -EOPNOTSUPP;
4811 }
4812
4813 /* If any of the VF's is already assigned to Guest OS, then
4814 * SRIOV for the same cannot be modified
4815 */
4816 if (current_vfs && pci_vfs_assigned(pdev)) {
4817 dev_err(&pdev->dev,
4818 "Cannot modify SR-IOV while VFs are assigned\n");
4819 num_vfs = current_vfs;
4820 return num_vfs;
4821 }
4822
4823 /* Disable SRIOV when zero is passed.
4824 * One needs to disable SRIOV before modifying it, else
4825 * stack throws the below warning:
4826 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4827 */
4828 if (!num_vfs) {
4829 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304830 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304831 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304832 adap->port[0] = NULL;
4833 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304834 /* free VF resources */
4835 kfree(adap->vfinfo);
4836 adap->vfinfo = NULL;
4837 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304838 return num_vfs;
4839 }
4840
4841 if (num_vfs != current_vfs) {
4842 err = pci_enable_sriov(pdev, num_vfs);
4843 if (err)
4844 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304845
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304846 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304847 err = config_mgmt_dev(pdev);
4848 if (err)
4849 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304850 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304851
4852 adap->vfinfo = kcalloc(adap->num_vfs,
4853 sizeof(struct vf_info), GFP_KERNEL);
4854 if (adap->vfinfo)
4855 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304856 return num_vfs;
4857}
4858#endif
4859
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004860static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004861{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004862 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004863 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004864 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004865 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304866 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304867 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304868 u32 whoami, pl_rev;
4869 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304870 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004871#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304872 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004873#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004874
4875 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4876
4877 err = pci_request_regions(pdev, KBUILD_MODNAME);
4878 if (err) {
4879 /* Just info, some other driver may have claimed the device. */
4880 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4881 return err;
4882 }
4883
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004884 err = pci_enable_device(pdev);
4885 if (err) {
4886 dev_err(&pdev->dev, "cannot enable PCI device\n");
4887 goto out_release_regions;
4888 }
4889
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304890 regs = pci_ioremap_bar(pdev, 0);
4891 if (!regs) {
4892 dev_err(&pdev->dev, "cannot map device registers\n");
4893 err = -ENOMEM;
4894 goto out_disable_device;
4895 }
4896
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304897 err = t4_wait_dev_ready(regs);
4898 if (err < 0)
4899 goto out_unmap_bar0;
4900
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304901 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304902 whoami = readl(regs + PL_WHOAMI_A);
4903 pl_rev = REV_G(readl(regs + PL_REV_A));
4904 chip = get_chip_type(pdev, pl_rev);
4905 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4906 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304907 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304908#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304909 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304910#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304911 pci_disable_device(pdev);
4912 pci_save_state(pdev); /* to restore SR-IOV later */
4913 goto sriov;
4914 }
4915
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004916 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004917 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004918 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4919 if (err) {
4920 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4921 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304922 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004923 }
4924 } else {
4925 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4926 if (err) {
4927 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304928 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004929 }
4930 }
4931
4932 pci_enable_pcie_error_reporting(pdev);
4933 pci_set_master(pdev);
4934 pci_save_state(pdev);
4935
4936 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4937 if (!adapter) {
4938 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304939 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004940 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304941 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004942
Anish Bhatt29aaee62014-08-20 13:44:06 -07004943 adapter->workq = create_singlethread_workqueue("cxgb4");
4944 if (!adapter->workq) {
4945 err = -ENOMEM;
4946 goto out_free_adapter;
4947 }
4948
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304949 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4950 (sizeof(struct mbox_cmd) *
4951 T4_OS_LOG_MBOX_CMDS),
4952 GFP_KERNEL);
4953 if (!adapter->mbox_log) {
4954 err = -ENOMEM;
4955 goto out_free_adapter;
4956 }
4957 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4958
Gavin Shan144be3d2014-01-23 12:27:34 +08004959 /* PCI device has been enabled */
4960 adapter->flags |= DEV_ENABLED;
4961
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304962 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004963 adapter->pdev = pdev;
4964 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304965 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304966 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304967 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05304968 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004969 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4970
Casey Leedomb0ba9d52017-08-15 11:23:26 +08004971 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
4972 * Ingress Packet Data to Free List Buffers in order to allow for
4973 * chipset performance optimizations between the Root Complex and
4974 * Memory Controllers. (Messages to the associated Ingress Queue
4975 * notifying new Packet Placement in the Free Lists Buffers will be
4976 * send without the Relaxed Ordering Attribute thus guaranteeing that
4977 * all preceding PCIe Transaction Layer Packets will be processed
4978 * first.) But some Root Complexes have various issues with Upstream
4979 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
4980 * The PCIe devices which under the Root Complexes will be cleared the
4981 * Relaxed Ordering bit in the configuration space, So we check our
4982 * PCIe configuration space to see if it's flagged with advice against
4983 * using Relaxed Ordering.
4984 */
4985 if (!pcie_relaxed_ordering_enabled(pdev))
4986 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
4987
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004988 spin_lock_init(&adapter->stats_lock);
4989 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004990 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05304991 spin_lock_init(&adapter->mbox_lock);
4992
4993 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004994
4995 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304996 INIT_WORK(&adapter->db_full_task, process_db_full);
4997 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004998
4999 err = t4_prep_adapter(adapter);
5000 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305001 goto out_free_adapter;
5002
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005003
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305004 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305005 s_qpp = (QUEUESPERPAGEPF0_S +
5006 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305007 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305008 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5009 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005010 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5011
5012 /* Each segment size is 128B. Write coalescing is enabled only
5013 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5014 * queue is less no of segments that can be accommodated in
5015 * a page size.
5016 */
5017 if (qpp > num_seg) {
5018 dev_err(&pdev->dev,
5019 "Incorrect number of egress queues per page\n");
5020 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305021 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005022 }
5023 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5024 pci_resource_len(pdev, 2));
5025 if (!adapter->bar2) {
5026 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5027 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305028 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005029 }
5030 }
5031
Vipul Pandya636f9d32012-09-26 02:39:39 +00005032 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005033 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305034#ifdef CONFIG_DEBUG_FS
5035 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5036#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005037 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005038 if (err)
5039 goto out_unmap_bar;
5040
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305041 /* configure SGE_STAT_CFG_A to read WC stats */
5042 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305043 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5044 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5045 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305046
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005047 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005048 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5049 MAX_ETH_QSETS);
5050 if (!netdev) {
5051 err = -ENOMEM;
5052 goto out_free_dev;
5053 }
5054
5055 SET_NETDEV_DEV(netdev, &pdev->dev);
5056
5057 adapter->port[i] = netdev;
5058 pi = netdev_priv(netdev);
5059 pi->adapter = adapter;
5060 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005061 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005062 netdev->irq = pdev->irq;
5063
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005064 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5065 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5066 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305067 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5068 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005069 if (highdma)
5070 netdev->hw_features |= NETIF_F_HIGHDMA;
5071 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005072 netdev->vlan_features = netdev->features & VLAN_FEAT;
5073
Jiri Pirko01789342011-08-16 06:29:00 +00005074 netdev->priv_flags |= IFF_UNICAST_FLT;
5075
Jarod Wilsond894be52016-10-20 13:55:16 -04005076 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305077 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005078 netdev->max_mtu = MAX_MTU;
5079
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005080 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005081#ifdef CONFIG_CHELSIO_T4_DCB
5082 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5083 cxgb4_dcb_state_init(netdev);
5084#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305085 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005086 }
5087
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305088 cxgb4_init_ethtool_dump(adapter);
5089
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005090 pci_set_drvdata(pdev, adapter);
5091
5092 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005093 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005094 if (err)
5095 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305096 } else if (adapter->params.nports == 1) {
5097 /* If we don't have a connection to the firmware -- possibly
5098 * because of an error -- grab the raw VPD parameters so we
5099 * can set the proper MAC Address on the debug network
5100 * interface that we've created.
5101 */
5102 u8 hw_addr[ETH_ALEN];
5103 u8 *na = adapter->params.vpd.na;
5104
5105 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5106 if (!err) {
5107 for (i = 0; i < ETH_ALEN; i++)
5108 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5109 hex2val(na[2 * i + 1]));
5110 t4_set_hw_addr(adapter, 0, hw_addr);
5111 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005112 }
5113
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305114 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005115 * soon as the first register_netdev completes.
5116 */
5117 cfg_queues(adapter);
5118
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305119 adapter->smt = t4_init_smt();
5120 if (!adapter->smt) {
5121 /* We tolerate a lack of SMT, giving up some functionality */
5122 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5123 }
5124
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305125 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005126 if (!adapter->l2t) {
5127 /* We tolerate a lack of L2T, giving up some functionality */
5128 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5129 adapter->params.offload = 0;
5130 }
5131
Anish Bhattb5a02f52015-01-14 15:17:34 -08005132#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305133 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5134 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5135 /* CLIP functionality is not present in hardware,
5136 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005137 */
5138 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305139 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005140 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305141 } else {
5142 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5143 adapter->clipt_end);
5144 if (!adapter->clipt) {
5145 /* We tolerate a lack of clip_table, giving up
5146 * some functionality
5147 */
5148 dev_warn(&pdev->dev,
5149 "could not allocate Clip table, continuing\n");
5150 adapter->params.offload = 0;
5151 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005152 }
5153#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305154
5155 for_each_port(adapter, i) {
5156 pi = adap2pinfo(adapter, i);
5157 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5158 if (!pi->sched_tbl)
5159 dev_warn(&pdev->dev,
5160 "could not activate scheduling on port %d\n",
5161 i);
5162 }
5163
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305164 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005165 dev_warn(&pdev->dev, "could not allocate TID table, "
5166 "continuing\n");
5167 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305168 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305169 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305170 if (!adapter->tc_u32)
5171 dev_warn(&pdev->dev,
5172 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305173
5174 cxgb4_init_tc_flower(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005175 }
5176
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305177 if (is_offload(adapter)) {
5178 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5179 u32 hash_base, hash_reg;
5180
5181 if (chip <= CHELSIO_T5) {
5182 hash_reg = LE_DB_TID_HASHBASE_A;
5183 hash_base = t4_read_reg(adapter, hash_reg);
5184 adapter->tids.hash_base = hash_base / 4;
5185 } else {
5186 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5187 hash_base = t4_read_reg(adapter, hash_reg);
5188 adapter->tids.hash_base = hash_base;
5189 }
5190 }
5191 }
5192
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005193 /* See what interrupts we'll be using */
5194 if (msi > 1 && enable_msix(adapter) == 0)
5195 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305196 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005197 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305198 if (msi > 1)
5199 free_msix_info(adapter);
5200 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005201
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305202 /* check for PCI Express bandwidth capabiltites */
5203 cxgb4_check_pcie_caps(adapter);
5204
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005205 err = init_rss(adapter);
5206 if (err)
5207 goto out_free_dev;
5208
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005209 /*
5210 * The card is now ready to go. If any errors occur during device
5211 * registration we do not fail the whole card but rather proceed only
5212 * with the ports we manage to register successfully. However we must
5213 * register at least one net device.
5214 */
5215 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005216 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305217 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005218 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5219 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5220
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305221 netif_carrier_off(adapter->port[i]);
5222
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005223 err = register_netdev(adapter->port[i]);
5224 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005225 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005226 adapter->chan_map[pi->tx_chan] = i;
5227 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005228 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005229 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005230 dev_err(&pdev->dev, "could not register any net devices\n");
5231 goto out_free_dev;
5232 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005233 if (err) {
5234 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5235 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005236 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005237
5238 if (cxgb4_debugfs_root) {
5239 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5240 cxgb4_debugfs_root);
5241 setup_debugfs(adapter);
5242 }
5243
David S. Miller88c51002011-10-07 13:38:43 -04005244 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5245 pdev->needs_freset = 1;
5246
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305247 if (is_uld(adapter)) {
5248 mutex_lock(&uld_mutex);
5249 list_add_tail(&adapter->list_node, &adapter_list);
5250 mutex_unlock(&uld_mutex);
5251 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005252
Atul Gupta9c33e422017-07-04 16:46:21 +05305253 if (!is_t4(adapter->params.chip))
5254 cxgb4_ptp_init(adapter);
5255
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305256 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305257 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305258 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305259
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305260sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005261#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305262 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5263 if (!adapter) {
5264 err = -ENOMEM;
5265 goto free_pci_region;
5266 }
5267
Hariprasad Shenai78294512016-08-11 21:06:23 +05305268 adapter->pdev = pdev;
5269 adapter->pdev_dev = &pdev->dev;
5270 adapter->name = pci_name(pdev);
5271 adapter->mbox = func;
5272 adapter->pf = func;
5273 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305274 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305275 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5276 (sizeof(struct mbox_cmd) *
5277 T4_OS_LOG_MBOX_CMDS),
5278 GFP_KERNEL);
5279 if (!adapter->mbox_log) {
5280 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305281 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305282 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305283 spin_lock_init(&adapter->mbox_lock);
5284 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305285
5286 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5287 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5288 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5289 &v, &port_vec);
5290 if (err < 0) {
5291 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
Ganesh Goudard0417842017-06-09 19:26:24 +05305292 goto free_mbox_log;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305293 }
5294
5295 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305296 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005297 return 0;
5298
Ganesh Goudard0417842017-06-09 19:26:24 +05305299free_mbox_log:
5300 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305301 free_adapter:
5302 kfree(adapter);
5303 free_pci_region:
5304 iounmap(regs);
5305 pci_disable_sriov(pdev);
5306 pci_release_regions(pdev);
5307 return err;
5308#else
5309 return 0;
5310#endif
5311
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005312 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005313 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305314 if (adapter->flags & USING_MSIX)
5315 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305316 if (adapter->num_uld || adapter->num_ofld_uld)
5317 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005318 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305319 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005320 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005321 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005322 if (adapter->workq)
5323 destroy_workqueue(adapter->workq);
5324
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305325 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005326 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305327 out_unmap_bar0:
5328 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005329 out_disable_device:
5330 pci_disable_pcie_error_reporting(pdev);
5331 pci_disable_device(pdev);
5332 out_release_regions:
5333 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005334 return err;
5335}
5336
Bill Pemberton91744942012-12-03 09:23:02 -05005337static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005338{
5339 struct adapter *adapter = pci_get_drvdata(pdev);
5340
Hariprasad Shenai78294512016-08-11 21:06:23 +05305341 if (!adapter) {
5342 pci_release_regions(pdev);
5343 return;
5344 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005345
Ganesh Goudare1f61982017-09-21 12:50:47 +05305346 adapter->flags |= SHUTTING_DOWN;
5347
Hariprasad Shenai78294512016-08-11 21:06:23 +05305348 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005349 int i;
5350
Anish Bhatt29aaee62014-08-20 13:44:06 -07005351 /* Tear down per-adapter Work Queue first since it can contain
5352 * references to our adapter data structure.
5353 */
5354 destroy_workqueue(adapter->workq);
5355
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005356 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005357 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005358 t4_uld_clean_up(adapter);
5359 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005360
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305361 disable_interrupts(adapter);
5362
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005363 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005364 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005365 unregister_netdev(adapter->port[i]);
5366
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005367 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005368
Atul Gupta9c33e422017-07-04 16:46:21 +05305369 if (!is_t4(adapter->params.chip))
5370 cxgb4_ptp_stop(adapter);
5371
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005372 /* If we allocated filters, free up state associated with any
5373 * valid filters ...
5374 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305375 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005376
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005377 if (adapter->flags & FULL_INIT_DONE)
5378 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005379
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305380 if (adapter->flags & USING_MSIX)
5381 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305382 if (adapter->num_uld || adapter->num_ofld_uld)
5383 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005384 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005385#if IS_ENABLED(CONFIG_IPV6)
5386 t4_cleanup_clip_tbl(adapter);
5387#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005388 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305389 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005390 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005391 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005392 if ((adapter->flags & DEV_ENABLED)) {
5393 pci_disable_device(pdev);
5394 adapter->flags &= ~DEV_ENABLED;
5395 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005396 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305397 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005398 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005399 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305400 }
5401#ifdef CONFIG_PCI_IOV
5402 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305403 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305404 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305405 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305406 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305407 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305408 kfree(adapter);
5409 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005410 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305411 }
5412#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005413}
5414
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305415/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5416 * delivery. This is essentially a stripped down version of the PCI remove()
5417 * function where we do the minimal amount of work necessary to shutdown any
5418 * further activity.
5419 */
5420static void shutdown_one(struct pci_dev *pdev)
5421{
5422 struct adapter *adapter = pci_get_drvdata(pdev);
5423
5424 /* As with remove_one() above (see extended comment), we only want do
5425 * do cleanup on PCI Devices which went all the way through init_one()
5426 * ...
5427 */
5428 if (!adapter) {
5429 pci_release_regions(pdev);
5430 return;
5431 }
5432
Ganesh Goudare1f61982017-09-21 12:50:47 +05305433 adapter->flags |= SHUTTING_DOWN;
5434
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305435 if (adapter->pf == 4) {
5436 int i;
5437
5438 for_each_port(adapter, i)
5439 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5440 cxgb_close(adapter->port[i]);
5441
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005442 if (is_uld(adapter)) {
5443 detach_ulds(adapter);
5444 t4_uld_clean_up(adapter);
5445 }
5446
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305447 disable_interrupts(adapter);
5448 disable_msi(adapter);
5449
5450 t4_sge_stop(adapter);
5451 if (adapter->flags & FW_OK)
5452 t4_fw_bye(adapter, adapter->mbox);
5453 }
5454#ifdef CONFIG_PCI_IOV
5455 else {
5456 if (adapter->port[0])
5457 unregister_netdev(adapter->port[0]);
5458 iounmap(adapter->regs);
5459 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305460 kfree(adapter->mbox_log);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305461 kfree(adapter);
5462 pci_disable_sriov(pdev);
5463 pci_release_regions(pdev);
5464 }
5465#endif
5466}
5467
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005468static struct pci_driver cxgb4_driver = {
5469 .name = KBUILD_MODNAME,
5470 .id_table = cxgb4_pci_tbl,
5471 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005472 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305473 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305474#ifdef CONFIG_PCI_IOV
5475 .sriov_configure = cxgb4_iov_configure,
5476#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005477 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005478};
5479
5480static int __init cxgb4_init_module(void)
5481{
5482 int ret;
5483
5484 /* Debugfs support is optional, just warn if this fails */
5485 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5486 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005487 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005488
5489 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005490 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005491 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305492
Anish Bhatt1bb60372014-10-14 20:07:22 -07005493#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005494 if (!inet6addr_registered) {
5495 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5496 inet6addr_registered = true;
5497 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005498#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305499
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005500 return ret;
5501}
5502
5503static void __exit cxgb4_cleanup_module(void)
5504{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005505#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305506 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005507 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5508 inet6addr_registered = false;
5509 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005510#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005511 pci_unregister_driver(&cxgb4_driver);
5512 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005513}
5514
5515module_init(cxgb4_init_module);
5516module_exit(cxgb4_cleanup_module);