blob: 449884f8dd67267eddc530006d56144b0d6c814f [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++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530533 if (txq->q_type == CXGB4_TXQ_ETH) {
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 {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530539 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000540
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530541 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000542 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
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000888 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530889 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000890 * have RDMA queues, and that's the right value.
891 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530892 if (rxq_info)
893 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
894
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000895 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530896 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000897 if (err)
898 goto freeout;
899 }
900
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530901 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530902 MPS_TRC_RSS_CONTROL_A :
903 MPS_T5_TRC_RSS_CONTROL_A,
904 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
905 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000906 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530907freeout:
908 t4_free_sge_resources(adap);
909 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000910}
911
912/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000913 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
914 * The allocated memory is cleared.
915 */
916void *t4_alloc_mem(size_t size)
917{
Joe Perches8be04b92013-06-19 12:15:53 -0700918 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000919
920 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +0000921 p = vzalloc(size);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000922 return p;
923}
924
925/*
926 * Free memory allocated through alloc_mem().
927 */
Hariprasad Shenaifd88b312014-11-07 09:35:23 +0530928void t4_free_mem(void *addr)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000929{
Pekka Enbergd2fcb542015-06-30 14:59:12 -0700930 kvfree(addr);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000931}
932
Anish Bhatt688848b2014-06-19 21:37:13 -0700933static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
934 void *accel_priv, select_queue_fallback_t fallback)
935{
936 int txq;
937
938#ifdef CONFIG_CHELSIO_T4_DCB
939 /* If a Data Center Bridging has been successfully negotiated on this
940 * link then we'll use the skb's priority to map it to a TX Queue.
941 * The skb's priority is determined via the VLAN Tag Priority Code
942 * Point field.
943 */
944 if (cxgb4_dcb_enabled(dev)) {
945 u16 vlan_tci;
946 int err;
947
948 err = vlan_get_tag(skb, &vlan_tci);
949 if (unlikely(err)) {
950 if (net_ratelimit())
951 netdev_warn(dev,
952 "TX Packet without VLAN Tag on DCB Link\n");
953 txq = 0;
954 } else {
955 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530956#ifdef CONFIG_CHELSIO_T4_FCOE
957 if (skb->protocol == htons(ETH_P_FCOE))
958 txq = skb->priority & 0x7;
959#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700960 }
961 return txq;
962 }
963#endif /* CONFIG_CHELSIO_T4_DCB */
964
965 if (select_queue) {
966 txq = (skb_rx_queue_recorded(skb)
967 ? skb_get_rx_queue(skb)
968 : smp_processor_id());
969
970 while (unlikely(txq >= dev->real_num_tx_queues))
971 txq -= dev->real_num_tx_queues;
972
973 return txq;
974 }
975
976 return fallback(dev, skb) % dev->real_num_tx_queues;
977}
978
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000979static int closest_timer(const struct sge *s, int time)
980{
981 int i, delta, match = 0, min_delta = INT_MAX;
982
983 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
984 delta = time - s->timer_val[i];
985 if (delta < 0)
986 delta = -delta;
987 if (delta < min_delta) {
988 min_delta = delta;
989 match = i;
990 }
991 }
992 return match;
993}
994
995static int closest_thres(const struct sge *s, int thres)
996{
997 int i, delta, match = 0, min_delta = INT_MAX;
998
999 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1000 delta = thres - s->counter_val[i];
1001 if (delta < 0)
1002 delta = -delta;
1003 if (delta < min_delta) {
1004 min_delta = delta;
1005 match = i;
1006 }
1007 }
1008 return match;
1009}
1010
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001011/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301012 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001013 * @q: the Rx queue
1014 * @us: the hold-off time in us, or 0 to disable timer
1015 * @cnt: the hold-off packet count, or 0 to disable counter
1016 *
1017 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1018 * one of the two needs to be enabled for the queue to generate interrupts.
1019 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301020int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1021 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001022{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301023 struct adapter *adap = q->adap;
1024
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001025 if ((us | cnt) == 0)
1026 cnt = 1;
1027
1028 if (cnt) {
1029 int err;
1030 u32 v, new_idx;
1031
1032 new_idx = closest_thres(&adap->sge, cnt);
1033 if (q->desc && q->pktcnt_idx != new_idx) {
1034 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301035 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1036 FW_PARAMS_PARAM_X_V(
1037 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1038 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301039 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1040 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001041 if (err)
1042 return err;
1043 }
1044 q->pktcnt_idx = new_idx;
1045 }
1046
1047 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301048 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001049 return 0;
1050}
1051
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001052static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001053{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001054 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001055 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001056 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001057
Patrick McHardyf6469682013-04-19 02:04:27 +00001058 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001059 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001060
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301061 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001062 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001063 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001064 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001065 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001066 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001067}
1068
Bill Pemberton91744942012-12-03 09:23:02 -05001069static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001070{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001071 if (IS_ERR_OR_NULL(adap->debugfs_root))
1072 return -1;
1073
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301074#ifdef CONFIG_DEBUG_FS
1075 t4_setup_debugfs(adap);
1076#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001077 return 0;
1078}
1079
1080/*
1081 * upper-layer driver support
1082 */
1083
1084/*
1085 * Allocate an active-open TID and set it to the supplied value.
1086 */
1087int cxgb4_alloc_atid(struct tid_info *t, void *data)
1088{
1089 int atid = -1;
1090
1091 spin_lock_bh(&t->atid_lock);
1092 if (t->afree) {
1093 union aopen_entry *p = t->afree;
1094
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001095 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001096 t->afree = p->next;
1097 p->data = data;
1098 t->atids_in_use++;
1099 }
1100 spin_unlock_bh(&t->atid_lock);
1101 return atid;
1102}
1103EXPORT_SYMBOL(cxgb4_alloc_atid);
1104
1105/*
1106 * Release an active-open TID.
1107 */
1108void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1109{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001110 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001111
1112 spin_lock_bh(&t->atid_lock);
1113 p->next = t->afree;
1114 t->afree = p;
1115 t->atids_in_use--;
1116 spin_unlock_bh(&t->atid_lock);
1117}
1118EXPORT_SYMBOL(cxgb4_free_atid);
1119
1120/*
1121 * Allocate a server TID and set it to the supplied value.
1122 */
1123int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1124{
1125 int stid;
1126
1127 spin_lock_bh(&t->stid_lock);
1128 if (family == PF_INET) {
1129 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1130 if (stid < t->nstids)
1131 __set_bit(stid, t->stid_bmap);
1132 else
1133 stid = -1;
1134 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301135 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001136 if (stid < 0)
1137 stid = -1;
1138 }
1139 if (stid >= 0) {
1140 t->stid_tab[stid].data = data;
1141 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301142 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1143 * This is equivalent to 4 TIDs. With CLIP enabled it
1144 * needs 2 TIDs.
1145 */
1146 if (family == PF_INET)
1147 t->stids_in_use++;
1148 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301149 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001150 }
1151 spin_unlock_bh(&t->stid_lock);
1152 return stid;
1153}
1154EXPORT_SYMBOL(cxgb4_alloc_stid);
1155
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001156/* Allocate a server filter TID and set it to the supplied value.
1157 */
1158int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1159{
1160 int stid;
1161
1162 spin_lock_bh(&t->stid_lock);
1163 if (family == PF_INET) {
1164 stid = find_next_zero_bit(t->stid_bmap,
1165 t->nstids + t->nsftids, t->nstids);
1166 if (stid < (t->nstids + t->nsftids))
1167 __set_bit(stid, t->stid_bmap);
1168 else
1169 stid = -1;
1170 } else {
1171 stid = -1;
1172 }
1173 if (stid >= 0) {
1174 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301175 stid -= t->nstids;
1176 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301177 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001178 }
1179 spin_unlock_bh(&t->stid_lock);
1180 return stid;
1181}
1182EXPORT_SYMBOL(cxgb4_alloc_sftid);
1183
1184/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001185 */
1186void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1187{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301188 /* Is it a server filter TID? */
1189 if (t->nsftids && (stid >= t->sftid_base)) {
1190 stid -= t->sftid_base;
1191 stid += t->nstids;
1192 } else {
1193 stid -= t->stid_base;
1194 }
1195
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001196 spin_lock_bh(&t->stid_lock);
1197 if (family == PF_INET)
1198 __clear_bit(stid, t->stid_bmap);
1199 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301200 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001201 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301202 if (stid < t->nstids) {
1203 if (family == PF_INET)
1204 t->stids_in_use--;
1205 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301206 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301207 } else {
1208 t->sftids_in_use--;
1209 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001210 spin_unlock_bh(&t->stid_lock);
1211}
1212EXPORT_SYMBOL(cxgb4_free_stid);
1213
1214/*
1215 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1216 */
1217static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1218 unsigned int tid)
1219{
1220 struct cpl_tid_release *req;
1221
1222 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1223 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1224 INIT_TP_WR(req, tid);
1225 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1226}
1227
1228/*
1229 * Queue a TID release request and if necessary schedule a work queue to
1230 * process it.
1231 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001232static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1233 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001234{
1235 void **p = &t->tid_tab[tid];
1236 struct adapter *adap = container_of(t, struct adapter, tids);
1237
1238 spin_lock_bh(&adap->tid_release_lock);
1239 *p = adap->tid_release_head;
1240 /* Low 2 bits encode the Tx channel number */
1241 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1242 if (!adap->tid_release_task_busy) {
1243 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001244 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001245 }
1246 spin_unlock_bh(&adap->tid_release_lock);
1247}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001248
1249/*
1250 * Process the list of pending TID release requests.
1251 */
1252static void process_tid_release_list(struct work_struct *work)
1253{
1254 struct sk_buff *skb;
1255 struct adapter *adap;
1256
1257 adap = container_of(work, struct adapter, tid_release_task);
1258
1259 spin_lock_bh(&adap->tid_release_lock);
1260 while (adap->tid_release_head) {
1261 void **p = adap->tid_release_head;
1262 unsigned int chan = (uintptr_t)p & 3;
1263 p = (void *)p - chan;
1264
1265 adap->tid_release_head = *p;
1266 *p = NULL;
1267 spin_unlock_bh(&adap->tid_release_lock);
1268
1269 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1270 GFP_KERNEL)))
1271 schedule_timeout_uninterruptible(1);
1272
1273 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1274 t4_ofld_send(adap, skb);
1275 spin_lock_bh(&adap->tid_release_lock);
1276 }
1277 adap->tid_release_task_busy = false;
1278 spin_unlock_bh(&adap->tid_release_lock);
1279}
1280
1281/*
1282 * Release a TID and inform HW. If we are unable to allocate the release
1283 * message we defer to a work queue.
1284 */
1285void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1286{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001287 struct sk_buff *skb;
1288 struct adapter *adap = container_of(t, struct adapter, tids);
1289
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301290 WARN_ON(tid >= t->ntids);
1291
1292 if (t->tid_tab[tid]) {
1293 t->tid_tab[tid] = NULL;
1294 if (t->hash_base && (tid >= t->hash_base))
1295 atomic_dec(&t->hash_tids_in_use);
1296 else
1297 atomic_dec(&t->tids_in_use);
1298 }
1299
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001300 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1301 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001302 mk_tid_release(skb, chan, tid);
1303 t4_ofld_send(adap, skb);
1304 } else
1305 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001306}
1307EXPORT_SYMBOL(cxgb4_remove_tid);
1308
1309/*
1310 * Allocate and initialize the TID tables. Returns 0 on success.
1311 */
1312static int tid_init(struct tid_info *t)
1313{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301314 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301315 unsigned int max_ftids = t->nftids + t->nsftids;
1316 unsigned int natids = t->natids;
1317 unsigned int stid_bmap_size;
1318 unsigned int ftid_bmap_size;
1319 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001320
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001321 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301322 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001323 size = t->ntids * sizeof(*t->tid_tab) +
1324 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001325 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001326 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001327 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301328 max_ftids * sizeof(*t->ftid_tab) +
1329 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001330
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001331 t->tid_tab = t4_alloc_mem(size);
1332 if (!t->tid_tab)
1333 return -ENOMEM;
1334
1335 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1336 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001337 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001338 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301339 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001340 spin_lock_init(&t->stid_lock);
1341 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301342 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001343
1344 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301345 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001346 t->afree = NULL;
1347 t->atids_in_use = 0;
1348 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301349 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001350
1351 /* Setup the free list for atid_tab and clear the stid bitmap. */
1352 if (natids) {
1353 while (--natids)
1354 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1355 t->afree = t->atid_tab;
1356 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301357
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301358 if (is_offload(adap)) {
1359 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1360 /* Reserve stid 0 for T4/T5 adapters */
1361 if (!t->stid_base &&
1362 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1363 __set_bit(0, t->stid_bmap);
1364 }
1365
1366 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001367 return 0;
1368}
1369
1370/**
1371 * cxgb4_create_server - create an IP server
1372 * @dev: the device
1373 * @stid: the server TID
1374 * @sip: local IP address to bind server to
1375 * @sport: the server's TCP port
1376 * @queue: queue to direct messages from this server to
1377 *
1378 * Create an IP server for the given port and address.
1379 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1380 */
1381int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001382 __be32 sip, __be16 sport, __be16 vlan,
1383 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001384{
1385 unsigned int chan;
1386 struct sk_buff *skb;
1387 struct adapter *adap;
1388 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301389 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001390
1391 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1392 if (!skb)
1393 return -ENOMEM;
1394
1395 adap = netdev2adap(dev);
1396 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1397 INIT_TP_WR(req, 0);
1398 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1399 req->local_port = sport;
1400 req->peer_port = htons(0);
1401 req->local_ip = sip;
1402 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001403 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001404 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001405 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1406 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301407 ret = t4_mgmt_tx(adap, skb);
1408 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001409}
1410EXPORT_SYMBOL(cxgb4_create_server);
1411
Vipul Pandya80f40c12013-07-04 16:10:45 +05301412/* cxgb4_create_server6 - create an IPv6 server
1413 * @dev: the device
1414 * @stid: the server TID
1415 * @sip: local IPv6 address to bind server to
1416 * @sport: the server's TCP port
1417 * @queue: queue to direct messages from this server to
1418 *
1419 * Create an IPv6 server for the given port and address.
1420 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1421 */
1422int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1423 const struct in6_addr *sip, __be16 sport,
1424 unsigned int queue)
1425{
1426 unsigned int chan;
1427 struct sk_buff *skb;
1428 struct adapter *adap;
1429 struct cpl_pass_open_req6 *req;
1430 int ret;
1431
1432 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1433 if (!skb)
1434 return -ENOMEM;
1435
1436 adap = netdev2adap(dev);
1437 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1438 INIT_TP_WR(req, 0);
1439 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1440 req->local_port = sport;
1441 req->peer_port = htons(0);
1442 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1443 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1444 req->peer_ip_hi = cpu_to_be64(0);
1445 req->peer_ip_lo = cpu_to_be64(0);
1446 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001447 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001448 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1449 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301450 ret = t4_mgmt_tx(adap, skb);
1451 return net_xmit_eval(ret);
1452}
1453EXPORT_SYMBOL(cxgb4_create_server6);
1454
1455int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1456 unsigned int queue, bool ipv6)
1457{
1458 struct sk_buff *skb;
1459 struct adapter *adap;
1460 struct cpl_close_listsvr_req *req;
1461 int ret;
1462
1463 adap = netdev2adap(dev);
1464
1465 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1466 if (!skb)
1467 return -ENOMEM;
1468
1469 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1470 INIT_TP_WR(req, 0);
1471 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001472 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1473 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301474 ret = t4_mgmt_tx(adap, skb);
1475 return net_xmit_eval(ret);
1476}
1477EXPORT_SYMBOL(cxgb4_remove_server);
1478
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001479/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001480 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1481 * @mtus: the HW MTU table
1482 * @mtu: the target MTU
1483 * @idx: index of selected entry in the MTU table
1484 *
1485 * Returns the index and the value in the HW MTU table that is closest to
1486 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1487 * table, in which case that smallest available value is selected.
1488 */
1489unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1490 unsigned int *idx)
1491{
1492 unsigned int i = 0;
1493
1494 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1495 ++i;
1496 if (idx)
1497 *idx = i;
1498 return mtus[i];
1499}
1500EXPORT_SYMBOL(cxgb4_best_mtu);
1501
1502/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301503 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1504 * @mtus: the HW MTU table
1505 * @header_size: Header Size
1506 * @data_size_max: maximum Data Segment Size
1507 * @data_size_align: desired Data Segment Size Alignment (2^N)
1508 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1509 *
1510 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1511 * MTU Table based solely on a Maximum MTU parameter, we break that
1512 * parameter up into a Header Size and Maximum Data Segment Size, and
1513 * provide a desired Data Segment Size Alignment. If we find an MTU in
1514 * the Hardware MTU Table which will result in a Data Segment Size with
1515 * the requested alignment _and_ that MTU isn't "too far" from the
1516 * closest MTU, then we'll return that rather than the closest MTU.
1517 */
1518unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1519 unsigned short header_size,
1520 unsigned short data_size_max,
1521 unsigned short data_size_align,
1522 unsigned int *mtu_idxp)
1523{
1524 unsigned short max_mtu = header_size + data_size_max;
1525 unsigned short data_size_align_mask = data_size_align - 1;
1526 int mtu_idx, aligned_mtu_idx;
1527
1528 /* Scan the MTU Table till we find an MTU which is larger than our
1529 * Maximum MTU or we reach the end of the table. Along the way,
1530 * record the last MTU found, if any, which will result in a Data
1531 * Segment Length matching the requested alignment.
1532 */
1533 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1534 unsigned short data_size = mtus[mtu_idx] - header_size;
1535
1536 /* If this MTU minus the Header Size would result in a
1537 * Data Segment Size of the desired alignment, remember it.
1538 */
1539 if ((data_size & data_size_align_mask) == 0)
1540 aligned_mtu_idx = mtu_idx;
1541
1542 /* If we're not at the end of the Hardware MTU Table and the
1543 * next element is larger than our Maximum MTU, drop out of
1544 * the loop.
1545 */
1546 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1547 break;
1548 }
1549
1550 /* If we fell out of the loop because we ran to the end of the table,
1551 * then we just have to use the last [largest] entry.
1552 */
1553 if (mtu_idx == NMTUS)
1554 mtu_idx--;
1555
1556 /* If we found an MTU which resulted in the requested Data Segment
1557 * Length alignment and that's "not far" from the largest MTU which is
1558 * less than or equal to the maximum MTU, then use that.
1559 */
1560 if (aligned_mtu_idx >= 0 &&
1561 mtu_idx - aligned_mtu_idx <= 1)
1562 mtu_idx = aligned_mtu_idx;
1563
1564 /* If the caller has passed in an MTU Index pointer, pass the
1565 * MTU Index back. Return the MTU value.
1566 */
1567 if (mtu_idxp)
1568 *mtu_idxp = mtu_idx;
1569 return mtus[mtu_idx];
1570}
1571EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1572
1573/**
Hariprasad S27999802015-09-23 17:19:26 +05301574 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1575 * @chip: chip type
1576 * @viid: VI id of the given port
1577 *
1578 * Return the SMT index for this VI.
1579 */
1580unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1581{
1582 /* In T4/T5, SMT contains 256 SMAC entries organized in
1583 * 128 rows of 2 entries each.
1584 * In T6, SMT contains 256 SMAC entries in 256 rows.
1585 * TODO: The below code needs to be updated when we add support
1586 * for 256 VFs.
1587 */
1588 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1589 return ((viid & 0x7f) << 1);
1590 else
1591 return (viid & 0x7f);
1592}
1593EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1594
1595/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001596 * cxgb4_port_chan - get the HW channel of a port
1597 * @dev: the net device for the port
1598 *
1599 * Return the HW Tx channel of the given port.
1600 */
1601unsigned int cxgb4_port_chan(const struct net_device *dev)
1602{
1603 return netdev2pinfo(dev)->tx_chan;
1604}
1605EXPORT_SYMBOL(cxgb4_port_chan);
1606
Vipul Pandya881806b2012-05-18 15:29:24 +05301607unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1608{
1609 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001610 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301611
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301612 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1613 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301614 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301615 lp_count = LP_COUNT_G(v1);
1616 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001617 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301618 lp_count = LP_COUNT_T5_G(v1);
1619 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001620 }
1621 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301622}
1623EXPORT_SYMBOL(cxgb4_dbfifo_count);
1624
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001625/**
1626 * cxgb4_port_viid - get the VI id of a port
1627 * @dev: the net device for the port
1628 *
1629 * Return the VI id of the given port.
1630 */
1631unsigned int cxgb4_port_viid(const struct net_device *dev)
1632{
1633 return netdev2pinfo(dev)->viid;
1634}
1635EXPORT_SYMBOL(cxgb4_port_viid);
1636
1637/**
1638 * cxgb4_port_idx - get the index of a port
1639 * @dev: the net device for the port
1640 *
1641 * Return the index of the given port.
1642 */
1643unsigned int cxgb4_port_idx(const struct net_device *dev)
1644{
1645 return netdev2pinfo(dev)->port_id;
1646}
1647EXPORT_SYMBOL(cxgb4_port_idx);
1648
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001649void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1650 struct tp_tcp_stats *v6)
1651{
1652 struct adapter *adap = pci_get_drvdata(pdev);
1653
1654 spin_lock(&adap->stats_lock);
1655 t4_tp_get_tcp_stats(adap, v4, v6);
1656 spin_unlock(&adap->stats_lock);
1657}
1658EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1659
1660void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1661 const unsigned int *pgsz_order)
1662{
1663 struct adapter *adap = netdev2adap(dev);
1664
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301665 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1666 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1667 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1668 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001669}
1670EXPORT_SYMBOL(cxgb4_iscsi_init);
1671
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301672int cxgb4_flush_eq_cache(struct net_device *dev)
1673{
1674 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301675
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301676 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301677}
1678EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1679
1680static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1681{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301682 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301683 __be64 indices;
1684 int ret;
1685
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301686 spin_lock(&adap->win0_lock);
1687 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1688 sizeof(indices), (__be32 *)&indices,
1689 T4_MEMORY_READ);
1690 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301691 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001692 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1693 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301694 }
1695 return ret;
1696}
1697
1698int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1699 u16 size)
1700{
1701 struct adapter *adap = netdev2adap(dev);
1702 u16 hw_pidx, hw_cidx;
1703 int ret;
1704
1705 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1706 if (ret)
1707 goto out;
1708
1709 if (pidx != hw_pidx) {
1710 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301711 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301712
1713 if (pidx >= hw_pidx)
1714 delta = pidx - hw_pidx;
1715 else
1716 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301717
1718 if (is_t4(adap->params.chip))
1719 val = PIDX_V(delta);
1720 else
1721 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301722 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301723 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1724 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301725 }
1726out:
1727 return ret;
1728}
1729EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1730
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301731int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1732{
1733 struct adapter *adap;
1734 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301735 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301736 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1737 int ret;
1738
1739 adap = netdev2adap(dev);
1740
1741 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1742
1743 /* Figure out where the offset lands in the Memory Type/Address scheme.
1744 * This code assumes that the memory is laid out starting at offset 0
1745 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1746 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1747 * MC0, and some have both MC0 and MC1.
1748 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301749 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1750 edc0_size = EDRAM0_SIZE_G(size) << 20;
1751 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1752 edc1_size = EDRAM1_SIZE_G(size) << 20;
1753 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1754 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301755
1756 edc0_end = edc0_size;
1757 edc1_end = edc0_end + edc1_size;
1758 mc0_end = edc1_end + mc0_size;
1759
1760 if (offset < edc0_end) {
1761 memtype = MEM_EDC0;
1762 memaddr = offset;
1763 } else if (offset < edc1_end) {
1764 memtype = MEM_EDC1;
1765 memaddr = offset - edc0_end;
1766 } else {
1767 if (offset < mc0_end) {
1768 memtype = MEM_MC0;
1769 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301770 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301771 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1772 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301773 mc1_end = mc0_end + mc1_size;
1774 if (offset < mc1_end) {
1775 memtype = MEM_MC1;
1776 memaddr = offset - mc0_end;
1777 } else {
1778 /* offset beyond the end of any memory */
1779 goto err;
1780 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301781 } else {
1782 /* T4/T6 only has a single memory channel */
1783 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301784 }
1785 }
1786
1787 spin_lock(&adap->win0_lock);
1788 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1789 spin_unlock(&adap->win0_lock);
1790 return ret;
1791
1792err:
1793 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1794 stag, offset);
1795 return -EINVAL;
1796}
1797EXPORT_SYMBOL(cxgb4_read_tpte);
1798
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301799u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1800{
1801 u32 hi, lo;
1802 struct adapter *adap;
1803
1804 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301805 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1806 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301807
1808 return ((u64)hi << 32) | (u64)lo;
1809}
1810EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1811
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301812int cxgb4_bar2_sge_qregs(struct net_device *dev,
1813 unsigned int qid,
1814 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301815 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301816 u64 *pbar2_qoffset,
1817 unsigned int *pbar2_qid)
1818{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301819 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301820 qid,
1821 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1822 ? T4_BAR2_QTYPE_EGRESS
1823 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301824 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301825 pbar2_qoffset,
1826 pbar2_qid);
1827}
1828EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1829
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001830static struct pci_driver cxgb4_driver;
1831
1832static void check_neigh_update(struct neighbour *neigh)
1833{
1834 const struct device *parent;
1835 const struct net_device *netdev = neigh->dev;
1836
1837 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1838 netdev = vlan_dev_real_dev(netdev);
1839 parent = netdev->dev.parent;
1840 if (parent && parent->driver == &cxgb4_driver.driver)
1841 t4_l2t_update(dev_get_drvdata(parent), neigh);
1842}
1843
1844static int netevent_cb(struct notifier_block *nb, unsigned long event,
1845 void *data)
1846{
1847 switch (event) {
1848 case NETEVENT_NEIGH_UPDATE:
1849 check_neigh_update(data);
1850 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001851 case NETEVENT_REDIRECT:
1852 default:
1853 break;
1854 }
1855 return 0;
1856}
1857
1858static bool netevent_registered;
1859static struct notifier_block cxgb4_netevent_nb = {
1860 .notifier_call = netevent_cb
1861};
1862
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301863static void drain_db_fifo(struct adapter *adap, int usecs)
1864{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001865 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301866
1867 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301868 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1869 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301870 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301871 lp_count = LP_COUNT_G(v1);
1872 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001873 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301874 lp_count = LP_COUNT_T5_G(v1);
1875 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001876 }
1877
1878 if (lp_count == 0 && hp_count == 0)
1879 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301880 set_current_state(TASK_UNINTERRUPTIBLE);
1881 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301882 } while (1);
1883}
1884
1885static void disable_txq_db(struct sge_txq *q)
1886{
Steve Wise05eb2382014-03-14 21:52:08 +05301887 unsigned long flags;
1888
1889 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301890 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301891 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301892}
1893
Steve Wise05eb2382014-03-14 21:52:08 +05301894static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301895{
1896 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301897 if (q->db_pidx_inc) {
1898 /* Make sure that all writes to the TX descriptors
1899 * are committed before we tell HW about them.
1900 */
1901 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301902 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1903 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301904 q->db_pidx_inc = 0;
1905 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301906 q->db_disabled = 0;
1907 spin_unlock_irq(&q->db_lock);
1908}
1909
1910static void disable_dbs(struct adapter *adap)
1911{
1912 int i;
1913
1914 for_each_ethrxq(&adap->sge, i)
1915 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301916 if (is_offload(adap)) {
1917 struct sge_uld_txq_info *txq_info =
1918 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1919
1920 if (txq_info) {
1921 for_each_ofldtxq(&adap->sge, i) {
1922 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1923
1924 disable_txq_db(&txq->q);
1925 }
1926 }
1927 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301928 for_each_port(adap, i)
1929 disable_txq_db(&adap->sge.ctrlq[i].q);
1930}
1931
1932static void enable_dbs(struct adapter *adap)
1933{
1934 int i;
1935
1936 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301937 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301938 if (is_offload(adap)) {
1939 struct sge_uld_txq_info *txq_info =
1940 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1941
1942 if (txq_info) {
1943 for_each_ofldtxq(&adap->sge, i) {
1944 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1945
1946 enable_txq_db(adap, &txq->q);
1947 }
1948 }
1949 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301950 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301951 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1952}
1953
1954static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1955{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301956 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1957
1958 if (adap->uld && adap->uld[type].handle)
1959 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301960}
1961
1962static void process_db_full(struct work_struct *work)
1963{
1964 struct adapter *adap;
1965
1966 adap = container_of(work, struct adapter, db_full_task);
1967
1968 drain_db_fifo(adap, dbfifo_drain_delay);
1969 enable_dbs(adap);
1970 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301971 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1972 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1973 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1974 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1975 else
1976 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1977 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301978}
1979
1980static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1981{
1982 u16 hw_pidx, hw_cidx;
1983 int ret;
1984
Steve Wise05eb2382014-03-14 21:52:08 +05301985 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301986 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1987 if (ret)
1988 goto out;
1989 if (q->db_pidx != hw_pidx) {
1990 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301991 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301992
1993 if (q->db_pidx >= hw_pidx)
1994 delta = q->db_pidx - hw_pidx;
1995 else
1996 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301997
1998 if (is_t4(adap->params.chip))
1999 val = PIDX_V(delta);
2000 else
2001 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302002 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302003 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2004 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302005 }
2006out:
2007 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302008 q->db_pidx_inc = 0;
2009 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302010 if (ret)
2011 CH_WARN(adap, "DB drop recovery failed.\n");
2012}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302013
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302014static void recover_all_queues(struct adapter *adap)
2015{
2016 int i;
2017
2018 for_each_ethrxq(&adap->sge, i)
2019 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302020 if (is_offload(adap)) {
2021 struct sge_uld_txq_info *txq_info =
2022 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2023 if (txq_info) {
2024 for_each_ofldtxq(&adap->sge, i) {
2025 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2026
2027 sync_txq_pidx(adap, &txq->q);
2028 }
2029 }
2030 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302031 for_each_port(adap, i)
2032 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2033}
2034
Vipul Pandya881806b2012-05-18 15:29:24 +05302035static void process_db_drop(struct work_struct *work)
2036{
2037 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302038
Vipul Pandya881806b2012-05-18 15:29:24 +05302039 adap = container_of(work, struct adapter, db_drop_task);
2040
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302041 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302042 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002043 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302044 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002045 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302046 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002047 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302048 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302049 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002050 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2051 u16 qid = (dropped_db >> 15) & 0x1ffff;
2052 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302053 u64 bar2_qoffset;
2054 unsigned int bar2_qid;
2055 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002056
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302057 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002058 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302059 if (ret)
2060 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2061 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2062 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302063 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302064 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002065
2066 /* Re-enable BAR2 WC */
2067 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2068 }
2069
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302070 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2071 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302072}
2073
2074void t4_db_full(struct adapter *adap)
2075{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302076 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302077 disable_dbs(adap);
2078 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302079 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2080 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002081 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002082 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302083}
2084
2085void t4_db_dropped(struct adapter *adap)
2086{
Steve Wise05eb2382014-03-14 21:52:08 +05302087 if (is_t4(adap->params.chip)) {
2088 disable_dbs(adap);
2089 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2090 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002091 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302092}
2093
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302094void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002095{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002096 if (!netevent_registered) {
2097 register_netevent_notifier(&cxgb4_netevent_nb);
2098 netevent_registered = true;
2099 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002100}
2101
2102static void detach_ulds(struct adapter *adap)
2103{
2104 unsigned int i;
2105
2106 mutex_lock(&uld_mutex);
2107 list_del(&adap->list_node);
2108 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302109 if (adap->uld && adap->uld[i].handle) {
2110 adap->uld[i].state_change(adap->uld[i].handle,
2111 CXGB4_STATE_DETACH);
2112 adap->uld[i].handle = NULL;
2113 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002114 if (netevent_registered && list_empty(&adapter_list)) {
2115 unregister_netevent_notifier(&cxgb4_netevent_nb);
2116 netevent_registered = false;
2117 }
2118 mutex_unlock(&uld_mutex);
2119}
2120
2121static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2122{
2123 unsigned int i;
2124
2125 mutex_lock(&uld_mutex);
2126 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302127 if (adap->uld && adap->uld[i].handle)
2128 adap->uld[i].state_change(adap->uld[i].handle,
2129 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002130 mutex_unlock(&uld_mutex);
2131}
2132
Anish Bhatt1bb60372014-10-14 20:07:22 -07002133#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002134static int cxgb4_inet6addr_handler(struct notifier_block *this,
2135 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302136{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002137 struct inet6_ifaddr *ifa = data;
2138 struct net_device *event_dev = ifa->idev->dev;
2139 const struct device *parent = NULL;
2140#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302141 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002142#endif
2143 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2144 event_dev = vlan_dev_real_dev(event_dev);
2145#if IS_ENABLED(CONFIG_BONDING)
2146 if (event_dev->flags & IFF_MASTER) {
2147 list_for_each_entry(adap, &adapter_list, list_node) {
2148 switch (event) {
2149 case NETDEV_UP:
2150 cxgb4_clip_get(adap->port[0],
2151 (const u32 *)ifa, 1);
2152 break;
2153 case NETDEV_DOWN:
2154 cxgb4_clip_release(adap->port[0],
2155 (const u32 *)ifa, 1);
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161 return NOTIFY_OK;
2162 }
2163#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302164
Anish Bhattb5a02f52015-01-14 15:17:34 -08002165 if (event_dev)
2166 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302167
Anish Bhattb5a02f52015-01-14 15:17:34 -08002168 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302169 switch (event) {
2170 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002171 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302172 break;
2173 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002174 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302175 break;
2176 default:
2177 break;
2178 }
2179 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002180 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302181}
2182
Anish Bhattb5a02f52015-01-14 15:17:34 -08002183static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302184static struct notifier_block cxgb4_inet6addr_notifier = {
2185 .notifier_call = cxgb4_inet6addr_handler
2186};
2187
Vipul Pandya01bcca62013-07-04 16:10:46 +05302188static void update_clip(const struct adapter *adap)
2189{
2190 int i;
2191 struct net_device *dev;
2192 int ret;
2193
2194 rcu_read_lock();
2195
2196 for (i = 0; i < MAX_NPORTS; i++) {
2197 dev = adap->port[i];
2198 ret = 0;
2199
2200 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002201 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302202
2203 if (ret < 0)
2204 break;
2205 }
2206 rcu_read_unlock();
2207}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002208#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302209
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002210/**
2211 * cxgb_up - enable the adapter
2212 * @adap: adapter being enabled
2213 *
2214 * Called when the first port is enabled, this function performs the
2215 * actions necessary to make an adapter operational, such as completing
2216 * the initialization of HW modules, and enabling interrupts.
2217 *
2218 * Must be called with the rtnl lock held.
2219 */
2220static int cxgb_up(struct adapter *adap)
2221{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002222 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002223
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002224 err = setup_sge_queues(adap);
2225 if (err)
2226 goto out;
2227 err = setup_rss(adap);
2228 if (err)
2229 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002230
2231 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002232 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002233 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2234 adap->msix_info[0].desc, adap);
2235 if (err)
2236 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002237 err = request_msix_queue_irqs(adap);
2238 if (err) {
2239 free_irq(adap->msix_info[0].vec, adap);
2240 goto irq_err;
2241 }
2242 } else {
2243 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2244 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002245 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002246 if (err)
2247 goto irq_err;
2248 }
2249 enable_rx(adap);
2250 t4_sge_start(adap);
2251 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002252 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002253 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002254#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302255 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002256#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302257 /* Initialize hash mac addr list*/
2258 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002259 out:
2260 return err;
2261 irq_err:
2262 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002263 freeq:
2264 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002265 goto out;
2266}
2267
2268static void cxgb_down(struct adapter *adapter)
2269{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002270 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302271 cancel_work_sync(&adapter->db_full_task);
2272 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002273 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002274 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002275
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002276 t4_sge_stop(adapter);
2277 t4_free_sge_resources(adapter);
2278 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002279}
2280
2281/*
2282 * net_device operations
2283 */
2284static int cxgb_open(struct net_device *dev)
2285{
2286 int err;
2287 struct port_info *pi = netdev_priv(dev);
2288 struct adapter *adapter = pi->adapter;
2289
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002290 netif_carrier_off(dev);
2291
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002292 if (!(adapter->flags & FULL_INIT_DONE)) {
2293 err = cxgb_up(adapter);
2294 if (err < 0)
2295 return err;
2296 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002297
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002298 err = link_start(dev);
2299 if (!err)
2300 netif_tx_start_all_queues(dev);
2301 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002302}
2303
2304static int cxgb_close(struct net_device *dev)
2305{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002306 struct port_info *pi = netdev_priv(dev);
2307 struct adapter *adapter = pi->adapter;
2308
2309 netif_tx_stop_all_queues(dev);
2310 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302311 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002312}
2313
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002314int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002315 __be32 sip, __be16 sport, __be16 vlan,
2316 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002317{
2318 int ret;
2319 struct filter_entry *f;
2320 struct adapter *adap;
2321 int i;
2322 u8 *val;
2323
2324 adap = netdev2adap(dev);
2325
Vipul Pandya1cab7752012-12-10 09:30:55 +00002326 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302327 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002328 stid += adap->tids.nftids;
2329
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002330 /* Check to make sure the filter requested is writable ...
2331 */
2332 f = &adap->tids.ftid_tab[stid];
2333 ret = writable_filter(f);
2334 if (ret)
2335 return ret;
2336
2337 /* Clear out any old resources being used by the filter before
2338 * we start constructing the new filter.
2339 */
2340 if (f->valid)
2341 clear_filter(adap, f);
2342
2343 /* Clear out filter specifications */
2344 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2345 f->fs.val.lport = cpu_to_be16(sport);
2346 f->fs.mask.lport = ~0;
2347 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002348 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002349 for (i = 0; i < 4; i++) {
2350 f->fs.val.lip[i] = val[i];
2351 f->fs.mask.lip[i] = ~0;
2352 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302353 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002354 f->fs.val.iport = port;
2355 f->fs.mask.iport = mask;
2356 }
2357 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002358
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302359 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302360 f->fs.val.proto = IPPROTO_TCP;
2361 f->fs.mask.proto = ~0;
2362 }
2363
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002364 f->fs.dirsteer = 1;
2365 f->fs.iq = queue;
2366 /* Mark filter as locked */
2367 f->locked = 1;
2368 f->fs.rpttid = 1;
2369
2370 ret = set_filter_wr(adap, stid);
2371 if (ret) {
2372 clear_filter(adap, f);
2373 return ret;
2374 }
2375
2376 return 0;
2377}
2378EXPORT_SYMBOL(cxgb4_create_server_filter);
2379
2380int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2381 unsigned int queue, bool ipv6)
2382{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002383 struct filter_entry *f;
2384 struct adapter *adap;
2385
2386 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002387
2388 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302389 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002390 stid += adap->tids.nftids;
2391
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002392 f = &adap->tids.ftid_tab[stid];
2393 /* Unlock the filter */
2394 f->locked = 0;
2395
Wei Yongjun8c148462016-08-20 15:32:41 +00002396 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002397}
2398EXPORT_SYMBOL(cxgb4_remove_server_filter);
2399
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002400static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2401 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002402{
2403 struct port_stats stats;
2404 struct port_info *p = netdev_priv(dev);
2405 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002406
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002407 /* Block retrieving statistics during EEH error
2408 * recovery. Otherwise, the recovery might fail
2409 * and the PCI device will be removed permanently
2410 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002411 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002412 if (!netif_device_present(dev)) {
2413 spin_unlock(&adapter->stats_lock);
2414 return ns;
2415 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302416 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2417 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002418 spin_unlock(&adapter->stats_lock);
2419
2420 ns->tx_bytes = stats.tx_octets;
2421 ns->tx_packets = stats.tx_frames;
2422 ns->rx_bytes = stats.rx_octets;
2423 ns->rx_packets = stats.rx_frames;
2424 ns->multicast = stats.rx_mcast_frames;
2425
2426 /* detailed rx_errors */
2427 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2428 stats.rx_runt;
2429 ns->rx_over_errors = 0;
2430 ns->rx_crc_errors = stats.rx_fcs_err;
2431 ns->rx_frame_errors = stats.rx_symbol_err;
2432 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2433 stats.rx_ovflow2 + stats.rx_ovflow3 +
2434 stats.rx_trunc0 + stats.rx_trunc1 +
2435 stats.rx_trunc2 + stats.rx_trunc3;
2436 ns->rx_missed_errors = 0;
2437
2438 /* detailed tx_errors */
2439 ns->tx_aborted_errors = 0;
2440 ns->tx_carrier_errors = 0;
2441 ns->tx_fifo_errors = 0;
2442 ns->tx_heartbeat_errors = 0;
2443 ns->tx_window_errors = 0;
2444
2445 ns->tx_errors = stats.tx_error_frames;
2446 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2447 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2448 return ns;
2449}
2450
2451static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2452{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002453 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002454 int ret = 0, prtad, devad;
2455 struct port_info *pi = netdev_priv(dev);
2456 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2457
2458 switch (cmd) {
2459 case SIOCGMIIPHY:
2460 if (pi->mdio_addr < 0)
2461 return -EOPNOTSUPP;
2462 data->phy_id = pi->mdio_addr;
2463 break;
2464 case SIOCGMIIREG:
2465 case SIOCSMIIREG:
2466 if (mdio_phy_id_is_c45(data->phy_id)) {
2467 prtad = mdio_phy_id_prtad(data->phy_id);
2468 devad = mdio_phy_id_devad(data->phy_id);
2469 } else if (data->phy_id < 32) {
2470 prtad = data->phy_id;
2471 devad = 0;
2472 data->reg_num &= 0x1f;
2473 } else
2474 return -EINVAL;
2475
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302476 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002477 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002478 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002479 data->reg_num, &data->val_out);
2480 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002481 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002482 data->reg_num, data->val_in);
2483 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302484 case SIOCGHWTSTAMP:
2485 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2486 sizeof(pi->tstamp_config)) ?
2487 -EFAULT : 0;
2488 case SIOCSHWTSTAMP:
2489 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2490 sizeof(pi->tstamp_config)))
2491 return -EFAULT;
2492
2493 switch (pi->tstamp_config.rx_filter) {
2494 case HWTSTAMP_FILTER_NONE:
2495 pi->rxtstamp = false;
2496 break;
2497 case HWTSTAMP_FILTER_ALL:
2498 pi->rxtstamp = true;
2499 break;
2500 default:
2501 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2502 return -ERANGE;
2503 }
2504
2505 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2506 sizeof(pi->tstamp_config)) ?
2507 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002508 default:
2509 return -EOPNOTSUPP;
2510 }
2511 return ret;
2512}
2513
2514static void cxgb_set_rxmode(struct net_device *dev)
2515{
2516 /* unfortunately we can't return errors to the stack */
2517 set_rxmode(dev, -1, false);
2518}
2519
2520static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2521{
2522 int ret;
2523 struct port_info *pi = netdev_priv(dev);
2524
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302525 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002526 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002527 if (!ret)
2528 dev->mtu = new_mtu;
2529 return ret;
2530}
2531
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302532#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302533static int dummy_open(struct net_device *dev)
2534{
2535 /* Turn carrier off since we don't have to transmit anything on this
2536 * interface.
2537 */
2538 netif_carrier_off(dev);
2539 return 0;
2540}
2541
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302542/* Fill MAC address that will be assigned by the FW */
2543static void fill_vf_station_mac_addr(struct adapter *adap)
2544{
2545 unsigned int i;
2546 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2547 int err;
2548 u8 *na;
2549 u16 a, b;
2550
2551 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2552 if (!err) {
2553 na = adap->params.vpd.na;
2554 for (i = 0; i < ETH_ALEN; i++)
2555 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2556 hex2val(na[2 * i + 1]));
2557 a = (hw_addr[0] << 8) | hw_addr[1];
2558 b = (hw_addr[1] << 8) | hw_addr[2];
2559 a ^= b;
2560 a |= 0x0200; /* locally assigned Ethernet MAC address */
2561 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2562 macaddr[0] = a >> 8;
2563 macaddr[1] = a & 0xff;
2564
2565 for (i = 2; i < 5; i++)
2566 macaddr[i] = hw_addr[i + 1];
2567
2568 for (i = 0; i < adap->num_vfs; i++) {
2569 macaddr[5] = adap->pf * 16 + i;
2570 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2571 }
2572 }
2573}
2574
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302575static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2576{
2577 struct port_info *pi = netdev_priv(dev);
2578 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302579 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302580
2581 /* verify MAC addr is valid */
2582 if (!is_valid_ether_addr(mac)) {
2583 dev_err(pi->adapter->pdev_dev,
2584 "Invalid Ethernet address %pM for VF %d\n",
2585 mac, vf);
2586 return -EINVAL;
2587 }
2588
2589 dev_info(pi->adapter->pdev_dev,
2590 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302591 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2592 if (!ret)
2593 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2594 return ret;
2595}
2596
2597static int cxgb_get_vf_config(struct net_device *dev,
2598 int vf, struct ifla_vf_info *ivi)
2599{
2600 struct port_info *pi = netdev_priv(dev);
2601 struct adapter *adap = pi->adapter;
2602
2603 if (vf >= adap->num_vfs)
2604 return -EINVAL;
2605 ivi->vf = vf;
2606 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2607 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302608}
2609#endif
2610
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002611static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2612{
2613 int ret;
2614 struct sockaddr *addr = p;
2615 struct port_info *pi = netdev_priv(dev);
2616
2617 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002618 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002619
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302620 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002621 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002622 if (ret < 0)
2623 return ret;
2624
2625 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2626 pi->xact_addr_filt = ret;
2627 return 0;
2628}
2629
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002630#ifdef CONFIG_NET_POLL_CONTROLLER
2631static void cxgb_netpoll(struct net_device *dev)
2632{
2633 struct port_info *pi = netdev_priv(dev);
2634 struct adapter *adap = pi->adapter;
2635
2636 if (adap->flags & USING_MSIX) {
2637 int i;
2638 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2639
2640 for (i = pi->nqsets; i; i--, rx++)
2641 t4_sge_intr_msix(0, &rx->rspq);
2642 } else
2643 t4_intr_handler(adap)(0, adap);
2644}
2645#endif
2646
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302647static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2648{
2649 struct port_info *pi = netdev_priv(dev);
2650 struct adapter *adap = pi->adapter;
2651 struct sched_class *e;
2652 struct ch_sched_params p;
2653 struct ch_sched_queue qe;
2654 u32 req_rate;
2655 int err = 0;
2656
2657 if (!can_sched(dev))
2658 return -ENOTSUPP;
2659
2660 if (index < 0 || index > pi->nqsets - 1)
2661 return -EINVAL;
2662
2663 if (!(adap->flags & FULL_INIT_DONE)) {
2664 dev_err(adap->pdev_dev,
2665 "Failed to rate limit on queue %d. Link Down?\n",
2666 index);
2667 return -EINVAL;
2668 }
2669
2670 /* Convert from Mbps to Kbps */
2671 req_rate = rate << 10;
2672
2673 /* Max rate is 10 Gbps */
2674 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2675 dev_err(adap->pdev_dev,
2676 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2677 rate, SCHED_MAX_RATE_KBPS);
2678 return -ERANGE;
2679 }
2680
2681 /* First unbind the queue from any existing class */
2682 memset(&qe, 0, sizeof(qe));
2683 qe.queue = index;
2684 qe.class = SCHED_CLS_NONE;
2685
2686 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2687 if (err) {
2688 dev_err(adap->pdev_dev,
2689 "Unbinding Queue %d on port %d fail. Err: %d\n",
2690 index, pi->port_id, err);
2691 return err;
2692 }
2693
2694 /* Queue already unbound */
2695 if (!req_rate)
2696 return 0;
2697
2698 /* Fetch any available unused or matching scheduling class */
2699 memset(&p, 0, sizeof(p));
2700 p.type = SCHED_CLASS_TYPE_PACKET;
2701 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2702 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2703 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2704 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2705 p.u.params.channel = pi->tx_chan;
2706 p.u.params.class = SCHED_CLS_NONE;
2707 p.u.params.minrate = 0;
2708 p.u.params.maxrate = req_rate;
2709 p.u.params.weight = 0;
2710 p.u.params.pktsize = dev->mtu;
2711
2712 e = cxgb4_sched_class_alloc(dev, &p);
2713 if (!e)
2714 return -ENOMEM;
2715
2716 /* Bind the queue to a scheduling class */
2717 memset(&qe, 0, sizeof(qe));
2718 qe.queue = index;
2719 qe.class = e->idx;
2720
2721 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2722 if (err)
2723 dev_err(adap->pdev_dev,
2724 "Queue rate limiting failed. Err: %d\n", err);
2725 return err;
2726}
2727
Baoyou Xie8efebd62016-09-30 15:34:25 +08002728static int cxgb_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
2729 struct tc_to_netdev *tc)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302730{
2731 struct port_info *pi = netdev2pinfo(dev);
2732 struct adapter *adap = netdev2adap(dev);
2733
2734 if (!(adap->flags & FULL_INIT_DONE)) {
2735 dev_err(adap->pdev_dev,
2736 "Failed to setup tc on port %d. Link Down?\n",
2737 pi->port_id);
2738 return -EINVAL;
2739 }
2740
2741 if (TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS) &&
2742 tc->type == TC_SETUP_CLSU32) {
2743 switch (tc->cls_u32->command) {
2744 case TC_CLSU32_NEW_KNODE:
2745 case TC_CLSU32_REPLACE_KNODE:
2746 return cxgb4_config_knode(dev, proto, tc->cls_u32);
2747 case TC_CLSU32_DELETE_KNODE:
2748 return cxgb4_delete_knode(dev, proto, tc->cls_u32);
2749 default:
2750 return -EOPNOTSUPP;
2751 }
2752 }
2753
2754 return -EOPNOTSUPP;
2755}
2756
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002757static const struct net_device_ops cxgb4_netdev_ops = {
2758 .ndo_open = cxgb_open,
2759 .ndo_stop = cxgb_close,
2760 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002761 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002762 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002763 .ndo_set_rx_mode = cxgb_set_rxmode,
2764 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00002765 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002766 .ndo_validate_addr = eth_validate_addr,
2767 .ndo_do_ioctl = cxgb_ioctl,
2768 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002769#ifdef CONFIG_NET_POLL_CONTROLLER
2770 .ndo_poll_controller = cxgb_netpoll,
2771#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05302772#ifdef CONFIG_CHELSIO_T4_FCOE
2773 .ndo_fcoe_enable = cxgb_fcoe_enable,
2774 .ndo_fcoe_disable = cxgb_fcoe_disable,
2775#endif /* CONFIG_CHELSIO_T4_FCOE */
Hariprasad Shenai3a336cb2015-02-04 15:32:52 +05302776#ifdef CONFIG_NET_RX_BUSY_POLL
2777 .ndo_busy_poll = cxgb_busy_poll,
2778#endif
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302779 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302780 .ndo_setup_tc = cxgb_setup_tc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002781};
2782
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302783#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302784static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2785 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302786 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302787 .ndo_get_vf_config = cxgb_get_vf_config,
Hariprasad Shenai78294512016-08-11 21:06:23 +05302788};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302789#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05302790
2791static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2792{
2793 struct adapter *adapter = netdev2adap(dev);
2794
2795 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2796 strlcpy(info->version, cxgb4_driver_version,
2797 sizeof(info->version));
2798 strlcpy(info->bus_info, pci_name(adapter->pdev),
2799 sizeof(info->bus_info));
2800}
2801
2802static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
2803 .get_drvinfo = get_drvinfo,
2804};
2805
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002806void t4_fatal_err(struct adapter *adap)
2807{
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302808 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002809 t4_intr_disable(adap);
2810 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
2811}
2812
2813static void setup_memwin(struct adapter *adap)
2814{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05302815 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002816
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05302817 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00002818}
2819
2820static void setup_memwin_rdma(struct adapter *adap)
2821{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002822 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05302823 u32 start;
2824 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002825
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05302826 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
2827 start &= PCI_BASE_ADDRESS_MEM_MASK;
2828 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002829 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
2830 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302831 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
2832 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002833 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302834 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002835 adap->vres.ocq.start);
2836 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302837 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002838 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002839}
2840
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002841static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
2842{
2843 u32 v;
2844 int ret;
2845
2846 /* get device capabilities */
2847 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302848 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2849 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05302850 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302851 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002852 if (ret < 0)
2853 return ret;
2854
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302855 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2856 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302857 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002858 if (ret < 0)
2859 return ret;
2860
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302861 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002862 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05302863 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
2864 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002865 if (ret < 0)
2866 return ret;
2867
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302868 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302869 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
2870 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002871 if (ret < 0)
2872 return ret;
2873
2874 t4_sge_init(adap);
2875
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002876 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302877 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302878 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302879 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
2880 v = t4_read_reg(adap, TP_PIO_DATA_A);
2881 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002882
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002883 /* first 4 Tx modulation queues point to consecutive Tx channels */
2884 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302885 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
2886 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002887
2888 /* associate each Tx modulation queue with consecutive Tx channels */
2889 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302890 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302891 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302892 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302893 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302894 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302895 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002896
2897#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
2898 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302899 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
2900 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2901 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2902 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2903 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
2904 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
2905 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2906 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2907 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2908 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002909 }
2910
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002911 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302912 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002913}
2914
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002915/*
2916 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
2917 */
2918#define MAX_ATIDS 8192U
2919
2920/*
2921 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00002922 *
2923 * If the firmware we're dealing with has Configuration File support, then
2924 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002925 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00002926
2927/*
2928 * Tweak configuration based on module parameters, etc. Most of these have
2929 * defaults assigned to them by Firmware Configuration Files (if we're using
2930 * them) but need to be explicitly set if we're using hard-coded
2931 * initialization. But even in the case of using Firmware Configuration
2932 * Files, we'd like to expose the ability to change these via module
2933 * parameters so these are essentially common tweaks/settings for
2934 * Configuration Files and hard-coded initialization ...
2935 */
2936static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002937{
Vipul Pandya636f9d32012-09-26 02:39:39 +00002938 /*
2939 * Fix up various Host-Dependent Parameters like Page Size, Cache
2940 * Line Size, etc. The firmware default is for a 4KB Page Size and
2941 * 64B Cache Line Size ...
2942 */
2943 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002944
Vipul Pandya636f9d32012-09-26 02:39:39 +00002945 /*
2946 * Process module parameters which affect early initialization.
2947 */
2948 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
2949 dev_err(&adapter->pdev->dev,
2950 "Ignoring illegal rx_dma_offset=%d, using 2\n",
2951 rx_dma_offset);
2952 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002953 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302954 t4_set_reg_field(adapter, SGE_CONTROL_A,
2955 PKTSHIFT_V(PKTSHIFT_M),
2956 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002957
Vipul Pandya636f9d32012-09-26 02:39:39 +00002958 /*
2959 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
2960 * adds the pseudo header itself.
2961 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302962 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
2963 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00002964
2965 return 0;
2966}
2967
Hariprasad Shenai01b69612015-05-22 21:58:21 +05302968/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
2969 * unto themselves and they contain their own firmware to perform their
2970 * tasks ...
2971 */
2972static int phy_aq1202_version(const u8 *phy_fw_data,
2973 size_t phy_fw_size)
2974{
2975 int offset;
2976
2977 /* At offset 0x8 you're looking for the primary image's
2978 * starting offset which is 3 Bytes wide
2979 *
2980 * At offset 0xa of the primary image, you look for the offset
2981 * of the DRAM segment which is 3 Bytes wide.
2982 *
2983 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
2984 * wide
2985 */
2986 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
2987 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
2988 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
2989
2990 offset = le24(phy_fw_data + 0x8) << 12;
2991 offset = le24(phy_fw_data + offset + 0xa);
2992 return be16(phy_fw_data + offset + 0x27e);
2993
2994 #undef be16
2995 #undef le16
2996 #undef le24
2997}
2998
2999static struct info_10gbt_phy_fw {
3000 unsigned int phy_fw_id; /* PCI Device ID */
3001 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3002 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3003 int phy_flash; /* Has FLASH for PHY Firmware */
3004} phy_info_array[] = {
3005 {
3006 PHY_AQ1202_DEVICEID,
3007 PHY_AQ1202_FIRMWARE,
3008 phy_aq1202_version,
3009 1,
3010 },
3011 {
3012 PHY_BCM84834_DEVICEID,
3013 PHY_BCM84834_FIRMWARE,
3014 NULL,
3015 0,
3016 },
3017 { 0, NULL, NULL },
3018};
3019
3020static struct info_10gbt_phy_fw *find_phy_info(int devid)
3021{
3022 int i;
3023
3024 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3025 if (phy_info_array[i].phy_fw_id == devid)
3026 return &phy_info_array[i];
3027 }
3028 return NULL;
3029}
3030
3031/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3032 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3033 * we return a negative error number. If we transfer new firmware we return 1
3034 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3035 */
3036static int adap_init0_phy(struct adapter *adap)
3037{
3038 const struct firmware *phyf;
3039 int ret;
3040 struct info_10gbt_phy_fw *phy_info;
3041
3042 /* Use the device ID to determine which PHY file to flash.
3043 */
3044 phy_info = find_phy_info(adap->pdev->device);
3045 if (!phy_info) {
3046 dev_warn(adap->pdev_dev,
3047 "No PHY Firmware file found for this PHY\n");
3048 return -EOPNOTSUPP;
3049 }
3050
3051 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3052 * use that. The adapter firmware provides us with a memory buffer
3053 * where we can load a PHY firmware file from the host if we want to
3054 * override the PHY firmware File in flash.
3055 */
3056 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3057 adap->pdev_dev);
3058 if (ret < 0) {
3059 /* For adapters without FLASH attached to PHY for their
3060 * firmware, it's obviously a fatal error if we can't get the
3061 * firmware to the adapter. For adapters with PHY firmware
3062 * FLASH storage, it's worth a warning if we can't find the
3063 * PHY Firmware but we'll neuter the error ...
3064 */
3065 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3066 "/lib/firmware/%s, error %d\n",
3067 phy_info->phy_fw_file, -ret);
3068 if (phy_info->phy_flash) {
3069 int cur_phy_fw_ver = 0;
3070
3071 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3072 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3073 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3074 ret = 0;
3075 }
3076
3077 return ret;
3078 }
3079
3080 /* Load PHY Firmware onto adapter.
3081 */
3082 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3083 phy_info->phy_fw_version,
3084 (u8 *)phyf->data, phyf->size);
3085 if (ret < 0)
3086 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3087 -ret);
3088 else if (ret > 0) {
3089 int new_phy_fw_ver = 0;
3090
3091 if (phy_info->phy_fw_version)
3092 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3093 phyf->size);
3094 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3095 "Firmware /lib/firmware/%s, version %#x\n",
3096 phy_info->phy_fw_file, new_phy_fw_ver);
3097 }
3098
3099 release_firmware(phyf);
3100
3101 return ret;
3102}
3103
Vipul Pandya636f9d32012-09-26 02:39:39 +00003104/*
3105 * Attempt to initialize the adapter via a Firmware Configuration File.
3106 */
3107static int adap_init0_config(struct adapter *adapter, int reset)
3108{
3109 struct fw_caps_config_cmd caps_cmd;
3110 const struct firmware *cf;
3111 unsigned long mtype = 0, maddr = 0;
3112 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303113 int ret;
3114 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003115 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303116 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003117
3118 /*
3119 * Reset device if necessary.
3120 */
3121 if (reset) {
3122 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303123 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003124 if (ret < 0)
3125 goto bye;
3126 }
3127
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303128 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3129 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3130 * to be performed after any global adapter RESET above since some
3131 * PHYs only have local RAM copies of the PHY firmware.
3132 */
3133 if (is_10gbt_device(adapter->pdev->device)) {
3134 ret = adap_init0_phy(adapter);
3135 if (ret < 0)
3136 goto bye;
3137 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003138 /*
3139 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3140 * then use that. Otherwise, use the configuration file stored
3141 * in the adapter flash ...
3142 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303143 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003144 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303145 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003146 break;
3147 case CHELSIO_T5:
3148 fw_config_file = FW5_CFNAME;
3149 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303150 case CHELSIO_T6:
3151 fw_config_file = FW6_CFNAME;
3152 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003153 default:
3154 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3155 adapter->pdev->device);
3156 ret = -EINVAL;
3157 goto bye;
3158 }
3159
3160 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003161 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303162 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003163 mtype = FW_MEMTYPE_CF_FLASH;
3164 maddr = t4_flash_cfg_addr(adapter);
3165 } else {
3166 u32 params[7], val[7];
3167
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303168 sprintf(fw_config_file_path,
3169 "/lib/firmware/%s", fw_config_file);
3170 config_name = fw_config_file_path;
3171
Vipul Pandya636f9d32012-09-26 02:39:39 +00003172 if (cf->size >= FLASH_CFG_MAX_SIZE)
3173 ret = -ENOMEM;
3174 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303175 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3176 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003177 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303178 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003179 if (ret == 0) {
3180 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303181 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003182 * sizes have to be in terms of multiples of 4
3183 * bytes. So, if the Configuration File isn't
3184 * a multiple of 4 bytes in length we'll have
3185 * to write that out separately since we can't
3186 * guarantee that the bytes following the
3187 * residual byte in the buffer returned by
3188 * request_firmware() are zeroed out ...
3189 */
3190 size_t resid = cf->size & 0x3;
3191 size_t size = cf->size & ~0x3;
3192 __be32 *data = (__be32 *)cf->data;
3193
Hariprasad Shenai51678652014-11-21 12:52:02 +05303194 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3195 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003196
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303197 spin_lock(&adapter->win0_lock);
3198 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3199 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003200 if (ret == 0 && resid != 0) {
3201 union {
3202 __be32 word;
3203 char buf[4];
3204 } last;
3205 int i;
3206
3207 last.word = data[size >> 2];
3208 for (i = resid; i < 4; i++)
3209 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303210 ret = t4_memory_rw(adapter, 0, mtype,
3211 maddr + size,
3212 4, &last.word,
3213 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003214 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303215 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003216 }
3217 }
3218
3219 release_firmware(cf);
3220 if (ret)
3221 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003222 }
3223
Vipul Pandya636f9d32012-09-26 02:39:39 +00003224 /*
3225 * Issue a Capability Configuration command to the firmware to get it
3226 * to parse the Configuration File. We don't use t4_fw_config_file()
3227 * because we want the ability to modify various features after we've
3228 * processed the configuration file ...
3229 */
3230 memset(&caps_cmd, 0, sizeof(caps_cmd));
3231 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303232 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3233 FW_CMD_REQUEST_F |
3234 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303235 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303236 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3237 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3238 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003239 FW_LEN16(caps_cmd));
3240 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3241 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303242
3243 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3244 * Configuration File in FLASH), our last gasp effort is to use the
3245 * Firmware Configuration File which is embedded in the firmware. A
3246 * very few early versions of the firmware didn't have one embedded
3247 * but we can ignore those.
3248 */
3249 if (ret == -ENOENT) {
3250 memset(&caps_cmd, 0, sizeof(caps_cmd));
3251 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303252 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3253 FW_CMD_REQUEST_F |
3254 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303255 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3256 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3257 sizeof(caps_cmd), &caps_cmd);
3258 config_name = "Firmware Default";
3259 }
3260
3261 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003262 if (ret < 0)
3263 goto bye;
3264
Vipul Pandya636f9d32012-09-26 02:39:39 +00003265 finiver = ntohl(caps_cmd.finiver);
3266 finicsum = ntohl(caps_cmd.finicsum);
3267 cfcsum = ntohl(caps_cmd.cfcsum);
3268 if (finicsum != cfcsum)
3269 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3270 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3271 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003272
Vipul Pandya636f9d32012-09-26 02:39:39 +00003273 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003274 * And now tell the firmware to use the configuration we just loaded.
3275 */
3276 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303277 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3278 FW_CMD_REQUEST_F |
3279 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303280 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003281 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3282 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003283 if (ret < 0)
3284 goto bye;
3285
Vipul Pandya636f9d32012-09-26 02:39:39 +00003286 /*
3287 * Tweak configuration based on system architecture, module
3288 * parameters, etc.
3289 */
3290 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003291 if (ret < 0)
3292 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003293
Vipul Pandya636f9d32012-09-26 02:39:39 +00003294 /*
3295 * And finally tell the firmware to initialize itself using the
3296 * parameters from the Configuration File.
3297 */
3298 ret = t4_fw_initialize(adapter, adapter->mbox);
3299 if (ret < 0)
3300 goto bye;
3301
Hariprasad Shenai06640312015-01-13 15:19:25 +05303302 /* Emit Firmware Configuration File information and return
3303 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003304 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003305 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303306 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3307 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003308 return 0;
3309
3310 /*
3311 * Something bad happened. Return the error ... (If the "error"
3312 * is that there's no Configuration File on the adapter we don't
3313 * want to issue a warning since this is fairly common.)
3314 */
3315bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303316 if (config_issued && ret != -ENOENT)
3317 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3318 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003319 return ret;
3320}
3321
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303322static struct fw_info fw_info_array[] = {
3323 {
3324 .chip = CHELSIO_T4,
3325 .fs_name = FW4_CFNAME,
3326 .fw_mod_name = FW4_FNAME,
3327 .fw_hdr = {
3328 .chip = FW_HDR_CHIP_T4,
3329 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3330 .intfver_nic = FW_INTFVER(T4, NIC),
3331 .intfver_vnic = FW_INTFVER(T4, VNIC),
3332 .intfver_ri = FW_INTFVER(T4, RI),
3333 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3334 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3335 },
3336 }, {
3337 .chip = CHELSIO_T5,
3338 .fs_name = FW5_CFNAME,
3339 .fw_mod_name = FW5_FNAME,
3340 .fw_hdr = {
3341 .chip = FW_HDR_CHIP_T5,
3342 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3343 .intfver_nic = FW_INTFVER(T5, NIC),
3344 .intfver_vnic = FW_INTFVER(T5, VNIC),
3345 .intfver_ri = FW_INTFVER(T5, RI),
3346 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3347 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3348 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303349 }, {
3350 .chip = CHELSIO_T6,
3351 .fs_name = FW6_CFNAME,
3352 .fw_mod_name = FW6_FNAME,
3353 .fw_hdr = {
3354 .chip = FW_HDR_CHIP_T6,
3355 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3356 .intfver_nic = FW_INTFVER(T6, NIC),
3357 .intfver_vnic = FW_INTFVER(T6, VNIC),
3358 .intfver_ofld = FW_INTFVER(T6, OFLD),
3359 .intfver_ri = FW_INTFVER(T6, RI),
3360 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3361 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3362 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3363 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3364 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303365 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303366
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303367};
3368
3369static struct fw_info *find_fw_info(int chip)
3370{
3371 int i;
3372
3373 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3374 if (fw_info_array[i].chip == chip)
3375 return &fw_info_array[i];
3376 }
3377 return NULL;
3378}
3379
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003380/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003381 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003382 */
3383static int adap_init0(struct adapter *adap)
3384{
3385 int ret;
3386 u32 v, port_vec;
3387 enum dev_state state;
3388 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003389 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303390 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003391
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303392 /* Grab Firmware Device Log parameters as early as possible so we have
3393 * access to it for debugging, etc.
3394 */
3395 ret = t4_init_devlog_params(adap);
3396 if (ret < 0)
3397 return ret;
3398
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303399 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303400 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3401 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003402 if (ret < 0) {
3403 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3404 ret);
3405 return ret;
3406 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003407 if (ret == adap->mbox)
3408 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003409
Vipul Pandya636f9d32012-09-26 02:39:39 +00003410 /*
3411 * If we're the Master PF Driver and the device is uninitialized,
3412 * then let's consider upgrading the firmware ... (We always want
3413 * to check the firmware version number in order to A. get it for
3414 * later reporting and B. to warn if the currently loaded firmware
3415 * is excessively mismatched relative to the driver.)
3416 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303417 t4_get_fw_version(adap, &adap->params.fw_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303418 t4_get_bs_version(adap, &adap->params.bs_vers);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303419 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303420 t4_get_exprom_version(adap, &adap->params.er_vers);
3421
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303422 ret = t4_check_fw_version(adap);
3423 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303424 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303425 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003426 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303427 struct fw_info *fw_info;
3428 struct fw_hdr *card_fw;
3429 const struct firmware *fw;
3430 const u8 *fw_data = NULL;
3431 unsigned int fw_size = 0;
3432
3433 /* This is the firmware whose headers the driver was compiled
3434 * against
3435 */
3436 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3437 if (fw_info == NULL) {
3438 dev_err(adap->pdev_dev,
3439 "unable to get firmware info for chip %d.\n",
3440 CHELSIO_CHIP_VERSION(adap->params.chip));
3441 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003442 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303443
3444 /* allocate memory to read the header of the firmware on the
3445 * card
3446 */
3447 card_fw = t4_alloc_mem(sizeof(*card_fw));
3448
3449 /* Get FW from from /lib/firmware/ */
3450 ret = request_firmware(&fw, fw_info->fw_mod_name,
3451 adap->pdev_dev);
3452 if (ret < 0) {
3453 dev_err(adap->pdev_dev,
3454 "unable to load firmware image %s, error %d\n",
3455 fw_info->fw_mod_name, ret);
3456 } else {
3457 fw_data = fw->data;
3458 fw_size = fw->size;
3459 }
3460
3461 /* upgrade FW logic */
3462 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3463 state, &reset);
3464
3465 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003466 release_firmware(fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303467 t4_free_mem(card_fw);
3468
Vipul Pandya636f9d32012-09-26 02:39:39 +00003469 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303470 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003471 }
3472
3473 /*
3474 * Grab VPD parameters. This should be done after we establish a
3475 * connection to the firmware since some of the VPD parameters
3476 * (notably the Core Clock frequency) are retrieved via requests to
3477 * the firmware. On the other hand, we need these fairly early on
3478 * so we do this right after getting ahold of the firmware.
3479 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303480 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003481 if (ret < 0)
3482 goto bye;
3483
Vipul Pandya636f9d32012-09-26 02:39:39 +00003484 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003485 * Find out what ports are available to us. Note that we need to do
3486 * this before calling adap_init0_no_config() since it needs nports
3487 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003488 */
3489 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303490 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3491 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303492 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003493 if (ret < 0)
3494 goto bye;
3495
3496 adap->params.nports = hweight32(port_vec);
3497 adap->params.portvec = port_vec;
3498
Hariprasad Shenai06640312015-01-13 15:19:25 +05303499 /* If the firmware is initialized already, emit a simply note to that
3500 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003501 */
3502 if (state == DEV_STATE_INIT) {
3503 dev_info(adap->pdev_dev, "Coming up as %s: "\
3504 "Adapter already initialized\n",
3505 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003506 } else {
3507 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3508 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303509
3510 /* Find out whether we're dealing with a version of the
3511 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003512 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303513 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3514 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303515 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303516 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003517
Hariprasad Shenai06640312015-01-13 15:19:25 +05303518 /* If the firmware doesn't support Configuration Files,
3519 * return an error.
3520 */
3521 if (ret < 0) {
3522 dev_err(adap->pdev_dev, "firmware doesn't support "
3523 "Firmware Configuration Files\n");
3524 goto bye;
3525 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003526
Hariprasad Shenai06640312015-01-13 15:19:25 +05303527 /* The firmware provides us with a memory buffer where we can
3528 * load a Configuration File from the host if we want to
3529 * override the Configuration File in flash.
3530 */
3531 ret = adap_init0_config(adap, reset);
3532 if (ret == -ENOENT) {
3533 dev_err(adap->pdev_dev, "no Configuration File "
3534 "present on adapter.\n");
3535 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003536 }
3537 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303538 dev_err(adap->pdev_dev, "could not initialize "
3539 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003540 goto bye;
3541 }
3542 }
3543
Hariprasad Shenai06640312015-01-13 15:19:25 +05303544 /* Give the SGE code a chance to pull in anything that it needs ...
3545 * Note that this must be called after we retrieve our VPD parameters
3546 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003547 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303548 ret = t4_sge_init(adap);
3549 if (ret < 0)
3550 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003551
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003552 if (is_bypass_device(adap->pdev->device))
3553 adap->params.bypass = 1;
3554
Vipul Pandya636f9d32012-09-26 02:39:39 +00003555 /*
3556 * Grab some of our basic fundamental operating parameters.
3557 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003558#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303559 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3560 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003561
3562#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303563 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3564 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3565 FW_PARAMS_PARAM_Y_V(0) | \
3566 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003567
Vipul Pandya636f9d32012-09-26 02:39:39 +00003568 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003569 params[1] = FW_PARAM_PFVF(L2T_START);
3570 params[2] = FW_PARAM_PFVF(L2T_END);
3571 params[3] = FW_PARAM_PFVF(FILTER_START);
3572 params[4] = FW_PARAM_PFVF(FILTER_END);
3573 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303574 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003575 if (ret < 0)
3576 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003577 adap->sge.egr_start = val[0];
3578 adap->l2t_start = val[1];
3579 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003580 adap->tids.ftid_base = val[3];
3581 adap->tids.nftids = val[4] - val[3] + 1;
3582 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003583
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303584 /* qids (ingress/egress) returned from firmware can be anywhere
3585 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3586 * Hence driver needs to allocate memory for this range to
3587 * store the queue info. Get the highest IQFLINT/EQ index returned
3588 * in FW_EQ_*_CMD.alloc command.
3589 */
3590 params[0] = FW_PARAM_PFVF(EQ_END);
3591 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303592 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303593 if (ret < 0)
3594 goto bye;
3595 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3596 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3597
3598 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3599 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3600 if (!adap->sge.egr_map) {
3601 ret = -ENOMEM;
3602 goto bye;
3603 }
3604
3605 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3606 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3607 if (!adap->sge.ingr_map) {
3608 ret = -ENOMEM;
3609 goto bye;
3610 }
3611
3612 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303613 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303614 */
3615 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3616 sizeof(long), GFP_KERNEL);
3617 if (!adap->sge.starving_fl) {
3618 ret = -ENOMEM;
3619 goto bye;
3620 }
3621
3622 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3623 sizeof(long), GFP_KERNEL);
3624 if (!adap->sge.txq_maperr) {
3625 ret = -ENOMEM;
3626 goto bye;
3627 }
3628
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303629#ifdef CONFIG_DEBUG_FS
3630 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3631 sizeof(long), GFP_KERNEL);
3632 if (!adap->sge.blocked_fl) {
3633 ret = -ENOMEM;
3634 goto bye;
3635 }
3636#endif
3637
Anish Bhattb5a02f52015-01-14 15:17:34 -08003638 params[0] = FW_PARAM_PFVF(CLIP_START);
3639 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303640 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003641 if (ret < 0)
3642 goto bye;
3643 adap->clipt_start = val[0];
3644 adap->clipt_end = val[1];
3645
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303646 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3647 * Classes supported by the hardware/firmware so we hard code it here
3648 * for now.
3649 */
3650 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3651
Vipul Pandya636f9d32012-09-26 02:39:39 +00003652 /* query params related to active filter region */
3653 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3654 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303655 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003656 /* If Active filter size is set we enable establishing
3657 * offload connection through firmware work request
3658 */
3659 if ((val[0] != val[1]) && (ret >= 0)) {
3660 adap->flags |= FW_OFLD_CONN;
3661 adap->tids.aftid_base = val[0];
3662 adap->tids.aftid_end = val[1];
3663 }
3664
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003665 /* If we're running on newer firmware, let it know that we're
3666 * prepared to deal with encapsulated CPL messages. Older
3667 * firmware won't understand this and we'll just get
3668 * unencapsulated messages ...
3669 */
3670 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3671 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303672 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003673
Vipul Pandya636f9d32012-09-26 02:39:39 +00003674 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303675 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3676 * capability. Earlier versions of the firmware didn't have the
3677 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3678 * permission to use ULPTX MEMWRITE DSGL.
3679 */
3680 if (is_t4(adap->params.chip)) {
3681 adap->params.ulptx_memwrite_dsgl = false;
3682 } else {
3683 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303684 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303685 1, params, val);
3686 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3687 }
3688
Steve Wise086de572016-09-16 07:54:49 -07003689 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3690 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3691 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3692 1, params, val);
3693 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3694
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303695 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003696 * Get device capabilities so we can determine what resources we need
3697 * to manage.
3698 */
3699 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303700 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3701 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303702 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003703 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3704 &caps_cmd);
3705 if (ret < 0)
3706 goto bye;
3707
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003708 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003709 /* query offload-related parameters */
3710 params[0] = FW_PARAM_DEV(NTID);
3711 params[1] = FW_PARAM_PFVF(SERVER_START);
3712 params[2] = FW_PARAM_PFVF(SERVER_END);
3713 params[3] = FW_PARAM_PFVF(TDDP_START);
3714 params[4] = FW_PARAM_PFVF(TDDP_END);
3715 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303716 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003717 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003718 if (ret < 0)
3719 goto bye;
3720 adap->tids.ntids = val[0];
3721 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3722 adap->tids.stid_base = val[1];
3723 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003724 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003725 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003726 * region into two parts. Regular filters get 1/3rd and server
3727 * filters get 2/3rd part. This is only enabled if workarond
3728 * path is enabled.
3729 * 1. For regular filters.
3730 * 2. Server filter: This are special filters which are used
3731 * to redirect SYN packets to offload queue.
3732 */
3733 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3734 adap->tids.sftid_base = adap->tids.ftid_base +
3735 DIV_ROUND_UP(adap->tids.nftids, 3);
3736 adap->tids.nsftids = adap->tids.nftids -
3737 DIV_ROUND_UP(adap->tids.nftids, 3);
3738 adap->tids.nftids = adap->tids.sftid_base -
3739 adap->tids.ftid_base;
3740 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003741 adap->vres.ddp.start = val[3];
3742 adap->vres.ddp.size = val[4] - val[3] + 1;
3743 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00003744
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003745 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303746 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003747 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003748 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003749 params[0] = FW_PARAM_PFVF(STAG_START);
3750 params[1] = FW_PARAM_PFVF(STAG_END);
3751 params[2] = FW_PARAM_PFVF(RQ_START);
3752 params[3] = FW_PARAM_PFVF(RQ_END);
3753 params[4] = FW_PARAM_PFVF(PBL_START);
3754 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303755 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003756 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003757 if (ret < 0)
3758 goto bye;
3759 adap->vres.stag.start = val[0];
3760 adap->vres.stag.size = val[1] - val[0] + 1;
3761 adap->vres.rq.start = val[2];
3762 adap->vres.rq.size = val[3] - val[2] + 1;
3763 adap->vres.pbl.start = val[4];
3764 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003765
3766 params[0] = FW_PARAM_PFVF(SQRQ_START);
3767 params[1] = FW_PARAM_PFVF(SQRQ_END);
3768 params[2] = FW_PARAM_PFVF(CQ_START);
3769 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003770 params[4] = FW_PARAM_PFVF(OCQ_START);
3771 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303772 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303773 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003774 if (ret < 0)
3775 goto bye;
3776 adap->vres.qp.start = val[0];
3777 adap->vres.qp.size = val[1] - val[0] + 1;
3778 adap->vres.cq.start = val[2];
3779 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003780 adap->vres.ocq.start = val[4];
3781 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303782
3783 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3784 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303785 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303786 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303787 if (ret < 0) {
3788 adap->params.max_ordird_qp = 8;
3789 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3790 ret = 0;
3791 } else {
3792 adap->params.max_ordird_qp = val[0];
3793 adap->params.max_ird_adapter = val[1];
3794 }
3795 dev_info(adap->pdev_dev,
3796 "max_ordird_qp %d max_ird_adapter %d\n",
3797 adap->params.max_ordird_qp,
3798 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303799 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003800 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003801 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003802 params[0] = FW_PARAM_PFVF(ISCSI_START);
3803 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303804 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003805 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003806 if (ret < 0)
3807 goto bye;
3808 adap->vres.iscsi.start = val[0];
3809 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303810 /* LIO target and cxgb4i initiaitor */
3811 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003812 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05303813 if (caps_cmd.cryptocaps) {
3814 /* Should query params here...TODO */
3815 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
3816 adap->num_uld += 1;
3817 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003818#undef FW_PARAM_PFVF
3819#undef FW_PARAM_DEV
3820
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303821 /* The MTU/MSS Table is initialized by now, so load their values. If
3822 * we're initializing the adapter, then we'll make any modifications
3823 * we want to the MTU/MSS Table and also initialize the congestion
3824 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003825 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003826 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303827 if (state != DEV_STATE_INIT) {
3828 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00003829
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303830 /* The default MTU Table contains values 1492 and 1500.
3831 * However, for TCP, it's better to have two values which are
3832 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
3833 * This allows us to have a TCP Data Payload which is a
3834 * multiple of 8 regardless of what combination of TCP Options
3835 * are in use (always a multiple of 4 bytes) which is
3836 * important for performance reasons. For instance, if no
3837 * options are in use, then we have a 20-byte IP header and a
3838 * 20-byte TCP header. In this case, a 1500-byte MSS would
3839 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
3840 * which is not a multiple of 8. So using an MSS of 1488 in
3841 * this case results in a TCP Data Payload of 1448 bytes which
3842 * is a multiple of 8. On the other hand, if 12-byte TCP Time
3843 * Stamps have been negotiated, then an MTU of 1500 bytes
3844 * results in a TCP Data Payload of 1448 bytes which, as
3845 * above, is a multiple of 8 bytes ...
3846 */
3847 for (i = 0; i < NMTUS; i++)
3848 if (adap->params.mtus[i] == 1492) {
3849 adap->params.mtus[i] = 1488;
3850 break;
3851 }
3852
3853 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3854 adap->params.b_wnd);
3855 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05303856 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003857 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05303858 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003859 return 0;
3860
3861 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003862 * Something bad happened. If a command timed out or failed with EIO
3863 * FW does not operate within its spec or something catastrophic
3864 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003865 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003866bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303867 kfree(adap->sge.egr_map);
3868 kfree(adap->sge.ingr_map);
3869 kfree(adap->sge.starving_fl);
3870 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303871#ifdef CONFIG_DEBUG_FS
3872 kfree(adap->sge.blocked_fl);
3873#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00003874 if (ret != -ETIMEDOUT && ret != -EIO)
3875 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003876 return ret;
3877}
3878
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003879/* EEH callbacks */
3880
3881static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
3882 pci_channel_state_t state)
3883{
3884 int i;
3885 struct adapter *adap = pci_get_drvdata(pdev);
3886
3887 if (!adap)
3888 goto out;
3889
3890 rtnl_lock();
3891 adap->flags &= ~FW_OK;
3892 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08003893 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003894 for_each_port(adap, i) {
3895 struct net_device *dev = adap->port[i];
3896
3897 netif_device_detach(dev);
3898 netif_carrier_off(dev);
3899 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08003900 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05303901 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003902 if (adap->flags & FULL_INIT_DONE)
3903 cxgb_down(adap);
3904 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08003905 if ((adap->flags & DEV_ENABLED)) {
3906 pci_disable_device(pdev);
3907 adap->flags &= ~DEV_ENABLED;
3908 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003909out: return state == pci_channel_io_perm_failure ?
3910 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
3911}
3912
3913static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
3914{
3915 int i, ret;
3916 struct fw_caps_config_cmd c;
3917 struct adapter *adap = pci_get_drvdata(pdev);
3918
3919 if (!adap) {
3920 pci_restore_state(pdev);
3921 pci_save_state(pdev);
3922 return PCI_ERS_RESULT_RECOVERED;
3923 }
3924
Gavin Shan144be3d2014-01-23 12:27:34 +08003925 if (!(adap->flags & DEV_ENABLED)) {
3926 if (pci_enable_device(pdev)) {
3927 dev_err(&pdev->dev, "Cannot reenable PCI "
3928 "device after reset\n");
3929 return PCI_ERS_RESULT_DISCONNECT;
3930 }
3931 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003932 }
3933
3934 pci_set_master(pdev);
3935 pci_restore_state(pdev);
3936 pci_save_state(pdev);
3937 pci_cleanup_aer_uncorrect_error_status(pdev);
3938
Hariprasad Shenai8203b502014-10-09 05:48:47 +05303939 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003940 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303941 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003942 return PCI_ERS_RESULT_DISCONNECT;
3943 adap->flags |= FW_OK;
3944 if (adap_init1(adap, &c))
3945 return PCI_ERS_RESULT_DISCONNECT;
3946
3947 for_each_port(adap, i) {
3948 struct port_info *p = adap2pinfo(adap, i);
3949
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303950 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003951 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003952 if (ret < 0)
3953 return PCI_ERS_RESULT_DISCONNECT;
3954 p->viid = ret;
3955 p->xact_addr_filt = -1;
3956 }
3957
3958 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3959 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003960 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003961 if (cxgb_up(adap))
3962 return PCI_ERS_RESULT_DISCONNECT;
3963 return PCI_ERS_RESULT_RECOVERED;
3964}
3965
3966static void eeh_resume(struct pci_dev *pdev)
3967{
3968 int i;
3969 struct adapter *adap = pci_get_drvdata(pdev);
3970
3971 if (!adap)
3972 return;
3973
3974 rtnl_lock();
3975 for_each_port(adap, i) {
3976 struct net_device *dev = adap->port[i];
3977
3978 if (netif_running(dev)) {
3979 link_start(dev);
3980 cxgb_set_rxmode(dev);
3981 }
3982 netif_device_attach(dev);
3983 }
3984 rtnl_unlock();
3985}
3986
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07003987static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003988 .error_detected = eeh_err_detected,
3989 .slot_reset = eeh_slot_reset,
3990 .resume = eeh_resume,
3991};
3992
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05303993/* Return true if the Link Configuration supports "High Speeds" (those greater
3994 * than 1Gb/s).
3995 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05303996static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003997{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05303998 unsigned int speeds, high_speeds;
3999
4000 speeds = FW_PORT_CAP_SPEED_V(FW_PORT_CAP_SPEED_G(lc->supported));
4001 high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
4002
4003 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004004}
4005
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004006/*
4007 * Perform default configuration of DMA queues depending on the number and type
4008 * of ports we found and the number of available CPUs. Most settings can be
4009 * modified by the admin prior to actual use.
4010 */
Bill Pemberton91744942012-12-03 09:23:02 -05004011static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004012{
4013 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304014 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004015#ifndef CONFIG_CHELSIO_T4_DCB
4016 int q10g = 0;
4017#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004018
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304019 /* Reduce memory usage in kdump environment, disable all offload.
4020 */
4021 if (is_kdump_kernel()) {
4022 adap->params.offload = 0;
4023 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304024 } else if (is_uld(adap) && t4_uld_mem_alloc(adap)) {
4025 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304026 adap->params.crypto = 0;
4027 }
4028
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304029 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004030#ifdef CONFIG_CHELSIO_T4_DCB
4031 /* For Data Center Bridging support we need to be able to support up
4032 * to 8 Traffic Priorities; each of which will be assigned to its
4033 * own TX Queue in order to prevent Head-Of-Line Blocking.
4034 */
4035 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4036 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4037 MAX_ETH_QSETS, adap->params.nports * 8);
4038 BUG_ON(1);
4039 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004040
Anish Bhatt688848b2014-06-19 21:37:13 -07004041 for_each_port(adap, i) {
4042 struct port_info *pi = adap2pinfo(adap, i);
4043
4044 pi->first_qset = qidx;
4045 pi->nqsets = 8;
4046 qidx += pi->nqsets;
4047 }
4048#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004049 /*
4050 * We default to 1 queue per non-10G port and up to # of cores queues
4051 * per 10G port.
4052 */
4053 if (n10g)
4054 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004055 if (q10g > netif_get_num_default_rss_queues())
4056 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004057
4058 for_each_port(adap, i) {
4059 struct port_info *pi = adap2pinfo(adap, i);
4060
4061 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304062 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004063 qidx += pi->nqsets;
4064 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004065#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004066
4067 s->ethqsets = qidx;
4068 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4069
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304070 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004071 /*
4072 * For offload we use 1 queue/channel if all ports are up to 1G,
4073 * otherwise we divide all available queues amongst the channels
4074 * capped by the number of available cores.
4075 */
4076 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304077 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304078 s->ofldqsets = roundup(i, adap->params.nports);
4079 } else {
4080 s->ofldqsets = adap->params.nports;
4081 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004082 }
4083
4084 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4085 struct sge_eth_rxq *r = &s->ethrxq[i];
4086
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304087 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004088 r->fl.size = 72;
4089 }
4090
4091 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4092 s->ethtxq[i].q.size = 1024;
4093
4094 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4095 s->ctrlq[i].q.size = 512;
4096
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304097 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304098 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004099}
4100
4101/*
4102 * Reduce the number of Ethernet queues across all ports to at most n.
4103 * n provides at least one queue per port.
4104 */
Bill Pemberton91744942012-12-03 09:23:02 -05004105static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004106{
4107 int i;
4108 struct port_info *pi;
4109
4110 while (n < adap->sge.ethqsets)
4111 for_each_port(adap, i) {
4112 pi = adap2pinfo(adap, i);
4113 if (pi->nqsets > 1) {
4114 pi->nqsets--;
4115 adap->sge.ethqsets--;
4116 if (adap->sge.ethqsets <= n)
4117 break;
4118 }
4119 }
4120
4121 n = 0;
4122 for_each_port(adap, i) {
4123 pi = adap2pinfo(adap, i);
4124 pi->first_qset = n;
4125 n += pi->nqsets;
4126 }
4127}
4128
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304129static int get_msix_info(struct adapter *adap)
4130{
4131 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304132 unsigned int max_ingq = 0;
4133
4134 if (is_offload(adap))
4135 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4136 if (is_pci_uld(adap))
4137 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4138
4139 if (!max_ingq)
4140 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304141
4142 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4143 if (!msix_info)
4144 return -ENOMEM;
4145
4146 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4147 sizeof(long), GFP_KERNEL);
4148 if (!adap->msix_bmap_ulds.msix_bmap) {
4149 kfree(msix_info);
4150 return -ENOMEM;
4151 }
4152 spin_lock_init(&adap->msix_bmap_ulds.lock);
4153 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304154out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304155 return 0;
4156}
4157
4158static void free_msix_info(struct adapter *adap)
4159{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304160 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304161 return;
4162
4163 kfree(adap->msix_info_ulds);
4164 kfree(adap->msix_bmap_ulds.msix_bmap);
4165}
4166
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004167/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4168#define EXTRA_VECS 2
4169
Bill Pemberton91744942012-12-03 09:23:02 -05004170static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004171{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304172 int ofld_need = 0, uld_need = 0;
4173 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004174 struct sge *s = &adap->sge;
4175 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304176 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304177 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004178
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304179 if (is_pci_uld(adap))
4180 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4181 if (is_offload(adap))
4182 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304183 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304184 GFP_KERNEL);
4185 if (!entries)
4186 return -ENOMEM;
4187
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304188 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304189 if (get_msix_info(adap)) {
4190 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304191 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304192 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304193
4194 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004195 entries[i].entry = i;
4196
4197 want = s->max_ethqsets + EXTRA_VECS;
4198 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304199 want += adap->num_ofld_uld * s->ofldqsets;
4200 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004201 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304202 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304203 want += adap->num_uld * s->ofldqsets;
4204 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304205 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004206#ifdef CONFIG_CHELSIO_T4_DCB
4207 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4208 * each port.
4209 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304210 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004211#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304212 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004213#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304214 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4215 if (allocated < 0) {
4216 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4217 " not using MSI-X\n");
4218 kfree(entries);
4219 return allocated;
4220 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004221
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304222 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004223 * Every group gets its minimum requirement and NIC gets top
4224 * priority for leftovers.
4225 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304226 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004227 if (i < s->max_ethqsets) {
4228 s->max_ethqsets = i;
4229 if (i < s->ethqsets)
4230 reduce_ethqs(adap, i);
4231 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304232 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304233 if (allocated < want)
4234 s->nqs_per_uld = nchan;
4235 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304236 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304237 }
4238
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304239 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004240 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304241 if (is_uld(adap)) {
4242 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304243 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304244 adap->msix_info_ulds[j].idx = i;
4245 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304246 adap->msix_bmap_ulds.mapsize = j;
4247 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304248 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304249 "nic %d per uld %d\n",
4250 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004251
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304252 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004253 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004254}
4255
4256#undef EXTRA_VECS
4257
Bill Pemberton91744942012-12-03 09:23:02 -05004258static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004259{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304260 unsigned int i;
4261 int err;
4262
4263 err = t4_init_rss_mode(adap, adap->mbox);
4264 if (err)
4265 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004266
4267 for_each_port(adap, i) {
4268 struct port_info *pi = adap2pinfo(adap, i);
4269
4270 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4271 if (!pi->rss)
4272 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004273 }
4274 return 0;
4275}
4276
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304277static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4278 enum pci_bus_speed *speed,
4279 enum pcie_link_width *width)
4280{
4281 u32 lnkcap1, lnkcap2;
4282 int err1, err2;
4283
4284#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4285
4286 *speed = PCI_SPEED_UNKNOWN;
4287 *width = PCIE_LNK_WIDTH_UNKNOWN;
4288
4289 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4290 &lnkcap1);
4291 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4292 &lnkcap2);
4293 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4294 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4295 *speed = PCIE_SPEED_8_0GT;
4296 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4297 *speed = PCIE_SPEED_5_0GT;
4298 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4299 *speed = PCIE_SPEED_2_5GT;
4300 }
4301 if (!err1) {
4302 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4303 if (!lnkcap2) { /* pre-r3.0 */
4304 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4305 *speed = PCIE_SPEED_5_0GT;
4306 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4307 *speed = PCIE_SPEED_2_5GT;
4308 }
4309 }
4310
4311 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4312 return err1 ? err1 : err2 ? err2 : -EINVAL;
4313 return 0;
4314}
4315
4316static void cxgb4_check_pcie_caps(struct adapter *adap)
4317{
4318 enum pcie_link_width width, width_cap;
4319 enum pci_bus_speed speed, speed_cap;
4320
4321#define PCIE_SPEED_STR(speed) \
4322 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4323 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4324 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4325 "Unknown")
4326
4327 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4328 dev_warn(adap->pdev_dev,
4329 "Unable to determine PCIe device BW capabilities\n");
4330 return;
4331 }
4332
4333 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4334 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4335 dev_warn(adap->pdev_dev,
4336 "Unable to determine PCI Express bandwidth.\n");
4337 return;
4338 }
4339
4340 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4341 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4342 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4343 width, width_cap);
4344 if (speed < speed_cap || width < width_cap)
4345 dev_info(adap->pdev_dev,
4346 "A slot with more lanes and/or higher speed is "
4347 "suggested for optimal performance.\n");
4348}
4349
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304350/* Dump basic information about the adapter */
4351static void print_adapter_info(struct adapter *adapter)
4352{
4353 /* Device information */
4354 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4355 adapter->params.vpd.id,
4356 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4357 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4358 adapter->params.vpd.sn, adapter->params.vpd.pn);
4359
4360 /* Firmware Version */
4361 if (!adapter->params.fw_vers)
4362 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4363 else
4364 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4365 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4366 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4367 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4368 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4369
4370 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4371 * Firmware, so dev_info() is more appropriate here.)
4372 */
4373 if (!adapter->params.bs_vers)
4374 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4375 else
4376 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4377 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4378 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4379 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4380 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4381
4382 /* TP Microcode Version */
4383 if (!adapter->params.tp_vers)
4384 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4385 else
4386 dev_info(adapter->pdev_dev,
4387 "TP Microcode version: %u.%u.%u.%u\n",
4388 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4389 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4390 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4391 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4392
4393 /* Expansion ROM version */
4394 if (!adapter->params.er_vers)
4395 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4396 else
4397 dev_info(adapter->pdev_dev,
4398 "Expansion ROM version: %u.%u.%u.%u\n",
4399 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4400 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4401 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4402 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4403
4404 /* Software/Hardware configuration */
4405 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4406 is_offload(adapter) ? "R" : "",
4407 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4408 (adapter->flags & USING_MSI) ? "MSI" : ""),
4409 is_offload(adapter) ? "Offload" : "non-Offload");
4410}
4411
Bill Pemberton91744942012-12-03 09:23:02 -05004412static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004413{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004414 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004415 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004416 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004417 const struct port_info *pi = netdev_priv(dev);
4418 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004419
4420 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4421 spd = " 2.5 GT/s";
4422 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4423 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004424 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4425 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004426
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004427 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4428 bufp += sprintf(bufp, "100/");
4429 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4430 bufp += sprintf(bufp, "1000/");
4431 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4432 bufp += sprintf(bufp, "10G/");
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304433 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
4434 bufp += sprintf(bufp, "25G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304435 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4436 bufp += sprintf(bufp, "40G/");
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304437 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
4438 bufp += sprintf(bufp, "100G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004439 if (bufp != buf)
4440 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304441 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004442
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304443 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4444 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004445}
4446
Bill Pemberton91744942012-12-03 09:23:02 -05004447static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004448{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004449 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004450}
4451
Dimitris Michailidis06546392010-07-11 12:01:16 +00004452/*
4453 * Free the following resources:
4454 * - memory used for tables
4455 * - MSI/MSI-X
4456 * - net devices
4457 * - resources FW is holding for us
4458 */
4459static void free_some_resources(struct adapter *adapter)
4460{
4461 unsigned int i;
4462
4463 t4_free_mem(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304464 t4_cleanup_sched(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004465 t4_free_mem(adapter->tids.tid_tab);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304466 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304467 kfree(adapter->sge.egr_map);
4468 kfree(adapter->sge.ingr_map);
4469 kfree(adapter->sge.starving_fl);
4470 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304471#ifdef CONFIG_DEBUG_FS
4472 kfree(adapter->sge.blocked_fl);
4473#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004474 disable_msi(adapter);
4475
4476 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004477 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304478 struct port_info *pi = adap2pinfo(adapter, i);
4479
4480 if (pi->viid != 0)
4481 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4482 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004483 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004484 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004485 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004486 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304487 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004488}
4489
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004490#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004491#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004492 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004493#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004494
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304495static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4496{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304497 u16 device_id;
4498
4499 /* Retrieve adapter's device ID */
4500 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004501
4502 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304503 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004504 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304505 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004506 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304507 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004508 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304509 default:
4510 dev_err(&pdev->dev, "Device %d is not supported\n",
4511 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304512 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004513 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304514}
4515
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304516#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304517static void dummy_setup(struct net_device *dev)
4518{
4519 dev->type = ARPHRD_NONE;
4520 dev->mtu = 0;
4521 dev->hard_header_len = 0;
4522 dev->addr_len = 0;
4523 dev->tx_queue_len = 0;
4524 dev->flags |= IFF_NOARP;
4525 dev->priv_flags |= IFF_NO_QUEUE;
4526
4527 /* Initialize the device structure. */
4528 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4529 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
4530 dev->destructor = free_netdev;
4531}
4532
4533static int config_mgmt_dev(struct pci_dev *pdev)
4534{
4535 struct adapter *adap = pci_get_drvdata(pdev);
4536 struct net_device *netdev;
4537 struct port_info *pi;
4538 char name[IFNAMSIZ];
4539 int err;
4540
4541 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
4542 netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, dummy_setup);
4543 if (!netdev)
4544 return -ENOMEM;
4545
4546 pi = netdev_priv(netdev);
4547 pi->adapter = adap;
4548 SET_NETDEV_DEV(netdev, &pdev->dev);
4549
4550 adap->port[0] = netdev;
4551
4552 err = register_netdev(adap->port[0]);
4553 if (err) {
4554 pr_info("Unable to register VF mgmt netdev %s\n", name);
4555 free_netdev(adap->port[0]);
4556 adap->port[0] = NULL;
4557 return err;
4558 }
4559 return 0;
4560}
4561
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304562static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4563{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304564 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304565 int err = 0;
4566 int current_vfs = pci_num_vf(pdev);
4567 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304568
Hariprasad Shenai78294512016-08-11 21:06:23 +05304569 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304570 /* Check if cxgb4 is the MASTER and fw is initialized */
4571 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4572 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4573 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4574 dev_warn(&pdev->dev,
4575 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4576 return -EOPNOTSUPP;
4577 }
4578
4579 /* If any of the VF's is already assigned to Guest OS, then
4580 * SRIOV for the same cannot be modified
4581 */
4582 if (current_vfs && pci_vfs_assigned(pdev)) {
4583 dev_err(&pdev->dev,
4584 "Cannot modify SR-IOV while VFs are assigned\n");
4585 num_vfs = current_vfs;
4586 return num_vfs;
4587 }
4588
4589 /* Disable SRIOV when zero is passed.
4590 * One needs to disable SRIOV before modifying it, else
4591 * stack throws the below warning:
4592 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4593 */
4594 if (!num_vfs) {
4595 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304596 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304597 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304598 adap->port[0] = NULL;
4599 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304600 /* free VF resources */
4601 kfree(adap->vfinfo);
4602 adap->vfinfo = NULL;
4603 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304604 return num_vfs;
4605 }
4606
4607 if (num_vfs != current_vfs) {
4608 err = pci_enable_sriov(pdev, num_vfs);
4609 if (err)
4610 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304611
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304612 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304613 err = config_mgmt_dev(pdev);
4614 if (err)
4615 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304616 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304617
4618 adap->vfinfo = kcalloc(adap->num_vfs,
4619 sizeof(struct vf_info), GFP_KERNEL);
4620 if (adap->vfinfo)
4621 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304622 return num_vfs;
4623}
4624#endif
4625
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004626static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004627{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004628 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004629 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004630 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004631 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304632 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304633 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304634 u32 whoami, pl_rev;
4635 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304636 static int adap_idx = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004637
4638 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4639
4640 err = pci_request_regions(pdev, KBUILD_MODNAME);
4641 if (err) {
4642 /* Just info, some other driver may have claimed the device. */
4643 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4644 return err;
4645 }
4646
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004647 err = pci_enable_device(pdev);
4648 if (err) {
4649 dev_err(&pdev->dev, "cannot enable PCI device\n");
4650 goto out_release_regions;
4651 }
4652
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304653 regs = pci_ioremap_bar(pdev, 0);
4654 if (!regs) {
4655 dev_err(&pdev->dev, "cannot map device registers\n");
4656 err = -ENOMEM;
4657 goto out_disable_device;
4658 }
4659
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304660 err = t4_wait_dev_ready(regs);
4661 if (err < 0)
4662 goto out_unmap_bar0;
4663
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304664 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304665 whoami = readl(regs + PL_WHOAMI_A);
4666 pl_rev = REV_G(readl(regs + PL_REV_A));
4667 chip = get_chip_type(pdev, pl_rev);
4668 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4669 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304670 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304671#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304672 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304673#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304674 pci_disable_device(pdev);
4675 pci_save_state(pdev); /* to restore SR-IOV later */
4676 goto sriov;
4677 }
4678
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004679 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004680 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004681 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4682 if (err) {
4683 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4684 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304685 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004686 }
4687 } else {
4688 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4689 if (err) {
4690 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304691 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004692 }
4693 }
4694
4695 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004696 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004697 pci_set_master(pdev);
4698 pci_save_state(pdev);
4699
4700 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4701 if (!adapter) {
4702 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304703 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004704 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304705 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004706
Anish Bhatt29aaee62014-08-20 13:44:06 -07004707 adapter->workq = create_singlethread_workqueue("cxgb4");
4708 if (!adapter->workq) {
4709 err = -ENOMEM;
4710 goto out_free_adapter;
4711 }
4712
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304713 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4714 (sizeof(struct mbox_cmd) *
4715 T4_OS_LOG_MBOX_CMDS),
4716 GFP_KERNEL);
4717 if (!adapter->mbox_log) {
4718 err = -ENOMEM;
4719 goto out_free_adapter;
4720 }
4721 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4722
Gavin Shan144be3d2014-01-23 12:27:34 +08004723 /* PCI device has been enabled */
4724 adapter->flags |= DEV_ENABLED;
4725
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304726 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004727 adapter->pdev = pdev;
4728 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304729 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304730 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304731 adapter->pf = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004732 adapter->msg_enable = dflt_msg_enable;
4733 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4734
4735 spin_lock_init(&adapter->stats_lock);
4736 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004737 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004738
4739 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304740 INIT_WORK(&adapter->db_full_task, process_db_full);
4741 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004742
4743 err = t4_prep_adapter(adapter);
4744 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304745 goto out_free_adapter;
4746
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004747
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304748 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304749 s_qpp = (QUEUESPERPAGEPF0_S +
4750 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304751 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304752 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4753 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004754 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4755
4756 /* Each segment size is 128B. Write coalescing is enabled only
4757 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4758 * queue is less no of segments that can be accommodated in
4759 * a page size.
4760 */
4761 if (qpp > num_seg) {
4762 dev_err(&pdev->dev,
4763 "Incorrect number of egress queues per page\n");
4764 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304765 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004766 }
4767 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4768 pci_resource_len(pdev, 2));
4769 if (!adapter->bar2) {
4770 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4771 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304772 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004773 }
4774 }
4775
Vipul Pandya636f9d32012-09-26 02:39:39 +00004776 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004777 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304778#ifdef CONFIG_DEBUG_FS
4779 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4780#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004781 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004782 if (err)
4783 goto out_unmap_bar;
4784
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304785 /* configure SGE_STAT_CFG_A to read WC stats */
4786 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304787 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4788 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4789 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304790
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004791 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004792 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4793 MAX_ETH_QSETS);
4794 if (!netdev) {
4795 err = -ENOMEM;
4796 goto out_free_dev;
4797 }
4798
4799 SET_NETDEV_DEV(netdev, &pdev->dev);
4800
4801 adapter->port[i] = netdev;
4802 pi = netdev_priv(netdev);
4803 pi->adapter = adapter;
4804 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004805 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004806 netdev->irq = pdev->irq;
4807
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004808 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4809 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4810 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304811 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
4812 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004813 if (highdma)
4814 netdev->hw_features |= NETIF_F_HIGHDMA;
4815 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004816 netdev->vlan_features = netdev->features & VLAN_FEAT;
4817
Jiri Pirko01789342011-08-16 06:29:00 +00004818 netdev->priv_flags |= IFF_UNICAST_FLT;
4819
Jarod Wilsond894be52016-10-20 13:55:16 -04004820 /* MTU range: 81 - 9600 */
4821 netdev->min_mtu = 81;
4822 netdev->max_mtu = MAX_MTU;
4823
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004824 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07004825#ifdef CONFIG_CHELSIO_T4_DCB
4826 netdev->dcbnl_ops = &cxgb4_dcb_ops;
4827 cxgb4_dcb_state_init(netdev);
4828#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304829 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004830 }
4831
4832 pci_set_drvdata(pdev, adapter);
4833
4834 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004835 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004836 if (err)
4837 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304838 } else if (adapter->params.nports == 1) {
4839 /* If we don't have a connection to the firmware -- possibly
4840 * because of an error -- grab the raw VPD parameters so we
4841 * can set the proper MAC Address on the debug network
4842 * interface that we've created.
4843 */
4844 u8 hw_addr[ETH_ALEN];
4845 u8 *na = adapter->params.vpd.na;
4846
4847 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
4848 if (!err) {
4849 for (i = 0; i < ETH_ALEN; i++)
4850 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
4851 hex2val(na[2 * i + 1]));
4852 t4_set_hw_addr(adapter, 0, hw_addr);
4853 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004854 }
4855
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304856 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004857 * soon as the first register_netdev completes.
4858 */
4859 cfg_queues(adapter);
4860
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05304861 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004862 if (!adapter->l2t) {
4863 /* We tolerate a lack of L2T, giving up some functionality */
4864 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
4865 adapter->params.offload = 0;
4866 }
4867
Anish Bhattb5a02f52015-01-14 15:17:34 -08004868#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304869 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
4870 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
4871 /* CLIP functionality is not present in hardware,
4872 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08004873 */
4874 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304875 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08004876 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304877 } else {
4878 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
4879 adapter->clipt_end);
4880 if (!adapter->clipt) {
4881 /* We tolerate a lack of clip_table, giving up
4882 * some functionality
4883 */
4884 dev_warn(&pdev->dev,
4885 "could not allocate Clip table, continuing\n");
4886 adapter->params.offload = 0;
4887 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08004888 }
4889#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304890
4891 for_each_port(adapter, i) {
4892 pi = adap2pinfo(adapter, i);
4893 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
4894 if (!pi->sched_tbl)
4895 dev_warn(&pdev->dev,
4896 "could not activate scheduling on port %d\n",
4897 i);
4898 }
4899
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05304900 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004901 dev_warn(&pdev->dev, "could not allocate TID table, "
4902 "continuing\n");
4903 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304904 } else {
4905 adapter->tc_u32 = cxgb4_init_tc_u32(adapter,
4906 CXGB4_MAX_LINK_HANDLE);
4907 if (!adapter->tc_u32)
4908 dev_warn(&pdev->dev,
4909 "could not offload tc u32, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004910 }
4911
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05304912 if (is_offload(adapter)) {
4913 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
4914 u32 hash_base, hash_reg;
4915
4916 if (chip <= CHELSIO_T5) {
4917 hash_reg = LE_DB_TID_HASHBASE_A;
4918 hash_base = t4_read_reg(adapter, hash_reg);
4919 adapter->tids.hash_base = hash_base / 4;
4920 } else {
4921 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
4922 hash_base = t4_read_reg(adapter, hash_reg);
4923 adapter->tids.hash_base = hash_base;
4924 }
4925 }
4926 }
4927
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00004928 /* See what interrupts we'll be using */
4929 if (msi > 1 && enable_msix(adapter) == 0)
4930 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304931 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00004932 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304933 if (msi > 1)
4934 free_msix_info(adapter);
4935 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00004936
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304937 /* check for PCI Express bandwidth capabiltites */
4938 cxgb4_check_pcie_caps(adapter);
4939
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004940 err = init_rss(adapter);
4941 if (err)
4942 goto out_free_dev;
4943
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004944 /*
4945 * The card is now ready to go. If any errors occur during device
4946 * registration we do not fail the whole card but rather proceed only
4947 * with the ports we manage to register successfully. However we must
4948 * register at least one net device.
4949 */
4950 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00004951 pi = adap2pinfo(adapter, i);
4952 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
4953 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
4954
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004955 err = register_netdev(adapter->port[i]);
4956 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004957 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004958 adapter->chan_map[pi->tx_chan] = i;
4959 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004960 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004961 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004962 dev_err(&pdev->dev, "could not register any net devices\n");
4963 goto out_free_dev;
4964 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00004965 if (err) {
4966 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
4967 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00004968 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004969
4970 if (cxgb4_debugfs_root) {
4971 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
4972 cxgb4_debugfs_root);
4973 setup_debugfs(adapter);
4974 }
4975
David S. Miller88c51002011-10-07 13:38:43 -04004976 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4977 pdev->needs_freset = 1;
4978
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304979 if (is_uld(adapter)) {
4980 mutex_lock(&uld_mutex);
4981 list_add_tail(&adapter->list_node, &adapter_list);
4982 mutex_unlock(&uld_mutex);
4983 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004984
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304985 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304986 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304987 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304988
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05304989sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004990#ifdef CONFIG_PCI_IOV
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304991 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0) {
4992 dev_warn(&pdev->dev,
4993 "Enabling SR-IOV VFs using the num_vf module "
4994 "parameter is deprecated - please use the pci sysfs "
4995 "interface instead.\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004996 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
4997 dev_info(&pdev->dev,
4998 "instantiated %u virtual functions\n",
4999 num_vf[func]);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305000 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05305001
5002 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5003 if (!adapter) {
5004 err = -ENOMEM;
5005 goto free_pci_region;
5006 }
5007
Hariprasad Shenai78294512016-08-11 21:06:23 +05305008 adapter->pdev = pdev;
5009 adapter->pdev_dev = &pdev->dev;
5010 adapter->name = pci_name(pdev);
5011 adapter->mbox = func;
5012 adapter->pf = func;
5013 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305014 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305015 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5016 (sizeof(struct mbox_cmd) *
5017 T4_OS_LOG_MBOX_CMDS),
5018 GFP_KERNEL);
5019 if (!adapter->mbox_log) {
5020 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305021 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305022 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05305023 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005024 return 0;
5025
Hariprasad Shenai78294512016-08-11 21:06:23 +05305026 free_adapter:
5027 kfree(adapter);
5028 free_pci_region:
5029 iounmap(regs);
5030 pci_disable_sriov(pdev);
5031 pci_release_regions(pdev);
5032 return err;
5033#else
5034 return 0;
5035#endif
5036
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005037 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005038 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305039 if (adapter->flags & USING_MSIX)
5040 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305041 if (adapter->num_uld || adapter->num_ofld_uld)
5042 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005043 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305044 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005045 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005046 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005047 if (adapter->workq)
5048 destroy_workqueue(adapter->workq);
5049
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305050 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005051 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305052 out_unmap_bar0:
5053 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005054 out_disable_device:
5055 pci_disable_pcie_error_reporting(pdev);
5056 pci_disable_device(pdev);
5057 out_release_regions:
5058 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005059 return err;
5060}
5061
Bill Pemberton91744942012-12-03 09:23:02 -05005062static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005063{
5064 struct adapter *adapter = pci_get_drvdata(pdev);
5065
Hariprasad Shenai78294512016-08-11 21:06:23 +05305066 if (!adapter) {
5067 pci_release_regions(pdev);
5068 return;
5069 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005070
Hariprasad Shenai78294512016-08-11 21:06:23 +05305071 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005072 int i;
5073
Anish Bhatt29aaee62014-08-20 13:44:06 -07005074 /* Tear down per-adapter Work Queue first since it can contain
5075 * references to our adapter data structure.
5076 */
5077 destroy_workqueue(adapter->workq);
5078
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305079 if (is_uld(adapter))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005080 detach_ulds(adapter);
5081
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305082 disable_interrupts(adapter);
5083
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005084 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005085 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005086 unregister_netdev(adapter->port[i]);
5087
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005088 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005089
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005090 /* If we allocated filters, free up state associated with any
5091 * valid filters ...
5092 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305093 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005094
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005095 if (adapter->flags & FULL_INIT_DONE)
5096 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005097
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305098 if (adapter->flags & USING_MSIX)
5099 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305100 if (adapter->num_uld || adapter->num_ofld_uld)
5101 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005102 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005103#if IS_ENABLED(CONFIG_IPV6)
5104 t4_cleanup_clip_tbl(adapter);
5105#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005106 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305107 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005108 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005109 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005110 if ((adapter->flags & DEV_ENABLED)) {
5111 pci_disable_device(pdev);
5112 adapter->flags &= ~DEV_ENABLED;
5113 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005114 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305115 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005116 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005117 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305118 }
5119#ifdef CONFIG_PCI_IOV
5120 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305121 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305122 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305123 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305124 kfree(adapter->vfinfo);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305125 kfree(adapter);
5126 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005127 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305128 }
5129#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005130}
5131
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305132/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5133 * delivery. This is essentially a stripped down version of the PCI remove()
5134 * function where we do the minimal amount of work necessary to shutdown any
5135 * further activity.
5136 */
5137static void shutdown_one(struct pci_dev *pdev)
5138{
5139 struct adapter *adapter = pci_get_drvdata(pdev);
5140
5141 /* As with remove_one() above (see extended comment), we only want do
5142 * do cleanup on PCI Devices which went all the way through init_one()
5143 * ...
5144 */
5145 if (!adapter) {
5146 pci_release_regions(pdev);
5147 return;
5148 }
5149
5150 if (adapter->pf == 4) {
5151 int i;
5152
5153 for_each_port(adapter, i)
5154 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5155 cxgb_close(adapter->port[i]);
5156
5157 t4_uld_clean_up(adapter);
5158 disable_interrupts(adapter);
5159 disable_msi(adapter);
5160
5161 t4_sge_stop(adapter);
5162 if (adapter->flags & FW_OK)
5163 t4_fw_bye(adapter, adapter->mbox);
5164 }
5165#ifdef CONFIG_PCI_IOV
5166 else {
5167 if (adapter->port[0])
5168 unregister_netdev(adapter->port[0]);
5169 iounmap(adapter->regs);
5170 kfree(adapter->vfinfo);
5171 kfree(adapter);
5172 pci_disable_sriov(pdev);
5173 pci_release_regions(pdev);
5174 }
5175#endif
5176}
5177
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005178static struct pci_driver cxgb4_driver = {
5179 .name = KBUILD_MODNAME,
5180 .id_table = cxgb4_pci_tbl,
5181 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005182 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305183 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305184#ifdef CONFIG_PCI_IOV
5185 .sriov_configure = cxgb4_iov_configure,
5186#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005187 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005188};
5189
5190static int __init cxgb4_init_module(void)
5191{
5192 int ret;
5193
5194 /* Debugfs support is optional, just warn if this fails */
5195 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5196 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005197 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005198
5199 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005200 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005201 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305202
Anish Bhatt1bb60372014-10-14 20:07:22 -07005203#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005204 if (!inet6addr_registered) {
5205 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5206 inet6addr_registered = true;
5207 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005208#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305209
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005210 return ret;
5211}
5212
5213static void __exit cxgb4_cleanup_module(void)
5214{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005215#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305216 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005217 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5218 inet6addr_registered = false;
5219 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005220#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005221 pci_unregister_driver(&cxgb4_driver);
5222 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005223}
5224
5225module_init(cxgb4_init_module);
5226module_exit(cxgb4_cleanup_module);