blob: 99c9b88d6d34a2aa152ad951ca6126c04bd0fff2 [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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080066#include <linux/uaccess.h>
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +053067#include <linux/crash_dump.h>
Ganesh Goudar846eac32018-01-10 18:15:08 +053068#include <net/udp_tunnel.h>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000069
70#include "cxgb4.h"
Rahul Lakkireddyd57fd6c2016-09-20 17:13:06 +053071#include "cxgb4_filter.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000072#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053073#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000074#include "t4_msg.h"
75#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053076#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070077#include "cxgb4_dcb.h"
Raju Rangojuc68644e2018-03-20 15:41:40 +053078#include "srq.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053079#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080080#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000081#include "l2t.h"
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +053082#include "smt.h"
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +053083#include "sched.h"
Rahul Lakkireddyd8931842016-09-20 17:13:09 +053084#include "cxgb4_tc_u32.h"
Kumar Sanghvi6a345b32017-09-21 23:41:13 +053085#include "cxgb4_tc_flower.h"
Atul Guptaa45695042017-07-04 16:46:20 +053086#include "cxgb4_ptp.h"
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +053087#include "cxgb4_cudbg.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000088
Hariprasad Shenai812034f2015-04-06 20:23:23 +053089char cxgb4_driver_name[] = KBUILD_MODNAME;
90
Vipul Pandya01bcca62013-07-04 16:10:46 +053091#ifdef DRV_VERSION
92#undef DRV_VERSION
93#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000094#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053095const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053096#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000097
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000098#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
99 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
100 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
101
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530102/* Macros needed to support the PCI Device ID Table ...
103 */
104#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530105 static const struct pci_device_id cxgb4_pci_tbl[] = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +0530106#define CXGB4_UNIFIED_PF 0x4
107
108#define CH_PCI_DEVICE_ID_FUNCTION CXGB4_UNIFIED_PF
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000109
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530110/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
111 * called for both.
112 */
113#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
114
115#define CH_PCI_ID_TABLE_ENTRY(devid) \
Ganesh Goudarbaf50862018-01-16 16:17:40 +0530116 {PCI_VDEVICE(CHELSIO, (devid)), CXGB4_UNIFIED_PF}
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530117
118#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
119 { 0, } \
120 }
121
122#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000123
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530124#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000125#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530126#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530127#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000128#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530129#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530130#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
131#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
132#define PHY_AQ1202_DEVICEID 0x4409
133#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000134
135MODULE_DESCRIPTION(DRV_DESC);
136MODULE_AUTHOR("Chelsio Communications");
137MODULE_LICENSE("Dual BSD/GPL");
138MODULE_VERSION(DRV_VERSION);
139MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530140MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000141MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530142MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000143
Vipul Pandya636f9d32012-09-26 02:39:39 +0000144/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000145 * The driver uses the best interrupt scheme available on a platform in the
146 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
147 * of these schemes the driver may consider as follows:
148 *
149 * msi = 2: choose from among all three options
150 * msi = 1: only consider MSI and INTx interrupts
151 * msi = 0: force INTx interrupts
152 */
153static int msi = 2;
154
155module_param(msi, int, 0644);
156MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
157
158/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000159 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
160 * offset by 2 bytes in order to have the IP headers line up on 4-byte
161 * boundaries. This is a requirement for many architectures which will throw
162 * a machine check fault if an attempt is made to access one of the 4-byte IP
163 * header fields on a non-4-byte boundary. And it's a major performance issue
164 * even on some architectures which allow it like some implementations of the
165 * x86 ISA. However, some architectures don't mind this and for some very
166 * edge-case performance sensitive applications (like forwarding large volumes
167 * of small packets), setting this DMA offset to 0 will decrease the number of
168 * PCI-E Bus transfers enough to measurably affect performance.
169 */
170static int rx_dma_offset = 2;
171
Anish Bhatt688848b2014-06-19 21:37:13 -0700172/* TX Queue select used to determine what algorithm to use for selecting TX
173 * queue. Select between the kernel provided function (select_queue=0) or user
174 * cxgb_select_queue function (select_queue=1)
175 *
176 * Default: select_queue=0
177 */
178static int select_queue;
179module_param(select_queue, int, 0644);
180MODULE_PARM_DESC(select_queue,
181 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
182
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000183static struct dentry *cxgb4_debugfs_root;
184
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530185LIST_HEAD(adapter_list);
186DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000187
188static void link_report(struct net_device *dev)
189{
190 if (!netif_carrier_ok(dev))
191 netdev_info(dev, "link down\n");
192 else {
193 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
194
Hariprasad Shenai85412252015-10-01 13:48:48 +0530195 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000196 const struct port_info *p = netdev_priv(dev);
197
198 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000199 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000200 s = "100Mbps";
201 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530202 case 1000:
203 s = "1Gbps";
204 break;
205 case 10000:
206 s = "10Gbps";
207 break;
208 case 25000:
209 s = "25Gbps";
210 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000211 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530212 s = "40Gbps";
213 break;
Ganesh Goudar7cbe5432018-03-10 17:34:50 +0530214 case 50000:
215 s = "50Gbps";
216 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530217 case 100000:
218 s = "100Gbps";
219 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530220 default:
221 pr_info("%s: unsupported speed: %d\n",
222 dev->name, p->link_cfg.speed);
223 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000224 }
225
226 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
227 fc[p->link_cfg.fc]);
228 }
229}
230
Anish Bhatt688848b2014-06-19 21:37:13 -0700231#ifdef CONFIG_CHELSIO_T4_DCB
232/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
233static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
234{
235 struct port_info *pi = netdev_priv(dev);
236 struct adapter *adap = pi->adapter;
237 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
238 int i;
239
240 /* We use a simple mapping of Port TX Queue Index to DCB
241 * Priority when we're enabling DCB.
242 */
243 for (i = 0; i < pi->nqsets; i++, txq++) {
244 u32 name, value;
245 int err;
246
Hariprasad Shenai51678652014-11-21 12:52:02 +0530247 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
248 FW_PARAMS_PARAM_X_V(
249 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
250 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700251 value = enable ? i : 0xffffffff;
252
253 /* Since we can be called while atomic (from "interrupt
254 * level") we need to issue the Set Parameters Commannd
255 * without sleeping (timeout < 0).
256 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530257 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530258 &name, &value,
259 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700260
261 if (err)
262 dev_err(adap->pdev_dev,
263 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
264 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700265 else
266 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700267 }
268}
Anish Bhatt688848b2014-06-19 21:37:13 -0700269
Baoyou Xie50935852016-09-25 14:10:09 +0800270static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530271{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530272 struct port_info *pi = netdev_priv(dev);
273
274 if (!pi->dcb.enabled)
275 return 0;
276
277 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
278 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530279}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200280#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530281
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000282void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
283{
284 struct net_device *dev = adapter->port[port_id];
285
286 /* Skip changes from disabled ports. */
287 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
288 if (link_stat)
289 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700290 else {
291#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530292 if (cxgb4_dcb_enabled(dev)) {
Ganesh Goudarba581f72017-09-23 16:07:28 +0530293 cxgb4_dcb_reset(dev);
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530294 dcb_tx_queue_prio_enable(dev, false);
295 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700296#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000297 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700298 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000299
300 link_report(dev);
301 }
302}
303
304void t4_os_portmod_changed(const struct adapter *adap, int port_id)
305{
306 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000307 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000308 };
309
310 const struct net_device *dev = adap->port[port_id];
311 const struct port_info *pi = netdev_priv(dev);
312
313 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
314 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000315 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000316 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530317 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
318 netdev_info(dev, "%s: unsupported port module inserted\n",
319 dev->name);
320 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
321 netdev_info(dev, "%s: unknown port module inserted\n",
322 dev->name);
323 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
324 netdev_info(dev, "%s: transceiver module error\n", dev->name);
325 else
326 netdev_info(dev, "%s: unknown module type %d inserted\n",
327 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000328}
329
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530330int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
331module_param(dbfifo_int_thresh, int, 0644);
332MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
333
Vipul Pandya404d9e32012-10-08 02:59:43 +0000334/*
335 * usecs to sleep while draining the dbfifo
336 */
337static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530338module_param(dbfifo_drain_delay, int, 0644);
339MODULE_PARM_DESC(dbfifo_drain_delay,
340 "usecs to sleep while draining the dbfifo");
341
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530342static inline int cxgb4_set_addr_hash(struct port_info *pi)
343{
344 struct adapter *adap = pi->adapter;
345 u64 vec = 0;
346 bool ucast = false;
347 struct hash_mac_addr *entry;
348
349 /* Calculate the hash vector for the updated list and program it */
350 list_for_each_entry(entry, &adap->mac_hlist, list) {
351 ucast |= is_unicast_ether_addr(entry->addr);
352 vec |= (1ULL << hash_mac_addr(entry->addr));
353 }
354 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
355 vec, false);
356}
357
358static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
359{
360 struct port_info *pi = netdev_priv(netdev);
361 struct adapter *adap = pi->adapter;
362 int ret;
363 u64 mhash = 0;
364 u64 uhash = 0;
365 bool free = false;
366 bool ucast = is_unicast_ether_addr(mac_addr);
367 const u8 *maclist[1] = {mac_addr};
368 struct hash_mac_addr *new_entry;
369
370 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
371 NULL, ucast ? &uhash : &mhash, false);
372 if (ret < 0)
373 goto out;
374 /* if hash != 0, then add the addr to hash addr list
375 * so on the end we will calculate the hash for the
376 * list and program it
377 */
378 if (uhash || mhash) {
379 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
380 if (!new_entry)
381 return -ENOMEM;
382 ether_addr_copy(new_entry->addr, mac_addr);
383 list_add_tail(&new_entry->list, &adap->mac_hlist);
384 ret = cxgb4_set_addr_hash(pi);
385 }
386out:
387 return ret < 0 ? ret : 0;
388}
389
390static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
391{
392 struct port_info *pi = netdev_priv(netdev);
393 struct adapter *adap = pi->adapter;
394 int ret;
395 const u8 *maclist[1] = {mac_addr};
396 struct hash_mac_addr *entry, *tmp;
397
398 /* If the MAC address to be removed is in the hash addr
399 * list, delete it from the list and update hash vector
400 */
401 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
402 if (ether_addr_equal(entry->addr, mac_addr)) {
403 list_del(&entry->list);
404 kfree(entry);
405 return cxgb4_set_addr_hash(pi);
406 }
407 }
408
409 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
410 return ret < 0 ? -EINVAL : 0;
411}
412
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000413/*
414 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
415 * If @mtu is -1 it is left unchanged.
416 */
417static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
418{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000419 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530420 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000421
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530422 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
423 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530424
425 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
426 (dev->flags & IFF_PROMISC) ? 1 : 0,
427 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
428 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000429}
430
431/**
432 * link_start - enable a port
433 * @dev: the port to enable
434 *
435 * Performs the MAC and PHY actions needed to enable a port.
436 */
437static int link_start(struct net_device *dev)
438{
439 int ret;
440 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530441 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000442
443 /*
444 * We do not set address filters and promiscuity here, the stack does
445 * that step explicitly.
446 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000447 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000448 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000449 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000450 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000451 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000452 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000453 if (ret >= 0) {
454 pi->xact_addr_filt = ret;
455 ret = 0;
456 }
457 }
458 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530459 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000460 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700461 if (ret == 0) {
462 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700463 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
464 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700465 local_bh_enable();
466 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700467
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000468 return ret;
469}
470
Anish Bhatt688848b2014-06-19 21:37:13 -0700471#ifdef CONFIG_CHELSIO_T4_DCB
472/* Handle a Data Center Bridging update message from the firmware. */
473static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
474{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530475 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530476 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700477 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
478 int new_dcb_enabled;
479
480 cxgb4_dcb_handle_fw_update(adap, pcmd);
481 new_dcb_enabled = cxgb4_dcb_enabled(dev);
482
483 /* If the DCB has become enabled or disabled on the port then we're
484 * going to need to set up/tear down DCB Priority parameters for the
485 * TX Queues associated with the port.
486 */
487 if (new_dcb_enabled != old_dcb_enabled)
488 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
489}
490#endif /* CONFIG_CHELSIO_T4_DCB */
491
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000492/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000493 */
494static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
495 const struct pkt_gl *gl)
496{
497 u8 opcode = ((const struct rss_header *)rsp)->opcode;
498
499 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000500
501 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
502 */
503 if (unlikely(opcode == CPL_FW4_MSG &&
504 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
505 rsp++;
506 opcode = ((const struct rss_header *)rsp)->opcode;
507 rsp++;
508 if (opcode != CPL_SGE_EGR_UPDATE) {
509 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
510 , opcode);
511 goto out;
512 }
513 }
514
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000515 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
516 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800517 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000518 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000519
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000520 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000521 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530522 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000523 struct sge_eth_txq *eq;
524
525 eq = container_of(txq, struct sge_eth_txq, q);
526 netif_tx_wake_queue(eq->txq);
527 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530528 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000529
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530530 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000531 tasklet_schedule(&oq->qresume_tsk);
532 }
533 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
534 const struct cpl_fw6_msg *p = (void *)rsp;
535
Anish Bhatt688848b2014-06-19 21:37:13 -0700536#ifdef CONFIG_CHELSIO_T4_DCB
537 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530538 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700539 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530540 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700541
542 if (cmd == FW_PORT_CMD &&
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530543 (action == FW_PORT_ACTION_GET_PORT_INFO ||
544 action == FW_PORT_ACTION_GET_PORT_INFO32)) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530545 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700546 be32_to_cpu(pcmd->op_to_portid));
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530547 struct net_device *dev;
548 int dcbxdis, state_input;
549
550 dev = q->adap->port[q->adap->chan_map[port]];
551 dcbxdis = (action == FW_PORT_ACTION_GET_PORT_INFO
552 ? !!(pcmd->u.info.dcbxdis_pkd &
553 FW_PORT_CMD_DCBXDIS_F)
554 : !!(pcmd->u.info32.lstatus32_to_cbllen32 &
555 FW_PORT_CMD_DCBXDIS32_F));
556 state_input = (dcbxdis
557 ? CXGB4_DCB_INPUT_FW_DISABLED
558 : CXGB4_DCB_INPUT_FW_ENABLED);
Anish Bhatt688848b2014-06-19 21:37:13 -0700559
560 cxgb4_dcb_state_fsm(dev, state_input);
561 }
562
563 if (cmd == FW_PORT_CMD &&
564 action == FW_PORT_ACTION_L2_DCB_CFG)
565 dcb_rpl(q->adap, pcmd);
566 else
567#endif
568 if (p->type == 0)
569 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000570 } else if (opcode == CPL_L2T_WRITE_RPL) {
571 const struct cpl_l2t_write_rpl *p = (void *)rsp;
572
573 do_l2t_write_rpl(q->adap, p);
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +0530574 } else if (opcode == CPL_SMT_WRITE_RPL) {
575 const struct cpl_smt_write_rpl *p = (void *)rsp;
576
577 do_smt_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);
Kumar Sanghvi12b276f2017-11-01 08:53:01 +0530582 } else if (opcode == CPL_ACT_OPEN_RPL) {
583 const struct cpl_act_open_rpl *p = (void *)rsp;
584
585 hash_filter_rpl(q->adap, p);
Kumar Sanghvi3b0b3be2017-11-01 08:53:02 +0530586 } else if (opcode == CPL_ABORT_RPL_RSS) {
587 const struct cpl_abort_rpl_rss *p = (void *)rsp;
588
589 hash_del_filter_rpl(q->adap, p);
Raju Rangojuc68644e2018-03-20 15:41:40 +0530590 } else if (opcode == CPL_SRQ_TABLE_RPL) {
591 const struct cpl_srq_table_rpl *p = (void *)rsp;
592
593 do_srq_table_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000594 } else
595 dev_err(q->adap->pdev_dev,
596 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000597out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000598 return 0;
599}
600
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000601static void disable_msi(struct adapter *adapter)
602{
603 if (adapter->flags & USING_MSIX) {
604 pci_disable_msix(adapter->pdev);
605 adapter->flags &= ~USING_MSIX;
606 } else if (adapter->flags & USING_MSI) {
607 pci_disable_msi(adapter->pdev);
608 adapter->flags &= ~USING_MSI;
609 }
610}
611
612/*
613 * Interrupt handler for non-data events used with MSI-X.
614 */
615static irqreturn_t t4_nondata_intr(int irq, void *cookie)
616{
617 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530618 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000619
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530620 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000621 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530622 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000623 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530624 if (adap->flags & MASTER_PF)
625 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000626 return IRQ_HANDLED;
627}
628
629/*
630 * Name the MSI-X interrupts.
631 */
632static void name_msix_vecs(struct adapter *adap)
633{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000634 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000635
636 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000637 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000638
639 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000640 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
641 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000642
643 /* Ethernet queues */
644 for_each_port(adap, j) {
645 struct net_device *d = adap->port[j];
646 const struct port_info *pi = netdev_priv(d);
647
Dimitris Michailidisba278162010-12-14 21:36:50 +0000648 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000649 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
650 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000651 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000652}
653
654static int request_msix_queue_irqs(struct adapter *adap)
655{
656 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530657 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530658 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000659
660 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
661 adap->msix_info[1].desc, &s->fw_evtq);
662 if (err)
663 return err;
664
665 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000666 err = request_irq(adap->msix_info[msi_index].vec,
667 t4_sge_intr_msix, 0,
668 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000669 &s->ethrxq[ethqidx].rspq);
670 if (err)
671 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000672 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000673 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000674 return 0;
675
676unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000677 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000678 free_irq(adap->msix_info[--msi_index].vec,
679 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000680 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
681 return err;
682}
683
684static void free_msix_queue_irqs(struct adapter *adap)
685{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000686 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000687 struct sge *s = &adap->sge;
688
689 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
690 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000691 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000692}
693
694/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530695 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000696 * @pi: the port
697 * @queues: array of queue indices for RSS
698 *
699 * Sets up the portion of the HW RSS table for the port's VI to distribute
700 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530701 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000702 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530703int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000704{
705 u16 *rss;
706 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530707 struct adapter *adapter = pi->adapter;
708 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000709
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530710 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000711 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
712 if (!rss)
713 return -ENOMEM;
714
715 /* map the queue indices to queue ids */
716 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530717 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000718
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530719 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000720 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530721 /* If Tunnel All Lookup isn't specified in the global RSS
722 * Configuration, then we need to specify a default Ingress
723 * Queue for any ingress packets which aren't hashed. We'll
724 * use our first ingress queue ...
725 */
726 if (!err)
727 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
728 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
729 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
730 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
731 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
732 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
733 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000734 kfree(rss);
735 return err;
736}
737
738/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000739 * setup_rss - configure RSS
740 * @adap: the adapter
741 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000742 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000743 */
744static int setup_rss(struct adapter *adap)
745{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530746 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000747
748 for_each_port(adap, i) {
749 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000750
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530751 /* Fill default values with equal distribution */
752 for (j = 0; j < pi->rss_size; j++)
753 pi->rss[j] = j % pi->nqsets;
754
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530755 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000756 if (err)
757 return err;
758 }
759 return 0;
760}
761
762/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000763 * Return the channel of the ingress queue with the given qid.
764 */
765static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
766{
767 qid -= p->ingr_start;
768 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
769}
770
771/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000772 * Wait until all NAPI handlers are descheduled.
773 */
774static void quiesce_rx(struct adapter *adap)
775{
776 int i;
777
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530778 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000779 struct sge_rspq *q = adap->sge.ingr_map[i];
780
Eric Dumazet5226b7912017-02-02 11:44:27 -0800781 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000782 napi_disable(&q->napi);
783 }
784}
785
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530786/* Disable interrupt and napi handler */
787static void disable_interrupts(struct adapter *adap)
788{
789 if (adap->flags & FULL_INIT_DONE) {
790 t4_intr_disable(adap);
791 if (adap->flags & USING_MSIX) {
792 free_msix_queue_irqs(adap);
793 free_irq(adap->msix_info[0].vec, adap);
794 } else {
795 free_irq(adap->pdev->irq, adap);
796 }
797 quiesce_rx(adap);
798 }
799}
800
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000801/*
802 * Enable NAPI scheduling and interrupt generation for all Rx queues.
803 */
804static void enable_rx(struct adapter *adap)
805{
806 int i;
807
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530808 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809 struct sge_rspq *q = adap->sge.ingr_map[i];
810
811 if (!q)
812 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800813 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000814 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800815
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000816 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530817 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
818 SEINTARM_V(q->intr_params) |
819 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000820 }
821}
822
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530823
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530824static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000825{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000826 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530827 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000828
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530829 bitmap_zero(s->starving_fl, s->egr_sz);
830 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000831
832 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530833 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000834 else {
835 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530836 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000837 if (err)
838 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530839 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000840 }
841
842 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530843 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530844 if (err)
845 t4_free_sge_resources(adap);
846 return err;
847}
848
849/**
850 * setup_sge_queues - configure SGE Tx/Rx/response queues
851 * @adap: the adapter
852 *
853 * Determines how many sets of SGE queues to use and initializes them.
854 * We support multiple queue sets per port if we have MSI-X, otherwise
855 * just one queue set per port.
856 */
857static int setup_sge_queues(struct adapter *adap)
858{
859 int err, i, j;
860 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530861 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530862 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000863
Ganesh Goudard427cae2017-06-16 15:36:09 +0530864 if (is_uld(adap))
865 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
866
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000867 for_each_port(adap, i) {
868 struct net_device *dev = adap->port[i];
869 struct port_info *pi = netdev_priv(dev);
870 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
871 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
872
873 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530874 if (adap->msi_idx > 0)
875 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000876 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530877 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530878 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530879 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530880 t4_get_tp_ch_map(adap,
881 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000882 if (err)
883 goto freeout;
884 q->rspq.idx = j;
885 memset(&q->stats, 0, sizeof(q->stats));
886 }
887 for (j = 0; j < pi->nqsets; j++, t++) {
888 err = t4_sge_alloc_eth_txq(adap, t, dev,
889 netdev_get_tx_queue(dev, j),
890 s->fw_evtq.cntxt_id);
891 if (err)
892 goto freeout;
893 }
894 }
895
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000896 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530897 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000898 * have RDMA queues, and that's the right value.
899 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530900 if (rxq_info)
901 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
902
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000903 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530904 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000905 if (err)
906 goto freeout;
907 }
908
Atul Guptaa45695042017-07-04 16:46:20 +0530909 if (!is_t4(adap->params.chip)) {
910 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
911 netdev_get_tx_queue(adap->port[0], 0)
912 , s->fw_evtq.cntxt_id);
913 if (err)
914 goto freeout;
915 }
916
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530917 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530918 MPS_TRC_RSS_CONTROL_A :
919 MPS_T5_TRC_RSS_CONTROL_A,
920 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
921 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000922 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530923freeout:
924 t4_free_sge_resources(adap);
925 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000926}
927
Anish Bhatt688848b2014-06-19 21:37:13 -0700928static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
929 void *accel_priv, select_queue_fallback_t fallback)
930{
931 int txq;
932
933#ifdef CONFIG_CHELSIO_T4_DCB
934 /* If a Data Center Bridging has been successfully negotiated on this
935 * link then we'll use the skb's priority to map it to a TX Queue.
936 * The skb's priority is determined via the VLAN Tag Priority Code
937 * Point field.
938 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530939 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700940 u16 vlan_tci;
941 int err;
942
943 err = vlan_get_tag(skb, &vlan_tci);
944 if (unlikely(err)) {
945 if (net_ratelimit())
946 netdev_warn(dev,
947 "TX Packet without VLAN Tag on DCB Link\n");
948 txq = 0;
949 } else {
950 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530951#ifdef CONFIG_CHELSIO_T4_FCOE
952 if (skb->protocol == htons(ETH_P_FCOE))
953 txq = skb->priority & 0x7;
954#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700955 }
956 return txq;
957 }
958#endif /* CONFIG_CHELSIO_T4_DCB */
959
960 if (select_queue) {
961 txq = (skb_rx_queue_recorded(skb)
962 ? skb_get_rx_queue(skb)
963 : smp_processor_id());
964
965 while (unlikely(txq >= dev->real_num_tx_queues))
966 txq -= dev->real_num_tx_queues;
967
968 return txq;
969 }
970
971 return fallback(dev, skb) % dev->real_num_tx_queues;
972}
973
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000974static int closest_timer(const struct sge *s, int time)
975{
976 int i, delta, match = 0, min_delta = INT_MAX;
977
978 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
979 delta = time - s->timer_val[i];
980 if (delta < 0)
981 delta = -delta;
982 if (delta < min_delta) {
983 min_delta = delta;
984 match = i;
985 }
986 }
987 return match;
988}
989
990static int closest_thres(const struct sge *s, int thres)
991{
992 int i, delta, match = 0, min_delta = INT_MAX;
993
994 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
995 delta = thres - s->counter_val[i];
996 if (delta < 0)
997 delta = -delta;
998 if (delta < min_delta) {
999 min_delta = delta;
1000 match = i;
1001 }
1002 }
1003 return match;
1004}
1005
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001006/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301007 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001008 * @q: the Rx queue
1009 * @us: the hold-off time in us, or 0 to disable timer
1010 * @cnt: the hold-off packet count, or 0 to disable counter
1011 *
1012 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1013 * one of the two needs to be enabled for the queue to generate interrupts.
1014 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301015int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1016 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001017{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301018 struct adapter *adap = q->adap;
1019
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001020 if ((us | cnt) == 0)
1021 cnt = 1;
1022
1023 if (cnt) {
1024 int err;
1025 u32 v, new_idx;
1026
1027 new_idx = closest_thres(&adap->sge, cnt);
1028 if (q->desc && q->pktcnt_idx != new_idx) {
1029 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301030 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1031 FW_PARAMS_PARAM_X_V(
1032 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1033 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301034 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1035 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001036 if (err)
1037 return err;
1038 }
1039 q->pktcnt_idx = new_idx;
1040 }
1041
1042 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301043 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001044 return 0;
1045}
1046
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001047static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001048{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001049 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001050 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001051 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001052
Patrick McHardyf6469682013-04-19 02:04:27 +00001053 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001054 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001055
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301056 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001057 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001058 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001059 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001060 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001061 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001062}
1063
Bill Pemberton91744942012-12-03 09:23:02 -05001064static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001065{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001066 if (IS_ERR_OR_NULL(adap->debugfs_root))
1067 return -1;
1068
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301069#ifdef CONFIG_DEBUG_FS
1070 t4_setup_debugfs(adap);
1071#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001072 return 0;
1073}
1074
1075/*
1076 * upper-layer driver support
1077 */
1078
1079/*
1080 * Allocate an active-open TID and set it to the supplied value.
1081 */
1082int cxgb4_alloc_atid(struct tid_info *t, void *data)
1083{
1084 int atid = -1;
1085
1086 spin_lock_bh(&t->atid_lock);
1087 if (t->afree) {
1088 union aopen_entry *p = t->afree;
1089
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001090 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001091 t->afree = p->next;
1092 p->data = data;
1093 t->atids_in_use++;
1094 }
1095 spin_unlock_bh(&t->atid_lock);
1096 return atid;
1097}
1098EXPORT_SYMBOL(cxgb4_alloc_atid);
1099
1100/*
1101 * Release an active-open TID.
1102 */
1103void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1104{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001105 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001106
1107 spin_lock_bh(&t->atid_lock);
1108 p->next = t->afree;
1109 t->afree = p;
1110 t->atids_in_use--;
1111 spin_unlock_bh(&t->atid_lock);
1112}
1113EXPORT_SYMBOL(cxgb4_free_atid);
1114
1115/*
1116 * Allocate a server TID and set it to the supplied value.
1117 */
1118int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1119{
1120 int stid;
1121
1122 spin_lock_bh(&t->stid_lock);
1123 if (family == PF_INET) {
1124 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1125 if (stid < t->nstids)
1126 __set_bit(stid, t->stid_bmap);
1127 else
1128 stid = -1;
1129 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301130 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001131 if (stid < 0)
1132 stid = -1;
1133 }
1134 if (stid >= 0) {
1135 t->stid_tab[stid].data = data;
1136 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301137 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1138 * This is equivalent to 4 TIDs. With CLIP enabled it
1139 * needs 2 TIDs.
1140 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301141 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301142 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301143 t->v6_stids_in_use += 2;
1144 } else {
1145 t->stids_in_use++;
1146 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001147 }
1148 spin_unlock_bh(&t->stid_lock);
1149 return stid;
1150}
1151EXPORT_SYMBOL(cxgb4_alloc_stid);
1152
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001153/* Allocate a server filter TID and set it to the supplied value.
1154 */
1155int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1156{
1157 int stid;
1158
1159 spin_lock_bh(&t->stid_lock);
1160 if (family == PF_INET) {
1161 stid = find_next_zero_bit(t->stid_bmap,
1162 t->nstids + t->nsftids, t->nstids);
1163 if (stid < (t->nstids + t->nsftids))
1164 __set_bit(stid, t->stid_bmap);
1165 else
1166 stid = -1;
1167 } else {
1168 stid = -1;
1169 }
1170 if (stid >= 0) {
1171 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301172 stid -= t->nstids;
1173 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301174 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001175 }
1176 spin_unlock_bh(&t->stid_lock);
1177 return stid;
1178}
1179EXPORT_SYMBOL(cxgb4_alloc_sftid);
1180
1181/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001182 */
1183void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1184{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301185 /* Is it a server filter TID? */
1186 if (t->nsftids && (stid >= t->sftid_base)) {
1187 stid -= t->sftid_base;
1188 stid += t->nstids;
1189 } else {
1190 stid -= t->stid_base;
1191 }
1192
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001193 spin_lock_bh(&t->stid_lock);
1194 if (family == PF_INET)
1195 __clear_bit(stid, t->stid_bmap);
1196 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301197 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001198 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301199 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301200 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301201 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301202 t->v6_stids_in_use -= 2;
1203 } else {
1204 t->stids_in_use--;
1205 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301206 } else {
1207 t->sftids_in_use--;
1208 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301209
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);
Johannes Berg4df864c2017-06-16 14:29:21 +02001223 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001224 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 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301285void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1286 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001287{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001288 struct sk_buff *skb;
1289 struct adapter *adap = container_of(t, struct adapter, tids);
1290
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301291 WARN_ON(tid >= t->ntids);
1292
1293 if (t->tid_tab[tid]) {
1294 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301295 atomic_dec(&t->conns_in_use);
1296 if (t->hash_base && (tid >= t->hash_base)) {
1297 if (family == AF_INET6)
1298 atomic_sub(2, &t->hash_tids_in_use);
1299 else
1300 atomic_dec(&t->hash_tids_in_use);
1301 } else {
1302 if (family == AF_INET6)
1303 atomic_sub(2, &t->tids_in_use);
1304 else
1305 atomic_dec(&t->tids_in_use);
1306 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301307 }
1308
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001309 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1310 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001311 mk_tid_release(skb, chan, tid);
1312 t4_ofld_send(adap, skb);
1313 } else
1314 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001315}
1316EXPORT_SYMBOL(cxgb4_remove_tid);
1317
1318/*
1319 * Allocate and initialize the TID tables. Returns 0 on success.
1320 */
1321static int tid_init(struct tid_info *t)
1322{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301323 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301324 unsigned int max_ftids = t->nftids + t->nsftids;
1325 unsigned int natids = t->natids;
1326 unsigned int stid_bmap_size;
1327 unsigned int ftid_bmap_size;
1328 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001329
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001330 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301331 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001332 size = t->ntids * sizeof(*t->tid_tab) +
1333 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001334 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001335 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001336 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301337 max_ftids * sizeof(*t->ftid_tab) +
1338 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001339
Michal Hocko752ade62017-05-08 15:57:27 -07001340 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001341 if (!t->tid_tab)
1342 return -ENOMEM;
1343
1344 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1345 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001346 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001347 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301348 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001349 spin_lock_init(&t->stid_lock);
1350 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301351 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001352
1353 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301354 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301355 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001356 t->afree = NULL;
1357 t->atids_in_use = 0;
1358 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301359 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301360 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001361
1362 /* Setup the free list for atid_tab and clear the stid bitmap. */
1363 if (natids) {
1364 while (--natids)
1365 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1366 t->afree = t->atid_tab;
1367 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301368
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301369 if (is_offload(adap)) {
1370 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1371 /* Reserve stid 0 for T4/T5 adapters */
1372 if (!t->stid_base &&
1373 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1374 __set_bit(0, t->stid_bmap);
1375 }
1376
1377 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001378 return 0;
1379}
1380
1381/**
1382 * cxgb4_create_server - create an IP server
1383 * @dev: the device
1384 * @stid: the server TID
1385 * @sip: local IP address to bind server to
1386 * @sport: the server's TCP port
1387 * @queue: queue to direct messages from this server to
1388 *
1389 * Create an IP server for the given port and address.
1390 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1391 */
1392int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001393 __be32 sip, __be16 sport, __be16 vlan,
1394 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001395{
1396 unsigned int chan;
1397 struct sk_buff *skb;
1398 struct adapter *adap;
1399 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301400 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001401
1402 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1403 if (!skb)
1404 return -ENOMEM;
1405
1406 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001407 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001408 INIT_TP_WR(req, 0);
1409 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1410 req->local_port = sport;
1411 req->peer_port = htons(0);
1412 req->local_ip = sip;
1413 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001414 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001415 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001416 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1417 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301418 ret = t4_mgmt_tx(adap, skb);
1419 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001420}
1421EXPORT_SYMBOL(cxgb4_create_server);
1422
Vipul Pandya80f40c12013-07-04 16:10:45 +05301423/* cxgb4_create_server6 - create an IPv6 server
1424 * @dev: the device
1425 * @stid: the server TID
1426 * @sip: local IPv6 address to bind server to
1427 * @sport: the server's TCP port
1428 * @queue: queue to direct messages from this server to
1429 *
1430 * Create an IPv6 server for the given port and address.
1431 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1432 */
1433int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1434 const struct in6_addr *sip, __be16 sport,
1435 unsigned int queue)
1436{
1437 unsigned int chan;
1438 struct sk_buff *skb;
1439 struct adapter *adap;
1440 struct cpl_pass_open_req6 *req;
1441 int ret;
1442
1443 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1444 if (!skb)
1445 return -ENOMEM;
1446
1447 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001448 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301449 INIT_TP_WR(req, 0);
1450 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1451 req->local_port = sport;
1452 req->peer_port = htons(0);
1453 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1454 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1455 req->peer_ip_hi = cpu_to_be64(0);
1456 req->peer_ip_lo = cpu_to_be64(0);
1457 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001458 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001459 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1460 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301461 ret = t4_mgmt_tx(adap, skb);
1462 return net_xmit_eval(ret);
1463}
1464EXPORT_SYMBOL(cxgb4_create_server6);
1465
1466int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1467 unsigned int queue, bool ipv6)
1468{
1469 struct sk_buff *skb;
1470 struct adapter *adap;
1471 struct cpl_close_listsvr_req *req;
1472 int ret;
1473
1474 adap = netdev2adap(dev);
1475
1476 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1477 if (!skb)
1478 return -ENOMEM;
1479
Johannes Berg4df864c2017-06-16 14:29:21 +02001480 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301481 INIT_TP_WR(req, 0);
1482 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001483 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1484 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301485 ret = t4_mgmt_tx(adap, skb);
1486 return net_xmit_eval(ret);
1487}
1488EXPORT_SYMBOL(cxgb4_remove_server);
1489
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001490/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001491 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1492 * @mtus: the HW MTU table
1493 * @mtu: the target MTU
1494 * @idx: index of selected entry in the MTU table
1495 *
1496 * Returns the index and the value in the HW MTU table that is closest to
1497 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1498 * table, in which case that smallest available value is selected.
1499 */
1500unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1501 unsigned int *idx)
1502{
1503 unsigned int i = 0;
1504
1505 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1506 ++i;
1507 if (idx)
1508 *idx = i;
1509 return mtus[i];
1510}
1511EXPORT_SYMBOL(cxgb4_best_mtu);
1512
1513/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301514 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1515 * @mtus: the HW MTU table
1516 * @header_size: Header Size
1517 * @data_size_max: maximum Data Segment Size
1518 * @data_size_align: desired Data Segment Size Alignment (2^N)
1519 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1520 *
1521 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1522 * MTU Table based solely on a Maximum MTU parameter, we break that
1523 * parameter up into a Header Size and Maximum Data Segment Size, and
1524 * provide a desired Data Segment Size Alignment. If we find an MTU in
1525 * the Hardware MTU Table which will result in a Data Segment Size with
1526 * the requested alignment _and_ that MTU isn't "too far" from the
1527 * closest MTU, then we'll return that rather than the closest MTU.
1528 */
1529unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1530 unsigned short header_size,
1531 unsigned short data_size_max,
1532 unsigned short data_size_align,
1533 unsigned int *mtu_idxp)
1534{
1535 unsigned short max_mtu = header_size + data_size_max;
1536 unsigned short data_size_align_mask = data_size_align - 1;
1537 int mtu_idx, aligned_mtu_idx;
1538
1539 /* Scan the MTU Table till we find an MTU which is larger than our
1540 * Maximum MTU or we reach the end of the table. Along the way,
1541 * record the last MTU found, if any, which will result in a Data
1542 * Segment Length matching the requested alignment.
1543 */
1544 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1545 unsigned short data_size = mtus[mtu_idx] - header_size;
1546
1547 /* If this MTU minus the Header Size would result in a
1548 * Data Segment Size of the desired alignment, remember it.
1549 */
1550 if ((data_size & data_size_align_mask) == 0)
1551 aligned_mtu_idx = mtu_idx;
1552
1553 /* If we're not at the end of the Hardware MTU Table and the
1554 * next element is larger than our Maximum MTU, drop out of
1555 * the loop.
1556 */
1557 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1558 break;
1559 }
1560
1561 /* If we fell out of the loop because we ran to the end of the table,
1562 * then we just have to use the last [largest] entry.
1563 */
1564 if (mtu_idx == NMTUS)
1565 mtu_idx--;
1566
1567 /* If we found an MTU which resulted in the requested Data Segment
1568 * Length alignment and that's "not far" from the largest MTU which is
1569 * less than or equal to the maximum MTU, then use that.
1570 */
1571 if (aligned_mtu_idx >= 0 &&
1572 mtu_idx - aligned_mtu_idx <= 1)
1573 mtu_idx = aligned_mtu_idx;
1574
1575 /* If the caller has passed in an MTU Index pointer, pass the
1576 * MTU Index back. Return the MTU value.
1577 */
1578 if (mtu_idxp)
1579 *mtu_idxp = mtu_idx;
1580 return mtus[mtu_idx];
1581}
1582EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1583
1584/**
Hariprasad S27999802015-09-23 17:19:26 +05301585 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1586 * @chip: chip type
1587 * @viid: VI id of the given port
1588 *
1589 * Return the SMT index for this VI.
1590 */
1591unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1592{
1593 /* In T4/T5, SMT contains 256 SMAC entries organized in
1594 * 128 rows of 2 entries each.
1595 * In T6, SMT contains 256 SMAC entries in 256 rows.
1596 * TODO: The below code needs to be updated when we add support
1597 * for 256 VFs.
1598 */
1599 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1600 return ((viid & 0x7f) << 1);
1601 else
1602 return (viid & 0x7f);
1603}
1604EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1605
1606/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001607 * cxgb4_port_chan - get the HW channel of a port
1608 * @dev: the net device for the port
1609 *
1610 * Return the HW Tx channel of the given port.
1611 */
1612unsigned int cxgb4_port_chan(const struct net_device *dev)
1613{
1614 return netdev2pinfo(dev)->tx_chan;
1615}
1616EXPORT_SYMBOL(cxgb4_port_chan);
1617
Vipul Pandya881806b2012-05-18 15:29:24 +05301618unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1619{
1620 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001621 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301622
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301623 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1624 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301625 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301626 lp_count = LP_COUNT_G(v1);
1627 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001628 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301629 lp_count = LP_COUNT_T5_G(v1);
1630 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001631 }
1632 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301633}
1634EXPORT_SYMBOL(cxgb4_dbfifo_count);
1635
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001636/**
1637 * cxgb4_port_viid - get the VI id of a port
1638 * @dev: the net device for the port
1639 *
1640 * Return the VI id of the given port.
1641 */
1642unsigned int cxgb4_port_viid(const struct net_device *dev)
1643{
1644 return netdev2pinfo(dev)->viid;
1645}
1646EXPORT_SYMBOL(cxgb4_port_viid);
1647
1648/**
1649 * cxgb4_port_idx - get the index of a port
1650 * @dev: the net device for the port
1651 *
1652 * Return the index of the given port.
1653 */
1654unsigned int cxgb4_port_idx(const struct net_device *dev)
1655{
1656 return netdev2pinfo(dev)->port_id;
1657}
1658EXPORT_SYMBOL(cxgb4_port_idx);
1659
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001660void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1661 struct tp_tcp_stats *v6)
1662{
1663 struct adapter *adap = pci_get_drvdata(pdev);
1664
1665 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301666 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001667 spin_unlock(&adap->stats_lock);
1668}
1669EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1670
1671void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1672 const unsigned int *pgsz_order)
1673{
1674 struct adapter *adap = netdev2adap(dev);
1675
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301676 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1677 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1678 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1679 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001680}
1681EXPORT_SYMBOL(cxgb4_iscsi_init);
1682
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301683int cxgb4_flush_eq_cache(struct net_device *dev)
1684{
1685 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301686
Rahul Lakkireddy736c3b92017-12-08 09:48:40 +05301687 return t4_sge_ctxt_flush(adap, adap->mbox, CTXT_EGRESS);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301688}
1689EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1690
1691static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1692{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301693 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301694 __be64 indices;
1695 int ret;
1696
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301697 spin_lock(&adap->win0_lock);
1698 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1699 sizeof(indices), (__be32 *)&indices,
1700 T4_MEMORY_READ);
1701 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301702 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001703 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1704 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301705 }
1706 return ret;
1707}
1708
1709int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1710 u16 size)
1711{
1712 struct adapter *adap = netdev2adap(dev);
1713 u16 hw_pidx, hw_cidx;
1714 int ret;
1715
1716 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1717 if (ret)
1718 goto out;
1719
1720 if (pidx != hw_pidx) {
1721 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301722 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301723
1724 if (pidx >= hw_pidx)
1725 delta = pidx - hw_pidx;
1726 else
1727 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301728
1729 if (is_t4(adap->params.chip))
1730 val = PIDX_V(delta);
1731 else
1732 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301733 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301734 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1735 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301736 }
1737out:
1738 return ret;
1739}
1740EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1741
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301742int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1743{
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301744 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301745 u32 edc0_end, edc1_end, mc0_end, mc1_end;
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05301746 u32 offset, memtype, memaddr;
1747 struct adapter *adap;
1748 u32 hma_size = 0;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301749 int ret;
1750
1751 adap = netdev2adap(dev);
1752
1753 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1754
1755 /* Figure out where the offset lands in the Memory Type/Address scheme.
1756 * This code assumes that the memory is laid out starting at offset 0
1757 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1758 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1759 * MC0, and some have both MC0 and MC1.
1760 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301761 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1762 edc0_size = EDRAM0_SIZE_G(size) << 20;
1763 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1764 edc1_size = EDRAM1_SIZE_G(size) << 20;
1765 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1766 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301767
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05301768 if (t4_read_reg(adap, MA_TARGET_MEM_ENABLE_A) & HMA_MUX_F) {
1769 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1770 hma_size = EXT_MEM1_SIZE_G(size) << 20;
1771 }
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301772 edc0_end = edc0_size;
1773 edc1_end = edc0_end + edc1_size;
1774 mc0_end = edc1_end + mc0_size;
1775
1776 if (offset < edc0_end) {
1777 memtype = MEM_EDC0;
1778 memaddr = offset;
1779 } else if (offset < edc1_end) {
1780 memtype = MEM_EDC1;
1781 memaddr = offset - edc0_end;
1782 } else {
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05301783 if (hma_size && (offset < (edc1_end + hma_size))) {
1784 memtype = MEM_HMA;
1785 memaddr = offset - edc1_end;
1786 } else if (offset < mc0_end) {
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301787 memtype = MEM_MC0;
1788 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301789 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301790 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1791 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301792 mc1_end = mc0_end + mc1_size;
1793 if (offset < mc1_end) {
1794 memtype = MEM_MC1;
1795 memaddr = offset - mc0_end;
1796 } else {
1797 /* offset beyond the end of any memory */
1798 goto err;
1799 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301800 } else {
1801 /* T4/T6 only has a single memory channel */
1802 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301803 }
1804 }
1805
1806 spin_lock(&adap->win0_lock);
1807 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1808 spin_unlock(&adap->win0_lock);
1809 return ret;
1810
1811err:
1812 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1813 stag, offset);
1814 return -EINVAL;
1815}
1816EXPORT_SYMBOL(cxgb4_read_tpte);
1817
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301818u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1819{
1820 u32 hi, lo;
1821 struct adapter *adap;
1822
1823 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301824 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1825 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301826
1827 return ((u64)hi << 32) | (u64)lo;
1828}
1829EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1830
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301831int cxgb4_bar2_sge_qregs(struct net_device *dev,
1832 unsigned int qid,
1833 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301834 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301835 u64 *pbar2_qoffset,
1836 unsigned int *pbar2_qid)
1837{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301838 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301839 qid,
1840 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1841 ? T4_BAR2_QTYPE_EGRESS
1842 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301843 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301844 pbar2_qoffset,
1845 pbar2_qid);
1846}
1847EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1848
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001849static struct pci_driver cxgb4_driver;
1850
1851static void check_neigh_update(struct neighbour *neigh)
1852{
1853 const struct device *parent;
1854 const struct net_device *netdev = neigh->dev;
1855
Parav Panditd0d7b102017-02-04 11:00:49 -06001856 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001857 netdev = vlan_dev_real_dev(netdev);
1858 parent = netdev->dev.parent;
1859 if (parent && parent->driver == &cxgb4_driver.driver)
1860 t4_l2t_update(dev_get_drvdata(parent), neigh);
1861}
1862
1863static int netevent_cb(struct notifier_block *nb, unsigned long event,
1864 void *data)
1865{
1866 switch (event) {
1867 case NETEVENT_NEIGH_UPDATE:
1868 check_neigh_update(data);
1869 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001870 case NETEVENT_REDIRECT:
1871 default:
1872 break;
1873 }
1874 return 0;
1875}
1876
1877static bool netevent_registered;
1878static struct notifier_block cxgb4_netevent_nb = {
1879 .notifier_call = netevent_cb
1880};
1881
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301882static void drain_db_fifo(struct adapter *adap, int usecs)
1883{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001884 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301885
1886 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301887 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1888 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301889 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301890 lp_count = LP_COUNT_G(v1);
1891 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001892 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301893 lp_count = LP_COUNT_T5_G(v1);
1894 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001895 }
1896
1897 if (lp_count == 0 && hp_count == 0)
1898 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301899 set_current_state(TASK_UNINTERRUPTIBLE);
1900 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301901 } while (1);
1902}
1903
1904static void disable_txq_db(struct sge_txq *q)
1905{
Steve Wise05eb2382014-03-14 21:52:08 +05301906 unsigned long flags;
1907
1908 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301909 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301910 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301911}
1912
Steve Wise05eb2382014-03-14 21:52:08 +05301913static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301914{
1915 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301916 if (q->db_pidx_inc) {
1917 /* Make sure that all writes to the TX descriptors
1918 * are committed before we tell HW about them.
1919 */
1920 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301921 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1922 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301923 q->db_pidx_inc = 0;
1924 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301925 q->db_disabled = 0;
1926 spin_unlock_irq(&q->db_lock);
1927}
1928
1929static void disable_dbs(struct adapter *adap)
1930{
1931 int i;
1932
1933 for_each_ethrxq(&adap->sge, i)
1934 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301935 if (is_offload(adap)) {
1936 struct sge_uld_txq_info *txq_info =
1937 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1938
1939 if (txq_info) {
1940 for_each_ofldtxq(&adap->sge, i) {
1941 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1942
1943 disable_txq_db(&txq->q);
1944 }
1945 }
1946 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301947 for_each_port(adap, i)
1948 disable_txq_db(&adap->sge.ctrlq[i].q);
1949}
1950
1951static void enable_dbs(struct adapter *adap)
1952{
1953 int i;
1954
1955 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301956 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301957 if (is_offload(adap)) {
1958 struct sge_uld_txq_info *txq_info =
1959 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1960
1961 if (txq_info) {
1962 for_each_ofldtxq(&adap->sge, i) {
1963 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1964
1965 enable_txq_db(adap, &txq->q);
1966 }
1967 }
1968 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301969 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301970 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1971}
1972
1973static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1974{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301975 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1976
1977 if (adap->uld && adap->uld[type].handle)
1978 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301979}
1980
1981static void process_db_full(struct work_struct *work)
1982{
1983 struct adapter *adap;
1984
1985 adap = container_of(work, struct adapter, db_full_task);
1986
1987 drain_db_fifo(adap, dbfifo_drain_delay);
1988 enable_dbs(adap);
1989 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301990 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1991 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1992 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1993 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1994 else
1995 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1996 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301997}
1998
1999static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
2000{
2001 u16 hw_pidx, hw_cidx;
2002 int ret;
2003
Steve Wise05eb2382014-03-14 21:52:08 +05302004 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302005 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2006 if (ret)
2007 goto out;
2008 if (q->db_pidx != hw_pidx) {
2009 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302010 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302011
2012 if (q->db_pidx >= hw_pidx)
2013 delta = q->db_pidx - hw_pidx;
2014 else
2015 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302016
2017 if (is_t4(adap->params.chip))
2018 val = PIDX_V(delta);
2019 else
2020 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302021 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302022 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2023 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302024 }
2025out:
2026 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302027 q->db_pidx_inc = 0;
2028 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302029 if (ret)
2030 CH_WARN(adap, "DB drop recovery failed.\n");
2031}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302032
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302033static void recover_all_queues(struct adapter *adap)
2034{
2035 int i;
2036
2037 for_each_ethrxq(&adap->sge, i)
2038 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302039 if (is_offload(adap)) {
2040 struct sge_uld_txq_info *txq_info =
2041 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2042 if (txq_info) {
2043 for_each_ofldtxq(&adap->sge, i) {
2044 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2045
2046 sync_txq_pidx(adap, &txq->q);
2047 }
2048 }
2049 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302050 for_each_port(adap, i)
2051 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2052}
2053
Vipul Pandya881806b2012-05-18 15:29:24 +05302054static void process_db_drop(struct work_struct *work)
2055{
2056 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302057
Vipul Pandya881806b2012-05-18 15:29:24 +05302058 adap = container_of(work, struct adapter, db_drop_task);
2059
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302060 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302061 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002062 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302063 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002064 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302065 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002066 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302067 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302068 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002069 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2070 u16 qid = (dropped_db >> 15) & 0x1ffff;
2071 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302072 u64 bar2_qoffset;
2073 unsigned int bar2_qid;
2074 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002075
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302076 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002077 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302078 if (ret)
2079 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2080 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2081 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302082 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302083 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002084
2085 /* Re-enable BAR2 WC */
2086 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2087 }
2088
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302089 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2090 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302091}
2092
2093void t4_db_full(struct adapter *adap)
2094{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302095 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302096 disable_dbs(adap);
2097 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302098 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2099 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002100 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002101 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302102}
2103
2104void t4_db_dropped(struct adapter *adap)
2105{
Steve Wise05eb2382014-03-14 21:52:08 +05302106 if (is_t4(adap->params.chip)) {
2107 disable_dbs(adap);
2108 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2109 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002110 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302111}
2112
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302113void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002114{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002115 if (!netevent_registered) {
2116 register_netevent_notifier(&cxgb4_netevent_nb);
2117 netevent_registered = true;
2118 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002119}
2120
2121static void detach_ulds(struct adapter *adap)
2122{
2123 unsigned int i;
2124
2125 mutex_lock(&uld_mutex);
2126 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002127
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002128 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002129 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302130 adap->uld[i].state_change(adap->uld[i].handle,
2131 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002132
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002133 if (netevent_registered && list_empty(&adapter_list)) {
2134 unregister_netevent_notifier(&cxgb4_netevent_nb);
2135 netevent_registered = false;
2136 }
2137 mutex_unlock(&uld_mutex);
2138}
2139
2140static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2141{
2142 unsigned int i;
2143
2144 mutex_lock(&uld_mutex);
2145 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302146 if (adap->uld && adap->uld[i].handle)
2147 adap->uld[i].state_change(adap->uld[i].handle,
2148 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002149 mutex_unlock(&uld_mutex);
2150}
2151
Anish Bhatt1bb60372014-10-14 20:07:22 -07002152#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002153static int cxgb4_inet6addr_handler(struct notifier_block *this,
2154 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302155{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002156 struct inet6_ifaddr *ifa = data;
2157 struct net_device *event_dev = ifa->idev->dev;
2158 const struct device *parent = NULL;
2159#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302160 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002161#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002162 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002163 event_dev = vlan_dev_real_dev(event_dev);
2164#if IS_ENABLED(CONFIG_BONDING)
2165 if (event_dev->flags & IFF_MASTER) {
2166 list_for_each_entry(adap, &adapter_list, list_node) {
2167 switch (event) {
2168 case NETDEV_UP:
2169 cxgb4_clip_get(adap->port[0],
2170 (const u32 *)ifa, 1);
2171 break;
2172 case NETDEV_DOWN:
2173 cxgb4_clip_release(adap->port[0],
2174 (const u32 *)ifa, 1);
2175 break;
2176 default:
2177 break;
2178 }
2179 }
2180 return NOTIFY_OK;
2181 }
2182#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302183
Anish Bhattb5a02f52015-01-14 15:17:34 -08002184 if (event_dev)
2185 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302186
Anish Bhattb5a02f52015-01-14 15:17:34 -08002187 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302188 switch (event) {
2189 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002190 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302191 break;
2192 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002193 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302194 break;
2195 default:
2196 break;
2197 }
2198 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002199 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302200}
2201
Anish Bhattb5a02f52015-01-14 15:17:34 -08002202static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302203static struct notifier_block cxgb4_inet6addr_notifier = {
2204 .notifier_call = cxgb4_inet6addr_handler
2205};
2206
Vipul Pandya01bcca62013-07-04 16:10:46 +05302207static void update_clip(const struct adapter *adap)
2208{
2209 int i;
2210 struct net_device *dev;
2211 int ret;
2212
2213 rcu_read_lock();
2214
2215 for (i = 0; i < MAX_NPORTS; i++) {
2216 dev = adap->port[i];
2217 ret = 0;
2218
2219 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002220 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302221
2222 if (ret < 0)
2223 break;
2224 }
2225 rcu_read_unlock();
2226}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002227#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302228
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002229/**
2230 * cxgb_up - enable the adapter
2231 * @adap: adapter being enabled
2232 *
2233 * Called when the first port is enabled, this function performs the
2234 * actions necessary to make an adapter operational, such as completing
2235 * the initialization of HW modules, and enabling interrupts.
2236 *
2237 * Must be called with the rtnl lock held.
2238 */
2239static int cxgb_up(struct adapter *adap)
2240{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002241 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002242
Raju Rangoju91060382017-06-19 17:40:48 +05302243 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002244 err = setup_sge_queues(adap);
2245 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302246 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002247 err = setup_rss(adap);
2248 if (err)
2249 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002250
2251 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002252 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002253 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2254 adap->msix_info[0].desc, adap);
2255 if (err)
2256 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002257 err = request_msix_queue_irqs(adap);
2258 if (err) {
2259 free_irq(adap->msix_info[0].vec, adap);
2260 goto irq_err;
2261 }
2262 } else {
2263 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2264 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002265 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002266 if (err)
2267 goto irq_err;
2268 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302269
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002270 enable_rx(adap);
2271 t4_sge_start(adap);
2272 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002273 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302274 mutex_unlock(&uld_mutex);
2275
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002276 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002277#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302278 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002279#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302280 /* Initialize hash mac addr list*/
2281 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002282 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302283
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002284 irq_err:
2285 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002286 freeq:
2287 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302288 rel_lock:
2289 mutex_unlock(&uld_mutex);
2290 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002291}
2292
2293static void cxgb_down(struct adapter *adapter)
2294{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002295 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302296 cancel_work_sync(&adapter->db_full_task);
2297 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002298 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002299 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002300
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002301 t4_sge_stop(adapter);
2302 t4_free_sge_resources(adapter);
2303 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002304}
2305
2306/*
2307 * net_device operations
2308 */
2309static int cxgb_open(struct net_device *dev)
2310{
2311 int err;
2312 struct port_info *pi = netdev_priv(dev);
2313 struct adapter *adapter = pi->adapter;
2314
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002315 netif_carrier_off(dev);
2316
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002317 if (!(adapter->flags & FULL_INIT_DONE)) {
2318 err = cxgb_up(adapter);
2319 if (err < 0)
2320 return err;
2321 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002322
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302323 /* It's possible that the basic port information could have
2324 * changed since we first read it.
2325 */
2326 err = t4_update_port_info(pi);
2327 if (err < 0)
2328 return err;
2329
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002330 err = link_start(dev);
2331 if (!err)
2332 netif_tx_start_all_queues(dev);
2333 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002334}
2335
2336static int cxgb_close(struct net_device *dev)
2337{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002338 struct port_info *pi = netdev_priv(dev);
2339 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302340 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002341
2342 netif_tx_stop_all_queues(dev);
2343 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302344 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2345#ifdef CONFIG_CHELSIO_T4_DCB
2346 cxgb4_dcb_reset(dev);
2347 dcb_tx_queue_prio_enable(dev, false);
2348#endif
2349 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002350}
2351
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002352int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002353 __be32 sip, __be16 sport, __be16 vlan,
2354 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002355{
2356 int ret;
2357 struct filter_entry *f;
2358 struct adapter *adap;
2359 int i;
2360 u8 *val;
2361
2362 adap = netdev2adap(dev);
2363
Vipul Pandya1cab7752012-12-10 09:30:55 +00002364 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302365 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002366 stid += adap->tids.nftids;
2367
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002368 /* Check to make sure the filter requested is writable ...
2369 */
2370 f = &adap->tids.ftid_tab[stid];
2371 ret = writable_filter(f);
2372 if (ret)
2373 return ret;
2374
2375 /* Clear out any old resources being used by the filter before
2376 * we start constructing the new filter.
2377 */
2378 if (f->valid)
2379 clear_filter(adap, f);
2380
2381 /* Clear out filter specifications */
2382 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2383 f->fs.val.lport = cpu_to_be16(sport);
2384 f->fs.mask.lport = ~0;
2385 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002386 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002387 for (i = 0; i < 4; i++) {
2388 f->fs.val.lip[i] = val[i];
2389 f->fs.mask.lip[i] = ~0;
2390 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302391 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002392 f->fs.val.iport = port;
2393 f->fs.mask.iport = mask;
2394 }
2395 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002396
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302397 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302398 f->fs.val.proto = IPPROTO_TCP;
2399 f->fs.mask.proto = ~0;
2400 }
2401
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002402 f->fs.dirsteer = 1;
2403 f->fs.iq = queue;
2404 /* Mark filter as locked */
2405 f->locked = 1;
2406 f->fs.rpttid = 1;
2407
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302408 /* Save the actual tid. We need this to get the corresponding
2409 * filter entry structure in filter_rpl.
2410 */
2411 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002412 ret = set_filter_wr(adap, stid);
2413 if (ret) {
2414 clear_filter(adap, f);
2415 return ret;
2416 }
2417
2418 return 0;
2419}
2420EXPORT_SYMBOL(cxgb4_create_server_filter);
2421
2422int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2423 unsigned int queue, bool ipv6)
2424{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002425 struct filter_entry *f;
2426 struct adapter *adap;
2427
2428 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002429
2430 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302431 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002432 stid += adap->tids.nftids;
2433
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002434 f = &adap->tids.ftid_tab[stid];
2435 /* Unlock the filter */
2436 f->locked = 0;
2437
Wei Yongjun8c148462016-08-20 15:32:41 +00002438 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002439}
2440EXPORT_SYMBOL(cxgb4_remove_server_filter);
2441
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002442static void cxgb_get_stats(struct net_device *dev,
2443 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002444{
2445 struct port_stats stats;
2446 struct port_info *p = netdev_priv(dev);
2447 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002448
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002449 /* Block retrieving statistics during EEH error
2450 * recovery. Otherwise, the recovery might fail
2451 * and the PCI device will be removed permanently
2452 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002453 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002454 if (!netif_device_present(dev)) {
2455 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002456 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002457 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302458 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2459 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002460 spin_unlock(&adapter->stats_lock);
2461
2462 ns->tx_bytes = stats.tx_octets;
2463 ns->tx_packets = stats.tx_frames;
2464 ns->rx_bytes = stats.rx_octets;
2465 ns->rx_packets = stats.rx_frames;
2466 ns->multicast = stats.rx_mcast_frames;
2467
2468 /* detailed rx_errors */
2469 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2470 stats.rx_runt;
2471 ns->rx_over_errors = 0;
2472 ns->rx_crc_errors = stats.rx_fcs_err;
2473 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302474 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002475 stats.rx_ovflow2 + stats.rx_ovflow3 +
2476 stats.rx_trunc0 + stats.rx_trunc1 +
2477 stats.rx_trunc2 + stats.rx_trunc3;
2478 ns->rx_missed_errors = 0;
2479
2480 /* detailed tx_errors */
2481 ns->tx_aborted_errors = 0;
2482 ns->tx_carrier_errors = 0;
2483 ns->tx_fifo_errors = 0;
2484 ns->tx_heartbeat_errors = 0;
2485 ns->tx_window_errors = 0;
2486
2487 ns->tx_errors = stats.tx_error_frames;
2488 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2489 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002490}
2491
2492static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2493{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002494 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002495 int ret = 0, prtad, devad;
2496 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302497 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002498 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2499
2500 switch (cmd) {
2501 case SIOCGMIIPHY:
2502 if (pi->mdio_addr < 0)
2503 return -EOPNOTSUPP;
2504 data->phy_id = pi->mdio_addr;
2505 break;
2506 case SIOCGMIIREG:
2507 case SIOCSMIIREG:
2508 if (mdio_phy_id_is_c45(data->phy_id)) {
2509 prtad = mdio_phy_id_prtad(data->phy_id);
2510 devad = mdio_phy_id_devad(data->phy_id);
2511 } else if (data->phy_id < 32) {
2512 prtad = data->phy_id;
2513 devad = 0;
2514 data->reg_num &= 0x1f;
2515 } else
2516 return -EINVAL;
2517
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302518 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002519 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002520 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002521 data->reg_num, &data->val_out);
2522 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002523 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002524 data->reg_num, data->val_in);
2525 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302526 case SIOCGHWTSTAMP:
2527 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2528 sizeof(pi->tstamp_config)) ?
2529 -EFAULT : 0;
2530 case SIOCSHWTSTAMP:
2531 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2532 sizeof(pi->tstamp_config)))
2533 return -EFAULT;
2534
Atul Guptaa45695042017-07-04 16:46:20 +05302535 if (!is_t4(adapter->params.chip)) {
2536 switch (pi->tstamp_config.tx_type) {
2537 case HWTSTAMP_TX_OFF:
2538 case HWTSTAMP_TX_ON:
2539 break;
2540 default:
2541 return -ERANGE;
2542 }
2543
2544 switch (pi->tstamp_config.rx_filter) {
2545 case HWTSTAMP_FILTER_NONE:
2546 pi->rxtstamp = false;
2547 break;
2548 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2549 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2550 cxgb4_ptprx_timestamping(pi, pi->port_id,
2551 PTP_TS_L4);
2552 break;
2553 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2554 cxgb4_ptprx_timestamping(pi, pi->port_id,
2555 PTP_TS_L2_L4);
2556 break;
2557 case HWTSTAMP_FILTER_ALL:
2558 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2559 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2560 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2561 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2562 pi->rxtstamp = true;
2563 break;
2564 default:
2565 pi->tstamp_config.rx_filter =
2566 HWTSTAMP_FILTER_NONE;
2567 return -ERANGE;
2568 }
2569
2570 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2571 (pi->tstamp_config.rx_filter ==
2572 HWTSTAMP_FILTER_NONE)) {
2573 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2574 pi->ptp_enable = false;
2575 }
2576
2577 if (pi->tstamp_config.rx_filter !=
2578 HWTSTAMP_FILTER_NONE) {
2579 if (cxgb4_ptp_redirect_rx_packet(adapter,
2580 pi) >= 0)
2581 pi->ptp_enable = true;
2582 }
2583 } else {
2584 /* For T4 Adapters */
2585 switch (pi->tstamp_config.rx_filter) {
2586 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302587 pi->rxtstamp = false;
2588 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302589 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302590 pi->rxtstamp = true;
2591 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302592 default:
2593 pi->tstamp_config.rx_filter =
2594 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302595 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302596 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302597 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302598 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2599 sizeof(pi->tstamp_config)) ?
2600 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002601 default:
2602 return -EOPNOTSUPP;
2603 }
2604 return ret;
2605}
2606
2607static void cxgb_set_rxmode(struct net_device *dev)
2608{
2609 /* unfortunately we can't return errors to the stack */
2610 set_rxmode(dev, -1, false);
2611}
2612
2613static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2614{
2615 int ret;
2616 struct port_info *pi = netdev_priv(dev);
2617
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302618 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002619 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002620 if (!ret)
2621 dev->mtu = new_mtu;
2622 return ret;
2623}
2624
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302625#ifdef CONFIG_PCI_IOV
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302626static int cxgb4_mgmt_open(struct net_device *dev)
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302627{
2628 /* Turn carrier off since we don't have to transmit anything on this
2629 * interface.
2630 */
2631 netif_carrier_off(dev);
2632 return 0;
2633}
2634
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302635/* Fill MAC address that will be assigned by the FW */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302636static void cxgb4_mgmt_fill_vf_station_mac_addr(struct adapter *adap)
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302637{
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302638 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302639 unsigned int i, vf, nvfs;
2640 u16 a, b;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302641 int err;
2642 u8 *na;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302643
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302644 adap->params.pci.vpd_cap_addr = pci_find_capability(adap->pdev,
2645 PCI_CAP_ID_VPD);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302646 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302647 if (err)
2648 return;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302649
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302650 na = adap->params.vpd.na;
2651 for (i = 0; i < ETH_ALEN; i++)
2652 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2653 hex2val(na[2 * i + 1]));
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302654
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302655 a = (hw_addr[0] << 8) | hw_addr[1];
2656 b = (hw_addr[1] << 8) | hw_addr[2];
2657 a ^= b;
2658 a |= 0x0200; /* locally assigned Ethernet MAC address */
2659 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2660 macaddr[0] = a >> 8;
2661 macaddr[1] = a & 0xff;
2662
2663 for (i = 2; i < 5; i++)
2664 macaddr[i] = hw_addr[i + 1];
2665
2666 for (vf = 0, nvfs = pci_sriov_get_totalvfs(adap->pdev);
2667 vf < nvfs; vf++) {
2668 macaddr[5] = adap->pf * 16 + vf;
2669 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, macaddr);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302670 }
2671}
2672
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302673static int cxgb4_mgmt_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302674{
2675 struct port_info *pi = netdev_priv(dev);
2676 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302677 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302678
2679 /* verify MAC addr is valid */
2680 if (!is_valid_ether_addr(mac)) {
2681 dev_err(pi->adapter->pdev_dev,
2682 "Invalid Ethernet address %pM for VF %d\n",
2683 mac, vf);
2684 return -EINVAL;
2685 }
2686
2687 dev_info(pi->adapter->pdev_dev,
2688 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302689 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2690 if (!ret)
2691 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2692 return ret;
2693}
2694
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302695static int cxgb4_mgmt_get_vf_config(struct net_device *dev,
2696 int vf, struct ifla_vf_info *ivi)
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302697{
2698 struct port_info *pi = netdev_priv(dev);
2699 struct adapter *adap = pi->adapter;
2700
2701 if (vf >= adap->num_vfs)
2702 return -EINVAL;
2703 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302704 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2705 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302706 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2707 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302708}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302709
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302710static int cxgb4_mgmt_get_phys_port_id(struct net_device *dev,
2711 struct netdev_phys_item_id *ppid)
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302712{
2713 struct port_info *pi = netdev_priv(dev);
2714 unsigned int phy_port_id;
2715
2716 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2717 ppid->id_len = sizeof(phy_port_id);
2718 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2719 return 0;
2720}
2721
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302722static int cxgb4_mgmt_set_vf_rate(struct net_device *dev, int vf,
2723 int min_tx_rate, int max_tx_rate)
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302724{
2725 struct port_info *pi = netdev_priv(dev);
2726 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302727 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302728 u32 fw_pfvf, fw_class;
2729 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302730 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302731 u16 pktsize;
2732
2733 if (vf >= adap->num_vfs)
2734 return -EINVAL;
2735
2736 if (min_tx_rate) {
2737 dev_err(adap->pdev_dev,
2738 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2739 min_tx_rate, vf);
2740 return -EINVAL;
2741 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302742
2743 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302744 if (ret != FW_SUCCESS) {
2745 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302746 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302747 return -EINVAL;
2748 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302749
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302750 if (!link_ok) {
2751 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2752 return -EINVAL;
2753 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302754
2755 if (max_tx_rate > speed) {
2756 dev_err(adap->pdev_dev,
2757 "Max tx rate %d for VF %d can't be > link-speed %u",
2758 max_tx_rate, vf, speed);
2759 return -EINVAL;
2760 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302761
2762 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302763 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2764 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2765 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2766 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2767 /* configure Traffic Class for rate-limiting */
2768 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2769 SCHED_CLASS_LEVEL_CL_RL,
2770 SCHED_CLASS_MODE_CLASS,
2771 SCHED_CLASS_RATEUNIT_BITS,
2772 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302773 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302774 max_tx_rate * 1000, 0, pktsize);
2775 if (ret) {
2776 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2777 ret);
2778 return -EINVAL;
2779 }
2780 dev_info(adap->pdev_dev,
2781 "Class %d with MSS %u configured with rate %u\n",
2782 class_id, pktsize, max_tx_rate);
2783
2784 /* bind VF to configured Traffic Class */
2785 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2786 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2787 fw_class = class_id;
2788 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2789 &fw_class);
2790 if (ret) {
2791 dev_err(adap->pdev_dev,
2792 "Err %d in binding VF %d to Traffic Class %d\n",
2793 ret, vf, class_id);
2794 return -EINVAL;
2795 }
2796 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2797 adap->pf, vf, class_id);
2798 adap->vfinfo[vf].tx_rate = max_tx_rate;
2799 return 0;
2800}
2801
Ganesh Goudar9d5fd922018-01-24 20:44:07 +05302802static int cxgb4_mgmt_set_vf_vlan(struct net_device *dev, int vf,
2803 u16 vlan, u8 qos, __be16 vlan_proto)
2804{
2805 struct port_info *pi = netdev_priv(dev);
2806 struct adapter *adap = pi->adapter;
2807 int ret;
2808
2809 if (vf >= adap->num_vfs || vlan > 4095 || qos > 7)
2810 return -EINVAL;
2811
2812 if (vlan_proto != htons(ETH_P_8021Q) || qos != 0)
2813 return -EPROTONOSUPPORT;
2814
2815 ret = t4_set_vlan_acl(adap, adap->mbox, vf + 1, vlan);
2816 if (!ret) {
2817 adap->vfinfo[vf].vlan = vlan;
2818 return 0;
2819 }
2820
2821 dev_err(adap->pdev_dev, "Err %d %s VLAN ACL for PF/VF %d/%d\n",
2822 ret, (vlan ? "setting" : "clearing"), adap->pf, vf);
2823 return ret;
2824}
2825#endif /* CONFIG_PCI_IOV */
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302826
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002827static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2828{
2829 int ret;
2830 struct sockaddr *addr = p;
2831 struct port_info *pi = netdev_priv(dev);
2832
2833 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002834 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002835
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302836 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002837 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002838 if (ret < 0)
2839 return ret;
2840
2841 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2842 pi->xact_addr_filt = ret;
2843 return 0;
2844}
2845
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002846#ifdef CONFIG_NET_POLL_CONTROLLER
2847static void cxgb_netpoll(struct net_device *dev)
2848{
2849 struct port_info *pi = netdev_priv(dev);
2850 struct adapter *adap = pi->adapter;
2851
2852 if (adap->flags & USING_MSIX) {
2853 int i;
2854 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2855
2856 for (i = pi->nqsets; i; i--, rx++)
2857 t4_sge_intr_msix(0, &rx->rspq);
2858 } else
2859 t4_intr_handler(adap)(0, adap);
2860}
2861#endif
2862
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302863static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2864{
2865 struct port_info *pi = netdev_priv(dev);
2866 struct adapter *adap = pi->adapter;
2867 struct sched_class *e;
2868 struct ch_sched_params p;
2869 struct ch_sched_queue qe;
2870 u32 req_rate;
2871 int err = 0;
2872
2873 if (!can_sched(dev))
2874 return -ENOTSUPP;
2875
2876 if (index < 0 || index > pi->nqsets - 1)
2877 return -EINVAL;
2878
2879 if (!(adap->flags & FULL_INIT_DONE)) {
2880 dev_err(adap->pdev_dev,
2881 "Failed to rate limit on queue %d. Link Down?\n",
2882 index);
2883 return -EINVAL;
2884 }
2885
2886 /* Convert from Mbps to Kbps */
2887 req_rate = rate << 10;
2888
Ganesh Goudard185efc2018-03-09 13:00:52 +05302889 /* Max rate is 100 Gbps */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302890 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2891 dev_err(adap->pdev_dev,
Ganesh Goudard185efc2018-03-09 13:00:52 +05302892 "Invalid rate %u Mbps, Max rate is %u Mbps\n",
2893 rate, SCHED_MAX_RATE_KBPS >> 10);
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302894 return -ERANGE;
2895 }
2896
2897 /* First unbind the queue from any existing class */
2898 memset(&qe, 0, sizeof(qe));
2899 qe.queue = index;
2900 qe.class = SCHED_CLS_NONE;
2901
2902 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2903 if (err) {
2904 dev_err(adap->pdev_dev,
2905 "Unbinding Queue %d on port %d fail. Err: %d\n",
2906 index, pi->port_id, err);
2907 return err;
2908 }
2909
2910 /* Queue already unbound */
2911 if (!req_rate)
2912 return 0;
2913
2914 /* Fetch any available unused or matching scheduling class */
2915 memset(&p, 0, sizeof(p));
2916 p.type = SCHED_CLASS_TYPE_PACKET;
2917 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2918 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2919 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2920 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2921 p.u.params.channel = pi->tx_chan;
2922 p.u.params.class = SCHED_CLS_NONE;
2923 p.u.params.minrate = 0;
2924 p.u.params.maxrate = req_rate;
2925 p.u.params.weight = 0;
2926 p.u.params.pktsize = dev->mtu;
2927
2928 e = cxgb4_sched_class_alloc(dev, &p);
2929 if (!e)
2930 return -ENOMEM;
2931
2932 /* Bind the queue to a scheduling class */
2933 memset(&qe, 0, sizeof(qe));
2934 qe.queue = index;
2935 qe.class = e->idx;
2936
2937 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2938 if (err)
2939 dev_err(adap->pdev_dev,
2940 "Queue rate limiting failed. Err: %d\n", err);
2941 return err;
2942}
2943
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302944static int cxgb_setup_tc_flower(struct net_device *dev,
2945 struct tc_cls_flower_offload *cls_flower)
2946{
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302947 switch (cls_flower->command) {
2948 case TC_CLSFLOWER_REPLACE:
2949 return cxgb4_tc_flower_replace(dev, cls_flower);
2950 case TC_CLSFLOWER_DESTROY:
2951 return cxgb4_tc_flower_destroy(dev, cls_flower);
2952 case TC_CLSFLOWER_STATS:
2953 return cxgb4_tc_flower_stats(dev, cls_flower);
2954 default:
2955 return -EOPNOTSUPP;
2956 }
2957}
2958
Jiri Pirkof7323042017-08-07 10:15:20 +02002959static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002960 struct tc_cls_u32_offload *cls_u32)
2961{
Jiri Pirkof7323042017-08-07 10:15:20 +02002962 switch (cls_u32->command) {
2963 case TC_CLSU32_NEW_KNODE:
2964 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002965 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002966 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002967 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002968 default:
2969 return -EOPNOTSUPP;
2970 }
2971}
2972
Jiri Pirkocd019e92017-10-19 15:50:40 +02002973static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2974 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302975{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002976 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302977 struct port_info *pi = netdev2pinfo(dev);
2978 struct adapter *adap = netdev2adap(dev);
2979
2980 if (!(adap->flags & FULL_INIT_DONE)) {
2981 dev_err(adap->pdev_dev,
2982 "Failed to setup tc on port %d. Link Down?\n",
2983 pi->port_id);
2984 return -EINVAL;
2985 }
2986
Jakub Kicinski2a84bba2018-01-25 14:00:46 -08002987 if (!tc_cls_can_offload_and_chain0(dev, type_data))
Jiri Pirko44ae12a2017-11-01 11:47:39 +01002988 return -EOPNOTSUPP;
2989
Jiri Pirkof7323042017-08-07 10:15:20 +02002990 switch (type) {
2991 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002992 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302993 case TC_SETUP_CLSFLOWER:
2994 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002995 default:
2996 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302997 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302998}
2999
Jiri Pirkocd019e92017-10-19 15:50:40 +02003000static int cxgb_setup_tc_block(struct net_device *dev,
3001 struct tc_block_offload *f)
3002{
3003 struct port_info *pi = netdev2pinfo(dev);
3004
3005 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3006 return -EOPNOTSUPP;
3007
3008 switch (f->command) {
3009 case TC_BLOCK_BIND:
3010 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
3011 pi, dev);
3012 case TC_BLOCK_UNBIND:
3013 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
3014 return 0;
3015 default:
3016 return -EOPNOTSUPP;
3017 }
3018}
3019
3020static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
3021 void *type_data)
3022{
3023 switch (type) {
Jiri Pirkocd019e92017-10-19 15:50:40 +02003024 case TC_SETUP_BLOCK:
3025 return cxgb_setup_tc_block(dev, type_data);
3026 default:
3027 return -EOPNOTSUPP;
3028 }
3029}
3030
Ganesh Goudar846eac32018-01-10 18:15:08 +05303031static void cxgb_del_udp_tunnel(struct net_device *netdev,
3032 struct udp_tunnel_info *ti)
3033{
3034 struct port_info *pi = netdev_priv(netdev);
3035 struct adapter *adapter = pi->adapter;
3036 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3037 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3038 int ret = 0, i;
3039
3040 if (chip_ver < CHELSIO_T6)
3041 return;
3042
3043 switch (ti->type) {
3044 case UDP_TUNNEL_TYPE_VXLAN:
3045 if (!adapter->vxlan_port_cnt ||
3046 adapter->vxlan_port != ti->port)
3047 return; /* Invalid VxLAN destination port */
3048
3049 adapter->vxlan_port_cnt--;
3050 if (adapter->vxlan_port_cnt)
3051 return;
3052
3053 adapter->vxlan_port = 0;
3054 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A, 0);
3055 break;
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303056 case UDP_TUNNEL_TYPE_GENEVE:
3057 if (!adapter->geneve_port_cnt ||
3058 adapter->geneve_port != ti->port)
3059 return; /* Invalid GENEVE destination port */
3060
3061 adapter->geneve_port_cnt--;
3062 if (adapter->geneve_port_cnt)
3063 return;
3064
3065 adapter->geneve_port = 0;
3066 t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A, 0);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303067 default:
3068 return;
3069 }
3070
3071 /* Matchall mac entries can be deleted only after all tunnel ports
3072 * are brought down or removed.
3073 */
3074 if (!adapter->rawf_cnt)
3075 return;
3076 for_each_port(adapter, i) {
3077 pi = adap2pinfo(adapter, i);
3078 ret = t4_free_raw_mac_filt(adapter, pi->viid,
3079 match_all_mac, match_all_mac,
3080 adapter->rawf_start +
3081 pi->port_id,
3082 1, pi->port_id, true);
3083 if (ret < 0) {
3084 netdev_info(netdev, "Failed to free mac filter entry, for port %d\n",
3085 i);
3086 return;
3087 }
3088 atomic_dec(&adapter->mps_encap[adapter->rawf_start +
3089 pi->port_id].refcnt);
3090 }
3091}
3092
3093static void cxgb_add_udp_tunnel(struct net_device *netdev,
3094 struct udp_tunnel_info *ti)
3095{
3096 struct port_info *pi = netdev_priv(netdev);
3097 struct adapter *adapter = pi->adapter;
3098 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3099 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3100 int i, ret;
3101
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303102 if (chip_ver < CHELSIO_T6 || !adapter->rawf_cnt)
Ganesh Goudar846eac32018-01-10 18:15:08 +05303103 return;
3104
3105 switch (ti->type) {
3106 case UDP_TUNNEL_TYPE_VXLAN:
Ganesh Goudar846eac32018-01-10 18:15:08 +05303107 /* Callback for adding vxlan port can be called with the same
3108 * port for both IPv4 and IPv6. We should not disable the
3109 * offloading when the same port for both protocols is added
3110 * and later one of them is removed.
3111 */
3112 if (adapter->vxlan_port_cnt &&
3113 adapter->vxlan_port == ti->port) {
3114 adapter->vxlan_port_cnt++;
3115 return;
3116 }
3117
3118 /* We will support only one VxLAN port */
3119 if (adapter->vxlan_port_cnt) {
3120 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3121 be16_to_cpu(adapter->vxlan_port),
3122 be16_to_cpu(ti->port));
3123 return;
3124 }
3125
3126 adapter->vxlan_port = ti->port;
3127 adapter->vxlan_port_cnt = 1;
3128
3129 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A,
3130 VXLAN_V(be16_to_cpu(ti->port)) | VXLAN_EN_F);
3131 break;
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303132 case UDP_TUNNEL_TYPE_GENEVE:
3133 if (adapter->geneve_port_cnt &&
3134 adapter->geneve_port == ti->port) {
3135 adapter->geneve_port_cnt++;
3136 return;
3137 }
3138
3139 /* We will support only one GENEVE port */
3140 if (adapter->geneve_port_cnt) {
3141 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3142 be16_to_cpu(adapter->geneve_port),
3143 be16_to_cpu(ti->port));
3144 return;
3145 }
3146
3147 adapter->geneve_port = ti->port;
3148 adapter->geneve_port_cnt = 1;
3149
3150 t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A,
3151 GENEVE_V(be16_to_cpu(ti->port)) | GENEVE_EN_F);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303152 default:
3153 return;
3154 }
3155
3156 /* Create a 'match all' mac filter entry for inner mac,
3157 * if raw mac interface is supported. Once the linux kernel provides
3158 * driver entry points for adding/deleting the inner mac addresses,
3159 * we will remove this 'match all' entry and fallback to adding
3160 * exact match filters.
3161 */
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303162 for_each_port(adapter, i) {
3163 pi = adap2pinfo(adapter, i);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303164
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303165 ret = t4_alloc_raw_mac_filt(adapter, pi->viid,
3166 match_all_mac,
3167 match_all_mac,
3168 adapter->rawf_start +
3169 pi->port_id,
3170 1, pi->port_id, true);
3171 if (ret < 0) {
3172 netdev_info(netdev, "Failed to allocate a mac filter entry, not adding port %d\n",
3173 be16_to_cpu(ti->port));
3174 cxgb_del_udp_tunnel(netdev, ti);
3175 return;
Ganesh Goudar846eac32018-01-10 18:15:08 +05303176 }
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303177 atomic_inc(&adapter->mps_encap[ret].refcnt);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303178 }
3179}
3180
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303181static netdev_features_t cxgb_features_check(struct sk_buff *skb,
3182 struct net_device *dev,
3183 netdev_features_t features)
3184{
3185 struct port_info *pi = netdev_priv(dev);
3186 struct adapter *adapter = pi->adapter;
3187
3188 if (CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3189 return features;
3190
3191 /* Check if hw supports offload for this packet */
3192 if (!skb->encapsulation || cxgb_encap_offload_supported(skb))
3193 return features;
3194
3195 /* Offload is not supported for this encapsulated packet */
3196 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3197}
3198
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303199static netdev_features_t cxgb_fix_features(struct net_device *dev,
3200 netdev_features_t features)
3201{
3202 /* Disable GRO, if RX_CSUM is disabled */
3203 if (!(features & NETIF_F_RXCSUM))
3204 features &= ~NETIF_F_GRO;
3205
3206 return features;
3207}
3208
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003209static const struct net_device_ops cxgb4_netdev_ops = {
3210 .ndo_open = cxgb_open,
3211 .ndo_stop = cxgb_close,
3212 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003213 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003214 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003215 .ndo_set_rx_mode = cxgb_set_rxmode,
3216 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003217 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003218 .ndo_validate_addr = eth_validate_addr,
3219 .ndo_do_ioctl = cxgb_ioctl,
3220 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003221#ifdef CONFIG_NET_POLL_CONTROLLER
3222 .ndo_poll_controller = cxgb_netpoll,
3223#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303224#ifdef CONFIG_CHELSIO_T4_FCOE
3225 .ndo_fcoe_enable = cxgb_fcoe_enable,
3226 .ndo_fcoe_disable = cxgb_fcoe_disable,
3227#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303228 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303229 .ndo_setup_tc = cxgb_setup_tc,
Ganesh Goudar846eac32018-01-10 18:15:08 +05303230 .ndo_udp_tunnel_add = cxgb_add_udp_tunnel,
3231 .ndo_udp_tunnel_del = cxgb_del_udp_tunnel,
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303232 .ndo_features_check = cxgb_features_check,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303233 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003234};
3235
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303236#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303237static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303238 .ndo_open = cxgb4_mgmt_open,
3239 .ndo_set_vf_mac = cxgb4_mgmt_set_vf_mac,
3240 .ndo_get_vf_config = cxgb4_mgmt_get_vf_config,
3241 .ndo_set_vf_rate = cxgb4_mgmt_set_vf_rate,
3242 .ndo_get_phys_port_id = cxgb4_mgmt_get_phys_port_id,
Ganesh Goudar9d5fd922018-01-24 20:44:07 +05303243 .ndo_set_vf_vlan = cxgb4_mgmt_set_vf_vlan,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303244};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303245#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303246
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303247static void cxgb4_mgmt_get_drvinfo(struct net_device *dev,
3248 struct ethtool_drvinfo *info)
Hariprasad Shenai78294512016-08-11 21:06:23 +05303249{
3250 struct adapter *adapter = netdev2adap(dev);
3251
3252 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3253 strlcpy(info->version, cxgb4_driver_version,
3254 sizeof(info->version));
3255 strlcpy(info->bus_info, pci_name(adapter->pdev),
3256 sizeof(info->bus_info));
3257}
3258
3259static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303260 .get_drvinfo = cxgb4_mgmt_get_drvinfo,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303261};
3262
Ganesh Goudar8b7372c2018-03-16 14:22:57 +05303263static void notify_fatal_err(struct work_struct *work)
3264{
3265 struct adapter *adap;
3266
3267 adap = container_of(work, struct adapter, fatal_err_notify_task);
3268 notify_ulds(adap, CXGB4_STATE_FATAL_ERROR);
3269}
3270
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003271void t4_fatal_err(struct adapter *adap)
3272{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303273 int port;
3274
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003275 if (pci_channel_offline(adap->pdev))
3276 return;
3277
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303278 /* Disable the SGE since ULDs are going to free resources that
3279 * could be exposed to the adapter. RDMA MWs for example...
3280 */
3281 t4_shutdown_adapter(adap);
3282 for_each_port(adap, port) {
3283 struct net_device *dev = adap->port[port];
3284
3285 /* If we get here in very early initialization the network
3286 * devices may not have been set up yet.
3287 */
3288 if (!dev)
3289 continue;
3290
3291 netif_tx_stop_all_queues(dev);
3292 netif_carrier_off(dev);
3293 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003294 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
Ganesh Goudar8b7372c2018-03-16 14:22:57 +05303295 queue_work(adap->workq, &adap->fatal_err_notify_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003296}
3297
3298static void setup_memwin(struct adapter *adap)
3299{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303300 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003301
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303302 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003303}
3304
3305static void setup_memwin_rdma(struct adapter *adap)
3306{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003307 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303308 u32 start;
3309 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003310
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303311 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3312 start &= PCI_BASE_ADDRESS_MEM_MASK;
3313 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003314 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3315 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303316 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3317 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003318 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303319 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003320 adap->vres.ocq.start);
3321 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303322 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003323 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003324}
3325
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05303326/* HMA Definitions */
3327
3328/* The maximum number of address that can be send in a single FW cmd */
3329#define HMA_MAX_ADDR_IN_CMD 5
3330
3331#define HMA_PAGE_SIZE PAGE_SIZE
3332
3333#define HMA_MAX_NO_FW_ADDRESS (16 << 10) /* FW supports 16K addresses */
3334
3335#define HMA_PAGE_ORDER \
3336 ((HMA_PAGE_SIZE < HMA_MAX_NO_FW_ADDRESS) ? \
3337 ilog2(HMA_MAX_NO_FW_ADDRESS / HMA_PAGE_SIZE) : 0)
3338
3339/* The minimum and maximum possible HMA sizes that can be specified in the FW
3340 * configuration(in units of MB).
3341 */
3342#define HMA_MIN_TOTAL_SIZE 1
3343#define HMA_MAX_TOTAL_SIZE \
3344 (((HMA_PAGE_SIZE << HMA_PAGE_ORDER) * \
3345 HMA_MAX_NO_FW_ADDRESS) >> 20)
3346
3347static void adap_free_hma_mem(struct adapter *adapter)
3348{
3349 struct scatterlist *iter;
3350 struct page *page;
3351 int i;
3352
3353 if (!adapter->hma.sgt)
3354 return;
3355
3356 if (adapter->hma.flags & HMA_DMA_MAPPED_FLAG) {
3357 dma_unmap_sg(adapter->pdev_dev, adapter->hma.sgt->sgl,
3358 adapter->hma.sgt->nents, PCI_DMA_BIDIRECTIONAL);
3359 adapter->hma.flags &= ~HMA_DMA_MAPPED_FLAG;
3360 }
3361
3362 for_each_sg(adapter->hma.sgt->sgl, iter,
3363 adapter->hma.sgt->orig_nents, i) {
3364 page = sg_page(iter);
3365 if (page)
3366 __free_pages(page, HMA_PAGE_ORDER);
3367 }
3368
3369 kfree(adapter->hma.phy_addr);
3370 sg_free_table(adapter->hma.sgt);
3371 kfree(adapter->hma.sgt);
3372 adapter->hma.sgt = NULL;
3373}
3374
3375static int adap_config_hma(struct adapter *adapter)
3376{
3377 struct scatterlist *sgl, *iter;
3378 struct sg_table *sgt;
3379 struct page *newpage;
3380 unsigned int i, j, k;
3381 u32 param, hma_size;
3382 unsigned int ncmds;
3383 size_t page_size;
3384 u32 page_order;
3385 int node, ret;
3386
3387 /* HMA is supported only for T6+ cards.
3388 * Avoid initializing HMA in kdump kernels.
3389 */
3390 if (is_kdump_kernel() ||
3391 CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3392 return 0;
3393
3394 /* Get the HMA region size required by fw */
3395 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3396 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_HMA_SIZE));
3397 ret = t4_query_params(adapter, adapter->mbox, adapter->pf, 0,
3398 1, &param, &hma_size);
3399 /* An error means card has its own memory or HMA is not supported by
3400 * the firmware. Return without any errors.
3401 */
3402 if (ret || !hma_size)
3403 return 0;
3404
3405 if (hma_size < HMA_MIN_TOTAL_SIZE ||
3406 hma_size > HMA_MAX_TOTAL_SIZE) {
3407 dev_err(adapter->pdev_dev,
3408 "HMA size %uMB beyond bounds(%u-%lu)MB\n",
3409 hma_size, HMA_MIN_TOTAL_SIZE, HMA_MAX_TOTAL_SIZE);
3410 return -EINVAL;
3411 }
3412
3413 page_size = HMA_PAGE_SIZE;
3414 page_order = HMA_PAGE_ORDER;
3415 adapter->hma.sgt = kzalloc(sizeof(*adapter->hma.sgt), GFP_KERNEL);
3416 if (unlikely(!adapter->hma.sgt)) {
3417 dev_err(adapter->pdev_dev, "HMA SG table allocation failed\n");
3418 return -ENOMEM;
3419 }
3420 sgt = adapter->hma.sgt;
3421 /* FW returned value will be in MB's
3422 */
3423 sgt->orig_nents = (hma_size << 20) / (page_size << page_order);
3424 if (sg_alloc_table(sgt, sgt->orig_nents, GFP_KERNEL)) {
3425 dev_err(adapter->pdev_dev, "HMA SGL allocation failed\n");
3426 kfree(adapter->hma.sgt);
3427 adapter->hma.sgt = NULL;
3428 return -ENOMEM;
3429 }
3430
3431 sgl = adapter->hma.sgt->sgl;
3432 node = dev_to_node(adapter->pdev_dev);
3433 for_each_sg(sgl, iter, sgt->orig_nents, i) {
3434 newpage = alloc_pages_node(node, __GFP_NOWARN | GFP_KERNEL,
3435 page_order);
3436 if (!newpage) {
3437 dev_err(adapter->pdev_dev,
3438 "Not enough memory for HMA page allocation\n");
3439 ret = -ENOMEM;
3440 goto free_hma;
3441 }
3442 sg_set_page(iter, newpage, page_size << page_order, 0);
3443 }
3444
3445 sgt->nents = dma_map_sg(adapter->pdev_dev, sgl, sgt->orig_nents,
3446 DMA_BIDIRECTIONAL);
3447 if (!sgt->nents) {
3448 dev_err(adapter->pdev_dev,
3449 "Not enough memory for HMA DMA mapping");
3450 ret = -ENOMEM;
3451 goto free_hma;
3452 }
3453 adapter->hma.flags |= HMA_DMA_MAPPED_FLAG;
3454
3455 adapter->hma.phy_addr = kcalloc(sgt->nents, sizeof(dma_addr_t),
3456 GFP_KERNEL);
3457 if (unlikely(!adapter->hma.phy_addr))
3458 goto free_hma;
3459
3460 for_each_sg(sgl, iter, sgt->nents, i) {
3461 newpage = sg_page(iter);
3462 adapter->hma.phy_addr[i] = sg_dma_address(iter);
3463 }
3464
3465 ncmds = DIV_ROUND_UP(sgt->nents, HMA_MAX_ADDR_IN_CMD);
3466 /* Pass on the addresses to firmware */
3467 for (i = 0, k = 0; i < ncmds; i++, k += HMA_MAX_ADDR_IN_CMD) {
3468 struct fw_hma_cmd hma_cmd;
3469 u8 naddr = HMA_MAX_ADDR_IN_CMD;
3470 u8 soc = 0, eoc = 0;
3471 u8 hma_mode = 1; /* Presently we support only Page table mode */
3472
3473 soc = (i == 0) ? 1 : 0;
3474 eoc = (i == ncmds - 1) ? 1 : 0;
3475
3476 /* For last cmd, set naddr corresponding to remaining
3477 * addresses
3478 */
3479 if (i == ncmds - 1) {
3480 naddr = sgt->nents % HMA_MAX_ADDR_IN_CMD;
3481 naddr = naddr ? naddr : HMA_MAX_ADDR_IN_CMD;
3482 }
3483 memset(&hma_cmd, 0, sizeof(hma_cmd));
3484 hma_cmd.op_pkd = htonl(FW_CMD_OP_V(FW_HMA_CMD) |
3485 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
3486 hma_cmd.retval_len16 = htonl(FW_LEN16(hma_cmd));
3487
3488 hma_cmd.mode_to_pcie_params =
3489 htonl(FW_HMA_CMD_MODE_V(hma_mode) |
3490 FW_HMA_CMD_SOC_V(soc) | FW_HMA_CMD_EOC_V(eoc));
3491
3492 /* HMA cmd size specified in MB's */
3493 hma_cmd.naddr_size =
3494 htonl(FW_HMA_CMD_SIZE_V(hma_size) |
3495 FW_HMA_CMD_NADDR_V(naddr));
3496
3497 /* Total Page size specified in units of 4K */
3498 hma_cmd.addr_size_pkd =
3499 htonl(FW_HMA_CMD_ADDR_SIZE_V
3500 ((page_size << page_order) >> 12));
3501
3502 /* Fill the 5 addresses */
3503 for (j = 0; j < naddr; j++) {
3504 hma_cmd.phy_address[j] =
3505 cpu_to_be64(adapter->hma.phy_addr[j + k]);
3506 }
3507 ret = t4_wr_mbox(adapter, adapter->mbox, &hma_cmd,
3508 sizeof(hma_cmd), &hma_cmd);
3509 if (ret) {
3510 dev_err(adapter->pdev_dev,
3511 "HMA FW command failed with err %d\n", ret);
3512 goto free_hma;
3513 }
3514 }
3515
3516 if (!ret)
3517 dev_info(adapter->pdev_dev,
3518 "Reserved %uMB host memory for HMA\n", hma_size);
3519 return ret;
3520
3521free_hma:
3522 adap_free_hma_mem(adapter);
3523 return ret;
3524}
3525
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003526static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3527{
3528 u32 v;
3529 int ret;
3530
3531 /* get device capabilities */
3532 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303533 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3534 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303535 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303536 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003537 if (ret < 0)
3538 return ret;
3539
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303540 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3541 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303542 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003543 if (ret < 0)
3544 return ret;
3545
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303546 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003547 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303548 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3549 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003550 if (ret < 0)
3551 return ret;
3552
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303553 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303554 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3555 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003556 if (ret < 0)
3557 return ret;
3558
3559 t4_sge_init(adap);
3560
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003561 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303562 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303563 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303564 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3565 v = t4_read_reg(adap, TP_PIO_DATA_A);
3566 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003567
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003568 /* first 4 Tx modulation queues point to consecutive Tx channels */
3569 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303570 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3571 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003572
3573 /* associate each Tx modulation queue with consecutive Tx channels */
3574 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303575 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303576 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303577 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303578 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303579 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303580 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003581
3582#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3583 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303584 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3585 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3586 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3587 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3588 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3589 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3590 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3591 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3592 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3593 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003594 }
3595
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003596 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303597 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003598}
3599
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003600/*
3601 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3602 */
3603#define MAX_ATIDS 8192U
3604
3605/*
3606 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003607 *
3608 * If the firmware we're dealing with has Configuration File support, then
3609 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003610 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003611
3612/*
3613 * Tweak configuration based on module parameters, etc. Most of these have
3614 * defaults assigned to them by Firmware Configuration Files (if we're using
3615 * them) but need to be explicitly set if we're using hard-coded
3616 * initialization. But even in the case of using Firmware Configuration
3617 * Files, we'd like to expose the ability to change these via module
3618 * parameters so these are essentially common tweaks/settings for
3619 * Configuration Files and hard-coded initialization ...
3620 */
3621static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003622{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003623 /*
3624 * Fix up various Host-Dependent Parameters like Page Size, Cache
3625 * Line Size, etc. The firmware default is for a 4KB Page Size and
3626 * 64B Cache Line Size ...
3627 */
3628 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003629
Vipul Pandya636f9d32012-09-26 02:39:39 +00003630 /*
3631 * Process module parameters which affect early initialization.
3632 */
3633 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3634 dev_err(&adapter->pdev->dev,
3635 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3636 rx_dma_offset);
3637 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003638 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303639 t4_set_reg_field(adapter, SGE_CONTROL_A,
3640 PKTSHIFT_V(PKTSHIFT_M),
3641 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003642
Vipul Pandya636f9d32012-09-26 02:39:39 +00003643 /*
3644 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3645 * adds the pseudo header itself.
3646 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303647 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3648 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003649
3650 return 0;
3651}
3652
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303653/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3654 * unto themselves and they contain their own firmware to perform their
3655 * tasks ...
3656 */
3657static int phy_aq1202_version(const u8 *phy_fw_data,
3658 size_t phy_fw_size)
3659{
3660 int offset;
3661
3662 /* At offset 0x8 you're looking for the primary image's
3663 * starting offset which is 3 Bytes wide
3664 *
3665 * At offset 0xa of the primary image, you look for the offset
3666 * of the DRAM segment which is 3 Bytes wide.
3667 *
3668 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3669 * wide
3670 */
3671 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3672 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3673 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3674
3675 offset = le24(phy_fw_data + 0x8) << 12;
3676 offset = le24(phy_fw_data + offset + 0xa);
3677 return be16(phy_fw_data + offset + 0x27e);
3678
3679 #undef be16
3680 #undef le16
3681 #undef le24
3682}
3683
3684static struct info_10gbt_phy_fw {
3685 unsigned int phy_fw_id; /* PCI Device ID */
3686 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3687 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3688 int phy_flash; /* Has FLASH for PHY Firmware */
3689} phy_info_array[] = {
3690 {
3691 PHY_AQ1202_DEVICEID,
3692 PHY_AQ1202_FIRMWARE,
3693 phy_aq1202_version,
3694 1,
3695 },
3696 {
3697 PHY_BCM84834_DEVICEID,
3698 PHY_BCM84834_FIRMWARE,
3699 NULL,
3700 0,
3701 },
3702 { 0, NULL, NULL },
3703};
3704
3705static struct info_10gbt_phy_fw *find_phy_info(int devid)
3706{
3707 int i;
3708
3709 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3710 if (phy_info_array[i].phy_fw_id == devid)
3711 return &phy_info_array[i];
3712 }
3713 return NULL;
3714}
3715
3716/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3717 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3718 * we return a negative error number. If we transfer new firmware we return 1
3719 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3720 */
3721static int adap_init0_phy(struct adapter *adap)
3722{
3723 const struct firmware *phyf;
3724 int ret;
3725 struct info_10gbt_phy_fw *phy_info;
3726
3727 /* Use the device ID to determine which PHY file to flash.
3728 */
3729 phy_info = find_phy_info(adap->pdev->device);
3730 if (!phy_info) {
3731 dev_warn(adap->pdev_dev,
3732 "No PHY Firmware file found for this PHY\n");
3733 return -EOPNOTSUPP;
3734 }
3735
3736 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3737 * use that. The adapter firmware provides us with a memory buffer
3738 * where we can load a PHY firmware file from the host if we want to
3739 * override the PHY firmware File in flash.
3740 */
3741 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3742 adap->pdev_dev);
3743 if (ret < 0) {
3744 /* For adapters without FLASH attached to PHY for their
3745 * firmware, it's obviously a fatal error if we can't get the
3746 * firmware to the adapter. For adapters with PHY firmware
3747 * FLASH storage, it's worth a warning if we can't find the
3748 * PHY Firmware but we'll neuter the error ...
3749 */
3750 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3751 "/lib/firmware/%s, error %d\n",
3752 phy_info->phy_fw_file, -ret);
3753 if (phy_info->phy_flash) {
3754 int cur_phy_fw_ver = 0;
3755
3756 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3757 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3758 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3759 ret = 0;
3760 }
3761
3762 return ret;
3763 }
3764
3765 /* Load PHY Firmware onto adapter.
3766 */
3767 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3768 phy_info->phy_fw_version,
3769 (u8 *)phyf->data, phyf->size);
3770 if (ret < 0)
3771 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3772 -ret);
3773 else if (ret > 0) {
3774 int new_phy_fw_ver = 0;
3775
3776 if (phy_info->phy_fw_version)
3777 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3778 phyf->size);
3779 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3780 "Firmware /lib/firmware/%s, version %#x\n",
3781 phy_info->phy_fw_file, new_phy_fw_ver);
3782 }
3783
3784 release_firmware(phyf);
3785
3786 return ret;
3787}
3788
Vipul Pandya636f9d32012-09-26 02:39:39 +00003789/*
3790 * Attempt to initialize the adapter via a Firmware Configuration File.
3791 */
3792static int adap_init0_config(struct adapter *adapter, int reset)
3793{
3794 struct fw_caps_config_cmd caps_cmd;
3795 const struct firmware *cf;
3796 unsigned long mtype = 0, maddr = 0;
3797 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303798 int ret;
3799 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003800 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303801 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003802
3803 /*
3804 * Reset device if necessary.
3805 */
3806 if (reset) {
3807 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303808 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003809 if (ret < 0)
3810 goto bye;
3811 }
3812
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303813 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3814 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3815 * to be performed after any global adapter RESET above since some
3816 * PHYs only have local RAM copies of the PHY firmware.
3817 */
3818 if (is_10gbt_device(adapter->pdev->device)) {
3819 ret = adap_init0_phy(adapter);
3820 if (ret < 0)
3821 goto bye;
3822 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003823 /*
3824 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3825 * then use that. Otherwise, use the configuration file stored
3826 * in the adapter flash ...
3827 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303828 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003829 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303830 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003831 break;
3832 case CHELSIO_T5:
3833 fw_config_file = FW5_CFNAME;
3834 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303835 case CHELSIO_T6:
3836 fw_config_file = FW6_CFNAME;
3837 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003838 default:
3839 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3840 adapter->pdev->device);
3841 ret = -EINVAL;
3842 goto bye;
3843 }
3844
3845 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003846 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303847 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003848 mtype = FW_MEMTYPE_CF_FLASH;
3849 maddr = t4_flash_cfg_addr(adapter);
3850 } else {
3851 u32 params[7], val[7];
3852
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303853 sprintf(fw_config_file_path,
3854 "/lib/firmware/%s", fw_config_file);
3855 config_name = fw_config_file_path;
3856
Vipul Pandya636f9d32012-09-26 02:39:39 +00003857 if (cf->size >= FLASH_CFG_MAX_SIZE)
3858 ret = -ENOMEM;
3859 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303860 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3861 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003862 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303863 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003864 if (ret == 0) {
3865 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303866 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003867 * sizes have to be in terms of multiples of 4
3868 * bytes. So, if the Configuration File isn't
3869 * a multiple of 4 bytes in length we'll have
3870 * to write that out separately since we can't
3871 * guarantee that the bytes following the
3872 * residual byte in the buffer returned by
3873 * request_firmware() are zeroed out ...
3874 */
3875 size_t resid = cf->size & 0x3;
3876 size_t size = cf->size & ~0x3;
3877 __be32 *data = (__be32 *)cf->data;
3878
Hariprasad Shenai51678652014-11-21 12:52:02 +05303879 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3880 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003881
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303882 spin_lock(&adapter->win0_lock);
3883 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3884 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003885 if (ret == 0 && resid != 0) {
3886 union {
3887 __be32 word;
3888 char buf[4];
3889 } last;
3890 int i;
3891
3892 last.word = data[size >> 2];
3893 for (i = resid; i < 4; i++)
3894 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303895 ret = t4_memory_rw(adapter, 0, mtype,
3896 maddr + size,
3897 4, &last.word,
3898 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003899 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303900 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003901 }
3902 }
3903
3904 release_firmware(cf);
3905 if (ret)
3906 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003907 }
3908
Vipul Pandya636f9d32012-09-26 02:39:39 +00003909 /*
3910 * Issue a Capability Configuration command to the firmware to get it
3911 * to parse the Configuration File. We don't use t4_fw_config_file()
3912 * because we want the ability to modify various features after we've
3913 * processed the configuration file ...
3914 */
3915 memset(&caps_cmd, 0, sizeof(caps_cmd));
3916 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303917 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3918 FW_CMD_REQUEST_F |
3919 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303920 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303921 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3922 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3923 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003924 FW_LEN16(caps_cmd));
3925 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3926 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303927
3928 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3929 * Configuration File in FLASH), our last gasp effort is to use the
3930 * Firmware Configuration File which is embedded in the firmware. A
3931 * very few early versions of the firmware didn't have one embedded
3932 * but we can ignore those.
3933 */
3934 if (ret == -ENOENT) {
3935 memset(&caps_cmd, 0, sizeof(caps_cmd));
3936 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303937 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3938 FW_CMD_REQUEST_F |
3939 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303940 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3941 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3942 sizeof(caps_cmd), &caps_cmd);
3943 config_name = "Firmware Default";
3944 }
3945
3946 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003947 if (ret < 0)
3948 goto bye;
3949
Vipul Pandya636f9d32012-09-26 02:39:39 +00003950 finiver = ntohl(caps_cmd.finiver);
3951 finicsum = ntohl(caps_cmd.finicsum);
3952 cfcsum = ntohl(caps_cmd.cfcsum);
3953 if (finicsum != cfcsum)
3954 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3955 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3956 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003957
Vipul Pandya636f9d32012-09-26 02:39:39 +00003958 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003959 * And now tell the firmware to use the configuration we just loaded.
3960 */
3961 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303962 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3963 FW_CMD_REQUEST_F |
3964 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303965 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003966 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3967 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003968 if (ret < 0)
3969 goto bye;
3970
Vipul Pandya636f9d32012-09-26 02:39:39 +00003971 /*
3972 * Tweak configuration based on system architecture, module
3973 * parameters, etc.
3974 */
3975 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003976 if (ret < 0)
3977 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003978
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05303979 /* We will proceed even if HMA init fails. */
3980 ret = adap_config_hma(adapter);
3981 if (ret)
3982 dev_err(adapter->pdev_dev,
3983 "HMA configuration failed with error %d\n", ret);
3984
Vipul Pandya636f9d32012-09-26 02:39:39 +00003985 /*
3986 * And finally tell the firmware to initialize itself using the
3987 * parameters from the Configuration File.
3988 */
3989 ret = t4_fw_initialize(adapter, adapter->mbox);
3990 if (ret < 0)
3991 goto bye;
3992
Hariprasad Shenai06640312015-01-13 15:19:25 +05303993 /* Emit Firmware Configuration File information and return
3994 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003995 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003996 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303997 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3998 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003999 return 0;
4000
4001 /*
4002 * Something bad happened. Return the error ... (If the "error"
4003 * is that there's no Configuration File on the adapter we don't
4004 * want to issue a warning since this is fairly common.)
4005 */
4006bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304007 if (config_issued && ret != -ENOENT)
4008 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
4009 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004010 return ret;
4011}
4012
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304013static struct fw_info fw_info_array[] = {
4014 {
4015 .chip = CHELSIO_T4,
4016 .fs_name = FW4_CFNAME,
4017 .fw_mod_name = FW4_FNAME,
4018 .fw_hdr = {
4019 .chip = FW_HDR_CHIP_T4,
4020 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
4021 .intfver_nic = FW_INTFVER(T4, NIC),
4022 .intfver_vnic = FW_INTFVER(T4, VNIC),
4023 .intfver_ri = FW_INTFVER(T4, RI),
4024 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
4025 .intfver_fcoe = FW_INTFVER(T4, FCOE),
4026 },
4027 }, {
4028 .chip = CHELSIO_T5,
4029 .fs_name = FW5_CFNAME,
4030 .fw_mod_name = FW5_FNAME,
4031 .fw_hdr = {
4032 .chip = FW_HDR_CHIP_T5,
4033 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
4034 .intfver_nic = FW_INTFVER(T5, NIC),
4035 .intfver_vnic = FW_INTFVER(T5, VNIC),
4036 .intfver_ri = FW_INTFVER(T5, RI),
4037 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
4038 .intfver_fcoe = FW_INTFVER(T5, FCOE),
4039 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05304040 }, {
4041 .chip = CHELSIO_T6,
4042 .fs_name = FW6_CFNAME,
4043 .fw_mod_name = FW6_FNAME,
4044 .fw_hdr = {
4045 .chip = FW_HDR_CHIP_T6,
4046 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
4047 .intfver_nic = FW_INTFVER(T6, NIC),
4048 .intfver_vnic = FW_INTFVER(T6, VNIC),
4049 .intfver_ofld = FW_INTFVER(T6, OFLD),
4050 .intfver_ri = FW_INTFVER(T6, RI),
4051 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4052 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
4053 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4054 .intfver_fcoe = FW_INTFVER(T6, FCOE),
4055 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304056 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05304057
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304058};
4059
4060static struct fw_info *find_fw_info(int chip)
4061{
4062 int i;
4063
4064 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
4065 if (fw_info_array[i].chip == chip)
4066 return &fw_info_array[i];
4067 }
4068 return NULL;
4069}
4070
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004071/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004072 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004073 */
4074static int adap_init0(struct adapter *adap)
4075{
4076 int ret;
4077 u32 v, port_vec;
4078 enum dev_state state;
4079 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00004080 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05304081 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004082
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05304083 /* Grab Firmware Device Log parameters as early as possible so we have
4084 * access to it for debugging, etc.
4085 */
4086 ret = t4_init_devlog_params(adap);
4087 if (ret < 0)
4088 return ret;
4089
Hariprasad Shenai666224d2014-12-11 11:11:43 +05304090 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05304091 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
4092 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004093 if (ret < 0) {
4094 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
4095 ret);
4096 return ret;
4097 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004098 if (ret == adap->mbox)
4099 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004100
Vipul Pandya636f9d32012-09-26 02:39:39 +00004101 /*
4102 * If we're the Master PF Driver and the device is uninitialized,
4103 * then let's consider upgrading the firmware ... (We always want
4104 * to check the firmware version number in order to A. get it for
4105 * later reporting and B. to warn if the currently loaded firmware
4106 * is excessively mismatched relative to the driver.)
4107 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304108
Ganesh Goudar760446f2017-07-20 18:28:48 +05304109 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05304110 ret = t4_check_fw_version(adap);
4111 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05304112 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05304113 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004114 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304115 struct fw_info *fw_info;
4116 struct fw_hdr *card_fw;
4117 const struct firmware *fw;
4118 const u8 *fw_data = NULL;
4119 unsigned int fw_size = 0;
4120
4121 /* This is the firmware whose headers the driver was compiled
4122 * against
4123 */
4124 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
4125 if (fw_info == NULL) {
4126 dev_err(adap->pdev_dev,
4127 "unable to get firmware info for chip %d.\n",
4128 CHELSIO_CHIP_VERSION(adap->params.chip));
4129 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004130 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304131
4132 /* allocate memory to read the header of the firmware on the
4133 * card
4134 */
Michal Hocko752ade62017-05-08 15:57:27 -07004135 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304136
4137 /* Get FW from from /lib/firmware/ */
4138 ret = request_firmware(&fw, fw_info->fw_mod_name,
4139 adap->pdev_dev);
4140 if (ret < 0) {
4141 dev_err(adap->pdev_dev,
4142 "unable to load firmware image %s, error %d\n",
4143 fw_info->fw_mod_name, ret);
4144 } else {
4145 fw_data = fw->data;
4146 fw_size = fw->size;
4147 }
4148
4149 /* upgrade FW logic */
4150 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
4151 state, &reset);
4152
4153 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01004154 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07004155 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304156
Vipul Pandya636f9d32012-09-26 02:39:39 +00004157 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304158 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004159 }
4160
4161 /*
4162 * Grab VPD parameters. This should be done after we establish a
4163 * connection to the firmware since some of the VPD parameters
4164 * (notably the Core Clock frequency) are retrieved via requests to
4165 * the firmware. On the other hand, we need these fairly early on
4166 * so we do this right after getting ahold of the firmware.
4167 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304168 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004169 if (ret < 0)
4170 goto bye;
4171
Vipul Pandya636f9d32012-09-26 02:39:39 +00004172 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004173 * Find out what ports are available to us. Note that we need to do
4174 * this before calling adap_init0_no_config() since it needs nports
4175 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00004176 */
4177 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05304178 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4179 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304180 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004181 if (ret < 0)
4182 goto bye;
4183
4184 adap->params.nports = hweight32(port_vec);
4185 adap->params.portvec = port_vec;
4186
Hariprasad Shenai06640312015-01-13 15:19:25 +05304187 /* If the firmware is initialized already, emit a simply note to that
4188 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004189 */
4190 if (state == DEV_STATE_INIT) {
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05304191 ret = adap_config_hma(adap);
4192 if (ret)
4193 dev_err(adap->pdev_dev,
4194 "HMA configuration failed with error %d\n",
4195 ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004196 dev_info(adap->pdev_dev, "Coming up as %s: "\
4197 "Adapter already initialized\n",
4198 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00004199 } else {
4200 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
4201 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05304202
4203 /* Find out whether we're dealing with a version of the
4204 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004205 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05304206 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4207 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304208 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05304209 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004210
Hariprasad Shenai06640312015-01-13 15:19:25 +05304211 /* If the firmware doesn't support Configuration Files,
4212 * return an error.
4213 */
4214 if (ret < 0) {
4215 dev_err(adap->pdev_dev, "firmware doesn't support "
4216 "Firmware Configuration Files\n");
4217 goto bye;
4218 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004219
Hariprasad Shenai06640312015-01-13 15:19:25 +05304220 /* The firmware provides us with a memory buffer where we can
4221 * load a Configuration File from the host if we want to
4222 * override the Configuration File in flash.
4223 */
4224 ret = adap_init0_config(adap, reset);
4225 if (ret == -ENOENT) {
4226 dev_err(adap->pdev_dev, "no Configuration File "
4227 "present on adapter.\n");
4228 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004229 }
4230 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05304231 dev_err(adap->pdev_dev, "could not initialize "
4232 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004233 goto bye;
4234 }
4235 }
4236
Hariprasad Shenai06640312015-01-13 15:19:25 +05304237 /* Give the SGE code a chance to pull in anything that it needs ...
4238 * Note that this must be called after we retrieve our VPD parameters
4239 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004240 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05304241 ret = t4_sge_init(adap);
4242 if (ret < 0)
4243 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004244
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00004245 if (is_bypass_device(adap->pdev->device))
4246 adap->params.bypass = 1;
4247
Vipul Pandya636f9d32012-09-26 02:39:39 +00004248 /*
4249 * Grab some of our basic fundamental operating parameters.
4250 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004251#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05304252 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
4253 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004254
4255#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05304256 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
4257 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
4258 FW_PARAMS_PARAM_Y_V(0) | \
4259 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004260
Vipul Pandya636f9d32012-09-26 02:39:39 +00004261 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004262 params[1] = FW_PARAM_PFVF(L2T_START);
4263 params[2] = FW_PARAM_PFVF(L2T_END);
4264 params[3] = FW_PARAM_PFVF(FILTER_START);
4265 params[4] = FW_PARAM_PFVF(FILTER_END);
4266 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304267 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004268 if (ret < 0)
4269 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004270 adap->sge.egr_start = val[0];
4271 adap->l2t_start = val[1];
4272 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004273 adap->tids.ftid_base = val[3];
4274 adap->tids.nftids = val[4] - val[3] + 1;
4275 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004276
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304277 /* qids (ingress/egress) returned from firmware can be anywhere
4278 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
4279 * Hence driver needs to allocate memory for this range to
4280 * store the queue info. Get the highest IQFLINT/EQ index returned
4281 * in FW_EQ_*_CMD.alloc command.
4282 */
4283 params[0] = FW_PARAM_PFVF(EQ_END);
4284 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304285 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304286 if (ret < 0)
4287 goto bye;
4288 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
4289 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
4290
4291 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
4292 sizeof(*adap->sge.egr_map), GFP_KERNEL);
4293 if (!adap->sge.egr_map) {
4294 ret = -ENOMEM;
4295 goto bye;
4296 }
4297
4298 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
4299 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
4300 if (!adap->sge.ingr_map) {
4301 ret = -ENOMEM;
4302 goto bye;
4303 }
4304
4305 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304306 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304307 */
4308 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4309 sizeof(long), GFP_KERNEL);
4310 if (!adap->sge.starving_fl) {
4311 ret = -ENOMEM;
4312 goto bye;
4313 }
4314
4315 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4316 sizeof(long), GFP_KERNEL);
4317 if (!adap->sge.txq_maperr) {
4318 ret = -ENOMEM;
4319 goto bye;
4320 }
4321
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304322#ifdef CONFIG_DEBUG_FS
4323 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4324 sizeof(long), GFP_KERNEL);
4325 if (!adap->sge.blocked_fl) {
4326 ret = -ENOMEM;
4327 goto bye;
4328 }
4329#endif
4330
Anish Bhattb5a02f52015-01-14 15:17:34 -08004331 params[0] = FW_PARAM_PFVF(CLIP_START);
4332 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304333 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08004334 if (ret < 0)
4335 goto bye;
4336 adap->clipt_start = val[0];
4337 adap->clipt_end = val[1];
4338
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304339 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
4340 * Classes supported by the hardware/firmware so we hard code it here
4341 * for now.
4342 */
4343 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
4344
Vipul Pandya636f9d32012-09-26 02:39:39 +00004345 /* query params related to active filter region */
4346 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
4347 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304348 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004349 /* If Active filter size is set we enable establishing
4350 * offload connection through firmware work request
4351 */
4352 if ((val[0] != val[1]) && (ret >= 0)) {
4353 adap->flags |= FW_OFLD_CONN;
4354 adap->tids.aftid_base = val[0];
4355 adap->tids.aftid_end = val[1];
4356 }
4357
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004358 /* If we're running on newer firmware, let it know that we're
4359 * prepared to deal with encapsulated CPL messages. Older
4360 * firmware won't understand this and we'll just get
4361 * unencapsulated messages ...
4362 */
4363 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
4364 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304365 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004366
Vipul Pandya636f9d32012-09-26 02:39:39 +00004367 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304368 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
4369 * capability. Earlier versions of the firmware didn't have the
4370 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
4371 * permission to use ULPTX MEMWRITE DSGL.
4372 */
4373 if (is_t4(adap->params.chip)) {
4374 adap->params.ulptx_memwrite_dsgl = false;
4375 } else {
4376 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304377 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304378 1, params, val);
4379 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4380 }
4381
Steve Wise086de572016-09-16 07:54:49 -07004382 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
4383 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
4384 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4385 1, params, val);
4386 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
4387
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05304388 /* See if FW supports FW_FILTER2 work request */
4389 if (is_t4(adap->params.chip)) {
4390 adap->params.filter2_wr_support = 0;
4391 } else {
4392 params[0] = FW_PARAM_DEV(FILTER2_WR);
4393 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4394 1, params, val);
4395 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
4396 }
4397
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304398 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004399 * Get device capabilities so we can determine what resources we need
4400 * to manage.
4401 */
4402 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304403 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4404 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304405 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004406 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4407 &caps_cmd);
4408 if (ret < 0)
4409 goto bye;
4410
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304411 if (caps_cmd.ofldcaps ||
4412 (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER))) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004413 /* query offload-related parameters */
4414 params[0] = FW_PARAM_DEV(NTID);
4415 params[1] = FW_PARAM_PFVF(SERVER_START);
4416 params[2] = FW_PARAM_PFVF(SERVER_END);
4417 params[3] = FW_PARAM_PFVF(TDDP_START);
4418 params[4] = FW_PARAM_PFVF(TDDP_END);
4419 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304420 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004421 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004422 if (ret < 0)
4423 goto bye;
4424 adap->tids.ntids = val[0];
4425 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4426 adap->tids.stid_base = val[1];
4427 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004428 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004429 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004430 * region into two parts. Regular filters get 1/3rd and server
4431 * filters get 2/3rd part. This is only enabled if workarond
4432 * path is enabled.
4433 * 1. For regular filters.
4434 * 2. Server filter: This are special filters which are used
4435 * to redirect SYN packets to offload queue.
4436 */
4437 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4438 adap->tids.sftid_base = adap->tids.ftid_base +
4439 DIV_ROUND_UP(adap->tids.nftids, 3);
4440 adap->tids.nsftids = adap->tids.nftids -
4441 DIV_ROUND_UP(adap->tids.nftids, 3);
4442 adap->tids.nftids = adap->tids.sftid_base -
4443 adap->tids.ftid_base;
4444 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004445 adap->vres.ddp.start = val[3];
4446 adap->vres.ddp.size = val[4] - val[3] + 1;
4447 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004448
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304449 if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4450 if (init_hash_filter(adap) < 0)
4451 goto bye;
4452 } else {
4453 adap->params.offload = 1;
4454 adap->num_ofld_uld += 1;
4455 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004456 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004457 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004458 params[0] = FW_PARAM_PFVF(STAG_START);
4459 params[1] = FW_PARAM_PFVF(STAG_END);
4460 params[2] = FW_PARAM_PFVF(RQ_START);
4461 params[3] = FW_PARAM_PFVF(RQ_END);
4462 params[4] = FW_PARAM_PFVF(PBL_START);
4463 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304464 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004465 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004466 if (ret < 0)
4467 goto bye;
4468 adap->vres.stag.start = val[0];
4469 adap->vres.stag.size = val[1] - val[0] + 1;
4470 adap->vres.rq.start = val[2];
4471 adap->vres.rq.size = val[3] - val[2] + 1;
4472 adap->vres.pbl.start = val[4];
4473 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004474
Raju Rangojuc68644e2018-03-20 15:41:40 +05304475 params[0] = FW_PARAM_PFVF(SRQ_START);
4476 params[1] = FW_PARAM_PFVF(SRQ_END);
4477 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4478 params, val);
4479 if (!ret) {
4480 adap->vres.srq.start = val[0];
4481 adap->vres.srq.size = val[1] - val[0] + 1;
4482 }
4483 if (adap->vres.srq.size) {
4484 adap->srq = t4_init_srq(adap->vres.srq.size);
4485 if (!adap->srq)
4486 dev_warn(&adap->pdev->dev, "could not allocate SRQ, continuing\n");
4487 }
4488
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004489 params[0] = FW_PARAM_PFVF(SQRQ_START);
4490 params[1] = FW_PARAM_PFVF(SQRQ_END);
4491 params[2] = FW_PARAM_PFVF(CQ_START);
4492 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004493 params[4] = FW_PARAM_PFVF(OCQ_START);
4494 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304495 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304496 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004497 if (ret < 0)
4498 goto bye;
4499 adap->vres.qp.start = val[0];
4500 adap->vres.qp.size = val[1] - val[0] + 1;
4501 adap->vres.cq.start = val[2];
4502 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004503 adap->vres.ocq.start = val[4];
4504 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304505
4506 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4507 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304508 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304509 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304510 if (ret < 0) {
4511 adap->params.max_ordird_qp = 8;
4512 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4513 ret = 0;
4514 } else {
4515 adap->params.max_ordird_qp = val[0];
4516 adap->params.max_ird_adapter = val[1];
4517 }
4518 dev_info(adap->pdev_dev,
4519 "max_ordird_qp %d max_ird_adapter %d\n",
4520 adap->params.max_ordird_qp,
4521 adap->params.max_ird_adapter);
Raju Rangoju43db9292018-03-20 15:41:41 +05304522
4523 /* Enable write_with_immediate if FW supports it */
4524 params[0] = FW_PARAM_DEV(RDMA_WRITE_WITH_IMM);
4525 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params,
4526 val);
4527 adap->params.write_w_imm_support = (ret == 0 && val[0] != 0);
Raju Rangojuf3910c62018-03-20 15:41:42 +05304528
4529 /* Enable write_cmpl if FW supports it */
4530 params[0] = FW_PARAM_DEV(RI_WRITE_CMPL_WR);
4531 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, params,
4532 val);
4533 adap->params.write_cmpl_support = (ret == 0 && val[0] != 0);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304534 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004535 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004536 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004537 params[0] = FW_PARAM_PFVF(ISCSI_START);
4538 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304539 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004540 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004541 if (ret < 0)
4542 goto bye;
4543 adap->vres.iscsi.start = val[0];
4544 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304545 /* LIO target and cxgb4i initiaitor */
4546 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004547 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304548 if (caps_cmd.cryptocaps) {
4549 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304550 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4551 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4552 params, val);
4553 if (ret < 0) {
4554 if (ret != -EINVAL)
4555 goto bye;
4556 } else {
4557 adap->vres.ncrypto_fc = val[0];
4558 }
Atul Guptaa6ec5722017-11-16 16:56:39 +05304559 adap->params.crypto = ntohs(caps_cmd.cryptocaps);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304560 adap->num_uld += 1;
4561 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004562#undef FW_PARAM_PFVF
4563#undef FW_PARAM_DEV
4564
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304565 /* The MTU/MSS Table is initialized by now, so load their values. If
4566 * we're initializing the adapter, then we'll make any modifications
4567 * we want to the MTU/MSS Table and also initialize the congestion
4568 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004569 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004570 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304571 if (state != DEV_STATE_INIT) {
4572 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004573
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304574 /* The default MTU Table contains values 1492 and 1500.
4575 * However, for TCP, it's better to have two values which are
4576 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4577 * This allows us to have a TCP Data Payload which is a
4578 * multiple of 8 regardless of what combination of TCP Options
4579 * are in use (always a multiple of 4 bytes) which is
4580 * important for performance reasons. For instance, if no
4581 * options are in use, then we have a 20-byte IP header and a
4582 * 20-byte TCP header. In this case, a 1500-byte MSS would
4583 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4584 * which is not a multiple of 8. So using an MSS of 1488 in
4585 * this case results in a TCP Data Payload of 1448 bytes which
4586 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4587 * Stamps have been negotiated, then an MTU of 1500 bytes
4588 * results in a TCP Data Payload of 1448 bytes which, as
4589 * above, is a multiple of 8 bytes ...
4590 */
4591 for (i = 0; i < NMTUS; i++)
4592 if (adap->params.mtus[i] == 1492) {
4593 adap->params.mtus[i] = 1488;
4594 break;
4595 }
4596
4597 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4598 adap->params.b_wnd);
4599 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304600 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004601 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304602 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004603 return 0;
4604
4605 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004606 * Something bad happened. If a command timed out or failed with EIO
4607 * FW does not operate within its spec or something catastrophic
4608 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004609 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004610bye:
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05304611 adap_free_hma_mem(adap);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304612 kfree(adap->sge.egr_map);
4613 kfree(adap->sge.ingr_map);
4614 kfree(adap->sge.starving_fl);
4615 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304616#ifdef CONFIG_DEBUG_FS
4617 kfree(adap->sge.blocked_fl);
4618#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004619 if (ret != -ETIMEDOUT && ret != -EIO)
4620 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004621 return ret;
4622}
4623
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004624/* EEH callbacks */
4625
4626static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4627 pci_channel_state_t state)
4628{
4629 int i;
4630 struct adapter *adap = pci_get_drvdata(pdev);
4631
4632 if (!adap)
4633 goto out;
4634
4635 rtnl_lock();
4636 adap->flags &= ~FW_OK;
4637 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004638 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004639 for_each_port(adap, i) {
4640 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004641 if (dev) {
4642 netif_device_detach(dev);
4643 netif_carrier_off(dev);
4644 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004645 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004646 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304647 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004648 if (adap->flags & FULL_INIT_DONE)
4649 cxgb_down(adap);
4650 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004651 if ((adap->flags & DEV_ENABLED)) {
4652 pci_disable_device(pdev);
4653 adap->flags &= ~DEV_ENABLED;
4654 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004655out: return state == pci_channel_io_perm_failure ?
4656 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4657}
4658
4659static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4660{
4661 int i, ret;
4662 struct fw_caps_config_cmd c;
4663 struct adapter *adap = pci_get_drvdata(pdev);
4664
4665 if (!adap) {
4666 pci_restore_state(pdev);
4667 pci_save_state(pdev);
4668 return PCI_ERS_RESULT_RECOVERED;
4669 }
4670
Gavin Shan144be3d2014-01-23 12:27:34 +08004671 if (!(adap->flags & DEV_ENABLED)) {
4672 if (pci_enable_device(pdev)) {
4673 dev_err(&pdev->dev, "Cannot reenable PCI "
4674 "device after reset\n");
4675 return PCI_ERS_RESULT_DISCONNECT;
4676 }
4677 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004678 }
4679
4680 pci_set_master(pdev);
4681 pci_restore_state(pdev);
4682 pci_save_state(pdev);
4683 pci_cleanup_aer_uncorrect_error_status(pdev);
4684
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304685 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004686 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304687 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004688 return PCI_ERS_RESULT_DISCONNECT;
4689 adap->flags |= FW_OK;
4690 if (adap_init1(adap, &c))
4691 return PCI_ERS_RESULT_DISCONNECT;
4692
4693 for_each_port(adap, i) {
4694 struct port_info *p = adap2pinfo(adap, i);
4695
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304696 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004697 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004698 if (ret < 0)
4699 return PCI_ERS_RESULT_DISCONNECT;
4700 p->viid = ret;
4701 p->xact_addr_filt = -1;
4702 }
4703
4704 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4705 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004706 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004707 if (cxgb_up(adap))
4708 return PCI_ERS_RESULT_DISCONNECT;
4709 return PCI_ERS_RESULT_RECOVERED;
4710}
4711
4712static void eeh_resume(struct pci_dev *pdev)
4713{
4714 int i;
4715 struct adapter *adap = pci_get_drvdata(pdev);
4716
4717 if (!adap)
4718 return;
4719
4720 rtnl_lock();
4721 for_each_port(adap, i) {
4722 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004723 if (dev) {
4724 if (netif_running(dev)) {
4725 link_start(dev);
4726 cxgb_set_rxmode(dev);
4727 }
4728 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004729 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004730 }
4731 rtnl_unlock();
4732}
4733
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004734static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004735 .error_detected = eeh_err_detected,
4736 .slot_reset = eeh_slot_reset,
4737 .resume = eeh_resume,
4738};
4739
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304740/* Return true if the Link Configuration supports "High Speeds" (those greater
4741 * than 1Gb/s).
4742 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304743static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004744{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304745 unsigned int speeds, high_speeds;
4746
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304747 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4748 high_speeds = speeds &
4749 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304750
4751 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004752}
4753
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004754/*
4755 * Perform default configuration of DMA queues depending on the number and type
4756 * of ports we found and the number of available CPUs. Most settings can be
4757 * modified by the admin prior to actual use.
4758 */
Bill Pemberton91744942012-12-03 09:23:02 -05004759static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004760{
4761 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304762 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004763#ifndef CONFIG_CHELSIO_T4_DCB
4764 int q10g = 0;
4765#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004766
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304767 /* Reduce memory usage in kdump environment, disable all offload.
4768 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304769 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304770 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304771 adap->params.crypto = 0;
4772 }
4773
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304774 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004775#ifdef CONFIG_CHELSIO_T4_DCB
4776 /* For Data Center Bridging support we need to be able to support up
4777 * to 8 Traffic Priorities; each of which will be assigned to its
4778 * own TX Queue in order to prevent Head-Of-Line Blocking.
4779 */
4780 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4781 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4782 MAX_ETH_QSETS, adap->params.nports * 8);
4783 BUG_ON(1);
4784 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004785
Anish Bhatt688848b2014-06-19 21:37:13 -07004786 for_each_port(adap, i) {
4787 struct port_info *pi = adap2pinfo(adap, i);
4788
4789 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304790 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004791 qidx += pi->nqsets;
4792 }
4793#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004794 /*
4795 * We default to 1 queue per non-10G port and up to # of cores queues
4796 * per 10G port.
4797 */
4798 if (n10g)
4799 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004800 if (q10g > netif_get_num_default_rss_queues())
4801 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004802
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304803 if (is_kdump_kernel())
4804 q10g = 1;
4805
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004806 for_each_port(adap, i) {
4807 struct port_info *pi = adap2pinfo(adap, i);
4808
4809 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304810 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004811 qidx += pi->nqsets;
4812 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004813#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004814
4815 s->ethqsets = qidx;
4816 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4817
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304818 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004819 /*
4820 * For offload we use 1 queue/channel if all ports are up to 1G,
4821 * otherwise we divide all available queues amongst the channels
4822 * capped by the number of available cores.
4823 */
4824 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304825 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304826 s->ofldqsets = roundup(i, adap->params.nports);
4827 } else {
4828 s->ofldqsets = adap->params.nports;
4829 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004830 }
4831
4832 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4833 struct sge_eth_rxq *r = &s->ethrxq[i];
4834
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304835 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004836 r->fl.size = 72;
4837 }
4838
4839 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4840 s->ethtxq[i].q.size = 1024;
4841
4842 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4843 s->ctrlq[i].q.size = 512;
4844
Atul Guptaa45695042017-07-04 16:46:20 +05304845 if (!is_t4(adap->params.chip))
4846 s->ptptxq.q.size = 8;
4847
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304848 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304849 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004850}
4851
4852/*
4853 * Reduce the number of Ethernet queues across all ports to at most n.
4854 * n provides at least one queue per port.
4855 */
Bill Pemberton91744942012-12-03 09:23:02 -05004856static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004857{
4858 int i;
4859 struct port_info *pi;
4860
4861 while (n < adap->sge.ethqsets)
4862 for_each_port(adap, i) {
4863 pi = adap2pinfo(adap, i);
4864 if (pi->nqsets > 1) {
4865 pi->nqsets--;
4866 adap->sge.ethqsets--;
4867 if (adap->sge.ethqsets <= n)
4868 break;
4869 }
4870 }
4871
4872 n = 0;
4873 for_each_port(adap, i) {
4874 pi = adap2pinfo(adap, i);
4875 pi->first_qset = n;
4876 n += pi->nqsets;
4877 }
4878}
4879
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304880static int get_msix_info(struct adapter *adap)
4881{
4882 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304883 unsigned int max_ingq = 0;
4884
4885 if (is_offload(adap))
4886 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4887 if (is_pci_uld(adap))
4888 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4889
4890 if (!max_ingq)
4891 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304892
4893 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4894 if (!msix_info)
4895 return -ENOMEM;
4896
4897 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4898 sizeof(long), GFP_KERNEL);
4899 if (!adap->msix_bmap_ulds.msix_bmap) {
4900 kfree(msix_info);
4901 return -ENOMEM;
4902 }
4903 spin_lock_init(&adap->msix_bmap_ulds.lock);
4904 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304905out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304906 return 0;
4907}
4908
4909static void free_msix_info(struct adapter *adap)
4910{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304911 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304912 return;
4913
4914 kfree(adap->msix_info_ulds);
4915 kfree(adap->msix_bmap_ulds.msix_bmap);
4916}
4917
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004918/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4919#define EXTRA_VECS 2
4920
Bill Pemberton91744942012-12-03 09:23:02 -05004921static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004922{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304923 int ofld_need = 0, uld_need = 0;
4924 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004925 struct sge *s = &adap->sge;
4926 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304927 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304928 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004929
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304930 if (is_pci_uld(adap))
4931 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4932 if (is_offload(adap))
4933 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304934 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304935 GFP_KERNEL);
4936 if (!entries)
4937 return -ENOMEM;
4938
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304939 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304940 if (get_msix_info(adap)) {
4941 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304942 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304943 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304944
4945 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004946 entries[i].entry = i;
4947
4948 want = s->max_ethqsets + EXTRA_VECS;
4949 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304950 want += adap->num_ofld_uld * s->ofldqsets;
4951 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004952 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304953 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304954 want += adap->num_uld * s->ofldqsets;
4955 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304956 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004957#ifdef CONFIG_CHELSIO_T4_DCB
4958 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4959 * each port.
4960 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304961 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004962#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304963 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004964#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304965 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4966 if (allocated < 0) {
4967 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4968 " not using MSI-X\n");
4969 kfree(entries);
4970 return allocated;
4971 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004972
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304973 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004974 * Every group gets its minimum requirement and NIC gets top
4975 * priority for leftovers.
4976 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304977 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004978 if (i < s->max_ethqsets) {
4979 s->max_ethqsets = i;
4980 if (i < s->ethqsets)
4981 reduce_ethqs(adap, i);
4982 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304983 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304984 if (allocated < want)
4985 s->nqs_per_uld = nchan;
4986 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304987 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304988 }
4989
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304990 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004991 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304992 if (is_uld(adap)) {
4993 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304994 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304995 adap->msix_info_ulds[j].idx = i;
4996 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304997 adap->msix_bmap_ulds.mapsize = j;
4998 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304999 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305000 "nic %d per uld %d\n",
5001 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01005002
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05305003 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01005004 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005005}
5006
5007#undef EXTRA_VECS
5008
Bill Pemberton91744942012-12-03 09:23:02 -05005009static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005010{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05305011 unsigned int i;
5012 int err;
5013
5014 err = t4_init_rss_mode(adap, adap->mbox);
5015 if (err)
5016 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005017
5018 for_each_port(adap, i) {
5019 struct port_info *pi = adap2pinfo(adap, i);
5020
5021 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
5022 if (!pi->rss)
5023 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005024 }
5025 return 0;
5026}
5027
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305028static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
5029 enum pci_bus_speed *speed,
5030 enum pcie_link_width *width)
5031{
5032 u32 lnkcap1, lnkcap2;
5033 int err1, err2;
5034
5035#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
5036
5037 *speed = PCI_SPEED_UNKNOWN;
5038 *width = PCIE_LNK_WIDTH_UNKNOWN;
5039
5040 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
5041 &lnkcap1);
5042 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
5043 &lnkcap2);
5044 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
5045 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
5046 *speed = PCIE_SPEED_8_0GT;
5047 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
5048 *speed = PCIE_SPEED_5_0GT;
5049 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
5050 *speed = PCIE_SPEED_2_5GT;
5051 }
5052 if (!err1) {
5053 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
5054 if (!lnkcap2) { /* pre-r3.0 */
5055 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
5056 *speed = PCIE_SPEED_5_0GT;
5057 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
5058 *speed = PCIE_SPEED_2_5GT;
5059 }
5060 }
5061
5062 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
5063 return err1 ? err1 : err2 ? err2 : -EINVAL;
5064 return 0;
5065}
5066
5067static void cxgb4_check_pcie_caps(struct adapter *adap)
5068{
5069 enum pcie_link_width width, width_cap;
5070 enum pci_bus_speed speed, speed_cap;
5071
5072#define PCIE_SPEED_STR(speed) \
5073 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
5074 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
5075 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
5076 "Unknown")
5077
5078 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
5079 dev_warn(adap->pdev_dev,
5080 "Unable to determine PCIe device BW capabilities\n");
5081 return;
5082 }
5083
5084 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
5085 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
5086 dev_warn(adap->pdev_dev,
5087 "Unable to determine PCI Express bandwidth.\n");
5088 return;
5089 }
5090
5091 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
5092 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
5093 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
5094 width, width_cap);
5095 if (speed < speed_cap || width < width_cap)
5096 dev_info(adap->pdev_dev,
5097 "A slot with more lanes and/or higher speed is "
5098 "suggested for optimal performance.\n");
5099}
5100
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305101/* Dump basic information about the adapter */
5102static void print_adapter_info(struct adapter *adapter)
5103{
Ganesh Goudar760446f2017-07-20 18:28:48 +05305104 /* Hardware/Firmware/etc. Version/Revision IDs */
5105 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305106
5107 /* Software/Hardware configuration */
5108 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
5109 is_offload(adapter) ? "R" : "",
5110 ((adapter->flags & USING_MSIX) ? "MSI-X" :
5111 (adapter->flags & USING_MSI) ? "MSI" : ""),
5112 is_offload(adapter) ? "Offload" : "non-Offload");
5113}
5114
Bill Pemberton91744942012-12-03 09:23:02 -05005115static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005116{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005117 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005118 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00005119 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005120 const struct port_info *pi = netdev_priv(dev);
5121 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00005122
5123 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
5124 spd = " 2.5 GT/s";
5125 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
5126 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07005127 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
5128 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005129
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305130 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05305131 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305132 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05305133 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305134 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005135 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305136 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05305137 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305138 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05305139 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305140 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
5141 bufp += sprintf(bufp, "50G/");
5142 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05305143 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305144 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
5145 bufp += sprintf(bufp, "200G/");
5146 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
5147 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005148 if (bufp != buf)
5149 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05305150 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005151
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305152 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
5153 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005154}
5155
Dimitris Michailidis06546392010-07-11 12:01:16 +00005156/*
5157 * Free the following resources:
5158 * - memory used for tables
5159 * - MSI/MSI-X
5160 * - net devices
5161 * - resources FW is holding for us
5162 */
5163static void free_some_resources(struct adapter *adapter)
5164{
5165 unsigned int i;
5166
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305167 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07005168 kvfree(adapter->l2t);
Raju Rangojuc68644e2018-03-20 15:41:40 +05305169 kvfree(adapter->srq);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305170 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07005171 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05305172 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305173 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05305174 kfree(adapter->sge.egr_map);
5175 kfree(adapter->sge.ingr_map);
5176 kfree(adapter->sge.starving_fl);
5177 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305178#ifdef CONFIG_DEBUG_FS
5179 kfree(adapter->sge.blocked_fl);
5180#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00005181 disable_msi(adapter);
5182
5183 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005184 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05305185 struct port_info *pi = adap2pinfo(adapter, i);
5186
5187 if (pi->viid != 0)
5188 t4_free_vi(adapter, adapter->mbox, adapter->pf,
5189 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005190 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005191 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005192 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00005193 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305194 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005195}
5196
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005197#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00005198#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005199 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005200#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005201
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305202static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
5203{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305204 u16 device_id;
5205
5206 /* Retrieve adapter's device ID */
5207 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02005208
5209 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305210 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02005211 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305212 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02005213 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305214 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02005215 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305216 default:
5217 dev_err(&pdev->dev, "Device %d is not supported\n",
5218 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305219 }
françois romieu46cdc9b2015-09-04 23:05:42 +02005220 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305221}
5222
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305223#ifdef CONFIG_PCI_IOV
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305224static void cxgb4_mgmt_setup(struct net_device *dev)
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305225{
5226 dev->type = ARPHRD_NONE;
5227 dev->mtu = 0;
5228 dev->hard_header_len = 0;
5229 dev->addr_len = 0;
5230 dev->tx_queue_len = 0;
5231 dev->flags |= IFF_NOARP;
5232 dev->priv_flags |= IFF_NO_QUEUE;
5233
5234 /* Initialize the device structure. */
5235 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
5236 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04005237 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305238}
5239
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305240static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
5241{
Hariprasad Shenai78294512016-08-11 21:06:23 +05305242 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305243 int err = 0;
5244 int current_vfs = pci_num_vf(pdev);
5245 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305246
Hariprasad Shenai78294512016-08-11 21:06:23 +05305247 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305248 /* Check if cxgb4 is the MASTER and fw is initialized */
Ganesh Goudarc4e43e12018-02-15 18:16:57 +05305249 if (num_vfs &&
5250 (!(pcie_fw & PCIE_FW_INIT_F) ||
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305251 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
Ganesh Goudarc4e43e12018-02-15 18:16:57 +05305252 PCIE_FW_MASTER_G(pcie_fw) != CXGB4_UNIFIED_PF)) {
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305253 dev_warn(&pdev->dev,
5254 "cxgb4 driver needs to be MASTER to support SRIOV\n");
5255 return -EOPNOTSUPP;
5256 }
5257
5258 /* If any of the VF's is already assigned to Guest OS, then
5259 * SRIOV for the same cannot be modified
5260 */
5261 if (current_vfs && pci_vfs_assigned(pdev)) {
5262 dev_err(&pdev->dev,
5263 "Cannot modify SR-IOV while VFs are assigned\n");
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305264 return current_vfs;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305265 }
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305266 /* Note that the upper-level code ensures that we're never called with
5267 * a non-zero "num_vfs" when we already have VFs instantiated. But
5268 * it never hurts to code defensively.
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305269 */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305270 if (num_vfs != 0 && current_vfs != 0)
5271 return -EBUSY;
5272
5273 /* Nothing to do for no change. */
5274 if (num_vfs == current_vfs)
5275 return num_vfs;
5276
5277 /* Disable SRIOV when zero is passed. */
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305278 if (!num_vfs) {
5279 pci_disable_sriov(pdev);
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305280 /* free VF Management Interface */
5281 unregister_netdev(adap->port[0]);
5282 free_netdev(adap->port[0]);
5283 adap->port[0] = NULL;
5284
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305285 /* free VF resources */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305286 adap->num_vfs = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305287 kfree(adap->vfinfo);
5288 adap->vfinfo = NULL;
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305289 return 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305290 }
5291
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305292 if (!current_vfs) {
5293 struct fw_pfvf_cmd port_cmd, port_rpl;
5294 struct net_device *netdev;
5295 unsigned int pmask, port;
5296 struct pci_dev *pbridge;
5297 struct port_info *pi;
5298 char name[IFNAMSIZ];
5299 u32 devcap2;
5300 u16 flags;
5301 int pos;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305302
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305303 /* If we want to instantiate Virtual Functions, then our
5304 * parent bridge's PCI-E needs to support Alternative Routing
5305 * ID (ARI) because our VFs will show up at function offset 8
5306 * and above.
5307 */
5308 pbridge = pdev->bus->self;
5309 pos = pci_find_capability(pbridge, PCI_CAP_ID_EXP);
5310 pci_read_config_word(pbridge, pos + PCI_EXP_FLAGS, &flags);
5311 pci_read_config_dword(pbridge, pos + PCI_EXP_DEVCAP2, &devcap2);
5312
5313 if ((flags & PCI_EXP_FLAGS_VERS) < 2 ||
5314 !(devcap2 & PCI_EXP_DEVCAP2_ARI)) {
5315 /* Our parent bridge does not support ARI so issue a
5316 * warning and skip instantiating the VFs. They
5317 * won't be reachable.
5318 */
5319 dev_warn(&pdev->dev, "Parent bridge %02x:%02x.%x doesn't support ARI; can't instantiate Virtual Functions\n",
5320 pbridge->bus->number, PCI_SLOT(pbridge->devfn),
5321 PCI_FUNC(pbridge->devfn));
5322 return -ENOTSUPP;
5323 }
5324 memset(&port_cmd, 0, sizeof(port_cmd));
5325 port_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
5326 FW_CMD_REQUEST_F |
5327 FW_CMD_READ_F |
5328 FW_PFVF_CMD_PFN_V(adap->pf) |
5329 FW_PFVF_CMD_VFN_V(0));
5330 port_cmd.retval_len16 = cpu_to_be32(FW_LEN16(port_cmd));
5331 err = t4_wr_mbox(adap, adap->mbox, &port_cmd, sizeof(port_cmd),
5332 &port_rpl);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305333 if (err)
5334 return err;
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305335 pmask = FW_PFVF_CMD_PMASK_G(be32_to_cpu(port_rpl.type_to_neq));
5336 port = ffs(pmask) - 1;
5337 /* Allocate VF Management Interface. */
5338 snprintf(name, IFNAMSIZ, "mgmtpf%d,%d", adap->adap_idx,
5339 adap->pf);
5340 netdev = alloc_netdev(sizeof(struct port_info),
5341 name, NET_NAME_UNKNOWN, cxgb4_mgmt_setup);
5342 if (!netdev)
5343 return -ENOMEM;
5344
5345 pi = netdev_priv(netdev);
5346 pi->adapter = adap;
5347 pi->lport = port;
5348 pi->tx_chan = port;
5349 SET_NETDEV_DEV(netdev, &pdev->dev);
5350
5351 adap->port[0] = netdev;
5352 pi->port_id = 0;
5353
5354 err = register_netdev(adap->port[0]);
5355 if (err) {
5356 pr_info("Unable to register VF mgmt netdev %s\n", name);
5357 free_netdev(adap->port[0]);
5358 adap->port[0] = NULL;
5359 return err;
5360 }
5361 /* Allocate and set up VF Information. */
5362 adap->vfinfo = kcalloc(pci_sriov_get_totalvfs(pdev),
5363 sizeof(struct vf_info), GFP_KERNEL);
5364 if (!adap->vfinfo) {
5365 unregister_netdev(adap->port[0]);
5366 free_netdev(adap->port[0]);
5367 adap->port[0] = NULL;
5368 return -ENOMEM;
5369 }
5370 cxgb4_mgmt_fill_vf_station_mac_addr(adap);
5371 }
5372 /* Instantiate the requested number of VFs. */
5373 err = pci_enable_sriov(pdev, num_vfs);
5374 if (err) {
5375 pr_info("Unable to instantiate %d VFs\n", num_vfs);
5376 if (!current_vfs) {
5377 unregister_netdev(adap->port[0]);
5378 free_netdev(adap->port[0]);
5379 adap->port[0] = NULL;
5380 kfree(adap->vfinfo);
5381 adap->vfinfo = NULL;
5382 }
5383 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305384 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305385
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305386 adap->num_vfs = num_vfs;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305387 return num_vfs;
5388}
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305389#endif /* CONFIG_PCI_IOV */
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305390
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00005391static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005392{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005393 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005394 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005395 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005396 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305397 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305398 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305399 u32 whoami, pl_rev;
5400 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305401 static int adap_idx = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005402
5403 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
5404
5405 err = pci_request_regions(pdev, KBUILD_MODNAME);
5406 if (err) {
5407 /* Just info, some other driver may have claimed the device. */
5408 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
5409 return err;
5410 }
5411
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005412 err = pci_enable_device(pdev);
5413 if (err) {
5414 dev_err(&pdev->dev, "cannot enable PCI device\n");
5415 goto out_release_regions;
5416 }
5417
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305418 regs = pci_ioremap_bar(pdev, 0);
5419 if (!regs) {
5420 dev_err(&pdev->dev, "cannot map device registers\n");
5421 err = -ENOMEM;
5422 goto out_disable_device;
5423 }
5424
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305425 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5426 if (!adapter) {
5427 err = -ENOMEM;
5428 goto out_unmap_bar0;
5429 }
5430
5431 adapter->regs = regs;
Hariprasad Shenai8203b502014-10-09 05:48:47 +05305432 err = t4_wait_dev_ready(regs);
5433 if (err < 0)
Christophe JAILLETe7294522018-02-06 21:17:17 +01005434 goto out_free_adapter;
Hariprasad Shenai8203b502014-10-09 05:48:47 +05305435
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305436 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305437 whoami = readl(regs + PL_WHOAMI_A);
5438 pl_rev = REV_G(readl(regs + PL_REV_A));
5439 chip = get_chip_type(pdev, pl_rev);
5440 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
5441 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305442
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305443 adapter->pdev = pdev;
5444 adapter->pdev_dev = &pdev->dev;
5445 adapter->name = pci_name(pdev);
5446 adapter->mbox = func;
5447 adapter->pf = func;
5448 adapter->msg_enable = DFLT_MSG_ENABLE;
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305449 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5450 (sizeof(struct mbox_cmd) *
5451 T4_OS_LOG_MBOX_CMDS),
5452 GFP_KERNEL);
5453 if (!adapter->mbox_log) {
5454 err = -ENOMEM;
5455 goto out_free_adapter;
5456 }
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305457 spin_lock_init(&adapter->mbox_lock);
5458 INIT_LIST_HEAD(&adapter->mlist.list);
5459 pci_set_drvdata(pdev, adapter);
5460
5461 if (func != ent->driver_data) {
5462 pci_disable_device(pdev);
5463 pci_save_state(pdev); /* to restore SR-IOV later */
5464 return 0;
5465 }
5466
5467 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
5468 highdma = true;
5469 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
5470 if (err) {
5471 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
5472 "coherent allocations\n");
5473 goto out_free_adapter;
5474 }
5475 } else {
5476 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5477 if (err) {
5478 dev_err(&pdev->dev, "no usable DMA configuration\n");
5479 goto out_free_adapter;
5480 }
5481 }
5482
5483 pci_enable_pcie_error_reporting(pdev);
5484 pci_set_master(pdev);
5485 pci_save_state(pdev);
5486 adap_idx++;
5487 adapter->workq = create_singlethread_workqueue("cxgb4");
5488 if (!adapter->workq) {
5489 err = -ENOMEM;
5490 goto out_free_adapter;
5491 }
5492
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305493 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
5494
Gavin Shan144be3d2014-01-23 12:27:34 +08005495 /* PCI device has been enabled */
5496 adapter->flags |= DEV_ENABLED;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005497 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
5498
Casey Leedomb0ba9d52017-08-15 11:23:26 +08005499 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
5500 * Ingress Packet Data to Free List Buffers in order to allow for
5501 * chipset performance optimizations between the Root Complex and
5502 * Memory Controllers. (Messages to the associated Ingress Queue
5503 * notifying new Packet Placement in the Free Lists Buffers will be
5504 * send without the Relaxed Ordering Attribute thus guaranteeing that
5505 * all preceding PCIe Transaction Layer Packets will be processed
5506 * first.) But some Root Complexes have various issues with Upstream
5507 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
5508 * The PCIe devices which under the Root Complexes will be cleared the
5509 * Relaxed Ordering bit in the configuration space, So we check our
5510 * PCIe configuration space to see if it's flagged with advice against
5511 * using Relaxed Ordering.
5512 */
5513 if (!pcie_relaxed_ordering_enabled(pdev))
5514 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
5515
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005516 spin_lock_init(&adapter->stats_lock);
5517 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07005518 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005519
5520 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305521 INIT_WORK(&adapter->db_full_task, process_db_full);
5522 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Ganesh Goudar8b7372c2018-03-16 14:22:57 +05305523 INIT_WORK(&adapter->fatal_err_notify_task, notify_fatal_err);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005524
5525 err = t4_prep_adapter(adapter);
5526 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305527 goto out_free_adapter;
5528
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005529
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305530 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305531 s_qpp = (QUEUESPERPAGEPF0_S +
5532 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305533 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305534 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5535 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005536 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5537
5538 /* Each segment size is 128B. Write coalescing is enabled only
5539 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5540 * queue is less no of segments that can be accommodated in
5541 * a page size.
5542 */
5543 if (qpp > num_seg) {
5544 dev_err(&pdev->dev,
5545 "Incorrect number of egress queues per page\n");
5546 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305547 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005548 }
5549 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5550 pci_resource_len(pdev, 2));
5551 if (!adapter->bar2) {
5552 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5553 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305554 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005555 }
5556 }
5557
Vipul Pandya636f9d32012-09-26 02:39:39 +00005558 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005559 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305560#ifdef CONFIG_DEBUG_FS
5561 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5562#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005563 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005564 if (err)
5565 goto out_unmap_bar;
5566
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305567 /* configure SGE_STAT_CFG_A to read WC stats */
5568 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305569 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5570 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5571 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305572
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005573 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005574 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5575 MAX_ETH_QSETS);
5576 if (!netdev) {
5577 err = -ENOMEM;
5578 goto out_free_dev;
5579 }
5580
5581 SET_NETDEV_DEV(netdev, &pdev->dev);
5582
5583 adapter->port[i] = netdev;
5584 pi = netdev_priv(netdev);
5585 pi->adapter = adapter;
5586 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005587 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005588 netdev->irq = pdev->irq;
5589
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005590 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5591 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5592 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305593 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5594 NETIF_F_HW_TC;
Ganesh Goudard0a12992018-01-10 18:15:26 +05305595
5596 if (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5)
5597 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
5598
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005599 if (highdma)
5600 netdev->hw_features |= NETIF_F_HIGHDMA;
5601 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005602 netdev->vlan_features = netdev->features & VLAN_FEAT;
5603
Jiri Pirko01789342011-08-16 06:29:00 +00005604 netdev->priv_flags |= IFF_UNICAST_FLT;
5605
Jarod Wilsond894be52016-10-20 13:55:16 -04005606 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305607 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005608 netdev->max_mtu = MAX_MTU;
5609
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005610 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005611#ifdef CONFIG_CHELSIO_T4_DCB
5612 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5613 cxgb4_dcb_state_init(netdev);
5614#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305615 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005616 }
5617
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305618 cxgb4_init_ethtool_dump(adapter);
5619
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005620 pci_set_drvdata(pdev, adapter);
5621
5622 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005623 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005624 if (err)
5625 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305626 } else if (adapter->params.nports == 1) {
5627 /* If we don't have a connection to the firmware -- possibly
5628 * because of an error -- grab the raw VPD parameters so we
5629 * can set the proper MAC Address on the debug network
5630 * interface that we've created.
5631 */
5632 u8 hw_addr[ETH_ALEN];
5633 u8 *na = adapter->params.vpd.na;
5634
5635 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5636 if (!err) {
5637 for (i = 0; i < ETH_ALEN; i++)
5638 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5639 hex2val(na[2 * i + 1]));
5640 t4_set_hw_addr(adapter, 0, hw_addr);
5641 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005642 }
5643
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305644 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005645 * soon as the first register_netdev completes.
5646 */
5647 cfg_queues(adapter);
5648
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305649 adapter->smt = t4_init_smt();
5650 if (!adapter->smt) {
5651 /* We tolerate a lack of SMT, giving up some functionality */
5652 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5653 }
5654
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305655 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005656 if (!adapter->l2t) {
5657 /* We tolerate a lack of L2T, giving up some functionality */
5658 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5659 adapter->params.offload = 0;
5660 }
5661
Anish Bhattb5a02f52015-01-14 15:17:34 -08005662#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305663 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5664 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5665 /* CLIP functionality is not present in hardware,
5666 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005667 */
5668 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305669 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005670 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305671 } else {
5672 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5673 adapter->clipt_end);
5674 if (!adapter->clipt) {
5675 /* We tolerate a lack of clip_table, giving up
5676 * some functionality
5677 */
5678 dev_warn(&pdev->dev,
5679 "could not allocate Clip table, continuing\n");
5680 adapter->params.offload = 0;
5681 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005682 }
5683#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305684
5685 for_each_port(adapter, i) {
5686 pi = adap2pinfo(adapter, i);
5687 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5688 if (!pi->sched_tbl)
5689 dev_warn(&pdev->dev,
5690 "could not activate scheduling on port %d\n",
5691 i);
5692 }
5693
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305694 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005695 dev_warn(&pdev->dev, "could not allocate TID table, "
5696 "continuing\n");
5697 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305698 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305699 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305700 if (!adapter->tc_u32)
5701 dev_warn(&pdev->dev,
5702 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305703
Kumar Sanghvi79e6d462017-11-01 08:53:04 +05305704 if (cxgb4_init_tc_flower(adapter))
5705 dev_warn(&pdev->dev,
5706 "could not offload tc flower, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005707 }
5708
Kumar Sanghvi5c312542017-11-01 08:53:00 +05305709 if (is_offload(adapter) || is_hashfilter(adapter)) {
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305710 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5711 u32 hash_base, hash_reg;
5712
5713 if (chip <= CHELSIO_T5) {
5714 hash_reg = LE_DB_TID_HASHBASE_A;
5715 hash_base = t4_read_reg(adapter, hash_reg);
5716 adapter->tids.hash_base = hash_base / 4;
5717 } else {
5718 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5719 hash_base = t4_read_reg(adapter, hash_reg);
5720 adapter->tids.hash_base = hash_base;
5721 }
5722 }
5723 }
5724
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005725 /* See what interrupts we'll be using */
5726 if (msi > 1 && enable_msix(adapter) == 0)
5727 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305728 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005729 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305730 if (msi > 1)
5731 free_msix_info(adapter);
5732 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005733
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305734 /* check for PCI Express bandwidth capabiltites */
5735 cxgb4_check_pcie_caps(adapter);
5736
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005737 err = init_rss(adapter);
5738 if (err)
5739 goto out_free_dev;
5740
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005741 /*
5742 * The card is now ready to go. If any errors occur during device
5743 * registration we do not fail the whole card but rather proceed only
5744 * with the ports we manage to register successfully. However we must
5745 * register at least one net device.
5746 */
5747 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005748 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305749 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005750 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5751 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5752
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305753 netif_carrier_off(adapter->port[i]);
5754
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005755 err = register_netdev(adapter->port[i]);
5756 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005757 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005758 adapter->chan_map[pi->tx_chan] = i;
5759 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005760 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005761 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005762 dev_err(&pdev->dev, "could not register any net devices\n");
5763 goto out_free_dev;
5764 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005765 if (err) {
5766 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5767 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005768 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005769
5770 if (cxgb4_debugfs_root) {
5771 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5772 cxgb4_debugfs_root);
5773 setup_debugfs(adapter);
5774 }
5775
David S. Miller88c51002011-10-07 13:38:43 -04005776 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5777 pdev->needs_freset = 1;
5778
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305779 if (is_uld(adapter)) {
5780 mutex_lock(&uld_mutex);
5781 list_add_tail(&adapter->list_node, &adapter_list);
5782 mutex_unlock(&uld_mutex);
5783 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005784
Atul Gupta9c33e422017-07-04 16:46:21 +05305785 if (!is_t4(adapter->params.chip))
5786 cxgb4_ptp_init(adapter);
5787
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305788 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305789 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305790 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305791
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005792 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005793 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305794 if (adapter->flags & USING_MSIX)
5795 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305796 if (adapter->num_uld || adapter->num_ofld_uld)
5797 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005798 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305799 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005800 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005801 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005802 if (adapter->workq)
5803 destroy_workqueue(adapter->workq);
5804
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305805 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005806 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305807 out_unmap_bar0:
5808 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005809 out_disable_device:
5810 pci_disable_pcie_error_reporting(pdev);
5811 pci_disable_device(pdev);
5812 out_release_regions:
5813 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005814 return err;
5815}
5816
Bill Pemberton91744942012-12-03 09:23:02 -05005817static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005818{
5819 struct adapter *adapter = pci_get_drvdata(pdev);
5820
Hariprasad Shenai78294512016-08-11 21:06:23 +05305821 if (!adapter) {
5822 pci_release_regions(pdev);
5823 return;
5824 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005825
Ganesh Goudare1f61982017-09-21 12:50:47 +05305826 adapter->flags |= SHUTTING_DOWN;
5827
Hariprasad Shenai78294512016-08-11 21:06:23 +05305828 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005829 int i;
5830
Anish Bhatt29aaee62014-08-20 13:44:06 -07005831 /* Tear down per-adapter Work Queue first since it can contain
5832 * references to our adapter data structure.
5833 */
5834 destroy_workqueue(adapter->workq);
5835
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005836 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005837 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005838 t4_uld_clean_up(adapter);
5839 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005840
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05305841 adap_free_hma_mem(adapter);
5842
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305843 disable_interrupts(adapter);
5844
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005845 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005846 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005847 unregister_netdev(adapter->port[i]);
5848
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005849 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005850
Atul Gupta9c33e422017-07-04 16:46:21 +05305851 if (!is_t4(adapter->params.chip))
5852 cxgb4_ptp_stop(adapter);
5853
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005854 /* If we allocated filters, free up state associated with any
5855 * valid filters ...
5856 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305857 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005858
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005859 if (adapter->flags & FULL_INIT_DONE)
5860 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005861
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305862 if (adapter->flags & USING_MSIX)
5863 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305864 if (adapter->num_uld || adapter->num_ofld_uld)
5865 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005866 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005867#if IS_ENABLED(CONFIG_IPV6)
5868 t4_cleanup_clip_tbl(adapter);
5869#endif
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305870 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005871 iounmap(adapter->bar2);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305872 }
5873#ifdef CONFIG_PCI_IOV
5874 else {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305875 cxgb4_iov_configure(adapter->pdev, 0);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305876 }
5877#endif
Ganesh Goudarc4e43e12018-02-15 18:16:57 +05305878 iounmap(adapter->regs);
5879 pci_disable_pcie_error_reporting(pdev);
5880 if ((adapter->flags & DEV_ENABLED)) {
5881 pci_disable_device(pdev);
5882 adapter->flags &= ~DEV_ENABLED;
5883 }
5884 pci_release_regions(pdev);
5885 kfree(adapter->mbox_log);
5886 synchronize_rcu();
5887 kfree(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005888}
5889
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305890/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5891 * delivery. This is essentially a stripped down version of the PCI remove()
5892 * function where we do the minimal amount of work necessary to shutdown any
5893 * further activity.
5894 */
5895static void shutdown_one(struct pci_dev *pdev)
5896{
5897 struct adapter *adapter = pci_get_drvdata(pdev);
5898
5899 /* As with remove_one() above (see extended comment), we only want do
5900 * do cleanup on PCI Devices which went all the way through init_one()
5901 * ...
5902 */
5903 if (!adapter) {
5904 pci_release_regions(pdev);
5905 return;
5906 }
5907
Ganesh Goudare1f61982017-09-21 12:50:47 +05305908 adapter->flags |= SHUTTING_DOWN;
5909
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305910 if (adapter->pf == 4) {
5911 int i;
5912
5913 for_each_port(adapter, i)
5914 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5915 cxgb_close(adapter->port[i]);
5916
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005917 if (is_uld(adapter)) {
5918 detach_ulds(adapter);
5919 t4_uld_clean_up(adapter);
5920 }
5921
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305922 disable_interrupts(adapter);
5923 disable_msi(adapter);
5924
5925 t4_sge_stop(adapter);
5926 if (adapter->flags & FW_OK)
5927 t4_fw_bye(adapter, adapter->mbox);
5928 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305929}
5930
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005931static struct pci_driver cxgb4_driver = {
5932 .name = KBUILD_MODNAME,
5933 .id_table = cxgb4_pci_tbl,
5934 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005935 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305936 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305937#ifdef CONFIG_PCI_IOV
5938 .sriov_configure = cxgb4_iov_configure,
5939#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005940 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005941};
5942
5943static int __init cxgb4_init_module(void)
5944{
5945 int ret;
5946
5947 /* Debugfs support is optional, just warn if this fails */
5948 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5949 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005950 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005951
5952 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005953 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005954 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305955
Anish Bhatt1bb60372014-10-14 20:07:22 -07005956#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005957 if (!inet6addr_registered) {
5958 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5959 inet6addr_registered = true;
5960 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005961#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305962
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005963 return ret;
5964}
5965
5966static void __exit cxgb4_cleanup_module(void)
5967{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005968#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305969 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005970 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5971 inet6addr_registered = false;
5972 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005973#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005974 pci_unregister_driver(&cxgb4_driver);
5975 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005976}
5977
5978module_init(cxgb4_init_module);
5979module_exit(cxgb4_cleanup_module);