blob: d1ebb84c073e320d78f44008c839c07f1fb9de6b [file] [log] [blame]
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/bitmap.h>
38#include <linux/crc32.h>
39#include <linux/ctype.h>
40#include <linux/debugfs.h>
41#include <linux/err.h>
42#include <linux/etherdevice.h>
43#include <linux/firmware.h>
Jiri Pirko01789342011-08-16 06:29:00 +000044#include <linux/if.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000045#include <linux/if_vlan.h>
46#include <linux/init.h>
47#include <linux/log2.h>
48#include <linux/mdio.h>
49#include <linux/module.h>
50#include <linux/moduleparam.h>
51#include <linux/mutex.h>
52#include <linux/netdevice.h>
53#include <linux/pci.h>
54#include <linux/aer.h>
55#include <linux/rtnetlink.h>
56#include <linux/sched.h>
57#include <linux/seq_file.h>
58#include <linux/sockios.h>
59#include <linux/vmalloc.h>
60#include <linux/workqueue.h>
61#include <net/neighbour.h>
62#include <net/netevent.h>
Vipul Pandya01bcca62013-07-04 16:10:46 +053063#include <net/addrconf.h>
David S. Miller1ef80192014-11-10 13:27:49 -050064#include <net/bonding.h>
Anish Bhattb5a02f52015-01-14 15:17:34 -080065#include <net/addrconf.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000066#include <asm/uaccess.h>
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +053067#include <linux/crash_dump.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000068
69#include "cxgb4.h"
70#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053071#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000072#include "t4_msg.h"
73#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053074#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070075#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053076#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080077#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000078#include "l2t.h"
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +053079#include "sched.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000080
Hariprasad Shenai812034f2015-04-06 20:23:23 +053081char cxgb4_driver_name[] = KBUILD_MODNAME;
82
Vipul Pandya01bcca62013-07-04 16:10:46 +053083#ifdef DRV_VERSION
84#undef DRV_VERSION
85#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000086#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053087const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053088#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000089
Vipul Pandyaf2b7e782012-12-10 09:30:52 +000090/* Host shadow copy of ingress filter entry. This is in host native format
91 * and doesn't match the ordering or bit order, etc. of the hardware of the
92 * firmware command. The use of bit-field structure elements is purely to
93 * remind ourselves of the field size limitations and save memory in the case
94 * where the filter table is large.
95 */
96struct filter_entry {
97 /* Administrative fields for filter.
98 */
99 u32 valid:1; /* filter allocated and valid */
100 u32 locked:1; /* filter is administratively locked */
101
102 u32 pending:1; /* filter action is pending firmware reply */
103 u32 smtidx:8; /* Source MAC Table index for smac */
104 struct l2t_entry *l2t; /* Layer Two Table entry for dmac */
105
106 /* The filter itself. Most of this is a straight copy of information
107 * provided by the extended ioctl(). Some fields are translated to
108 * internal forms -- for instance the Ingress Queue ID passed in from
109 * the ioctl() is translated into the Absolute Ingress Queue ID.
110 */
111 struct ch_filter_specification fs;
112};
113
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000114#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
115 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
116 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
117
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530118/* Macros needed to support the PCI Device ID Table ...
119 */
120#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530121 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530122#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000123
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530124/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
125 * called for both.
126 */
127#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
128
129#define CH_PCI_ID_TABLE_ENTRY(devid) \
130 {PCI_VDEVICE(CHELSIO, (devid)), 4}
131
132#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
133 { 0, } \
134 }
135
136#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000137
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530138#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000139#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530140#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530141#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000142#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530143#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530144#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
145#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
146#define PHY_AQ1202_DEVICEID 0x4409
147#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000148
149MODULE_DESCRIPTION(DRV_DESC);
150MODULE_AUTHOR("Chelsio Communications");
151MODULE_LICENSE("Dual BSD/GPL");
152MODULE_VERSION(DRV_VERSION);
153MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530154MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000155MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530156MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000157
Vipul Pandya636f9d32012-09-26 02:39:39 +0000158/*
159 * Normally we're willing to become the firmware's Master PF but will be happy
160 * if another PF has already become the Master and initialized the adapter.
161 * Setting "force_init" will cause this driver to forcibly establish itself as
162 * the Master PF and initialize the adapter.
163 */
164static uint force_init;
165
166module_param(force_init, uint, 0644);
Hariprasad Shenaid7d3e252015-12-24 16:24:53 +0530167MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
168 "deprecated parameter");
Vipul Pandya13ee15d2012-09-26 02:39:40 +0000169
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000170static int dflt_msg_enable = DFLT_MSG_ENABLE;
171
172module_param(dflt_msg_enable, int, 0644);
Hariprasad Shenai8a21ec42016-04-05 09:52:21 +0530173MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
174 "deprecated parameter");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000175
176/*
177 * The driver uses the best interrupt scheme available on a platform in the
178 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
179 * of these schemes the driver may consider as follows:
180 *
181 * msi = 2: choose from among all three options
182 * msi = 1: only consider MSI and INTx interrupts
183 * msi = 0: force INTx interrupts
184 */
185static int msi = 2;
186
187module_param(msi, int, 0644);
188MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
189
190/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000191 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
192 * offset by 2 bytes in order to have the IP headers line up on 4-byte
193 * boundaries. This is a requirement for many architectures which will throw
194 * a machine check fault if an attempt is made to access one of the 4-byte IP
195 * header fields on a non-4-byte boundary. And it's a major performance issue
196 * even on some architectures which allow it like some implementations of the
197 * x86 ISA. However, some architectures don't mind this and for some very
198 * edge-case performance sensitive applications (like forwarding large volumes
199 * of small packets), setting this DMA offset to 0 will decrease the number of
200 * PCI-E Bus transfers enough to measurably affect performance.
201 */
202static int rx_dma_offset = 2;
203
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000204#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000205/* Configure the number of PCI-E Virtual Function which are to be instantiated
206 * on SR-IOV Capable Physical Functions.
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000207 */
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000208static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000209
210module_param_array(num_vf, uint, NULL, 0644);
Hariprasad Shenaib6244202016-06-14 14:39:31 +0530211MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3, deprecated parameter - please use the pci sysfs interface.");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000212#endif
213
Anish Bhatt688848b2014-06-19 21:37:13 -0700214/* TX Queue select used to determine what algorithm to use for selecting TX
215 * queue. Select between the kernel provided function (select_queue=0) or user
216 * cxgb_select_queue function (select_queue=1)
217 *
218 * Default: select_queue=0
219 */
220static int select_queue;
221module_param(select_queue, int, 0644);
222MODULE_PARM_DESC(select_queue,
223 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
224
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000225static struct dentry *cxgb4_debugfs_root;
226
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530227LIST_HEAD(adapter_list);
228DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000229
230static void link_report(struct net_device *dev)
231{
232 if (!netif_carrier_ok(dev))
233 netdev_info(dev, "link down\n");
234 else {
235 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
236
Hariprasad Shenai85412252015-10-01 13:48:48 +0530237 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000238 const struct port_info *p = netdev_priv(dev);
239
240 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000241 case 10000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000242 s = "10Gbps";
243 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000244 case 1000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000245 s = "1000Mbps";
246 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000247 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000248 s = "100Mbps";
249 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000250 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530251 s = "40Gbps";
252 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530253 default:
254 pr_info("%s: unsupported speed: %d\n",
255 dev->name, p->link_cfg.speed);
256 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000257 }
258
259 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
260 fc[p->link_cfg.fc]);
261 }
262}
263
Anish Bhatt688848b2014-06-19 21:37:13 -0700264#ifdef CONFIG_CHELSIO_T4_DCB
265/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
266static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
267{
268 struct port_info *pi = netdev_priv(dev);
269 struct adapter *adap = pi->adapter;
270 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
271 int i;
272
273 /* We use a simple mapping of Port TX Queue Index to DCB
274 * Priority when we're enabling DCB.
275 */
276 for (i = 0; i < pi->nqsets; i++, txq++) {
277 u32 name, value;
278 int err;
279
Hariprasad Shenai51678652014-11-21 12:52:02 +0530280 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
281 FW_PARAMS_PARAM_X_V(
282 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
283 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700284 value = enable ? i : 0xffffffff;
285
286 /* Since we can be called while atomic (from "interrupt
287 * level") we need to issue the Set Parameters Commannd
288 * without sleeping (timeout < 0).
289 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530290 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530291 &name, &value,
292 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700293
294 if (err)
295 dev_err(adap->pdev_dev,
296 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
297 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700298 else
299 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700300 }
301}
302#endif /* CONFIG_CHELSIO_T4_DCB */
303
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530304int cxgb4_dcb_enabled(const struct net_device *dev)
305{
306#ifdef CONFIG_CHELSIO_T4_DCB
307 struct port_info *pi = netdev_priv(dev);
308
309 if (!pi->dcb.enabled)
310 return 0;
311
312 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
313 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
314#else
315 return 0;
316#endif
317}
318EXPORT_SYMBOL(cxgb4_dcb_enabled);
319
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000320void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
321{
322 struct net_device *dev = adapter->port[port_id];
323
324 /* Skip changes from disabled ports. */
325 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
326 if (link_stat)
327 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700328 else {
329#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530330 if (cxgb4_dcb_enabled(dev)) {
331 cxgb4_dcb_state_init(dev);
332 dcb_tx_queue_prio_enable(dev, false);
333 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700334#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000335 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700336 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000337
338 link_report(dev);
339 }
340}
341
342void t4_os_portmod_changed(const struct adapter *adap, int port_id)
343{
344 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000345 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000346 };
347
348 const struct net_device *dev = adap->port[port_id];
349 const struct port_info *pi = netdev_priv(dev);
350
351 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
352 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000353 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000354 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530355 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
356 netdev_info(dev, "%s: unsupported port module inserted\n",
357 dev->name);
358 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
359 netdev_info(dev, "%s: unknown port module inserted\n",
360 dev->name);
361 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
362 netdev_info(dev, "%s: transceiver module error\n", dev->name);
363 else
364 netdev_info(dev, "%s: unknown module type %d inserted\n",
365 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000366}
367
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530368int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
369module_param(dbfifo_int_thresh, int, 0644);
370MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
371
Vipul Pandya404d9e32012-10-08 02:59:43 +0000372/*
373 * usecs to sleep while draining the dbfifo
374 */
375static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530376module_param(dbfifo_drain_delay, int, 0644);
377MODULE_PARM_DESC(dbfifo_drain_delay,
378 "usecs to sleep while draining the dbfifo");
379
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530380static inline int cxgb4_set_addr_hash(struct port_info *pi)
381{
382 struct adapter *adap = pi->adapter;
383 u64 vec = 0;
384 bool ucast = false;
385 struct hash_mac_addr *entry;
386
387 /* Calculate the hash vector for the updated list and program it */
388 list_for_each_entry(entry, &adap->mac_hlist, list) {
389 ucast |= is_unicast_ether_addr(entry->addr);
390 vec |= (1ULL << hash_mac_addr(entry->addr));
391 }
392 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
393 vec, false);
394}
395
396static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
397{
398 struct port_info *pi = netdev_priv(netdev);
399 struct adapter *adap = pi->adapter;
400 int ret;
401 u64 mhash = 0;
402 u64 uhash = 0;
403 bool free = false;
404 bool ucast = is_unicast_ether_addr(mac_addr);
405 const u8 *maclist[1] = {mac_addr};
406 struct hash_mac_addr *new_entry;
407
408 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
409 NULL, ucast ? &uhash : &mhash, false);
410 if (ret < 0)
411 goto out;
412 /* if hash != 0, then add the addr to hash addr list
413 * so on the end we will calculate the hash for the
414 * list and program it
415 */
416 if (uhash || mhash) {
417 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
418 if (!new_entry)
419 return -ENOMEM;
420 ether_addr_copy(new_entry->addr, mac_addr);
421 list_add_tail(&new_entry->list, &adap->mac_hlist);
422 ret = cxgb4_set_addr_hash(pi);
423 }
424out:
425 return ret < 0 ? ret : 0;
426}
427
428static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
429{
430 struct port_info *pi = netdev_priv(netdev);
431 struct adapter *adap = pi->adapter;
432 int ret;
433 const u8 *maclist[1] = {mac_addr};
434 struct hash_mac_addr *entry, *tmp;
435
436 /* If the MAC address to be removed is in the hash addr
437 * list, delete it from the list and update hash vector
438 */
439 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
440 if (ether_addr_equal(entry->addr, mac_addr)) {
441 list_del(&entry->list);
442 kfree(entry);
443 return cxgb4_set_addr_hash(pi);
444 }
445 }
446
447 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
448 return ret < 0 ? -EINVAL : 0;
449}
450
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000451/*
452 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
453 * If @mtu is -1 it is left unchanged.
454 */
455static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
456{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000457 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530458 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000459
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530460 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
461 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530462
463 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
464 (dev->flags & IFF_PROMISC) ? 1 : 0,
465 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
466 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000467}
468
469/**
470 * link_start - enable a port
471 * @dev: the port to enable
472 *
473 * Performs the MAC and PHY actions needed to enable a port.
474 */
475static int link_start(struct net_device *dev)
476{
477 int ret;
478 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530479 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000480
481 /*
482 * We do not set address filters and promiscuity here, the stack does
483 * that step explicitly.
484 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000485 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000486 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000487 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000488 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000489 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000490 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000491 if (ret >= 0) {
492 pi->xact_addr_filt = ret;
493 ret = 0;
494 }
495 }
496 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530497 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000498 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700499 if (ret == 0) {
500 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700501 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
502 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700503 local_bh_enable();
504 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700505
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000506 return ret;
507}
508
Anish Bhatt688848b2014-06-19 21:37:13 -0700509#ifdef CONFIG_CHELSIO_T4_DCB
510/* Handle a Data Center Bridging update message from the firmware. */
511static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
512{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530513 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530514 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700515 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
516 int new_dcb_enabled;
517
518 cxgb4_dcb_handle_fw_update(adap, pcmd);
519 new_dcb_enabled = cxgb4_dcb_enabled(dev);
520
521 /* If the DCB has become enabled or disabled on the port then we're
522 * going to need to set up/tear down DCB Priority parameters for the
523 * TX Queues associated with the port.
524 */
525 if (new_dcb_enabled != old_dcb_enabled)
526 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
527}
528#endif /* CONFIG_CHELSIO_T4_DCB */
529
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000530/* Clear a filter and release any of its resources that we own. This also
531 * clears the filter's "pending" status.
532 */
533static void clear_filter(struct adapter *adap, struct filter_entry *f)
534{
535 /* If the new or old filter have loopback rewriteing rules then we'll
536 * need to free any existing Layer Two Table (L2T) entries of the old
537 * filter rule. The firmware will handle freeing up any Source MAC
538 * Table (SMT) entries used for rewriting Source MAC Addresses in
539 * loopback rules.
540 */
541 if (f->l2t)
542 cxgb4_l2t_release(f->l2t);
543
544 /* The zeroing of the filter rule below clears the filter valid,
545 * pending, locked flags, l2t pointer, etc. so it's all we need for
546 * this operation.
547 */
548 memset(f, 0, sizeof(*f));
549}
550
551/* Handle a filter write/deletion reply.
552 */
553static void filter_rpl(struct adapter *adap, const struct cpl_set_tcb_rpl *rpl)
554{
555 unsigned int idx = GET_TID(rpl);
556 unsigned int nidx = idx - adap->tids.ftid_base;
557 unsigned int ret;
558 struct filter_entry *f;
559
560 if (idx >= adap->tids.ftid_base && nidx <
561 (adap->tids.nftids + adap->tids.nsftids)) {
562 idx = nidx;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800563 ret = TCB_COOKIE_G(rpl->cookie);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000564 f = &adap->tids.ftid_tab[idx];
565
566 if (ret == FW_FILTER_WR_FLT_DELETED) {
567 /* Clear the filter when we get confirmation from the
568 * hardware that the filter has been deleted.
569 */
570 clear_filter(adap, f);
571 } else if (ret == FW_FILTER_WR_SMT_TBL_FULL) {
572 dev_err(adap->pdev_dev, "filter %u setup failed due to full SMT\n",
573 idx);
574 clear_filter(adap, f);
575 } else if (ret == FW_FILTER_WR_FLT_ADDED) {
576 f->smtidx = (be64_to_cpu(rpl->oldval) >> 24) & 0xff;
577 f->pending = 0; /* asynchronous setup completed */
578 f->valid = 1;
579 } else {
580 /* Something went wrong. Issue a warning about the
581 * problem and clear everything out.
582 */
583 dev_err(adap->pdev_dev, "filter %u setup failed with error %u\n",
584 idx, ret);
585 clear_filter(adap, f);
586 }
587 }
588}
589
590/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000591 */
592static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
593 const struct pkt_gl *gl)
594{
595 u8 opcode = ((const struct rss_header *)rsp)->opcode;
596
597 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000598
599 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
600 */
601 if (unlikely(opcode == CPL_FW4_MSG &&
602 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
603 rsp++;
604 opcode = ((const struct rss_header *)rsp)->opcode;
605 rsp++;
606 if (opcode != CPL_SGE_EGR_UPDATE) {
607 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
608 , opcode);
609 goto out;
610 }
611 }
612
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000613 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
614 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800615 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000616 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000617
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000618 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000619 txq->restarts++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000620 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000621 struct sge_eth_txq *eq;
622
623 eq = container_of(txq, struct sge_eth_txq, q);
624 netif_tx_wake_queue(eq->txq);
625 } else {
626 struct sge_ofld_txq *oq;
627
628 oq = container_of(txq, struct sge_ofld_txq, q);
629 tasklet_schedule(&oq->qresume_tsk);
630 }
631 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
632 const struct cpl_fw6_msg *p = (void *)rsp;
633
Anish Bhatt688848b2014-06-19 21:37:13 -0700634#ifdef CONFIG_CHELSIO_T4_DCB
635 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530636 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700637 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530638 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700639
640 if (cmd == FW_PORT_CMD &&
641 action == FW_PORT_ACTION_GET_PORT_INFO) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530642 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700643 be32_to_cpu(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530644 struct net_device *dev =
645 q->adap->port[q->adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700646 int state_input = ((pcmd->u.info.dcbxdis_pkd &
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530647 FW_PORT_CMD_DCBXDIS_F)
Anish Bhatt688848b2014-06-19 21:37:13 -0700648 ? CXGB4_DCB_INPUT_FW_DISABLED
649 : CXGB4_DCB_INPUT_FW_ENABLED);
650
651 cxgb4_dcb_state_fsm(dev, state_input);
652 }
653
654 if (cmd == FW_PORT_CMD &&
655 action == FW_PORT_ACTION_L2_DCB_CFG)
656 dcb_rpl(q->adap, pcmd);
657 else
658#endif
659 if (p->type == 0)
660 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000661 } else if (opcode == CPL_L2T_WRITE_RPL) {
662 const struct cpl_l2t_write_rpl *p = (void *)rsp;
663
664 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000665 } else if (opcode == CPL_SET_TCB_RPL) {
666 const struct cpl_set_tcb_rpl *p = (void *)rsp;
667
668 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000669 } else
670 dev_err(q->adap->pdev_dev,
671 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000672out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000673 return 0;
674}
675
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000676static void disable_msi(struct adapter *adapter)
677{
678 if (adapter->flags & USING_MSIX) {
679 pci_disable_msix(adapter->pdev);
680 adapter->flags &= ~USING_MSIX;
681 } else if (adapter->flags & USING_MSI) {
682 pci_disable_msi(adapter->pdev);
683 adapter->flags &= ~USING_MSI;
684 }
685}
686
687/*
688 * Interrupt handler for non-data events used with MSI-X.
689 */
690static irqreturn_t t4_nondata_intr(int irq, void *cookie)
691{
692 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530693 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000694
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530695 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000696 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530697 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000698 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530699 if (adap->flags & MASTER_PF)
700 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000701 return IRQ_HANDLED;
702}
703
704/*
705 * Name the MSI-X interrupts.
706 */
707static void name_msix_vecs(struct adapter *adap)
708{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000709 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000710
711 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000712 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000713
714 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000715 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
716 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000717
718 /* Ethernet queues */
719 for_each_port(adap, j) {
720 struct net_device *d = adap->port[j];
721 const struct port_info *pi = netdev_priv(d);
722
Dimitris Michailidisba278162010-12-14 21:36:50 +0000723 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000724 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
725 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000726 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000727}
728
729static int request_msix_queue_irqs(struct adapter *adap)
730{
731 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530732 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530733 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000734
735 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
736 adap->msix_info[1].desc, &s->fw_evtq);
737 if (err)
738 return err;
739
740 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000741 err = request_irq(adap->msix_info[msi_index].vec,
742 t4_sge_intr_msix, 0,
743 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000744 &s->ethrxq[ethqidx].rspq);
745 if (err)
746 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000747 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000748 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000749 return 0;
750
751unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000752 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000753 free_irq(adap->msix_info[--msi_index].vec,
754 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000755 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
756 return err;
757}
758
759static void free_msix_queue_irqs(struct adapter *adap)
760{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000761 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000762 struct sge *s = &adap->sge;
763
764 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
765 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000766 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000767}
768
769/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530770 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000771 * @pi: the port
772 * @queues: array of queue indices for RSS
773 *
774 * Sets up the portion of the HW RSS table for the port's VI to distribute
775 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530776 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000777 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530778int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000779{
780 u16 *rss;
781 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530782 struct adapter *adapter = pi->adapter;
783 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000784
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530785 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000786 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
787 if (!rss)
788 return -ENOMEM;
789
790 /* map the queue indices to queue ids */
791 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530792 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000793
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530794 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000795 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530796 /* If Tunnel All Lookup isn't specified in the global RSS
797 * Configuration, then we need to specify a default Ingress
798 * Queue for any ingress packets which aren't hashed. We'll
799 * use our first ingress queue ...
800 */
801 if (!err)
802 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
803 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
804 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
805 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
806 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
807 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
808 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000809 kfree(rss);
810 return err;
811}
812
813/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000814 * setup_rss - configure RSS
815 * @adap: the adapter
816 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000817 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000818 */
819static int setup_rss(struct adapter *adap)
820{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530821 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000822
823 for_each_port(adap, i) {
824 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000825
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530826 /* Fill default values with equal distribution */
827 for (j = 0; j < pi->rss_size; j++)
828 pi->rss[j] = j % pi->nqsets;
829
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530830 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000831 if (err)
832 return err;
833 }
834 return 0;
835}
836
837/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000838 * Return the channel of the ingress queue with the given qid.
839 */
840static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
841{
842 qid -= p->ingr_start;
843 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
844}
845
846/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000847 * Wait until all NAPI handlers are descheduled.
848 */
849static void quiesce_rx(struct adapter *adap)
850{
851 int i;
852
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530853 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000854 struct sge_rspq *q = adap->sge.ingr_map[i];
855
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530856 if (q && q->handler) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000857 napi_disable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530858 local_bh_disable();
859 while (!cxgb_poll_lock_napi(q))
860 mdelay(1);
861 local_bh_enable();
862 }
863
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000864 }
865}
866
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530867/* Disable interrupt and napi handler */
868static void disable_interrupts(struct adapter *adap)
869{
870 if (adap->flags & FULL_INIT_DONE) {
871 t4_intr_disable(adap);
872 if (adap->flags & USING_MSIX) {
873 free_msix_queue_irqs(adap);
874 free_irq(adap->msix_info[0].vec, adap);
875 } else {
876 free_irq(adap->pdev->irq, adap);
877 }
878 quiesce_rx(adap);
879 }
880}
881
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000882/*
883 * Enable NAPI scheduling and interrupt generation for all Rx queues.
884 */
885static void enable_rx(struct adapter *adap)
886{
887 int i;
888
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530889 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000890 struct sge_rspq *q = adap->sge.ingr_map[i];
891
892 if (!q)
893 continue;
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530894 if (q->handler) {
895 cxgb_busy_poll_init_lock(q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000896 napi_enable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530897 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000898 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530899 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
900 SEINTARM_V(q->intr_params) |
901 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000902 }
903}
904
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530905
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530906static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000907{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000908 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530909 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000910
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530911 bitmap_zero(s->starving_fl, s->egr_sz);
912 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000913
914 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530915 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000916 else {
917 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530918 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000919 if (err)
920 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530921 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000922 }
923
924 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530925 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530926 if (err)
927 t4_free_sge_resources(adap);
928 return err;
929}
930
931/**
932 * setup_sge_queues - configure SGE Tx/Rx/response queues
933 * @adap: the adapter
934 *
935 * Determines how many sets of SGE queues to use and initializes them.
936 * We support multiple queue sets per port if we have MSI-X, otherwise
937 * just one queue set per port.
938 */
939static int setup_sge_queues(struct adapter *adap)
940{
941 int err, i, j;
942 struct sge *s = &adap->sge;
943 struct sge_uld_rxq_info *rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
944 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000945
946 for_each_port(adap, i) {
947 struct net_device *dev = adap->port[i];
948 struct port_info *pi = netdev_priv(dev);
949 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
950 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
951
952 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530953 if (adap->msi_idx > 0)
954 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000955 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530956 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530957 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530958 NULL,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530959 t4_get_mps_bg_map(adap,
960 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000961 if (err)
962 goto freeout;
963 q->rspq.idx = j;
964 memset(&q->stats, 0, sizeof(q->stats));
965 }
966 for (j = 0; j < pi->nqsets; j++, t++) {
967 err = t4_sge_alloc_eth_txq(adap, t, dev,
968 netdev_get_tx_queue(dev, j),
969 s->fw_evtq.cntxt_id);
970 if (err)
971 goto freeout;
972 }
973 }
974
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530975 j = s->ofldqsets / adap->params.nports; /* iscsi queues per channel */
976 for_each_ofldtxq(s, i) {
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530977 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
978 adap->port[i / j],
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000979 s->fw_evtq.cntxt_id);
980 if (err)
981 goto freeout;
982 }
983
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000984 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530985 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000986 * have RDMA queues, and that's the right value.
987 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530988 if (rxq_info)
989 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
990
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000991 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530992 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000993 if (err)
994 goto freeout;
995 }
996
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530997 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530998 MPS_TRC_RSS_CONTROL_A :
999 MPS_T5_TRC_RSS_CONTROL_A,
1000 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
1001 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001002 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301003freeout:
1004 t4_free_sge_resources(adap);
1005 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001006}
1007
1008/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001009 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1010 * The allocated memory is cleared.
1011 */
1012void *t4_alloc_mem(size_t size)
1013{
Joe Perches8be04b92013-06-19 12:15:53 -07001014 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001015
1016 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001017 p = vzalloc(size);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001018 return p;
1019}
1020
1021/*
1022 * Free memory allocated through alloc_mem().
1023 */
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301024void t4_free_mem(void *addr)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001025{
Pekka Enbergd2fcb542015-06-30 14:59:12 -07001026 kvfree(addr);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001027}
1028
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001029/* Send a Work Request to write the filter at a specified index. We construct
1030 * a Firmware Filter Work Request to have the work done and put the indicated
1031 * filter into "pending" mode which will prevent any further actions against
1032 * it till we get a reply from the firmware on the completion status of the
1033 * request.
1034 */
1035static int set_filter_wr(struct adapter *adapter, int fidx)
1036{
1037 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1038 struct sk_buff *skb;
1039 struct fw_filter_wr *fwr;
1040 unsigned int ftid;
1041
Michal Hockof72f1162015-04-14 13:24:33 -07001042 skb = alloc_skb(sizeof(*fwr), GFP_KERNEL);
1043 if (!skb)
1044 return -ENOMEM;
1045
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001046 /* If the new filter requires loopback Destination MAC and/or VLAN
1047 * rewriting then we need to allocate a Layer 2 Table (L2T) entry for
1048 * the filter.
1049 */
1050 if (f->fs.newdmac || f->fs.newvlan) {
1051 /* allocate L2T entry for new filter */
Hariprasad Shenaif7502652015-12-17 13:45:08 +05301052 f->l2t = t4_l2t_alloc_switching(adapter, f->fs.vlan,
1053 f->fs.eport, f->fs.dmac);
Michal Hockof72f1162015-04-14 13:24:33 -07001054 if (f->l2t == NULL) {
1055 kfree_skb(skb);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001056 return -ENOMEM;
1057 }
1058 }
1059
1060 ftid = adapter->tids.ftid_base + fidx;
1061
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001062 fwr = (struct fw_filter_wr *)__skb_put(skb, sizeof(*fwr));
1063 memset(fwr, 0, sizeof(*fwr));
1064
1065 /* It would be nice to put most of the following in t4_hw.c but most
1066 * of the work is translating the cxgbtool ch_filter_specification
1067 * into the Work Request and the definition of that structure is
1068 * currently in cxgbtool.h which isn't appropriate to pull into the
1069 * common code. We may eventually try to come up with a more neutral
1070 * filter specification structure but for now it's easiest to simply
1071 * put this fairly direct code in line ...
1072 */
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05301073 fwr->op_pkd = htonl(FW_WR_OP_V(FW_FILTER_WR));
1074 fwr->len16_pkd = htonl(FW_WR_LEN16_V(sizeof(*fwr)/16));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001075 fwr->tid_to_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301076 htonl(FW_FILTER_WR_TID_V(ftid) |
1077 FW_FILTER_WR_RQTYPE_V(f->fs.type) |
1078 FW_FILTER_WR_NOREPLY_V(0) |
1079 FW_FILTER_WR_IQ_V(f->fs.iq));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001080 fwr->del_filter_to_l2tix =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301081 htonl(FW_FILTER_WR_RPTTID_V(f->fs.rpttid) |
1082 FW_FILTER_WR_DROP_V(f->fs.action == FILTER_DROP) |
1083 FW_FILTER_WR_DIRSTEER_V(f->fs.dirsteer) |
1084 FW_FILTER_WR_MASKHASH_V(f->fs.maskhash) |
1085 FW_FILTER_WR_DIRSTEERHASH_V(f->fs.dirsteerhash) |
1086 FW_FILTER_WR_LPBK_V(f->fs.action == FILTER_SWITCH) |
1087 FW_FILTER_WR_DMAC_V(f->fs.newdmac) |
1088 FW_FILTER_WR_SMAC_V(f->fs.newsmac) |
1089 FW_FILTER_WR_INSVLAN_V(f->fs.newvlan == VLAN_INSERT ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001090 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301091 FW_FILTER_WR_RMVLAN_V(f->fs.newvlan == VLAN_REMOVE ||
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001092 f->fs.newvlan == VLAN_REWRITE) |
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301093 FW_FILTER_WR_HITCNTS_V(f->fs.hitcnts) |
1094 FW_FILTER_WR_TXCHAN_V(f->fs.eport) |
1095 FW_FILTER_WR_PRIO_V(f->fs.prio) |
1096 FW_FILTER_WR_L2TIX_V(f->l2t ? f->l2t->idx : 0));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001097 fwr->ethtype = htons(f->fs.val.ethtype);
1098 fwr->ethtypem = htons(f->fs.mask.ethtype);
1099 fwr->frag_to_ovlan_vldm =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301100 (FW_FILTER_WR_FRAG_V(f->fs.val.frag) |
1101 FW_FILTER_WR_FRAGM_V(f->fs.mask.frag) |
1102 FW_FILTER_WR_IVLAN_VLD_V(f->fs.val.ivlan_vld) |
1103 FW_FILTER_WR_OVLAN_VLD_V(f->fs.val.ovlan_vld) |
1104 FW_FILTER_WR_IVLAN_VLDM_V(f->fs.mask.ivlan_vld) |
1105 FW_FILTER_WR_OVLAN_VLDM_V(f->fs.mask.ovlan_vld));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001106 fwr->smac_sel = 0;
1107 fwr->rx_chan_rx_rpl_iq =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301108 htons(FW_FILTER_WR_RX_CHAN_V(0) |
1109 FW_FILTER_WR_RX_RPL_IQ_V(adapter->sge.fw_evtq.abs_id));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001110 fwr->maci_to_matchtypem =
Hariprasad Shenai77a80e22014-11-21 12:52:01 +05301111 htonl(FW_FILTER_WR_MACI_V(f->fs.val.macidx) |
1112 FW_FILTER_WR_MACIM_V(f->fs.mask.macidx) |
1113 FW_FILTER_WR_FCOE_V(f->fs.val.fcoe) |
1114 FW_FILTER_WR_FCOEM_V(f->fs.mask.fcoe) |
1115 FW_FILTER_WR_PORT_V(f->fs.val.iport) |
1116 FW_FILTER_WR_PORTM_V(f->fs.mask.iport) |
1117 FW_FILTER_WR_MATCHTYPE_V(f->fs.val.matchtype) |
1118 FW_FILTER_WR_MATCHTYPEM_V(f->fs.mask.matchtype));
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001119 fwr->ptcl = f->fs.val.proto;
1120 fwr->ptclm = f->fs.mask.proto;
1121 fwr->ttyp = f->fs.val.tos;
1122 fwr->ttypm = f->fs.mask.tos;
1123 fwr->ivlan = htons(f->fs.val.ivlan);
1124 fwr->ivlanm = htons(f->fs.mask.ivlan);
1125 fwr->ovlan = htons(f->fs.val.ovlan);
1126 fwr->ovlanm = htons(f->fs.mask.ovlan);
1127 memcpy(fwr->lip, f->fs.val.lip, sizeof(fwr->lip));
1128 memcpy(fwr->lipm, f->fs.mask.lip, sizeof(fwr->lipm));
1129 memcpy(fwr->fip, f->fs.val.fip, sizeof(fwr->fip));
1130 memcpy(fwr->fipm, f->fs.mask.fip, sizeof(fwr->fipm));
1131 fwr->lp = htons(f->fs.val.lport);
1132 fwr->lpm = htons(f->fs.mask.lport);
1133 fwr->fp = htons(f->fs.val.fport);
1134 fwr->fpm = htons(f->fs.mask.fport);
1135 if (f->fs.newsmac)
1136 memcpy(fwr->sma, f->fs.smac, sizeof(fwr->sma));
1137
1138 /* Mark the filter as "pending" and ship off the Filter Work Request.
1139 * When we get the Work Request Reply we'll clear the pending status.
1140 */
1141 f->pending = 1;
1142 set_wr_txq(skb, CPL_PRIORITY_CONTROL, f->fs.val.iport & 0x3);
1143 t4_ofld_send(adapter, skb);
1144 return 0;
1145}
1146
1147/* Delete the filter at a specified index.
1148 */
1149static int del_filter_wr(struct adapter *adapter, int fidx)
1150{
1151 struct filter_entry *f = &adapter->tids.ftid_tab[fidx];
1152 struct sk_buff *skb;
1153 struct fw_filter_wr *fwr;
1154 unsigned int len, ftid;
1155
1156 len = sizeof(*fwr);
1157 ftid = adapter->tids.ftid_base + fidx;
1158
Michal Hockof72f1162015-04-14 13:24:33 -07001159 skb = alloc_skb(len, GFP_KERNEL);
1160 if (!skb)
1161 return -ENOMEM;
1162
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001163 fwr = (struct fw_filter_wr *)__skb_put(skb, len);
1164 t4_mk_filtdelwr(ftid, fwr, adapter->sge.fw_evtq.abs_id);
1165
1166 /* Mark the filter as "pending" and ship off the Filter Work Request.
1167 * When we get the Work Request Reply we'll clear the pending status.
1168 */
1169 f->pending = 1;
1170 t4_mgmt_tx(adapter, skb);
1171 return 0;
1172}
1173
Anish Bhatt688848b2014-06-19 21:37:13 -07001174static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
1175 void *accel_priv, select_queue_fallback_t fallback)
1176{
1177 int txq;
1178
1179#ifdef CONFIG_CHELSIO_T4_DCB
1180 /* If a Data Center Bridging has been successfully negotiated on this
1181 * link then we'll use the skb's priority to map it to a TX Queue.
1182 * The skb's priority is determined via the VLAN Tag Priority Code
1183 * Point field.
1184 */
1185 if (cxgb4_dcb_enabled(dev)) {
1186 u16 vlan_tci;
1187 int err;
1188
1189 err = vlan_get_tag(skb, &vlan_tci);
1190 if (unlikely(err)) {
1191 if (net_ratelimit())
1192 netdev_warn(dev,
1193 "TX Packet without VLAN Tag on DCB Link\n");
1194 txq = 0;
1195 } else {
1196 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +05301197#ifdef CONFIG_CHELSIO_T4_FCOE
1198 if (skb->protocol == htons(ETH_P_FCOE))
1199 txq = skb->priority & 0x7;
1200#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -07001201 }
1202 return txq;
1203 }
1204#endif /* CONFIG_CHELSIO_T4_DCB */
1205
1206 if (select_queue) {
1207 txq = (skb_rx_queue_recorded(skb)
1208 ? skb_get_rx_queue(skb)
1209 : smp_processor_id());
1210
1211 while (unlikely(txq >= dev->real_num_tx_queues))
1212 txq -= dev->real_num_tx_queues;
1213
1214 return txq;
1215 }
1216
1217 return fallback(dev, skb) % dev->real_num_tx_queues;
1218}
1219
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001220static int closest_timer(const struct sge *s, int time)
1221{
1222 int i, delta, match = 0, min_delta = INT_MAX;
1223
1224 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
1225 delta = time - s->timer_val[i];
1226 if (delta < 0)
1227 delta = -delta;
1228 if (delta < min_delta) {
1229 min_delta = delta;
1230 match = i;
1231 }
1232 }
1233 return match;
1234}
1235
1236static int closest_thres(const struct sge *s, int thres)
1237{
1238 int i, delta, match = 0, min_delta = INT_MAX;
1239
1240 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1241 delta = thres - s->counter_val[i];
1242 if (delta < 0)
1243 delta = -delta;
1244 if (delta < min_delta) {
1245 min_delta = delta;
1246 match = i;
1247 }
1248 }
1249 return match;
1250}
1251
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001252/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301253 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001254 * @q: the Rx queue
1255 * @us: the hold-off time in us, or 0 to disable timer
1256 * @cnt: the hold-off packet count, or 0 to disable counter
1257 *
1258 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1259 * one of the two needs to be enabled for the queue to generate interrupts.
1260 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301261int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1262 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001263{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301264 struct adapter *adap = q->adap;
1265
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001266 if ((us | cnt) == 0)
1267 cnt = 1;
1268
1269 if (cnt) {
1270 int err;
1271 u32 v, new_idx;
1272
1273 new_idx = closest_thres(&adap->sge, cnt);
1274 if (q->desc && q->pktcnt_idx != new_idx) {
1275 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301276 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1277 FW_PARAMS_PARAM_X_V(
1278 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1279 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301280 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1281 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001282 if (err)
1283 return err;
1284 }
1285 q->pktcnt_idx = new_idx;
1286 }
1287
1288 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301289 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001290 return 0;
1291}
1292
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001293static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001294{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001295 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001296 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001297 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001298
Patrick McHardyf6469682013-04-19 02:04:27 +00001299 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001300 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001301
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301302 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001303 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001304 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001305 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001306 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001307 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001308}
1309
Bill Pemberton91744942012-12-03 09:23:02 -05001310static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001311{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001312 if (IS_ERR_OR_NULL(adap->debugfs_root))
1313 return -1;
1314
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301315#ifdef CONFIG_DEBUG_FS
1316 t4_setup_debugfs(adap);
1317#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001318 return 0;
1319}
1320
1321/*
1322 * upper-layer driver support
1323 */
1324
1325/*
1326 * Allocate an active-open TID and set it to the supplied value.
1327 */
1328int cxgb4_alloc_atid(struct tid_info *t, void *data)
1329{
1330 int atid = -1;
1331
1332 spin_lock_bh(&t->atid_lock);
1333 if (t->afree) {
1334 union aopen_entry *p = t->afree;
1335
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001336 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001337 t->afree = p->next;
1338 p->data = data;
1339 t->atids_in_use++;
1340 }
1341 spin_unlock_bh(&t->atid_lock);
1342 return atid;
1343}
1344EXPORT_SYMBOL(cxgb4_alloc_atid);
1345
1346/*
1347 * Release an active-open TID.
1348 */
1349void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1350{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001351 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001352
1353 spin_lock_bh(&t->atid_lock);
1354 p->next = t->afree;
1355 t->afree = p;
1356 t->atids_in_use--;
1357 spin_unlock_bh(&t->atid_lock);
1358}
1359EXPORT_SYMBOL(cxgb4_free_atid);
1360
1361/*
1362 * Allocate a server TID and set it to the supplied value.
1363 */
1364int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1365{
1366 int stid;
1367
1368 spin_lock_bh(&t->stid_lock);
1369 if (family == PF_INET) {
1370 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1371 if (stid < t->nstids)
1372 __set_bit(stid, t->stid_bmap);
1373 else
1374 stid = -1;
1375 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301376 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001377 if (stid < 0)
1378 stid = -1;
1379 }
1380 if (stid >= 0) {
1381 t->stid_tab[stid].data = data;
1382 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301383 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1384 * This is equivalent to 4 TIDs. With CLIP enabled it
1385 * needs 2 TIDs.
1386 */
1387 if (family == PF_INET)
1388 t->stids_in_use++;
1389 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301390 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001391 }
1392 spin_unlock_bh(&t->stid_lock);
1393 return stid;
1394}
1395EXPORT_SYMBOL(cxgb4_alloc_stid);
1396
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001397/* Allocate a server filter TID and set it to the supplied value.
1398 */
1399int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1400{
1401 int stid;
1402
1403 spin_lock_bh(&t->stid_lock);
1404 if (family == PF_INET) {
1405 stid = find_next_zero_bit(t->stid_bmap,
1406 t->nstids + t->nsftids, t->nstids);
1407 if (stid < (t->nstids + t->nsftids))
1408 __set_bit(stid, t->stid_bmap);
1409 else
1410 stid = -1;
1411 } else {
1412 stid = -1;
1413 }
1414 if (stid >= 0) {
1415 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301416 stid -= t->nstids;
1417 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301418 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001419 }
1420 spin_unlock_bh(&t->stid_lock);
1421 return stid;
1422}
1423EXPORT_SYMBOL(cxgb4_alloc_sftid);
1424
1425/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001426 */
1427void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1428{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301429 /* Is it a server filter TID? */
1430 if (t->nsftids && (stid >= t->sftid_base)) {
1431 stid -= t->sftid_base;
1432 stid += t->nstids;
1433 } else {
1434 stid -= t->stid_base;
1435 }
1436
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001437 spin_lock_bh(&t->stid_lock);
1438 if (family == PF_INET)
1439 __clear_bit(stid, t->stid_bmap);
1440 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301441 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001442 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301443 if (stid < t->nstids) {
1444 if (family == PF_INET)
1445 t->stids_in_use--;
1446 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301447 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301448 } else {
1449 t->sftids_in_use--;
1450 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001451 spin_unlock_bh(&t->stid_lock);
1452}
1453EXPORT_SYMBOL(cxgb4_free_stid);
1454
1455/*
1456 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1457 */
1458static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1459 unsigned int tid)
1460{
1461 struct cpl_tid_release *req;
1462
1463 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1464 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1465 INIT_TP_WR(req, tid);
1466 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1467}
1468
1469/*
1470 * Queue a TID release request and if necessary schedule a work queue to
1471 * process it.
1472 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001473static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1474 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001475{
1476 void **p = &t->tid_tab[tid];
1477 struct adapter *adap = container_of(t, struct adapter, tids);
1478
1479 spin_lock_bh(&adap->tid_release_lock);
1480 *p = adap->tid_release_head;
1481 /* Low 2 bits encode the Tx channel number */
1482 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1483 if (!adap->tid_release_task_busy) {
1484 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001485 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001486 }
1487 spin_unlock_bh(&adap->tid_release_lock);
1488}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001489
1490/*
1491 * Process the list of pending TID release requests.
1492 */
1493static void process_tid_release_list(struct work_struct *work)
1494{
1495 struct sk_buff *skb;
1496 struct adapter *adap;
1497
1498 adap = container_of(work, struct adapter, tid_release_task);
1499
1500 spin_lock_bh(&adap->tid_release_lock);
1501 while (adap->tid_release_head) {
1502 void **p = adap->tid_release_head;
1503 unsigned int chan = (uintptr_t)p & 3;
1504 p = (void *)p - chan;
1505
1506 adap->tid_release_head = *p;
1507 *p = NULL;
1508 spin_unlock_bh(&adap->tid_release_lock);
1509
1510 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1511 GFP_KERNEL)))
1512 schedule_timeout_uninterruptible(1);
1513
1514 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1515 t4_ofld_send(adap, skb);
1516 spin_lock_bh(&adap->tid_release_lock);
1517 }
1518 adap->tid_release_task_busy = false;
1519 spin_unlock_bh(&adap->tid_release_lock);
1520}
1521
1522/*
1523 * Release a TID and inform HW. If we are unable to allocate the release
1524 * message we defer to a work queue.
1525 */
1526void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1527{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001528 struct sk_buff *skb;
1529 struct adapter *adap = container_of(t, struct adapter, tids);
1530
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301531 WARN_ON(tid >= t->ntids);
1532
1533 if (t->tid_tab[tid]) {
1534 t->tid_tab[tid] = NULL;
1535 if (t->hash_base && (tid >= t->hash_base))
1536 atomic_dec(&t->hash_tids_in_use);
1537 else
1538 atomic_dec(&t->tids_in_use);
1539 }
1540
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001541 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1542 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001543 mk_tid_release(skb, chan, tid);
1544 t4_ofld_send(adap, skb);
1545 } else
1546 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001547}
1548EXPORT_SYMBOL(cxgb4_remove_tid);
1549
1550/*
1551 * Allocate and initialize the TID tables. Returns 0 on success.
1552 */
1553static int tid_init(struct tid_info *t)
1554{
1555 size_t size;
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001556 unsigned int stid_bmap_size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001557 unsigned int natids = t->natids;
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301558 struct adapter *adap = container_of(t, struct adapter, tids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001559
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001560 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001561 size = t->ntids * sizeof(*t->tid_tab) +
1562 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001563 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001564 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001565 stid_bmap_size * sizeof(long) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001566 t->nftids * sizeof(*t->ftid_tab) +
1567 t->nsftids * sizeof(*t->ftid_tab);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001568
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001569 t->tid_tab = t4_alloc_mem(size);
1570 if (!t->tid_tab)
1571 return -ENOMEM;
1572
1573 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1574 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001575 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001576 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001577 spin_lock_init(&t->stid_lock);
1578 spin_lock_init(&t->atid_lock);
1579
1580 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301581 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001582 t->afree = NULL;
1583 t->atids_in_use = 0;
1584 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301585 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001586
1587 /* Setup the free list for atid_tab and clear the stid bitmap. */
1588 if (natids) {
1589 while (--natids)
1590 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1591 t->afree = t->atid_tab;
1592 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001593 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301594 /* Reserve stid 0 for T4/T5 adapters */
1595 if (!t->stid_base &&
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301596 (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5))
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301597 __set_bit(0, t->stid_bmap);
1598
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001599 return 0;
1600}
1601
1602/**
1603 * cxgb4_create_server - create an IP server
1604 * @dev: the device
1605 * @stid: the server TID
1606 * @sip: local IP address to bind server to
1607 * @sport: the server's TCP port
1608 * @queue: queue to direct messages from this server to
1609 *
1610 * Create an IP server for the given port and address.
1611 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1612 */
1613int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001614 __be32 sip, __be16 sport, __be16 vlan,
1615 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001616{
1617 unsigned int chan;
1618 struct sk_buff *skb;
1619 struct adapter *adap;
1620 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301621 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001622
1623 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1624 if (!skb)
1625 return -ENOMEM;
1626
1627 adap = netdev2adap(dev);
1628 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1629 INIT_TP_WR(req, 0);
1630 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1631 req->local_port = sport;
1632 req->peer_port = htons(0);
1633 req->local_ip = sip;
1634 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001635 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001636 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001637 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1638 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301639 ret = t4_mgmt_tx(adap, skb);
1640 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001641}
1642EXPORT_SYMBOL(cxgb4_create_server);
1643
Vipul Pandya80f40c12013-07-04 16:10:45 +05301644/* cxgb4_create_server6 - create an IPv6 server
1645 * @dev: the device
1646 * @stid: the server TID
1647 * @sip: local IPv6 address to bind server to
1648 * @sport: the server's TCP port
1649 * @queue: queue to direct messages from this server to
1650 *
1651 * Create an IPv6 server for the given port and address.
1652 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1653 */
1654int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1655 const struct in6_addr *sip, __be16 sport,
1656 unsigned int queue)
1657{
1658 unsigned int chan;
1659 struct sk_buff *skb;
1660 struct adapter *adap;
1661 struct cpl_pass_open_req6 *req;
1662 int ret;
1663
1664 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1665 if (!skb)
1666 return -ENOMEM;
1667
1668 adap = netdev2adap(dev);
1669 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1670 INIT_TP_WR(req, 0);
1671 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1672 req->local_port = sport;
1673 req->peer_port = htons(0);
1674 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1675 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1676 req->peer_ip_hi = cpu_to_be64(0);
1677 req->peer_ip_lo = cpu_to_be64(0);
1678 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001679 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001680 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1681 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301682 ret = t4_mgmt_tx(adap, skb);
1683 return net_xmit_eval(ret);
1684}
1685EXPORT_SYMBOL(cxgb4_create_server6);
1686
1687int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1688 unsigned int queue, bool ipv6)
1689{
1690 struct sk_buff *skb;
1691 struct adapter *adap;
1692 struct cpl_close_listsvr_req *req;
1693 int ret;
1694
1695 adap = netdev2adap(dev);
1696
1697 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1698 if (!skb)
1699 return -ENOMEM;
1700
1701 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1702 INIT_TP_WR(req, 0);
1703 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001704 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1705 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301706 ret = t4_mgmt_tx(adap, skb);
1707 return net_xmit_eval(ret);
1708}
1709EXPORT_SYMBOL(cxgb4_remove_server);
1710
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001711/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001712 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1713 * @mtus: the HW MTU table
1714 * @mtu: the target MTU
1715 * @idx: index of selected entry in the MTU table
1716 *
1717 * Returns the index and the value in the HW MTU table that is closest to
1718 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1719 * table, in which case that smallest available value is selected.
1720 */
1721unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1722 unsigned int *idx)
1723{
1724 unsigned int i = 0;
1725
1726 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1727 ++i;
1728 if (idx)
1729 *idx = i;
1730 return mtus[i];
1731}
1732EXPORT_SYMBOL(cxgb4_best_mtu);
1733
1734/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301735 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1736 * @mtus: the HW MTU table
1737 * @header_size: Header Size
1738 * @data_size_max: maximum Data Segment Size
1739 * @data_size_align: desired Data Segment Size Alignment (2^N)
1740 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1741 *
1742 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1743 * MTU Table based solely on a Maximum MTU parameter, we break that
1744 * parameter up into a Header Size and Maximum Data Segment Size, and
1745 * provide a desired Data Segment Size Alignment. If we find an MTU in
1746 * the Hardware MTU Table which will result in a Data Segment Size with
1747 * the requested alignment _and_ that MTU isn't "too far" from the
1748 * closest MTU, then we'll return that rather than the closest MTU.
1749 */
1750unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1751 unsigned short header_size,
1752 unsigned short data_size_max,
1753 unsigned short data_size_align,
1754 unsigned int *mtu_idxp)
1755{
1756 unsigned short max_mtu = header_size + data_size_max;
1757 unsigned short data_size_align_mask = data_size_align - 1;
1758 int mtu_idx, aligned_mtu_idx;
1759
1760 /* Scan the MTU Table till we find an MTU which is larger than our
1761 * Maximum MTU or we reach the end of the table. Along the way,
1762 * record the last MTU found, if any, which will result in a Data
1763 * Segment Length matching the requested alignment.
1764 */
1765 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1766 unsigned short data_size = mtus[mtu_idx] - header_size;
1767
1768 /* If this MTU minus the Header Size would result in a
1769 * Data Segment Size of the desired alignment, remember it.
1770 */
1771 if ((data_size & data_size_align_mask) == 0)
1772 aligned_mtu_idx = mtu_idx;
1773
1774 /* If we're not at the end of the Hardware MTU Table and the
1775 * next element is larger than our Maximum MTU, drop out of
1776 * the loop.
1777 */
1778 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1779 break;
1780 }
1781
1782 /* If we fell out of the loop because we ran to the end of the table,
1783 * then we just have to use the last [largest] entry.
1784 */
1785 if (mtu_idx == NMTUS)
1786 mtu_idx--;
1787
1788 /* If we found an MTU which resulted in the requested Data Segment
1789 * Length alignment and that's "not far" from the largest MTU which is
1790 * less than or equal to the maximum MTU, then use that.
1791 */
1792 if (aligned_mtu_idx >= 0 &&
1793 mtu_idx - aligned_mtu_idx <= 1)
1794 mtu_idx = aligned_mtu_idx;
1795
1796 /* If the caller has passed in an MTU Index pointer, pass the
1797 * MTU Index back. Return the MTU value.
1798 */
1799 if (mtu_idxp)
1800 *mtu_idxp = mtu_idx;
1801 return mtus[mtu_idx];
1802}
1803EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1804
1805/**
Hariprasad S27999802015-09-23 17:19:26 +05301806 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1807 * @chip: chip type
1808 * @viid: VI id of the given port
1809 *
1810 * Return the SMT index for this VI.
1811 */
1812unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1813{
1814 /* In T4/T5, SMT contains 256 SMAC entries organized in
1815 * 128 rows of 2 entries each.
1816 * In T6, SMT contains 256 SMAC entries in 256 rows.
1817 * TODO: The below code needs to be updated when we add support
1818 * for 256 VFs.
1819 */
1820 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1821 return ((viid & 0x7f) << 1);
1822 else
1823 return (viid & 0x7f);
1824}
1825EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1826
1827/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001828 * cxgb4_port_chan - get the HW channel of a port
1829 * @dev: the net device for the port
1830 *
1831 * Return the HW Tx channel of the given port.
1832 */
1833unsigned int cxgb4_port_chan(const struct net_device *dev)
1834{
1835 return netdev2pinfo(dev)->tx_chan;
1836}
1837EXPORT_SYMBOL(cxgb4_port_chan);
1838
Vipul Pandya881806b2012-05-18 15:29:24 +05301839unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1840{
1841 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001842 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301843
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301844 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1845 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301846 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301847 lp_count = LP_COUNT_G(v1);
1848 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001849 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301850 lp_count = LP_COUNT_T5_G(v1);
1851 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001852 }
1853 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301854}
1855EXPORT_SYMBOL(cxgb4_dbfifo_count);
1856
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001857/**
1858 * cxgb4_port_viid - get the VI id of a port
1859 * @dev: the net device for the port
1860 *
1861 * Return the VI id of the given port.
1862 */
1863unsigned int cxgb4_port_viid(const struct net_device *dev)
1864{
1865 return netdev2pinfo(dev)->viid;
1866}
1867EXPORT_SYMBOL(cxgb4_port_viid);
1868
1869/**
1870 * cxgb4_port_idx - get the index of a port
1871 * @dev: the net device for the port
1872 *
1873 * Return the index of the given port.
1874 */
1875unsigned int cxgb4_port_idx(const struct net_device *dev)
1876{
1877 return netdev2pinfo(dev)->port_id;
1878}
1879EXPORT_SYMBOL(cxgb4_port_idx);
1880
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001881void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1882 struct tp_tcp_stats *v6)
1883{
1884 struct adapter *adap = pci_get_drvdata(pdev);
1885
1886 spin_lock(&adap->stats_lock);
1887 t4_tp_get_tcp_stats(adap, v4, v6);
1888 spin_unlock(&adap->stats_lock);
1889}
1890EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1891
1892void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1893 const unsigned int *pgsz_order)
1894{
1895 struct adapter *adap = netdev2adap(dev);
1896
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301897 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1898 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1899 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1900 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001901}
1902EXPORT_SYMBOL(cxgb4_iscsi_init);
1903
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301904int cxgb4_flush_eq_cache(struct net_device *dev)
1905{
1906 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301907
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301908 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301909}
1910EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1911
1912static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1913{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301914 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301915 __be64 indices;
1916 int ret;
1917
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301918 spin_lock(&adap->win0_lock);
1919 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1920 sizeof(indices), (__be32 *)&indices,
1921 T4_MEMORY_READ);
1922 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301923 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001924 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1925 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301926 }
1927 return ret;
1928}
1929
1930int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1931 u16 size)
1932{
1933 struct adapter *adap = netdev2adap(dev);
1934 u16 hw_pidx, hw_cidx;
1935 int ret;
1936
1937 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1938 if (ret)
1939 goto out;
1940
1941 if (pidx != hw_pidx) {
1942 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301943 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301944
1945 if (pidx >= hw_pidx)
1946 delta = pidx - hw_pidx;
1947 else
1948 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301949
1950 if (is_t4(adap->params.chip))
1951 val = PIDX_V(delta);
1952 else
1953 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301954 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301955 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1956 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301957 }
1958out:
1959 return ret;
1960}
1961EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1962
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301963int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1964{
1965 struct adapter *adap;
1966 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301967 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301968 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1969 int ret;
1970
1971 adap = netdev2adap(dev);
1972
1973 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1974
1975 /* Figure out where the offset lands in the Memory Type/Address scheme.
1976 * This code assumes that the memory is laid out starting at offset 0
1977 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1978 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1979 * MC0, and some have both MC0 and MC1.
1980 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301981 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1982 edc0_size = EDRAM0_SIZE_G(size) << 20;
1983 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1984 edc1_size = EDRAM1_SIZE_G(size) << 20;
1985 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1986 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301987
1988 edc0_end = edc0_size;
1989 edc1_end = edc0_end + edc1_size;
1990 mc0_end = edc1_end + mc0_size;
1991
1992 if (offset < edc0_end) {
1993 memtype = MEM_EDC0;
1994 memaddr = offset;
1995 } else if (offset < edc1_end) {
1996 memtype = MEM_EDC1;
1997 memaddr = offset - edc0_end;
1998 } else {
1999 if (offset < mc0_end) {
2000 memtype = MEM_MC0;
2001 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302002 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05302003 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
2004 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302005 mc1_end = mc0_end + mc1_size;
2006 if (offset < mc1_end) {
2007 memtype = MEM_MC1;
2008 memaddr = offset - mc0_end;
2009 } else {
2010 /* offset beyond the end of any memory */
2011 goto err;
2012 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302013 } else {
2014 /* T4/T6 only has a single memory channel */
2015 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05302016 }
2017 }
2018
2019 spin_lock(&adap->win0_lock);
2020 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
2021 spin_unlock(&adap->win0_lock);
2022 return ret;
2023
2024err:
2025 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
2026 stag, offset);
2027 return -EINVAL;
2028}
2029EXPORT_SYMBOL(cxgb4_read_tpte);
2030
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302031u64 cxgb4_read_sge_timestamp(struct net_device *dev)
2032{
2033 u32 hi, lo;
2034 struct adapter *adap;
2035
2036 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302037 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
2038 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05302039
2040 return ((u64)hi << 32) | (u64)lo;
2041}
2042EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
2043
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302044int cxgb4_bar2_sge_qregs(struct net_device *dev,
2045 unsigned int qid,
2046 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05302047 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302048 u64 *pbar2_qoffset,
2049 unsigned int *pbar2_qid)
2050{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302051 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302052 qid,
2053 (qtype == CXGB4_BAR2_QTYPE_EGRESS
2054 ? T4_BAR2_QTYPE_EGRESS
2055 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05302056 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302057 pbar2_qoffset,
2058 pbar2_qid);
2059}
2060EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
2061
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002062static struct pci_driver cxgb4_driver;
2063
2064static void check_neigh_update(struct neighbour *neigh)
2065{
2066 const struct device *parent;
2067 const struct net_device *netdev = neigh->dev;
2068
2069 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2070 netdev = vlan_dev_real_dev(netdev);
2071 parent = netdev->dev.parent;
2072 if (parent && parent->driver == &cxgb4_driver.driver)
2073 t4_l2t_update(dev_get_drvdata(parent), neigh);
2074}
2075
2076static int netevent_cb(struct notifier_block *nb, unsigned long event,
2077 void *data)
2078{
2079 switch (event) {
2080 case NETEVENT_NEIGH_UPDATE:
2081 check_neigh_update(data);
2082 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002083 case NETEVENT_REDIRECT:
2084 default:
2085 break;
2086 }
2087 return 0;
2088}
2089
2090static bool netevent_registered;
2091static struct notifier_block cxgb4_netevent_nb = {
2092 .notifier_call = netevent_cb
2093};
2094
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302095static void drain_db_fifo(struct adapter *adap, int usecs)
2096{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002097 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302098
2099 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302100 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
2101 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302102 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302103 lp_count = LP_COUNT_G(v1);
2104 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002105 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302106 lp_count = LP_COUNT_T5_G(v1);
2107 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002108 }
2109
2110 if (lp_count == 0 && hp_count == 0)
2111 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302112 set_current_state(TASK_UNINTERRUPTIBLE);
2113 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302114 } while (1);
2115}
2116
2117static void disable_txq_db(struct sge_txq *q)
2118{
Steve Wise05eb2382014-03-14 21:52:08 +05302119 unsigned long flags;
2120
2121 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302122 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05302123 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302124}
2125
Steve Wise05eb2382014-03-14 21:52:08 +05302126static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302127{
2128 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05302129 if (q->db_pidx_inc) {
2130 /* Make sure that all writes to the TX descriptors
2131 * are committed before we tell HW about them.
2132 */
2133 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302134 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2135 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05302136 q->db_pidx_inc = 0;
2137 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302138 q->db_disabled = 0;
2139 spin_unlock_irq(&q->db_lock);
2140}
2141
2142static void disable_dbs(struct adapter *adap)
2143{
2144 int i;
2145
2146 for_each_ethrxq(&adap->sge, i)
2147 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302148 for_each_ofldtxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302149 disable_txq_db(&adap->sge.ofldtxq[i].q);
2150 for_each_port(adap, i)
2151 disable_txq_db(&adap->sge.ctrlq[i].q);
2152}
2153
2154static void enable_dbs(struct adapter *adap)
2155{
2156 int i;
2157
2158 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302159 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302160 for_each_ofldtxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302161 enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302162 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05302163 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
2164}
2165
2166static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
2167{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302168 enum cxgb4_uld type = CXGB4_ULD_RDMA;
2169
2170 if (adap->uld && adap->uld[type].handle)
2171 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05302172}
2173
2174static void process_db_full(struct work_struct *work)
2175{
2176 struct adapter *adap;
2177
2178 adap = container_of(work, struct adapter, db_full_task);
2179
2180 drain_db_fifo(adap, dbfifo_drain_delay);
2181 enable_dbs(adap);
2182 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302183 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2184 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2185 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
2186 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
2187 else
2188 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2189 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302190}
2191
2192static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
2193{
2194 u16 hw_pidx, hw_cidx;
2195 int ret;
2196
Steve Wise05eb2382014-03-14 21:52:08 +05302197 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302198 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2199 if (ret)
2200 goto out;
2201 if (q->db_pidx != hw_pidx) {
2202 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302203 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302204
2205 if (q->db_pidx >= hw_pidx)
2206 delta = q->db_pidx - hw_pidx;
2207 else
2208 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302209
2210 if (is_t4(adap->params.chip))
2211 val = PIDX_V(delta);
2212 else
2213 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302214 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302215 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2216 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302217 }
2218out:
2219 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302220 q->db_pidx_inc = 0;
2221 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302222 if (ret)
2223 CH_WARN(adap, "DB drop recovery failed.\n");
2224}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302225
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302226static void recover_all_queues(struct adapter *adap)
2227{
2228 int i;
2229
2230 for_each_ethrxq(&adap->sge, i)
2231 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302232 for_each_ofldtxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302233 sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
2234 for_each_port(adap, i)
2235 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2236}
2237
Vipul Pandya881806b2012-05-18 15:29:24 +05302238static void process_db_drop(struct work_struct *work)
2239{
2240 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302241
Vipul Pandya881806b2012-05-18 15:29:24 +05302242 adap = container_of(work, struct adapter, db_drop_task);
2243
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302244 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302245 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002246 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302247 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002248 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302249 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002250 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302251 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302252 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002253 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2254 u16 qid = (dropped_db >> 15) & 0x1ffff;
2255 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302256 u64 bar2_qoffset;
2257 unsigned int bar2_qid;
2258 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002259
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302260 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002261 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302262 if (ret)
2263 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2264 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2265 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302266 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302267 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002268
2269 /* Re-enable BAR2 WC */
2270 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2271 }
2272
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302273 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2274 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302275}
2276
2277void t4_db_full(struct adapter *adap)
2278{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302279 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302280 disable_dbs(adap);
2281 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302282 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2283 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002284 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002285 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302286}
2287
2288void t4_db_dropped(struct adapter *adap)
2289{
Steve Wise05eb2382014-03-14 21:52:08 +05302290 if (is_t4(adap->params.chip)) {
2291 disable_dbs(adap);
2292 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2293 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002294 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302295}
2296
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302297void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002298{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002299 if (!netevent_registered) {
2300 register_netevent_notifier(&cxgb4_netevent_nb);
2301 netevent_registered = true;
2302 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002303}
2304
2305static void detach_ulds(struct adapter *adap)
2306{
2307 unsigned int i;
2308
2309 mutex_lock(&uld_mutex);
2310 list_del(&adap->list_node);
2311 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302312 if (adap->uld && adap->uld[i].handle) {
2313 adap->uld[i].state_change(adap->uld[i].handle,
2314 CXGB4_STATE_DETACH);
2315 adap->uld[i].handle = NULL;
2316 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002317 if (netevent_registered && list_empty(&adapter_list)) {
2318 unregister_netevent_notifier(&cxgb4_netevent_nb);
2319 netevent_registered = false;
2320 }
2321 mutex_unlock(&uld_mutex);
2322}
2323
2324static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2325{
2326 unsigned int i;
2327
2328 mutex_lock(&uld_mutex);
2329 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302330 if (adap->uld && adap->uld[i].handle)
2331 adap->uld[i].state_change(adap->uld[i].handle,
2332 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002333 mutex_unlock(&uld_mutex);
2334}
2335
Anish Bhatt1bb60372014-10-14 20:07:22 -07002336#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002337static int cxgb4_inet6addr_handler(struct notifier_block *this,
2338 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302339{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002340 struct inet6_ifaddr *ifa = data;
2341 struct net_device *event_dev = ifa->idev->dev;
2342 const struct device *parent = NULL;
2343#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302344 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002345#endif
2346 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2347 event_dev = vlan_dev_real_dev(event_dev);
2348#if IS_ENABLED(CONFIG_BONDING)
2349 if (event_dev->flags & IFF_MASTER) {
2350 list_for_each_entry(adap, &adapter_list, list_node) {
2351 switch (event) {
2352 case NETDEV_UP:
2353 cxgb4_clip_get(adap->port[0],
2354 (const u32 *)ifa, 1);
2355 break;
2356 case NETDEV_DOWN:
2357 cxgb4_clip_release(adap->port[0],
2358 (const u32 *)ifa, 1);
2359 break;
2360 default:
2361 break;
2362 }
2363 }
2364 return NOTIFY_OK;
2365 }
2366#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302367
Anish Bhattb5a02f52015-01-14 15:17:34 -08002368 if (event_dev)
2369 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302370
Anish Bhattb5a02f52015-01-14 15:17:34 -08002371 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302372 switch (event) {
2373 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002374 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302375 break;
2376 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002377 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302378 break;
2379 default:
2380 break;
2381 }
2382 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002383 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302384}
2385
Anish Bhattb5a02f52015-01-14 15:17:34 -08002386static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302387static struct notifier_block cxgb4_inet6addr_notifier = {
2388 .notifier_call = cxgb4_inet6addr_handler
2389};
2390
Vipul Pandya01bcca62013-07-04 16:10:46 +05302391static void update_clip(const struct adapter *adap)
2392{
2393 int i;
2394 struct net_device *dev;
2395 int ret;
2396
2397 rcu_read_lock();
2398
2399 for (i = 0; i < MAX_NPORTS; i++) {
2400 dev = adap->port[i];
2401 ret = 0;
2402
2403 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002404 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302405
2406 if (ret < 0)
2407 break;
2408 }
2409 rcu_read_unlock();
2410}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002411#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302412
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002413/**
2414 * cxgb_up - enable the adapter
2415 * @adap: adapter being enabled
2416 *
2417 * Called when the first port is enabled, this function performs the
2418 * actions necessary to make an adapter operational, such as completing
2419 * the initialization of HW modules, and enabling interrupts.
2420 *
2421 * Must be called with the rtnl lock held.
2422 */
2423static int cxgb_up(struct adapter *adap)
2424{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002425 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002426
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002427 err = setup_sge_queues(adap);
2428 if (err)
2429 goto out;
2430 err = setup_rss(adap);
2431 if (err)
2432 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002433
2434 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002435 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002436 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2437 adap->msix_info[0].desc, adap);
2438 if (err)
2439 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002440 err = request_msix_queue_irqs(adap);
2441 if (err) {
2442 free_irq(adap->msix_info[0].vec, adap);
2443 goto irq_err;
2444 }
2445 } else {
2446 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2447 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002448 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002449 if (err)
2450 goto irq_err;
2451 }
2452 enable_rx(adap);
2453 t4_sge_start(adap);
2454 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002455 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002456 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002457#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302458 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002459#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302460 /* Initialize hash mac addr list*/
2461 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002462 out:
2463 return err;
2464 irq_err:
2465 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002466 freeq:
2467 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002468 goto out;
2469}
2470
2471static void cxgb_down(struct adapter *adapter)
2472{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002473 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302474 cancel_work_sync(&adapter->db_full_task);
2475 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002476 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002477 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002478
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002479 t4_sge_stop(adapter);
2480 t4_free_sge_resources(adapter);
2481 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002482}
2483
2484/*
2485 * net_device operations
2486 */
2487static int cxgb_open(struct net_device *dev)
2488{
2489 int err;
2490 struct port_info *pi = netdev_priv(dev);
2491 struct adapter *adapter = pi->adapter;
2492
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002493 netif_carrier_off(dev);
2494
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002495 if (!(adapter->flags & FULL_INIT_DONE)) {
2496 err = cxgb_up(adapter);
2497 if (err < 0)
2498 return err;
2499 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002500
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002501 err = link_start(dev);
2502 if (!err)
2503 netif_tx_start_all_queues(dev);
2504 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002505}
2506
2507static int cxgb_close(struct net_device *dev)
2508{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002509 struct port_info *pi = netdev_priv(dev);
2510 struct adapter *adapter = pi->adapter;
2511
2512 netif_tx_stop_all_queues(dev);
2513 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302514 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002515}
2516
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002517/* Return an error number if the indicated filter isn't writable ...
2518 */
2519static int writable_filter(struct filter_entry *f)
2520{
2521 if (f->locked)
2522 return -EPERM;
2523 if (f->pending)
2524 return -EBUSY;
2525
2526 return 0;
2527}
2528
2529/* Delete the filter at the specified index (if valid). The checks for all
2530 * the common problems with doing this like the filter being locked, currently
2531 * pending in another operation, etc.
2532 */
2533static int delete_filter(struct adapter *adapter, unsigned int fidx)
2534{
2535 struct filter_entry *f;
2536 int ret;
2537
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002538 if (fidx >= adapter->tids.nftids + adapter->tids.nsftids)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00002539 return -EINVAL;
2540
2541 f = &adapter->tids.ftid_tab[fidx];
2542 ret = writable_filter(f);
2543 if (ret)
2544 return ret;
2545 if (f->valid)
2546 return del_filter_wr(adapter, fidx);
2547
2548 return 0;
2549}
2550
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002551int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002552 __be32 sip, __be16 sport, __be16 vlan,
2553 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002554{
2555 int ret;
2556 struct filter_entry *f;
2557 struct adapter *adap;
2558 int i;
2559 u8 *val;
2560
2561 adap = netdev2adap(dev);
2562
Vipul Pandya1cab7752012-12-10 09:30:55 +00002563 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302564 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002565 stid += adap->tids.nftids;
2566
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002567 /* Check to make sure the filter requested is writable ...
2568 */
2569 f = &adap->tids.ftid_tab[stid];
2570 ret = writable_filter(f);
2571 if (ret)
2572 return ret;
2573
2574 /* Clear out any old resources being used by the filter before
2575 * we start constructing the new filter.
2576 */
2577 if (f->valid)
2578 clear_filter(adap, f);
2579
2580 /* Clear out filter specifications */
2581 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2582 f->fs.val.lport = cpu_to_be16(sport);
2583 f->fs.mask.lport = ~0;
2584 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002585 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002586 for (i = 0; i < 4; i++) {
2587 f->fs.val.lip[i] = val[i];
2588 f->fs.mask.lip[i] = ~0;
2589 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302590 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002591 f->fs.val.iport = port;
2592 f->fs.mask.iport = mask;
2593 }
2594 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002595
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302596 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302597 f->fs.val.proto = IPPROTO_TCP;
2598 f->fs.mask.proto = ~0;
2599 }
2600
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002601 f->fs.dirsteer = 1;
2602 f->fs.iq = queue;
2603 /* Mark filter as locked */
2604 f->locked = 1;
2605 f->fs.rpttid = 1;
2606
2607 ret = set_filter_wr(adap, stid);
2608 if (ret) {
2609 clear_filter(adap, f);
2610 return ret;
2611 }
2612
2613 return 0;
2614}
2615EXPORT_SYMBOL(cxgb4_create_server_filter);
2616
2617int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2618 unsigned int queue, bool ipv6)
2619{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002620 struct filter_entry *f;
2621 struct adapter *adap;
2622
2623 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002624
2625 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302626 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002627 stid += adap->tids.nftids;
2628
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002629 f = &adap->tids.ftid_tab[stid];
2630 /* Unlock the filter */
2631 f->locked = 0;
2632
Wei Yongjun8c148462016-08-20 15:32:41 +00002633 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002634}
2635EXPORT_SYMBOL(cxgb4_remove_server_filter);
2636
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002637static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2638 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002639{
2640 struct port_stats stats;
2641 struct port_info *p = netdev_priv(dev);
2642 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002643
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002644 /* Block retrieving statistics during EEH error
2645 * recovery. Otherwise, the recovery might fail
2646 * and the PCI device will be removed permanently
2647 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002648 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002649 if (!netif_device_present(dev)) {
2650 spin_unlock(&adapter->stats_lock);
2651 return ns;
2652 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302653 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2654 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002655 spin_unlock(&adapter->stats_lock);
2656
2657 ns->tx_bytes = stats.tx_octets;
2658 ns->tx_packets = stats.tx_frames;
2659 ns->rx_bytes = stats.rx_octets;
2660 ns->rx_packets = stats.rx_frames;
2661 ns->multicast = stats.rx_mcast_frames;
2662
2663 /* detailed rx_errors */
2664 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2665 stats.rx_runt;
2666 ns->rx_over_errors = 0;
2667 ns->rx_crc_errors = stats.rx_fcs_err;
2668 ns->rx_frame_errors = stats.rx_symbol_err;
2669 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2670 stats.rx_ovflow2 + stats.rx_ovflow3 +
2671 stats.rx_trunc0 + stats.rx_trunc1 +
2672 stats.rx_trunc2 + stats.rx_trunc3;
2673 ns->rx_missed_errors = 0;
2674
2675 /* detailed tx_errors */
2676 ns->tx_aborted_errors = 0;
2677 ns->tx_carrier_errors = 0;
2678 ns->tx_fifo_errors = 0;
2679 ns->tx_heartbeat_errors = 0;
2680 ns->tx_window_errors = 0;
2681
2682 ns->tx_errors = stats.tx_error_frames;
2683 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2684 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2685 return ns;
2686}
2687
2688static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2689{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002690 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002691 int ret = 0, prtad, devad;
2692 struct port_info *pi = netdev_priv(dev);
2693 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2694
2695 switch (cmd) {
2696 case SIOCGMIIPHY:
2697 if (pi->mdio_addr < 0)
2698 return -EOPNOTSUPP;
2699 data->phy_id = pi->mdio_addr;
2700 break;
2701 case SIOCGMIIREG:
2702 case SIOCSMIIREG:
2703 if (mdio_phy_id_is_c45(data->phy_id)) {
2704 prtad = mdio_phy_id_prtad(data->phy_id);
2705 devad = mdio_phy_id_devad(data->phy_id);
2706 } else if (data->phy_id < 32) {
2707 prtad = data->phy_id;
2708 devad = 0;
2709 data->reg_num &= 0x1f;
2710 } else
2711 return -EINVAL;
2712
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302713 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002714 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002715 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002716 data->reg_num, &data->val_out);
2717 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002718 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002719 data->reg_num, data->val_in);
2720 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302721 case SIOCGHWTSTAMP:
2722 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2723 sizeof(pi->tstamp_config)) ?
2724 -EFAULT : 0;
2725 case SIOCSHWTSTAMP:
2726 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2727 sizeof(pi->tstamp_config)))
2728 return -EFAULT;
2729
2730 switch (pi->tstamp_config.rx_filter) {
2731 case HWTSTAMP_FILTER_NONE:
2732 pi->rxtstamp = false;
2733 break;
2734 case HWTSTAMP_FILTER_ALL:
2735 pi->rxtstamp = true;
2736 break;
2737 default:
2738 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2739 return -ERANGE;
2740 }
2741
2742 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2743 sizeof(pi->tstamp_config)) ?
2744 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002745 default:
2746 return -EOPNOTSUPP;
2747 }
2748 return ret;
2749}
2750
2751static void cxgb_set_rxmode(struct net_device *dev)
2752{
2753 /* unfortunately we can't return errors to the stack */
2754 set_rxmode(dev, -1, false);
2755}
2756
2757static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2758{
2759 int ret;
2760 struct port_info *pi = netdev_priv(dev);
2761
2762 if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
2763 return -EINVAL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302764 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002765 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002766 if (!ret)
2767 dev->mtu = new_mtu;
2768 return ret;
2769}
2770
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302771#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302772static int dummy_open(struct net_device *dev)
2773{
2774 /* Turn carrier off since we don't have to transmit anything on this
2775 * interface.
2776 */
2777 netif_carrier_off(dev);
2778 return 0;
2779}
2780
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302781/* Fill MAC address that will be assigned by the FW */
2782static void fill_vf_station_mac_addr(struct adapter *adap)
2783{
2784 unsigned int i;
2785 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2786 int err;
2787 u8 *na;
2788 u16 a, b;
2789
2790 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2791 if (!err) {
2792 na = adap->params.vpd.na;
2793 for (i = 0; i < ETH_ALEN; i++)
2794 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2795 hex2val(na[2 * i + 1]));
2796 a = (hw_addr[0] << 8) | hw_addr[1];
2797 b = (hw_addr[1] << 8) | hw_addr[2];
2798 a ^= b;
2799 a |= 0x0200; /* locally assigned Ethernet MAC address */
2800 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2801 macaddr[0] = a >> 8;
2802 macaddr[1] = a & 0xff;
2803
2804 for (i = 2; i < 5; i++)
2805 macaddr[i] = hw_addr[i + 1];
2806
2807 for (i = 0; i < adap->num_vfs; i++) {
2808 macaddr[5] = adap->pf * 16 + i;
2809 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2810 }
2811 }
2812}
2813
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302814static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2815{
2816 struct port_info *pi = netdev_priv(dev);
2817 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302818 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302819
2820 /* verify MAC addr is valid */
2821 if (!is_valid_ether_addr(mac)) {
2822 dev_err(pi->adapter->pdev_dev,
2823 "Invalid Ethernet address %pM for VF %d\n",
2824 mac, vf);
2825 return -EINVAL;
2826 }
2827
2828 dev_info(pi->adapter->pdev_dev,
2829 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302830 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2831 if (!ret)
2832 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2833 return ret;
2834}
2835
2836static int cxgb_get_vf_config(struct net_device *dev,
2837 int vf, struct ifla_vf_info *ivi)
2838{
2839 struct port_info *pi = netdev_priv(dev);
2840 struct adapter *adap = pi->adapter;
2841
2842 if (vf >= adap->num_vfs)
2843 return -EINVAL;
2844 ivi->vf = vf;
2845 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2846 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302847}
2848#endif
2849
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002850static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2851{
2852 int ret;
2853 struct sockaddr *addr = p;
2854 struct port_info *pi = netdev_priv(dev);
2855
2856 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002857 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002858
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302859 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002860 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002861 if (ret < 0)
2862 return ret;
2863
2864 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2865 pi->xact_addr_filt = ret;
2866 return 0;
2867}
2868
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002869#ifdef CONFIG_NET_POLL_CONTROLLER
2870static void cxgb_netpoll(struct net_device *dev)
2871{
2872 struct port_info *pi = netdev_priv(dev);
2873 struct adapter *adap = pi->adapter;
2874
2875 if (adap->flags & USING_MSIX) {
2876 int i;
2877 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2878
2879 for (i = pi->nqsets; i; i--, rx++)
2880 t4_sge_intr_msix(0, &rx->rspq);
2881 } else
2882 t4_intr_handler(adap)(0, adap);
2883}
2884#endif
2885
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302886static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2887{
2888 struct port_info *pi = netdev_priv(dev);
2889 struct adapter *adap = pi->adapter;
2890 struct sched_class *e;
2891 struct ch_sched_params p;
2892 struct ch_sched_queue qe;
2893 u32 req_rate;
2894 int err = 0;
2895
2896 if (!can_sched(dev))
2897 return -ENOTSUPP;
2898
2899 if (index < 0 || index > pi->nqsets - 1)
2900 return -EINVAL;
2901
2902 if (!(adap->flags & FULL_INIT_DONE)) {
2903 dev_err(adap->pdev_dev,
2904 "Failed to rate limit on queue %d. Link Down?\n",
2905 index);
2906 return -EINVAL;
2907 }
2908
2909 /* Convert from Mbps to Kbps */
2910 req_rate = rate << 10;
2911
2912 /* Max rate is 10 Gbps */
2913 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2914 dev_err(adap->pdev_dev,
2915 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2916 rate, SCHED_MAX_RATE_KBPS);
2917 return -ERANGE;
2918 }
2919
2920 /* First unbind the queue from any existing class */
2921 memset(&qe, 0, sizeof(qe));
2922 qe.queue = index;
2923 qe.class = SCHED_CLS_NONE;
2924
2925 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2926 if (err) {
2927 dev_err(adap->pdev_dev,
2928 "Unbinding Queue %d on port %d fail. Err: %d\n",
2929 index, pi->port_id, err);
2930 return err;
2931 }
2932
2933 /* Queue already unbound */
2934 if (!req_rate)
2935 return 0;
2936
2937 /* Fetch any available unused or matching scheduling class */
2938 memset(&p, 0, sizeof(p));
2939 p.type = SCHED_CLASS_TYPE_PACKET;
2940 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2941 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2942 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2943 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2944 p.u.params.channel = pi->tx_chan;
2945 p.u.params.class = SCHED_CLS_NONE;
2946 p.u.params.minrate = 0;
2947 p.u.params.maxrate = req_rate;
2948 p.u.params.weight = 0;
2949 p.u.params.pktsize = dev->mtu;
2950
2951 e = cxgb4_sched_class_alloc(dev, &p);
2952 if (!e)
2953 return -ENOMEM;
2954
2955 /* Bind the queue to a scheduling class */
2956 memset(&qe, 0, sizeof(qe));
2957 qe.queue = index;
2958 qe.class = e->idx;
2959
2960 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2961 if (err)
2962 dev_err(adap->pdev_dev,
2963 "Queue rate limiting failed. Err: %d\n", err);
2964 return err;
2965}
2966
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002967static const struct net_device_ops cxgb4_netdev_ops = {
2968 .ndo_open = cxgb_open,
2969 .ndo_stop = cxgb_close,
2970 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002971 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002972 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002973 .ndo_set_rx_mode = cxgb_set_rxmode,
2974 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00002975 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002976 .ndo_validate_addr = eth_validate_addr,
2977 .ndo_do_ioctl = cxgb_ioctl,
2978 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002979#ifdef CONFIG_NET_POLL_CONTROLLER
2980 .ndo_poll_controller = cxgb_netpoll,
2981#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05302982#ifdef CONFIG_CHELSIO_T4_FCOE
2983 .ndo_fcoe_enable = cxgb_fcoe_enable,
2984 .ndo_fcoe_disable = cxgb_fcoe_disable,
2985#endif /* CONFIG_CHELSIO_T4_FCOE */
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302986#ifdef CONFIG_NET_RX_BUSY_POLL
2987 .ndo_busy_poll = cxgb_busy_poll,
2988#endif
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302989 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002990};
2991
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302992#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302993static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2994 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302995 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302996 .ndo_get_vf_config = cxgb_get_vf_config,
Hariprasad Shenai78294512016-08-11 21:06:23 +05302997};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302998#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05302999
3000static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3001{
3002 struct adapter *adapter = netdev2adap(dev);
3003
3004 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3005 strlcpy(info->version, cxgb4_driver_version,
3006 sizeof(info->version));
3007 strlcpy(info->bus_info, pci_name(adapter->pdev),
3008 sizeof(info->bus_info));
3009}
3010
3011static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3012 .get_drvinfo = get_drvinfo,
3013};
3014
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003015void t4_fatal_err(struct adapter *adap)
3016{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303017 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003018 t4_intr_disable(adap);
3019 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3020}
3021
3022static void setup_memwin(struct adapter *adap)
3023{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303024 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003025
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303026 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003027}
3028
3029static void setup_memwin_rdma(struct adapter *adap)
3030{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003031 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303032 u32 start;
3033 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003034
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303035 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3036 start &= PCI_BASE_ADDRESS_MEM_MASK;
3037 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003038 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3039 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303040 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3041 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003042 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303043 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003044 adap->vres.ocq.start);
3045 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303046 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003047 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003048}
3049
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003050static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3051{
3052 u32 v;
3053 int ret;
3054
3055 /* get device capabilities */
3056 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303057 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3058 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303059 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303060 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003061 if (ret < 0)
3062 return ret;
3063
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303064 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3065 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303066 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003067 if (ret < 0)
3068 return ret;
3069
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303070 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003071 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303072 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3073 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003074 if (ret < 0)
3075 return ret;
3076
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303077 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303078 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3079 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003080 if (ret < 0)
3081 return ret;
3082
3083 t4_sge_init(adap);
3084
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003085 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303086 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303087 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303088 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3089 v = t4_read_reg(adap, TP_PIO_DATA_A);
3090 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003091
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003092 /* first 4 Tx modulation queues point to consecutive Tx channels */
3093 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303094 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3095 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003096
3097 /* associate each Tx modulation queue with consecutive Tx channels */
3098 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303099 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303100 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303101 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303102 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303103 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303104 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003105
3106#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3107 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303108 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3109 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3110 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3111 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3112 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3113 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3114 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3115 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3116 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3117 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003118 }
3119
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003120 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303121 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003122}
3123
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003124/*
3125 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3126 */
3127#define MAX_ATIDS 8192U
3128
3129/*
3130 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003131 *
3132 * If the firmware we're dealing with has Configuration File support, then
3133 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003134 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003135
3136/*
3137 * Tweak configuration based on module parameters, etc. Most of these have
3138 * defaults assigned to them by Firmware Configuration Files (if we're using
3139 * them) but need to be explicitly set if we're using hard-coded
3140 * initialization. But even in the case of using Firmware Configuration
3141 * Files, we'd like to expose the ability to change these via module
3142 * parameters so these are essentially common tweaks/settings for
3143 * Configuration Files and hard-coded initialization ...
3144 */
3145static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003146{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003147 /*
3148 * Fix up various Host-Dependent Parameters like Page Size, Cache
3149 * Line Size, etc. The firmware default is for a 4KB Page Size and
3150 * 64B Cache Line Size ...
3151 */
3152 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003153
Vipul Pandya636f9d32012-09-26 02:39:39 +00003154 /*
3155 * Process module parameters which affect early initialization.
3156 */
3157 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3158 dev_err(&adapter->pdev->dev,
3159 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3160 rx_dma_offset);
3161 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003162 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303163 t4_set_reg_field(adapter, SGE_CONTROL_A,
3164 PKTSHIFT_V(PKTSHIFT_M),
3165 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003166
Vipul Pandya636f9d32012-09-26 02:39:39 +00003167 /*
3168 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3169 * adds the pseudo header itself.
3170 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303171 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3172 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003173
3174 return 0;
3175}
3176
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303177/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3178 * unto themselves and they contain their own firmware to perform their
3179 * tasks ...
3180 */
3181static int phy_aq1202_version(const u8 *phy_fw_data,
3182 size_t phy_fw_size)
3183{
3184 int offset;
3185
3186 /* At offset 0x8 you're looking for the primary image's
3187 * starting offset which is 3 Bytes wide
3188 *
3189 * At offset 0xa of the primary image, you look for the offset
3190 * of the DRAM segment which is 3 Bytes wide.
3191 *
3192 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3193 * wide
3194 */
3195 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3196 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3197 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3198
3199 offset = le24(phy_fw_data + 0x8) << 12;
3200 offset = le24(phy_fw_data + offset + 0xa);
3201 return be16(phy_fw_data + offset + 0x27e);
3202
3203 #undef be16
3204 #undef le16
3205 #undef le24
3206}
3207
3208static struct info_10gbt_phy_fw {
3209 unsigned int phy_fw_id; /* PCI Device ID */
3210 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3211 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3212 int phy_flash; /* Has FLASH for PHY Firmware */
3213} phy_info_array[] = {
3214 {
3215 PHY_AQ1202_DEVICEID,
3216 PHY_AQ1202_FIRMWARE,
3217 phy_aq1202_version,
3218 1,
3219 },
3220 {
3221 PHY_BCM84834_DEVICEID,
3222 PHY_BCM84834_FIRMWARE,
3223 NULL,
3224 0,
3225 },
3226 { 0, NULL, NULL },
3227};
3228
3229static struct info_10gbt_phy_fw *find_phy_info(int devid)
3230{
3231 int i;
3232
3233 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3234 if (phy_info_array[i].phy_fw_id == devid)
3235 return &phy_info_array[i];
3236 }
3237 return NULL;
3238}
3239
3240/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3241 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3242 * we return a negative error number. If we transfer new firmware we return 1
3243 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3244 */
3245static int adap_init0_phy(struct adapter *adap)
3246{
3247 const struct firmware *phyf;
3248 int ret;
3249 struct info_10gbt_phy_fw *phy_info;
3250
3251 /* Use the device ID to determine which PHY file to flash.
3252 */
3253 phy_info = find_phy_info(adap->pdev->device);
3254 if (!phy_info) {
3255 dev_warn(adap->pdev_dev,
3256 "No PHY Firmware file found for this PHY\n");
3257 return -EOPNOTSUPP;
3258 }
3259
3260 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3261 * use that. The adapter firmware provides us with a memory buffer
3262 * where we can load a PHY firmware file from the host if we want to
3263 * override the PHY firmware File in flash.
3264 */
3265 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3266 adap->pdev_dev);
3267 if (ret < 0) {
3268 /* For adapters without FLASH attached to PHY for their
3269 * firmware, it's obviously a fatal error if we can't get the
3270 * firmware to the adapter. For adapters with PHY firmware
3271 * FLASH storage, it's worth a warning if we can't find the
3272 * PHY Firmware but we'll neuter the error ...
3273 */
3274 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3275 "/lib/firmware/%s, error %d\n",
3276 phy_info->phy_fw_file, -ret);
3277 if (phy_info->phy_flash) {
3278 int cur_phy_fw_ver = 0;
3279
3280 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3281 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3282 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3283 ret = 0;
3284 }
3285
3286 return ret;
3287 }
3288
3289 /* Load PHY Firmware onto adapter.
3290 */
3291 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3292 phy_info->phy_fw_version,
3293 (u8 *)phyf->data, phyf->size);
3294 if (ret < 0)
3295 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3296 -ret);
3297 else if (ret > 0) {
3298 int new_phy_fw_ver = 0;
3299
3300 if (phy_info->phy_fw_version)
3301 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3302 phyf->size);
3303 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3304 "Firmware /lib/firmware/%s, version %#x\n",
3305 phy_info->phy_fw_file, new_phy_fw_ver);
3306 }
3307
3308 release_firmware(phyf);
3309
3310 return ret;
3311}
3312
Vipul Pandya636f9d32012-09-26 02:39:39 +00003313/*
3314 * Attempt to initialize the adapter via a Firmware Configuration File.
3315 */
3316static int adap_init0_config(struct adapter *adapter, int reset)
3317{
3318 struct fw_caps_config_cmd caps_cmd;
3319 const struct firmware *cf;
3320 unsigned long mtype = 0, maddr = 0;
3321 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303322 int ret;
3323 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003324 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303325 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003326
3327 /*
3328 * Reset device if necessary.
3329 */
3330 if (reset) {
3331 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303332 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003333 if (ret < 0)
3334 goto bye;
3335 }
3336
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303337 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3338 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3339 * to be performed after any global adapter RESET above since some
3340 * PHYs only have local RAM copies of the PHY firmware.
3341 */
3342 if (is_10gbt_device(adapter->pdev->device)) {
3343 ret = adap_init0_phy(adapter);
3344 if (ret < 0)
3345 goto bye;
3346 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003347 /*
3348 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3349 * then use that. Otherwise, use the configuration file stored
3350 * in the adapter flash ...
3351 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303352 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003353 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303354 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003355 break;
3356 case CHELSIO_T5:
3357 fw_config_file = FW5_CFNAME;
3358 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303359 case CHELSIO_T6:
3360 fw_config_file = FW6_CFNAME;
3361 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003362 default:
3363 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3364 adapter->pdev->device);
3365 ret = -EINVAL;
3366 goto bye;
3367 }
3368
3369 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003370 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303371 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003372 mtype = FW_MEMTYPE_CF_FLASH;
3373 maddr = t4_flash_cfg_addr(adapter);
3374 } else {
3375 u32 params[7], val[7];
3376
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303377 sprintf(fw_config_file_path,
3378 "/lib/firmware/%s", fw_config_file);
3379 config_name = fw_config_file_path;
3380
Vipul Pandya636f9d32012-09-26 02:39:39 +00003381 if (cf->size >= FLASH_CFG_MAX_SIZE)
3382 ret = -ENOMEM;
3383 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303384 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3385 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003386 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303387 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003388 if (ret == 0) {
3389 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303390 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003391 * sizes have to be in terms of multiples of 4
3392 * bytes. So, if the Configuration File isn't
3393 * a multiple of 4 bytes in length we'll have
3394 * to write that out separately since we can't
3395 * guarantee that the bytes following the
3396 * residual byte in the buffer returned by
3397 * request_firmware() are zeroed out ...
3398 */
3399 size_t resid = cf->size & 0x3;
3400 size_t size = cf->size & ~0x3;
3401 __be32 *data = (__be32 *)cf->data;
3402
Hariprasad Shenai51678652014-11-21 12:52:02 +05303403 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3404 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003405
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303406 spin_lock(&adapter->win0_lock);
3407 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3408 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003409 if (ret == 0 && resid != 0) {
3410 union {
3411 __be32 word;
3412 char buf[4];
3413 } last;
3414 int i;
3415
3416 last.word = data[size >> 2];
3417 for (i = resid; i < 4; i++)
3418 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303419 ret = t4_memory_rw(adapter, 0, mtype,
3420 maddr + size,
3421 4, &last.word,
3422 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003423 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303424 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003425 }
3426 }
3427
3428 release_firmware(cf);
3429 if (ret)
3430 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003431 }
3432
Vipul Pandya636f9d32012-09-26 02:39:39 +00003433 /*
3434 * Issue a Capability Configuration command to the firmware to get it
3435 * to parse the Configuration File. We don't use t4_fw_config_file()
3436 * because we want the ability to modify various features after we've
3437 * processed the configuration file ...
3438 */
3439 memset(&caps_cmd, 0, sizeof(caps_cmd));
3440 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303441 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3442 FW_CMD_REQUEST_F |
3443 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303444 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303445 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3446 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3447 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003448 FW_LEN16(caps_cmd));
3449 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3450 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303451
3452 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3453 * Configuration File in FLASH), our last gasp effort is to use the
3454 * Firmware Configuration File which is embedded in the firmware. A
3455 * very few early versions of the firmware didn't have one embedded
3456 * but we can ignore those.
3457 */
3458 if (ret == -ENOENT) {
3459 memset(&caps_cmd, 0, sizeof(caps_cmd));
3460 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303461 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3462 FW_CMD_REQUEST_F |
3463 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303464 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3465 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3466 sizeof(caps_cmd), &caps_cmd);
3467 config_name = "Firmware Default";
3468 }
3469
3470 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003471 if (ret < 0)
3472 goto bye;
3473
Vipul Pandya636f9d32012-09-26 02:39:39 +00003474 finiver = ntohl(caps_cmd.finiver);
3475 finicsum = ntohl(caps_cmd.finicsum);
3476 cfcsum = ntohl(caps_cmd.cfcsum);
3477 if (finicsum != cfcsum)
3478 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3479 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3480 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003481
Vipul Pandya636f9d32012-09-26 02:39:39 +00003482 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003483 * And now tell the firmware to use the configuration we just loaded.
3484 */
3485 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303486 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3487 FW_CMD_REQUEST_F |
3488 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303489 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003490 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3491 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003492 if (ret < 0)
3493 goto bye;
3494
Vipul Pandya636f9d32012-09-26 02:39:39 +00003495 /*
3496 * Tweak configuration based on system architecture, module
3497 * parameters, etc.
3498 */
3499 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003500 if (ret < 0)
3501 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003502
Vipul Pandya636f9d32012-09-26 02:39:39 +00003503 /*
3504 * And finally tell the firmware to initialize itself using the
3505 * parameters from the Configuration File.
3506 */
3507 ret = t4_fw_initialize(adapter, adapter->mbox);
3508 if (ret < 0)
3509 goto bye;
3510
Hariprasad Shenai06640312015-01-13 15:19:25 +05303511 /* Emit Firmware Configuration File information and return
3512 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003513 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003514 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303515 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3516 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003517 return 0;
3518
3519 /*
3520 * Something bad happened. Return the error ... (If the "error"
3521 * is that there's no Configuration File on the adapter we don't
3522 * want to issue a warning since this is fairly common.)
3523 */
3524bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303525 if (config_issued && ret != -ENOENT)
3526 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3527 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003528 return ret;
3529}
3530
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303531static struct fw_info fw_info_array[] = {
3532 {
3533 .chip = CHELSIO_T4,
3534 .fs_name = FW4_CFNAME,
3535 .fw_mod_name = FW4_FNAME,
3536 .fw_hdr = {
3537 .chip = FW_HDR_CHIP_T4,
3538 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3539 .intfver_nic = FW_INTFVER(T4, NIC),
3540 .intfver_vnic = FW_INTFVER(T4, VNIC),
3541 .intfver_ri = FW_INTFVER(T4, RI),
3542 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3543 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3544 },
3545 }, {
3546 .chip = CHELSIO_T5,
3547 .fs_name = FW5_CFNAME,
3548 .fw_mod_name = FW5_FNAME,
3549 .fw_hdr = {
3550 .chip = FW_HDR_CHIP_T5,
3551 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3552 .intfver_nic = FW_INTFVER(T5, NIC),
3553 .intfver_vnic = FW_INTFVER(T5, VNIC),
3554 .intfver_ri = FW_INTFVER(T5, RI),
3555 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3556 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3557 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303558 }, {
3559 .chip = CHELSIO_T6,
3560 .fs_name = FW6_CFNAME,
3561 .fw_mod_name = FW6_FNAME,
3562 .fw_hdr = {
3563 .chip = FW_HDR_CHIP_T6,
3564 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3565 .intfver_nic = FW_INTFVER(T6, NIC),
3566 .intfver_vnic = FW_INTFVER(T6, VNIC),
3567 .intfver_ofld = FW_INTFVER(T6, OFLD),
3568 .intfver_ri = FW_INTFVER(T6, RI),
3569 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3570 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3571 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3572 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3573 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303574 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303575
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303576};
3577
3578static struct fw_info *find_fw_info(int chip)
3579{
3580 int i;
3581
3582 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3583 if (fw_info_array[i].chip == chip)
3584 return &fw_info_array[i];
3585 }
3586 return NULL;
3587}
3588
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003589/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003590 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003591 */
3592static int adap_init0(struct adapter *adap)
3593{
3594 int ret;
3595 u32 v, port_vec;
3596 enum dev_state state;
3597 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003598 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303599 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003600
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303601 /* Grab Firmware Device Log parameters as early as possible so we have
3602 * access to it for debugging, etc.
3603 */
3604 ret = t4_init_devlog_params(adap);
3605 if (ret < 0)
3606 return ret;
3607
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303608 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303609 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3610 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003611 if (ret < 0) {
3612 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3613 ret);
3614 return ret;
3615 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003616 if (ret == adap->mbox)
3617 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003618
Vipul Pandya636f9d32012-09-26 02:39:39 +00003619 /*
3620 * If we're the Master PF Driver and the device is uninitialized,
3621 * then let's consider upgrading the firmware ... (We always want
3622 * to check the firmware version number in order to A. get it for
3623 * later reporting and B. to warn if the currently loaded firmware
3624 * is excessively mismatched relative to the driver.)
3625 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303626 t4_get_fw_version(adap, &adap->params.fw_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303627 t4_get_bs_version(adap, &adap->params.bs_vers);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303628 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303629 t4_get_exprom_version(adap, &adap->params.er_vers);
3630
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303631 ret = t4_check_fw_version(adap);
3632 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303633 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303634 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003635 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303636 struct fw_info *fw_info;
3637 struct fw_hdr *card_fw;
3638 const struct firmware *fw;
3639 const u8 *fw_data = NULL;
3640 unsigned int fw_size = 0;
3641
3642 /* This is the firmware whose headers the driver was compiled
3643 * against
3644 */
3645 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3646 if (fw_info == NULL) {
3647 dev_err(adap->pdev_dev,
3648 "unable to get firmware info for chip %d.\n",
3649 CHELSIO_CHIP_VERSION(adap->params.chip));
3650 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003651 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303652
3653 /* allocate memory to read the header of the firmware on the
3654 * card
3655 */
3656 card_fw = t4_alloc_mem(sizeof(*card_fw));
3657
3658 /* Get FW from from /lib/firmware/ */
3659 ret = request_firmware(&fw, fw_info->fw_mod_name,
3660 adap->pdev_dev);
3661 if (ret < 0) {
3662 dev_err(adap->pdev_dev,
3663 "unable to load firmware image %s, error %d\n",
3664 fw_info->fw_mod_name, ret);
3665 } else {
3666 fw_data = fw->data;
3667 fw_size = fw->size;
3668 }
3669
3670 /* upgrade FW logic */
3671 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3672 state, &reset);
3673
3674 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003675 release_firmware(fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303676 t4_free_mem(card_fw);
3677
Vipul Pandya636f9d32012-09-26 02:39:39 +00003678 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303679 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003680 }
3681
3682 /*
3683 * Grab VPD parameters. This should be done after we establish a
3684 * connection to the firmware since some of the VPD parameters
3685 * (notably the Core Clock frequency) are retrieved via requests to
3686 * the firmware. On the other hand, we need these fairly early on
3687 * so we do this right after getting ahold of the firmware.
3688 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303689 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003690 if (ret < 0)
3691 goto bye;
3692
Vipul Pandya636f9d32012-09-26 02:39:39 +00003693 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003694 * Find out what ports are available to us. Note that we need to do
3695 * this before calling adap_init0_no_config() since it needs nports
3696 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003697 */
3698 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303699 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3700 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303701 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003702 if (ret < 0)
3703 goto bye;
3704
3705 adap->params.nports = hweight32(port_vec);
3706 adap->params.portvec = port_vec;
3707
Hariprasad Shenai06640312015-01-13 15:19:25 +05303708 /* If the firmware is initialized already, emit a simply note to that
3709 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003710 */
3711 if (state == DEV_STATE_INIT) {
3712 dev_info(adap->pdev_dev, "Coming up as %s: "\
3713 "Adapter already initialized\n",
3714 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003715 } else {
3716 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3717 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303718
3719 /* Find out whether we're dealing with a version of the
3720 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003721 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303722 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3723 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303724 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303725 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003726
Hariprasad Shenai06640312015-01-13 15:19:25 +05303727 /* If the firmware doesn't support Configuration Files,
3728 * return an error.
3729 */
3730 if (ret < 0) {
3731 dev_err(adap->pdev_dev, "firmware doesn't support "
3732 "Firmware Configuration Files\n");
3733 goto bye;
3734 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003735
Hariprasad Shenai06640312015-01-13 15:19:25 +05303736 /* The firmware provides us with a memory buffer where we can
3737 * load a Configuration File from the host if we want to
3738 * override the Configuration File in flash.
3739 */
3740 ret = adap_init0_config(adap, reset);
3741 if (ret == -ENOENT) {
3742 dev_err(adap->pdev_dev, "no Configuration File "
3743 "present on adapter.\n");
3744 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003745 }
3746 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303747 dev_err(adap->pdev_dev, "could not initialize "
3748 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003749 goto bye;
3750 }
3751 }
3752
Hariprasad Shenai06640312015-01-13 15:19:25 +05303753 /* Give the SGE code a chance to pull in anything that it needs ...
3754 * Note that this must be called after we retrieve our VPD parameters
3755 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003756 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303757 ret = t4_sge_init(adap);
3758 if (ret < 0)
3759 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003760
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003761 if (is_bypass_device(adap->pdev->device))
3762 adap->params.bypass = 1;
3763
Vipul Pandya636f9d32012-09-26 02:39:39 +00003764 /*
3765 * Grab some of our basic fundamental operating parameters.
3766 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003767#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303768 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3769 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003770
3771#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303772 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3773 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3774 FW_PARAMS_PARAM_Y_V(0) | \
3775 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003776
Vipul Pandya636f9d32012-09-26 02:39:39 +00003777 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003778 params[1] = FW_PARAM_PFVF(L2T_START);
3779 params[2] = FW_PARAM_PFVF(L2T_END);
3780 params[3] = FW_PARAM_PFVF(FILTER_START);
3781 params[4] = FW_PARAM_PFVF(FILTER_END);
3782 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303783 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003784 if (ret < 0)
3785 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003786 adap->sge.egr_start = val[0];
3787 adap->l2t_start = val[1];
3788 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003789 adap->tids.ftid_base = val[3];
3790 adap->tids.nftids = val[4] - val[3] + 1;
3791 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003792
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303793 /* qids (ingress/egress) returned from firmware can be anywhere
3794 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3795 * Hence driver needs to allocate memory for this range to
3796 * store the queue info. Get the highest IQFLINT/EQ index returned
3797 * in FW_EQ_*_CMD.alloc command.
3798 */
3799 params[0] = FW_PARAM_PFVF(EQ_END);
3800 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303801 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303802 if (ret < 0)
3803 goto bye;
3804 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3805 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3806
3807 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3808 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3809 if (!adap->sge.egr_map) {
3810 ret = -ENOMEM;
3811 goto bye;
3812 }
3813
3814 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3815 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3816 if (!adap->sge.ingr_map) {
3817 ret = -ENOMEM;
3818 goto bye;
3819 }
3820
3821 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303822 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303823 */
3824 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3825 sizeof(long), GFP_KERNEL);
3826 if (!adap->sge.starving_fl) {
3827 ret = -ENOMEM;
3828 goto bye;
3829 }
3830
3831 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3832 sizeof(long), GFP_KERNEL);
3833 if (!adap->sge.txq_maperr) {
3834 ret = -ENOMEM;
3835 goto bye;
3836 }
3837
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303838#ifdef CONFIG_DEBUG_FS
3839 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3840 sizeof(long), GFP_KERNEL);
3841 if (!adap->sge.blocked_fl) {
3842 ret = -ENOMEM;
3843 goto bye;
3844 }
3845#endif
3846
Anish Bhattb5a02f52015-01-14 15:17:34 -08003847 params[0] = FW_PARAM_PFVF(CLIP_START);
3848 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303849 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003850 if (ret < 0)
3851 goto bye;
3852 adap->clipt_start = val[0];
3853 adap->clipt_end = val[1];
3854
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303855 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3856 * Classes supported by the hardware/firmware so we hard code it here
3857 * for now.
3858 */
3859 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3860
Vipul Pandya636f9d32012-09-26 02:39:39 +00003861 /* query params related to active filter region */
3862 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3863 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303864 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003865 /* If Active filter size is set we enable establishing
3866 * offload connection through firmware work request
3867 */
3868 if ((val[0] != val[1]) && (ret >= 0)) {
3869 adap->flags |= FW_OFLD_CONN;
3870 adap->tids.aftid_base = val[0];
3871 adap->tids.aftid_end = val[1];
3872 }
3873
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003874 /* If we're running on newer firmware, let it know that we're
3875 * prepared to deal with encapsulated CPL messages. Older
3876 * firmware won't understand this and we'll just get
3877 * unencapsulated messages ...
3878 */
3879 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3880 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303881 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003882
Vipul Pandya636f9d32012-09-26 02:39:39 +00003883 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303884 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3885 * capability. Earlier versions of the firmware didn't have the
3886 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3887 * permission to use ULPTX MEMWRITE DSGL.
3888 */
3889 if (is_t4(adap->params.chip)) {
3890 adap->params.ulptx_memwrite_dsgl = false;
3891 } else {
3892 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303893 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303894 1, params, val);
3895 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3896 }
3897
3898 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003899 * Get device capabilities so we can determine what resources we need
3900 * to manage.
3901 */
3902 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303903 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3904 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303905 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003906 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3907 &caps_cmd);
3908 if (ret < 0)
3909 goto bye;
3910
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003911 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003912 /* query offload-related parameters */
3913 params[0] = FW_PARAM_DEV(NTID);
3914 params[1] = FW_PARAM_PFVF(SERVER_START);
3915 params[2] = FW_PARAM_PFVF(SERVER_END);
3916 params[3] = FW_PARAM_PFVF(TDDP_START);
3917 params[4] = FW_PARAM_PFVF(TDDP_END);
3918 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303919 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003920 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003921 if (ret < 0)
3922 goto bye;
3923 adap->tids.ntids = val[0];
3924 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3925 adap->tids.stid_base = val[1];
3926 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003927 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003928 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003929 * region into two parts. Regular filters get 1/3rd and server
3930 * filters get 2/3rd part. This is only enabled if workarond
3931 * path is enabled.
3932 * 1. For regular filters.
3933 * 2. Server filter: This are special filters which are used
3934 * to redirect SYN packets to offload queue.
3935 */
3936 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3937 adap->tids.sftid_base = adap->tids.ftid_base +
3938 DIV_ROUND_UP(adap->tids.nftids, 3);
3939 adap->tids.nsftids = adap->tids.nftids -
3940 DIV_ROUND_UP(adap->tids.nftids, 3);
3941 adap->tids.nftids = adap->tids.sftid_base -
3942 adap->tids.ftid_base;
3943 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003944 adap->vres.ddp.start = val[3];
3945 adap->vres.ddp.size = val[4] - val[3] + 1;
3946 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00003947
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003948 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303949 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003950 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003951 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003952 params[0] = FW_PARAM_PFVF(STAG_START);
3953 params[1] = FW_PARAM_PFVF(STAG_END);
3954 params[2] = FW_PARAM_PFVF(RQ_START);
3955 params[3] = FW_PARAM_PFVF(RQ_END);
3956 params[4] = FW_PARAM_PFVF(PBL_START);
3957 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303958 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003959 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003960 if (ret < 0)
3961 goto bye;
3962 adap->vres.stag.start = val[0];
3963 adap->vres.stag.size = val[1] - val[0] + 1;
3964 adap->vres.rq.start = val[2];
3965 adap->vres.rq.size = val[3] - val[2] + 1;
3966 adap->vres.pbl.start = val[4];
3967 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003968
3969 params[0] = FW_PARAM_PFVF(SQRQ_START);
3970 params[1] = FW_PARAM_PFVF(SQRQ_END);
3971 params[2] = FW_PARAM_PFVF(CQ_START);
3972 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003973 params[4] = FW_PARAM_PFVF(OCQ_START);
3974 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303975 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303976 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003977 if (ret < 0)
3978 goto bye;
3979 adap->vres.qp.start = val[0];
3980 adap->vres.qp.size = val[1] - val[0] + 1;
3981 adap->vres.cq.start = val[2];
3982 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003983 adap->vres.ocq.start = val[4];
3984 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303985
3986 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3987 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303988 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303989 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303990 if (ret < 0) {
3991 adap->params.max_ordird_qp = 8;
3992 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3993 ret = 0;
3994 } else {
3995 adap->params.max_ordird_qp = val[0];
3996 adap->params.max_ird_adapter = val[1];
3997 }
3998 dev_info(adap->pdev_dev,
3999 "max_ordird_qp %d max_ird_adapter %d\n",
4000 adap->params.max_ordird_qp,
4001 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304002 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004003 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004004 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004005 params[0] = FW_PARAM_PFVF(ISCSI_START);
4006 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304007 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004008 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004009 if (ret < 0)
4010 goto bye;
4011 adap->vres.iscsi.start = val[0];
4012 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304013 /* LIO target and cxgb4i initiaitor */
4014 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004015 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304016 if (caps_cmd.cryptocaps) {
4017 /* Should query params here...TODO */
4018 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4019 adap->num_uld += 1;
4020 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004021#undef FW_PARAM_PFVF
4022#undef FW_PARAM_DEV
4023
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304024 /* The MTU/MSS Table is initialized by now, so load their values. If
4025 * we're initializing the adapter, then we'll make any modifications
4026 * we want to the MTU/MSS Table and also initialize the congestion
4027 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004028 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004029 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304030 if (state != DEV_STATE_INIT) {
4031 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004032
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304033 /* The default MTU Table contains values 1492 and 1500.
4034 * However, for TCP, it's better to have two values which are
4035 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4036 * This allows us to have a TCP Data Payload which is a
4037 * multiple of 8 regardless of what combination of TCP Options
4038 * are in use (always a multiple of 4 bytes) which is
4039 * important for performance reasons. For instance, if no
4040 * options are in use, then we have a 20-byte IP header and a
4041 * 20-byte TCP header. In this case, a 1500-byte MSS would
4042 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4043 * which is not a multiple of 8. So using an MSS of 1488 in
4044 * this case results in a TCP Data Payload of 1448 bytes which
4045 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4046 * Stamps have been negotiated, then an MTU of 1500 bytes
4047 * results in a TCP Data Payload of 1448 bytes which, as
4048 * above, is a multiple of 8 bytes ...
4049 */
4050 for (i = 0; i < NMTUS; i++)
4051 if (adap->params.mtus[i] == 1492) {
4052 adap->params.mtus[i] = 1488;
4053 break;
4054 }
4055
4056 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4057 adap->params.b_wnd);
4058 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304059 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004060 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05304061 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004062 return 0;
4063
4064 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004065 * Something bad happened. If a command timed out or failed with EIO
4066 * FW does not operate within its spec or something catastrophic
4067 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004068 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004069bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304070 kfree(adap->sge.egr_map);
4071 kfree(adap->sge.ingr_map);
4072 kfree(adap->sge.starving_fl);
4073 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304074#ifdef CONFIG_DEBUG_FS
4075 kfree(adap->sge.blocked_fl);
4076#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004077 if (ret != -ETIMEDOUT && ret != -EIO)
4078 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004079 return ret;
4080}
4081
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004082/* EEH callbacks */
4083
4084static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4085 pci_channel_state_t state)
4086{
4087 int i;
4088 struct adapter *adap = pci_get_drvdata(pdev);
4089
4090 if (!adap)
4091 goto out;
4092
4093 rtnl_lock();
4094 adap->flags &= ~FW_OK;
4095 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004096 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004097 for_each_port(adap, i) {
4098 struct net_device *dev = adap->port[i];
4099
4100 netif_device_detach(dev);
4101 netif_carrier_off(dev);
4102 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004103 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304104 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004105 if (adap->flags & FULL_INIT_DONE)
4106 cxgb_down(adap);
4107 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004108 if ((adap->flags & DEV_ENABLED)) {
4109 pci_disable_device(pdev);
4110 adap->flags &= ~DEV_ENABLED;
4111 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004112out: return state == pci_channel_io_perm_failure ?
4113 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4114}
4115
4116static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4117{
4118 int i, ret;
4119 struct fw_caps_config_cmd c;
4120 struct adapter *adap = pci_get_drvdata(pdev);
4121
4122 if (!adap) {
4123 pci_restore_state(pdev);
4124 pci_save_state(pdev);
4125 return PCI_ERS_RESULT_RECOVERED;
4126 }
4127
Gavin Shan144be3d2014-01-23 12:27:34 +08004128 if (!(adap->flags & DEV_ENABLED)) {
4129 if (pci_enable_device(pdev)) {
4130 dev_err(&pdev->dev, "Cannot reenable PCI "
4131 "device after reset\n");
4132 return PCI_ERS_RESULT_DISCONNECT;
4133 }
4134 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004135 }
4136
4137 pci_set_master(pdev);
4138 pci_restore_state(pdev);
4139 pci_save_state(pdev);
4140 pci_cleanup_aer_uncorrect_error_status(pdev);
4141
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304142 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004143 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304144 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004145 return PCI_ERS_RESULT_DISCONNECT;
4146 adap->flags |= FW_OK;
4147 if (adap_init1(adap, &c))
4148 return PCI_ERS_RESULT_DISCONNECT;
4149
4150 for_each_port(adap, i) {
4151 struct port_info *p = adap2pinfo(adap, i);
4152
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304153 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004154 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004155 if (ret < 0)
4156 return PCI_ERS_RESULT_DISCONNECT;
4157 p->viid = ret;
4158 p->xact_addr_filt = -1;
4159 }
4160
4161 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4162 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004163 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004164 if (cxgb_up(adap))
4165 return PCI_ERS_RESULT_DISCONNECT;
4166 return PCI_ERS_RESULT_RECOVERED;
4167}
4168
4169static void eeh_resume(struct pci_dev *pdev)
4170{
4171 int i;
4172 struct adapter *adap = pci_get_drvdata(pdev);
4173
4174 if (!adap)
4175 return;
4176
4177 rtnl_lock();
4178 for_each_port(adap, i) {
4179 struct net_device *dev = adap->port[i];
4180
4181 if (netif_running(dev)) {
4182 link_start(dev);
4183 cxgb_set_rxmode(dev);
4184 }
4185 netif_device_attach(dev);
4186 }
4187 rtnl_unlock();
4188}
4189
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004190static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004191 .error_detected = eeh_err_detected,
4192 .slot_reset = eeh_slot_reset,
4193 .resume = eeh_resume,
4194};
4195
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304196static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004197{
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304198 return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0 ||
4199 (lc->supported & FW_PORT_CAP_SPEED_40G) != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004200}
4201
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004202/*
4203 * Perform default configuration of DMA queues depending on the number and type
4204 * of ports we found and the number of available CPUs. Most settings can be
4205 * modified by the admin prior to actual use.
4206 */
Bill Pemberton91744942012-12-03 09:23:02 -05004207static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004208{
4209 struct sge *s = &adap->sge;
Anish Bhatt688848b2014-06-19 21:37:13 -07004210 int i, n10g = 0, qidx = 0;
4211#ifndef CONFIG_CHELSIO_T4_DCB
4212 int q10g = 0;
4213#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004214
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304215 /* Reduce memory usage in kdump environment, disable all offload.
4216 */
4217 if (is_kdump_kernel()) {
4218 adap->params.offload = 0;
4219 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304220 } else if (is_uld(adap) && t4_uld_mem_alloc(adap)) {
4221 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304222 adap->params.crypto = 0;
4223 }
4224
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004225 for_each_port(adap, i)
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304226 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004227#ifdef CONFIG_CHELSIO_T4_DCB
4228 /* For Data Center Bridging support we need to be able to support up
4229 * to 8 Traffic Priorities; each of which will be assigned to its
4230 * own TX Queue in order to prevent Head-Of-Line Blocking.
4231 */
4232 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4233 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4234 MAX_ETH_QSETS, adap->params.nports * 8);
4235 BUG_ON(1);
4236 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004237
Anish Bhatt688848b2014-06-19 21:37:13 -07004238 for_each_port(adap, i) {
4239 struct port_info *pi = adap2pinfo(adap, i);
4240
4241 pi->first_qset = qidx;
4242 pi->nqsets = 8;
4243 qidx += pi->nqsets;
4244 }
4245#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004246 /*
4247 * We default to 1 queue per non-10G port and up to # of cores queues
4248 * per 10G port.
4249 */
4250 if (n10g)
4251 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004252 if (q10g > netif_get_num_default_rss_queues())
4253 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004254
4255 for_each_port(adap, i) {
4256 struct port_info *pi = adap2pinfo(adap, i);
4257
4258 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304259 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004260 qidx += pi->nqsets;
4261 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004262#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004263
4264 s->ethqsets = qidx;
4265 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4266
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304267 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004268 /*
4269 * For offload we use 1 queue/channel if all ports are up to 1G,
4270 * otherwise we divide all available queues amongst the channels
4271 * capped by the number of available cores.
4272 */
4273 if (n10g) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304274 i = num_online_cpus();
4275 s->ofldqsets = roundup(i, adap->params.nports);
4276 } else {
4277 s->ofldqsets = adap->params.nports;
4278 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004279 }
4280
4281 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4282 struct sge_eth_rxq *r = &s->ethrxq[i];
4283
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304284 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004285 r->fl.size = 72;
4286 }
4287
4288 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4289 s->ethtxq[i].q.size = 1024;
4290
4291 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4292 s->ctrlq[i].q.size = 512;
4293
4294 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
4295 s->ofldtxq[i].q.size = 1024;
4296
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304297 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304298 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004299}
4300
4301/*
4302 * Reduce the number of Ethernet queues across all ports to at most n.
4303 * n provides at least one queue per port.
4304 */
Bill Pemberton91744942012-12-03 09:23:02 -05004305static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004306{
4307 int i;
4308 struct port_info *pi;
4309
4310 while (n < adap->sge.ethqsets)
4311 for_each_port(adap, i) {
4312 pi = adap2pinfo(adap, i);
4313 if (pi->nqsets > 1) {
4314 pi->nqsets--;
4315 adap->sge.ethqsets--;
4316 if (adap->sge.ethqsets <= n)
4317 break;
4318 }
4319 }
4320
4321 n = 0;
4322 for_each_port(adap, i) {
4323 pi = adap2pinfo(adap, i);
4324 pi->first_qset = n;
4325 n += pi->nqsets;
4326 }
4327}
4328
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304329static int get_msix_info(struct adapter *adap)
4330{
4331 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304332 unsigned int max_ingq = 0;
4333
4334 if (is_offload(adap))
4335 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4336 if (is_pci_uld(adap))
4337 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4338
4339 if (!max_ingq)
4340 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304341
4342 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4343 if (!msix_info)
4344 return -ENOMEM;
4345
4346 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4347 sizeof(long), GFP_KERNEL);
4348 if (!adap->msix_bmap_ulds.msix_bmap) {
4349 kfree(msix_info);
4350 return -ENOMEM;
4351 }
4352 spin_lock_init(&adap->msix_bmap_ulds.lock);
4353 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304354out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304355 return 0;
4356}
4357
4358static void free_msix_info(struct adapter *adap)
4359{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304360 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304361 return;
4362
4363 kfree(adap->msix_info_ulds);
4364 kfree(adap->msix_bmap_ulds.msix_bmap);
4365}
4366
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004367/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4368#define EXTRA_VECS 2
4369
Bill Pemberton91744942012-12-03 09:23:02 -05004370static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004371{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304372 int ofld_need = 0, uld_need = 0;
4373 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004374 struct sge *s = &adap->sge;
4375 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304376 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304377 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004378
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304379 if (is_pci_uld(adap))
4380 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4381 if (is_offload(adap))
4382 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304383 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304384 GFP_KERNEL);
4385 if (!entries)
4386 return -ENOMEM;
4387
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304388 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304389 if (get_msix_info(adap)) {
4390 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304391 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304392 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304393
4394 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004395 entries[i].entry = i;
4396
4397 want = s->max_ethqsets + EXTRA_VECS;
4398 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304399 want += adap->num_ofld_uld * s->ofldqsets;
4400 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004401 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304402 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304403 want += adap->num_uld * s->ofldqsets;
4404 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304405 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004406#ifdef CONFIG_CHELSIO_T4_DCB
4407 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4408 * each port.
4409 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304410 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004411#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304412 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004413#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304414 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4415 if (allocated < 0) {
4416 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4417 " not using MSI-X\n");
4418 kfree(entries);
4419 return allocated;
4420 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004421
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304422 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004423 * Every group gets its minimum requirement and NIC gets top
4424 * priority for leftovers.
4425 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304426 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004427 if (i < s->max_ethqsets) {
4428 s->max_ethqsets = i;
4429 if (i < s->ethqsets)
4430 reduce_ethqs(adap, i);
4431 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304432 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304433 if (allocated < want)
4434 s->nqs_per_uld = nchan;
4435 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304436 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304437 }
4438
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304439 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004440 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304441 if (is_uld(adap)) {
4442 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304443 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304444 adap->msix_info_ulds[j].idx = i;
4445 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304446 adap->msix_bmap_ulds.mapsize = j;
4447 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304448 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304449 "nic %d per uld %d\n",
4450 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004451
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304452 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004453 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004454}
4455
4456#undef EXTRA_VECS
4457
Bill Pemberton91744942012-12-03 09:23:02 -05004458static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004459{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304460 unsigned int i;
4461 int err;
4462
4463 err = t4_init_rss_mode(adap, adap->mbox);
4464 if (err)
4465 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004466
4467 for_each_port(adap, i) {
4468 struct port_info *pi = adap2pinfo(adap, i);
4469
4470 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4471 if (!pi->rss)
4472 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004473 }
4474 return 0;
4475}
4476
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304477static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4478 enum pci_bus_speed *speed,
4479 enum pcie_link_width *width)
4480{
4481 u32 lnkcap1, lnkcap2;
4482 int err1, err2;
4483
4484#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4485
4486 *speed = PCI_SPEED_UNKNOWN;
4487 *width = PCIE_LNK_WIDTH_UNKNOWN;
4488
4489 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4490 &lnkcap1);
4491 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4492 &lnkcap2);
4493 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4494 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4495 *speed = PCIE_SPEED_8_0GT;
4496 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4497 *speed = PCIE_SPEED_5_0GT;
4498 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4499 *speed = PCIE_SPEED_2_5GT;
4500 }
4501 if (!err1) {
4502 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4503 if (!lnkcap2) { /* pre-r3.0 */
4504 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4505 *speed = PCIE_SPEED_5_0GT;
4506 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4507 *speed = PCIE_SPEED_2_5GT;
4508 }
4509 }
4510
4511 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4512 return err1 ? err1 : err2 ? err2 : -EINVAL;
4513 return 0;
4514}
4515
4516static void cxgb4_check_pcie_caps(struct adapter *adap)
4517{
4518 enum pcie_link_width width, width_cap;
4519 enum pci_bus_speed speed, speed_cap;
4520
4521#define PCIE_SPEED_STR(speed) \
4522 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4523 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4524 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4525 "Unknown")
4526
4527 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4528 dev_warn(adap->pdev_dev,
4529 "Unable to determine PCIe device BW capabilities\n");
4530 return;
4531 }
4532
4533 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4534 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4535 dev_warn(adap->pdev_dev,
4536 "Unable to determine PCI Express bandwidth.\n");
4537 return;
4538 }
4539
4540 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4541 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4542 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4543 width, width_cap);
4544 if (speed < speed_cap || width < width_cap)
4545 dev_info(adap->pdev_dev,
4546 "A slot with more lanes and/or higher speed is "
4547 "suggested for optimal performance.\n");
4548}
4549
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304550/* Dump basic information about the adapter */
4551static void print_adapter_info(struct adapter *adapter)
4552{
4553 /* Device information */
4554 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4555 adapter->params.vpd.id,
4556 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4557 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4558 adapter->params.vpd.sn, adapter->params.vpd.pn);
4559
4560 /* Firmware Version */
4561 if (!adapter->params.fw_vers)
4562 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4563 else
4564 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4565 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4566 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4567 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4568 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4569
4570 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4571 * Firmware, so dev_info() is more appropriate here.)
4572 */
4573 if (!adapter->params.bs_vers)
4574 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4575 else
4576 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4577 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4578 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4579 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4580 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4581
4582 /* TP Microcode Version */
4583 if (!adapter->params.tp_vers)
4584 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4585 else
4586 dev_info(adapter->pdev_dev,
4587 "TP Microcode version: %u.%u.%u.%u\n",
4588 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4589 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4590 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4591 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4592
4593 /* Expansion ROM version */
4594 if (!adapter->params.er_vers)
4595 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4596 else
4597 dev_info(adapter->pdev_dev,
4598 "Expansion ROM version: %u.%u.%u.%u\n",
4599 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4600 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4601 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4602 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4603
4604 /* Software/Hardware configuration */
4605 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4606 is_offload(adapter) ? "R" : "",
4607 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4608 (adapter->flags & USING_MSI) ? "MSI" : ""),
4609 is_offload(adapter) ? "Offload" : "non-Offload");
4610}
4611
Bill Pemberton91744942012-12-03 09:23:02 -05004612static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004613{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004614 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004615 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004616 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004617 const struct port_info *pi = netdev_priv(dev);
4618 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004619
4620 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4621 spd = " 2.5 GT/s";
4622 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4623 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004624 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4625 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004626
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004627 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4628 bufp += sprintf(bufp, "100/");
4629 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4630 bufp += sprintf(bufp, "1000/");
4631 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4632 bufp += sprintf(bufp, "10G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304633 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4634 bufp += sprintf(bufp, "40G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004635 if (bufp != buf)
4636 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304637 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004638
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304639 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4640 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004641}
4642
Bill Pemberton91744942012-12-03 09:23:02 -05004643static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004644{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004645 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004646}
4647
Dimitris Michailidis06546392010-07-11 12:01:16 +00004648/*
4649 * Free the following resources:
4650 * - memory used for tables
4651 * - MSI/MSI-X
4652 * - net devices
4653 * - resources FW is holding for us
4654 */
4655static void free_some_resources(struct adapter *adapter)
4656{
4657 unsigned int i;
4658
4659 t4_free_mem(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304660 t4_cleanup_sched(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004661 t4_free_mem(adapter->tids.tid_tab);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304662 kfree(adapter->sge.egr_map);
4663 kfree(adapter->sge.ingr_map);
4664 kfree(adapter->sge.starving_fl);
4665 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304666#ifdef CONFIG_DEBUG_FS
4667 kfree(adapter->sge.blocked_fl);
4668#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004669 disable_msi(adapter);
4670
4671 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004672 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304673 struct port_info *pi = adap2pinfo(adapter, i);
4674
4675 if (pi->viid != 0)
4676 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4677 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004678 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004679 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004680 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004681 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304682 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004683}
4684
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004685#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004686#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004687 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004688#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004689
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304690static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4691{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304692 u16 device_id;
4693
4694 /* Retrieve adapter's device ID */
4695 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004696
4697 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304698 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004699 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304700 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004701 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304702 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004703 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304704 default:
4705 dev_err(&pdev->dev, "Device %d is not supported\n",
4706 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304707 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004708 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304709}
4710
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304711#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304712static void dummy_setup(struct net_device *dev)
4713{
4714 dev->type = ARPHRD_NONE;
4715 dev->mtu = 0;
4716 dev->hard_header_len = 0;
4717 dev->addr_len = 0;
4718 dev->tx_queue_len = 0;
4719 dev->flags |= IFF_NOARP;
4720 dev->priv_flags |= IFF_NO_QUEUE;
4721
4722 /* Initialize the device structure. */
4723 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4724 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
4725 dev->destructor = free_netdev;
4726}
4727
4728static int config_mgmt_dev(struct pci_dev *pdev)
4729{
4730 struct adapter *adap = pci_get_drvdata(pdev);
4731 struct net_device *netdev;
4732 struct port_info *pi;
4733 char name[IFNAMSIZ];
4734 int err;
4735
4736 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
4737 netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, dummy_setup);
4738 if (!netdev)
4739 return -ENOMEM;
4740
4741 pi = netdev_priv(netdev);
4742 pi->adapter = adap;
4743 SET_NETDEV_DEV(netdev, &pdev->dev);
4744
4745 adap->port[0] = netdev;
4746
4747 err = register_netdev(adap->port[0]);
4748 if (err) {
4749 pr_info("Unable to register VF mgmt netdev %s\n", name);
4750 free_netdev(adap->port[0]);
4751 adap->port[0] = NULL;
4752 return err;
4753 }
4754 return 0;
4755}
4756
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304757static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4758{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304759 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304760 int err = 0;
4761 int current_vfs = pci_num_vf(pdev);
4762 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304763
Hariprasad Shenai78294512016-08-11 21:06:23 +05304764 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304765 /* Check if cxgb4 is the MASTER and fw is initialized */
4766 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4767 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4768 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4769 dev_warn(&pdev->dev,
4770 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4771 return -EOPNOTSUPP;
4772 }
4773
4774 /* If any of the VF's is already assigned to Guest OS, then
4775 * SRIOV for the same cannot be modified
4776 */
4777 if (current_vfs && pci_vfs_assigned(pdev)) {
4778 dev_err(&pdev->dev,
4779 "Cannot modify SR-IOV while VFs are assigned\n");
4780 num_vfs = current_vfs;
4781 return num_vfs;
4782 }
4783
4784 /* Disable SRIOV when zero is passed.
4785 * One needs to disable SRIOV before modifying it, else
4786 * stack throws the below warning:
4787 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4788 */
4789 if (!num_vfs) {
4790 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304791 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304792 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304793 adap->port[0] = NULL;
4794 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304795 /* free VF resources */
4796 kfree(adap->vfinfo);
4797 adap->vfinfo = NULL;
4798 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304799 return num_vfs;
4800 }
4801
4802 if (num_vfs != current_vfs) {
4803 err = pci_enable_sriov(pdev, num_vfs);
4804 if (err)
4805 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304806
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304807 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304808 err = config_mgmt_dev(pdev);
4809 if (err)
4810 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304811 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304812
4813 adap->vfinfo = kcalloc(adap->num_vfs,
4814 sizeof(struct vf_info), GFP_KERNEL);
4815 if (adap->vfinfo)
4816 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304817 return num_vfs;
4818}
4819#endif
4820
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004821static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004822{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004823 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004824 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004825 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004826 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304827 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304828 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304829 u32 whoami, pl_rev;
4830 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304831 static int adap_idx = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004832
4833 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4834
4835 err = pci_request_regions(pdev, KBUILD_MODNAME);
4836 if (err) {
4837 /* Just info, some other driver may have claimed the device. */
4838 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4839 return err;
4840 }
4841
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004842 err = pci_enable_device(pdev);
4843 if (err) {
4844 dev_err(&pdev->dev, "cannot enable PCI device\n");
4845 goto out_release_regions;
4846 }
4847
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304848 regs = pci_ioremap_bar(pdev, 0);
4849 if (!regs) {
4850 dev_err(&pdev->dev, "cannot map device registers\n");
4851 err = -ENOMEM;
4852 goto out_disable_device;
4853 }
4854
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304855 err = t4_wait_dev_ready(regs);
4856 if (err < 0)
4857 goto out_unmap_bar0;
4858
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304859 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304860 whoami = readl(regs + PL_WHOAMI_A);
4861 pl_rev = REV_G(readl(regs + PL_REV_A));
4862 chip = get_chip_type(pdev, pl_rev);
4863 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4864 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304865 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304866#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304867 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304868#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304869 pci_disable_device(pdev);
4870 pci_save_state(pdev); /* to restore SR-IOV later */
4871 goto sriov;
4872 }
4873
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004874 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004875 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004876 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4877 if (err) {
4878 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4879 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304880 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004881 }
4882 } else {
4883 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4884 if (err) {
4885 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304886 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004887 }
4888 }
4889
4890 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004891 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004892 pci_set_master(pdev);
4893 pci_save_state(pdev);
4894
4895 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4896 if (!adapter) {
4897 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304898 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004899 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304900 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004901
Anish Bhatt29aaee62014-08-20 13:44:06 -07004902 adapter->workq = create_singlethread_workqueue("cxgb4");
4903 if (!adapter->workq) {
4904 err = -ENOMEM;
4905 goto out_free_adapter;
4906 }
4907
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304908 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4909 (sizeof(struct mbox_cmd) *
4910 T4_OS_LOG_MBOX_CMDS),
4911 GFP_KERNEL);
4912 if (!adapter->mbox_log) {
4913 err = -ENOMEM;
4914 goto out_free_adapter;
4915 }
4916 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4917
Gavin Shan144be3d2014-01-23 12:27:34 +08004918 /* PCI device has been enabled */
4919 adapter->flags |= DEV_ENABLED;
4920
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304921 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004922 adapter->pdev = pdev;
4923 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304924 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304925 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304926 adapter->pf = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004927 adapter->msg_enable = dflt_msg_enable;
4928 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4929
4930 spin_lock_init(&adapter->stats_lock);
4931 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004932 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004933
4934 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304935 INIT_WORK(&adapter->db_full_task, process_db_full);
4936 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004937
4938 err = t4_prep_adapter(adapter);
4939 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304940 goto out_free_adapter;
4941
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004942
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304943 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304944 s_qpp = (QUEUESPERPAGEPF0_S +
4945 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304946 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304947 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4948 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004949 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4950
4951 /* Each segment size is 128B. Write coalescing is enabled only
4952 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4953 * queue is less no of segments that can be accommodated in
4954 * a page size.
4955 */
4956 if (qpp > num_seg) {
4957 dev_err(&pdev->dev,
4958 "Incorrect number of egress queues per page\n");
4959 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304960 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004961 }
4962 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4963 pci_resource_len(pdev, 2));
4964 if (!adapter->bar2) {
4965 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4966 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304967 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004968 }
4969 }
4970
Vipul Pandya636f9d32012-09-26 02:39:39 +00004971 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004972 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304973#ifdef CONFIG_DEBUG_FS
4974 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4975#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004976 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004977 if (err)
4978 goto out_unmap_bar;
4979
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304980 /* configure SGE_STAT_CFG_A to read WC stats */
4981 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304982 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4983 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4984 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304985
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004986 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004987 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4988 MAX_ETH_QSETS);
4989 if (!netdev) {
4990 err = -ENOMEM;
4991 goto out_free_dev;
4992 }
4993
4994 SET_NETDEV_DEV(netdev, &pdev->dev);
4995
4996 adapter->port[i] = netdev;
4997 pi = netdev_priv(netdev);
4998 pi->adapter = adapter;
4999 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005000 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005001 netdev->irq = pdev->irq;
5002
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005003 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5004 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5005 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Patrick McHardyf6469682013-04-19 02:04:27 +00005006 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005007 if (highdma)
5008 netdev->hw_features |= NETIF_F_HIGHDMA;
5009 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005010 netdev->vlan_features = netdev->features & VLAN_FEAT;
5011
Jiri Pirko01789342011-08-16 06:29:00 +00005012 netdev->priv_flags |= IFF_UNICAST_FLT;
5013
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005014 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005015#ifdef CONFIG_CHELSIO_T4_DCB
5016 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5017 cxgb4_dcb_state_init(netdev);
5018#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305019 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005020 }
5021
5022 pci_set_drvdata(pdev, adapter);
5023
5024 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005025 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005026 if (err)
5027 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305028 } else if (adapter->params.nports == 1) {
5029 /* If we don't have a connection to the firmware -- possibly
5030 * because of an error -- grab the raw VPD parameters so we
5031 * can set the proper MAC Address on the debug network
5032 * interface that we've created.
5033 */
5034 u8 hw_addr[ETH_ALEN];
5035 u8 *na = adapter->params.vpd.na;
5036
5037 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5038 if (!err) {
5039 for (i = 0; i < ETH_ALEN; i++)
5040 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5041 hex2val(na[2 * i + 1]));
5042 t4_set_hw_addr(adapter, 0, hw_addr);
5043 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005044 }
5045
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305046 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005047 * soon as the first register_netdev completes.
5048 */
5049 cfg_queues(adapter);
5050
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305051 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005052 if (!adapter->l2t) {
5053 /* We tolerate a lack of L2T, giving up some functionality */
5054 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5055 adapter->params.offload = 0;
5056 }
5057
Anish Bhattb5a02f52015-01-14 15:17:34 -08005058#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305059 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5060 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5061 /* CLIP functionality is not present in hardware,
5062 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005063 */
5064 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305065 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005066 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305067 } else {
5068 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5069 adapter->clipt_end);
5070 if (!adapter->clipt) {
5071 /* We tolerate a lack of clip_table, giving up
5072 * some functionality
5073 */
5074 dev_warn(&pdev->dev,
5075 "could not allocate Clip table, continuing\n");
5076 adapter->params.offload = 0;
5077 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005078 }
5079#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305080
5081 for_each_port(adapter, i) {
5082 pi = adap2pinfo(adapter, i);
5083 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5084 if (!pi->sched_tbl)
5085 dev_warn(&pdev->dev,
5086 "could not activate scheduling on port %d\n",
5087 i);
5088 }
5089
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005090 if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
5091 dev_warn(&pdev->dev, "could not allocate TID table, "
5092 "continuing\n");
5093 adapter->params.offload = 0;
5094 }
5095
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305096 if (is_offload(adapter)) {
5097 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5098 u32 hash_base, hash_reg;
5099
5100 if (chip <= CHELSIO_T5) {
5101 hash_reg = LE_DB_TID_HASHBASE_A;
5102 hash_base = t4_read_reg(adapter, hash_reg);
5103 adapter->tids.hash_base = hash_base / 4;
5104 } else {
5105 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5106 hash_base = t4_read_reg(adapter, hash_reg);
5107 adapter->tids.hash_base = hash_base;
5108 }
5109 }
5110 }
5111
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005112 /* See what interrupts we'll be using */
5113 if (msi > 1 && enable_msix(adapter) == 0)
5114 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305115 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005116 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305117 if (msi > 1)
5118 free_msix_info(adapter);
5119 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005120
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305121 /* check for PCI Express bandwidth capabiltites */
5122 cxgb4_check_pcie_caps(adapter);
5123
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005124 err = init_rss(adapter);
5125 if (err)
5126 goto out_free_dev;
5127
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005128 /*
5129 * The card is now ready to go. If any errors occur during device
5130 * registration we do not fail the whole card but rather proceed only
5131 * with the ports we manage to register successfully. However we must
5132 * register at least one net device.
5133 */
5134 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005135 pi = adap2pinfo(adapter, i);
5136 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5137 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5138
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005139 err = register_netdev(adapter->port[i]);
5140 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005141 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005142 adapter->chan_map[pi->tx_chan] = i;
5143 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005144 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005145 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005146 dev_err(&pdev->dev, "could not register any net devices\n");
5147 goto out_free_dev;
5148 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005149 if (err) {
5150 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5151 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005152 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005153
5154 if (cxgb4_debugfs_root) {
5155 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5156 cxgb4_debugfs_root);
5157 setup_debugfs(adapter);
5158 }
5159
David S. Miller88c51002011-10-07 13:38:43 -04005160 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5161 pdev->needs_freset = 1;
5162
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305163 if (is_uld(adapter)) {
5164 mutex_lock(&uld_mutex);
5165 list_add_tail(&adapter->list_node, &adapter_list);
5166 mutex_unlock(&uld_mutex);
5167 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005168
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305169 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305170 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305171 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305172
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305173sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005174#ifdef CONFIG_PCI_IOV
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305175 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0) {
5176 dev_warn(&pdev->dev,
5177 "Enabling SR-IOV VFs using the num_vf module "
5178 "parameter is deprecated - please use the pci sysfs "
5179 "interface instead.\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005180 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
5181 dev_info(&pdev->dev,
5182 "instantiated %u virtual functions\n",
5183 num_vf[func]);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305184 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05305185
5186 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5187 if (!adapter) {
5188 err = -ENOMEM;
5189 goto free_pci_region;
5190 }
5191
Hariprasad Shenai78294512016-08-11 21:06:23 +05305192 adapter->pdev = pdev;
5193 adapter->pdev_dev = &pdev->dev;
5194 adapter->name = pci_name(pdev);
5195 adapter->mbox = func;
5196 adapter->pf = func;
5197 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305198 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305199 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5200 (sizeof(struct mbox_cmd) *
5201 T4_OS_LOG_MBOX_CMDS),
5202 GFP_KERNEL);
5203 if (!adapter->mbox_log) {
5204 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305205 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305206 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05305207 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005208 return 0;
5209
Hariprasad Shenai78294512016-08-11 21:06:23 +05305210 free_adapter:
5211 kfree(adapter);
5212 free_pci_region:
5213 iounmap(regs);
5214 pci_disable_sriov(pdev);
5215 pci_release_regions(pdev);
5216 return err;
5217#else
5218 return 0;
5219#endif
5220
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005221 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005222 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305223 if (adapter->flags & USING_MSIX)
5224 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305225 if (adapter->num_uld || adapter->num_ofld_uld)
5226 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005227 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305228 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005229 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005230 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005231 if (adapter->workq)
5232 destroy_workqueue(adapter->workq);
5233
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305234 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005235 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305236 out_unmap_bar0:
5237 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005238 out_disable_device:
5239 pci_disable_pcie_error_reporting(pdev);
5240 pci_disable_device(pdev);
5241 out_release_regions:
5242 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005243 return err;
5244}
5245
Bill Pemberton91744942012-12-03 09:23:02 -05005246static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005247{
5248 struct adapter *adapter = pci_get_drvdata(pdev);
5249
Hariprasad Shenai78294512016-08-11 21:06:23 +05305250 if (!adapter) {
5251 pci_release_regions(pdev);
5252 return;
5253 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005254
Hariprasad Shenai78294512016-08-11 21:06:23 +05305255 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005256 int i;
5257
Anish Bhatt29aaee62014-08-20 13:44:06 -07005258 /* Tear down per-adapter Work Queue first since it can contain
5259 * references to our adapter data structure.
5260 */
5261 destroy_workqueue(adapter->workq);
5262
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305263 if (is_uld(adapter))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005264 detach_ulds(adapter);
5265
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305266 disable_interrupts(adapter);
5267
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005268 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005269 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005270 unregister_netdev(adapter->port[i]);
5271
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005272 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005273
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005274 /* If we allocated filters, free up state associated with any
5275 * valid filters ...
5276 */
5277 if (adapter->tids.ftid_tab) {
5278 struct filter_entry *f = &adapter->tids.ftid_tab[0];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00005279 for (i = 0; i < (adapter->tids.nftids +
5280 adapter->tids.nsftids); i++, f++)
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005281 if (f->valid)
5282 clear_filter(adapter, f);
5283 }
5284
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005285 if (adapter->flags & FULL_INIT_DONE)
5286 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005287
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305288 if (adapter->flags & USING_MSIX)
5289 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305290 if (adapter->num_uld || adapter->num_ofld_uld)
5291 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005292 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005293#if IS_ENABLED(CONFIG_IPV6)
5294 t4_cleanup_clip_tbl(adapter);
5295#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005296 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305297 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005298 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005299 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005300 if ((adapter->flags & DEV_ENABLED)) {
5301 pci_disable_device(pdev);
5302 adapter->flags &= ~DEV_ENABLED;
5303 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005304 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305305 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005306 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005307 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305308 }
5309#ifdef CONFIG_PCI_IOV
5310 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305311 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305312 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305313 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305314 kfree(adapter->vfinfo);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305315 kfree(adapter);
5316 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005317 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305318 }
5319#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005320}
5321
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305322/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5323 * delivery. This is essentially a stripped down version of the PCI remove()
5324 * function where we do the minimal amount of work necessary to shutdown any
5325 * further activity.
5326 */
5327static void shutdown_one(struct pci_dev *pdev)
5328{
5329 struct adapter *adapter = pci_get_drvdata(pdev);
5330
5331 /* As with remove_one() above (see extended comment), we only want do
5332 * do cleanup on PCI Devices which went all the way through init_one()
5333 * ...
5334 */
5335 if (!adapter) {
5336 pci_release_regions(pdev);
5337 return;
5338 }
5339
5340 if (adapter->pf == 4) {
5341 int i;
5342
5343 for_each_port(adapter, i)
5344 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5345 cxgb_close(adapter->port[i]);
5346
5347 t4_uld_clean_up(adapter);
5348 disable_interrupts(adapter);
5349 disable_msi(adapter);
5350
5351 t4_sge_stop(adapter);
5352 if (adapter->flags & FW_OK)
5353 t4_fw_bye(adapter, adapter->mbox);
5354 }
5355#ifdef CONFIG_PCI_IOV
5356 else {
5357 if (adapter->port[0])
5358 unregister_netdev(adapter->port[0]);
5359 iounmap(adapter->regs);
5360 kfree(adapter->vfinfo);
5361 kfree(adapter);
5362 pci_disable_sriov(pdev);
5363 pci_release_regions(pdev);
5364 }
5365#endif
5366}
5367
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005368static struct pci_driver cxgb4_driver = {
5369 .name = KBUILD_MODNAME,
5370 .id_table = cxgb4_pci_tbl,
5371 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005372 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305373 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305374#ifdef CONFIG_PCI_IOV
5375 .sriov_configure = cxgb4_iov_configure,
5376#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005377 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005378};
5379
5380static int __init cxgb4_init_module(void)
5381{
5382 int ret;
5383
5384 /* Debugfs support is optional, just warn if this fails */
5385 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5386 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005387 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005388
5389 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005390 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005391 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305392
Anish Bhatt1bb60372014-10-14 20:07:22 -07005393#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005394 if (!inet6addr_registered) {
5395 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5396 inet6addr_registered = true;
5397 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005398#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305399
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005400 return ret;
5401}
5402
5403static void __exit cxgb4_cleanup_module(void)
5404{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005405#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305406 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005407 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5408 inet6addr_registered = false;
5409 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005410#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005411 pci_unregister_driver(&cxgb4_driver);
5412 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005413}
5414
5415module_init(cxgb4_init_module);
5416module_exit(cxgb4_cleanup_module);