blob: 6ce4344ea6fb73b2ba2510d6dfd4f2f06dfdd8a8 [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>
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +053067#include <linux/crash_dump.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000068
69#include "cxgb4.h"
70#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053071#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000072#include "t4_msg.h"
73#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053074#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070075#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053076#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080077#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000078#include "l2t.h"
79
Hariprasad Shenai812034f2015-04-06 20:23:23 +053080char cxgb4_driver_name[] = KBUILD_MODNAME;
81
Vipul Pandya01bcca62013-07-04 16:10:46 +053082#ifdef DRV_VERSION
83#undef DRV_VERSION
84#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000085#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053086const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053087#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000088
Vipul Pandyaf2b7e782012-12-10 09:30:52 +000089/* Host shadow copy of ingress filter entry. This is in host native format
90 * and doesn't match the ordering or bit order, etc. of the hardware of the
91 * firmware command. The use of bit-field structure elements is purely to
92 * remind ourselves of the field size limitations and save memory in the case
93 * where the filter table is large.
94 */
95struct filter_entry {
96 /* Administrative fields for filter.
97 */
98 u32 valid:1; /* filter allocated and valid */
99 u32 locked:1; /* filter is administratively locked */
100
101 u32 pending:1; /* filter action is pending firmware reply */
102 u32 smtidx:8; /* Source MAC Table index for smac */
103 struct l2t_entry *l2t; /* Layer Two Table entry for dmac */
104
105 /* The filter itself. Most of this is a straight copy of information
106 * provided by the extended ioctl(). Some fields are translated to
107 * internal forms -- for instance the Ingress Queue ID passed in from
108 * the ioctl() is translated into the Absolute Ingress Queue ID.
109 */
110 struct ch_filter_specification fs;
111};
112
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000113#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
114 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
115 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
116
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530117/* Macros needed to support the PCI Device ID Table ...
118 */
119#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530120 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530121#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000122
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530123/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
124 * called for both.
125 */
126#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
127
128#define CH_PCI_ID_TABLE_ENTRY(devid) \
129 {PCI_VDEVICE(CHELSIO, (devid)), 4}
130
131#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
132 { 0, } \
133 }
134
135#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000136
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530137#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000138#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530139#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530140#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000141#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530142#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530143#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
144#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
145#define PHY_AQ1202_DEVICEID 0x4409
146#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000147
148MODULE_DESCRIPTION(DRV_DESC);
149MODULE_AUTHOR("Chelsio Communications");
150MODULE_LICENSE("Dual BSD/GPL");
151MODULE_VERSION(DRV_VERSION);
152MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530153MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000154MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530155MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000156
Vipul Pandya636f9d32012-09-26 02:39:39 +0000157/*
158 * Normally we're willing to become the firmware's Master PF but will be happy
159 * if another PF has already become the Master and initialized the adapter.
160 * Setting "force_init" will cause this driver to forcibly establish itself as
161 * the Master PF and initialize the adapter.
162 */
163static uint force_init;
164
165module_param(force_init, uint, 0644);
Hariprasad Shenaid7d3e252015-12-24 16:24:53 +0530166MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
167 "deprecated parameter");
Vipul Pandya13ee15d2012-09-26 02:39:40 +0000168
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000169static int dflt_msg_enable = DFLT_MSG_ENABLE;
170
171module_param(dflt_msg_enable, int, 0644);
Hariprasad Shenai8a21ec42016-04-05 09:52:21 +0530172MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
173 "deprecated parameter");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000174
175/*
176 * The driver uses the best interrupt scheme available on a platform in the
177 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
178 * of these schemes the driver may consider as follows:
179 *
180 * msi = 2: choose from among all three options
181 * msi = 1: only consider MSI and INTx interrupts
182 * msi = 0: force INTx interrupts
183 */
184static int msi = 2;
185
186module_param(msi, int, 0644);
187MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
188
189/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000190 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
191 * offset by 2 bytes in order to have the IP headers line up on 4-byte
192 * boundaries. This is a requirement for many architectures which will throw
193 * a machine check fault if an attempt is made to access one of the 4-byte IP
194 * header fields on a non-4-byte boundary. And it's a major performance issue
195 * even on some architectures which allow it like some implementations of the
196 * x86 ISA. However, some architectures don't mind this and for some very
197 * edge-case performance sensitive applications (like forwarding large volumes
198 * of small packets), setting this DMA offset to 0 will decrease the number of
199 * PCI-E Bus transfers enough to measurably affect performance.
200 */
201static int rx_dma_offset = 2;
202
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000203#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000204/* Configure the number of PCI-E Virtual Function which are to be instantiated
205 * on SR-IOV Capable Physical Functions.
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000206 */
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000207static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000208
209module_param_array(num_vf, uint, NULL, 0644);
Hariprasad Shenaib6244202016-06-14 14:39:31 +0530210MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3, deprecated parameter - please use the pci sysfs interface.");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000211#endif
212
Anish Bhatt688848b2014-06-19 21:37:13 -0700213/* TX Queue select used to determine what algorithm to use for selecting TX
214 * queue. Select between the kernel provided function (select_queue=0) or user
215 * cxgb_select_queue function (select_queue=1)
216 *
217 * Default: select_queue=0
218 */
219static int select_queue;
220module_param(select_queue, int, 0644);
221MODULE_PARM_DESC(select_queue,
222 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
223
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000224static struct dentry *cxgb4_debugfs_root;
225
226static LIST_HEAD(adapter_list);
227static DEFINE_MUTEX(uld_mutex);
Vipul Pandya01bcca62013-07-04 16:10:46 +0530228/* Adapter list to be accessed from atomic context */
229static LIST_HEAD(adap_rcu_list);
230static DEFINE_SPINLOCK(adap_rcu_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000231static struct cxgb4_uld_info ulds[CXGB4_ULD_MAX];
Varun Prakashf2692d12016-02-14 23:02:40 +0530232static const char *const uld_str[] = { "RDMA", "iSCSI", "iSCSIT" };
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000233
234static void link_report(struct net_device *dev)
235{
236 if (!netif_carrier_ok(dev))
237 netdev_info(dev, "link down\n");
238 else {
239 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
240
Hariprasad Shenai85412252015-10-01 13:48:48 +0530241 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000242 const struct port_info *p = netdev_priv(dev);
243
244 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000245 case 10000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000246 s = "10Gbps";
247 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000248 case 1000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000249 s = "1000Mbps";
250 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000251 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000252 s = "100Mbps";
253 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000254 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530255 s = "40Gbps";
256 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530257 default:
258 pr_info("%s: unsupported speed: %d\n",
259 dev->name, p->link_cfg.speed);
260 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000261 }
262
263 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
264 fc[p->link_cfg.fc]);
265 }
266}
267
Anish Bhatt688848b2014-06-19 21:37:13 -0700268#ifdef CONFIG_CHELSIO_T4_DCB
269/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
270static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
271{
272 struct port_info *pi = netdev_priv(dev);
273 struct adapter *adap = pi->adapter;
274 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
275 int i;
276
277 /* We use a simple mapping of Port TX Queue Index to DCB
278 * Priority when we're enabling DCB.
279 */
280 for (i = 0; i < pi->nqsets; i++, txq++) {
281 u32 name, value;
282 int err;
283
Hariprasad Shenai51678652014-11-21 12:52:02 +0530284 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
285 FW_PARAMS_PARAM_X_V(
286 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
287 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700288 value = enable ? i : 0xffffffff;
289
290 /* Since we can be called while atomic (from "interrupt
291 * level") we need to issue the Set Parameters Commannd
292 * without sleeping (timeout < 0).
293 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530294 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530295 &name, &value,
296 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700297
298 if (err)
299 dev_err(adap->pdev_dev,
300 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
301 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700302 else
303 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700304 }
305}
306#endif /* CONFIG_CHELSIO_T4_DCB */
307
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530308int cxgb4_dcb_enabled(const struct net_device *dev)
309{
310#ifdef CONFIG_CHELSIO_T4_DCB
311 struct port_info *pi = netdev_priv(dev);
312
313 if (!pi->dcb.enabled)
314 return 0;
315
316 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
317 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
318#else
319 return 0;
320#endif
321}
322EXPORT_SYMBOL(cxgb4_dcb_enabled);
323
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000324void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
325{
326 struct net_device *dev = adapter->port[port_id];
327
328 /* Skip changes from disabled ports. */
329 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
330 if (link_stat)
331 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700332 else {
333#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530334 if (cxgb4_dcb_enabled(dev)) {
335 cxgb4_dcb_state_init(dev);
336 dcb_tx_queue_prio_enable(dev, false);
337 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700338#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000339 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700340 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000341
342 link_report(dev);
343 }
344}
345
346void t4_os_portmod_changed(const struct adapter *adap, int port_id)
347{
348 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000349 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000350 };
351
352 const struct net_device *dev = adap->port[port_id];
353 const struct port_info *pi = netdev_priv(dev);
354
355 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
356 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000357 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000358 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530359 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
360 netdev_info(dev, "%s: unsupported port module inserted\n",
361 dev->name);
362 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
363 netdev_info(dev, "%s: unknown port module inserted\n",
364 dev->name);
365 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
366 netdev_info(dev, "%s: transceiver module error\n", dev->name);
367 else
368 netdev_info(dev, "%s: unknown module type %d inserted\n",
369 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000370}
371
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530372int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
373module_param(dbfifo_int_thresh, int, 0644);
374MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
375
Vipul Pandya404d9e32012-10-08 02:59:43 +0000376/*
377 * usecs to sleep while draining the dbfifo
378 */
379static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530380module_param(dbfifo_drain_delay, int, 0644);
381MODULE_PARM_DESC(dbfifo_drain_delay,
382 "usecs to sleep while draining the dbfifo");
383
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530384static inline int cxgb4_set_addr_hash(struct port_info *pi)
385{
386 struct adapter *adap = pi->adapter;
387 u64 vec = 0;
388 bool ucast = false;
389 struct hash_mac_addr *entry;
390
391 /* Calculate the hash vector for the updated list and program it */
392 list_for_each_entry(entry, &adap->mac_hlist, list) {
393 ucast |= is_unicast_ether_addr(entry->addr);
394 vec |= (1ULL << hash_mac_addr(entry->addr));
395 }
396 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
397 vec, false);
398}
399
400static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
401{
402 struct port_info *pi = netdev_priv(netdev);
403 struct adapter *adap = pi->adapter;
404 int ret;
405 u64 mhash = 0;
406 u64 uhash = 0;
407 bool free = false;
408 bool ucast = is_unicast_ether_addr(mac_addr);
409 const u8 *maclist[1] = {mac_addr};
410 struct hash_mac_addr *new_entry;
411
412 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
413 NULL, ucast ? &uhash : &mhash, false);
414 if (ret < 0)
415 goto out;
416 /* if hash != 0, then add the addr to hash addr list
417 * so on the end we will calculate the hash for the
418 * list and program it
419 */
420 if (uhash || mhash) {
421 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
422 if (!new_entry)
423 return -ENOMEM;
424 ether_addr_copy(new_entry->addr, mac_addr);
425 list_add_tail(&new_entry->list, &adap->mac_hlist);
426 ret = cxgb4_set_addr_hash(pi);
427 }
428out:
429 return ret < 0 ? ret : 0;
430}
431
432static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
433{
434 struct port_info *pi = netdev_priv(netdev);
435 struct adapter *adap = pi->adapter;
436 int ret;
437 const u8 *maclist[1] = {mac_addr};
438 struct hash_mac_addr *entry, *tmp;
439
440 /* If the MAC address to be removed is in the hash addr
441 * list, delete it from the list and update hash vector
442 */
443 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
444 if (ether_addr_equal(entry->addr, mac_addr)) {
445 list_del(&entry->list);
446 kfree(entry);
447 return cxgb4_set_addr_hash(pi);
448 }
449 }
450
451 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
452 return ret < 0 ? -EINVAL : 0;
453}
454
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000455/*
456 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
457 * If @mtu is -1 it is left unchanged.
458 */
459static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
460{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000461 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530462 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000463
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530464 if (!(dev->flags & IFF_PROMISC)) {
465 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
466 if (!(dev->flags & IFF_ALLMULTI))
467 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
468 }
469
470 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
471 (dev->flags & IFF_PROMISC) ? 1 : 0,
472 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
473 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000474}
475
476/**
477 * link_start - enable a port
478 * @dev: the port to enable
479 *
480 * Performs the MAC and PHY actions needed to enable a port.
481 */
482static int link_start(struct net_device *dev)
483{
484 int ret;
485 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530486 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000487
488 /*
489 * We do not set address filters and promiscuity here, the stack does
490 * that step explicitly.
491 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000492 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000493 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000494 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000495 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000496 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000497 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000498 if (ret >= 0) {
499 pi->xact_addr_filt = ret;
500 ret = 0;
501 }
502 }
503 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530504 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000505 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700506 if (ret == 0) {
507 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700508 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
509 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700510 local_bh_enable();
511 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700512
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000513 return ret;
514}
515
Anish Bhatt688848b2014-06-19 21:37:13 -0700516#ifdef CONFIG_CHELSIO_T4_DCB
517/* Handle a Data Center Bridging update message from the firmware. */
518static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
519{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530520 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530521 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700522 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
523 int new_dcb_enabled;
524
525 cxgb4_dcb_handle_fw_update(adap, pcmd);
526 new_dcb_enabled = cxgb4_dcb_enabled(dev);
527
528 /* If the DCB has become enabled or disabled on the port then we're
529 * going to need to set up/tear down DCB Priority parameters for the
530 * TX Queues associated with the port.
531 */
532 if (new_dcb_enabled != old_dcb_enabled)
533 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
534}
535#endif /* CONFIG_CHELSIO_T4_DCB */
536
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000537/* Clear a filter and release any of its resources that we own. This also
538 * clears the filter's "pending" status.
539 */
540static void clear_filter(struct adapter *adap, struct filter_entry *f)
541{
542 /* If the new or old filter have loopback rewriteing rules then we'll
543 * need to free any existing Layer Two Table (L2T) entries of the old
544 * filter rule. The firmware will handle freeing up any Source MAC
545 * Table (SMT) entries used for rewriting Source MAC Addresses in
546 * loopback rules.
547 */
548 if (f->l2t)
549 cxgb4_l2t_release(f->l2t);
550
551 /* The zeroing of the filter rule below clears the filter valid,
552 * pending, locked flags, l2t pointer, etc. so it's all we need for
553 * this operation.
554 */
555 memset(f, 0, sizeof(*f));
556}
557
558/* Handle a filter write/deletion reply.
559 */
560static void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
561{
562 unsigned int idx = GET_TID(rpl);
563 unsigned int nidx = idx - adap->tids.ftid_base;
564 unsigned int ret;
565 struct filter_entry *f;
566
567 if (idx >= adap->tids.ftid_base && nidx <
568 (adap->tids.nftids + adap->tids.nsftids)) {
569 idx = nidx;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800570 ret = TCB_COOKIE_G(rpl->cookie);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000571 f = &adap->tids.ftid_tab[idx];
572
573 if (ret == FW_FILTER_WR_FLT_DELETED) {
574 /* Clear the filter when we get confirmation from the
575 * hardware that the filter has been deleted.
576 */
577 clear_filter(adap, f);
578 } else if (ret == FW_FILTER_WR_SMT_TBL_FULL) {
579 dev_err(adap->pdev_dev, "filter %u setup failed due to full SMT\n",
580 idx);
581 clear_filter(adap, f);
582 } else if (ret == FW_FILTER_WR_FLT_ADDED) {
583 f->smtidx = (be64_to_cpu(rpl->oldval) >> 24) & 0xff;
584 f->pending = 0; /* asynchronous setup completed */
585 f->valid = 1;
586 } else {
587 /* Something went wrong. Issue a warning about the
588 * problem and clear everything out.
589 */
590 dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
591 idx, ret);
592 clear_filter(adap, f);
593 }
594 }
595}
596
597/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000598 */
599static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
600 const struct pkt_gl *gl)
601{
602 u8 opcode = ((const struct rss_header *)rsp)->opcode;
603
604 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000605
606 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
607 */
608 if (unlikely(opcode == CPL_FW4_MSG &&
609 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
610 rsp++;
611 opcode = ((const struct rss_header *)rsp)->opcode;
612 rsp++;
613 if (opcode != CPL_SGE_EGR_UPDATE) {
614 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
615 , opcode);
616 goto out;
617 }
618 }
619
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000620 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
621 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800622 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000623 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000624
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000625 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000626 txq->restarts++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000627 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000628 struct sge_eth_txq *eq;
629
630 eq = container_of(txq, struct sge_eth_txq, q);
631 netif_tx_wake_queue(eq->txq);
632 } else {
633 struct sge_ofld_txq *oq;
634
635 oq = container_of(txq, struct sge_ofld_txq, q);
636 tasklet_schedule(&oq->qresume_tsk);
637 }
638 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
639 const struct cpl_fw6_msg *p = (void *)rsp;
640
Anish Bhatt688848b2014-06-19 21:37:13 -0700641#ifdef CONFIG_CHELSIO_T4_DCB
642 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530643 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700644 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530645 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700646
647 if (cmd == FW_PORT_CMD &&
648 action == FW_PORT_ACTION_GET_PORT_INFO) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530649 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700650 be32_to_cpu(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530651 struct net_device *dev =
652 q->adap->port[q->adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700653 int state_input = ((pcmd->u.info.dcbxdis_pkd &
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530654 FW_PORT_CMD_DCBXDIS_F)
Anish Bhatt688848b2014-06-19 21:37:13 -0700655 ? CXGB4_DCB_INPUT_FW_DISABLED
656 : CXGB4_DCB_INPUT_FW_ENABLED);
657
658 cxgb4_dcb_state_fsm(dev, state_input);
659 }
660
661 if (cmd == FW_PORT_CMD &&
662 action == FW_PORT_ACTION_L2_DCB_CFG)
663 dcb_rpl(q->adap, pcmd);
664 else
665#endif
666 if (p->type == 0)
667 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000668 } else if (opcode == CPL_L2T_WRITE_RPL) {
669 const struct cpl_l2t_write_rpl *p = (void *)rsp;
670
671 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000672 } else if (opcode == CPL_SET_TCB_RPL) {
673 const struct cpl_set_tcb_rpl *p = (void *)rsp;
674
675 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000676 } else
677 dev_err(q->adap->pdev_dev,
678 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000679out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000680 return 0;
681}
682
Varun Prakash2337ba42016-02-14 23:02:41 +0530683/* Flush the aggregated lro sessions */
684static void uldrx_flush_handler(struct sge_rspq *q)
685{
686 if (ulds[q->uld].lro_flush)
687 ulds[q->uld].lro_flush(&q->lro_mgr);
688}
689
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000690/**
691 * uldrx_handler - response queue handler for ULD queues
692 * @q: the response queue that received the packet
693 * @rsp: the response queue descriptor holding the offload message
694 * @gl: the gather list of packet fragments
695 *
696 * Deliver an ingress offload packet to a ULD. All processing is done by
697 * the ULD, we just maintain statistics.
698 */
699static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
700 const struct pkt_gl *gl)
701{
702 struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
Varun Prakash2337ba42016-02-14 23:02:41 +0530703 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000704
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000705 /* FW can send CPLs encapsulated in a CPL_FW4_MSG.
706 */
707 if (((const struct rss_header *)rsp)->opcode == CPL_FW4_MSG &&
708 ((const struct cpl_fw4_msg *)(rsp + 1))->type == FW_TYPE_RSSCPL)
709 rsp += 2;
710
Varun Prakash2337ba42016-02-14 23:02:41 +0530711 if (q->flush_handler)
712 ret = ulds[q->uld].lro_rx_handler(q->adap->uld_handle[q->uld],
713 rsp, gl, &q->lro_mgr,
714 &q->napi);
715 else
716 ret = ulds[q->uld].rx_handler(q->adap->uld_handle[q->uld],
717 rsp, gl);
718
719 if (ret) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000720 rxq->stats.nomem++;
721 return -1;
722 }
Varun Prakash2337ba42016-02-14 23:02:41 +0530723
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000724 if (gl == NULL)
725 rxq->stats.imm++;
726 else if (gl == CXGB4_MSG_AN)
727 rxq->stats.an++;
728 else
729 rxq->stats.pkts++;
730 return 0;
731}
732
733static void disable_msi(struct adapter *adapter)
734{
735 if (adapter->flags & USING_MSIX) {
736 pci_disable_msix(adapter->pdev);
737 adapter->flags &= ~USING_MSIX;
738 } else if (adapter->flags & USING_MSI) {
739 pci_disable_msi(adapter->pdev);
740 adapter->flags &= ~USING_MSI;
741 }
742}
743
744/*
745 * Interrupt handler for non-data events used with MSI-X.
746 */
747static irqreturn_t t4_nondata_intr(int irq, void *cookie)
748{
749 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530750 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000751
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530752 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000753 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530754 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000755 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530756 if (adap->flags & MASTER_PF)
757 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000758 return IRQ_HANDLED;
759}
760
761/*
762 * Name the MSI-X interrupts.
763 */
764static void name_msix_vecs(struct adapter *adap)
765{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000766 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000767
768 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000769 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000770
771 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000772 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
773 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000774
775 /* Ethernet queues */
776 for_each_port(adap, j) {
777 struct net_device *d = adap->port[j];
778 const struct port_info *pi = netdev_priv(d);
779
Dimitris Michailidisba278162010-12-14 21:36:50 +0000780 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000781 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
782 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000783 }
784
785 /* offload queues */
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530786 for_each_iscsirxq(&adap->sge, i)
787 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-iscsi%d",
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000788 adap->port[0]->name, i);
Dimitris Michailidisba278162010-12-14 21:36:50 +0000789
Varun Prakashf2692d12016-02-14 23:02:40 +0530790 for_each_iscsitrxq(&adap->sge, i)
791 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-iSCSIT%d",
792 adap->port[0]->name, i);
793
Dimitris Michailidisba278162010-12-14 21:36:50 +0000794 for_each_rdmarxq(&adap->sge, i)
795 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma%d",
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000796 adap->port[0]->name, i);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530797
798 for_each_rdmaciq(&adap->sge, i)
799 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma-ciq%d",
800 adap->port[0]->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000801}
802
803static int request_msix_queue_irqs(struct adapter *adap)
804{
805 struct sge *s = &adap->sge;
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530806 int err, ethqidx, iscsiqidx = 0, rdmaqidx = 0, rdmaciqqidx = 0;
Varun Prakashf2692d12016-02-14 23:02:40 +0530807 int iscsitqidx = 0;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530808 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809
810 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
811 adap->msix_info[1].desc, &s->fw_evtq);
812 if (err)
813 return err;
814
815 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000816 err = request_irq(adap->msix_info[msi_index].vec,
817 t4_sge_intr_msix, 0,
818 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000819 &s->ethrxq[ethqidx].rspq);
820 if (err)
821 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000822 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000823 }
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530824 for_each_iscsirxq(s, iscsiqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000825 err = request_irq(adap->msix_info[msi_index].vec,
826 t4_sge_intr_msix, 0,
827 adap->msix_info[msi_index].desc,
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530828 &s->iscsirxq[iscsiqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000829 if (err)
830 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000831 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000832 }
Varun Prakashf2692d12016-02-14 23:02:40 +0530833 for_each_iscsitrxq(s, iscsitqidx) {
834 err = request_irq(adap->msix_info[msi_index].vec,
835 t4_sge_intr_msix, 0,
836 adap->msix_info[msi_index].desc,
837 &s->iscsitrxq[iscsitqidx].rspq);
838 if (err)
839 goto unwind;
840 msi_index++;
841 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000842 for_each_rdmarxq(s, rdmaqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000843 err = request_irq(adap->msix_info[msi_index].vec,
844 t4_sge_intr_msix, 0,
845 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000846 &s->rdmarxq[rdmaqidx].rspq);
847 if (err)
848 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000849 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000850 }
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530851 for_each_rdmaciq(s, rdmaciqqidx) {
852 err = request_irq(adap->msix_info[msi_index].vec,
853 t4_sge_intr_msix, 0,
854 adap->msix_info[msi_index].desc,
855 &s->rdmaciq[rdmaciqqidx].rspq);
856 if (err)
857 goto unwind;
858 msi_index++;
859 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000860 return 0;
861
862unwind:
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530863 while (--rdmaciqqidx >= 0)
864 free_irq(adap->msix_info[--msi_index].vec,
865 &s->rdmaciq[rdmaciqqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000866 while (--rdmaqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000867 free_irq(adap->msix_info[--msi_index].vec,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000868 &s->rdmarxq[rdmaqidx].rspq);
Varun Prakashf2692d12016-02-14 23:02:40 +0530869 while (--iscsitqidx >= 0)
870 free_irq(adap->msix_info[--msi_index].vec,
871 &s->iscsitrxq[iscsitqidx].rspq);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530872 while (--iscsiqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000873 free_irq(adap->msix_info[--msi_index].vec,
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530874 &s->iscsirxq[iscsiqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000875 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000876 free_irq(adap->msix_info[--msi_index].vec,
877 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000878 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
879 return err;
880}
881
882static void free_msix_queue_irqs(struct adapter *adap)
883{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000884 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000885 struct sge *s = &adap->sge;
886
887 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
888 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000889 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530890 for_each_iscsirxq(s, i)
891 free_irq(adap->msix_info[msi_index++].vec,
892 &s->iscsirxq[i].rspq);
Varun Prakashf2692d12016-02-14 23:02:40 +0530893 for_each_iscsitrxq(s, i)
894 free_irq(adap->msix_info[msi_index++].vec,
895 &s->iscsitrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000896 for_each_rdmarxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000897 free_irq(adap->msix_info[msi_index++].vec, &s->rdmarxq[i].rspq);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530898 for_each_rdmaciq(s, i)
899 free_irq(adap->msix_info[msi_index++].vec, &s->rdmaciq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000900}
901
902/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530903 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000904 * @pi: the port
905 * @queues: array of queue indices for RSS
906 *
907 * Sets up the portion of the HW RSS table for the port's VI to distribute
908 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530909 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000910 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530911int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000912{
913 u16 *rss;
914 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530915 struct adapter *adapter = pi->adapter;
916 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000917
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530918 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000919 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
920 if (!rss)
921 return -ENOMEM;
922
923 /* map the queue indices to queue ids */
924 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530925 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000926
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530927 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000928 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530929 /* If Tunnel All Lookup isn't specified in the global RSS
930 * Configuration, then we need to specify a default Ingress
931 * Queue for any ingress packets which aren't hashed. We'll
932 * use our first ingress queue ...
933 */
934 if (!err)
935 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
936 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
937 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
938 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
939 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
940 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
941 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000942 kfree(rss);
943 return err;
944}
945
946/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000947 * setup_rss - configure RSS
948 * @adap: the adapter
949 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000950 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000951 */
952static int setup_rss(struct adapter *adap)
953{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530954 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000955
956 for_each_port(adap, i) {
957 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000958
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530959 /* Fill default values with equal distribution */
960 for (j = 0; j < pi->rss_size; j++)
961 pi->rss[j] = j % pi->nqsets;
962
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530963 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000964 if (err)
965 return err;
966 }
967 return 0;
968}
969
970/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000971 * Return the channel of the ingress queue with the given qid.
972 */
973static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
974{
975 qid -= p->ingr_start;
976 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
977}
978
979/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000980 * Wait until all NAPI handlers are descheduled.
981 */
982static void quiesce_rx(struct adapter *adap)
983{
984 int i;
985
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530986 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000987 struct sge_rspq *q = adap->sge.ingr_map[i];
988
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530989 if (q && q->handler) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000990 napi_disable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530991 local_bh_disable();
992 while (!cxgb_poll_lock_napi(q))
993 mdelay(1);
994 local_bh_enable();
995 }
996
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000997 }
998}
999
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05301000/* Disable interrupt and napi handler */
1001static void disable_interrupts(struct adapter *adap)
1002{
1003 if (adap->flags & FULL_INIT_DONE) {
1004 t4_intr_disable(adap);
1005 if (adap->flags & USING_MSIX) {
1006 free_msix_queue_irqs(adap);
1007 free_irq(adap->msix_info[0].vec, adap);
1008 } else {
1009 free_irq(adap->pdev->irq, adap);
1010 }
1011 quiesce_rx(adap);
1012 }
1013}
1014
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001015/*
1016 * Enable NAPI scheduling and interrupt generation for all Rx queues.
1017 */
1018static void enable_rx(struct adapter *adap)
1019{
1020 int i;
1021
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301022 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001023 struct sge_rspq *q = adap->sge.ingr_map[i];
1024
1025 if (!q)
1026 continue;
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301027 if (q->handler) {
1028 cxgb_busy_poll_init_lock(q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001029 napi_enable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301030 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001031 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301032 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
1033 SEINTARM_V(q->intr_params) |
1034 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001035 }
1036}
1037
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301038static int alloc_ofld_rxqs(struct adapter *adap, struct sge_ofld_rxq *q,
1039 unsigned int nq, unsigned int per_chan, int msi_idx,
Varun Prakash2337ba42016-02-14 23:02:41 +05301040 u16 *ids, bool lro)
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301041{
1042 int i, err;
1043
1044 for (i = 0; i < nq; i++, q++) {
1045 if (msi_idx > 0)
1046 msi_idx++;
1047 err = t4_sge_alloc_rxq(adap, &q->rspq, false,
1048 adap->port[i / per_chan],
1049 msi_idx, q->fl.size ? &q->fl : NULL,
Varun Prakash2337ba42016-02-14 23:02:41 +05301050 uldrx_handler,
1051 lro ? uldrx_flush_handler : NULL,
1052 0);
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301053 if (err)
1054 return err;
1055 memset(&q->stats, 0, sizeof(q->stats));
1056 if (ids)
1057 ids[i] = q->rspq.abs_id;
1058 }
1059 return 0;
1060}
1061
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001062/**
1063 * setup_sge_queues - configure SGE Tx/Rx/response queues
1064 * @adap: the adapter
1065 *
1066 * Determines how many sets of SGE queues to use and initializes them.
1067 * We support multiple queue sets per port if we have MSI-X, otherwise
1068 * just one queue set per port.
1069 */
1070static int setup_sge_queues(struct adapter *adap)
1071{
1072 int err, msi_idx, i, j;
1073 struct sge *s = &adap->sge;
1074
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301075 bitmap_zero(s->starving_fl, s->egr_sz);
1076 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001077
1078 if (adap->flags & USING_MSIX)
1079 msi_idx = 1; /* vector 0 is for non-queue interrupts */
1080 else {
1081 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +05301082 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001083 if (err)
1084 return err;
1085 msi_idx = -((int)s->intrq.abs_id + 1);
1086 }
1087
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301088 /* NOTE: If you add/delete any Ingress/Egress Queue allocations in here,
1089 * don't forget to update the following which need to be
1090 * synchronized to and changes here.
1091 *
1092 * 1. The calculations of MAX_INGQ in cxgb4.h.
1093 *
1094 * 2. Update enable_msix/name_msix_vecs/request_msix_queue_irqs
1095 * to accommodate any new/deleted Ingress Queues
1096 * which need MSI-X Vectors.
1097 *
1098 * 3. Update sge_qinfo_show() to include information on the
1099 * new/deleted queues.
1100 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001101 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Varun Prakash2337ba42016-02-14 23:02:41 +05301102 msi_idx, NULL, fwevtq_handler, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001103 if (err) {
1104freeout: t4_free_sge_resources(adap);
1105 return err;
1106 }
1107
1108 for_each_port(adap, i) {
1109 struct net_device *dev = adap->port[i];
1110 struct port_info *pi = netdev_priv(dev);
1111 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
1112 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
1113
1114 for (j = 0; j < pi->nqsets; j++, q++) {
1115 if (msi_idx > 0)
1116 msi_idx++;
1117 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
1118 msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05301119 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +05301120 NULL,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05301121 t4_get_mps_bg_map(adap,
1122 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001123 if (err)
1124 goto freeout;
1125 q->rspq.idx = j;
1126 memset(&q->stats, 0, sizeof(q->stats));
1127 }
1128 for (j = 0; j < pi->nqsets; j++, t++) {
1129 err = t4_sge_alloc_eth_txq(adap, t, dev,
1130 netdev_get_tx_queue(dev, j),
1131 s->fw_evtq.cntxt_id);
1132 if (err)
1133 goto freeout;
1134 }
1135 }
1136
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05301137 j = s->iscsiqsets / adap->params.nports; /* iscsi queues per channel */
1138 for_each_iscsirxq(s, i) {
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301139 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
1140 adap->port[i / j],
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001141 s->fw_evtq.cntxt_id);
1142 if (err)
1143 goto freeout;
1144 }
1145
Varun Prakash2337ba42016-02-14 23:02:41 +05301146#define ALLOC_OFLD_RXQS(firstq, nq, per_chan, ids, lro) do { \
1147 err = alloc_ofld_rxqs(adap, firstq, nq, per_chan, msi_idx, ids, lro); \
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301148 if (err) \
1149 goto freeout; \
1150 if (msi_idx > 0) \
1151 msi_idx += nq; \
1152} while (0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001153
Varun Prakash2337ba42016-02-14 23:02:41 +05301154 ALLOC_OFLD_RXQS(s->iscsirxq, s->iscsiqsets, j, s->iscsi_rxq, false);
1155 ALLOC_OFLD_RXQS(s->iscsitrxq, s->niscsitq, j, s->iscsit_rxq, true);
1156 ALLOC_OFLD_RXQS(s->rdmarxq, s->rdmaqs, 1, s->rdma_rxq, false);
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05301157 j = s->rdmaciqs / adap->params.nports; /* rdmaq queues per channel */
Varun Prakash2337ba42016-02-14 23:02:41 +05301158 ALLOC_OFLD_RXQS(s->rdmaciq, s->rdmaciqs, j, s->rdma_ciq, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001159
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301160#undef ALLOC_OFLD_RXQS
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05301161
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001162 for_each_port(adap, i) {
1163 /*
1164 * Note that ->rdmarxq[i].rspq.cntxt_id below is 0 if we don't
1165 * have RDMA queues, and that's the right value.
1166 */
1167 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
1168 s->fw_evtq.cntxt_id,
1169 s->rdmarxq[i].rspq.cntxt_id);
1170 if (err)
1171 goto freeout;
1172 }
1173
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +05301174 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05301175 MPS_TRC_RSS_CONTROL_A :
1176 MPS_T5_TRC_RSS_CONTROL_A,
1177 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
1178 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001179 return 0;
1180}
1181
1182/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001183 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1184 * The allocated memory is cleared.
1185 */
1186void *t4_alloc_mem(size_t size)
1187{
Joe Perches8be04b92013-06-19 12:15:53 -07001188 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001189
1190 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001191 p = vzalloc(size);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001192 return p;
1193}
1194
1195/*
1196 * Free memory allocated through alloc_mem().
1197 */
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301198void t4_free_mem(void *addr)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001199{
Pekka Enbergd2fcb542015-06-30 14:59:12 -07001200 kvfree(addr);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001201}
1202
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001203/* Send a Work Request to write the filter at a specified index. We construct
1204 * a Firmware Filter Work Request to have the work done and put the indicated
1205 * filter into "pending" mode which will prevent any further actions against
1206 * it till we get a reply from the firmware on the completion status of the
1207 * request.
1208 */
1209static int set_filter_wr(struct adapter *adapter, int fidx)
1210{
1211 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1212 struct sk_buff *skb;
1213 struct fw_filter_wr *fwr;
1214 unsigned int ftid;
1215
Michal Hockof72f1162015-04-14 13:24:33 -07001216 skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
1217 if (!skb)
1218 return -ENOMEM;
1219
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001220 /* If the new filter requires loopback Destination MAC and/or VLAN
1221 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1222 * the filter.
1223 */
1224 if (f->fs.newdmac || f->fs.newvlan) {
1225 /* allocate L2T entry for new filter */
Hariprasad Shenaif7502652015-12-17 13:45:08 +05301226 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
1227 f->fs.eport, f->fs.dmac);
Michal Hockof72f1162015-04-14 13:24:33 -07001228 if (f->l2t == NULL) {
1229 kfree_skb(skb);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001230 return -ENOMEM;
1231 }
1232 }
1233
1234 ftid = adapter->tids.ftid_base + fidx;
1235
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001236 fwr = (struct fw_filter_wr *)__skb_put(skb, sizeof(*fwr));
1237 memset(fwr, 0, sizeof(*fwr));
1238
1239 /* It would be nice to put most of the following in t4_hw.c but most
1240 * of the work is translating the cxgbtool ch_filter_specification
1241 * into the Work Request and the definition of that structure is
1242 * currently in cxgbtool.h which isn't appropriate to pull into the
1243 * common code. We may eventually try to come up with a more neutral
1244 * filter specification structure but for now it's easiest to simply
1245 * put this fairly direct code in line ...
1246 */
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301247 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
1248 fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr)/16));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001249 fwr->tid_to_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301250 htonl(FW_FILTER_WR_TID_V(ftid) |
1251 FW_FILTER_WR_RQTYPE_V(f->fs.type) |
1252 FW_FILTER_WR_NOREPLY_V(0) |
1253 FW_FILTER_WR_IQ_V(f->fs.iq));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001254 fwr->del_filter_to_l2tix =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301255 htonl(FW_FILTER_WR_RPTTID_V(f->fs.rpttid) |
1256 FW_FILTER_WR_DROP_V(f->fs.action == FILTER_DROP) |
1257 FW_FILTER_WR_DIRSTEER_V(f->fs.dirsteer) |
1258 FW_FILTER_WR_MASKHASH_V(f->fs.maskhash) |
1259 FW_FILTER_WR_DIRSTEERHASH_V(f->fs.dirsteerhash) |
1260 FW_FILTER_WR_LPBK_V(f->fs.action == FILTER_SWITCH) |
1261 FW_FILTER_WR_DMAC_V(f->fs.newdmac) |
1262 FW_FILTER_WR_SMAC_V(f->fs.newsmac) |
1263 FW_FILTER_WR_INSVLAN_V(f->fs.newvlan == VLAN_INSERT ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001264 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301265 FW_FILTER_WR_RMVLAN_V(f->fs.newvlan == VLAN_REMOVE ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001266 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301267 FW_FILTER_WR_HITCNTS_V(f->fs.hitcnts) |
1268 FW_FILTER_WR_TXCHAN_V(f->fs.eport) |
1269 FW_FILTER_WR_PRIO_V(f->fs.prio) |
1270 FW_FILTER_WR_L2TIX_V(f->l2t ? f->l2t->idx : 0));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001271 fwr->ethtype = htons(f->fs.val.ethtype);
1272 fwr->ethtypem = htons(f->fs.mask.ethtype);
1273 fwr->frag_to_ovlan_vldm =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301274 (FW_FILTER_WR_FRAG_V(f->fs.val.frag) |
1275 FW_FILTER_WR_FRAGM_V(f->fs.mask.frag) |
1276 FW_FILTER_WR_IVLAN_VLD_V(f->fs.val.ivlan_vld) |
1277 FW_FILTER_WR_OVLAN_VLD_V(f->fs.val.ovlan_vld) |
1278 FW_FILTER_WR_IVLAN_VLDM_V(f->fs.mask.ivlan_vld) |
1279 FW_FILTER_WR_OVLAN_VLDM_V(f->fs.mask.ovlan_vld));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001280 fwr->smac_sel = 0;
1281 fwr->rx_chan_rx_rpl_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301282 htons(FW_FILTER_WR_RX_CHAN_V(0) |
1283 FW_FILTER_WR_RX_RPL_IQ_V(adapter->sge.fw_evtq.abs_id));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001284 fwr->maci_to_matchtypem =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301285 htonl(FW_FILTER_WR_MACI_V(f->fs.val.macidx) |
1286 FW_FILTER_WR_MACIM_V(f->fs.mask.macidx) |
1287 FW_FILTER_WR_FCOE_V(f->fs.val.fcoe) |
1288 FW_FILTER_WR_FCOEM_V(f->fs.mask.fcoe) |
1289 FW_FILTER_WR_PORT_V(f->fs.val.iport) |
1290 FW_FILTER_WR_PORTM_V(f->fs.mask.iport) |
1291 FW_FILTER_WR_MATCHTYPE_V(f->fs.val.matchtype) |
1292 FW_FILTER_WR_MATCHTYPEM_V(f->fs.mask.matchtype));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001293 fwr->ptcl = f->fs.val.proto;
1294 fwr->ptclm = f->fs.mask.proto;
1295 fwr->ttyp = f->fs.val.tos;
1296 fwr->ttypm = f->fs.mask.tos;
1297 fwr->ivlan = htons(f->fs.val.ivlan);
1298 fwr->ivlanm = htons(f->fs.mask.ivlan);
1299 fwr->ovlan = htons(f->fs.val.ovlan);
1300 fwr->ovlanm = htons(f->fs.mask.ovlan);
1301 memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
1302 memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
1303 memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
1304 memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
1305 fwr->lp = htons(f->fs.val.lport);
1306 fwr->lpm = htons(f->fs.mask.lport);
1307 fwr->fp = htons(f->fs.val.fport);
1308 fwr->fpm = htons(f->fs.mask.fport);
1309 if (f->fs.newsmac)
1310 memcpy(fwr->sma, f->fs.smac, sizeof(fwr->sma));
1311
1312 /* Mark the filter as "pending" and ship off the Filter Work Request.
1313 * When we get the Work Request Reply we'll clear the pending status.
1314 */
1315 f->pending = 1;
1316 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
1317 t4_ofld_send(adapter, skb);
1318 return 0;
1319}
1320
1321/* Delete the filter at a specified index.
1322 */
1323static int del_filter_wr(struct adapter *adapter, int fidx)
1324{
1325 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1326 struct sk_buff *skb;
1327 struct fw_filter_wr *fwr;
1328 unsigned int len, ftid;
1329
1330 len = sizeof(*fwr);
1331 ftid = adapter->tids.ftid_base + fidx;
1332
Michal Hockof72f1162015-04-14 13:24:33 -07001333 skb = alloc_skb(len, GFP_KERNEL);
1334 if (!skb)
1335 return -ENOMEM;
1336
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001337 fwr = (struct fw_filter_wr *)__skb_put(skb, len);
1338 t4_mk_filtdelwr(ftid, fwr, adapter->sge.fw_evtq.abs_id);
1339
1340 /* Mark the filter as "pending" and ship off the Filter Work Request.
1341 * When we get the Work Request Reply we'll clear the pending status.
1342 */
1343 f->pending = 1;
1344 t4_mgmt_tx(adapter, skb);
1345 return 0;
1346}
1347
Anish Bhatt688848b2014-06-19 21:37:13 -07001348static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
1349 void *accel_priv, select_queue_fallback_t fallback)
1350{
1351 int txq;
1352
1353#ifdef CONFIG_CHELSIO_T4_DCB
1354 /* If a Data Center Bridging has been successfully negotiated on this
1355 * link then we'll use the skb's priority to map it to a TX Queue.
1356 * The skb's priority is determined via the VLAN Tag Priority Code
1357 * Point field.
1358 */
1359 if (cxgb4_dcb_enabled(dev)) {
1360 u16 vlan_tci;
1361 int err;
1362
1363 err = vlan_get_tag(skb, &vlan_tci);
1364 if (unlikely(err)) {
1365 if (net_ratelimit())
1366 netdev_warn(dev,
1367 "TX Packet without VLAN Tag on DCB Link\n");
1368 txq = 0;
1369 } else {
1370 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +05301371#ifdef CONFIG_CHELSIO_T4_FCOE
1372 if (skb->protocol == htons(ETH_P_FCOE))
1373 txq = skb->priority & 0x7;
1374#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -07001375 }
1376 return txq;
1377 }
1378#endif /* CONFIG_CHELSIO_T4_DCB */
1379
1380 if (select_queue) {
1381 txq = (skb_rx_queue_recorded(skb)
1382 ? skb_get_rx_queue(skb)
1383 : smp_processor_id());
1384
1385 while (unlikely(txq >= dev->real_num_tx_queues))
1386 txq -= dev->real_num_tx_queues;
1387
1388 return txq;
1389 }
1390
1391 return fallback(dev, skb) % dev->real_num_tx_queues;
1392}
1393
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001394static int closest_timer(const struct sge *s, int time)
1395{
1396 int i, delta, match = 0, min_delta = INT_MAX;
1397
1398 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
1399 delta = time - s->timer_val[i];
1400 if (delta < 0)
1401 delta = -delta;
1402 if (delta < min_delta) {
1403 min_delta = delta;
1404 match = i;
1405 }
1406 }
1407 return match;
1408}
1409
1410static int closest_thres(const struct sge *s, int thres)
1411{
1412 int i, delta, match = 0, min_delta = INT_MAX;
1413
1414 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1415 delta = thres - s->counter_val[i];
1416 if (delta < 0)
1417 delta = -delta;
1418 if (delta < min_delta) {
1419 min_delta = delta;
1420 match = i;
1421 }
1422 }
1423 return match;
1424}
1425
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001426/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301427 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001428 * @q: the Rx queue
1429 * @us: the hold-off time in us, or 0 to disable timer
1430 * @cnt: the hold-off packet count, or 0 to disable counter
1431 *
1432 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1433 * one of the two needs to be enabled for the queue to generate interrupts.
1434 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301435int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1436 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001437{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301438 struct adapter *adap = q->adap;
1439
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001440 if ((us | cnt) == 0)
1441 cnt = 1;
1442
1443 if (cnt) {
1444 int err;
1445 u32 v, new_idx;
1446
1447 new_idx = closest_thres(&adap->sge, cnt);
1448 if (q->desc && q->pktcnt_idx != new_idx) {
1449 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301450 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1451 FW_PARAMS_PARAM_X_V(
1452 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1453 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301454 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1455 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001456 if (err)
1457 return err;
1458 }
1459 q->pktcnt_idx = new_idx;
1460 }
1461
1462 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301463 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001464 return 0;
1465}
1466
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001467static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001468{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001469 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001470 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001471 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001472
Patrick McHardyf6469682013-04-19 02:04:27 +00001473 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001474 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001475
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301476 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001477 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001478 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001479 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001480 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001481 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001482}
1483
Bill Pemberton91744942012-12-03 09:23:02 -05001484static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001485{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001486 if (IS_ERR_OR_NULL(adap->debugfs_root))
1487 return -1;
1488
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301489#ifdef CONFIG_DEBUG_FS
1490 t4_setup_debugfs(adap);
1491#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001492 return 0;
1493}
1494
1495/*
1496 * upper-layer driver support
1497 */
1498
1499/*
1500 * Allocate an active-open TID and set it to the supplied value.
1501 */
1502int cxgb4_alloc_atid(struct tid_info *t, void *data)
1503{
1504 int atid = -1;
1505
1506 spin_lock_bh(&t->atid_lock);
1507 if (t->afree) {
1508 union aopen_entry *p = t->afree;
1509
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001510 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001511 t->afree = p->next;
1512 p->data = data;
1513 t->atids_in_use++;
1514 }
1515 spin_unlock_bh(&t->atid_lock);
1516 return atid;
1517}
1518EXPORT_SYMBOL(cxgb4_alloc_atid);
1519
1520/*
1521 * Release an active-open TID.
1522 */
1523void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1524{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001525 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001526
1527 spin_lock_bh(&t->atid_lock);
1528 p->next = t->afree;
1529 t->afree = p;
1530 t->atids_in_use--;
1531 spin_unlock_bh(&t->atid_lock);
1532}
1533EXPORT_SYMBOL(cxgb4_free_atid);
1534
1535/*
1536 * Allocate a server TID and set it to the supplied value.
1537 */
1538int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1539{
1540 int stid;
1541
1542 spin_lock_bh(&t->stid_lock);
1543 if (family == PF_INET) {
1544 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1545 if (stid < t->nstids)
1546 __set_bit(stid, t->stid_bmap);
1547 else
1548 stid = -1;
1549 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301550 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001551 if (stid < 0)
1552 stid = -1;
1553 }
1554 if (stid >= 0) {
1555 t->stid_tab[stid].data = data;
1556 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301557 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1558 * This is equivalent to 4 TIDs. With CLIP enabled it
1559 * needs 2 TIDs.
1560 */
1561 if (family == PF_INET)
1562 t->stids_in_use++;
1563 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301564 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001565 }
1566 spin_unlock_bh(&t->stid_lock);
1567 return stid;
1568}
1569EXPORT_SYMBOL(cxgb4_alloc_stid);
1570
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001571/* Allocate a server filter TID and set it to the supplied value.
1572 */
1573int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1574{
1575 int stid;
1576
1577 spin_lock_bh(&t->stid_lock);
1578 if (family == PF_INET) {
1579 stid = find_next_zero_bit(t->stid_bmap,
1580 t->nstids + t->nsftids, t->nstids);
1581 if (stid < (t->nstids + t->nsftids))
1582 __set_bit(stid, t->stid_bmap);
1583 else
1584 stid = -1;
1585 } else {
1586 stid = -1;
1587 }
1588 if (stid >= 0) {
1589 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301590 stid -= t->nstids;
1591 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301592 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001593 }
1594 spin_unlock_bh(&t->stid_lock);
1595 return stid;
1596}
1597EXPORT_SYMBOL(cxgb4_alloc_sftid);
1598
1599/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001600 */
1601void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1602{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301603 /* Is it a server filter TID? */
1604 if (t->nsftids && (stid >= t->sftid_base)) {
1605 stid -= t->sftid_base;
1606 stid += t->nstids;
1607 } else {
1608 stid -= t->stid_base;
1609 }
1610
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001611 spin_lock_bh(&t->stid_lock);
1612 if (family == PF_INET)
1613 __clear_bit(stid, t->stid_bmap);
1614 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301615 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001616 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301617 if (stid < t->nstids) {
1618 if (family == PF_INET)
1619 t->stids_in_use--;
1620 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301621 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301622 } else {
1623 t->sftids_in_use--;
1624 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001625 spin_unlock_bh(&t->stid_lock);
1626}
1627EXPORT_SYMBOL(cxgb4_free_stid);
1628
1629/*
1630 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1631 */
1632static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1633 unsigned int tid)
1634{
1635 struct cpl_tid_release *req;
1636
1637 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1638 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1639 INIT_TP_WR(req, tid);
1640 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1641}
1642
1643/*
1644 * Queue a TID release request and if necessary schedule a work queue to
1645 * process it.
1646 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001647static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1648 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001649{
1650 void **p = &t->tid_tab[tid];
1651 struct adapter *adap = container_of(t, struct adapter, tids);
1652
1653 spin_lock_bh(&adap->tid_release_lock);
1654 *p = adap->tid_release_head;
1655 /* Low 2 bits encode the Tx channel number */
1656 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1657 if (!adap->tid_release_task_busy) {
1658 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001659 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001660 }
1661 spin_unlock_bh(&adap->tid_release_lock);
1662}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001663
1664/*
1665 * Process the list of pending TID release requests.
1666 */
1667static void process_tid_release_list(struct work_struct *work)
1668{
1669 struct sk_buff *skb;
1670 struct adapter *adap;
1671
1672 adap = container_of(work, struct adapter, tid_release_task);
1673
1674 spin_lock_bh(&adap->tid_release_lock);
1675 while (adap->tid_release_head) {
1676 void **p = adap->tid_release_head;
1677 unsigned int chan = (uintptr_t)p & 3;
1678 p = (void *)p - chan;
1679
1680 adap->tid_release_head = *p;
1681 *p = NULL;
1682 spin_unlock_bh(&adap->tid_release_lock);
1683
1684 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1685 GFP_KERNEL)))
1686 schedule_timeout_uninterruptible(1);
1687
1688 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1689 t4_ofld_send(adap, skb);
1690 spin_lock_bh(&adap->tid_release_lock);
1691 }
1692 adap->tid_release_task_busy = false;
1693 spin_unlock_bh(&adap->tid_release_lock);
1694}
1695
1696/*
1697 * Release a TID and inform HW. If we are unable to allocate the release
1698 * message we defer to a work queue.
1699 */
1700void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1701{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001702 struct sk_buff *skb;
1703 struct adapter *adap = container_of(t, struct adapter, tids);
1704
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301705 WARN_ON(tid >= t->ntids);
1706
1707 if (t->tid_tab[tid]) {
1708 t->tid_tab[tid] = NULL;
1709 if (t->hash_base && (tid >= t->hash_base))
1710 atomic_dec(&t->hash_tids_in_use);
1711 else
1712 atomic_dec(&t->tids_in_use);
1713 }
1714
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001715 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1716 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001717 mk_tid_release(skb, chan, tid);
1718 t4_ofld_send(adap, skb);
1719 } else
1720 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001721}
1722EXPORT_SYMBOL(cxgb4_remove_tid);
1723
1724/*
1725 * Allocate and initialize the TID tables. Returns 0 on success.
1726 */
1727static int tid_init(struct tid_info *t)
1728{
1729 size_t size;
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001730 unsigned int stid_bmap_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001731 unsigned int natids = t->natids;
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301732 struct adapter *adap = container_of(t, struct adapter, tids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001733
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001734 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001735 size = t->ntids * sizeof(*t->tid_tab) +
1736 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001737 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001738 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001739 stid_bmap_size * sizeof(long) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001740 t->nftids * sizeof(*t->ftid_tab) +
1741 t->nsftids * sizeof(*t->ftid_tab);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001742
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001743 t->tid_tab = t4_alloc_mem(size);
1744 if (!t->tid_tab)
1745 return -ENOMEM;
1746
1747 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1748 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001749 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001750 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001751 spin_lock_init(&t->stid_lock);
1752 spin_lock_init(&t->atid_lock);
1753
1754 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301755 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001756 t->afree = NULL;
1757 t->atids_in_use = 0;
1758 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301759 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001760
1761 /* Setup the free list for atid_tab and clear the stid bitmap. */
1762 if (natids) {
1763 while (--natids)
1764 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1765 t->afree = t->atid_tab;
1766 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001767 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301768 /* Reserve stid 0 for T4/T5 adapters */
1769 if (!t->stid_base &&
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301770 (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5))
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301771 __set_bit(0, t->stid_bmap);
1772
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001773 return 0;
1774}
1775
1776/**
1777 * cxgb4_create_server - create an IP server
1778 * @dev: the device
1779 * @stid: the server TID
1780 * @sip: local IP address to bind server to
1781 * @sport: the server's TCP port
1782 * @queue: queue to direct messages from this server to
1783 *
1784 * Create an IP server for the given port and address.
1785 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1786 */
1787int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001788 __be32 sip, __be16 sport, __be16 vlan,
1789 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001790{
1791 unsigned int chan;
1792 struct sk_buff *skb;
1793 struct adapter *adap;
1794 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301795 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001796
1797 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1798 if (!skb)
1799 return -ENOMEM;
1800
1801 adap = netdev2adap(dev);
1802 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1803 INIT_TP_WR(req, 0);
1804 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1805 req->local_port = sport;
1806 req->peer_port = htons(0);
1807 req->local_ip = sip;
1808 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001809 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001810 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001811 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1812 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301813 ret = t4_mgmt_tx(adap, skb);
1814 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001815}
1816EXPORT_SYMBOL(cxgb4_create_server);
1817
Vipul Pandya80f40c12013-07-04 16:10:45 +05301818/* cxgb4_create_server6 - create an IPv6 server
1819 * @dev: the device
1820 * @stid: the server TID
1821 * @sip: local IPv6 address to bind server to
1822 * @sport: the server's TCP port
1823 * @queue: queue to direct messages from this server to
1824 *
1825 * Create an IPv6 server for the given port and address.
1826 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1827 */
1828int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1829 const struct in6_addr *sip, __be16 sport,
1830 unsigned int queue)
1831{
1832 unsigned int chan;
1833 struct sk_buff *skb;
1834 struct adapter *adap;
1835 struct cpl_pass_open_req6 *req;
1836 int ret;
1837
1838 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1839 if (!skb)
1840 return -ENOMEM;
1841
1842 adap = netdev2adap(dev);
1843 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1844 INIT_TP_WR(req, 0);
1845 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1846 req->local_port = sport;
1847 req->peer_port = htons(0);
1848 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1849 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1850 req->peer_ip_hi = cpu_to_be64(0);
1851 req->peer_ip_lo = cpu_to_be64(0);
1852 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001853 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001854 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1855 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301856 ret = t4_mgmt_tx(adap, skb);
1857 return net_xmit_eval(ret);
1858}
1859EXPORT_SYMBOL(cxgb4_create_server6);
1860
1861int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1862 unsigned int queue, bool ipv6)
1863{
1864 struct sk_buff *skb;
1865 struct adapter *adap;
1866 struct cpl_close_listsvr_req *req;
1867 int ret;
1868
1869 adap = netdev2adap(dev);
1870
1871 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1872 if (!skb)
1873 return -ENOMEM;
1874
1875 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1876 INIT_TP_WR(req, 0);
1877 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001878 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1879 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301880 ret = t4_mgmt_tx(adap, skb);
1881 return net_xmit_eval(ret);
1882}
1883EXPORT_SYMBOL(cxgb4_remove_server);
1884
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001885/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001886 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1887 * @mtus: the HW MTU table
1888 * @mtu: the target MTU
1889 * @idx: index of selected entry in the MTU table
1890 *
1891 * Returns the index and the value in the HW MTU table that is closest to
1892 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1893 * table, in which case that smallest available value is selected.
1894 */
1895unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1896 unsigned int *idx)
1897{
1898 unsigned int i = 0;
1899
1900 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1901 ++i;
1902 if (idx)
1903 *idx = i;
1904 return mtus[i];
1905}
1906EXPORT_SYMBOL(cxgb4_best_mtu);
1907
1908/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301909 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1910 * @mtus: the HW MTU table
1911 * @header_size: Header Size
1912 * @data_size_max: maximum Data Segment Size
1913 * @data_size_align: desired Data Segment Size Alignment (2^N)
1914 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1915 *
1916 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1917 * MTU Table based solely on a Maximum MTU parameter, we break that
1918 * parameter up into a Header Size and Maximum Data Segment Size, and
1919 * provide a desired Data Segment Size Alignment. If we find an MTU in
1920 * the Hardware MTU Table which will result in a Data Segment Size with
1921 * the requested alignment _and_ that MTU isn't "too far" from the
1922 * closest MTU, then we'll return that rather than the closest MTU.
1923 */
1924unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1925 unsigned short header_size,
1926 unsigned short data_size_max,
1927 unsigned short data_size_align,
1928 unsigned int *mtu_idxp)
1929{
1930 unsigned short max_mtu = header_size + data_size_max;
1931 unsigned short data_size_align_mask = data_size_align - 1;
1932 int mtu_idx, aligned_mtu_idx;
1933
1934 /* Scan the MTU Table till we find an MTU which is larger than our
1935 * Maximum MTU or we reach the end of the table. Along the way,
1936 * record the last MTU found, if any, which will result in a Data
1937 * Segment Length matching the requested alignment.
1938 */
1939 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1940 unsigned short data_size = mtus[mtu_idx] - header_size;
1941
1942 /* If this MTU minus the Header Size would result in a
1943 * Data Segment Size of the desired alignment, remember it.
1944 */
1945 if ((data_size & data_size_align_mask) == 0)
1946 aligned_mtu_idx = mtu_idx;
1947
1948 /* If we're not at the end of the Hardware MTU Table and the
1949 * next element is larger than our Maximum MTU, drop out of
1950 * the loop.
1951 */
1952 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1953 break;
1954 }
1955
1956 /* If we fell out of the loop because we ran to the end of the table,
1957 * then we just have to use the last [largest] entry.
1958 */
1959 if (mtu_idx == NMTUS)
1960 mtu_idx--;
1961
1962 /* If we found an MTU which resulted in the requested Data Segment
1963 * Length alignment and that's "not far" from the largest MTU which is
1964 * less than or equal to the maximum MTU, then use that.
1965 */
1966 if (aligned_mtu_idx >= 0 &&
1967 mtu_idx - aligned_mtu_idx <= 1)
1968 mtu_idx = aligned_mtu_idx;
1969
1970 /* If the caller has passed in an MTU Index pointer, pass the
1971 * MTU Index back. Return the MTU value.
1972 */
1973 if (mtu_idxp)
1974 *mtu_idxp = mtu_idx;
1975 return mtus[mtu_idx];
1976}
1977EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1978
1979/**
Hariprasad S27999802015-09-23 17:19:26 +05301980 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1981 * @chip: chip type
1982 * @viid: VI id of the given port
1983 *
1984 * Return the SMT index for this VI.
1985 */
1986unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1987{
1988 /* In T4/T5, SMT contains 256 SMAC entries organized in
1989 * 128 rows of 2 entries each.
1990 * In T6, SMT contains 256 SMAC entries in 256 rows.
1991 * TODO: The below code needs to be updated when we add support
1992 * for 256 VFs.
1993 */
1994 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1995 return ((viid & 0x7f) << 1);
1996 else
1997 return (viid & 0x7f);
1998}
1999EXPORT_SYMBOL(cxgb4_tp_smt_idx);
2000
2001/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002002 * cxgb4_port_chan - get the HW channel of a port
2003 * @dev: the net device for the port
2004 *
2005 * Return the HW Tx channel of the given port.
2006 */
2007unsigned int cxgb4_port_chan(const struct net_device *dev)
2008{
2009 return netdev2pinfo(dev)->tx_chan;
2010}
2011EXPORT_SYMBOL(cxgb4_port_chan);
2012
Vipul Pandya881806b2012-05-18 15:29:24 +05302013unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
2014{
2015 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002016 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05302017
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302018 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2019 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302020 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302021 lp_count = LP_COUNT_G(v1);
2022 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002023 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302024 lp_count = LP_COUNT_T5_G(v1);
2025 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002026 }
2027 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05302028}
2029EXPORT_SYMBOL(cxgb4_dbfifo_count);
2030
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002031/**
2032 * cxgb4_port_viid - get the VI id of a port
2033 * @dev: the net device for the port
2034 *
2035 * Return the VI id of the given port.
2036 */
2037unsigned int cxgb4_port_viid(const struct net_device *dev)
2038{
2039 return netdev2pinfo(dev)->viid;
2040}
2041EXPORT_SYMBOL(cxgb4_port_viid);
2042
2043/**
2044 * cxgb4_port_idx - get the index of a port
2045 * @dev: the net device for the port
2046 *
2047 * Return the index of the given port.
2048 */
2049unsigned int cxgb4_port_idx(const struct net_device *dev)
2050{
2051 return netdev2pinfo(dev)->port_id;
2052}
2053EXPORT_SYMBOL(cxgb4_port_idx);
2054
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002055void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
2056 struct tp_tcp_stats *v6)
2057{
2058 struct adapter *adap = pci_get_drvdata(pdev);
2059
2060 spin_lock(&adap->stats_lock);
2061 t4_tp_get_tcp_stats(adap, v4, v6);
2062 spin_unlock(&adap->stats_lock);
2063}
2064EXPORT_SYMBOL(cxgb4_get_tcp_stats);
2065
2066void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
2067 const unsigned int *pgsz_order)
2068{
2069 struct adapter *adap = netdev2adap(dev);
2070
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302071 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
2072 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
2073 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
2074 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002075}
2076EXPORT_SYMBOL(cxgb4_iscsi_init);
2077
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302078int cxgb4_flush_eq_cache(struct net_device *dev)
2079{
2080 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302081
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05302082 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302083}
2084EXPORT_SYMBOL(cxgb4_flush_eq_cache);
2085
2086static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
2087{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302088 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302089 __be64 indices;
2090 int ret;
2091
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05302092 spin_lock(&adap->win0_lock);
2093 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
2094 sizeof(indices), (__be32 *)&indices,
2095 T4_MEMORY_READ);
2096 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302097 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00002098 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
2099 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302100 }
2101 return ret;
2102}
2103
2104int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
2105 u16 size)
2106{
2107 struct adapter *adap = netdev2adap(dev);
2108 u16 hw_pidx, hw_cidx;
2109 int ret;
2110
2111 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
2112 if (ret)
2113 goto out;
2114
2115 if (pidx != hw_pidx) {
2116 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302117 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302118
2119 if (pidx >= hw_pidx)
2120 delta = pidx - hw_pidx;
2121 else
2122 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302123
2124 if (is_t4(adap->params.chip))
2125 val = PIDX_V(delta);
2126 else
2127 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302128 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302129 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2130 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302131 }
2132out:
2133 return ret;
2134}
2135EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
2136
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302137int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
2138{
2139 struct adapter *adap;
2140 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302141 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302142 u32 edc0_end, edc1_end, mc0_end, mc1_end;
2143 int ret;
2144
2145 adap = netdev2adap(dev);
2146
2147 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
2148
2149 /* Figure out where the offset lands in the Memory Type/Address scheme.
2150 * This code assumes that the memory is laid out starting at offset 0
2151 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
2152 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
2153 * MC0, and some have both MC0 and MC1.
2154 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302155 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
2156 edc0_size = EDRAM0_SIZE_G(size) << 20;
2157 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
2158 edc1_size = EDRAM1_SIZE_G(size) << 20;
2159 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
2160 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302161
2162 edc0_end = edc0_size;
2163 edc1_end = edc0_end + edc1_size;
2164 mc0_end = edc1_end + mc0_size;
2165
2166 if (offset < edc0_end) {
2167 memtype = MEM_EDC0;
2168 memaddr = offset;
2169 } else if (offset < edc1_end) {
2170 memtype = MEM_EDC1;
2171 memaddr = offset - edc0_end;
2172 } else {
2173 if (offset < mc0_end) {
2174 memtype = MEM_MC0;
2175 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302176 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302177 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2178 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302179 mc1_end = mc0_end + mc1_size;
2180 if (offset < mc1_end) {
2181 memtype = MEM_MC1;
2182 memaddr = offset - mc0_end;
2183 } else {
2184 /* offset beyond the end of any memory */
2185 goto err;
2186 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302187 } else {
2188 /* T4/T6 only has a single memory channel */
2189 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302190 }
2191 }
2192
2193 spin_lock(&adap->win0_lock);
2194 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
2195 spin_unlock(&adap->win0_lock);
2196 return ret;
2197
2198err:
2199 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
2200 stag, offset);
2201 return -EINVAL;
2202}
2203EXPORT_SYMBOL(cxgb4_read_tpte);
2204
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302205u64 cxgb4_read_sge_timestamp(struct net_device *dev)
2206{
2207 u32 hi, lo;
2208 struct adapter *adap;
2209
2210 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302211 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
2212 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302213
2214 return ((u64)hi << 32) | (u64)lo;
2215}
2216EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
2217
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302218int cxgb4_bar2_sge_qregs(struct net_device *dev,
2219 unsigned int qid,
2220 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05302221 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302222 u64 *pbar2_qoffset,
2223 unsigned int *pbar2_qid)
2224{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302225 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302226 qid,
2227 (qtype == CXGB4_BAR2_QTYPE_EGRESS
2228 ? T4_BAR2_QTYPE_EGRESS
2229 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05302230 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302231 pbar2_qoffset,
2232 pbar2_qid);
2233}
2234EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
2235
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002236static struct pci_driver cxgb4_driver;
2237
2238static void check_neigh_update(struct neighbour *neigh)
2239{
2240 const struct device *parent;
2241 const struct net_device *netdev = neigh->dev;
2242
2243 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2244 netdev = vlan_dev_real_dev(netdev);
2245 parent = netdev->dev.parent;
2246 if (parent && parent->driver == &cxgb4_driver.driver)
2247 t4_l2t_update(dev_get_drvdata(parent), neigh);
2248}
2249
2250static int netevent_cb(struct notifier_block *nb, unsigned long event,
2251 void *data)
2252{
2253 switch (event) {
2254 case NETEVENT_NEIGH_UPDATE:
2255 check_neigh_update(data);
2256 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002257 case NETEVENT_REDIRECT:
2258 default:
2259 break;
2260 }
2261 return 0;
2262}
2263
2264static bool netevent_registered;
2265static struct notifier_block cxgb4_netevent_nb = {
2266 .notifier_call = netevent_cb
2267};
2268
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302269static void drain_db_fifo(struct adapter *adap, int usecs)
2270{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002271 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302272
2273 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302274 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2275 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302276 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302277 lp_count = LP_COUNT_G(v1);
2278 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002279 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302280 lp_count = LP_COUNT_T5_G(v1);
2281 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002282 }
2283
2284 if (lp_count == 0 && hp_count == 0)
2285 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302286 set_current_state(TASK_UNINTERRUPTIBLE);
2287 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302288 } while (1);
2289}
2290
2291static void disable_txq_db(struct sge_txq *q)
2292{
Steve Wise05eb2382014-03-14 21:52:08 +05302293 unsigned long flags;
2294
2295 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302296 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05302297 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302298}
2299
Steve Wise05eb2382014-03-14 21:52:08 +05302300static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302301{
2302 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05302303 if (q->db_pidx_inc) {
2304 /* Make sure that all writes to the TX descriptors
2305 * are committed before we tell HW about them.
2306 */
2307 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302308 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2309 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05302310 q->db_pidx_inc = 0;
2311 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302312 q->db_disabled = 0;
2313 spin_unlock_irq(&q->db_lock);
2314}
2315
2316static void disable_dbs(struct adapter *adap)
2317{
2318 int i;
2319
2320 for_each_ethrxq(&adap->sge, i)
2321 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302322 for_each_iscsirxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302323 disable_txq_db(&adap->sge.ofldtxq[i].q);
2324 for_each_port(adap, i)
2325 disable_txq_db(&adap->sge.ctrlq[i].q);
2326}
2327
2328static void enable_dbs(struct adapter *adap)
2329{
2330 int i;
2331
2332 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302333 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302334 for_each_iscsirxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302335 enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302336 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302337 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
2338}
2339
2340static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
2341{
2342 if (adap->uld_handle[CXGB4_ULD_RDMA])
2343 ulds[CXGB4_ULD_RDMA].control(adap->uld_handle[CXGB4_ULD_RDMA],
2344 cmd);
2345}
2346
2347static void process_db_full(struct work_struct *work)
2348{
2349 struct adapter *adap;
2350
2351 adap = container_of(work, struct adapter, db_full_task);
2352
2353 drain_db_fifo(adap, dbfifo_drain_delay);
2354 enable_dbs(adap);
2355 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302356 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2357 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2358 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
2359 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
2360 else
2361 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2362 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302363}
2364
2365static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
2366{
2367 u16 hw_pidx, hw_cidx;
2368 int ret;
2369
Steve Wise05eb2382014-03-14 21:52:08 +05302370 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302371 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2372 if (ret)
2373 goto out;
2374 if (q->db_pidx != hw_pidx) {
2375 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302376 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302377
2378 if (q->db_pidx >= hw_pidx)
2379 delta = q->db_pidx - hw_pidx;
2380 else
2381 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302382
2383 if (is_t4(adap->params.chip))
2384 val = PIDX_V(delta);
2385 else
2386 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302387 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302388 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2389 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302390 }
2391out:
2392 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302393 q->db_pidx_inc = 0;
2394 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302395 if (ret)
2396 CH_WARN(adap, "DB drop recovery failed.\n");
2397}
2398static void recover_all_queues(struct adapter *adap)
2399{
2400 int i;
2401
2402 for_each_ethrxq(&adap->sge, i)
2403 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302404 for_each_iscsirxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302405 sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
2406 for_each_port(adap, i)
2407 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2408}
2409
Vipul Pandya881806b2012-05-18 15:29:24 +05302410static void process_db_drop(struct work_struct *work)
2411{
2412 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302413
Vipul Pandya881806b2012-05-18 15:29:24 +05302414 adap = container_of(work, struct adapter, db_drop_task);
2415
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302416 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302417 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002418 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302419 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002420 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302421 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002422 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302423 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302424 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002425 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2426 u16 qid = (dropped_db >> 15) & 0x1ffff;
2427 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302428 u64 bar2_qoffset;
2429 unsigned int bar2_qid;
2430 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002431
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302432 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002433 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302434 if (ret)
2435 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2436 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2437 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302438 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302439 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002440
2441 /* Re-enable BAR2 WC */
2442 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2443 }
2444
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302445 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2446 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302447}
2448
2449void t4_db_full(struct adapter *adap)
2450{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302451 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302452 disable_dbs(adap);
2453 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302454 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2455 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002456 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002457 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302458}
2459
2460void t4_db_dropped(struct adapter *adap)
2461{
Steve Wise05eb2382014-03-14 21:52:08 +05302462 if (is_t4(adap->params.chip)) {
2463 disable_dbs(adap);
2464 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2465 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002466 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302467}
2468
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002469static void uld_attach(struct adapter *adap, unsigned int uld)
2470{
2471 void *handle;
2472 struct cxgb4_lld_info lli;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002473 unsigned short i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002474
2475 lli.pdev = adap->pdev;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302476 lli.pf = adap->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002477 lli.l2t = adap->l2t;
2478 lli.tids = &adap->tids;
2479 lli.ports = adap->port;
2480 lli.vr = &adap->vres;
2481 lli.mtus = adap->params.mtus;
2482 if (uld == CXGB4_ULD_RDMA) {
2483 lli.rxq_ids = adap->sge.rdma_rxq;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05302484 lli.ciq_ids = adap->sge.rdma_ciq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002485 lli.nrxq = adap->sge.rdmaqs;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05302486 lli.nciq = adap->sge.rdmaciqs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002487 } else if (uld == CXGB4_ULD_ISCSI) {
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302488 lli.rxq_ids = adap->sge.iscsi_rxq;
2489 lli.nrxq = adap->sge.iscsiqsets;
Varun Prakashf2692d12016-02-14 23:02:40 +05302490 } else if (uld == CXGB4_ULD_ISCSIT) {
2491 lli.rxq_ids = adap->sge.iscsit_rxq;
2492 lli.nrxq = adap->sge.niscsitq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002493 }
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302494 lli.ntxq = adap->sge.iscsiqsets;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002495 lli.nchan = adap->params.nports;
2496 lli.nports = adap->params.nports;
2497 lli.wr_cred = adap->params.ofldq_wr_cred;
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302498 lli.adapter_type = adap->params.chip;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302499 lli.iscsi_iolen = MAXRXDATA_G(t4_read_reg(adap, TP_PARA_REG2_A));
Varun Prakash7714cb9e2016-02-14 23:07:39 +05302500 lli.iscsi_tagmask = t4_read_reg(adap, ULP_RX_ISCSI_TAGMASK_A);
2501 lli.iscsi_pgsz_order = t4_read_reg(adap, ULP_RX_ISCSI_PSZ_A);
2502 lli.iscsi_llimit = t4_read_reg(adap, ULP_RX_ISCSI_LLIMIT_A);
2503 lli.iscsi_ppm = &adap->iscsi_ppm;
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302504 lli.cclk_ps = 1000000000 / adap->params.vpd.cclk;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302505 lli.udb_density = 1 << adap->params.sge.eq_qpp;
2506 lli.ucq_density = 1 << adap->params.sge.iq_qpp;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05302507 lli.filt_mode = adap->params.tp.vlan_pri_map;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002508 /* MODQ_REQ_MAP sets queues 0-3 to chan 0-3 */
2509 for (i = 0; i < NCHAN; i++)
2510 lli.tx_modq[i] = i;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302511 lli.gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS_A);
2512 lli.db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL_A);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002513 lli.fw_vers = adap->params.fw_vers;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302514 lli.dbfifo_int_thresh = dbfifo_int_thresh;
Hariprasad Shenai04e10e22014-07-14 21:34:51 +05302515 lli.sge_ingpadboundary = adap->sge.fl_align;
2516 lli.sge_egrstatuspagesize = adap->sge.stat_len;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002517 lli.sge_pktshift = adap->sge.pktshift;
2518 lli.enable_fw_ofld_conn = adap->flags & FW_OFLD_CONN;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05302519 lli.max_ordird_qp = adap->params.max_ordird_qp;
2520 lli.max_ird_adapter = adap->params.max_ird_adapter;
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05302521 lli.ulptx_memwrite_dsgl = adap->params.ulptx_memwrite_dsgl;
Hariprasad Shenai982b81e2015-05-05 14:59:54 +05302522 lli.nodeid = dev_to_node(adap->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002523
2524 handle = ulds[uld].add(&lli);
2525 if (IS_ERR(handle)) {
2526 dev_warn(adap->pdev_dev,
2527 "could not attach to the %s driver, error %ld\n",
2528 uld_str[uld], PTR_ERR(handle));
2529 return;
2530 }
2531
2532 adap->uld_handle[uld] = handle;
2533
2534 if (!netevent_registered) {
2535 register_netevent_notifier(&cxgb4_netevent_nb);
2536 netevent_registered = true;
2537 }
Dimitris Michailidise29f5db2010-05-18 10:07:13 +00002538
2539 if (adap->flags & FULL_INIT_DONE)
2540 ulds[uld].state_change(handle, CXGB4_STATE_UP);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002541}
2542
2543static void attach_ulds(struct adapter *adap)
2544{
2545 unsigned int i;
2546
Vipul Pandya01bcca62013-07-04 16:10:46 +05302547 spin_lock(&adap_rcu_lock);
2548 list_add_tail_rcu(&adap->rcu_node, &adap_rcu_list);
2549 spin_unlock(&adap_rcu_lock);
2550
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002551 mutex_lock(&uld_mutex);
2552 list_add_tail(&adap->list_node, &adapter_list);
2553 for (i = 0; i < CXGB4_ULD_MAX; i++)
2554 if (ulds[i].add)
2555 uld_attach(adap, i);
2556 mutex_unlock(&uld_mutex);
2557}
2558
2559static void detach_ulds(struct adapter *adap)
2560{
2561 unsigned int i;
2562
2563 mutex_lock(&uld_mutex);
2564 list_del(&adap->list_node);
2565 for (i = 0; i < CXGB4_ULD_MAX; i++)
2566 if (adap->uld_handle[i]) {
2567 ulds[i].state_change(adap->uld_handle[i],
2568 CXGB4_STATE_DETACH);
2569 adap->uld_handle[i] = NULL;
2570 }
2571 if (netevent_registered && list_empty(&adapter_list)) {
2572 unregister_netevent_notifier(&cxgb4_netevent_nb);
2573 netevent_registered = false;
2574 }
2575 mutex_unlock(&uld_mutex);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302576
2577 spin_lock(&adap_rcu_lock);
2578 list_del_rcu(&adap->rcu_node);
2579 spin_unlock(&adap_rcu_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002580}
2581
2582static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2583{
2584 unsigned int i;
2585
2586 mutex_lock(&uld_mutex);
2587 for (i = 0; i < CXGB4_ULD_MAX; i++)
2588 if (adap->uld_handle[i])
2589 ulds[i].state_change(adap->uld_handle[i], new_state);
2590 mutex_unlock(&uld_mutex);
2591}
2592
2593/**
2594 * cxgb4_register_uld - register an upper-layer driver
2595 * @type: the ULD type
2596 * @p: the ULD methods
2597 *
2598 * Registers an upper-layer driver with this driver and notifies the ULD
2599 * about any presently available devices that support its type. Returns
2600 * %-EBUSY if a ULD of the same type is already registered.
2601 */
2602int cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p)
2603{
2604 int ret = 0;
2605 struct adapter *adap;
2606
2607 if (type >= CXGB4_ULD_MAX)
2608 return -EINVAL;
2609 mutex_lock(&uld_mutex);
2610 if (ulds[type].add) {
2611 ret = -EBUSY;
2612 goto out;
2613 }
2614 ulds[type] = *p;
2615 list_for_each_entry(adap, &adapter_list, list_node)
2616 uld_attach(adap, type);
2617out: mutex_unlock(&uld_mutex);
2618 return ret;
2619}
2620EXPORT_SYMBOL(cxgb4_register_uld);
2621
2622/**
2623 * cxgb4_unregister_uld - unregister an upper-layer driver
2624 * @type: the ULD type
2625 *
2626 * Unregisters an existing upper-layer driver.
2627 */
2628int cxgb4_unregister_uld(enum cxgb4_uld type)
2629{
2630 struct adapter *adap;
2631
2632 if (type >= CXGB4_ULD_MAX)
2633 return -EINVAL;
2634 mutex_lock(&uld_mutex);
2635 list_for_each_entry(adap, &adapter_list, list_node)
2636 adap->uld_handle[type] = NULL;
2637 ulds[type].add = NULL;
2638 mutex_unlock(&uld_mutex);
2639 return 0;
2640}
2641EXPORT_SYMBOL(cxgb4_unregister_uld);
2642
Anish Bhatt1bb60372014-10-14 20:07:22 -07002643#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002644static int cxgb4_inet6addr_handler(struct notifier_block *this,
2645 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302646{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002647 struct inet6_ifaddr *ifa = data;
2648 struct net_device *event_dev = ifa->idev->dev;
2649 const struct device *parent = NULL;
2650#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302651 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002652#endif
2653 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2654 event_dev = vlan_dev_real_dev(event_dev);
2655#if IS_ENABLED(CONFIG_BONDING)
2656 if (event_dev->flags & IFF_MASTER) {
2657 list_for_each_entry(adap, &adapter_list, list_node) {
2658 switch (event) {
2659 case NETDEV_UP:
2660 cxgb4_clip_get(adap->port[0],
2661 (const u32 *)ifa, 1);
2662 break;
2663 case NETDEV_DOWN:
2664 cxgb4_clip_release(adap->port[0],
2665 (const u32 *)ifa, 1);
2666 break;
2667 default:
2668 break;
2669 }
2670 }
2671 return NOTIFY_OK;
2672 }
2673#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302674
Anish Bhattb5a02f52015-01-14 15:17:34 -08002675 if (event_dev)
2676 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302677
Anish Bhattb5a02f52015-01-14 15:17:34 -08002678 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302679 switch (event) {
2680 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002681 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302682 break;
2683 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002684 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302685 break;
2686 default:
2687 break;
2688 }
2689 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002690 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302691}
2692
Anish Bhattb5a02f52015-01-14 15:17:34 -08002693static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302694static struct notifier_block cxgb4_inet6addr_notifier = {
2695 .notifier_call = cxgb4_inet6addr_handler
2696};
2697
Vipul Pandya01bcca62013-07-04 16:10:46 +05302698static void update_clip(const struct adapter *adap)
2699{
2700 int i;
2701 struct net_device *dev;
2702 int ret;
2703
2704 rcu_read_lock();
2705
2706 for (i = 0; i < MAX_NPORTS; i++) {
2707 dev = adap->port[i];
2708 ret = 0;
2709
2710 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002711 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302712
2713 if (ret < 0)
2714 break;
2715 }
2716 rcu_read_unlock();
2717}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002718#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302719
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002720/**
2721 * cxgb_up - enable the adapter
2722 * @adap: adapter being enabled
2723 *
2724 * Called when the first port is enabled, this function performs the
2725 * actions necessary to make an adapter operational, such as completing
2726 * the initialization of HW modules, and enabling interrupts.
2727 *
2728 * Must be called with the rtnl lock held.
2729 */
2730static int cxgb_up(struct adapter *adap)
2731{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002732 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002733
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002734 err = setup_sge_queues(adap);
2735 if (err)
2736 goto out;
2737 err = setup_rss(adap);
2738 if (err)
2739 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002740
2741 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002742 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002743 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2744 adap->msix_info[0].desc, adap);
2745 if (err)
2746 goto irq_err;
2747
2748 err = request_msix_queue_irqs(adap);
2749 if (err) {
2750 free_irq(adap->msix_info[0].vec, adap);
2751 goto irq_err;
2752 }
2753 } else {
2754 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2755 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002756 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002757 if (err)
2758 goto irq_err;
2759 }
2760 enable_rx(adap);
2761 t4_sge_start(adap);
2762 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002763 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002764 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002765#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302766 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002767#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302768 /* Initialize hash mac addr list*/
2769 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002770 out:
2771 return err;
2772 irq_err:
2773 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002774 freeq:
2775 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002776 goto out;
2777}
2778
2779static void cxgb_down(struct adapter *adapter)
2780{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002781 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302782 cancel_work_sync(&adapter->db_full_task);
2783 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002784 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002785 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002786
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002787 t4_sge_stop(adapter);
2788 t4_free_sge_resources(adapter);
2789 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002790}
2791
2792/*
2793 * net_device operations
2794 */
2795static int cxgb_open(struct net_device *dev)
2796{
2797 int err;
2798 struct port_info *pi = netdev_priv(dev);
2799 struct adapter *adapter = pi->adapter;
2800
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002801 netif_carrier_off(dev);
2802
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002803 if (!(adapter->flags & FULL_INIT_DONE)) {
2804 err = cxgb_up(adapter);
2805 if (err < 0)
2806 return err;
2807 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002808
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002809 err = link_start(dev);
2810 if (!err)
2811 netif_tx_start_all_queues(dev);
2812 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002813}
2814
2815static int cxgb_close(struct net_device *dev)
2816{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002817 struct port_info *pi = netdev_priv(dev);
2818 struct adapter *adapter = pi->adapter;
2819
2820 netif_tx_stop_all_queues(dev);
2821 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302822 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002823}
2824
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002825/* Return an error number if the indicated filter isn't writable ...
2826 */
2827static int writable_filter(struct filter_entry *f)
2828{
2829 if (f->locked)
2830 return -EPERM;
2831 if (f->pending)
2832 return -EBUSY;
2833
2834 return 0;
2835}
2836
2837/* Delete the filter at the specified index (if valid). The checks for all
2838 * the common problems with doing this like the filter being locked, currently
2839 * pending in another operation, etc.
2840 */
2841static int delete_filter(struct adapter *adapter, unsigned int fidx)
2842{
2843 struct filter_entry *f;
2844 int ret;
2845
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002846 if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002847 return -EINVAL;
2848
2849 f = &adapter->tids.ftid_tab[fidx];
2850 ret = writable_filter(f);
2851 if (ret)
2852 return ret;
2853 if (f->valid)
2854 return del_filter_wr(adapter, fidx);
2855
2856 return 0;
2857}
2858
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002859int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002860 __be32 sip, __be16 sport, __be16 vlan,
2861 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002862{
2863 int ret;
2864 struct filter_entry *f;
2865 struct adapter *adap;
2866 int i;
2867 u8 *val;
2868
2869 adap = netdev2adap(dev);
2870
Vipul Pandya1cab7752012-12-10 09:30:55 +00002871 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302872 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002873 stid += adap->tids.nftids;
2874
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002875 /* Check to make sure the filter requested is writable ...
2876 */
2877 f = &adap->tids.ftid_tab[stid];
2878 ret = writable_filter(f);
2879 if (ret)
2880 return ret;
2881
2882 /* Clear out any old resources being used by the filter before
2883 * we start constructing the new filter.
2884 */
2885 if (f->valid)
2886 clear_filter(adap, f);
2887
2888 /* Clear out filter specifications */
2889 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2890 f->fs.val.lport = cpu_to_be16(sport);
2891 f->fs.mask.lport = ~0;
2892 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002893 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002894 for (i = 0; i < 4; i++) {
2895 f->fs.val.lip[i] = val[i];
2896 f->fs.mask.lip[i] = ~0;
2897 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302898 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002899 f->fs.val.iport = port;
2900 f->fs.mask.iport = mask;
2901 }
2902 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002903
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302904 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302905 f->fs.val.proto = IPPROTO_TCP;
2906 f->fs.mask.proto = ~0;
2907 }
2908
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002909 f->fs.dirsteer = 1;
2910 f->fs.iq = queue;
2911 /* Mark filter as locked */
2912 f->locked = 1;
2913 f->fs.rpttid = 1;
2914
2915 ret = set_filter_wr(adap, stid);
2916 if (ret) {
2917 clear_filter(adap, f);
2918 return ret;
2919 }
2920
2921 return 0;
2922}
2923EXPORT_SYMBOL(cxgb4_create_server_filter);
2924
2925int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2926 unsigned int queue, bool ipv6)
2927{
2928 int ret;
2929 struct filter_entry *f;
2930 struct adapter *adap;
2931
2932 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002933
2934 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302935 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002936 stid += adap->tids.nftids;
2937
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002938 f = &adap->tids.ftid_tab[stid];
2939 /* Unlock the filter */
2940 f->locked = 0;
2941
2942 ret = delete_filter(adap, stid);
2943 if (ret)
2944 return ret;
2945
2946 return 0;
2947}
2948EXPORT_SYMBOL(cxgb4_remove_server_filter);
2949
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002950static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2951 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002952{
2953 struct port_stats stats;
2954 struct port_info *p = netdev_priv(dev);
2955 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002956
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002957 /* Block retrieving statistics during EEH error
2958 * recovery. Otherwise, the recovery might fail
2959 * and the PCI device will be removed permanently
2960 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002961 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002962 if (!netif_device_present(dev)) {
2963 spin_unlock(&adapter->stats_lock);
2964 return ns;
2965 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302966 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2967 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002968 spin_unlock(&adapter->stats_lock);
2969
2970 ns->tx_bytes = stats.tx_octets;
2971 ns->tx_packets = stats.tx_frames;
2972 ns->rx_bytes = stats.rx_octets;
2973 ns->rx_packets = stats.rx_frames;
2974 ns->multicast = stats.rx_mcast_frames;
2975
2976 /* detailed rx_errors */
2977 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2978 stats.rx_runt;
2979 ns->rx_over_errors = 0;
2980 ns->rx_crc_errors = stats.rx_fcs_err;
2981 ns->rx_frame_errors = stats.rx_symbol_err;
2982 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2983 stats.rx_ovflow2 + stats.rx_ovflow3 +
2984 stats.rx_trunc0 + stats.rx_trunc1 +
2985 stats.rx_trunc2 + stats.rx_trunc3;
2986 ns->rx_missed_errors = 0;
2987
2988 /* detailed tx_errors */
2989 ns->tx_aborted_errors = 0;
2990 ns->tx_carrier_errors = 0;
2991 ns->tx_fifo_errors = 0;
2992 ns->tx_heartbeat_errors = 0;
2993 ns->tx_window_errors = 0;
2994
2995 ns->tx_errors = stats.tx_error_frames;
2996 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2997 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2998 return ns;
2999}
3000
3001static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
3002{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003003 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003004 int ret = 0, prtad, devad;
3005 struct port_info *pi = netdev_priv(dev);
3006 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
3007
3008 switch (cmd) {
3009 case SIOCGMIIPHY:
3010 if (pi->mdio_addr < 0)
3011 return -EOPNOTSUPP;
3012 data->phy_id = pi->mdio_addr;
3013 break;
3014 case SIOCGMIIREG:
3015 case SIOCSMIIREG:
3016 if (mdio_phy_id_is_c45(data->phy_id)) {
3017 prtad = mdio_phy_id_prtad(data->phy_id);
3018 devad = mdio_phy_id_devad(data->phy_id);
3019 } else if (data->phy_id < 32) {
3020 prtad = data->phy_id;
3021 devad = 0;
3022 data->reg_num &= 0x1f;
3023 } else
3024 return -EINVAL;
3025
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303026 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003027 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003028 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003029 data->reg_num, &data->val_out);
3030 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003031 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003032 data->reg_num, data->val_in);
3033 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05303034 case SIOCGHWTSTAMP:
3035 return copy_to_user(req->ifr_data, &pi->tstamp_config,
3036 sizeof(pi->tstamp_config)) ?
3037 -EFAULT : 0;
3038 case SIOCSHWTSTAMP:
3039 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
3040 sizeof(pi->tstamp_config)))
3041 return -EFAULT;
3042
3043 switch (pi->tstamp_config.rx_filter) {
3044 case HWTSTAMP_FILTER_NONE:
3045 pi->rxtstamp = false;
3046 break;
3047 case HWTSTAMP_FILTER_ALL:
3048 pi->rxtstamp = true;
3049 break;
3050 default:
3051 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3052 return -ERANGE;
3053 }
3054
3055 return copy_to_user(req->ifr_data, &pi->tstamp_config,
3056 sizeof(pi->tstamp_config)) ?
3057 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003058 default:
3059 return -EOPNOTSUPP;
3060 }
3061 return ret;
3062}
3063
3064static void cxgb_set_rxmode(struct net_device *dev)
3065{
3066 /* unfortunately we can't return errors to the stack */
3067 set_rxmode(dev, -1, false);
3068}
3069
3070static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
3071{
3072 int ret;
3073 struct port_info *pi = netdev_priv(dev);
3074
3075 if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
3076 return -EINVAL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303077 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003078 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003079 if (!ret)
3080 dev->mtu = new_mtu;
3081 return ret;
3082}
3083
3084static int cxgb_set_mac_addr(struct net_device *dev, void *p)
3085{
3086 int ret;
3087 struct sockaddr *addr = p;
3088 struct port_info *pi = netdev_priv(dev);
3089
3090 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00003091 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003092
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303093 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003094 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003095 if (ret < 0)
3096 return ret;
3097
3098 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3099 pi->xact_addr_filt = ret;
3100 return 0;
3101}
3102
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003103#ifdef CONFIG_NET_POLL_CONTROLLER
3104static void cxgb_netpoll(struct net_device *dev)
3105{
3106 struct port_info *pi = netdev_priv(dev);
3107 struct adapter *adap = pi->adapter;
3108
3109 if (adap->flags & USING_MSIX) {
3110 int i;
3111 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
3112
3113 for (i = pi->nqsets; i; i--, rx++)
3114 t4_sge_intr_msix(0, &rx->rspq);
3115 } else
3116 t4_intr_handler(adap)(0, adap);
3117}
3118#endif
3119
3120static const struct net_device_ops cxgb4_netdev_ops = {
3121 .ndo_open = cxgb_open,
3122 .ndo_stop = cxgb_close,
3123 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003124 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003125 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003126 .ndo_set_rx_mode = cxgb_set_rxmode,
3127 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003128 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003129 .ndo_validate_addr = eth_validate_addr,
3130 .ndo_do_ioctl = cxgb_ioctl,
3131 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003132#ifdef CONFIG_NET_POLL_CONTROLLER
3133 .ndo_poll_controller = cxgb_netpoll,
3134#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303135#ifdef CONFIG_CHELSIO_T4_FCOE
3136 .ndo_fcoe_enable = cxgb_fcoe_enable,
3137 .ndo_fcoe_disable = cxgb_fcoe_disable,
3138#endif /* CONFIG_CHELSIO_T4_FCOE */
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05303139#ifdef CONFIG_NET_RX_BUSY_POLL
3140 .ndo_busy_poll = cxgb_busy_poll,
3141#endif
3142
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003143};
3144
3145void t4_fatal_err(struct adapter *adap)
3146{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303147 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003148 t4_intr_disable(adap);
3149 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3150}
3151
3152static void setup_memwin(struct adapter *adap)
3153{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303154 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003155
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303156 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003157}
3158
3159static void setup_memwin_rdma(struct adapter *adap)
3160{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003161 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303162 u32 start;
3163 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003164
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303165 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3166 start &= PCI_BASE_ADDRESS_MEM_MASK;
3167 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003168 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3169 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303170 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3171 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003172 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303173 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003174 adap->vres.ocq.start);
3175 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303176 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003177 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003178}
3179
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003180static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3181{
3182 u32 v;
3183 int ret;
3184
3185 /* get device capabilities */
3186 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303187 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3188 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303189 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303190 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003191 if (ret < 0)
3192 return ret;
3193
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303194 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3195 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303196 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003197 if (ret < 0)
3198 return ret;
3199
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303200 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003201 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303202 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3203 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003204 if (ret < 0)
3205 return ret;
3206
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303207 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303208 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3209 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003210 if (ret < 0)
3211 return ret;
3212
3213 t4_sge_init(adap);
3214
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003215 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303216 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303217 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303218 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3219 v = t4_read_reg(adap, TP_PIO_DATA_A);
3220 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003221
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003222 /* first 4 Tx modulation queues point to consecutive Tx channels */
3223 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303224 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3225 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003226
3227 /* associate each Tx modulation queue with consecutive Tx channels */
3228 v = 0x84218421;
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_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303231 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303232 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303233 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303234 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003235
3236#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3237 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303238 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3239 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3240 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3241 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3242 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3243 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3244 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3245 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3246 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3247 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003248 }
3249
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003250 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303251 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003252}
3253
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003254/*
3255 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3256 */
3257#define MAX_ATIDS 8192U
3258
3259/*
3260 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003261 *
3262 * If the firmware we're dealing with has Configuration File support, then
3263 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003264 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003265
3266/*
3267 * Tweak configuration based on module parameters, etc. Most of these have
3268 * defaults assigned to them by Firmware Configuration Files (if we're using
3269 * them) but need to be explicitly set if we're using hard-coded
3270 * initialization. But even in the case of using Firmware Configuration
3271 * Files, we'd like to expose the ability to change these via module
3272 * parameters so these are essentially common tweaks/settings for
3273 * Configuration Files and hard-coded initialization ...
3274 */
3275static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003276{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003277 /*
3278 * Fix up various Host-Dependent Parameters like Page Size, Cache
3279 * Line Size, etc. The firmware default is for a 4KB Page Size and
3280 * 64B Cache Line Size ...
3281 */
3282 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003283
Vipul Pandya636f9d32012-09-26 02:39:39 +00003284 /*
3285 * Process module parameters which affect early initialization.
3286 */
3287 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3288 dev_err(&adapter->pdev->dev,
3289 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3290 rx_dma_offset);
3291 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003292 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303293 t4_set_reg_field(adapter, SGE_CONTROL_A,
3294 PKTSHIFT_V(PKTSHIFT_M),
3295 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003296
Vipul Pandya636f9d32012-09-26 02:39:39 +00003297 /*
3298 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3299 * adds the pseudo header itself.
3300 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303301 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3302 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003303
3304 return 0;
3305}
3306
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303307/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3308 * unto themselves and they contain their own firmware to perform their
3309 * tasks ...
3310 */
3311static int phy_aq1202_version(const u8 *phy_fw_data,
3312 size_t phy_fw_size)
3313{
3314 int offset;
3315
3316 /* At offset 0x8 you're looking for the primary image's
3317 * starting offset which is 3 Bytes wide
3318 *
3319 * At offset 0xa of the primary image, you look for the offset
3320 * of the DRAM segment which is 3 Bytes wide.
3321 *
3322 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3323 * wide
3324 */
3325 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3326 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3327 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3328
3329 offset = le24(phy_fw_data + 0x8) << 12;
3330 offset = le24(phy_fw_data + offset + 0xa);
3331 return be16(phy_fw_data + offset + 0x27e);
3332
3333 #undef be16
3334 #undef le16
3335 #undef le24
3336}
3337
3338static struct info_10gbt_phy_fw {
3339 unsigned int phy_fw_id; /* PCI Device ID */
3340 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3341 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3342 int phy_flash; /* Has FLASH for PHY Firmware */
3343} phy_info_array[] = {
3344 {
3345 PHY_AQ1202_DEVICEID,
3346 PHY_AQ1202_FIRMWARE,
3347 phy_aq1202_version,
3348 1,
3349 },
3350 {
3351 PHY_BCM84834_DEVICEID,
3352 PHY_BCM84834_FIRMWARE,
3353 NULL,
3354 0,
3355 },
3356 { 0, NULL, NULL },
3357};
3358
3359static struct info_10gbt_phy_fw *find_phy_info(int devid)
3360{
3361 int i;
3362
3363 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3364 if (phy_info_array[i].phy_fw_id == devid)
3365 return &phy_info_array[i];
3366 }
3367 return NULL;
3368}
3369
3370/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3371 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3372 * we return a negative error number. If we transfer new firmware we return 1
3373 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3374 */
3375static int adap_init0_phy(struct adapter *adap)
3376{
3377 const struct firmware *phyf;
3378 int ret;
3379 struct info_10gbt_phy_fw *phy_info;
3380
3381 /* Use the device ID to determine which PHY file to flash.
3382 */
3383 phy_info = find_phy_info(adap->pdev->device);
3384 if (!phy_info) {
3385 dev_warn(adap->pdev_dev,
3386 "No PHY Firmware file found for this PHY\n");
3387 return -EOPNOTSUPP;
3388 }
3389
3390 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3391 * use that. The adapter firmware provides us with a memory buffer
3392 * where we can load a PHY firmware file from the host if we want to
3393 * override the PHY firmware File in flash.
3394 */
3395 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3396 adap->pdev_dev);
3397 if (ret < 0) {
3398 /* For adapters without FLASH attached to PHY for their
3399 * firmware, it's obviously a fatal error if we can't get the
3400 * firmware to the adapter. For adapters with PHY firmware
3401 * FLASH storage, it's worth a warning if we can't find the
3402 * PHY Firmware but we'll neuter the error ...
3403 */
3404 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3405 "/lib/firmware/%s, error %d\n",
3406 phy_info->phy_fw_file, -ret);
3407 if (phy_info->phy_flash) {
3408 int cur_phy_fw_ver = 0;
3409
3410 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3411 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3412 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3413 ret = 0;
3414 }
3415
3416 return ret;
3417 }
3418
3419 /* Load PHY Firmware onto adapter.
3420 */
3421 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3422 phy_info->phy_fw_version,
3423 (u8 *)phyf->data, phyf->size);
3424 if (ret < 0)
3425 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3426 -ret);
3427 else if (ret > 0) {
3428 int new_phy_fw_ver = 0;
3429
3430 if (phy_info->phy_fw_version)
3431 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3432 phyf->size);
3433 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3434 "Firmware /lib/firmware/%s, version %#x\n",
3435 phy_info->phy_fw_file, new_phy_fw_ver);
3436 }
3437
3438 release_firmware(phyf);
3439
3440 return ret;
3441}
3442
Vipul Pandya636f9d32012-09-26 02:39:39 +00003443/*
3444 * Attempt to initialize the adapter via a Firmware Configuration File.
3445 */
3446static int adap_init0_config(struct adapter *adapter, int reset)
3447{
3448 struct fw_caps_config_cmd caps_cmd;
3449 const struct firmware *cf;
3450 unsigned long mtype = 0, maddr = 0;
3451 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303452 int ret;
3453 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003454 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303455 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003456
3457 /*
3458 * Reset device if necessary.
3459 */
3460 if (reset) {
3461 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303462 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003463 if (ret < 0)
3464 goto bye;
3465 }
3466
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303467 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3468 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3469 * to be performed after any global adapter RESET above since some
3470 * PHYs only have local RAM copies of the PHY firmware.
3471 */
3472 if (is_10gbt_device(adapter->pdev->device)) {
3473 ret = adap_init0_phy(adapter);
3474 if (ret < 0)
3475 goto bye;
3476 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003477 /*
3478 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3479 * then use that. Otherwise, use the configuration file stored
3480 * in the adapter flash ...
3481 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303482 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003483 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303484 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003485 break;
3486 case CHELSIO_T5:
3487 fw_config_file = FW5_CFNAME;
3488 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303489 case CHELSIO_T6:
3490 fw_config_file = FW6_CFNAME;
3491 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003492 default:
3493 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3494 adapter->pdev->device);
3495 ret = -EINVAL;
3496 goto bye;
3497 }
3498
3499 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003500 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303501 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003502 mtype = FW_MEMTYPE_CF_FLASH;
3503 maddr = t4_flash_cfg_addr(adapter);
3504 } else {
3505 u32 params[7], val[7];
3506
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303507 sprintf(fw_config_file_path,
3508 "/lib/firmware/%s", fw_config_file);
3509 config_name = fw_config_file_path;
3510
Vipul Pandya636f9d32012-09-26 02:39:39 +00003511 if (cf->size >= FLASH_CFG_MAX_SIZE)
3512 ret = -ENOMEM;
3513 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303514 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3515 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003516 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303517 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003518 if (ret == 0) {
3519 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303520 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003521 * sizes have to be in terms of multiples of 4
3522 * bytes. So, if the Configuration File isn't
3523 * a multiple of 4 bytes in length we'll have
3524 * to write that out separately since we can't
3525 * guarantee that the bytes following the
3526 * residual byte in the buffer returned by
3527 * request_firmware() are zeroed out ...
3528 */
3529 size_t resid = cf->size & 0x3;
3530 size_t size = cf->size & ~0x3;
3531 __be32 *data = (__be32 *)cf->data;
3532
Hariprasad Shenai51678652014-11-21 12:52:02 +05303533 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3534 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003535
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303536 spin_lock(&adapter->win0_lock);
3537 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3538 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003539 if (ret == 0 && resid != 0) {
3540 union {
3541 __be32 word;
3542 char buf[4];
3543 } last;
3544 int i;
3545
3546 last.word = data[size >> 2];
3547 for (i = resid; i < 4; i++)
3548 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303549 ret = t4_memory_rw(adapter, 0, mtype,
3550 maddr + size,
3551 4, &last.word,
3552 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003553 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303554 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003555 }
3556 }
3557
3558 release_firmware(cf);
3559 if (ret)
3560 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003561 }
3562
Vipul Pandya636f9d32012-09-26 02:39:39 +00003563 /*
3564 * Issue a Capability Configuration command to the firmware to get it
3565 * to parse the Configuration File. We don't use t4_fw_config_file()
3566 * because we want the ability to modify various features after we've
3567 * processed the configuration file ...
3568 */
3569 memset(&caps_cmd, 0, sizeof(caps_cmd));
3570 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303571 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3572 FW_CMD_REQUEST_F |
3573 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303574 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303575 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3576 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3577 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003578 FW_LEN16(caps_cmd));
3579 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3580 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303581
3582 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3583 * Configuration File in FLASH), our last gasp effort is to use the
3584 * Firmware Configuration File which is embedded in the firmware. A
3585 * very few early versions of the firmware didn't have one embedded
3586 * but we can ignore those.
3587 */
3588 if (ret == -ENOENT) {
3589 memset(&caps_cmd, 0, sizeof(caps_cmd));
3590 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303591 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3592 FW_CMD_REQUEST_F |
3593 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303594 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3595 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3596 sizeof(caps_cmd), &caps_cmd);
3597 config_name = "Firmware Default";
3598 }
3599
3600 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003601 if (ret < 0)
3602 goto bye;
3603
Vipul Pandya636f9d32012-09-26 02:39:39 +00003604 finiver = ntohl(caps_cmd.finiver);
3605 finicsum = ntohl(caps_cmd.finicsum);
3606 cfcsum = ntohl(caps_cmd.cfcsum);
3607 if (finicsum != cfcsum)
3608 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3609 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3610 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003611
Vipul Pandya636f9d32012-09-26 02:39:39 +00003612 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003613 * And now tell the firmware to use the configuration we just loaded.
3614 */
3615 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303616 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3617 FW_CMD_REQUEST_F |
3618 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303619 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003620 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3621 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003622 if (ret < 0)
3623 goto bye;
3624
Vipul Pandya636f9d32012-09-26 02:39:39 +00003625 /*
3626 * Tweak configuration based on system architecture, module
3627 * parameters, etc.
3628 */
3629 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003630 if (ret < 0)
3631 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003632
Vipul Pandya636f9d32012-09-26 02:39:39 +00003633 /*
3634 * And finally tell the firmware to initialize itself using the
3635 * parameters from the Configuration File.
3636 */
3637 ret = t4_fw_initialize(adapter, adapter->mbox);
3638 if (ret < 0)
3639 goto bye;
3640
Hariprasad Shenai06640312015-01-13 15:19:25 +05303641 /* Emit Firmware Configuration File information and return
3642 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003643 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003644 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303645 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3646 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003647 return 0;
3648
3649 /*
3650 * Something bad happened. Return the error ... (If the "error"
3651 * is that there's no Configuration File on the adapter we don't
3652 * want to issue a warning since this is fairly common.)
3653 */
3654bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303655 if (config_issued && ret != -ENOENT)
3656 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3657 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003658 return ret;
3659}
3660
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303661static struct fw_info fw_info_array[] = {
3662 {
3663 .chip = CHELSIO_T4,
3664 .fs_name = FW4_CFNAME,
3665 .fw_mod_name = FW4_FNAME,
3666 .fw_hdr = {
3667 .chip = FW_HDR_CHIP_T4,
3668 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3669 .intfver_nic = FW_INTFVER(T4, NIC),
3670 .intfver_vnic = FW_INTFVER(T4, VNIC),
3671 .intfver_ri = FW_INTFVER(T4, RI),
3672 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3673 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3674 },
3675 }, {
3676 .chip = CHELSIO_T5,
3677 .fs_name = FW5_CFNAME,
3678 .fw_mod_name = FW5_FNAME,
3679 .fw_hdr = {
3680 .chip = FW_HDR_CHIP_T5,
3681 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3682 .intfver_nic = FW_INTFVER(T5, NIC),
3683 .intfver_vnic = FW_INTFVER(T5, VNIC),
3684 .intfver_ri = FW_INTFVER(T5, RI),
3685 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3686 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3687 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303688 }, {
3689 .chip = CHELSIO_T6,
3690 .fs_name = FW6_CFNAME,
3691 .fw_mod_name = FW6_FNAME,
3692 .fw_hdr = {
3693 .chip = FW_HDR_CHIP_T6,
3694 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3695 .intfver_nic = FW_INTFVER(T6, NIC),
3696 .intfver_vnic = FW_INTFVER(T6, VNIC),
3697 .intfver_ofld = FW_INTFVER(T6, OFLD),
3698 .intfver_ri = FW_INTFVER(T6, RI),
3699 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3700 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3701 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3702 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3703 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303704 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303705
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303706};
3707
3708static struct fw_info *find_fw_info(int chip)
3709{
3710 int i;
3711
3712 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3713 if (fw_info_array[i].chip == chip)
3714 return &fw_info_array[i];
3715 }
3716 return NULL;
3717}
3718
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003719/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003720 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003721 */
3722static int adap_init0(struct adapter *adap)
3723{
3724 int ret;
3725 u32 v, port_vec;
3726 enum dev_state state;
3727 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003728 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303729 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003730
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303731 /* Grab Firmware Device Log parameters as early as possible so we have
3732 * access to it for debugging, etc.
3733 */
3734 ret = t4_init_devlog_params(adap);
3735 if (ret < 0)
3736 return ret;
3737
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303738 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303739 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3740 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003741 if (ret < 0) {
3742 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3743 ret);
3744 return ret;
3745 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003746 if (ret == adap->mbox)
3747 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003748
Vipul Pandya636f9d32012-09-26 02:39:39 +00003749 /*
3750 * If we're the Master PF Driver and the device is uninitialized,
3751 * then let's consider upgrading the firmware ... (We always want
3752 * to check the firmware version number in order to A. get it for
3753 * later reporting and B. to warn if the currently loaded firmware
3754 * is excessively mismatched relative to the driver.)
3755 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303756 t4_get_fw_version(adap, &adap->params.fw_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303757 t4_get_bs_version(adap, &adap->params.bs_vers);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303758 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303759 t4_get_exprom_version(adap, &adap->params.er_vers);
3760
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303761 ret = t4_check_fw_version(adap);
3762 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303763 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303764 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003765 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303766 struct fw_info *fw_info;
3767 struct fw_hdr *card_fw;
3768 const struct firmware *fw;
3769 const u8 *fw_data = NULL;
3770 unsigned int fw_size = 0;
3771
3772 /* This is the firmware whose headers the driver was compiled
3773 * against
3774 */
3775 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3776 if (fw_info == NULL) {
3777 dev_err(adap->pdev_dev,
3778 "unable to get firmware info for chip %d.\n",
3779 CHELSIO_CHIP_VERSION(adap->params.chip));
3780 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003781 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303782
3783 /* allocate memory to read the header of the firmware on the
3784 * card
3785 */
3786 card_fw = t4_alloc_mem(sizeof(*card_fw));
3787
3788 /* Get FW from from /lib/firmware/ */
3789 ret = request_firmware(&fw, fw_info->fw_mod_name,
3790 adap->pdev_dev);
3791 if (ret < 0) {
3792 dev_err(adap->pdev_dev,
3793 "unable to load firmware image %s, error %d\n",
3794 fw_info->fw_mod_name, ret);
3795 } else {
3796 fw_data = fw->data;
3797 fw_size = fw->size;
3798 }
3799
3800 /* upgrade FW logic */
3801 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3802 state, &reset);
3803
3804 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003805 release_firmware(fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303806 t4_free_mem(card_fw);
3807
Vipul Pandya636f9d32012-09-26 02:39:39 +00003808 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303809 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003810 }
3811
3812 /*
3813 * Grab VPD parameters. This should be done after we establish a
3814 * connection to the firmware since some of the VPD parameters
3815 * (notably the Core Clock frequency) are retrieved via requests to
3816 * the firmware. On the other hand, we need these fairly early on
3817 * so we do this right after getting ahold of the firmware.
3818 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303819 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003820 if (ret < 0)
3821 goto bye;
3822
Vipul Pandya636f9d32012-09-26 02:39:39 +00003823 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003824 * Find out what ports are available to us. Note that we need to do
3825 * this before calling adap_init0_no_config() since it needs nports
3826 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003827 */
3828 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303829 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3830 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303831 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003832 if (ret < 0)
3833 goto bye;
3834
3835 adap->params.nports = hweight32(port_vec);
3836 adap->params.portvec = port_vec;
3837
Hariprasad Shenai06640312015-01-13 15:19:25 +05303838 /* If the firmware is initialized already, emit a simply note to that
3839 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003840 */
3841 if (state == DEV_STATE_INIT) {
3842 dev_info(adap->pdev_dev, "Coming up as %s: "\
3843 "Adapter already initialized\n",
3844 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003845 } else {
3846 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3847 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303848
3849 /* Find out whether we're dealing with a version of the
3850 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003851 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303852 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3853 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303854 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303855 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003856
Hariprasad Shenai06640312015-01-13 15:19:25 +05303857 /* If the firmware doesn't support Configuration Files,
3858 * return an error.
3859 */
3860 if (ret < 0) {
3861 dev_err(adap->pdev_dev, "firmware doesn't support "
3862 "Firmware Configuration Files\n");
3863 goto bye;
3864 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003865
Hariprasad Shenai06640312015-01-13 15:19:25 +05303866 /* The firmware provides us with a memory buffer where we can
3867 * load a Configuration File from the host if we want to
3868 * override the Configuration File in flash.
3869 */
3870 ret = adap_init0_config(adap, reset);
3871 if (ret == -ENOENT) {
3872 dev_err(adap->pdev_dev, "no Configuration File "
3873 "present on adapter.\n");
3874 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003875 }
3876 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303877 dev_err(adap->pdev_dev, "could not initialize "
3878 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003879 goto bye;
3880 }
3881 }
3882
Hariprasad Shenai06640312015-01-13 15:19:25 +05303883 /* Give the SGE code a chance to pull in anything that it needs ...
3884 * Note that this must be called after we retrieve our VPD parameters
3885 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003886 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303887 ret = t4_sge_init(adap);
3888 if (ret < 0)
3889 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003890
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003891 if (is_bypass_device(adap->pdev->device))
3892 adap->params.bypass = 1;
3893
Vipul Pandya636f9d32012-09-26 02:39:39 +00003894 /*
3895 * Grab some of our basic fundamental operating parameters.
3896 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003897#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303898 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3899 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003900
3901#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303902 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3903 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3904 FW_PARAMS_PARAM_Y_V(0) | \
3905 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003906
Vipul Pandya636f9d32012-09-26 02:39:39 +00003907 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003908 params[1] = FW_PARAM_PFVF(L2T_START);
3909 params[2] = FW_PARAM_PFVF(L2T_END);
3910 params[3] = FW_PARAM_PFVF(FILTER_START);
3911 params[4] = FW_PARAM_PFVF(FILTER_END);
3912 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303913 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003914 if (ret < 0)
3915 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003916 adap->sge.egr_start = val[0];
3917 adap->l2t_start = val[1];
3918 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003919 adap->tids.ftid_base = val[3];
3920 adap->tids.nftids = val[4] - val[3] + 1;
3921 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003922
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303923 /* qids (ingress/egress) returned from firmware can be anywhere
3924 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3925 * Hence driver needs to allocate memory for this range to
3926 * store the queue info. Get the highest IQFLINT/EQ index returned
3927 * in FW_EQ_*_CMD.alloc command.
3928 */
3929 params[0] = FW_PARAM_PFVF(EQ_END);
3930 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303931 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303932 if (ret < 0)
3933 goto bye;
3934 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3935 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3936
3937 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3938 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3939 if (!adap->sge.egr_map) {
3940 ret = -ENOMEM;
3941 goto bye;
3942 }
3943
3944 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3945 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3946 if (!adap->sge.ingr_map) {
3947 ret = -ENOMEM;
3948 goto bye;
3949 }
3950
3951 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303952 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303953 */
3954 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3955 sizeof(long), GFP_KERNEL);
3956 if (!adap->sge.starving_fl) {
3957 ret = -ENOMEM;
3958 goto bye;
3959 }
3960
3961 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3962 sizeof(long), GFP_KERNEL);
3963 if (!adap->sge.txq_maperr) {
3964 ret = -ENOMEM;
3965 goto bye;
3966 }
3967
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303968#ifdef CONFIG_DEBUG_FS
3969 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3970 sizeof(long), GFP_KERNEL);
3971 if (!adap->sge.blocked_fl) {
3972 ret = -ENOMEM;
3973 goto bye;
3974 }
3975#endif
3976
Anish Bhattb5a02f52015-01-14 15:17:34 -08003977 params[0] = FW_PARAM_PFVF(CLIP_START);
3978 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303979 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003980 if (ret < 0)
3981 goto bye;
3982 adap->clipt_start = val[0];
3983 adap->clipt_end = val[1];
3984
Vipul Pandya636f9d32012-09-26 02:39:39 +00003985 /* query params related to active filter region */
3986 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3987 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303988 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003989 /* If Active filter size is set we enable establishing
3990 * offload connection through firmware work request
3991 */
3992 if ((val[0] != val[1]) && (ret >= 0)) {
3993 adap->flags |= FW_OFLD_CONN;
3994 adap->tids.aftid_base = val[0];
3995 adap->tids.aftid_end = val[1];
3996 }
3997
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003998 /* If we're running on newer firmware, let it know that we're
3999 * prepared to deal with encapsulated CPL messages. Older
4000 * firmware won't understand this and we'll just get
4001 * unencapsulated messages ...
4002 */
4003 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
4004 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304005 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004006
Vipul Pandya636f9d32012-09-26 02:39:39 +00004007 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304008 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
4009 * capability. Earlier versions of the firmware didn't have the
4010 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
4011 * permission to use ULPTX MEMWRITE DSGL.
4012 */
4013 if (is_t4(adap->params.chip)) {
4014 adap->params.ulptx_memwrite_dsgl = false;
4015 } else {
4016 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304017 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304018 1, params, val);
4019 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4020 }
4021
4022 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004023 * Get device capabilities so we can determine what resources we need
4024 * to manage.
4025 */
4026 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304027 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4028 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304029 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004030 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4031 &caps_cmd);
4032 if (ret < 0)
4033 goto bye;
4034
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004035 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004036 /* query offload-related parameters */
4037 params[0] = FW_PARAM_DEV(NTID);
4038 params[1] = FW_PARAM_PFVF(SERVER_START);
4039 params[2] = FW_PARAM_PFVF(SERVER_END);
4040 params[3] = FW_PARAM_PFVF(TDDP_START);
4041 params[4] = FW_PARAM_PFVF(TDDP_END);
4042 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304043 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004044 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004045 if (ret < 0)
4046 goto bye;
4047 adap->tids.ntids = val[0];
4048 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4049 adap->tids.stid_base = val[1];
4050 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004051 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004052 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004053 * region into two parts. Regular filters get 1/3rd and server
4054 * filters get 2/3rd part. This is only enabled if workarond
4055 * path is enabled.
4056 * 1. For regular filters.
4057 * 2. Server filter: This are special filters which are used
4058 * to redirect SYN packets to offload queue.
4059 */
4060 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4061 adap->tids.sftid_base = adap->tids.ftid_base +
4062 DIV_ROUND_UP(adap->tids.nftids, 3);
4063 adap->tids.nsftids = adap->tids.nftids -
4064 DIV_ROUND_UP(adap->tids.nftids, 3);
4065 adap->tids.nftids = adap->tids.sftid_base -
4066 adap->tids.ftid_base;
4067 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004068 adap->vres.ddp.start = val[3];
4069 adap->vres.ddp.size = val[4] - val[3] + 1;
4070 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004071
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004072 adap->params.offload = 1;
4073 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004074 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004075 params[0] = FW_PARAM_PFVF(STAG_START);
4076 params[1] = FW_PARAM_PFVF(STAG_END);
4077 params[2] = FW_PARAM_PFVF(RQ_START);
4078 params[3] = FW_PARAM_PFVF(RQ_END);
4079 params[4] = FW_PARAM_PFVF(PBL_START);
4080 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304081 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004082 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004083 if (ret < 0)
4084 goto bye;
4085 adap->vres.stag.start = val[0];
4086 adap->vres.stag.size = val[1] - val[0] + 1;
4087 adap->vres.rq.start = val[2];
4088 adap->vres.rq.size = val[3] - val[2] + 1;
4089 adap->vres.pbl.start = val[4];
4090 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004091
4092 params[0] = FW_PARAM_PFVF(SQRQ_START);
4093 params[1] = FW_PARAM_PFVF(SQRQ_END);
4094 params[2] = FW_PARAM_PFVF(CQ_START);
4095 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004096 params[4] = FW_PARAM_PFVF(OCQ_START);
4097 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304098 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304099 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004100 if (ret < 0)
4101 goto bye;
4102 adap->vres.qp.start = val[0];
4103 adap->vres.qp.size = val[1] - val[0] + 1;
4104 adap->vres.cq.start = val[2];
4105 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004106 adap->vres.ocq.start = val[4];
4107 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304108
4109 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4110 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304111 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304112 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304113 if (ret < 0) {
4114 adap->params.max_ordird_qp = 8;
4115 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4116 ret = 0;
4117 } else {
4118 adap->params.max_ordird_qp = val[0];
4119 adap->params.max_ird_adapter = val[1];
4120 }
4121 dev_info(adap->pdev_dev,
4122 "max_ordird_qp %d max_ird_adapter %d\n",
4123 adap->params.max_ordird_qp,
4124 adap->params.max_ird_adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004125 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004126 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004127 params[0] = FW_PARAM_PFVF(ISCSI_START);
4128 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304129 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004130 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004131 if (ret < 0)
4132 goto bye;
4133 adap->vres.iscsi.start = val[0];
4134 adap->vres.iscsi.size = val[1] - val[0] + 1;
4135 }
4136#undef FW_PARAM_PFVF
4137#undef FW_PARAM_DEV
4138
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304139 /* The MTU/MSS Table is initialized by now, so load their values. If
4140 * we're initializing the adapter, then we'll make any modifications
4141 * we want to the MTU/MSS Table and also initialize the congestion
4142 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004143 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004144 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304145 if (state != DEV_STATE_INIT) {
4146 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004147
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304148 /* The default MTU Table contains values 1492 and 1500.
4149 * However, for TCP, it's better to have two values which are
4150 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4151 * This allows us to have a TCP Data Payload which is a
4152 * multiple of 8 regardless of what combination of TCP Options
4153 * are in use (always a multiple of 4 bytes) which is
4154 * important for performance reasons. For instance, if no
4155 * options are in use, then we have a 20-byte IP header and a
4156 * 20-byte TCP header. In this case, a 1500-byte MSS would
4157 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4158 * which is not a multiple of 8. So using an MSS of 1488 in
4159 * this case results in a TCP Data Payload of 1448 bytes which
4160 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4161 * Stamps have been negotiated, then an MTU of 1500 bytes
4162 * results in a TCP Data Payload of 1448 bytes which, as
4163 * above, is a multiple of 8 bytes ...
4164 */
4165 for (i = 0; i < NMTUS; i++)
4166 if (adap->params.mtus[i] == 1492) {
4167 adap->params.mtus[i] = 1488;
4168 break;
4169 }
4170
4171 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4172 adap->params.b_wnd);
4173 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304174 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004175 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05304176 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004177 return 0;
4178
4179 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004180 * Something bad happened. If a command timed out or failed with EIO
4181 * FW does not operate within its spec or something catastrophic
4182 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004183 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004184bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304185 kfree(adap->sge.egr_map);
4186 kfree(adap->sge.ingr_map);
4187 kfree(adap->sge.starving_fl);
4188 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304189#ifdef CONFIG_DEBUG_FS
4190 kfree(adap->sge.blocked_fl);
4191#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004192 if (ret != -ETIMEDOUT && ret != -EIO)
4193 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004194 return ret;
4195}
4196
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004197/* EEH callbacks */
4198
4199static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4200 pci_channel_state_t state)
4201{
4202 int i;
4203 struct adapter *adap = pci_get_drvdata(pdev);
4204
4205 if (!adap)
4206 goto out;
4207
4208 rtnl_lock();
4209 adap->flags &= ~FW_OK;
4210 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004211 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004212 for_each_port(adap, i) {
4213 struct net_device *dev = adap->port[i];
4214
4215 netif_device_detach(dev);
4216 netif_carrier_off(dev);
4217 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004218 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304219 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004220 if (adap->flags & FULL_INIT_DONE)
4221 cxgb_down(adap);
4222 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004223 if ((adap->flags & DEV_ENABLED)) {
4224 pci_disable_device(pdev);
4225 adap->flags &= ~DEV_ENABLED;
4226 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004227out: return state == pci_channel_io_perm_failure ?
4228 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4229}
4230
4231static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4232{
4233 int i, ret;
4234 struct fw_caps_config_cmd c;
4235 struct adapter *adap = pci_get_drvdata(pdev);
4236
4237 if (!adap) {
4238 pci_restore_state(pdev);
4239 pci_save_state(pdev);
4240 return PCI_ERS_RESULT_RECOVERED;
4241 }
4242
Gavin Shan144be3d2014-01-23 12:27:34 +08004243 if (!(adap->flags & DEV_ENABLED)) {
4244 if (pci_enable_device(pdev)) {
4245 dev_err(&pdev->dev, "Cannot reenable PCI "
4246 "device after reset\n");
4247 return PCI_ERS_RESULT_DISCONNECT;
4248 }
4249 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004250 }
4251
4252 pci_set_master(pdev);
4253 pci_restore_state(pdev);
4254 pci_save_state(pdev);
4255 pci_cleanup_aer_uncorrect_error_status(pdev);
4256
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304257 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004258 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304259 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004260 return PCI_ERS_RESULT_DISCONNECT;
4261 adap->flags |= FW_OK;
4262 if (adap_init1(adap, &c))
4263 return PCI_ERS_RESULT_DISCONNECT;
4264
4265 for_each_port(adap, i) {
4266 struct port_info *p = adap2pinfo(adap, i);
4267
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304268 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004269 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004270 if (ret < 0)
4271 return PCI_ERS_RESULT_DISCONNECT;
4272 p->viid = ret;
4273 p->xact_addr_filt = -1;
4274 }
4275
4276 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4277 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004278 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004279 if (cxgb_up(adap))
4280 return PCI_ERS_RESULT_DISCONNECT;
4281 return PCI_ERS_RESULT_RECOVERED;
4282}
4283
4284static void eeh_resume(struct pci_dev *pdev)
4285{
4286 int i;
4287 struct adapter *adap = pci_get_drvdata(pdev);
4288
4289 if (!adap)
4290 return;
4291
4292 rtnl_lock();
4293 for_each_port(adap, i) {
4294 struct net_device *dev = adap->port[i];
4295
4296 if (netif_running(dev)) {
4297 link_start(dev);
4298 cxgb_set_rxmode(dev);
4299 }
4300 netif_device_attach(dev);
4301 }
4302 rtnl_unlock();
4303}
4304
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004305static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004306 .error_detected = eeh_err_detected,
4307 .slot_reset = eeh_slot_reset,
4308 .resume = eeh_resume,
4309};
4310
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304311static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004312{
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304313 return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
4314 (lc->supported & FW_PORT_CAP_SPEED_40G) != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004315}
4316
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304317static inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
4318 unsigned int us, unsigned int cnt,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004319 unsigned int size, unsigned int iqe_size)
4320{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304321 q->adap = adap;
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304322 cxgb4_set_rspq_intr_params(q, us, cnt);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004323 q->iqe_len = iqe_size;
4324 q->size = size;
4325}
4326
4327/*
4328 * Perform default configuration of DMA queues depending on the number and type
4329 * of ports we found and the number of available CPUs. Most settings can be
4330 * modified by the admin prior to actual use.
4331 */
Bill Pemberton91744942012-12-03 09:23:02 -05004332static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004333{
4334 struct sge *s = &adap->sge;
Anish Bhatt688848b2014-06-19 21:37:13 -07004335 int i, n10g = 0, qidx = 0;
4336#ifndef CONFIG_CHELSIO_T4_DCB
4337 int q10g = 0;
4338#endif
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304339 int ciq_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004340
4341 for_each_port(adap, i)
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304342 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004343#ifdef CONFIG_CHELSIO_T4_DCB
4344 /* For Data Center Bridging support we need to be able to support up
4345 * to 8 Traffic Priorities; each of which will be assigned to its
4346 * own TX Queue in order to prevent Head-Of-Line Blocking.
4347 */
4348 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4349 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4350 MAX_ETH_QSETS, adap->params.nports * 8);
4351 BUG_ON(1);
4352 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004353
Anish Bhatt688848b2014-06-19 21:37:13 -07004354 for_each_port(adap, i) {
4355 struct port_info *pi = adap2pinfo(adap, i);
4356
4357 pi->first_qset = qidx;
4358 pi->nqsets = 8;
4359 qidx += pi->nqsets;
4360 }
4361#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004362 /*
4363 * We default to 1 queue per non-10G port and up to # of cores queues
4364 * per 10G port.
4365 */
4366 if (n10g)
4367 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004368 if (q10g > netif_get_num_default_rss_queues())
4369 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004370
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05304371 /* Reduce memory usage in kdump environment, disable all offload.
4372 */
4373 if (is_kdump_kernel())
4374 adap->params.offload = 0;
4375
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004376 for_each_port(adap, i) {
4377 struct port_info *pi = adap2pinfo(adap, i);
4378
4379 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304380 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004381 qidx += pi->nqsets;
4382 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004383#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004384
4385 s->ethqsets = qidx;
4386 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4387
4388 if (is_offload(adap)) {
4389 /*
4390 * For offload we use 1 queue/channel if all ports are up to 1G,
4391 * otherwise we divide all available queues amongst the channels
4392 * capped by the number of available cores.
4393 */
4394 if (n10g) {
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304395 i = min_t(int, ARRAY_SIZE(s->iscsirxq),
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004396 num_online_cpus());
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304397 s->iscsiqsets = roundup(i, adap->params.nports);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004398 } else
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304399 s->iscsiqsets = adap->params.nports;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004400 /* For RDMA one Rx queue per channel suffices */
4401 s->rdmaqs = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304402 /* Try and allow at least 1 CIQ per cpu rounding down
4403 * to the number of ports, with a minimum of 1 per port.
4404 * A 2 port card in a 6 cpu system: 6 CIQs, 3 / port.
4405 * A 4 port card in a 6 cpu system: 4 CIQs, 1 / port.
4406 * A 4 port card in a 2 cpu system: 4 CIQs, 1 / port.
4407 */
4408 s->rdmaciqs = min_t(int, MAX_RDMA_CIQS, num_online_cpus());
4409 s->rdmaciqs = (s->rdmaciqs / adap->params.nports) *
4410 adap->params.nports;
4411 s->rdmaciqs = max_t(int, s->rdmaciqs, adap->params.nports);
Varun Prakashf2692d12016-02-14 23:02:40 +05304412
4413 if (!is_t4(adap->params.chip))
4414 s->niscsitq = s->iscsiqsets;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004415 }
4416
4417 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4418 struct sge_eth_rxq *r = &s->ethrxq[i];
4419
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304420 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004421 r->fl.size = 72;
4422 }
4423
4424 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4425 s->ethtxq[i].q.size = 1024;
4426
4427 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4428 s->ctrlq[i].q.size = 512;
4429
4430 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
4431 s->ofldtxq[i].q.size = 1024;
4432
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304433 for (i = 0; i < ARRAY_SIZE(s->iscsirxq); i++) {
4434 struct sge_ofld_rxq *r = &s->iscsirxq[i];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004435
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304436 init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004437 r->rspq.uld = CXGB4_ULD_ISCSI;
4438 r->fl.size = 72;
4439 }
4440
Varun Prakashf2692d12016-02-14 23:02:40 +05304441 if (!is_t4(adap->params.chip)) {
4442 for (i = 0; i < ARRAY_SIZE(s->iscsitrxq); i++) {
4443 struct sge_ofld_rxq *r = &s->iscsitrxq[i];
4444
4445 init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
4446 r->rspq.uld = CXGB4_ULD_ISCSIT;
4447 r->fl.size = 72;
4448 }
4449 }
4450
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004451 for (i = 0; i < ARRAY_SIZE(s->rdmarxq); i++) {
4452 struct sge_ofld_rxq *r = &s->rdmarxq[i];
4453
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304454 init_rspq(adap, &r->rspq, 5, 1, 511, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004455 r->rspq.uld = CXGB4_ULD_RDMA;
4456 r->fl.size = 72;
4457 }
4458
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304459 ciq_size = 64 + adap->vres.cq.size + adap->tids.nftids;
4460 if (ciq_size > SGE_MAX_IQ_SIZE) {
4461 CH_WARN(adap, "CIQ size too small for available IQs\n");
4462 ciq_size = SGE_MAX_IQ_SIZE;
4463 }
4464
4465 for (i = 0; i < ARRAY_SIZE(s->rdmaciq); i++) {
4466 struct sge_ofld_rxq *r = &s->rdmaciq[i];
4467
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304468 init_rspq(adap, &r->rspq, 5, 1, ciq_size, 64);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304469 r->rspq.uld = CXGB4_ULD_RDMA;
4470 }
4471
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304472 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
4473 init_rspq(adap, &s->intrq, 0, 1, 2 * MAX_INGQ, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004474}
4475
4476/*
4477 * Reduce the number of Ethernet queues across all ports to at most n.
4478 * n provides at least one queue per port.
4479 */
Bill Pemberton91744942012-12-03 09:23:02 -05004480static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004481{
4482 int i;
4483 struct port_info *pi;
4484
4485 while (n < adap->sge.ethqsets)
4486 for_each_port(adap, i) {
4487 pi = adap2pinfo(adap, i);
4488 if (pi->nqsets > 1) {
4489 pi->nqsets--;
4490 adap->sge.ethqsets--;
4491 if (adap->sge.ethqsets <= n)
4492 break;
4493 }
4494 }
4495
4496 n = 0;
4497 for_each_port(adap, i) {
4498 pi = adap2pinfo(adap, i);
4499 pi->first_qset = n;
4500 n += pi->nqsets;
4501 }
4502}
4503
4504/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4505#define EXTRA_VECS 2
4506
Bill Pemberton91744942012-12-03 09:23:02 -05004507static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004508{
4509 int ofld_need = 0;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304510 int i, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004511 struct sge *s = &adap->sge;
4512 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304513 struct msix_entry *entries;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004514
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304515 entries = kmalloc(sizeof(*entries) * (MAX_INGQ + 1),
4516 GFP_KERNEL);
4517 if (!entries)
4518 return -ENOMEM;
4519
4520 for (i = 0; i < MAX_INGQ + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004521 entries[i].entry = i;
4522
4523 want = s->max_ethqsets + EXTRA_VECS;
4524 if (is_offload(adap)) {
Varun Prakashf2692d12016-02-14 23:02:40 +05304525 want += s->rdmaqs + s->rdmaciqs + s->iscsiqsets +
4526 s->niscsitq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004527 /* need nchan for each possible ULD */
Varun Prakashf2692d12016-02-14 23:02:40 +05304528 if (is_t4(adap->params.chip))
4529 ofld_need = 3 * nchan;
4530 else
4531 ofld_need = 4 * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004532 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004533#ifdef CONFIG_CHELSIO_T4_DCB
4534 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4535 * each port.
4536 */
4537 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need;
4538#else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004539 need = adap->params.nports + EXTRA_VECS + ofld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004540#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304541 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4542 if (allocated < 0) {
4543 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4544 " not using MSI-X\n");
4545 kfree(entries);
4546 return allocated;
4547 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004548
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304549 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004550 * Every group gets its minimum requirement and NIC gets top
4551 * priority for leftovers.
4552 */
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304553 i = allocated - EXTRA_VECS - ofld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004554 if (i < s->max_ethqsets) {
4555 s->max_ethqsets = i;
4556 if (i < s->ethqsets)
4557 reduce_ethqs(adap, i);
4558 }
4559 if (is_offload(adap)) {
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304560 if (allocated < want) {
4561 s->rdmaqs = nchan;
4562 s->rdmaciqs = nchan;
Varun Prakashf2692d12016-02-14 23:02:40 +05304563
4564 if (!is_t4(adap->params.chip))
4565 s->niscsitq = nchan;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304566 }
4567
4568 /* leftovers go to OFLD */
4569 i = allocated - EXTRA_VECS - s->max_ethqsets -
Varun Prakashf2692d12016-02-14 23:02:40 +05304570 s->rdmaqs - s->rdmaciqs - s->niscsitq;
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304571 s->iscsiqsets = (i / nchan) * nchan; /* round down */
Varun Prakashf2692d12016-02-14 23:02:40 +05304572
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004573 }
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304574 for (i = 0; i < allocated; ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004575 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304576 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
4577 "nic %d iscsi %d rdma cpl %d rdma ciq %d\n",
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304578 allocated, s->max_ethqsets, s->iscsiqsets, s->rdmaqs,
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304579 s->rdmaciqs);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004580
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304581 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004582 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004583}
4584
4585#undef EXTRA_VECS
4586
Bill Pemberton91744942012-12-03 09:23:02 -05004587static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004588{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304589 unsigned int i;
4590 int err;
4591
4592 err = t4_init_rss_mode(adap, adap->mbox);
4593 if (err)
4594 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004595
4596 for_each_port(adap, i) {
4597 struct port_info *pi = adap2pinfo(adap, i);
4598
4599 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4600 if (!pi->rss)
4601 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004602 }
4603 return 0;
4604}
4605
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304606static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4607 enum pci_bus_speed *speed,
4608 enum pcie_link_width *width)
4609{
4610 u32 lnkcap1, lnkcap2;
4611 int err1, err2;
4612
4613#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4614
4615 *speed = PCI_SPEED_UNKNOWN;
4616 *width = PCIE_LNK_WIDTH_UNKNOWN;
4617
4618 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4619 &lnkcap1);
4620 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4621 &lnkcap2);
4622 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4623 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4624 *speed = PCIE_SPEED_8_0GT;
4625 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4626 *speed = PCIE_SPEED_5_0GT;
4627 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4628 *speed = PCIE_SPEED_2_5GT;
4629 }
4630 if (!err1) {
4631 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4632 if (!lnkcap2) { /* pre-r3.0 */
4633 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4634 *speed = PCIE_SPEED_5_0GT;
4635 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4636 *speed = PCIE_SPEED_2_5GT;
4637 }
4638 }
4639
4640 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4641 return err1 ? err1 : err2 ? err2 : -EINVAL;
4642 return 0;
4643}
4644
4645static void cxgb4_check_pcie_caps(struct adapter *adap)
4646{
4647 enum pcie_link_width width, width_cap;
4648 enum pci_bus_speed speed, speed_cap;
4649
4650#define PCIE_SPEED_STR(speed) \
4651 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4652 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4653 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4654 "Unknown")
4655
4656 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4657 dev_warn(adap->pdev_dev,
4658 "Unable to determine PCIe device BW capabilities\n");
4659 return;
4660 }
4661
4662 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4663 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4664 dev_warn(adap->pdev_dev,
4665 "Unable to determine PCI Express bandwidth.\n");
4666 return;
4667 }
4668
4669 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4670 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4671 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4672 width, width_cap);
4673 if (speed < speed_cap || width < width_cap)
4674 dev_info(adap->pdev_dev,
4675 "A slot with more lanes and/or higher speed is "
4676 "suggested for optimal performance.\n");
4677}
4678
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304679/* Dump basic information about the adapter */
4680static void print_adapter_info(struct adapter *adapter)
4681{
4682 /* Device information */
4683 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4684 adapter->params.vpd.id,
4685 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4686 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4687 adapter->params.vpd.sn, adapter->params.vpd.pn);
4688
4689 /* Firmware Version */
4690 if (!adapter->params.fw_vers)
4691 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4692 else
4693 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4694 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4695 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4696 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4697 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4698
4699 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4700 * Firmware, so dev_info() is more appropriate here.)
4701 */
4702 if (!adapter->params.bs_vers)
4703 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4704 else
4705 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4706 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4707 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4708 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4709 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4710
4711 /* TP Microcode Version */
4712 if (!adapter->params.tp_vers)
4713 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4714 else
4715 dev_info(adapter->pdev_dev,
4716 "TP Microcode version: %u.%u.%u.%u\n",
4717 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4718 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4719 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4720 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4721
4722 /* Expansion ROM version */
4723 if (!adapter->params.er_vers)
4724 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4725 else
4726 dev_info(adapter->pdev_dev,
4727 "Expansion ROM version: %u.%u.%u.%u\n",
4728 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4729 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4730 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4731 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4732
4733 /* Software/Hardware configuration */
4734 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4735 is_offload(adapter) ? "R" : "",
4736 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4737 (adapter->flags & USING_MSI) ? "MSI" : ""),
4738 is_offload(adapter) ? "Offload" : "non-Offload");
4739}
4740
Bill Pemberton91744942012-12-03 09:23:02 -05004741static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004742{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004743 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004744 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004745 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004746 const struct port_info *pi = netdev_priv(dev);
4747 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004748
4749 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4750 spd = " 2.5 GT/s";
4751 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4752 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004753 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4754 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004755
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004756 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4757 bufp += sprintf(bufp, "100/");
4758 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4759 bufp += sprintf(bufp, "1000/");
4760 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4761 bufp += sprintf(bufp, "10G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304762 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4763 bufp += sprintf(bufp, "40G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004764 if (bufp != buf)
4765 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304766 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004767
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304768 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4769 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004770}
4771
Bill Pemberton91744942012-12-03 09:23:02 -05004772static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004773{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004774 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004775}
4776
Dimitris Michailidis06546392010-07-11 12:01:16 +00004777/*
4778 * Free the following resources:
4779 * - memory used for tables
4780 * - MSI/MSI-X
4781 * - net devices
4782 * - resources FW is holding for us
4783 */
4784static void free_some_resources(struct adapter *adapter)
4785{
4786 unsigned int i;
4787
4788 t4_free_mem(adapter->l2t);
4789 t4_free_mem(adapter->tids.tid_tab);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304790 kfree(adapter->sge.egr_map);
4791 kfree(adapter->sge.ingr_map);
4792 kfree(adapter->sge.starving_fl);
4793 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304794#ifdef CONFIG_DEBUG_FS
4795 kfree(adapter->sge.blocked_fl);
4796#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004797 disable_msi(adapter);
4798
4799 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004800 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304801 struct port_info *pi = adap2pinfo(adapter, i);
4802
4803 if (pi->viid != 0)
4804 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4805 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004806 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004807 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004808 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004809 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304810 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004811}
4812
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004813#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004814#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004815 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004816#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004817
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304818static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4819{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304820 u16 device_id;
4821
4822 /* Retrieve adapter's device ID */
4823 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004824
4825 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304826 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004827 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304828 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004829 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304830 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004831 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304832 default:
4833 dev_err(&pdev->dev, "Device %d is not supported\n",
4834 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304835 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004836 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304837}
4838
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304839#ifdef CONFIG_PCI_IOV
4840static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4841{
4842 int err = 0;
4843 int current_vfs = pci_num_vf(pdev);
4844 u32 pcie_fw;
4845 void __iomem *regs;
4846
4847 regs = pci_ioremap_bar(pdev, 0);
4848 if (!regs) {
4849 dev_err(&pdev->dev, "cannot map device registers\n");
4850 return -ENOMEM;
4851 }
4852
4853 pcie_fw = readl(regs + PCIE_FW_A);
4854 iounmap(regs);
4855 /* Check if cxgb4 is the MASTER and fw is initialized */
4856 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4857 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4858 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4859 dev_warn(&pdev->dev,
4860 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4861 return -EOPNOTSUPP;
4862 }
4863
4864 /* If any of the VF's is already assigned to Guest OS, then
4865 * SRIOV for the same cannot be modified
4866 */
4867 if (current_vfs && pci_vfs_assigned(pdev)) {
4868 dev_err(&pdev->dev,
4869 "Cannot modify SR-IOV while VFs are assigned\n");
4870 num_vfs = current_vfs;
4871 return num_vfs;
4872 }
4873
4874 /* Disable SRIOV when zero is passed.
4875 * One needs to disable SRIOV before modifying it, else
4876 * stack throws the below warning:
4877 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4878 */
4879 if (!num_vfs) {
4880 pci_disable_sriov(pdev);
4881 return num_vfs;
4882 }
4883
4884 if (num_vfs != current_vfs) {
4885 err = pci_enable_sriov(pdev, num_vfs);
4886 if (err)
4887 return err;
4888 }
4889 return num_vfs;
4890}
4891#endif
4892
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004893static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004894{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004895 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004896 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004897 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004898 struct adapter *adapter = NULL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304899 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304900 u32 whoami, pl_rev;
4901 enum chip_type chip;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004902
4903 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4904
4905 err = pci_request_regions(pdev, KBUILD_MODNAME);
4906 if (err) {
4907 /* Just info, some other driver may have claimed the device. */
4908 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4909 return err;
4910 }
4911
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004912 err = pci_enable_device(pdev);
4913 if (err) {
4914 dev_err(&pdev->dev, "cannot enable PCI device\n");
4915 goto out_release_regions;
4916 }
4917
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304918 regs = pci_ioremap_bar(pdev, 0);
4919 if (!regs) {
4920 dev_err(&pdev->dev, "cannot map device registers\n");
4921 err = -ENOMEM;
4922 goto out_disable_device;
4923 }
4924
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304925 err = t4_wait_dev_ready(regs);
4926 if (err < 0)
4927 goto out_unmap_bar0;
4928
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304929 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304930 whoami = readl(regs + PL_WHOAMI_A);
4931 pl_rev = REV_G(readl(regs + PL_REV_A));
4932 chip = get_chip_type(pdev, pl_rev);
4933 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4934 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304935 if (func != ent->driver_data) {
4936 iounmap(regs);
4937 pci_disable_device(pdev);
4938 pci_save_state(pdev); /* to restore SR-IOV later */
4939 goto sriov;
4940 }
4941
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004942 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004943 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004944 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4945 if (err) {
4946 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4947 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304948 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004949 }
4950 } else {
4951 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4952 if (err) {
4953 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304954 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004955 }
4956 }
4957
4958 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004959 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004960 pci_set_master(pdev);
4961 pci_save_state(pdev);
4962
4963 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4964 if (!adapter) {
4965 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304966 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004967 }
4968
Anish Bhatt29aaee62014-08-20 13:44:06 -07004969 adapter->workq = create_singlethread_workqueue("cxgb4");
4970 if (!adapter->workq) {
4971 err = -ENOMEM;
4972 goto out_free_adapter;
4973 }
4974
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304975 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4976 (sizeof(struct mbox_cmd) *
4977 T4_OS_LOG_MBOX_CMDS),
4978 GFP_KERNEL);
4979 if (!adapter->mbox_log) {
4980 err = -ENOMEM;
4981 goto out_free_adapter;
4982 }
4983 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4984
Gavin Shan144be3d2014-01-23 12:27:34 +08004985 /* PCI device has been enabled */
4986 adapter->flags |= DEV_ENABLED;
4987
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304988 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004989 adapter->pdev = pdev;
4990 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304991 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304992 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304993 adapter->pf = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004994 adapter->msg_enable = dflt_msg_enable;
4995 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4996
4997 spin_lock_init(&adapter->stats_lock);
4998 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004999 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005000
5001 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305002 INIT_WORK(&adapter->db_full_task, process_db_full);
5003 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005004
5005 err = t4_prep_adapter(adapter);
5006 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305007 goto out_free_adapter;
5008
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005009
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305010 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305011 s_qpp = (QUEUESPERPAGEPF0_S +
5012 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305013 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305014 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5015 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005016 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5017
5018 /* Each segment size is 128B. Write coalescing is enabled only
5019 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5020 * queue is less no of segments that can be accommodated in
5021 * a page size.
5022 */
5023 if (qpp > num_seg) {
5024 dev_err(&pdev->dev,
5025 "Incorrect number of egress queues per page\n");
5026 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305027 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005028 }
5029 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5030 pci_resource_len(pdev, 2));
5031 if (!adapter->bar2) {
5032 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5033 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305034 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005035 }
5036 }
5037
Vipul Pandya636f9d32012-09-26 02:39:39 +00005038 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005039 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305040#ifdef CONFIG_DEBUG_FS
5041 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5042#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005043 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005044 if (err)
5045 goto out_unmap_bar;
5046
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305047 /* configure SGE_STAT_CFG_A to read WC stats */
5048 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305049 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5050 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5051 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305052
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005053 for_each_port(adapter, i) {
5054 struct net_device *netdev;
5055
5056 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5057 MAX_ETH_QSETS);
5058 if (!netdev) {
5059 err = -ENOMEM;
5060 goto out_free_dev;
5061 }
5062
5063 SET_NETDEV_DEV(netdev, &pdev->dev);
5064
5065 adapter->port[i] = netdev;
5066 pi = netdev_priv(netdev);
5067 pi->adapter = adapter;
5068 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005069 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005070 netdev->irq = pdev->irq;
5071
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005072 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5073 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5074 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Patrick McHardyf6469682013-04-19 02:04:27 +00005075 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005076 if (highdma)
5077 netdev->hw_features |= NETIF_F_HIGHDMA;
5078 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005079 netdev->vlan_features = netdev->features & VLAN_FEAT;
5080
Jiri Pirko01789342011-08-16 06:29:00 +00005081 netdev->priv_flags |= IFF_UNICAST_FLT;
5082
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005083 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005084#ifdef CONFIG_CHELSIO_T4_DCB
5085 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5086 cxgb4_dcb_state_init(netdev);
5087#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305088 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005089 }
5090
5091 pci_set_drvdata(pdev, adapter);
5092
5093 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005094 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005095 if (err)
5096 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305097 } else if (adapter->params.nports == 1) {
5098 /* If we don't have a connection to the firmware -- possibly
5099 * because of an error -- grab the raw VPD parameters so we
5100 * can set the proper MAC Address on the debug network
5101 * interface that we've created.
5102 */
5103 u8 hw_addr[ETH_ALEN];
5104 u8 *na = adapter->params.vpd.na;
5105
5106 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5107 if (!err) {
5108 for (i = 0; i < ETH_ALEN; i++)
5109 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5110 hex2val(na[2 * i + 1]));
5111 t4_set_hw_addr(adapter, 0, hw_addr);
5112 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005113 }
5114
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305115 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005116 * soon as the first register_netdev completes.
5117 */
5118 cfg_queues(adapter);
5119
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305120 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005121 if (!adapter->l2t) {
5122 /* We tolerate a lack of L2T, giving up some functionality */
5123 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5124 adapter->params.offload = 0;
5125 }
5126
Anish Bhattb5a02f52015-01-14 15:17:34 -08005127#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305128 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5129 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5130 /* CLIP functionality is not present in hardware,
5131 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005132 */
5133 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305134 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005135 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305136 } else {
5137 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5138 adapter->clipt_end);
5139 if (!adapter->clipt) {
5140 /* We tolerate a lack of clip_table, giving up
5141 * some functionality
5142 */
5143 dev_warn(&pdev->dev,
5144 "could not allocate Clip table, continuing\n");
5145 adapter->params.offload = 0;
5146 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005147 }
5148#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005149 if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
5150 dev_warn(&pdev->dev, "could not allocate TID table, "
5151 "continuing\n");
5152 adapter->params.offload = 0;
5153 }
5154
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305155 if (is_offload(adapter)) {
5156 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5157 u32 hash_base, hash_reg;
5158
5159 if (chip <= CHELSIO_T5) {
5160 hash_reg = LE_DB_TID_HASHBASE_A;
5161 hash_base = t4_read_reg(adapter, hash_reg);
5162 adapter->tids.hash_base = hash_base / 4;
5163 } else {
5164 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5165 hash_base = t4_read_reg(adapter, hash_reg);
5166 adapter->tids.hash_base = hash_base;
5167 }
5168 }
5169 }
5170
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005171 /* See what interrupts we'll be using */
5172 if (msi > 1 && enable_msix(adapter) == 0)
5173 adapter->flags |= USING_MSIX;
5174 else if (msi > 0 && pci_enable_msi(pdev) == 0)
5175 adapter->flags |= USING_MSI;
5176
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305177 /* check for PCI Express bandwidth capabiltites */
5178 cxgb4_check_pcie_caps(adapter);
5179
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005180 err = init_rss(adapter);
5181 if (err)
5182 goto out_free_dev;
5183
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005184 /*
5185 * The card is now ready to go. If any errors occur during device
5186 * registration we do not fail the whole card but rather proceed only
5187 * with the ports we manage to register successfully. However we must
5188 * register at least one net device.
5189 */
5190 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005191 pi = adap2pinfo(adapter, i);
5192 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5193 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5194
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005195 err = register_netdev(adapter->port[i]);
5196 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005197 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005198 adapter->chan_map[pi->tx_chan] = i;
5199 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005200 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005201 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005202 dev_err(&pdev->dev, "could not register any net devices\n");
5203 goto out_free_dev;
5204 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005205 if (err) {
5206 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5207 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005208 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005209
5210 if (cxgb4_debugfs_root) {
5211 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5212 cxgb4_debugfs_root);
5213 setup_debugfs(adapter);
5214 }
5215
David S. Miller88c51002011-10-07 13:38:43 -04005216 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5217 pdev->needs_freset = 1;
5218
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005219 if (is_offload(adapter))
5220 attach_ulds(adapter);
5221
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305222 print_adapter_info(adapter);
5223
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305224sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005225#ifdef CONFIG_PCI_IOV
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305226 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0) {
5227 dev_warn(&pdev->dev,
5228 "Enabling SR-IOV VFs using the num_vf module "
5229 "parameter is deprecated - please use the pci sysfs "
5230 "interface instead.\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005231 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
5232 dev_info(&pdev->dev,
5233 "instantiated %u virtual functions\n",
5234 num_vf[func]);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305235 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005236#endif
5237 return 0;
5238
5239 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005240 free_some_resources(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005241 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305242 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005243 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005244 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005245 if (adapter->workq)
5246 destroy_workqueue(adapter->workq);
5247
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305248 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005249 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305250 out_unmap_bar0:
5251 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005252 out_disable_device:
5253 pci_disable_pcie_error_reporting(pdev);
5254 pci_disable_device(pdev);
5255 out_release_regions:
5256 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005257 return err;
5258}
5259
Bill Pemberton91744942012-12-03 09:23:02 -05005260static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005261{
5262 struct adapter *adapter = pci_get_drvdata(pdev);
5263
Vipul Pandya636f9d32012-09-26 02:39:39 +00005264#ifdef CONFIG_PCI_IOV
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005265 pci_disable_sriov(pdev);
5266
Vipul Pandya636f9d32012-09-26 02:39:39 +00005267#endif
5268
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005269 if (adapter) {
5270 int i;
5271
Anish Bhatt29aaee62014-08-20 13:44:06 -07005272 /* Tear down per-adapter Work Queue first since it can contain
5273 * references to our adapter data structure.
5274 */
5275 destroy_workqueue(adapter->workq);
5276
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005277 if (is_offload(adapter))
5278 detach_ulds(adapter);
5279
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305280 disable_interrupts(adapter);
5281
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005282 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005283 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005284 unregister_netdev(adapter->port[i]);
5285
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005286 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005287
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005288 /* If we allocated filters, free up state associated with any
5289 * valid filters ...
5290 */
5291 if (adapter->tids.ftid_tab) {
5292 struct filter_entry *f = &adapter->tids.ftid_tab[0];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00005293 for (i = 0; i < (adapter->tids.nftids +
5294 adapter->tids.nsftids); i++, f++)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005295 if (f->valid)
5296 clear_filter(adapter, f);
5297 }
5298
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005299 if (adapter->flags & FULL_INIT_DONE)
5300 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005301
Dimitris Michailidis06546392010-07-11 12:01:16 +00005302 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005303#if IS_ENABLED(CONFIG_IPV6)
5304 t4_cleanup_clip_tbl(adapter);
5305#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005306 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305307 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005308 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005309 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005310 if ((adapter->flags & DEV_ENABLED)) {
5311 pci_disable_device(pdev);
5312 adapter->flags &= ~DEV_ENABLED;
5313 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005314 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305315 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005316 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005317 kfree(adapter);
Dimitris Michailidisa069ec92010-09-30 09:17:12 +00005318 } else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005319 pci_release_regions(pdev);
5320}
5321
5322static struct pci_driver cxgb4_driver = {
5323 .name = KBUILD_MODNAME,
5324 .id_table = cxgb4_pci_tbl,
5325 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005326 .remove = remove_one,
Thadeu Lima de Souza Cascardo687d7052014-02-24 17:04:52 -03005327 .shutdown = remove_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305328#ifdef CONFIG_PCI_IOV
5329 .sriov_configure = cxgb4_iov_configure,
5330#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005331 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005332};
5333
5334static int __init cxgb4_init_module(void)
5335{
5336 int ret;
5337
5338 /* Debugfs support is optional, just warn if this fails */
5339 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5340 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005341 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005342
5343 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005344 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005345 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305346
Anish Bhatt1bb60372014-10-14 20:07:22 -07005347#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005348 if (!inet6addr_registered) {
5349 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5350 inet6addr_registered = true;
5351 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005352#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305353
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005354 return ret;
5355}
5356
5357static void __exit cxgb4_cleanup_module(void)
5358{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005359#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305360 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005361 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5362 inet6addr_registered = false;
5363 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005364#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005365 pci_unregister_driver(&cxgb4_driver);
5366 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005367}
5368
5369module_init(cxgb4_init_module);
5370module_exit(cxgb4_cleanup_module);