blob: abc425bfc744b7b8e0b9eaea7174b8957dce53e0 [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 *
Anish Bhattce100b8b2014-06-19 21:37:15 -07004 * Copyright (c) 2003-2014 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>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000066#include <asm/uaccess.h>
67
68#include "cxgb4.h"
69#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053070#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000071#include "t4_msg.h"
72#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053073#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070074#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053075#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080076#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000077#include "l2t.h"
78
Hariprasad Shenai812034f2015-04-06 20:23:23 +053079char cxgb4_driver_name[] = KBUILD_MODNAME;
80
Vipul Pandya01bcca62013-07-04 16:10:46 +053081#ifdef DRV_VERSION
82#undef DRV_VERSION
83#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000084#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053085const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053086#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000087
Vipul Pandyaf2b7e782012-12-10 09:30:52 +000088/* Host shadow copy of ingress filter entry. This is in host native format
89 * and doesn't match the ordering or bit order, etc. of the hardware of the
90 * firmware command. The use of bit-field structure elements is purely to
91 * remind ourselves of the field size limitations and save memory in the case
92 * where the filter table is large.
93 */
94struct filter_entry {
95 /* Administrative fields for filter.
96 */
97 u32 valid:1; /* filter allocated and valid */
98 u32 locked:1; /* filter is administratively locked */
99
100 u32 pending:1; /* filter action is pending firmware reply */
101 u32 smtidx:8; /* Source MAC Table index for smac */
102 struct l2t_entry *l2t; /* Layer Two Table entry for dmac */
103
104 /* The filter itself. Most of this is a straight copy of information
105 * provided by the extended ioctl(). Some fields are translated to
106 * internal forms -- for instance the Ingress Queue ID passed in from
107 * the ioctl() is translated into the Absolute Ingress Queue ID.
108 */
109 struct ch_filter_specification fs;
110};
111
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000112#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
113 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
114 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
115
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530116/* Macros needed to support the PCI Device ID Table ...
117 */
118#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530119 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530120#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000121
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530122/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
123 * called for both.
124 */
125#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
126
127#define CH_PCI_ID_TABLE_ENTRY(devid) \
128 {PCI_VDEVICE(CHELSIO, (devid)), 4}
129
130#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
131 { 0, } \
132 }
133
134#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000135
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530136#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000137#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530138#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530139#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000140#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530141#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530142#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
143#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
144#define PHY_AQ1202_DEVICEID 0x4409
145#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000146
147MODULE_DESCRIPTION(DRV_DESC);
148MODULE_AUTHOR("Chelsio Communications");
149MODULE_LICENSE("Dual BSD/GPL");
150MODULE_VERSION(DRV_VERSION);
151MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530152MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000153MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530154MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000155
Vipul Pandya636f9d32012-09-26 02:39:39 +0000156/*
157 * Normally we're willing to become the firmware's Master PF but will be happy
158 * if another PF has already become the Master and initialized the adapter.
159 * Setting "force_init" will cause this driver to forcibly establish itself as
160 * the Master PF and initialize the adapter.
161 */
162static uint force_init;
163
164module_param(force_init, uint, 0644);
Hariprasad Shenaid7d3e252015-12-24 16:24:53 +0530165MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
166 "deprecated parameter");
Vipul Pandya13ee15d2012-09-26 02:39:40 +0000167
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000168static int dflt_msg_enable = DFLT_MSG_ENABLE;
169
170module_param(dflt_msg_enable, int, 0644);
Hariprasad Shenai8a21ec42016-04-05 09:52:21 +0530171MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
172 "deprecated parameter");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000173
174/*
175 * The driver uses the best interrupt scheme available on a platform in the
176 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
177 * of these schemes the driver may consider as follows:
178 *
179 * msi = 2: choose from among all three options
180 * msi = 1: only consider MSI and INTx interrupts
181 * msi = 0: force INTx interrupts
182 */
183static int msi = 2;
184
185module_param(msi, int, 0644);
186MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
187
188/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000189 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
190 * offset by 2 bytes in order to have the IP headers line up on 4-byte
191 * boundaries. This is a requirement for many architectures which will throw
192 * a machine check fault if an attempt is made to access one of the 4-byte IP
193 * header fields on a non-4-byte boundary. And it's a major performance issue
194 * even on some architectures which allow it like some implementations of the
195 * x86 ISA. However, some architectures don't mind this and for some very
196 * edge-case performance sensitive applications (like forwarding large volumes
197 * of small packets), setting this DMA offset to 0 will decrease the number of
198 * PCI-E Bus transfers enough to measurably affect performance.
199 */
200static int rx_dma_offset = 2;
201
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000202#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000203/* Configure the number of PCI-E Virtual Function which are to be instantiated
204 * on SR-IOV Capable Physical Functions.
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000205 */
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000206static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000207
208module_param_array(num_vf, uint, NULL, 0644);
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000209MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000210#endif
211
Anish Bhatt688848b2014-06-19 21:37:13 -0700212/* TX Queue select used to determine what algorithm to use for selecting TX
213 * queue. Select between the kernel provided function (select_queue=0) or user
214 * cxgb_select_queue function (select_queue=1)
215 *
216 * Default: select_queue=0
217 */
218static int select_queue;
219module_param(select_queue, int, 0644);
220MODULE_PARM_DESC(select_queue,
221 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
222
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000223static struct dentry *cxgb4_debugfs_root;
224
225static LIST_HEAD(adapter_list);
226static DEFINE_MUTEX(uld_mutex);
Vipul Pandya01bcca62013-07-04 16:10:46 +0530227/* Adapter list to be accessed from atomic context */
228static LIST_HEAD(adap_rcu_list);
229static DEFINE_SPINLOCK(adap_rcu_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000230static struct cxgb4_uld_info ulds[CXGB4_ULD_MAX];
Varun Prakashf2692d12016-02-14 23:02:40 +0530231static const char *const uld_str[] = { "RDMA", "iSCSI", "iSCSIT" };
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000232
233static void link_report(struct net_device *dev)
234{
235 if (!netif_carrier_ok(dev))
236 netdev_info(dev, "link down\n");
237 else {
238 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
239
Hariprasad Shenai85412252015-10-01 13:48:48 +0530240 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000241 const struct port_info *p = netdev_priv(dev);
242
243 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000244 case 10000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000245 s = "10Gbps";
246 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000247 case 1000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000248 s = "1000Mbps";
249 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000250 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000251 s = "100Mbps";
252 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000253 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530254 s = "40Gbps";
255 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530256 default:
257 pr_info("%s: unsupported speed: %d\n",
258 dev->name, p->link_cfg.speed);
259 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000260 }
261
262 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
263 fc[p->link_cfg.fc]);
264 }
265}
266
Anish Bhatt688848b2014-06-19 21:37:13 -0700267#ifdef CONFIG_CHELSIO_T4_DCB
268/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
269static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
270{
271 struct port_info *pi = netdev_priv(dev);
272 struct adapter *adap = pi->adapter;
273 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
274 int i;
275
276 /* We use a simple mapping of Port TX Queue Index to DCB
277 * Priority when we're enabling DCB.
278 */
279 for (i = 0; i < pi->nqsets; i++, txq++) {
280 u32 name, value;
281 int err;
282
Hariprasad Shenai51678652014-11-21 12:52:02 +0530283 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
284 FW_PARAMS_PARAM_X_V(
285 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
286 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700287 value = enable ? i : 0xffffffff;
288
289 /* Since we can be called while atomic (from "interrupt
290 * level") we need to issue the Set Parameters Commannd
291 * without sleeping (timeout < 0).
292 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530293 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530294 &name, &value,
295 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700296
297 if (err)
298 dev_err(adap->pdev_dev,
299 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
300 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700301 else
302 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700303 }
304}
305#endif /* CONFIG_CHELSIO_T4_DCB */
306
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000307void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
308{
309 struct net_device *dev = adapter->port[port_id];
310
311 /* Skip changes from disabled ports. */
312 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
313 if (link_stat)
314 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700315 else {
316#ifdef CONFIG_CHELSIO_T4_DCB
317 cxgb4_dcb_state_init(dev);
318 dcb_tx_queue_prio_enable(dev, false);
319#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000320 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700321 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000322
323 link_report(dev);
324 }
325}
326
327void t4_os_portmod_changed(const struct adapter *adap, int port_id)
328{
329 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000330 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000331 };
332
333 const struct net_device *dev = adap->port[port_id];
334 const struct port_info *pi = netdev_priv(dev);
335
336 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
337 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000338 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000339 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530340 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
341 netdev_info(dev, "%s: unsupported port module inserted\n",
342 dev->name);
343 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
344 netdev_info(dev, "%s: unknown port module inserted\n",
345 dev->name);
346 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
347 netdev_info(dev, "%s: transceiver module error\n", dev->name);
348 else
349 netdev_info(dev, "%s: unknown module type %d inserted\n",
350 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000351}
352
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530353int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
354module_param(dbfifo_int_thresh, int, 0644);
355MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
356
Vipul Pandya404d9e32012-10-08 02:59:43 +0000357/*
358 * usecs to sleep while draining the dbfifo
359 */
360static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530361module_param(dbfifo_drain_delay, int, 0644);
362MODULE_PARM_DESC(dbfifo_drain_delay,
363 "usecs to sleep while draining the dbfifo");
364
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530365static inline int cxgb4_set_addr_hash(struct port_info *pi)
366{
367 struct adapter *adap = pi->adapter;
368 u64 vec = 0;
369 bool ucast = false;
370 struct hash_mac_addr *entry;
371
372 /* Calculate the hash vector for the updated list and program it */
373 list_for_each_entry(entry, &adap->mac_hlist, list) {
374 ucast |= is_unicast_ether_addr(entry->addr);
375 vec |= (1ULL << hash_mac_addr(entry->addr));
376 }
377 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
378 vec, false);
379}
380
381static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
382{
383 struct port_info *pi = netdev_priv(netdev);
384 struct adapter *adap = pi->adapter;
385 int ret;
386 u64 mhash = 0;
387 u64 uhash = 0;
388 bool free = false;
389 bool ucast = is_unicast_ether_addr(mac_addr);
390 const u8 *maclist[1] = {mac_addr};
391 struct hash_mac_addr *new_entry;
392
393 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
394 NULL, ucast ? &uhash : &mhash, false);
395 if (ret < 0)
396 goto out;
397 /* if hash != 0, then add the addr to hash addr list
398 * so on the end we will calculate the hash for the
399 * list and program it
400 */
401 if (uhash || mhash) {
402 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
403 if (!new_entry)
404 return -ENOMEM;
405 ether_addr_copy(new_entry->addr, mac_addr);
406 list_add_tail(&new_entry->list, &adap->mac_hlist);
407 ret = cxgb4_set_addr_hash(pi);
408 }
409out:
410 return ret < 0 ? ret : 0;
411}
412
413static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
414{
415 struct port_info *pi = netdev_priv(netdev);
416 struct adapter *adap = pi->adapter;
417 int ret;
418 const u8 *maclist[1] = {mac_addr};
419 struct hash_mac_addr *entry, *tmp;
420
421 /* If the MAC address to be removed is in the hash addr
422 * list, delete it from the list and update hash vector
423 */
424 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
425 if (ether_addr_equal(entry->addr, mac_addr)) {
426 list_del(&entry->list);
427 kfree(entry);
428 return cxgb4_set_addr_hash(pi);
429 }
430 }
431
432 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
433 return ret < 0 ? -EINVAL : 0;
434}
435
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000436/*
437 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
438 * If @mtu is -1 it is left unchanged.
439 */
440static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
441{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000442 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530443 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000444
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530445 if (!(dev->flags & IFF_PROMISC)) {
446 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
447 if (!(dev->flags & IFF_ALLMULTI))
448 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
449 }
450
451 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
452 (dev->flags & IFF_PROMISC) ? 1 : 0,
453 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
454 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000455}
456
457/**
458 * link_start - enable a port
459 * @dev: the port to enable
460 *
461 * Performs the MAC and PHY actions needed to enable a port.
462 */
463static int link_start(struct net_device *dev)
464{
465 int ret;
466 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530467 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000468
469 /*
470 * We do not set address filters and promiscuity here, the stack does
471 * that step explicitly.
472 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000473 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000474 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000475 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000476 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000477 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000478 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000479 if (ret >= 0) {
480 pi->xact_addr_filt = ret;
481 ret = 0;
482 }
483 }
484 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530485 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000486 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700487 if (ret == 0) {
488 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700489 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
490 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700491 local_bh_enable();
492 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700493
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000494 return ret;
495}
496
Anish Bhatt688848b2014-06-19 21:37:13 -0700497int cxgb4_dcb_enabled(const struct net_device *dev)
498{
499#ifdef CONFIG_CHELSIO_T4_DCB
500 struct port_info *pi = netdev_priv(dev);
501
Anish Bhatt3bb06262014-10-23 14:37:31 -0700502 if (!pi->dcb.enabled)
503 return 0;
504
505 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
506 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Anish Bhatt688848b2014-06-19 21:37:13 -0700507#else
508 return 0;
509#endif
510}
511EXPORT_SYMBOL(cxgb4_dcb_enabled);
512
513#ifdef CONFIG_CHELSIO_T4_DCB
514/* Handle a Data Center Bridging update message from the firmware. */
515static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
516{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530517 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700518 struct net_device *dev = adap->port[port];
519 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
520 int new_dcb_enabled;
521
522 cxgb4_dcb_handle_fw_update(adap, pcmd);
523 new_dcb_enabled = cxgb4_dcb_enabled(dev);
524
525 /* If the DCB has become enabled or disabled on the port then we're
526 * going to need to set up/tear down DCB Priority parameters for the
527 * TX Queues associated with the port.
528 */
529 if (new_dcb_enabled != old_dcb_enabled)
530 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
531}
532#endif /* CONFIG_CHELSIO_T4_DCB */
533
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000534/* Clear a filter and release any of its resources that we own. This also
535 * clears the filter's "pending" status.
536 */
537static void clear_filter(struct adapter *adap, struct filter_entry *f)
538{
539 /* If the new or old filter have loopback rewriteing rules then we'll
540 * need to free any existing Layer Two Table (L2T) entries of the old
541 * filter rule. The firmware will handle freeing up any Source MAC
542 * Table (SMT) entries used for rewriting Source MAC Addresses in
543 * loopback rules.
544 */
545 if (f->l2t)
546 cxgb4_l2t_release(f->l2t);
547
548 /* The zeroing of the filter rule below clears the filter valid,
549 * pending, locked flags, l2t pointer, etc. so it's all we need for
550 * this operation.
551 */
552 memset(f, 0, sizeof(*f));
553}
554
555/* Handle a filter write/deletion reply.
556 */
557static void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
558{
559 unsigned int idx = GET_TID(rpl);
560 unsigned int nidx = idx - adap->tids.ftid_base;
561 unsigned int ret;
562 struct filter_entry *f;
563
564 if (idx >= adap->tids.ftid_base && nidx <
565 (adap->tids.nftids + adap->tids.nsftids)) {
566 idx = nidx;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800567 ret = TCB_COOKIE_G(rpl->cookie);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000568 f = &adap->tids.ftid_tab[idx];
569
570 if (ret == FW_FILTER_WR_FLT_DELETED) {
571 /* Clear the filter when we get confirmation from the
572 * hardware that the filter has been deleted.
573 */
574 clear_filter(adap, f);
575 } else if (ret == FW_FILTER_WR_SMT_TBL_FULL) {
576 dev_err(adap->pdev_dev, "filter %u setup failed due to full SMT\n",
577 idx);
578 clear_filter(adap, f);
579 } else if (ret == FW_FILTER_WR_FLT_ADDED) {
580 f->smtidx = (be64_to_cpu(rpl->oldval) >> 24) & 0xff;
581 f->pending = 0; /* asynchronous setup completed */
582 f->valid = 1;
583 } else {
584 /* Something went wrong. Issue a warning about the
585 * problem and clear everything out.
586 */
587 dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
588 idx, ret);
589 clear_filter(adap, f);
590 }
591 }
592}
593
594/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000595 */
596static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
597 const struct pkt_gl *gl)
598{
599 u8 opcode = ((const struct rss_header *)rsp)->opcode;
600
601 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000602
603 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
604 */
605 if (unlikely(opcode == CPL_FW4_MSG &&
606 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
607 rsp++;
608 opcode = ((const struct rss_header *)rsp)->opcode;
609 rsp++;
610 if (opcode != CPL_SGE_EGR_UPDATE) {
611 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
612 , opcode);
613 goto out;
614 }
615 }
616
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000617 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
618 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800619 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000620 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000621
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000622 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000623 txq->restarts++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000624 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000625 struct sge_eth_txq *eq;
626
627 eq = container_of(txq, struct sge_eth_txq, q);
628 netif_tx_wake_queue(eq->txq);
629 } else {
630 struct sge_ofld_txq *oq;
631
632 oq = container_of(txq, struct sge_ofld_txq, q);
633 tasklet_schedule(&oq->qresume_tsk);
634 }
635 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
636 const struct cpl_fw6_msg *p = (void *)rsp;
637
Anish Bhatt688848b2014-06-19 21:37:13 -0700638#ifdef CONFIG_CHELSIO_T4_DCB
639 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530640 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700641 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530642 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700643
644 if (cmd == FW_PORT_CMD &&
645 action == FW_PORT_ACTION_GET_PORT_INFO) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530646 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700647 be32_to_cpu(pcmd->op_to_portid));
648 struct net_device *dev = q->adap->port[port];
649 int state_input = ((pcmd->u.info.dcbxdis_pkd &
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530650 FW_PORT_CMD_DCBXDIS_F)
Anish Bhatt688848b2014-06-19 21:37:13 -0700651 ? CXGB4_DCB_INPUT_FW_DISABLED
652 : CXGB4_DCB_INPUT_FW_ENABLED);
653
654 cxgb4_dcb_state_fsm(dev, state_input);
655 }
656
657 if (cmd == FW_PORT_CMD &&
658 action == FW_PORT_ACTION_L2_DCB_CFG)
659 dcb_rpl(q->adap, pcmd);
660 else
661#endif
662 if (p->type == 0)
663 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000664 } else if (opcode == CPL_L2T_WRITE_RPL) {
665 const struct cpl_l2t_write_rpl *p = (void *)rsp;
666
667 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000668 } else if (opcode == CPL_SET_TCB_RPL) {
669 const struct cpl_set_tcb_rpl *p = (void *)rsp;
670
671 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000672 } else
673 dev_err(q->adap->pdev_dev,
674 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000675out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000676 return 0;
677}
678
Varun Prakash2337ba42016-02-14 23:02:41 +0530679/* Flush the aggregated lro sessions */
680static void uldrx_flush_handler(struct sge_rspq *q)
681{
682 if (ulds[q->uld].lro_flush)
683 ulds[q->uld].lro_flush(&q->lro_mgr);
684}
685
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000686/**
687 * uldrx_handler - response queue handler for ULD queues
688 * @q: the response queue that received the packet
689 * @rsp: the response queue descriptor holding the offload message
690 * @gl: the gather list of packet fragments
691 *
692 * Deliver an ingress offload packet to a ULD. All processing is done by
693 * the ULD, we just maintain statistics.
694 */
695static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
696 const struct pkt_gl *gl)
697{
698 struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
Varun Prakash2337ba42016-02-14 23:02:41 +0530699 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000700
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000701 /* FW can send CPLs encapsulated in a CPL_FW4_MSG.
702 */
703 if (((const struct rss_header *)rsp)->opcode == CPL_FW4_MSG &&
704 ((const struct cpl_fw4_msg *)(rsp + 1))->type == FW_TYPE_RSSCPL)
705 rsp += 2;
706
Varun Prakash2337ba42016-02-14 23:02:41 +0530707 if (q->flush_handler)
708 ret = ulds[q->uld].lro_rx_handler(q->adap->uld_handle[q->uld],
709 rsp, gl, &q->lro_mgr,
710 &q->napi);
711 else
712 ret = ulds[q->uld].rx_handler(q->adap->uld_handle[q->uld],
713 rsp, gl);
714
715 if (ret) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000716 rxq->stats.nomem++;
717 return -1;
718 }
Varun Prakash2337ba42016-02-14 23:02:41 +0530719
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000720 if (gl == NULL)
721 rxq->stats.imm++;
722 else if (gl == CXGB4_MSG_AN)
723 rxq->stats.an++;
724 else
725 rxq->stats.pkts++;
726 return 0;
727}
728
729static void disable_msi(struct adapter *adapter)
730{
731 if (adapter->flags & USING_MSIX) {
732 pci_disable_msix(adapter->pdev);
733 adapter->flags &= ~USING_MSIX;
734 } else if (adapter->flags & USING_MSI) {
735 pci_disable_msi(adapter->pdev);
736 adapter->flags &= ~USING_MSI;
737 }
738}
739
740/*
741 * Interrupt handler for non-data events used with MSI-X.
742 */
743static irqreturn_t t4_nondata_intr(int irq, void *cookie)
744{
745 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530746 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000747
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530748 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000749 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530750 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000751 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530752 if (adap->flags & MASTER_PF)
753 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000754 return IRQ_HANDLED;
755}
756
757/*
758 * Name the MSI-X interrupts.
759 */
760static void name_msix_vecs(struct adapter *adap)
761{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000762 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000763
764 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000765 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000766
767 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000768 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
769 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000770
771 /* Ethernet queues */
772 for_each_port(adap, j) {
773 struct net_device *d = adap->port[j];
774 const struct port_info *pi = netdev_priv(d);
775
Dimitris Michailidisba278162010-12-14 21:36:50 +0000776 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000777 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
778 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000779 }
780
781 /* offload queues */
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530782 for_each_iscsirxq(&adap->sge, i)
783 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-iscsi%d",
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000784 adap->port[0]->name, i);
Dimitris Michailidisba278162010-12-14 21:36:50 +0000785
Varun Prakashf2692d12016-02-14 23:02:40 +0530786 for_each_iscsitrxq(&adap->sge, i)
787 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-iSCSIT%d",
788 adap->port[0]->name, i);
789
Dimitris Michailidisba278162010-12-14 21:36:50 +0000790 for_each_rdmarxq(&adap->sge, i)
791 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma%d",
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000792 adap->port[0]->name, i);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530793
794 for_each_rdmaciq(&adap->sge, i)
795 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma-ciq%d",
796 adap->port[0]->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000797}
798
799static int request_msix_queue_irqs(struct adapter *adap)
800{
801 struct sge *s = &adap->sge;
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530802 int err, ethqidx, iscsiqidx = 0, rdmaqidx = 0, rdmaciqqidx = 0;
Varun Prakashf2692d12016-02-14 23:02:40 +0530803 int iscsitqidx = 0;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530804 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000805
806 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
807 adap->msix_info[1].desc, &s->fw_evtq);
808 if (err)
809 return err;
810
811 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000812 err = request_irq(adap->msix_info[msi_index].vec,
813 t4_sge_intr_msix, 0,
814 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815 &s->ethrxq[ethqidx].rspq);
816 if (err)
817 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000818 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000819 }
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530820 for_each_iscsirxq(s, iscsiqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000821 err = request_irq(adap->msix_info[msi_index].vec,
822 t4_sge_intr_msix, 0,
823 adap->msix_info[msi_index].desc,
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530824 &s->iscsirxq[iscsiqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000825 if (err)
826 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000827 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000828 }
Varun Prakashf2692d12016-02-14 23:02:40 +0530829 for_each_iscsitrxq(s, iscsitqidx) {
830 err = request_irq(adap->msix_info[msi_index].vec,
831 t4_sge_intr_msix, 0,
832 adap->msix_info[msi_index].desc,
833 &s->iscsitrxq[iscsitqidx].rspq);
834 if (err)
835 goto unwind;
836 msi_index++;
837 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000838 for_each_rdmarxq(s, rdmaqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000839 err = request_irq(adap->msix_info[msi_index].vec,
840 t4_sge_intr_msix, 0,
841 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000842 &s->rdmarxq[rdmaqidx].rspq);
843 if (err)
844 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000845 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000846 }
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530847 for_each_rdmaciq(s, rdmaciqqidx) {
848 err = request_irq(adap->msix_info[msi_index].vec,
849 t4_sge_intr_msix, 0,
850 adap->msix_info[msi_index].desc,
851 &s->rdmaciq[rdmaciqqidx].rspq);
852 if (err)
853 goto unwind;
854 msi_index++;
855 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000856 return 0;
857
858unwind:
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530859 while (--rdmaciqqidx >= 0)
860 free_irq(adap->msix_info[--msi_index].vec,
861 &s->rdmaciq[rdmaciqqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000862 while (--rdmaqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000863 free_irq(adap->msix_info[--msi_index].vec,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000864 &s->rdmarxq[rdmaqidx].rspq);
Varun Prakashf2692d12016-02-14 23:02:40 +0530865 while (--iscsitqidx >= 0)
866 free_irq(adap->msix_info[--msi_index].vec,
867 &s->iscsitrxq[iscsitqidx].rspq);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530868 while (--iscsiqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000869 free_irq(adap->msix_info[--msi_index].vec,
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530870 &s->iscsirxq[iscsiqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000871 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000872 free_irq(adap->msix_info[--msi_index].vec,
873 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000874 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
875 return err;
876}
877
878static void free_msix_queue_irqs(struct adapter *adap)
879{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000880 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000881 struct sge *s = &adap->sge;
882
883 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
884 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000885 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530886 for_each_iscsirxq(s, i)
887 free_irq(adap->msix_info[msi_index++].vec,
888 &s->iscsirxq[i].rspq);
Varun Prakashf2692d12016-02-14 23:02:40 +0530889 for_each_iscsitrxq(s, i)
890 free_irq(adap->msix_info[msi_index++].vec,
891 &s->iscsitrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000892 for_each_rdmarxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000893 free_irq(adap->msix_info[msi_index++].vec, &s->rdmarxq[i].rspq);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530894 for_each_rdmaciq(s, i)
895 free_irq(adap->msix_info[msi_index++].vec, &s->rdmaciq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000896}
897
898/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530899 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000900 * @pi: the port
901 * @queues: array of queue indices for RSS
902 *
903 * Sets up the portion of the HW RSS table for the port's VI to distribute
904 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530905 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000906 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530907int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000908{
909 u16 *rss;
910 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530911 struct adapter *adapter = pi->adapter;
912 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000913
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530914 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000915 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
916 if (!rss)
917 return -ENOMEM;
918
919 /* map the queue indices to queue ids */
920 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530921 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000922
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530923 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000924 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530925 /* If Tunnel All Lookup isn't specified in the global RSS
926 * Configuration, then we need to specify a default Ingress
927 * Queue for any ingress packets which aren't hashed. We'll
928 * use our first ingress queue ...
929 */
930 if (!err)
931 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
932 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
933 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
934 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
935 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
936 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
937 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000938 kfree(rss);
939 return err;
940}
941
942/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000943 * setup_rss - configure RSS
944 * @adap: the adapter
945 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000946 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000947 */
948static int setup_rss(struct adapter *adap)
949{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530950 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000951
952 for_each_port(adap, i) {
953 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000954
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530955 /* Fill default values with equal distribution */
956 for (j = 0; j < pi->rss_size; j++)
957 pi->rss[j] = j % pi->nqsets;
958
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530959 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000960 if (err)
961 return err;
962 }
963 return 0;
964}
965
966/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000967 * Return the channel of the ingress queue with the given qid.
968 */
969static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
970{
971 qid -= p->ingr_start;
972 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
973}
974
975/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000976 * Wait until all NAPI handlers are descheduled.
977 */
978static void quiesce_rx(struct adapter *adap)
979{
980 int i;
981
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530982 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000983 struct sge_rspq *q = adap->sge.ingr_map[i];
984
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530985 if (q && q->handler) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000986 napi_disable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530987 local_bh_disable();
988 while (!cxgb_poll_lock_napi(q))
989 mdelay(1);
990 local_bh_enable();
991 }
992
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000993 }
994}
995
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530996/* Disable interrupt and napi handler */
997static void disable_interrupts(struct adapter *adap)
998{
999 if (adap->flags & FULL_INIT_DONE) {
1000 t4_intr_disable(adap);
1001 if (adap->flags & USING_MSIX) {
1002 free_msix_queue_irqs(adap);
1003 free_irq(adap->msix_info[0].vec, adap);
1004 } else {
1005 free_irq(adap->pdev->irq, adap);
1006 }
1007 quiesce_rx(adap);
1008 }
1009}
1010
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001011/*
1012 * Enable NAPI scheduling and interrupt generation for all Rx queues.
1013 */
1014static void enable_rx(struct adapter *adap)
1015{
1016 int i;
1017
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301018 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001019 struct sge_rspq *q = adap->sge.ingr_map[i];
1020
1021 if (!q)
1022 continue;
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301023 if (q->handler) {
1024 cxgb_busy_poll_init_lock(q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001025 napi_enable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301026 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001027 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301028 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
1029 SEINTARM_V(q->intr_params) |
1030 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001031 }
1032}
1033
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301034static int alloc_ofld_rxqs(struct adapter *adap, struct sge_ofld_rxq *q,
1035 unsigned int nq, unsigned int per_chan, int msi_idx,
Varun Prakash2337ba42016-02-14 23:02:41 +05301036 u16 *ids, bool lro)
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301037{
1038 int i, err;
1039
1040 for (i = 0; i < nq; i++, q++) {
1041 if (msi_idx > 0)
1042 msi_idx++;
1043 err = t4_sge_alloc_rxq(adap, &q->rspq, false,
1044 adap->port[i / per_chan],
1045 msi_idx, q->fl.size ? &q->fl : NULL,
Varun Prakash2337ba42016-02-14 23:02:41 +05301046 uldrx_handler,
1047 lro ? uldrx_flush_handler : NULL,
1048 0);
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301049 if (err)
1050 return err;
1051 memset(&q->stats, 0, sizeof(q->stats));
1052 if (ids)
1053 ids[i] = q->rspq.abs_id;
1054 }
1055 return 0;
1056}
1057
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001058/**
1059 * setup_sge_queues - configure SGE Tx/Rx/response queues
1060 * @adap: the adapter
1061 *
1062 * Determines how many sets of SGE queues to use and initializes them.
1063 * We support multiple queue sets per port if we have MSI-X, otherwise
1064 * just one queue set per port.
1065 */
1066static int setup_sge_queues(struct adapter *adap)
1067{
1068 int err, msi_idx, i, j;
1069 struct sge *s = &adap->sge;
1070
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301071 bitmap_zero(s->starving_fl, s->egr_sz);
1072 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001073
1074 if (adap->flags & USING_MSIX)
1075 msi_idx = 1; /* vector 0 is for non-queue interrupts */
1076 else {
1077 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +05301078 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001079 if (err)
1080 return err;
1081 msi_idx = -((int)s->intrq.abs_id + 1);
1082 }
1083
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301084 /* NOTE: If you add/delete any Ingress/Egress Queue allocations in here,
1085 * don't forget to update the following which need to be
1086 * synchronized to and changes here.
1087 *
1088 * 1. The calculations of MAX_INGQ in cxgb4.h.
1089 *
1090 * 2. Update enable_msix/name_msix_vecs/request_msix_queue_irqs
1091 * to accommodate any new/deleted Ingress Queues
1092 * which need MSI-X Vectors.
1093 *
1094 * 3. Update sge_qinfo_show() to include information on the
1095 * new/deleted queues.
1096 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001097 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Varun Prakash2337ba42016-02-14 23:02:41 +05301098 msi_idx, NULL, fwevtq_handler, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001099 if (err) {
1100freeout: t4_free_sge_resources(adap);
1101 return err;
1102 }
1103
1104 for_each_port(adap, i) {
1105 struct net_device *dev = adap->port[i];
1106 struct port_info *pi = netdev_priv(dev);
1107 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
1108 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
1109
1110 for (j = 0; j < pi->nqsets; j++, q++) {
1111 if (msi_idx > 0)
1112 msi_idx++;
1113 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
1114 msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05301115 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +05301116 NULL,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05301117 t4_get_mps_bg_map(adap,
1118 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001119 if (err)
1120 goto freeout;
1121 q->rspq.idx = j;
1122 memset(&q->stats, 0, sizeof(q->stats));
1123 }
1124 for (j = 0; j < pi->nqsets; j++, t++) {
1125 err = t4_sge_alloc_eth_txq(adap, t, dev,
1126 netdev_get_tx_queue(dev, j),
1127 s->fw_evtq.cntxt_id);
1128 if (err)
1129 goto freeout;
1130 }
1131 }
1132
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05301133 j = s->iscsiqsets / adap->params.nports; /* iscsi queues per channel */
1134 for_each_iscsirxq(s, i) {
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301135 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
1136 adap->port[i / j],
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001137 s->fw_evtq.cntxt_id);
1138 if (err)
1139 goto freeout;
1140 }
1141
Varun Prakash2337ba42016-02-14 23:02:41 +05301142#define ALLOC_OFLD_RXQS(firstq, nq, per_chan, ids, lro) do { \
1143 err = alloc_ofld_rxqs(adap, firstq, nq, per_chan, msi_idx, ids, lro); \
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301144 if (err) \
1145 goto freeout; \
1146 if (msi_idx > 0) \
1147 msi_idx += nq; \
1148} while (0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001149
Varun Prakash2337ba42016-02-14 23:02:41 +05301150 ALLOC_OFLD_RXQS(s->iscsirxq, s->iscsiqsets, j, s->iscsi_rxq, false);
1151 ALLOC_OFLD_RXQS(s->iscsitrxq, s->niscsitq, j, s->iscsit_rxq, true);
1152 ALLOC_OFLD_RXQS(s->rdmarxq, s->rdmaqs, 1, s->rdma_rxq, false);
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05301153 j = s->rdmaciqs / adap->params.nports; /* rdmaq queues per channel */
Varun Prakash2337ba42016-02-14 23:02:41 +05301154 ALLOC_OFLD_RXQS(s->rdmaciq, s->rdmaciqs, j, s->rdma_ciq, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001155
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301156#undef ALLOC_OFLD_RXQS
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05301157
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001158 for_each_port(adap, i) {
1159 /*
1160 * Note that ->rdmarxq[i].rspq.cntxt_id below is 0 if we don't
1161 * have RDMA queues, and that's the right value.
1162 */
1163 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
1164 s->fw_evtq.cntxt_id,
1165 s->rdmarxq[i].rspq.cntxt_id);
1166 if (err)
1167 goto freeout;
1168 }
1169
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +05301170 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05301171 MPS_TRC_RSS_CONTROL_A :
1172 MPS_T5_TRC_RSS_CONTROL_A,
1173 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
1174 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001175 return 0;
1176}
1177
1178/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001179 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1180 * The allocated memory is cleared.
1181 */
1182void *t4_alloc_mem(size_t size)
1183{
Joe Perches8be04b92013-06-19 12:15:53 -07001184 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001185
1186 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001187 p = vzalloc(size);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001188 return p;
1189}
1190
1191/*
1192 * Free memory allocated through alloc_mem().
1193 */
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301194void t4_free_mem(void *addr)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001195{
Pekka Enbergd2fcb542015-06-30 14:59:12 -07001196 kvfree(addr);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001197}
1198
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001199/* Send a Work Request to write the filter at a specified index. We construct
1200 * a Firmware Filter Work Request to have the work done and put the indicated
1201 * filter into "pending" mode which will prevent any further actions against
1202 * it till we get a reply from the firmware on the completion status of the
1203 * request.
1204 */
1205static int set_filter_wr(struct adapter *adapter, int fidx)
1206{
1207 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1208 struct sk_buff *skb;
1209 struct fw_filter_wr *fwr;
1210 unsigned int ftid;
1211
Michal Hockof72f1162015-04-14 13:24:33 -07001212 skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
1213 if (!skb)
1214 return -ENOMEM;
1215
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001216 /* If the new filter requires loopback Destination MAC and/or VLAN
1217 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1218 * the filter.
1219 */
1220 if (f->fs.newdmac || f->fs.newvlan) {
1221 /* allocate L2T entry for new filter */
Hariprasad Shenaif7502652015-12-17 13:45:08 +05301222 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
1223 f->fs.eport, f->fs.dmac);
Michal Hockof72f1162015-04-14 13:24:33 -07001224 if (f->l2t == NULL) {
1225 kfree_skb(skb);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001226 return -ENOMEM;
1227 }
1228 }
1229
1230 ftid = adapter->tids.ftid_base + fidx;
1231
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001232 fwr = (struct fw_filter_wr *)__skb_put(skb, sizeof(*fwr));
1233 memset(fwr, 0, sizeof(*fwr));
1234
1235 /* It would be nice to put most of the following in t4_hw.c but most
1236 * of the work is translating the cxgbtool ch_filter_specification
1237 * into the Work Request and the definition of that structure is
1238 * currently in cxgbtool.h which isn't appropriate to pull into the
1239 * common code. We may eventually try to come up with a more neutral
1240 * filter specification structure but for now it's easiest to simply
1241 * put this fairly direct code in line ...
1242 */
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301243 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
1244 fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr)/16));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001245 fwr->tid_to_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301246 htonl(FW_FILTER_WR_TID_V(ftid) |
1247 FW_FILTER_WR_RQTYPE_V(f->fs.type) |
1248 FW_FILTER_WR_NOREPLY_V(0) |
1249 FW_FILTER_WR_IQ_V(f->fs.iq));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001250 fwr->del_filter_to_l2tix =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301251 htonl(FW_FILTER_WR_RPTTID_V(f->fs.rpttid) |
1252 FW_FILTER_WR_DROP_V(f->fs.action == FILTER_DROP) |
1253 FW_FILTER_WR_DIRSTEER_V(f->fs.dirsteer) |
1254 FW_FILTER_WR_MASKHASH_V(f->fs.maskhash) |
1255 FW_FILTER_WR_DIRSTEERHASH_V(f->fs.dirsteerhash) |
1256 FW_FILTER_WR_LPBK_V(f->fs.action == FILTER_SWITCH) |
1257 FW_FILTER_WR_DMAC_V(f->fs.newdmac) |
1258 FW_FILTER_WR_SMAC_V(f->fs.newsmac) |
1259 FW_FILTER_WR_INSVLAN_V(f->fs.newvlan == VLAN_INSERT ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001260 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301261 FW_FILTER_WR_RMVLAN_V(f->fs.newvlan == VLAN_REMOVE ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001262 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301263 FW_FILTER_WR_HITCNTS_V(f->fs.hitcnts) |
1264 FW_FILTER_WR_TXCHAN_V(f->fs.eport) |
1265 FW_FILTER_WR_PRIO_V(f->fs.prio) |
1266 FW_FILTER_WR_L2TIX_V(f->l2t ? f->l2t->idx : 0));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001267 fwr->ethtype = htons(f->fs.val.ethtype);
1268 fwr->ethtypem = htons(f->fs.mask.ethtype);
1269 fwr->frag_to_ovlan_vldm =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301270 (FW_FILTER_WR_FRAG_V(f->fs.val.frag) |
1271 FW_FILTER_WR_FRAGM_V(f->fs.mask.frag) |
1272 FW_FILTER_WR_IVLAN_VLD_V(f->fs.val.ivlan_vld) |
1273 FW_FILTER_WR_OVLAN_VLD_V(f->fs.val.ovlan_vld) |
1274 FW_FILTER_WR_IVLAN_VLDM_V(f->fs.mask.ivlan_vld) |
1275 FW_FILTER_WR_OVLAN_VLDM_V(f->fs.mask.ovlan_vld));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001276 fwr->smac_sel = 0;
1277 fwr->rx_chan_rx_rpl_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301278 htons(FW_FILTER_WR_RX_CHAN_V(0) |
1279 FW_FILTER_WR_RX_RPL_IQ_V(adapter->sge.fw_evtq.abs_id));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001280 fwr->maci_to_matchtypem =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301281 htonl(FW_FILTER_WR_MACI_V(f->fs.val.macidx) |
1282 FW_FILTER_WR_MACIM_V(f->fs.mask.macidx) |
1283 FW_FILTER_WR_FCOE_V(f->fs.val.fcoe) |
1284 FW_FILTER_WR_FCOEM_V(f->fs.mask.fcoe) |
1285 FW_FILTER_WR_PORT_V(f->fs.val.iport) |
1286 FW_FILTER_WR_PORTM_V(f->fs.mask.iport) |
1287 FW_FILTER_WR_MATCHTYPE_V(f->fs.val.matchtype) |
1288 FW_FILTER_WR_MATCHTYPEM_V(f->fs.mask.matchtype));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001289 fwr->ptcl = f->fs.val.proto;
1290 fwr->ptclm = f->fs.mask.proto;
1291 fwr->ttyp = f->fs.val.tos;
1292 fwr->ttypm = f->fs.mask.tos;
1293 fwr->ivlan = htons(f->fs.val.ivlan);
1294 fwr->ivlanm = htons(f->fs.mask.ivlan);
1295 fwr->ovlan = htons(f->fs.val.ovlan);
1296 fwr->ovlanm = htons(f->fs.mask.ovlan);
1297 memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
1298 memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
1299 memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
1300 memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
1301 fwr->lp = htons(f->fs.val.lport);
1302 fwr->lpm = htons(f->fs.mask.lport);
1303 fwr->fp = htons(f->fs.val.fport);
1304 fwr->fpm = htons(f->fs.mask.fport);
1305 if (f->fs.newsmac)
1306 memcpy(fwr->sma, f->fs.smac, sizeof(fwr->sma));
1307
1308 /* Mark the filter as "pending" and ship off the Filter Work Request.
1309 * When we get the Work Request Reply we'll clear the pending status.
1310 */
1311 f->pending = 1;
1312 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
1313 t4_ofld_send(adapter, skb);
1314 return 0;
1315}
1316
1317/* Delete the filter at a specified index.
1318 */
1319static int del_filter_wr(struct adapter *adapter, int fidx)
1320{
1321 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1322 struct sk_buff *skb;
1323 struct fw_filter_wr *fwr;
1324 unsigned int len, ftid;
1325
1326 len = sizeof(*fwr);
1327 ftid = adapter->tids.ftid_base + fidx;
1328
Michal Hockof72f1162015-04-14 13:24:33 -07001329 skb = alloc_skb(len, GFP_KERNEL);
1330 if (!skb)
1331 return -ENOMEM;
1332
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001333 fwr = (struct fw_filter_wr *)__skb_put(skb, len);
1334 t4_mk_filtdelwr(ftid, fwr, adapter->sge.fw_evtq.abs_id);
1335
1336 /* Mark the filter as "pending" and ship off the Filter Work Request.
1337 * When we get the Work Request Reply we'll clear the pending status.
1338 */
1339 f->pending = 1;
1340 t4_mgmt_tx(adapter, skb);
1341 return 0;
1342}
1343
Anish Bhatt688848b2014-06-19 21:37:13 -07001344static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
1345 void *accel_priv, select_queue_fallback_t fallback)
1346{
1347 int txq;
1348
1349#ifdef CONFIG_CHELSIO_T4_DCB
1350 /* If a Data Center Bridging has been successfully negotiated on this
1351 * link then we'll use the skb's priority to map it to a TX Queue.
1352 * The skb's priority is determined via the VLAN Tag Priority Code
1353 * Point field.
1354 */
1355 if (cxgb4_dcb_enabled(dev)) {
1356 u16 vlan_tci;
1357 int err;
1358
1359 err = vlan_get_tag(skb, &vlan_tci);
1360 if (unlikely(err)) {
1361 if (net_ratelimit())
1362 netdev_warn(dev,
1363 "TX Packet without VLAN Tag on DCB Link\n");
1364 txq = 0;
1365 } else {
1366 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +05301367#ifdef CONFIG_CHELSIO_T4_FCOE
1368 if (skb->protocol == htons(ETH_P_FCOE))
1369 txq = skb->priority & 0x7;
1370#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -07001371 }
1372 return txq;
1373 }
1374#endif /* CONFIG_CHELSIO_T4_DCB */
1375
1376 if (select_queue) {
1377 txq = (skb_rx_queue_recorded(skb)
1378 ? skb_get_rx_queue(skb)
1379 : smp_processor_id());
1380
1381 while (unlikely(txq >= dev->real_num_tx_queues))
1382 txq -= dev->real_num_tx_queues;
1383
1384 return txq;
1385 }
1386
1387 return fallback(dev, skb) % dev->real_num_tx_queues;
1388}
1389
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001390static int closest_timer(const struct sge *s, int time)
1391{
1392 int i, delta, match = 0, min_delta = INT_MAX;
1393
1394 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
1395 delta = time - s->timer_val[i];
1396 if (delta < 0)
1397 delta = -delta;
1398 if (delta < min_delta) {
1399 min_delta = delta;
1400 match = i;
1401 }
1402 }
1403 return match;
1404}
1405
1406static int closest_thres(const struct sge *s, int thres)
1407{
1408 int i, delta, match = 0, min_delta = INT_MAX;
1409
1410 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1411 delta = thres - s->counter_val[i];
1412 if (delta < 0)
1413 delta = -delta;
1414 if (delta < min_delta) {
1415 min_delta = delta;
1416 match = i;
1417 }
1418 }
1419 return match;
1420}
1421
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001422/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301423 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001424 * @q: the Rx queue
1425 * @us: the hold-off time in us, or 0 to disable timer
1426 * @cnt: the hold-off packet count, or 0 to disable counter
1427 *
1428 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1429 * one of the two needs to be enabled for the queue to generate interrupts.
1430 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301431int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1432 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001433{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301434 struct adapter *adap = q->adap;
1435
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001436 if ((us | cnt) == 0)
1437 cnt = 1;
1438
1439 if (cnt) {
1440 int err;
1441 u32 v, new_idx;
1442
1443 new_idx = closest_thres(&adap->sge, cnt);
1444 if (q->desc && q->pktcnt_idx != new_idx) {
1445 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301446 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1447 FW_PARAMS_PARAM_X_V(
1448 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1449 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301450 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1451 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001452 if (err)
1453 return err;
1454 }
1455 q->pktcnt_idx = new_idx;
1456 }
1457
1458 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301459 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001460 return 0;
1461}
1462
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001463static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001464{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001465 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001466 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001467 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001468
Patrick McHardyf6469682013-04-19 02:04:27 +00001469 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001470 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001471
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301472 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001473 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001474 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001475 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001476 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001477 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001478}
1479
Bill Pemberton91744942012-12-03 09:23:02 -05001480static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001481{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001482 if (IS_ERR_OR_NULL(adap->debugfs_root))
1483 return -1;
1484
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301485#ifdef CONFIG_DEBUG_FS
1486 t4_setup_debugfs(adap);
1487#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001488 return 0;
1489}
1490
1491/*
1492 * upper-layer driver support
1493 */
1494
1495/*
1496 * Allocate an active-open TID and set it to the supplied value.
1497 */
1498int cxgb4_alloc_atid(struct tid_info *t, void *data)
1499{
1500 int atid = -1;
1501
1502 spin_lock_bh(&t->atid_lock);
1503 if (t->afree) {
1504 union aopen_entry *p = t->afree;
1505
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001506 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001507 t->afree = p->next;
1508 p->data = data;
1509 t->atids_in_use++;
1510 }
1511 spin_unlock_bh(&t->atid_lock);
1512 return atid;
1513}
1514EXPORT_SYMBOL(cxgb4_alloc_atid);
1515
1516/*
1517 * Release an active-open TID.
1518 */
1519void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1520{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001521 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001522
1523 spin_lock_bh(&t->atid_lock);
1524 p->next = t->afree;
1525 t->afree = p;
1526 t->atids_in_use--;
1527 spin_unlock_bh(&t->atid_lock);
1528}
1529EXPORT_SYMBOL(cxgb4_free_atid);
1530
1531/*
1532 * Allocate a server TID and set it to the supplied value.
1533 */
1534int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1535{
1536 int stid;
1537
1538 spin_lock_bh(&t->stid_lock);
1539 if (family == PF_INET) {
1540 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1541 if (stid < t->nstids)
1542 __set_bit(stid, t->stid_bmap);
1543 else
1544 stid = -1;
1545 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301546 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001547 if (stid < 0)
1548 stid = -1;
1549 }
1550 if (stid >= 0) {
1551 t->stid_tab[stid].data = data;
1552 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301553 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1554 * This is equivalent to 4 TIDs. With CLIP enabled it
1555 * needs 2 TIDs.
1556 */
1557 if (family == PF_INET)
1558 t->stids_in_use++;
1559 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301560 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001561 }
1562 spin_unlock_bh(&t->stid_lock);
1563 return stid;
1564}
1565EXPORT_SYMBOL(cxgb4_alloc_stid);
1566
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001567/* Allocate a server filter TID and set it to the supplied value.
1568 */
1569int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1570{
1571 int stid;
1572
1573 spin_lock_bh(&t->stid_lock);
1574 if (family == PF_INET) {
1575 stid = find_next_zero_bit(t->stid_bmap,
1576 t->nstids + t->nsftids, t->nstids);
1577 if (stid < (t->nstids + t->nsftids))
1578 __set_bit(stid, t->stid_bmap);
1579 else
1580 stid = -1;
1581 } else {
1582 stid = -1;
1583 }
1584 if (stid >= 0) {
1585 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301586 stid -= t->nstids;
1587 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301588 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001589 }
1590 spin_unlock_bh(&t->stid_lock);
1591 return stid;
1592}
1593EXPORT_SYMBOL(cxgb4_alloc_sftid);
1594
1595/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001596 */
1597void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1598{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301599 /* Is it a server filter TID? */
1600 if (t->nsftids && (stid >= t->sftid_base)) {
1601 stid -= t->sftid_base;
1602 stid += t->nstids;
1603 } else {
1604 stid -= t->stid_base;
1605 }
1606
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001607 spin_lock_bh(&t->stid_lock);
1608 if (family == PF_INET)
1609 __clear_bit(stid, t->stid_bmap);
1610 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301611 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001612 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301613 if (stid < t->nstids) {
1614 if (family == PF_INET)
1615 t->stids_in_use--;
1616 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301617 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301618 } else {
1619 t->sftids_in_use--;
1620 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001621 spin_unlock_bh(&t->stid_lock);
1622}
1623EXPORT_SYMBOL(cxgb4_free_stid);
1624
1625/*
1626 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1627 */
1628static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1629 unsigned int tid)
1630{
1631 struct cpl_tid_release *req;
1632
1633 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1634 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1635 INIT_TP_WR(req, tid);
1636 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1637}
1638
1639/*
1640 * Queue a TID release request and if necessary schedule a work queue to
1641 * process it.
1642 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001643static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1644 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001645{
1646 void **p = &t->tid_tab[tid];
1647 struct adapter *adap = container_of(t, struct adapter, tids);
1648
1649 spin_lock_bh(&adap->tid_release_lock);
1650 *p = adap->tid_release_head;
1651 /* Low 2 bits encode the Tx channel number */
1652 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1653 if (!adap->tid_release_task_busy) {
1654 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001655 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001656 }
1657 spin_unlock_bh(&adap->tid_release_lock);
1658}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001659
1660/*
1661 * Process the list of pending TID release requests.
1662 */
1663static void process_tid_release_list(struct work_struct *work)
1664{
1665 struct sk_buff *skb;
1666 struct adapter *adap;
1667
1668 adap = container_of(work, struct adapter, tid_release_task);
1669
1670 spin_lock_bh(&adap->tid_release_lock);
1671 while (adap->tid_release_head) {
1672 void **p = adap->tid_release_head;
1673 unsigned int chan = (uintptr_t)p & 3;
1674 p = (void *)p - chan;
1675
1676 adap->tid_release_head = *p;
1677 *p = NULL;
1678 spin_unlock_bh(&adap->tid_release_lock);
1679
1680 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1681 GFP_KERNEL)))
1682 schedule_timeout_uninterruptible(1);
1683
1684 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1685 t4_ofld_send(adap, skb);
1686 spin_lock_bh(&adap->tid_release_lock);
1687 }
1688 adap->tid_release_task_busy = false;
1689 spin_unlock_bh(&adap->tid_release_lock);
1690}
1691
1692/*
1693 * Release a TID and inform HW. If we are unable to allocate the release
1694 * message we defer to a work queue.
1695 */
1696void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1697{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001698 struct sk_buff *skb;
1699 struct adapter *adap = container_of(t, struct adapter, tids);
1700
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301701 WARN_ON(tid >= t->ntids);
1702
1703 if (t->tid_tab[tid]) {
1704 t->tid_tab[tid] = NULL;
1705 if (t->hash_base && (tid >= t->hash_base))
1706 atomic_dec(&t->hash_tids_in_use);
1707 else
1708 atomic_dec(&t->tids_in_use);
1709 }
1710
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001711 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1712 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001713 mk_tid_release(skb, chan, tid);
1714 t4_ofld_send(adap, skb);
1715 } else
1716 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001717}
1718EXPORT_SYMBOL(cxgb4_remove_tid);
1719
1720/*
1721 * Allocate and initialize the TID tables. Returns 0 on success.
1722 */
1723static int tid_init(struct tid_info *t)
1724{
1725 size_t size;
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001726 unsigned int stid_bmap_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001727 unsigned int natids = t->natids;
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301728 struct adapter *adap = container_of(t, struct adapter, tids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001729
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001730 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001731 size = t->ntids * sizeof(*t->tid_tab) +
1732 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001733 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001734 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001735 stid_bmap_size * sizeof(long) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001736 t->nftids * sizeof(*t->ftid_tab) +
1737 t->nsftids * sizeof(*t->ftid_tab);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001738
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001739 t->tid_tab = t4_alloc_mem(size);
1740 if (!t->tid_tab)
1741 return -ENOMEM;
1742
1743 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1744 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001745 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001746 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001747 spin_lock_init(&t->stid_lock);
1748 spin_lock_init(&t->atid_lock);
1749
1750 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301751 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001752 t->afree = NULL;
1753 t->atids_in_use = 0;
1754 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301755 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001756
1757 /* Setup the free list for atid_tab and clear the stid bitmap. */
1758 if (natids) {
1759 while (--natids)
1760 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1761 t->afree = t->atid_tab;
1762 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001763 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301764 /* Reserve stid 0 for T4/T5 adapters */
1765 if (!t->stid_base &&
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301766 (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5))
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301767 __set_bit(0, t->stid_bmap);
1768
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001769 return 0;
1770}
1771
1772/**
1773 * cxgb4_create_server - create an IP server
1774 * @dev: the device
1775 * @stid: the server TID
1776 * @sip: local IP address to bind server to
1777 * @sport: the server's TCP port
1778 * @queue: queue to direct messages from this server to
1779 *
1780 * Create an IP server for the given port and address.
1781 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1782 */
1783int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001784 __be32 sip, __be16 sport, __be16 vlan,
1785 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001786{
1787 unsigned int chan;
1788 struct sk_buff *skb;
1789 struct adapter *adap;
1790 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301791 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001792
1793 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1794 if (!skb)
1795 return -ENOMEM;
1796
1797 adap = netdev2adap(dev);
1798 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1799 INIT_TP_WR(req, 0);
1800 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1801 req->local_port = sport;
1802 req->peer_port = htons(0);
1803 req->local_ip = sip;
1804 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001805 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001806 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001807 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1808 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301809 ret = t4_mgmt_tx(adap, skb);
1810 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001811}
1812EXPORT_SYMBOL(cxgb4_create_server);
1813
Vipul Pandya80f40c12013-07-04 16:10:45 +05301814/* cxgb4_create_server6 - create an IPv6 server
1815 * @dev: the device
1816 * @stid: the server TID
1817 * @sip: local IPv6 address to bind server to
1818 * @sport: the server's TCP port
1819 * @queue: queue to direct messages from this server to
1820 *
1821 * Create an IPv6 server for the given port and address.
1822 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1823 */
1824int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1825 const struct in6_addr *sip, __be16 sport,
1826 unsigned int queue)
1827{
1828 unsigned int chan;
1829 struct sk_buff *skb;
1830 struct adapter *adap;
1831 struct cpl_pass_open_req6 *req;
1832 int ret;
1833
1834 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1835 if (!skb)
1836 return -ENOMEM;
1837
1838 adap = netdev2adap(dev);
1839 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1840 INIT_TP_WR(req, 0);
1841 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1842 req->local_port = sport;
1843 req->peer_port = htons(0);
1844 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1845 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1846 req->peer_ip_hi = cpu_to_be64(0);
1847 req->peer_ip_lo = cpu_to_be64(0);
1848 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001849 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001850 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1851 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301852 ret = t4_mgmt_tx(adap, skb);
1853 return net_xmit_eval(ret);
1854}
1855EXPORT_SYMBOL(cxgb4_create_server6);
1856
1857int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1858 unsigned int queue, bool ipv6)
1859{
1860 struct sk_buff *skb;
1861 struct adapter *adap;
1862 struct cpl_close_listsvr_req *req;
1863 int ret;
1864
1865 adap = netdev2adap(dev);
1866
1867 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1868 if (!skb)
1869 return -ENOMEM;
1870
1871 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1872 INIT_TP_WR(req, 0);
1873 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001874 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1875 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301876 ret = t4_mgmt_tx(adap, skb);
1877 return net_xmit_eval(ret);
1878}
1879EXPORT_SYMBOL(cxgb4_remove_server);
1880
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001881/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001882 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1883 * @mtus: the HW MTU table
1884 * @mtu: the target MTU
1885 * @idx: index of selected entry in the MTU table
1886 *
1887 * Returns the index and the value in the HW MTU table that is closest to
1888 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1889 * table, in which case that smallest available value is selected.
1890 */
1891unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1892 unsigned int *idx)
1893{
1894 unsigned int i = 0;
1895
1896 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1897 ++i;
1898 if (idx)
1899 *idx = i;
1900 return mtus[i];
1901}
1902EXPORT_SYMBOL(cxgb4_best_mtu);
1903
1904/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301905 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1906 * @mtus: the HW MTU table
1907 * @header_size: Header Size
1908 * @data_size_max: maximum Data Segment Size
1909 * @data_size_align: desired Data Segment Size Alignment (2^N)
1910 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1911 *
1912 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1913 * MTU Table based solely on a Maximum MTU parameter, we break that
1914 * parameter up into a Header Size and Maximum Data Segment Size, and
1915 * provide a desired Data Segment Size Alignment. If we find an MTU in
1916 * the Hardware MTU Table which will result in a Data Segment Size with
1917 * the requested alignment _and_ that MTU isn't "too far" from the
1918 * closest MTU, then we'll return that rather than the closest MTU.
1919 */
1920unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1921 unsigned short header_size,
1922 unsigned short data_size_max,
1923 unsigned short data_size_align,
1924 unsigned int *mtu_idxp)
1925{
1926 unsigned short max_mtu = header_size + data_size_max;
1927 unsigned short data_size_align_mask = data_size_align - 1;
1928 int mtu_idx, aligned_mtu_idx;
1929
1930 /* Scan the MTU Table till we find an MTU which is larger than our
1931 * Maximum MTU or we reach the end of the table. Along the way,
1932 * record the last MTU found, if any, which will result in a Data
1933 * Segment Length matching the requested alignment.
1934 */
1935 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1936 unsigned short data_size = mtus[mtu_idx] - header_size;
1937
1938 /* If this MTU minus the Header Size would result in a
1939 * Data Segment Size of the desired alignment, remember it.
1940 */
1941 if ((data_size & data_size_align_mask) == 0)
1942 aligned_mtu_idx = mtu_idx;
1943
1944 /* If we're not at the end of the Hardware MTU Table and the
1945 * next element is larger than our Maximum MTU, drop out of
1946 * the loop.
1947 */
1948 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1949 break;
1950 }
1951
1952 /* If we fell out of the loop because we ran to the end of the table,
1953 * then we just have to use the last [largest] entry.
1954 */
1955 if (mtu_idx == NMTUS)
1956 mtu_idx--;
1957
1958 /* If we found an MTU which resulted in the requested Data Segment
1959 * Length alignment and that's "not far" from the largest MTU which is
1960 * less than or equal to the maximum MTU, then use that.
1961 */
1962 if (aligned_mtu_idx >= 0 &&
1963 mtu_idx - aligned_mtu_idx <= 1)
1964 mtu_idx = aligned_mtu_idx;
1965
1966 /* If the caller has passed in an MTU Index pointer, pass the
1967 * MTU Index back. Return the MTU value.
1968 */
1969 if (mtu_idxp)
1970 *mtu_idxp = mtu_idx;
1971 return mtus[mtu_idx];
1972}
1973EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1974
1975/**
Hariprasad S27999802015-09-23 17:19:26 +05301976 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1977 * @chip: chip type
1978 * @viid: VI id of the given port
1979 *
1980 * Return the SMT index for this VI.
1981 */
1982unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1983{
1984 /* In T4/T5, SMT contains 256 SMAC entries organized in
1985 * 128 rows of 2 entries each.
1986 * In T6, SMT contains 256 SMAC entries in 256 rows.
1987 * TODO: The below code needs to be updated when we add support
1988 * for 256 VFs.
1989 */
1990 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1991 return ((viid & 0x7f) << 1);
1992 else
1993 return (viid & 0x7f);
1994}
1995EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1996
1997/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001998 * cxgb4_port_chan - get the HW channel of a port
1999 * @dev: the net device for the port
2000 *
2001 * Return the HW Tx channel of the given port.
2002 */
2003unsigned int cxgb4_port_chan(const struct net_device *dev)
2004{
2005 return netdev2pinfo(dev)->tx_chan;
2006}
2007EXPORT_SYMBOL(cxgb4_port_chan);
2008
Vipul Pandya881806b2012-05-18 15:29:24 +05302009unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
2010{
2011 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002012 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05302013
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302014 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2015 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302016 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302017 lp_count = LP_COUNT_G(v1);
2018 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002019 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302020 lp_count = LP_COUNT_T5_G(v1);
2021 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002022 }
2023 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05302024}
2025EXPORT_SYMBOL(cxgb4_dbfifo_count);
2026
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002027/**
2028 * cxgb4_port_viid - get the VI id of a port
2029 * @dev: the net device for the port
2030 *
2031 * Return the VI id of the given port.
2032 */
2033unsigned int cxgb4_port_viid(const struct net_device *dev)
2034{
2035 return netdev2pinfo(dev)->viid;
2036}
2037EXPORT_SYMBOL(cxgb4_port_viid);
2038
2039/**
2040 * cxgb4_port_idx - get the index of a port
2041 * @dev: the net device for the port
2042 *
2043 * Return the index of the given port.
2044 */
2045unsigned int cxgb4_port_idx(const struct net_device *dev)
2046{
2047 return netdev2pinfo(dev)->port_id;
2048}
2049EXPORT_SYMBOL(cxgb4_port_idx);
2050
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002051void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
2052 struct tp_tcp_stats *v6)
2053{
2054 struct adapter *adap = pci_get_drvdata(pdev);
2055
2056 spin_lock(&adap->stats_lock);
2057 t4_tp_get_tcp_stats(adap, v4, v6);
2058 spin_unlock(&adap->stats_lock);
2059}
2060EXPORT_SYMBOL(cxgb4_get_tcp_stats);
2061
2062void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
2063 const unsigned int *pgsz_order)
2064{
2065 struct adapter *adap = netdev2adap(dev);
2066
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302067 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
2068 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
2069 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
2070 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002071}
2072EXPORT_SYMBOL(cxgb4_iscsi_init);
2073
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302074int cxgb4_flush_eq_cache(struct net_device *dev)
2075{
2076 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302077
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05302078 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302079}
2080EXPORT_SYMBOL(cxgb4_flush_eq_cache);
2081
2082static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
2083{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302084 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302085 __be64 indices;
2086 int ret;
2087
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05302088 spin_lock(&adap->win0_lock);
2089 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
2090 sizeof(indices), (__be32 *)&indices,
2091 T4_MEMORY_READ);
2092 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302093 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00002094 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
2095 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302096 }
2097 return ret;
2098}
2099
2100int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
2101 u16 size)
2102{
2103 struct adapter *adap = netdev2adap(dev);
2104 u16 hw_pidx, hw_cidx;
2105 int ret;
2106
2107 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
2108 if (ret)
2109 goto out;
2110
2111 if (pidx != hw_pidx) {
2112 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302113 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302114
2115 if (pidx >= hw_pidx)
2116 delta = pidx - hw_pidx;
2117 else
2118 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302119
2120 if (is_t4(adap->params.chip))
2121 val = PIDX_V(delta);
2122 else
2123 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302124 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302125 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2126 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302127 }
2128out:
2129 return ret;
2130}
2131EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
2132
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302133int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
2134{
2135 struct adapter *adap;
2136 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302137 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302138 u32 edc0_end, edc1_end, mc0_end, mc1_end;
2139 int ret;
2140
2141 adap = netdev2adap(dev);
2142
2143 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
2144
2145 /* Figure out where the offset lands in the Memory Type/Address scheme.
2146 * This code assumes that the memory is laid out starting at offset 0
2147 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
2148 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
2149 * MC0, and some have both MC0 and MC1.
2150 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302151 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
2152 edc0_size = EDRAM0_SIZE_G(size) << 20;
2153 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
2154 edc1_size = EDRAM1_SIZE_G(size) << 20;
2155 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
2156 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302157
2158 edc0_end = edc0_size;
2159 edc1_end = edc0_end + edc1_size;
2160 mc0_end = edc1_end + mc0_size;
2161
2162 if (offset < edc0_end) {
2163 memtype = MEM_EDC0;
2164 memaddr = offset;
2165 } else if (offset < edc1_end) {
2166 memtype = MEM_EDC1;
2167 memaddr = offset - edc0_end;
2168 } else {
2169 if (offset < mc0_end) {
2170 memtype = MEM_MC0;
2171 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302172 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302173 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2174 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302175 mc1_end = mc0_end + mc1_size;
2176 if (offset < mc1_end) {
2177 memtype = MEM_MC1;
2178 memaddr = offset - mc0_end;
2179 } else {
2180 /* offset beyond the end of any memory */
2181 goto err;
2182 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302183 } else {
2184 /* T4/T6 only has a single memory channel */
2185 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302186 }
2187 }
2188
2189 spin_lock(&adap->win0_lock);
2190 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
2191 spin_unlock(&adap->win0_lock);
2192 return ret;
2193
2194err:
2195 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
2196 stag, offset);
2197 return -EINVAL;
2198}
2199EXPORT_SYMBOL(cxgb4_read_tpte);
2200
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302201u64 cxgb4_read_sge_timestamp(struct net_device *dev)
2202{
2203 u32 hi, lo;
2204 struct adapter *adap;
2205
2206 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302207 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
2208 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302209
2210 return ((u64)hi << 32) | (u64)lo;
2211}
2212EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
2213
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302214int cxgb4_bar2_sge_qregs(struct net_device *dev,
2215 unsigned int qid,
2216 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05302217 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302218 u64 *pbar2_qoffset,
2219 unsigned int *pbar2_qid)
2220{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302221 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302222 qid,
2223 (qtype == CXGB4_BAR2_QTYPE_EGRESS
2224 ? T4_BAR2_QTYPE_EGRESS
2225 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05302226 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302227 pbar2_qoffset,
2228 pbar2_qid);
2229}
2230EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
2231
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002232static struct pci_driver cxgb4_driver;
2233
2234static void check_neigh_update(struct neighbour *neigh)
2235{
2236 const struct device *parent;
2237 const struct net_device *netdev = neigh->dev;
2238
2239 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2240 netdev = vlan_dev_real_dev(netdev);
2241 parent = netdev->dev.parent;
2242 if (parent && parent->driver == &cxgb4_driver.driver)
2243 t4_l2t_update(dev_get_drvdata(parent), neigh);
2244}
2245
2246static int netevent_cb(struct notifier_block *nb, unsigned long event,
2247 void *data)
2248{
2249 switch (event) {
2250 case NETEVENT_NEIGH_UPDATE:
2251 check_neigh_update(data);
2252 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002253 case NETEVENT_REDIRECT:
2254 default:
2255 break;
2256 }
2257 return 0;
2258}
2259
2260static bool netevent_registered;
2261static struct notifier_block cxgb4_netevent_nb = {
2262 .notifier_call = netevent_cb
2263};
2264
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302265static void drain_db_fifo(struct adapter *adap, int usecs)
2266{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002267 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302268
2269 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302270 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2271 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302272 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302273 lp_count = LP_COUNT_G(v1);
2274 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002275 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302276 lp_count = LP_COUNT_T5_G(v1);
2277 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002278 }
2279
2280 if (lp_count == 0 && hp_count == 0)
2281 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302282 set_current_state(TASK_UNINTERRUPTIBLE);
2283 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302284 } while (1);
2285}
2286
2287static void disable_txq_db(struct sge_txq *q)
2288{
Steve Wise05eb2382014-03-14 21:52:08 +05302289 unsigned long flags;
2290
2291 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302292 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05302293 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302294}
2295
Steve Wise05eb2382014-03-14 21:52:08 +05302296static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302297{
2298 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05302299 if (q->db_pidx_inc) {
2300 /* Make sure that all writes to the TX descriptors
2301 * are committed before we tell HW about them.
2302 */
2303 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302304 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2305 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05302306 q->db_pidx_inc = 0;
2307 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302308 q->db_disabled = 0;
2309 spin_unlock_irq(&q->db_lock);
2310}
2311
2312static void disable_dbs(struct adapter *adap)
2313{
2314 int i;
2315
2316 for_each_ethrxq(&adap->sge, i)
2317 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302318 for_each_iscsirxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302319 disable_txq_db(&adap->sge.ofldtxq[i].q);
2320 for_each_port(adap, i)
2321 disable_txq_db(&adap->sge.ctrlq[i].q);
2322}
2323
2324static void enable_dbs(struct adapter *adap)
2325{
2326 int i;
2327
2328 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302329 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302330 for_each_iscsirxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302331 enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302332 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302333 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
2334}
2335
2336static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
2337{
2338 if (adap->uld_handle[CXGB4_ULD_RDMA])
2339 ulds[CXGB4_ULD_RDMA].control(adap->uld_handle[CXGB4_ULD_RDMA],
2340 cmd);
2341}
2342
2343static void process_db_full(struct work_struct *work)
2344{
2345 struct adapter *adap;
2346
2347 adap = container_of(work, struct adapter, db_full_task);
2348
2349 drain_db_fifo(adap, dbfifo_drain_delay);
2350 enable_dbs(adap);
2351 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302352 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2353 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2354 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
2355 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
2356 else
2357 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2358 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302359}
2360
2361static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
2362{
2363 u16 hw_pidx, hw_cidx;
2364 int ret;
2365
Steve Wise05eb2382014-03-14 21:52:08 +05302366 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302367 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2368 if (ret)
2369 goto out;
2370 if (q->db_pidx != hw_pidx) {
2371 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302372 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302373
2374 if (q->db_pidx >= hw_pidx)
2375 delta = q->db_pidx - hw_pidx;
2376 else
2377 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302378
2379 if (is_t4(adap->params.chip))
2380 val = PIDX_V(delta);
2381 else
2382 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302383 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302384 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2385 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302386 }
2387out:
2388 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302389 q->db_pidx_inc = 0;
2390 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302391 if (ret)
2392 CH_WARN(adap, "DB drop recovery failed.\n");
2393}
2394static void recover_all_queues(struct adapter *adap)
2395{
2396 int i;
2397
2398 for_each_ethrxq(&adap->sge, i)
2399 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302400 for_each_iscsirxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302401 sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
2402 for_each_port(adap, i)
2403 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2404}
2405
Vipul Pandya881806b2012-05-18 15:29:24 +05302406static void process_db_drop(struct work_struct *work)
2407{
2408 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302409
Vipul Pandya881806b2012-05-18 15:29:24 +05302410 adap = container_of(work, struct adapter, db_drop_task);
2411
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302412 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302413 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002414 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302415 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002416 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302417 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002418 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302419 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302420 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002421 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2422 u16 qid = (dropped_db >> 15) & 0x1ffff;
2423 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302424 u64 bar2_qoffset;
2425 unsigned int bar2_qid;
2426 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002427
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302428 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002429 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302430 if (ret)
2431 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2432 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2433 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302434 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302435 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002436
2437 /* Re-enable BAR2 WC */
2438 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2439 }
2440
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302441 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2442 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302443}
2444
2445void t4_db_full(struct adapter *adap)
2446{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302447 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302448 disable_dbs(adap);
2449 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302450 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2451 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002452 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002453 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302454}
2455
2456void t4_db_dropped(struct adapter *adap)
2457{
Steve Wise05eb2382014-03-14 21:52:08 +05302458 if (is_t4(adap->params.chip)) {
2459 disable_dbs(adap);
2460 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2461 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002462 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302463}
2464
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002465static void uld_attach(struct adapter *adap, unsigned int uld)
2466{
2467 void *handle;
2468 struct cxgb4_lld_info lli;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002469 unsigned short i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002470
2471 lli.pdev = adap->pdev;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302472 lli.pf = adap->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002473 lli.l2t = adap->l2t;
2474 lli.tids = &adap->tids;
2475 lli.ports = adap->port;
2476 lli.vr = &adap->vres;
2477 lli.mtus = adap->params.mtus;
2478 if (uld == CXGB4_ULD_RDMA) {
2479 lli.rxq_ids = adap->sge.rdma_rxq;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05302480 lli.ciq_ids = adap->sge.rdma_ciq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002481 lli.nrxq = adap->sge.rdmaqs;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05302482 lli.nciq = adap->sge.rdmaciqs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002483 } else if (uld == CXGB4_ULD_ISCSI) {
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302484 lli.rxq_ids = adap->sge.iscsi_rxq;
2485 lli.nrxq = adap->sge.iscsiqsets;
Varun Prakashf2692d12016-02-14 23:02:40 +05302486 } else if (uld == CXGB4_ULD_ISCSIT) {
2487 lli.rxq_ids = adap->sge.iscsit_rxq;
2488 lli.nrxq = adap->sge.niscsitq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002489 }
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302490 lli.ntxq = adap->sge.iscsiqsets;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002491 lli.nchan = adap->params.nports;
2492 lli.nports = adap->params.nports;
2493 lli.wr_cred = adap->params.ofldq_wr_cred;
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302494 lli.adapter_type = adap->params.chip;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302495 lli.iscsi_iolen = MAXRXDATA_G(t4_read_reg(adap, TP_PARA_REG2_A));
Varun Prakash7714cb9e2016-02-14 23:07:39 +05302496 lli.iscsi_tagmask = t4_read_reg(adap, ULP_RX_ISCSI_TAGMASK_A);
2497 lli.iscsi_pgsz_order = t4_read_reg(adap, ULP_RX_ISCSI_PSZ_A);
2498 lli.iscsi_llimit = t4_read_reg(adap, ULP_RX_ISCSI_LLIMIT_A);
2499 lli.iscsi_ppm = &adap->iscsi_ppm;
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302500 lli.cclk_ps = 1000000000 / adap->params.vpd.cclk;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302501 lli.udb_density = 1 << adap->params.sge.eq_qpp;
2502 lli.ucq_density = 1 << adap->params.sge.iq_qpp;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05302503 lli.filt_mode = adap->params.tp.vlan_pri_map;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002504 /* MODQ_REQ_MAP sets queues 0-3 to chan 0-3 */
2505 for (i = 0; i < NCHAN; i++)
2506 lli.tx_modq[i] = i;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302507 lli.gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS_A);
2508 lli.db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL_A);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002509 lli.fw_vers = adap->params.fw_vers;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302510 lli.dbfifo_int_thresh = dbfifo_int_thresh;
Hariprasad Shenai04e10e22014-07-14 21:34:51 +05302511 lli.sge_ingpadboundary = adap->sge.fl_align;
2512 lli.sge_egrstatuspagesize = adap->sge.stat_len;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002513 lli.sge_pktshift = adap->sge.pktshift;
2514 lli.enable_fw_ofld_conn = adap->flags & FW_OFLD_CONN;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05302515 lli.max_ordird_qp = adap->params.max_ordird_qp;
2516 lli.max_ird_adapter = adap->params.max_ird_adapter;
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05302517 lli.ulptx_memwrite_dsgl = adap->params.ulptx_memwrite_dsgl;
Hariprasad Shenai982b81e2015-05-05 14:59:54 +05302518 lli.nodeid = dev_to_node(adap->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002519
2520 handle = ulds[uld].add(&lli);
2521 if (IS_ERR(handle)) {
2522 dev_warn(adap->pdev_dev,
2523 "could not attach to the %s driver, error %ld\n",
2524 uld_str[uld], PTR_ERR(handle));
2525 return;
2526 }
2527
2528 adap->uld_handle[uld] = handle;
2529
2530 if (!netevent_registered) {
2531 register_netevent_notifier(&cxgb4_netevent_nb);
2532 netevent_registered = true;
2533 }
Dimitris Michailidise29f5db2010-05-18 10:07:13 +00002534
2535 if (adap->flags & FULL_INIT_DONE)
2536 ulds[uld].state_change(handle, CXGB4_STATE_UP);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002537}
2538
2539static void attach_ulds(struct adapter *adap)
2540{
2541 unsigned int i;
2542
Vipul Pandya01bcca62013-07-04 16:10:46 +05302543 spin_lock(&adap_rcu_lock);
2544 list_add_tail_rcu(&adap->rcu_node, &adap_rcu_list);
2545 spin_unlock(&adap_rcu_lock);
2546
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002547 mutex_lock(&uld_mutex);
2548 list_add_tail(&adap->list_node, &adapter_list);
2549 for (i = 0; i < CXGB4_ULD_MAX; i++)
2550 if (ulds[i].add)
2551 uld_attach(adap, i);
2552 mutex_unlock(&uld_mutex);
2553}
2554
2555static void detach_ulds(struct adapter *adap)
2556{
2557 unsigned int i;
2558
2559 mutex_lock(&uld_mutex);
2560 list_del(&adap->list_node);
2561 for (i = 0; i < CXGB4_ULD_MAX; i++)
2562 if (adap->uld_handle[i]) {
2563 ulds[i].state_change(adap->uld_handle[i],
2564 CXGB4_STATE_DETACH);
2565 adap->uld_handle[i] = NULL;
2566 }
2567 if (netevent_registered && list_empty(&adapter_list)) {
2568 unregister_netevent_notifier(&cxgb4_netevent_nb);
2569 netevent_registered = false;
2570 }
2571 mutex_unlock(&uld_mutex);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302572
2573 spin_lock(&adap_rcu_lock);
2574 list_del_rcu(&adap->rcu_node);
2575 spin_unlock(&adap_rcu_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002576}
2577
2578static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2579{
2580 unsigned int i;
2581
2582 mutex_lock(&uld_mutex);
2583 for (i = 0; i < CXGB4_ULD_MAX; i++)
2584 if (adap->uld_handle[i])
2585 ulds[i].state_change(adap->uld_handle[i], new_state);
2586 mutex_unlock(&uld_mutex);
2587}
2588
2589/**
2590 * cxgb4_register_uld - register an upper-layer driver
2591 * @type: the ULD type
2592 * @p: the ULD methods
2593 *
2594 * Registers an upper-layer driver with this driver and notifies the ULD
2595 * about any presently available devices that support its type. Returns
2596 * %-EBUSY if a ULD of the same type is already registered.
2597 */
2598int cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p)
2599{
2600 int ret = 0;
2601 struct adapter *adap;
2602
2603 if (type >= CXGB4_ULD_MAX)
2604 return -EINVAL;
2605 mutex_lock(&uld_mutex);
2606 if (ulds[type].add) {
2607 ret = -EBUSY;
2608 goto out;
2609 }
2610 ulds[type] = *p;
2611 list_for_each_entry(adap, &adapter_list, list_node)
2612 uld_attach(adap, type);
2613out: mutex_unlock(&uld_mutex);
2614 return ret;
2615}
2616EXPORT_SYMBOL(cxgb4_register_uld);
2617
2618/**
2619 * cxgb4_unregister_uld - unregister an upper-layer driver
2620 * @type: the ULD type
2621 *
2622 * Unregisters an existing upper-layer driver.
2623 */
2624int cxgb4_unregister_uld(enum cxgb4_uld type)
2625{
2626 struct adapter *adap;
2627
2628 if (type >= CXGB4_ULD_MAX)
2629 return -EINVAL;
2630 mutex_lock(&uld_mutex);
2631 list_for_each_entry(adap, &adapter_list, list_node)
2632 adap->uld_handle[type] = NULL;
2633 ulds[type].add = NULL;
2634 mutex_unlock(&uld_mutex);
2635 return 0;
2636}
2637EXPORT_SYMBOL(cxgb4_unregister_uld);
2638
Anish Bhatt1bb60372014-10-14 20:07:22 -07002639#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002640static int cxgb4_inet6addr_handler(struct notifier_block *this,
2641 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302642{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002643 struct inet6_ifaddr *ifa = data;
2644 struct net_device *event_dev = ifa->idev->dev;
2645 const struct device *parent = NULL;
2646#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302647 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002648#endif
2649 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2650 event_dev = vlan_dev_real_dev(event_dev);
2651#if IS_ENABLED(CONFIG_BONDING)
2652 if (event_dev->flags & IFF_MASTER) {
2653 list_for_each_entry(adap, &adapter_list, list_node) {
2654 switch (event) {
2655 case NETDEV_UP:
2656 cxgb4_clip_get(adap->port[0],
2657 (const u32 *)ifa, 1);
2658 break;
2659 case NETDEV_DOWN:
2660 cxgb4_clip_release(adap->port[0],
2661 (const u32 *)ifa, 1);
2662 break;
2663 default:
2664 break;
2665 }
2666 }
2667 return NOTIFY_OK;
2668 }
2669#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302670
Anish Bhattb5a02f52015-01-14 15:17:34 -08002671 if (event_dev)
2672 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302673
Anish Bhattb5a02f52015-01-14 15:17:34 -08002674 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302675 switch (event) {
2676 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002677 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302678 break;
2679 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002680 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302681 break;
2682 default:
2683 break;
2684 }
2685 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002686 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302687}
2688
Anish Bhattb5a02f52015-01-14 15:17:34 -08002689static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302690static struct notifier_block cxgb4_inet6addr_notifier = {
2691 .notifier_call = cxgb4_inet6addr_handler
2692};
2693
Vipul Pandya01bcca62013-07-04 16:10:46 +05302694static void update_clip(const struct adapter *adap)
2695{
2696 int i;
2697 struct net_device *dev;
2698 int ret;
2699
2700 rcu_read_lock();
2701
2702 for (i = 0; i < MAX_NPORTS; i++) {
2703 dev = adap->port[i];
2704 ret = 0;
2705
2706 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002707 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302708
2709 if (ret < 0)
2710 break;
2711 }
2712 rcu_read_unlock();
2713}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002714#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302715
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002716/**
2717 * cxgb_up - enable the adapter
2718 * @adap: adapter being enabled
2719 *
2720 * Called when the first port is enabled, this function performs the
2721 * actions necessary to make an adapter operational, such as completing
2722 * the initialization of HW modules, and enabling interrupts.
2723 *
2724 * Must be called with the rtnl lock held.
2725 */
2726static int cxgb_up(struct adapter *adap)
2727{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002728 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002729
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002730 err = setup_sge_queues(adap);
2731 if (err)
2732 goto out;
2733 err = setup_rss(adap);
2734 if (err)
2735 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002736
2737 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002738 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002739 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2740 adap->msix_info[0].desc, adap);
2741 if (err)
2742 goto irq_err;
2743
2744 err = request_msix_queue_irqs(adap);
2745 if (err) {
2746 free_irq(adap->msix_info[0].vec, adap);
2747 goto irq_err;
2748 }
2749 } else {
2750 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2751 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002752 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002753 if (err)
2754 goto irq_err;
2755 }
2756 enable_rx(adap);
2757 t4_sge_start(adap);
2758 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002759 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002760 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002761#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302762 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002763#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302764 /* Initialize hash mac addr list*/
2765 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002766 out:
2767 return err;
2768 irq_err:
2769 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002770 freeq:
2771 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002772 goto out;
2773}
2774
2775static void cxgb_down(struct adapter *adapter)
2776{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002777 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302778 cancel_work_sync(&adapter->db_full_task);
2779 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002780 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002781 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002782
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002783 t4_sge_stop(adapter);
2784 t4_free_sge_resources(adapter);
2785 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002786}
2787
2788/*
2789 * net_device operations
2790 */
2791static int cxgb_open(struct net_device *dev)
2792{
2793 int err;
2794 struct port_info *pi = netdev_priv(dev);
2795 struct adapter *adapter = pi->adapter;
2796
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002797 netif_carrier_off(dev);
2798
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002799 if (!(adapter->flags & FULL_INIT_DONE)) {
2800 err = cxgb_up(adapter);
2801 if (err < 0)
2802 return err;
2803 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002804
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002805 err = link_start(dev);
2806 if (!err)
2807 netif_tx_start_all_queues(dev);
2808 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002809}
2810
2811static int cxgb_close(struct net_device *dev)
2812{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002813 struct port_info *pi = netdev_priv(dev);
2814 struct adapter *adapter = pi->adapter;
2815
2816 netif_tx_stop_all_queues(dev);
2817 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302818 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002819}
2820
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002821/* Return an error number if the indicated filter isn't writable ...
2822 */
2823static int writable_filter(struct filter_entry *f)
2824{
2825 if (f->locked)
2826 return -EPERM;
2827 if (f->pending)
2828 return -EBUSY;
2829
2830 return 0;
2831}
2832
2833/* Delete the filter at the specified index (if valid). The checks for all
2834 * the common problems with doing this like the filter being locked, currently
2835 * pending in another operation, etc.
2836 */
2837static int delete_filter(struct adapter *adapter, unsigned int fidx)
2838{
2839 struct filter_entry *f;
2840 int ret;
2841
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002842 if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002843 return -EINVAL;
2844
2845 f = &adapter->tids.ftid_tab[fidx];
2846 ret = writable_filter(f);
2847 if (ret)
2848 return ret;
2849 if (f->valid)
2850 return del_filter_wr(adapter, fidx);
2851
2852 return 0;
2853}
2854
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002855int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002856 __be32 sip, __be16 sport, __be16 vlan,
2857 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002858{
2859 int ret;
2860 struct filter_entry *f;
2861 struct adapter *adap;
2862 int i;
2863 u8 *val;
2864
2865 adap = netdev2adap(dev);
2866
Vipul Pandya1cab7752012-12-10 09:30:55 +00002867 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302868 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002869 stid += adap->tids.nftids;
2870
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002871 /* Check to make sure the filter requested is writable ...
2872 */
2873 f = &adap->tids.ftid_tab[stid];
2874 ret = writable_filter(f);
2875 if (ret)
2876 return ret;
2877
2878 /* Clear out any old resources being used by the filter before
2879 * we start constructing the new filter.
2880 */
2881 if (f->valid)
2882 clear_filter(adap, f);
2883
2884 /* Clear out filter specifications */
2885 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2886 f->fs.val.lport = cpu_to_be16(sport);
2887 f->fs.mask.lport = ~0;
2888 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002889 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002890 for (i = 0; i < 4; i++) {
2891 f->fs.val.lip[i] = val[i];
2892 f->fs.mask.lip[i] = ~0;
2893 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302894 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002895 f->fs.val.iport = port;
2896 f->fs.mask.iport = mask;
2897 }
2898 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002899
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302900 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302901 f->fs.val.proto = IPPROTO_TCP;
2902 f->fs.mask.proto = ~0;
2903 }
2904
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002905 f->fs.dirsteer = 1;
2906 f->fs.iq = queue;
2907 /* Mark filter as locked */
2908 f->locked = 1;
2909 f->fs.rpttid = 1;
2910
2911 ret = set_filter_wr(adap, stid);
2912 if (ret) {
2913 clear_filter(adap, f);
2914 return ret;
2915 }
2916
2917 return 0;
2918}
2919EXPORT_SYMBOL(cxgb4_create_server_filter);
2920
2921int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2922 unsigned int queue, bool ipv6)
2923{
2924 int ret;
2925 struct filter_entry *f;
2926 struct adapter *adap;
2927
2928 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002929
2930 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302931 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002932 stid += adap->tids.nftids;
2933
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002934 f = &adap->tids.ftid_tab[stid];
2935 /* Unlock the filter */
2936 f->locked = 0;
2937
2938 ret = delete_filter(adap, stid);
2939 if (ret)
2940 return ret;
2941
2942 return 0;
2943}
2944EXPORT_SYMBOL(cxgb4_remove_server_filter);
2945
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002946static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2947 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002948{
2949 struct port_stats stats;
2950 struct port_info *p = netdev_priv(dev);
2951 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002952
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002953 /* Block retrieving statistics during EEH error
2954 * recovery. Otherwise, the recovery might fail
2955 * and the PCI device will be removed permanently
2956 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002957 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002958 if (!netif_device_present(dev)) {
2959 spin_unlock(&adapter->stats_lock);
2960 return ns;
2961 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302962 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2963 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002964 spin_unlock(&adapter->stats_lock);
2965
2966 ns->tx_bytes = stats.tx_octets;
2967 ns->tx_packets = stats.tx_frames;
2968 ns->rx_bytes = stats.rx_octets;
2969 ns->rx_packets = stats.rx_frames;
2970 ns->multicast = stats.rx_mcast_frames;
2971
2972 /* detailed rx_errors */
2973 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2974 stats.rx_runt;
2975 ns->rx_over_errors = 0;
2976 ns->rx_crc_errors = stats.rx_fcs_err;
2977 ns->rx_frame_errors = stats.rx_symbol_err;
2978 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2979 stats.rx_ovflow2 + stats.rx_ovflow3 +
2980 stats.rx_trunc0 + stats.rx_trunc1 +
2981 stats.rx_trunc2 + stats.rx_trunc3;
2982 ns->rx_missed_errors = 0;
2983
2984 /* detailed tx_errors */
2985 ns->tx_aborted_errors = 0;
2986 ns->tx_carrier_errors = 0;
2987 ns->tx_fifo_errors = 0;
2988 ns->tx_heartbeat_errors = 0;
2989 ns->tx_window_errors = 0;
2990
2991 ns->tx_errors = stats.tx_error_frames;
2992 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2993 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2994 return ns;
2995}
2996
2997static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2998{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002999 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003000 int ret = 0, prtad, devad;
3001 struct port_info *pi = netdev_priv(dev);
3002 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
3003
3004 switch (cmd) {
3005 case SIOCGMIIPHY:
3006 if (pi->mdio_addr < 0)
3007 return -EOPNOTSUPP;
3008 data->phy_id = pi->mdio_addr;
3009 break;
3010 case SIOCGMIIREG:
3011 case SIOCSMIIREG:
3012 if (mdio_phy_id_is_c45(data->phy_id)) {
3013 prtad = mdio_phy_id_prtad(data->phy_id);
3014 devad = mdio_phy_id_devad(data->phy_id);
3015 } else if (data->phy_id < 32) {
3016 prtad = data->phy_id;
3017 devad = 0;
3018 data->reg_num &= 0x1f;
3019 } else
3020 return -EINVAL;
3021
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303022 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003023 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003024 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003025 data->reg_num, &data->val_out);
3026 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003027 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003028 data->reg_num, data->val_in);
3029 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05303030 case SIOCGHWTSTAMP:
3031 return copy_to_user(req->ifr_data, &pi->tstamp_config,
3032 sizeof(pi->tstamp_config)) ?
3033 -EFAULT : 0;
3034 case SIOCSHWTSTAMP:
3035 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
3036 sizeof(pi->tstamp_config)))
3037 return -EFAULT;
3038
3039 switch (pi->tstamp_config.rx_filter) {
3040 case HWTSTAMP_FILTER_NONE:
3041 pi->rxtstamp = false;
3042 break;
3043 case HWTSTAMP_FILTER_ALL:
3044 pi->rxtstamp = true;
3045 break;
3046 default:
3047 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3048 return -ERANGE;
3049 }
3050
3051 return copy_to_user(req->ifr_data, &pi->tstamp_config,
3052 sizeof(pi->tstamp_config)) ?
3053 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003054 default:
3055 return -EOPNOTSUPP;
3056 }
3057 return ret;
3058}
3059
3060static void cxgb_set_rxmode(struct net_device *dev)
3061{
3062 /* unfortunately we can't return errors to the stack */
3063 set_rxmode(dev, -1, false);
3064}
3065
3066static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
3067{
3068 int ret;
3069 struct port_info *pi = netdev_priv(dev);
3070
3071 if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
3072 return -EINVAL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303073 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003074 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003075 if (!ret)
3076 dev->mtu = new_mtu;
3077 return ret;
3078}
3079
3080static int cxgb_set_mac_addr(struct net_device *dev, void *p)
3081{
3082 int ret;
3083 struct sockaddr *addr = p;
3084 struct port_info *pi = netdev_priv(dev);
3085
3086 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00003087 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003088
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303089 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003090 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003091 if (ret < 0)
3092 return ret;
3093
3094 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3095 pi->xact_addr_filt = ret;
3096 return 0;
3097}
3098
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003099#ifdef CONFIG_NET_POLL_CONTROLLER
3100static void cxgb_netpoll(struct net_device *dev)
3101{
3102 struct port_info *pi = netdev_priv(dev);
3103 struct adapter *adap = pi->adapter;
3104
3105 if (adap->flags & USING_MSIX) {
3106 int i;
3107 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
3108
3109 for (i = pi->nqsets; i; i--, rx++)
3110 t4_sge_intr_msix(0, &rx->rspq);
3111 } else
3112 t4_intr_handler(adap)(0, adap);
3113}
3114#endif
3115
3116static const struct net_device_ops cxgb4_netdev_ops = {
3117 .ndo_open = cxgb_open,
3118 .ndo_stop = cxgb_close,
3119 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003120 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003121 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003122 .ndo_set_rx_mode = cxgb_set_rxmode,
3123 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003124 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003125 .ndo_validate_addr = eth_validate_addr,
3126 .ndo_do_ioctl = cxgb_ioctl,
3127 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003128#ifdef CONFIG_NET_POLL_CONTROLLER
3129 .ndo_poll_controller = cxgb_netpoll,
3130#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303131#ifdef CONFIG_CHELSIO_T4_FCOE
3132 .ndo_fcoe_enable = cxgb_fcoe_enable,
3133 .ndo_fcoe_disable = cxgb_fcoe_disable,
3134#endif /* CONFIG_CHELSIO_T4_FCOE */
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05303135#ifdef CONFIG_NET_RX_BUSY_POLL
3136 .ndo_busy_poll = cxgb_busy_poll,
3137#endif
3138
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003139};
3140
3141void t4_fatal_err(struct adapter *adap)
3142{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303143 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003144 t4_intr_disable(adap);
3145 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3146}
3147
3148static void setup_memwin(struct adapter *adap)
3149{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303150 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003151
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303152 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003153}
3154
3155static void setup_memwin_rdma(struct adapter *adap)
3156{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003157 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303158 u32 start;
3159 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003160
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303161 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3162 start &= PCI_BASE_ADDRESS_MEM_MASK;
3163 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003164 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3165 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303166 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3167 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003168 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303169 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003170 adap->vres.ocq.start);
3171 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303172 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003173 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003174}
3175
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003176static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3177{
3178 u32 v;
3179 int ret;
3180
3181 /* get device capabilities */
3182 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303183 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3184 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303185 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303186 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003187 if (ret < 0)
3188 return ret;
3189
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303190 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3191 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303192 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003193 if (ret < 0)
3194 return ret;
3195
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303196 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003197 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303198 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3199 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003200 if (ret < 0)
3201 return ret;
3202
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303203 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303204 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3205 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003206 if (ret < 0)
3207 return ret;
3208
3209 t4_sge_init(adap);
3210
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003211 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303212 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303213 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303214 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3215 v = t4_read_reg(adap, TP_PIO_DATA_A);
3216 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003217
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003218 /* first 4 Tx modulation queues point to consecutive Tx channels */
3219 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303220 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3221 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003222
3223 /* associate each Tx modulation queue with consecutive Tx channels */
3224 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303225 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303226 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303227 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303228 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303229 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303230 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003231
3232#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3233 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303234 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3235 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3236 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3237 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3238 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3239 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3240 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3241 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3242 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3243 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003244 }
3245
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003246 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303247 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003248}
3249
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003250/*
3251 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3252 */
3253#define MAX_ATIDS 8192U
3254
3255/*
3256 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003257 *
3258 * If the firmware we're dealing with has Configuration File support, then
3259 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003260 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003261
3262/*
3263 * Tweak configuration based on module parameters, etc. Most of these have
3264 * defaults assigned to them by Firmware Configuration Files (if we're using
3265 * them) but need to be explicitly set if we're using hard-coded
3266 * initialization. But even in the case of using Firmware Configuration
3267 * Files, we'd like to expose the ability to change these via module
3268 * parameters so these are essentially common tweaks/settings for
3269 * Configuration Files and hard-coded initialization ...
3270 */
3271static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003272{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003273 /*
3274 * Fix up various Host-Dependent Parameters like Page Size, Cache
3275 * Line Size, etc. The firmware default is for a 4KB Page Size and
3276 * 64B Cache Line Size ...
3277 */
3278 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003279
Vipul Pandya636f9d32012-09-26 02:39:39 +00003280 /*
3281 * Process module parameters which affect early initialization.
3282 */
3283 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3284 dev_err(&adapter->pdev->dev,
3285 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3286 rx_dma_offset);
3287 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003288 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303289 t4_set_reg_field(adapter, SGE_CONTROL_A,
3290 PKTSHIFT_V(PKTSHIFT_M),
3291 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003292
Vipul Pandya636f9d32012-09-26 02:39:39 +00003293 /*
3294 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3295 * adds the pseudo header itself.
3296 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303297 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3298 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003299
3300 return 0;
3301}
3302
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303303/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3304 * unto themselves and they contain their own firmware to perform their
3305 * tasks ...
3306 */
3307static int phy_aq1202_version(const u8 *phy_fw_data,
3308 size_t phy_fw_size)
3309{
3310 int offset;
3311
3312 /* At offset 0x8 you're looking for the primary image's
3313 * starting offset which is 3 Bytes wide
3314 *
3315 * At offset 0xa of the primary image, you look for the offset
3316 * of the DRAM segment which is 3 Bytes wide.
3317 *
3318 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3319 * wide
3320 */
3321 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3322 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3323 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3324
3325 offset = le24(phy_fw_data + 0x8) << 12;
3326 offset = le24(phy_fw_data + offset + 0xa);
3327 return be16(phy_fw_data + offset + 0x27e);
3328
3329 #undef be16
3330 #undef le16
3331 #undef le24
3332}
3333
3334static struct info_10gbt_phy_fw {
3335 unsigned int phy_fw_id; /* PCI Device ID */
3336 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3337 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3338 int phy_flash; /* Has FLASH for PHY Firmware */
3339} phy_info_array[] = {
3340 {
3341 PHY_AQ1202_DEVICEID,
3342 PHY_AQ1202_FIRMWARE,
3343 phy_aq1202_version,
3344 1,
3345 },
3346 {
3347 PHY_BCM84834_DEVICEID,
3348 PHY_BCM84834_FIRMWARE,
3349 NULL,
3350 0,
3351 },
3352 { 0, NULL, NULL },
3353};
3354
3355static struct info_10gbt_phy_fw *find_phy_info(int devid)
3356{
3357 int i;
3358
3359 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3360 if (phy_info_array[i].phy_fw_id == devid)
3361 return &phy_info_array[i];
3362 }
3363 return NULL;
3364}
3365
3366/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3367 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3368 * we return a negative error number. If we transfer new firmware we return 1
3369 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3370 */
3371static int adap_init0_phy(struct adapter *adap)
3372{
3373 const struct firmware *phyf;
3374 int ret;
3375 struct info_10gbt_phy_fw *phy_info;
3376
3377 /* Use the device ID to determine which PHY file to flash.
3378 */
3379 phy_info = find_phy_info(adap->pdev->device);
3380 if (!phy_info) {
3381 dev_warn(adap->pdev_dev,
3382 "No PHY Firmware file found for this PHY\n");
3383 return -EOPNOTSUPP;
3384 }
3385
3386 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3387 * use that. The adapter firmware provides us with a memory buffer
3388 * where we can load a PHY firmware file from the host if we want to
3389 * override the PHY firmware File in flash.
3390 */
3391 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3392 adap->pdev_dev);
3393 if (ret < 0) {
3394 /* For adapters without FLASH attached to PHY for their
3395 * firmware, it's obviously a fatal error if we can't get the
3396 * firmware to the adapter. For adapters with PHY firmware
3397 * FLASH storage, it's worth a warning if we can't find the
3398 * PHY Firmware but we'll neuter the error ...
3399 */
3400 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3401 "/lib/firmware/%s, error %d\n",
3402 phy_info->phy_fw_file, -ret);
3403 if (phy_info->phy_flash) {
3404 int cur_phy_fw_ver = 0;
3405
3406 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3407 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3408 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3409 ret = 0;
3410 }
3411
3412 return ret;
3413 }
3414
3415 /* Load PHY Firmware onto adapter.
3416 */
3417 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3418 phy_info->phy_fw_version,
3419 (u8 *)phyf->data, phyf->size);
3420 if (ret < 0)
3421 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3422 -ret);
3423 else if (ret > 0) {
3424 int new_phy_fw_ver = 0;
3425
3426 if (phy_info->phy_fw_version)
3427 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3428 phyf->size);
3429 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3430 "Firmware /lib/firmware/%s, version %#x\n",
3431 phy_info->phy_fw_file, new_phy_fw_ver);
3432 }
3433
3434 release_firmware(phyf);
3435
3436 return ret;
3437}
3438
Vipul Pandya636f9d32012-09-26 02:39:39 +00003439/*
3440 * Attempt to initialize the adapter via a Firmware Configuration File.
3441 */
3442static int adap_init0_config(struct adapter *adapter, int reset)
3443{
3444 struct fw_caps_config_cmd caps_cmd;
3445 const struct firmware *cf;
3446 unsigned long mtype = 0, maddr = 0;
3447 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303448 int ret;
3449 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003450 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303451 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003452
3453 /*
3454 * Reset device if necessary.
3455 */
3456 if (reset) {
3457 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303458 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003459 if (ret < 0)
3460 goto bye;
3461 }
3462
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303463 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3464 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3465 * to be performed after any global adapter RESET above since some
3466 * PHYs only have local RAM copies of the PHY firmware.
3467 */
3468 if (is_10gbt_device(adapter->pdev->device)) {
3469 ret = adap_init0_phy(adapter);
3470 if (ret < 0)
3471 goto bye;
3472 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003473 /*
3474 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3475 * then use that. Otherwise, use the configuration file stored
3476 * in the adapter flash ...
3477 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303478 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003479 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303480 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003481 break;
3482 case CHELSIO_T5:
3483 fw_config_file = FW5_CFNAME;
3484 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303485 case CHELSIO_T6:
3486 fw_config_file = FW6_CFNAME;
3487 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003488 default:
3489 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3490 adapter->pdev->device);
3491 ret = -EINVAL;
3492 goto bye;
3493 }
3494
3495 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003496 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303497 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003498 mtype = FW_MEMTYPE_CF_FLASH;
3499 maddr = t4_flash_cfg_addr(adapter);
3500 } else {
3501 u32 params[7], val[7];
3502
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303503 sprintf(fw_config_file_path,
3504 "/lib/firmware/%s", fw_config_file);
3505 config_name = fw_config_file_path;
3506
Vipul Pandya636f9d32012-09-26 02:39:39 +00003507 if (cf->size >= FLASH_CFG_MAX_SIZE)
3508 ret = -ENOMEM;
3509 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303510 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3511 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003512 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303513 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003514 if (ret == 0) {
3515 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303516 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003517 * sizes have to be in terms of multiples of 4
3518 * bytes. So, if the Configuration File isn't
3519 * a multiple of 4 bytes in length we'll have
3520 * to write that out separately since we can't
3521 * guarantee that the bytes following the
3522 * residual byte in the buffer returned by
3523 * request_firmware() are zeroed out ...
3524 */
3525 size_t resid = cf->size & 0x3;
3526 size_t size = cf->size & ~0x3;
3527 __be32 *data = (__be32 *)cf->data;
3528
Hariprasad Shenai51678652014-11-21 12:52:02 +05303529 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3530 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003531
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303532 spin_lock(&adapter->win0_lock);
3533 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3534 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003535 if (ret == 0 && resid != 0) {
3536 union {
3537 __be32 word;
3538 char buf[4];
3539 } last;
3540 int i;
3541
3542 last.word = data[size >> 2];
3543 for (i = resid; i < 4; i++)
3544 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303545 ret = t4_memory_rw(adapter, 0, mtype,
3546 maddr + size,
3547 4, &last.word,
3548 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003549 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303550 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003551 }
3552 }
3553
3554 release_firmware(cf);
3555 if (ret)
3556 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003557 }
3558
Vipul Pandya636f9d32012-09-26 02:39:39 +00003559 /*
3560 * Issue a Capability Configuration command to the firmware to get it
3561 * to parse the Configuration File. We don't use t4_fw_config_file()
3562 * because we want the ability to modify various features after we've
3563 * processed the configuration file ...
3564 */
3565 memset(&caps_cmd, 0, sizeof(caps_cmd));
3566 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303567 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3568 FW_CMD_REQUEST_F |
3569 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303570 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303571 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3572 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3573 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003574 FW_LEN16(caps_cmd));
3575 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3576 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303577
3578 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3579 * Configuration File in FLASH), our last gasp effort is to use the
3580 * Firmware Configuration File which is embedded in the firmware. A
3581 * very few early versions of the firmware didn't have one embedded
3582 * but we can ignore those.
3583 */
3584 if (ret == -ENOENT) {
3585 memset(&caps_cmd, 0, sizeof(caps_cmd));
3586 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303587 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3588 FW_CMD_REQUEST_F |
3589 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303590 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3591 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3592 sizeof(caps_cmd), &caps_cmd);
3593 config_name = "Firmware Default";
3594 }
3595
3596 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003597 if (ret < 0)
3598 goto bye;
3599
Vipul Pandya636f9d32012-09-26 02:39:39 +00003600 finiver = ntohl(caps_cmd.finiver);
3601 finicsum = ntohl(caps_cmd.finicsum);
3602 cfcsum = ntohl(caps_cmd.cfcsum);
3603 if (finicsum != cfcsum)
3604 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3605 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3606 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003607
Vipul Pandya636f9d32012-09-26 02:39:39 +00003608 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003609 * And now tell the firmware to use the configuration we just loaded.
3610 */
3611 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303612 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3613 FW_CMD_REQUEST_F |
3614 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303615 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003616 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3617 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003618 if (ret < 0)
3619 goto bye;
3620
Vipul Pandya636f9d32012-09-26 02:39:39 +00003621 /*
3622 * Tweak configuration based on system architecture, module
3623 * parameters, etc.
3624 */
3625 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003626 if (ret < 0)
3627 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003628
Vipul Pandya636f9d32012-09-26 02:39:39 +00003629 /*
3630 * And finally tell the firmware to initialize itself using the
3631 * parameters from the Configuration File.
3632 */
3633 ret = t4_fw_initialize(adapter, adapter->mbox);
3634 if (ret < 0)
3635 goto bye;
3636
Hariprasad Shenai06640312015-01-13 15:19:25 +05303637 /* Emit Firmware Configuration File information and return
3638 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003639 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003640 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303641 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3642 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003643 return 0;
3644
3645 /*
3646 * Something bad happened. Return the error ... (If the "error"
3647 * is that there's no Configuration File on the adapter we don't
3648 * want to issue a warning since this is fairly common.)
3649 */
3650bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303651 if (config_issued && ret != -ENOENT)
3652 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3653 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003654 return ret;
3655}
3656
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303657static struct fw_info fw_info_array[] = {
3658 {
3659 .chip = CHELSIO_T4,
3660 .fs_name = FW4_CFNAME,
3661 .fw_mod_name = FW4_FNAME,
3662 .fw_hdr = {
3663 .chip = FW_HDR_CHIP_T4,
3664 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3665 .intfver_nic = FW_INTFVER(T4, NIC),
3666 .intfver_vnic = FW_INTFVER(T4, VNIC),
3667 .intfver_ri = FW_INTFVER(T4, RI),
3668 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3669 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3670 },
3671 }, {
3672 .chip = CHELSIO_T5,
3673 .fs_name = FW5_CFNAME,
3674 .fw_mod_name = FW5_FNAME,
3675 .fw_hdr = {
3676 .chip = FW_HDR_CHIP_T5,
3677 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3678 .intfver_nic = FW_INTFVER(T5, NIC),
3679 .intfver_vnic = FW_INTFVER(T5, VNIC),
3680 .intfver_ri = FW_INTFVER(T5, RI),
3681 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3682 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3683 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303684 }, {
3685 .chip = CHELSIO_T6,
3686 .fs_name = FW6_CFNAME,
3687 .fw_mod_name = FW6_FNAME,
3688 .fw_hdr = {
3689 .chip = FW_HDR_CHIP_T6,
3690 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3691 .intfver_nic = FW_INTFVER(T6, NIC),
3692 .intfver_vnic = FW_INTFVER(T6, VNIC),
3693 .intfver_ofld = FW_INTFVER(T6, OFLD),
3694 .intfver_ri = FW_INTFVER(T6, RI),
3695 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3696 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3697 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3698 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3699 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303700 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303701
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303702};
3703
3704static struct fw_info *find_fw_info(int chip)
3705{
3706 int i;
3707
3708 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3709 if (fw_info_array[i].chip == chip)
3710 return &fw_info_array[i];
3711 }
3712 return NULL;
3713}
3714
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003715/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003716 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003717 */
3718static int adap_init0(struct adapter *adap)
3719{
3720 int ret;
3721 u32 v, port_vec;
3722 enum dev_state state;
3723 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003724 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303725 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003726
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303727 /* Grab Firmware Device Log parameters as early as possible so we have
3728 * access to it for debugging, etc.
3729 */
3730 ret = t4_init_devlog_params(adap);
3731 if (ret < 0)
3732 return ret;
3733
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303734 /* Contact FW, advertising Master capability */
3735 ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003736 if (ret < 0) {
3737 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3738 ret);
3739 return ret;
3740 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003741 if (ret == adap->mbox)
3742 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003743
Vipul Pandya636f9d32012-09-26 02:39:39 +00003744 /*
3745 * If we're the Master PF Driver and the device is uninitialized,
3746 * then let's consider upgrading the firmware ... (We always want
3747 * to check the firmware version number in order to A. get it for
3748 * later reporting and B. to warn if the currently loaded firmware
3749 * is excessively mismatched relative to the driver.)
3750 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303751 t4_get_fw_version(adap, &adap->params.fw_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303752 t4_get_bs_version(adap, &adap->params.bs_vers);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303753 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303754 t4_get_exprom_version(adap, &adap->params.er_vers);
3755
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303756 ret = t4_check_fw_version(adap);
3757 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303758 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303759 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003760 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303761 struct fw_info *fw_info;
3762 struct fw_hdr *card_fw;
3763 const struct firmware *fw;
3764 const u8 *fw_data = NULL;
3765 unsigned int fw_size = 0;
3766
3767 /* This is the firmware whose headers the driver was compiled
3768 * against
3769 */
3770 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3771 if (fw_info == NULL) {
3772 dev_err(adap->pdev_dev,
3773 "unable to get firmware info for chip %d.\n",
3774 CHELSIO_CHIP_VERSION(adap->params.chip));
3775 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003776 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303777
3778 /* allocate memory to read the header of the firmware on the
3779 * card
3780 */
3781 card_fw = t4_alloc_mem(sizeof(*card_fw));
3782
3783 /* Get FW from from /lib/firmware/ */
3784 ret = request_firmware(&fw, fw_info->fw_mod_name,
3785 adap->pdev_dev);
3786 if (ret < 0) {
3787 dev_err(adap->pdev_dev,
3788 "unable to load firmware image %s, error %d\n",
3789 fw_info->fw_mod_name, ret);
3790 } else {
3791 fw_data = fw->data;
3792 fw_size = fw->size;
3793 }
3794
3795 /* upgrade FW logic */
3796 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3797 state, &reset);
3798
3799 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003800 release_firmware(fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303801 t4_free_mem(card_fw);
3802
Vipul Pandya636f9d32012-09-26 02:39:39 +00003803 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303804 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003805 }
3806
3807 /*
3808 * Grab VPD parameters. This should be done after we establish a
3809 * connection to the firmware since some of the VPD parameters
3810 * (notably the Core Clock frequency) are retrieved via requests to
3811 * the firmware. On the other hand, we need these fairly early on
3812 * so we do this right after getting ahold of the firmware.
3813 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303814 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003815 if (ret < 0)
3816 goto bye;
3817
Vipul Pandya636f9d32012-09-26 02:39:39 +00003818 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003819 * Find out what ports are available to us. Note that we need to do
3820 * this before calling adap_init0_no_config() since it needs nports
3821 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003822 */
3823 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303824 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3825 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303826 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003827 if (ret < 0)
3828 goto bye;
3829
3830 adap->params.nports = hweight32(port_vec);
3831 adap->params.portvec = port_vec;
3832
Hariprasad Shenai06640312015-01-13 15:19:25 +05303833 /* If the firmware is initialized already, emit a simply note to that
3834 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003835 */
3836 if (state == DEV_STATE_INIT) {
3837 dev_info(adap->pdev_dev, "Coming up as %s: "\
3838 "Adapter already initialized\n",
3839 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003840 } else {
3841 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3842 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303843
3844 /* Find out whether we're dealing with a version of the
3845 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003846 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303847 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3848 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303849 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303850 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003851
Hariprasad Shenai06640312015-01-13 15:19:25 +05303852 /* If the firmware doesn't support Configuration Files,
3853 * return an error.
3854 */
3855 if (ret < 0) {
3856 dev_err(adap->pdev_dev, "firmware doesn't support "
3857 "Firmware Configuration Files\n");
3858 goto bye;
3859 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003860
Hariprasad Shenai06640312015-01-13 15:19:25 +05303861 /* The firmware provides us with a memory buffer where we can
3862 * load a Configuration File from the host if we want to
3863 * override the Configuration File in flash.
3864 */
3865 ret = adap_init0_config(adap, reset);
3866 if (ret == -ENOENT) {
3867 dev_err(adap->pdev_dev, "no Configuration File "
3868 "present on adapter.\n");
3869 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003870 }
3871 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303872 dev_err(adap->pdev_dev, "could not initialize "
3873 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003874 goto bye;
3875 }
3876 }
3877
Hariprasad Shenai06640312015-01-13 15:19:25 +05303878 /* Give the SGE code a chance to pull in anything that it needs ...
3879 * Note that this must be called after we retrieve our VPD parameters
3880 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003881 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303882 ret = t4_sge_init(adap);
3883 if (ret < 0)
3884 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003885
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003886 if (is_bypass_device(adap->pdev->device))
3887 adap->params.bypass = 1;
3888
Vipul Pandya636f9d32012-09-26 02:39:39 +00003889 /*
3890 * Grab some of our basic fundamental operating parameters.
3891 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003892#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303893 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3894 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003895
3896#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303897 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3898 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3899 FW_PARAMS_PARAM_Y_V(0) | \
3900 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003901
Vipul Pandya636f9d32012-09-26 02:39:39 +00003902 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003903 params[1] = FW_PARAM_PFVF(L2T_START);
3904 params[2] = FW_PARAM_PFVF(L2T_END);
3905 params[3] = FW_PARAM_PFVF(FILTER_START);
3906 params[4] = FW_PARAM_PFVF(FILTER_END);
3907 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303908 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003909 if (ret < 0)
3910 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003911 adap->sge.egr_start = val[0];
3912 adap->l2t_start = val[1];
3913 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003914 adap->tids.ftid_base = val[3];
3915 adap->tids.nftids = val[4] - val[3] + 1;
3916 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003917
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303918 /* qids (ingress/egress) returned from firmware can be anywhere
3919 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3920 * Hence driver needs to allocate memory for this range to
3921 * store the queue info. Get the highest IQFLINT/EQ index returned
3922 * in FW_EQ_*_CMD.alloc command.
3923 */
3924 params[0] = FW_PARAM_PFVF(EQ_END);
3925 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303926 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303927 if (ret < 0)
3928 goto bye;
3929 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3930 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3931
3932 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3933 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3934 if (!adap->sge.egr_map) {
3935 ret = -ENOMEM;
3936 goto bye;
3937 }
3938
3939 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3940 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3941 if (!adap->sge.ingr_map) {
3942 ret = -ENOMEM;
3943 goto bye;
3944 }
3945
3946 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303947 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303948 */
3949 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3950 sizeof(long), GFP_KERNEL);
3951 if (!adap->sge.starving_fl) {
3952 ret = -ENOMEM;
3953 goto bye;
3954 }
3955
3956 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3957 sizeof(long), GFP_KERNEL);
3958 if (!adap->sge.txq_maperr) {
3959 ret = -ENOMEM;
3960 goto bye;
3961 }
3962
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303963#ifdef CONFIG_DEBUG_FS
3964 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3965 sizeof(long), GFP_KERNEL);
3966 if (!adap->sge.blocked_fl) {
3967 ret = -ENOMEM;
3968 goto bye;
3969 }
3970#endif
3971
Anish Bhattb5a02f52015-01-14 15:17:34 -08003972 params[0] = FW_PARAM_PFVF(CLIP_START);
3973 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303974 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003975 if (ret < 0)
3976 goto bye;
3977 adap->clipt_start = val[0];
3978 adap->clipt_end = val[1];
3979
Vipul Pandya636f9d32012-09-26 02:39:39 +00003980 /* query params related to active filter region */
3981 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3982 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303983 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003984 /* If Active filter size is set we enable establishing
3985 * offload connection through firmware work request
3986 */
3987 if ((val[0] != val[1]) && (ret >= 0)) {
3988 adap->flags |= FW_OFLD_CONN;
3989 adap->tids.aftid_base = val[0];
3990 adap->tids.aftid_end = val[1];
3991 }
3992
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003993 /* If we're running on newer firmware, let it know that we're
3994 * prepared to deal with encapsulated CPL messages. Older
3995 * firmware won't understand this and we'll just get
3996 * unencapsulated messages ...
3997 */
3998 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3999 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304000 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004001
Vipul Pandya636f9d32012-09-26 02:39:39 +00004002 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304003 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
4004 * capability. Earlier versions of the firmware didn't have the
4005 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
4006 * permission to use ULPTX MEMWRITE DSGL.
4007 */
4008 if (is_t4(adap->params.chip)) {
4009 adap->params.ulptx_memwrite_dsgl = false;
4010 } else {
4011 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304012 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304013 1, params, val);
4014 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4015 }
4016
4017 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004018 * Get device capabilities so we can determine what resources we need
4019 * to manage.
4020 */
4021 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304022 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4023 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304024 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004025 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4026 &caps_cmd);
4027 if (ret < 0)
4028 goto bye;
4029
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004030 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004031 /* query offload-related parameters */
4032 params[0] = FW_PARAM_DEV(NTID);
4033 params[1] = FW_PARAM_PFVF(SERVER_START);
4034 params[2] = FW_PARAM_PFVF(SERVER_END);
4035 params[3] = FW_PARAM_PFVF(TDDP_START);
4036 params[4] = FW_PARAM_PFVF(TDDP_END);
4037 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304038 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004039 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004040 if (ret < 0)
4041 goto bye;
4042 adap->tids.ntids = val[0];
4043 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4044 adap->tids.stid_base = val[1];
4045 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004046 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004047 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004048 * region into two parts. Regular filters get 1/3rd and server
4049 * filters get 2/3rd part. This is only enabled if workarond
4050 * path is enabled.
4051 * 1. For regular filters.
4052 * 2. Server filter: This are special filters which are used
4053 * to redirect SYN packets to offload queue.
4054 */
4055 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4056 adap->tids.sftid_base = adap->tids.ftid_base +
4057 DIV_ROUND_UP(adap->tids.nftids, 3);
4058 adap->tids.nsftids = adap->tids.nftids -
4059 DIV_ROUND_UP(adap->tids.nftids, 3);
4060 adap->tids.nftids = adap->tids.sftid_base -
4061 adap->tids.ftid_base;
4062 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004063 adap->vres.ddp.start = val[3];
4064 adap->vres.ddp.size = val[4] - val[3] + 1;
4065 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004066
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004067 adap->params.offload = 1;
4068 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004069 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004070 params[0] = FW_PARAM_PFVF(STAG_START);
4071 params[1] = FW_PARAM_PFVF(STAG_END);
4072 params[2] = FW_PARAM_PFVF(RQ_START);
4073 params[3] = FW_PARAM_PFVF(RQ_END);
4074 params[4] = FW_PARAM_PFVF(PBL_START);
4075 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304076 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004077 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004078 if (ret < 0)
4079 goto bye;
4080 adap->vres.stag.start = val[0];
4081 adap->vres.stag.size = val[1] - val[0] + 1;
4082 adap->vres.rq.start = val[2];
4083 adap->vres.rq.size = val[3] - val[2] + 1;
4084 adap->vres.pbl.start = val[4];
4085 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004086
4087 params[0] = FW_PARAM_PFVF(SQRQ_START);
4088 params[1] = FW_PARAM_PFVF(SQRQ_END);
4089 params[2] = FW_PARAM_PFVF(CQ_START);
4090 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004091 params[4] = FW_PARAM_PFVF(OCQ_START);
4092 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304093 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304094 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004095 if (ret < 0)
4096 goto bye;
4097 adap->vres.qp.start = val[0];
4098 adap->vres.qp.size = val[1] - val[0] + 1;
4099 adap->vres.cq.start = val[2];
4100 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004101 adap->vres.ocq.start = val[4];
4102 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304103
4104 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4105 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304106 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304107 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304108 if (ret < 0) {
4109 adap->params.max_ordird_qp = 8;
4110 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4111 ret = 0;
4112 } else {
4113 adap->params.max_ordird_qp = val[0];
4114 adap->params.max_ird_adapter = val[1];
4115 }
4116 dev_info(adap->pdev_dev,
4117 "max_ordird_qp %d max_ird_adapter %d\n",
4118 adap->params.max_ordird_qp,
4119 adap->params.max_ird_adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004120 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004121 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004122 params[0] = FW_PARAM_PFVF(ISCSI_START);
4123 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304124 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004125 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004126 if (ret < 0)
4127 goto bye;
4128 adap->vres.iscsi.start = val[0];
4129 adap->vres.iscsi.size = val[1] - val[0] + 1;
4130 }
4131#undef FW_PARAM_PFVF
4132#undef FW_PARAM_DEV
4133
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304134 /* The MTU/MSS Table is initialized by now, so load their values. If
4135 * we're initializing the adapter, then we'll make any modifications
4136 * we want to the MTU/MSS Table and also initialize the congestion
4137 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004138 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004139 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304140 if (state != DEV_STATE_INIT) {
4141 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004142
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304143 /* The default MTU Table contains values 1492 and 1500.
4144 * However, for TCP, it's better to have two values which are
4145 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4146 * This allows us to have a TCP Data Payload which is a
4147 * multiple of 8 regardless of what combination of TCP Options
4148 * are in use (always a multiple of 4 bytes) which is
4149 * important for performance reasons. For instance, if no
4150 * options are in use, then we have a 20-byte IP header and a
4151 * 20-byte TCP header. In this case, a 1500-byte MSS would
4152 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4153 * which is not a multiple of 8. So using an MSS of 1488 in
4154 * this case results in a TCP Data Payload of 1448 bytes which
4155 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4156 * Stamps have been negotiated, then an MTU of 1500 bytes
4157 * results in a TCP Data Payload of 1448 bytes which, as
4158 * above, is a multiple of 8 bytes ...
4159 */
4160 for (i = 0; i < NMTUS; i++)
4161 if (adap->params.mtus[i] == 1492) {
4162 adap->params.mtus[i] = 1488;
4163 break;
4164 }
4165
4166 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4167 adap->params.b_wnd);
4168 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304169 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004170 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05304171 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004172 return 0;
4173
4174 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004175 * Something bad happened. If a command timed out or failed with EIO
4176 * FW does not operate within its spec or something catastrophic
4177 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004178 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004179bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304180 kfree(adap->sge.egr_map);
4181 kfree(adap->sge.ingr_map);
4182 kfree(adap->sge.starving_fl);
4183 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304184#ifdef CONFIG_DEBUG_FS
4185 kfree(adap->sge.blocked_fl);
4186#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004187 if (ret != -ETIMEDOUT && ret != -EIO)
4188 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004189 return ret;
4190}
4191
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004192/* EEH callbacks */
4193
4194static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4195 pci_channel_state_t state)
4196{
4197 int i;
4198 struct adapter *adap = pci_get_drvdata(pdev);
4199
4200 if (!adap)
4201 goto out;
4202
4203 rtnl_lock();
4204 adap->flags &= ~FW_OK;
4205 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004206 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004207 for_each_port(adap, i) {
4208 struct net_device *dev = adap->port[i];
4209
4210 netif_device_detach(dev);
4211 netif_carrier_off(dev);
4212 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004213 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304214 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004215 if (adap->flags & FULL_INIT_DONE)
4216 cxgb_down(adap);
4217 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004218 if ((adap->flags & DEV_ENABLED)) {
4219 pci_disable_device(pdev);
4220 adap->flags &= ~DEV_ENABLED;
4221 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004222out: return state == pci_channel_io_perm_failure ?
4223 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4224}
4225
4226static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4227{
4228 int i, ret;
4229 struct fw_caps_config_cmd c;
4230 struct adapter *adap = pci_get_drvdata(pdev);
4231
4232 if (!adap) {
4233 pci_restore_state(pdev);
4234 pci_save_state(pdev);
4235 return PCI_ERS_RESULT_RECOVERED;
4236 }
4237
Gavin Shan144be3d2014-01-23 12:27:34 +08004238 if (!(adap->flags & DEV_ENABLED)) {
4239 if (pci_enable_device(pdev)) {
4240 dev_err(&pdev->dev, "Cannot reenable PCI "
4241 "device after reset\n");
4242 return PCI_ERS_RESULT_DISCONNECT;
4243 }
4244 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004245 }
4246
4247 pci_set_master(pdev);
4248 pci_restore_state(pdev);
4249 pci_save_state(pdev);
4250 pci_cleanup_aer_uncorrect_error_status(pdev);
4251
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304252 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004253 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304254 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004255 return PCI_ERS_RESULT_DISCONNECT;
4256 adap->flags |= FW_OK;
4257 if (adap_init1(adap, &c))
4258 return PCI_ERS_RESULT_DISCONNECT;
4259
4260 for_each_port(adap, i) {
4261 struct port_info *p = adap2pinfo(adap, i);
4262
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304263 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004264 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004265 if (ret < 0)
4266 return PCI_ERS_RESULT_DISCONNECT;
4267 p->viid = ret;
4268 p->xact_addr_filt = -1;
4269 }
4270
4271 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4272 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004273 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004274 if (cxgb_up(adap))
4275 return PCI_ERS_RESULT_DISCONNECT;
4276 return PCI_ERS_RESULT_RECOVERED;
4277}
4278
4279static void eeh_resume(struct pci_dev *pdev)
4280{
4281 int i;
4282 struct adapter *adap = pci_get_drvdata(pdev);
4283
4284 if (!adap)
4285 return;
4286
4287 rtnl_lock();
4288 for_each_port(adap, i) {
4289 struct net_device *dev = adap->port[i];
4290
4291 if (netif_running(dev)) {
4292 link_start(dev);
4293 cxgb_set_rxmode(dev);
4294 }
4295 netif_device_attach(dev);
4296 }
4297 rtnl_unlock();
4298}
4299
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004300static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004301 .error_detected = eeh_err_detected,
4302 .slot_reset = eeh_slot_reset,
4303 .resume = eeh_resume,
4304};
4305
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304306static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004307{
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304308 return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
4309 (lc->supported & FW_PORT_CAP_SPEED_40G) != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004310}
4311
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304312static inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
4313 unsigned int us, unsigned int cnt,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004314 unsigned int size, unsigned int iqe_size)
4315{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304316 q->adap = adap;
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304317 cxgb4_set_rspq_intr_params(q, us, cnt);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004318 q->iqe_len = iqe_size;
4319 q->size = size;
4320}
4321
4322/*
4323 * Perform default configuration of DMA queues depending on the number and type
4324 * of ports we found and the number of available CPUs. Most settings can be
4325 * modified by the admin prior to actual use.
4326 */
Bill Pemberton91744942012-12-03 09:23:02 -05004327static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004328{
4329 struct sge *s = &adap->sge;
Anish Bhatt688848b2014-06-19 21:37:13 -07004330 int i, n10g = 0, qidx = 0;
4331#ifndef CONFIG_CHELSIO_T4_DCB
4332 int q10g = 0;
4333#endif
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304334 int ciq_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004335
4336 for_each_port(adap, i)
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304337 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004338#ifdef CONFIG_CHELSIO_T4_DCB
4339 /* For Data Center Bridging support we need to be able to support up
4340 * to 8 Traffic Priorities; each of which will be assigned to its
4341 * own TX Queue in order to prevent Head-Of-Line Blocking.
4342 */
4343 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4344 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4345 MAX_ETH_QSETS, adap->params.nports * 8);
4346 BUG_ON(1);
4347 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004348
Anish Bhatt688848b2014-06-19 21:37:13 -07004349 for_each_port(adap, i) {
4350 struct port_info *pi = adap2pinfo(adap, i);
4351
4352 pi->first_qset = qidx;
4353 pi->nqsets = 8;
4354 qidx += pi->nqsets;
4355 }
4356#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004357 /*
4358 * We default to 1 queue per non-10G port and up to # of cores queues
4359 * per 10G port.
4360 */
4361 if (n10g)
4362 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004363 if (q10g > netif_get_num_default_rss_queues())
4364 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004365
4366 for_each_port(adap, i) {
4367 struct port_info *pi = adap2pinfo(adap, i);
4368
4369 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304370 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004371 qidx += pi->nqsets;
4372 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004373#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004374
4375 s->ethqsets = qidx;
4376 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4377
4378 if (is_offload(adap)) {
4379 /*
4380 * For offload we use 1 queue/channel if all ports are up to 1G,
4381 * otherwise we divide all available queues amongst the channels
4382 * capped by the number of available cores.
4383 */
4384 if (n10g) {
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304385 i = min_t(int, ARRAY_SIZE(s->iscsirxq),
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004386 num_online_cpus());
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304387 s->iscsiqsets = roundup(i, adap->params.nports);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004388 } else
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304389 s->iscsiqsets = adap->params.nports;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004390 /* For RDMA one Rx queue per channel suffices */
4391 s->rdmaqs = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304392 /* Try and allow at least 1 CIQ per cpu rounding down
4393 * to the number of ports, with a minimum of 1 per port.
4394 * A 2 port card in a 6 cpu system: 6 CIQs, 3 / port.
4395 * A 4 port card in a 6 cpu system: 4 CIQs, 1 / port.
4396 * A 4 port card in a 2 cpu system: 4 CIQs, 1 / port.
4397 */
4398 s->rdmaciqs = min_t(int, MAX_RDMA_CIQS, num_online_cpus());
4399 s->rdmaciqs = (s->rdmaciqs / adap->params.nports) *
4400 adap->params.nports;
4401 s->rdmaciqs = max_t(int, s->rdmaciqs, adap->params.nports);
Varun Prakashf2692d12016-02-14 23:02:40 +05304402
4403 if (!is_t4(adap->params.chip))
4404 s->niscsitq = s->iscsiqsets;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004405 }
4406
4407 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4408 struct sge_eth_rxq *r = &s->ethrxq[i];
4409
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304410 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004411 r->fl.size = 72;
4412 }
4413
4414 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4415 s->ethtxq[i].q.size = 1024;
4416
4417 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4418 s->ctrlq[i].q.size = 512;
4419
4420 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
4421 s->ofldtxq[i].q.size = 1024;
4422
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304423 for (i = 0; i < ARRAY_SIZE(s->iscsirxq); i++) {
4424 struct sge_ofld_rxq *r = &s->iscsirxq[i];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004425
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304426 init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004427 r->rspq.uld = CXGB4_ULD_ISCSI;
4428 r->fl.size = 72;
4429 }
4430
Varun Prakashf2692d12016-02-14 23:02:40 +05304431 if (!is_t4(adap->params.chip)) {
4432 for (i = 0; i < ARRAY_SIZE(s->iscsitrxq); i++) {
4433 struct sge_ofld_rxq *r = &s->iscsitrxq[i];
4434
4435 init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
4436 r->rspq.uld = CXGB4_ULD_ISCSIT;
4437 r->fl.size = 72;
4438 }
4439 }
4440
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004441 for (i = 0; i < ARRAY_SIZE(s->rdmarxq); i++) {
4442 struct sge_ofld_rxq *r = &s->rdmarxq[i];
4443
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304444 init_rspq(adap, &r->rspq, 5, 1, 511, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004445 r->rspq.uld = CXGB4_ULD_RDMA;
4446 r->fl.size = 72;
4447 }
4448
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304449 ciq_size = 64 + adap->vres.cq.size + adap->tids.nftids;
4450 if (ciq_size > SGE_MAX_IQ_SIZE) {
4451 CH_WARN(adap, "CIQ size too small for available IQs\n");
4452 ciq_size = SGE_MAX_IQ_SIZE;
4453 }
4454
4455 for (i = 0; i < ARRAY_SIZE(s->rdmaciq); i++) {
4456 struct sge_ofld_rxq *r = &s->rdmaciq[i];
4457
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304458 init_rspq(adap, &r->rspq, 5, 1, ciq_size, 64);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304459 r->rspq.uld = CXGB4_ULD_RDMA;
4460 }
4461
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304462 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
4463 init_rspq(adap, &s->intrq, 0, 1, 2 * MAX_INGQ, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004464}
4465
4466/*
4467 * Reduce the number of Ethernet queues across all ports to at most n.
4468 * n provides at least one queue per port.
4469 */
Bill Pemberton91744942012-12-03 09:23:02 -05004470static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004471{
4472 int i;
4473 struct port_info *pi;
4474
4475 while (n < adap->sge.ethqsets)
4476 for_each_port(adap, i) {
4477 pi = adap2pinfo(adap, i);
4478 if (pi->nqsets > 1) {
4479 pi->nqsets--;
4480 adap->sge.ethqsets--;
4481 if (adap->sge.ethqsets <= n)
4482 break;
4483 }
4484 }
4485
4486 n = 0;
4487 for_each_port(adap, i) {
4488 pi = adap2pinfo(adap, i);
4489 pi->first_qset = n;
4490 n += pi->nqsets;
4491 }
4492}
4493
4494/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4495#define EXTRA_VECS 2
4496
Bill Pemberton91744942012-12-03 09:23:02 -05004497static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004498{
4499 int ofld_need = 0;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304500 int i, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004501 struct sge *s = &adap->sge;
4502 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304503 struct msix_entry *entries;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004504
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304505 entries = kmalloc(sizeof(*entries) * (MAX_INGQ + 1),
4506 GFP_KERNEL);
4507 if (!entries)
4508 return -ENOMEM;
4509
4510 for (i = 0; i < MAX_INGQ + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004511 entries[i].entry = i;
4512
4513 want = s->max_ethqsets + EXTRA_VECS;
4514 if (is_offload(adap)) {
Varun Prakashf2692d12016-02-14 23:02:40 +05304515 want += s->rdmaqs + s->rdmaciqs + s->iscsiqsets +
4516 s->niscsitq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004517 /* need nchan for each possible ULD */
Varun Prakashf2692d12016-02-14 23:02:40 +05304518 if (is_t4(adap->params.chip))
4519 ofld_need = 3 * nchan;
4520 else
4521 ofld_need = 4 * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004522 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004523#ifdef CONFIG_CHELSIO_T4_DCB
4524 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4525 * each port.
4526 */
4527 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need;
4528#else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004529 need = adap->params.nports + EXTRA_VECS + ofld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004530#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304531 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4532 if (allocated < 0) {
4533 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4534 " not using MSI-X\n");
4535 kfree(entries);
4536 return allocated;
4537 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004538
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304539 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004540 * Every group gets its minimum requirement and NIC gets top
4541 * priority for leftovers.
4542 */
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304543 i = allocated - EXTRA_VECS - ofld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004544 if (i < s->max_ethqsets) {
4545 s->max_ethqsets = i;
4546 if (i < s->ethqsets)
4547 reduce_ethqs(adap, i);
4548 }
4549 if (is_offload(adap)) {
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304550 if (allocated < want) {
4551 s->rdmaqs = nchan;
4552 s->rdmaciqs = nchan;
Varun Prakashf2692d12016-02-14 23:02:40 +05304553
4554 if (!is_t4(adap->params.chip))
4555 s->niscsitq = nchan;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304556 }
4557
4558 /* leftovers go to OFLD */
4559 i = allocated - EXTRA_VECS - s->max_ethqsets -
Varun Prakashf2692d12016-02-14 23:02:40 +05304560 s->rdmaqs - s->rdmaciqs - s->niscsitq;
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304561 s->iscsiqsets = (i / nchan) * nchan; /* round down */
Varun Prakashf2692d12016-02-14 23:02:40 +05304562
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004563 }
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304564 for (i = 0; i < allocated; ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004565 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304566 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
4567 "nic %d iscsi %d rdma cpl %d rdma ciq %d\n",
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304568 allocated, s->max_ethqsets, s->iscsiqsets, s->rdmaqs,
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304569 s->rdmaciqs);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004570
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304571 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004572 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004573}
4574
4575#undef EXTRA_VECS
4576
Bill Pemberton91744942012-12-03 09:23:02 -05004577static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004578{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304579 unsigned int i;
4580 int err;
4581
4582 err = t4_init_rss_mode(adap, adap->mbox);
4583 if (err)
4584 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004585
4586 for_each_port(adap, i) {
4587 struct port_info *pi = adap2pinfo(adap, i);
4588
4589 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4590 if (!pi->rss)
4591 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004592 }
4593 return 0;
4594}
4595
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304596static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4597 enum pci_bus_speed *speed,
4598 enum pcie_link_width *width)
4599{
4600 u32 lnkcap1, lnkcap2;
4601 int err1, err2;
4602
4603#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4604
4605 *speed = PCI_SPEED_UNKNOWN;
4606 *width = PCIE_LNK_WIDTH_UNKNOWN;
4607
4608 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4609 &lnkcap1);
4610 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4611 &lnkcap2);
4612 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4613 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4614 *speed = PCIE_SPEED_8_0GT;
4615 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4616 *speed = PCIE_SPEED_5_0GT;
4617 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4618 *speed = PCIE_SPEED_2_5GT;
4619 }
4620 if (!err1) {
4621 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4622 if (!lnkcap2) { /* pre-r3.0 */
4623 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4624 *speed = PCIE_SPEED_5_0GT;
4625 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4626 *speed = PCIE_SPEED_2_5GT;
4627 }
4628 }
4629
4630 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4631 return err1 ? err1 : err2 ? err2 : -EINVAL;
4632 return 0;
4633}
4634
4635static void cxgb4_check_pcie_caps(struct adapter *adap)
4636{
4637 enum pcie_link_width width, width_cap;
4638 enum pci_bus_speed speed, speed_cap;
4639
4640#define PCIE_SPEED_STR(speed) \
4641 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4642 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4643 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4644 "Unknown")
4645
4646 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4647 dev_warn(adap->pdev_dev,
4648 "Unable to determine PCIe device BW capabilities\n");
4649 return;
4650 }
4651
4652 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4653 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4654 dev_warn(adap->pdev_dev,
4655 "Unable to determine PCI Express bandwidth.\n");
4656 return;
4657 }
4658
4659 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4660 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4661 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4662 width, width_cap);
4663 if (speed < speed_cap || width < width_cap)
4664 dev_info(adap->pdev_dev,
4665 "A slot with more lanes and/or higher speed is "
4666 "suggested for optimal performance.\n");
4667}
4668
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304669/* Dump basic information about the adapter */
4670static void print_adapter_info(struct adapter *adapter)
4671{
4672 /* Device information */
4673 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4674 adapter->params.vpd.id,
4675 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4676 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4677 adapter->params.vpd.sn, adapter->params.vpd.pn);
4678
4679 /* Firmware Version */
4680 if (!adapter->params.fw_vers)
4681 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4682 else
4683 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4684 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4685 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4686 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4687 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4688
4689 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4690 * Firmware, so dev_info() is more appropriate here.)
4691 */
4692 if (!adapter->params.bs_vers)
4693 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4694 else
4695 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4696 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4697 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4698 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4699 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4700
4701 /* TP Microcode Version */
4702 if (!adapter->params.tp_vers)
4703 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4704 else
4705 dev_info(adapter->pdev_dev,
4706 "TP Microcode version: %u.%u.%u.%u\n",
4707 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4708 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4709 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4710 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4711
4712 /* Expansion ROM version */
4713 if (!adapter->params.er_vers)
4714 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4715 else
4716 dev_info(adapter->pdev_dev,
4717 "Expansion ROM version: %u.%u.%u.%u\n",
4718 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4719 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4720 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4721 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4722
4723 /* Software/Hardware configuration */
4724 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4725 is_offload(adapter) ? "R" : "",
4726 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4727 (adapter->flags & USING_MSI) ? "MSI" : ""),
4728 is_offload(adapter) ? "Offload" : "non-Offload");
4729}
4730
Bill Pemberton91744942012-12-03 09:23:02 -05004731static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004732{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004733 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004734 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004735 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004736 const struct port_info *pi = netdev_priv(dev);
4737 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004738
4739 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4740 spd = " 2.5 GT/s";
4741 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4742 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004743 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4744 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004745
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004746 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4747 bufp += sprintf(bufp, "100/");
4748 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4749 bufp += sprintf(bufp, "1000/");
4750 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4751 bufp += sprintf(bufp, "10G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304752 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4753 bufp += sprintf(bufp, "40G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004754 if (bufp != buf)
4755 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304756 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004757
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304758 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4759 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004760}
4761
Bill Pemberton91744942012-12-03 09:23:02 -05004762static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004763{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004764 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004765}
4766
Dimitris Michailidis06546392010-07-11 12:01:16 +00004767/*
4768 * Free the following resources:
4769 * - memory used for tables
4770 * - MSI/MSI-X
4771 * - net devices
4772 * - resources FW is holding for us
4773 */
4774static void free_some_resources(struct adapter *adapter)
4775{
4776 unsigned int i;
4777
4778 t4_free_mem(adapter->l2t);
4779 t4_free_mem(adapter->tids.tid_tab);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304780 kfree(adapter->sge.egr_map);
4781 kfree(adapter->sge.ingr_map);
4782 kfree(adapter->sge.starving_fl);
4783 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304784#ifdef CONFIG_DEBUG_FS
4785 kfree(adapter->sge.blocked_fl);
4786#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004787 disable_msi(adapter);
4788
4789 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004790 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304791 struct port_info *pi = adap2pinfo(adapter, i);
4792
4793 if (pi->viid != 0)
4794 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4795 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004796 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004797 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004798 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004799 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304800 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004801}
4802
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004803#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004804#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004805 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004806#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004807
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304808static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4809{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304810 u16 device_id;
4811
4812 /* Retrieve adapter's device ID */
4813 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004814
4815 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304816 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004817 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304818 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004819 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304820 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004821 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304822 default:
4823 dev_err(&pdev->dev, "Device %d is not supported\n",
4824 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304825 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004826 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304827}
4828
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004829static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004830{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004831 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004832 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004833 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004834 struct adapter *adapter = NULL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304835 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304836 u32 whoami, pl_rev;
4837 enum chip_type chip;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004838
4839 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4840
4841 err = pci_request_regions(pdev, KBUILD_MODNAME);
4842 if (err) {
4843 /* Just info, some other driver may have claimed the device. */
4844 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4845 return err;
4846 }
4847
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004848 err = pci_enable_device(pdev);
4849 if (err) {
4850 dev_err(&pdev->dev, "cannot enable PCI device\n");
4851 goto out_release_regions;
4852 }
4853
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304854 regs = pci_ioremap_bar(pdev, 0);
4855 if (!regs) {
4856 dev_err(&pdev->dev, "cannot map device registers\n");
4857 err = -ENOMEM;
4858 goto out_disable_device;
4859 }
4860
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304861 err = t4_wait_dev_ready(regs);
4862 if (err < 0)
4863 goto out_unmap_bar0;
4864
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304865 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304866 whoami = readl(regs + PL_WHOAMI_A);
4867 pl_rev = REV_G(readl(regs + PL_REV_A));
4868 chip = get_chip_type(pdev, pl_rev);
4869 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4870 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304871 if (func != ent->driver_data) {
4872 iounmap(regs);
4873 pci_disable_device(pdev);
4874 pci_save_state(pdev); /* to restore SR-IOV later */
4875 goto sriov;
4876 }
4877
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004878 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004879 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004880 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4881 if (err) {
4882 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4883 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304884 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004885 }
4886 } else {
4887 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4888 if (err) {
4889 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304890 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004891 }
4892 }
4893
4894 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004895 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004896 pci_set_master(pdev);
4897 pci_save_state(pdev);
4898
4899 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4900 if (!adapter) {
4901 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304902 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004903 }
4904
Anish Bhatt29aaee62014-08-20 13:44:06 -07004905 adapter->workq = create_singlethread_workqueue("cxgb4");
4906 if (!adapter->workq) {
4907 err = -ENOMEM;
4908 goto out_free_adapter;
4909 }
4910
Gavin Shan144be3d2014-01-23 12:27:34 +08004911 /* PCI device has been enabled */
4912 adapter->flags |= DEV_ENABLED;
4913
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304914 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004915 adapter->pdev = pdev;
4916 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304917 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304918 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304919 adapter->pf = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004920 adapter->msg_enable = dflt_msg_enable;
4921 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4922
4923 spin_lock_init(&adapter->stats_lock);
4924 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004925 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004926
4927 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304928 INIT_WORK(&adapter->db_full_task, process_db_full);
4929 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004930
4931 err = t4_prep_adapter(adapter);
4932 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304933 goto out_free_adapter;
4934
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004935
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304936 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304937 s_qpp = (QUEUESPERPAGEPF0_S +
4938 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304939 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304940 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4941 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004942 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4943
4944 /* Each segment size is 128B. Write coalescing is enabled only
4945 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4946 * queue is less no of segments that can be accommodated in
4947 * a page size.
4948 */
4949 if (qpp > num_seg) {
4950 dev_err(&pdev->dev,
4951 "Incorrect number of egress queues per page\n");
4952 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304953 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004954 }
4955 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4956 pci_resource_len(pdev, 2));
4957 if (!adapter->bar2) {
4958 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4959 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304960 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004961 }
4962 }
4963
Vipul Pandya636f9d32012-09-26 02:39:39 +00004964 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004965 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304966#ifdef CONFIG_DEBUG_FS
4967 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4968#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004969 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004970 if (err)
4971 goto out_unmap_bar;
4972
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304973 /* configure SGE_STAT_CFG_A to read WC stats */
4974 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304975 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4976 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4977 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304978
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004979 for_each_port(adapter, i) {
4980 struct net_device *netdev;
4981
4982 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4983 MAX_ETH_QSETS);
4984 if (!netdev) {
4985 err = -ENOMEM;
4986 goto out_free_dev;
4987 }
4988
4989 SET_NETDEV_DEV(netdev, &pdev->dev);
4990
4991 adapter->port[i] = netdev;
4992 pi = netdev_priv(netdev);
4993 pi->adapter = adapter;
4994 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004995 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004996 netdev->irq = pdev->irq;
4997
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004998 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4999 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5000 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Patrick McHardyf6469682013-04-19 02:04:27 +00005001 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005002 if (highdma)
5003 netdev->hw_features |= NETIF_F_HIGHDMA;
5004 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005005 netdev->vlan_features = netdev->features & VLAN_FEAT;
5006
Jiri Pirko01789342011-08-16 06:29:00 +00005007 netdev->priv_flags |= IFF_UNICAST_FLT;
5008
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005009 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005010#ifdef CONFIG_CHELSIO_T4_DCB
5011 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5012 cxgb4_dcb_state_init(netdev);
5013#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305014 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005015 }
5016
5017 pci_set_drvdata(pdev, adapter);
5018
5019 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005020 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005021 if (err)
5022 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305023 } else if (adapter->params.nports == 1) {
5024 /* If we don't have a connection to the firmware -- possibly
5025 * because of an error -- grab the raw VPD parameters so we
5026 * can set the proper MAC Address on the debug network
5027 * interface that we've created.
5028 */
5029 u8 hw_addr[ETH_ALEN];
5030 u8 *na = adapter->params.vpd.na;
5031
5032 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5033 if (!err) {
5034 for (i = 0; i < ETH_ALEN; i++)
5035 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5036 hex2val(na[2 * i + 1]));
5037 t4_set_hw_addr(adapter, 0, hw_addr);
5038 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005039 }
5040
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305041 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005042 * soon as the first register_netdev completes.
5043 */
5044 cfg_queues(adapter);
5045
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305046 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005047 if (!adapter->l2t) {
5048 /* We tolerate a lack of L2T, giving up some functionality */
5049 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5050 adapter->params.offload = 0;
5051 }
5052
Anish Bhattb5a02f52015-01-14 15:17:34 -08005053#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305054 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5055 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5056 /* CLIP functionality is not present in hardware,
5057 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005058 */
5059 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305060 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005061 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305062 } else {
5063 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5064 adapter->clipt_end);
5065 if (!adapter->clipt) {
5066 /* We tolerate a lack of clip_table, giving up
5067 * some functionality
5068 */
5069 dev_warn(&pdev->dev,
5070 "could not allocate Clip table, continuing\n");
5071 adapter->params.offload = 0;
5072 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005073 }
5074#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005075 if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
5076 dev_warn(&pdev->dev, "could not allocate TID table, "
5077 "continuing\n");
5078 adapter->params.offload = 0;
5079 }
5080
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305081 if (is_offload(adapter)) {
5082 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5083 u32 hash_base, hash_reg;
5084
5085 if (chip <= CHELSIO_T5) {
5086 hash_reg = LE_DB_TID_HASHBASE_A;
5087 hash_base = t4_read_reg(adapter, hash_reg);
5088 adapter->tids.hash_base = hash_base / 4;
5089 } else {
5090 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5091 hash_base = t4_read_reg(adapter, hash_reg);
5092 adapter->tids.hash_base = hash_base;
5093 }
5094 }
5095 }
5096
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005097 /* See what interrupts we'll be using */
5098 if (msi > 1 && enable_msix(adapter) == 0)
5099 adapter->flags |= USING_MSIX;
5100 else if (msi > 0 && pci_enable_msi(pdev) == 0)
5101 adapter->flags |= USING_MSI;
5102
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305103 /* check for PCI Express bandwidth capabiltites */
5104 cxgb4_check_pcie_caps(adapter);
5105
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005106 err = init_rss(adapter);
5107 if (err)
5108 goto out_free_dev;
5109
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005110 /*
5111 * The card is now ready to go. If any errors occur during device
5112 * registration we do not fail the whole card but rather proceed only
5113 * with the ports we manage to register successfully. However we must
5114 * register at least one net device.
5115 */
5116 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005117 pi = adap2pinfo(adapter, i);
5118 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5119 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5120
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005121 err = register_netdev(adapter->port[i]);
5122 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005123 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005124 adapter->chan_map[pi->tx_chan] = i;
5125 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005126 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005127 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005128 dev_err(&pdev->dev, "could not register any net devices\n");
5129 goto out_free_dev;
5130 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005131 if (err) {
5132 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5133 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005134 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005135
5136 if (cxgb4_debugfs_root) {
5137 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5138 cxgb4_debugfs_root);
5139 setup_debugfs(adapter);
5140 }
5141
David S. Miller88c51002011-10-07 13:38:43 -04005142 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5143 pdev->needs_freset = 1;
5144
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005145 if (is_offload(adapter))
5146 attach_ulds(adapter);
5147
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305148 print_adapter_info(adapter);
5149
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305150sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005151#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +00005152 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005153 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
5154 dev_info(&pdev->dev,
5155 "instantiated %u virtual functions\n",
5156 num_vf[func]);
5157#endif
5158 return 0;
5159
5160 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005161 free_some_resources(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005162 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305163 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005164 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005165 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005166 if (adapter->workq)
5167 destroy_workqueue(adapter->workq);
5168
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005169 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305170 out_unmap_bar0:
5171 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005172 out_disable_device:
5173 pci_disable_pcie_error_reporting(pdev);
5174 pci_disable_device(pdev);
5175 out_release_regions:
5176 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005177 return err;
5178}
5179
Bill Pemberton91744942012-12-03 09:23:02 -05005180static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005181{
5182 struct adapter *adapter = pci_get_drvdata(pdev);
5183
Vipul Pandya636f9d32012-09-26 02:39:39 +00005184#ifdef CONFIG_PCI_IOV
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005185 pci_disable_sriov(pdev);
5186
Vipul Pandya636f9d32012-09-26 02:39:39 +00005187#endif
5188
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005189 if (adapter) {
5190 int i;
5191
Anish Bhatt29aaee62014-08-20 13:44:06 -07005192 /* Tear down per-adapter Work Queue first since it can contain
5193 * references to our adapter data structure.
5194 */
5195 destroy_workqueue(adapter->workq);
5196
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005197 if (is_offload(adapter))
5198 detach_ulds(adapter);
5199
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305200 disable_interrupts(adapter);
5201
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005202 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005203 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005204 unregister_netdev(adapter->port[i]);
5205
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005206 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005207
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005208 /* If we allocated filters, free up state associated with any
5209 * valid filters ...
5210 */
5211 if (adapter->tids.ftid_tab) {
5212 struct filter_entry *f = &adapter->tids.ftid_tab[0];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00005213 for (i = 0; i < (adapter->tids.nftids +
5214 adapter->tids.nsftids); i++, f++)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005215 if (f->valid)
5216 clear_filter(adapter, f);
5217 }
5218
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005219 if (adapter->flags & FULL_INIT_DONE)
5220 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005221
Dimitris Michailidis06546392010-07-11 12:01:16 +00005222 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005223#if IS_ENABLED(CONFIG_IPV6)
5224 t4_cleanup_clip_tbl(adapter);
5225#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005226 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305227 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005228 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005229 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005230 if ((adapter->flags & DEV_ENABLED)) {
5231 pci_disable_device(pdev);
5232 adapter->flags &= ~DEV_ENABLED;
5233 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005234 pci_release_regions(pdev);
Li RongQingee9a33b2014-06-20 17:32:36 +08005235 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005236 kfree(adapter);
Dimitris Michailidisa069ec92010-09-30 09:17:12 +00005237 } else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005238 pci_release_regions(pdev);
5239}
5240
5241static struct pci_driver cxgb4_driver = {
5242 .name = KBUILD_MODNAME,
5243 .id_table = cxgb4_pci_tbl,
5244 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005245 .remove = remove_one,
Thadeu Lima de Souza Cascardo687d7052014-02-24 17:04:52 -03005246 .shutdown = remove_one,
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005247 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005248};
5249
5250static int __init cxgb4_init_module(void)
5251{
5252 int ret;
5253
5254 /* Debugfs support is optional, just warn if this fails */
5255 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5256 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005257 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005258
5259 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005260 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005261 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305262
Anish Bhatt1bb60372014-10-14 20:07:22 -07005263#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005264 if (!inet6addr_registered) {
5265 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5266 inet6addr_registered = true;
5267 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005268#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305269
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005270 return ret;
5271}
5272
5273static void __exit cxgb4_cleanup_module(void)
5274{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005275#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305276 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005277 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5278 inet6addr_registered = false;
5279 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005280#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005281 pci_unregister_driver(&cxgb4_driver);
5282 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005283}
5284
5285module_init(cxgb4_init_module);
5286module_exit(cxgb4_cleanup_module);