blob: 19dc9e25aa72d20e0050f5b3f2080e9704647f71 [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"
Rahul Lakkireddyd57fd6c2016-09-20 17:13:06 +053070#include "cxgb4_filter.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000071#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053072#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000073#include "t4_msg.h"
74#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053075#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070076#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053077#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080078#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000079#include "l2t.h"
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +053080#include "sched.h"
Rahul Lakkireddyd8931842016-09-20 17:13:09 +053081#include "cxgb4_tc_u32.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000082
Hariprasad Shenai812034f2015-04-06 20:23:23 +053083char cxgb4_driver_name[] = KBUILD_MODNAME;
84
Vipul Pandya01bcca62013-07-04 16:10:46 +053085#ifdef DRV_VERSION
86#undef DRV_VERSION
87#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000088#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053089const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053090#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000091
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000092#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
93 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
94 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
95
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +053096/* Macros needed to support the PCI Device ID Table ...
97 */
98#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +053099 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530100#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000101
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530102/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
103 * called for both.
104 */
105#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
106
107#define CH_PCI_ID_TABLE_ENTRY(devid) \
108 {PCI_VDEVICE(CHELSIO, (devid)), 4}
109
110#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
111 { 0, } \
112 }
113
114#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000115
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530116#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000117#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530118#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530119#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000120#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530121#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530122#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
123#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
124#define PHY_AQ1202_DEVICEID 0x4409
125#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000126
127MODULE_DESCRIPTION(DRV_DESC);
128MODULE_AUTHOR("Chelsio Communications");
129MODULE_LICENSE("Dual BSD/GPL");
130MODULE_VERSION(DRV_VERSION);
131MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530132MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000133MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530134MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000135
Vipul Pandya636f9d32012-09-26 02:39:39 +0000136/*
137 * Normally we're willing to become the firmware's Master PF but will be happy
138 * if another PF has already become the Master and initialized the adapter.
139 * Setting "force_init" will cause this driver to forcibly establish itself as
140 * the Master PF and initialize the adapter.
141 */
142static uint force_init;
143
144module_param(force_init, uint, 0644);
Hariprasad Shenaid7d3e252015-12-24 16:24:53 +0530145MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
146 "deprecated parameter");
Vipul Pandya13ee15d2012-09-26 02:39:40 +0000147
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000148static int dflt_msg_enable = DFLT_MSG_ENABLE;
149
150module_param(dflt_msg_enable, int, 0644);
Hariprasad Shenai8a21ec42016-04-05 09:52:21 +0530151MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
152 "deprecated parameter");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000153
154/*
155 * The driver uses the best interrupt scheme available on a platform in the
156 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
157 * of these schemes the driver may consider as follows:
158 *
159 * msi = 2: choose from among all three options
160 * msi = 1: only consider MSI and INTx interrupts
161 * msi = 0: force INTx interrupts
162 */
163static int msi = 2;
164
165module_param(msi, int, 0644);
166MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
167
168/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000169 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
170 * offset by 2 bytes in order to have the IP headers line up on 4-byte
171 * boundaries. This is a requirement for many architectures which will throw
172 * a machine check fault if an attempt is made to access one of the 4-byte IP
173 * header fields on a non-4-byte boundary. And it's a major performance issue
174 * even on some architectures which allow it like some implementations of the
175 * x86 ISA. However, some architectures don't mind this and for some very
176 * edge-case performance sensitive applications (like forwarding large volumes
177 * of small packets), setting this DMA offset to 0 will decrease the number of
178 * PCI-E Bus transfers enough to measurably affect performance.
179 */
180static int rx_dma_offset = 2;
181
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000182#ifdef CONFIG_PCI_IOV
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000183/* Configure the number of PCI-E Virtual Function which are to be instantiated
184 * on SR-IOV Capable Physical Functions.
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000185 */
Santosh Rastapur7d6727c2013-03-14 05:08:56 +0000186static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000187
188module_param_array(num_vf, uint, NULL, 0644);
Hariprasad Shenaib6244202016-06-14 14:39:31 +0530189MODULE_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 +0000190#endif
191
Anish Bhatt688848b2014-06-19 21:37:13 -0700192/* TX Queue select used to determine what algorithm to use for selecting TX
193 * queue. Select between the kernel provided function (select_queue=0) or user
194 * cxgb_select_queue function (select_queue=1)
195 *
196 * Default: select_queue=0
197 */
198static int select_queue;
199module_param(select_queue, int, 0644);
200MODULE_PARM_DESC(select_queue,
201 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
202
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000203static struct dentry *cxgb4_debugfs_root;
204
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530205LIST_HEAD(adapter_list);
206DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000207
208static void link_report(struct net_device *dev)
209{
210 if (!netif_carrier_ok(dev))
211 netdev_info(dev, "link down\n");
212 else {
213 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
214
Hariprasad Shenai85412252015-10-01 13:48:48 +0530215 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000216 const struct port_info *p = netdev_priv(dev);
217
218 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000219 case 10000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000220 s = "10Gbps";
221 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000222 case 1000:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000223 s = "1000Mbps";
224 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000225 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000226 s = "100Mbps";
227 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000228 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530229 s = "40Gbps";
230 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530231 default:
232 pr_info("%s: unsupported speed: %d\n",
233 dev->name, p->link_cfg.speed);
234 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000235 }
236
237 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
238 fc[p->link_cfg.fc]);
239 }
240}
241
Anish Bhatt688848b2014-06-19 21:37:13 -0700242#ifdef CONFIG_CHELSIO_T4_DCB
243/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
244static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
245{
246 struct port_info *pi = netdev_priv(dev);
247 struct adapter *adap = pi->adapter;
248 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
249 int i;
250
251 /* We use a simple mapping of Port TX Queue Index to DCB
252 * Priority when we're enabling DCB.
253 */
254 for (i = 0; i < pi->nqsets; i++, txq++) {
255 u32 name, value;
256 int err;
257
Hariprasad Shenai51678652014-11-21 12:52:02 +0530258 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
259 FW_PARAMS_PARAM_X_V(
260 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
261 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700262 value = enable ? i : 0xffffffff;
263
264 /* Since we can be called while atomic (from "interrupt
265 * level") we need to issue the Set Parameters Commannd
266 * without sleeping (timeout < 0).
267 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530268 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530269 &name, &value,
270 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700271
272 if (err)
273 dev_err(adap->pdev_dev,
274 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
275 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700276 else
277 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700278 }
279}
Anish Bhatt688848b2014-06-19 21:37:13 -0700280
Baoyou Xie50935852016-09-25 14:10:09 +0800281static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530282{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530283 struct port_info *pi = netdev_priv(dev);
284
285 if (!pi->dcb.enabled)
286 return 0;
287
288 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
289 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530290}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200291#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530292
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000293void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
294{
295 struct net_device *dev = adapter->port[port_id];
296
297 /* Skip changes from disabled ports. */
298 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
299 if (link_stat)
300 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700301 else {
302#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530303 if (cxgb4_dcb_enabled(dev)) {
304 cxgb4_dcb_state_init(dev);
305 dcb_tx_queue_prio_enable(dev, false);
306 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700307#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000308 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700309 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000310
311 link_report(dev);
312 }
313}
314
315void t4_os_portmod_changed(const struct adapter *adap, int port_id)
316{
317 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000318 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000319 };
320
321 const struct net_device *dev = adap->port[port_id];
322 const struct port_info *pi = netdev_priv(dev);
323
324 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
325 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000326 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000327 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530328 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
329 netdev_info(dev, "%s: unsupported port module inserted\n",
330 dev->name);
331 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
332 netdev_info(dev, "%s: unknown port module inserted\n",
333 dev->name);
334 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
335 netdev_info(dev, "%s: transceiver module error\n", dev->name);
336 else
337 netdev_info(dev, "%s: unknown module type %d inserted\n",
338 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000339}
340
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530341int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
342module_param(dbfifo_int_thresh, int, 0644);
343MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
344
Vipul Pandya404d9e32012-10-08 02:59:43 +0000345/*
346 * usecs to sleep while draining the dbfifo
347 */
348static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530349module_param(dbfifo_drain_delay, int, 0644);
350MODULE_PARM_DESC(dbfifo_drain_delay,
351 "usecs to sleep while draining the dbfifo");
352
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530353static inline int cxgb4_set_addr_hash(struct port_info *pi)
354{
355 struct adapter *adap = pi->adapter;
356 u64 vec = 0;
357 bool ucast = false;
358 struct hash_mac_addr *entry;
359
360 /* Calculate the hash vector for the updated list and program it */
361 list_for_each_entry(entry, &adap->mac_hlist, list) {
362 ucast |= is_unicast_ether_addr(entry->addr);
363 vec |= (1ULL << hash_mac_addr(entry->addr));
364 }
365 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
366 vec, false);
367}
368
369static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
370{
371 struct port_info *pi = netdev_priv(netdev);
372 struct adapter *adap = pi->adapter;
373 int ret;
374 u64 mhash = 0;
375 u64 uhash = 0;
376 bool free = false;
377 bool ucast = is_unicast_ether_addr(mac_addr);
378 const u8 *maclist[1] = {mac_addr};
379 struct hash_mac_addr *new_entry;
380
381 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
382 NULL, ucast ? &uhash : &mhash, false);
383 if (ret < 0)
384 goto out;
385 /* if hash != 0, then add the addr to hash addr list
386 * so on the end we will calculate the hash for the
387 * list and program it
388 */
389 if (uhash || mhash) {
390 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
391 if (!new_entry)
392 return -ENOMEM;
393 ether_addr_copy(new_entry->addr, mac_addr);
394 list_add_tail(&new_entry->list, &adap->mac_hlist);
395 ret = cxgb4_set_addr_hash(pi);
396 }
397out:
398 return ret < 0 ? ret : 0;
399}
400
401static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
402{
403 struct port_info *pi = netdev_priv(netdev);
404 struct adapter *adap = pi->adapter;
405 int ret;
406 const u8 *maclist[1] = {mac_addr};
407 struct hash_mac_addr *entry, *tmp;
408
409 /* If the MAC address to be removed is in the hash addr
410 * list, delete it from the list and update hash vector
411 */
412 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
413 if (ether_addr_equal(entry->addr, mac_addr)) {
414 list_del(&entry->list);
415 kfree(entry);
416 return cxgb4_set_addr_hash(pi);
417 }
418 }
419
420 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
421 return ret < 0 ? -EINVAL : 0;
422}
423
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000424/*
425 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
426 * If @mtu is -1 it is left unchanged.
427 */
428static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
429{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000430 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530431 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000432
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530433 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
434 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530435
436 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
437 (dev->flags & IFF_PROMISC) ? 1 : 0,
438 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
439 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000440}
441
442/**
443 * link_start - enable a port
444 * @dev: the port to enable
445 *
446 * Performs the MAC and PHY actions needed to enable a port.
447 */
448static int link_start(struct net_device *dev)
449{
450 int ret;
451 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530452 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000453
454 /*
455 * We do not set address filters and promiscuity here, the stack does
456 * that step explicitly.
457 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000458 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000459 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000460 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000461 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000462 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000463 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000464 if (ret >= 0) {
465 pi->xact_addr_filt = ret;
466 ret = 0;
467 }
468 }
469 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530470 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000471 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700472 if (ret == 0) {
473 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700474 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
475 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700476 local_bh_enable();
477 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700478
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000479 return ret;
480}
481
Anish Bhatt688848b2014-06-19 21:37:13 -0700482#ifdef CONFIG_CHELSIO_T4_DCB
483/* Handle a Data Center Bridging update message from the firmware. */
484static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
485{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530486 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530487 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700488 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
489 int new_dcb_enabled;
490
491 cxgb4_dcb_handle_fw_update(adap, pcmd);
492 new_dcb_enabled = cxgb4_dcb_enabled(dev);
493
494 /* If the DCB has become enabled or disabled on the port then we're
495 * going to need to set up/tear down DCB Priority parameters for the
496 * TX Queues associated with the port.
497 */
498 if (new_dcb_enabled != old_dcb_enabled)
499 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
500}
501#endif /* CONFIG_CHELSIO_T4_DCB */
502
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000503/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000504 */
505static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
506 const struct pkt_gl *gl)
507{
508 u8 opcode = ((const struct rss_header *)rsp)->opcode;
509
510 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000511
512 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
513 */
514 if (unlikely(opcode == CPL_FW4_MSG &&
515 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
516 rsp++;
517 opcode = ((const struct rss_header *)rsp)->opcode;
518 rsp++;
519 if (opcode != CPL_SGE_EGR_UPDATE) {
520 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
521 , opcode);
522 goto out;
523 }
524 }
525
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000526 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
527 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800528 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000529 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000530
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000531 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000532 txq->restarts++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000533 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000534 struct sge_eth_txq *eq;
535
536 eq = container_of(txq, struct sge_eth_txq, q);
537 netif_tx_wake_queue(eq->txq);
538 } else {
539 struct sge_ofld_txq *oq;
540
541 oq = container_of(txq, struct sge_ofld_txq, q);
542 tasklet_schedule(&oq->qresume_tsk);
543 }
544 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
545 const struct cpl_fw6_msg *p = (void *)rsp;
546
Anish Bhatt688848b2014-06-19 21:37:13 -0700547#ifdef CONFIG_CHELSIO_T4_DCB
548 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530549 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700550 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530551 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700552
553 if (cmd == FW_PORT_CMD &&
554 action == FW_PORT_ACTION_GET_PORT_INFO) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530555 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700556 be32_to_cpu(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530557 struct net_device *dev =
558 q->adap->port[q->adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700559 int state_input = ((pcmd->u.info.dcbxdis_pkd &
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530560 FW_PORT_CMD_DCBXDIS_F)
Anish Bhatt688848b2014-06-19 21:37:13 -0700561 ? CXGB4_DCB_INPUT_FW_DISABLED
562 : CXGB4_DCB_INPUT_FW_ENABLED);
563
564 cxgb4_dcb_state_fsm(dev, state_input);
565 }
566
567 if (cmd == FW_PORT_CMD &&
568 action == FW_PORT_ACTION_L2_DCB_CFG)
569 dcb_rpl(q->adap, pcmd);
570 else
571#endif
572 if (p->type == 0)
573 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000574 } else if (opcode == CPL_L2T_WRITE_RPL) {
575 const struct cpl_l2t_write_rpl *p = (void *)rsp;
576
577 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000578 } else if (opcode == CPL_SET_TCB_RPL) {
579 const struct cpl_set_tcb_rpl *p = (void *)rsp;
580
581 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000582 } else
583 dev_err(q->adap->pdev_dev,
584 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000585out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000586 return 0;
587}
588
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000589static void disable_msi(struct adapter *adapter)
590{
591 if (adapter->flags & USING_MSIX) {
592 pci_disable_msix(adapter->pdev);
593 adapter->flags &= ~USING_MSIX;
594 } else if (adapter->flags & USING_MSI) {
595 pci_disable_msi(adapter->pdev);
596 adapter->flags &= ~USING_MSI;
597 }
598}
599
600/*
601 * Interrupt handler for non-data events used with MSI-X.
602 */
603static irqreturn_t t4_nondata_intr(int irq, void *cookie)
604{
605 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530606 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000607
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530608 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000609 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530610 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000611 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530612 if (adap->flags & MASTER_PF)
613 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000614 return IRQ_HANDLED;
615}
616
617/*
618 * Name the MSI-X interrupts.
619 */
620static void name_msix_vecs(struct adapter *adap)
621{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000622 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000623
624 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000625 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000626
627 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000628 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
629 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000630
631 /* Ethernet queues */
632 for_each_port(adap, j) {
633 struct net_device *d = adap->port[j];
634 const struct port_info *pi = netdev_priv(d);
635
Dimitris Michailidisba278162010-12-14 21:36:50 +0000636 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000637 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
638 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000639 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000640}
641
642static int request_msix_queue_irqs(struct adapter *adap)
643{
644 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530645 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530646 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000647
648 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
649 adap->msix_info[1].desc, &s->fw_evtq);
650 if (err)
651 return err;
652
653 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000654 err = request_irq(adap->msix_info[msi_index].vec,
655 t4_sge_intr_msix, 0,
656 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000657 &s->ethrxq[ethqidx].rspq);
658 if (err)
659 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000660 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000661 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000662 return 0;
663
664unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000665 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000666 free_irq(adap->msix_info[--msi_index].vec,
667 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000668 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
669 return err;
670}
671
672static void free_msix_queue_irqs(struct adapter *adap)
673{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000674 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000675 struct sge *s = &adap->sge;
676
677 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
678 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000679 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000680}
681
682/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530683 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000684 * @pi: the port
685 * @queues: array of queue indices for RSS
686 *
687 * Sets up the portion of the HW RSS table for the port's VI to distribute
688 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530689 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000690 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530691int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000692{
693 u16 *rss;
694 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530695 struct adapter *adapter = pi->adapter;
696 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000697
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530698 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000699 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
700 if (!rss)
701 return -ENOMEM;
702
703 /* map the queue indices to queue ids */
704 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530705 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000706
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530707 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000708 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530709 /* If Tunnel All Lookup isn't specified in the global RSS
710 * Configuration, then we need to specify a default Ingress
711 * Queue for any ingress packets which aren't hashed. We'll
712 * use our first ingress queue ...
713 */
714 if (!err)
715 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
716 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
717 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
718 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
719 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
720 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
721 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000722 kfree(rss);
723 return err;
724}
725
726/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000727 * setup_rss - configure RSS
728 * @adap: the adapter
729 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000730 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000731 */
732static int setup_rss(struct adapter *adap)
733{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530734 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000735
736 for_each_port(adap, i) {
737 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000738
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530739 /* Fill default values with equal distribution */
740 for (j = 0; j < pi->rss_size; j++)
741 pi->rss[j] = j % pi->nqsets;
742
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530743 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000744 if (err)
745 return err;
746 }
747 return 0;
748}
749
750/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000751 * Return the channel of the ingress queue with the given qid.
752 */
753static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
754{
755 qid -= p->ingr_start;
756 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
757}
758
759/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000760 * Wait until all NAPI handlers are descheduled.
761 */
762static void quiesce_rx(struct adapter *adap)
763{
764 int i;
765
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530766 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000767 struct sge_rspq *q = adap->sge.ingr_map[i];
768
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530769 if (q && q->handler) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000770 napi_disable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530771 local_bh_disable();
772 while (!cxgb_poll_lock_napi(q))
773 mdelay(1);
774 local_bh_enable();
775 }
776
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000777 }
778}
779
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530780/* Disable interrupt and napi handler */
781static void disable_interrupts(struct adapter *adap)
782{
783 if (adap->flags & FULL_INIT_DONE) {
784 t4_intr_disable(adap);
785 if (adap->flags & USING_MSIX) {
786 free_msix_queue_irqs(adap);
787 free_irq(adap->msix_info[0].vec, adap);
788 } else {
789 free_irq(adap->pdev->irq, adap);
790 }
791 quiesce_rx(adap);
792 }
793}
794
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000795/*
796 * Enable NAPI scheduling and interrupt generation for all Rx queues.
797 */
798static void enable_rx(struct adapter *adap)
799{
800 int i;
801
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530802 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000803 struct sge_rspq *q = adap->sge.ingr_map[i];
804
805 if (!q)
806 continue;
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530807 if (q->handler) {
808 cxgb_busy_poll_init_lock(q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809 napi_enable(&q->napi);
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +0530810 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000811 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530812 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
813 SEINTARM_V(q->intr_params) |
814 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815 }
816}
817
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530818
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530819static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000820{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000821 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530822 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000823
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530824 bitmap_zero(s->starving_fl, s->egr_sz);
825 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000826
827 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530828 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000829 else {
830 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530831 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000832 if (err)
833 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530834 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000835 }
836
837 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530838 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530839 if (err)
840 t4_free_sge_resources(adap);
841 return err;
842}
843
844/**
845 * setup_sge_queues - configure SGE Tx/Rx/response queues
846 * @adap: the adapter
847 *
848 * Determines how many sets of SGE queues to use and initializes them.
849 * We support multiple queue sets per port if we have MSI-X, otherwise
850 * just one queue set per port.
851 */
852static int setup_sge_queues(struct adapter *adap)
853{
854 int err, i, j;
855 struct sge *s = &adap->sge;
856 struct sge_uld_rxq_info *rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
857 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000858
859 for_each_port(adap, i) {
860 struct net_device *dev = adap->port[i];
861 struct port_info *pi = netdev_priv(dev);
862 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
863 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
864
865 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530866 if (adap->msi_idx > 0)
867 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000868 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530869 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530870 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530871 NULL,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530872 t4_get_mps_bg_map(adap,
873 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000874 if (err)
875 goto freeout;
876 q->rspq.idx = j;
877 memset(&q->stats, 0, sizeof(q->stats));
878 }
879 for (j = 0; j < pi->nqsets; j++, t++) {
880 err = t4_sge_alloc_eth_txq(adap, t, dev,
881 netdev_get_tx_queue(dev, j),
882 s->fw_evtq.cntxt_id);
883 if (err)
884 goto freeout;
885 }
886 }
887
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530888 j = s->ofldqsets / adap->params.nports; /* iscsi queues per channel */
889 for_each_ofldtxq(s, i) {
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530890 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
891 adap->port[i / j],
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000892 s->fw_evtq.cntxt_id);
893 if (err)
894 goto freeout;
895 }
896
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000897 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530898 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000899 * have RDMA queues, and that's the right value.
900 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530901 if (rxq_info)
902 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
903
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000904 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530905 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000906 if (err)
907 goto freeout;
908 }
909
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530910 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530911 MPS_TRC_RSS_CONTROL_A :
912 MPS_T5_TRC_RSS_CONTROL_A,
913 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
914 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000915 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530916freeout:
917 t4_free_sge_resources(adap);
918 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000919}
920
921/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000922 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
923 * The allocated memory is cleared.
924 */
925void *t4_alloc_mem(size_t size)
926{
Joe Perches8be04b92013-06-19 12:15:53 -0700927 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000928
929 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +0000930 p = vzalloc(size);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000931 return p;
932}
933
934/*
935 * Free memory allocated through alloc_mem().
936 */
Hariprasad Shenaifd88b312014-11-07 09:35:23 +0530937void t4_free_mem(void *addr)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000938{
Pekka Enbergd2fcb542015-06-30 14:59:12 -0700939 kvfree(addr);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000940}
941
Anish Bhatt688848b2014-06-19 21:37:13 -0700942static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
943 void *accel_priv, select_queue_fallback_t fallback)
944{
945 int txq;
946
947#ifdef CONFIG_CHELSIO_T4_DCB
948 /* If a Data Center Bridging has been successfully negotiated on this
949 * link then we'll use the skb's priority to map it to a TX Queue.
950 * The skb's priority is determined via the VLAN Tag Priority Code
951 * Point field.
952 */
953 if (cxgb4_dcb_enabled(dev)) {
954 u16 vlan_tci;
955 int err;
956
957 err = vlan_get_tag(skb, &vlan_tci);
958 if (unlikely(err)) {
959 if (net_ratelimit())
960 netdev_warn(dev,
961 "TX Packet without VLAN Tag on DCB Link\n");
962 txq = 0;
963 } else {
964 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530965#ifdef CONFIG_CHELSIO_T4_FCOE
966 if (skb->protocol == htons(ETH_P_FCOE))
967 txq = skb->priority & 0x7;
968#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700969 }
970 return txq;
971 }
972#endif /* CONFIG_CHELSIO_T4_DCB */
973
974 if (select_queue) {
975 txq = (skb_rx_queue_recorded(skb)
976 ? skb_get_rx_queue(skb)
977 : smp_processor_id());
978
979 while (unlikely(txq >= dev->real_num_tx_queues))
980 txq -= dev->real_num_tx_queues;
981
982 return txq;
983 }
984
985 return fallback(dev, skb) % dev->real_num_tx_queues;
986}
987
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000988static int closest_timer(const struct sge *s, int time)
989{
990 int i, delta, match = 0, min_delta = INT_MAX;
991
992 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
993 delta = time - s->timer_val[i];
994 if (delta < 0)
995 delta = -delta;
996 if (delta < min_delta) {
997 min_delta = delta;
998 match = i;
999 }
1000 }
1001 return match;
1002}
1003
1004static int closest_thres(const struct sge *s, int thres)
1005{
1006 int i, delta, match = 0, min_delta = INT_MAX;
1007
1008 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1009 delta = thres - s->counter_val[i];
1010 if (delta < 0)
1011 delta = -delta;
1012 if (delta < min_delta) {
1013 min_delta = delta;
1014 match = i;
1015 }
1016 }
1017 return match;
1018}
1019
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001020/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301021 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001022 * @q: the Rx queue
1023 * @us: the hold-off time in us, or 0 to disable timer
1024 * @cnt: the hold-off packet count, or 0 to disable counter
1025 *
1026 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1027 * one of the two needs to be enabled for the queue to generate interrupts.
1028 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301029int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1030 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001031{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301032 struct adapter *adap = q->adap;
1033
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001034 if ((us | cnt) == 0)
1035 cnt = 1;
1036
1037 if (cnt) {
1038 int err;
1039 u32 v, new_idx;
1040
1041 new_idx = closest_thres(&adap->sge, cnt);
1042 if (q->desc && q->pktcnt_idx != new_idx) {
1043 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301044 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1045 FW_PARAMS_PARAM_X_V(
1046 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1047 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301048 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1049 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001050 if (err)
1051 return err;
1052 }
1053 q->pktcnt_idx = new_idx;
1054 }
1055
1056 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301057 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001058 return 0;
1059}
1060
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001061static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001062{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001063 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001064 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001065 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001066
Patrick McHardyf6469682013-04-19 02:04:27 +00001067 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001068 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001069
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301070 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001071 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001072 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001073 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001074 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001075 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001076}
1077
Bill Pemberton91744942012-12-03 09:23:02 -05001078static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001079{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001080 if (IS_ERR_OR_NULL(adap->debugfs_root))
1081 return -1;
1082
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301083#ifdef CONFIG_DEBUG_FS
1084 t4_setup_debugfs(adap);
1085#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001086 return 0;
1087}
1088
1089/*
1090 * upper-layer driver support
1091 */
1092
1093/*
1094 * Allocate an active-open TID and set it to the supplied value.
1095 */
1096int cxgb4_alloc_atid(struct tid_info *t, void *data)
1097{
1098 int atid = -1;
1099
1100 spin_lock_bh(&t->atid_lock);
1101 if (t->afree) {
1102 union aopen_entry *p = t->afree;
1103
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001104 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001105 t->afree = p->next;
1106 p->data = data;
1107 t->atids_in_use++;
1108 }
1109 spin_unlock_bh(&t->atid_lock);
1110 return atid;
1111}
1112EXPORT_SYMBOL(cxgb4_alloc_atid);
1113
1114/*
1115 * Release an active-open TID.
1116 */
1117void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1118{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001119 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001120
1121 spin_lock_bh(&t->atid_lock);
1122 p->next = t->afree;
1123 t->afree = p;
1124 t->atids_in_use--;
1125 spin_unlock_bh(&t->atid_lock);
1126}
1127EXPORT_SYMBOL(cxgb4_free_atid);
1128
1129/*
1130 * Allocate a server TID and set it to the supplied value.
1131 */
1132int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1133{
1134 int stid;
1135
1136 spin_lock_bh(&t->stid_lock);
1137 if (family == PF_INET) {
1138 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1139 if (stid < t->nstids)
1140 __set_bit(stid, t->stid_bmap);
1141 else
1142 stid = -1;
1143 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301144 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001145 if (stid < 0)
1146 stid = -1;
1147 }
1148 if (stid >= 0) {
1149 t->stid_tab[stid].data = data;
1150 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301151 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1152 * This is equivalent to 4 TIDs. With CLIP enabled it
1153 * needs 2 TIDs.
1154 */
1155 if (family == PF_INET)
1156 t->stids_in_use++;
1157 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301158 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001159 }
1160 spin_unlock_bh(&t->stid_lock);
1161 return stid;
1162}
1163EXPORT_SYMBOL(cxgb4_alloc_stid);
1164
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001165/* Allocate a server filter TID and set it to the supplied value.
1166 */
1167int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1168{
1169 int stid;
1170
1171 spin_lock_bh(&t->stid_lock);
1172 if (family == PF_INET) {
1173 stid = find_next_zero_bit(t->stid_bmap,
1174 t->nstids + t->nsftids, t->nstids);
1175 if (stid < (t->nstids + t->nsftids))
1176 __set_bit(stid, t->stid_bmap);
1177 else
1178 stid = -1;
1179 } else {
1180 stid = -1;
1181 }
1182 if (stid >= 0) {
1183 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301184 stid -= t->nstids;
1185 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301186 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001187 }
1188 spin_unlock_bh(&t->stid_lock);
1189 return stid;
1190}
1191EXPORT_SYMBOL(cxgb4_alloc_sftid);
1192
1193/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001194 */
1195void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1196{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301197 /* Is it a server filter TID? */
1198 if (t->nsftids && (stid >= t->sftid_base)) {
1199 stid -= t->sftid_base;
1200 stid += t->nstids;
1201 } else {
1202 stid -= t->stid_base;
1203 }
1204
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001205 spin_lock_bh(&t->stid_lock);
1206 if (family == PF_INET)
1207 __clear_bit(stid, t->stid_bmap);
1208 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301209 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001210 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301211 if (stid < t->nstids) {
1212 if (family == PF_INET)
1213 t->stids_in_use--;
1214 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301215 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301216 } else {
1217 t->sftids_in_use--;
1218 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001219 spin_unlock_bh(&t->stid_lock);
1220}
1221EXPORT_SYMBOL(cxgb4_free_stid);
1222
1223/*
1224 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1225 */
1226static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1227 unsigned int tid)
1228{
1229 struct cpl_tid_release *req;
1230
1231 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1232 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1233 INIT_TP_WR(req, tid);
1234 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1235}
1236
1237/*
1238 * Queue a TID release request and if necessary schedule a work queue to
1239 * process it.
1240 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001241static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1242 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001243{
1244 void **p = &t->tid_tab[tid];
1245 struct adapter *adap = container_of(t, struct adapter, tids);
1246
1247 spin_lock_bh(&adap->tid_release_lock);
1248 *p = adap->tid_release_head;
1249 /* Low 2 bits encode the Tx channel number */
1250 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1251 if (!adap->tid_release_task_busy) {
1252 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001253 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001254 }
1255 spin_unlock_bh(&adap->tid_release_lock);
1256}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001257
1258/*
1259 * Process the list of pending TID release requests.
1260 */
1261static void process_tid_release_list(struct work_struct *work)
1262{
1263 struct sk_buff *skb;
1264 struct adapter *adap;
1265
1266 adap = container_of(work, struct adapter, tid_release_task);
1267
1268 spin_lock_bh(&adap->tid_release_lock);
1269 while (adap->tid_release_head) {
1270 void **p = adap->tid_release_head;
1271 unsigned int chan = (uintptr_t)p & 3;
1272 p = (void *)p - chan;
1273
1274 adap->tid_release_head = *p;
1275 *p = NULL;
1276 spin_unlock_bh(&adap->tid_release_lock);
1277
1278 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1279 GFP_KERNEL)))
1280 schedule_timeout_uninterruptible(1);
1281
1282 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1283 t4_ofld_send(adap, skb);
1284 spin_lock_bh(&adap->tid_release_lock);
1285 }
1286 adap->tid_release_task_busy = false;
1287 spin_unlock_bh(&adap->tid_release_lock);
1288}
1289
1290/*
1291 * Release a TID and inform HW. If we are unable to allocate the release
1292 * message we defer to a work queue.
1293 */
1294void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1295{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001296 struct sk_buff *skb;
1297 struct adapter *adap = container_of(t, struct adapter, tids);
1298
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301299 WARN_ON(tid >= t->ntids);
1300
1301 if (t->tid_tab[tid]) {
1302 t->tid_tab[tid] = NULL;
1303 if (t->hash_base && (tid >= t->hash_base))
1304 atomic_dec(&t->hash_tids_in_use);
1305 else
1306 atomic_dec(&t->tids_in_use);
1307 }
1308
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001309 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1310 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001311 mk_tid_release(skb, chan, tid);
1312 t4_ofld_send(adap, skb);
1313 } else
1314 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001315}
1316EXPORT_SYMBOL(cxgb4_remove_tid);
1317
1318/*
1319 * Allocate and initialize the TID tables. Returns 0 on success.
1320 */
1321static int tid_init(struct tid_info *t)
1322{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301323 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301324 unsigned int max_ftids = t->nftids + t->nsftids;
1325 unsigned int natids = t->natids;
1326 unsigned int stid_bmap_size;
1327 unsigned int ftid_bmap_size;
1328 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001329
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001330 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301331 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001332 size = t->ntids * sizeof(*t->tid_tab) +
1333 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001334 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001335 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001336 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301337 max_ftids * sizeof(*t->ftid_tab) +
1338 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001339
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001340 t->tid_tab = t4_alloc_mem(size);
1341 if (!t->tid_tab)
1342 return -ENOMEM;
1343
1344 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1345 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001346 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001347 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301348 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001349 spin_lock_init(&t->stid_lock);
1350 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301351 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001352
1353 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301354 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001355 t->afree = NULL;
1356 t->atids_in_use = 0;
1357 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301358 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001359
1360 /* Setup the free list for atid_tab and clear the stid bitmap. */
1361 if (natids) {
1362 while (--natids)
1363 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1364 t->afree = t->atid_tab;
1365 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301366
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301367 if (is_offload(adap)) {
1368 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1369 /* Reserve stid 0 for T4/T5 adapters */
1370 if (!t->stid_base &&
1371 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1372 __set_bit(0, t->stid_bmap);
1373 }
1374
1375 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001376 return 0;
1377}
1378
1379/**
1380 * cxgb4_create_server - create an IP server
1381 * @dev: the device
1382 * @stid: the server TID
1383 * @sip: local IP address to bind server to
1384 * @sport: the server's TCP port
1385 * @queue: queue to direct messages from this server to
1386 *
1387 * Create an IP server for the given port and address.
1388 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1389 */
1390int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001391 __be32 sip, __be16 sport, __be16 vlan,
1392 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001393{
1394 unsigned int chan;
1395 struct sk_buff *skb;
1396 struct adapter *adap;
1397 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301398 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001399
1400 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1401 if (!skb)
1402 return -ENOMEM;
1403
1404 adap = netdev2adap(dev);
1405 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1406 INIT_TP_WR(req, 0);
1407 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1408 req->local_port = sport;
1409 req->peer_port = htons(0);
1410 req->local_ip = sip;
1411 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001412 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001413 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001414 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1415 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301416 ret = t4_mgmt_tx(adap, skb);
1417 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001418}
1419EXPORT_SYMBOL(cxgb4_create_server);
1420
Vipul Pandya80f40c12013-07-04 16:10:45 +05301421/* cxgb4_create_server6 - create an IPv6 server
1422 * @dev: the device
1423 * @stid: the server TID
1424 * @sip: local IPv6 address to bind server to
1425 * @sport: the server's TCP port
1426 * @queue: queue to direct messages from this server to
1427 *
1428 * Create an IPv6 server for the given port and address.
1429 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1430 */
1431int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1432 const struct in6_addr *sip, __be16 sport,
1433 unsigned int queue)
1434{
1435 unsigned int chan;
1436 struct sk_buff *skb;
1437 struct adapter *adap;
1438 struct cpl_pass_open_req6 *req;
1439 int ret;
1440
1441 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1442 if (!skb)
1443 return -ENOMEM;
1444
1445 adap = netdev2adap(dev);
1446 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1447 INIT_TP_WR(req, 0);
1448 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1449 req->local_port = sport;
1450 req->peer_port = htons(0);
1451 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1452 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1453 req->peer_ip_hi = cpu_to_be64(0);
1454 req->peer_ip_lo = cpu_to_be64(0);
1455 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001456 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001457 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1458 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301459 ret = t4_mgmt_tx(adap, skb);
1460 return net_xmit_eval(ret);
1461}
1462EXPORT_SYMBOL(cxgb4_create_server6);
1463
1464int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1465 unsigned int queue, bool ipv6)
1466{
1467 struct sk_buff *skb;
1468 struct adapter *adap;
1469 struct cpl_close_listsvr_req *req;
1470 int ret;
1471
1472 adap = netdev2adap(dev);
1473
1474 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1475 if (!skb)
1476 return -ENOMEM;
1477
1478 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1479 INIT_TP_WR(req, 0);
1480 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001481 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1482 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301483 ret = t4_mgmt_tx(adap, skb);
1484 return net_xmit_eval(ret);
1485}
1486EXPORT_SYMBOL(cxgb4_remove_server);
1487
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001488/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001489 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1490 * @mtus: the HW MTU table
1491 * @mtu: the target MTU
1492 * @idx: index of selected entry in the MTU table
1493 *
1494 * Returns the index and the value in the HW MTU table that is closest to
1495 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1496 * table, in which case that smallest available value is selected.
1497 */
1498unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1499 unsigned int *idx)
1500{
1501 unsigned int i = 0;
1502
1503 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1504 ++i;
1505 if (idx)
1506 *idx = i;
1507 return mtus[i];
1508}
1509EXPORT_SYMBOL(cxgb4_best_mtu);
1510
1511/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301512 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1513 * @mtus: the HW MTU table
1514 * @header_size: Header Size
1515 * @data_size_max: maximum Data Segment Size
1516 * @data_size_align: desired Data Segment Size Alignment (2^N)
1517 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1518 *
1519 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1520 * MTU Table based solely on a Maximum MTU parameter, we break that
1521 * parameter up into a Header Size and Maximum Data Segment Size, and
1522 * provide a desired Data Segment Size Alignment. If we find an MTU in
1523 * the Hardware MTU Table which will result in a Data Segment Size with
1524 * the requested alignment _and_ that MTU isn't "too far" from the
1525 * closest MTU, then we'll return that rather than the closest MTU.
1526 */
1527unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1528 unsigned short header_size,
1529 unsigned short data_size_max,
1530 unsigned short data_size_align,
1531 unsigned int *mtu_idxp)
1532{
1533 unsigned short max_mtu = header_size + data_size_max;
1534 unsigned short data_size_align_mask = data_size_align - 1;
1535 int mtu_idx, aligned_mtu_idx;
1536
1537 /* Scan the MTU Table till we find an MTU which is larger than our
1538 * Maximum MTU or we reach the end of the table. Along the way,
1539 * record the last MTU found, if any, which will result in a Data
1540 * Segment Length matching the requested alignment.
1541 */
1542 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1543 unsigned short data_size = mtus[mtu_idx] - header_size;
1544
1545 /* If this MTU minus the Header Size would result in a
1546 * Data Segment Size of the desired alignment, remember it.
1547 */
1548 if ((data_size & data_size_align_mask) == 0)
1549 aligned_mtu_idx = mtu_idx;
1550
1551 /* If we're not at the end of the Hardware MTU Table and the
1552 * next element is larger than our Maximum MTU, drop out of
1553 * the loop.
1554 */
1555 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1556 break;
1557 }
1558
1559 /* If we fell out of the loop because we ran to the end of the table,
1560 * then we just have to use the last [largest] entry.
1561 */
1562 if (mtu_idx == NMTUS)
1563 mtu_idx--;
1564
1565 /* If we found an MTU which resulted in the requested Data Segment
1566 * Length alignment and that's "not far" from the largest MTU which is
1567 * less than or equal to the maximum MTU, then use that.
1568 */
1569 if (aligned_mtu_idx >= 0 &&
1570 mtu_idx - aligned_mtu_idx <= 1)
1571 mtu_idx = aligned_mtu_idx;
1572
1573 /* If the caller has passed in an MTU Index pointer, pass the
1574 * MTU Index back. Return the MTU value.
1575 */
1576 if (mtu_idxp)
1577 *mtu_idxp = mtu_idx;
1578 return mtus[mtu_idx];
1579}
1580EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1581
1582/**
Hariprasad S27999802015-09-23 17:19:26 +05301583 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1584 * @chip: chip type
1585 * @viid: VI id of the given port
1586 *
1587 * Return the SMT index for this VI.
1588 */
1589unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1590{
1591 /* In T4/T5, SMT contains 256 SMAC entries organized in
1592 * 128 rows of 2 entries each.
1593 * In T6, SMT contains 256 SMAC entries in 256 rows.
1594 * TODO: The below code needs to be updated when we add support
1595 * for 256 VFs.
1596 */
1597 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1598 return ((viid & 0x7f) << 1);
1599 else
1600 return (viid & 0x7f);
1601}
1602EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1603
1604/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001605 * cxgb4_port_chan - get the HW channel of a port
1606 * @dev: the net device for the port
1607 *
1608 * Return the HW Tx channel of the given port.
1609 */
1610unsigned int cxgb4_port_chan(const struct net_device *dev)
1611{
1612 return netdev2pinfo(dev)->tx_chan;
1613}
1614EXPORT_SYMBOL(cxgb4_port_chan);
1615
Vipul Pandya881806b2012-05-18 15:29:24 +05301616unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1617{
1618 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001619 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301620
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301621 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1622 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301623 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301624 lp_count = LP_COUNT_G(v1);
1625 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001626 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301627 lp_count = LP_COUNT_T5_G(v1);
1628 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001629 }
1630 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301631}
1632EXPORT_SYMBOL(cxgb4_dbfifo_count);
1633
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001634/**
1635 * cxgb4_port_viid - get the VI id of a port
1636 * @dev: the net device for the port
1637 *
1638 * Return the VI id of the given port.
1639 */
1640unsigned int cxgb4_port_viid(const struct net_device *dev)
1641{
1642 return netdev2pinfo(dev)->viid;
1643}
1644EXPORT_SYMBOL(cxgb4_port_viid);
1645
1646/**
1647 * cxgb4_port_idx - get the index of a port
1648 * @dev: the net device for the port
1649 *
1650 * Return the index of the given port.
1651 */
1652unsigned int cxgb4_port_idx(const struct net_device *dev)
1653{
1654 return netdev2pinfo(dev)->port_id;
1655}
1656EXPORT_SYMBOL(cxgb4_port_idx);
1657
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001658void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1659 struct tp_tcp_stats *v6)
1660{
1661 struct adapter *adap = pci_get_drvdata(pdev);
1662
1663 spin_lock(&adap->stats_lock);
1664 t4_tp_get_tcp_stats(adap, v4, v6);
1665 spin_unlock(&adap->stats_lock);
1666}
1667EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1668
1669void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1670 const unsigned int *pgsz_order)
1671{
1672 struct adapter *adap = netdev2adap(dev);
1673
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301674 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1675 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1676 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1677 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001678}
1679EXPORT_SYMBOL(cxgb4_iscsi_init);
1680
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301681int cxgb4_flush_eq_cache(struct net_device *dev)
1682{
1683 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301684
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301685 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301686}
1687EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1688
1689static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1690{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301691 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301692 __be64 indices;
1693 int ret;
1694
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301695 spin_lock(&adap->win0_lock);
1696 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1697 sizeof(indices), (__be32 *)&indices,
1698 T4_MEMORY_READ);
1699 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301700 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001701 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1702 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301703 }
1704 return ret;
1705}
1706
1707int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1708 u16 size)
1709{
1710 struct adapter *adap = netdev2adap(dev);
1711 u16 hw_pidx, hw_cidx;
1712 int ret;
1713
1714 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1715 if (ret)
1716 goto out;
1717
1718 if (pidx != hw_pidx) {
1719 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301720 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301721
1722 if (pidx >= hw_pidx)
1723 delta = pidx - hw_pidx;
1724 else
1725 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301726
1727 if (is_t4(adap->params.chip))
1728 val = PIDX_V(delta);
1729 else
1730 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301731 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301732 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1733 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301734 }
1735out:
1736 return ret;
1737}
1738EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1739
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301740int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1741{
1742 struct adapter *adap;
1743 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301744 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301745 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1746 int ret;
1747
1748 adap = netdev2adap(dev);
1749
1750 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1751
1752 /* Figure out where the offset lands in the Memory Type/Address scheme.
1753 * This code assumes that the memory is laid out starting at offset 0
1754 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1755 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1756 * MC0, and some have both MC0 and MC1.
1757 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301758 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1759 edc0_size = EDRAM0_SIZE_G(size) << 20;
1760 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1761 edc1_size = EDRAM1_SIZE_G(size) << 20;
1762 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1763 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301764
1765 edc0_end = edc0_size;
1766 edc1_end = edc0_end + edc1_size;
1767 mc0_end = edc1_end + mc0_size;
1768
1769 if (offset < edc0_end) {
1770 memtype = MEM_EDC0;
1771 memaddr = offset;
1772 } else if (offset < edc1_end) {
1773 memtype = MEM_EDC1;
1774 memaddr = offset - edc0_end;
1775 } else {
1776 if (offset < mc0_end) {
1777 memtype = MEM_MC0;
1778 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301779 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301780 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1781 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301782 mc1_end = mc0_end + mc1_size;
1783 if (offset < mc1_end) {
1784 memtype = MEM_MC1;
1785 memaddr = offset - mc0_end;
1786 } else {
1787 /* offset beyond the end of any memory */
1788 goto err;
1789 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301790 } else {
1791 /* T4/T6 only has a single memory channel */
1792 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301793 }
1794 }
1795
1796 spin_lock(&adap->win0_lock);
1797 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1798 spin_unlock(&adap->win0_lock);
1799 return ret;
1800
1801err:
1802 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1803 stag, offset);
1804 return -EINVAL;
1805}
1806EXPORT_SYMBOL(cxgb4_read_tpte);
1807
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301808u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1809{
1810 u32 hi, lo;
1811 struct adapter *adap;
1812
1813 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301814 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1815 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301816
1817 return ((u64)hi << 32) | (u64)lo;
1818}
1819EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1820
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301821int cxgb4_bar2_sge_qregs(struct net_device *dev,
1822 unsigned int qid,
1823 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301824 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301825 u64 *pbar2_qoffset,
1826 unsigned int *pbar2_qid)
1827{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301828 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301829 qid,
1830 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1831 ? T4_BAR2_QTYPE_EGRESS
1832 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301833 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301834 pbar2_qoffset,
1835 pbar2_qid);
1836}
1837EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1838
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001839static struct pci_driver cxgb4_driver;
1840
1841static void check_neigh_update(struct neighbour *neigh)
1842{
1843 const struct device *parent;
1844 const struct net_device *netdev = neigh->dev;
1845
1846 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1847 netdev = vlan_dev_real_dev(netdev);
1848 parent = netdev->dev.parent;
1849 if (parent && parent->driver == &cxgb4_driver.driver)
1850 t4_l2t_update(dev_get_drvdata(parent), neigh);
1851}
1852
1853static int netevent_cb(struct notifier_block *nb, unsigned long event,
1854 void *data)
1855{
1856 switch (event) {
1857 case NETEVENT_NEIGH_UPDATE:
1858 check_neigh_update(data);
1859 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001860 case NETEVENT_REDIRECT:
1861 default:
1862 break;
1863 }
1864 return 0;
1865}
1866
1867static bool netevent_registered;
1868static struct notifier_block cxgb4_netevent_nb = {
1869 .notifier_call = netevent_cb
1870};
1871
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301872static void drain_db_fifo(struct adapter *adap, int usecs)
1873{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001874 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301875
1876 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301877 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1878 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301879 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301880 lp_count = LP_COUNT_G(v1);
1881 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001882 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301883 lp_count = LP_COUNT_T5_G(v1);
1884 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001885 }
1886
1887 if (lp_count == 0 && hp_count == 0)
1888 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301889 set_current_state(TASK_UNINTERRUPTIBLE);
1890 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301891 } while (1);
1892}
1893
1894static void disable_txq_db(struct sge_txq *q)
1895{
Steve Wise05eb2382014-03-14 21:52:08 +05301896 unsigned long flags;
1897
1898 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301899 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301900 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301901}
1902
Steve Wise05eb2382014-03-14 21:52:08 +05301903static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301904{
1905 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301906 if (q->db_pidx_inc) {
1907 /* Make sure that all writes to the TX descriptors
1908 * are committed before we tell HW about them.
1909 */
1910 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301911 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1912 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301913 q->db_pidx_inc = 0;
1914 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301915 q->db_disabled = 0;
1916 spin_unlock_irq(&q->db_lock);
1917}
1918
1919static void disable_dbs(struct adapter *adap)
1920{
1921 int i;
1922
1923 for_each_ethrxq(&adap->sge, i)
1924 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301925 for_each_ofldtxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301926 disable_txq_db(&adap->sge.ofldtxq[i].q);
1927 for_each_port(adap, i)
1928 disable_txq_db(&adap->sge.ctrlq[i].q);
1929}
1930
1931static void enable_dbs(struct adapter *adap)
1932{
1933 int i;
1934
1935 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301936 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301937 for_each_ofldtxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301938 enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301939 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301940 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1941}
1942
1943static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1944{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301945 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1946
1947 if (adap->uld && adap->uld[type].handle)
1948 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301949}
1950
1951static void process_db_full(struct work_struct *work)
1952{
1953 struct adapter *adap;
1954
1955 adap = container_of(work, struct adapter, db_full_task);
1956
1957 drain_db_fifo(adap, dbfifo_drain_delay);
1958 enable_dbs(adap);
1959 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301960 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1961 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1962 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1963 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1964 else
1965 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1966 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301967}
1968
1969static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1970{
1971 u16 hw_pidx, hw_cidx;
1972 int ret;
1973
Steve Wise05eb2382014-03-14 21:52:08 +05301974 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301975 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1976 if (ret)
1977 goto out;
1978 if (q->db_pidx != hw_pidx) {
1979 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301980 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301981
1982 if (q->db_pidx >= hw_pidx)
1983 delta = q->db_pidx - hw_pidx;
1984 else
1985 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301986
1987 if (is_t4(adap->params.chip))
1988 val = PIDX_V(delta);
1989 else
1990 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301991 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301992 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1993 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301994 }
1995out:
1996 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05301997 q->db_pidx_inc = 0;
1998 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301999 if (ret)
2000 CH_WARN(adap, "DB drop recovery failed.\n");
2001}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302002
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302003static void recover_all_queues(struct adapter *adap)
2004{
2005 int i;
2006
2007 for_each_ethrxq(&adap->sge, i)
2008 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302009 for_each_ofldtxq(&adap->sge, i)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302010 sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
2011 for_each_port(adap, i)
2012 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2013}
2014
Vipul Pandya881806b2012-05-18 15:29:24 +05302015static void process_db_drop(struct work_struct *work)
2016{
2017 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302018
Vipul Pandya881806b2012-05-18 15:29:24 +05302019 adap = container_of(work, struct adapter, db_drop_task);
2020
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302021 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302022 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002023 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302024 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002025 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302026 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002027 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302028 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302029 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002030 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2031 u16 qid = (dropped_db >> 15) & 0x1ffff;
2032 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302033 u64 bar2_qoffset;
2034 unsigned int bar2_qid;
2035 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002036
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302037 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002038 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302039 if (ret)
2040 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2041 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2042 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302043 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302044 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002045
2046 /* Re-enable BAR2 WC */
2047 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2048 }
2049
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302050 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2051 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302052}
2053
2054void t4_db_full(struct adapter *adap)
2055{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302056 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302057 disable_dbs(adap);
2058 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302059 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2060 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002061 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002062 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302063}
2064
2065void t4_db_dropped(struct adapter *adap)
2066{
Steve Wise05eb2382014-03-14 21:52:08 +05302067 if (is_t4(adap->params.chip)) {
2068 disable_dbs(adap);
2069 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2070 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002071 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302072}
2073
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302074void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002075{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002076 if (!netevent_registered) {
2077 register_netevent_notifier(&cxgb4_netevent_nb);
2078 netevent_registered = true;
2079 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002080}
2081
2082static void detach_ulds(struct adapter *adap)
2083{
2084 unsigned int i;
2085
2086 mutex_lock(&uld_mutex);
2087 list_del(&adap->list_node);
2088 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302089 if (adap->uld && adap->uld[i].handle) {
2090 adap->uld[i].state_change(adap->uld[i].handle,
2091 CXGB4_STATE_DETACH);
2092 adap->uld[i].handle = NULL;
2093 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002094 if (netevent_registered && list_empty(&adapter_list)) {
2095 unregister_netevent_notifier(&cxgb4_netevent_nb);
2096 netevent_registered = false;
2097 }
2098 mutex_unlock(&uld_mutex);
2099}
2100
2101static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2102{
2103 unsigned int i;
2104
2105 mutex_lock(&uld_mutex);
2106 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302107 if (adap->uld && adap->uld[i].handle)
2108 adap->uld[i].state_change(adap->uld[i].handle,
2109 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002110 mutex_unlock(&uld_mutex);
2111}
2112
Anish Bhatt1bb60372014-10-14 20:07:22 -07002113#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002114static int cxgb4_inet6addr_handler(struct notifier_block *this,
2115 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302116{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002117 struct inet6_ifaddr *ifa = data;
2118 struct net_device *event_dev = ifa->idev->dev;
2119 const struct device *parent = NULL;
2120#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302121 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002122#endif
2123 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2124 event_dev = vlan_dev_real_dev(event_dev);
2125#if IS_ENABLED(CONFIG_BONDING)
2126 if (event_dev->flags & IFF_MASTER) {
2127 list_for_each_entry(adap, &adapter_list, list_node) {
2128 switch (event) {
2129 case NETDEV_UP:
2130 cxgb4_clip_get(adap->port[0],
2131 (const u32 *)ifa, 1);
2132 break;
2133 case NETDEV_DOWN:
2134 cxgb4_clip_release(adap->port[0],
2135 (const u32 *)ifa, 1);
2136 break;
2137 default:
2138 break;
2139 }
2140 }
2141 return NOTIFY_OK;
2142 }
2143#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302144
Anish Bhattb5a02f52015-01-14 15:17:34 -08002145 if (event_dev)
2146 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302147
Anish Bhattb5a02f52015-01-14 15:17:34 -08002148 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302149 switch (event) {
2150 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002151 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302152 break;
2153 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002154 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302155 break;
2156 default:
2157 break;
2158 }
2159 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002160 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302161}
2162
Anish Bhattb5a02f52015-01-14 15:17:34 -08002163static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302164static struct notifier_block cxgb4_inet6addr_notifier = {
2165 .notifier_call = cxgb4_inet6addr_handler
2166};
2167
Vipul Pandya01bcca62013-07-04 16:10:46 +05302168static void update_clip(const struct adapter *adap)
2169{
2170 int i;
2171 struct net_device *dev;
2172 int ret;
2173
2174 rcu_read_lock();
2175
2176 for (i = 0; i < MAX_NPORTS; i++) {
2177 dev = adap->port[i];
2178 ret = 0;
2179
2180 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002181 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302182
2183 if (ret < 0)
2184 break;
2185 }
2186 rcu_read_unlock();
2187}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002188#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302189
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002190/**
2191 * cxgb_up - enable the adapter
2192 * @adap: adapter being enabled
2193 *
2194 * Called when the first port is enabled, this function performs the
2195 * actions necessary to make an adapter operational, such as completing
2196 * the initialization of HW modules, and enabling interrupts.
2197 *
2198 * Must be called with the rtnl lock held.
2199 */
2200static int cxgb_up(struct adapter *adap)
2201{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002202 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002203
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002204 err = setup_sge_queues(adap);
2205 if (err)
2206 goto out;
2207 err = setup_rss(adap);
2208 if (err)
2209 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002210
2211 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002212 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002213 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2214 adap->msix_info[0].desc, adap);
2215 if (err)
2216 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002217 err = request_msix_queue_irqs(adap);
2218 if (err) {
2219 free_irq(adap->msix_info[0].vec, adap);
2220 goto irq_err;
2221 }
2222 } else {
2223 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2224 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002225 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002226 if (err)
2227 goto irq_err;
2228 }
2229 enable_rx(adap);
2230 t4_sge_start(adap);
2231 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002232 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002233 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002234#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302235 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002236#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302237 /* Initialize hash mac addr list*/
2238 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002239 out:
2240 return err;
2241 irq_err:
2242 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002243 freeq:
2244 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002245 goto out;
2246}
2247
2248static void cxgb_down(struct adapter *adapter)
2249{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002250 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302251 cancel_work_sync(&adapter->db_full_task);
2252 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002253 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002254 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002255
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002256 t4_sge_stop(adapter);
2257 t4_free_sge_resources(adapter);
2258 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002259}
2260
2261/*
2262 * net_device operations
2263 */
2264static int cxgb_open(struct net_device *dev)
2265{
2266 int err;
2267 struct port_info *pi = netdev_priv(dev);
2268 struct adapter *adapter = pi->adapter;
2269
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002270 netif_carrier_off(dev);
2271
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002272 if (!(adapter->flags & FULL_INIT_DONE)) {
2273 err = cxgb_up(adapter);
2274 if (err < 0)
2275 return err;
2276 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002277
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002278 err = link_start(dev);
2279 if (!err)
2280 netif_tx_start_all_queues(dev);
2281 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002282}
2283
2284static int cxgb_close(struct net_device *dev)
2285{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002286 struct port_info *pi = netdev_priv(dev);
2287 struct adapter *adapter = pi->adapter;
2288
2289 netif_tx_stop_all_queues(dev);
2290 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302291 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002292}
2293
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002294int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002295 __be32 sip, __be16 sport, __be16 vlan,
2296 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002297{
2298 int ret;
2299 struct filter_entry *f;
2300 struct adapter *adap;
2301 int i;
2302 u8 *val;
2303
2304 adap = netdev2adap(dev);
2305
Vipul Pandya1cab7752012-12-10 09:30:55 +00002306 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302307 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002308 stid += adap->tids.nftids;
2309
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002310 /* Check to make sure the filter requested is writable ...
2311 */
2312 f = &adap->tids.ftid_tab[stid];
2313 ret = writable_filter(f);
2314 if (ret)
2315 return ret;
2316
2317 /* Clear out any old resources being used by the filter before
2318 * we start constructing the new filter.
2319 */
2320 if (f->valid)
2321 clear_filter(adap, f);
2322
2323 /* Clear out filter specifications */
2324 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2325 f->fs.val.lport = cpu_to_be16(sport);
2326 f->fs.mask.lport = ~0;
2327 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002328 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002329 for (i = 0; i < 4; i++) {
2330 f->fs.val.lip[i] = val[i];
2331 f->fs.mask.lip[i] = ~0;
2332 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302333 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002334 f->fs.val.iport = port;
2335 f->fs.mask.iport = mask;
2336 }
2337 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002338
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302339 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302340 f->fs.val.proto = IPPROTO_TCP;
2341 f->fs.mask.proto = ~0;
2342 }
2343
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002344 f->fs.dirsteer = 1;
2345 f->fs.iq = queue;
2346 /* Mark filter as locked */
2347 f->locked = 1;
2348 f->fs.rpttid = 1;
2349
2350 ret = set_filter_wr(adap, stid);
2351 if (ret) {
2352 clear_filter(adap, f);
2353 return ret;
2354 }
2355
2356 return 0;
2357}
2358EXPORT_SYMBOL(cxgb4_create_server_filter);
2359
2360int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2361 unsigned int queue, bool ipv6)
2362{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002363 struct filter_entry *f;
2364 struct adapter *adap;
2365
2366 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002367
2368 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302369 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002370 stid += adap->tids.nftids;
2371
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002372 f = &adap->tids.ftid_tab[stid];
2373 /* Unlock the filter */
2374 f->locked = 0;
2375
Wei Yongjun8c148462016-08-20 15:32:41 +00002376 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002377}
2378EXPORT_SYMBOL(cxgb4_remove_server_filter);
2379
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002380static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2381 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002382{
2383 struct port_stats stats;
2384 struct port_info *p = netdev_priv(dev);
2385 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002386
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002387 /* Block retrieving statistics during EEH error
2388 * recovery. Otherwise, the recovery might fail
2389 * and the PCI device will be removed permanently
2390 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002391 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002392 if (!netif_device_present(dev)) {
2393 spin_unlock(&adapter->stats_lock);
2394 return ns;
2395 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302396 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2397 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002398 spin_unlock(&adapter->stats_lock);
2399
2400 ns->tx_bytes = stats.tx_octets;
2401 ns->tx_packets = stats.tx_frames;
2402 ns->rx_bytes = stats.rx_octets;
2403 ns->rx_packets = stats.rx_frames;
2404 ns->multicast = stats.rx_mcast_frames;
2405
2406 /* detailed rx_errors */
2407 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2408 stats.rx_runt;
2409 ns->rx_over_errors = 0;
2410 ns->rx_crc_errors = stats.rx_fcs_err;
2411 ns->rx_frame_errors = stats.rx_symbol_err;
2412 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2413 stats.rx_ovflow2 + stats.rx_ovflow3 +
2414 stats.rx_trunc0 + stats.rx_trunc1 +
2415 stats.rx_trunc2 + stats.rx_trunc3;
2416 ns->rx_missed_errors = 0;
2417
2418 /* detailed tx_errors */
2419 ns->tx_aborted_errors = 0;
2420 ns->tx_carrier_errors = 0;
2421 ns->tx_fifo_errors = 0;
2422 ns->tx_heartbeat_errors = 0;
2423 ns->tx_window_errors = 0;
2424
2425 ns->tx_errors = stats.tx_error_frames;
2426 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2427 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2428 return ns;
2429}
2430
2431static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2432{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002433 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002434 int ret = 0, prtad, devad;
2435 struct port_info *pi = netdev_priv(dev);
2436 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2437
2438 switch (cmd) {
2439 case SIOCGMIIPHY:
2440 if (pi->mdio_addr < 0)
2441 return -EOPNOTSUPP;
2442 data->phy_id = pi->mdio_addr;
2443 break;
2444 case SIOCGMIIREG:
2445 case SIOCSMIIREG:
2446 if (mdio_phy_id_is_c45(data->phy_id)) {
2447 prtad = mdio_phy_id_prtad(data->phy_id);
2448 devad = mdio_phy_id_devad(data->phy_id);
2449 } else if (data->phy_id < 32) {
2450 prtad = data->phy_id;
2451 devad = 0;
2452 data->reg_num &= 0x1f;
2453 } else
2454 return -EINVAL;
2455
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302456 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002457 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002458 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002459 data->reg_num, &data->val_out);
2460 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002461 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002462 data->reg_num, data->val_in);
2463 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302464 case SIOCGHWTSTAMP:
2465 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2466 sizeof(pi->tstamp_config)) ?
2467 -EFAULT : 0;
2468 case SIOCSHWTSTAMP:
2469 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2470 sizeof(pi->tstamp_config)))
2471 return -EFAULT;
2472
2473 switch (pi->tstamp_config.rx_filter) {
2474 case HWTSTAMP_FILTER_NONE:
2475 pi->rxtstamp = false;
2476 break;
2477 case HWTSTAMP_FILTER_ALL:
2478 pi->rxtstamp = true;
2479 break;
2480 default:
2481 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2482 return -ERANGE;
2483 }
2484
2485 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2486 sizeof(pi->tstamp_config)) ?
2487 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002488 default:
2489 return -EOPNOTSUPP;
2490 }
2491 return ret;
2492}
2493
2494static void cxgb_set_rxmode(struct net_device *dev)
2495{
2496 /* unfortunately we can't return errors to the stack */
2497 set_rxmode(dev, -1, false);
2498}
2499
2500static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2501{
2502 int ret;
2503 struct port_info *pi = netdev_priv(dev);
2504
2505 if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
2506 return -EINVAL;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302507 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002508 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002509 if (!ret)
2510 dev->mtu = new_mtu;
2511 return ret;
2512}
2513
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302514#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302515static int dummy_open(struct net_device *dev)
2516{
2517 /* Turn carrier off since we don't have to transmit anything on this
2518 * interface.
2519 */
2520 netif_carrier_off(dev);
2521 return 0;
2522}
2523
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302524/* Fill MAC address that will be assigned by the FW */
2525static void fill_vf_station_mac_addr(struct adapter *adap)
2526{
2527 unsigned int i;
2528 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2529 int err;
2530 u8 *na;
2531 u16 a, b;
2532
2533 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2534 if (!err) {
2535 na = adap->params.vpd.na;
2536 for (i = 0; i < ETH_ALEN; i++)
2537 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2538 hex2val(na[2 * i + 1]));
2539 a = (hw_addr[0] << 8) | hw_addr[1];
2540 b = (hw_addr[1] << 8) | hw_addr[2];
2541 a ^= b;
2542 a |= 0x0200; /* locally assigned Ethernet MAC address */
2543 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2544 macaddr[0] = a >> 8;
2545 macaddr[1] = a & 0xff;
2546
2547 for (i = 2; i < 5; i++)
2548 macaddr[i] = hw_addr[i + 1];
2549
2550 for (i = 0; i < adap->num_vfs; i++) {
2551 macaddr[5] = adap->pf * 16 + i;
2552 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2553 }
2554 }
2555}
2556
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302557static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2558{
2559 struct port_info *pi = netdev_priv(dev);
2560 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302561 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302562
2563 /* verify MAC addr is valid */
2564 if (!is_valid_ether_addr(mac)) {
2565 dev_err(pi->adapter->pdev_dev,
2566 "Invalid Ethernet address %pM for VF %d\n",
2567 mac, vf);
2568 return -EINVAL;
2569 }
2570
2571 dev_info(pi->adapter->pdev_dev,
2572 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302573 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2574 if (!ret)
2575 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2576 return ret;
2577}
2578
2579static int cxgb_get_vf_config(struct net_device *dev,
2580 int vf, struct ifla_vf_info *ivi)
2581{
2582 struct port_info *pi = netdev_priv(dev);
2583 struct adapter *adap = pi->adapter;
2584
2585 if (vf >= adap->num_vfs)
2586 return -EINVAL;
2587 ivi->vf = vf;
2588 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2589 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302590}
2591#endif
2592
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002593static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2594{
2595 int ret;
2596 struct sockaddr *addr = p;
2597 struct port_info *pi = netdev_priv(dev);
2598
2599 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002600 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002601
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302602 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002603 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002604 if (ret < 0)
2605 return ret;
2606
2607 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2608 pi->xact_addr_filt = ret;
2609 return 0;
2610}
2611
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002612#ifdef CONFIG_NET_POLL_CONTROLLER
2613static void cxgb_netpoll(struct net_device *dev)
2614{
2615 struct port_info *pi = netdev_priv(dev);
2616 struct adapter *adap = pi->adapter;
2617
2618 if (adap->flags & USING_MSIX) {
2619 int i;
2620 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2621
2622 for (i = pi->nqsets; i; i--, rx++)
2623 t4_sge_intr_msix(0, &rx->rspq);
2624 } else
2625 t4_intr_handler(adap)(0, adap);
2626}
2627#endif
2628
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302629static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2630{
2631 struct port_info *pi = netdev_priv(dev);
2632 struct adapter *adap = pi->adapter;
2633 struct sched_class *e;
2634 struct ch_sched_params p;
2635 struct ch_sched_queue qe;
2636 u32 req_rate;
2637 int err = 0;
2638
2639 if (!can_sched(dev))
2640 return -ENOTSUPP;
2641
2642 if (index < 0 || index > pi->nqsets - 1)
2643 return -EINVAL;
2644
2645 if (!(adap->flags & FULL_INIT_DONE)) {
2646 dev_err(adap->pdev_dev,
2647 "Failed to rate limit on queue %d. Link Down?\n",
2648 index);
2649 return -EINVAL;
2650 }
2651
2652 /* Convert from Mbps to Kbps */
2653 req_rate = rate << 10;
2654
2655 /* Max rate is 10 Gbps */
2656 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2657 dev_err(adap->pdev_dev,
2658 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2659 rate, SCHED_MAX_RATE_KBPS);
2660 return -ERANGE;
2661 }
2662
2663 /* First unbind the queue from any existing class */
2664 memset(&qe, 0, sizeof(qe));
2665 qe.queue = index;
2666 qe.class = SCHED_CLS_NONE;
2667
2668 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2669 if (err) {
2670 dev_err(adap->pdev_dev,
2671 "Unbinding Queue %d on port %d fail. Err: %d\n",
2672 index, pi->port_id, err);
2673 return err;
2674 }
2675
2676 /* Queue already unbound */
2677 if (!req_rate)
2678 return 0;
2679
2680 /* Fetch any available unused or matching scheduling class */
2681 memset(&p, 0, sizeof(p));
2682 p.type = SCHED_CLASS_TYPE_PACKET;
2683 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2684 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2685 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2686 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2687 p.u.params.channel = pi->tx_chan;
2688 p.u.params.class = SCHED_CLS_NONE;
2689 p.u.params.minrate = 0;
2690 p.u.params.maxrate = req_rate;
2691 p.u.params.weight = 0;
2692 p.u.params.pktsize = dev->mtu;
2693
2694 e = cxgb4_sched_class_alloc(dev, &p);
2695 if (!e)
2696 return -ENOMEM;
2697
2698 /* Bind the queue to a scheduling class */
2699 memset(&qe, 0, sizeof(qe));
2700 qe.queue = index;
2701 qe.class = e->idx;
2702
2703 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2704 if (err)
2705 dev_err(adap->pdev_dev,
2706 "Queue rate limiting failed. Err: %d\n", err);
2707 return err;
2708}
2709
Baoyou Xie8efebd62016-09-30 15:34:25 +08002710static int cxgb_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
2711 struct tc_to_netdev *tc)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302712{
2713 struct port_info *pi = netdev2pinfo(dev);
2714 struct adapter *adap = netdev2adap(dev);
2715
2716 if (!(adap->flags & FULL_INIT_DONE)) {
2717 dev_err(adap->pdev_dev,
2718 "Failed to setup tc on port %d. Link Down?\n",
2719 pi->port_id);
2720 return -EINVAL;
2721 }
2722
2723 if (TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS) &&
2724 tc->type == TC_SETUP_CLSU32) {
2725 switch (tc->cls_u32->command) {
2726 case TC_CLSU32_NEW_KNODE:
2727 case TC_CLSU32_REPLACE_KNODE:
2728 return cxgb4_config_knode(dev, proto, tc->cls_u32);
2729 case TC_CLSU32_DELETE_KNODE:
2730 return cxgb4_delete_knode(dev, proto, tc->cls_u32);
2731 default:
2732 return -EOPNOTSUPP;
2733 }
2734 }
2735
2736 return -EOPNOTSUPP;
2737}
2738
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002739static const struct net_device_ops cxgb4_netdev_ops = {
2740 .ndo_open = cxgb_open,
2741 .ndo_stop = cxgb_close,
2742 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002743 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002744 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002745 .ndo_set_rx_mode = cxgb_set_rxmode,
2746 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00002747 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002748 .ndo_validate_addr = eth_validate_addr,
2749 .ndo_do_ioctl = cxgb_ioctl,
2750 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002751#ifdef CONFIG_NET_POLL_CONTROLLER
2752 .ndo_poll_controller = cxgb_netpoll,
2753#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05302754#ifdef CONFIG_CHELSIO_T4_FCOE
2755 .ndo_fcoe_enable = cxgb_fcoe_enable,
2756 .ndo_fcoe_disable = cxgb_fcoe_disable,
2757#endif /* CONFIG_CHELSIO_T4_FCOE */
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302758#ifdef CONFIG_NET_RX_BUSY_POLL
2759 .ndo_busy_poll = cxgb_busy_poll,
2760#endif
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302761 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302762 .ndo_setup_tc = cxgb_setup_tc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002763};
2764
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302765#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302766static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2767 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302768 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302769 .ndo_get_vf_config = cxgb_get_vf_config,
Hariprasad Shenai78294512016-08-11 21:06:23 +05302770};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302771#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05302772
2773static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2774{
2775 struct adapter *adapter = netdev2adap(dev);
2776
2777 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2778 strlcpy(info->version, cxgb4_driver_version,
2779 sizeof(info->version));
2780 strlcpy(info->bus_info, pci_name(adapter->pdev),
2781 sizeof(info->bus_info));
2782}
2783
2784static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
2785 .get_drvinfo = get_drvinfo,
2786};
2787
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002788void t4_fatal_err(struct adapter *adap)
2789{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302790 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002791 t4_intr_disable(adap);
2792 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
2793}
2794
2795static void setup_memwin(struct adapter *adap)
2796{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05302797 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002798
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05302799 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00002800}
2801
2802static void setup_memwin_rdma(struct adapter *adap)
2803{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002804 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05302805 u32 start;
2806 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002807
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05302808 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
2809 start &= PCI_BASE_ADDRESS_MEM_MASK;
2810 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002811 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
2812 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302813 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
2814 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002815 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302816 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002817 adap->vres.ocq.start);
2818 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302819 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002820 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002821}
2822
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002823static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
2824{
2825 u32 v;
2826 int ret;
2827
2828 /* get device capabilities */
2829 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302830 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2831 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05302832 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302833 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002834 if (ret < 0)
2835 return ret;
2836
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302837 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2838 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302839 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002840 if (ret < 0)
2841 return ret;
2842
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302843 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002844 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05302845 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
2846 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002847 if (ret < 0)
2848 return ret;
2849
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302850 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302851 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
2852 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002853 if (ret < 0)
2854 return ret;
2855
2856 t4_sge_init(adap);
2857
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002858 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302859 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302860 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302861 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
2862 v = t4_read_reg(adap, TP_PIO_DATA_A);
2863 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002864
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002865 /* first 4 Tx modulation queues point to consecutive Tx channels */
2866 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302867 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
2868 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002869
2870 /* associate each Tx modulation queue with consecutive Tx channels */
2871 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302872 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302873 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302874 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302875 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302876 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302877 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002878
2879#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
2880 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302881 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
2882 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2883 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2884 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2885 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
2886 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
2887 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2888 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2889 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2890 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002891 }
2892
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002893 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302894 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002895}
2896
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002897/*
2898 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
2899 */
2900#define MAX_ATIDS 8192U
2901
2902/*
2903 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00002904 *
2905 * If the firmware we're dealing with has Configuration File support, then
2906 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002907 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00002908
2909/*
2910 * Tweak configuration based on module parameters, etc. Most of these have
2911 * defaults assigned to them by Firmware Configuration Files (if we're using
2912 * them) but need to be explicitly set if we're using hard-coded
2913 * initialization. But even in the case of using Firmware Configuration
2914 * Files, we'd like to expose the ability to change these via module
2915 * parameters so these are essentially common tweaks/settings for
2916 * Configuration Files and hard-coded initialization ...
2917 */
2918static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002919{
Vipul Pandya636f9d32012-09-26 02:39:39 +00002920 /*
2921 * Fix up various Host-Dependent Parameters like Page Size, Cache
2922 * Line Size, etc. The firmware default is for a 4KB Page Size and
2923 * 64B Cache Line Size ...
2924 */
2925 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002926
Vipul Pandya636f9d32012-09-26 02:39:39 +00002927 /*
2928 * Process module parameters which affect early initialization.
2929 */
2930 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
2931 dev_err(&adapter->pdev->dev,
2932 "Ignoring illegal rx_dma_offset=%d, using 2\n",
2933 rx_dma_offset);
2934 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002935 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302936 t4_set_reg_field(adapter, SGE_CONTROL_A,
2937 PKTSHIFT_V(PKTSHIFT_M),
2938 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002939
Vipul Pandya636f9d32012-09-26 02:39:39 +00002940 /*
2941 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
2942 * adds the pseudo header itself.
2943 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302944 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
2945 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00002946
2947 return 0;
2948}
2949
Hariprasad Shenai01b69612015-05-22 21:58:21 +05302950/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
2951 * unto themselves and they contain their own firmware to perform their
2952 * tasks ...
2953 */
2954static int phy_aq1202_version(const u8 *phy_fw_data,
2955 size_t phy_fw_size)
2956{
2957 int offset;
2958
2959 /* At offset 0x8 you're looking for the primary image's
2960 * starting offset which is 3 Bytes wide
2961 *
2962 * At offset 0xa of the primary image, you look for the offset
2963 * of the DRAM segment which is 3 Bytes wide.
2964 *
2965 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
2966 * wide
2967 */
2968 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
2969 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
2970 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
2971
2972 offset = le24(phy_fw_data + 0x8) << 12;
2973 offset = le24(phy_fw_data + offset + 0xa);
2974 return be16(phy_fw_data + offset + 0x27e);
2975
2976 #undef be16
2977 #undef le16
2978 #undef le24
2979}
2980
2981static struct info_10gbt_phy_fw {
2982 unsigned int phy_fw_id; /* PCI Device ID */
2983 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
2984 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
2985 int phy_flash; /* Has FLASH for PHY Firmware */
2986} phy_info_array[] = {
2987 {
2988 PHY_AQ1202_DEVICEID,
2989 PHY_AQ1202_FIRMWARE,
2990 phy_aq1202_version,
2991 1,
2992 },
2993 {
2994 PHY_BCM84834_DEVICEID,
2995 PHY_BCM84834_FIRMWARE,
2996 NULL,
2997 0,
2998 },
2999 { 0, NULL, NULL },
3000};
3001
3002static struct info_10gbt_phy_fw *find_phy_info(int devid)
3003{
3004 int i;
3005
3006 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3007 if (phy_info_array[i].phy_fw_id == devid)
3008 return &phy_info_array[i];
3009 }
3010 return NULL;
3011}
3012
3013/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3014 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3015 * we return a negative error number. If we transfer new firmware we return 1
3016 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3017 */
3018static int adap_init0_phy(struct adapter *adap)
3019{
3020 const struct firmware *phyf;
3021 int ret;
3022 struct info_10gbt_phy_fw *phy_info;
3023
3024 /* Use the device ID to determine which PHY file to flash.
3025 */
3026 phy_info = find_phy_info(adap->pdev->device);
3027 if (!phy_info) {
3028 dev_warn(adap->pdev_dev,
3029 "No PHY Firmware file found for this PHY\n");
3030 return -EOPNOTSUPP;
3031 }
3032
3033 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3034 * use that. The adapter firmware provides us with a memory buffer
3035 * where we can load a PHY firmware file from the host if we want to
3036 * override the PHY firmware File in flash.
3037 */
3038 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3039 adap->pdev_dev);
3040 if (ret < 0) {
3041 /* For adapters without FLASH attached to PHY for their
3042 * firmware, it's obviously a fatal error if we can't get the
3043 * firmware to the adapter. For adapters with PHY firmware
3044 * FLASH storage, it's worth a warning if we can't find the
3045 * PHY Firmware but we'll neuter the error ...
3046 */
3047 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3048 "/lib/firmware/%s, error %d\n",
3049 phy_info->phy_fw_file, -ret);
3050 if (phy_info->phy_flash) {
3051 int cur_phy_fw_ver = 0;
3052
3053 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3054 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3055 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3056 ret = 0;
3057 }
3058
3059 return ret;
3060 }
3061
3062 /* Load PHY Firmware onto adapter.
3063 */
3064 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3065 phy_info->phy_fw_version,
3066 (u8 *)phyf->data, phyf->size);
3067 if (ret < 0)
3068 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3069 -ret);
3070 else if (ret > 0) {
3071 int new_phy_fw_ver = 0;
3072
3073 if (phy_info->phy_fw_version)
3074 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3075 phyf->size);
3076 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3077 "Firmware /lib/firmware/%s, version %#x\n",
3078 phy_info->phy_fw_file, new_phy_fw_ver);
3079 }
3080
3081 release_firmware(phyf);
3082
3083 return ret;
3084}
3085
Vipul Pandya636f9d32012-09-26 02:39:39 +00003086/*
3087 * Attempt to initialize the adapter via a Firmware Configuration File.
3088 */
3089static int adap_init0_config(struct adapter *adapter, int reset)
3090{
3091 struct fw_caps_config_cmd caps_cmd;
3092 const struct firmware *cf;
3093 unsigned long mtype = 0, maddr = 0;
3094 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303095 int ret;
3096 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003097 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303098 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003099
3100 /*
3101 * Reset device if necessary.
3102 */
3103 if (reset) {
3104 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303105 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003106 if (ret < 0)
3107 goto bye;
3108 }
3109
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303110 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3111 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3112 * to be performed after any global adapter RESET above since some
3113 * PHYs only have local RAM copies of the PHY firmware.
3114 */
3115 if (is_10gbt_device(adapter->pdev->device)) {
3116 ret = adap_init0_phy(adapter);
3117 if (ret < 0)
3118 goto bye;
3119 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003120 /*
3121 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3122 * then use that. Otherwise, use the configuration file stored
3123 * in the adapter flash ...
3124 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303125 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003126 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303127 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003128 break;
3129 case CHELSIO_T5:
3130 fw_config_file = FW5_CFNAME;
3131 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303132 case CHELSIO_T6:
3133 fw_config_file = FW6_CFNAME;
3134 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003135 default:
3136 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3137 adapter->pdev->device);
3138 ret = -EINVAL;
3139 goto bye;
3140 }
3141
3142 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003143 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303144 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003145 mtype = FW_MEMTYPE_CF_FLASH;
3146 maddr = t4_flash_cfg_addr(adapter);
3147 } else {
3148 u32 params[7], val[7];
3149
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303150 sprintf(fw_config_file_path,
3151 "/lib/firmware/%s", fw_config_file);
3152 config_name = fw_config_file_path;
3153
Vipul Pandya636f9d32012-09-26 02:39:39 +00003154 if (cf->size >= FLASH_CFG_MAX_SIZE)
3155 ret = -ENOMEM;
3156 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303157 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3158 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003159 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303160 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003161 if (ret == 0) {
3162 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303163 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003164 * sizes have to be in terms of multiples of 4
3165 * bytes. So, if the Configuration File isn't
3166 * a multiple of 4 bytes in length we'll have
3167 * to write that out separately since we can't
3168 * guarantee that the bytes following the
3169 * residual byte in the buffer returned by
3170 * request_firmware() are zeroed out ...
3171 */
3172 size_t resid = cf->size & 0x3;
3173 size_t size = cf->size & ~0x3;
3174 __be32 *data = (__be32 *)cf->data;
3175
Hariprasad Shenai51678652014-11-21 12:52:02 +05303176 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3177 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003178
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303179 spin_lock(&adapter->win0_lock);
3180 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3181 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003182 if (ret == 0 && resid != 0) {
3183 union {
3184 __be32 word;
3185 char buf[4];
3186 } last;
3187 int i;
3188
3189 last.word = data[size >> 2];
3190 for (i = resid; i < 4; i++)
3191 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303192 ret = t4_memory_rw(adapter, 0, mtype,
3193 maddr + size,
3194 4, &last.word,
3195 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003196 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303197 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003198 }
3199 }
3200
3201 release_firmware(cf);
3202 if (ret)
3203 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003204 }
3205
Vipul Pandya636f9d32012-09-26 02:39:39 +00003206 /*
3207 * Issue a Capability Configuration command to the firmware to get it
3208 * to parse the Configuration File. We don't use t4_fw_config_file()
3209 * because we want the ability to modify various features after we've
3210 * processed the configuration file ...
3211 */
3212 memset(&caps_cmd, 0, sizeof(caps_cmd));
3213 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303214 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3215 FW_CMD_REQUEST_F |
3216 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303217 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303218 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3219 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3220 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003221 FW_LEN16(caps_cmd));
3222 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3223 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303224
3225 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3226 * Configuration File in FLASH), our last gasp effort is to use the
3227 * Firmware Configuration File which is embedded in the firmware. A
3228 * very few early versions of the firmware didn't have one embedded
3229 * but we can ignore those.
3230 */
3231 if (ret == -ENOENT) {
3232 memset(&caps_cmd, 0, sizeof(caps_cmd));
3233 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303234 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3235 FW_CMD_REQUEST_F |
3236 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303237 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3238 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3239 sizeof(caps_cmd), &caps_cmd);
3240 config_name = "Firmware Default";
3241 }
3242
3243 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003244 if (ret < 0)
3245 goto bye;
3246
Vipul Pandya636f9d32012-09-26 02:39:39 +00003247 finiver = ntohl(caps_cmd.finiver);
3248 finicsum = ntohl(caps_cmd.finicsum);
3249 cfcsum = ntohl(caps_cmd.cfcsum);
3250 if (finicsum != cfcsum)
3251 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3252 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3253 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003254
Vipul Pandya636f9d32012-09-26 02:39:39 +00003255 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003256 * And now tell the firmware to use the configuration we just loaded.
3257 */
3258 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303259 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3260 FW_CMD_REQUEST_F |
3261 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303262 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003263 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3264 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003265 if (ret < 0)
3266 goto bye;
3267
Vipul Pandya636f9d32012-09-26 02:39:39 +00003268 /*
3269 * Tweak configuration based on system architecture, module
3270 * parameters, etc.
3271 */
3272 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003273 if (ret < 0)
3274 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003275
Vipul Pandya636f9d32012-09-26 02:39:39 +00003276 /*
3277 * And finally tell the firmware to initialize itself using the
3278 * parameters from the Configuration File.
3279 */
3280 ret = t4_fw_initialize(adapter, adapter->mbox);
3281 if (ret < 0)
3282 goto bye;
3283
Hariprasad Shenai06640312015-01-13 15:19:25 +05303284 /* Emit Firmware Configuration File information and return
3285 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003286 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003287 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303288 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3289 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003290 return 0;
3291
3292 /*
3293 * Something bad happened. Return the error ... (If the "error"
3294 * is that there's no Configuration File on the adapter we don't
3295 * want to issue a warning since this is fairly common.)
3296 */
3297bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303298 if (config_issued && ret != -ENOENT)
3299 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3300 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003301 return ret;
3302}
3303
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303304static struct fw_info fw_info_array[] = {
3305 {
3306 .chip = CHELSIO_T4,
3307 .fs_name = FW4_CFNAME,
3308 .fw_mod_name = FW4_FNAME,
3309 .fw_hdr = {
3310 .chip = FW_HDR_CHIP_T4,
3311 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3312 .intfver_nic = FW_INTFVER(T4, NIC),
3313 .intfver_vnic = FW_INTFVER(T4, VNIC),
3314 .intfver_ri = FW_INTFVER(T4, RI),
3315 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3316 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3317 },
3318 }, {
3319 .chip = CHELSIO_T5,
3320 .fs_name = FW5_CFNAME,
3321 .fw_mod_name = FW5_FNAME,
3322 .fw_hdr = {
3323 .chip = FW_HDR_CHIP_T5,
3324 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3325 .intfver_nic = FW_INTFVER(T5, NIC),
3326 .intfver_vnic = FW_INTFVER(T5, VNIC),
3327 .intfver_ri = FW_INTFVER(T5, RI),
3328 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3329 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3330 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303331 }, {
3332 .chip = CHELSIO_T6,
3333 .fs_name = FW6_CFNAME,
3334 .fw_mod_name = FW6_FNAME,
3335 .fw_hdr = {
3336 .chip = FW_HDR_CHIP_T6,
3337 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3338 .intfver_nic = FW_INTFVER(T6, NIC),
3339 .intfver_vnic = FW_INTFVER(T6, VNIC),
3340 .intfver_ofld = FW_INTFVER(T6, OFLD),
3341 .intfver_ri = FW_INTFVER(T6, RI),
3342 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3343 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3344 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3345 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3346 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303347 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303348
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303349};
3350
3351static struct fw_info *find_fw_info(int chip)
3352{
3353 int i;
3354
3355 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3356 if (fw_info_array[i].chip == chip)
3357 return &fw_info_array[i];
3358 }
3359 return NULL;
3360}
3361
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003362/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003363 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003364 */
3365static int adap_init0(struct adapter *adap)
3366{
3367 int ret;
3368 u32 v, port_vec;
3369 enum dev_state state;
3370 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003371 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303372 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003373
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303374 /* Grab Firmware Device Log parameters as early as possible so we have
3375 * access to it for debugging, etc.
3376 */
3377 ret = t4_init_devlog_params(adap);
3378 if (ret < 0)
3379 return ret;
3380
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303381 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303382 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3383 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003384 if (ret < 0) {
3385 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3386 ret);
3387 return ret;
3388 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003389 if (ret == adap->mbox)
3390 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003391
Vipul Pandya636f9d32012-09-26 02:39:39 +00003392 /*
3393 * If we're the Master PF Driver and the device is uninitialized,
3394 * then let's consider upgrading the firmware ... (We always want
3395 * to check the firmware version number in order to A. get it for
3396 * later reporting and B. to warn if the currently loaded firmware
3397 * is excessively mismatched relative to the driver.)
3398 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303399 t4_get_fw_version(adap, &adap->params.fw_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303400 t4_get_bs_version(adap, &adap->params.bs_vers);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303401 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303402 t4_get_exprom_version(adap, &adap->params.er_vers);
3403
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303404 ret = t4_check_fw_version(adap);
3405 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303406 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303407 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003408 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303409 struct fw_info *fw_info;
3410 struct fw_hdr *card_fw;
3411 const struct firmware *fw;
3412 const u8 *fw_data = NULL;
3413 unsigned int fw_size = 0;
3414
3415 /* This is the firmware whose headers the driver was compiled
3416 * against
3417 */
3418 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3419 if (fw_info == NULL) {
3420 dev_err(adap->pdev_dev,
3421 "unable to get firmware info for chip %d.\n",
3422 CHELSIO_CHIP_VERSION(adap->params.chip));
3423 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003424 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303425
3426 /* allocate memory to read the header of the firmware on the
3427 * card
3428 */
3429 card_fw = t4_alloc_mem(sizeof(*card_fw));
3430
3431 /* Get FW from from /lib/firmware/ */
3432 ret = request_firmware(&fw, fw_info->fw_mod_name,
3433 adap->pdev_dev);
3434 if (ret < 0) {
3435 dev_err(adap->pdev_dev,
3436 "unable to load firmware image %s, error %d\n",
3437 fw_info->fw_mod_name, ret);
3438 } else {
3439 fw_data = fw->data;
3440 fw_size = fw->size;
3441 }
3442
3443 /* upgrade FW logic */
3444 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3445 state, &reset);
3446
3447 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003448 release_firmware(fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303449 t4_free_mem(card_fw);
3450
Vipul Pandya636f9d32012-09-26 02:39:39 +00003451 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303452 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003453 }
3454
3455 /*
3456 * Grab VPD parameters. This should be done after we establish a
3457 * connection to the firmware since some of the VPD parameters
3458 * (notably the Core Clock frequency) are retrieved via requests to
3459 * the firmware. On the other hand, we need these fairly early on
3460 * so we do this right after getting ahold of the firmware.
3461 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303462 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003463 if (ret < 0)
3464 goto bye;
3465
Vipul Pandya636f9d32012-09-26 02:39:39 +00003466 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003467 * Find out what ports are available to us. Note that we need to do
3468 * this before calling adap_init0_no_config() since it needs nports
3469 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003470 */
3471 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303472 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3473 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303474 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003475 if (ret < 0)
3476 goto bye;
3477
3478 adap->params.nports = hweight32(port_vec);
3479 adap->params.portvec = port_vec;
3480
Hariprasad Shenai06640312015-01-13 15:19:25 +05303481 /* If the firmware is initialized already, emit a simply note to that
3482 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003483 */
3484 if (state == DEV_STATE_INIT) {
3485 dev_info(adap->pdev_dev, "Coming up as %s: "\
3486 "Adapter already initialized\n",
3487 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003488 } else {
3489 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3490 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303491
3492 /* Find out whether we're dealing with a version of the
3493 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003494 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303495 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3496 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303497 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303498 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003499
Hariprasad Shenai06640312015-01-13 15:19:25 +05303500 /* If the firmware doesn't support Configuration Files,
3501 * return an error.
3502 */
3503 if (ret < 0) {
3504 dev_err(adap->pdev_dev, "firmware doesn't support "
3505 "Firmware Configuration Files\n");
3506 goto bye;
3507 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003508
Hariprasad Shenai06640312015-01-13 15:19:25 +05303509 /* The firmware provides us with a memory buffer where we can
3510 * load a Configuration File from the host if we want to
3511 * override the Configuration File in flash.
3512 */
3513 ret = adap_init0_config(adap, reset);
3514 if (ret == -ENOENT) {
3515 dev_err(adap->pdev_dev, "no Configuration File "
3516 "present on adapter.\n");
3517 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003518 }
3519 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303520 dev_err(adap->pdev_dev, "could not initialize "
3521 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003522 goto bye;
3523 }
3524 }
3525
Hariprasad Shenai06640312015-01-13 15:19:25 +05303526 /* Give the SGE code a chance to pull in anything that it needs ...
3527 * Note that this must be called after we retrieve our VPD parameters
3528 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003529 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303530 ret = t4_sge_init(adap);
3531 if (ret < 0)
3532 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003533
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003534 if (is_bypass_device(adap->pdev->device))
3535 adap->params.bypass = 1;
3536
Vipul Pandya636f9d32012-09-26 02:39:39 +00003537 /*
3538 * Grab some of our basic fundamental operating parameters.
3539 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003540#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303541 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3542 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003543
3544#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303545 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3546 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3547 FW_PARAMS_PARAM_Y_V(0) | \
3548 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003549
Vipul Pandya636f9d32012-09-26 02:39:39 +00003550 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003551 params[1] = FW_PARAM_PFVF(L2T_START);
3552 params[2] = FW_PARAM_PFVF(L2T_END);
3553 params[3] = FW_PARAM_PFVF(FILTER_START);
3554 params[4] = FW_PARAM_PFVF(FILTER_END);
3555 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303556 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003557 if (ret < 0)
3558 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003559 adap->sge.egr_start = val[0];
3560 adap->l2t_start = val[1];
3561 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003562 adap->tids.ftid_base = val[3];
3563 adap->tids.nftids = val[4] - val[3] + 1;
3564 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003565
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303566 /* qids (ingress/egress) returned from firmware can be anywhere
3567 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3568 * Hence driver needs to allocate memory for this range to
3569 * store the queue info. Get the highest IQFLINT/EQ index returned
3570 * in FW_EQ_*_CMD.alloc command.
3571 */
3572 params[0] = FW_PARAM_PFVF(EQ_END);
3573 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303574 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303575 if (ret < 0)
3576 goto bye;
3577 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3578 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3579
3580 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3581 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3582 if (!adap->sge.egr_map) {
3583 ret = -ENOMEM;
3584 goto bye;
3585 }
3586
3587 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3588 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3589 if (!adap->sge.ingr_map) {
3590 ret = -ENOMEM;
3591 goto bye;
3592 }
3593
3594 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303595 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303596 */
3597 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3598 sizeof(long), GFP_KERNEL);
3599 if (!adap->sge.starving_fl) {
3600 ret = -ENOMEM;
3601 goto bye;
3602 }
3603
3604 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3605 sizeof(long), GFP_KERNEL);
3606 if (!adap->sge.txq_maperr) {
3607 ret = -ENOMEM;
3608 goto bye;
3609 }
3610
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303611#ifdef CONFIG_DEBUG_FS
3612 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3613 sizeof(long), GFP_KERNEL);
3614 if (!adap->sge.blocked_fl) {
3615 ret = -ENOMEM;
3616 goto bye;
3617 }
3618#endif
3619
Anish Bhattb5a02f52015-01-14 15:17:34 -08003620 params[0] = FW_PARAM_PFVF(CLIP_START);
3621 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303622 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003623 if (ret < 0)
3624 goto bye;
3625 adap->clipt_start = val[0];
3626 adap->clipt_end = val[1];
3627
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303628 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3629 * Classes supported by the hardware/firmware so we hard code it here
3630 * for now.
3631 */
3632 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3633
Vipul Pandya636f9d32012-09-26 02:39:39 +00003634 /* query params related to active filter region */
3635 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3636 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303637 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003638 /* If Active filter size is set we enable establishing
3639 * offload connection through firmware work request
3640 */
3641 if ((val[0] != val[1]) && (ret >= 0)) {
3642 adap->flags |= FW_OFLD_CONN;
3643 adap->tids.aftid_base = val[0];
3644 adap->tids.aftid_end = val[1];
3645 }
3646
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003647 /* If we're running on newer firmware, let it know that we're
3648 * prepared to deal with encapsulated CPL messages. Older
3649 * firmware won't understand this and we'll just get
3650 * unencapsulated messages ...
3651 */
3652 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3653 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303654 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003655
Vipul Pandya636f9d32012-09-26 02:39:39 +00003656 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303657 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3658 * capability. Earlier versions of the firmware didn't have the
3659 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3660 * permission to use ULPTX MEMWRITE DSGL.
3661 */
3662 if (is_t4(adap->params.chip)) {
3663 adap->params.ulptx_memwrite_dsgl = false;
3664 } else {
3665 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303666 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303667 1, params, val);
3668 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3669 }
3670
Steve Wise086de572016-09-16 07:54:49 -07003671 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3672 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3673 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3674 1, params, val);
3675 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3676
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303677 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003678 * Get device capabilities so we can determine what resources we need
3679 * to manage.
3680 */
3681 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303682 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3683 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303684 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003685 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3686 &caps_cmd);
3687 if (ret < 0)
3688 goto bye;
3689
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003690 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003691 /* query offload-related parameters */
3692 params[0] = FW_PARAM_DEV(NTID);
3693 params[1] = FW_PARAM_PFVF(SERVER_START);
3694 params[2] = FW_PARAM_PFVF(SERVER_END);
3695 params[3] = FW_PARAM_PFVF(TDDP_START);
3696 params[4] = FW_PARAM_PFVF(TDDP_END);
3697 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303698 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003699 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003700 if (ret < 0)
3701 goto bye;
3702 adap->tids.ntids = val[0];
3703 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3704 adap->tids.stid_base = val[1];
3705 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003706 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003707 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003708 * region into two parts. Regular filters get 1/3rd and server
3709 * filters get 2/3rd part. This is only enabled if workarond
3710 * path is enabled.
3711 * 1. For regular filters.
3712 * 2. Server filter: This are special filters which are used
3713 * to redirect SYN packets to offload queue.
3714 */
3715 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3716 adap->tids.sftid_base = adap->tids.ftid_base +
3717 DIV_ROUND_UP(adap->tids.nftids, 3);
3718 adap->tids.nsftids = adap->tids.nftids -
3719 DIV_ROUND_UP(adap->tids.nftids, 3);
3720 adap->tids.nftids = adap->tids.sftid_base -
3721 adap->tids.ftid_base;
3722 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003723 adap->vres.ddp.start = val[3];
3724 adap->vres.ddp.size = val[4] - val[3] + 1;
3725 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00003726
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003727 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303728 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003729 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003730 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003731 params[0] = FW_PARAM_PFVF(STAG_START);
3732 params[1] = FW_PARAM_PFVF(STAG_END);
3733 params[2] = FW_PARAM_PFVF(RQ_START);
3734 params[3] = FW_PARAM_PFVF(RQ_END);
3735 params[4] = FW_PARAM_PFVF(PBL_START);
3736 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303737 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003738 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003739 if (ret < 0)
3740 goto bye;
3741 adap->vres.stag.start = val[0];
3742 adap->vres.stag.size = val[1] - val[0] + 1;
3743 adap->vres.rq.start = val[2];
3744 adap->vres.rq.size = val[3] - val[2] + 1;
3745 adap->vres.pbl.start = val[4];
3746 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003747
3748 params[0] = FW_PARAM_PFVF(SQRQ_START);
3749 params[1] = FW_PARAM_PFVF(SQRQ_END);
3750 params[2] = FW_PARAM_PFVF(CQ_START);
3751 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003752 params[4] = FW_PARAM_PFVF(OCQ_START);
3753 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303754 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303755 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003756 if (ret < 0)
3757 goto bye;
3758 adap->vres.qp.start = val[0];
3759 adap->vres.qp.size = val[1] - val[0] + 1;
3760 adap->vres.cq.start = val[2];
3761 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003762 adap->vres.ocq.start = val[4];
3763 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303764
3765 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3766 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303767 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303768 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303769 if (ret < 0) {
3770 adap->params.max_ordird_qp = 8;
3771 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3772 ret = 0;
3773 } else {
3774 adap->params.max_ordird_qp = val[0];
3775 adap->params.max_ird_adapter = val[1];
3776 }
3777 dev_info(adap->pdev_dev,
3778 "max_ordird_qp %d max_ird_adapter %d\n",
3779 adap->params.max_ordird_qp,
3780 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303781 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003782 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003783 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003784 params[0] = FW_PARAM_PFVF(ISCSI_START);
3785 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303786 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003787 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003788 if (ret < 0)
3789 goto bye;
3790 adap->vres.iscsi.start = val[0];
3791 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303792 /* LIO target and cxgb4i initiaitor */
3793 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003794 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05303795 if (caps_cmd.cryptocaps) {
3796 /* Should query params here...TODO */
3797 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
3798 adap->num_uld += 1;
3799 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003800#undef FW_PARAM_PFVF
3801#undef FW_PARAM_DEV
3802
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303803 /* The MTU/MSS Table is initialized by now, so load their values. If
3804 * we're initializing the adapter, then we'll make any modifications
3805 * we want to the MTU/MSS Table and also initialize the congestion
3806 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003807 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003808 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303809 if (state != DEV_STATE_INIT) {
3810 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00003811
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303812 /* The default MTU Table contains values 1492 and 1500.
3813 * However, for TCP, it's better to have two values which are
3814 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
3815 * This allows us to have a TCP Data Payload which is a
3816 * multiple of 8 regardless of what combination of TCP Options
3817 * are in use (always a multiple of 4 bytes) which is
3818 * important for performance reasons. For instance, if no
3819 * options are in use, then we have a 20-byte IP header and a
3820 * 20-byte TCP header. In this case, a 1500-byte MSS would
3821 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
3822 * which is not a multiple of 8. So using an MSS of 1488 in
3823 * this case results in a TCP Data Payload of 1448 bytes which
3824 * is a multiple of 8. On the other hand, if 12-byte TCP Time
3825 * Stamps have been negotiated, then an MTU of 1500 bytes
3826 * results in a TCP Data Payload of 1448 bytes which, as
3827 * above, is a multiple of 8 bytes ...
3828 */
3829 for (i = 0; i < NMTUS; i++)
3830 if (adap->params.mtus[i] == 1492) {
3831 adap->params.mtus[i] = 1488;
3832 break;
3833 }
3834
3835 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3836 adap->params.b_wnd);
3837 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05303838 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003839 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05303840 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003841 return 0;
3842
3843 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003844 * Something bad happened. If a command timed out or failed with EIO
3845 * FW does not operate within its spec or something catastrophic
3846 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003847 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003848bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303849 kfree(adap->sge.egr_map);
3850 kfree(adap->sge.ingr_map);
3851 kfree(adap->sge.starving_fl);
3852 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303853#ifdef CONFIG_DEBUG_FS
3854 kfree(adap->sge.blocked_fl);
3855#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00003856 if (ret != -ETIMEDOUT && ret != -EIO)
3857 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003858 return ret;
3859}
3860
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003861/* EEH callbacks */
3862
3863static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
3864 pci_channel_state_t state)
3865{
3866 int i;
3867 struct adapter *adap = pci_get_drvdata(pdev);
3868
3869 if (!adap)
3870 goto out;
3871
3872 rtnl_lock();
3873 adap->flags &= ~FW_OK;
3874 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08003875 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003876 for_each_port(adap, i) {
3877 struct net_device *dev = adap->port[i];
3878
3879 netif_device_detach(dev);
3880 netif_carrier_off(dev);
3881 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08003882 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05303883 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003884 if (adap->flags & FULL_INIT_DONE)
3885 cxgb_down(adap);
3886 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08003887 if ((adap->flags & DEV_ENABLED)) {
3888 pci_disable_device(pdev);
3889 adap->flags &= ~DEV_ENABLED;
3890 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003891out: return state == pci_channel_io_perm_failure ?
3892 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
3893}
3894
3895static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
3896{
3897 int i, ret;
3898 struct fw_caps_config_cmd c;
3899 struct adapter *adap = pci_get_drvdata(pdev);
3900
3901 if (!adap) {
3902 pci_restore_state(pdev);
3903 pci_save_state(pdev);
3904 return PCI_ERS_RESULT_RECOVERED;
3905 }
3906
Gavin Shan144be3d2014-01-23 12:27:34 +08003907 if (!(adap->flags & DEV_ENABLED)) {
3908 if (pci_enable_device(pdev)) {
3909 dev_err(&pdev->dev, "Cannot reenable PCI "
3910 "device after reset\n");
3911 return PCI_ERS_RESULT_DISCONNECT;
3912 }
3913 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003914 }
3915
3916 pci_set_master(pdev);
3917 pci_restore_state(pdev);
3918 pci_save_state(pdev);
3919 pci_cleanup_aer_uncorrect_error_status(pdev);
3920
Hariprasad Shenai8203b502014-10-09 05:48:47 +05303921 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003922 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303923 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003924 return PCI_ERS_RESULT_DISCONNECT;
3925 adap->flags |= FW_OK;
3926 if (adap_init1(adap, &c))
3927 return PCI_ERS_RESULT_DISCONNECT;
3928
3929 for_each_port(adap, i) {
3930 struct port_info *p = adap2pinfo(adap, i);
3931
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303932 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003933 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003934 if (ret < 0)
3935 return PCI_ERS_RESULT_DISCONNECT;
3936 p->viid = ret;
3937 p->xact_addr_filt = -1;
3938 }
3939
3940 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3941 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003942 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003943 if (cxgb_up(adap))
3944 return PCI_ERS_RESULT_DISCONNECT;
3945 return PCI_ERS_RESULT_RECOVERED;
3946}
3947
3948static void eeh_resume(struct pci_dev *pdev)
3949{
3950 int i;
3951 struct adapter *adap = pci_get_drvdata(pdev);
3952
3953 if (!adap)
3954 return;
3955
3956 rtnl_lock();
3957 for_each_port(adap, i) {
3958 struct net_device *dev = adap->port[i];
3959
3960 if (netif_running(dev)) {
3961 link_start(dev);
3962 cxgb_set_rxmode(dev);
3963 }
3964 netif_device_attach(dev);
3965 }
3966 rtnl_unlock();
3967}
3968
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07003969static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003970 .error_detected = eeh_err_detected,
3971 .slot_reset = eeh_slot_reset,
3972 .resume = eeh_resume,
3973};
3974
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05303975/* Return true if the Link Configuration supports "High Speeds" (those greater
3976 * than 1Gb/s).
3977 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05303978static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003979{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05303980 unsigned int speeds, high_speeds;
3981
3982 speeds = FW_PORT_CAP_SPEED_V(FW_PORT_CAP_SPEED_G(lc->supported));
3983 high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
3984
3985 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003986}
3987
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003988/*
3989 * Perform default configuration of DMA queues depending on the number and type
3990 * of ports we found and the number of available CPUs. Most settings can be
3991 * modified by the admin prior to actual use.
3992 */
Bill Pemberton91744942012-12-03 09:23:02 -05003993static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003994{
3995 struct sge *s = &adap->sge;
Anish Bhatt688848b2014-06-19 21:37:13 -07003996 int i, n10g = 0, qidx = 0;
3997#ifndef CONFIG_CHELSIO_T4_DCB
3998 int q10g = 0;
3999#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004000
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304001 /* Reduce memory usage in kdump environment, disable all offload.
4002 */
4003 if (is_kdump_kernel()) {
4004 adap->params.offload = 0;
4005 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304006 } else if (is_uld(adap) && t4_uld_mem_alloc(adap)) {
4007 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304008 adap->params.crypto = 0;
4009 }
4010
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004011 for_each_port(adap, i)
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304012 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004013#ifdef CONFIG_CHELSIO_T4_DCB
4014 /* For Data Center Bridging support we need to be able to support up
4015 * to 8 Traffic Priorities; each of which will be assigned to its
4016 * own TX Queue in order to prevent Head-Of-Line Blocking.
4017 */
4018 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4019 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4020 MAX_ETH_QSETS, adap->params.nports * 8);
4021 BUG_ON(1);
4022 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004023
Anish Bhatt688848b2014-06-19 21:37:13 -07004024 for_each_port(adap, i) {
4025 struct port_info *pi = adap2pinfo(adap, i);
4026
4027 pi->first_qset = qidx;
4028 pi->nqsets = 8;
4029 qidx += pi->nqsets;
4030 }
4031#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004032 /*
4033 * We default to 1 queue per non-10G port and up to # of cores queues
4034 * per 10G port.
4035 */
4036 if (n10g)
4037 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004038 if (q10g > netif_get_num_default_rss_queues())
4039 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004040
4041 for_each_port(adap, i) {
4042 struct port_info *pi = adap2pinfo(adap, i);
4043
4044 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304045 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004046 qidx += pi->nqsets;
4047 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004048#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004049
4050 s->ethqsets = qidx;
4051 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4052
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304053 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004054 /*
4055 * For offload we use 1 queue/channel if all ports are up to 1G,
4056 * otherwise we divide all available queues amongst the channels
4057 * capped by the number of available cores.
4058 */
4059 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304060 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304061 s->ofldqsets = roundup(i, adap->params.nports);
4062 } else {
4063 s->ofldqsets = adap->params.nports;
4064 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004065 }
4066
4067 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4068 struct sge_eth_rxq *r = &s->ethrxq[i];
4069
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304070 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004071 r->fl.size = 72;
4072 }
4073
4074 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4075 s->ethtxq[i].q.size = 1024;
4076
4077 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4078 s->ctrlq[i].q.size = 512;
4079
4080 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
4081 s->ofldtxq[i].q.size = 1024;
4082
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304083 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304084 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004085}
4086
4087/*
4088 * Reduce the number of Ethernet queues across all ports to at most n.
4089 * n provides at least one queue per port.
4090 */
Bill Pemberton91744942012-12-03 09:23:02 -05004091static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004092{
4093 int i;
4094 struct port_info *pi;
4095
4096 while (n < adap->sge.ethqsets)
4097 for_each_port(adap, i) {
4098 pi = adap2pinfo(adap, i);
4099 if (pi->nqsets > 1) {
4100 pi->nqsets--;
4101 adap->sge.ethqsets--;
4102 if (adap->sge.ethqsets <= n)
4103 break;
4104 }
4105 }
4106
4107 n = 0;
4108 for_each_port(adap, i) {
4109 pi = adap2pinfo(adap, i);
4110 pi->first_qset = n;
4111 n += pi->nqsets;
4112 }
4113}
4114
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304115static int get_msix_info(struct adapter *adap)
4116{
4117 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304118 unsigned int max_ingq = 0;
4119
4120 if (is_offload(adap))
4121 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4122 if (is_pci_uld(adap))
4123 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4124
4125 if (!max_ingq)
4126 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304127
4128 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4129 if (!msix_info)
4130 return -ENOMEM;
4131
4132 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4133 sizeof(long), GFP_KERNEL);
4134 if (!adap->msix_bmap_ulds.msix_bmap) {
4135 kfree(msix_info);
4136 return -ENOMEM;
4137 }
4138 spin_lock_init(&adap->msix_bmap_ulds.lock);
4139 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304140out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304141 return 0;
4142}
4143
4144static void free_msix_info(struct adapter *adap)
4145{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304146 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304147 return;
4148
4149 kfree(adap->msix_info_ulds);
4150 kfree(adap->msix_bmap_ulds.msix_bmap);
4151}
4152
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004153/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4154#define EXTRA_VECS 2
4155
Bill Pemberton91744942012-12-03 09:23:02 -05004156static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004157{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304158 int ofld_need = 0, uld_need = 0;
4159 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004160 struct sge *s = &adap->sge;
4161 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304162 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304163 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004164
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304165 if (is_pci_uld(adap))
4166 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4167 if (is_offload(adap))
4168 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304169 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304170 GFP_KERNEL);
4171 if (!entries)
4172 return -ENOMEM;
4173
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304174 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304175 if (get_msix_info(adap)) {
4176 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304177 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304178 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304179
4180 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004181 entries[i].entry = i;
4182
4183 want = s->max_ethqsets + EXTRA_VECS;
4184 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304185 want += adap->num_ofld_uld * s->ofldqsets;
4186 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004187 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304188 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304189 want += adap->num_uld * s->ofldqsets;
4190 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304191 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004192#ifdef CONFIG_CHELSIO_T4_DCB
4193 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4194 * each port.
4195 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304196 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004197#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304198 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004199#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304200 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4201 if (allocated < 0) {
4202 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4203 " not using MSI-X\n");
4204 kfree(entries);
4205 return allocated;
4206 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004207
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304208 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004209 * Every group gets its minimum requirement and NIC gets top
4210 * priority for leftovers.
4211 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304212 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004213 if (i < s->max_ethqsets) {
4214 s->max_ethqsets = i;
4215 if (i < s->ethqsets)
4216 reduce_ethqs(adap, i);
4217 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304218 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304219 if (allocated < want)
4220 s->nqs_per_uld = nchan;
4221 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304222 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304223 }
4224
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304225 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004226 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304227 if (is_uld(adap)) {
4228 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304229 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304230 adap->msix_info_ulds[j].idx = i;
4231 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304232 adap->msix_bmap_ulds.mapsize = j;
4233 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304234 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304235 "nic %d per uld %d\n",
4236 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004237
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304238 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004239 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004240}
4241
4242#undef EXTRA_VECS
4243
Bill Pemberton91744942012-12-03 09:23:02 -05004244static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004245{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304246 unsigned int i;
4247 int err;
4248
4249 err = t4_init_rss_mode(adap, adap->mbox);
4250 if (err)
4251 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004252
4253 for_each_port(adap, i) {
4254 struct port_info *pi = adap2pinfo(adap, i);
4255
4256 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4257 if (!pi->rss)
4258 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004259 }
4260 return 0;
4261}
4262
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304263static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4264 enum pci_bus_speed *speed,
4265 enum pcie_link_width *width)
4266{
4267 u32 lnkcap1, lnkcap2;
4268 int err1, err2;
4269
4270#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4271
4272 *speed = PCI_SPEED_UNKNOWN;
4273 *width = PCIE_LNK_WIDTH_UNKNOWN;
4274
4275 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4276 &lnkcap1);
4277 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4278 &lnkcap2);
4279 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4280 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4281 *speed = PCIE_SPEED_8_0GT;
4282 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4283 *speed = PCIE_SPEED_5_0GT;
4284 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4285 *speed = PCIE_SPEED_2_5GT;
4286 }
4287 if (!err1) {
4288 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4289 if (!lnkcap2) { /* pre-r3.0 */
4290 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4291 *speed = PCIE_SPEED_5_0GT;
4292 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4293 *speed = PCIE_SPEED_2_5GT;
4294 }
4295 }
4296
4297 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4298 return err1 ? err1 : err2 ? err2 : -EINVAL;
4299 return 0;
4300}
4301
4302static void cxgb4_check_pcie_caps(struct adapter *adap)
4303{
4304 enum pcie_link_width width, width_cap;
4305 enum pci_bus_speed speed, speed_cap;
4306
4307#define PCIE_SPEED_STR(speed) \
4308 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4309 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4310 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4311 "Unknown")
4312
4313 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4314 dev_warn(adap->pdev_dev,
4315 "Unable to determine PCIe device BW capabilities\n");
4316 return;
4317 }
4318
4319 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4320 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4321 dev_warn(adap->pdev_dev,
4322 "Unable to determine PCI Express bandwidth.\n");
4323 return;
4324 }
4325
4326 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4327 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4328 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4329 width, width_cap);
4330 if (speed < speed_cap || width < width_cap)
4331 dev_info(adap->pdev_dev,
4332 "A slot with more lanes and/or higher speed is "
4333 "suggested for optimal performance.\n");
4334}
4335
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304336/* Dump basic information about the adapter */
4337static void print_adapter_info(struct adapter *adapter)
4338{
4339 /* Device information */
4340 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4341 adapter->params.vpd.id,
4342 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4343 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4344 adapter->params.vpd.sn, adapter->params.vpd.pn);
4345
4346 /* Firmware Version */
4347 if (!adapter->params.fw_vers)
4348 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4349 else
4350 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4351 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4352 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4353 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4354 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4355
4356 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4357 * Firmware, so dev_info() is more appropriate here.)
4358 */
4359 if (!adapter->params.bs_vers)
4360 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4361 else
4362 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4363 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4364 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4365 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4366 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4367
4368 /* TP Microcode Version */
4369 if (!adapter->params.tp_vers)
4370 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4371 else
4372 dev_info(adapter->pdev_dev,
4373 "TP Microcode version: %u.%u.%u.%u\n",
4374 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4375 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4376 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4377 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4378
4379 /* Expansion ROM version */
4380 if (!adapter->params.er_vers)
4381 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4382 else
4383 dev_info(adapter->pdev_dev,
4384 "Expansion ROM version: %u.%u.%u.%u\n",
4385 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4386 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4387 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4388 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4389
4390 /* Software/Hardware configuration */
4391 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4392 is_offload(adapter) ? "R" : "",
4393 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4394 (adapter->flags & USING_MSI) ? "MSI" : ""),
4395 is_offload(adapter) ? "Offload" : "non-Offload");
4396}
4397
Bill Pemberton91744942012-12-03 09:23:02 -05004398static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004399{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004400 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004401 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004402 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004403 const struct port_info *pi = netdev_priv(dev);
4404 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004405
4406 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4407 spd = " 2.5 GT/s";
4408 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4409 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004410 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4411 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004412
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004413 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4414 bufp += sprintf(bufp, "100/");
4415 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4416 bufp += sprintf(bufp, "1000/");
4417 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4418 bufp += sprintf(bufp, "10G/");
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304419 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
4420 bufp += sprintf(bufp, "25G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304421 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4422 bufp += sprintf(bufp, "40G/");
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304423 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
4424 bufp += sprintf(bufp, "100G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004425 if (bufp != buf)
4426 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304427 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004428
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304429 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4430 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004431}
4432
Bill Pemberton91744942012-12-03 09:23:02 -05004433static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004434{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004435 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004436}
4437
Dimitris Michailidis06546392010-07-11 12:01:16 +00004438/*
4439 * Free the following resources:
4440 * - memory used for tables
4441 * - MSI/MSI-X
4442 * - net devices
4443 * - resources FW is holding for us
4444 */
4445static void free_some_resources(struct adapter *adapter)
4446{
4447 unsigned int i;
4448
4449 t4_free_mem(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304450 t4_cleanup_sched(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004451 t4_free_mem(adapter->tids.tid_tab);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304452 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304453 kfree(adapter->sge.egr_map);
4454 kfree(adapter->sge.ingr_map);
4455 kfree(adapter->sge.starving_fl);
4456 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304457#ifdef CONFIG_DEBUG_FS
4458 kfree(adapter->sge.blocked_fl);
4459#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004460 disable_msi(adapter);
4461
4462 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004463 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304464 struct port_info *pi = adap2pinfo(adapter, i);
4465
4466 if (pi->viid != 0)
4467 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4468 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004469 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004470 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004471 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004472 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304473 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004474}
4475
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004476#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004477#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004478 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004479#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004480
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304481static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4482{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304483 u16 device_id;
4484
4485 /* Retrieve adapter's device ID */
4486 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004487
4488 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304489 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004490 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304491 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004492 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304493 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004494 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304495 default:
4496 dev_err(&pdev->dev, "Device %d is not supported\n",
4497 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304498 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004499 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304500}
4501
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304502#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304503static void dummy_setup(struct net_device *dev)
4504{
4505 dev->type = ARPHRD_NONE;
4506 dev->mtu = 0;
4507 dev->hard_header_len = 0;
4508 dev->addr_len = 0;
4509 dev->tx_queue_len = 0;
4510 dev->flags |= IFF_NOARP;
4511 dev->priv_flags |= IFF_NO_QUEUE;
4512
4513 /* Initialize the device structure. */
4514 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4515 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
4516 dev->destructor = free_netdev;
4517}
4518
4519static int config_mgmt_dev(struct pci_dev *pdev)
4520{
4521 struct adapter *adap = pci_get_drvdata(pdev);
4522 struct net_device *netdev;
4523 struct port_info *pi;
4524 char name[IFNAMSIZ];
4525 int err;
4526
4527 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
4528 netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, dummy_setup);
4529 if (!netdev)
4530 return -ENOMEM;
4531
4532 pi = netdev_priv(netdev);
4533 pi->adapter = adap;
4534 SET_NETDEV_DEV(netdev, &pdev->dev);
4535
4536 adap->port[0] = netdev;
4537
4538 err = register_netdev(adap->port[0]);
4539 if (err) {
4540 pr_info("Unable to register VF mgmt netdev %s\n", name);
4541 free_netdev(adap->port[0]);
4542 adap->port[0] = NULL;
4543 return err;
4544 }
4545 return 0;
4546}
4547
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304548static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4549{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304550 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304551 int err = 0;
4552 int current_vfs = pci_num_vf(pdev);
4553 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304554
Hariprasad Shenai78294512016-08-11 21:06:23 +05304555 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304556 /* Check if cxgb4 is the MASTER and fw is initialized */
4557 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4558 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4559 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4560 dev_warn(&pdev->dev,
4561 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4562 return -EOPNOTSUPP;
4563 }
4564
4565 /* If any of the VF's is already assigned to Guest OS, then
4566 * SRIOV for the same cannot be modified
4567 */
4568 if (current_vfs && pci_vfs_assigned(pdev)) {
4569 dev_err(&pdev->dev,
4570 "Cannot modify SR-IOV while VFs are assigned\n");
4571 num_vfs = current_vfs;
4572 return num_vfs;
4573 }
4574
4575 /* Disable SRIOV when zero is passed.
4576 * One needs to disable SRIOV before modifying it, else
4577 * stack throws the below warning:
4578 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4579 */
4580 if (!num_vfs) {
4581 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304582 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304583 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304584 adap->port[0] = NULL;
4585 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304586 /* free VF resources */
4587 kfree(adap->vfinfo);
4588 adap->vfinfo = NULL;
4589 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304590 return num_vfs;
4591 }
4592
4593 if (num_vfs != current_vfs) {
4594 err = pci_enable_sriov(pdev, num_vfs);
4595 if (err)
4596 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304597
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304598 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304599 err = config_mgmt_dev(pdev);
4600 if (err)
4601 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304602 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304603
4604 adap->vfinfo = kcalloc(adap->num_vfs,
4605 sizeof(struct vf_info), GFP_KERNEL);
4606 if (adap->vfinfo)
4607 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304608 return num_vfs;
4609}
4610#endif
4611
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004612static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004613{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004614 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004615 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004616 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004617 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304618 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304619 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304620 u32 whoami, pl_rev;
4621 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304622 static int adap_idx = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004623
4624 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4625
4626 err = pci_request_regions(pdev, KBUILD_MODNAME);
4627 if (err) {
4628 /* Just info, some other driver may have claimed the device. */
4629 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4630 return err;
4631 }
4632
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004633 err = pci_enable_device(pdev);
4634 if (err) {
4635 dev_err(&pdev->dev, "cannot enable PCI device\n");
4636 goto out_release_regions;
4637 }
4638
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304639 regs = pci_ioremap_bar(pdev, 0);
4640 if (!regs) {
4641 dev_err(&pdev->dev, "cannot map device registers\n");
4642 err = -ENOMEM;
4643 goto out_disable_device;
4644 }
4645
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304646 err = t4_wait_dev_ready(regs);
4647 if (err < 0)
4648 goto out_unmap_bar0;
4649
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304650 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304651 whoami = readl(regs + PL_WHOAMI_A);
4652 pl_rev = REV_G(readl(regs + PL_REV_A));
4653 chip = get_chip_type(pdev, pl_rev);
4654 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4655 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304656 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304657#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304658 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304659#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304660 pci_disable_device(pdev);
4661 pci_save_state(pdev); /* to restore SR-IOV later */
4662 goto sriov;
4663 }
4664
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004665 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004666 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004667 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4668 if (err) {
4669 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4670 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304671 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004672 }
4673 } else {
4674 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4675 if (err) {
4676 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304677 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004678 }
4679 }
4680
4681 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004682 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004683 pci_set_master(pdev);
4684 pci_save_state(pdev);
4685
4686 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4687 if (!adapter) {
4688 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304689 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004690 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304691 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004692
Anish Bhatt29aaee62014-08-20 13:44:06 -07004693 adapter->workq = create_singlethread_workqueue("cxgb4");
4694 if (!adapter->workq) {
4695 err = -ENOMEM;
4696 goto out_free_adapter;
4697 }
4698
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304699 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4700 (sizeof(struct mbox_cmd) *
4701 T4_OS_LOG_MBOX_CMDS),
4702 GFP_KERNEL);
4703 if (!adapter->mbox_log) {
4704 err = -ENOMEM;
4705 goto out_free_adapter;
4706 }
4707 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4708
Gavin Shan144be3d2014-01-23 12:27:34 +08004709 /* PCI device has been enabled */
4710 adapter->flags |= DEV_ENABLED;
4711
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304712 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004713 adapter->pdev = pdev;
4714 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304715 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304716 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304717 adapter->pf = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004718 adapter->msg_enable = dflt_msg_enable;
4719 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4720
4721 spin_lock_init(&adapter->stats_lock);
4722 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004723 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004724
4725 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304726 INIT_WORK(&adapter->db_full_task, process_db_full);
4727 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004728
4729 err = t4_prep_adapter(adapter);
4730 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304731 goto out_free_adapter;
4732
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004733
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304734 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304735 s_qpp = (QUEUESPERPAGEPF0_S +
4736 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304737 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304738 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4739 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004740 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4741
4742 /* Each segment size is 128B. Write coalescing is enabled only
4743 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4744 * queue is less no of segments that can be accommodated in
4745 * a page size.
4746 */
4747 if (qpp > num_seg) {
4748 dev_err(&pdev->dev,
4749 "Incorrect number of egress queues per page\n");
4750 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304751 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004752 }
4753 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4754 pci_resource_len(pdev, 2));
4755 if (!adapter->bar2) {
4756 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4757 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304758 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004759 }
4760 }
4761
Vipul Pandya636f9d32012-09-26 02:39:39 +00004762 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004763 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304764#ifdef CONFIG_DEBUG_FS
4765 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4766#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004767 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004768 if (err)
4769 goto out_unmap_bar;
4770
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304771 /* configure SGE_STAT_CFG_A to read WC stats */
4772 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304773 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4774 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4775 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304776
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004777 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004778 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4779 MAX_ETH_QSETS);
4780 if (!netdev) {
4781 err = -ENOMEM;
4782 goto out_free_dev;
4783 }
4784
4785 SET_NETDEV_DEV(netdev, &pdev->dev);
4786
4787 adapter->port[i] = netdev;
4788 pi = netdev_priv(netdev);
4789 pi->adapter = adapter;
4790 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004791 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004792 netdev->irq = pdev->irq;
4793
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004794 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4795 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4796 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304797 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
4798 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004799 if (highdma)
4800 netdev->hw_features |= NETIF_F_HIGHDMA;
4801 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004802 netdev->vlan_features = netdev->features & VLAN_FEAT;
4803
Jiri Pirko01789342011-08-16 06:29:00 +00004804 netdev->priv_flags |= IFF_UNICAST_FLT;
4805
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004806 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07004807#ifdef CONFIG_CHELSIO_T4_DCB
4808 netdev->dcbnl_ops = &cxgb4_dcb_ops;
4809 cxgb4_dcb_state_init(netdev);
4810#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304811 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004812 }
4813
4814 pci_set_drvdata(pdev, adapter);
4815
4816 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004817 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004818 if (err)
4819 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304820 } else if (adapter->params.nports == 1) {
4821 /* If we don't have a connection to the firmware -- possibly
4822 * because of an error -- grab the raw VPD parameters so we
4823 * can set the proper MAC Address on the debug network
4824 * interface that we've created.
4825 */
4826 u8 hw_addr[ETH_ALEN];
4827 u8 *na = adapter->params.vpd.na;
4828
4829 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
4830 if (!err) {
4831 for (i = 0; i < ETH_ALEN; i++)
4832 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
4833 hex2val(na[2 * i + 1]));
4834 t4_set_hw_addr(adapter, 0, hw_addr);
4835 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004836 }
4837
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304838 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004839 * soon as the first register_netdev completes.
4840 */
4841 cfg_queues(adapter);
4842
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05304843 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004844 if (!adapter->l2t) {
4845 /* We tolerate a lack of L2T, giving up some functionality */
4846 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
4847 adapter->params.offload = 0;
4848 }
4849
Anish Bhattb5a02f52015-01-14 15:17:34 -08004850#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304851 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
4852 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
4853 /* CLIP functionality is not present in hardware,
4854 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08004855 */
4856 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304857 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08004858 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304859 } else {
4860 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
4861 adapter->clipt_end);
4862 if (!adapter->clipt) {
4863 /* We tolerate a lack of clip_table, giving up
4864 * some functionality
4865 */
4866 dev_warn(&pdev->dev,
4867 "could not allocate Clip table, continuing\n");
4868 adapter->params.offload = 0;
4869 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08004870 }
4871#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304872
4873 for_each_port(adapter, i) {
4874 pi = adap2pinfo(adapter, i);
4875 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
4876 if (!pi->sched_tbl)
4877 dev_warn(&pdev->dev,
4878 "could not activate scheduling on port %d\n",
4879 i);
4880 }
4881
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05304882 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004883 dev_warn(&pdev->dev, "could not allocate TID table, "
4884 "continuing\n");
4885 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304886 } else {
4887 adapter->tc_u32 = cxgb4_init_tc_u32(adapter,
4888 CXGB4_MAX_LINK_HANDLE);
4889 if (!adapter->tc_u32)
4890 dev_warn(&pdev->dev,
4891 "could not offload tc u32, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004892 }
4893
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05304894 if (is_offload(adapter)) {
4895 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
4896 u32 hash_base, hash_reg;
4897
4898 if (chip <= CHELSIO_T5) {
4899 hash_reg = LE_DB_TID_HASHBASE_A;
4900 hash_base = t4_read_reg(adapter, hash_reg);
4901 adapter->tids.hash_base = hash_base / 4;
4902 } else {
4903 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
4904 hash_base = t4_read_reg(adapter, hash_reg);
4905 adapter->tids.hash_base = hash_base;
4906 }
4907 }
4908 }
4909
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00004910 /* See what interrupts we'll be using */
4911 if (msi > 1 && enable_msix(adapter) == 0)
4912 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304913 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00004914 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304915 if (msi > 1)
4916 free_msix_info(adapter);
4917 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00004918
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304919 /* check for PCI Express bandwidth capabiltites */
4920 cxgb4_check_pcie_caps(adapter);
4921
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004922 err = init_rss(adapter);
4923 if (err)
4924 goto out_free_dev;
4925
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004926 /*
4927 * The card is now ready to go. If any errors occur during device
4928 * registration we do not fail the whole card but rather proceed only
4929 * with the ports we manage to register successfully. However we must
4930 * register at least one net device.
4931 */
4932 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00004933 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05304934 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00004935 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
4936 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
4937
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004938 err = register_netdev(adapter->port[i]);
4939 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004940 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004941 adapter->chan_map[pi->tx_chan] = i;
4942 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004943 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004944 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004945 dev_err(&pdev->dev, "could not register any net devices\n");
4946 goto out_free_dev;
4947 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004948 if (err) {
4949 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
4950 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00004951 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004952
4953 if (cxgb4_debugfs_root) {
4954 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
4955 cxgb4_debugfs_root);
4956 setup_debugfs(adapter);
4957 }
4958
David S. Miller88c51002011-10-07 13:38:43 -04004959 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4960 pdev->needs_freset = 1;
4961
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304962 if (is_uld(adapter)) {
4963 mutex_lock(&uld_mutex);
4964 list_add_tail(&adapter->list_node, &adapter_list);
4965 mutex_unlock(&uld_mutex);
4966 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004967
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304968 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304969 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304970 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304971
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05304972sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004973#ifdef CONFIG_PCI_IOV
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304974 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0) {
4975 dev_warn(&pdev->dev,
4976 "Enabling SR-IOV VFs using the num_vf module "
4977 "parameter is deprecated - please use the pci sysfs "
4978 "interface instead.\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004979 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
4980 dev_info(&pdev->dev,
4981 "instantiated %u virtual functions\n",
4982 num_vf[func]);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304983 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304984
4985 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4986 if (!adapter) {
4987 err = -ENOMEM;
4988 goto free_pci_region;
4989 }
4990
Hariprasad Shenai78294512016-08-11 21:06:23 +05304991 adapter->pdev = pdev;
4992 adapter->pdev_dev = &pdev->dev;
4993 adapter->name = pci_name(pdev);
4994 adapter->mbox = func;
4995 adapter->pf = func;
4996 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304997 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304998 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4999 (sizeof(struct mbox_cmd) *
5000 T4_OS_LOG_MBOX_CMDS),
5001 GFP_KERNEL);
5002 if (!adapter->mbox_log) {
5003 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305004 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305005 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05305006 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005007 return 0;
5008
Hariprasad Shenai78294512016-08-11 21:06:23 +05305009 free_adapter:
5010 kfree(adapter);
5011 free_pci_region:
5012 iounmap(regs);
5013 pci_disable_sriov(pdev);
5014 pci_release_regions(pdev);
5015 return err;
5016#else
5017 return 0;
5018#endif
5019
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005020 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005021 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305022 if (adapter->flags & USING_MSIX)
5023 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305024 if (adapter->num_uld || adapter->num_ofld_uld)
5025 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005026 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305027 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005028 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005029 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005030 if (adapter->workq)
5031 destroy_workqueue(adapter->workq);
5032
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305033 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005034 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305035 out_unmap_bar0:
5036 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005037 out_disable_device:
5038 pci_disable_pcie_error_reporting(pdev);
5039 pci_disable_device(pdev);
5040 out_release_regions:
5041 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005042 return err;
5043}
5044
Bill Pemberton91744942012-12-03 09:23:02 -05005045static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005046{
5047 struct adapter *adapter = pci_get_drvdata(pdev);
5048
Hariprasad Shenai78294512016-08-11 21:06:23 +05305049 if (!adapter) {
5050 pci_release_regions(pdev);
5051 return;
5052 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005053
Hariprasad Shenai78294512016-08-11 21:06:23 +05305054 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005055 int i;
5056
Anish Bhatt29aaee62014-08-20 13:44:06 -07005057 /* Tear down per-adapter Work Queue first since it can contain
5058 * references to our adapter data structure.
5059 */
5060 destroy_workqueue(adapter->workq);
5061
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305062 if (is_uld(adapter))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005063 detach_ulds(adapter);
5064
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305065 disable_interrupts(adapter);
5066
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005067 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005068 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005069 unregister_netdev(adapter->port[i]);
5070
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005071 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005072
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005073 /* If we allocated filters, free up state associated with any
5074 * valid filters ...
5075 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305076 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005077
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005078 if (adapter->flags & FULL_INIT_DONE)
5079 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005080
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305081 if (adapter->flags & USING_MSIX)
5082 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305083 if (adapter->num_uld || adapter->num_ofld_uld)
5084 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005085 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005086#if IS_ENABLED(CONFIG_IPV6)
5087 t4_cleanup_clip_tbl(adapter);
5088#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005089 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305090 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005091 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005092 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005093 if ((adapter->flags & DEV_ENABLED)) {
5094 pci_disable_device(pdev);
5095 adapter->flags &= ~DEV_ENABLED;
5096 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005097 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305098 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005099 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005100 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305101 }
5102#ifdef CONFIG_PCI_IOV
5103 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305104 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305105 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305106 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305107 kfree(adapter->vfinfo);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305108 kfree(adapter);
5109 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005110 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305111 }
5112#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005113}
5114
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305115/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5116 * delivery. This is essentially a stripped down version of the PCI remove()
5117 * function where we do the minimal amount of work necessary to shutdown any
5118 * further activity.
5119 */
5120static void shutdown_one(struct pci_dev *pdev)
5121{
5122 struct adapter *adapter = pci_get_drvdata(pdev);
5123
5124 /* As with remove_one() above (see extended comment), we only want do
5125 * do cleanup on PCI Devices which went all the way through init_one()
5126 * ...
5127 */
5128 if (!adapter) {
5129 pci_release_regions(pdev);
5130 return;
5131 }
5132
5133 if (adapter->pf == 4) {
5134 int i;
5135
5136 for_each_port(adapter, i)
5137 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5138 cxgb_close(adapter->port[i]);
5139
5140 t4_uld_clean_up(adapter);
5141 disable_interrupts(adapter);
5142 disable_msi(adapter);
5143
5144 t4_sge_stop(adapter);
5145 if (adapter->flags & FW_OK)
5146 t4_fw_bye(adapter, adapter->mbox);
5147 }
5148#ifdef CONFIG_PCI_IOV
5149 else {
5150 if (adapter->port[0])
5151 unregister_netdev(adapter->port[0]);
5152 iounmap(adapter->regs);
5153 kfree(adapter->vfinfo);
5154 kfree(adapter);
5155 pci_disable_sriov(pdev);
5156 pci_release_regions(pdev);
5157 }
5158#endif
5159}
5160
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005161static struct pci_driver cxgb4_driver = {
5162 .name = KBUILD_MODNAME,
5163 .id_table = cxgb4_pci_tbl,
5164 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005165 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305166 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305167#ifdef CONFIG_PCI_IOV
5168 .sriov_configure = cxgb4_iov_configure,
5169#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005170 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005171};
5172
5173static int __init cxgb4_init_module(void)
5174{
5175 int ret;
5176
5177 /* Debugfs support is optional, just warn if this fails */
5178 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5179 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005180 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005181
5182 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005183 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005184 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305185
Anish Bhatt1bb60372014-10-14 20:07:22 -07005186#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005187 if (!inet6addr_registered) {
5188 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5189 inet6addr_registered = true;
5190 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005191#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305192
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005193 return ret;
5194}
5195
5196static void __exit cxgb4_cleanup_module(void)
5197{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005198#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305199 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005200 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5201 inet6addr_registered = false;
5202 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005203#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005204 pci_unregister_driver(&cxgb4_driver);
5205 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005206}
5207
5208module_init(cxgb4_init_module);
5209module_exit(cxgb4_cleanup_module);