blob: a1e329ec24cd1fd8e6d6d2cf68a8d55d886e69d1 [file] [log] [blame]
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
Anish Bhattce100b8b2014-06-19 21:37:15 -07004 * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/bitmap.h>
38#include <linux/crc32.h>
39#include <linux/ctype.h>
40#include <linux/debugfs.h>
41#include <linux/err.h>
42#include <linux/etherdevice.h>
43#include <linux/firmware.h>
Jiri Pirko01789342011-08-16 06:29:00 +000044#include <linux/if.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000045#include <linux/if_vlan.h>
46#include <linux/init.h>
47#include <linux/log2.h>
48#include <linux/mdio.h>
49#include <linux/module.h>
50#include <linux/moduleparam.h>
51#include <linux/mutex.h>
52#include <linux/netdevice.h>
53#include <linux/pci.h>
54#include <linux/aer.h>
55#include <linux/rtnetlink.h>
56#include <linux/sched.h>
57#include <linux/seq_file.h>
58#include <linux/sockios.h>
59#include <linux/vmalloc.h>
60#include <linux/workqueue.h>
61#include <net/neighbour.h>
62#include <net/netevent.h>
Vipul Pandya01bcca62013-07-04 16:10:46 +053063#include <net/addrconf.h>
David S. Miller1ef80192014-11-10 13:27:49 -050064#include <net/bonding.h>
Anish Bhattb5a02f52015-01-14 15:17:34 -080065#include <net/addrconf.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000066#include <asm/uaccess.h>
67
68#include "cxgb4.h"
69#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053070#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000071#include "t4_msg.h"
72#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053073#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070074#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053075#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080076#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000077#include "l2t.h"
78
Hariprasad Shenai812034f2015-04-06 20:23:23 +053079char cxgb4_driver_name[] = KBUILD_MODNAME;
80
Vipul Pandya01bcca62013-07-04 16:10:46 +053081#ifdef DRV_VERSION
82#undef DRV_VERSION
83#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000084#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053085const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053086#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000087
Vipul Pandyaf2b7e782012-12-10 09:30:52 +000088/* Host shadow copy of ingress filter entry. This is in host native format
89 * and doesn't match the ordering or bit order, etc. of the hardware of the
90 * firmware command. The use of bit-field structure elements is purely to
91 * remind ourselves of the field size limitations and save memory in the case
92 * where the filter table is large.
93 */
94struct filter_entry {
95 /* Administrative fields for filter.
96 */
97 u32 valid:1; /* filter allocated and valid */
98 u32 locked:1; /* filter is administratively locked */
99
100 u32 pending:1; /* filter action is pending firmware reply */
101 u32 smtidx:8; /* Source MAC Table index for smac */
102 struct l2t_entry *l2t; /* Layer Two Table entry for dmac */
103
104 /* The filter itself. Most of this is a straight copy of information
105 * provided by the extended ioctl(). Some fields are translated to
106 * internal forms -- for instance the Ingress Queue ID passed in from
107 * the ioctl() is translated into the Absolute Ingress Queue ID.
108 */
109 struct ch_filter_specification fs;
110};
111
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000112#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
113 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
114 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
115
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530116/* Macros needed to support the PCI Device ID Table ...
117 */
118#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530119 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530120#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000121
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530122/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
123 * called for both.
124 */
125#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
126
127#define CH_PCI_ID_TABLE_ENTRY(devid) \
128 {PCI_VDEVICE(CHELSIO, (devid)), 4}
129
130#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
131 { 0, } \
132 }
133
134#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000135
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530136#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000137#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530138#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530139#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000140#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530141#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530142#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
143#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
144#define PHY_AQ1202_DEVICEID 0x4409
145#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000146
147MODULE_DESCRIPTION(DRV_DESC);
148MODULE_AUTHOR("Chelsio Communications");
149MODULE_LICENSE("Dual BSD/GPL");
150MODULE_VERSION(DRV_VERSION);
151MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530152MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000153MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530154MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000155
Vipul Pandya636f9d32012-09-26 02:39:39 +0000156/*
157 * Normally we're willing to become the firmware's Master PF but will be happy
158 * if another PF has already become the Master and initialized the adapter.
159 * Setting "force_init" will cause this driver to forcibly establish itself as
160 * the Master PF and initialize the adapter.
161 */
162static uint force_init;
163
164module_param(force_init, uint, 0644);
Hariprasad Shenaid7d3e252015-12-24 16:24:53 +0530165MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
166 "deprecated parameter");
Vipul Pandya13ee15d2012-09-26 02:39:40 +0000167
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000168static int dflt_msg_enable = DFLT_MSG_ENABLE;
169
170module_param(dflt_msg_enable, int, 0644);
Hariprasad Shenai8a21ec42016-04-05 09:52:21 +0530171MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
172 "deprecated parameter");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000173
174/*
175 * The driver uses the best interrupt scheme available on a platform in the
176 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
177 * of these schemes the driver may consider as follows:
178 *
179 * msi = 2: choose from among all three options
180 * msi = 1: only consider MSI and INTx interrupts
181 * msi = 0: force INTx interrupts
182 */
183static int msi = 2;
184
185module_param(msi, int, 0644);
186MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
187
188/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000189 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
190 * offset by 2 bytes in order to have the IP headers line up on 4-byte
191 * boundaries. This is a requirement for many architectures which will throw
192 * a machine check fault if an attempt is made to access one of the 4-byte IP
193 * header fields on a non-4-byte boundary. And it's a major performance issue
194 * even on some architectures which allow it like some implementations of the
195 * x86 ISA. However, some architectures don't mind this and for some very
196 * edge-case performance sensitive applications (like forwarding large volumes
197 * of small packets), setting this DMA offset to 0 will decrease the number of
198 * PCI-E Bus transfers enough to measurably affect performance.
199 */
200static int rx_dma_offset = 2;
201
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000202#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000203/* Configure the number of PCI-E Virtual Function which are to be instantiated
204 * on SR-IOV Capable Physical Functions.
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000205 */
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000206static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000207
208module_param_array(num_vf, uint, NULL, 0644);
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000209MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000210#endif
211
Anish Bhatt688848b2014-06-19 21:37:13 -0700212/* TX Queue select used to determine what algorithm to use for selecting TX
213 * queue. Select between the kernel provided function (select_queue=0) or user
214 * cxgb_select_queue function (select_queue=1)
215 *
216 * Default: select_queue=0
217 */
218static int select_queue;
219module_param(select_queue, int, 0644);
220MODULE_PARM_DESC(select_queue,
221 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
222
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000223static struct dentry *cxgb4_debugfs_root;
224
225static LIST_HEAD(adapter_list);
226static DEFINE_MUTEX(uld_mutex);
Vipul Pandya01bcca62013-07-04 16:10:46 +0530227/* Adapter list to be accessed from atomic context */
228static LIST_HEAD(adap_rcu_list);
229static DEFINE_SPINLOCK(adap_rcu_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000230static struct cxgb4_uld_info ulds[CXGB4_ULD_MAX];
Varun Prakashf2692d12016-02-14 23:02:40 +0530231static const char *const uld_str[] = { "RDMA", "iSCSI", "iSCSIT" };
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000232
233static void link_report(struct net_device *dev)
234{
235 if (!netif_carrier_ok(dev))
236 netdev_info(dev, "link down\n");
237 else {
238 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
239
Hariprasad Shenai85412252015-10-01 13:48:48 +0530240 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000241 const struct port_info *p = netdev_priv(dev);
242
243 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000244 case 10000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000245 s = "10Gbps";
246 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000247 case 1000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000248 s = "1000Mbps";
249 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000250 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000251 s = "100Mbps";
252 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000253 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530254 s = "40Gbps";
255 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530256 default:
257 pr_info("%s: unsupported speed: %d\n",
258 dev->name, p->link_cfg.speed);
259 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000260 }
261
262 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
263 fc[p->link_cfg.fc]);
264 }
265}
266
Anish Bhatt688848b2014-06-19 21:37:13 -0700267#ifdef CONFIG_CHELSIO_T4_DCB
268/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
269static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
270{
271 struct port_info *pi = netdev_priv(dev);
272 struct adapter *adap = pi->adapter;
273 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
274 int i;
275
276 /* We use a simple mapping of Port TX Queue Index to DCB
277 * Priority when we're enabling DCB.
278 */
279 for (i = 0; i < pi->nqsets; i++, txq++) {
280 u32 name, value;
281 int err;
282
Hariprasad Shenai51678652014-11-21 12:52:02 +0530283 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
284 FW_PARAMS_PARAM_X_V(
285 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
286 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700287 value = enable ? i : 0xffffffff;
288
289 /* Since we can be called while atomic (from "interrupt
290 * level") we need to issue the Set Parameters Commannd
291 * without sleeping (timeout < 0).
292 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530293 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530294 &name, &value,
295 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700296
297 if (err)
298 dev_err(adap->pdev_dev,
299 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
300 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700301 else
302 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700303 }
304}
305#endif /* CONFIG_CHELSIO_T4_DCB */
306
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000307void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
308{
309 struct net_device *dev = adapter->port[port_id];
310
311 /* Skip changes from disabled ports. */
312 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
313 if (link_stat)
314 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700315 else {
316#ifdef CONFIG_CHELSIO_T4_DCB
317 cxgb4_dcb_state_init(dev);
318 dcb_tx_queue_prio_enable(dev, false);
319#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000320 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700321 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000322
323 link_report(dev);
324 }
325}
326
327void t4_os_portmod_changed(const struct adapter *adap, int port_id)
328{
329 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000330 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000331 };
332
333 const struct net_device *dev = adap->port[port_id];
334 const struct port_info *pi = netdev_priv(dev);
335
336 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
337 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000338 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000339 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
340}
341
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530342int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
343module_param(dbfifo_int_thresh, int, 0644);
344MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
345
Vipul Pandya404d9e32012-10-08 02:59:43 +0000346/*
347 * usecs to sleep while draining the dbfifo
348 */
349static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530350module_param(dbfifo_drain_delay, int, 0644);
351MODULE_PARM_DESC(dbfifo_drain_delay,
352 "usecs to sleep while draining the dbfifo");
353
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530354static inline int cxgb4_set_addr_hash(struct port_info *pi)
355{
356 struct adapter *adap = pi->adapter;
357 u64 vec = 0;
358 bool ucast = false;
359 struct hash_mac_addr *entry;
360
361 /* Calculate the hash vector for the updated list and program it */
362 list_for_each_entry(entry, &adap->mac_hlist, list) {
363 ucast |= is_unicast_ether_addr(entry->addr);
364 vec |= (1ULL << hash_mac_addr(entry->addr));
365 }
366 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
367 vec, false);
368}
369
370static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
371{
372 struct port_info *pi = netdev_priv(netdev);
373 struct adapter *adap = pi->adapter;
374 int ret;
375 u64 mhash = 0;
376 u64 uhash = 0;
377 bool free = false;
378 bool ucast = is_unicast_ether_addr(mac_addr);
379 const u8 *maclist[1] = {mac_addr};
380 struct hash_mac_addr *new_entry;
381
382 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
383 NULL, ucast ? &uhash : &mhash, false);
384 if (ret < 0)
385 goto out;
386 /* if hash != 0, then add the addr to hash addr list
387 * so on the end we will calculate the hash for the
388 * list and program it
389 */
390 if (uhash || mhash) {
391 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
392 if (!new_entry)
393 return -ENOMEM;
394 ether_addr_copy(new_entry->addr, mac_addr);
395 list_add_tail(&new_entry->list, &adap->mac_hlist);
396 ret = cxgb4_set_addr_hash(pi);
397 }
398out:
399 return ret < 0 ? ret : 0;
400}
401
402static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
403{
404 struct port_info *pi = netdev_priv(netdev);
405 struct adapter *adap = pi->adapter;
406 int ret;
407 const u8 *maclist[1] = {mac_addr};
408 struct hash_mac_addr *entry, *tmp;
409
410 /* If the MAC address to be removed is in the hash addr
411 * list, delete it from the list and update hash vector
412 */
413 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
414 if (ether_addr_equal(entry->addr, mac_addr)) {
415 list_del(&entry->list);
416 kfree(entry);
417 return cxgb4_set_addr_hash(pi);
418 }
419 }
420
421 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
422 return ret < 0 ? -EINVAL : 0;
423}
424
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000425/*
426 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
427 * If @mtu is -1 it is left unchanged.
428 */
429static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
430{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000431 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530432 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000433
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530434 if (!(dev->flags & IFF_PROMISC)) {
435 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
436 if (!(dev->flags & IFF_ALLMULTI))
437 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
438 }
439
440 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
441 (dev->flags & IFF_PROMISC) ? 1 : 0,
442 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
443 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000444}
445
446/**
447 * link_start - enable a port
448 * @dev: the port to enable
449 *
450 * Performs the MAC and PHY actions needed to enable a port.
451 */
452static int link_start(struct net_device *dev)
453{
454 int ret;
455 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530456 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000457
458 /*
459 * We do not set address filters and promiscuity here, the stack does
460 * that step explicitly.
461 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000462 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000463 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000464 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000465 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000466 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000467 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000468 if (ret >= 0) {
469 pi->xact_addr_filt = ret;
470 ret = 0;
471 }
472 }
473 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530474 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000475 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700476 if (ret == 0) {
477 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700478 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
479 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700480 local_bh_enable();
481 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700482
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000483 return ret;
484}
485
Anish Bhatt688848b2014-06-19 21:37:13 -0700486int cxgb4_dcb_enabled(const struct net_device *dev)
487{
488#ifdef CONFIG_CHELSIO_T4_DCB
489 struct port_info *pi = netdev_priv(dev);
490
Anish Bhatt3bb06262014-10-23 14:37:31 -0700491 if (!pi->dcb.enabled)
492 return 0;
493
494 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
495 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Anish Bhatt688848b2014-06-19 21:37:13 -0700496#else
497 return 0;
498#endif
499}
500EXPORT_SYMBOL(cxgb4_dcb_enabled);
501
502#ifdef CONFIG_CHELSIO_T4_DCB
503/* Handle a Data Center Bridging update message from the firmware. */
504static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
505{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530506 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700507 struct net_device *dev = adap->port[port];
508 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
509 int new_dcb_enabled;
510
511 cxgb4_dcb_handle_fw_update(adap, pcmd);
512 new_dcb_enabled = cxgb4_dcb_enabled(dev);
513
514 /* If the DCB has become enabled or disabled on the port then we're
515 * going to need to set up/tear down DCB Priority parameters for the
516 * TX Queues associated with the port.
517 */
518 if (new_dcb_enabled != old_dcb_enabled)
519 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
520}
521#endif /* CONFIG_CHELSIO_T4_DCB */
522
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000523/* Clear a filter and release any of its resources that we own. This also
524 * clears the filter's "pending" status.
525 */
526static void clear_filter(struct adapter *adap, struct filter_entry *f)
527{
528 /* If the new or old filter have loopback rewriteing rules then we'll
529 * need to free any existing Layer Two Table (L2T) entries of the old
530 * filter rule. The firmware will handle freeing up any Source MAC
531 * Table (SMT) entries used for rewriting Source MAC Addresses in
532 * loopback rules.
533 */
534 if (f->l2t)
535 cxgb4_l2t_release(f->l2t);
536
537 /* The zeroing of the filter rule below clears the filter valid,
538 * pending, locked flags, l2t pointer, etc. so it's all we need for
539 * this operation.
540 */
541 memset(f, 0, sizeof(*f));
542}
543
544/* Handle a filter write/deletion reply.
545 */
546static void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
547{
548 unsigned int idx = GET_TID(rpl);
549 unsigned int nidx = idx - adap->tids.ftid_base;
550 unsigned int ret;
551 struct filter_entry *f;
552
553 if (idx >= adap->tids.ftid_base && nidx <
554 (adap->tids.nftids + adap->tids.nsftids)) {
555 idx = nidx;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800556 ret = TCB_COOKIE_G(rpl->cookie);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000557 f = &adap->tids.ftid_tab[idx];
558
559 if (ret == FW_FILTER_WR_FLT_DELETED) {
560 /* Clear the filter when we get confirmation from the
561 * hardware that the filter has been deleted.
562 */
563 clear_filter(adap, f);
564 } else if (ret == FW_FILTER_WR_SMT_TBL_FULL) {
565 dev_err(adap->pdev_dev, "filter %u setup failed due to full SMT\n",
566 idx);
567 clear_filter(adap, f);
568 } else if (ret == FW_FILTER_WR_FLT_ADDED) {
569 f->smtidx = (be64_to_cpu(rpl->oldval) >> 24) & 0xff;
570 f->pending = 0; /* asynchronous setup completed */
571 f->valid = 1;
572 } else {
573 /* Something went wrong. Issue a warning about the
574 * problem and clear everything out.
575 */
576 dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
577 idx, ret);
578 clear_filter(adap, f);
579 }
580 }
581}
582
583/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000584 */
585static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
586 const struct pkt_gl *gl)
587{
588 u8 opcode = ((const struct rss_header *)rsp)->opcode;
589
590 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000591
592 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
593 */
594 if (unlikely(opcode == CPL_FW4_MSG &&
595 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
596 rsp++;
597 opcode = ((const struct rss_header *)rsp)->opcode;
598 rsp++;
599 if (opcode != CPL_SGE_EGR_UPDATE) {
600 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
601 , opcode);
602 goto out;
603 }
604 }
605
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000606 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
607 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800608 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000609 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000610
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000611 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000612 txq->restarts++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000613 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000614 struct sge_eth_txq *eq;
615
616 eq = container_of(txq, struct sge_eth_txq, q);
617 netif_tx_wake_queue(eq->txq);
618 } else {
619 struct sge_ofld_txq *oq;
620
621 oq = container_of(txq, struct sge_ofld_txq, q);
622 tasklet_schedule(&oq->qresume_tsk);
623 }
624 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
625 const struct cpl_fw6_msg *p = (void *)rsp;
626
Anish Bhatt688848b2014-06-19 21:37:13 -0700627#ifdef CONFIG_CHELSIO_T4_DCB
628 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530629 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700630 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530631 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700632
633 if (cmd == FW_PORT_CMD &&
634 action == FW_PORT_ACTION_GET_PORT_INFO) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530635 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700636 be32_to_cpu(pcmd->op_to_portid));
637 struct net_device *dev = q->adap->port[port];
638 int state_input = ((pcmd->u.info.dcbxdis_pkd &
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530639 FW_PORT_CMD_DCBXDIS_F)
Anish Bhatt688848b2014-06-19 21:37:13 -0700640 ? CXGB4_DCB_INPUT_FW_DISABLED
641 : CXGB4_DCB_INPUT_FW_ENABLED);
642
643 cxgb4_dcb_state_fsm(dev, state_input);
644 }
645
646 if (cmd == FW_PORT_CMD &&
647 action == FW_PORT_ACTION_L2_DCB_CFG)
648 dcb_rpl(q->adap, pcmd);
649 else
650#endif
651 if (p->type == 0)
652 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000653 } else if (opcode == CPL_L2T_WRITE_RPL) {
654 const struct cpl_l2t_write_rpl *p = (void *)rsp;
655
656 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000657 } else if (opcode == CPL_SET_TCB_RPL) {
658 const struct cpl_set_tcb_rpl *p = (void *)rsp;
659
660 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000661 } else
662 dev_err(q->adap->pdev_dev,
663 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000664out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000665 return 0;
666}
667
Varun Prakash2337ba42016-02-14 23:02:41 +0530668/* Flush the aggregated lro sessions */
669static void uldrx_flush_handler(struct sge_rspq *q)
670{
671 if (ulds[q->uld].lro_flush)
672 ulds[q->uld].lro_flush(&q->lro_mgr);
673}
674
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000675/**
676 * uldrx_handler - response queue handler for ULD queues
677 * @q: the response queue that received the packet
678 * @rsp: the response queue descriptor holding the offload message
679 * @gl: the gather list of packet fragments
680 *
681 * Deliver an ingress offload packet to a ULD. All processing is done by
682 * the ULD, we just maintain statistics.
683 */
684static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
685 const struct pkt_gl *gl)
686{
687 struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
Varun Prakash2337ba42016-02-14 23:02:41 +0530688 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000689
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000690 /* FW can send CPLs encapsulated in a CPL_FW4_MSG.
691 */
692 if (((const struct rss_header *)rsp)->opcode == CPL_FW4_MSG &&
693 ((const struct cpl_fw4_msg *)(rsp + 1))->type == FW_TYPE_RSSCPL)
694 rsp += 2;
695
Varun Prakash2337ba42016-02-14 23:02:41 +0530696 if (q->flush_handler)
697 ret = ulds[q->uld].lro_rx_handler(q->adap->uld_handle[q->uld],
698 rsp, gl, &q->lro_mgr,
699 &q->napi);
700 else
701 ret = ulds[q->uld].rx_handler(q->adap->uld_handle[q->uld],
702 rsp, gl);
703
704 if (ret) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000705 rxq->stats.nomem++;
706 return -1;
707 }
Varun Prakash2337ba42016-02-14 23:02:41 +0530708
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000709 if (gl == NULL)
710 rxq->stats.imm++;
711 else if (gl == CXGB4_MSG_AN)
712 rxq->stats.an++;
713 else
714 rxq->stats.pkts++;
715 return 0;
716}
717
718static void disable_msi(struct adapter *adapter)
719{
720 if (adapter->flags & USING_MSIX) {
721 pci_disable_msix(adapter->pdev);
722 adapter->flags &= ~USING_MSIX;
723 } else if (adapter->flags & USING_MSI) {
724 pci_disable_msi(adapter->pdev);
725 adapter->flags &= ~USING_MSI;
726 }
727}
728
729/*
730 * Interrupt handler for non-data events used with MSI-X.
731 */
732static irqreturn_t t4_nondata_intr(int irq, void *cookie)
733{
734 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530735 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000736
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530737 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000738 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530739 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000740 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530741 if (adap->flags & MASTER_PF)
742 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000743 return IRQ_HANDLED;
744}
745
746/*
747 * Name the MSI-X interrupts.
748 */
749static void name_msix_vecs(struct adapter *adap)
750{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000751 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000752
753 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000754 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000755
756 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000757 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
758 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000759
760 /* Ethernet queues */
761 for_each_port(adap, j) {
762 struct net_device *d = adap->port[j];
763 const struct port_info *pi = netdev_priv(d);
764
Dimitris Michailidisba278162010-12-14 21:36:50 +0000765 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000766 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
767 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000768 }
769
770 /* offload queues */
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530771 for_each_iscsirxq(&adap->sge, i)
772 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-iscsi%d",
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000773 adap->port[0]->name, i);
Dimitris Michailidisba278162010-12-14 21:36:50 +0000774
Varun Prakashf2692d12016-02-14 23:02:40 +0530775 for_each_iscsitrxq(&adap->sge, i)
776 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-iSCSIT%d",
777 adap->port[0]->name, i);
778
Dimitris Michailidisba278162010-12-14 21:36:50 +0000779 for_each_rdmarxq(&adap->sge, i)
780 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma%d",
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000781 adap->port[0]->name, i);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530782
783 for_each_rdmaciq(&adap->sge, i)
784 snprintf(adap->msix_info[msi_idx++].desc, n, "%s-rdma-ciq%d",
785 adap->port[0]->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000786}
787
788static int request_msix_queue_irqs(struct adapter *adap)
789{
790 struct sge *s = &adap->sge;
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530791 int err, ethqidx, iscsiqidx = 0, rdmaqidx = 0, rdmaciqqidx = 0;
Varun Prakashf2692d12016-02-14 23:02:40 +0530792 int iscsitqidx = 0;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530793 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000794
795 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
796 adap->msix_info[1].desc, &s->fw_evtq);
797 if (err)
798 return err;
799
800 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000801 err = request_irq(adap->msix_info[msi_index].vec,
802 t4_sge_intr_msix, 0,
803 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000804 &s->ethrxq[ethqidx].rspq);
805 if (err)
806 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000807 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000808 }
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530809 for_each_iscsirxq(s, iscsiqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000810 err = request_irq(adap->msix_info[msi_index].vec,
811 t4_sge_intr_msix, 0,
812 adap->msix_info[msi_index].desc,
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530813 &s->iscsirxq[iscsiqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000814 if (err)
815 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000816 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000817 }
Varun Prakashf2692d12016-02-14 23:02:40 +0530818 for_each_iscsitrxq(s, iscsitqidx) {
819 err = request_irq(adap->msix_info[msi_index].vec,
820 t4_sge_intr_msix, 0,
821 adap->msix_info[msi_index].desc,
822 &s->iscsitrxq[iscsitqidx].rspq);
823 if (err)
824 goto unwind;
825 msi_index++;
826 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000827 for_each_rdmarxq(s, rdmaqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000828 err = request_irq(adap->msix_info[msi_index].vec,
829 t4_sge_intr_msix, 0,
830 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000831 &s->rdmarxq[rdmaqidx].rspq);
832 if (err)
833 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000834 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000835 }
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530836 for_each_rdmaciq(s, rdmaciqqidx) {
837 err = request_irq(adap->msix_info[msi_index].vec,
838 t4_sge_intr_msix, 0,
839 adap->msix_info[msi_index].desc,
840 &s->rdmaciq[rdmaciqqidx].rspq);
841 if (err)
842 goto unwind;
843 msi_index++;
844 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000845 return 0;
846
847unwind:
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530848 while (--rdmaciqqidx >= 0)
849 free_irq(adap->msix_info[--msi_index].vec,
850 &s->rdmaciq[rdmaciqqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000851 while (--rdmaqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000852 free_irq(adap->msix_info[--msi_index].vec,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000853 &s->rdmarxq[rdmaqidx].rspq);
Varun Prakashf2692d12016-02-14 23:02:40 +0530854 while (--iscsitqidx >= 0)
855 free_irq(adap->msix_info[--msi_index].vec,
856 &s->iscsitrxq[iscsitqidx].rspq);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530857 while (--iscsiqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000858 free_irq(adap->msix_info[--msi_index].vec,
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530859 &s->iscsirxq[iscsiqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000860 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000861 free_irq(adap->msix_info[--msi_index].vec,
862 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000863 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
864 return err;
865}
866
867static void free_msix_queue_irqs(struct adapter *adap)
868{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000869 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000870 struct sge *s = &adap->sge;
871
872 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
873 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000874 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +0530875 for_each_iscsirxq(s, i)
876 free_irq(adap->msix_info[msi_index++].vec,
877 &s->iscsirxq[i].rspq);
Varun Prakashf2692d12016-02-14 23:02:40 +0530878 for_each_iscsitrxq(s, i)
879 free_irq(adap->msix_info[msi_index++].vec,
880 &s->iscsitrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000881 for_each_rdmarxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000882 free_irq(adap->msix_info[msi_index++].vec, &s->rdmarxq[i].rspq);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530883 for_each_rdmaciq(s, i)
884 free_irq(adap->msix_info[msi_index++].vec, &s->rdmaciq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000885}
886
887/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530888 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000889 * @pi: the port
890 * @queues: array of queue indices for RSS
891 *
892 * Sets up the portion of the HW RSS table for the port's VI to distribute
893 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530894 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000895 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530896int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000897{
898 u16 *rss;
899 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530900 struct adapter *adapter = pi->adapter;
901 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000902
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530903 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000904 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
905 if (!rss)
906 return -ENOMEM;
907
908 /* map the queue indices to queue ids */
909 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530910 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000911
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530912 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000913 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530914 /* If Tunnel All Lookup isn't specified in the global RSS
915 * Configuration, then we need to specify a default Ingress
916 * Queue for any ingress packets which aren't hashed. We'll
917 * use our first ingress queue ...
918 */
919 if (!err)
920 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
921 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
922 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
923 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
924 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
925 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
926 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000927 kfree(rss);
928 return err;
929}
930
931/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000932 * setup_rss - configure RSS
933 * @adap: the adapter
934 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000935 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000936 */
937static int setup_rss(struct adapter *adap)
938{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530939 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000940
941 for_each_port(adap, i) {
942 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000943
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530944 /* Fill default values with equal distribution */
945 for (j = 0; j < pi->rss_size; j++)
946 pi->rss[j] = j % pi->nqsets;
947
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530948 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000949 if (err)
950 return err;
951 }
952 return 0;
953}
954
955/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000956 * Return the channel of the ingress queue with the given qid.
957 */
958static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
959{
960 qid -= p->ingr_start;
961 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
962}
963
964/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000965 * Wait until all NAPI handlers are descheduled.
966 */
967static void quiesce_rx(struct adapter *adap)
968{
969 int i;
970
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530971 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000972 struct sge_rspq *q = adap->sge.ingr_map[i];
973
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530974 if (q && q->handler) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000975 napi_disable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530976 local_bh_disable();
977 while (!cxgb_poll_lock_napi(q))
978 mdelay(1);
979 local_bh_enable();
980 }
981
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000982 }
983}
984
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530985/* Disable interrupt and napi handler */
986static void disable_interrupts(struct adapter *adap)
987{
988 if (adap->flags & FULL_INIT_DONE) {
989 t4_intr_disable(adap);
990 if (adap->flags & USING_MSIX) {
991 free_msix_queue_irqs(adap);
992 free_irq(adap->msix_info[0].vec, adap);
993 } else {
994 free_irq(adap->pdev->irq, adap);
995 }
996 quiesce_rx(adap);
997 }
998}
999
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001000/*
1001 * Enable NAPI scheduling and interrupt generation for all Rx queues.
1002 */
1003static void enable_rx(struct adapter *adap)
1004{
1005 int i;
1006
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301007 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001008 struct sge_rspq *q = adap->sge.ingr_map[i];
1009
1010 if (!q)
1011 continue;
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301012 if (q->handler) {
1013 cxgb_busy_poll_init_lock(q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001014 napi_enable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05301015 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001016 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301017 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
1018 SEINTARM_V(q->intr_params) |
1019 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001020 }
1021}
1022
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301023static int alloc_ofld_rxqs(struct adapter *adap, struct sge_ofld_rxq *q,
1024 unsigned int nq, unsigned int per_chan, int msi_idx,
Varun Prakash2337ba42016-02-14 23:02:41 +05301025 u16 *ids, bool lro)
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301026{
1027 int i, err;
1028
1029 for (i = 0; i < nq; i++, q++) {
1030 if (msi_idx > 0)
1031 msi_idx++;
1032 err = t4_sge_alloc_rxq(adap, &q->rspq, false,
1033 adap->port[i / per_chan],
1034 msi_idx, q->fl.size ? &q->fl : NULL,
Varun Prakash2337ba42016-02-14 23:02:41 +05301035 uldrx_handler,
1036 lro ? uldrx_flush_handler : NULL,
1037 0);
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301038 if (err)
1039 return err;
1040 memset(&q->stats, 0, sizeof(q->stats));
1041 if (ids)
1042 ids[i] = q->rspq.abs_id;
1043 }
1044 return 0;
1045}
1046
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001047/**
1048 * setup_sge_queues - configure SGE Tx/Rx/response queues
1049 * @adap: the adapter
1050 *
1051 * Determines how many sets of SGE queues to use and initializes them.
1052 * We support multiple queue sets per port if we have MSI-X, otherwise
1053 * just one queue set per port.
1054 */
1055static int setup_sge_queues(struct adapter *adap)
1056{
1057 int err, msi_idx, i, j;
1058 struct sge *s = &adap->sge;
1059
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301060 bitmap_zero(s->starving_fl, s->egr_sz);
1061 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001062
1063 if (adap->flags & USING_MSIX)
1064 msi_idx = 1; /* vector 0 is for non-queue interrupts */
1065 else {
1066 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +05301067 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001068 if (err)
1069 return err;
1070 msi_idx = -((int)s->intrq.abs_id + 1);
1071 }
1072
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05301073 /* NOTE: If you add/delete any Ingress/Egress Queue allocations in here,
1074 * don't forget to update the following which need to be
1075 * synchronized to and changes here.
1076 *
1077 * 1. The calculations of MAX_INGQ in cxgb4.h.
1078 *
1079 * 2. Update enable_msix/name_msix_vecs/request_msix_queue_irqs
1080 * to accommodate any new/deleted Ingress Queues
1081 * which need MSI-X Vectors.
1082 *
1083 * 3. Update sge_qinfo_show() to include information on the
1084 * new/deleted queues.
1085 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001086 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Varun Prakash2337ba42016-02-14 23:02:41 +05301087 msi_idx, NULL, fwevtq_handler, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001088 if (err) {
1089freeout: t4_free_sge_resources(adap);
1090 return err;
1091 }
1092
1093 for_each_port(adap, i) {
1094 struct net_device *dev = adap->port[i];
1095 struct port_info *pi = netdev_priv(dev);
1096 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
1097 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
1098
1099 for (j = 0; j < pi->nqsets; j++, q++) {
1100 if (msi_idx > 0)
1101 msi_idx++;
1102 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
1103 msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05301104 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +05301105 NULL,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +05301106 t4_get_mps_bg_map(adap,
1107 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001108 if (err)
1109 goto freeout;
1110 q->rspq.idx = j;
1111 memset(&q->stats, 0, sizeof(q->stats));
1112 }
1113 for (j = 0; j < pi->nqsets; j++, t++) {
1114 err = t4_sge_alloc_eth_txq(adap, t, dev,
1115 netdev_get_tx_queue(dev, j),
1116 s->fw_evtq.cntxt_id);
1117 if (err)
1118 goto freeout;
1119 }
1120 }
1121
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05301122 j = s->iscsiqsets / adap->params.nports; /* iscsi queues per channel */
1123 for_each_iscsirxq(s, i) {
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301124 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
1125 adap->port[i / j],
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001126 s->fw_evtq.cntxt_id);
1127 if (err)
1128 goto freeout;
1129 }
1130
Varun Prakash2337ba42016-02-14 23:02:41 +05301131#define ALLOC_OFLD_RXQS(firstq, nq, per_chan, ids, lro) do { \
1132 err = alloc_ofld_rxqs(adap, firstq, nq, per_chan, msi_idx, ids, lro); \
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301133 if (err) \
1134 goto freeout; \
1135 if (msi_idx > 0) \
1136 msi_idx += nq; \
1137} while (0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001138
Varun Prakash2337ba42016-02-14 23:02:41 +05301139 ALLOC_OFLD_RXQS(s->iscsirxq, s->iscsiqsets, j, s->iscsi_rxq, false);
1140 ALLOC_OFLD_RXQS(s->iscsitrxq, s->niscsitq, j, s->iscsit_rxq, true);
1141 ALLOC_OFLD_RXQS(s->rdmarxq, s->rdmaqs, 1, s->rdma_rxq, false);
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05301142 j = s->rdmaciqs / adap->params.nports; /* rdmaq queues per channel */
Varun Prakash2337ba42016-02-14 23:02:41 +05301143 ALLOC_OFLD_RXQS(s->rdmaciq, s->rdmaciqs, j, s->rdma_ciq, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001144
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +05301145#undef ALLOC_OFLD_RXQS
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05301146
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001147 for_each_port(adap, i) {
1148 /*
1149 * Note that ->rdmarxq[i].rspq.cntxt_id below is 0 if we don't
1150 * have RDMA queues, and that's the right value.
1151 */
1152 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
1153 s->fw_evtq.cntxt_id,
1154 s->rdmarxq[i].rspq.cntxt_id);
1155 if (err)
1156 goto freeout;
1157 }
1158
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +05301159 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05301160 MPS_TRC_RSS_CONTROL_A :
1161 MPS_T5_TRC_RSS_CONTROL_A,
1162 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
1163 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001164 return 0;
1165}
1166
1167/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001168 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1169 * The allocated memory is cleared.
1170 */
1171void *t4_alloc_mem(size_t size)
1172{
Joe Perches8be04b92013-06-19 12:15:53 -07001173 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001174
1175 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001176 p = vzalloc(size);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001177 return p;
1178}
1179
1180/*
1181 * Free memory allocated through alloc_mem().
1182 */
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301183void t4_free_mem(void *addr)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001184{
Pekka Enbergd2fcb542015-06-30 14:59:12 -07001185 kvfree(addr);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001186}
1187
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001188/* Send a Work Request to write the filter at a specified index. We construct
1189 * a Firmware Filter Work Request to have the work done and put the indicated
1190 * filter into "pending" mode which will prevent any further actions against
1191 * it till we get a reply from the firmware on the completion status of the
1192 * request.
1193 */
1194static int set_filter_wr(struct adapter *adapter, int fidx)
1195{
1196 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1197 struct sk_buff *skb;
1198 struct fw_filter_wr *fwr;
1199 unsigned int ftid;
1200
Michal Hockof72f1162015-04-14 13:24:33 -07001201 skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
1202 if (!skb)
1203 return -ENOMEM;
1204
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001205 /* If the new filter requires loopback Destination MAC and/or VLAN
1206 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1207 * the filter.
1208 */
1209 if (f->fs.newdmac || f->fs.newvlan) {
1210 /* allocate L2T entry for new filter */
Hariprasad Shenaif7502652015-12-17 13:45:08 +05301211 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
1212 f->fs.eport, f->fs.dmac);
Michal Hockof72f1162015-04-14 13:24:33 -07001213 if (f->l2t == NULL) {
1214 kfree_skb(skb);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001215 return -ENOMEM;
1216 }
1217 }
1218
1219 ftid = adapter->tids.ftid_base + fidx;
1220
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001221 fwr = (struct fw_filter_wr *)__skb_put(skb, sizeof(*fwr));
1222 memset(fwr, 0, sizeof(*fwr));
1223
1224 /* It would be nice to put most of the following in t4_hw.c but most
1225 * of the work is translating the cxgbtool ch_filter_specification
1226 * into the Work Request and the definition of that structure is
1227 * currently in cxgbtool.h which isn't appropriate to pull into the
1228 * common code. We may eventually try to come up with a more neutral
1229 * filter specification structure but for now it's easiest to simply
1230 * put this fairly direct code in line ...
1231 */
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301232 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
1233 fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr)/16));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001234 fwr->tid_to_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301235 htonl(FW_FILTER_WR_TID_V(ftid) |
1236 FW_FILTER_WR_RQTYPE_V(f->fs.type) |
1237 FW_FILTER_WR_NOREPLY_V(0) |
1238 FW_FILTER_WR_IQ_V(f->fs.iq));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001239 fwr->del_filter_to_l2tix =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301240 htonl(FW_FILTER_WR_RPTTID_V(f->fs.rpttid) |
1241 FW_FILTER_WR_DROP_V(f->fs.action == FILTER_DROP) |
1242 FW_FILTER_WR_DIRSTEER_V(f->fs.dirsteer) |
1243 FW_FILTER_WR_MASKHASH_V(f->fs.maskhash) |
1244 FW_FILTER_WR_DIRSTEERHASH_V(f->fs.dirsteerhash) |
1245 FW_FILTER_WR_LPBK_V(f->fs.action == FILTER_SWITCH) |
1246 FW_FILTER_WR_DMAC_V(f->fs.newdmac) |
1247 FW_FILTER_WR_SMAC_V(f->fs.newsmac) |
1248 FW_FILTER_WR_INSVLAN_V(f->fs.newvlan == VLAN_INSERT ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001249 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301250 FW_FILTER_WR_RMVLAN_V(f->fs.newvlan == VLAN_REMOVE ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001251 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301252 FW_FILTER_WR_HITCNTS_V(f->fs.hitcnts) |
1253 FW_FILTER_WR_TXCHAN_V(f->fs.eport) |
1254 FW_FILTER_WR_PRIO_V(f->fs.prio) |
1255 FW_FILTER_WR_L2TIX_V(f->l2t ? f->l2t->idx : 0));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001256 fwr->ethtype = htons(f->fs.val.ethtype);
1257 fwr->ethtypem = htons(f->fs.mask.ethtype);
1258 fwr->frag_to_ovlan_vldm =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301259 (FW_FILTER_WR_FRAG_V(f->fs.val.frag) |
1260 FW_FILTER_WR_FRAGM_V(f->fs.mask.frag) |
1261 FW_FILTER_WR_IVLAN_VLD_V(f->fs.val.ivlan_vld) |
1262 FW_FILTER_WR_OVLAN_VLD_V(f->fs.val.ovlan_vld) |
1263 FW_FILTER_WR_IVLAN_VLDM_V(f->fs.mask.ivlan_vld) |
1264 FW_FILTER_WR_OVLAN_VLDM_V(f->fs.mask.ovlan_vld));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001265 fwr->smac_sel = 0;
1266 fwr->rx_chan_rx_rpl_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301267 htons(FW_FILTER_WR_RX_CHAN_V(0) |
1268 FW_FILTER_WR_RX_RPL_IQ_V(adapter->sge.fw_evtq.abs_id));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001269 fwr->maci_to_matchtypem =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301270 htonl(FW_FILTER_WR_MACI_V(f->fs.val.macidx) |
1271 FW_FILTER_WR_MACIM_V(f->fs.mask.macidx) |
1272 FW_FILTER_WR_FCOE_V(f->fs.val.fcoe) |
1273 FW_FILTER_WR_FCOEM_V(f->fs.mask.fcoe) |
1274 FW_FILTER_WR_PORT_V(f->fs.val.iport) |
1275 FW_FILTER_WR_PORTM_V(f->fs.mask.iport) |
1276 FW_FILTER_WR_MATCHTYPE_V(f->fs.val.matchtype) |
1277 FW_FILTER_WR_MATCHTYPEM_V(f->fs.mask.matchtype));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001278 fwr->ptcl = f->fs.val.proto;
1279 fwr->ptclm = f->fs.mask.proto;
1280 fwr->ttyp = f->fs.val.tos;
1281 fwr->ttypm = f->fs.mask.tos;
1282 fwr->ivlan = htons(f->fs.val.ivlan);
1283 fwr->ivlanm = htons(f->fs.mask.ivlan);
1284 fwr->ovlan = htons(f->fs.val.ovlan);
1285 fwr->ovlanm = htons(f->fs.mask.ovlan);
1286 memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
1287 memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
1288 memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
1289 memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
1290 fwr->lp = htons(f->fs.val.lport);
1291 fwr->lpm = htons(f->fs.mask.lport);
1292 fwr->fp = htons(f->fs.val.fport);
1293 fwr->fpm = htons(f->fs.mask.fport);
1294 if (f->fs.newsmac)
1295 memcpy(fwr->sma, f->fs.smac, sizeof(fwr->sma));
1296
1297 /* Mark the filter as "pending" and ship off the Filter Work Request.
1298 * When we get the Work Request Reply we'll clear the pending status.
1299 */
1300 f->pending = 1;
1301 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
1302 t4_ofld_send(adapter, skb);
1303 return 0;
1304}
1305
1306/* Delete the filter at a specified index.
1307 */
1308static int del_filter_wr(struct adapter *adapter, int fidx)
1309{
1310 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1311 struct sk_buff *skb;
1312 struct fw_filter_wr *fwr;
1313 unsigned int len, ftid;
1314
1315 len = sizeof(*fwr);
1316 ftid = adapter->tids.ftid_base + fidx;
1317
Michal Hockof72f1162015-04-14 13:24:33 -07001318 skb = alloc_skb(len, GFP_KERNEL);
1319 if (!skb)
1320 return -ENOMEM;
1321
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001322 fwr = (struct fw_filter_wr *)__skb_put(skb, len);
1323 t4_mk_filtdelwr(ftid, fwr, adapter->sge.fw_evtq.abs_id);
1324
1325 /* Mark the filter as "pending" and ship off the Filter Work Request.
1326 * When we get the Work Request Reply we'll clear the pending status.
1327 */
1328 f->pending = 1;
1329 t4_mgmt_tx(adapter, skb);
1330 return 0;
1331}
1332
Anish Bhatt688848b2014-06-19 21:37:13 -07001333static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
1334 void *accel_priv, select_queue_fallback_t fallback)
1335{
1336 int txq;
1337
1338#ifdef CONFIG_CHELSIO_T4_DCB
1339 /* If a Data Center Bridging has been successfully negotiated on this
1340 * link then we'll use the skb's priority to map it to a TX Queue.
1341 * The skb's priority is determined via the VLAN Tag Priority Code
1342 * Point field.
1343 */
1344 if (cxgb4_dcb_enabled(dev)) {
1345 u16 vlan_tci;
1346 int err;
1347
1348 err = vlan_get_tag(skb, &vlan_tci);
1349 if (unlikely(err)) {
1350 if (net_ratelimit())
1351 netdev_warn(dev,
1352 "TX Packet without VLAN Tag on DCB Link\n");
1353 txq = 0;
1354 } else {
1355 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +05301356#ifdef CONFIG_CHELSIO_T4_FCOE
1357 if (skb->protocol == htons(ETH_P_FCOE))
1358 txq = skb->priority & 0x7;
1359#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -07001360 }
1361 return txq;
1362 }
1363#endif /* CONFIG_CHELSIO_T4_DCB */
1364
1365 if (select_queue) {
1366 txq = (skb_rx_queue_recorded(skb)
1367 ? skb_get_rx_queue(skb)
1368 : smp_processor_id());
1369
1370 while (unlikely(txq >= dev->real_num_tx_queues))
1371 txq -= dev->real_num_tx_queues;
1372
1373 return txq;
1374 }
1375
1376 return fallback(dev, skb) % dev->real_num_tx_queues;
1377}
1378
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001379static int closest_timer(const struct sge *s, int time)
1380{
1381 int i, delta, match = 0, min_delta = INT_MAX;
1382
1383 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
1384 delta = time - s->timer_val[i];
1385 if (delta < 0)
1386 delta = -delta;
1387 if (delta < min_delta) {
1388 min_delta = delta;
1389 match = i;
1390 }
1391 }
1392 return match;
1393}
1394
1395static int closest_thres(const struct sge *s, int thres)
1396{
1397 int i, delta, match = 0, min_delta = INT_MAX;
1398
1399 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1400 delta = thres - s->counter_val[i];
1401 if (delta < 0)
1402 delta = -delta;
1403 if (delta < min_delta) {
1404 min_delta = delta;
1405 match = i;
1406 }
1407 }
1408 return match;
1409}
1410
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001411/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301412 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001413 * @q: the Rx queue
1414 * @us: the hold-off time in us, or 0 to disable timer
1415 * @cnt: the hold-off packet count, or 0 to disable counter
1416 *
1417 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1418 * one of the two needs to be enabled for the queue to generate interrupts.
1419 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301420int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1421 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001422{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301423 struct adapter *adap = q->adap;
1424
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001425 if ((us | cnt) == 0)
1426 cnt = 1;
1427
1428 if (cnt) {
1429 int err;
1430 u32 v, new_idx;
1431
1432 new_idx = closest_thres(&adap->sge, cnt);
1433 if (q->desc && q->pktcnt_idx != new_idx) {
1434 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301435 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1436 FW_PARAMS_PARAM_X_V(
1437 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1438 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301439 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1440 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001441 if (err)
1442 return err;
1443 }
1444 q->pktcnt_idx = new_idx;
1445 }
1446
1447 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301448 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001449 return 0;
1450}
1451
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001452static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001453{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001454 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001455 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001456 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001457
Patrick McHardyf6469682013-04-19 02:04:27 +00001458 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001459 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001460
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301461 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001462 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001463 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001464 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001465 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001466 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001467}
1468
Bill Pemberton91744942012-12-03 09:23:02 -05001469static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001470{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001471 if (IS_ERR_OR_NULL(adap->debugfs_root))
1472 return -1;
1473
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301474#ifdef CONFIG_DEBUG_FS
1475 t4_setup_debugfs(adap);
1476#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001477 return 0;
1478}
1479
1480/*
1481 * upper-layer driver support
1482 */
1483
1484/*
1485 * Allocate an active-open TID and set it to the supplied value.
1486 */
1487int cxgb4_alloc_atid(struct tid_info *t, void *data)
1488{
1489 int atid = -1;
1490
1491 spin_lock_bh(&t->atid_lock);
1492 if (t->afree) {
1493 union aopen_entry *p = t->afree;
1494
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001495 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001496 t->afree = p->next;
1497 p->data = data;
1498 t->atids_in_use++;
1499 }
1500 spin_unlock_bh(&t->atid_lock);
1501 return atid;
1502}
1503EXPORT_SYMBOL(cxgb4_alloc_atid);
1504
1505/*
1506 * Release an active-open TID.
1507 */
1508void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1509{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001510 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001511
1512 spin_lock_bh(&t->atid_lock);
1513 p->next = t->afree;
1514 t->afree = p;
1515 t->atids_in_use--;
1516 spin_unlock_bh(&t->atid_lock);
1517}
1518EXPORT_SYMBOL(cxgb4_free_atid);
1519
1520/*
1521 * Allocate a server TID and set it to the supplied value.
1522 */
1523int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1524{
1525 int stid;
1526
1527 spin_lock_bh(&t->stid_lock);
1528 if (family == PF_INET) {
1529 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1530 if (stid < t->nstids)
1531 __set_bit(stid, t->stid_bmap);
1532 else
1533 stid = -1;
1534 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301535 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001536 if (stid < 0)
1537 stid = -1;
1538 }
1539 if (stid >= 0) {
1540 t->stid_tab[stid].data = data;
1541 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301542 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1543 * This is equivalent to 4 TIDs. With CLIP enabled it
1544 * needs 2 TIDs.
1545 */
1546 if (family == PF_INET)
1547 t->stids_in_use++;
1548 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301549 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001550 }
1551 spin_unlock_bh(&t->stid_lock);
1552 return stid;
1553}
1554EXPORT_SYMBOL(cxgb4_alloc_stid);
1555
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001556/* Allocate a server filter TID and set it to the supplied value.
1557 */
1558int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1559{
1560 int stid;
1561
1562 spin_lock_bh(&t->stid_lock);
1563 if (family == PF_INET) {
1564 stid = find_next_zero_bit(t->stid_bmap,
1565 t->nstids + t->nsftids, t->nstids);
1566 if (stid < (t->nstids + t->nsftids))
1567 __set_bit(stid, t->stid_bmap);
1568 else
1569 stid = -1;
1570 } else {
1571 stid = -1;
1572 }
1573 if (stid >= 0) {
1574 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301575 stid -= t->nstids;
1576 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301577 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001578 }
1579 spin_unlock_bh(&t->stid_lock);
1580 return stid;
1581}
1582EXPORT_SYMBOL(cxgb4_alloc_sftid);
1583
1584/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001585 */
1586void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1587{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301588 /* Is it a server filter TID? */
1589 if (t->nsftids && (stid >= t->sftid_base)) {
1590 stid -= t->sftid_base;
1591 stid += t->nstids;
1592 } else {
1593 stid -= t->stid_base;
1594 }
1595
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001596 spin_lock_bh(&t->stid_lock);
1597 if (family == PF_INET)
1598 __clear_bit(stid, t->stid_bmap);
1599 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301600 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001601 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301602 if (stid < t->nstids) {
1603 if (family == PF_INET)
1604 t->stids_in_use--;
1605 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301606 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301607 } else {
1608 t->sftids_in_use--;
1609 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001610 spin_unlock_bh(&t->stid_lock);
1611}
1612EXPORT_SYMBOL(cxgb4_free_stid);
1613
1614/*
1615 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1616 */
1617static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1618 unsigned int tid)
1619{
1620 struct cpl_tid_release *req;
1621
1622 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1623 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1624 INIT_TP_WR(req, tid);
1625 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1626}
1627
1628/*
1629 * Queue a TID release request and if necessary schedule a work queue to
1630 * process it.
1631 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001632static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1633 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001634{
1635 void **p = &t->tid_tab[tid];
1636 struct adapter *adap = container_of(t, struct adapter, tids);
1637
1638 spin_lock_bh(&adap->tid_release_lock);
1639 *p = adap->tid_release_head;
1640 /* Low 2 bits encode the Tx channel number */
1641 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1642 if (!adap->tid_release_task_busy) {
1643 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001644 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001645 }
1646 spin_unlock_bh(&adap->tid_release_lock);
1647}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001648
1649/*
1650 * Process the list of pending TID release requests.
1651 */
1652static void process_tid_release_list(struct work_struct *work)
1653{
1654 struct sk_buff *skb;
1655 struct adapter *adap;
1656
1657 adap = container_of(work, struct adapter, tid_release_task);
1658
1659 spin_lock_bh(&adap->tid_release_lock);
1660 while (adap->tid_release_head) {
1661 void **p = adap->tid_release_head;
1662 unsigned int chan = (uintptr_t)p & 3;
1663 p = (void *)p - chan;
1664
1665 adap->tid_release_head = *p;
1666 *p = NULL;
1667 spin_unlock_bh(&adap->tid_release_lock);
1668
1669 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1670 GFP_KERNEL)))
1671 schedule_timeout_uninterruptible(1);
1672
1673 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1674 t4_ofld_send(adap, skb);
1675 spin_lock_bh(&adap->tid_release_lock);
1676 }
1677 adap->tid_release_task_busy = false;
1678 spin_unlock_bh(&adap->tid_release_lock);
1679}
1680
1681/*
1682 * Release a TID and inform HW. If we are unable to allocate the release
1683 * message we defer to a work queue.
1684 */
1685void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1686{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001687 struct sk_buff *skb;
1688 struct adapter *adap = container_of(t, struct adapter, tids);
1689
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301690 WARN_ON(tid >= t->ntids);
1691
1692 if (t->tid_tab[tid]) {
1693 t->tid_tab[tid] = NULL;
1694 if (t->hash_base && (tid >= t->hash_base))
1695 atomic_dec(&t->hash_tids_in_use);
1696 else
1697 atomic_dec(&t->tids_in_use);
1698 }
1699
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001700 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1701 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001702 mk_tid_release(skb, chan, tid);
1703 t4_ofld_send(adap, skb);
1704 } else
1705 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001706}
1707EXPORT_SYMBOL(cxgb4_remove_tid);
1708
1709/*
1710 * Allocate and initialize the TID tables. Returns 0 on success.
1711 */
1712static int tid_init(struct tid_info *t)
1713{
1714 size_t size;
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001715 unsigned int stid_bmap_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001716 unsigned int natids = t->natids;
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301717 struct adapter *adap = container_of(t, struct adapter, tids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001718
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001719 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001720 size = t->ntids * sizeof(*t->tid_tab) +
1721 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001722 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001723 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001724 stid_bmap_size * sizeof(long) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001725 t->nftids * sizeof(*t->ftid_tab) +
1726 t->nsftids * sizeof(*t->ftid_tab);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001727
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001728 t->tid_tab = t4_alloc_mem(size);
1729 if (!t->tid_tab)
1730 return -ENOMEM;
1731
1732 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1733 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001734 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001735 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001736 spin_lock_init(&t->stid_lock);
1737 spin_lock_init(&t->atid_lock);
1738
1739 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301740 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001741 t->afree = NULL;
1742 t->atids_in_use = 0;
1743 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301744 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001745
1746 /* Setup the free list for atid_tab and clear the stid bitmap. */
1747 if (natids) {
1748 while (--natids)
1749 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1750 t->afree = t->atid_tab;
1751 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001752 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301753 /* Reserve stid 0 for T4/T5 adapters */
1754 if (!t->stid_base &&
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301755 (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5))
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301756 __set_bit(0, t->stid_bmap);
1757
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001758 return 0;
1759}
1760
1761/**
1762 * cxgb4_create_server - create an IP server
1763 * @dev: the device
1764 * @stid: the server TID
1765 * @sip: local IP address to bind server to
1766 * @sport: the server's TCP port
1767 * @queue: queue to direct messages from this server to
1768 *
1769 * Create an IP server for the given port and address.
1770 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1771 */
1772int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001773 __be32 sip, __be16 sport, __be16 vlan,
1774 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001775{
1776 unsigned int chan;
1777 struct sk_buff *skb;
1778 struct adapter *adap;
1779 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301780 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001781
1782 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1783 if (!skb)
1784 return -ENOMEM;
1785
1786 adap = netdev2adap(dev);
1787 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1788 INIT_TP_WR(req, 0);
1789 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1790 req->local_port = sport;
1791 req->peer_port = htons(0);
1792 req->local_ip = sip;
1793 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001794 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001795 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001796 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1797 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301798 ret = t4_mgmt_tx(adap, skb);
1799 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001800}
1801EXPORT_SYMBOL(cxgb4_create_server);
1802
Vipul Pandya80f40c12013-07-04 16:10:45 +05301803/* cxgb4_create_server6 - create an IPv6 server
1804 * @dev: the device
1805 * @stid: the server TID
1806 * @sip: local IPv6 address to bind server to
1807 * @sport: the server's TCP port
1808 * @queue: queue to direct messages from this server to
1809 *
1810 * Create an IPv6 server for the given port and address.
1811 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1812 */
1813int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1814 const struct in6_addr *sip, __be16 sport,
1815 unsigned int queue)
1816{
1817 unsigned int chan;
1818 struct sk_buff *skb;
1819 struct adapter *adap;
1820 struct cpl_pass_open_req6 *req;
1821 int ret;
1822
1823 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1824 if (!skb)
1825 return -ENOMEM;
1826
1827 adap = netdev2adap(dev);
1828 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1829 INIT_TP_WR(req, 0);
1830 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1831 req->local_port = sport;
1832 req->peer_port = htons(0);
1833 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1834 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1835 req->peer_ip_hi = cpu_to_be64(0);
1836 req->peer_ip_lo = cpu_to_be64(0);
1837 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001838 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001839 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1840 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301841 ret = t4_mgmt_tx(adap, skb);
1842 return net_xmit_eval(ret);
1843}
1844EXPORT_SYMBOL(cxgb4_create_server6);
1845
1846int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1847 unsigned int queue, bool ipv6)
1848{
1849 struct sk_buff *skb;
1850 struct adapter *adap;
1851 struct cpl_close_listsvr_req *req;
1852 int ret;
1853
1854 adap = netdev2adap(dev);
1855
1856 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1857 if (!skb)
1858 return -ENOMEM;
1859
1860 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1861 INIT_TP_WR(req, 0);
1862 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001863 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1864 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301865 ret = t4_mgmt_tx(adap, skb);
1866 return net_xmit_eval(ret);
1867}
1868EXPORT_SYMBOL(cxgb4_remove_server);
1869
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001870/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001871 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1872 * @mtus: the HW MTU table
1873 * @mtu: the target MTU
1874 * @idx: index of selected entry in the MTU table
1875 *
1876 * Returns the index and the value in the HW MTU table that is closest to
1877 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1878 * table, in which case that smallest available value is selected.
1879 */
1880unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1881 unsigned int *idx)
1882{
1883 unsigned int i = 0;
1884
1885 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1886 ++i;
1887 if (idx)
1888 *idx = i;
1889 return mtus[i];
1890}
1891EXPORT_SYMBOL(cxgb4_best_mtu);
1892
1893/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301894 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1895 * @mtus: the HW MTU table
1896 * @header_size: Header Size
1897 * @data_size_max: maximum Data Segment Size
1898 * @data_size_align: desired Data Segment Size Alignment (2^N)
1899 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1900 *
1901 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1902 * MTU Table based solely on a Maximum MTU parameter, we break that
1903 * parameter up into a Header Size and Maximum Data Segment Size, and
1904 * provide a desired Data Segment Size Alignment. If we find an MTU in
1905 * the Hardware MTU Table which will result in a Data Segment Size with
1906 * the requested alignment _and_ that MTU isn't "too far" from the
1907 * closest MTU, then we'll return that rather than the closest MTU.
1908 */
1909unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1910 unsigned short header_size,
1911 unsigned short data_size_max,
1912 unsigned short data_size_align,
1913 unsigned int *mtu_idxp)
1914{
1915 unsigned short max_mtu = header_size + data_size_max;
1916 unsigned short data_size_align_mask = data_size_align - 1;
1917 int mtu_idx, aligned_mtu_idx;
1918
1919 /* Scan the MTU Table till we find an MTU which is larger than our
1920 * Maximum MTU or we reach the end of the table. Along the way,
1921 * record the last MTU found, if any, which will result in a Data
1922 * Segment Length matching the requested alignment.
1923 */
1924 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1925 unsigned short data_size = mtus[mtu_idx] - header_size;
1926
1927 /* If this MTU minus the Header Size would result in a
1928 * Data Segment Size of the desired alignment, remember it.
1929 */
1930 if ((data_size & data_size_align_mask) == 0)
1931 aligned_mtu_idx = mtu_idx;
1932
1933 /* If we're not at the end of the Hardware MTU Table and the
1934 * next element is larger than our Maximum MTU, drop out of
1935 * the loop.
1936 */
1937 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1938 break;
1939 }
1940
1941 /* If we fell out of the loop because we ran to the end of the table,
1942 * then we just have to use the last [largest] entry.
1943 */
1944 if (mtu_idx == NMTUS)
1945 mtu_idx--;
1946
1947 /* If we found an MTU which resulted in the requested Data Segment
1948 * Length alignment and that's "not far" from the largest MTU which is
1949 * less than or equal to the maximum MTU, then use that.
1950 */
1951 if (aligned_mtu_idx >= 0 &&
1952 mtu_idx - aligned_mtu_idx <= 1)
1953 mtu_idx = aligned_mtu_idx;
1954
1955 /* If the caller has passed in an MTU Index pointer, pass the
1956 * MTU Index back. Return the MTU value.
1957 */
1958 if (mtu_idxp)
1959 *mtu_idxp = mtu_idx;
1960 return mtus[mtu_idx];
1961}
1962EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1963
1964/**
Hariprasad S27999802015-09-23 17:19:26 +05301965 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1966 * @chip: chip type
1967 * @viid: VI id of the given port
1968 *
1969 * Return the SMT index for this VI.
1970 */
1971unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1972{
1973 /* In T4/T5, SMT contains 256 SMAC entries organized in
1974 * 128 rows of 2 entries each.
1975 * In T6, SMT contains 256 SMAC entries in 256 rows.
1976 * TODO: The below code needs to be updated when we add support
1977 * for 256 VFs.
1978 */
1979 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1980 return ((viid & 0x7f) << 1);
1981 else
1982 return (viid & 0x7f);
1983}
1984EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1985
1986/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001987 * cxgb4_port_chan - get the HW channel of a port
1988 * @dev: the net device for the port
1989 *
1990 * Return the HW Tx channel of the given port.
1991 */
1992unsigned int cxgb4_port_chan(const struct net_device *dev)
1993{
1994 return netdev2pinfo(dev)->tx_chan;
1995}
1996EXPORT_SYMBOL(cxgb4_port_chan);
1997
Vipul Pandya881806b2012-05-18 15:29:24 +05301998unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1999{
2000 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002001 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05302002
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302003 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2004 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302005 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302006 lp_count = LP_COUNT_G(v1);
2007 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002008 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302009 lp_count = LP_COUNT_T5_G(v1);
2010 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002011 }
2012 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05302013}
2014EXPORT_SYMBOL(cxgb4_dbfifo_count);
2015
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002016/**
2017 * cxgb4_port_viid - get the VI id of a port
2018 * @dev: the net device for the port
2019 *
2020 * Return the VI id of the given port.
2021 */
2022unsigned int cxgb4_port_viid(const struct net_device *dev)
2023{
2024 return netdev2pinfo(dev)->viid;
2025}
2026EXPORT_SYMBOL(cxgb4_port_viid);
2027
2028/**
2029 * cxgb4_port_idx - get the index of a port
2030 * @dev: the net device for the port
2031 *
2032 * Return the index of the given port.
2033 */
2034unsigned int cxgb4_port_idx(const struct net_device *dev)
2035{
2036 return netdev2pinfo(dev)->port_id;
2037}
2038EXPORT_SYMBOL(cxgb4_port_idx);
2039
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002040void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
2041 struct tp_tcp_stats *v6)
2042{
2043 struct adapter *adap = pci_get_drvdata(pdev);
2044
2045 spin_lock(&adap->stats_lock);
2046 t4_tp_get_tcp_stats(adap, v4, v6);
2047 spin_unlock(&adap->stats_lock);
2048}
2049EXPORT_SYMBOL(cxgb4_get_tcp_stats);
2050
2051void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
2052 const unsigned int *pgsz_order)
2053{
2054 struct adapter *adap = netdev2adap(dev);
2055
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302056 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
2057 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
2058 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
2059 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002060}
2061EXPORT_SYMBOL(cxgb4_iscsi_init);
2062
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302063int cxgb4_flush_eq_cache(struct net_device *dev)
2064{
2065 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302066
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05302067 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302068}
2069EXPORT_SYMBOL(cxgb4_flush_eq_cache);
2070
2071static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
2072{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302073 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302074 __be64 indices;
2075 int ret;
2076
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05302077 spin_lock(&adap->win0_lock);
2078 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
2079 sizeof(indices), (__be32 *)&indices,
2080 T4_MEMORY_READ);
2081 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302082 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00002083 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
2084 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302085 }
2086 return ret;
2087}
2088
2089int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
2090 u16 size)
2091{
2092 struct adapter *adap = netdev2adap(dev);
2093 u16 hw_pidx, hw_cidx;
2094 int ret;
2095
2096 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
2097 if (ret)
2098 goto out;
2099
2100 if (pidx != hw_pidx) {
2101 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302102 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302103
2104 if (pidx >= hw_pidx)
2105 delta = pidx - hw_pidx;
2106 else
2107 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302108
2109 if (is_t4(adap->params.chip))
2110 val = PIDX_V(delta);
2111 else
2112 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302113 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302114 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2115 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302116 }
2117out:
2118 return ret;
2119}
2120EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
2121
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302122int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
2123{
2124 struct adapter *adap;
2125 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302126 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302127 u32 edc0_end, edc1_end, mc0_end, mc1_end;
2128 int ret;
2129
2130 adap = netdev2adap(dev);
2131
2132 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
2133
2134 /* Figure out where the offset lands in the Memory Type/Address scheme.
2135 * This code assumes that the memory is laid out starting at offset 0
2136 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
2137 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
2138 * MC0, and some have both MC0 and MC1.
2139 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302140 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
2141 edc0_size = EDRAM0_SIZE_G(size) << 20;
2142 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
2143 edc1_size = EDRAM1_SIZE_G(size) << 20;
2144 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
2145 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302146
2147 edc0_end = edc0_size;
2148 edc1_end = edc0_end + edc1_size;
2149 mc0_end = edc1_end + mc0_size;
2150
2151 if (offset < edc0_end) {
2152 memtype = MEM_EDC0;
2153 memaddr = offset;
2154 } else if (offset < edc1_end) {
2155 memtype = MEM_EDC1;
2156 memaddr = offset - edc0_end;
2157 } else {
2158 if (offset < mc0_end) {
2159 memtype = MEM_MC0;
2160 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302161 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302162 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2163 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302164 mc1_end = mc0_end + mc1_size;
2165 if (offset < mc1_end) {
2166 memtype = MEM_MC1;
2167 memaddr = offset - mc0_end;
2168 } else {
2169 /* offset beyond the end of any memory */
2170 goto err;
2171 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302172 } else {
2173 /* T4/T6 only has a single memory channel */
2174 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302175 }
2176 }
2177
2178 spin_lock(&adap->win0_lock);
2179 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
2180 spin_unlock(&adap->win0_lock);
2181 return ret;
2182
2183err:
2184 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
2185 stag, offset);
2186 return -EINVAL;
2187}
2188EXPORT_SYMBOL(cxgb4_read_tpte);
2189
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302190u64 cxgb4_read_sge_timestamp(struct net_device *dev)
2191{
2192 u32 hi, lo;
2193 struct adapter *adap;
2194
2195 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302196 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
2197 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302198
2199 return ((u64)hi << 32) | (u64)lo;
2200}
2201EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
2202
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302203int cxgb4_bar2_sge_qregs(struct net_device *dev,
2204 unsigned int qid,
2205 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05302206 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302207 u64 *pbar2_qoffset,
2208 unsigned int *pbar2_qid)
2209{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302210 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302211 qid,
2212 (qtype == CXGB4_BAR2_QTYPE_EGRESS
2213 ? T4_BAR2_QTYPE_EGRESS
2214 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05302215 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302216 pbar2_qoffset,
2217 pbar2_qid);
2218}
2219EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
2220
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002221static struct pci_driver cxgb4_driver;
2222
2223static void check_neigh_update(struct neighbour *neigh)
2224{
2225 const struct device *parent;
2226 const struct net_device *netdev = neigh->dev;
2227
2228 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2229 netdev = vlan_dev_real_dev(netdev);
2230 parent = netdev->dev.parent;
2231 if (parent && parent->driver == &cxgb4_driver.driver)
2232 t4_l2t_update(dev_get_drvdata(parent), neigh);
2233}
2234
2235static int netevent_cb(struct notifier_block *nb, unsigned long event,
2236 void *data)
2237{
2238 switch (event) {
2239 case NETEVENT_NEIGH_UPDATE:
2240 check_neigh_update(data);
2241 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002242 case NETEVENT_REDIRECT:
2243 default:
2244 break;
2245 }
2246 return 0;
2247}
2248
2249static bool netevent_registered;
2250static struct notifier_block cxgb4_netevent_nb = {
2251 .notifier_call = netevent_cb
2252};
2253
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302254static void drain_db_fifo(struct adapter *adap, int usecs)
2255{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002256 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302257
2258 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302259 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2260 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302261 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302262 lp_count = LP_COUNT_G(v1);
2263 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002264 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302265 lp_count = LP_COUNT_T5_G(v1);
2266 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002267 }
2268
2269 if (lp_count == 0 && hp_count == 0)
2270 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302271 set_current_state(TASK_UNINTERRUPTIBLE);
2272 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302273 } while (1);
2274}
2275
2276static void disable_txq_db(struct sge_txq *q)
2277{
Steve Wise05eb2382014-03-14 21:52:08 +05302278 unsigned long flags;
2279
2280 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302281 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05302282 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302283}
2284
Steve Wise05eb2382014-03-14 21:52:08 +05302285static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302286{
2287 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05302288 if (q->db_pidx_inc) {
2289 /* Make sure that all writes to the TX descriptors
2290 * are committed before we tell HW about them.
2291 */
2292 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302293 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2294 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05302295 q->db_pidx_inc = 0;
2296 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302297 q->db_disabled = 0;
2298 spin_unlock_irq(&q->db_lock);
2299}
2300
2301static void disable_dbs(struct adapter *adap)
2302{
2303 int i;
2304
2305 for_each_ethrxq(&adap->sge, i)
2306 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302307 for_each_iscsirxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302308 disable_txq_db(&adap->sge.ofldtxq[i].q);
2309 for_each_port(adap, i)
2310 disable_txq_db(&adap->sge.ctrlq[i].q);
2311}
2312
2313static void enable_dbs(struct adapter *adap)
2314{
2315 int i;
2316
2317 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302318 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302319 for_each_iscsirxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302320 enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302321 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302322 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
2323}
2324
2325static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
2326{
2327 if (adap->uld_handle[CXGB4_ULD_RDMA])
2328 ulds[CXGB4_ULD_RDMA].control(adap->uld_handle[CXGB4_ULD_RDMA],
2329 cmd);
2330}
2331
2332static void process_db_full(struct work_struct *work)
2333{
2334 struct adapter *adap;
2335
2336 adap = container_of(work, struct adapter, db_full_task);
2337
2338 drain_db_fifo(adap, dbfifo_drain_delay);
2339 enable_dbs(adap);
2340 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302341 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2342 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2343 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
2344 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
2345 else
2346 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2347 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302348}
2349
2350static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
2351{
2352 u16 hw_pidx, hw_cidx;
2353 int ret;
2354
Steve Wise05eb2382014-03-14 21:52:08 +05302355 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302356 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2357 if (ret)
2358 goto out;
2359 if (q->db_pidx != hw_pidx) {
2360 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302361 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302362
2363 if (q->db_pidx >= hw_pidx)
2364 delta = q->db_pidx - hw_pidx;
2365 else
2366 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302367
2368 if (is_t4(adap->params.chip))
2369 val = PIDX_V(delta);
2370 else
2371 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302372 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302373 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2374 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302375 }
2376out:
2377 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302378 q->db_pidx_inc = 0;
2379 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302380 if (ret)
2381 CH_WARN(adap, "DB drop recovery failed.\n");
2382}
2383static void recover_all_queues(struct adapter *adap)
2384{
2385 int i;
2386
2387 for_each_ethrxq(&adap->sge, i)
2388 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302389 for_each_iscsirxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302390 sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
2391 for_each_port(adap, i)
2392 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2393}
2394
Vipul Pandya881806b2012-05-18 15:29:24 +05302395static void process_db_drop(struct work_struct *work)
2396{
2397 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302398
Vipul Pandya881806b2012-05-18 15:29:24 +05302399 adap = container_of(work, struct adapter, db_drop_task);
2400
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302401 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302402 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002403 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302404 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002405 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302406 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002407 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302408 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302409 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002410 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2411 u16 qid = (dropped_db >> 15) & 0x1ffff;
2412 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302413 u64 bar2_qoffset;
2414 unsigned int bar2_qid;
2415 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002416
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302417 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002418 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302419 if (ret)
2420 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2421 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2422 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302423 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302424 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002425
2426 /* Re-enable BAR2 WC */
2427 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2428 }
2429
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302430 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2431 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302432}
2433
2434void t4_db_full(struct adapter *adap)
2435{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302436 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302437 disable_dbs(adap);
2438 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302439 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2440 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002441 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002442 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302443}
2444
2445void t4_db_dropped(struct adapter *adap)
2446{
Steve Wise05eb2382014-03-14 21:52:08 +05302447 if (is_t4(adap->params.chip)) {
2448 disable_dbs(adap);
2449 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2450 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002451 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302452}
2453
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002454static void uld_attach(struct adapter *adap, unsigned int uld)
2455{
2456 void *handle;
2457 struct cxgb4_lld_info lli;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002458 unsigned short i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002459
2460 lli.pdev = adap->pdev;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302461 lli.pf = adap->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002462 lli.l2t = adap->l2t;
2463 lli.tids = &adap->tids;
2464 lli.ports = adap->port;
2465 lli.vr = &adap->vres;
2466 lli.mtus = adap->params.mtus;
2467 if (uld == CXGB4_ULD_RDMA) {
2468 lli.rxq_ids = adap->sge.rdma_rxq;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05302469 lli.ciq_ids = adap->sge.rdma_ciq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002470 lli.nrxq = adap->sge.rdmaqs;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05302471 lli.nciq = adap->sge.rdmaciqs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002472 } else if (uld == CXGB4_ULD_ISCSI) {
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302473 lli.rxq_ids = adap->sge.iscsi_rxq;
2474 lli.nrxq = adap->sge.iscsiqsets;
Varun Prakashf2692d12016-02-14 23:02:40 +05302475 } else if (uld == CXGB4_ULD_ISCSIT) {
2476 lli.rxq_ids = adap->sge.iscsit_rxq;
2477 lli.nrxq = adap->sge.niscsitq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002478 }
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05302479 lli.ntxq = adap->sge.iscsiqsets;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002480 lli.nchan = adap->params.nports;
2481 lli.nports = adap->params.nports;
2482 lli.wr_cred = adap->params.ofldq_wr_cred;
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302483 lli.adapter_type = adap->params.chip;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302484 lli.iscsi_iolen = MAXRXDATA_G(t4_read_reg(adap, TP_PARA_REG2_A));
Varun Prakash7714cb9e2016-02-14 23:07:39 +05302485 lli.iscsi_tagmask = t4_read_reg(adap, ULP_RX_ISCSI_TAGMASK_A);
2486 lli.iscsi_pgsz_order = t4_read_reg(adap, ULP_RX_ISCSI_PSZ_A);
2487 lli.iscsi_llimit = t4_read_reg(adap, ULP_RX_ISCSI_LLIMIT_A);
2488 lli.iscsi_ppm = &adap->iscsi_ppm;
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302489 lli.cclk_ps = 1000000000 / adap->params.vpd.cclk;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302490 lli.udb_density = 1 << adap->params.sge.eq_qpp;
2491 lli.ucq_density = 1 << adap->params.sge.iq_qpp;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05302492 lli.filt_mode = adap->params.tp.vlan_pri_map;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002493 /* MODQ_REQ_MAP sets queues 0-3 to chan 0-3 */
2494 for (i = 0; i < NCHAN; i++)
2495 lli.tx_modq[i] = i;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302496 lli.gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS_A);
2497 lli.db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL_A);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002498 lli.fw_vers = adap->params.fw_vers;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302499 lli.dbfifo_int_thresh = dbfifo_int_thresh;
Hariprasad Shenai04e10e22014-07-14 21:34:51 +05302500 lli.sge_ingpadboundary = adap->sge.fl_align;
2501 lli.sge_egrstatuspagesize = adap->sge.stat_len;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002502 lli.sge_pktshift = adap->sge.pktshift;
2503 lli.enable_fw_ofld_conn = adap->flags & FW_OFLD_CONN;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05302504 lli.max_ordird_qp = adap->params.max_ordird_qp;
2505 lli.max_ird_adapter = adap->params.max_ird_adapter;
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05302506 lli.ulptx_memwrite_dsgl = adap->params.ulptx_memwrite_dsgl;
Hariprasad Shenai982b81e2015-05-05 14:59:54 +05302507 lli.nodeid = dev_to_node(adap->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002508
2509 handle = ulds[uld].add(&lli);
2510 if (IS_ERR(handle)) {
2511 dev_warn(adap->pdev_dev,
2512 "could not attach to the %s driver, error %ld\n",
2513 uld_str[uld], PTR_ERR(handle));
2514 return;
2515 }
2516
2517 adap->uld_handle[uld] = handle;
2518
2519 if (!netevent_registered) {
2520 register_netevent_notifier(&cxgb4_netevent_nb);
2521 netevent_registered = true;
2522 }
Dimitris Michailidise29f5db2010-05-18 10:07:13 +00002523
2524 if (adap->flags & FULL_INIT_DONE)
2525 ulds[uld].state_change(handle, CXGB4_STATE_UP);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002526}
2527
2528static void attach_ulds(struct adapter *adap)
2529{
2530 unsigned int i;
2531
Vipul Pandya01bcca62013-07-04 16:10:46 +05302532 spin_lock(&adap_rcu_lock);
2533 list_add_tail_rcu(&adap->rcu_node, &adap_rcu_list);
2534 spin_unlock(&adap_rcu_lock);
2535
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002536 mutex_lock(&uld_mutex);
2537 list_add_tail(&adap->list_node, &adapter_list);
2538 for (i = 0; i < CXGB4_ULD_MAX; i++)
2539 if (ulds[i].add)
2540 uld_attach(adap, i);
2541 mutex_unlock(&uld_mutex);
2542}
2543
2544static void detach_ulds(struct adapter *adap)
2545{
2546 unsigned int i;
2547
2548 mutex_lock(&uld_mutex);
2549 list_del(&adap->list_node);
2550 for (i = 0; i < CXGB4_ULD_MAX; i++)
2551 if (adap->uld_handle[i]) {
2552 ulds[i].state_change(adap->uld_handle[i],
2553 CXGB4_STATE_DETACH);
2554 adap->uld_handle[i] = NULL;
2555 }
2556 if (netevent_registered && list_empty(&adapter_list)) {
2557 unregister_netevent_notifier(&cxgb4_netevent_nb);
2558 netevent_registered = false;
2559 }
2560 mutex_unlock(&uld_mutex);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302561
2562 spin_lock(&adap_rcu_lock);
2563 list_del_rcu(&adap->rcu_node);
2564 spin_unlock(&adap_rcu_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002565}
2566
2567static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2568{
2569 unsigned int i;
2570
2571 mutex_lock(&uld_mutex);
2572 for (i = 0; i < CXGB4_ULD_MAX; i++)
2573 if (adap->uld_handle[i])
2574 ulds[i].state_change(adap->uld_handle[i], new_state);
2575 mutex_unlock(&uld_mutex);
2576}
2577
2578/**
2579 * cxgb4_register_uld - register an upper-layer driver
2580 * @type: the ULD type
2581 * @p: the ULD methods
2582 *
2583 * Registers an upper-layer driver with this driver and notifies the ULD
2584 * about any presently available devices that support its type. Returns
2585 * %-EBUSY if a ULD of the same type is already registered.
2586 */
2587int cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p)
2588{
2589 int ret = 0;
2590 struct adapter *adap;
2591
2592 if (type >= CXGB4_ULD_MAX)
2593 return -EINVAL;
2594 mutex_lock(&uld_mutex);
2595 if (ulds[type].add) {
2596 ret = -EBUSY;
2597 goto out;
2598 }
2599 ulds[type] = *p;
2600 list_for_each_entry(adap, &adapter_list, list_node)
2601 uld_attach(adap, type);
2602out: mutex_unlock(&uld_mutex);
2603 return ret;
2604}
2605EXPORT_SYMBOL(cxgb4_register_uld);
2606
2607/**
2608 * cxgb4_unregister_uld - unregister an upper-layer driver
2609 * @type: the ULD type
2610 *
2611 * Unregisters an existing upper-layer driver.
2612 */
2613int cxgb4_unregister_uld(enum cxgb4_uld type)
2614{
2615 struct adapter *adap;
2616
2617 if (type >= CXGB4_ULD_MAX)
2618 return -EINVAL;
2619 mutex_lock(&uld_mutex);
2620 list_for_each_entry(adap, &adapter_list, list_node)
2621 adap->uld_handle[type] = NULL;
2622 ulds[type].add = NULL;
2623 mutex_unlock(&uld_mutex);
2624 return 0;
2625}
2626EXPORT_SYMBOL(cxgb4_unregister_uld);
2627
Anish Bhatt1bb60372014-10-14 20:07:22 -07002628#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002629static int cxgb4_inet6addr_handler(struct notifier_block *this,
2630 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302631{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002632 struct inet6_ifaddr *ifa = data;
2633 struct net_device *event_dev = ifa->idev->dev;
2634 const struct device *parent = NULL;
2635#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302636 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002637#endif
2638 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2639 event_dev = vlan_dev_real_dev(event_dev);
2640#if IS_ENABLED(CONFIG_BONDING)
2641 if (event_dev->flags & IFF_MASTER) {
2642 list_for_each_entry(adap, &adapter_list, list_node) {
2643 switch (event) {
2644 case NETDEV_UP:
2645 cxgb4_clip_get(adap->port[0],
2646 (const u32 *)ifa, 1);
2647 break;
2648 case NETDEV_DOWN:
2649 cxgb4_clip_release(adap->port[0],
2650 (const u32 *)ifa, 1);
2651 break;
2652 default:
2653 break;
2654 }
2655 }
2656 return NOTIFY_OK;
2657 }
2658#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302659
Anish Bhattb5a02f52015-01-14 15:17:34 -08002660 if (event_dev)
2661 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302662
Anish Bhattb5a02f52015-01-14 15:17:34 -08002663 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302664 switch (event) {
2665 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002666 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302667 break;
2668 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002669 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302670 break;
2671 default:
2672 break;
2673 }
2674 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002675 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302676}
2677
Anish Bhattb5a02f52015-01-14 15:17:34 -08002678static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302679static struct notifier_block cxgb4_inet6addr_notifier = {
2680 .notifier_call = cxgb4_inet6addr_handler
2681};
2682
Vipul Pandya01bcca62013-07-04 16:10:46 +05302683static void update_clip(const struct adapter *adap)
2684{
2685 int i;
2686 struct net_device *dev;
2687 int ret;
2688
2689 rcu_read_lock();
2690
2691 for (i = 0; i < MAX_NPORTS; i++) {
2692 dev = adap->port[i];
2693 ret = 0;
2694
2695 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002696 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302697
2698 if (ret < 0)
2699 break;
2700 }
2701 rcu_read_unlock();
2702}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002703#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302704
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002705/**
2706 * cxgb_up - enable the adapter
2707 * @adap: adapter being enabled
2708 *
2709 * Called when the first port is enabled, this function performs the
2710 * actions necessary to make an adapter operational, such as completing
2711 * the initialization of HW modules, and enabling interrupts.
2712 *
2713 * Must be called with the rtnl lock held.
2714 */
2715static int cxgb_up(struct adapter *adap)
2716{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002717 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002718
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002719 err = setup_sge_queues(adap);
2720 if (err)
2721 goto out;
2722 err = setup_rss(adap);
2723 if (err)
2724 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002725
2726 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002727 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002728 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2729 adap->msix_info[0].desc, adap);
2730 if (err)
2731 goto irq_err;
2732
2733 err = request_msix_queue_irqs(adap);
2734 if (err) {
2735 free_irq(adap->msix_info[0].vec, adap);
2736 goto irq_err;
2737 }
2738 } else {
2739 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2740 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002741 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002742 if (err)
2743 goto irq_err;
2744 }
2745 enable_rx(adap);
2746 t4_sge_start(adap);
2747 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002748 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002749 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002750#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302751 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002752#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302753 /* Initialize hash mac addr list*/
2754 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002755 out:
2756 return err;
2757 irq_err:
2758 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002759 freeq:
2760 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002761 goto out;
2762}
2763
2764static void cxgb_down(struct adapter *adapter)
2765{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002766 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302767 cancel_work_sync(&adapter->db_full_task);
2768 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002769 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002770 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002771
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002772 t4_sge_stop(adapter);
2773 t4_free_sge_resources(adapter);
2774 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002775}
2776
2777/*
2778 * net_device operations
2779 */
2780static int cxgb_open(struct net_device *dev)
2781{
2782 int err;
2783 struct port_info *pi = netdev_priv(dev);
2784 struct adapter *adapter = pi->adapter;
2785
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002786 netif_carrier_off(dev);
2787
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002788 if (!(adapter->flags & FULL_INIT_DONE)) {
2789 err = cxgb_up(adapter);
2790 if (err < 0)
2791 return err;
2792 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002793
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002794 err = link_start(dev);
2795 if (!err)
2796 netif_tx_start_all_queues(dev);
2797 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002798}
2799
2800static int cxgb_close(struct net_device *dev)
2801{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002802 struct port_info *pi = netdev_priv(dev);
2803 struct adapter *adapter = pi->adapter;
2804
2805 netif_tx_stop_all_queues(dev);
2806 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302807 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002808}
2809
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002810/* Return an error number if the indicated filter isn't writable ...
2811 */
2812static int writable_filter(struct filter_entry *f)
2813{
2814 if (f->locked)
2815 return -EPERM;
2816 if (f->pending)
2817 return -EBUSY;
2818
2819 return 0;
2820}
2821
2822/* Delete the filter at the specified index (if valid). The checks for all
2823 * the common problems with doing this like the filter being locked, currently
2824 * pending in another operation, etc.
2825 */
2826static int delete_filter(struct adapter *adapter, unsigned int fidx)
2827{
2828 struct filter_entry *f;
2829 int ret;
2830
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002831 if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002832 return -EINVAL;
2833
2834 f = &adapter->tids.ftid_tab[fidx];
2835 ret = writable_filter(f);
2836 if (ret)
2837 return ret;
2838 if (f->valid)
2839 return del_filter_wr(adapter, fidx);
2840
2841 return 0;
2842}
2843
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002844int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002845 __be32 sip, __be16 sport, __be16 vlan,
2846 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002847{
2848 int ret;
2849 struct filter_entry *f;
2850 struct adapter *adap;
2851 int i;
2852 u8 *val;
2853
2854 adap = netdev2adap(dev);
2855
Vipul Pandya1cab7752012-12-10 09:30:55 +00002856 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302857 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002858 stid += adap->tids.nftids;
2859
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002860 /* Check to make sure the filter requested is writable ...
2861 */
2862 f = &adap->tids.ftid_tab[stid];
2863 ret = writable_filter(f);
2864 if (ret)
2865 return ret;
2866
2867 /* Clear out any old resources being used by the filter before
2868 * we start constructing the new filter.
2869 */
2870 if (f->valid)
2871 clear_filter(adap, f);
2872
2873 /* Clear out filter specifications */
2874 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2875 f->fs.val.lport = cpu_to_be16(sport);
2876 f->fs.mask.lport = ~0;
2877 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002878 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002879 for (i = 0; i < 4; i++) {
2880 f->fs.val.lip[i] = val[i];
2881 f->fs.mask.lip[i] = ~0;
2882 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302883 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002884 f->fs.val.iport = port;
2885 f->fs.mask.iport = mask;
2886 }
2887 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002888
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302889 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302890 f->fs.val.proto = IPPROTO_TCP;
2891 f->fs.mask.proto = ~0;
2892 }
2893
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002894 f->fs.dirsteer = 1;
2895 f->fs.iq = queue;
2896 /* Mark filter as locked */
2897 f->locked = 1;
2898 f->fs.rpttid = 1;
2899
2900 ret = set_filter_wr(adap, stid);
2901 if (ret) {
2902 clear_filter(adap, f);
2903 return ret;
2904 }
2905
2906 return 0;
2907}
2908EXPORT_SYMBOL(cxgb4_create_server_filter);
2909
2910int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2911 unsigned int queue, bool ipv6)
2912{
2913 int ret;
2914 struct filter_entry *f;
2915 struct adapter *adap;
2916
2917 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002918
2919 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302920 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002921 stid += adap->tids.nftids;
2922
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002923 f = &adap->tids.ftid_tab[stid];
2924 /* Unlock the filter */
2925 f->locked = 0;
2926
2927 ret = delete_filter(adap, stid);
2928 if (ret)
2929 return ret;
2930
2931 return 0;
2932}
2933EXPORT_SYMBOL(cxgb4_remove_server_filter);
2934
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002935static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2936 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002937{
2938 struct port_stats stats;
2939 struct port_info *p = netdev_priv(dev);
2940 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002941
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002942 /* Block retrieving statistics during EEH error
2943 * recovery. Otherwise, the recovery might fail
2944 * and the PCI device will be removed permanently
2945 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002946 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002947 if (!netif_device_present(dev)) {
2948 spin_unlock(&adapter->stats_lock);
2949 return ns;
2950 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302951 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2952 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002953 spin_unlock(&adapter->stats_lock);
2954
2955 ns->tx_bytes = stats.tx_octets;
2956 ns->tx_packets = stats.tx_frames;
2957 ns->rx_bytes = stats.rx_octets;
2958 ns->rx_packets = stats.rx_frames;
2959 ns->multicast = stats.rx_mcast_frames;
2960
2961 /* detailed rx_errors */
2962 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2963 stats.rx_runt;
2964 ns->rx_over_errors = 0;
2965 ns->rx_crc_errors = stats.rx_fcs_err;
2966 ns->rx_frame_errors = stats.rx_symbol_err;
2967 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2968 stats.rx_ovflow2 + stats.rx_ovflow3 +
2969 stats.rx_trunc0 + stats.rx_trunc1 +
2970 stats.rx_trunc2 + stats.rx_trunc3;
2971 ns->rx_missed_errors = 0;
2972
2973 /* detailed tx_errors */
2974 ns->tx_aborted_errors = 0;
2975 ns->tx_carrier_errors = 0;
2976 ns->tx_fifo_errors = 0;
2977 ns->tx_heartbeat_errors = 0;
2978 ns->tx_window_errors = 0;
2979
2980 ns->tx_errors = stats.tx_error_frames;
2981 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2982 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2983 return ns;
2984}
2985
2986static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2987{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002988 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002989 int ret = 0, prtad, devad;
2990 struct port_info *pi = netdev_priv(dev);
2991 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2992
2993 switch (cmd) {
2994 case SIOCGMIIPHY:
2995 if (pi->mdio_addr < 0)
2996 return -EOPNOTSUPP;
2997 data->phy_id = pi->mdio_addr;
2998 break;
2999 case SIOCGMIIREG:
3000 case SIOCSMIIREG:
3001 if (mdio_phy_id_is_c45(data->phy_id)) {
3002 prtad = mdio_phy_id_prtad(data->phy_id);
3003 devad = mdio_phy_id_devad(data->phy_id);
3004 } else if (data->phy_id < 32) {
3005 prtad = data->phy_id;
3006 devad = 0;
3007 data->reg_num &= 0x1f;
3008 } else
3009 return -EINVAL;
3010
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303011 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003012 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003013 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003014 data->reg_num, &data->val_out);
3015 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003016 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003017 data->reg_num, data->val_in);
3018 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05303019 case SIOCGHWTSTAMP:
3020 return copy_to_user(req->ifr_data, &pi->tstamp_config,
3021 sizeof(pi->tstamp_config)) ?
3022 -EFAULT : 0;
3023 case SIOCSHWTSTAMP:
3024 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
3025 sizeof(pi->tstamp_config)))
3026 return -EFAULT;
3027
3028 switch (pi->tstamp_config.rx_filter) {
3029 case HWTSTAMP_FILTER_NONE:
3030 pi->rxtstamp = false;
3031 break;
3032 case HWTSTAMP_FILTER_ALL:
3033 pi->rxtstamp = true;
3034 break;
3035 default:
3036 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
3037 return -ERANGE;
3038 }
3039
3040 return copy_to_user(req->ifr_data, &pi->tstamp_config,
3041 sizeof(pi->tstamp_config)) ?
3042 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003043 default:
3044 return -EOPNOTSUPP;
3045 }
3046 return ret;
3047}
3048
3049static void cxgb_set_rxmode(struct net_device *dev)
3050{
3051 /* unfortunately we can't return errors to the stack */
3052 set_rxmode(dev, -1, false);
3053}
3054
3055static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
3056{
3057 int ret;
3058 struct port_info *pi = netdev_priv(dev);
3059
3060 if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
3061 return -EINVAL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303062 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003063 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003064 if (!ret)
3065 dev->mtu = new_mtu;
3066 return ret;
3067}
3068
3069static int cxgb_set_mac_addr(struct net_device *dev, void *p)
3070{
3071 int ret;
3072 struct sockaddr *addr = p;
3073 struct port_info *pi = netdev_priv(dev);
3074
3075 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00003076 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003077
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303078 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003079 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003080 if (ret < 0)
3081 return ret;
3082
3083 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3084 pi->xact_addr_filt = ret;
3085 return 0;
3086}
3087
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003088#ifdef CONFIG_NET_POLL_CONTROLLER
3089static void cxgb_netpoll(struct net_device *dev)
3090{
3091 struct port_info *pi = netdev_priv(dev);
3092 struct adapter *adap = pi->adapter;
3093
3094 if (adap->flags & USING_MSIX) {
3095 int i;
3096 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
3097
3098 for (i = pi->nqsets; i; i--, rx++)
3099 t4_sge_intr_msix(0, &rx->rspq);
3100 } else
3101 t4_intr_handler(adap)(0, adap);
3102}
3103#endif
3104
3105static const struct net_device_ops cxgb4_netdev_ops = {
3106 .ndo_open = cxgb_open,
3107 .ndo_stop = cxgb_close,
3108 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003109 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003110 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003111 .ndo_set_rx_mode = cxgb_set_rxmode,
3112 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003113 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003114 .ndo_validate_addr = eth_validate_addr,
3115 .ndo_do_ioctl = cxgb_ioctl,
3116 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003117#ifdef CONFIG_NET_POLL_CONTROLLER
3118 .ndo_poll_controller = cxgb_netpoll,
3119#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303120#ifdef CONFIG_CHELSIO_T4_FCOE
3121 .ndo_fcoe_enable = cxgb_fcoe_enable,
3122 .ndo_fcoe_disable = cxgb_fcoe_disable,
3123#endif /* CONFIG_CHELSIO_T4_FCOE */
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05303124#ifdef CONFIG_NET_RX_BUSY_POLL
3125 .ndo_busy_poll = cxgb_busy_poll,
3126#endif
3127
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003128};
3129
3130void t4_fatal_err(struct adapter *adap)
3131{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303132 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003133 t4_intr_disable(adap);
3134 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3135}
3136
3137static void setup_memwin(struct adapter *adap)
3138{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303139 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003140
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303141 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003142}
3143
3144static void setup_memwin_rdma(struct adapter *adap)
3145{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003146 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303147 u32 start;
3148 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003149
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303150 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3151 start &= PCI_BASE_ADDRESS_MEM_MASK;
3152 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003153 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3154 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303155 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3156 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003157 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303158 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003159 adap->vres.ocq.start);
3160 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303161 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003162 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003163}
3164
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003165static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3166{
3167 u32 v;
3168 int ret;
3169
3170 /* get device capabilities */
3171 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303172 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3173 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303174 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303175 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003176 if (ret < 0)
3177 return ret;
3178
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303179 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3180 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303181 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003182 if (ret < 0)
3183 return ret;
3184
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303185 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003186 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303187 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3188 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003189 if (ret < 0)
3190 return ret;
3191
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303192 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303193 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3194 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003195 if (ret < 0)
3196 return ret;
3197
3198 t4_sge_init(adap);
3199
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003200 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303201 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303202 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303203 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3204 v = t4_read_reg(adap, TP_PIO_DATA_A);
3205 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003206
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003207 /* first 4 Tx modulation queues point to consecutive Tx channels */
3208 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303209 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3210 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003211
3212 /* associate each Tx modulation queue with consecutive Tx channels */
3213 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303214 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303215 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303216 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303217 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303218 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303219 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003220
3221#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3222 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303223 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3224 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3225 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3226 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3227 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3228 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3229 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3230 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3231 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3232 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003233 }
3234
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003235 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303236 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003237}
3238
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003239/*
3240 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3241 */
3242#define MAX_ATIDS 8192U
3243
3244/*
3245 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003246 *
3247 * If the firmware we're dealing with has Configuration File support, then
3248 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003249 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003250
3251/*
3252 * Tweak configuration based on module parameters, etc. Most of these have
3253 * defaults assigned to them by Firmware Configuration Files (if we're using
3254 * them) but need to be explicitly set if we're using hard-coded
3255 * initialization. But even in the case of using Firmware Configuration
3256 * Files, we'd like to expose the ability to change these via module
3257 * parameters so these are essentially common tweaks/settings for
3258 * Configuration Files and hard-coded initialization ...
3259 */
3260static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003261{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003262 /*
3263 * Fix up various Host-Dependent Parameters like Page Size, Cache
3264 * Line Size, etc. The firmware default is for a 4KB Page Size and
3265 * 64B Cache Line Size ...
3266 */
3267 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003268
Vipul Pandya636f9d32012-09-26 02:39:39 +00003269 /*
3270 * Process module parameters which affect early initialization.
3271 */
3272 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3273 dev_err(&adapter->pdev->dev,
3274 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3275 rx_dma_offset);
3276 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003277 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303278 t4_set_reg_field(adapter, SGE_CONTROL_A,
3279 PKTSHIFT_V(PKTSHIFT_M),
3280 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003281
Vipul Pandya636f9d32012-09-26 02:39:39 +00003282 /*
3283 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3284 * adds the pseudo header itself.
3285 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303286 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3287 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003288
3289 return 0;
3290}
3291
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303292/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3293 * unto themselves and they contain their own firmware to perform their
3294 * tasks ...
3295 */
3296static int phy_aq1202_version(const u8 *phy_fw_data,
3297 size_t phy_fw_size)
3298{
3299 int offset;
3300
3301 /* At offset 0x8 you're looking for the primary image's
3302 * starting offset which is 3 Bytes wide
3303 *
3304 * At offset 0xa of the primary image, you look for the offset
3305 * of the DRAM segment which is 3 Bytes wide.
3306 *
3307 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3308 * wide
3309 */
3310 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3311 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3312 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3313
3314 offset = le24(phy_fw_data + 0x8) << 12;
3315 offset = le24(phy_fw_data + offset + 0xa);
3316 return be16(phy_fw_data + offset + 0x27e);
3317
3318 #undef be16
3319 #undef le16
3320 #undef le24
3321}
3322
3323static struct info_10gbt_phy_fw {
3324 unsigned int phy_fw_id; /* PCI Device ID */
3325 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3326 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3327 int phy_flash; /* Has FLASH for PHY Firmware */
3328} phy_info_array[] = {
3329 {
3330 PHY_AQ1202_DEVICEID,
3331 PHY_AQ1202_FIRMWARE,
3332 phy_aq1202_version,
3333 1,
3334 },
3335 {
3336 PHY_BCM84834_DEVICEID,
3337 PHY_BCM84834_FIRMWARE,
3338 NULL,
3339 0,
3340 },
3341 { 0, NULL, NULL },
3342};
3343
3344static struct info_10gbt_phy_fw *find_phy_info(int devid)
3345{
3346 int i;
3347
3348 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3349 if (phy_info_array[i].phy_fw_id == devid)
3350 return &phy_info_array[i];
3351 }
3352 return NULL;
3353}
3354
3355/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3356 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3357 * we return a negative error number. If we transfer new firmware we return 1
3358 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3359 */
3360static int adap_init0_phy(struct adapter *adap)
3361{
3362 const struct firmware *phyf;
3363 int ret;
3364 struct info_10gbt_phy_fw *phy_info;
3365
3366 /* Use the device ID to determine which PHY file to flash.
3367 */
3368 phy_info = find_phy_info(adap->pdev->device);
3369 if (!phy_info) {
3370 dev_warn(adap->pdev_dev,
3371 "No PHY Firmware file found for this PHY\n");
3372 return -EOPNOTSUPP;
3373 }
3374
3375 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3376 * use that. The adapter firmware provides us with a memory buffer
3377 * where we can load a PHY firmware file from the host if we want to
3378 * override the PHY firmware File in flash.
3379 */
3380 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3381 adap->pdev_dev);
3382 if (ret < 0) {
3383 /* For adapters without FLASH attached to PHY for their
3384 * firmware, it's obviously a fatal error if we can't get the
3385 * firmware to the adapter. For adapters with PHY firmware
3386 * FLASH storage, it's worth a warning if we can't find the
3387 * PHY Firmware but we'll neuter the error ...
3388 */
3389 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3390 "/lib/firmware/%s, error %d\n",
3391 phy_info->phy_fw_file, -ret);
3392 if (phy_info->phy_flash) {
3393 int cur_phy_fw_ver = 0;
3394
3395 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3396 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3397 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3398 ret = 0;
3399 }
3400
3401 return ret;
3402 }
3403
3404 /* Load PHY Firmware onto adapter.
3405 */
3406 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3407 phy_info->phy_fw_version,
3408 (u8 *)phyf->data, phyf->size);
3409 if (ret < 0)
3410 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3411 -ret);
3412 else if (ret > 0) {
3413 int new_phy_fw_ver = 0;
3414
3415 if (phy_info->phy_fw_version)
3416 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3417 phyf->size);
3418 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3419 "Firmware /lib/firmware/%s, version %#x\n",
3420 phy_info->phy_fw_file, new_phy_fw_ver);
3421 }
3422
3423 release_firmware(phyf);
3424
3425 return ret;
3426}
3427
Vipul Pandya636f9d32012-09-26 02:39:39 +00003428/*
3429 * Attempt to initialize the adapter via a Firmware Configuration File.
3430 */
3431static int adap_init0_config(struct adapter *adapter, int reset)
3432{
3433 struct fw_caps_config_cmd caps_cmd;
3434 const struct firmware *cf;
3435 unsigned long mtype = 0, maddr = 0;
3436 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303437 int ret;
3438 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003439 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303440 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003441
3442 /*
3443 * Reset device if necessary.
3444 */
3445 if (reset) {
3446 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303447 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003448 if (ret < 0)
3449 goto bye;
3450 }
3451
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303452 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3453 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3454 * to be performed after any global adapter RESET above since some
3455 * PHYs only have local RAM copies of the PHY firmware.
3456 */
3457 if (is_10gbt_device(adapter->pdev->device)) {
3458 ret = adap_init0_phy(adapter);
3459 if (ret < 0)
3460 goto bye;
3461 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003462 /*
3463 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3464 * then use that. Otherwise, use the configuration file stored
3465 * in the adapter flash ...
3466 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303467 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003468 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303469 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003470 break;
3471 case CHELSIO_T5:
3472 fw_config_file = FW5_CFNAME;
3473 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303474 case CHELSIO_T6:
3475 fw_config_file = FW6_CFNAME;
3476 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003477 default:
3478 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3479 adapter->pdev->device);
3480 ret = -EINVAL;
3481 goto bye;
3482 }
3483
3484 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003485 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303486 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003487 mtype = FW_MEMTYPE_CF_FLASH;
3488 maddr = t4_flash_cfg_addr(adapter);
3489 } else {
3490 u32 params[7], val[7];
3491
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303492 sprintf(fw_config_file_path,
3493 "/lib/firmware/%s", fw_config_file);
3494 config_name = fw_config_file_path;
3495
Vipul Pandya636f9d32012-09-26 02:39:39 +00003496 if (cf->size >= FLASH_CFG_MAX_SIZE)
3497 ret = -ENOMEM;
3498 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303499 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3500 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003501 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303502 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003503 if (ret == 0) {
3504 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303505 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003506 * sizes have to be in terms of multiples of 4
3507 * bytes. So, if the Configuration File isn't
3508 * a multiple of 4 bytes in length we'll have
3509 * to write that out separately since we can't
3510 * guarantee that the bytes following the
3511 * residual byte in the buffer returned by
3512 * request_firmware() are zeroed out ...
3513 */
3514 size_t resid = cf->size & 0x3;
3515 size_t size = cf->size & ~0x3;
3516 __be32 *data = (__be32 *)cf->data;
3517
Hariprasad Shenai51678652014-11-21 12:52:02 +05303518 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3519 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003520
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303521 spin_lock(&adapter->win0_lock);
3522 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3523 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003524 if (ret == 0 && resid != 0) {
3525 union {
3526 __be32 word;
3527 char buf[4];
3528 } last;
3529 int i;
3530
3531 last.word = data[size >> 2];
3532 for (i = resid; i < 4; i++)
3533 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303534 ret = t4_memory_rw(adapter, 0, mtype,
3535 maddr + size,
3536 4, &last.word,
3537 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003538 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303539 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003540 }
3541 }
3542
3543 release_firmware(cf);
3544 if (ret)
3545 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003546 }
3547
Vipul Pandya636f9d32012-09-26 02:39:39 +00003548 /*
3549 * Issue a Capability Configuration command to the firmware to get it
3550 * to parse the Configuration File. We don't use t4_fw_config_file()
3551 * because we want the ability to modify various features after we've
3552 * processed the configuration file ...
3553 */
3554 memset(&caps_cmd, 0, sizeof(caps_cmd));
3555 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303556 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3557 FW_CMD_REQUEST_F |
3558 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303559 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303560 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3561 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3562 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003563 FW_LEN16(caps_cmd));
3564 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3565 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303566
3567 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3568 * Configuration File in FLASH), our last gasp effort is to use the
3569 * Firmware Configuration File which is embedded in the firmware. A
3570 * very few early versions of the firmware didn't have one embedded
3571 * but we can ignore those.
3572 */
3573 if (ret == -ENOENT) {
3574 memset(&caps_cmd, 0, sizeof(caps_cmd));
3575 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303576 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3577 FW_CMD_REQUEST_F |
3578 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303579 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3580 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3581 sizeof(caps_cmd), &caps_cmd);
3582 config_name = "Firmware Default";
3583 }
3584
3585 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003586 if (ret < 0)
3587 goto bye;
3588
Vipul Pandya636f9d32012-09-26 02:39:39 +00003589 finiver = ntohl(caps_cmd.finiver);
3590 finicsum = ntohl(caps_cmd.finicsum);
3591 cfcsum = ntohl(caps_cmd.cfcsum);
3592 if (finicsum != cfcsum)
3593 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3594 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3595 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003596
Vipul Pandya636f9d32012-09-26 02:39:39 +00003597 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003598 * And now tell the firmware to use the configuration we just loaded.
3599 */
3600 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303601 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3602 FW_CMD_REQUEST_F |
3603 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303604 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003605 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3606 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003607 if (ret < 0)
3608 goto bye;
3609
Vipul Pandya636f9d32012-09-26 02:39:39 +00003610 /*
3611 * Tweak configuration based on system architecture, module
3612 * parameters, etc.
3613 */
3614 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003615 if (ret < 0)
3616 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003617
Vipul Pandya636f9d32012-09-26 02:39:39 +00003618 /*
3619 * And finally tell the firmware to initialize itself using the
3620 * parameters from the Configuration File.
3621 */
3622 ret = t4_fw_initialize(adapter, adapter->mbox);
3623 if (ret < 0)
3624 goto bye;
3625
Hariprasad Shenai06640312015-01-13 15:19:25 +05303626 /* Emit Firmware Configuration File information and return
3627 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003628 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003629 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303630 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3631 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003632 return 0;
3633
3634 /*
3635 * Something bad happened. Return the error ... (If the "error"
3636 * is that there's no Configuration File on the adapter we don't
3637 * want to issue a warning since this is fairly common.)
3638 */
3639bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303640 if (config_issued && ret != -ENOENT)
3641 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3642 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003643 return ret;
3644}
3645
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303646static struct fw_info fw_info_array[] = {
3647 {
3648 .chip = CHELSIO_T4,
3649 .fs_name = FW4_CFNAME,
3650 .fw_mod_name = FW4_FNAME,
3651 .fw_hdr = {
3652 .chip = FW_HDR_CHIP_T4,
3653 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3654 .intfver_nic = FW_INTFVER(T4, NIC),
3655 .intfver_vnic = FW_INTFVER(T4, VNIC),
3656 .intfver_ri = FW_INTFVER(T4, RI),
3657 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3658 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3659 },
3660 }, {
3661 .chip = CHELSIO_T5,
3662 .fs_name = FW5_CFNAME,
3663 .fw_mod_name = FW5_FNAME,
3664 .fw_hdr = {
3665 .chip = FW_HDR_CHIP_T5,
3666 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3667 .intfver_nic = FW_INTFVER(T5, NIC),
3668 .intfver_vnic = FW_INTFVER(T5, VNIC),
3669 .intfver_ri = FW_INTFVER(T5, RI),
3670 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3671 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3672 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303673 }, {
3674 .chip = CHELSIO_T6,
3675 .fs_name = FW6_CFNAME,
3676 .fw_mod_name = FW6_FNAME,
3677 .fw_hdr = {
3678 .chip = FW_HDR_CHIP_T6,
3679 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3680 .intfver_nic = FW_INTFVER(T6, NIC),
3681 .intfver_vnic = FW_INTFVER(T6, VNIC),
3682 .intfver_ofld = FW_INTFVER(T6, OFLD),
3683 .intfver_ri = FW_INTFVER(T6, RI),
3684 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3685 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3686 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3687 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3688 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303689 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303690
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303691};
3692
3693static struct fw_info *find_fw_info(int chip)
3694{
3695 int i;
3696
3697 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3698 if (fw_info_array[i].chip == chip)
3699 return &fw_info_array[i];
3700 }
3701 return NULL;
3702}
3703
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003704/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003705 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003706 */
3707static int adap_init0(struct adapter *adap)
3708{
3709 int ret;
3710 u32 v, port_vec;
3711 enum dev_state state;
3712 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003713 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303714 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003715
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303716 /* Grab Firmware Device Log parameters as early as possible so we have
3717 * access to it for debugging, etc.
3718 */
3719 ret = t4_init_devlog_params(adap);
3720 if (ret < 0)
3721 return ret;
3722
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303723 /* Contact FW, advertising Master capability */
3724 ret = t4_fw_hello(adap, adap->mbox, adap->mbox, MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003725 if (ret < 0) {
3726 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3727 ret);
3728 return ret;
3729 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003730 if (ret == adap->mbox)
3731 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003732
Vipul Pandya636f9d32012-09-26 02:39:39 +00003733 /*
3734 * If we're the Master PF Driver and the device is uninitialized,
3735 * then let's consider upgrading the firmware ... (We always want
3736 * to check the firmware version number in order to A. get it for
3737 * later reporting and B. to warn if the currently loaded firmware
3738 * is excessively mismatched relative to the driver.)
3739 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303740 t4_get_fw_version(adap, &adap->params.fw_vers);
3741 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303742 ret = t4_check_fw_version(adap);
3743 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303744 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303745 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003746 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303747 struct fw_info *fw_info;
3748 struct fw_hdr *card_fw;
3749 const struct firmware *fw;
3750 const u8 *fw_data = NULL;
3751 unsigned int fw_size = 0;
3752
3753 /* This is the firmware whose headers the driver was compiled
3754 * against
3755 */
3756 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3757 if (fw_info == NULL) {
3758 dev_err(adap->pdev_dev,
3759 "unable to get firmware info for chip %d.\n",
3760 CHELSIO_CHIP_VERSION(adap->params.chip));
3761 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003762 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303763
3764 /* allocate memory to read the header of the firmware on the
3765 * card
3766 */
3767 card_fw = t4_alloc_mem(sizeof(*card_fw));
3768
3769 /* Get FW from from /lib/firmware/ */
3770 ret = request_firmware(&fw, fw_info->fw_mod_name,
3771 adap->pdev_dev);
3772 if (ret < 0) {
3773 dev_err(adap->pdev_dev,
3774 "unable to load firmware image %s, error %d\n",
3775 fw_info->fw_mod_name, ret);
3776 } else {
3777 fw_data = fw->data;
3778 fw_size = fw->size;
3779 }
3780
3781 /* upgrade FW logic */
3782 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3783 state, &reset);
3784
3785 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003786 release_firmware(fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303787 t4_free_mem(card_fw);
3788
Vipul Pandya636f9d32012-09-26 02:39:39 +00003789 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303790 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003791 }
3792
3793 /*
3794 * Grab VPD parameters. This should be done after we establish a
3795 * connection to the firmware since some of the VPD parameters
3796 * (notably the Core Clock frequency) are retrieved via requests to
3797 * the firmware. On the other hand, we need these fairly early on
3798 * so we do this right after getting ahold of the firmware.
3799 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303800 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003801 if (ret < 0)
3802 goto bye;
3803
Vipul Pandya636f9d32012-09-26 02:39:39 +00003804 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003805 * Find out what ports are available to us. Note that we need to do
3806 * this before calling adap_init0_no_config() since it needs nports
3807 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003808 */
3809 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303810 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3811 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303812 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003813 if (ret < 0)
3814 goto bye;
3815
3816 adap->params.nports = hweight32(port_vec);
3817 adap->params.portvec = port_vec;
3818
Hariprasad Shenai06640312015-01-13 15:19:25 +05303819 /* If the firmware is initialized already, emit a simply note to that
3820 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003821 */
3822 if (state == DEV_STATE_INIT) {
3823 dev_info(adap->pdev_dev, "Coming up as %s: "\
3824 "Adapter already initialized\n",
3825 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003826 } else {
3827 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3828 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303829
3830 /* Find out whether we're dealing with a version of the
3831 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003832 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303833 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3834 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303835 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303836 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003837
Hariprasad Shenai06640312015-01-13 15:19:25 +05303838 /* If the firmware doesn't support Configuration Files,
3839 * return an error.
3840 */
3841 if (ret < 0) {
3842 dev_err(adap->pdev_dev, "firmware doesn't support "
3843 "Firmware Configuration Files\n");
3844 goto bye;
3845 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003846
Hariprasad Shenai06640312015-01-13 15:19:25 +05303847 /* The firmware provides us with a memory buffer where we can
3848 * load a Configuration File from the host if we want to
3849 * override the Configuration File in flash.
3850 */
3851 ret = adap_init0_config(adap, reset);
3852 if (ret == -ENOENT) {
3853 dev_err(adap->pdev_dev, "no Configuration File "
3854 "present on adapter.\n");
3855 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003856 }
3857 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303858 dev_err(adap->pdev_dev, "could not initialize "
3859 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003860 goto bye;
3861 }
3862 }
3863
Hariprasad Shenai06640312015-01-13 15:19:25 +05303864 /* Give the SGE code a chance to pull in anything that it needs ...
3865 * Note that this must be called after we retrieve our VPD parameters
3866 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003867 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303868 ret = t4_sge_init(adap);
3869 if (ret < 0)
3870 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003871
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003872 if (is_bypass_device(adap->pdev->device))
3873 adap->params.bypass = 1;
3874
Vipul Pandya636f9d32012-09-26 02:39:39 +00003875 /*
3876 * Grab some of our basic fundamental operating parameters.
3877 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003878#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303879 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3880 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003881
3882#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303883 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3884 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3885 FW_PARAMS_PARAM_Y_V(0) | \
3886 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003887
Vipul Pandya636f9d32012-09-26 02:39:39 +00003888 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003889 params[1] = FW_PARAM_PFVF(L2T_START);
3890 params[2] = FW_PARAM_PFVF(L2T_END);
3891 params[3] = FW_PARAM_PFVF(FILTER_START);
3892 params[4] = FW_PARAM_PFVF(FILTER_END);
3893 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303894 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003895 if (ret < 0)
3896 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003897 adap->sge.egr_start = val[0];
3898 adap->l2t_start = val[1];
3899 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003900 adap->tids.ftid_base = val[3];
3901 adap->tids.nftids = val[4] - val[3] + 1;
3902 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003903
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303904 /* qids (ingress/egress) returned from firmware can be anywhere
3905 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3906 * Hence driver needs to allocate memory for this range to
3907 * store the queue info. Get the highest IQFLINT/EQ index returned
3908 * in FW_EQ_*_CMD.alloc command.
3909 */
3910 params[0] = FW_PARAM_PFVF(EQ_END);
3911 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303912 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303913 if (ret < 0)
3914 goto bye;
3915 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3916 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3917
3918 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3919 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3920 if (!adap->sge.egr_map) {
3921 ret = -ENOMEM;
3922 goto bye;
3923 }
3924
3925 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3926 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3927 if (!adap->sge.ingr_map) {
3928 ret = -ENOMEM;
3929 goto bye;
3930 }
3931
3932 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303933 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303934 */
3935 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3936 sizeof(long), GFP_KERNEL);
3937 if (!adap->sge.starving_fl) {
3938 ret = -ENOMEM;
3939 goto bye;
3940 }
3941
3942 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3943 sizeof(long), GFP_KERNEL);
3944 if (!adap->sge.txq_maperr) {
3945 ret = -ENOMEM;
3946 goto bye;
3947 }
3948
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303949#ifdef CONFIG_DEBUG_FS
3950 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3951 sizeof(long), GFP_KERNEL);
3952 if (!adap->sge.blocked_fl) {
3953 ret = -ENOMEM;
3954 goto bye;
3955 }
3956#endif
3957
Anish Bhattb5a02f52015-01-14 15:17:34 -08003958 params[0] = FW_PARAM_PFVF(CLIP_START);
3959 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303960 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003961 if (ret < 0)
3962 goto bye;
3963 adap->clipt_start = val[0];
3964 adap->clipt_end = val[1];
3965
Vipul Pandya636f9d32012-09-26 02:39:39 +00003966 /* query params related to active filter region */
3967 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3968 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303969 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003970 /* If Active filter size is set we enable establishing
3971 * offload connection through firmware work request
3972 */
3973 if ((val[0] != val[1]) && (ret >= 0)) {
3974 adap->flags |= FW_OFLD_CONN;
3975 adap->tids.aftid_base = val[0];
3976 adap->tids.aftid_end = val[1];
3977 }
3978
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003979 /* If we're running on newer firmware, let it know that we're
3980 * prepared to deal with encapsulated CPL messages. Older
3981 * firmware won't understand this and we'll just get
3982 * unencapsulated messages ...
3983 */
3984 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3985 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303986 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003987
Vipul Pandya636f9d32012-09-26 02:39:39 +00003988 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303989 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3990 * capability. Earlier versions of the firmware didn't have the
3991 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3992 * permission to use ULPTX MEMWRITE DSGL.
3993 */
3994 if (is_t4(adap->params.chip)) {
3995 adap->params.ulptx_memwrite_dsgl = false;
3996 } else {
3997 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303998 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303999 1, params, val);
4000 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4001 }
4002
4003 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004004 * Get device capabilities so we can determine what resources we need
4005 * to manage.
4006 */
4007 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304008 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4009 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304010 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004011 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4012 &caps_cmd);
4013 if (ret < 0)
4014 goto bye;
4015
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004016 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004017 /* query offload-related parameters */
4018 params[0] = FW_PARAM_DEV(NTID);
4019 params[1] = FW_PARAM_PFVF(SERVER_START);
4020 params[2] = FW_PARAM_PFVF(SERVER_END);
4021 params[3] = FW_PARAM_PFVF(TDDP_START);
4022 params[4] = FW_PARAM_PFVF(TDDP_END);
4023 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304024 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004025 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004026 if (ret < 0)
4027 goto bye;
4028 adap->tids.ntids = val[0];
4029 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4030 adap->tids.stid_base = val[1];
4031 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004032 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004033 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004034 * region into two parts. Regular filters get 1/3rd and server
4035 * filters get 2/3rd part. This is only enabled if workarond
4036 * path is enabled.
4037 * 1. For regular filters.
4038 * 2. Server filter: This are special filters which are used
4039 * to redirect SYN packets to offload queue.
4040 */
4041 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4042 adap->tids.sftid_base = adap->tids.ftid_base +
4043 DIV_ROUND_UP(adap->tids.nftids, 3);
4044 adap->tids.nsftids = adap->tids.nftids -
4045 DIV_ROUND_UP(adap->tids.nftids, 3);
4046 adap->tids.nftids = adap->tids.sftid_base -
4047 adap->tids.ftid_base;
4048 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004049 adap->vres.ddp.start = val[3];
4050 adap->vres.ddp.size = val[4] - val[3] + 1;
4051 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004052
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004053 adap->params.offload = 1;
4054 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004055 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004056 params[0] = FW_PARAM_PFVF(STAG_START);
4057 params[1] = FW_PARAM_PFVF(STAG_END);
4058 params[2] = FW_PARAM_PFVF(RQ_START);
4059 params[3] = FW_PARAM_PFVF(RQ_END);
4060 params[4] = FW_PARAM_PFVF(PBL_START);
4061 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304062 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004063 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004064 if (ret < 0)
4065 goto bye;
4066 adap->vres.stag.start = val[0];
4067 adap->vres.stag.size = val[1] - val[0] + 1;
4068 adap->vres.rq.start = val[2];
4069 adap->vres.rq.size = val[3] - val[2] + 1;
4070 adap->vres.pbl.start = val[4];
4071 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004072
4073 params[0] = FW_PARAM_PFVF(SQRQ_START);
4074 params[1] = FW_PARAM_PFVF(SQRQ_END);
4075 params[2] = FW_PARAM_PFVF(CQ_START);
4076 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004077 params[4] = FW_PARAM_PFVF(OCQ_START);
4078 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304079 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304080 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004081 if (ret < 0)
4082 goto bye;
4083 adap->vres.qp.start = val[0];
4084 adap->vres.qp.size = val[1] - val[0] + 1;
4085 adap->vres.cq.start = val[2];
4086 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004087 adap->vres.ocq.start = val[4];
4088 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304089
4090 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4091 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304092 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304093 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304094 if (ret < 0) {
4095 adap->params.max_ordird_qp = 8;
4096 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4097 ret = 0;
4098 } else {
4099 adap->params.max_ordird_qp = val[0];
4100 adap->params.max_ird_adapter = val[1];
4101 }
4102 dev_info(adap->pdev_dev,
4103 "max_ordird_qp %d max_ird_adapter %d\n",
4104 adap->params.max_ordird_qp,
4105 adap->params.max_ird_adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004106 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004107 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004108 params[0] = FW_PARAM_PFVF(ISCSI_START);
4109 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304110 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004111 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004112 if (ret < 0)
4113 goto bye;
4114 adap->vres.iscsi.start = val[0];
4115 adap->vres.iscsi.size = val[1] - val[0] + 1;
4116 }
4117#undef FW_PARAM_PFVF
4118#undef FW_PARAM_DEV
4119
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304120 /* The MTU/MSS Table is initialized by now, so load their values. If
4121 * we're initializing the adapter, then we'll make any modifications
4122 * we want to the MTU/MSS Table and also initialize the congestion
4123 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004124 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004125 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304126 if (state != DEV_STATE_INIT) {
4127 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004128
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304129 /* The default MTU Table contains values 1492 and 1500.
4130 * However, for TCP, it's better to have two values which are
4131 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4132 * This allows us to have a TCP Data Payload which is a
4133 * multiple of 8 regardless of what combination of TCP Options
4134 * are in use (always a multiple of 4 bytes) which is
4135 * important for performance reasons. For instance, if no
4136 * options are in use, then we have a 20-byte IP header and a
4137 * 20-byte TCP header. In this case, a 1500-byte MSS would
4138 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4139 * which is not a multiple of 8. So using an MSS of 1488 in
4140 * this case results in a TCP Data Payload of 1448 bytes which
4141 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4142 * Stamps have been negotiated, then an MTU of 1500 bytes
4143 * results in a TCP Data Payload of 1448 bytes which, as
4144 * above, is a multiple of 8 bytes ...
4145 */
4146 for (i = 0; i < NMTUS; i++)
4147 if (adap->params.mtus[i] == 1492) {
4148 adap->params.mtus[i] = 1488;
4149 break;
4150 }
4151
4152 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4153 adap->params.b_wnd);
4154 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304155 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004156 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05304157 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004158 return 0;
4159
4160 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004161 * Something bad happened. If a command timed out or failed with EIO
4162 * FW does not operate within its spec or something catastrophic
4163 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004164 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004165bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304166 kfree(adap->sge.egr_map);
4167 kfree(adap->sge.ingr_map);
4168 kfree(adap->sge.starving_fl);
4169 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304170#ifdef CONFIG_DEBUG_FS
4171 kfree(adap->sge.blocked_fl);
4172#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004173 if (ret != -ETIMEDOUT && ret != -EIO)
4174 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004175 return ret;
4176}
4177
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004178/* EEH callbacks */
4179
4180static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4181 pci_channel_state_t state)
4182{
4183 int i;
4184 struct adapter *adap = pci_get_drvdata(pdev);
4185
4186 if (!adap)
4187 goto out;
4188
4189 rtnl_lock();
4190 adap->flags &= ~FW_OK;
4191 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004192 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004193 for_each_port(adap, i) {
4194 struct net_device *dev = adap->port[i];
4195
4196 netif_device_detach(dev);
4197 netif_carrier_off(dev);
4198 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004199 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304200 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004201 if (adap->flags & FULL_INIT_DONE)
4202 cxgb_down(adap);
4203 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004204 if ((adap->flags & DEV_ENABLED)) {
4205 pci_disable_device(pdev);
4206 adap->flags &= ~DEV_ENABLED;
4207 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004208out: return state == pci_channel_io_perm_failure ?
4209 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4210}
4211
4212static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4213{
4214 int i, ret;
4215 struct fw_caps_config_cmd c;
4216 struct adapter *adap = pci_get_drvdata(pdev);
4217
4218 if (!adap) {
4219 pci_restore_state(pdev);
4220 pci_save_state(pdev);
4221 return PCI_ERS_RESULT_RECOVERED;
4222 }
4223
Gavin Shan144be3d2014-01-23 12:27:34 +08004224 if (!(adap->flags & DEV_ENABLED)) {
4225 if (pci_enable_device(pdev)) {
4226 dev_err(&pdev->dev, "Cannot reenable PCI "
4227 "device after reset\n");
4228 return PCI_ERS_RESULT_DISCONNECT;
4229 }
4230 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004231 }
4232
4233 pci_set_master(pdev);
4234 pci_restore_state(pdev);
4235 pci_save_state(pdev);
4236 pci_cleanup_aer_uncorrect_error_status(pdev);
4237
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304238 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004239 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304240 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004241 return PCI_ERS_RESULT_DISCONNECT;
4242 adap->flags |= FW_OK;
4243 if (adap_init1(adap, &c))
4244 return PCI_ERS_RESULT_DISCONNECT;
4245
4246 for_each_port(adap, i) {
4247 struct port_info *p = adap2pinfo(adap, i);
4248
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304249 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004250 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004251 if (ret < 0)
4252 return PCI_ERS_RESULT_DISCONNECT;
4253 p->viid = ret;
4254 p->xact_addr_filt = -1;
4255 }
4256
4257 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4258 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004259 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004260 if (cxgb_up(adap))
4261 return PCI_ERS_RESULT_DISCONNECT;
4262 return PCI_ERS_RESULT_RECOVERED;
4263}
4264
4265static void eeh_resume(struct pci_dev *pdev)
4266{
4267 int i;
4268 struct adapter *adap = pci_get_drvdata(pdev);
4269
4270 if (!adap)
4271 return;
4272
4273 rtnl_lock();
4274 for_each_port(adap, i) {
4275 struct net_device *dev = adap->port[i];
4276
4277 if (netif_running(dev)) {
4278 link_start(dev);
4279 cxgb_set_rxmode(dev);
4280 }
4281 netif_device_attach(dev);
4282 }
4283 rtnl_unlock();
4284}
4285
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004286static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004287 .error_detected = eeh_err_detected,
4288 .slot_reset = eeh_slot_reset,
4289 .resume = eeh_resume,
4290};
4291
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304292static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004293{
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304294 return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
4295 (lc->supported & FW_PORT_CAP_SPEED_40G) != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004296}
4297
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304298static inline void init_rspq(struct adapter *adap, struct sge_rspq *q,
4299 unsigned int us, unsigned int cnt,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004300 unsigned int size, unsigned int iqe_size)
4301{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304302 q->adap = adap;
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304303 cxgb4_set_rspq_intr_params(q, us, cnt);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004304 q->iqe_len = iqe_size;
4305 q->size = size;
4306}
4307
4308/*
4309 * Perform default configuration of DMA queues depending on the number and type
4310 * of ports we found and the number of available CPUs. Most settings can be
4311 * modified by the admin prior to actual use.
4312 */
Bill Pemberton91744942012-12-03 09:23:02 -05004313static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004314{
4315 struct sge *s = &adap->sge;
Anish Bhatt688848b2014-06-19 21:37:13 -07004316 int i, n10g = 0, qidx = 0;
4317#ifndef CONFIG_CHELSIO_T4_DCB
4318 int q10g = 0;
4319#endif
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304320 int ciq_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004321
4322 for_each_port(adap, i)
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304323 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004324#ifdef CONFIG_CHELSIO_T4_DCB
4325 /* For Data Center Bridging support we need to be able to support up
4326 * to 8 Traffic Priorities; each of which will be assigned to its
4327 * own TX Queue in order to prevent Head-Of-Line Blocking.
4328 */
4329 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4330 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4331 MAX_ETH_QSETS, adap->params.nports * 8);
4332 BUG_ON(1);
4333 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004334
Anish Bhatt688848b2014-06-19 21:37:13 -07004335 for_each_port(adap, i) {
4336 struct port_info *pi = adap2pinfo(adap, i);
4337
4338 pi->first_qset = qidx;
4339 pi->nqsets = 8;
4340 qidx += pi->nqsets;
4341 }
4342#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004343 /*
4344 * We default to 1 queue per non-10G port and up to # of cores queues
4345 * per 10G port.
4346 */
4347 if (n10g)
4348 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004349 if (q10g > netif_get_num_default_rss_queues())
4350 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004351
4352 for_each_port(adap, i) {
4353 struct port_info *pi = adap2pinfo(adap, i);
4354
4355 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304356 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004357 qidx += pi->nqsets;
4358 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004359#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004360
4361 s->ethqsets = qidx;
4362 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4363
4364 if (is_offload(adap)) {
4365 /*
4366 * For offload we use 1 queue/channel if all ports are up to 1G,
4367 * otherwise we divide all available queues amongst the channels
4368 * capped by the number of available cores.
4369 */
4370 if (n10g) {
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304371 i = min_t(int, ARRAY_SIZE(s->iscsirxq),
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004372 num_online_cpus());
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304373 s->iscsiqsets = roundup(i, adap->params.nports);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004374 } else
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304375 s->iscsiqsets = adap->params.nports;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004376 /* For RDMA one Rx queue per channel suffices */
4377 s->rdmaqs = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304378 /* Try and allow at least 1 CIQ per cpu rounding down
4379 * to the number of ports, with a minimum of 1 per port.
4380 * A 2 port card in a 6 cpu system: 6 CIQs, 3 / port.
4381 * A 4 port card in a 6 cpu system: 4 CIQs, 1 / port.
4382 * A 4 port card in a 2 cpu system: 4 CIQs, 1 / port.
4383 */
4384 s->rdmaciqs = min_t(int, MAX_RDMA_CIQS, num_online_cpus());
4385 s->rdmaciqs = (s->rdmaciqs / adap->params.nports) *
4386 adap->params.nports;
4387 s->rdmaciqs = max_t(int, s->rdmaciqs, adap->params.nports);
Varun Prakashf2692d12016-02-14 23:02:40 +05304388
4389 if (!is_t4(adap->params.chip))
4390 s->niscsitq = s->iscsiqsets;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004391 }
4392
4393 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4394 struct sge_eth_rxq *r = &s->ethrxq[i];
4395
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304396 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004397 r->fl.size = 72;
4398 }
4399
4400 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4401 s->ethtxq[i].q.size = 1024;
4402
4403 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4404 s->ctrlq[i].q.size = 512;
4405
4406 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
4407 s->ofldtxq[i].q.size = 1024;
4408
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304409 for (i = 0; i < ARRAY_SIZE(s->iscsirxq); i++) {
4410 struct sge_ofld_rxq *r = &s->iscsirxq[i];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004411
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304412 init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004413 r->rspq.uld = CXGB4_ULD_ISCSI;
4414 r->fl.size = 72;
4415 }
4416
Varun Prakashf2692d12016-02-14 23:02:40 +05304417 if (!is_t4(adap->params.chip)) {
4418 for (i = 0; i < ARRAY_SIZE(s->iscsitrxq); i++) {
4419 struct sge_ofld_rxq *r = &s->iscsitrxq[i];
4420
4421 init_rspq(adap, &r->rspq, 5, 1, 1024, 64);
4422 r->rspq.uld = CXGB4_ULD_ISCSIT;
4423 r->fl.size = 72;
4424 }
4425 }
4426
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004427 for (i = 0; i < ARRAY_SIZE(s->rdmarxq); i++) {
4428 struct sge_ofld_rxq *r = &s->rdmarxq[i];
4429
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304430 init_rspq(adap, &r->rspq, 5, 1, 511, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004431 r->rspq.uld = CXGB4_ULD_RDMA;
4432 r->fl.size = 72;
4433 }
4434
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304435 ciq_size = 64 + adap->vres.cq.size + adap->tids.nftids;
4436 if (ciq_size > SGE_MAX_IQ_SIZE) {
4437 CH_WARN(adap, "CIQ size too small for available IQs\n");
4438 ciq_size = SGE_MAX_IQ_SIZE;
4439 }
4440
4441 for (i = 0; i < ARRAY_SIZE(s->rdmaciq); i++) {
4442 struct sge_ofld_rxq *r = &s->rdmaciq[i];
4443
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304444 init_rspq(adap, &r->rspq, 5, 1, ciq_size, 64);
Hariprasad Shenaicf38be62014-06-06 21:40:42 +05304445 r->rspq.uld = CXGB4_ULD_RDMA;
4446 }
4447
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304448 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
4449 init_rspq(adap, &s->intrq, 0, 1, 2 * MAX_INGQ, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004450}
4451
4452/*
4453 * Reduce the number of Ethernet queues across all ports to at most n.
4454 * n provides at least one queue per port.
4455 */
Bill Pemberton91744942012-12-03 09:23:02 -05004456static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004457{
4458 int i;
4459 struct port_info *pi;
4460
4461 while (n < adap->sge.ethqsets)
4462 for_each_port(adap, i) {
4463 pi = adap2pinfo(adap, i);
4464 if (pi->nqsets > 1) {
4465 pi->nqsets--;
4466 adap->sge.ethqsets--;
4467 if (adap->sge.ethqsets <= n)
4468 break;
4469 }
4470 }
4471
4472 n = 0;
4473 for_each_port(adap, i) {
4474 pi = adap2pinfo(adap, i);
4475 pi->first_qset = n;
4476 n += pi->nqsets;
4477 }
4478}
4479
4480/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4481#define EXTRA_VECS 2
4482
Bill Pemberton91744942012-12-03 09:23:02 -05004483static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004484{
4485 int ofld_need = 0;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304486 int i, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004487 struct sge *s = &adap->sge;
4488 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304489 struct msix_entry *entries;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004490
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304491 entries = kmalloc(sizeof(*entries) * (MAX_INGQ + 1),
4492 GFP_KERNEL);
4493 if (!entries)
4494 return -ENOMEM;
4495
4496 for (i = 0; i < MAX_INGQ + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004497 entries[i].entry = i;
4498
4499 want = s->max_ethqsets + EXTRA_VECS;
4500 if (is_offload(adap)) {
Varun Prakashf2692d12016-02-14 23:02:40 +05304501 want += s->rdmaqs + s->rdmaciqs + s->iscsiqsets +
4502 s->niscsitq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004503 /* need nchan for each possible ULD */
Varun Prakashf2692d12016-02-14 23:02:40 +05304504 if (is_t4(adap->params.chip))
4505 ofld_need = 3 * nchan;
4506 else
4507 ofld_need = 4 * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004508 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004509#ifdef CONFIG_CHELSIO_T4_DCB
4510 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4511 * each port.
4512 */
4513 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need;
4514#else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004515 need = adap->params.nports + EXTRA_VECS + ofld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004516#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304517 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4518 if (allocated < 0) {
4519 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4520 " not using MSI-X\n");
4521 kfree(entries);
4522 return allocated;
4523 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004524
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304525 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004526 * Every group gets its minimum requirement and NIC gets top
4527 * priority for leftovers.
4528 */
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304529 i = allocated - EXTRA_VECS - ofld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004530 if (i < s->max_ethqsets) {
4531 s->max_ethqsets = i;
4532 if (i < s->ethqsets)
4533 reduce_ethqs(adap, i);
4534 }
4535 if (is_offload(adap)) {
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304536 if (allocated < want) {
4537 s->rdmaqs = nchan;
4538 s->rdmaciqs = nchan;
Varun Prakashf2692d12016-02-14 23:02:40 +05304539
4540 if (!is_t4(adap->params.chip))
4541 s->niscsitq = nchan;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304542 }
4543
4544 /* leftovers go to OFLD */
4545 i = allocated - EXTRA_VECS - s->max_ethqsets -
Varun Prakashf2692d12016-02-14 23:02:40 +05304546 s->rdmaqs - s->rdmaciqs - s->niscsitq;
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304547 s->iscsiqsets = (i / nchan) * nchan; /* round down */
Varun Prakashf2692d12016-02-14 23:02:40 +05304548
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004549 }
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304550 for (i = 0; i < allocated; ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004551 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304552 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
4553 "nic %d iscsi %d rdma cpl %d rdma ciq %d\n",
Hariprasad Shenaif90ce562015-12-23 11:29:54 +05304554 allocated, s->max_ethqsets, s->iscsiqsets, s->rdmaqs,
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304555 s->rdmaciqs);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004556
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304557 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004558 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004559}
4560
4561#undef EXTRA_VECS
4562
Bill Pemberton91744942012-12-03 09:23:02 -05004563static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004564{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304565 unsigned int i;
4566 int err;
4567
4568 err = t4_init_rss_mode(adap, adap->mbox);
4569 if (err)
4570 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004571
4572 for_each_port(adap, i) {
4573 struct port_info *pi = adap2pinfo(adap, i);
4574
4575 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4576 if (!pi->rss)
4577 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004578 }
4579 return 0;
4580}
4581
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304582static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4583 enum pci_bus_speed *speed,
4584 enum pcie_link_width *width)
4585{
4586 u32 lnkcap1, lnkcap2;
4587 int err1, err2;
4588
4589#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4590
4591 *speed = PCI_SPEED_UNKNOWN;
4592 *width = PCIE_LNK_WIDTH_UNKNOWN;
4593
4594 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4595 &lnkcap1);
4596 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4597 &lnkcap2);
4598 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4599 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4600 *speed = PCIE_SPEED_8_0GT;
4601 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4602 *speed = PCIE_SPEED_5_0GT;
4603 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4604 *speed = PCIE_SPEED_2_5GT;
4605 }
4606 if (!err1) {
4607 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4608 if (!lnkcap2) { /* pre-r3.0 */
4609 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4610 *speed = PCIE_SPEED_5_0GT;
4611 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4612 *speed = PCIE_SPEED_2_5GT;
4613 }
4614 }
4615
4616 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4617 return err1 ? err1 : err2 ? err2 : -EINVAL;
4618 return 0;
4619}
4620
4621static void cxgb4_check_pcie_caps(struct adapter *adap)
4622{
4623 enum pcie_link_width width, width_cap;
4624 enum pci_bus_speed speed, speed_cap;
4625
4626#define PCIE_SPEED_STR(speed) \
4627 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4628 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4629 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4630 "Unknown")
4631
4632 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4633 dev_warn(adap->pdev_dev,
4634 "Unable to determine PCIe device BW capabilities\n");
4635 return;
4636 }
4637
4638 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4639 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4640 dev_warn(adap->pdev_dev,
4641 "Unable to determine PCI Express bandwidth.\n");
4642 return;
4643 }
4644
4645 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4646 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4647 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4648 width, width_cap);
4649 if (speed < speed_cap || width < width_cap)
4650 dev_info(adap->pdev_dev,
4651 "A slot with more lanes and/or higher speed is "
4652 "suggested for optimal performance.\n");
4653}
4654
Bill Pemberton91744942012-12-03 09:23:02 -05004655static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004656{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004657 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004658 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004659 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004660 const struct port_info *pi = netdev_priv(dev);
4661 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004662
4663 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4664 spd = " 2.5 GT/s";
4665 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4666 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004667 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4668 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004669
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004670 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4671 bufp += sprintf(bufp, "100/");
4672 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4673 bufp += sprintf(bufp, "1000/");
4674 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4675 bufp += sprintf(bufp, "10G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304676 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4677 bufp += sprintf(bufp, "40G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004678 if (bufp != buf)
4679 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304680 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004681
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304682 netdev_info(dev, "Chelsio %s rev %d %s %sNIC %s\n",
Santosh Rastapur0a57a532013-03-14 05:08:49 +00004683 adap->params.vpd.id,
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304684 CHELSIO_CHIP_RELEASE(adap->params.chip), buf,
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304685 is_offload(adap) ? "R" : "",
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004686 (adap->flags & USING_MSIX) ? " MSI-X" :
4687 (adap->flags & USING_MSI) ? " MSI" : "");
Kumar Sanghvia94cd702014-02-18 17:56:09 +05304688 netdev_info(dev, "S/N: %s, P/N: %s\n",
4689 adap->params.vpd.sn, adap->params.vpd.pn);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004690}
4691
Bill Pemberton91744942012-12-03 09:23:02 -05004692static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004693{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004694 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004695}
4696
Dimitris Michailidis06546392010-07-11 12:01:16 +00004697/*
4698 * Free the following resources:
4699 * - memory used for tables
4700 * - MSI/MSI-X
4701 * - net devices
4702 * - resources FW is holding for us
4703 */
4704static void free_some_resources(struct adapter *adapter)
4705{
4706 unsigned int i;
4707
4708 t4_free_mem(adapter->l2t);
4709 t4_free_mem(adapter->tids.tid_tab);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304710 kfree(adapter->sge.egr_map);
4711 kfree(adapter->sge.ingr_map);
4712 kfree(adapter->sge.starving_fl);
4713 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304714#ifdef CONFIG_DEBUG_FS
4715 kfree(adapter->sge.blocked_fl);
4716#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004717 disable_msi(adapter);
4718
4719 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004720 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304721 struct port_info *pi = adap2pinfo(adapter, i);
4722
4723 if (pi->viid != 0)
4724 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4725 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004726 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004727 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004728 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004729 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304730 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004731}
4732
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004733#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004734#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004735 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004736#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004737
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304738static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4739{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304740 u16 device_id;
4741
4742 /* Retrieve adapter's device ID */
4743 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004744
4745 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304746 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004747 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304748 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004749 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304750 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004751 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304752 default:
4753 dev_err(&pdev->dev, "Device %d is not supported\n",
4754 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304755 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004756 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304757}
4758
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004759static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004760{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004761 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004762 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004763 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004764 struct adapter *adapter = NULL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304765 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304766 u32 whoami, pl_rev;
4767 enum chip_type chip;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004768
4769 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4770
4771 err = pci_request_regions(pdev, KBUILD_MODNAME);
4772 if (err) {
4773 /* Just info, some other driver may have claimed the device. */
4774 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4775 return err;
4776 }
4777
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004778 err = pci_enable_device(pdev);
4779 if (err) {
4780 dev_err(&pdev->dev, "cannot enable PCI device\n");
4781 goto out_release_regions;
4782 }
4783
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304784 regs = pci_ioremap_bar(pdev, 0);
4785 if (!regs) {
4786 dev_err(&pdev->dev, "cannot map device registers\n");
4787 err = -ENOMEM;
4788 goto out_disable_device;
4789 }
4790
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304791 err = t4_wait_dev_ready(regs);
4792 if (err < 0)
4793 goto out_unmap_bar0;
4794
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304795 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304796 whoami = readl(regs + PL_WHOAMI_A);
4797 pl_rev = REV_G(readl(regs + PL_REV_A));
4798 chip = get_chip_type(pdev, pl_rev);
4799 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4800 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304801 if (func != ent->driver_data) {
4802 iounmap(regs);
4803 pci_disable_device(pdev);
4804 pci_save_state(pdev); /* to restore SR-IOV later */
4805 goto sriov;
4806 }
4807
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004808 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004809 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004810 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4811 if (err) {
4812 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4813 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304814 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004815 }
4816 } else {
4817 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4818 if (err) {
4819 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304820 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004821 }
4822 }
4823
4824 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004825 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004826 pci_set_master(pdev);
4827 pci_save_state(pdev);
4828
4829 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4830 if (!adapter) {
4831 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304832 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004833 }
4834
Anish Bhatt29aaee62014-08-20 13:44:06 -07004835 adapter->workq = create_singlethread_workqueue("cxgb4");
4836 if (!adapter->workq) {
4837 err = -ENOMEM;
4838 goto out_free_adapter;
4839 }
4840
Gavin Shan144be3d2014-01-23 12:27:34 +08004841 /* PCI device has been enabled */
4842 adapter->flags |= DEV_ENABLED;
4843
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304844 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004845 adapter->pdev = pdev;
4846 adapter->pdev_dev = &pdev->dev;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304847 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304848 adapter->pf = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004849 adapter->msg_enable = dflt_msg_enable;
4850 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4851
4852 spin_lock_init(&adapter->stats_lock);
4853 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004854 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004855
4856 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304857 INIT_WORK(&adapter->db_full_task, process_db_full);
4858 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004859
4860 err = t4_prep_adapter(adapter);
4861 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304862 goto out_free_adapter;
4863
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004864
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304865 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304866 s_qpp = (QUEUESPERPAGEPF0_S +
4867 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304868 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304869 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4870 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004871 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4872
4873 /* Each segment size is 128B. Write coalescing is enabled only
4874 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4875 * queue is less no of segments that can be accommodated in
4876 * a page size.
4877 */
4878 if (qpp > num_seg) {
4879 dev_err(&pdev->dev,
4880 "Incorrect number of egress queues per page\n");
4881 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304882 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004883 }
4884 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4885 pci_resource_len(pdev, 2));
4886 if (!adapter->bar2) {
4887 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4888 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304889 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004890 }
4891 }
4892
Vipul Pandya636f9d32012-09-26 02:39:39 +00004893 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004894 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304895#ifdef CONFIG_DEBUG_FS
4896 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4897#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004898 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004899 if (err)
4900 goto out_unmap_bar;
4901
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304902 /* configure SGE_STAT_CFG_A to read WC stats */
4903 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304904 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4905 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4906 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304907
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004908 for_each_port(adapter, i) {
4909 struct net_device *netdev;
4910
4911 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4912 MAX_ETH_QSETS);
4913 if (!netdev) {
4914 err = -ENOMEM;
4915 goto out_free_dev;
4916 }
4917
4918 SET_NETDEV_DEV(netdev, &pdev->dev);
4919
4920 adapter->port[i] = netdev;
4921 pi = netdev_priv(netdev);
4922 pi->adapter = adapter;
4923 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004924 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004925 netdev->irq = pdev->irq;
4926
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004927 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4928 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4929 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Patrick McHardyf6469682013-04-19 02:04:27 +00004930 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004931 if (highdma)
4932 netdev->hw_features |= NETIF_F_HIGHDMA;
4933 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004934 netdev->vlan_features = netdev->features & VLAN_FEAT;
4935
Jiri Pirko01789342011-08-16 06:29:00 +00004936 netdev->priv_flags |= IFF_UNICAST_FLT;
4937
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004938 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07004939#ifdef CONFIG_CHELSIO_T4_DCB
4940 netdev->dcbnl_ops = &cxgb4_dcb_ops;
4941 cxgb4_dcb_state_init(netdev);
4942#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304943 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004944 }
4945
4946 pci_set_drvdata(pdev, adapter);
4947
4948 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004949 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004950 if (err)
4951 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304952 } else if (adapter->params.nports == 1) {
4953 /* If we don't have a connection to the firmware -- possibly
4954 * because of an error -- grab the raw VPD parameters so we
4955 * can set the proper MAC Address on the debug network
4956 * interface that we've created.
4957 */
4958 u8 hw_addr[ETH_ALEN];
4959 u8 *na = adapter->params.vpd.na;
4960
4961 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
4962 if (!err) {
4963 for (i = 0; i < ETH_ALEN; i++)
4964 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
4965 hex2val(na[2 * i + 1]));
4966 t4_set_hw_addr(adapter, 0, hw_addr);
4967 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004968 }
4969
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304970 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004971 * soon as the first register_netdev completes.
4972 */
4973 cfg_queues(adapter);
4974
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05304975 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004976 if (!adapter->l2t) {
4977 /* We tolerate a lack of L2T, giving up some functionality */
4978 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
4979 adapter->params.offload = 0;
4980 }
4981
Anish Bhattb5a02f52015-01-14 15:17:34 -08004982#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304983 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
4984 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
4985 /* CLIP functionality is not present in hardware,
4986 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08004987 */
4988 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304989 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08004990 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304991 } else {
4992 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
4993 adapter->clipt_end);
4994 if (!adapter->clipt) {
4995 /* We tolerate a lack of clip_table, giving up
4996 * some functionality
4997 */
4998 dev_warn(&pdev->dev,
4999 "could not allocate Clip table, continuing\n");
5000 adapter->params.offload = 0;
5001 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005002 }
5003#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005004 if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
5005 dev_warn(&pdev->dev, "could not allocate TID table, "
5006 "continuing\n");
5007 adapter->params.offload = 0;
5008 }
5009
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305010 if (is_offload(adapter)) {
5011 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5012 u32 hash_base, hash_reg;
5013
5014 if (chip <= CHELSIO_T5) {
5015 hash_reg = LE_DB_TID_HASHBASE_A;
5016 hash_base = t4_read_reg(adapter, hash_reg);
5017 adapter->tids.hash_base = hash_base / 4;
5018 } else {
5019 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5020 hash_base = t4_read_reg(adapter, hash_reg);
5021 adapter->tids.hash_base = hash_base;
5022 }
5023 }
5024 }
5025
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005026 /* See what interrupts we'll be using */
5027 if (msi > 1 && enable_msix(adapter) == 0)
5028 adapter->flags |= USING_MSIX;
5029 else if (msi > 0 && pci_enable_msi(pdev) == 0)
5030 adapter->flags |= USING_MSI;
5031
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305032 /* check for PCI Express bandwidth capabiltites */
5033 cxgb4_check_pcie_caps(adapter);
5034
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005035 err = init_rss(adapter);
5036 if (err)
5037 goto out_free_dev;
5038
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005039 /*
5040 * The card is now ready to go. If any errors occur during device
5041 * registration we do not fail the whole card but rather proceed only
5042 * with the ports we manage to register successfully. However we must
5043 * register at least one net device.
5044 */
5045 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005046 pi = adap2pinfo(adapter, i);
5047 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5048 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5049
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005050 err = register_netdev(adapter->port[i]);
5051 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005052 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005053 adapter->chan_map[pi->tx_chan] = i;
5054 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005055 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005056 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005057 dev_err(&pdev->dev, "could not register any net devices\n");
5058 goto out_free_dev;
5059 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005060 if (err) {
5061 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5062 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005063 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005064
5065 if (cxgb4_debugfs_root) {
5066 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5067 cxgb4_debugfs_root);
5068 setup_debugfs(adapter);
5069 }
5070
David S. Miller88c51002011-10-07 13:38:43 -04005071 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5072 pdev->needs_freset = 1;
5073
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005074 if (is_offload(adapter))
5075 attach_ulds(adapter);
5076
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305077sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005078#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +00005079 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005080 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
5081 dev_info(&pdev->dev,
5082 "instantiated %u virtual functions\n",
5083 num_vf[func]);
5084#endif
5085 return 0;
5086
5087 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005088 free_some_resources(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005089 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305090 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005091 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005092 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005093 if (adapter->workq)
5094 destroy_workqueue(adapter->workq);
5095
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005096 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305097 out_unmap_bar0:
5098 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005099 out_disable_device:
5100 pci_disable_pcie_error_reporting(pdev);
5101 pci_disable_device(pdev);
5102 out_release_regions:
5103 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005104 return err;
5105}
5106
Bill Pemberton91744942012-12-03 09:23:02 -05005107static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005108{
5109 struct adapter *adapter = pci_get_drvdata(pdev);
5110
Vipul Pandya636f9d32012-09-26 02:39:39 +00005111#ifdef CONFIG_PCI_IOV
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005112 pci_disable_sriov(pdev);
5113
Vipul Pandya636f9d32012-09-26 02:39:39 +00005114#endif
5115
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005116 if (adapter) {
5117 int i;
5118
Anish Bhatt29aaee62014-08-20 13:44:06 -07005119 /* Tear down per-adapter Work Queue first since it can contain
5120 * references to our adapter data structure.
5121 */
5122 destroy_workqueue(adapter->workq);
5123
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005124 if (is_offload(adapter))
5125 detach_ulds(adapter);
5126
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305127 disable_interrupts(adapter);
5128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005129 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005130 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005131 unregister_netdev(adapter->port[i]);
5132
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005133 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005134
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005135 /* If we allocated filters, free up state associated with any
5136 * valid filters ...
5137 */
5138 if (adapter->tids.ftid_tab) {
5139 struct filter_entry *f = &adapter->tids.ftid_tab[0];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00005140 for (i = 0; i < (adapter->tids.nftids +
5141 adapter->tids.nsftids); i++, f++)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005142 if (f->valid)
5143 clear_filter(adapter, f);
5144 }
5145
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005146 if (adapter->flags & FULL_INIT_DONE)
5147 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005148
Dimitris Michailidis06546392010-07-11 12:01:16 +00005149 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005150#if IS_ENABLED(CONFIG_IPV6)
5151 t4_cleanup_clip_tbl(adapter);
5152#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005153 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305154 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005155 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005156 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005157 if ((adapter->flags & DEV_ENABLED)) {
5158 pci_disable_device(pdev);
5159 adapter->flags &= ~DEV_ENABLED;
5160 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005161 pci_release_regions(pdev);
Li RongQingee9a33b2014-06-20 17:32:36 +08005162 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005163 kfree(adapter);
Dimitris Michailidisa069ec92010-09-30 09:17:12 +00005164 } else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005165 pci_release_regions(pdev);
5166}
5167
5168static struct pci_driver cxgb4_driver = {
5169 .name = KBUILD_MODNAME,
5170 .id_table = cxgb4_pci_tbl,
5171 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005172 .remove = remove_one,
Thadeu Lima de Souza Cascardo687d7052014-02-24 17:04:52 -03005173 .shutdown = remove_one,
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005174 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005175};
5176
5177static int __init cxgb4_init_module(void)
5178{
5179 int ret;
5180
5181 /* Debugfs support is optional, just warn if this fails */
5182 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5183 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005184 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005185
5186 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005187 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005188 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305189
Anish Bhatt1bb60372014-10-14 20:07:22 -07005190#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005191 if (!inet6addr_registered) {
5192 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5193 inet6addr_registered = true;
5194 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005195#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305196
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005197 return ret;
5198}
5199
5200static void __exit cxgb4_cleanup_module(void)
5201{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005202#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305203 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005204 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5205 inet6addr_registered = false;
5206 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005207#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005208 pci_unregister_driver(&cxgb4_driver);
5209 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005210}
5211
5212module_init(cxgb4_init_module);
5213module_exit(cxgb4_cleanup_module);