blob: 796b37de464f08d6d088a3c999e87fbd1484dee4 [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{
2892 if (!is_classid_clsact_ingress(cls_flower->common.classid) ||
2893 cls_flower->common.chain_index)
2894 return -EOPNOTSUPP;
2895
2896 switch (cls_flower->command) {
2897 case TC_CLSFLOWER_REPLACE:
2898 return cxgb4_tc_flower_replace(dev, cls_flower);
2899 case TC_CLSFLOWER_DESTROY:
2900 return cxgb4_tc_flower_destroy(dev, cls_flower);
2901 case TC_CLSFLOWER_STATS:
2902 return cxgb4_tc_flower_stats(dev, cls_flower);
2903 default:
2904 return -EOPNOTSUPP;
2905 }
2906}
2907
Jiri Pirkof7323042017-08-07 10:15:20 +02002908static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002909 struct tc_cls_u32_offload *cls_u32)
2910{
Chris Mi7f3b39d2017-08-18 07:24:20 -04002911 if (!is_classid_clsact_ingress(cls_u32->common.classid) ||
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002912 cls_u32->common.chain_index)
Jiri Pirkof7323042017-08-07 10:15:20 +02002913 return -EOPNOTSUPP;
2914
2915 switch (cls_u32->command) {
2916 case TC_CLSU32_NEW_KNODE:
2917 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002918 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002919 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002920 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002921 default:
2922 return -EOPNOTSUPP;
2923 }
2924}
2925
Jiri Pirko2572ac52017-08-07 10:15:17 +02002926static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02002927 void *type_data)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302928{
2929 struct port_info *pi = netdev2pinfo(dev);
2930 struct adapter *adap = netdev2adap(dev);
2931
2932 if (!(adap->flags & FULL_INIT_DONE)) {
2933 dev_err(adap->pdev_dev,
2934 "Failed to setup tc on port %d. Link Down?\n",
2935 pi->port_id);
2936 return -EINVAL;
2937 }
2938
Jiri Pirkof7323042017-08-07 10:15:20 +02002939 switch (type) {
2940 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002941 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302942 case TC_SETUP_CLSFLOWER:
2943 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002944 default:
2945 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302946 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302947}
2948
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302949static netdev_features_t cxgb_fix_features(struct net_device *dev,
2950 netdev_features_t features)
2951{
2952 /* Disable GRO, if RX_CSUM is disabled */
2953 if (!(features & NETIF_F_RXCSUM))
2954 features &= ~NETIF_F_GRO;
2955
2956 return features;
2957}
2958
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002959static const struct net_device_ops cxgb4_netdev_ops = {
2960 .ndo_open = cxgb_open,
2961 .ndo_stop = cxgb_close,
2962 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002963 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002964 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002965 .ndo_set_rx_mode = cxgb_set_rxmode,
2966 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00002967 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002968 .ndo_validate_addr = eth_validate_addr,
2969 .ndo_do_ioctl = cxgb_ioctl,
2970 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002971#ifdef CONFIG_NET_POLL_CONTROLLER
2972 .ndo_poll_controller = cxgb_netpoll,
2973#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05302974#ifdef CONFIG_CHELSIO_T4_FCOE
2975 .ndo_fcoe_enable = cxgb_fcoe_enable,
2976 .ndo_fcoe_disable = cxgb_fcoe_disable,
2977#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302978 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302979 .ndo_setup_tc = cxgb_setup_tc,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302980 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002981};
2982
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302983#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302984static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2985 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302986 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302987 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302988 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302989 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05302990};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302991#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05302992
2993static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2994{
2995 struct adapter *adapter = netdev2adap(dev);
2996
2997 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2998 strlcpy(info->version, cxgb4_driver_version,
2999 sizeof(info->version));
3000 strlcpy(info->bus_info, pci_name(adapter->pdev),
3001 sizeof(info->bus_info));
3002}
3003
3004static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3005 .get_drvinfo = get_drvinfo,
3006};
3007
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003008void t4_fatal_err(struct adapter *adap)
3009{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303010 int port;
3011
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003012 if (pci_channel_offline(adap->pdev))
3013 return;
3014
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303015 /* Disable the SGE since ULDs are going to free resources that
3016 * could be exposed to the adapter. RDMA MWs for example...
3017 */
3018 t4_shutdown_adapter(adap);
3019 for_each_port(adap, port) {
3020 struct net_device *dev = adap->port[port];
3021
3022 /* If we get here in very early initialization the network
3023 * devices may not have been set up yet.
3024 */
3025 if (!dev)
3026 continue;
3027
3028 netif_tx_stop_all_queues(dev);
3029 netif_carrier_off(dev);
3030 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003031 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3032}
3033
3034static void setup_memwin(struct adapter *adap)
3035{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303036 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003037
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303038 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003039}
3040
3041static void setup_memwin_rdma(struct adapter *adap)
3042{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003043 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303044 u32 start;
3045 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003046
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303047 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3048 start &= PCI_BASE_ADDRESS_MEM_MASK;
3049 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003050 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3051 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303052 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3053 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003054 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303055 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003056 adap->vres.ocq.start);
3057 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303058 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003059 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003060}
3061
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003062static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3063{
3064 u32 v;
3065 int ret;
3066
3067 /* get device capabilities */
3068 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303069 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3070 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303071 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303072 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003073 if (ret < 0)
3074 return ret;
3075
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303076 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3077 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303078 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003079 if (ret < 0)
3080 return ret;
3081
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303082 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003083 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303084 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3085 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003086 if (ret < 0)
3087 return ret;
3088
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303089 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303090 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3091 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003092 if (ret < 0)
3093 return ret;
3094
3095 t4_sge_init(adap);
3096
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003097 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303098 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303099 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303100 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3101 v = t4_read_reg(adap, TP_PIO_DATA_A);
3102 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003103
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003104 /* first 4 Tx modulation queues point to consecutive Tx channels */
3105 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303106 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3107 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003108
3109 /* associate each Tx modulation queue with consecutive Tx channels */
3110 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303111 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303112 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303113 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303114 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303115 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303116 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003117
3118#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3119 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303120 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3121 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3122 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3123 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3124 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3125 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3126 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3127 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3128 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3129 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003130 }
3131
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003132 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303133 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003134}
3135
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003136/*
3137 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3138 */
3139#define MAX_ATIDS 8192U
3140
3141/*
3142 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003143 *
3144 * If the firmware we're dealing with has Configuration File support, then
3145 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003146 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003147
3148/*
3149 * Tweak configuration based on module parameters, etc. Most of these have
3150 * defaults assigned to them by Firmware Configuration Files (if we're using
3151 * them) but need to be explicitly set if we're using hard-coded
3152 * initialization. But even in the case of using Firmware Configuration
3153 * Files, we'd like to expose the ability to change these via module
3154 * parameters so these are essentially common tweaks/settings for
3155 * Configuration Files and hard-coded initialization ...
3156 */
3157static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003158{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003159 /*
3160 * Fix up various Host-Dependent Parameters like Page Size, Cache
3161 * Line Size, etc. The firmware default is for a 4KB Page Size and
3162 * 64B Cache Line Size ...
3163 */
3164 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003165
Vipul Pandya636f9d32012-09-26 02:39:39 +00003166 /*
3167 * Process module parameters which affect early initialization.
3168 */
3169 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3170 dev_err(&adapter->pdev->dev,
3171 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3172 rx_dma_offset);
3173 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003174 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303175 t4_set_reg_field(adapter, SGE_CONTROL_A,
3176 PKTSHIFT_V(PKTSHIFT_M),
3177 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003178
Vipul Pandya636f9d32012-09-26 02:39:39 +00003179 /*
3180 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3181 * adds the pseudo header itself.
3182 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303183 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3184 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003185
3186 return 0;
3187}
3188
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303189/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3190 * unto themselves and they contain their own firmware to perform their
3191 * tasks ...
3192 */
3193static int phy_aq1202_version(const u8 *phy_fw_data,
3194 size_t phy_fw_size)
3195{
3196 int offset;
3197
3198 /* At offset 0x8 you're looking for the primary image's
3199 * starting offset which is 3 Bytes wide
3200 *
3201 * At offset 0xa of the primary image, you look for the offset
3202 * of the DRAM segment which is 3 Bytes wide.
3203 *
3204 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3205 * wide
3206 */
3207 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3208 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3209 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3210
3211 offset = le24(phy_fw_data + 0x8) << 12;
3212 offset = le24(phy_fw_data + offset + 0xa);
3213 return be16(phy_fw_data + offset + 0x27e);
3214
3215 #undef be16
3216 #undef le16
3217 #undef le24
3218}
3219
3220static struct info_10gbt_phy_fw {
3221 unsigned int phy_fw_id; /* PCI Device ID */
3222 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3223 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3224 int phy_flash; /* Has FLASH for PHY Firmware */
3225} phy_info_array[] = {
3226 {
3227 PHY_AQ1202_DEVICEID,
3228 PHY_AQ1202_FIRMWARE,
3229 phy_aq1202_version,
3230 1,
3231 },
3232 {
3233 PHY_BCM84834_DEVICEID,
3234 PHY_BCM84834_FIRMWARE,
3235 NULL,
3236 0,
3237 },
3238 { 0, NULL, NULL },
3239};
3240
3241static struct info_10gbt_phy_fw *find_phy_info(int devid)
3242{
3243 int i;
3244
3245 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3246 if (phy_info_array[i].phy_fw_id == devid)
3247 return &phy_info_array[i];
3248 }
3249 return NULL;
3250}
3251
3252/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3253 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3254 * we return a negative error number. If we transfer new firmware we return 1
3255 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3256 */
3257static int adap_init0_phy(struct adapter *adap)
3258{
3259 const struct firmware *phyf;
3260 int ret;
3261 struct info_10gbt_phy_fw *phy_info;
3262
3263 /* Use the device ID to determine which PHY file to flash.
3264 */
3265 phy_info = find_phy_info(adap->pdev->device);
3266 if (!phy_info) {
3267 dev_warn(adap->pdev_dev,
3268 "No PHY Firmware file found for this PHY\n");
3269 return -EOPNOTSUPP;
3270 }
3271
3272 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3273 * use that. The adapter firmware provides us with a memory buffer
3274 * where we can load a PHY firmware file from the host if we want to
3275 * override the PHY firmware File in flash.
3276 */
3277 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3278 adap->pdev_dev);
3279 if (ret < 0) {
3280 /* For adapters without FLASH attached to PHY for their
3281 * firmware, it's obviously a fatal error if we can't get the
3282 * firmware to the adapter. For adapters with PHY firmware
3283 * FLASH storage, it's worth a warning if we can't find the
3284 * PHY Firmware but we'll neuter the error ...
3285 */
3286 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3287 "/lib/firmware/%s, error %d\n",
3288 phy_info->phy_fw_file, -ret);
3289 if (phy_info->phy_flash) {
3290 int cur_phy_fw_ver = 0;
3291
3292 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3293 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3294 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3295 ret = 0;
3296 }
3297
3298 return ret;
3299 }
3300
3301 /* Load PHY Firmware onto adapter.
3302 */
3303 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3304 phy_info->phy_fw_version,
3305 (u8 *)phyf->data, phyf->size);
3306 if (ret < 0)
3307 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3308 -ret);
3309 else if (ret > 0) {
3310 int new_phy_fw_ver = 0;
3311
3312 if (phy_info->phy_fw_version)
3313 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3314 phyf->size);
3315 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3316 "Firmware /lib/firmware/%s, version %#x\n",
3317 phy_info->phy_fw_file, new_phy_fw_ver);
3318 }
3319
3320 release_firmware(phyf);
3321
3322 return ret;
3323}
3324
Vipul Pandya636f9d32012-09-26 02:39:39 +00003325/*
3326 * Attempt to initialize the adapter via a Firmware Configuration File.
3327 */
3328static int adap_init0_config(struct adapter *adapter, int reset)
3329{
3330 struct fw_caps_config_cmd caps_cmd;
3331 const struct firmware *cf;
3332 unsigned long mtype = 0, maddr = 0;
3333 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303334 int ret;
3335 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003336 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303337 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003338
3339 /*
3340 * Reset device if necessary.
3341 */
3342 if (reset) {
3343 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303344 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003345 if (ret < 0)
3346 goto bye;
3347 }
3348
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303349 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3350 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3351 * to be performed after any global adapter RESET above since some
3352 * PHYs only have local RAM copies of the PHY firmware.
3353 */
3354 if (is_10gbt_device(adapter->pdev->device)) {
3355 ret = adap_init0_phy(adapter);
3356 if (ret < 0)
3357 goto bye;
3358 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003359 /*
3360 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3361 * then use that. Otherwise, use the configuration file stored
3362 * in the adapter flash ...
3363 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303364 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003365 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303366 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003367 break;
3368 case CHELSIO_T5:
3369 fw_config_file = FW5_CFNAME;
3370 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303371 case CHELSIO_T6:
3372 fw_config_file = FW6_CFNAME;
3373 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003374 default:
3375 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3376 adapter->pdev->device);
3377 ret = -EINVAL;
3378 goto bye;
3379 }
3380
3381 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003382 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303383 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003384 mtype = FW_MEMTYPE_CF_FLASH;
3385 maddr = t4_flash_cfg_addr(adapter);
3386 } else {
3387 u32 params[7], val[7];
3388
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303389 sprintf(fw_config_file_path,
3390 "/lib/firmware/%s", fw_config_file);
3391 config_name = fw_config_file_path;
3392
Vipul Pandya636f9d32012-09-26 02:39:39 +00003393 if (cf->size >= FLASH_CFG_MAX_SIZE)
3394 ret = -ENOMEM;
3395 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303396 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3397 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003398 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303399 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003400 if (ret == 0) {
3401 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303402 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003403 * sizes have to be in terms of multiples of 4
3404 * bytes. So, if the Configuration File isn't
3405 * a multiple of 4 bytes in length we'll have
3406 * to write that out separately since we can't
3407 * guarantee that the bytes following the
3408 * residual byte in the buffer returned by
3409 * request_firmware() are zeroed out ...
3410 */
3411 size_t resid = cf->size & 0x3;
3412 size_t size = cf->size & ~0x3;
3413 __be32 *data = (__be32 *)cf->data;
3414
Hariprasad Shenai51678652014-11-21 12:52:02 +05303415 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3416 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003417
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303418 spin_lock(&adapter->win0_lock);
3419 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3420 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003421 if (ret == 0 && resid != 0) {
3422 union {
3423 __be32 word;
3424 char buf[4];
3425 } last;
3426 int i;
3427
3428 last.word = data[size >> 2];
3429 for (i = resid; i < 4; i++)
3430 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303431 ret = t4_memory_rw(adapter, 0, mtype,
3432 maddr + size,
3433 4, &last.word,
3434 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003435 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303436 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003437 }
3438 }
3439
3440 release_firmware(cf);
3441 if (ret)
3442 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003443 }
3444
Vipul Pandya636f9d32012-09-26 02:39:39 +00003445 /*
3446 * Issue a Capability Configuration command to the firmware to get it
3447 * to parse the Configuration File. We don't use t4_fw_config_file()
3448 * because we want the ability to modify various features after we've
3449 * processed the configuration file ...
3450 */
3451 memset(&caps_cmd, 0, sizeof(caps_cmd));
3452 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303453 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3454 FW_CMD_REQUEST_F |
3455 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303456 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303457 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3458 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3459 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003460 FW_LEN16(caps_cmd));
3461 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3462 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303463
3464 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3465 * Configuration File in FLASH), our last gasp effort is to use the
3466 * Firmware Configuration File which is embedded in the firmware. A
3467 * very few early versions of the firmware didn't have one embedded
3468 * but we can ignore those.
3469 */
3470 if (ret == -ENOENT) {
3471 memset(&caps_cmd, 0, sizeof(caps_cmd));
3472 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303473 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3474 FW_CMD_REQUEST_F |
3475 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303476 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3477 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3478 sizeof(caps_cmd), &caps_cmd);
3479 config_name = "Firmware Default";
3480 }
3481
3482 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003483 if (ret < 0)
3484 goto bye;
3485
Vipul Pandya636f9d32012-09-26 02:39:39 +00003486 finiver = ntohl(caps_cmd.finiver);
3487 finicsum = ntohl(caps_cmd.finicsum);
3488 cfcsum = ntohl(caps_cmd.cfcsum);
3489 if (finicsum != cfcsum)
3490 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3491 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3492 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003493
Vipul Pandya636f9d32012-09-26 02:39:39 +00003494 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003495 * And now tell the firmware to use the configuration we just loaded.
3496 */
3497 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303498 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3499 FW_CMD_REQUEST_F |
3500 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303501 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003502 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3503 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003504 if (ret < 0)
3505 goto bye;
3506
Vipul Pandya636f9d32012-09-26 02:39:39 +00003507 /*
3508 * Tweak configuration based on system architecture, module
3509 * parameters, etc.
3510 */
3511 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003512 if (ret < 0)
3513 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003514
Vipul Pandya636f9d32012-09-26 02:39:39 +00003515 /*
3516 * And finally tell the firmware to initialize itself using the
3517 * parameters from the Configuration File.
3518 */
3519 ret = t4_fw_initialize(adapter, adapter->mbox);
3520 if (ret < 0)
3521 goto bye;
3522
Hariprasad Shenai06640312015-01-13 15:19:25 +05303523 /* Emit Firmware Configuration File information and return
3524 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003525 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003526 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303527 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3528 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003529 return 0;
3530
3531 /*
3532 * Something bad happened. Return the error ... (If the "error"
3533 * is that there's no Configuration File on the adapter we don't
3534 * want to issue a warning since this is fairly common.)
3535 */
3536bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303537 if (config_issued && ret != -ENOENT)
3538 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3539 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003540 return ret;
3541}
3542
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303543static struct fw_info fw_info_array[] = {
3544 {
3545 .chip = CHELSIO_T4,
3546 .fs_name = FW4_CFNAME,
3547 .fw_mod_name = FW4_FNAME,
3548 .fw_hdr = {
3549 .chip = FW_HDR_CHIP_T4,
3550 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3551 .intfver_nic = FW_INTFVER(T4, NIC),
3552 .intfver_vnic = FW_INTFVER(T4, VNIC),
3553 .intfver_ri = FW_INTFVER(T4, RI),
3554 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3555 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3556 },
3557 }, {
3558 .chip = CHELSIO_T5,
3559 .fs_name = FW5_CFNAME,
3560 .fw_mod_name = FW5_FNAME,
3561 .fw_hdr = {
3562 .chip = FW_HDR_CHIP_T5,
3563 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3564 .intfver_nic = FW_INTFVER(T5, NIC),
3565 .intfver_vnic = FW_INTFVER(T5, VNIC),
3566 .intfver_ri = FW_INTFVER(T5, RI),
3567 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3568 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3569 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303570 }, {
3571 .chip = CHELSIO_T6,
3572 .fs_name = FW6_CFNAME,
3573 .fw_mod_name = FW6_FNAME,
3574 .fw_hdr = {
3575 .chip = FW_HDR_CHIP_T6,
3576 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3577 .intfver_nic = FW_INTFVER(T6, NIC),
3578 .intfver_vnic = FW_INTFVER(T6, VNIC),
3579 .intfver_ofld = FW_INTFVER(T6, OFLD),
3580 .intfver_ri = FW_INTFVER(T6, RI),
3581 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3582 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3583 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3584 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3585 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303586 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303587
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303588};
3589
3590static struct fw_info *find_fw_info(int chip)
3591{
3592 int i;
3593
3594 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3595 if (fw_info_array[i].chip == chip)
3596 return &fw_info_array[i];
3597 }
3598 return NULL;
3599}
3600
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003601/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003602 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003603 */
3604static int adap_init0(struct adapter *adap)
3605{
3606 int ret;
3607 u32 v, port_vec;
3608 enum dev_state state;
3609 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003610 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303611 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003612
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303613 /* Grab Firmware Device Log parameters as early as possible so we have
3614 * access to it for debugging, etc.
3615 */
3616 ret = t4_init_devlog_params(adap);
3617 if (ret < 0)
3618 return ret;
3619
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303620 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303621 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3622 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003623 if (ret < 0) {
3624 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3625 ret);
3626 return ret;
3627 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003628 if (ret == adap->mbox)
3629 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003630
Vipul Pandya636f9d32012-09-26 02:39:39 +00003631 /*
3632 * If we're the Master PF Driver and the device is uninitialized,
3633 * then let's consider upgrading the firmware ... (We always want
3634 * to check the firmware version number in order to A. get it for
3635 * later reporting and B. to warn if the currently loaded firmware
3636 * is excessively mismatched relative to the driver.)
3637 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303638
Ganesh Goudar760446f2017-07-20 18:28:48 +05303639 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303640 ret = t4_check_fw_version(adap);
3641 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303642 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303643 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003644 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303645 struct fw_info *fw_info;
3646 struct fw_hdr *card_fw;
3647 const struct firmware *fw;
3648 const u8 *fw_data = NULL;
3649 unsigned int fw_size = 0;
3650
3651 /* This is the firmware whose headers the driver was compiled
3652 * against
3653 */
3654 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3655 if (fw_info == NULL) {
3656 dev_err(adap->pdev_dev,
3657 "unable to get firmware info for chip %d.\n",
3658 CHELSIO_CHIP_VERSION(adap->params.chip));
3659 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003660 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303661
3662 /* allocate memory to read the header of the firmware on the
3663 * card
3664 */
Michal Hocko752ade62017-05-08 15:57:27 -07003665 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303666
3667 /* Get FW from from /lib/firmware/ */
3668 ret = request_firmware(&fw, fw_info->fw_mod_name,
3669 adap->pdev_dev);
3670 if (ret < 0) {
3671 dev_err(adap->pdev_dev,
3672 "unable to load firmware image %s, error %d\n",
3673 fw_info->fw_mod_name, ret);
3674 } else {
3675 fw_data = fw->data;
3676 fw_size = fw->size;
3677 }
3678
3679 /* upgrade FW logic */
3680 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3681 state, &reset);
3682
3683 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003684 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003685 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303686
Vipul Pandya636f9d32012-09-26 02:39:39 +00003687 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303688 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003689 }
3690
3691 /*
3692 * Grab VPD parameters. This should be done after we establish a
3693 * connection to the firmware since some of the VPD parameters
3694 * (notably the Core Clock frequency) are retrieved via requests to
3695 * the firmware. On the other hand, we need these fairly early on
3696 * so we do this right after getting ahold of the firmware.
3697 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303698 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003699 if (ret < 0)
3700 goto bye;
3701
Vipul Pandya636f9d32012-09-26 02:39:39 +00003702 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003703 * Find out what ports are available to us. Note that we need to do
3704 * this before calling adap_init0_no_config() since it needs nports
3705 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003706 */
3707 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303708 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3709 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303710 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003711 if (ret < 0)
3712 goto bye;
3713
3714 adap->params.nports = hweight32(port_vec);
3715 adap->params.portvec = port_vec;
3716
Hariprasad Shenai06640312015-01-13 15:19:25 +05303717 /* If the firmware is initialized already, emit a simply note to that
3718 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003719 */
3720 if (state == DEV_STATE_INIT) {
3721 dev_info(adap->pdev_dev, "Coming up as %s: "\
3722 "Adapter already initialized\n",
3723 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003724 } else {
3725 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3726 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303727
3728 /* Find out whether we're dealing with a version of the
3729 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003730 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303731 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3732 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303733 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303734 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003735
Hariprasad Shenai06640312015-01-13 15:19:25 +05303736 /* If the firmware doesn't support Configuration Files,
3737 * return an error.
3738 */
3739 if (ret < 0) {
3740 dev_err(adap->pdev_dev, "firmware doesn't support "
3741 "Firmware Configuration Files\n");
3742 goto bye;
3743 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003744
Hariprasad Shenai06640312015-01-13 15:19:25 +05303745 /* The firmware provides us with a memory buffer where we can
3746 * load a Configuration File from the host if we want to
3747 * override the Configuration File in flash.
3748 */
3749 ret = adap_init0_config(adap, reset);
3750 if (ret == -ENOENT) {
3751 dev_err(adap->pdev_dev, "no Configuration File "
3752 "present on adapter.\n");
3753 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003754 }
3755 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303756 dev_err(adap->pdev_dev, "could not initialize "
3757 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003758 goto bye;
3759 }
3760 }
3761
Hariprasad Shenai06640312015-01-13 15:19:25 +05303762 /* Give the SGE code a chance to pull in anything that it needs ...
3763 * Note that this must be called after we retrieve our VPD parameters
3764 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003765 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303766 ret = t4_sge_init(adap);
3767 if (ret < 0)
3768 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003769
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003770 if (is_bypass_device(adap->pdev->device))
3771 adap->params.bypass = 1;
3772
Vipul Pandya636f9d32012-09-26 02:39:39 +00003773 /*
3774 * Grab some of our basic fundamental operating parameters.
3775 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003776#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303777 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3778 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003779
3780#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303781 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3782 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3783 FW_PARAMS_PARAM_Y_V(0) | \
3784 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003785
Vipul Pandya636f9d32012-09-26 02:39:39 +00003786 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003787 params[1] = FW_PARAM_PFVF(L2T_START);
3788 params[2] = FW_PARAM_PFVF(L2T_END);
3789 params[3] = FW_PARAM_PFVF(FILTER_START);
3790 params[4] = FW_PARAM_PFVF(FILTER_END);
3791 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303792 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003793 if (ret < 0)
3794 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003795 adap->sge.egr_start = val[0];
3796 adap->l2t_start = val[1];
3797 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003798 adap->tids.ftid_base = val[3];
3799 adap->tids.nftids = val[4] - val[3] + 1;
3800 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003801
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303802 /* qids (ingress/egress) returned from firmware can be anywhere
3803 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3804 * Hence driver needs to allocate memory for this range to
3805 * store the queue info. Get the highest IQFLINT/EQ index returned
3806 * in FW_EQ_*_CMD.alloc command.
3807 */
3808 params[0] = FW_PARAM_PFVF(EQ_END);
3809 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303810 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303811 if (ret < 0)
3812 goto bye;
3813 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3814 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3815
3816 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3817 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3818 if (!adap->sge.egr_map) {
3819 ret = -ENOMEM;
3820 goto bye;
3821 }
3822
3823 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3824 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3825 if (!adap->sge.ingr_map) {
3826 ret = -ENOMEM;
3827 goto bye;
3828 }
3829
3830 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303831 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303832 */
3833 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3834 sizeof(long), GFP_KERNEL);
3835 if (!adap->sge.starving_fl) {
3836 ret = -ENOMEM;
3837 goto bye;
3838 }
3839
3840 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3841 sizeof(long), GFP_KERNEL);
3842 if (!adap->sge.txq_maperr) {
3843 ret = -ENOMEM;
3844 goto bye;
3845 }
3846
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303847#ifdef CONFIG_DEBUG_FS
3848 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3849 sizeof(long), GFP_KERNEL);
3850 if (!adap->sge.blocked_fl) {
3851 ret = -ENOMEM;
3852 goto bye;
3853 }
3854#endif
3855
Anish Bhattb5a02f52015-01-14 15:17:34 -08003856 params[0] = FW_PARAM_PFVF(CLIP_START);
3857 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303858 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003859 if (ret < 0)
3860 goto bye;
3861 adap->clipt_start = val[0];
3862 adap->clipt_end = val[1];
3863
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303864 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3865 * Classes supported by the hardware/firmware so we hard code it here
3866 * for now.
3867 */
3868 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3869
Vipul Pandya636f9d32012-09-26 02:39:39 +00003870 /* query params related to active filter region */
3871 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3872 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303873 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003874 /* If Active filter size is set we enable establishing
3875 * offload connection through firmware work request
3876 */
3877 if ((val[0] != val[1]) && (ret >= 0)) {
3878 adap->flags |= FW_OFLD_CONN;
3879 adap->tids.aftid_base = val[0];
3880 adap->tids.aftid_end = val[1];
3881 }
3882
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003883 /* If we're running on newer firmware, let it know that we're
3884 * prepared to deal with encapsulated CPL messages. Older
3885 * firmware won't understand this and we'll just get
3886 * unencapsulated messages ...
3887 */
3888 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3889 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303890 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003891
Vipul Pandya636f9d32012-09-26 02:39:39 +00003892 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303893 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3894 * capability. Earlier versions of the firmware didn't have the
3895 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3896 * permission to use ULPTX MEMWRITE DSGL.
3897 */
3898 if (is_t4(adap->params.chip)) {
3899 adap->params.ulptx_memwrite_dsgl = false;
3900 } else {
3901 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303902 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303903 1, params, val);
3904 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3905 }
3906
Steve Wise086de572016-09-16 07:54:49 -07003907 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3908 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3909 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3910 1, params, val);
3911 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3912
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303913 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003914 * Get device capabilities so we can determine what resources we need
3915 * to manage.
3916 */
3917 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303918 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3919 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303920 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003921 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3922 &caps_cmd);
3923 if (ret < 0)
3924 goto bye;
3925
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003926 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003927 /* query offload-related parameters */
3928 params[0] = FW_PARAM_DEV(NTID);
3929 params[1] = FW_PARAM_PFVF(SERVER_START);
3930 params[2] = FW_PARAM_PFVF(SERVER_END);
3931 params[3] = FW_PARAM_PFVF(TDDP_START);
3932 params[4] = FW_PARAM_PFVF(TDDP_END);
3933 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303934 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003935 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003936 if (ret < 0)
3937 goto bye;
3938 adap->tids.ntids = val[0];
3939 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3940 adap->tids.stid_base = val[1];
3941 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003942 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003943 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003944 * region into two parts. Regular filters get 1/3rd and server
3945 * filters get 2/3rd part. This is only enabled if workarond
3946 * path is enabled.
3947 * 1. For regular filters.
3948 * 2. Server filter: This are special filters which are used
3949 * to redirect SYN packets to offload queue.
3950 */
3951 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3952 adap->tids.sftid_base = adap->tids.ftid_base +
3953 DIV_ROUND_UP(adap->tids.nftids, 3);
3954 adap->tids.nsftids = adap->tids.nftids -
3955 DIV_ROUND_UP(adap->tids.nftids, 3);
3956 adap->tids.nftids = adap->tids.sftid_base -
3957 adap->tids.ftid_base;
3958 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003959 adap->vres.ddp.start = val[3];
3960 adap->vres.ddp.size = val[4] - val[3] + 1;
3961 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00003962
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003963 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303964 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003965 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003966 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003967 params[0] = FW_PARAM_PFVF(STAG_START);
3968 params[1] = FW_PARAM_PFVF(STAG_END);
3969 params[2] = FW_PARAM_PFVF(RQ_START);
3970 params[3] = FW_PARAM_PFVF(RQ_END);
3971 params[4] = FW_PARAM_PFVF(PBL_START);
3972 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303973 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003974 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003975 if (ret < 0)
3976 goto bye;
3977 adap->vres.stag.start = val[0];
3978 adap->vres.stag.size = val[1] - val[0] + 1;
3979 adap->vres.rq.start = val[2];
3980 adap->vres.rq.size = val[3] - val[2] + 1;
3981 adap->vres.pbl.start = val[4];
3982 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003983
3984 params[0] = FW_PARAM_PFVF(SQRQ_START);
3985 params[1] = FW_PARAM_PFVF(SQRQ_END);
3986 params[2] = FW_PARAM_PFVF(CQ_START);
3987 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003988 params[4] = FW_PARAM_PFVF(OCQ_START);
3989 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303990 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303991 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003992 if (ret < 0)
3993 goto bye;
3994 adap->vres.qp.start = val[0];
3995 adap->vres.qp.size = val[1] - val[0] + 1;
3996 adap->vres.cq.start = val[2];
3997 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003998 adap->vres.ocq.start = val[4];
3999 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304000
4001 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4002 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304003 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304004 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304005 if (ret < 0) {
4006 adap->params.max_ordird_qp = 8;
4007 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4008 ret = 0;
4009 } else {
4010 adap->params.max_ordird_qp = val[0];
4011 adap->params.max_ird_adapter = val[1];
4012 }
4013 dev_info(adap->pdev_dev,
4014 "max_ordird_qp %d max_ird_adapter %d\n",
4015 adap->params.max_ordird_qp,
4016 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304017 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004018 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004019 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004020 params[0] = FW_PARAM_PFVF(ISCSI_START);
4021 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304022 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004023 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004024 if (ret < 0)
4025 goto bye;
4026 adap->vres.iscsi.start = val[0];
4027 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304028 /* LIO target and cxgb4i initiaitor */
4029 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004030 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304031 if (caps_cmd.cryptocaps) {
4032 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304033 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4034 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4035 params, val);
4036 if (ret < 0) {
4037 if (ret != -EINVAL)
4038 goto bye;
4039 } else {
4040 adap->vres.ncrypto_fc = val[0];
4041 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304042 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4043 adap->num_uld += 1;
4044 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004045#undef FW_PARAM_PFVF
4046#undef FW_PARAM_DEV
4047
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304048 /* The MTU/MSS Table is initialized by now, so load their values. If
4049 * we're initializing the adapter, then we'll make any modifications
4050 * we want to the MTU/MSS Table and also initialize the congestion
4051 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004052 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004053 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304054 if (state != DEV_STATE_INIT) {
4055 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004056
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304057 /* The default MTU Table contains values 1492 and 1500.
4058 * However, for TCP, it's better to have two values which are
4059 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4060 * This allows us to have a TCP Data Payload which is a
4061 * multiple of 8 regardless of what combination of TCP Options
4062 * are in use (always a multiple of 4 bytes) which is
4063 * important for performance reasons. For instance, if no
4064 * options are in use, then we have a 20-byte IP header and a
4065 * 20-byte TCP header. In this case, a 1500-byte MSS would
4066 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4067 * which is not a multiple of 8. So using an MSS of 1488 in
4068 * this case results in a TCP Data Payload of 1448 bytes which
4069 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4070 * Stamps have been negotiated, then an MTU of 1500 bytes
4071 * results in a TCP Data Payload of 1448 bytes which, as
4072 * above, is a multiple of 8 bytes ...
4073 */
4074 for (i = 0; i < NMTUS; i++)
4075 if (adap->params.mtus[i] == 1492) {
4076 adap->params.mtus[i] = 1488;
4077 break;
4078 }
4079
4080 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4081 adap->params.b_wnd);
4082 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304083 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004084 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304085 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004086 return 0;
4087
4088 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004089 * Something bad happened. If a command timed out or failed with EIO
4090 * FW does not operate within its spec or something catastrophic
4091 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004092 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004093bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304094 kfree(adap->sge.egr_map);
4095 kfree(adap->sge.ingr_map);
4096 kfree(adap->sge.starving_fl);
4097 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304098#ifdef CONFIG_DEBUG_FS
4099 kfree(adap->sge.blocked_fl);
4100#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004101 if (ret != -ETIMEDOUT && ret != -EIO)
4102 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004103 return ret;
4104}
4105
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004106/* EEH callbacks */
4107
4108static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4109 pci_channel_state_t state)
4110{
4111 int i;
4112 struct adapter *adap = pci_get_drvdata(pdev);
4113
4114 if (!adap)
4115 goto out;
4116
4117 rtnl_lock();
4118 adap->flags &= ~FW_OK;
4119 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004120 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004121 for_each_port(adap, i) {
4122 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004123 if (dev) {
4124 netif_device_detach(dev);
4125 netif_carrier_off(dev);
4126 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004127 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004128 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304129 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004130 if (adap->flags & FULL_INIT_DONE)
4131 cxgb_down(adap);
4132 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004133 if ((adap->flags & DEV_ENABLED)) {
4134 pci_disable_device(pdev);
4135 adap->flags &= ~DEV_ENABLED;
4136 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004137out: return state == pci_channel_io_perm_failure ?
4138 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4139}
4140
4141static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4142{
4143 int i, ret;
4144 struct fw_caps_config_cmd c;
4145 struct adapter *adap = pci_get_drvdata(pdev);
4146
4147 if (!adap) {
4148 pci_restore_state(pdev);
4149 pci_save_state(pdev);
4150 return PCI_ERS_RESULT_RECOVERED;
4151 }
4152
Gavin Shan144be3d2014-01-23 12:27:34 +08004153 if (!(adap->flags & DEV_ENABLED)) {
4154 if (pci_enable_device(pdev)) {
4155 dev_err(&pdev->dev, "Cannot reenable PCI "
4156 "device after reset\n");
4157 return PCI_ERS_RESULT_DISCONNECT;
4158 }
4159 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004160 }
4161
4162 pci_set_master(pdev);
4163 pci_restore_state(pdev);
4164 pci_save_state(pdev);
4165 pci_cleanup_aer_uncorrect_error_status(pdev);
4166
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304167 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004168 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304169 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004170 return PCI_ERS_RESULT_DISCONNECT;
4171 adap->flags |= FW_OK;
4172 if (adap_init1(adap, &c))
4173 return PCI_ERS_RESULT_DISCONNECT;
4174
4175 for_each_port(adap, i) {
4176 struct port_info *p = adap2pinfo(adap, i);
4177
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304178 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004179 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004180 if (ret < 0)
4181 return PCI_ERS_RESULT_DISCONNECT;
4182 p->viid = ret;
4183 p->xact_addr_filt = -1;
4184 }
4185
4186 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4187 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004188 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004189 if (cxgb_up(adap))
4190 return PCI_ERS_RESULT_DISCONNECT;
4191 return PCI_ERS_RESULT_RECOVERED;
4192}
4193
4194static void eeh_resume(struct pci_dev *pdev)
4195{
4196 int i;
4197 struct adapter *adap = pci_get_drvdata(pdev);
4198
4199 if (!adap)
4200 return;
4201
4202 rtnl_lock();
4203 for_each_port(adap, i) {
4204 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004205 if (dev) {
4206 if (netif_running(dev)) {
4207 link_start(dev);
4208 cxgb_set_rxmode(dev);
4209 }
4210 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004211 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004212 }
4213 rtnl_unlock();
4214}
4215
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004216static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004217 .error_detected = eeh_err_detected,
4218 .slot_reset = eeh_slot_reset,
4219 .resume = eeh_resume,
4220};
4221
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304222/* Return true if the Link Configuration supports "High Speeds" (those greater
4223 * than 1Gb/s).
4224 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304225static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004226{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304227 unsigned int speeds, high_speeds;
4228
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304229 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4230 high_speeds = speeds &
4231 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304232
4233 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004234}
4235
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004236/*
4237 * Perform default configuration of DMA queues depending on the number and type
4238 * of ports we found and the number of available CPUs. Most settings can be
4239 * modified by the admin prior to actual use.
4240 */
Bill Pemberton91744942012-12-03 09:23:02 -05004241static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004242{
4243 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304244 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004245#ifndef CONFIG_CHELSIO_T4_DCB
4246 int q10g = 0;
4247#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004248
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304249 /* Reduce memory usage in kdump environment, disable all offload.
4250 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304251 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304252 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304253 adap->params.crypto = 0;
4254 }
4255
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304256 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004257#ifdef CONFIG_CHELSIO_T4_DCB
4258 /* For Data Center Bridging support we need to be able to support up
4259 * to 8 Traffic Priorities; each of which will be assigned to its
4260 * own TX Queue in order to prevent Head-Of-Line Blocking.
4261 */
4262 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4263 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4264 MAX_ETH_QSETS, adap->params.nports * 8);
4265 BUG_ON(1);
4266 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004267
Anish Bhatt688848b2014-06-19 21:37:13 -07004268 for_each_port(adap, i) {
4269 struct port_info *pi = adap2pinfo(adap, i);
4270
4271 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304272 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004273 qidx += pi->nqsets;
4274 }
4275#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004276 /*
4277 * We default to 1 queue per non-10G port and up to # of cores queues
4278 * per 10G port.
4279 */
4280 if (n10g)
4281 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004282 if (q10g > netif_get_num_default_rss_queues())
4283 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004284
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304285 if (is_kdump_kernel())
4286 q10g = 1;
4287
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004288 for_each_port(adap, i) {
4289 struct port_info *pi = adap2pinfo(adap, i);
4290
4291 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304292 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004293 qidx += pi->nqsets;
4294 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004295#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004296
4297 s->ethqsets = qidx;
4298 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4299
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304300 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004301 /*
4302 * For offload we use 1 queue/channel if all ports are up to 1G,
4303 * otherwise we divide all available queues amongst the channels
4304 * capped by the number of available cores.
4305 */
4306 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304307 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304308 s->ofldqsets = roundup(i, adap->params.nports);
4309 } else {
4310 s->ofldqsets = adap->params.nports;
4311 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004312 }
4313
4314 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4315 struct sge_eth_rxq *r = &s->ethrxq[i];
4316
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304317 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004318 r->fl.size = 72;
4319 }
4320
4321 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4322 s->ethtxq[i].q.size = 1024;
4323
4324 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4325 s->ctrlq[i].q.size = 512;
4326
Atul Guptaa45695042017-07-04 16:46:20 +05304327 if (!is_t4(adap->params.chip))
4328 s->ptptxq.q.size = 8;
4329
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304330 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304331 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004332}
4333
4334/*
4335 * Reduce the number of Ethernet queues across all ports to at most n.
4336 * n provides at least one queue per port.
4337 */
Bill Pemberton91744942012-12-03 09:23:02 -05004338static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004339{
4340 int i;
4341 struct port_info *pi;
4342
4343 while (n < adap->sge.ethqsets)
4344 for_each_port(adap, i) {
4345 pi = adap2pinfo(adap, i);
4346 if (pi->nqsets > 1) {
4347 pi->nqsets--;
4348 adap->sge.ethqsets--;
4349 if (adap->sge.ethqsets <= n)
4350 break;
4351 }
4352 }
4353
4354 n = 0;
4355 for_each_port(adap, i) {
4356 pi = adap2pinfo(adap, i);
4357 pi->first_qset = n;
4358 n += pi->nqsets;
4359 }
4360}
4361
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304362static int get_msix_info(struct adapter *adap)
4363{
4364 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304365 unsigned int max_ingq = 0;
4366
4367 if (is_offload(adap))
4368 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4369 if (is_pci_uld(adap))
4370 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4371
4372 if (!max_ingq)
4373 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304374
4375 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4376 if (!msix_info)
4377 return -ENOMEM;
4378
4379 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4380 sizeof(long), GFP_KERNEL);
4381 if (!adap->msix_bmap_ulds.msix_bmap) {
4382 kfree(msix_info);
4383 return -ENOMEM;
4384 }
4385 spin_lock_init(&adap->msix_bmap_ulds.lock);
4386 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304387out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304388 return 0;
4389}
4390
4391static void free_msix_info(struct adapter *adap)
4392{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304393 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304394 return;
4395
4396 kfree(adap->msix_info_ulds);
4397 kfree(adap->msix_bmap_ulds.msix_bmap);
4398}
4399
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004400/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4401#define EXTRA_VECS 2
4402
Bill Pemberton91744942012-12-03 09:23:02 -05004403static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004404{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304405 int ofld_need = 0, uld_need = 0;
4406 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004407 struct sge *s = &adap->sge;
4408 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304409 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304410 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004411
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304412 if (is_pci_uld(adap))
4413 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4414 if (is_offload(adap))
4415 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304416 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304417 GFP_KERNEL);
4418 if (!entries)
4419 return -ENOMEM;
4420
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304421 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304422 if (get_msix_info(adap)) {
4423 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304424 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304425 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304426
4427 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004428 entries[i].entry = i;
4429
4430 want = s->max_ethqsets + EXTRA_VECS;
4431 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304432 want += adap->num_ofld_uld * s->ofldqsets;
4433 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004434 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304435 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304436 want += adap->num_uld * s->ofldqsets;
4437 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304438 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004439#ifdef CONFIG_CHELSIO_T4_DCB
4440 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4441 * each port.
4442 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304443 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004444#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304445 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004446#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304447 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4448 if (allocated < 0) {
4449 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4450 " not using MSI-X\n");
4451 kfree(entries);
4452 return allocated;
4453 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004454
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304455 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004456 * Every group gets its minimum requirement and NIC gets top
4457 * priority for leftovers.
4458 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304459 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004460 if (i < s->max_ethqsets) {
4461 s->max_ethqsets = i;
4462 if (i < s->ethqsets)
4463 reduce_ethqs(adap, i);
4464 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304465 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304466 if (allocated < want)
4467 s->nqs_per_uld = nchan;
4468 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304469 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304470 }
4471
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304472 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004473 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304474 if (is_uld(adap)) {
4475 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304476 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304477 adap->msix_info_ulds[j].idx = i;
4478 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304479 adap->msix_bmap_ulds.mapsize = j;
4480 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304481 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304482 "nic %d per uld %d\n",
4483 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004484
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304485 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004486 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004487}
4488
4489#undef EXTRA_VECS
4490
Bill Pemberton91744942012-12-03 09:23:02 -05004491static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004492{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304493 unsigned int i;
4494 int err;
4495
4496 err = t4_init_rss_mode(adap, adap->mbox);
4497 if (err)
4498 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004499
4500 for_each_port(adap, i) {
4501 struct port_info *pi = adap2pinfo(adap, i);
4502
4503 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4504 if (!pi->rss)
4505 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004506 }
4507 return 0;
4508}
4509
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304510static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4511 enum pci_bus_speed *speed,
4512 enum pcie_link_width *width)
4513{
4514 u32 lnkcap1, lnkcap2;
4515 int err1, err2;
4516
4517#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4518
4519 *speed = PCI_SPEED_UNKNOWN;
4520 *width = PCIE_LNK_WIDTH_UNKNOWN;
4521
4522 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4523 &lnkcap1);
4524 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4525 &lnkcap2);
4526 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4527 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4528 *speed = PCIE_SPEED_8_0GT;
4529 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4530 *speed = PCIE_SPEED_5_0GT;
4531 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4532 *speed = PCIE_SPEED_2_5GT;
4533 }
4534 if (!err1) {
4535 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4536 if (!lnkcap2) { /* pre-r3.0 */
4537 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4538 *speed = PCIE_SPEED_5_0GT;
4539 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4540 *speed = PCIE_SPEED_2_5GT;
4541 }
4542 }
4543
4544 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4545 return err1 ? err1 : err2 ? err2 : -EINVAL;
4546 return 0;
4547}
4548
4549static void cxgb4_check_pcie_caps(struct adapter *adap)
4550{
4551 enum pcie_link_width width, width_cap;
4552 enum pci_bus_speed speed, speed_cap;
4553
4554#define PCIE_SPEED_STR(speed) \
4555 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4556 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4557 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4558 "Unknown")
4559
4560 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4561 dev_warn(adap->pdev_dev,
4562 "Unable to determine PCIe device BW capabilities\n");
4563 return;
4564 }
4565
4566 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4567 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4568 dev_warn(adap->pdev_dev,
4569 "Unable to determine PCI Express bandwidth.\n");
4570 return;
4571 }
4572
4573 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4574 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4575 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4576 width, width_cap);
4577 if (speed < speed_cap || width < width_cap)
4578 dev_info(adap->pdev_dev,
4579 "A slot with more lanes and/or higher speed is "
4580 "suggested for optimal performance.\n");
4581}
4582
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304583/* Dump basic information about the adapter */
4584static void print_adapter_info(struct adapter *adapter)
4585{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304586 /* Hardware/Firmware/etc. Version/Revision IDs */
4587 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304588
4589 /* Software/Hardware configuration */
4590 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4591 is_offload(adapter) ? "R" : "",
4592 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4593 (adapter->flags & USING_MSI) ? "MSI" : ""),
4594 is_offload(adapter) ? "Offload" : "non-Offload");
4595}
4596
Bill Pemberton91744942012-12-03 09:23:02 -05004597static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004598{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004599 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004600 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004601 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004602 const struct port_info *pi = netdev_priv(dev);
4603 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004604
4605 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4606 spd = " 2.5 GT/s";
4607 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4608 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004609 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4610 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004611
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304612 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304613 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304614 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304615 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304616 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004617 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304618 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304619 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304620 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304621 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304622 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4623 bufp += sprintf(bufp, "50G/");
4624 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304625 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304626 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4627 bufp += sprintf(bufp, "200G/");
4628 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4629 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004630 if (bufp != buf)
4631 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304632 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004633
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304634 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4635 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004636}
4637
Dimitris Michailidis06546392010-07-11 12:01:16 +00004638/*
4639 * Free the following resources:
4640 * - memory used for tables
4641 * - MSI/MSI-X
4642 * - net devices
4643 * - resources FW is holding for us
4644 */
4645static void free_some_resources(struct adapter *adapter)
4646{
4647 unsigned int i;
4648
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05304649 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07004650 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304651 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004652 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304653 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304654 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304655 kfree(adapter->sge.egr_map);
4656 kfree(adapter->sge.ingr_map);
4657 kfree(adapter->sge.starving_fl);
4658 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304659#ifdef CONFIG_DEBUG_FS
4660 kfree(adapter->sge.blocked_fl);
4661#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004662 disable_msi(adapter);
4663
4664 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004665 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304666 struct port_info *pi = adap2pinfo(adapter, i);
4667
4668 if (pi->viid != 0)
4669 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4670 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004671 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004672 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004673 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004674 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304675 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004676}
4677
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004678#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004679#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004680 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004681#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004682
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304683static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4684{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304685 u16 device_id;
4686
4687 /* Retrieve adapter's device ID */
4688 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004689
4690 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304691 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004692 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304693 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004694 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304695 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004696 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304697 default:
4698 dev_err(&pdev->dev, "Device %d is not supported\n",
4699 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304700 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004701 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304702}
4703
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304704#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304705static void dummy_setup(struct net_device *dev)
4706{
4707 dev->type = ARPHRD_NONE;
4708 dev->mtu = 0;
4709 dev->hard_header_len = 0;
4710 dev->addr_len = 0;
4711 dev->tx_queue_len = 0;
4712 dev->flags |= IFF_NOARP;
4713 dev->priv_flags |= IFF_NO_QUEUE;
4714
4715 /* Initialize the device structure. */
4716 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4717 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004718 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304719}
4720
4721static int config_mgmt_dev(struct pci_dev *pdev)
4722{
4723 struct adapter *adap = pci_get_drvdata(pdev);
4724 struct net_device *netdev;
4725 struct port_info *pi;
4726 char name[IFNAMSIZ];
4727 int err;
4728
4729 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304730 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4731 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304732 if (!netdev)
4733 return -ENOMEM;
4734
4735 pi = netdev_priv(netdev);
4736 pi->adapter = adap;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304737 pi->tx_chan = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304738 SET_NETDEV_DEV(netdev, &pdev->dev);
4739
4740 adap->port[0] = netdev;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304741 pi->port_id = 0;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304742
4743 err = register_netdev(adap->port[0]);
4744 if (err) {
4745 pr_info("Unable to register VF mgmt netdev %s\n", name);
4746 free_netdev(adap->port[0]);
4747 adap->port[0] = NULL;
4748 return err;
4749 }
4750 return 0;
4751}
4752
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304753static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4754{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304755 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304756 int err = 0;
4757 int current_vfs = pci_num_vf(pdev);
4758 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304759
Hariprasad Shenai78294512016-08-11 21:06:23 +05304760 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304761 /* Check if cxgb4 is the MASTER and fw is initialized */
4762 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4763 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4764 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4765 dev_warn(&pdev->dev,
4766 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4767 return -EOPNOTSUPP;
4768 }
4769
4770 /* If any of the VF's is already assigned to Guest OS, then
4771 * SRIOV for the same cannot be modified
4772 */
4773 if (current_vfs && pci_vfs_assigned(pdev)) {
4774 dev_err(&pdev->dev,
4775 "Cannot modify SR-IOV while VFs are assigned\n");
4776 num_vfs = current_vfs;
4777 return num_vfs;
4778 }
4779
4780 /* Disable SRIOV when zero is passed.
4781 * One needs to disable SRIOV before modifying it, else
4782 * stack throws the below warning:
4783 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4784 */
4785 if (!num_vfs) {
4786 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304787 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304788 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304789 adap->port[0] = NULL;
4790 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304791 /* free VF resources */
4792 kfree(adap->vfinfo);
4793 adap->vfinfo = NULL;
4794 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304795 return num_vfs;
4796 }
4797
4798 if (num_vfs != current_vfs) {
4799 err = pci_enable_sriov(pdev, num_vfs);
4800 if (err)
4801 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304802
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304803 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304804 err = config_mgmt_dev(pdev);
4805 if (err)
4806 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304807 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304808
4809 adap->vfinfo = kcalloc(adap->num_vfs,
4810 sizeof(struct vf_info), GFP_KERNEL);
4811 if (adap->vfinfo)
4812 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304813 return num_vfs;
4814}
4815#endif
4816
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004817static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004818{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004819 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004820 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004821 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004822 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304823 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304824 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304825 u32 whoami, pl_rev;
4826 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304827 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004828#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304829 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004830#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004831
4832 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4833
4834 err = pci_request_regions(pdev, KBUILD_MODNAME);
4835 if (err) {
4836 /* Just info, some other driver may have claimed the device. */
4837 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4838 return err;
4839 }
4840
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004841 err = pci_enable_device(pdev);
4842 if (err) {
4843 dev_err(&pdev->dev, "cannot enable PCI device\n");
4844 goto out_release_regions;
4845 }
4846
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304847 regs = pci_ioremap_bar(pdev, 0);
4848 if (!regs) {
4849 dev_err(&pdev->dev, "cannot map device registers\n");
4850 err = -ENOMEM;
4851 goto out_disable_device;
4852 }
4853
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304854 err = t4_wait_dev_ready(regs);
4855 if (err < 0)
4856 goto out_unmap_bar0;
4857
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304858 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304859 whoami = readl(regs + PL_WHOAMI_A);
4860 pl_rev = REV_G(readl(regs + PL_REV_A));
4861 chip = get_chip_type(pdev, pl_rev);
4862 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4863 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304864 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304865#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304866 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304867#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304868 pci_disable_device(pdev);
4869 pci_save_state(pdev); /* to restore SR-IOV later */
4870 goto sriov;
4871 }
4872
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004873 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004874 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004875 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4876 if (err) {
4877 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4878 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304879 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004880 }
4881 } else {
4882 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4883 if (err) {
4884 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304885 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004886 }
4887 }
4888
4889 pci_enable_pcie_error_reporting(pdev);
4890 pci_set_master(pdev);
4891 pci_save_state(pdev);
4892
4893 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4894 if (!adapter) {
4895 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304896 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004897 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304898 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004899
Anish Bhatt29aaee62014-08-20 13:44:06 -07004900 adapter->workq = create_singlethread_workqueue("cxgb4");
4901 if (!adapter->workq) {
4902 err = -ENOMEM;
4903 goto out_free_adapter;
4904 }
4905
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304906 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4907 (sizeof(struct mbox_cmd) *
4908 T4_OS_LOG_MBOX_CMDS),
4909 GFP_KERNEL);
4910 if (!adapter->mbox_log) {
4911 err = -ENOMEM;
4912 goto out_free_adapter;
4913 }
4914 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4915
Gavin Shan144be3d2014-01-23 12:27:34 +08004916 /* PCI device has been enabled */
4917 adapter->flags |= DEV_ENABLED;
4918
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304919 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004920 adapter->pdev = pdev;
4921 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304922 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304923 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304924 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05304925 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004926 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4927
Casey Leedomb0ba9d52017-08-15 11:23:26 +08004928 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
4929 * Ingress Packet Data to Free List Buffers in order to allow for
4930 * chipset performance optimizations between the Root Complex and
4931 * Memory Controllers. (Messages to the associated Ingress Queue
4932 * notifying new Packet Placement in the Free Lists Buffers will be
4933 * send without the Relaxed Ordering Attribute thus guaranteeing that
4934 * all preceding PCIe Transaction Layer Packets will be processed
4935 * first.) But some Root Complexes have various issues with Upstream
4936 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
4937 * The PCIe devices which under the Root Complexes will be cleared the
4938 * Relaxed Ordering bit in the configuration space, So we check our
4939 * PCIe configuration space to see if it's flagged with advice against
4940 * using Relaxed Ordering.
4941 */
4942 if (!pcie_relaxed_ordering_enabled(pdev))
4943 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
4944
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004945 spin_lock_init(&adapter->stats_lock);
4946 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004947 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05304948 spin_lock_init(&adapter->mbox_lock);
4949
4950 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004951
4952 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304953 INIT_WORK(&adapter->db_full_task, process_db_full);
4954 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004955
4956 err = t4_prep_adapter(adapter);
4957 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304958 goto out_free_adapter;
4959
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004960
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304961 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304962 s_qpp = (QUEUESPERPAGEPF0_S +
4963 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304964 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304965 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4966 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004967 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4968
4969 /* Each segment size is 128B. Write coalescing is enabled only
4970 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4971 * queue is less no of segments that can be accommodated in
4972 * a page size.
4973 */
4974 if (qpp > num_seg) {
4975 dev_err(&pdev->dev,
4976 "Incorrect number of egress queues per page\n");
4977 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304978 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004979 }
4980 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4981 pci_resource_len(pdev, 2));
4982 if (!adapter->bar2) {
4983 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4984 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304985 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004986 }
4987 }
4988
Vipul Pandya636f9d32012-09-26 02:39:39 +00004989 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004990 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304991#ifdef CONFIG_DEBUG_FS
4992 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4993#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004994 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004995 if (err)
4996 goto out_unmap_bar;
4997
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304998 /* configure SGE_STAT_CFG_A to read WC stats */
4999 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305000 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5001 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5002 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305003
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005004 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005005 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5006 MAX_ETH_QSETS);
5007 if (!netdev) {
5008 err = -ENOMEM;
5009 goto out_free_dev;
5010 }
5011
5012 SET_NETDEV_DEV(netdev, &pdev->dev);
5013
5014 adapter->port[i] = netdev;
5015 pi = netdev_priv(netdev);
5016 pi->adapter = adapter;
5017 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005018 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005019 netdev->irq = pdev->irq;
5020
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005021 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5022 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5023 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305024 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5025 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005026 if (highdma)
5027 netdev->hw_features |= NETIF_F_HIGHDMA;
5028 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005029 netdev->vlan_features = netdev->features & VLAN_FEAT;
5030
Jiri Pirko01789342011-08-16 06:29:00 +00005031 netdev->priv_flags |= IFF_UNICAST_FLT;
5032
Jarod Wilsond894be52016-10-20 13:55:16 -04005033 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305034 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005035 netdev->max_mtu = MAX_MTU;
5036
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005037 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005038#ifdef CONFIG_CHELSIO_T4_DCB
5039 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5040 cxgb4_dcb_state_init(netdev);
5041#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305042 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005043 }
5044
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305045 cxgb4_init_ethtool_dump(adapter);
5046
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005047 pci_set_drvdata(pdev, adapter);
5048
5049 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005050 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005051 if (err)
5052 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305053 } else if (adapter->params.nports == 1) {
5054 /* If we don't have a connection to the firmware -- possibly
5055 * because of an error -- grab the raw VPD parameters so we
5056 * can set the proper MAC Address on the debug network
5057 * interface that we've created.
5058 */
5059 u8 hw_addr[ETH_ALEN];
5060 u8 *na = adapter->params.vpd.na;
5061
5062 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5063 if (!err) {
5064 for (i = 0; i < ETH_ALEN; i++)
5065 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5066 hex2val(na[2 * i + 1]));
5067 t4_set_hw_addr(adapter, 0, hw_addr);
5068 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005069 }
5070
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305071 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005072 * soon as the first register_netdev completes.
5073 */
5074 cfg_queues(adapter);
5075
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305076 adapter->smt = t4_init_smt();
5077 if (!adapter->smt) {
5078 /* We tolerate a lack of SMT, giving up some functionality */
5079 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5080 }
5081
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305082 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005083 if (!adapter->l2t) {
5084 /* We tolerate a lack of L2T, giving up some functionality */
5085 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5086 adapter->params.offload = 0;
5087 }
5088
Anish Bhattb5a02f52015-01-14 15:17:34 -08005089#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305090 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5091 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5092 /* CLIP functionality is not present in hardware,
5093 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005094 */
5095 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305096 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005097 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305098 } else {
5099 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5100 adapter->clipt_end);
5101 if (!adapter->clipt) {
5102 /* We tolerate a lack of clip_table, giving up
5103 * some functionality
5104 */
5105 dev_warn(&pdev->dev,
5106 "could not allocate Clip table, continuing\n");
5107 adapter->params.offload = 0;
5108 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005109 }
5110#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305111
5112 for_each_port(adapter, i) {
5113 pi = adap2pinfo(adapter, i);
5114 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5115 if (!pi->sched_tbl)
5116 dev_warn(&pdev->dev,
5117 "could not activate scheduling on port %d\n",
5118 i);
5119 }
5120
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305121 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005122 dev_warn(&pdev->dev, "could not allocate TID table, "
5123 "continuing\n");
5124 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305125 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305126 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305127 if (!adapter->tc_u32)
5128 dev_warn(&pdev->dev,
5129 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305130
5131 cxgb4_init_tc_flower(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005132 }
5133
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305134 if (is_offload(adapter)) {
5135 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5136 u32 hash_base, hash_reg;
5137
5138 if (chip <= CHELSIO_T5) {
5139 hash_reg = LE_DB_TID_HASHBASE_A;
5140 hash_base = t4_read_reg(adapter, hash_reg);
5141 adapter->tids.hash_base = hash_base / 4;
5142 } else {
5143 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5144 hash_base = t4_read_reg(adapter, hash_reg);
5145 adapter->tids.hash_base = hash_base;
5146 }
5147 }
5148 }
5149
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005150 /* See what interrupts we'll be using */
5151 if (msi > 1 && enable_msix(adapter) == 0)
5152 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305153 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005154 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305155 if (msi > 1)
5156 free_msix_info(adapter);
5157 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005158
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305159 /* check for PCI Express bandwidth capabiltites */
5160 cxgb4_check_pcie_caps(adapter);
5161
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005162 err = init_rss(adapter);
5163 if (err)
5164 goto out_free_dev;
5165
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005166 /*
5167 * The card is now ready to go. If any errors occur during device
5168 * registration we do not fail the whole card but rather proceed only
5169 * with the ports we manage to register successfully. However we must
5170 * register at least one net device.
5171 */
5172 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005173 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305174 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005175 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5176 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5177
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305178 netif_carrier_off(adapter->port[i]);
5179
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005180 err = register_netdev(adapter->port[i]);
5181 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005182 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005183 adapter->chan_map[pi->tx_chan] = i;
5184 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005185 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005186 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005187 dev_err(&pdev->dev, "could not register any net devices\n");
5188 goto out_free_dev;
5189 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005190 if (err) {
5191 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5192 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005193 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005194
5195 if (cxgb4_debugfs_root) {
5196 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5197 cxgb4_debugfs_root);
5198 setup_debugfs(adapter);
5199 }
5200
David S. Miller88c51002011-10-07 13:38:43 -04005201 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5202 pdev->needs_freset = 1;
5203
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305204 if (is_uld(adapter)) {
5205 mutex_lock(&uld_mutex);
5206 list_add_tail(&adapter->list_node, &adapter_list);
5207 mutex_unlock(&uld_mutex);
5208 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005209
Atul Gupta9c33e422017-07-04 16:46:21 +05305210 if (!is_t4(adapter->params.chip))
5211 cxgb4_ptp_init(adapter);
5212
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305213 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305214 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305215 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305216
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305217sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005218#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305219 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5220 if (!adapter) {
5221 err = -ENOMEM;
5222 goto free_pci_region;
5223 }
5224
Hariprasad Shenai78294512016-08-11 21:06:23 +05305225 adapter->pdev = pdev;
5226 adapter->pdev_dev = &pdev->dev;
5227 adapter->name = pci_name(pdev);
5228 adapter->mbox = func;
5229 adapter->pf = func;
5230 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305231 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305232 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5233 (sizeof(struct mbox_cmd) *
5234 T4_OS_LOG_MBOX_CMDS),
5235 GFP_KERNEL);
5236 if (!adapter->mbox_log) {
5237 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305238 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305239 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305240 spin_lock_init(&adapter->mbox_lock);
5241 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305242
5243 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5244 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5245 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5246 &v, &port_vec);
5247 if (err < 0) {
5248 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
Ganesh Goudard0417842017-06-09 19:26:24 +05305249 goto free_mbox_log;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305250 }
5251
5252 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305253 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005254 return 0;
5255
Ganesh Goudard0417842017-06-09 19:26:24 +05305256free_mbox_log:
5257 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305258 free_adapter:
5259 kfree(adapter);
5260 free_pci_region:
5261 iounmap(regs);
5262 pci_disable_sriov(pdev);
5263 pci_release_regions(pdev);
5264 return err;
5265#else
5266 return 0;
5267#endif
5268
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005269 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005270 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305271 if (adapter->flags & USING_MSIX)
5272 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305273 if (adapter->num_uld || adapter->num_ofld_uld)
5274 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005275 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305276 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005277 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005278 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005279 if (adapter->workq)
5280 destroy_workqueue(adapter->workq);
5281
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305282 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005283 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305284 out_unmap_bar0:
5285 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005286 out_disable_device:
5287 pci_disable_pcie_error_reporting(pdev);
5288 pci_disable_device(pdev);
5289 out_release_regions:
5290 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005291 return err;
5292}
5293
Bill Pemberton91744942012-12-03 09:23:02 -05005294static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005295{
5296 struct adapter *adapter = pci_get_drvdata(pdev);
5297
Hariprasad Shenai78294512016-08-11 21:06:23 +05305298 if (!adapter) {
5299 pci_release_regions(pdev);
5300 return;
5301 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005302
Ganesh Goudare1f61982017-09-21 12:50:47 +05305303 adapter->flags |= SHUTTING_DOWN;
5304
Hariprasad Shenai78294512016-08-11 21:06:23 +05305305 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005306 int i;
5307
Anish Bhatt29aaee62014-08-20 13:44:06 -07005308 /* Tear down per-adapter Work Queue first since it can contain
5309 * references to our adapter data structure.
5310 */
5311 destroy_workqueue(adapter->workq);
5312
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005313 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005314 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005315 t4_uld_clean_up(adapter);
5316 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005317
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305318 disable_interrupts(adapter);
5319
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005320 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005321 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005322 unregister_netdev(adapter->port[i]);
5323
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005324 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005325
Atul Gupta9c33e422017-07-04 16:46:21 +05305326 if (!is_t4(adapter->params.chip))
5327 cxgb4_ptp_stop(adapter);
5328
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005329 /* If we allocated filters, free up state associated with any
5330 * valid filters ...
5331 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305332 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005333
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005334 if (adapter->flags & FULL_INIT_DONE)
5335 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005336
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305337 if (adapter->flags & USING_MSIX)
5338 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305339 if (adapter->num_uld || adapter->num_ofld_uld)
5340 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005341 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005342#if IS_ENABLED(CONFIG_IPV6)
5343 t4_cleanup_clip_tbl(adapter);
5344#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005345 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305346 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005347 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005348 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005349 if ((adapter->flags & DEV_ENABLED)) {
5350 pci_disable_device(pdev);
5351 adapter->flags &= ~DEV_ENABLED;
5352 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005353 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305354 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005355 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005356 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305357 }
5358#ifdef CONFIG_PCI_IOV
5359 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305360 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305361 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305362 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305363 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305364 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305365 kfree(adapter);
5366 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005367 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305368 }
5369#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005370}
5371
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305372/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5373 * delivery. This is essentially a stripped down version of the PCI remove()
5374 * function where we do the minimal amount of work necessary to shutdown any
5375 * further activity.
5376 */
5377static void shutdown_one(struct pci_dev *pdev)
5378{
5379 struct adapter *adapter = pci_get_drvdata(pdev);
5380
5381 /* As with remove_one() above (see extended comment), we only want do
5382 * do cleanup on PCI Devices which went all the way through init_one()
5383 * ...
5384 */
5385 if (!adapter) {
5386 pci_release_regions(pdev);
5387 return;
5388 }
5389
Ganesh Goudare1f61982017-09-21 12:50:47 +05305390 adapter->flags |= SHUTTING_DOWN;
5391
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305392 if (adapter->pf == 4) {
5393 int i;
5394
5395 for_each_port(adapter, i)
5396 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5397 cxgb_close(adapter->port[i]);
5398
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005399 if (is_uld(adapter)) {
5400 detach_ulds(adapter);
5401 t4_uld_clean_up(adapter);
5402 }
5403
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305404 disable_interrupts(adapter);
5405 disable_msi(adapter);
5406
5407 t4_sge_stop(adapter);
5408 if (adapter->flags & FW_OK)
5409 t4_fw_bye(adapter, adapter->mbox);
5410 }
5411#ifdef CONFIG_PCI_IOV
5412 else {
5413 if (adapter->port[0])
5414 unregister_netdev(adapter->port[0]);
5415 iounmap(adapter->regs);
5416 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305417 kfree(adapter->mbox_log);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305418 kfree(adapter);
5419 pci_disable_sriov(pdev);
5420 pci_release_regions(pdev);
5421 }
5422#endif
5423}
5424
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005425static struct pci_driver cxgb4_driver = {
5426 .name = KBUILD_MODNAME,
5427 .id_table = cxgb4_pci_tbl,
5428 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005429 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305430 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305431#ifdef CONFIG_PCI_IOV
5432 .sriov_configure = cxgb4_iov_configure,
5433#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005434 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005435};
5436
5437static int __init cxgb4_init_module(void)
5438{
5439 int ret;
5440
5441 /* Debugfs support is optional, just warn if this fails */
5442 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5443 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005444 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005445
5446 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005447 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005448 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305449
Anish Bhatt1bb60372014-10-14 20:07:22 -07005450#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005451 if (!inet6addr_registered) {
5452 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5453 inet6addr_registered = true;
5454 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005455#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305456
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005457 return ret;
5458}
5459
5460static void __exit cxgb4_cleanup_module(void)
5461{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005462#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305463 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005464 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5465 inet6addr_registered = false;
5466 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005467#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005468 pci_unregister_driver(&cxgb4_driver);
5469 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005470}
5471
5472module_init(cxgb4_init_module);
5473module_exit(cxgb4_cleanup_module);