blob: 5a349e1576cbfa42a8f023ea3f53aca3d7437d98 [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"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053078#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080079#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000080#include "l2t.h"
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +053081#include "smt.h"
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +053082#include "sched.h"
Rahul Lakkireddyd8931842016-09-20 17:13:09 +053083#include "cxgb4_tc_u32.h"
Kumar Sanghvi6a345b32017-09-21 23:41:13 +053084#include "cxgb4_tc_flower.h"
Atul Guptaa45695042017-07-04 16:46:20 +053085#include "cxgb4_ptp.h"
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +053086#include "cxgb4_cudbg.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000087
Hariprasad Shenai812034f2015-04-06 20:23:23 +053088char cxgb4_driver_name[] = KBUILD_MODNAME;
89
Vipul Pandya01bcca62013-07-04 16:10:46 +053090#ifdef DRV_VERSION
91#undef DRV_VERSION
92#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000093#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053094const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053095#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000096
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000097#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
98 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
99 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
100
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530101/* Macros needed to support the PCI Device ID Table ...
102 */
103#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530104 static const struct pci_device_id cxgb4_pci_tbl[] = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +0530105#define CXGB4_UNIFIED_PF 0x4
106
107#define CH_PCI_DEVICE_ID_FUNCTION CXGB4_UNIFIED_PF
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000108
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530109/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
110 * called for both.
111 */
112#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
113
114#define CH_PCI_ID_TABLE_ENTRY(devid) \
Ganesh Goudarbaf50862018-01-16 16:17:40 +0530115 {PCI_VDEVICE(CHELSIO, (devid)), CXGB4_UNIFIED_PF}
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530116
117#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
118 { 0, } \
119 }
120
121#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000122
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530123#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000124#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530125#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530126#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000127#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530128#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530129#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
130#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
131#define PHY_AQ1202_DEVICEID 0x4409
132#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000133
134MODULE_DESCRIPTION(DRV_DESC);
135MODULE_AUTHOR("Chelsio Communications");
136MODULE_LICENSE("Dual BSD/GPL");
137MODULE_VERSION(DRV_VERSION);
138MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530139MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000140MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530141MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000142
Vipul Pandya636f9d32012-09-26 02:39:39 +0000143/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000144 * The driver uses the best interrupt scheme available on a platform in the
145 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
146 * of these schemes the driver may consider as follows:
147 *
148 * msi = 2: choose from among all three options
149 * msi = 1: only consider MSI and INTx interrupts
150 * msi = 0: force INTx interrupts
151 */
152static int msi = 2;
153
154module_param(msi, int, 0644);
155MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
156
157/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000158 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
159 * offset by 2 bytes in order to have the IP headers line up on 4-byte
160 * boundaries. This is a requirement for many architectures which will throw
161 * a machine check fault if an attempt is made to access one of the 4-byte IP
162 * header fields on a non-4-byte boundary. And it's a major performance issue
163 * even on some architectures which allow it like some implementations of the
164 * x86 ISA. However, some architectures don't mind this and for some very
165 * edge-case performance sensitive applications (like forwarding large volumes
166 * of small packets), setting this DMA offset to 0 will decrease the number of
167 * PCI-E Bus transfers enough to measurably affect performance.
168 */
169static int rx_dma_offset = 2;
170
Anish Bhatt688848b2014-06-19 21:37:13 -0700171/* TX Queue select used to determine what algorithm to use for selecting TX
172 * queue. Select between the kernel provided function (select_queue=0) or user
173 * cxgb_select_queue function (select_queue=1)
174 *
175 * Default: select_queue=0
176 */
177static int select_queue;
178module_param(select_queue, int, 0644);
179MODULE_PARM_DESC(select_queue,
180 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
181
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000182static struct dentry *cxgb4_debugfs_root;
183
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530184LIST_HEAD(adapter_list);
185DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000186
187static void link_report(struct net_device *dev)
188{
189 if (!netif_carrier_ok(dev))
190 netdev_info(dev, "link down\n");
191 else {
192 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
193
Hariprasad Shenai85412252015-10-01 13:48:48 +0530194 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000195 const struct port_info *p = netdev_priv(dev);
196
197 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000198 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000199 s = "100Mbps";
200 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530201 case 1000:
202 s = "1Gbps";
203 break;
204 case 10000:
205 s = "10Gbps";
206 break;
207 case 25000:
208 s = "25Gbps";
209 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000210 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530211 s = "40Gbps";
212 break;
Ganesh Goudar7cbe5432018-03-10 17:34:50 +0530213 case 50000:
214 s = "50Gbps";
215 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530216 case 100000:
217 s = "100Gbps";
218 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530219 default:
220 pr_info("%s: unsupported speed: %d\n",
221 dev->name, p->link_cfg.speed);
222 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000223 }
224
225 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
226 fc[p->link_cfg.fc]);
227 }
228}
229
Anish Bhatt688848b2014-06-19 21:37:13 -0700230#ifdef CONFIG_CHELSIO_T4_DCB
231/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
232static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
233{
234 struct port_info *pi = netdev_priv(dev);
235 struct adapter *adap = pi->adapter;
236 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
237 int i;
238
239 /* We use a simple mapping of Port TX Queue Index to DCB
240 * Priority when we're enabling DCB.
241 */
242 for (i = 0; i < pi->nqsets; i++, txq++) {
243 u32 name, value;
244 int err;
245
Hariprasad Shenai51678652014-11-21 12:52:02 +0530246 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
247 FW_PARAMS_PARAM_X_V(
248 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
249 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700250 value = enable ? i : 0xffffffff;
251
252 /* Since we can be called while atomic (from "interrupt
253 * level") we need to issue the Set Parameters Commannd
254 * without sleeping (timeout < 0).
255 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530256 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530257 &name, &value,
258 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700259
260 if (err)
261 dev_err(adap->pdev_dev,
262 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
263 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700264 else
265 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700266 }
267}
Anish Bhatt688848b2014-06-19 21:37:13 -0700268
Baoyou Xie50935852016-09-25 14:10:09 +0800269static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530270{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530271 struct port_info *pi = netdev_priv(dev);
272
273 if (!pi->dcb.enabled)
274 return 0;
275
276 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
277 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530278}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200279#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530280
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000281void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
282{
283 struct net_device *dev = adapter->port[port_id];
284
285 /* Skip changes from disabled ports. */
286 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
287 if (link_stat)
288 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700289 else {
290#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530291 if (cxgb4_dcb_enabled(dev)) {
Ganesh Goudarba581f72017-09-23 16:07:28 +0530292 cxgb4_dcb_reset(dev);
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530293 dcb_tx_queue_prio_enable(dev, false);
294 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700295#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000296 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700297 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000298
299 link_report(dev);
300 }
301}
302
303void t4_os_portmod_changed(const struct adapter *adap, int port_id)
304{
305 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000306 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000307 };
308
309 const struct net_device *dev = adap->port[port_id];
310 const struct port_info *pi = netdev_priv(dev);
311
312 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
313 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000314 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000315 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530316 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
317 netdev_info(dev, "%s: unsupported port module inserted\n",
318 dev->name);
319 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
320 netdev_info(dev, "%s: unknown port module inserted\n",
321 dev->name);
322 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
323 netdev_info(dev, "%s: transceiver module error\n", dev->name);
324 else
325 netdev_info(dev, "%s: unknown module type %d inserted\n",
326 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000327}
328
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530329int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
330module_param(dbfifo_int_thresh, int, 0644);
331MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
332
Vipul Pandya404d9e32012-10-08 02:59:43 +0000333/*
334 * usecs to sleep while draining the dbfifo
335 */
336static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530337module_param(dbfifo_drain_delay, int, 0644);
338MODULE_PARM_DESC(dbfifo_drain_delay,
339 "usecs to sleep while draining the dbfifo");
340
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530341static inline int cxgb4_set_addr_hash(struct port_info *pi)
342{
343 struct adapter *adap = pi->adapter;
344 u64 vec = 0;
345 bool ucast = false;
346 struct hash_mac_addr *entry;
347
348 /* Calculate the hash vector for the updated list and program it */
349 list_for_each_entry(entry, &adap->mac_hlist, list) {
350 ucast |= is_unicast_ether_addr(entry->addr);
351 vec |= (1ULL << hash_mac_addr(entry->addr));
352 }
353 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
354 vec, false);
355}
356
357static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
358{
359 struct port_info *pi = netdev_priv(netdev);
360 struct adapter *adap = pi->adapter;
361 int ret;
362 u64 mhash = 0;
363 u64 uhash = 0;
364 bool free = false;
365 bool ucast = is_unicast_ether_addr(mac_addr);
366 const u8 *maclist[1] = {mac_addr};
367 struct hash_mac_addr *new_entry;
368
369 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
370 NULL, ucast ? &uhash : &mhash, false);
371 if (ret < 0)
372 goto out;
373 /* if hash != 0, then add the addr to hash addr list
374 * so on the end we will calculate the hash for the
375 * list and program it
376 */
377 if (uhash || mhash) {
378 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
379 if (!new_entry)
380 return -ENOMEM;
381 ether_addr_copy(new_entry->addr, mac_addr);
382 list_add_tail(&new_entry->list, &adap->mac_hlist);
383 ret = cxgb4_set_addr_hash(pi);
384 }
385out:
386 return ret < 0 ? ret : 0;
387}
388
389static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
390{
391 struct port_info *pi = netdev_priv(netdev);
392 struct adapter *adap = pi->adapter;
393 int ret;
394 const u8 *maclist[1] = {mac_addr};
395 struct hash_mac_addr *entry, *tmp;
396
397 /* If the MAC address to be removed is in the hash addr
398 * list, delete it from the list and update hash vector
399 */
400 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
401 if (ether_addr_equal(entry->addr, mac_addr)) {
402 list_del(&entry->list);
403 kfree(entry);
404 return cxgb4_set_addr_hash(pi);
405 }
406 }
407
408 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
409 return ret < 0 ? -EINVAL : 0;
410}
411
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000412/*
413 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
414 * If @mtu is -1 it is left unchanged.
415 */
416static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
417{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000418 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530419 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000420
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530421 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
422 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530423
424 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
425 (dev->flags & IFF_PROMISC) ? 1 : 0,
426 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
427 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000428}
429
430/**
431 * link_start - enable a port
432 * @dev: the port to enable
433 *
434 * Performs the MAC and PHY actions needed to enable a port.
435 */
436static int link_start(struct net_device *dev)
437{
438 int ret;
439 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530440 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000441
442 /*
443 * We do not set address filters and promiscuity here, the stack does
444 * that step explicitly.
445 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000446 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000447 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000448 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000449 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000450 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000451 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000452 if (ret >= 0) {
453 pi->xact_addr_filt = ret;
454 ret = 0;
455 }
456 }
457 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530458 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000459 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700460 if (ret == 0) {
461 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700462 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
463 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700464 local_bh_enable();
465 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700466
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000467 return ret;
468}
469
Anish Bhatt688848b2014-06-19 21:37:13 -0700470#ifdef CONFIG_CHELSIO_T4_DCB
471/* Handle a Data Center Bridging update message from the firmware. */
472static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
473{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530474 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530475 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700476 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
477 int new_dcb_enabled;
478
479 cxgb4_dcb_handle_fw_update(adap, pcmd);
480 new_dcb_enabled = cxgb4_dcb_enabled(dev);
481
482 /* If the DCB has become enabled or disabled on the port then we're
483 * going to need to set up/tear down DCB Priority parameters for the
484 * TX Queues associated with the port.
485 */
486 if (new_dcb_enabled != old_dcb_enabled)
487 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
488}
489#endif /* CONFIG_CHELSIO_T4_DCB */
490
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000491/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000492 */
493static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
494 const struct pkt_gl *gl)
495{
496 u8 opcode = ((const struct rss_header *)rsp)->opcode;
497
498 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000499
500 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
501 */
502 if (unlikely(opcode == CPL_FW4_MSG &&
503 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
504 rsp++;
505 opcode = ((const struct rss_header *)rsp)->opcode;
506 rsp++;
507 if (opcode != CPL_SGE_EGR_UPDATE) {
508 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
509 , opcode);
510 goto out;
511 }
512 }
513
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000514 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
515 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800516 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000517 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000518
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000519 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000520 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530521 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000522 struct sge_eth_txq *eq;
523
524 eq = container_of(txq, struct sge_eth_txq, q);
525 netif_tx_wake_queue(eq->txq);
526 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530527 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000528
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530529 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000530 tasklet_schedule(&oq->qresume_tsk);
531 }
532 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
533 const struct cpl_fw6_msg *p = (void *)rsp;
534
Anish Bhatt688848b2014-06-19 21:37:13 -0700535#ifdef CONFIG_CHELSIO_T4_DCB
536 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530537 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700538 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530539 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700540
541 if (cmd == FW_PORT_CMD &&
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530542 (action == FW_PORT_ACTION_GET_PORT_INFO ||
543 action == FW_PORT_ACTION_GET_PORT_INFO32)) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530544 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700545 be32_to_cpu(pcmd->op_to_portid));
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530546 struct net_device *dev;
547 int dcbxdis, state_input;
548
549 dev = q->adap->port[q->adap->chan_map[port]];
550 dcbxdis = (action == FW_PORT_ACTION_GET_PORT_INFO
551 ? !!(pcmd->u.info.dcbxdis_pkd &
552 FW_PORT_CMD_DCBXDIS_F)
553 : !!(pcmd->u.info32.lstatus32_to_cbllen32 &
554 FW_PORT_CMD_DCBXDIS32_F));
555 state_input = (dcbxdis
556 ? CXGB4_DCB_INPUT_FW_DISABLED
557 : CXGB4_DCB_INPUT_FW_ENABLED);
Anish Bhatt688848b2014-06-19 21:37:13 -0700558
559 cxgb4_dcb_state_fsm(dev, state_input);
560 }
561
562 if (cmd == FW_PORT_CMD &&
563 action == FW_PORT_ACTION_L2_DCB_CFG)
564 dcb_rpl(q->adap, pcmd);
565 else
566#endif
567 if (p->type == 0)
568 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000569 } else if (opcode == CPL_L2T_WRITE_RPL) {
570 const struct cpl_l2t_write_rpl *p = (void *)rsp;
571
572 do_l2t_write_rpl(q->adap, p);
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +0530573 } else if (opcode == CPL_SMT_WRITE_RPL) {
574 const struct cpl_smt_write_rpl *p = (void *)rsp;
575
576 do_smt_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000577 } else if (opcode == CPL_SET_TCB_RPL) {
578 const struct cpl_set_tcb_rpl *p = (void *)rsp;
579
580 filter_rpl(q->adap, p);
Kumar Sanghvi12b276f2017-11-01 08:53:01 +0530581 } else if (opcode == CPL_ACT_OPEN_RPL) {
582 const struct cpl_act_open_rpl *p = (void *)rsp;
583
584 hash_filter_rpl(q->adap, p);
Kumar Sanghvi3b0b3be2017-11-01 08:53:02 +0530585 } else if (opcode == CPL_ABORT_RPL_RSS) {
586 const struct cpl_abort_rpl_rss *p = (void *)rsp;
587
588 hash_del_filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000589 } else
590 dev_err(q->adap->pdev_dev,
591 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000592out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000593 return 0;
594}
595
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000596static void disable_msi(struct adapter *adapter)
597{
598 if (adapter->flags & USING_MSIX) {
599 pci_disable_msix(adapter->pdev);
600 adapter->flags &= ~USING_MSIX;
601 } else if (adapter->flags & USING_MSI) {
602 pci_disable_msi(adapter->pdev);
603 adapter->flags &= ~USING_MSI;
604 }
605}
606
607/*
608 * Interrupt handler for non-data events used with MSI-X.
609 */
610static irqreturn_t t4_nondata_intr(int irq, void *cookie)
611{
612 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530613 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000614
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530615 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000616 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530617 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000618 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530619 if (adap->flags & MASTER_PF)
620 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000621 return IRQ_HANDLED;
622}
623
624/*
625 * Name the MSI-X interrupts.
626 */
627static void name_msix_vecs(struct adapter *adap)
628{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000629 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000630
631 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000632 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000633
634 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000635 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
636 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000637
638 /* Ethernet queues */
639 for_each_port(adap, j) {
640 struct net_device *d = adap->port[j];
641 const struct port_info *pi = netdev_priv(d);
642
Dimitris Michailidisba278162010-12-14 21:36:50 +0000643 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000644 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
645 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000646 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000647}
648
649static int request_msix_queue_irqs(struct adapter *adap)
650{
651 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530652 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530653 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000654
655 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
656 adap->msix_info[1].desc, &s->fw_evtq);
657 if (err)
658 return err;
659
660 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000661 err = request_irq(adap->msix_info[msi_index].vec,
662 t4_sge_intr_msix, 0,
663 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000664 &s->ethrxq[ethqidx].rspq);
665 if (err)
666 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000667 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000668 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000669 return 0;
670
671unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000672 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000673 free_irq(adap->msix_info[--msi_index].vec,
674 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000675 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
676 return err;
677}
678
679static void free_msix_queue_irqs(struct adapter *adap)
680{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000681 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000682 struct sge *s = &adap->sge;
683
684 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
685 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000686 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000687}
688
689/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530690 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000691 * @pi: the port
692 * @queues: array of queue indices for RSS
693 *
694 * Sets up the portion of the HW RSS table for the port's VI to distribute
695 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530696 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000697 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530698int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000699{
700 u16 *rss;
701 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530702 struct adapter *adapter = pi->adapter;
703 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000704
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530705 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000706 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
707 if (!rss)
708 return -ENOMEM;
709
710 /* map the queue indices to queue ids */
711 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530712 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000713
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530714 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000715 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530716 /* If Tunnel All Lookup isn't specified in the global RSS
717 * Configuration, then we need to specify a default Ingress
718 * Queue for any ingress packets which aren't hashed. We'll
719 * use our first ingress queue ...
720 */
721 if (!err)
722 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
723 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
724 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
725 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
726 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
727 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
728 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000729 kfree(rss);
730 return err;
731}
732
733/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000734 * setup_rss - configure RSS
735 * @adap: the adapter
736 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000737 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000738 */
739static int setup_rss(struct adapter *adap)
740{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530741 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000742
743 for_each_port(adap, i) {
744 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000745
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530746 /* Fill default values with equal distribution */
747 for (j = 0; j < pi->rss_size; j++)
748 pi->rss[j] = j % pi->nqsets;
749
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530750 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000751 if (err)
752 return err;
753 }
754 return 0;
755}
756
757/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000758 * Return the channel of the ingress queue with the given qid.
759 */
760static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
761{
762 qid -= p->ingr_start;
763 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
764}
765
766/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000767 * Wait until all NAPI handlers are descheduled.
768 */
769static void quiesce_rx(struct adapter *adap)
770{
771 int i;
772
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530773 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000774 struct sge_rspq *q = adap->sge.ingr_map[i];
775
Eric Dumazet5226b7912017-02-02 11:44:27 -0800776 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000777 napi_disable(&q->napi);
778 }
779}
780
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530781/* Disable interrupt and napi handler */
782static void disable_interrupts(struct adapter *adap)
783{
784 if (adap->flags & FULL_INIT_DONE) {
785 t4_intr_disable(adap);
786 if (adap->flags & USING_MSIX) {
787 free_msix_queue_irqs(adap);
788 free_irq(adap->msix_info[0].vec, adap);
789 } else {
790 free_irq(adap->pdev->irq, adap);
791 }
792 quiesce_rx(adap);
793 }
794}
795
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000796/*
797 * Enable NAPI scheduling and interrupt generation for all Rx queues.
798 */
799static void enable_rx(struct adapter *adap)
800{
801 int i;
802
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530803 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000804 struct sge_rspq *q = adap->sge.ingr_map[i];
805
806 if (!q)
807 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800808 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800810
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000811 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530812 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
813 SEINTARM_V(q->intr_params) |
814 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815 }
816}
817
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530818
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530819static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000820{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000821 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530822 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000823
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530824 bitmap_zero(s->starving_fl, s->egr_sz);
825 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000826
827 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530828 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000829 else {
830 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530831 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000832 if (err)
833 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530834 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000835 }
836
837 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530838 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530839 if (err)
840 t4_free_sge_resources(adap);
841 return err;
842}
843
844/**
845 * setup_sge_queues - configure SGE Tx/Rx/response queues
846 * @adap: the adapter
847 *
848 * Determines how many sets of SGE queues to use and initializes them.
849 * We support multiple queue sets per port if we have MSI-X, otherwise
850 * just one queue set per port.
851 */
852static int setup_sge_queues(struct adapter *adap)
853{
854 int err, i, j;
855 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530856 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530857 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000858
Ganesh Goudard427cae2017-06-16 15:36:09 +0530859 if (is_uld(adap))
860 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
861
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000862 for_each_port(adap, i) {
863 struct net_device *dev = adap->port[i];
864 struct port_info *pi = netdev_priv(dev);
865 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
866 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
867
868 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530869 if (adap->msi_idx > 0)
870 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000871 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530872 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530873 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530874 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530875 t4_get_tp_ch_map(adap,
876 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000877 if (err)
878 goto freeout;
879 q->rspq.idx = j;
880 memset(&q->stats, 0, sizeof(q->stats));
881 }
882 for (j = 0; j < pi->nqsets; j++, t++) {
883 err = t4_sge_alloc_eth_txq(adap, t, dev,
884 netdev_get_tx_queue(dev, j),
885 s->fw_evtq.cntxt_id);
886 if (err)
887 goto freeout;
888 }
889 }
890
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000891 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530892 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000893 * have RDMA queues, and that's the right value.
894 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530895 if (rxq_info)
896 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
897
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000898 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530899 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000900 if (err)
901 goto freeout;
902 }
903
Atul Guptaa45695042017-07-04 16:46:20 +0530904 if (!is_t4(adap->params.chip)) {
905 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
906 netdev_get_tx_queue(adap->port[0], 0)
907 , s->fw_evtq.cntxt_id);
908 if (err)
909 goto freeout;
910 }
911
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530912 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530913 MPS_TRC_RSS_CONTROL_A :
914 MPS_T5_TRC_RSS_CONTROL_A,
915 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
916 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000917 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530918freeout:
919 t4_free_sge_resources(adap);
920 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000921}
922
Anish Bhatt688848b2014-06-19 21:37:13 -0700923static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
924 void *accel_priv, select_queue_fallback_t fallback)
925{
926 int txq;
927
928#ifdef CONFIG_CHELSIO_T4_DCB
929 /* If a Data Center Bridging has been successfully negotiated on this
930 * link then we'll use the skb's priority to map it to a TX Queue.
931 * The skb's priority is determined via the VLAN Tag Priority Code
932 * Point field.
933 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530934 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700935 u16 vlan_tci;
936 int err;
937
938 err = vlan_get_tag(skb, &vlan_tci);
939 if (unlikely(err)) {
940 if (net_ratelimit())
941 netdev_warn(dev,
942 "TX Packet without VLAN Tag on DCB Link\n");
943 txq = 0;
944 } else {
945 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530946#ifdef CONFIG_CHELSIO_T4_FCOE
947 if (skb->protocol == htons(ETH_P_FCOE))
948 txq = skb->priority & 0x7;
949#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700950 }
951 return txq;
952 }
953#endif /* CONFIG_CHELSIO_T4_DCB */
954
955 if (select_queue) {
956 txq = (skb_rx_queue_recorded(skb)
957 ? skb_get_rx_queue(skb)
958 : smp_processor_id());
959
960 while (unlikely(txq >= dev->real_num_tx_queues))
961 txq -= dev->real_num_tx_queues;
962
963 return txq;
964 }
965
966 return fallback(dev, skb) % dev->real_num_tx_queues;
967}
968
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000969static int closest_timer(const struct sge *s, int time)
970{
971 int i, delta, match = 0, min_delta = INT_MAX;
972
973 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
974 delta = time - s->timer_val[i];
975 if (delta < 0)
976 delta = -delta;
977 if (delta < min_delta) {
978 min_delta = delta;
979 match = i;
980 }
981 }
982 return match;
983}
984
985static int closest_thres(const struct sge *s, int thres)
986{
987 int i, delta, match = 0, min_delta = INT_MAX;
988
989 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
990 delta = thres - s->counter_val[i];
991 if (delta < 0)
992 delta = -delta;
993 if (delta < min_delta) {
994 min_delta = delta;
995 match = i;
996 }
997 }
998 return match;
999}
1000
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001001/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301002 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001003 * @q: the Rx queue
1004 * @us: the hold-off time in us, or 0 to disable timer
1005 * @cnt: the hold-off packet count, or 0 to disable counter
1006 *
1007 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1008 * one of the two needs to be enabled for the queue to generate interrupts.
1009 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301010int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1011 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001012{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301013 struct adapter *adap = q->adap;
1014
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001015 if ((us | cnt) == 0)
1016 cnt = 1;
1017
1018 if (cnt) {
1019 int err;
1020 u32 v, new_idx;
1021
1022 new_idx = closest_thres(&adap->sge, cnt);
1023 if (q->desc && q->pktcnt_idx != new_idx) {
1024 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301025 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1026 FW_PARAMS_PARAM_X_V(
1027 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1028 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301029 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1030 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001031 if (err)
1032 return err;
1033 }
1034 q->pktcnt_idx = new_idx;
1035 }
1036
1037 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301038 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001039 return 0;
1040}
1041
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001042static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001043{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001044 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001045 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001046 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001047
Patrick McHardyf6469682013-04-19 02:04:27 +00001048 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001049 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001050
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301051 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001052 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001053 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001054 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001055 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001056 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001057}
1058
Bill Pemberton91744942012-12-03 09:23:02 -05001059static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001060{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001061 if (IS_ERR_OR_NULL(adap->debugfs_root))
1062 return -1;
1063
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301064#ifdef CONFIG_DEBUG_FS
1065 t4_setup_debugfs(adap);
1066#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001067 return 0;
1068}
1069
1070/*
1071 * upper-layer driver support
1072 */
1073
1074/*
1075 * Allocate an active-open TID and set it to the supplied value.
1076 */
1077int cxgb4_alloc_atid(struct tid_info *t, void *data)
1078{
1079 int atid = -1;
1080
1081 spin_lock_bh(&t->atid_lock);
1082 if (t->afree) {
1083 union aopen_entry *p = t->afree;
1084
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001085 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001086 t->afree = p->next;
1087 p->data = data;
1088 t->atids_in_use++;
1089 }
1090 spin_unlock_bh(&t->atid_lock);
1091 return atid;
1092}
1093EXPORT_SYMBOL(cxgb4_alloc_atid);
1094
1095/*
1096 * Release an active-open TID.
1097 */
1098void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1099{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001100 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001101
1102 spin_lock_bh(&t->atid_lock);
1103 p->next = t->afree;
1104 t->afree = p;
1105 t->atids_in_use--;
1106 spin_unlock_bh(&t->atid_lock);
1107}
1108EXPORT_SYMBOL(cxgb4_free_atid);
1109
1110/*
1111 * Allocate a server TID and set it to the supplied value.
1112 */
1113int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1114{
1115 int stid;
1116
1117 spin_lock_bh(&t->stid_lock);
1118 if (family == PF_INET) {
1119 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1120 if (stid < t->nstids)
1121 __set_bit(stid, t->stid_bmap);
1122 else
1123 stid = -1;
1124 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301125 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001126 if (stid < 0)
1127 stid = -1;
1128 }
1129 if (stid >= 0) {
1130 t->stid_tab[stid].data = data;
1131 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301132 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1133 * This is equivalent to 4 TIDs. With CLIP enabled it
1134 * needs 2 TIDs.
1135 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301136 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301137 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301138 t->v6_stids_in_use += 2;
1139 } else {
1140 t->stids_in_use++;
1141 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001142 }
1143 spin_unlock_bh(&t->stid_lock);
1144 return stid;
1145}
1146EXPORT_SYMBOL(cxgb4_alloc_stid);
1147
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001148/* Allocate a server filter TID and set it to the supplied value.
1149 */
1150int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1151{
1152 int stid;
1153
1154 spin_lock_bh(&t->stid_lock);
1155 if (family == PF_INET) {
1156 stid = find_next_zero_bit(t->stid_bmap,
1157 t->nstids + t->nsftids, t->nstids);
1158 if (stid < (t->nstids + t->nsftids))
1159 __set_bit(stid, t->stid_bmap);
1160 else
1161 stid = -1;
1162 } else {
1163 stid = -1;
1164 }
1165 if (stid >= 0) {
1166 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301167 stid -= t->nstids;
1168 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301169 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001170 }
1171 spin_unlock_bh(&t->stid_lock);
1172 return stid;
1173}
1174EXPORT_SYMBOL(cxgb4_alloc_sftid);
1175
1176/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001177 */
1178void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1179{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301180 /* Is it a server filter TID? */
1181 if (t->nsftids && (stid >= t->sftid_base)) {
1182 stid -= t->sftid_base;
1183 stid += t->nstids;
1184 } else {
1185 stid -= t->stid_base;
1186 }
1187
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001188 spin_lock_bh(&t->stid_lock);
1189 if (family == PF_INET)
1190 __clear_bit(stid, t->stid_bmap);
1191 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301192 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001193 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301194 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301195 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301196 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301197 t->v6_stids_in_use -= 2;
1198 } else {
1199 t->stids_in_use--;
1200 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301201 } else {
1202 t->sftids_in_use--;
1203 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301204
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001205 spin_unlock_bh(&t->stid_lock);
1206}
1207EXPORT_SYMBOL(cxgb4_free_stid);
1208
1209/*
1210 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1211 */
1212static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1213 unsigned int tid)
1214{
1215 struct cpl_tid_release *req;
1216
1217 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001218 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001219 INIT_TP_WR(req, tid);
1220 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1221}
1222
1223/*
1224 * Queue a TID release request and if necessary schedule a work queue to
1225 * process it.
1226 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001227static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1228 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001229{
1230 void **p = &t->tid_tab[tid];
1231 struct adapter *adap = container_of(t, struct adapter, tids);
1232
1233 spin_lock_bh(&adap->tid_release_lock);
1234 *p = adap->tid_release_head;
1235 /* Low 2 bits encode the Tx channel number */
1236 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1237 if (!adap->tid_release_task_busy) {
1238 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001239 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001240 }
1241 spin_unlock_bh(&adap->tid_release_lock);
1242}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001243
1244/*
1245 * Process the list of pending TID release requests.
1246 */
1247static void process_tid_release_list(struct work_struct *work)
1248{
1249 struct sk_buff *skb;
1250 struct adapter *adap;
1251
1252 adap = container_of(work, struct adapter, tid_release_task);
1253
1254 spin_lock_bh(&adap->tid_release_lock);
1255 while (adap->tid_release_head) {
1256 void **p = adap->tid_release_head;
1257 unsigned int chan = (uintptr_t)p & 3;
1258 p = (void *)p - chan;
1259
1260 adap->tid_release_head = *p;
1261 *p = NULL;
1262 spin_unlock_bh(&adap->tid_release_lock);
1263
1264 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1265 GFP_KERNEL)))
1266 schedule_timeout_uninterruptible(1);
1267
1268 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1269 t4_ofld_send(adap, skb);
1270 spin_lock_bh(&adap->tid_release_lock);
1271 }
1272 adap->tid_release_task_busy = false;
1273 spin_unlock_bh(&adap->tid_release_lock);
1274}
1275
1276/*
1277 * Release a TID and inform HW. If we are unable to allocate the release
1278 * message we defer to a work queue.
1279 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301280void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1281 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001282{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001283 struct sk_buff *skb;
1284 struct adapter *adap = container_of(t, struct adapter, tids);
1285
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301286 WARN_ON(tid >= t->ntids);
1287
1288 if (t->tid_tab[tid]) {
1289 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301290 atomic_dec(&t->conns_in_use);
1291 if (t->hash_base && (tid >= t->hash_base)) {
1292 if (family == AF_INET6)
1293 atomic_sub(2, &t->hash_tids_in_use);
1294 else
1295 atomic_dec(&t->hash_tids_in_use);
1296 } else {
1297 if (family == AF_INET6)
1298 atomic_sub(2, &t->tids_in_use);
1299 else
1300 atomic_dec(&t->tids_in_use);
1301 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301302 }
1303
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001304 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1305 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001306 mk_tid_release(skb, chan, tid);
1307 t4_ofld_send(adap, skb);
1308 } else
1309 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001310}
1311EXPORT_SYMBOL(cxgb4_remove_tid);
1312
1313/*
1314 * Allocate and initialize the TID tables. Returns 0 on success.
1315 */
1316static int tid_init(struct tid_info *t)
1317{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301318 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301319 unsigned int max_ftids = t->nftids + t->nsftids;
1320 unsigned int natids = t->natids;
1321 unsigned int stid_bmap_size;
1322 unsigned int ftid_bmap_size;
1323 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001324
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001325 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301326 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001327 size = t->ntids * sizeof(*t->tid_tab) +
1328 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001329 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001330 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001331 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301332 max_ftids * sizeof(*t->ftid_tab) +
1333 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001334
Michal Hocko752ade62017-05-08 15:57:27 -07001335 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001336 if (!t->tid_tab)
1337 return -ENOMEM;
1338
1339 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1340 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001341 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001342 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301343 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001344 spin_lock_init(&t->stid_lock);
1345 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301346 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001347
1348 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301349 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301350 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001351 t->afree = NULL;
1352 t->atids_in_use = 0;
1353 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301354 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301355 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001356
1357 /* Setup the free list for atid_tab and clear the stid bitmap. */
1358 if (natids) {
1359 while (--natids)
1360 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1361 t->afree = t->atid_tab;
1362 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301363
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301364 if (is_offload(adap)) {
1365 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1366 /* Reserve stid 0 for T4/T5 adapters */
1367 if (!t->stid_base &&
1368 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1369 __set_bit(0, t->stid_bmap);
1370 }
1371
1372 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001373 return 0;
1374}
1375
1376/**
1377 * cxgb4_create_server - create an IP server
1378 * @dev: the device
1379 * @stid: the server TID
1380 * @sip: local IP address to bind server to
1381 * @sport: the server's TCP port
1382 * @queue: queue to direct messages from this server to
1383 *
1384 * Create an IP server for the given port and address.
1385 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1386 */
1387int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001388 __be32 sip, __be16 sport, __be16 vlan,
1389 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001390{
1391 unsigned int chan;
1392 struct sk_buff *skb;
1393 struct adapter *adap;
1394 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301395 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001396
1397 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1398 if (!skb)
1399 return -ENOMEM;
1400
1401 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001402 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001403 INIT_TP_WR(req, 0);
1404 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1405 req->local_port = sport;
1406 req->peer_port = htons(0);
1407 req->local_ip = sip;
1408 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001409 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001410 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001411 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1412 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301413 ret = t4_mgmt_tx(adap, skb);
1414 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001415}
1416EXPORT_SYMBOL(cxgb4_create_server);
1417
Vipul Pandya80f40c12013-07-04 16:10:45 +05301418/* cxgb4_create_server6 - create an IPv6 server
1419 * @dev: the device
1420 * @stid: the server TID
1421 * @sip: local IPv6 address to bind server to
1422 * @sport: the server's TCP port
1423 * @queue: queue to direct messages from this server to
1424 *
1425 * Create an IPv6 server for the given port and address.
1426 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1427 */
1428int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1429 const struct in6_addr *sip, __be16 sport,
1430 unsigned int queue)
1431{
1432 unsigned int chan;
1433 struct sk_buff *skb;
1434 struct adapter *adap;
1435 struct cpl_pass_open_req6 *req;
1436 int ret;
1437
1438 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1439 if (!skb)
1440 return -ENOMEM;
1441
1442 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001443 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301444 INIT_TP_WR(req, 0);
1445 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1446 req->local_port = sport;
1447 req->peer_port = htons(0);
1448 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1449 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1450 req->peer_ip_hi = cpu_to_be64(0);
1451 req->peer_ip_lo = cpu_to_be64(0);
1452 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001453 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001454 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1455 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301456 ret = t4_mgmt_tx(adap, skb);
1457 return net_xmit_eval(ret);
1458}
1459EXPORT_SYMBOL(cxgb4_create_server6);
1460
1461int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1462 unsigned int queue, bool ipv6)
1463{
1464 struct sk_buff *skb;
1465 struct adapter *adap;
1466 struct cpl_close_listsvr_req *req;
1467 int ret;
1468
1469 adap = netdev2adap(dev);
1470
1471 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1472 if (!skb)
1473 return -ENOMEM;
1474
Johannes Berg4df864c2017-06-16 14:29:21 +02001475 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301476 INIT_TP_WR(req, 0);
1477 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001478 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1479 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301480 ret = t4_mgmt_tx(adap, skb);
1481 return net_xmit_eval(ret);
1482}
1483EXPORT_SYMBOL(cxgb4_remove_server);
1484
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001485/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001486 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1487 * @mtus: the HW MTU table
1488 * @mtu: the target MTU
1489 * @idx: index of selected entry in the MTU table
1490 *
1491 * Returns the index and the value in the HW MTU table that is closest to
1492 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1493 * table, in which case that smallest available value is selected.
1494 */
1495unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1496 unsigned int *idx)
1497{
1498 unsigned int i = 0;
1499
1500 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1501 ++i;
1502 if (idx)
1503 *idx = i;
1504 return mtus[i];
1505}
1506EXPORT_SYMBOL(cxgb4_best_mtu);
1507
1508/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301509 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1510 * @mtus: the HW MTU table
1511 * @header_size: Header Size
1512 * @data_size_max: maximum Data Segment Size
1513 * @data_size_align: desired Data Segment Size Alignment (2^N)
1514 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1515 *
1516 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1517 * MTU Table based solely on a Maximum MTU parameter, we break that
1518 * parameter up into a Header Size and Maximum Data Segment Size, and
1519 * provide a desired Data Segment Size Alignment. If we find an MTU in
1520 * the Hardware MTU Table which will result in a Data Segment Size with
1521 * the requested alignment _and_ that MTU isn't "too far" from the
1522 * closest MTU, then we'll return that rather than the closest MTU.
1523 */
1524unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1525 unsigned short header_size,
1526 unsigned short data_size_max,
1527 unsigned short data_size_align,
1528 unsigned int *mtu_idxp)
1529{
1530 unsigned short max_mtu = header_size + data_size_max;
1531 unsigned short data_size_align_mask = data_size_align - 1;
1532 int mtu_idx, aligned_mtu_idx;
1533
1534 /* Scan the MTU Table till we find an MTU which is larger than our
1535 * Maximum MTU or we reach the end of the table. Along the way,
1536 * record the last MTU found, if any, which will result in a Data
1537 * Segment Length matching the requested alignment.
1538 */
1539 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1540 unsigned short data_size = mtus[mtu_idx] - header_size;
1541
1542 /* If this MTU minus the Header Size would result in a
1543 * Data Segment Size of the desired alignment, remember it.
1544 */
1545 if ((data_size & data_size_align_mask) == 0)
1546 aligned_mtu_idx = mtu_idx;
1547
1548 /* If we're not at the end of the Hardware MTU Table and the
1549 * next element is larger than our Maximum MTU, drop out of
1550 * the loop.
1551 */
1552 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1553 break;
1554 }
1555
1556 /* If we fell out of the loop because we ran to the end of the table,
1557 * then we just have to use the last [largest] entry.
1558 */
1559 if (mtu_idx == NMTUS)
1560 mtu_idx--;
1561
1562 /* If we found an MTU which resulted in the requested Data Segment
1563 * Length alignment and that's "not far" from the largest MTU which is
1564 * less than or equal to the maximum MTU, then use that.
1565 */
1566 if (aligned_mtu_idx >= 0 &&
1567 mtu_idx - aligned_mtu_idx <= 1)
1568 mtu_idx = aligned_mtu_idx;
1569
1570 /* If the caller has passed in an MTU Index pointer, pass the
1571 * MTU Index back. Return the MTU value.
1572 */
1573 if (mtu_idxp)
1574 *mtu_idxp = mtu_idx;
1575 return mtus[mtu_idx];
1576}
1577EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1578
1579/**
Hariprasad S27999802015-09-23 17:19:26 +05301580 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1581 * @chip: chip type
1582 * @viid: VI id of the given port
1583 *
1584 * Return the SMT index for this VI.
1585 */
1586unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1587{
1588 /* In T4/T5, SMT contains 256 SMAC entries organized in
1589 * 128 rows of 2 entries each.
1590 * In T6, SMT contains 256 SMAC entries in 256 rows.
1591 * TODO: The below code needs to be updated when we add support
1592 * for 256 VFs.
1593 */
1594 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1595 return ((viid & 0x7f) << 1);
1596 else
1597 return (viid & 0x7f);
1598}
1599EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1600
1601/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001602 * cxgb4_port_chan - get the HW channel of a port
1603 * @dev: the net device for the port
1604 *
1605 * Return the HW Tx channel of the given port.
1606 */
1607unsigned int cxgb4_port_chan(const struct net_device *dev)
1608{
1609 return netdev2pinfo(dev)->tx_chan;
1610}
1611EXPORT_SYMBOL(cxgb4_port_chan);
1612
Vipul Pandya881806b2012-05-18 15:29:24 +05301613unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1614{
1615 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001616 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301617
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301618 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1619 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301620 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301621 lp_count = LP_COUNT_G(v1);
1622 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001623 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301624 lp_count = LP_COUNT_T5_G(v1);
1625 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001626 }
1627 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301628}
1629EXPORT_SYMBOL(cxgb4_dbfifo_count);
1630
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001631/**
1632 * cxgb4_port_viid - get the VI id of a port
1633 * @dev: the net device for the port
1634 *
1635 * Return the VI id of the given port.
1636 */
1637unsigned int cxgb4_port_viid(const struct net_device *dev)
1638{
1639 return netdev2pinfo(dev)->viid;
1640}
1641EXPORT_SYMBOL(cxgb4_port_viid);
1642
1643/**
1644 * cxgb4_port_idx - get the index of a port
1645 * @dev: the net device for the port
1646 *
1647 * Return the index of the given port.
1648 */
1649unsigned int cxgb4_port_idx(const struct net_device *dev)
1650{
1651 return netdev2pinfo(dev)->port_id;
1652}
1653EXPORT_SYMBOL(cxgb4_port_idx);
1654
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001655void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1656 struct tp_tcp_stats *v6)
1657{
1658 struct adapter *adap = pci_get_drvdata(pdev);
1659
1660 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301661 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001662 spin_unlock(&adap->stats_lock);
1663}
1664EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1665
1666void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1667 const unsigned int *pgsz_order)
1668{
1669 struct adapter *adap = netdev2adap(dev);
1670
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301671 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1672 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1673 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1674 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001675}
1676EXPORT_SYMBOL(cxgb4_iscsi_init);
1677
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301678int cxgb4_flush_eq_cache(struct net_device *dev)
1679{
1680 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301681
Rahul Lakkireddy736c3b92017-12-08 09:48:40 +05301682 return t4_sge_ctxt_flush(adap, adap->mbox, CTXT_EGRESS);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301683}
1684EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1685
1686static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1687{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301688 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301689 __be64 indices;
1690 int ret;
1691
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301692 spin_lock(&adap->win0_lock);
1693 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1694 sizeof(indices), (__be32 *)&indices,
1695 T4_MEMORY_READ);
1696 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301697 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001698 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1699 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301700 }
1701 return ret;
1702}
1703
1704int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1705 u16 size)
1706{
1707 struct adapter *adap = netdev2adap(dev);
1708 u16 hw_pidx, hw_cidx;
1709 int ret;
1710
1711 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1712 if (ret)
1713 goto out;
1714
1715 if (pidx != hw_pidx) {
1716 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301717 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301718
1719 if (pidx >= hw_pidx)
1720 delta = pidx - hw_pidx;
1721 else
1722 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301723
1724 if (is_t4(adap->params.chip))
1725 val = PIDX_V(delta);
1726 else
1727 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301728 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301729 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1730 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301731 }
1732out:
1733 return ret;
1734}
1735EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1736
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301737int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1738{
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301739 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301740 u32 edc0_end, edc1_end, mc0_end, mc1_end;
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05301741 u32 offset, memtype, memaddr;
1742 struct adapter *adap;
1743 u32 hma_size = 0;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301744 int ret;
1745
1746 adap = netdev2adap(dev);
1747
1748 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1749
1750 /* Figure out where the offset lands in the Memory Type/Address scheme.
1751 * This code assumes that the memory is laid out starting at offset 0
1752 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1753 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1754 * MC0, and some have both MC0 and MC1.
1755 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301756 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1757 edc0_size = EDRAM0_SIZE_G(size) << 20;
1758 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1759 edc1_size = EDRAM1_SIZE_G(size) << 20;
1760 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1761 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301762
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05301763 if (t4_read_reg(adap, MA_TARGET_MEM_ENABLE_A) & HMA_MUX_F) {
1764 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1765 hma_size = EXT_MEM1_SIZE_G(size) << 20;
1766 }
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301767 edc0_end = edc0_size;
1768 edc1_end = edc0_end + edc1_size;
1769 mc0_end = edc1_end + mc0_size;
1770
1771 if (offset < edc0_end) {
1772 memtype = MEM_EDC0;
1773 memaddr = offset;
1774 } else if (offset < edc1_end) {
1775 memtype = MEM_EDC1;
1776 memaddr = offset - edc0_end;
1777 } else {
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05301778 if (hma_size && (offset < (edc1_end + hma_size))) {
1779 memtype = MEM_HMA;
1780 memaddr = offset - edc1_end;
1781 } else if (offset < mc0_end) {
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301782 memtype = MEM_MC0;
1783 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301784 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301785 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1786 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301787 mc1_end = mc0_end + mc1_size;
1788 if (offset < mc1_end) {
1789 memtype = MEM_MC1;
1790 memaddr = offset - mc0_end;
1791 } else {
1792 /* offset beyond the end of any memory */
1793 goto err;
1794 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301795 } else {
1796 /* T4/T6 only has a single memory channel */
1797 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301798 }
1799 }
1800
1801 spin_lock(&adap->win0_lock);
1802 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1803 spin_unlock(&adap->win0_lock);
1804 return ret;
1805
1806err:
1807 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1808 stag, offset);
1809 return -EINVAL;
1810}
1811EXPORT_SYMBOL(cxgb4_read_tpte);
1812
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301813u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1814{
1815 u32 hi, lo;
1816 struct adapter *adap;
1817
1818 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301819 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1820 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301821
1822 return ((u64)hi << 32) | (u64)lo;
1823}
1824EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1825
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301826int cxgb4_bar2_sge_qregs(struct net_device *dev,
1827 unsigned int qid,
1828 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301829 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301830 u64 *pbar2_qoffset,
1831 unsigned int *pbar2_qid)
1832{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301833 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301834 qid,
1835 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1836 ? T4_BAR2_QTYPE_EGRESS
1837 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301838 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301839 pbar2_qoffset,
1840 pbar2_qid);
1841}
1842EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1843
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001844static struct pci_driver cxgb4_driver;
1845
1846static void check_neigh_update(struct neighbour *neigh)
1847{
1848 const struct device *parent;
1849 const struct net_device *netdev = neigh->dev;
1850
Parav Panditd0d7b102017-02-04 11:00:49 -06001851 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001852 netdev = vlan_dev_real_dev(netdev);
1853 parent = netdev->dev.parent;
1854 if (parent && parent->driver == &cxgb4_driver.driver)
1855 t4_l2t_update(dev_get_drvdata(parent), neigh);
1856}
1857
1858static int netevent_cb(struct notifier_block *nb, unsigned long event,
1859 void *data)
1860{
1861 switch (event) {
1862 case NETEVENT_NEIGH_UPDATE:
1863 check_neigh_update(data);
1864 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001865 case NETEVENT_REDIRECT:
1866 default:
1867 break;
1868 }
1869 return 0;
1870}
1871
1872static bool netevent_registered;
1873static struct notifier_block cxgb4_netevent_nb = {
1874 .notifier_call = netevent_cb
1875};
1876
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301877static void drain_db_fifo(struct adapter *adap, int usecs)
1878{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001879 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301880
1881 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301882 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1883 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301884 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301885 lp_count = LP_COUNT_G(v1);
1886 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001887 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301888 lp_count = LP_COUNT_T5_G(v1);
1889 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001890 }
1891
1892 if (lp_count == 0 && hp_count == 0)
1893 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301894 set_current_state(TASK_UNINTERRUPTIBLE);
1895 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301896 } while (1);
1897}
1898
1899static void disable_txq_db(struct sge_txq *q)
1900{
Steve Wise05eb2382014-03-14 21:52:08 +05301901 unsigned long flags;
1902
1903 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301904 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301905 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301906}
1907
Steve Wise05eb2382014-03-14 21:52:08 +05301908static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301909{
1910 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301911 if (q->db_pidx_inc) {
1912 /* Make sure that all writes to the TX descriptors
1913 * are committed before we tell HW about them.
1914 */
1915 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301916 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1917 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301918 q->db_pidx_inc = 0;
1919 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301920 q->db_disabled = 0;
1921 spin_unlock_irq(&q->db_lock);
1922}
1923
1924static void disable_dbs(struct adapter *adap)
1925{
1926 int i;
1927
1928 for_each_ethrxq(&adap->sge, i)
1929 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301930 if (is_offload(adap)) {
1931 struct sge_uld_txq_info *txq_info =
1932 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1933
1934 if (txq_info) {
1935 for_each_ofldtxq(&adap->sge, i) {
1936 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1937
1938 disable_txq_db(&txq->q);
1939 }
1940 }
1941 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301942 for_each_port(adap, i)
1943 disable_txq_db(&adap->sge.ctrlq[i].q);
1944}
1945
1946static void enable_dbs(struct adapter *adap)
1947{
1948 int i;
1949
1950 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301951 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301952 if (is_offload(adap)) {
1953 struct sge_uld_txq_info *txq_info =
1954 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1955
1956 if (txq_info) {
1957 for_each_ofldtxq(&adap->sge, i) {
1958 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1959
1960 enable_txq_db(adap, &txq->q);
1961 }
1962 }
1963 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301964 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301965 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1966}
1967
1968static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1969{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301970 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1971
1972 if (adap->uld && adap->uld[type].handle)
1973 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301974}
1975
1976static void process_db_full(struct work_struct *work)
1977{
1978 struct adapter *adap;
1979
1980 adap = container_of(work, struct adapter, db_full_task);
1981
1982 drain_db_fifo(adap, dbfifo_drain_delay);
1983 enable_dbs(adap);
1984 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301985 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1986 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1987 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1988 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1989 else
1990 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1991 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301992}
1993
1994static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1995{
1996 u16 hw_pidx, hw_cidx;
1997 int ret;
1998
Steve Wise05eb2382014-03-14 21:52:08 +05301999 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302000 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
2001 if (ret)
2002 goto out;
2003 if (q->db_pidx != hw_pidx) {
2004 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302005 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302006
2007 if (q->db_pidx >= hw_pidx)
2008 delta = q->db_pidx - hw_pidx;
2009 else
2010 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302011
2012 if (is_t4(adap->params.chip))
2013 val = PIDX_V(delta);
2014 else
2015 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302016 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302017 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2018 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302019 }
2020out:
2021 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302022 q->db_pidx_inc = 0;
2023 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302024 if (ret)
2025 CH_WARN(adap, "DB drop recovery failed.\n");
2026}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302027
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302028static void recover_all_queues(struct adapter *adap)
2029{
2030 int i;
2031
2032 for_each_ethrxq(&adap->sge, i)
2033 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302034 if (is_offload(adap)) {
2035 struct sge_uld_txq_info *txq_info =
2036 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2037 if (txq_info) {
2038 for_each_ofldtxq(&adap->sge, i) {
2039 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2040
2041 sync_txq_pidx(adap, &txq->q);
2042 }
2043 }
2044 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302045 for_each_port(adap, i)
2046 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2047}
2048
Vipul Pandya881806b2012-05-18 15:29:24 +05302049static void process_db_drop(struct work_struct *work)
2050{
2051 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302052
Vipul Pandya881806b2012-05-18 15:29:24 +05302053 adap = container_of(work, struct adapter, db_drop_task);
2054
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302055 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302056 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002057 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302058 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002059 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302060 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002061 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302062 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302063 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002064 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2065 u16 qid = (dropped_db >> 15) & 0x1ffff;
2066 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302067 u64 bar2_qoffset;
2068 unsigned int bar2_qid;
2069 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002070
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302071 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002072 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302073 if (ret)
2074 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2075 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2076 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302077 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302078 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002079
2080 /* Re-enable BAR2 WC */
2081 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2082 }
2083
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302084 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2085 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302086}
2087
2088void t4_db_full(struct adapter *adap)
2089{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302090 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302091 disable_dbs(adap);
2092 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302093 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2094 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002095 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002096 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302097}
2098
2099void t4_db_dropped(struct adapter *adap)
2100{
Steve Wise05eb2382014-03-14 21:52:08 +05302101 if (is_t4(adap->params.chip)) {
2102 disable_dbs(adap);
2103 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2104 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002105 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302106}
2107
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302108void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002109{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002110 if (!netevent_registered) {
2111 register_netevent_notifier(&cxgb4_netevent_nb);
2112 netevent_registered = true;
2113 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002114}
2115
2116static void detach_ulds(struct adapter *adap)
2117{
2118 unsigned int i;
2119
2120 mutex_lock(&uld_mutex);
2121 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002122
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002123 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002124 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302125 adap->uld[i].state_change(adap->uld[i].handle,
2126 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002127
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002128 if (netevent_registered && list_empty(&adapter_list)) {
2129 unregister_netevent_notifier(&cxgb4_netevent_nb);
2130 netevent_registered = false;
2131 }
2132 mutex_unlock(&uld_mutex);
2133}
2134
2135static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2136{
2137 unsigned int i;
2138
2139 mutex_lock(&uld_mutex);
2140 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302141 if (adap->uld && adap->uld[i].handle)
2142 adap->uld[i].state_change(adap->uld[i].handle,
2143 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002144 mutex_unlock(&uld_mutex);
2145}
2146
Anish Bhatt1bb60372014-10-14 20:07:22 -07002147#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002148static int cxgb4_inet6addr_handler(struct notifier_block *this,
2149 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302150{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002151 struct inet6_ifaddr *ifa = data;
2152 struct net_device *event_dev = ifa->idev->dev;
2153 const struct device *parent = NULL;
2154#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302155 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002156#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002157 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002158 event_dev = vlan_dev_real_dev(event_dev);
2159#if IS_ENABLED(CONFIG_BONDING)
2160 if (event_dev->flags & IFF_MASTER) {
2161 list_for_each_entry(adap, &adapter_list, list_node) {
2162 switch (event) {
2163 case NETDEV_UP:
2164 cxgb4_clip_get(adap->port[0],
2165 (const u32 *)ifa, 1);
2166 break;
2167 case NETDEV_DOWN:
2168 cxgb4_clip_release(adap->port[0],
2169 (const u32 *)ifa, 1);
2170 break;
2171 default:
2172 break;
2173 }
2174 }
2175 return NOTIFY_OK;
2176 }
2177#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302178
Anish Bhattb5a02f52015-01-14 15:17:34 -08002179 if (event_dev)
2180 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302181
Anish Bhattb5a02f52015-01-14 15:17:34 -08002182 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302183 switch (event) {
2184 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002185 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302186 break;
2187 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002188 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302189 break;
2190 default:
2191 break;
2192 }
2193 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002194 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302195}
2196
Anish Bhattb5a02f52015-01-14 15:17:34 -08002197static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302198static struct notifier_block cxgb4_inet6addr_notifier = {
2199 .notifier_call = cxgb4_inet6addr_handler
2200};
2201
Vipul Pandya01bcca62013-07-04 16:10:46 +05302202static void update_clip(const struct adapter *adap)
2203{
2204 int i;
2205 struct net_device *dev;
2206 int ret;
2207
2208 rcu_read_lock();
2209
2210 for (i = 0; i < MAX_NPORTS; i++) {
2211 dev = adap->port[i];
2212 ret = 0;
2213
2214 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002215 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302216
2217 if (ret < 0)
2218 break;
2219 }
2220 rcu_read_unlock();
2221}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002222#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302223
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002224/**
2225 * cxgb_up - enable the adapter
2226 * @adap: adapter being enabled
2227 *
2228 * Called when the first port is enabled, this function performs the
2229 * actions necessary to make an adapter operational, such as completing
2230 * the initialization of HW modules, and enabling interrupts.
2231 *
2232 * Must be called with the rtnl lock held.
2233 */
2234static int cxgb_up(struct adapter *adap)
2235{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002236 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002237
Raju Rangoju91060382017-06-19 17:40:48 +05302238 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002239 err = setup_sge_queues(adap);
2240 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302241 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002242 err = setup_rss(adap);
2243 if (err)
2244 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002245
2246 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002247 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002248 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2249 adap->msix_info[0].desc, adap);
2250 if (err)
2251 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002252 err = request_msix_queue_irqs(adap);
2253 if (err) {
2254 free_irq(adap->msix_info[0].vec, adap);
2255 goto irq_err;
2256 }
2257 } else {
2258 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2259 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002260 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002261 if (err)
2262 goto irq_err;
2263 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302264
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002265 enable_rx(adap);
2266 t4_sge_start(adap);
2267 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002268 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302269 mutex_unlock(&uld_mutex);
2270
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002271 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002272#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302273 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002274#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302275 /* Initialize hash mac addr list*/
2276 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002277 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302278
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002279 irq_err:
2280 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002281 freeq:
2282 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302283 rel_lock:
2284 mutex_unlock(&uld_mutex);
2285 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002286}
2287
2288static void cxgb_down(struct adapter *adapter)
2289{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002290 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302291 cancel_work_sync(&adapter->db_full_task);
2292 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002293 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002294 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002295
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002296 t4_sge_stop(adapter);
2297 t4_free_sge_resources(adapter);
2298 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002299}
2300
2301/*
2302 * net_device operations
2303 */
2304static int cxgb_open(struct net_device *dev)
2305{
2306 int err;
2307 struct port_info *pi = netdev_priv(dev);
2308 struct adapter *adapter = pi->adapter;
2309
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002310 netif_carrier_off(dev);
2311
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002312 if (!(adapter->flags & FULL_INIT_DONE)) {
2313 err = cxgb_up(adapter);
2314 if (err < 0)
2315 return err;
2316 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002317
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302318 /* It's possible that the basic port information could have
2319 * changed since we first read it.
2320 */
2321 err = t4_update_port_info(pi);
2322 if (err < 0)
2323 return err;
2324
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002325 err = link_start(dev);
2326 if (!err)
2327 netif_tx_start_all_queues(dev);
2328 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002329}
2330
2331static int cxgb_close(struct net_device *dev)
2332{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002333 struct port_info *pi = netdev_priv(dev);
2334 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302335 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002336
2337 netif_tx_stop_all_queues(dev);
2338 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302339 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2340#ifdef CONFIG_CHELSIO_T4_DCB
2341 cxgb4_dcb_reset(dev);
2342 dcb_tx_queue_prio_enable(dev, false);
2343#endif
2344 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002345}
2346
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002347int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002348 __be32 sip, __be16 sport, __be16 vlan,
2349 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002350{
2351 int ret;
2352 struct filter_entry *f;
2353 struct adapter *adap;
2354 int i;
2355 u8 *val;
2356
2357 adap = netdev2adap(dev);
2358
Vipul Pandya1cab7752012-12-10 09:30:55 +00002359 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302360 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002361 stid += adap->tids.nftids;
2362
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002363 /* Check to make sure the filter requested is writable ...
2364 */
2365 f = &adap->tids.ftid_tab[stid];
2366 ret = writable_filter(f);
2367 if (ret)
2368 return ret;
2369
2370 /* Clear out any old resources being used by the filter before
2371 * we start constructing the new filter.
2372 */
2373 if (f->valid)
2374 clear_filter(adap, f);
2375
2376 /* Clear out filter specifications */
2377 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2378 f->fs.val.lport = cpu_to_be16(sport);
2379 f->fs.mask.lport = ~0;
2380 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002381 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002382 for (i = 0; i < 4; i++) {
2383 f->fs.val.lip[i] = val[i];
2384 f->fs.mask.lip[i] = ~0;
2385 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302386 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002387 f->fs.val.iport = port;
2388 f->fs.mask.iport = mask;
2389 }
2390 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002391
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302392 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302393 f->fs.val.proto = IPPROTO_TCP;
2394 f->fs.mask.proto = ~0;
2395 }
2396
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002397 f->fs.dirsteer = 1;
2398 f->fs.iq = queue;
2399 /* Mark filter as locked */
2400 f->locked = 1;
2401 f->fs.rpttid = 1;
2402
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302403 /* Save the actual tid. We need this to get the corresponding
2404 * filter entry structure in filter_rpl.
2405 */
2406 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002407 ret = set_filter_wr(adap, stid);
2408 if (ret) {
2409 clear_filter(adap, f);
2410 return ret;
2411 }
2412
2413 return 0;
2414}
2415EXPORT_SYMBOL(cxgb4_create_server_filter);
2416
2417int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2418 unsigned int queue, bool ipv6)
2419{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002420 struct filter_entry *f;
2421 struct adapter *adap;
2422
2423 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002424
2425 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302426 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002427 stid += adap->tids.nftids;
2428
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002429 f = &adap->tids.ftid_tab[stid];
2430 /* Unlock the filter */
2431 f->locked = 0;
2432
Wei Yongjun8c148462016-08-20 15:32:41 +00002433 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002434}
2435EXPORT_SYMBOL(cxgb4_remove_server_filter);
2436
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002437static void cxgb_get_stats(struct net_device *dev,
2438 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002439{
2440 struct port_stats stats;
2441 struct port_info *p = netdev_priv(dev);
2442 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002443
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002444 /* Block retrieving statistics during EEH error
2445 * recovery. Otherwise, the recovery might fail
2446 * and the PCI device will be removed permanently
2447 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002448 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002449 if (!netif_device_present(dev)) {
2450 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002451 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002452 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302453 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2454 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002455 spin_unlock(&adapter->stats_lock);
2456
2457 ns->tx_bytes = stats.tx_octets;
2458 ns->tx_packets = stats.tx_frames;
2459 ns->rx_bytes = stats.rx_octets;
2460 ns->rx_packets = stats.rx_frames;
2461 ns->multicast = stats.rx_mcast_frames;
2462
2463 /* detailed rx_errors */
2464 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2465 stats.rx_runt;
2466 ns->rx_over_errors = 0;
2467 ns->rx_crc_errors = stats.rx_fcs_err;
2468 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302469 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002470 stats.rx_ovflow2 + stats.rx_ovflow3 +
2471 stats.rx_trunc0 + stats.rx_trunc1 +
2472 stats.rx_trunc2 + stats.rx_trunc3;
2473 ns->rx_missed_errors = 0;
2474
2475 /* detailed tx_errors */
2476 ns->tx_aborted_errors = 0;
2477 ns->tx_carrier_errors = 0;
2478 ns->tx_fifo_errors = 0;
2479 ns->tx_heartbeat_errors = 0;
2480 ns->tx_window_errors = 0;
2481
2482 ns->tx_errors = stats.tx_error_frames;
2483 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2484 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002485}
2486
2487static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2488{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002489 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002490 int ret = 0, prtad, devad;
2491 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302492 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002493 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2494
2495 switch (cmd) {
2496 case SIOCGMIIPHY:
2497 if (pi->mdio_addr < 0)
2498 return -EOPNOTSUPP;
2499 data->phy_id = pi->mdio_addr;
2500 break;
2501 case SIOCGMIIREG:
2502 case SIOCSMIIREG:
2503 if (mdio_phy_id_is_c45(data->phy_id)) {
2504 prtad = mdio_phy_id_prtad(data->phy_id);
2505 devad = mdio_phy_id_devad(data->phy_id);
2506 } else if (data->phy_id < 32) {
2507 prtad = data->phy_id;
2508 devad = 0;
2509 data->reg_num &= 0x1f;
2510 } else
2511 return -EINVAL;
2512
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302513 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002514 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002515 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002516 data->reg_num, &data->val_out);
2517 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002518 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002519 data->reg_num, data->val_in);
2520 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302521 case SIOCGHWTSTAMP:
2522 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2523 sizeof(pi->tstamp_config)) ?
2524 -EFAULT : 0;
2525 case SIOCSHWTSTAMP:
2526 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2527 sizeof(pi->tstamp_config)))
2528 return -EFAULT;
2529
Atul Guptaa45695042017-07-04 16:46:20 +05302530 if (!is_t4(adapter->params.chip)) {
2531 switch (pi->tstamp_config.tx_type) {
2532 case HWTSTAMP_TX_OFF:
2533 case HWTSTAMP_TX_ON:
2534 break;
2535 default:
2536 return -ERANGE;
2537 }
2538
2539 switch (pi->tstamp_config.rx_filter) {
2540 case HWTSTAMP_FILTER_NONE:
2541 pi->rxtstamp = false;
2542 break;
2543 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2544 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2545 cxgb4_ptprx_timestamping(pi, pi->port_id,
2546 PTP_TS_L4);
2547 break;
2548 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2549 cxgb4_ptprx_timestamping(pi, pi->port_id,
2550 PTP_TS_L2_L4);
2551 break;
2552 case HWTSTAMP_FILTER_ALL:
2553 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2554 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2555 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2556 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2557 pi->rxtstamp = true;
2558 break;
2559 default:
2560 pi->tstamp_config.rx_filter =
2561 HWTSTAMP_FILTER_NONE;
2562 return -ERANGE;
2563 }
2564
2565 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2566 (pi->tstamp_config.rx_filter ==
2567 HWTSTAMP_FILTER_NONE)) {
2568 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2569 pi->ptp_enable = false;
2570 }
2571
2572 if (pi->tstamp_config.rx_filter !=
2573 HWTSTAMP_FILTER_NONE) {
2574 if (cxgb4_ptp_redirect_rx_packet(adapter,
2575 pi) >= 0)
2576 pi->ptp_enable = true;
2577 }
2578 } else {
2579 /* For T4 Adapters */
2580 switch (pi->tstamp_config.rx_filter) {
2581 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302582 pi->rxtstamp = false;
2583 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302584 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302585 pi->rxtstamp = true;
2586 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302587 default:
2588 pi->tstamp_config.rx_filter =
2589 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302590 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302591 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302592 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302593 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2594 sizeof(pi->tstamp_config)) ?
2595 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002596 default:
2597 return -EOPNOTSUPP;
2598 }
2599 return ret;
2600}
2601
2602static void cxgb_set_rxmode(struct net_device *dev)
2603{
2604 /* unfortunately we can't return errors to the stack */
2605 set_rxmode(dev, -1, false);
2606}
2607
2608static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2609{
2610 int ret;
2611 struct port_info *pi = netdev_priv(dev);
2612
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302613 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002614 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002615 if (!ret)
2616 dev->mtu = new_mtu;
2617 return ret;
2618}
2619
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302620#ifdef CONFIG_PCI_IOV
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302621static int cxgb4_mgmt_open(struct net_device *dev)
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302622{
2623 /* Turn carrier off since we don't have to transmit anything on this
2624 * interface.
2625 */
2626 netif_carrier_off(dev);
2627 return 0;
2628}
2629
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302630/* Fill MAC address that will be assigned by the FW */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302631static void cxgb4_mgmt_fill_vf_station_mac_addr(struct adapter *adap)
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302632{
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302633 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302634 unsigned int i, vf, nvfs;
2635 u16 a, b;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302636 int err;
2637 u8 *na;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302638
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302639 adap->params.pci.vpd_cap_addr = pci_find_capability(adap->pdev,
2640 PCI_CAP_ID_VPD);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302641 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302642 if (err)
2643 return;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302644
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302645 na = adap->params.vpd.na;
2646 for (i = 0; i < ETH_ALEN; i++)
2647 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2648 hex2val(na[2 * i + 1]));
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302649
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302650 a = (hw_addr[0] << 8) | hw_addr[1];
2651 b = (hw_addr[1] << 8) | hw_addr[2];
2652 a ^= b;
2653 a |= 0x0200; /* locally assigned Ethernet MAC address */
2654 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2655 macaddr[0] = a >> 8;
2656 macaddr[1] = a & 0xff;
2657
2658 for (i = 2; i < 5; i++)
2659 macaddr[i] = hw_addr[i + 1];
2660
2661 for (vf = 0, nvfs = pci_sriov_get_totalvfs(adap->pdev);
2662 vf < nvfs; vf++) {
2663 macaddr[5] = adap->pf * 16 + vf;
2664 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, macaddr);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302665 }
2666}
2667
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302668static int cxgb4_mgmt_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302669{
2670 struct port_info *pi = netdev_priv(dev);
2671 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302672 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302673
2674 /* verify MAC addr is valid */
2675 if (!is_valid_ether_addr(mac)) {
2676 dev_err(pi->adapter->pdev_dev,
2677 "Invalid Ethernet address %pM for VF %d\n",
2678 mac, vf);
2679 return -EINVAL;
2680 }
2681
2682 dev_info(pi->adapter->pdev_dev,
2683 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302684 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2685 if (!ret)
2686 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2687 return ret;
2688}
2689
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302690static int cxgb4_mgmt_get_vf_config(struct net_device *dev,
2691 int vf, struct ifla_vf_info *ivi)
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302692{
2693 struct port_info *pi = netdev_priv(dev);
2694 struct adapter *adap = pi->adapter;
2695
2696 if (vf >= adap->num_vfs)
2697 return -EINVAL;
2698 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302699 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2700 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302701 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2702 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302703}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302704
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302705static int cxgb4_mgmt_get_phys_port_id(struct net_device *dev,
2706 struct netdev_phys_item_id *ppid)
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302707{
2708 struct port_info *pi = netdev_priv(dev);
2709 unsigned int phy_port_id;
2710
2711 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2712 ppid->id_len = sizeof(phy_port_id);
2713 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2714 return 0;
2715}
2716
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302717static int cxgb4_mgmt_set_vf_rate(struct net_device *dev, int vf,
2718 int min_tx_rate, int max_tx_rate)
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302719{
2720 struct port_info *pi = netdev_priv(dev);
2721 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302722 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302723 u32 fw_pfvf, fw_class;
2724 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302725 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302726 u16 pktsize;
2727
2728 if (vf >= adap->num_vfs)
2729 return -EINVAL;
2730
2731 if (min_tx_rate) {
2732 dev_err(adap->pdev_dev,
2733 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2734 min_tx_rate, vf);
2735 return -EINVAL;
2736 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302737
2738 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302739 if (ret != FW_SUCCESS) {
2740 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302741 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302742 return -EINVAL;
2743 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302744
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302745 if (!link_ok) {
2746 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2747 return -EINVAL;
2748 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302749
2750 if (max_tx_rate > speed) {
2751 dev_err(adap->pdev_dev,
2752 "Max tx rate %d for VF %d can't be > link-speed %u",
2753 max_tx_rate, vf, speed);
2754 return -EINVAL;
2755 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302756
2757 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302758 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2759 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2760 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2761 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2762 /* configure Traffic Class for rate-limiting */
2763 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2764 SCHED_CLASS_LEVEL_CL_RL,
2765 SCHED_CLASS_MODE_CLASS,
2766 SCHED_CLASS_RATEUNIT_BITS,
2767 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302768 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302769 max_tx_rate * 1000, 0, pktsize);
2770 if (ret) {
2771 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2772 ret);
2773 return -EINVAL;
2774 }
2775 dev_info(adap->pdev_dev,
2776 "Class %d with MSS %u configured with rate %u\n",
2777 class_id, pktsize, max_tx_rate);
2778
2779 /* bind VF to configured Traffic Class */
2780 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2781 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2782 fw_class = class_id;
2783 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2784 &fw_class);
2785 if (ret) {
2786 dev_err(adap->pdev_dev,
2787 "Err %d in binding VF %d to Traffic Class %d\n",
2788 ret, vf, class_id);
2789 return -EINVAL;
2790 }
2791 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2792 adap->pf, vf, class_id);
2793 adap->vfinfo[vf].tx_rate = max_tx_rate;
2794 return 0;
2795}
2796
Ganesh Goudar9d5fd922018-01-24 20:44:07 +05302797static int cxgb4_mgmt_set_vf_vlan(struct net_device *dev, int vf,
2798 u16 vlan, u8 qos, __be16 vlan_proto)
2799{
2800 struct port_info *pi = netdev_priv(dev);
2801 struct adapter *adap = pi->adapter;
2802 int ret;
2803
2804 if (vf >= adap->num_vfs || vlan > 4095 || qos > 7)
2805 return -EINVAL;
2806
2807 if (vlan_proto != htons(ETH_P_8021Q) || qos != 0)
2808 return -EPROTONOSUPPORT;
2809
2810 ret = t4_set_vlan_acl(adap, adap->mbox, vf + 1, vlan);
2811 if (!ret) {
2812 adap->vfinfo[vf].vlan = vlan;
2813 return 0;
2814 }
2815
2816 dev_err(adap->pdev_dev, "Err %d %s VLAN ACL for PF/VF %d/%d\n",
2817 ret, (vlan ? "setting" : "clearing"), adap->pf, vf);
2818 return ret;
2819}
2820#endif /* CONFIG_PCI_IOV */
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302821
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002822static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2823{
2824 int ret;
2825 struct sockaddr *addr = p;
2826 struct port_info *pi = netdev_priv(dev);
2827
2828 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002829 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002830
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302831 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002832 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002833 if (ret < 0)
2834 return ret;
2835
2836 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2837 pi->xact_addr_filt = ret;
2838 return 0;
2839}
2840
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002841#ifdef CONFIG_NET_POLL_CONTROLLER
2842static void cxgb_netpoll(struct net_device *dev)
2843{
2844 struct port_info *pi = netdev_priv(dev);
2845 struct adapter *adap = pi->adapter;
2846
2847 if (adap->flags & USING_MSIX) {
2848 int i;
2849 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2850
2851 for (i = pi->nqsets; i; i--, rx++)
2852 t4_sge_intr_msix(0, &rx->rspq);
2853 } else
2854 t4_intr_handler(adap)(0, adap);
2855}
2856#endif
2857
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302858static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2859{
2860 struct port_info *pi = netdev_priv(dev);
2861 struct adapter *adap = pi->adapter;
2862 struct sched_class *e;
2863 struct ch_sched_params p;
2864 struct ch_sched_queue qe;
2865 u32 req_rate;
2866 int err = 0;
2867
2868 if (!can_sched(dev))
2869 return -ENOTSUPP;
2870
2871 if (index < 0 || index > pi->nqsets - 1)
2872 return -EINVAL;
2873
2874 if (!(adap->flags & FULL_INIT_DONE)) {
2875 dev_err(adap->pdev_dev,
2876 "Failed to rate limit on queue %d. Link Down?\n",
2877 index);
2878 return -EINVAL;
2879 }
2880
2881 /* Convert from Mbps to Kbps */
2882 req_rate = rate << 10;
2883
Ganesh Goudard185efc2018-03-09 13:00:52 +05302884 /* Max rate is 100 Gbps */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302885 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2886 dev_err(adap->pdev_dev,
Ganesh Goudard185efc2018-03-09 13:00:52 +05302887 "Invalid rate %u Mbps, Max rate is %u Mbps\n",
2888 rate, SCHED_MAX_RATE_KBPS >> 10);
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302889 return -ERANGE;
2890 }
2891
2892 /* First unbind the queue from any existing class */
2893 memset(&qe, 0, sizeof(qe));
2894 qe.queue = index;
2895 qe.class = SCHED_CLS_NONE;
2896
2897 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2898 if (err) {
2899 dev_err(adap->pdev_dev,
2900 "Unbinding Queue %d on port %d fail. Err: %d\n",
2901 index, pi->port_id, err);
2902 return err;
2903 }
2904
2905 /* Queue already unbound */
2906 if (!req_rate)
2907 return 0;
2908
2909 /* Fetch any available unused or matching scheduling class */
2910 memset(&p, 0, sizeof(p));
2911 p.type = SCHED_CLASS_TYPE_PACKET;
2912 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2913 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2914 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2915 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2916 p.u.params.channel = pi->tx_chan;
2917 p.u.params.class = SCHED_CLS_NONE;
2918 p.u.params.minrate = 0;
2919 p.u.params.maxrate = req_rate;
2920 p.u.params.weight = 0;
2921 p.u.params.pktsize = dev->mtu;
2922
2923 e = cxgb4_sched_class_alloc(dev, &p);
2924 if (!e)
2925 return -ENOMEM;
2926
2927 /* Bind the queue to a scheduling class */
2928 memset(&qe, 0, sizeof(qe));
2929 qe.queue = index;
2930 qe.class = e->idx;
2931
2932 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2933 if (err)
2934 dev_err(adap->pdev_dev,
2935 "Queue rate limiting failed. Err: %d\n", err);
2936 return err;
2937}
2938
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302939static int cxgb_setup_tc_flower(struct net_device *dev,
2940 struct tc_cls_flower_offload *cls_flower)
2941{
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302942 switch (cls_flower->command) {
2943 case TC_CLSFLOWER_REPLACE:
2944 return cxgb4_tc_flower_replace(dev, cls_flower);
2945 case TC_CLSFLOWER_DESTROY:
2946 return cxgb4_tc_flower_destroy(dev, cls_flower);
2947 case TC_CLSFLOWER_STATS:
2948 return cxgb4_tc_flower_stats(dev, cls_flower);
2949 default:
2950 return -EOPNOTSUPP;
2951 }
2952}
2953
Jiri Pirkof7323042017-08-07 10:15:20 +02002954static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002955 struct tc_cls_u32_offload *cls_u32)
2956{
Jiri Pirkof7323042017-08-07 10:15:20 +02002957 switch (cls_u32->command) {
2958 case TC_CLSU32_NEW_KNODE:
2959 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002960 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002961 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002962 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002963 default:
2964 return -EOPNOTSUPP;
2965 }
2966}
2967
Jiri Pirkocd019e92017-10-19 15:50:40 +02002968static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2969 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302970{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002971 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302972 struct port_info *pi = netdev2pinfo(dev);
2973 struct adapter *adap = netdev2adap(dev);
2974
2975 if (!(adap->flags & FULL_INIT_DONE)) {
2976 dev_err(adap->pdev_dev,
2977 "Failed to setup tc on port %d. Link Down?\n",
2978 pi->port_id);
2979 return -EINVAL;
2980 }
2981
Jakub Kicinski2a84bba2018-01-25 14:00:46 -08002982 if (!tc_cls_can_offload_and_chain0(dev, type_data))
Jiri Pirko44ae12a2017-11-01 11:47:39 +01002983 return -EOPNOTSUPP;
2984
Jiri Pirkof7323042017-08-07 10:15:20 +02002985 switch (type) {
2986 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002987 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302988 case TC_SETUP_CLSFLOWER:
2989 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002990 default:
2991 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302992 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302993}
2994
Jiri Pirkocd019e92017-10-19 15:50:40 +02002995static int cxgb_setup_tc_block(struct net_device *dev,
2996 struct tc_block_offload *f)
2997{
2998 struct port_info *pi = netdev2pinfo(dev);
2999
3000 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3001 return -EOPNOTSUPP;
3002
3003 switch (f->command) {
3004 case TC_BLOCK_BIND:
3005 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
3006 pi, dev);
3007 case TC_BLOCK_UNBIND:
3008 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
3009 return 0;
3010 default:
3011 return -EOPNOTSUPP;
3012 }
3013}
3014
3015static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
3016 void *type_data)
3017{
3018 switch (type) {
Jiri Pirkocd019e92017-10-19 15:50:40 +02003019 case TC_SETUP_BLOCK:
3020 return cxgb_setup_tc_block(dev, type_data);
3021 default:
3022 return -EOPNOTSUPP;
3023 }
3024}
3025
Ganesh Goudar846eac32018-01-10 18:15:08 +05303026static void cxgb_del_udp_tunnel(struct net_device *netdev,
3027 struct udp_tunnel_info *ti)
3028{
3029 struct port_info *pi = netdev_priv(netdev);
3030 struct adapter *adapter = pi->adapter;
3031 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3032 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3033 int ret = 0, i;
3034
3035 if (chip_ver < CHELSIO_T6)
3036 return;
3037
3038 switch (ti->type) {
3039 case UDP_TUNNEL_TYPE_VXLAN:
3040 if (!adapter->vxlan_port_cnt ||
3041 adapter->vxlan_port != ti->port)
3042 return; /* Invalid VxLAN destination port */
3043
3044 adapter->vxlan_port_cnt--;
3045 if (adapter->vxlan_port_cnt)
3046 return;
3047
3048 adapter->vxlan_port = 0;
3049 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A, 0);
3050 break;
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303051 case UDP_TUNNEL_TYPE_GENEVE:
3052 if (!adapter->geneve_port_cnt ||
3053 adapter->geneve_port != ti->port)
3054 return; /* Invalid GENEVE destination port */
3055
3056 adapter->geneve_port_cnt--;
3057 if (adapter->geneve_port_cnt)
3058 return;
3059
3060 adapter->geneve_port = 0;
3061 t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A, 0);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303062 default:
3063 return;
3064 }
3065
3066 /* Matchall mac entries can be deleted only after all tunnel ports
3067 * are brought down or removed.
3068 */
3069 if (!adapter->rawf_cnt)
3070 return;
3071 for_each_port(adapter, i) {
3072 pi = adap2pinfo(adapter, i);
3073 ret = t4_free_raw_mac_filt(adapter, pi->viid,
3074 match_all_mac, match_all_mac,
3075 adapter->rawf_start +
3076 pi->port_id,
3077 1, pi->port_id, true);
3078 if (ret < 0) {
3079 netdev_info(netdev, "Failed to free mac filter entry, for port %d\n",
3080 i);
3081 return;
3082 }
3083 atomic_dec(&adapter->mps_encap[adapter->rawf_start +
3084 pi->port_id].refcnt);
3085 }
3086}
3087
3088static void cxgb_add_udp_tunnel(struct net_device *netdev,
3089 struct udp_tunnel_info *ti)
3090{
3091 struct port_info *pi = netdev_priv(netdev);
3092 struct adapter *adapter = pi->adapter;
3093 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3094 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3095 int i, ret;
3096
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303097 if (chip_ver < CHELSIO_T6 || !adapter->rawf_cnt)
Ganesh Goudar846eac32018-01-10 18:15:08 +05303098 return;
3099
3100 switch (ti->type) {
3101 case UDP_TUNNEL_TYPE_VXLAN:
Ganesh Goudar846eac32018-01-10 18:15:08 +05303102 /* Callback for adding vxlan port can be called with the same
3103 * port for both IPv4 and IPv6. We should not disable the
3104 * offloading when the same port for both protocols is added
3105 * and later one of them is removed.
3106 */
3107 if (adapter->vxlan_port_cnt &&
3108 adapter->vxlan_port == ti->port) {
3109 adapter->vxlan_port_cnt++;
3110 return;
3111 }
3112
3113 /* We will support only one VxLAN port */
3114 if (adapter->vxlan_port_cnt) {
3115 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3116 be16_to_cpu(adapter->vxlan_port),
3117 be16_to_cpu(ti->port));
3118 return;
3119 }
3120
3121 adapter->vxlan_port = ti->port;
3122 adapter->vxlan_port_cnt = 1;
3123
3124 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A,
3125 VXLAN_V(be16_to_cpu(ti->port)) | VXLAN_EN_F);
3126 break;
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303127 case UDP_TUNNEL_TYPE_GENEVE:
3128 if (adapter->geneve_port_cnt &&
3129 adapter->geneve_port == ti->port) {
3130 adapter->geneve_port_cnt++;
3131 return;
3132 }
3133
3134 /* We will support only one GENEVE port */
3135 if (adapter->geneve_port_cnt) {
3136 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3137 be16_to_cpu(adapter->geneve_port),
3138 be16_to_cpu(ti->port));
3139 return;
3140 }
3141
3142 adapter->geneve_port = ti->port;
3143 adapter->geneve_port_cnt = 1;
3144
3145 t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A,
3146 GENEVE_V(be16_to_cpu(ti->port)) | GENEVE_EN_F);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303147 default:
3148 return;
3149 }
3150
3151 /* Create a 'match all' mac filter entry for inner mac,
3152 * if raw mac interface is supported. Once the linux kernel provides
3153 * driver entry points for adding/deleting the inner mac addresses,
3154 * we will remove this 'match all' entry and fallback to adding
3155 * exact match filters.
3156 */
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303157 for_each_port(adapter, i) {
3158 pi = adap2pinfo(adapter, i);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303159
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303160 ret = t4_alloc_raw_mac_filt(adapter, pi->viid,
3161 match_all_mac,
3162 match_all_mac,
3163 adapter->rawf_start +
3164 pi->port_id,
3165 1, pi->port_id, true);
3166 if (ret < 0) {
3167 netdev_info(netdev, "Failed to allocate a mac filter entry, not adding port %d\n",
3168 be16_to_cpu(ti->port));
3169 cxgb_del_udp_tunnel(netdev, ti);
3170 return;
Ganesh Goudar846eac32018-01-10 18:15:08 +05303171 }
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303172 atomic_inc(&adapter->mps_encap[ret].refcnt);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303173 }
3174}
3175
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303176static netdev_features_t cxgb_features_check(struct sk_buff *skb,
3177 struct net_device *dev,
3178 netdev_features_t features)
3179{
3180 struct port_info *pi = netdev_priv(dev);
3181 struct adapter *adapter = pi->adapter;
3182
3183 if (CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3184 return features;
3185
3186 /* Check if hw supports offload for this packet */
3187 if (!skb->encapsulation || cxgb_encap_offload_supported(skb))
3188 return features;
3189
3190 /* Offload is not supported for this encapsulated packet */
3191 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3192}
3193
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303194static netdev_features_t cxgb_fix_features(struct net_device *dev,
3195 netdev_features_t features)
3196{
3197 /* Disable GRO, if RX_CSUM is disabled */
3198 if (!(features & NETIF_F_RXCSUM))
3199 features &= ~NETIF_F_GRO;
3200
3201 return features;
3202}
3203
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003204static const struct net_device_ops cxgb4_netdev_ops = {
3205 .ndo_open = cxgb_open,
3206 .ndo_stop = cxgb_close,
3207 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003208 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003209 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003210 .ndo_set_rx_mode = cxgb_set_rxmode,
3211 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003212 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003213 .ndo_validate_addr = eth_validate_addr,
3214 .ndo_do_ioctl = cxgb_ioctl,
3215 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003216#ifdef CONFIG_NET_POLL_CONTROLLER
3217 .ndo_poll_controller = cxgb_netpoll,
3218#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303219#ifdef CONFIG_CHELSIO_T4_FCOE
3220 .ndo_fcoe_enable = cxgb_fcoe_enable,
3221 .ndo_fcoe_disable = cxgb_fcoe_disable,
3222#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303223 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303224 .ndo_setup_tc = cxgb_setup_tc,
Ganesh Goudar846eac32018-01-10 18:15:08 +05303225 .ndo_udp_tunnel_add = cxgb_add_udp_tunnel,
3226 .ndo_udp_tunnel_del = cxgb_del_udp_tunnel,
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303227 .ndo_features_check = cxgb_features_check,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303228 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003229};
3230
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303231#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303232static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303233 .ndo_open = cxgb4_mgmt_open,
3234 .ndo_set_vf_mac = cxgb4_mgmt_set_vf_mac,
3235 .ndo_get_vf_config = cxgb4_mgmt_get_vf_config,
3236 .ndo_set_vf_rate = cxgb4_mgmt_set_vf_rate,
3237 .ndo_get_phys_port_id = cxgb4_mgmt_get_phys_port_id,
Ganesh Goudar9d5fd922018-01-24 20:44:07 +05303238 .ndo_set_vf_vlan = cxgb4_mgmt_set_vf_vlan,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303239};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303240#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303241
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303242static void cxgb4_mgmt_get_drvinfo(struct net_device *dev,
3243 struct ethtool_drvinfo *info)
Hariprasad Shenai78294512016-08-11 21:06:23 +05303244{
3245 struct adapter *adapter = netdev2adap(dev);
3246
3247 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3248 strlcpy(info->version, cxgb4_driver_version,
3249 sizeof(info->version));
3250 strlcpy(info->bus_info, pci_name(adapter->pdev),
3251 sizeof(info->bus_info));
3252}
3253
3254static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303255 .get_drvinfo = cxgb4_mgmt_get_drvinfo,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303256};
3257
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003258void t4_fatal_err(struct adapter *adap)
3259{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303260 int port;
3261
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003262 if (pci_channel_offline(adap->pdev))
3263 return;
3264
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303265 /* Disable the SGE since ULDs are going to free resources that
3266 * could be exposed to the adapter. RDMA MWs for example...
3267 */
3268 t4_shutdown_adapter(adap);
3269 for_each_port(adap, port) {
3270 struct net_device *dev = adap->port[port];
3271
3272 /* If we get here in very early initialization the network
3273 * devices may not have been set up yet.
3274 */
3275 if (!dev)
3276 continue;
3277
3278 netif_tx_stop_all_queues(dev);
3279 netif_carrier_off(dev);
3280 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003281 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3282}
3283
3284static void setup_memwin(struct adapter *adap)
3285{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303286 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003287
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303288 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003289}
3290
3291static void setup_memwin_rdma(struct adapter *adap)
3292{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003293 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303294 u32 start;
3295 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003296
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303297 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3298 start &= PCI_BASE_ADDRESS_MEM_MASK;
3299 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003300 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3301 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303302 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3303 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003304 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303305 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003306 adap->vres.ocq.start);
3307 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303308 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003309 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003310}
3311
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05303312/* HMA Definitions */
3313
3314/* The maximum number of address that can be send in a single FW cmd */
3315#define HMA_MAX_ADDR_IN_CMD 5
3316
3317#define HMA_PAGE_SIZE PAGE_SIZE
3318
3319#define HMA_MAX_NO_FW_ADDRESS (16 << 10) /* FW supports 16K addresses */
3320
3321#define HMA_PAGE_ORDER \
3322 ((HMA_PAGE_SIZE < HMA_MAX_NO_FW_ADDRESS) ? \
3323 ilog2(HMA_MAX_NO_FW_ADDRESS / HMA_PAGE_SIZE) : 0)
3324
3325/* The minimum and maximum possible HMA sizes that can be specified in the FW
3326 * configuration(in units of MB).
3327 */
3328#define HMA_MIN_TOTAL_SIZE 1
3329#define HMA_MAX_TOTAL_SIZE \
3330 (((HMA_PAGE_SIZE << HMA_PAGE_ORDER) * \
3331 HMA_MAX_NO_FW_ADDRESS) >> 20)
3332
3333static void adap_free_hma_mem(struct adapter *adapter)
3334{
3335 struct scatterlist *iter;
3336 struct page *page;
3337 int i;
3338
3339 if (!adapter->hma.sgt)
3340 return;
3341
3342 if (adapter->hma.flags & HMA_DMA_MAPPED_FLAG) {
3343 dma_unmap_sg(adapter->pdev_dev, adapter->hma.sgt->sgl,
3344 adapter->hma.sgt->nents, PCI_DMA_BIDIRECTIONAL);
3345 adapter->hma.flags &= ~HMA_DMA_MAPPED_FLAG;
3346 }
3347
3348 for_each_sg(adapter->hma.sgt->sgl, iter,
3349 adapter->hma.sgt->orig_nents, i) {
3350 page = sg_page(iter);
3351 if (page)
3352 __free_pages(page, HMA_PAGE_ORDER);
3353 }
3354
3355 kfree(adapter->hma.phy_addr);
3356 sg_free_table(adapter->hma.sgt);
3357 kfree(adapter->hma.sgt);
3358 adapter->hma.sgt = NULL;
3359}
3360
3361static int adap_config_hma(struct adapter *adapter)
3362{
3363 struct scatterlist *sgl, *iter;
3364 struct sg_table *sgt;
3365 struct page *newpage;
3366 unsigned int i, j, k;
3367 u32 param, hma_size;
3368 unsigned int ncmds;
3369 size_t page_size;
3370 u32 page_order;
3371 int node, ret;
3372
3373 /* HMA is supported only for T6+ cards.
3374 * Avoid initializing HMA in kdump kernels.
3375 */
3376 if (is_kdump_kernel() ||
3377 CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3378 return 0;
3379
3380 /* Get the HMA region size required by fw */
3381 param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3382 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_HMA_SIZE));
3383 ret = t4_query_params(adapter, adapter->mbox, adapter->pf, 0,
3384 1, &param, &hma_size);
3385 /* An error means card has its own memory or HMA is not supported by
3386 * the firmware. Return without any errors.
3387 */
3388 if (ret || !hma_size)
3389 return 0;
3390
3391 if (hma_size < HMA_MIN_TOTAL_SIZE ||
3392 hma_size > HMA_MAX_TOTAL_SIZE) {
3393 dev_err(adapter->pdev_dev,
3394 "HMA size %uMB beyond bounds(%u-%lu)MB\n",
3395 hma_size, HMA_MIN_TOTAL_SIZE, HMA_MAX_TOTAL_SIZE);
3396 return -EINVAL;
3397 }
3398
3399 page_size = HMA_PAGE_SIZE;
3400 page_order = HMA_PAGE_ORDER;
3401 adapter->hma.sgt = kzalloc(sizeof(*adapter->hma.sgt), GFP_KERNEL);
3402 if (unlikely(!adapter->hma.sgt)) {
3403 dev_err(adapter->pdev_dev, "HMA SG table allocation failed\n");
3404 return -ENOMEM;
3405 }
3406 sgt = adapter->hma.sgt;
3407 /* FW returned value will be in MB's
3408 */
3409 sgt->orig_nents = (hma_size << 20) / (page_size << page_order);
3410 if (sg_alloc_table(sgt, sgt->orig_nents, GFP_KERNEL)) {
3411 dev_err(adapter->pdev_dev, "HMA SGL allocation failed\n");
3412 kfree(adapter->hma.sgt);
3413 adapter->hma.sgt = NULL;
3414 return -ENOMEM;
3415 }
3416
3417 sgl = adapter->hma.sgt->sgl;
3418 node = dev_to_node(adapter->pdev_dev);
3419 for_each_sg(sgl, iter, sgt->orig_nents, i) {
3420 newpage = alloc_pages_node(node, __GFP_NOWARN | GFP_KERNEL,
3421 page_order);
3422 if (!newpage) {
3423 dev_err(adapter->pdev_dev,
3424 "Not enough memory for HMA page allocation\n");
3425 ret = -ENOMEM;
3426 goto free_hma;
3427 }
3428 sg_set_page(iter, newpage, page_size << page_order, 0);
3429 }
3430
3431 sgt->nents = dma_map_sg(adapter->pdev_dev, sgl, sgt->orig_nents,
3432 DMA_BIDIRECTIONAL);
3433 if (!sgt->nents) {
3434 dev_err(adapter->pdev_dev,
3435 "Not enough memory for HMA DMA mapping");
3436 ret = -ENOMEM;
3437 goto free_hma;
3438 }
3439 adapter->hma.flags |= HMA_DMA_MAPPED_FLAG;
3440
3441 adapter->hma.phy_addr = kcalloc(sgt->nents, sizeof(dma_addr_t),
3442 GFP_KERNEL);
3443 if (unlikely(!adapter->hma.phy_addr))
3444 goto free_hma;
3445
3446 for_each_sg(sgl, iter, sgt->nents, i) {
3447 newpage = sg_page(iter);
3448 adapter->hma.phy_addr[i] = sg_dma_address(iter);
3449 }
3450
3451 ncmds = DIV_ROUND_UP(sgt->nents, HMA_MAX_ADDR_IN_CMD);
3452 /* Pass on the addresses to firmware */
3453 for (i = 0, k = 0; i < ncmds; i++, k += HMA_MAX_ADDR_IN_CMD) {
3454 struct fw_hma_cmd hma_cmd;
3455 u8 naddr = HMA_MAX_ADDR_IN_CMD;
3456 u8 soc = 0, eoc = 0;
3457 u8 hma_mode = 1; /* Presently we support only Page table mode */
3458
3459 soc = (i == 0) ? 1 : 0;
3460 eoc = (i == ncmds - 1) ? 1 : 0;
3461
3462 /* For last cmd, set naddr corresponding to remaining
3463 * addresses
3464 */
3465 if (i == ncmds - 1) {
3466 naddr = sgt->nents % HMA_MAX_ADDR_IN_CMD;
3467 naddr = naddr ? naddr : HMA_MAX_ADDR_IN_CMD;
3468 }
3469 memset(&hma_cmd, 0, sizeof(hma_cmd));
3470 hma_cmd.op_pkd = htonl(FW_CMD_OP_V(FW_HMA_CMD) |
3471 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
3472 hma_cmd.retval_len16 = htonl(FW_LEN16(hma_cmd));
3473
3474 hma_cmd.mode_to_pcie_params =
3475 htonl(FW_HMA_CMD_MODE_V(hma_mode) |
3476 FW_HMA_CMD_SOC_V(soc) | FW_HMA_CMD_EOC_V(eoc));
3477
3478 /* HMA cmd size specified in MB's */
3479 hma_cmd.naddr_size =
3480 htonl(FW_HMA_CMD_SIZE_V(hma_size) |
3481 FW_HMA_CMD_NADDR_V(naddr));
3482
3483 /* Total Page size specified in units of 4K */
3484 hma_cmd.addr_size_pkd =
3485 htonl(FW_HMA_CMD_ADDR_SIZE_V
3486 ((page_size << page_order) >> 12));
3487
3488 /* Fill the 5 addresses */
3489 for (j = 0; j < naddr; j++) {
3490 hma_cmd.phy_address[j] =
3491 cpu_to_be64(adapter->hma.phy_addr[j + k]);
3492 }
3493 ret = t4_wr_mbox(adapter, adapter->mbox, &hma_cmd,
3494 sizeof(hma_cmd), &hma_cmd);
3495 if (ret) {
3496 dev_err(adapter->pdev_dev,
3497 "HMA FW command failed with err %d\n", ret);
3498 goto free_hma;
3499 }
3500 }
3501
3502 if (!ret)
3503 dev_info(adapter->pdev_dev,
3504 "Reserved %uMB host memory for HMA\n", hma_size);
3505 return ret;
3506
3507free_hma:
3508 adap_free_hma_mem(adapter);
3509 return ret;
3510}
3511
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003512static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3513{
3514 u32 v;
3515 int ret;
3516
3517 /* get device capabilities */
3518 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303519 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3520 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303521 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303522 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003523 if (ret < 0)
3524 return ret;
3525
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303526 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3527 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303528 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003529 if (ret < 0)
3530 return ret;
3531
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303532 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003533 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303534 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3535 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003536 if (ret < 0)
3537 return ret;
3538
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303539 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303540 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3541 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003542 if (ret < 0)
3543 return ret;
3544
3545 t4_sge_init(adap);
3546
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003547 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303548 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303549 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303550 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3551 v = t4_read_reg(adap, TP_PIO_DATA_A);
3552 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003553
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003554 /* first 4 Tx modulation queues point to consecutive Tx channels */
3555 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303556 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3557 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003558
3559 /* associate each Tx modulation queue with consecutive Tx channels */
3560 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303561 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303562 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303563 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303564 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303565 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303566 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003567
3568#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3569 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303570 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3571 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3572 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3573 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3574 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3575 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3576 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3577 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3578 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3579 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003580 }
3581
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003582 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303583 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003584}
3585
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003586/*
3587 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3588 */
3589#define MAX_ATIDS 8192U
3590
3591/*
3592 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003593 *
3594 * If the firmware we're dealing with has Configuration File support, then
3595 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003596 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003597
3598/*
3599 * Tweak configuration based on module parameters, etc. Most of these have
3600 * defaults assigned to them by Firmware Configuration Files (if we're using
3601 * them) but need to be explicitly set if we're using hard-coded
3602 * initialization. But even in the case of using Firmware Configuration
3603 * Files, we'd like to expose the ability to change these via module
3604 * parameters so these are essentially common tweaks/settings for
3605 * Configuration Files and hard-coded initialization ...
3606 */
3607static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003608{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003609 /*
3610 * Fix up various Host-Dependent Parameters like Page Size, Cache
3611 * Line Size, etc. The firmware default is for a 4KB Page Size and
3612 * 64B Cache Line Size ...
3613 */
3614 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003615
Vipul Pandya636f9d32012-09-26 02:39:39 +00003616 /*
3617 * Process module parameters which affect early initialization.
3618 */
3619 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3620 dev_err(&adapter->pdev->dev,
3621 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3622 rx_dma_offset);
3623 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003624 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303625 t4_set_reg_field(adapter, SGE_CONTROL_A,
3626 PKTSHIFT_V(PKTSHIFT_M),
3627 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003628
Vipul Pandya636f9d32012-09-26 02:39:39 +00003629 /*
3630 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3631 * adds the pseudo header itself.
3632 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303633 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3634 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003635
3636 return 0;
3637}
3638
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303639/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3640 * unto themselves and they contain their own firmware to perform their
3641 * tasks ...
3642 */
3643static int phy_aq1202_version(const u8 *phy_fw_data,
3644 size_t phy_fw_size)
3645{
3646 int offset;
3647
3648 /* At offset 0x8 you're looking for the primary image's
3649 * starting offset which is 3 Bytes wide
3650 *
3651 * At offset 0xa of the primary image, you look for the offset
3652 * of the DRAM segment which is 3 Bytes wide.
3653 *
3654 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3655 * wide
3656 */
3657 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3658 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3659 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3660
3661 offset = le24(phy_fw_data + 0x8) << 12;
3662 offset = le24(phy_fw_data + offset + 0xa);
3663 return be16(phy_fw_data + offset + 0x27e);
3664
3665 #undef be16
3666 #undef le16
3667 #undef le24
3668}
3669
3670static struct info_10gbt_phy_fw {
3671 unsigned int phy_fw_id; /* PCI Device ID */
3672 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3673 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3674 int phy_flash; /* Has FLASH for PHY Firmware */
3675} phy_info_array[] = {
3676 {
3677 PHY_AQ1202_DEVICEID,
3678 PHY_AQ1202_FIRMWARE,
3679 phy_aq1202_version,
3680 1,
3681 },
3682 {
3683 PHY_BCM84834_DEVICEID,
3684 PHY_BCM84834_FIRMWARE,
3685 NULL,
3686 0,
3687 },
3688 { 0, NULL, NULL },
3689};
3690
3691static struct info_10gbt_phy_fw *find_phy_info(int devid)
3692{
3693 int i;
3694
3695 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3696 if (phy_info_array[i].phy_fw_id == devid)
3697 return &phy_info_array[i];
3698 }
3699 return NULL;
3700}
3701
3702/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3703 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3704 * we return a negative error number. If we transfer new firmware we return 1
3705 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3706 */
3707static int adap_init0_phy(struct adapter *adap)
3708{
3709 const struct firmware *phyf;
3710 int ret;
3711 struct info_10gbt_phy_fw *phy_info;
3712
3713 /* Use the device ID to determine which PHY file to flash.
3714 */
3715 phy_info = find_phy_info(adap->pdev->device);
3716 if (!phy_info) {
3717 dev_warn(adap->pdev_dev,
3718 "No PHY Firmware file found for this PHY\n");
3719 return -EOPNOTSUPP;
3720 }
3721
3722 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3723 * use that. The adapter firmware provides us with a memory buffer
3724 * where we can load a PHY firmware file from the host if we want to
3725 * override the PHY firmware File in flash.
3726 */
3727 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3728 adap->pdev_dev);
3729 if (ret < 0) {
3730 /* For adapters without FLASH attached to PHY for their
3731 * firmware, it's obviously a fatal error if we can't get the
3732 * firmware to the adapter. For adapters with PHY firmware
3733 * FLASH storage, it's worth a warning if we can't find the
3734 * PHY Firmware but we'll neuter the error ...
3735 */
3736 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3737 "/lib/firmware/%s, error %d\n",
3738 phy_info->phy_fw_file, -ret);
3739 if (phy_info->phy_flash) {
3740 int cur_phy_fw_ver = 0;
3741
3742 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3743 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3744 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3745 ret = 0;
3746 }
3747
3748 return ret;
3749 }
3750
3751 /* Load PHY Firmware onto adapter.
3752 */
3753 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3754 phy_info->phy_fw_version,
3755 (u8 *)phyf->data, phyf->size);
3756 if (ret < 0)
3757 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3758 -ret);
3759 else if (ret > 0) {
3760 int new_phy_fw_ver = 0;
3761
3762 if (phy_info->phy_fw_version)
3763 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3764 phyf->size);
3765 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3766 "Firmware /lib/firmware/%s, version %#x\n",
3767 phy_info->phy_fw_file, new_phy_fw_ver);
3768 }
3769
3770 release_firmware(phyf);
3771
3772 return ret;
3773}
3774
Vipul Pandya636f9d32012-09-26 02:39:39 +00003775/*
3776 * Attempt to initialize the adapter via a Firmware Configuration File.
3777 */
3778static int adap_init0_config(struct adapter *adapter, int reset)
3779{
3780 struct fw_caps_config_cmd caps_cmd;
3781 const struct firmware *cf;
3782 unsigned long mtype = 0, maddr = 0;
3783 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303784 int ret;
3785 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003786 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303787 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003788
3789 /*
3790 * Reset device if necessary.
3791 */
3792 if (reset) {
3793 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303794 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003795 if (ret < 0)
3796 goto bye;
3797 }
3798
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303799 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3800 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3801 * to be performed after any global adapter RESET above since some
3802 * PHYs only have local RAM copies of the PHY firmware.
3803 */
3804 if (is_10gbt_device(adapter->pdev->device)) {
3805 ret = adap_init0_phy(adapter);
3806 if (ret < 0)
3807 goto bye;
3808 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003809 /*
3810 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3811 * then use that. Otherwise, use the configuration file stored
3812 * in the adapter flash ...
3813 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303814 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003815 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303816 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003817 break;
3818 case CHELSIO_T5:
3819 fw_config_file = FW5_CFNAME;
3820 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303821 case CHELSIO_T6:
3822 fw_config_file = FW6_CFNAME;
3823 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003824 default:
3825 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3826 adapter->pdev->device);
3827 ret = -EINVAL;
3828 goto bye;
3829 }
3830
3831 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003832 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303833 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003834 mtype = FW_MEMTYPE_CF_FLASH;
3835 maddr = t4_flash_cfg_addr(adapter);
3836 } else {
3837 u32 params[7], val[7];
3838
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303839 sprintf(fw_config_file_path,
3840 "/lib/firmware/%s", fw_config_file);
3841 config_name = fw_config_file_path;
3842
Vipul Pandya636f9d32012-09-26 02:39:39 +00003843 if (cf->size >= FLASH_CFG_MAX_SIZE)
3844 ret = -ENOMEM;
3845 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303846 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3847 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003848 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303849 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003850 if (ret == 0) {
3851 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303852 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003853 * sizes have to be in terms of multiples of 4
3854 * bytes. So, if the Configuration File isn't
3855 * a multiple of 4 bytes in length we'll have
3856 * to write that out separately since we can't
3857 * guarantee that the bytes following the
3858 * residual byte in the buffer returned by
3859 * request_firmware() are zeroed out ...
3860 */
3861 size_t resid = cf->size & 0x3;
3862 size_t size = cf->size & ~0x3;
3863 __be32 *data = (__be32 *)cf->data;
3864
Hariprasad Shenai51678652014-11-21 12:52:02 +05303865 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3866 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003867
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303868 spin_lock(&adapter->win0_lock);
3869 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3870 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003871 if (ret == 0 && resid != 0) {
3872 union {
3873 __be32 word;
3874 char buf[4];
3875 } last;
3876 int i;
3877
3878 last.word = data[size >> 2];
3879 for (i = resid; i < 4; i++)
3880 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303881 ret = t4_memory_rw(adapter, 0, mtype,
3882 maddr + size,
3883 4, &last.word,
3884 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003885 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303886 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003887 }
3888 }
3889
3890 release_firmware(cf);
3891 if (ret)
3892 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003893 }
3894
Vipul Pandya636f9d32012-09-26 02:39:39 +00003895 /*
3896 * Issue a Capability Configuration command to the firmware to get it
3897 * to parse the Configuration File. We don't use t4_fw_config_file()
3898 * because we want the ability to modify various features after we've
3899 * processed the configuration file ...
3900 */
3901 memset(&caps_cmd, 0, sizeof(caps_cmd));
3902 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303903 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3904 FW_CMD_REQUEST_F |
3905 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303906 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303907 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3908 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3909 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003910 FW_LEN16(caps_cmd));
3911 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3912 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303913
3914 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3915 * Configuration File in FLASH), our last gasp effort is to use the
3916 * Firmware Configuration File which is embedded in the firmware. A
3917 * very few early versions of the firmware didn't have one embedded
3918 * but we can ignore those.
3919 */
3920 if (ret == -ENOENT) {
3921 memset(&caps_cmd, 0, sizeof(caps_cmd));
3922 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303923 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3924 FW_CMD_REQUEST_F |
3925 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303926 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3927 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3928 sizeof(caps_cmd), &caps_cmd);
3929 config_name = "Firmware Default";
3930 }
3931
3932 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003933 if (ret < 0)
3934 goto bye;
3935
Vipul Pandya636f9d32012-09-26 02:39:39 +00003936 finiver = ntohl(caps_cmd.finiver);
3937 finicsum = ntohl(caps_cmd.finicsum);
3938 cfcsum = ntohl(caps_cmd.cfcsum);
3939 if (finicsum != cfcsum)
3940 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3941 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3942 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003943
Vipul Pandya636f9d32012-09-26 02:39:39 +00003944 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003945 * And now tell the firmware to use the configuration we just loaded.
3946 */
3947 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303948 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3949 FW_CMD_REQUEST_F |
3950 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303951 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003952 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3953 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003954 if (ret < 0)
3955 goto bye;
3956
Vipul Pandya636f9d32012-09-26 02:39:39 +00003957 /*
3958 * Tweak configuration based on system architecture, module
3959 * parameters, etc.
3960 */
3961 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003962 if (ret < 0)
3963 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003964
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05303965 /* We will proceed even if HMA init fails. */
3966 ret = adap_config_hma(adapter);
3967 if (ret)
3968 dev_err(adapter->pdev_dev,
3969 "HMA configuration failed with error %d\n", ret);
3970
Vipul Pandya636f9d32012-09-26 02:39:39 +00003971 /*
3972 * And finally tell the firmware to initialize itself using the
3973 * parameters from the Configuration File.
3974 */
3975 ret = t4_fw_initialize(adapter, adapter->mbox);
3976 if (ret < 0)
3977 goto bye;
3978
Hariprasad Shenai06640312015-01-13 15:19:25 +05303979 /* Emit Firmware Configuration File information and return
3980 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003981 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003982 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303983 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3984 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003985 return 0;
3986
3987 /*
3988 * Something bad happened. Return the error ... (If the "error"
3989 * is that there's no Configuration File on the adapter we don't
3990 * want to issue a warning since this is fairly common.)
3991 */
3992bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303993 if (config_issued && ret != -ENOENT)
3994 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3995 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003996 return ret;
3997}
3998
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303999static struct fw_info fw_info_array[] = {
4000 {
4001 .chip = CHELSIO_T4,
4002 .fs_name = FW4_CFNAME,
4003 .fw_mod_name = FW4_FNAME,
4004 .fw_hdr = {
4005 .chip = FW_HDR_CHIP_T4,
4006 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
4007 .intfver_nic = FW_INTFVER(T4, NIC),
4008 .intfver_vnic = FW_INTFVER(T4, VNIC),
4009 .intfver_ri = FW_INTFVER(T4, RI),
4010 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
4011 .intfver_fcoe = FW_INTFVER(T4, FCOE),
4012 },
4013 }, {
4014 .chip = CHELSIO_T5,
4015 .fs_name = FW5_CFNAME,
4016 .fw_mod_name = FW5_FNAME,
4017 .fw_hdr = {
4018 .chip = FW_HDR_CHIP_T5,
4019 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
4020 .intfver_nic = FW_INTFVER(T5, NIC),
4021 .intfver_vnic = FW_INTFVER(T5, VNIC),
4022 .intfver_ri = FW_INTFVER(T5, RI),
4023 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
4024 .intfver_fcoe = FW_INTFVER(T5, FCOE),
4025 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05304026 }, {
4027 .chip = CHELSIO_T6,
4028 .fs_name = FW6_CFNAME,
4029 .fw_mod_name = FW6_FNAME,
4030 .fw_hdr = {
4031 .chip = FW_HDR_CHIP_T6,
4032 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
4033 .intfver_nic = FW_INTFVER(T6, NIC),
4034 .intfver_vnic = FW_INTFVER(T6, VNIC),
4035 .intfver_ofld = FW_INTFVER(T6, OFLD),
4036 .intfver_ri = FW_INTFVER(T6, RI),
4037 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
4038 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
4039 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
4040 .intfver_fcoe = FW_INTFVER(T6, FCOE),
4041 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304042 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05304043
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304044};
4045
4046static struct fw_info *find_fw_info(int chip)
4047{
4048 int i;
4049
4050 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
4051 if (fw_info_array[i].chip == chip)
4052 return &fw_info_array[i];
4053 }
4054 return NULL;
4055}
4056
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004057/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004058 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004059 */
4060static int adap_init0(struct adapter *adap)
4061{
4062 int ret;
4063 u32 v, port_vec;
4064 enum dev_state state;
4065 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00004066 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05304067 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004068
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05304069 /* Grab Firmware Device Log parameters as early as possible so we have
4070 * access to it for debugging, etc.
4071 */
4072 ret = t4_init_devlog_params(adap);
4073 if (ret < 0)
4074 return ret;
4075
Hariprasad Shenai666224d2014-12-11 11:11:43 +05304076 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05304077 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
4078 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004079 if (ret < 0) {
4080 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
4081 ret);
4082 return ret;
4083 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004084 if (ret == adap->mbox)
4085 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004086
Vipul Pandya636f9d32012-09-26 02:39:39 +00004087 /*
4088 * If we're the Master PF Driver and the device is uninitialized,
4089 * then let's consider upgrading the firmware ... (We always want
4090 * to check the firmware version number in order to A. get it for
4091 * later reporting and B. to warn if the currently loaded firmware
4092 * is excessively mismatched relative to the driver.)
4093 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304094
Ganesh Goudar760446f2017-07-20 18:28:48 +05304095 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05304096 ret = t4_check_fw_version(adap);
4097 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05304098 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05304099 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004100 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304101 struct fw_info *fw_info;
4102 struct fw_hdr *card_fw;
4103 const struct firmware *fw;
4104 const u8 *fw_data = NULL;
4105 unsigned int fw_size = 0;
4106
4107 /* This is the firmware whose headers the driver was compiled
4108 * against
4109 */
4110 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
4111 if (fw_info == NULL) {
4112 dev_err(adap->pdev_dev,
4113 "unable to get firmware info for chip %d.\n",
4114 CHELSIO_CHIP_VERSION(adap->params.chip));
4115 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004116 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304117
4118 /* allocate memory to read the header of the firmware on the
4119 * card
4120 */
Michal Hocko752ade62017-05-08 15:57:27 -07004121 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304122
4123 /* Get FW from from /lib/firmware/ */
4124 ret = request_firmware(&fw, fw_info->fw_mod_name,
4125 adap->pdev_dev);
4126 if (ret < 0) {
4127 dev_err(adap->pdev_dev,
4128 "unable to load firmware image %s, error %d\n",
4129 fw_info->fw_mod_name, ret);
4130 } else {
4131 fw_data = fw->data;
4132 fw_size = fw->size;
4133 }
4134
4135 /* upgrade FW logic */
4136 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
4137 state, &reset);
4138
4139 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01004140 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07004141 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304142
Vipul Pandya636f9d32012-09-26 02:39:39 +00004143 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05304144 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004145 }
4146
4147 /*
4148 * Grab VPD parameters. This should be done after we establish a
4149 * connection to the firmware since some of the VPD parameters
4150 * (notably the Core Clock frequency) are retrieved via requests to
4151 * the firmware. On the other hand, we need these fairly early on
4152 * so we do this right after getting ahold of the firmware.
4153 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304154 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004155 if (ret < 0)
4156 goto bye;
4157
Vipul Pandya636f9d32012-09-26 02:39:39 +00004158 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004159 * Find out what ports are available to us. Note that we need to do
4160 * this before calling adap_init0_no_config() since it needs nports
4161 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00004162 */
4163 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05304164 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4165 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304166 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004167 if (ret < 0)
4168 goto bye;
4169
4170 adap->params.nports = hweight32(port_vec);
4171 adap->params.portvec = port_vec;
4172
Hariprasad Shenai06640312015-01-13 15:19:25 +05304173 /* If the firmware is initialized already, emit a simply note to that
4174 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004175 */
4176 if (state == DEV_STATE_INIT) {
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05304177 ret = adap_config_hma(adap);
4178 if (ret)
4179 dev_err(adap->pdev_dev,
4180 "HMA configuration failed with error %d\n",
4181 ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004182 dev_info(adap->pdev_dev, "Coming up as %s: "\
4183 "Adapter already initialized\n",
4184 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00004185 } else {
4186 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
4187 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05304188
4189 /* Find out whether we're dealing with a version of the
4190 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004191 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05304192 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
4193 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304194 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05304195 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004196
Hariprasad Shenai06640312015-01-13 15:19:25 +05304197 /* If the firmware doesn't support Configuration Files,
4198 * return an error.
4199 */
4200 if (ret < 0) {
4201 dev_err(adap->pdev_dev, "firmware doesn't support "
4202 "Firmware Configuration Files\n");
4203 goto bye;
4204 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00004205
Hariprasad Shenai06640312015-01-13 15:19:25 +05304206 /* The firmware provides us with a memory buffer where we can
4207 * load a Configuration File from the host if we want to
4208 * override the Configuration File in flash.
4209 */
4210 ret = adap_init0_config(adap, reset);
4211 if (ret == -ENOENT) {
4212 dev_err(adap->pdev_dev, "no Configuration File "
4213 "present on adapter.\n");
4214 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004215 }
4216 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05304217 dev_err(adap->pdev_dev, "could not initialize "
4218 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004219 goto bye;
4220 }
4221 }
4222
Hariprasad Shenai06640312015-01-13 15:19:25 +05304223 /* Give the SGE code a chance to pull in anything that it needs ...
4224 * Note that this must be called after we retrieve our VPD parameters
4225 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004226 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05304227 ret = t4_sge_init(adap);
4228 if (ret < 0)
4229 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004230
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00004231 if (is_bypass_device(adap->pdev->device))
4232 adap->params.bypass = 1;
4233
Vipul Pandya636f9d32012-09-26 02:39:39 +00004234 /*
4235 * Grab some of our basic fundamental operating parameters.
4236 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004237#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05304238 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
4239 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004240
4241#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05304242 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
4243 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
4244 FW_PARAMS_PARAM_Y_V(0) | \
4245 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004246
Vipul Pandya636f9d32012-09-26 02:39:39 +00004247 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004248 params[1] = FW_PARAM_PFVF(L2T_START);
4249 params[2] = FW_PARAM_PFVF(L2T_END);
4250 params[3] = FW_PARAM_PFVF(FILTER_START);
4251 params[4] = FW_PARAM_PFVF(FILTER_END);
4252 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304253 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004254 if (ret < 0)
4255 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004256 adap->sge.egr_start = val[0];
4257 adap->l2t_start = val[1];
4258 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004259 adap->tids.ftid_base = val[3];
4260 adap->tids.nftids = val[4] - val[3] + 1;
4261 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004262
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304263 /* qids (ingress/egress) returned from firmware can be anywhere
4264 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
4265 * Hence driver needs to allocate memory for this range to
4266 * store the queue info. Get the highest IQFLINT/EQ index returned
4267 * in FW_EQ_*_CMD.alloc command.
4268 */
4269 params[0] = FW_PARAM_PFVF(EQ_END);
4270 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304271 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304272 if (ret < 0)
4273 goto bye;
4274 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
4275 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
4276
4277 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
4278 sizeof(*adap->sge.egr_map), GFP_KERNEL);
4279 if (!adap->sge.egr_map) {
4280 ret = -ENOMEM;
4281 goto bye;
4282 }
4283
4284 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
4285 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
4286 if (!adap->sge.ingr_map) {
4287 ret = -ENOMEM;
4288 goto bye;
4289 }
4290
4291 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304292 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304293 */
4294 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4295 sizeof(long), GFP_KERNEL);
4296 if (!adap->sge.starving_fl) {
4297 ret = -ENOMEM;
4298 goto bye;
4299 }
4300
4301 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4302 sizeof(long), GFP_KERNEL);
4303 if (!adap->sge.txq_maperr) {
4304 ret = -ENOMEM;
4305 goto bye;
4306 }
4307
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304308#ifdef CONFIG_DEBUG_FS
4309 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4310 sizeof(long), GFP_KERNEL);
4311 if (!adap->sge.blocked_fl) {
4312 ret = -ENOMEM;
4313 goto bye;
4314 }
4315#endif
4316
Anish Bhattb5a02f52015-01-14 15:17:34 -08004317 params[0] = FW_PARAM_PFVF(CLIP_START);
4318 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304319 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08004320 if (ret < 0)
4321 goto bye;
4322 adap->clipt_start = val[0];
4323 adap->clipt_end = val[1];
4324
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304325 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
4326 * Classes supported by the hardware/firmware so we hard code it here
4327 * for now.
4328 */
4329 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
4330
Vipul Pandya636f9d32012-09-26 02:39:39 +00004331 /* query params related to active filter region */
4332 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
4333 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304334 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004335 /* If Active filter size is set we enable establishing
4336 * offload connection through firmware work request
4337 */
4338 if ((val[0] != val[1]) && (ret >= 0)) {
4339 adap->flags |= FW_OFLD_CONN;
4340 adap->tids.aftid_base = val[0];
4341 adap->tids.aftid_end = val[1];
4342 }
4343
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004344 /* If we're running on newer firmware, let it know that we're
4345 * prepared to deal with encapsulated CPL messages. Older
4346 * firmware won't understand this and we'll just get
4347 * unencapsulated messages ...
4348 */
4349 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
4350 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304351 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004352
Vipul Pandya636f9d32012-09-26 02:39:39 +00004353 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304354 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
4355 * capability. Earlier versions of the firmware didn't have the
4356 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
4357 * permission to use ULPTX MEMWRITE DSGL.
4358 */
4359 if (is_t4(adap->params.chip)) {
4360 adap->params.ulptx_memwrite_dsgl = false;
4361 } else {
4362 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304363 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304364 1, params, val);
4365 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4366 }
4367
Steve Wise086de572016-09-16 07:54:49 -07004368 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
4369 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
4370 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4371 1, params, val);
4372 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
4373
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05304374 /* See if FW supports FW_FILTER2 work request */
4375 if (is_t4(adap->params.chip)) {
4376 adap->params.filter2_wr_support = 0;
4377 } else {
4378 params[0] = FW_PARAM_DEV(FILTER2_WR);
4379 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4380 1, params, val);
4381 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
4382 }
4383
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304384 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004385 * Get device capabilities so we can determine what resources we need
4386 * to manage.
4387 */
4388 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304389 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4390 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304391 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004392 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4393 &caps_cmd);
4394 if (ret < 0)
4395 goto bye;
4396
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304397 if (caps_cmd.ofldcaps ||
4398 (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER))) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004399 /* query offload-related parameters */
4400 params[0] = FW_PARAM_DEV(NTID);
4401 params[1] = FW_PARAM_PFVF(SERVER_START);
4402 params[2] = FW_PARAM_PFVF(SERVER_END);
4403 params[3] = FW_PARAM_PFVF(TDDP_START);
4404 params[4] = FW_PARAM_PFVF(TDDP_END);
4405 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304406 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004407 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004408 if (ret < 0)
4409 goto bye;
4410 adap->tids.ntids = val[0];
4411 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4412 adap->tids.stid_base = val[1];
4413 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004414 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004415 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004416 * region into two parts. Regular filters get 1/3rd and server
4417 * filters get 2/3rd part. This is only enabled if workarond
4418 * path is enabled.
4419 * 1. For regular filters.
4420 * 2. Server filter: This are special filters which are used
4421 * to redirect SYN packets to offload queue.
4422 */
4423 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4424 adap->tids.sftid_base = adap->tids.ftid_base +
4425 DIV_ROUND_UP(adap->tids.nftids, 3);
4426 adap->tids.nsftids = adap->tids.nftids -
4427 DIV_ROUND_UP(adap->tids.nftids, 3);
4428 adap->tids.nftids = adap->tids.sftid_base -
4429 adap->tids.ftid_base;
4430 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004431 adap->vres.ddp.start = val[3];
4432 adap->vres.ddp.size = val[4] - val[3] + 1;
4433 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004434
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304435 if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4436 if (init_hash_filter(adap) < 0)
4437 goto bye;
4438 } else {
4439 adap->params.offload = 1;
4440 adap->num_ofld_uld += 1;
4441 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004442 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004443 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004444 params[0] = FW_PARAM_PFVF(STAG_START);
4445 params[1] = FW_PARAM_PFVF(STAG_END);
4446 params[2] = FW_PARAM_PFVF(RQ_START);
4447 params[3] = FW_PARAM_PFVF(RQ_END);
4448 params[4] = FW_PARAM_PFVF(PBL_START);
4449 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304450 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004451 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004452 if (ret < 0)
4453 goto bye;
4454 adap->vres.stag.start = val[0];
4455 adap->vres.stag.size = val[1] - val[0] + 1;
4456 adap->vres.rq.start = val[2];
4457 adap->vres.rq.size = val[3] - val[2] + 1;
4458 adap->vres.pbl.start = val[4];
4459 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004460
4461 params[0] = FW_PARAM_PFVF(SQRQ_START);
4462 params[1] = FW_PARAM_PFVF(SQRQ_END);
4463 params[2] = FW_PARAM_PFVF(CQ_START);
4464 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004465 params[4] = FW_PARAM_PFVF(OCQ_START);
4466 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304467 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304468 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004469 if (ret < 0)
4470 goto bye;
4471 adap->vres.qp.start = val[0];
4472 adap->vres.qp.size = val[1] - val[0] + 1;
4473 adap->vres.cq.start = val[2];
4474 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004475 adap->vres.ocq.start = val[4];
4476 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304477
4478 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4479 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304480 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304481 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304482 if (ret < 0) {
4483 adap->params.max_ordird_qp = 8;
4484 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4485 ret = 0;
4486 } else {
4487 adap->params.max_ordird_qp = val[0];
4488 adap->params.max_ird_adapter = val[1];
4489 }
4490 dev_info(adap->pdev_dev,
4491 "max_ordird_qp %d max_ird_adapter %d\n",
4492 adap->params.max_ordird_qp,
4493 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304494 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004495 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004496 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004497 params[0] = FW_PARAM_PFVF(ISCSI_START);
4498 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304499 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004500 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004501 if (ret < 0)
4502 goto bye;
4503 adap->vres.iscsi.start = val[0];
4504 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304505 /* LIO target and cxgb4i initiaitor */
4506 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004507 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304508 if (caps_cmd.cryptocaps) {
4509 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304510 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4511 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4512 params, val);
4513 if (ret < 0) {
4514 if (ret != -EINVAL)
4515 goto bye;
4516 } else {
4517 adap->vres.ncrypto_fc = val[0];
4518 }
Atul Guptaa6ec5722017-11-16 16:56:39 +05304519 adap->params.crypto = ntohs(caps_cmd.cryptocaps);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304520 adap->num_uld += 1;
4521 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004522#undef FW_PARAM_PFVF
4523#undef FW_PARAM_DEV
4524
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304525 /* The MTU/MSS Table is initialized by now, so load their values. If
4526 * we're initializing the adapter, then we'll make any modifications
4527 * we want to the MTU/MSS Table and also initialize the congestion
4528 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004529 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004530 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304531 if (state != DEV_STATE_INIT) {
4532 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004533
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304534 /* The default MTU Table contains values 1492 and 1500.
4535 * However, for TCP, it's better to have two values which are
4536 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4537 * This allows us to have a TCP Data Payload which is a
4538 * multiple of 8 regardless of what combination of TCP Options
4539 * are in use (always a multiple of 4 bytes) which is
4540 * important for performance reasons. For instance, if no
4541 * options are in use, then we have a 20-byte IP header and a
4542 * 20-byte TCP header. In this case, a 1500-byte MSS would
4543 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4544 * which is not a multiple of 8. So using an MSS of 1488 in
4545 * this case results in a TCP Data Payload of 1448 bytes which
4546 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4547 * Stamps have been negotiated, then an MTU of 1500 bytes
4548 * results in a TCP Data Payload of 1448 bytes which, as
4549 * above, is a multiple of 8 bytes ...
4550 */
4551 for (i = 0; i < NMTUS; i++)
4552 if (adap->params.mtus[i] == 1492) {
4553 adap->params.mtus[i] = 1488;
4554 break;
4555 }
4556
4557 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4558 adap->params.b_wnd);
4559 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304560 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004561 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304562 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004563 return 0;
4564
4565 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004566 * Something bad happened. If a command timed out or failed with EIO
4567 * FW does not operate within its spec or something catastrophic
4568 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004569 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004570bye:
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05304571 adap_free_hma_mem(adap);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304572 kfree(adap->sge.egr_map);
4573 kfree(adap->sge.ingr_map);
4574 kfree(adap->sge.starving_fl);
4575 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304576#ifdef CONFIG_DEBUG_FS
4577 kfree(adap->sge.blocked_fl);
4578#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004579 if (ret != -ETIMEDOUT && ret != -EIO)
4580 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004581 return ret;
4582}
4583
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004584/* EEH callbacks */
4585
4586static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4587 pci_channel_state_t state)
4588{
4589 int i;
4590 struct adapter *adap = pci_get_drvdata(pdev);
4591
4592 if (!adap)
4593 goto out;
4594
4595 rtnl_lock();
4596 adap->flags &= ~FW_OK;
4597 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004598 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004599 for_each_port(adap, i) {
4600 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004601 if (dev) {
4602 netif_device_detach(dev);
4603 netif_carrier_off(dev);
4604 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004605 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004606 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304607 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004608 if (adap->flags & FULL_INIT_DONE)
4609 cxgb_down(adap);
4610 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004611 if ((adap->flags & DEV_ENABLED)) {
4612 pci_disable_device(pdev);
4613 adap->flags &= ~DEV_ENABLED;
4614 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004615out: return state == pci_channel_io_perm_failure ?
4616 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4617}
4618
4619static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4620{
4621 int i, ret;
4622 struct fw_caps_config_cmd c;
4623 struct adapter *adap = pci_get_drvdata(pdev);
4624
4625 if (!adap) {
4626 pci_restore_state(pdev);
4627 pci_save_state(pdev);
4628 return PCI_ERS_RESULT_RECOVERED;
4629 }
4630
Gavin Shan144be3d2014-01-23 12:27:34 +08004631 if (!(adap->flags & DEV_ENABLED)) {
4632 if (pci_enable_device(pdev)) {
4633 dev_err(&pdev->dev, "Cannot reenable PCI "
4634 "device after reset\n");
4635 return PCI_ERS_RESULT_DISCONNECT;
4636 }
4637 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004638 }
4639
4640 pci_set_master(pdev);
4641 pci_restore_state(pdev);
4642 pci_save_state(pdev);
4643 pci_cleanup_aer_uncorrect_error_status(pdev);
4644
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304645 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004646 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304647 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004648 return PCI_ERS_RESULT_DISCONNECT;
4649 adap->flags |= FW_OK;
4650 if (adap_init1(adap, &c))
4651 return PCI_ERS_RESULT_DISCONNECT;
4652
4653 for_each_port(adap, i) {
4654 struct port_info *p = adap2pinfo(adap, i);
4655
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304656 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004657 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004658 if (ret < 0)
4659 return PCI_ERS_RESULT_DISCONNECT;
4660 p->viid = ret;
4661 p->xact_addr_filt = -1;
4662 }
4663
4664 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4665 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004666 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004667 if (cxgb_up(adap))
4668 return PCI_ERS_RESULT_DISCONNECT;
4669 return PCI_ERS_RESULT_RECOVERED;
4670}
4671
4672static void eeh_resume(struct pci_dev *pdev)
4673{
4674 int i;
4675 struct adapter *adap = pci_get_drvdata(pdev);
4676
4677 if (!adap)
4678 return;
4679
4680 rtnl_lock();
4681 for_each_port(adap, i) {
4682 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004683 if (dev) {
4684 if (netif_running(dev)) {
4685 link_start(dev);
4686 cxgb_set_rxmode(dev);
4687 }
4688 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004689 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004690 }
4691 rtnl_unlock();
4692}
4693
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004694static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004695 .error_detected = eeh_err_detected,
4696 .slot_reset = eeh_slot_reset,
4697 .resume = eeh_resume,
4698};
4699
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304700/* Return true if the Link Configuration supports "High Speeds" (those greater
4701 * than 1Gb/s).
4702 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304703static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004704{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304705 unsigned int speeds, high_speeds;
4706
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304707 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4708 high_speeds = speeds &
4709 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304710
4711 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004712}
4713
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004714/*
4715 * Perform default configuration of DMA queues depending on the number and type
4716 * of ports we found and the number of available CPUs. Most settings can be
4717 * modified by the admin prior to actual use.
4718 */
Bill Pemberton91744942012-12-03 09:23:02 -05004719static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004720{
4721 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304722 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004723#ifndef CONFIG_CHELSIO_T4_DCB
4724 int q10g = 0;
4725#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004726
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304727 /* Reduce memory usage in kdump environment, disable all offload.
4728 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304729 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304730 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304731 adap->params.crypto = 0;
4732 }
4733
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304734 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004735#ifdef CONFIG_CHELSIO_T4_DCB
4736 /* For Data Center Bridging support we need to be able to support up
4737 * to 8 Traffic Priorities; each of which will be assigned to its
4738 * own TX Queue in order to prevent Head-Of-Line Blocking.
4739 */
4740 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4741 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4742 MAX_ETH_QSETS, adap->params.nports * 8);
4743 BUG_ON(1);
4744 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004745
Anish Bhatt688848b2014-06-19 21:37:13 -07004746 for_each_port(adap, i) {
4747 struct port_info *pi = adap2pinfo(adap, i);
4748
4749 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304750 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004751 qidx += pi->nqsets;
4752 }
4753#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004754 /*
4755 * We default to 1 queue per non-10G port and up to # of cores queues
4756 * per 10G port.
4757 */
4758 if (n10g)
4759 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004760 if (q10g > netif_get_num_default_rss_queues())
4761 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004762
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304763 if (is_kdump_kernel())
4764 q10g = 1;
4765
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004766 for_each_port(adap, i) {
4767 struct port_info *pi = adap2pinfo(adap, i);
4768
4769 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304770 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004771 qidx += pi->nqsets;
4772 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004773#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004774
4775 s->ethqsets = qidx;
4776 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4777
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304778 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004779 /*
4780 * For offload we use 1 queue/channel if all ports are up to 1G,
4781 * otherwise we divide all available queues amongst the channels
4782 * capped by the number of available cores.
4783 */
4784 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304785 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304786 s->ofldqsets = roundup(i, adap->params.nports);
4787 } else {
4788 s->ofldqsets = adap->params.nports;
4789 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004790 }
4791
4792 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4793 struct sge_eth_rxq *r = &s->ethrxq[i];
4794
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304795 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004796 r->fl.size = 72;
4797 }
4798
4799 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4800 s->ethtxq[i].q.size = 1024;
4801
4802 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4803 s->ctrlq[i].q.size = 512;
4804
Atul Guptaa45695042017-07-04 16:46:20 +05304805 if (!is_t4(adap->params.chip))
4806 s->ptptxq.q.size = 8;
4807
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304808 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304809 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004810}
4811
4812/*
4813 * Reduce the number of Ethernet queues across all ports to at most n.
4814 * n provides at least one queue per port.
4815 */
Bill Pemberton91744942012-12-03 09:23:02 -05004816static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004817{
4818 int i;
4819 struct port_info *pi;
4820
4821 while (n < adap->sge.ethqsets)
4822 for_each_port(adap, i) {
4823 pi = adap2pinfo(adap, i);
4824 if (pi->nqsets > 1) {
4825 pi->nqsets--;
4826 adap->sge.ethqsets--;
4827 if (adap->sge.ethqsets <= n)
4828 break;
4829 }
4830 }
4831
4832 n = 0;
4833 for_each_port(adap, i) {
4834 pi = adap2pinfo(adap, i);
4835 pi->first_qset = n;
4836 n += pi->nqsets;
4837 }
4838}
4839
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304840static int get_msix_info(struct adapter *adap)
4841{
4842 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304843 unsigned int max_ingq = 0;
4844
4845 if (is_offload(adap))
4846 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4847 if (is_pci_uld(adap))
4848 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4849
4850 if (!max_ingq)
4851 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304852
4853 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4854 if (!msix_info)
4855 return -ENOMEM;
4856
4857 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4858 sizeof(long), GFP_KERNEL);
4859 if (!adap->msix_bmap_ulds.msix_bmap) {
4860 kfree(msix_info);
4861 return -ENOMEM;
4862 }
4863 spin_lock_init(&adap->msix_bmap_ulds.lock);
4864 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304865out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304866 return 0;
4867}
4868
4869static void free_msix_info(struct adapter *adap)
4870{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304871 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304872 return;
4873
4874 kfree(adap->msix_info_ulds);
4875 kfree(adap->msix_bmap_ulds.msix_bmap);
4876}
4877
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004878/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4879#define EXTRA_VECS 2
4880
Bill Pemberton91744942012-12-03 09:23:02 -05004881static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004882{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304883 int ofld_need = 0, uld_need = 0;
4884 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004885 struct sge *s = &adap->sge;
4886 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304887 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304888 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004889
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304890 if (is_pci_uld(adap))
4891 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4892 if (is_offload(adap))
4893 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304894 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304895 GFP_KERNEL);
4896 if (!entries)
4897 return -ENOMEM;
4898
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304899 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304900 if (get_msix_info(adap)) {
4901 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304902 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304903 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304904
4905 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004906 entries[i].entry = i;
4907
4908 want = s->max_ethqsets + EXTRA_VECS;
4909 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304910 want += adap->num_ofld_uld * s->ofldqsets;
4911 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004912 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304913 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304914 want += adap->num_uld * s->ofldqsets;
4915 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304916 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004917#ifdef CONFIG_CHELSIO_T4_DCB
4918 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4919 * each port.
4920 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304921 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004922#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304923 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004924#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304925 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4926 if (allocated < 0) {
4927 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4928 " not using MSI-X\n");
4929 kfree(entries);
4930 return allocated;
4931 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004932
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304933 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004934 * Every group gets its minimum requirement and NIC gets top
4935 * priority for leftovers.
4936 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304937 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004938 if (i < s->max_ethqsets) {
4939 s->max_ethqsets = i;
4940 if (i < s->ethqsets)
4941 reduce_ethqs(adap, i);
4942 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304943 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304944 if (allocated < want)
4945 s->nqs_per_uld = nchan;
4946 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304947 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304948 }
4949
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304950 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004951 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304952 if (is_uld(adap)) {
4953 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304954 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304955 adap->msix_info_ulds[j].idx = i;
4956 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304957 adap->msix_bmap_ulds.mapsize = j;
4958 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304959 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304960 "nic %d per uld %d\n",
4961 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004962
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304963 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004964 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004965}
4966
4967#undef EXTRA_VECS
4968
Bill Pemberton91744942012-12-03 09:23:02 -05004969static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004970{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304971 unsigned int i;
4972 int err;
4973
4974 err = t4_init_rss_mode(adap, adap->mbox);
4975 if (err)
4976 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004977
4978 for_each_port(adap, i) {
4979 struct port_info *pi = adap2pinfo(adap, i);
4980
4981 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4982 if (!pi->rss)
4983 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004984 }
4985 return 0;
4986}
4987
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304988static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4989 enum pci_bus_speed *speed,
4990 enum pcie_link_width *width)
4991{
4992 u32 lnkcap1, lnkcap2;
4993 int err1, err2;
4994
4995#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4996
4997 *speed = PCI_SPEED_UNKNOWN;
4998 *width = PCIE_LNK_WIDTH_UNKNOWN;
4999
5000 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
5001 &lnkcap1);
5002 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
5003 &lnkcap2);
5004 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
5005 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
5006 *speed = PCIE_SPEED_8_0GT;
5007 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
5008 *speed = PCIE_SPEED_5_0GT;
5009 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
5010 *speed = PCIE_SPEED_2_5GT;
5011 }
5012 if (!err1) {
5013 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
5014 if (!lnkcap2) { /* pre-r3.0 */
5015 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
5016 *speed = PCIE_SPEED_5_0GT;
5017 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
5018 *speed = PCIE_SPEED_2_5GT;
5019 }
5020 }
5021
5022 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
5023 return err1 ? err1 : err2 ? err2 : -EINVAL;
5024 return 0;
5025}
5026
5027static void cxgb4_check_pcie_caps(struct adapter *adap)
5028{
5029 enum pcie_link_width width, width_cap;
5030 enum pci_bus_speed speed, speed_cap;
5031
5032#define PCIE_SPEED_STR(speed) \
5033 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
5034 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
5035 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
5036 "Unknown")
5037
5038 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
5039 dev_warn(adap->pdev_dev,
5040 "Unable to determine PCIe device BW capabilities\n");
5041 return;
5042 }
5043
5044 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
5045 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
5046 dev_warn(adap->pdev_dev,
5047 "Unable to determine PCI Express bandwidth.\n");
5048 return;
5049 }
5050
5051 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
5052 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
5053 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
5054 width, width_cap);
5055 if (speed < speed_cap || width < width_cap)
5056 dev_info(adap->pdev_dev,
5057 "A slot with more lanes and/or higher speed is "
5058 "suggested for optimal performance.\n");
5059}
5060
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305061/* Dump basic information about the adapter */
5062static void print_adapter_info(struct adapter *adapter)
5063{
Ganesh Goudar760446f2017-07-20 18:28:48 +05305064 /* Hardware/Firmware/etc. Version/Revision IDs */
5065 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305066
5067 /* Software/Hardware configuration */
5068 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
5069 is_offload(adapter) ? "R" : "",
5070 ((adapter->flags & USING_MSIX) ? "MSI-X" :
5071 (adapter->flags & USING_MSI) ? "MSI" : ""),
5072 is_offload(adapter) ? "Offload" : "non-Offload");
5073}
5074
Bill Pemberton91744942012-12-03 09:23:02 -05005075static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005076{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005077 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005078 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00005079 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005080 const struct port_info *pi = netdev_priv(dev);
5081 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00005082
5083 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
5084 spd = " 2.5 GT/s";
5085 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
5086 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07005087 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
5088 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005089
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305090 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05305091 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305092 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05305093 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305094 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005095 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305096 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05305097 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305098 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05305099 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305100 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
5101 bufp += sprintf(bufp, "50G/");
5102 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05305103 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05305104 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
5105 bufp += sprintf(bufp, "200G/");
5106 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
5107 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00005108 if (bufp != buf)
5109 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05305110 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005111
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305112 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
5113 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005114}
5115
Dimitris Michailidis06546392010-07-11 12:01:16 +00005116/*
5117 * Free the following resources:
5118 * - memory used for tables
5119 * - MSI/MSI-X
5120 * - net devices
5121 * - resources FW is holding for us
5122 */
5123static void free_some_resources(struct adapter *adapter)
5124{
5125 unsigned int i;
5126
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305127 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07005128 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305129 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07005130 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05305131 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305132 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05305133 kfree(adapter->sge.egr_map);
5134 kfree(adapter->sge.ingr_map);
5135 kfree(adapter->sge.starving_fl);
5136 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305137#ifdef CONFIG_DEBUG_FS
5138 kfree(adapter->sge.blocked_fl);
5139#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00005140 disable_msi(adapter);
5141
5142 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005143 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05305144 struct port_info *pi = adap2pinfo(adapter, i);
5145
5146 if (pi->viid != 0)
5147 t4_free_vi(adapter, adapter->mbox, adapter->pf,
5148 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005149 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005150 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005151 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00005152 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305153 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005154}
5155
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005156#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00005157#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005158 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005159#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005160
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305161static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
5162{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305163 u16 device_id;
5164
5165 /* Retrieve adapter's device ID */
5166 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02005167
5168 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305169 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02005170 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305171 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02005172 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305173 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02005174 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305175 default:
5176 dev_err(&pdev->dev, "Device %d is not supported\n",
5177 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305178 }
françois romieu46cdc9b2015-09-04 23:05:42 +02005179 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305180}
5181
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305182#ifdef CONFIG_PCI_IOV
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305183static void cxgb4_mgmt_setup(struct net_device *dev)
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305184{
5185 dev->type = ARPHRD_NONE;
5186 dev->mtu = 0;
5187 dev->hard_header_len = 0;
5188 dev->addr_len = 0;
5189 dev->tx_queue_len = 0;
5190 dev->flags |= IFF_NOARP;
5191 dev->priv_flags |= IFF_NO_QUEUE;
5192
5193 /* Initialize the device structure. */
5194 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
5195 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04005196 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305197}
5198
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305199static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
5200{
Hariprasad Shenai78294512016-08-11 21:06:23 +05305201 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305202 int err = 0;
5203 int current_vfs = pci_num_vf(pdev);
5204 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305205
Hariprasad Shenai78294512016-08-11 21:06:23 +05305206 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305207 /* Check if cxgb4 is the MASTER and fw is initialized */
Ganesh Goudarc4e43e12018-02-15 18:16:57 +05305208 if (num_vfs &&
5209 (!(pcie_fw & PCIE_FW_INIT_F) ||
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305210 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
Ganesh Goudarc4e43e12018-02-15 18:16:57 +05305211 PCIE_FW_MASTER_G(pcie_fw) != CXGB4_UNIFIED_PF)) {
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305212 dev_warn(&pdev->dev,
5213 "cxgb4 driver needs to be MASTER to support SRIOV\n");
5214 return -EOPNOTSUPP;
5215 }
5216
5217 /* If any of the VF's is already assigned to Guest OS, then
5218 * SRIOV for the same cannot be modified
5219 */
5220 if (current_vfs && pci_vfs_assigned(pdev)) {
5221 dev_err(&pdev->dev,
5222 "Cannot modify SR-IOV while VFs are assigned\n");
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305223 return current_vfs;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305224 }
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305225 /* Note that the upper-level code ensures that we're never called with
5226 * a non-zero "num_vfs" when we already have VFs instantiated. But
5227 * it never hurts to code defensively.
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305228 */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305229 if (num_vfs != 0 && current_vfs != 0)
5230 return -EBUSY;
5231
5232 /* Nothing to do for no change. */
5233 if (num_vfs == current_vfs)
5234 return num_vfs;
5235
5236 /* Disable SRIOV when zero is passed. */
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305237 if (!num_vfs) {
5238 pci_disable_sriov(pdev);
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305239 /* free VF Management Interface */
5240 unregister_netdev(adap->port[0]);
5241 free_netdev(adap->port[0]);
5242 adap->port[0] = NULL;
5243
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305244 /* free VF resources */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305245 adap->num_vfs = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305246 kfree(adap->vfinfo);
5247 adap->vfinfo = NULL;
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305248 return 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305249 }
5250
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305251 if (!current_vfs) {
5252 struct fw_pfvf_cmd port_cmd, port_rpl;
5253 struct net_device *netdev;
5254 unsigned int pmask, port;
5255 struct pci_dev *pbridge;
5256 struct port_info *pi;
5257 char name[IFNAMSIZ];
5258 u32 devcap2;
5259 u16 flags;
5260 int pos;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305261
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305262 /* If we want to instantiate Virtual Functions, then our
5263 * parent bridge's PCI-E needs to support Alternative Routing
5264 * ID (ARI) because our VFs will show up at function offset 8
5265 * and above.
5266 */
5267 pbridge = pdev->bus->self;
5268 pos = pci_find_capability(pbridge, PCI_CAP_ID_EXP);
5269 pci_read_config_word(pbridge, pos + PCI_EXP_FLAGS, &flags);
5270 pci_read_config_dword(pbridge, pos + PCI_EXP_DEVCAP2, &devcap2);
5271
5272 if ((flags & PCI_EXP_FLAGS_VERS) < 2 ||
5273 !(devcap2 & PCI_EXP_DEVCAP2_ARI)) {
5274 /* Our parent bridge does not support ARI so issue a
5275 * warning and skip instantiating the VFs. They
5276 * won't be reachable.
5277 */
5278 dev_warn(&pdev->dev, "Parent bridge %02x:%02x.%x doesn't support ARI; can't instantiate Virtual Functions\n",
5279 pbridge->bus->number, PCI_SLOT(pbridge->devfn),
5280 PCI_FUNC(pbridge->devfn));
5281 return -ENOTSUPP;
5282 }
5283 memset(&port_cmd, 0, sizeof(port_cmd));
5284 port_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
5285 FW_CMD_REQUEST_F |
5286 FW_CMD_READ_F |
5287 FW_PFVF_CMD_PFN_V(adap->pf) |
5288 FW_PFVF_CMD_VFN_V(0));
5289 port_cmd.retval_len16 = cpu_to_be32(FW_LEN16(port_cmd));
5290 err = t4_wr_mbox(adap, adap->mbox, &port_cmd, sizeof(port_cmd),
5291 &port_rpl);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305292 if (err)
5293 return err;
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305294 pmask = FW_PFVF_CMD_PMASK_G(be32_to_cpu(port_rpl.type_to_neq));
5295 port = ffs(pmask) - 1;
5296 /* Allocate VF Management Interface. */
5297 snprintf(name, IFNAMSIZ, "mgmtpf%d,%d", adap->adap_idx,
5298 adap->pf);
5299 netdev = alloc_netdev(sizeof(struct port_info),
5300 name, NET_NAME_UNKNOWN, cxgb4_mgmt_setup);
5301 if (!netdev)
5302 return -ENOMEM;
5303
5304 pi = netdev_priv(netdev);
5305 pi->adapter = adap;
5306 pi->lport = port;
5307 pi->tx_chan = port;
5308 SET_NETDEV_DEV(netdev, &pdev->dev);
5309
5310 adap->port[0] = netdev;
5311 pi->port_id = 0;
5312
5313 err = register_netdev(adap->port[0]);
5314 if (err) {
5315 pr_info("Unable to register VF mgmt netdev %s\n", name);
5316 free_netdev(adap->port[0]);
5317 adap->port[0] = NULL;
5318 return err;
5319 }
5320 /* Allocate and set up VF Information. */
5321 adap->vfinfo = kcalloc(pci_sriov_get_totalvfs(pdev),
5322 sizeof(struct vf_info), GFP_KERNEL);
5323 if (!adap->vfinfo) {
5324 unregister_netdev(adap->port[0]);
5325 free_netdev(adap->port[0]);
5326 adap->port[0] = NULL;
5327 return -ENOMEM;
5328 }
5329 cxgb4_mgmt_fill_vf_station_mac_addr(adap);
5330 }
5331 /* Instantiate the requested number of VFs. */
5332 err = pci_enable_sriov(pdev, num_vfs);
5333 if (err) {
5334 pr_info("Unable to instantiate %d VFs\n", num_vfs);
5335 if (!current_vfs) {
5336 unregister_netdev(adap->port[0]);
5337 free_netdev(adap->port[0]);
5338 adap->port[0] = NULL;
5339 kfree(adap->vfinfo);
5340 adap->vfinfo = NULL;
5341 }
5342 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305343 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305344
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305345 adap->num_vfs = num_vfs;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305346 return num_vfs;
5347}
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305348#endif /* CONFIG_PCI_IOV */
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305349
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00005350static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005351{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005352 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005353 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005354 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005355 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305356 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305357 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305358 u32 whoami, pl_rev;
5359 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305360 static int adap_idx = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005361
5362 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
5363
5364 err = pci_request_regions(pdev, KBUILD_MODNAME);
5365 if (err) {
5366 /* Just info, some other driver may have claimed the device. */
5367 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
5368 return err;
5369 }
5370
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005371 err = pci_enable_device(pdev);
5372 if (err) {
5373 dev_err(&pdev->dev, "cannot enable PCI device\n");
5374 goto out_release_regions;
5375 }
5376
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305377 regs = pci_ioremap_bar(pdev, 0);
5378 if (!regs) {
5379 dev_err(&pdev->dev, "cannot map device registers\n");
5380 err = -ENOMEM;
5381 goto out_disable_device;
5382 }
5383
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305384 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5385 if (!adapter) {
5386 err = -ENOMEM;
5387 goto out_unmap_bar0;
5388 }
5389
5390 adapter->regs = regs;
Hariprasad Shenai8203b502014-10-09 05:48:47 +05305391 err = t4_wait_dev_ready(regs);
5392 if (err < 0)
Christophe JAILLETe7294522018-02-06 21:17:17 +01005393 goto out_free_adapter;
Hariprasad Shenai8203b502014-10-09 05:48:47 +05305394
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305395 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305396 whoami = readl(regs + PL_WHOAMI_A);
5397 pl_rev = REV_G(readl(regs + PL_REV_A));
5398 chip = get_chip_type(pdev, pl_rev);
5399 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
5400 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305401
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305402 adapter->pdev = pdev;
5403 adapter->pdev_dev = &pdev->dev;
5404 adapter->name = pci_name(pdev);
5405 adapter->mbox = func;
5406 adapter->pf = func;
5407 adapter->msg_enable = DFLT_MSG_ENABLE;
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305408 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5409 (sizeof(struct mbox_cmd) *
5410 T4_OS_LOG_MBOX_CMDS),
5411 GFP_KERNEL);
5412 if (!adapter->mbox_log) {
5413 err = -ENOMEM;
5414 goto out_free_adapter;
5415 }
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305416 spin_lock_init(&adapter->mbox_lock);
5417 INIT_LIST_HEAD(&adapter->mlist.list);
5418 pci_set_drvdata(pdev, adapter);
5419
5420 if (func != ent->driver_data) {
5421 pci_disable_device(pdev);
5422 pci_save_state(pdev); /* to restore SR-IOV later */
5423 return 0;
5424 }
5425
5426 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
5427 highdma = true;
5428 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
5429 if (err) {
5430 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
5431 "coherent allocations\n");
5432 goto out_free_adapter;
5433 }
5434 } else {
5435 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5436 if (err) {
5437 dev_err(&pdev->dev, "no usable DMA configuration\n");
5438 goto out_free_adapter;
5439 }
5440 }
5441
5442 pci_enable_pcie_error_reporting(pdev);
5443 pci_set_master(pdev);
5444 pci_save_state(pdev);
5445 adap_idx++;
5446 adapter->workq = create_singlethread_workqueue("cxgb4");
5447 if (!adapter->workq) {
5448 err = -ENOMEM;
5449 goto out_free_adapter;
5450 }
5451
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305452 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
5453
Gavin Shan144be3d2014-01-23 12:27:34 +08005454 /* PCI device has been enabled */
5455 adapter->flags |= DEV_ENABLED;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005456 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
5457
Casey Leedomb0ba9d52017-08-15 11:23:26 +08005458 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
5459 * Ingress Packet Data to Free List Buffers in order to allow for
5460 * chipset performance optimizations between the Root Complex and
5461 * Memory Controllers. (Messages to the associated Ingress Queue
5462 * notifying new Packet Placement in the Free Lists Buffers will be
5463 * send without the Relaxed Ordering Attribute thus guaranteeing that
5464 * all preceding PCIe Transaction Layer Packets will be processed
5465 * first.) But some Root Complexes have various issues with Upstream
5466 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
5467 * The PCIe devices which under the Root Complexes will be cleared the
5468 * Relaxed Ordering bit in the configuration space, So we check our
5469 * PCIe configuration space to see if it's flagged with advice against
5470 * using Relaxed Ordering.
5471 */
5472 if (!pcie_relaxed_ordering_enabled(pdev))
5473 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
5474
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005475 spin_lock_init(&adapter->stats_lock);
5476 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07005477 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005478
5479 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305480 INIT_WORK(&adapter->db_full_task, process_db_full);
5481 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005482
5483 err = t4_prep_adapter(adapter);
5484 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305485 goto out_free_adapter;
5486
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005487
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305488 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305489 s_qpp = (QUEUESPERPAGEPF0_S +
5490 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305491 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305492 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5493 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005494 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5495
5496 /* Each segment size is 128B. Write coalescing is enabled only
5497 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5498 * queue is less no of segments that can be accommodated in
5499 * a page size.
5500 */
5501 if (qpp > num_seg) {
5502 dev_err(&pdev->dev,
5503 "Incorrect number of egress queues per page\n");
5504 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305505 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005506 }
5507 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5508 pci_resource_len(pdev, 2));
5509 if (!adapter->bar2) {
5510 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5511 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305512 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005513 }
5514 }
5515
Vipul Pandya636f9d32012-09-26 02:39:39 +00005516 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005517 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305518#ifdef CONFIG_DEBUG_FS
5519 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5520#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005521 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005522 if (err)
5523 goto out_unmap_bar;
5524
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305525 /* configure SGE_STAT_CFG_A to read WC stats */
5526 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305527 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5528 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5529 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305530
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005531 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005532 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5533 MAX_ETH_QSETS);
5534 if (!netdev) {
5535 err = -ENOMEM;
5536 goto out_free_dev;
5537 }
5538
5539 SET_NETDEV_DEV(netdev, &pdev->dev);
5540
5541 adapter->port[i] = netdev;
5542 pi = netdev_priv(netdev);
5543 pi->adapter = adapter;
5544 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005545 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005546 netdev->irq = pdev->irq;
5547
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005548 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5549 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5550 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305551 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5552 NETIF_F_HW_TC;
Ganesh Goudard0a12992018-01-10 18:15:26 +05305553
5554 if (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5)
5555 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
5556
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005557 if (highdma)
5558 netdev->hw_features |= NETIF_F_HIGHDMA;
5559 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005560 netdev->vlan_features = netdev->features & VLAN_FEAT;
5561
Jiri Pirko01789342011-08-16 06:29:00 +00005562 netdev->priv_flags |= IFF_UNICAST_FLT;
5563
Jarod Wilsond894be52016-10-20 13:55:16 -04005564 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305565 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005566 netdev->max_mtu = MAX_MTU;
5567
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005568 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005569#ifdef CONFIG_CHELSIO_T4_DCB
5570 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5571 cxgb4_dcb_state_init(netdev);
5572#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305573 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005574 }
5575
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305576 cxgb4_init_ethtool_dump(adapter);
5577
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005578 pci_set_drvdata(pdev, adapter);
5579
5580 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005581 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005582 if (err)
5583 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305584 } else if (adapter->params.nports == 1) {
5585 /* If we don't have a connection to the firmware -- possibly
5586 * because of an error -- grab the raw VPD parameters so we
5587 * can set the proper MAC Address on the debug network
5588 * interface that we've created.
5589 */
5590 u8 hw_addr[ETH_ALEN];
5591 u8 *na = adapter->params.vpd.na;
5592
5593 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5594 if (!err) {
5595 for (i = 0; i < ETH_ALEN; i++)
5596 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5597 hex2val(na[2 * i + 1]));
5598 t4_set_hw_addr(adapter, 0, hw_addr);
5599 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005600 }
5601
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305602 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005603 * soon as the first register_netdev completes.
5604 */
5605 cfg_queues(adapter);
5606
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305607 adapter->smt = t4_init_smt();
5608 if (!adapter->smt) {
5609 /* We tolerate a lack of SMT, giving up some functionality */
5610 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5611 }
5612
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305613 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005614 if (!adapter->l2t) {
5615 /* We tolerate a lack of L2T, giving up some functionality */
5616 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5617 adapter->params.offload = 0;
5618 }
5619
Anish Bhattb5a02f52015-01-14 15:17:34 -08005620#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305621 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5622 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5623 /* CLIP functionality is not present in hardware,
5624 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005625 */
5626 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305627 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005628 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305629 } else {
5630 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5631 adapter->clipt_end);
5632 if (!adapter->clipt) {
5633 /* We tolerate a lack of clip_table, giving up
5634 * some functionality
5635 */
5636 dev_warn(&pdev->dev,
5637 "could not allocate Clip table, continuing\n");
5638 adapter->params.offload = 0;
5639 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005640 }
5641#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305642
5643 for_each_port(adapter, i) {
5644 pi = adap2pinfo(adapter, i);
5645 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5646 if (!pi->sched_tbl)
5647 dev_warn(&pdev->dev,
5648 "could not activate scheduling on port %d\n",
5649 i);
5650 }
5651
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305652 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005653 dev_warn(&pdev->dev, "could not allocate TID table, "
5654 "continuing\n");
5655 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305656 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305657 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305658 if (!adapter->tc_u32)
5659 dev_warn(&pdev->dev,
5660 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305661
Kumar Sanghvi79e6d462017-11-01 08:53:04 +05305662 if (cxgb4_init_tc_flower(adapter))
5663 dev_warn(&pdev->dev,
5664 "could not offload tc flower, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005665 }
5666
Kumar Sanghvi5c312542017-11-01 08:53:00 +05305667 if (is_offload(adapter) || is_hashfilter(adapter)) {
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305668 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5669 u32 hash_base, hash_reg;
5670
5671 if (chip <= CHELSIO_T5) {
5672 hash_reg = LE_DB_TID_HASHBASE_A;
5673 hash_base = t4_read_reg(adapter, hash_reg);
5674 adapter->tids.hash_base = hash_base / 4;
5675 } else {
5676 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5677 hash_base = t4_read_reg(adapter, hash_reg);
5678 adapter->tids.hash_base = hash_base;
5679 }
5680 }
5681 }
5682
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005683 /* See what interrupts we'll be using */
5684 if (msi > 1 && enable_msix(adapter) == 0)
5685 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305686 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005687 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305688 if (msi > 1)
5689 free_msix_info(adapter);
5690 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005691
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305692 /* check for PCI Express bandwidth capabiltites */
5693 cxgb4_check_pcie_caps(adapter);
5694
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005695 err = init_rss(adapter);
5696 if (err)
5697 goto out_free_dev;
5698
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005699 /*
5700 * The card is now ready to go. If any errors occur during device
5701 * registration we do not fail the whole card but rather proceed only
5702 * with the ports we manage to register successfully. However we must
5703 * register at least one net device.
5704 */
5705 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005706 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305707 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005708 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5709 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5710
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305711 netif_carrier_off(adapter->port[i]);
5712
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005713 err = register_netdev(adapter->port[i]);
5714 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005715 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005716 adapter->chan_map[pi->tx_chan] = i;
5717 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005718 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005719 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005720 dev_err(&pdev->dev, "could not register any net devices\n");
5721 goto out_free_dev;
5722 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005723 if (err) {
5724 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5725 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005726 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005727
5728 if (cxgb4_debugfs_root) {
5729 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5730 cxgb4_debugfs_root);
5731 setup_debugfs(adapter);
5732 }
5733
David S. Miller88c51002011-10-07 13:38:43 -04005734 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5735 pdev->needs_freset = 1;
5736
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305737 if (is_uld(adapter)) {
5738 mutex_lock(&uld_mutex);
5739 list_add_tail(&adapter->list_node, &adapter_list);
5740 mutex_unlock(&uld_mutex);
5741 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005742
Atul Gupta9c33e422017-07-04 16:46:21 +05305743 if (!is_t4(adapter->params.chip))
5744 cxgb4_ptp_init(adapter);
5745
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305746 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305747 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305748 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305749
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005750 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005751 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305752 if (adapter->flags & USING_MSIX)
5753 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305754 if (adapter->num_uld || adapter->num_ofld_uld)
5755 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005756 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305757 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005758 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005759 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005760 if (adapter->workq)
5761 destroy_workqueue(adapter->workq);
5762
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305763 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005764 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305765 out_unmap_bar0:
5766 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005767 out_disable_device:
5768 pci_disable_pcie_error_reporting(pdev);
5769 pci_disable_device(pdev);
5770 out_release_regions:
5771 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005772 return err;
5773}
5774
Bill Pemberton91744942012-12-03 09:23:02 -05005775static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005776{
5777 struct adapter *adapter = pci_get_drvdata(pdev);
5778
Hariprasad Shenai78294512016-08-11 21:06:23 +05305779 if (!adapter) {
5780 pci_release_regions(pdev);
5781 return;
5782 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005783
Ganesh Goudare1f61982017-09-21 12:50:47 +05305784 adapter->flags |= SHUTTING_DOWN;
5785
Hariprasad Shenai78294512016-08-11 21:06:23 +05305786 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005787 int i;
5788
Anish Bhatt29aaee62014-08-20 13:44:06 -07005789 /* Tear down per-adapter Work Queue first since it can contain
5790 * references to our adapter data structure.
5791 */
5792 destroy_workqueue(adapter->workq);
5793
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005794 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005795 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005796 t4_uld_clean_up(adapter);
5797 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005798
Arjun Vynipadath8b4e6b32018-03-13 16:24:45 +05305799 adap_free_hma_mem(adapter);
5800
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305801 disable_interrupts(adapter);
5802
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005803 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005804 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005805 unregister_netdev(adapter->port[i]);
5806
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005807 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005808
Atul Gupta9c33e422017-07-04 16:46:21 +05305809 if (!is_t4(adapter->params.chip))
5810 cxgb4_ptp_stop(adapter);
5811
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005812 /* If we allocated filters, free up state associated with any
5813 * valid filters ...
5814 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305815 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005816
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005817 if (adapter->flags & FULL_INIT_DONE)
5818 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005819
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305820 if (adapter->flags & USING_MSIX)
5821 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305822 if (adapter->num_uld || adapter->num_ofld_uld)
5823 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005824 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005825#if IS_ENABLED(CONFIG_IPV6)
5826 t4_cleanup_clip_tbl(adapter);
5827#endif
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305828 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005829 iounmap(adapter->bar2);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305830 }
5831#ifdef CONFIG_PCI_IOV
5832 else {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305833 cxgb4_iov_configure(adapter->pdev, 0);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305834 }
5835#endif
Ganesh Goudarc4e43e12018-02-15 18:16:57 +05305836 iounmap(adapter->regs);
5837 pci_disable_pcie_error_reporting(pdev);
5838 if ((adapter->flags & DEV_ENABLED)) {
5839 pci_disable_device(pdev);
5840 adapter->flags &= ~DEV_ENABLED;
5841 }
5842 pci_release_regions(pdev);
5843 kfree(adapter->mbox_log);
5844 synchronize_rcu();
5845 kfree(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005846}
5847
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305848/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5849 * delivery. This is essentially a stripped down version of the PCI remove()
5850 * function where we do the minimal amount of work necessary to shutdown any
5851 * further activity.
5852 */
5853static void shutdown_one(struct pci_dev *pdev)
5854{
5855 struct adapter *adapter = pci_get_drvdata(pdev);
5856
5857 /* As with remove_one() above (see extended comment), we only want do
5858 * do cleanup on PCI Devices which went all the way through init_one()
5859 * ...
5860 */
5861 if (!adapter) {
5862 pci_release_regions(pdev);
5863 return;
5864 }
5865
Ganesh Goudare1f61982017-09-21 12:50:47 +05305866 adapter->flags |= SHUTTING_DOWN;
5867
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305868 if (adapter->pf == 4) {
5869 int i;
5870
5871 for_each_port(adapter, i)
5872 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5873 cxgb_close(adapter->port[i]);
5874
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005875 if (is_uld(adapter)) {
5876 detach_ulds(adapter);
5877 t4_uld_clean_up(adapter);
5878 }
5879
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305880 disable_interrupts(adapter);
5881 disable_msi(adapter);
5882
5883 t4_sge_stop(adapter);
5884 if (adapter->flags & FW_OK)
5885 t4_fw_bye(adapter, adapter->mbox);
5886 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305887}
5888
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005889static struct pci_driver cxgb4_driver = {
5890 .name = KBUILD_MODNAME,
5891 .id_table = cxgb4_pci_tbl,
5892 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005893 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305894 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305895#ifdef CONFIG_PCI_IOV
5896 .sriov_configure = cxgb4_iov_configure,
5897#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005898 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005899};
5900
5901static int __init cxgb4_init_module(void)
5902{
5903 int ret;
5904
5905 /* Debugfs support is optional, just warn if this fails */
5906 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5907 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005908 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005909
5910 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005911 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005912 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305913
Anish Bhatt1bb60372014-10-14 20:07:22 -07005914#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005915 if (!inet6addr_registered) {
5916 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5917 inet6addr_registered = true;
5918 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005919#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305920
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005921 return ret;
5922}
5923
5924static void __exit cxgb4_cleanup_module(void)
5925{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005926#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305927 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005928 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5929 inet6addr_registered = false;
5930 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005931#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005932 pci_unregister_driver(&cxgb4_driver);
5933 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005934}
5935
5936module_init(cxgb4_init_module);
5937module_exit(cxgb4_cleanup_module);