blob: 1e3cd8abc56d80cb53a7ff6ea76084c599280477 [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 Goudar5e78f7f2017-01-06 16:51:46 +0530213 case 100000:
214 s = "100Gbps";
215 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530216 default:
217 pr_info("%s: unsupported speed: %d\n",
218 dev->name, p->link_cfg.speed);
219 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000220 }
221
222 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
223 fc[p->link_cfg.fc]);
224 }
225}
226
Anish Bhatt688848b2014-06-19 21:37:13 -0700227#ifdef CONFIG_CHELSIO_T4_DCB
228/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
229static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
230{
231 struct port_info *pi = netdev_priv(dev);
232 struct adapter *adap = pi->adapter;
233 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
234 int i;
235
236 /* We use a simple mapping of Port TX Queue Index to DCB
237 * Priority when we're enabling DCB.
238 */
239 for (i = 0; i < pi->nqsets; i++, txq++) {
240 u32 name, value;
241 int err;
242
Hariprasad Shenai51678652014-11-21 12:52:02 +0530243 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
244 FW_PARAMS_PARAM_X_V(
245 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
246 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700247 value = enable ? i : 0xffffffff;
248
249 /* Since we can be called while atomic (from "interrupt
250 * level") we need to issue the Set Parameters Commannd
251 * without sleeping (timeout < 0).
252 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530253 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530254 &name, &value,
255 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700256
257 if (err)
258 dev_err(adap->pdev_dev,
259 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
260 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700261 else
262 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700263 }
264}
Anish Bhatt688848b2014-06-19 21:37:13 -0700265
Baoyou Xie50935852016-09-25 14:10:09 +0800266static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530267{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530268 struct port_info *pi = netdev_priv(dev);
269
270 if (!pi->dcb.enabled)
271 return 0;
272
273 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
274 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530275}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200276#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530277
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000278void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
279{
280 struct net_device *dev = adapter->port[port_id];
281
282 /* Skip changes from disabled ports. */
283 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
284 if (link_stat)
285 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700286 else {
287#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530288 if (cxgb4_dcb_enabled(dev)) {
Ganesh Goudarba581f72017-09-23 16:07:28 +0530289 cxgb4_dcb_reset(dev);
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530290 dcb_tx_queue_prio_enable(dev, false);
291 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700292#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000293 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700294 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000295
296 link_report(dev);
297 }
298}
299
300void t4_os_portmod_changed(const struct adapter *adap, int port_id)
301{
302 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000303 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000304 };
305
306 const struct net_device *dev = adap->port[port_id];
307 const struct port_info *pi = netdev_priv(dev);
308
309 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
310 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000311 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000312 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530313 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
314 netdev_info(dev, "%s: unsupported port module inserted\n",
315 dev->name);
316 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
317 netdev_info(dev, "%s: unknown port module inserted\n",
318 dev->name);
319 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
320 netdev_info(dev, "%s: transceiver module error\n", dev->name);
321 else
322 netdev_info(dev, "%s: unknown module type %d inserted\n",
323 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000324}
325
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530326int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
327module_param(dbfifo_int_thresh, int, 0644);
328MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
329
Vipul Pandya404d9e32012-10-08 02:59:43 +0000330/*
331 * usecs to sleep while draining the dbfifo
332 */
333static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530334module_param(dbfifo_drain_delay, int, 0644);
335MODULE_PARM_DESC(dbfifo_drain_delay,
336 "usecs to sleep while draining the dbfifo");
337
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530338static inline int cxgb4_set_addr_hash(struct port_info *pi)
339{
340 struct adapter *adap = pi->adapter;
341 u64 vec = 0;
342 bool ucast = false;
343 struct hash_mac_addr *entry;
344
345 /* Calculate the hash vector for the updated list and program it */
346 list_for_each_entry(entry, &adap->mac_hlist, list) {
347 ucast |= is_unicast_ether_addr(entry->addr);
348 vec |= (1ULL << hash_mac_addr(entry->addr));
349 }
350 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
351 vec, false);
352}
353
354static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
355{
356 struct port_info *pi = netdev_priv(netdev);
357 struct adapter *adap = pi->adapter;
358 int ret;
359 u64 mhash = 0;
360 u64 uhash = 0;
361 bool free = false;
362 bool ucast = is_unicast_ether_addr(mac_addr);
363 const u8 *maclist[1] = {mac_addr};
364 struct hash_mac_addr *new_entry;
365
366 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
367 NULL, ucast ? &uhash : &mhash, false);
368 if (ret < 0)
369 goto out;
370 /* if hash != 0, then add the addr to hash addr list
371 * so on the end we will calculate the hash for the
372 * list and program it
373 */
374 if (uhash || mhash) {
375 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
376 if (!new_entry)
377 return -ENOMEM;
378 ether_addr_copy(new_entry->addr, mac_addr);
379 list_add_tail(&new_entry->list, &adap->mac_hlist);
380 ret = cxgb4_set_addr_hash(pi);
381 }
382out:
383 return ret < 0 ? ret : 0;
384}
385
386static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
387{
388 struct port_info *pi = netdev_priv(netdev);
389 struct adapter *adap = pi->adapter;
390 int ret;
391 const u8 *maclist[1] = {mac_addr};
392 struct hash_mac_addr *entry, *tmp;
393
394 /* If the MAC address to be removed is in the hash addr
395 * list, delete it from the list and update hash vector
396 */
397 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
398 if (ether_addr_equal(entry->addr, mac_addr)) {
399 list_del(&entry->list);
400 kfree(entry);
401 return cxgb4_set_addr_hash(pi);
402 }
403 }
404
405 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
406 return ret < 0 ? -EINVAL : 0;
407}
408
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000409/*
410 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
411 * If @mtu is -1 it is left unchanged.
412 */
413static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
414{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000415 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530416 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000417
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530418 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
419 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530420
421 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
422 (dev->flags & IFF_PROMISC) ? 1 : 0,
423 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
424 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000425}
426
427/**
428 * link_start - enable a port
429 * @dev: the port to enable
430 *
431 * Performs the MAC and PHY actions needed to enable a port.
432 */
433static int link_start(struct net_device *dev)
434{
435 int ret;
436 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530437 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000438
439 /*
440 * We do not set address filters and promiscuity here, the stack does
441 * that step explicitly.
442 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000443 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000444 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000445 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000446 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000447 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000448 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000449 if (ret >= 0) {
450 pi->xact_addr_filt = ret;
451 ret = 0;
452 }
453 }
454 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530455 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000456 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700457 if (ret == 0) {
458 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700459 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
460 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700461 local_bh_enable();
462 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700463
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000464 return ret;
465}
466
Anish Bhatt688848b2014-06-19 21:37:13 -0700467#ifdef CONFIG_CHELSIO_T4_DCB
468/* Handle a Data Center Bridging update message from the firmware. */
469static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
470{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530471 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530472 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700473 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
474 int new_dcb_enabled;
475
476 cxgb4_dcb_handle_fw_update(adap, pcmd);
477 new_dcb_enabled = cxgb4_dcb_enabled(dev);
478
479 /* If the DCB has become enabled or disabled on the port then we're
480 * going to need to set up/tear down DCB Priority parameters for the
481 * TX Queues associated with the port.
482 */
483 if (new_dcb_enabled != old_dcb_enabled)
484 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
485}
486#endif /* CONFIG_CHELSIO_T4_DCB */
487
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000488/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000489 */
490static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
491 const struct pkt_gl *gl)
492{
493 u8 opcode = ((const struct rss_header *)rsp)->opcode;
494
495 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000496
497 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
498 */
499 if (unlikely(opcode == CPL_FW4_MSG &&
500 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
501 rsp++;
502 opcode = ((const struct rss_header *)rsp)->opcode;
503 rsp++;
504 if (opcode != CPL_SGE_EGR_UPDATE) {
505 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
506 , opcode);
507 goto out;
508 }
509 }
510
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000511 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
512 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800513 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000514 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000515
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000516 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000517 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530518 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000519 struct sge_eth_txq *eq;
520
521 eq = container_of(txq, struct sge_eth_txq, q);
522 netif_tx_wake_queue(eq->txq);
523 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530524 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000525
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530526 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000527 tasklet_schedule(&oq->qresume_tsk);
528 }
529 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
530 const struct cpl_fw6_msg *p = (void *)rsp;
531
Anish Bhatt688848b2014-06-19 21:37:13 -0700532#ifdef CONFIG_CHELSIO_T4_DCB
533 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530534 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700535 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530536 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700537
538 if (cmd == FW_PORT_CMD &&
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530539 (action == FW_PORT_ACTION_GET_PORT_INFO ||
540 action == FW_PORT_ACTION_GET_PORT_INFO32)) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530541 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700542 be32_to_cpu(pcmd->op_to_portid));
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530543 struct net_device *dev;
544 int dcbxdis, state_input;
545
546 dev = q->adap->port[q->adap->chan_map[port]];
547 dcbxdis = (action == FW_PORT_ACTION_GET_PORT_INFO
548 ? !!(pcmd->u.info.dcbxdis_pkd &
549 FW_PORT_CMD_DCBXDIS_F)
550 : !!(pcmd->u.info32.lstatus32_to_cbllen32 &
551 FW_PORT_CMD_DCBXDIS32_F));
552 state_input = (dcbxdis
553 ? CXGB4_DCB_INPUT_FW_DISABLED
554 : CXGB4_DCB_INPUT_FW_ENABLED);
Anish Bhatt688848b2014-06-19 21:37:13 -0700555
556 cxgb4_dcb_state_fsm(dev, state_input);
557 }
558
559 if (cmd == FW_PORT_CMD &&
560 action == FW_PORT_ACTION_L2_DCB_CFG)
561 dcb_rpl(q->adap, pcmd);
562 else
563#endif
564 if (p->type == 0)
565 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000566 } else if (opcode == CPL_L2T_WRITE_RPL) {
567 const struct cpl_l2t_write_rpl *p = (void *)rsp;
568
569 do_l2t_write_rpl(q->adap, p);
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +0530570 } else if (opcode == CPL_SMT_WRITE_RPL) {
571 const struct cpl_smt_write_rpl *p = (void *)rsp;
572
573 do_smt_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000574 } else if (opcode == CPL_SET_TCB_RPL) {
575 const struct cpl_set_tcb_rpl *p = (void *)rsp;
576
577 filter_rpl(q->adap, p);
Kumar Sanghvi12b276f2017-11-01 08:53:01 +0530578 } else if (opcode == CPL_ACT_OPEN_RPL) {
579 const struct cpl_act_open_rpl *p = (void *)rsp;
580
581 hash_filter_rpl(q->adap, p);
Kumar Sanghvi3b0b3be2017-11-01 08:53:02 +0530582 } else if (opcode == CPL_ABORT_RPL_RSS) {
583 const struct cpl_abort_rpl_rss *p = (void *)rsp;
584
585 hash_del_filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000586 } else
587 dev_err(q->adap->pdev_dev,
588 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000589out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000590 return 0;
591}
592
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000593static void disable_msi(struct adapter *adapter)
594{
595 if (adapter->flags & USING_MSIX) {
596 pci_disable_msix(adapter->pdev);
597 adapter->flags &= ~USING_MSIX;
598 } else if (adapter->flags & USING_MSI) {
599 pci_disable_msi(adapter->pdev);
600 adapter->flags &= ~USING_MSI;
601 }
602}
603
604/*
605 * Interrupt handler for non-data events used with MSI-X.
606 */
607static irqreturn_t t4_nondata_intr(int irq, void *cookie)
608{
609 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530610 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000611
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530612 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000613 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530614 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000615 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530616 if (adap->flags & MASTER_PF)
617 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000618 return IRQ_HANDLED;
619}
620
621/*
622 * Name the MSI-X interrupts.
623 */
624static void name_msix_vecs(struct adapter *adap)
625{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000626 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000627
628 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000629 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000630
631 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000632 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
633 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000634
635 /* Ethernet queues */
636 for_each_port(adap, j) {
637 struct net_device *d = adap->port[j];
638 const struct port_info *pi = netdev_priv(d);
639
Dimitris Michailidisba278162010-12-14 21:36:50 +0000640 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000641 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
642 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000643 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000644}
645
646static int request_msix_queue_irqs(struct adapter *adap)
647{
648 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530649 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530650 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000651
652 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
653 adap->msix_info[1].desc, &s->fw_evtq);
654 if (err)
655 return err;
656
657 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000658 err = request_irq(adap->msix_info[msi_index].vec,
659 t4_sge_intr_msix, 0,
660 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000661 &s->ethrxq[ethqidx].rspq);
662 if (err)
663 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000664 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000665 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000666 return 0;
667
668unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000669 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000670 free_irq(adap->msix_info[--msi_index].vec,
671 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000672 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
673 return err;
674}
675
676static void free_msix_queue_irqs(struct adapter *adap)
677{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000678 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000679 struct sge *s = &adap->sge;
680
681 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
682 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000683 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000684}
685
686/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530687 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000688 * @pi: the port
689 * @queues: array of queue indices for RSS
690 *
691 * Sets up the portion of the HW RSS table for the port's VI to distribute
692 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530693 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000694 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530695int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000696{
697 u16 *rss;
698 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530699 struct adapter *adapter = pi->adapter;
700 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000701
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530702 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000703 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
704 if (!rss)
705 return -ENOMEM;
706
707 /* map the queue indices to queue ids */
708 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530709 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000710
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530711 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000712 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530713 /* If Tunnel All Lookup isn't specified in the global RSS
714 * Configuration, then we need to specify a default Ingress
715 * Queue for any ingress packets which aren't hashed. We'll
716 * use our first ingress queue ...
717 */
718 if (!err)
719 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
720 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
721 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
722 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
723 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
724 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
725 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000726 kfree(rss);
727 return err;
728}
729
730/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000731 * setup_rss - configure RSS
732 * @adap: the adapter
733 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000734 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000735 */
736static int setup_rss(struct adapter *adap)
737{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530738 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000739
740 for_each_port(adap, i) {
741 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000742
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530743 /* Fill default values with equal distribution */
744 for (j = 0; j < pi->rss_size; j++)
745 pi->rss[j] = j % pi->nqsets;
746
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530747 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000748 if (err)
749 return err;
750 }
751 return 0;
752}
753
754/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000755 * Return the channel of the ingress queue with the given qid.
756 */
757static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
758{
759 qid -= p->ingr_start;
760 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
761}
762
763/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000764 * Wait until all NAPI handlers are descheduled.
765 */
766static void quiesce_rx(struct adapter *adap)
767{
768 int i;
769
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530770 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000771 struct sge_rspq *q = adap->sge.ingr_map[i];
772
Eric Dumazet5226b7912017-02-02 11:44:27 -0800773 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000774 napi_disable(&q->napi);
775 }
776}
777
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530778/* Disable interrupt and napi handler */
779static void disable_interrupts(struct adapter *adap)
780{
781 if (adap->flags & FULL_INIT_DONE) {
782 t4_intr_disable(adap);
783 if (adap->flags & USING_MSIX) {
784 free_msix_queue_irqs(adap);
785 free_irq(adap->msix_info[0].vec, adap);
786 } else {
787 free_irq(adap->pdev->irq, adap);
788 }
789 quiesce_rx(adap);
790 }
791}
792
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000793/*
794 * Enable NAPI scheduling and interrupt generation for all Rx queues.
795 */
796static void enable_rx(struct adapter *adap)
797{
798 int i;
799
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530800 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000801 struct sge_rspq *q = adap->sge.ingr_map[i];
802
803 if (!q)
804 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800805 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000806 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800807
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000808 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530809 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
810 SEINTARM_V(q->intr_params) |
811 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000812 }
813}
814
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530815
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530816static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000817{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000818 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530819 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000820
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530821 bitmap_zero(s->starving_fl, s->egr_sz);
822 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000823
824 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530825 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000826 else {
827 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530828 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000829 if (err)
830 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530831 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000832 }
833
834 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530835 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530836 if (err)
837 t4_free_sge_resources(adap);
838 return err;
839}
840
841/**
842 * setup_sge_queues - configure SGE Tx/Rx/response queues
843 * @adap: the adapter
844 *
845 * Determines how many sets of SGE queues to use and initializes them.
846 * We support multiple queue sets per port if we have MSI-X, otherwise
847 * just one queue set per port.
848 */
849static int setup_sge_queues(struct adapter *adap)
850{
851 int err, i, j;
852 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530853 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530854 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000855
Ganesh Goudard427cae2017-06-16 15:36:09 +0530856 if (is_uld(adap))
857 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
858
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000859 for_each_port(adap, i) {
860 struct net_device *dev = adap->port[i];
861 struct port_info *pi = netdev_priv(dev);
862 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
863 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
864
865 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530866 if (adap->msi_idx > 0)
867 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000868 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530869 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530870 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530871 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530872 t4_get_tp_ch_map(adap,
873 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000874 if (err)
875 goto freeout;
876 q->rspq.idx = j;
877 memset(&q->stats, 0, sizeof(q->stats));
878 }
879 for (j = 0; j < pi->nqsets; j++, t++) {
880 err = t4_sge_alloc_eth_txq(adap, t, dev,
881 netdev_get_tx_queue(dev, j),
882 s->fw_evtq.cntxt_id);
883 if (err)
884 goto freeout;
885 }
886 }
887
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000888 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530889 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000890 * have RDMA queues, and that's the right value.
891 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530892 if (rxq_info)
893 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
894
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000895 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530896 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000897 if (err)
898 goto freeout;
899 }
900
Atul Guptaa45695042017-07-04 16:46:20 +0530901 if (!is_t4(adap->params.chip)) {
902 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
903 netdev_get_tx_queue(adap->port[0], 0)
904 , s->fw_evtq.cntxt_id);
905 if (err)
906 goto freeout;
907 }
908
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530909 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530910 MPS_TRC_RSS_CONTROL_A :
911 MPS_T5_TRC_RSS_CONTROL_A,
912 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
913 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000914 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530915freeout:
916 t4_free_sge_resources(adap);
917 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000918}
919
Anish Bhatt688848b2014-06-19 21:37:13 -0700920static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
921 void *accel_priv, select_queue_fallback_t fallback)
922{
923 int txq;
924
925#ifdef CONFIG_CHELSIO_T4_DCB
926 /* If a Data Center Bridging has been successfully negotiated on this
927 * link then we'll use the skb's priority to map it to a TX Queue.
928 * The skb's priority is determined via the VLAN Tag Priority Code
929 * Point field.
930 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530931 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700932 u16 vlan_tci;
933 int err;
934
935 err = vlan_get_tag(skb, &vlan_tci);
936 if (unlikely(err)) {
937 if (net_ratelimit())
938 netdev_warn(dev,
939 "TX Packet without VLAN Tag on DCB Link\n");
940 txq = 0;
941 } else {
942 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530943#ifdef CONFIG_CHELSIO_T4_FCOE
944 if (skb->protocol == htons(ETH_P_FCOE))
945 txq = skb->priority & 0x7;
946#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700947 }
948 return txq;
949 }
950#endif /* CONFIG_CHELSIO_T4_DCB */
951
952 if (select_queue) {
953 txq = (skb_rx_queue_recorded(skb)
954 ? skb_get_rx_queue(skb)
955 : smp_processor_id());
956
957 while (unlikely(txq >= dev->real_num_tx_queues))
958 txq -= dev->real_num_tx_queues;
959
960 return txq;
961 }
962
963 return fallback(dev, skb) % dev->real_num_tx_queues;
964}
965
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000966static int closest_timer(const struct sge *s, int time)
967{
968 int i, delta, match = 0, min_delta = INT_MAX;
969
970 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
971 delta = time - s->timer_val[i];
972 if (delta < 0)
973 delta = -delta;
974 if (delta < min_delta) {
975 min_delta = delta;
976 match = i;
977 }
978 }
979 return match;
980}
981
982static int closest_thres(const struct sge *s, int thres)
983{
984 int i, delta, match = 0, min_delta = INT_MAX;
985
986 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
987 delta = thres - s->counter_val[i];
988 if (delta < 0)
989 delta = -delta;
990 if (delta < min_delta) {
991 min_delta = delta;
992 match = i;
993 }
994 }
995 return match;
996}
997
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000998/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530999 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001000 * @q: the Rx queue
1001 * @us: the hold-off time in us, or 0 to disable timer
1002 * @cnt: the hold-off packet count, or 0 to disable counter
1003 *
1004 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1005 * one of the two needs to be enabled for the queue to generate interrupts.
1006 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301007int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1008 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001009{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301010 struct adapter *adap = q->adap;
1011
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001012 if ((us | cnt) == 0)
1013 cnt = 1;
1014
1015 if (cnt) {
1016 int err;
1017 u32 v, new_idx;
1018
1019 new_idx = closest_thres(&adap->sge, cnt);
1020 if (q->desc && q->pktcnt_idx != new_idx) {
1021 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301022 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1023 FW_PARAMS_PARAM_X_V(
1024 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1025 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301026 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1027 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001028 if (err)
1029 return err;
1030 }
1031 q->pktcnt_idx = new_idx;
1032 }
1033
1034 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301035 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001036 return 0;
1037}
1038
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001039static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001040{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001041 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001042 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001043 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001044
Patrick McHardyf6469682013-04-19 02:04:27 +00001045 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001046 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001047
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301048 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001049 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001050 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001051 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001052 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001053 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001054}
1055
Bill Pemberton91744942012-12-03 09:23:02 -05001056static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001057{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001058 if (IS_ERR_OR_NULL(adap->debugfs_root))
1059 return -1;
1060
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301061#ifdef CONFIG_DEBUG_FS
1062 t4_setup_debugfs(adap);
1063#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001064 return 0;
1065}
1066
1067/*
1068 * upper-layer driver support
1069 */
1070
1071/*
1072 * Allocate an active-open TID and set it to the supplied value.
1073 */
1074int cxgb4_alloc_atid(struct tid_info *t, void *data)
1075{
1076 int atid = -1;
1077
1078 spin_lock_bh(&t->atid_lock);
1079 if (t->afree) {
1080 union aopen_entry *p = t->afree;
1081
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001082 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001083 t->afree = p->next;
1084 p->data = data;
1085 t->atids_in_use++;
1086 }
1087 spin_unlock_bh(&t->atid_lock);
1088 return atid;
1089}
1090EXPORT_SYMBOL(cxgb4_alloc_atid);
1091
1092/*
1093 * Release an active-open TID.
1094 */
1095void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1096{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001097 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001098
1099 spin_lock_bh(&t->atid_lock);
1100 p->next = t->afree;
1101 t->afree = p;
1102 t->atids_in_use--;
1103 spin_unlock_bh(&t->atid_lock);
1104}
1105EXPORT_SYMBOL(cxgb4_free_atid);
1106
1107/*
1108 * Allocate a server TID and set it to the supplied value.
1109 */
1110int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1111{
1112 int stid;
1113
1114 spin_lock_bh(&t->stid_lock);
1115 if (family == PF_INET) {
1116 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1117 if (stid < t->nstids)
1118 __set_bit(stid, t->stid_bmap);
1119 else
1120 stid = -1;
1121 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301122 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001123 if (stid < 0)
1124 stid = -1;
1125 }
1126 if (stid >= 0) {
1127 t->stid_tab[stid].data = data;
1128 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301129 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1130 * This is equivalent to 4 TIDs. With CLIP enabled it
1131 * needs 2 TIDs.
1132 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301133 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301134 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301135 t->v6_stids_in_use += 2;
1136 } else {
1137 t->stids_in_use++;
1138 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001139 }
1140 spin_unlock_bh(&t->stid_lock);
1141 return stid;
1142}
1143EXPORT_SYMBOL(cxgb4_alloc_stid);
1144
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001145/* Allocate a server filter TID and set it to the supplied value.
1146 */
1147int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1148{
1149 int stid;
1150
1151 spin_lock_bh(&t->stid_lock);
1152 if (family == PF_INET) {
1153 stid = find_next_zero_bit(t->stid_bmap,
1154 t->nstids + t->nsftids, t->nstids);
1155 if (stid < (t->nstids + t->nsftids))
1156 __set_bit(stid, t->stid_bmap);
1157 else
1158 stid = -1;
1159 } else {
1160 stid = -1;
1161 }
1162 if (stid >= 0) {
1163 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301164 stid -= t->nstids;
1165 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301166 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001167 }
1168 spin_unlock_bh(&t->stid_lock);
1169 return stid;
1170}
1171EXPORT_SYMBOL(cxgb4_alloc_sftid);
1172
1173/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001174 */
1175void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1176{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301177 /* Is it a server filter TID? */
1178 if (t->nsftids && (stid >= t->sftid_base)) {
1179 stid -= t->sftid_base;
1180 stid += t->nstids;
1181 } else {
1182 stid -= t->stid_base;
1183 }
1184
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001185 spin_lock_bh(&t->stid_lock);
1186 if (family == PF_INET)
1187 __clear_bit(stid, t->stid_bmap);
1188 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301189 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001190 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301191 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301192 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301193 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301194 t->v6_stids_in_use -= 2;
1195 } else {
1196 t->stids_in_use--;
1197 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301198 } else {
1199 t->sftids_in_use--;
1200 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301201
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001202 spin_unlock_bh(&t->stid_lock);
1203}
1204EXPORT_SYMBOL(cxgb4_free_stid);
1205
1206/*
1207 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1208 */
1209static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1210 unsigned int tid)
1211{
1212 struct cpl_tid_release *req;
1213
1214 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001215 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001216 INIT_TP_WR(req, tid);
1217 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1218}
1219
1220/*
1221 * Queue a TID release request and if necessary schedule a work queue to
1222 * process it.
1223 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001224static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1225 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001226{
1227 void **p = &t->tid_tab[tid];
1228 struct adapter *adap = container_of(t, struct adapter, tids);
1229
1230 spin_lock_bh(&adap->tid_release_lock);
1231 *p = adap->tid_release_head;
1232 /* Low 2 bits encode the Tx channel number */
1233 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1234 if (!adap->tid_release_task_busy) {
1235 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001236 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001237 }
1238 spin_unlock_bh(&adap->tid_release_lock);
1239}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001240
1241/*
1242 * Process the list of pending TID release requests.
1243 */
1244static void process_tid_release_list(struct work_struct *work)
1245{
1246 struct sk_buff *skb;
1247 struct adapter *adap;
1248
1249 adap = container_of(work, struct adapter, tid_release_task);
1250
1251 spin_lock_bh(&adap->tid_release_lock);
1252 while (adap->tid_release_head) {
1253 void **p = adap->tid_release_head;
1254 unsigned int chan = (uintptr_t)p & 3;
1255 p = (void *)p - chan;
1256
1257 adap->tid_release_head = *p;
1258 *p = NULL;
1259 spin_unlock_bh(&adap->tid_release_lock);
1260
1261 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1262 GFP_KERNEL)))
1263 schedule_timeout_uninterruptible(1);
1264
1265 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1266 t4_ofld_send(adap, skb);
1267 spin_lock_bh(&adap->tid_release_lock);
1268 }
1269 adap->tid_release_task_busy = false;
1270 spin_unlock_bh(&adap->tid_release_lock);
1271}
1272
1273/*
1274 * Release a TID and inform HW. If we are unable to allocate the release
1275 * message we defer to a work queue.
1276 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301277void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1278 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001279{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001280 struct sk_buff *skb;
1281 struct adapter *adap = container_of(t, struct adapter, tids);
1282
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301283 WARN_ON(tid >= t->ntids);
1284
1285 if (t->tid_tab[tid]) {
1286 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301287 atomic_dec(&t->conns_in_use);
1288 if (t->hash_base && (tid >= t->hash_base)) {
1289 if (family == AF_INET6)
1290 atomic_sub(2, &t->hash_tids_in_use);
1291 else
1292 atomic_dec(&t->hash_tids_in_use);
1293 } else {
1294 if (family == AF_INET6)
1295 atomic_sub(2, &t->tids_in_use);
1296 else
1297 atomic_dec(&t->tids_in_use);
1298 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301299 }
1300
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001301 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1302 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001303 mk_tid_release(skb, chan, tid);
1304 t4_ofld_send(adap, skb);
1305 } else
1306 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001307}
1308EXPORT_SYMBOL(cxgb4_remove_tid);
1309
1310/*
1311 * Allocate and initialize the TID tables. Returns 0 on success.
1312 */
1313static int tid_init(struct tid_info *t)
1314{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301315 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301316 unsigned int max_ftids = t->nftids + t->nsftids;
1317 unsigned int natids = t->natids;
1318 unsigned int stid_bmap_size;
1319 unsigned int ftid_bmap_size;
1320 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001321
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001322 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301323 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001324 size = t->ntids * sizeof(*t->tid_tab) +
1325 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001326 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001327 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001328 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301329 max_ftids * sizeof(*t->ftid_tab) +
1330 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001331
Michal Hocko752ade62017-05-08 15:57:27 -07001332 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001333 if (!t->tid_tab)
1334 return -ENOMEM;
1335
1336 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1337 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001338 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001339 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301340 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001341 spin_lock_init(&t->stid_lock);
1342 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301343 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001344
1345 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301346 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301347 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001348 t->afree = NULL;
1349 t->atids_in_use = 0;
1350 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301351 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301352 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001353
1354 /* Setup the free list for atid_tab and clear the stid bitmap. */
1355 if (natids) {
1356 while (--natids)
1357 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1358 t->afree = t->atid_tab;
1359 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301360
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301361 if (is_offload(adap)) {
1362 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1363 /* Reserve stid 0 for T4/T5 adapters */
1364 if (!t->stid_base &&
1365 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1366 __set_bit(0, t->stid_bmap);
1367 }
1368
1369 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001370 return 0;
1371}
1372
1373/**
1374 * cxgb4_create_server - create an IP server
1375 * @dev: the device
1376 * @stid: the server TID
1377 * @sip: local IP address to bind server to
1378 * @sport: the server's TCP port
1379 * @queue: queue to direct messages from this server to
1380 *
1381 * Create an IP server for the given port and address.
1382 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1383 */
1384int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001385 __be32 sip, __be16 sport, __be16 vlan,
1386 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001387{
1388 unsigned int chan;
1389 struct sk_buff *skb;
1390 struct adapter *adap;
1391 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301392 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001393
1394 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1395 if (!skb)
1396 return -ENOMEM;
1397
1398 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001399 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001400 INIT_TP_WR(req, 0);
1401 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1402 req->local_port = sport;
1403 req->peer_port = htons(0);
1404 req->local_ip = sip;
1405 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001406 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001407 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001408 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1409 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301410 ret = t4_mgmt_tx(adap, skb);
1411 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001412}
1413EXPORT_SYMBOL(cxgb4_create_server);
1414
Vipul Pandya80f40c12013-07-04 16:10:45 +05301415/* cxgb4_create_server6 - create an IPv6 server
1416 * @dev: the device
1417 * @stid: the server TID
1418 * @sip: local IPv6 address to bind server to
1419 * @sport: the server's TCP port
1420 * @queue: queue to direct messages from this server to
1421 *
1422 * Create an IPv6 server for the given port and address.
1423 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1424 */
1425int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1426 const struct in6_addr *sip, __be16 sport,
1427 unsigned int queue)
1428{
1429 unsigned int chan;
1430 struct sk_buff *skb;
1431 struct adapter *adap;
1432 struct cpl_pass_open_req6 *req;
1433 int ret;
1434
1435 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1436 if (!skb)
1437 return -ENOMEM;
1438
1439 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001440 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301441 INIT_TP_WR(req, 0);
1442 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1443 req->local_port = sport;
1444 req->peer_port = htons(0);
1445 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1446 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1447 req->peer_ip_hi = cpu_to_be64(0);
1448 req->peer_ip_lo = cpu_to_be64(0);
1449 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001450 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001451 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1452 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301453 ret = t4_mgmt_tx(adap, skb);
1454 return net_xmit_eval(ret);
1455}
1456EXPORT_SYMBOL(cxgb4_create_server6);
1457
1458int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1459 unsigned int queue, bool ipv6)
1460{
1461 struct sk_buff *skb;
1462 struct adapter *adap;
1463 struct cpl_close_listsvr_req *req;
1464 int ret;
1465
1466 adap = netdev2adap(dev);
1467
1468 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1469 if (!skb)
1470 return -ENOMEM;
1471
Johannes Berg4df864c2017-06-16 14:29:21 +02001472 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301473 INIT_TP_WR(req, 0);
1474 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001475 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1476 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301477 ret = t4_mgmt_tx(adap, skb);
1478 return net_xmit_eval(ret);
1479}
1480EXPORT_SYMBOL(cxgb4_remove_server);
1481
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001482/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001483 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1484 * @mtus: the HW MTU table
1485 * @mtu: the target MTU
1486 * @idx: index of selected entry in the MTU table
1487 *
1488 * Returns the index and the value in the HW MTU table that is closest to
1489 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1490 * table, in which case that smallest available value is selected.
1491 */
1492unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1493 unsigned int *idx)
1494{
1495 unsigned int i = 0;
1496
1497 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1498 ++i;
1499 if (idx)
1500 *idx = i;
1501 return mtus[i];
1502}
1503EXPORT_SYMBOL(cxgb4_best_mtu);
1504
1505/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301506 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1507 * @mtus: the HW MTU table
1508 * @header_size: Header Size
1509 * @data_size_max: maximum Data Segment Size
1510 * @data_size_align: desired Data Segment Size Alignment (2^N)
1511 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1512 *
1513 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1514 * MTU Table based solely on a Maximum MTU parameter, we break that
1515 * parameter up into a Header Size and Maximum Data Segment Size, and
1516 * provide a desired Data Segment Size Alignment. If we find an MTU in
1517 * the Hardware MTU Table which will result in a Data Segment Size with
1518 * the requested alignment _and_ that MTU isn't "too far" from the
1519 * closest MTU, then we'll return that rather than the closest MTU.
1520 */
1521unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1522 unsigned short header_size,
1523 unsigned short data_size_max,
1524 unsigned short data_size_align,
1525 unsigned int *mtu_idxp)
1526{
1527 unsigned short max_mtu = header_size + data_size_max;
1528 unsigned short data_size_align_mask = data_size_align - 1;
1529 int mtu_idx, aligned_mtu_idx;
1530
1531 /* Scan the MTU Table till we find an MTU which is larger than our
1532 * Maximum MTU or we reach the end of the table. Along the way,
1533 * record the last MTU found, if any, which will result in a Data
1534 * Segment Length matching the requested alignment.
1535 */
1536 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1537 unsigned short data_size = mtus[mtu_idx] - header_size;
1538
1539 /* If this MTU minus the Header Size would result in a
1540 * Data Segment Size of the desired alignment, remember it.
1541 */
1542 if ((data_size & data_size_align_mask) == 0)
1543 aligned_mtu_idx = mtu_idx;
1544
1545 /* If we're not at the end of the Hardware MTU Table and the
1546 * next element is larger than our Maximum MTU, drop out of
1547 * the loop.
1548 */
1549 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1550 break;
1551 }
1552
1553 /* If we fell out of the loop because we ran to the end of the table,
1554 * then we just have to use the last [largest] entry.
1555 */
1556 if (mtu_idx == NMTUS)
1557 mtu_idx--;
1558
1559 /* If we found an MTU which resulted in the requested Data Segment
1560 * Length alignment and that's "not far" from the largest MTU which is
1561 * less than or equal to the maximum MTU, then use that.
1562 */
1563 if (aligned_mtu_idx >= 0 &&
1564 mtu_idx - aligned_mtu_idx <= 1)
1565 mtu_idx = aligned_mtu_idx;
1566
1567 /* If the caller has passed in an MTU Index pointer, pass the
1568 * MTU Index back. Return the MTU value.
1569 */
1570 if (mtu_idxp)
1571 *mtu_idxp = mtu_idx;
1572 return mtus[mtu_idx];
1573}
1574EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1575
1576/**
Hariprasad S27999802015-09-23 17:19:26 +05301577 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1578 * @chip: chip type
1579 * @viid: VI id of the given port
1580 *
1581 * Return the SMT index for this VI.
1582 */
1583unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1584{
1585 /* In T4/T5, SMT contains 256 SMAC entries organized in
1586 * 128 rows of 2 entries each.
1587 * In T6, SMT contains 256 SMAC entries in 256 rows.
1588 * TODO: The below code needs to be updated when we add support
1589 * for 256 VFs.
1590 */
1591 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1592 return ((viid & 0x7f) << 1);
1593 else
1594 return (viid & 0x7f);
1595}
1596EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1597
1598/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001599 * cxgb4_port_chan - get the HW channel of a port
1600 * @dev: the net device for the port
1601 *
1602 * Return the HW Tx channel of the given port.
1603 */
1604unsigned int cxgb4_port_chan(const struct net_device *dev)
1605{
1606 return netdev2pinfo(dev)->tx_chan;
1607}
1608EXPORT_SYMBOL(cxgb4_port_chan);
1609
Vipul Pandya881806b2012-05-18 15:29:24 +05301610unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1611{
1612 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001613 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301614
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301615 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1616 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301617 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301618 lp_count = LP_COUNT_G(v1);
1619 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001620 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301621 lp_count = LP_COUNT_T5_G(v1);
1622 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001623 }
1624 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301625}
1626EXPORT_SYMBOL(cxgb4_dbfifo_count);
1627
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001628/**
1629 * cxgb4_port_viid - get the VI id of a port
1630 * @dev: the net device for the port
1631 *
1632 * Return the VI id of the given port.
1633 */
1634unsigned int cxgb4_port_viid(const struct net_device *dev)
1635{
1636 return netdev2pinfo(dev)->viid;
1637}
1638EXPORT_SYMBOL(cxgb4_port_viid);
1639
1640/**
1641 * cxgb4_port_idx - get the index of a port
1642 * @dev: the net device for the port
1643 *
1644 * Return the index of the given port.
1645 */
1646unsigned int cxgb4_port_idx(const struct net_device *dev)
1647{
1648 return netdev2pinfo(dev)->port_id;
1649}
1650EXPORT_SYMBOL(cxgb4_port_idx);
1651
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001652void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1653 struct tp_tcp_stats *v6)
1654{
1655 struct adapter *adap = pci_get_drvdata(pdev);
1656
1657 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301658 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001659 spin_unlock(&adap->stats_lock);
1660}
1661EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1662
1663void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1664 const unsigned int *pgsz_order)
1665{
1666 struct adapter *adap = netdev2adap(dev);
1667
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301668 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1669 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1670 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1671 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001672}
1673EXPORT_SYMBOL(cxgb4_iscsi_init);
1674
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301675int cxgb4_flush_eq_cache(struct net_device *dev)
1676{
1677 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301678
Rahul Lakkireddy736c3b92017-12-08 09:48:40 +05301679 return t4_sge_ctxt_flush(adap, adap->mbox, CTXT_EGRESS);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301680}
1681EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1682
1683static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1684{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301685 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301686 __be64 indices;
1687 int ret;
1688
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301689 spin_lock(&adap->win0_lock);
1690 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1691 sizeof(indices), (__be32 *)&indices,
1692 T4_MEMORY_READ);
1693 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301694 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001695 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1696 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301697 }
1698 return ret;
1699}
1700
1701int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1702 u16 size)
1703{
1704 struct adapter *adap = netdev2adap(dev);
1705 u16 hw_pidx, hw_cidx;
1706 int ret;
1707
1708 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1709 if (ret)
1710 goto out;
1711
1712 if (pidx != hw_pidx) {
1713 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301714 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301715
1716 if (pidx >= hw_pidx)
1717 delta = pidx - hw_pidx;
1718 else
1719 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301720
1721 if (is_t4(adap->params.chip))
1722 val = PIDX_V(delta);
1723 else
1724 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301725 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301726 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1727 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301728 }
1729out:
1730 return ret;
1731}
1732EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1733
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301734int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1735{
1736 struct adapter *adap;
1737 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301738 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301739 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1740 int ret;
1741
1742 adap = netdev2adap(dev);
1743
1744 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1745
1746 /* Figure out where the offset lands in the Memory Type/Address scheme.
1747 * This code assumes that the memory is laid out starting at offset 0
1748 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1749 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1750 * MC0, and some have both MC0 and MC1.
1751 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301752 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1753 edc0_size = EDRAM0_SIZE_G(size) << 20;
1754 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1755 edc1_size = EDRAM1_SIZE_G(size) << 20;
1756 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1757 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301758
1759 edc0_end = edc0_size;
1760 edc1_end = edc0_end + edc1_size;
1761 mc0_end = edc1_end + mc0_size;
1762
1763 if (offset < edc0_end) {
1764 memtype = MEM_EDC0;
1765 memaddr = offset;
1766 } else if (offset < edc1_end) {
1767 memtype = MEM_EDC1;
1768 memaddr = offset - edc0_end;
1769 } else {
1770 if (offset < mc0_end) {
1771 memtype = MEM_MC0;
1772 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301773 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301774 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1775 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301776 mc1_end = mc0_end + mc1_size;
1777 if (offset < mc1_end) {
1778 memtype = MEM_MC1;
1779 memaddr = offset - mc0_end;
1780 } else {
1781 /* offset beyond the end of any memory */
1782 goto err;
1783 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301784 } else {
1785 /* T4/T6 only has a single memory channel */
1786 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301787 }
1788 }
1789
1790 spin_lock(&adap->win0_lock);
1791 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1792 spin_unlock(&adap->win0_lock);
1793 return ret;
1794
1795err:
1796 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1797 stag, offset);
1798 return -EINVAL;
1799}
1800EXPORT_SYMBOL(cxgb4_read_tpte);
1801
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301802u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1803{
1804 u32 hi, lo;
1805 struct adapter *adap;
1806
1807 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301808 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1809 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301810
1811 return ((u64)hi << 32) | (u64)lo;
1812}
1813EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1814
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301815int cxgb4_bar2_sge_qregs(struct net_device *dev,
1816 unsigned int qid,
1817 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301818 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301819 u64 *pbar2_qoffset,
1820 unsigned int *pbar2_qid)
1821{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301822 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301823 qid,
1824 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1825 ? T4_BAR2_QTYPE_EGRESS
1826 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301827 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301828 pbar2_qoffset,
1829 pbar2_qid);
1830}
1831EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1832
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001833static struct pci_driver cxgb4_driver;
1834
1835static void check_neigh_update(struct neighbour *neigh)
1836{
1837 const struct device *parent;
1838 const struct net_device *netdev = neigh->dev;
1839
Parav Panditd0d7b102017-02-04 11:00:49 -06001840 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001841 netdev = vlan_dev_real_dev(netdev);
1842 parent = netdev->dev.parent;
1843 if (parent && parent->driver == &cxgb4_driver.driver)
1844 t4_l2t_update(dev_get_drvdata(parent), neigh);
1845}
1846
1847static int netevent_cb(struct notifier_block *nb, unsigned long event,
1848 void *data)
1849{
1850 switch (event) {
1851 case NETEVENT_NEIGH_UPDATE:
1852 check_neigh_update(data);
1853 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001854 case NETEVENT_REDIRECT:
1855 default:
1856 break;
1857 }
1858 return 0;
1859}
1860
1861static bool netevent_registered;
1862static struct notifier_block cxgb4_netevent_nb = {
1863 .notifier_call = netevent_cb
1864};
1865
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301866static void drain_db_fifo(struct adapter *adap, int usecs)
1867{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001868 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301869
1870 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301871 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1872 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301873 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301874 lp_count = LP_COUNT_G(v1);
1875 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001876 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301877 lp_count = LP_COUNT_T5_G(v1);
1878 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001879 }
1880
1881 if (lp_count == 0 && hp_count == 0)
1882 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301883 set_current_state(TASK_UNINTERRUPTIBLE);
1884 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301885 } while (1);
1886}
1887
1888static void disable_txq_db(struct sge_txq *q)
1889{
Steve Wise05eb2382014-03-14 21:52:08 +05301890 unsigned long flags;
1891
1892 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301893 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301894 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301895}
1896
Steve Wise05eb2382014-03-14 21:52:08 +05301897static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301898{
1899 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301900 if (q->db_pidx_inc) {
1901 /* Make sure that all writes to the TX descriptors
1902 * are committed before we tell HW about them.
1903 */
1904 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301905 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1906 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301907 q->db_pidx_inc = 0;
1908 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301909 q->db_disabled = 0;
1910 spin_unlock_irq(&q->db_lock);
1911}
1912
1913static void disable_dbs(struct adapter *adap)
1914{
1915 int i;
1916
1917 for_each_ethrxq(&adap->sge, i)
1918 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301919 if (is_offload(adap)) {
1920 struct sge_uld_txq_info *txq_info =
1921 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1922
1923 if (txq_info) {
1924 for_each_ofldtxq(&adap->sge, i) {
1925 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1926
1927 disable_txq_db(&txq->q);
1928 }
1929 }
1930 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301931 for_each_port(adap, i)
1932 disable_txq_db(&adap->sge.ctrlq[i].q);
1933}
1934
1935static void enable_dbs(struct adapter *adap)
1936{
1937 int i;
1938
1939 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301940 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301941 if (is_offload(adap)) {
1942 struct sge_uld_txq_info *txq_info =
1943 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1944
1945 if (txq_info) {
1946 for_each_ofldtxq(&adap->sge, i) {
1947 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1948
1949 enable_txq_db(adap, &txq->q);
1950 }
1951 }
1952 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301953 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301954 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1955}
1956
1957static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1958{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301959 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1960
1961 if (adap->uld && adap->uld[type].handle)
1962 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301963}
1964
1965static void process_db_full(struct work_struct *work)
1966{
1967 struct adapter *adap;
1968
1969 adap = container_of(work, struct adapter, db_full_task);
1970
1971 drain_db_fifo(adap, dbfifo_drain_delay);
1972 enable_dbs(adap);
1973 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301974 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1975 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1976 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1977 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1978 else
1979 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1980 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301981}
1982
1983static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1984{
1985 u16 hw_pidx, hw_cidx;
1986 int ret;
1987
Steve Wise05eb2382014-03-14 21:52:08 +05301988 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301989 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1990 if (ret)
1991 goto out;
1992 if (q->db_pidx != hw_pidx) {
1993 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301994 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301995
1996 if (q->db_pidx >= hw_pidx)
1997 delta = q->db_pidx - hw_pidx;
1998 else
1999 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302000
2001 if (is_t4(adap->params.chip))
2002 val = PIDX_V(delta);
2003 else
2004 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302005 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302006 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2007 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302008 }
2009out:
2010 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302011 q->db_pidx_inc = 0;
2012 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302013 if (ret)
2014 CH_WARN(adap, "DB drop recovery failed.\n");
2015}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302016
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302017static void recover_all_queues(struct adapter *adap)
2018{
2019 int i;
2020
2021 for_each_ethrxq(&adap->sge, i)
2022 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302023 if (is_offload(adap)) {
2024 struct sge_uld_txq_info *txq_info =
2025 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2026 if (txq_info) {
2027 for_each_ofldtxq(&adap->sge, i) {
2028 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2029
2030 sync_txq_pidx(adap, &txq->q);
2031 }
2032 }
2033 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302034 for_each_port(adap, i)
2035 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2036}
2037
Vipul Pandya881806b2012-05-18 15:29:24 +05302038static void process_db_drop(struct work_struct *work)
2039{
2040 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302041
Vipul Pandya881806b2012-05-18 15:29:24 +05302042 adap = container_of(work, struct adapter, db_drop_task);
2043
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302044 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302045 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002046 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302047 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002048 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302049 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002050 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302051 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302052 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002053 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2054 u16 qid = (dropped_db >> 15) & 0x1ffff;
2055 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302056 u64 bar2_qoffset;
2057 unsigned int bar2_qid;
2058 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002059
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302060 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002061 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302062 if (ret)
2063 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2064 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2065 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302066 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302067 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002068
2069 /* Re-enable BAR2 WC */
2070 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2071 }
2072
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302073 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2074 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302075}
2076
2077void t4_db_full(struct adapter *adap)
2078{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302079 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302080 disable_dbs(adap);
2081 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302082 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2083 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002084 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002085 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302086}
2087
2088void t4_db_dropped(struct adapter *adap)
2089{
Steve Wise05eb2382014-03-14 21:52:08 +05302090 if (is_t4(adap->params.chip)) {
2091 disable_dbs(adap);
2092 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2093 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002094 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302095}
2096
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302097void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002098{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002099 if (!netevent_registered) {
2100 register_netevent_notifier(&cxgb4_netevent_nb);
2101 netevent_registered = true;
2102 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002103}
2104
2105static void detach_ulds(struct adapter *adap)
2106{
2107 unsigned int i;
2108
2109 mutex_lock(&uld_mutex);
2110 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002111
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002112 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002113 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302114 adap->uld[i].state_change(adap->uld[i].handle,
2115 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002116
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002117 if (netevent_registered && list_empty(&adapter_list)) {
2118 unregister_netevent_notifier(&cxgb4_netevent_nb);
2119 netevent_registered = false;
2120 }
2121 mutex_unlock(&uld_mutex);
2122}
2123
2124static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2125{
2126 unsigned int i;
2127
2128 mutex_lock(&uld_mutex);
2129 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302130 if (adap->uld && adap->uld[i].handle)
2131 adap->uld[i].state_change(adap->uld[i].handle,
2132 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002133 mutex_unlock(&uld_mutex);
2134}
2135
Anish Bhatt1bb60372014-10-14 20:07:22 -07002136#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002137static int cxgb4_inet6addr_handler(struct notifier_block *this,
2138 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302139{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002140 struct inet6_ifaddr *ifa = data;
2141 struct net_device *event_dev = ifa->idev->dev;
2142 const struct device *parent = NULL;
2143#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302144 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002145#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002146 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002147 event_dev = vlan_dev_real_dev(event_dev);
2148#if IS_ENABLED(CONFIG_BONDING)
2149 if (event_dev->flags & IFF_MASTER) {
2150 list_for_each_entry(adap, &adapter_list, list_node) {
2151 switch (event) {
2152 case NETDEV_UP:
2153 cxgb4_clip_get(adap->port[0],
2154 (const u32 *)ifa, 1);
2155 break;
2156 case NETDEV_DOWN:
2157 cxgb4_clip_release(adap->port[0],
2158 (const u32 *)ifa, 1);
2159 break;
2160 default:
2161 break;
2162 }
2163 }
2164 return NOTIFY_OK;
2165 }
2166#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302167
Anish Bhattb5a02f52015-01-14 15:17:34 -08002168 if (event_dev)
2169 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302170
Anish Bhattb5a02f52015-01-14 15:17:34 -08002171 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302172 switch (event) {
2173 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002174 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302175 break;
2176 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002177 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302178 break;
2179 default:
2180 break;
2181 }
2182 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002183 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302184}
2185
Anish Bhattb5a02f52015-01-14 15:17:34 -08002186static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302187static struct notifier_block cxgb4_inet6addr_notifier = {
2188 .notifier_call = cxgb4_inet6addr_handler
2189};
2190
Vipul Pandya01bcca62013-07-04 16:10:46 +05302191static void update_clip(const struct adapter *adap)
2192{
2193 int i;
2194 struct net_device *dev;
2195 int ret;
2196
2197 rcu_read_lock();
2198
2199 for (i = 0; i < MAX_NPORTS; i++) {
2200 dev = adap->port[i];
2201 ret = 0;
2202
2203 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002204 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302205
2206 if (ret < 0)
2207 break;
2208 }
2209 rcu_read_unlock();
2210}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002211#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302212
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002213/**
2214 * cxgb_up - enable the adapter
2215 * @adap: adapter being enabled
2216 *
2217 * Called when the first port is enabled, this function performs the
2218 * actions necessary to make an adapter operational, such as completing
2219 * the initialization of HW modules, and enabling interrupts.
2220 *
2221 * Must be called with the rtnl lock held.
2222 */
2223static int cxgb_up(struct adapter *adap)
2224{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002225 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002226
Raju Rangoju91060382017-06-19 17:40:48 +05302227 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002228 err = setup_sge_queues(adap);
2229 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302230 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002231 err = setup_rss(adap);
2232 if (err)
2233 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002234
2235 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002236 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002237 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2238 adap->msix_info[0].desc, adap);
2239 if (err)
2240 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002241 err = request_msix_queue_irqs(adap);
2242 if (err) {
2243 free_irq(adap->msix_info[0].vec, adap);
2244 goto irq_err;
2245 }
2246 } else {
2247 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2248 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002249 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002250 if (err)
2251 goto irq_err;
2252 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302253
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002254 enable_rx(adap);
2255 t4_sge_start(adap);
2256 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002257 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302258 mutex_unlock(&uld_mutex);
2259
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002260 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002261#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302262 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002263#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302264 /* Initialize hash mac addr list*/
2265 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002266 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302267
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002268 irq_err:
2269 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002270 freeq:
2271 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302272 rel_lock:
2273 mutex_unlock(&uld_mutex);
2274 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002275}
2276
2277static void cxgb_down(struct adapter *adapter)
2278{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002279 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302280 cancel_work_sync(&adapter->db_full_task);
2281 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002282 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002283 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002284
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002285 t4_sge_stop(adapter);
2286 t4_free_sge_resources(adapter);
2287 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002288}
2289
2290/*
2291 * net_device operations
2292 */
2293static int cxgb_open(struct net_device *dev)
2294{
2295 int err;
2296 struct port_info *pi = netdev_priv(dev);
2297 struct adapter *adapter = pi->adapter;
2298
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002299 netif_carrier_off(dev);
2300
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002301 if (!(adapter->flags & FULL_INIT_DONE)) {
2302 err = cxgb_up(adapter);
2303 if (err < 0)
2304 return err;
2305 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002306
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302307 /* It's possible that the basic port information could have
2308 * changed since we first read it.
2309 */
2310 err = t4_update_port_info(pi);
2311 if (err < 0)
2312 return err;
2313
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002314 err = link_start(dev);
2315 if (!err)
2316 netif_tx_start_all_queues(dev);
2317 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002318}
2319
2320static int cxgb_close(struct net_device *dev)
2321{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002322 struct port_info *pi = netdev_priv(dev);
2323 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302324 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002325
2326 netif_tx_stop_all_queues(dev);
2327 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302328 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2329#ifdef CONFIG_CHELSIO_T4_DCB
2330 cxgb4_dcb_reset(dev);
2331 dcb_tx_queue_prio_enable(dev, false);
2332#endif
2333 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002334}
2335
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002336int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002337 __be32 sip, __be16 sport, __be16 vlan,
2338 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002339{
2340 int ret;
2341 struct filter_entry *f;
2342 struct adapter *adap;
2343 int i;
2344 u8 *val;
2345
2346 adap = netdev2adap(dev);
2347
Vipul Pandya1cab7752012-12-10 09:30:55 +00002348 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302349 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002350 stid += adap->tids.nftids;
2351
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002352 /* Check to make sure the filter requested is writable ...
2353 */
2354 f = &adap->tids.ftid_tab[stid];
2355 ret = writable_filter(f);
2356 if (ret)
2357 return ret;
2358
2359 /* Clear out any old resources being used by the filter before
2360 * we start constructing the new filter.
2361 */
2362 if (f->valid)
2363 clear_filter(adap, f);
2364
2365 /* Clear out filter specifications */
2366 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2367 f->fs.val.lport = cpu_to_be16(sport);
2368 f->fs.mask.lport = ~0;
2369 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002370 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002371 for (i = 0; i < 4; i++) {
2372 f->fs.val.lip[i] = val[i];
2373 f->fs.mask.lip[i] = ~0;
2374 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302375 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002376 f->fs.val.iport = port;
2377 f->fs.mask.iport = mask;
2378 }
2379 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002380
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302381 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302382 f->fs.val.proto = IPPROTO_TCP;
2383 f->fs.mask.proto = ~0;
2384 }
2385
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002386 f->fs.dirsteer = 1;
2387 f->fs.iq = queue;
2388 /* Mark filter as locked */
2389 f->locked = 1;
2390 f->fs.rpttid = 1;
2391
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302392 /* Save the actual tid. We need this to get the corresponding
2393 * filter entry structure in filter_rpl.
2394 */
2395 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002396 ret = set_filter_wr(adap, stid);
2397 if (ret) {
2398 clear_filter(adap, f);
2399 return ret;
2400 }
2401
2402 return 0;
2403}
2404EXPORT_SYMBOL(cxgb4_create_server_filter);
2405
2406int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2407 unsigned int queue, bool ipv6)
2408{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002409 struct filter_entry *f;
2410 struct adapter *adap;
2411
2412 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002413
2414 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302415 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002416 stid += adap->tids.nftids;
2417
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002418 f = &adap->tids.ftid_tab[stid];
2419 /* Unlock the filter */
2420 f->locked = 0;
2421
Wei Yongjun8c148462016-08-20 15:32:41 +00002422 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002423}
2424EXPORT_SYMBOL(cxgb4_remove_server_filter);
2425
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002426static void cxgb_get_stats(struct net_device *dev,
2427 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002428{
2429 struct port_stats stats;
2430 struct port_info *p = netdev_priv(dev);
2431 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002432
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002433 /* Block retrieving statistics during EEH error
2434 * recovery. Otherwise, the recovery might fail
2435 * and the PCI device will be removed permanently
2436 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002437 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002438 if (!netif_device_present(dev)) {
2439 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002440 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002441 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302442 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2443 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002444 spin_unlock(&adapter->stats_lock);
2445
2446 ns->tx_bytes = stats.tx_octets;
2447 ns->tx_packets = stats.tx_frames;
2448 ns->rx_bytes = stats.rx_octets;
2449 ns->rx_packets = stats.rx_frames;
2450 ns->multicast = stats.rx_mcast_frames;
2451
2452 /* detailed rx_errors */
2453 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2454 stats.rx_runt;
2455 ns->rx_over_errors = 0;
2456 ns->rx_crc_errors = stats.rx_fcs_err;
2457 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302458 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002459 stats.rx_ovflow2 + stats.rx_ovflow3 +
2460 stats.rx_trunc0 + stats.rx_trunc1 +
2461 stats.rx_trunc2 + stats.rx_trunc3;
2462 ns->rx_missed_errors = 0;
2463
2464 /* detailed tx_errors */
2465 ns->tx_aborted_errors = 0;
2466 ns->tx_carrier_errors = 0;
2467 ns->tx_fifo_errors = 0;
2468 ns->tx_heartbeat_errors = 0;
2469 ns->tx_window_errors = 0;
2470
2471 ns->tx_errors = stats.tx_error_frames;
2472 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2473 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002474}
2475
2476static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2477{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002478 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002479 int ret = 0, prtad, devad;
2480 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302481 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002482 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2483
2484 switch (cmd) {
2485 case SIOCGMIIPHY:
2486 if (pi->mdio_addr < 0)
2487 return -EOPNOTSUPP;
2488 data->phy_id = pi->mdio_addr;
2489 break;
2490 case SIOCGMIIREG:
2491 case SIOCSMIIREG:
2492 if (mdio_phy_id_is_c45(data->phy_id)) {
2493 prtad = mdio_phy_id_prtad(data->phy_id);
2494 devad = mdio_phy_id_devad(data->phy_id);
2495 } else if (data->phy_id < 32) {
2496 prtad = data->phy_id;
2497 devad = 0;
2498 data->reg_num &= 0x1f;
2499 } else
2500 return -EINVAL;
2501
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302502 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002503 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002504 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002505 data->reg_num, &data->val_out);
2506 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002507 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002508 data->reg_num, data->val_in);
2509 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302510 case SIOCGHWTSTAMP:
2511 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2512 sizeof(pi->tstamp_config)) ?
2513 -EFAULT : 0;
2514 case SIOCSHWTSTAMP:
2515 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2516 sizeof(pi->tstamp_config)))
2517 return -EFAULT;
2518
Atul Guptaa45695042017-07-04 16:46:20 +05302519 if (!is_t4(adapter->params.chip)) {
2520 switch (pi->tstamp_config.tx_type) {
2521 case HWTSTAMP_TX_OFF:
2522 case HWTSTAMP_TX_ON:
2523 break;
2524 default:
2525 return -ERANGE;
2526 }
2527
2528 switch (pi->tstamp_config.rx_filter) {
2529 case HWTSTAMP_FILTER_NONE:
2530 pi->rxtstamp = false;
2531 break;
2532 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2533 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2534 cxgb4_ptprx_timestamping(pi, pi->port_id,
2535 PTP_TS_L4);
2536 break;
2537 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2538 cxgb4_ptprx_timestamping(pi, pi->port_id,
2539 PTP_TS_L2_L4);
2540 break;
2541 case HWTSTAMP_FILTER_ALL:
2542 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2543 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2544 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2545 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2546 pi->rxtstamp = true;
2547 break;
2548 default:
2549 pi->tstamp_config.rx_filter =
2550 HWTSTAMP_FILTER_NONE;
2551 return -ERANGE;
2552 }
2553
2554 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2555 (pi->tstamp_config.rx_filter ==
2556 HWTSTAMP_FILTER_NONE)) {
2557 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2558 pi->ptp_enable = false;
2559 }
2560
2561 if (pi->tstamp_config.rx_filter !=
2562 HWTSTAMP_FILTER_NONE) {
2563 if (cxgb4_ptp_redirect_rx_packet(adapter,
2564 pi) >= 0)
2565 pi->ptp_enable = true;
2566 }
2567 } else {
2568 /* For T4 Adapters */
2569 switch (pi->tstamp_config.rx_filter) {
2570 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302571 pi->rxtstamp = false;
2572 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302573 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302574 pi->rxtstamp = true;
2575 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302576 default:
2577 pi->tstamp_config.rx_filter =
2578 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302579 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302580 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302581 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302582 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2583 sizeof(pi->tstamp_config)) ?
2584 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002585 default:
2586 return -EOPNOTSUPP;
2587 }
2588 return ret;
2589}
2590
2591static void cxgb_set_rxmode(struct net_device *dev)
2592{
2593 /* unfortunately we can't return errors to the stack */
2594 set_rxmode(dev, -1, false);
2595}
2596
2597static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2598{
2599 int ret;
2600 struct port_info *pi = netdev_priv(dev);
2601
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302602 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002603 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002604 if (!ret)
2605 dev->mtu = new_mtu;
2606 return ret;
2607}
2608
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302609#ifdef CONFIG_PCI_IOV
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302610static int cxgb4_mgmt_open(struct net_device *dev)
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302611{
2612 /* Turn carrier off since we don't have to transmit anything on this
2613 * interface.
2614 */
2615 netif_carrier_off(dev);
2616 return 0;
2617}
2618
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302619/* Fill MAC address that will be assigned by the FW */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302620static void cxgb4_mgmt_fill_vf_station_mac_addr(struct adapter *adap)
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302621{
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302622 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302623 unsigned int i, vf, nvfs;
2624 u16 a, b;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302625 int err;
2626 u8 *na;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302627
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302628 adap->params.pci.vpd_cap_addr = pci_find_capability(adap->pdev,
2629 PCI_CAP_ID_VPD);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302630 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302631 if (err)
2632 return;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302633
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302634 na = adap->params.vpd.na;
2635 for (i = 0; i < ETH_ALEN; i++)
2636 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2637 hex2val(na[2 * i + 1]));
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302638
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302639 a = (hw_addr[0] << 8) | hw_addr[1];
2640 b = (hw_addr[1] << 8) | hw_addr[2];
2641 a ^= b;
2642 a |= 0x0200; /* locally assigned Ethernet MAC address */
2643 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2644 macaddr[0] = a >> 8;
2645 macaddr[1] = a & 0xff;
2646
2647 for (i = 2; i < 5; i++)
2648 macaddr[i] = hw_addr[i + 1];
2649
2650 for (vf = 0, nvfs = pci_sriov_get_totalvfs(adap->pdev);
2651 vf < nvfs; vf++) {
2652 macaddr[5] = adap->pf * 16 + vf;
2653 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, macaddr);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302654 }
2655}
2656
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302657static int cxgb4_mgmt_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302658{
2659 struct port_info *pi = netdev_priv(dev);
2660 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302661 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302662
2663 /* verify MAC addr is valid */
2664 if (!is_valid_ether_addr(mac)) {
2665 dev_err(pi->adapter->pdev_dev,
2666 "Invalid Ethernet address %pM for VF %d\n",
2667 mac, vf);
2668 return -EINVAL;
2669 }
2670
2671 dev_info(pi->adapter->pdev_dev,
2672 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302673 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2674 if (!ret)
2675 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2676 return ret;
2677}
2678
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302679static int cxgb4_mgmt_get_vf_config(struct net_device *dev,
2680 int vf, struct ifla_vf_info *ivi)
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302681{
2682 struct port_info *pi = netdev_priv(dev);
2683 struct adapter *adap = pi->adapter;
2684
2685 if (vf >= adap->num_vfs)
2686 return -EINVAL;
2687 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302688 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2689 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302690 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2691 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302692}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302693
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302694static int cxgb4_mgmt_get_phys_port_id(struct net_device *dev,
2695 struct netdev_phys_item_id *ppid)
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302696{
2697 struct port_info *pi = netdev_priv(dev);
2698 unsigned int phy_port_id;
2699
2700 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2701 ppid->id_len = sizeof(phy_port_id);
2702 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2703 return 0;
2704}
2705
Ganesh Goudarbaf50862018-01-16 16:17:40 +05302706static int cxgb4_mgmt_set_vf_rate(struct net_device *dev, int vf,
2707 int min_tx_rate, int max_tx_rate)
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302708{
2709 struct port_info *pi = netdev_priv(dev);
2710 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302711 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302712 u32 fw_pfvf, fw_class;
2713 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302714 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302715 u16 pktsize;
2716
2717 if (vf >= adap->num_vfs)
2718 return -EINVAL;
2719
2720 if (min_tx_rate) {
2721 dev_err(adap->pdev_dev,
2722 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2723 min_tx_rate, vf);
2724 return -EINVAL;
2725 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302726
2727 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302728 if (ret != FW_SUCCESS) {
2729 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302730 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302731 return -EINVAL;
2732 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302733
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302734 if (!link_ok) {
2735 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2736 return -EINVAL;
2737 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302738
2739 if (max_tx_rate > speed) {
2740 dev_err(adap->pdev_dev,
2741 "Max tx rate %d for VF %d can't be > link-speed %u",
2742 max_tx_rate, vf, speed);
2743 return -EINVAL;
2744 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302745
2746 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302747 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2748 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2749 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2750 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2751 /* configure Traffic Class for rate-limiting */
2752 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2753 SCHED_CLASS_LEVEL_CL_RL,
2754 SCHED_CLASS_MODE_CLASS,
2755 SCHED_CLASS_RATEUNIT_BITS,
2756 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302757 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302758 max_tx_rate * 1000, 0, pktsize);
2759 if (ret) {
2760 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2761 ret);
2762 return -EINVAL;
2763 }
2764 dev_info(adap->pdev_dev,
2765 "Class %d with MSS %u configured with rate %u\n",
2766 class_id, pktsize, max_tx_rate);
2767
2768 /* bind VF to configured Traffic Class */
2769 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2770 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2771 fw_class = class_id;
2772 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2773 &fw_class);
2774 if (ret) {
2775 dev_err(adap->pdev_dev,
2776 "Err %d in binding VF %d to Traffic Class %d\n",
2777 ret, vf, class_id);
2778 return -EINVAL;
2779 }
2780 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2781 adap->pf, vf, class_id);
2782 adap->vfinfo[vf].tx_rate = max_tx_rate;
2783 return 0;
2784}
2785
Ganesh Goudar9d5fd922018-01-24 20:44:07 +05302786static int cxgb4_mgmt_set_vf_vlan(struct net_device *dev, int vf,
2787 u16 vlan, u8 qos, __be16 vlan_proto)
2788{
2789 struct port_info *pi = netdev_priv(dev);
2790 struct adapter *adap = pi->adapter;
2791 int ret;
2792
2793 if (vf >= adap->num_vfs || vlan > 4095 || qos > 7)
2794 return -EINVAL;
2795
2796 if (vlan_proto != htons(ETH_P_8021Q) || qos != 0)
2797 return -EPROTONOSUPPORT;
2798
2799 ret = t4_set_vlan_acl(adap, adap->mbox, vf + 1, vlan);
2800 if (!ret) {
2801 adap->vfinfo[vf].vlan = vlan;
2802 return 0;
2803 }
2804
2805 dev_err(adap->pdev_dev, "Err %d %s VLAN ACL for PF/VF %d/%d\n",
2806 ret, (vlan ? "setting" : "clearing"), adap->pf, vf);
2807 return ret;
2808}
2809#endif /* CONFIG_PCI_IOV */
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302810
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002811static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2812{
2813 int ret;
2814 struct sockaddr *addr = p;
2815 struct port_info *pi = netdev_priv(dev);
2816
2817 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002818 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002819
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302820 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002821 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002822 if (ret < 0)
2823 return ret;
2824
2825 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2826 pi->xact_addr_filt = ret;
2827 return 0;
2828}
2829
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002830#ifdef CONFIG_NET_POLL_CONTROLLER
2831static void cxgb_netpoll(struct net_device *dev)
2832{
2833 struct port_info *pi = netdev_priv(dev);
2834 struct adapter *adap = pi->adapter;
2835
2836 if (adap->flags & USING_MSIX) {
2837 int i;
2838 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2839
2840 for (i = pi->nqsets; i; i--, rx++)
2841 t4_sge_intr_msix(0, &rx->rspq);
2842 } else
2843 t4_intr_handler(adap)(0, adap);
2844}
2845#endif
2846
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302847static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2848{
2849 struct port_info *pi = netdev_priv(dev);
2850 struct adapter *adap = pi->adapter;
2851 struct sched_class *e;
2852 struct ch_sched_params p;
2853 struct ch_sched_queue qe;
2854 u32 req_rate;
2855 int err = 0;
2856
2857 if (!can_sched(dev))
2858 return -ENOTSUPP;
2859
2860 if (index < 0 || index > pi->nqsets - 1)
2861 return -EINVAL;
2862
2863 if (!(adap->flags & FULL_INIT_DONE)) {
2864 dev_err(adap->pdev_dev,
2865 "Failed to rate limit on queue %d. Link Down?\n",
2866 index);
2867 return -EINVAL;
2868 }
2869
2870 /* Convert from Mbps to Kbps */
2871 req_rate = rate << 10;
2872
2873 /* Max rate is 10 Gbps */
2874 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2875 dev_err(adap->pdev_dev,
2876 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2877 rate, SCHED_MAX_RATE_KBPS);
2878 return -ERANGE;
2879 }
2880
2881 /* First unbind the queue from any existing class */
2882 memset(&qe, 0, sizeof(qe));
2883 qe.queue = index;
2884 qe.class = SCHED_CLS_NONE;
2885
2886 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2887 if (err) {
2888 dev_err(adap->pdev_dev,
2889 "Unbinding Queue %d on port %d fail. Err: %d\n",
2890 index, pi->port_id, err);
2891 return err;
2892 }
2893
2894 /* Queue already unbound */
2895 if (!req_rate)
2896 return 0;
2897
2898 /* Fetch any available unused or matching scheduling class */
2899 memset(&p, 0, sizeof(p));
2900 p.type = SCHED_CLASS_TYPE_PACKET;
2901 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2902 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2903 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2904 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2905 p.u.params.channel = pi->tx_chan;
2906 p.u.params.class = SCHED_CLS_NONE;
2907 p.u.params.minrate = 0;
2908 p.u.params.maxrate = req_rate;
2909 p.u.params.weight = 0;
2910 p.u.params.pktsize = dev->mtu;
2911
2912 e = cxgb4_sched_class_alloc(dev, &p);
2913 if (!e)
2914 return -ENOMEM;
2915
2916 /* Bind the queue to a scheduling class */
2917 memset(&qe, 0, sizeof(qe));
2918 qe.queue = index;
2919 qe.class = e->idx;
2920
2921 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2922 if (err)
2923 dev_err(adap->pdev_dev,
2924 "Queue rate limiting failed. Err: %d\n", err);
2925 return err;
2926}
2927
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302928static int cxgb_setup_tc_flower(struct net_device *dev,
2929 struct tc_cls_flower_offload *cls_flower)
2930{
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302931 switch (cls_flower->command) {
2932 case TC_CLSFLOWER_REPLACE:
2933 return cxgb4_tc_flower_replace(dev, cls_flower);
2934 case TC_CLSFLOWER_DESTROY:
2935 return cxgb4_tc_flower_destroy(dev, cls_flower);
2936 case TC_CLSFLOWER_STATS:
2937 return cxgb4_tc_flower_stats(dev, cls_flower);
2938 default:
2939 return -EOPNOTSUPP;
2940 }
2941}
2942
Jiri Pirkof7323042017-08-07 10:15:20 +02002943static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002944 struct tc_cls_u32_offload *cls_u32)
2945{
Jiri Pirkof7323042017-08-07 10:15:20 +02002946 switch (cls_u32->command) {
2947 case TC_CLSU32_NEW_KNODE:
2948 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002949 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002950 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002951 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002952 default:
2953 return -EOPNOTSUPP;
2954 }
2955}
2956
Jiri Pirkocd019e92017-10-19 15:50:40 +02002957static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2958 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302959{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002960 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302961 struct port_info *pi = netdev2pinfo(dev);
2962 struct adapter *adap = netdev2adap(dev);
2963
2964 if (!(adap->flags & FULL_INIT_DONE)) {
2965 dev_err(adap->pdev_dev,
2966 "Failed to setup tc on port %d. Link Down?\n",
2967 pi->port_id);
2968 return -EINVAL;
2969 }
2970
Jakub Kicinski2a84bba2018-01-25 14:00:46 -08002971 if (!tc_cls_can_offload_and_chain0(dev, type_data))
Jiri Pirko44ae12a2017-11-01 11:47:39 +01002972 return -EOPNOTSUPP;
2973
Jiri Pirkof7323042017-08-07 10:15:20 +02002974 switch (type) {
2975 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002976 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302977 case TC_SETUP_CLSFLOWER:
2978 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002979 default:
2980 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302981 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302982}
2983
Jiri Pirkocd019e92017-10-19 15:50:40 +02002984static int cxgb_setup_tc_block(struct net_device *dev,
2985 struct tc_block_offload *f)
2986{
2987 struct port_info *pi = netdev2pinfo(dev);
2988
2989 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2990 return -EOPNOTSUPP;
2991
2992 switch (f->command) {
2993 case TC_BLOCK_BIND:
2994 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
2995 pi, dev);
2996 case TC_BLOCK_UNBIND:
2997 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
2998 return 0;
2999 default:
3000 return -EOPNOTSUPP;
3001 }
3002}
3003
3004static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
3005 void *type_data)
3006{
3007 switch (type) {
Jiri Pirkocd019e92017-10-19 15:50:40 +02003008 case TC_SETUP_BLOCK:
3009 return cxgb_setup_tc_block(dev, type_data);
3010 default:
3011 return -EOPNOTSUPP;
3012 }
3013}
3014
Ganesh Goudar846eac32018-01-10 18:15:08 +05303015static void cxgb_del_udp_tunnel(struct net_device *netdev,
3016 struct udp_tunnel_info *ti)
3017{
3018 struct port_info *pi = netdev_priv(netdev);
3019 struct adapter *adapter = pi->adapter;
3020 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3021 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3022 int ret = 0, i;
3023
3024 if (chip_ver < CHELSIO_T6)
3025 return;
3026
3027 switch (ti->type) {
3028 case UDP_TUNNEL_TYPE_VXLAN:
3029 if (!adapter->vxlan_port_cnt ||
3030 adapter->vxlan_port != ti->port)
3031 return; /* Invalid VxLAN destination port */
3032
3033 adapter->vxlan_port_cnt--;
3034 if (adapter->vxlan_port_cnt)
3035 return;
3036
3037 adapter->vxlan_port = 0;
3038 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A, 0);
3039 break;
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303040 case UDP_TUNNEL_TYPE_GENEVE:
3041 if (!adapter->geneve_port_cnt ||
3042 adapter->geneve_port != ti->port)
3043 return; /* Invalid GENEVE destination port */
3044
3045 adapter->geneve_port_cnt--;
3046 if (adapter->geneve_port_cnt)
3047 return;
3048
3049 adapter->geneve_port = 0;
3050 t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A, 0);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303051 default:
3052 return;
3053 }
3054
3055 /* Matchall mac entries can be deleted only after all tunnel ports
3056 * are brought down or removed.
3057 */
3058 if (!adapter->rawf_cnt)
3059 return;
3060 for_each_port(adapter, i) {
3061 pi = adap2pinfo(adapter, i);
3062 ret = t4_free_raw_mac_filt(adapter, pi->viid,
3063 match_all_mac, match_all_mac,
3064 adapter->rawf_start +
3065 pi->port_id,
3066 1, pi->port_id, true);
3067 if (ret < 0) {
3068 netdev_info(netdev, "Failed to free mac filter entry, for port %d\n",
3069 i);
3070 return;
3071 }
3072 atomic_dec(&adapter->mps_encap[adapter->rawf_start +
3073 pi->port_id].refcnt);
3074 }
3075}
3076
3077static void cxgb_add_udp_tunnel(struct net_device *netdev,
3078 struct udp_tunnel_info *ti)
3079{
3080 struct port_info *pi = netdev_priv(netdev);
3081 struct adapter *adapter = pi->adapter;
3082 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3083 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3084 int i, ret;
3085
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303086 if (chip_ver < CHELSIO_T6 || !adapter->rawf_cnt)
Ganesh Goudar846eac32018-01-10 18:15:08 +05303087 return;
3088
3089 switch (ti->type) {
3090 case UDP_TUNNEL_TYPE_VXLAN:
Ganesh Goudar846eac32018-01-10 18:15:08 +05303091 /* Callback for adding vxlan port can be called with the same
3092 * port for both IPv4 and IPv6. We should not disable the
3093 * offloading when the same port for both protocols is added
3094 * and later one of them is removed.
3095 */
3096 if (adapter->vxlan_port_cnt &&
3097 adapter->vxlan_port == ti->port) {
3098 adapter->vxlan_port_cnt++;
3099 return;
3100 }
3101
3102 /* We will support only one VxLAN port */
3103 if (adapter->vxlan_port_cnt) {
3104 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3105 be16_to_cpu(adapter->vxlan_port),
3106 be16_to_cpu(ti->port));
3107 return;
3108 }
3109
3110 adapter->vxlan_port = ti->port;
3111 adapter->vxlan_port_cnt = 1;
3112
3113 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A,
3114 VXLAN_V(be16_to_cpu(ti->port)) | VXLAN_EN_F);
3115 break;
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303116 case UDP_TUNNEL_TYPE_GENEVE:
3117 if (adapter->geneve_port_cnt &&
3118 adapter->geneve_port == ti->port) {
3119 adapter->geneve_port_cnt++;
3120 return;
3121 }
3122
3123 /* We will support only one GENEVE port */
3124 if (adapter->geneve_port_cnt) {
3125 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3126 be16_to_cpu(adapter->geneve_port),
3127 be16_to_cpu(ti->port));
3128 return;
3129 }
3130
3131 adapter->geneve_port = ti->port;
3132 adapter->geneve_port_cnt = 1;
3133
3134 t4_write_reg(adapter, MPS_RX_GENEVE_TYPE_A,
3135 GENEVE_V(be16_to_cpu(ti->port)) | GENEVE_EN_F);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303136 default:
3137 return;
3138 }
3139
3140 /* Create a 'match all' mac filter entry for inner mac,
3141 * if raw mac interface is supported. Once the linux kernel provides
3142 * driver entry points for adding/deleting the inner mac addresses,
3143 * we will remove this 'match all' entry and fallback to adding
3144 * exact match filters.
3145 */
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303146 for_each_port(adapter, i) {
3147 pi = adap2pinfo(adapter, i);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303148
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303149 ret = t4_alloc_raw_mac_filt(adapter, pi->viid,
3150 match_all_mac,
3151 match_all_mac,
3152 adapter->rawf_start +
3153 pi->port_id,
3154 1, pi->port_id, true);
3155 if (ret < 0) {
3156 netdev_info(netdev, "Failed to allocate a mac filter entry, not adding port %d\n",
3157 be16_to_cpu(ti->port));
3158 cxgb_del_udp_tunnel(netdev, ti);
3159 return;
Ganesh Goudar846eac32018-01-10 18:15:08 +05303160 }
Ganesh Goudarc746fc02018-01-22 18:48:26 +05303161 atomic_inc(&adapter->mps_encap[ret].refcnt);
Ganesh Goudar846eac32018-01-10 18:15:08 +05303162 }
3163}
3164
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303165static netdev_features_t cxgb_features_check(struct sk_buff *skb,
3166 struct net_device *dev,
3167 netdev_features_t features)
3168{
3169 struct port_info *pi = netdev_priv(dev);
3170 struct adapter *adapter = pi->adapter;
3171
3172 if (CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3173 return features;
3174
3175 /* Check if hw supports offload for this packet */
3176 if (!skb->encapsulation || cxgb_encap_offload_supported(skb))
3177 return features;
3178
3179 /* Offload is not supported for this encapsulated packet */
3180 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3181}
3182
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303183static netdev_features_t cxgb_fix_features(struct net_device *dev,
3184 netdev_features_t features)
3185{
3186 /* Disable GRO, if RX_CSUM is disabled */
3187 if (!(features & NETIF_F_RXCSUM))
3188 features &= ~NETIF_F_GRO;
3189
3190 return features;
3191}
3192
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003193static const struct net_device_ops cxgb4_netdev_ops = {
3194 .ndo_open = cxgb_open,
3195 .ndo_stop = cxgb_close,
3196 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003197 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003198 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003199 .ndo_set_rx_mode = cxgb_set_rxmode,
3200 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003201 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003202 .ndo_validate_addr = eth_validate_addr,
3203 .ndo_do_ioctl = cxgb_ioctl,
3204 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003205#ifdef CONFIG_NET_POLL_CONTROLLER
3206 .ndo_poll_controller = cxgb_netpoll,
3207#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303208#ifdef CONFIG_CHELSIO_T4_FCOE
3209 .ndo_fcoe_enable = cxgb_fcoe_enable,
3210 .ndo_fcoe_disable = cxgb_fcoe_disable,
3211#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303212 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303213 .ndo_setup_tc = cxgb_setup_tc,
Ganesh Goudar846eac32018-01-10 18:15:08 +05303214 .ndo_udp_tunnel_add = cxgb_add_udp_tunnel,
3215 .ndo_udp_tunnel_del = cxgb_del_udp_tunnel,
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303216 .ndo_features_check = cxgb_features_check,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303217 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003218};
3219
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303220#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303221static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303222 .ndo_open = cxgb4_mgmt_open,
3223 .ndo_set_vf_mac = cxgb4_mgmt_set_vf_mac,
3224 .ndo_get_vf_config = cxgb4_mgmt_get_vf_config,
3225 .ndo_set_vf_rate = cxgb4_mgmt_set_vf_rate,
3226 .ndo_get_phys_port_id = cxgb4_mgmt_get_phys_port_id,
Ganesh Goudar9d5fd922018-01-24 20:44:07 +05303227 .ndo_set_vf_vlan = cxgb4_mgmt_set_vf_vlan,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303228};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303229#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303230
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303231static void cxgb4_mgmt_get_drvinfo(struct net_device *dev,
3232 struct ethtool_drvinfo *info)
Hariprasad Shenai78294512016-08-11 21:06:23 +05303233{
3234 struct adapter *adapter = netdev2adap(dev);
3235
3236 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3237 strlcpy(info->version, cxgb4_driver_version,
3238 sizeof(info->version));
3239 strlcpy(info->bus_info, pci_name(adapter->pdev),
3240 sizeof(info->bus_info));
3241}
3242
3243static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05303244 .get_drvinfo = cxgb4_mgmt_get_drvinfo,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303245};
3246
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003247void t4_fatal_err(struct adapter *adap)
3248{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303249 int port;
3250
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003251 if (pci_channel_offline(adap->pdev))
3252 return;
3253
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303254 /* Disable the SGE since ULDs are going to free resources that
3255 * could be exposed to the adapter. RDMA MWs for example...
3256 */
3257 t4_shutdown_adapter(adap);
3258 for_each_port(adap, port) {
3259 struct net_device *dev = adap->port[port];
3260
3261 /* If we get here in very early initialization the network
3262 * devices may not have been set up yet.
3263 */
3264 if (!dev)
3265 continue;
3266
3267 netif_tx_stop_all_queues(dev);
3268 netif_carrier_off(dev);
3269 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003270 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3271}
3272
3273static void setup_memwin(struct adapter *adap)
3274{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303275 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003276
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303277 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003278}
3279
3280static void setup_memwin_rdma(struct adapter *adap)
3281{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003282 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303283 u32 start;
3284 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003285
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303286 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3287 start &= PCI_BASE_ADDRESS_MEM_MASK;
3288 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003289 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3290 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303291 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3292 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003293 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303294 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003295 adap->vres.ocq.start);
3296 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303297 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003298 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003299}
3300
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003301static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3302{
3303 u32 v;
3304 int ret;
3305
3306 /* get device capabilities */
3307 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303308 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3309 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303310 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303311 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003312 if (ret < 0)
3313 return ret;
3314
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303315 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3316 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303317 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003318 if (ret < 0)
3319 return ret;
3320
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303321 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003322 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303323 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3324 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003325 if (ret < 0)
3326 return ret;
3327
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303328 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303329 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3330 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003331 if (ret < 0)
3332 return ret;
3333
3334 t4_sge_init(adap);
3335
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003336 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303337 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303338 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303339 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3340 v = t4_read_reg(adap, TP_PIO_DATA_A);
3341 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003342
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003343 /* first 4 Tx modulation queues point to consecutive Tx channels */
3344 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303345 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3346 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003347
3348 /* associate each Tx modulation queue with consecutive Tx channels */
3349 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303350 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303351 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303352 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303353 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303354 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303355 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003356
3357#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3358 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303359 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3360 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3361 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3362 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3363 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3364 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3365 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3366 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3367 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3368 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003369 }
3370
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003371 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303372 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003373}
3374
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003375/*
3376 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3377 */
3378#define MAX_ATIDS 8192U
3379
3380/*
3381 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003382 *
3383 * If the firmware we're dealing with has Configuration File support, then
3384 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003385 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003386
3387/*
3388 * Tweak configuration based on module parameters, etc. Most of these have
3389 * defaults assigned to them by Firmware Configuration Files (if we're using
3390 * them) but need to be explicitly set if we're using hard-coded
3391 * initialization. But even in the case of using Firmware Configuration
3392 * Files, we'd like to expose the ability to change these via module
3393 * parameters so these are essentially common tweaks/settings for
3394 * Configuration Files and hard-coded initialization ...
3395 */
3396static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003397{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003398 /*
3399 * Fix up various Host-Dependent Parameters like Page Size, Cache
3400 * Line Size, etc. The firmware default is for a 4KB Page Size and
3401 * 64B Cache Line Size ...
3402 */
3403 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003404
Vipul Pandya636f9d32012-09-26 02:39:39 +00003405 /*
3406 * Process module parameters which affect early initialization.
3407 */
3408 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3409 dev_err(&adapter->pdev->dev,
3410 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3411 rx_dma_offset);
3412 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003413 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303414 t4_set_reg_field(adapter, SGE_CONTROL_A,
3415 PKTSHIFT_V(PKTSHIFT_M),
3416 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003417
Vipul Pandya636f9d32012-09-26 02:39:39 +00003418 /*
3419 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3420 * adds the pseudo header itself.
3421 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303422 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3423 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003424
3425 return 0;
3426}
3427
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303428/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3429 * unto themselves and they contain their own firmware to perform their
3430 * tasks ...
3431 */
3432static int phy_aq1202_version(const u8 *phy_fw_data,
3433 size_t phy_fw_size)
3434{
3435 int offset;
3436
3437 /* At offset 0x8 you're looking for the primary image's
3438 * starting offset which is 3 Bytes wide
3439 *
3440 * At offset 0xa of the primary image, you look for the offset
3441 * of the DRAM segment which is 3 Bytes wide.
3442 *
3443 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3444 * wide
3445 */
3446 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3447 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3448 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3449
3450 offset = le24(phy_fw_data + 0x8) << 12;
3451 offset = le24(phy_fw_data + offset + 0xa);
3452 return be16(phy_fw_data + offset + 0x27e);
3453
3454 #undef be16
3455 #undef le16
3456 #undef le24
3457}
3458
3459static struct info_10gbt_phy_fw {
3460 unsigned int phy_fw_id; /* PCI Device ID */
3461 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3462 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3463 int phy_flash; /* Has FLASH for PHY Firmware */
3464} phy_info_array[] = {
3465 {
3466 PHY_AQ1202_DEVICEID,
3467 PHY_AQ1202_FIRMWARE,
3468 phy_aq1202_version,
3469 1,
3470 },
3471 {
3472 PHY_BCM84834_DEVICEID,
3473 PHY_BCM84834_FIRMWARE,
3474 NULL,
3475 0,
3476 },
3477 { 0, NULL, NULL },
3478};
3479
3480static struct info_10gbt_phy_fw *find_phy_info(int devid)
3481{
3482 int i;
3483
3484 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3485 if (phy_info_array[i].phy_fw_id == devid)
3486 return &phy_info_array[i];
3487 }
3488 return NULL;
3489}
3490
3491/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3492 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3493 * we return a negative error number. If we transfer new firmware we return 1
3494 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3495 */
3496static int adap_init0_phy(struct adapter *adap)
3497{
3498 const struct firmware *phyf;
3499 int ret;
3500 struct info_10gbt_phy_fw *phy_info;
3501
3502 /* Use the device ID to determine which PHY file to flash.
3503 */
3504 phy_info = find_phy_info(adap->pdev->device);
3505 if (!phy_info) {
3506 dev_warn(adap->pdev_dev,
3507 "No PHY Firmware file found for this PHY\n");
3508 return -EOPNOTSUPP;
3509 }
3510
3511 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3512 * use that. The adapter firmware provides us with a memory buffer
3513 * where we can load a PHY firmware file from the host if we want to
3514 * override the PHY firmware File in flash.
3515 */
3516 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3517 adap->pdev_dev);
3518 if (ret < 0) {
3519 /* For adapters without FLASH attached to PHY for their
3520 * firmware, it's obviously a fatal error if we can't get the
3521 * firmware to the adapter. For adapters with PHY firmware
3522 * FLASH storage, it's worth a warning if we can't find the
3523 * PHY Firmware but we'll neuter the error ...
3524 */
3525 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3526 "/lib/firmware/%s, error %d\n",
3527 phy_info->phy_fw_file, -ret);
3528 if (phy_info->phy_flash) {
3529 int cur_phy_fw_ver = 0;
3530
3531 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3532 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3533 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3534 ret = 0;
3535 }
3536
3537 return ret;
3538 }
3539
3540 /* Load PHY Firmware onto adapter.
3541 */
3542 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3543 phy_info->phy_fw_version,
3544 (u8 *)phyf->data, phyf->size);
3545 if (ret < 0)
3546 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3547 -ret);
3548 else if (ret > 0) {
3549 int new_phy_fw_ver = 0;
3550
3551 if (phy_info->phy_fw_version)
3552 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3553 phyf->size);
3554 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3555 "Firmware /lib/firmware/%s, version %#x\n",
3556 phy_info->phy_fw_file, new_phy_fw_ver);
3557 }
3558
3559 release_firmware(phyf);
3560
3561 return ret;
3562}
3563
Vipul Pandya636f9d32012-09-26 02:39:39 +00003564/*
3565 * Attempt to initialize the adapter via a Firmware Configuration File.
3566 */
3567static int adap_init0_config(struct adapter *adapter, int reset)
3568{
3569 struct fw_caps_config_cmd caps_cmd;
3570 const struct firmware *cf;
3571 unsigned long mtype = 0, maddr = 0;
3572 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303573 int ret;
3574 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003575 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303576 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003577
3578 /*
3579 * Reset device if necessary.
3580 */
3581 if (reset) {
3582 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303583 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003584 if (ret < 0)
3585 goto bye;
3586 }
3587
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303588 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3589 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3590 * to be performed after any global adapter RESET above since some
3591 * PHYs only have local RAM copies of the PHY firmware.
3592 */
3593 if (is_10gbt_device(adapter->pdev->device)) {
3594 ret = adap_init0_phy(adapter);
3595 if (ret < 0)
3596 goto bye;
3597 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003598 /*
3599 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3600 * then use that. Otherwise, use the configuration file stored
3601 * in the adapter flash ...
3602 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303603 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003604 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303605 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003606 break;
3607 case CHELSIO_T5:
3608 fw_config_file = FW5_CFNAME;
3609 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303610 case CHELSIO_T6:
3611 fw_config_file = FW6_CFNAME;
3612 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003613 default:
3614 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3615 adapter->pdev->device);
3616 ret = -EINVAL;
3617 goto bye;
3618 }
3619
3620 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003621 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303622 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003623 mtype = FW_MEMTYPE_CF_FLASH;
3624 maddr = t4_flash_cfg_addr(adapter);
3625 } else {
3626 u32 params[7], val[7];
3627
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303628 sprintf(fw_config_file_path,
3629 "/lib/firmware/%s", fw_config_file);
3630 config_name = fw_config_file_path;
3631
Vipul Pandya636f9d32012-09-26 02:39:39 +00003632 if (cf->size >= FLASH_CFG_MAX_SIZE)
3633 ret = -ENOMEM;
3634 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303635 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3636 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003637 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303638 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003639 if (ret == 0) {
3640 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303641 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003642 * sizes have to be in terms of multiples of 4
3643 * bytes. So, if the Configuration File isn't
3644 * a multiple of 4 bytes in length we'll have
3645 * to write that out separately since we can't
3646 * guarantee that the bytes following the
3647 * residual byte in the buffer returned by
3648 * request_firmware() are zeroed out ...
3649 */
3650 size_t resid = cf->size & 0x3;
3651 size_t size = cf->size & ~0x3;
3652 __be32 *data = (__be32 *)cf->data;
3653
Hariprasad Shenai51678652014-11-21 12:52:02 +05303654 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3655 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003656
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303657 spin_lock(&adapter->win0_lock);
3658 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3659 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003660 if (ret == 0 && resid != 0) {
3661 union {
3662 __be32 word;
3663 char buf[4];
3664 } last;
3665 int i;
3666
3667 last.word = data[size >> 2];
3668 for (i = resid; i < 4; i++)
3669 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303670 ret = t4_memory_rw(adapter, 0, mtype,
3671 maddr + size,
3672 4, &last.word,
3673 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003674 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303675 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003676 }
3677 }
3678
3679 release_firmware(cf);
3680 if (ret)
3681 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003682 }
3683
Vipul Pandya636f9d32012-09-26 02:39:39 +00003684 /*
3685 * Issue a Capability Configuration command to the firmware to get it
3686 * to parse the Configuration File. We don't use t4_fw_config_file()
3687 * because we want the ability to modify various features after we've
3688 * processed the configuration file ...
3689 */
3690 memset(&caps_cmd, 0, sizeof(caps_cmd));
3691 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303692 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3693 FW_CMD_REQUEST_F |
3694 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303695 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303696 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3697 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3698 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003699 FW_LEN16(caps_cmd));
3700 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3701 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303702
3703 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3704 * Configuration File in FLASH), our last gasp effort is to use the
3705 * Firmware Configuration File which is embedded in the firmware. A
3706 * very few early versions of the firmware didn't have one embedded
3707 * but we can ignore those.
3708 */
3709 if (ret == -ENOENT) {
3710 memset(&caps_cmd, 0, sizeof(caps_cmd));
3711 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303712 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3713 FW_CMD_REQUEST_F |
3714 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303715 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3716 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3717 sizeof(caps_cmd), &caps_cmd);
3718 config_name = "Firmware Default";
3719 }
3720
3721 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003722 if (ret < 0)
3723 goto bye;
3724
Vipul Pandya636f9d32012-09-26 02:39:39 +00003725 finiver = ntohl(caps_cmd.finiver);
3726 finicsum = ntohl(caps_cmd.finicsum);
3727 cfcsum = ntohl(caps_cmd.cfcsum);
3728 if (finicsum != cfcsum)
3729 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3730 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3731 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003732
Vipul Pandya636f9d32012-09-26 02:39:39 +00003733 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003734 * And now tell the firmware to use the configuration we just loaded.
3735 */
3736 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303737 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3738 FW_CMD_REQUEST_F |
3739 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303740 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003741 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3742 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003743 if (ret < 0)
3744 goto bye;
3745
Vipul Pandya636f9d32012-09-26 02:39:39 +00003746 /*
3747 * Tweak configuration based on system architecture, module
3748 * parameters, etc.
3749 */
3750 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003751 if (ret < 0)
3752 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003753
Vipul Pandya636f9d32012-09-26 02:39:39 +00003754 /*
3755 * And finally tell the firmware to initialize itself using the
3756 * parameters from the Configuration File.
3757 */
3758 ret = t4_fw_initialize(adapter, adapter->mbox);
3759 if (ret < 0)
3760 goto bye;
3761
Hariprasad Shenai06640312015-01-13 15:19:25 +05303762 /* Emit Firmware Configuration File information and return
3763 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003764 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003765 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303766 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3767 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003768 return 0;
3769
3770 /*
3771 * Something bad happened. Return the error ... (If the "error"
3772 * is that there's no Configuration File on the adapter we don't
3773 * want to issue a warning since this is fairly common.)
3774 */
3775bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303776 if (config_issued && ret != -ENOENT)
3777 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3778 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003779 return ret;
3780}
3781
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303782static struct fw_info fw_info_array[] = {
3783 {
3784 .chip = CHELSIO_T4,
3785 .fs_name = FW4_CFNAME,
3786 .fw_mod_name = FW4_FNAME,
3787 .fw_hdr = {
3788 .chip = FW_HDR_CHIP_T4,
3789 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3790 .intfver_nic = FW_INTFVER(T4, NIC),
3791 .intfver_vnic = FW_INTFVER(T4, VNIC),
3792 .intfver_ri = FW_INTFVER(T4, RI),
3793 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3794 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3795 },
3796 }, {
3797 .chip = CHELSIO_T5,
3798 .fs_name = FW5_CFNAME,
3799 .fw_mod_name = FW5_FNAME,
3800 .fw_hdr = {
3801 .chip = FW_HDR_CHIP_T5,
3802 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3803 .intfver_nic = FW_INTFVER(T5, NIC),
3804 .intfver_vnic = FW_INTFVER(T5, VNIC),
3805 .intfver_ri = FW_INTFVER(T5, RI),
3806 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3807 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3808 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303809 }, {
3810 .chip = CHELSIO_T6,
3811 .fs_name = FW6_CFNAME,
3812 .fw_mod_name = FW6_FNAME,
3813 .fw_hdr = {
3814 .chip = FW_HDR_CHIP_T6,
3815 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3816 .intfver_nic = FW_INTFVER(T6, NIC),
3817 .intfver_vnic = FW_INTFVER(T6, VNIC),
3818 .intfver_ofld = FW_INTFVER(T6, OFLD),
3819 .intfver_ri = FW_INTFVER(T6, RI),
3820 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3821 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3822 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3823 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3824 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303825 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303826
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303827};
3828
3829static struct fw_info *find_fw_info(int chip)
3830{
3831 int i;
3832
3833 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3834 if (fw_info_array[i].chip == chip)
3835 return &fw_info_array[i];
3836 }
3837 return NULL;
3838}
3839
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003840/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003841 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003842 */
3843static int adap_init0(struct adapter *adap)
3844{
3845 int ret;
3846 u32 v, port_vec;
3847 enum dev_state state;
3848 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003849 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303850 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003851
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303852 /* Grab Firmware Device Log parameters as early as possible so we have
3853 * access to it for debugging, etc.
3854 */
3855 ret = t4_init_devlog_params(adap);
3856 if (ret < 0)
3857 return ret;
3858
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303859 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303860 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3861 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003862 if (ret < 0) {
3863 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3864 ret);
3865 return ret;
3866 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003867 if (ret == adap->mbox)
3868 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003869
Vipul Pandya636f9d32012-09-26 02:39:39 +00003870 /*
3871 * If we're the Master PF Driver and the device is uninitialized,
3872 * then let's consider upgrading the firmware ... (We always want
3873 * to check the firmware version number in order to A. get it for
3874 * later reporting and B. to warn if the currently loaded firmware
3875 * is excessively mismatched relative to the driver.)
3876 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303877
Ganesh Goudar760446f2017-07-20 18:28:48 +05303878 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303879 ret = t4_check_fw_version(adap);
3880 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303881 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303882 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003883 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303884 struct fw_info *fw_info;
3885 struct fw_hdr *card_fw;
3886 const struct firmware *fw;
3887 const u8 *fw_data = NULL;
3888 unsigned int fw_size = 0;
3889
3890 /* This is the firmware whose headers the driver was compiled
3891 * against
3892 */
3893 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3894 if (fw_info == NULL) {
3895 dev_err(adap->pdev_dev,
3896 "unable to get firmware info for chip %d.\n",
3897 CHELSIO_CHIP_VERSION(adap->params.chip));
3898 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003899 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303900
3901 /* allocate memory to read the header of the firmware on the
3902 * card
3903 */
Michal Hocko752ade62017-05-08 15:57:27 -07003904 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303905
3906 /* Get FW from from /lib/firmware/ */
3907 ret = request_firmware(&fw, fw_info->fw_mod_name,
3908 adap->pdev_dev);
3909 if (ret < 0) {
3910 dev_err(adap->pdev_dev,
3911 "unable to load firmware image %s, error %d\n",
3912 fw_info->fw_mod_name, ret);
3913 } else {
3914 fw_data = fw->data;
3915 fw_size = fw->size;
3916 }
3917
3918 /* upgrade FW logic */
3919 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3920 state, &reset);
3921
3922 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003923 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003924 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303925
Vipul Pandya636f9d32012-09-26 02:39:39 +00003926 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303927 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003928 }
3929
3930 /*
3931 * Grab VPD parameters. This should be done after we establish a
3932 * connection to the firmware since some of the VPD parameters
3933 * (notably the Core Clock frequency) are retrieved via requests to
3934 * the firmware. On the other hand, we need these fairly early on
3935 * so we do this right after getting ahold of the firmware.
3936 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303937 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003938 if (ret < 0)
3939 goto bye;
3940
Vipul Pandya636f9d32012-09-26 02:39:39 +00003941 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003942 * Find out what ports are available to us. Note that we need to do
3943 * this before calling adap_init0_no_config() since it needs nports
3944 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003945 */
3946 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303947 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3948 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303949 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003950 if (ret < 0)
3951 goto bye;
3952
3953 adap->params.nports = hweight32(port_vec);
3954 adap->params.portvec = port_vec;
3955
Hariprasad Shenai06640312015-01-13 15:19:25 +05303956 /* If the firmware is initialized already, emit a simply note to that
3957 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003958 */
3959 if (state == DEV_STATE_INIT) {
3960 dev_info(adap->pdev_dev, "Coming up as %s: "\
3961 "Adapter already initialized\n",
3962 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003963 } else {
3964 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3965 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303966
3967 /* Find out whether we're dealing with a version of the
3968 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003969 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303970 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3971 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303972 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303973 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003974
Hariprasad Shenai06640312015-01-13 15:19:25 +05303975 /* If the firmware doesn't support Configuration Files,
3976 * return an error.
3977 */
3978 if (ret < 0) {
3979 dev_err(adap->pdev_dev, "firmware doesn't support "
3980 "Firmware Configuration Files\n");
3981 goto bye;
3982 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003983
Hariprasad Shenai06640312015-01-13 15:19:25 +05303984 /* The firmware provides us with a memory buffer where we can
3985 * load a Configuration File from the host if we want to
3986 * override the Configuration File in flash.
3987 */
3988 ret = adap_init0_config(adap, reset);
3989 if (ret == -ENOENT) {
3990 dev_err(adap->pdev_dev, "no Configuration File "
3991 "present on adapter.\n");
3992 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003993 }
3994 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303995 dev_err(adap->pdev_dev, "could not initialize "
3996 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003997 goto bye;
3998 }
3999 }
4000
Hariprasad Shenai06640312015-01-13 15:19:25 +05304001 /* Give the SGE code a chance to pull in anything that it needs ...
4002 * Note that this must be called after we retrieve our VPD parameters
4003 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004004 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05304005 ret = t4_sge_init(adap);
4006 if (ret < 0)
4007 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004008
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00004009 if (is_bypass_device(adap->pdev->device))
4010 adap->params.bypass = 1;
4011
Vipul Pandya636f9d32012-09-26 02:39:39 +00004012 /*
4013 * Grab some of our basic fundamental operating parameters.
4014 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004015#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05304016 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
4017 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004018
4019#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05304020 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
4021 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
4022 FW_PARAMS_PARAM_Y_V(0) | \
4023 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004024
Vipul Pandya636f9d32012-09-26 02:39:39 +00004025 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004026 params[1] = FW_PARAM_PFVF(L2T_START);
4027 params[2] = FW_PARAM_PFVF(L2T_END);
4028 params[3] = FW_PARAM_PFVF(FILTER_START);
4029 params[4] = FW_PARAM_PFVF(FILTER_END);
4030 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304031 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004032 if (ret < 0)
4033 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004034 adap->sge.egr_start = val[0];
4035 adap->l2t_start = val[1];
4036 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004037 adap->tids.ftid_base = val[3];
4038 adap->tids.nftids = val[4] - val[3] + 1;
4039 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004040
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304041 /* qids (ingress/egress) returned from firmware can be anywhere
4042 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
4043 * Hence driver needs to allocate memory for this range to
4044 * store the queue info. Get the highest IQFLINT/EQ index returned
4045 * in FW_EQ_*_CMD.alloc command.
4046 */
4047 params[0] = FW_PARAM_PFVF(EQ_END);
4048 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304049 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304050 if (ret < 0)
4051 goto bye;
4052 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
4053 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
4054
4055 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
4056 sizeof(*adap->sge.egr_map), GFP_KERNEL);
4057 if (!adap->sge.egr_map) {
4058 ret = -ENOMEM;
4059 goto bye;
4060 }
4061
4062 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
4063 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
4064 if (!adap->sge.ingr_map) {
4065 ret = -ENOMEM;
4066 goto bye;
4067 }
4068
4069 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304070 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304071 */
4072 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4073 sizeof(long), GFP_KERNEL);
4074 if (!adap->sge.starving_fl) {
4075 ret = -ENOMEM;
4076 goto bye;
4077 }
4078
4079 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4080 sizeof(long), GFP_KERNEL);
4081 if (!adap->sge.txq_maperr) {
4082 ret = -ENOMEM;
4083 goto bye;
4084 }
4085
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304086#ifdef CONFIG_DEBUG_FS
4087 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4088 sizeof(long), GFP_KERNEL);
4089 if (!adap->sge.blocked_fl) {
4090 ret = -ENOMEM;
4091 goto bye;
4092 }
4093#endif
4094
Anish Bhattb5a02f52015-01-14 15:17:34 -08004095 params[0] = FW_PARAM_PFVF(CLIP_START);
4096 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304097 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08004098 if (ret < 0)
4099 goto bye;
4100 adap->clipt_start = val[0];
4101 adap->clipt_end = val[1];
4102
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304103 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
4104 * Classes supported by the hardware/firmware so we hard code it here
4105 * for now.
4106 */
4107 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
4108
Vipul Pandya636f9d32012-09-26 02:39:39 +00004109 /* query params related to active filter region */
4110 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
4111 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304112 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004113 /* If Active filter size is set we enable establishing
4114 * offload connection through firmware work request
4115 */
4116 if ((val[0] != val[1]) && (ret >= 0)) {
4117 adap->flags |= FW_OFLD_CONN;
4118 adap->tids.aftid_base = val[0];
4119 adap->tids.aftid_end = val[1];
4120 }
4121
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004122 /* If we're running on newer firmware, let it know that we're
4123 * prepared to deal with encapsulated CPL messages. Older
4124 * firmware won't understand this and we'll just get
4125 * unencapsulated messages ...
4126 */
4127 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
4128 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304129 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004130
Vipul Pandya636f9d32012-09-26 02:39:39 +00004131 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304132 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
4133 * capability. Earlier versions of the firmware didn't have the
4134 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
4135 * permission to use ULPTX MEMWRITE DSGL.
4136 */
4137 if (is_t4(adap->params.chip)) {
4138 adap->params.ulptx_memwrite_dsgl = false;
4139 } else {
4140 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304141 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304142 1, params, val);
4143 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4144 }
4145
Steve Wise086de572016-09-16 07:54:49 -07004146 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
4147 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
4148 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4149 1, params, val);
4150 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
4151
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05304152 /* See if FW supports FW_FILTER2 work request */
4153 if (is_t4(adap->params.chip)) {
4154 adap->params.filter2_wr_support = 0;
4155 } else {
4156 params[0] = FW_PARAM_DEV(FILTER2_WR);
4157 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4158 1, params, val);
4159 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
4160 }
4161
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304162 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004163 * Get device capabilities so we can determine what resources we need
4164 * to manage.
4165 */
4166 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304167 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4168 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304169 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004170 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4171 &caps_cmd);
4172 if (ret < 0)
4173 goto bye;
4174
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304175 if (caps_cmd.ofldcaps ||
4176 (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER))) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004177 /* query offload-related parameters */
4178 params[0] = FW_PARAM_DEV(NTID);
4179 params[1] = FW_PARAM_PFVF(SERVER_START);
4180 params[2] = FW_PARAM_PFVF(SERVER_END);
4181 params[3] = FW_PARAM_PFVF(TDDP_START);
4182 params[4] = FW_PARAM_PFVF(TDDP_END);
4183 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304184 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004185 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004186 if (ret < 0)
4187 goto bye;
4188 adap->tids.ntids = val[0];
4189 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4190 adap->tids.stid_base = val[1];
4191 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004192 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004193 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004194 * region into two parts. Regular filters get 1/3rd and server
4195 * filters get 2/3rd part. This is only enabled if workarond
4196 * path is enabled.
4197 * 1. For regular filters.
4198 * 2. Server filter: This are special filters which are used
4199 * to redirect SYN packets to offload queue.
4200 */
4201 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4202 adap->tids.sftid_base = adap->tids.ftid_base +
4203 DIV_ROUND_UP(adap->tids.nftids, 3);
4204 adap->tids.nsftids = adap->tids.nftids -
4205 DIV_ROUND_UP(adap->tids.nftids, 3);
4206 adap->tids.nftids = adap->tids.sftid_base -
4207 adap->tids.ftid_base;
4208 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004209 adap->vres.ddp.start = val[3];
4210 adap->vres.ddp.size = val[4] - val[3] + 1;
4211 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004212
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304213 if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4214 if (init_hash_filter(adap) < 0)
4215 goto bye;
4216 } else {
4217 adap->params.offload = 1;
4218 adap->num_ofld_uld += 1;
4219 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004220 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004221 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004222 params[0] = FW_PARAM_PFVF(STAG_START);
4223 params[1] = FW_PARAM_PFVF(STAG_END);
4224 params[2] = FW_PARAM_PFVF(RQ_START);
4225 params[3] = FW_PARAM_PFVF(RQ_END);
4226 params[4] = FW_PARAM_PFVF(PBL_START);
4227 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304228 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004229 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004230 if (ret < 0)
4231 goto bye;
4232 adap->vres.stag.start = val[0];
4233 adap->vres.stag.size = val[1] - val[0] + 1;
4234 adap->vres.rq.start = val[2];
4235 adap->vres.rq.size = val[3] - val[2] + 1;
4236 adap->vres.pbl.start = val[4];
4237 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004238
4239 params[0] = FW_PARAM_PFVF(SQRQ_START);
4240 params[1] = FW_PARAM_PFVF(SQRQ_END);
4241 params[2] = FW_PARAM_PFVF(CQ_START);
4242 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004243 params[4] = FW_PARAM_PFVF(OCQ_START);
4244 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304245 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304246 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004247 if (ret < 0)
4248 goto bye;
4249 adap->vres.qp.start = val[0];
4250 adap->vres.qp.size = val[1] - val[0] + 1;
4251 adap->vres.cq.start = val[2];
4252 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004253 adap->vres.ocq.start = val[4];
4254 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304255
4256 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4257 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304258 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304259 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304260 if (ret < 0) {
4261 adap->params.max_ordird_qp = 8;
4262 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4263 ret = 0;
4264 } else {
4265 adap->params.max_ordird_qp = val[0];
4266 adap->params.max_ird_adapter = val[1];
4267 }
4268 dev_info(adap->pdev_dev,
4269 "max_ordird_qp %d max_ird_adapter %d\n",
4270 adap->params.max_ordird_qp,
4271 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304272 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004273 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004274 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004275 params[0] = FW_PARAM_PFVF(ISCSI_START);
4276 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304277 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004278 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004279 if (ret < 0)
4280 goto bye;
4281 adap->vres.iscsi.start = val[0];
4282 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304283 /* LIO target and cxgb4i initiaitor */
4284 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004285 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304286 if (caps_cmd.cryptocaps) {
4287 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304288 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4289 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4290 params, val);
4291 if (ret < 0) {
4292 if (ret != -EINVAL)
4293 goto bye;
4294 } else {
4295 adap->vres.ncrypto_fc = val[0];
4296 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304297 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4298 adap->num_uld += 1;
4299 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004300#undef FW_PARAM_PFVF
4301#undef FW_PARAM_DEV
4302
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304303 /* The MTU/MSS Table is initialized by now, so load their values. If
4304 * we're initializing the adapter, then we'll make any modifications
4305 * we want to the MTU/MSS Table and also initialize the congestion
4306 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004307 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004308 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304309 if (state != DEV_STATE_INIT) {
4310 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004311
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304312 /* The default MTU Table contains values 1492 and 1500.
4313 * However, for TCP, it's better to have two values which are
4314 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4315 * This allows us to have a TCP Data Payload which is a
4316 * multiple of 8 regardless of what combination of TCP Options
4317 * are in use (always a multiple of 4 bytes) which is
4318 * important for performance reasons. For instance, if no
4319 * options are in use, then we have a 20-byte IP header and a
4320 * 20-byte TCP header. In this case, a 1500-byte MSS would
4321 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4322 * which is not a multiple of 8. So using an MSS of 1488 in
4323 * this case results in a TCP Data Payload of 1448 bytes which
4324 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4325 * Stamps have been negotiated, then an MTU of 1500 bytes
4326 * results in a TCP Data Payload of 1448 bytes which, as
4327 * above, is a multiple of 8 bytes ...
4328 */
4329 for (i = 0; i < NMTUS; i++)
4330 if (adap->params.mtus[i] == 1492) {
4331 adap->params.mtus[i] = 1488;
4332 break;
4333 }
4334
4335 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4336 adap->params.b_wnd);
4337 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304338 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004339 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304340 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004341 return 0;
4342
4343 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004344 * Something bad happened. If a command timed out or failed with EIO
4345 * FW does not operate within its spec or something catastrophic
4346 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004347 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004348bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304349 kfree(adap->sge.egr_map);
4350 kfree(adap->sge.ingr_map);
4351 kfree(adap->sge.starving_fl);
4352 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304353#ifdef CONFIG_DEBUG_FS
4354 kfree(adap->sge.blocked_fl);
4355#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004356 if (ret != -ETIMEDOUT && ret != -EIO)
4357 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004358 return ret;
4359}
4360
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004361/* EEH callbacks */
4362
4363static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4364 pci_channel_state_t state)
4365{
4366 int i;
4367 struct adapter *adap = pci_get_drvdata(pdev);
4368
4369 if (!adap)
4370 goto out;
4371
4372 rtnl_lock();
4373 adap->flags &= ~FW_OK;
4374 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004375 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004376 for_each_port(adap, i) {
4377 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004378 if (dev) {
4379 netif_device_detach(dev);
4380 netif_carrier_off(dev);
4381 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004382 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004383 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304384 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004385 if (adap->flags & FULL_INIT_DONE)
4386 cxgb_down(adap);
4387 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004388 if ((adap->flags & DEV_ENABLED)) {
4389 pci_disable_device(pdev);
4390 adap->flags &= ~DEV_ENABLED;
4391 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004392out: return state == pci_channel_io_perm_failure ?
4393 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4394}
4395
4396static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4397{
4398 int i, ret;
4399 struct fw_caps_config_cmd c;
4400 struct adapter *adap = pci_get_drvdata(pdev);
4401
4402 if (!adap) {
4403 pci_restore_state(pdev);
4404 pci_save_state(pdev);
4405 return PCI_ERS_RESULT_RECOVERED;
4406 }
4407
Gavin Shan144be3d2014-01-23 12:27:34 +08004408 if (!(adap->flags & DEV_ENABLED)) {
4409 if (pci_enable_device(pdev)) {
4410 dev_err(&pdev->dev, "Cannot reenable PCI "
4411 "device after reset\n");
4412 return PCI_ERS_RESULT_DISCONNECT;
4413 }
4414 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004415 }
4416
4417 pci_set_master(pdev);
4418 pci_restore_state(pdev);
4419 pci_save_state(pdev);
4420 pci_cleanup_aer_uncorrect_error_status(pdev);
4421
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304422 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004423 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304424 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004425 return PCI_ERS_RESULT_DISCONNECT;
4426 adap->flags |= FW_OK;
4427 if (adap_init1(adap, &c))
4428 return PCI_ERS_RESULT_DISCONNECT;
4429
4430 for_each_port(adap, i) {
4431 struct port_info *p = adap2pinfo(adap, i);
4432
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304433 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004434 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004435 if (ret < 0)
4436 return PCI_ERS_RESULT_DISCONNECT;
4437 p->viid = ret;
4438 p->xact_addr_filt = -1;
4439 }
4440
4441 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4442 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004443 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004444 if (cxgb_up(adap))
4445 return PCI_ERS_RESULT_DISCONNECT;
4446 return PCI_ERS_RESULT_RECOVERED;
4447}
4448
4449static void eeh_resume(struct pci_dev *pdev)
4450{
4451 int i;
4452 struct adapter *adap = pci_get_drvdata(pdev);
4453
4454 if (!adap)
4455 return;
4456
4457 rtnl_lock();
4458 for_each_port(adap, i) {
4459 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004460 if (dev) {
4461 if (netif_running(dev)) {
4462 link_start(dev);
4463 cxgb_set_rxmode(dev);
4464 }
4465 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004466 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004467 }
4468 rtnl_unlock();
4469}
4470
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004471static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004472 .error_detected = eeh_err_detected,
4473 .slot_reset = eeh_slot_reset,
4474 .resume = eeh_resume,
4475};
4476
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304477/* Return true if the Link Configuration supports "High Speeds" (those greater
4478 * than 1Gb/s).
4479 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304480static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004481{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304482 unsigned int speeds, high_speeds;
4483
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304484 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4485 high_speeds = speeds &
4486 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304487
4488 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004489}
4490
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004491/*
4492 * Perform default configuration of DMA queues depending on the number and type
4493 * of ports we found and the number of available CPUs. Most settings can be
4494 * modified by the admin prior to actual use.
4495 */
Bill Pemberton91744942012-12-03 09:23:02 -05004496static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004497{
4498 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304499 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004500#ifndef CONFIG_CHELSIO_T4_DCB
4501 int q10g = 0;
4502#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004503
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304504 /* Reduce memory usage in kdump environment, disable all offload.
4505 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304506 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304507 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304508 adap->params.crypto = 0;
4509 }
4510
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304511 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004512#ifdef CONFIG_CHELSIO_T4_DCB
4513 /* For Data Center Bridging support we need to be able to support up
4514 * to 8 Traffic Priorities; each of which will be assigned to its
4515 * own TX Queue in order to prevent Head-Of-Line Blocking.
4516 */
4517 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4518 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4519 MAX_ETH_QSETS, adap->params.nports * 8);
4520 BUG_ON(1);
4521 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004522
Anish Bhatt688848b2014-06-19 21:37:13 -07004523 for_each_port(adap, i) {
4524 struct port_info *pi = adap2pinfo(adap, i);
4525
4526 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304527 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004528 qidx += pi->nqsets;
4529 }
4530#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004531 /*
4532 * We default to 1 queue per non-10G port and up to # of cores queues
4533 * per 10G port.
4534 */
4535 if (n10g)
4536 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004537 if (q10g > netif_get_num_default_rss_queues())
4538 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004539
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304540 if (is_kdump_kernel())
4541 q10g = 1;
4542
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004543 for_each_port(adap, i) {
4544 struct port_info *pi = adap2pinfo(adap, i);
4545
4546 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304547 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004548 qidx += pi->nqsets;
4549 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004550#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004551
4552 s->ethqsets = qidx;
4553 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4554
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304555 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004556 /*
4557 * For offload we use 1 queue/channel if all ports are up to 1G,
4558 * otherwise we divide all available queues amongst the channels
4559 * capped by the number of available cores.
4560 */
4561 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304562 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304563 s->ofldqsets = roundup(i, adap->params.nports);
4564 } else {
4565 s->ofldqsets = adap->params.nports;
4566 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004567 }
4568
4569 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4570 struct sge_eth_rxq *r = &s->ethrxq[i];
4571
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304572 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004573 r->fl.size = 72;
4574 }
4575
4576 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4577 s->ethtxq[i].q.size = 1024;
4578
4579 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4580 s->ctrlq[i].q.size = 512;
4581
Atul Guptaa45695042017-07-04 16:46:20 +05304582 if (!is_t4(adap->params.chip))
4583 s->ptptxq.q.size = 8;
4584
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304585 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304586 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004587}
4588
4589/*
4590 * Reduce the number of Ethernet queues across all ports to at most n.
4591 * n provides at least one queue per port.
4592 */
Bill Pemberton91744942012-12-03 09:23:02 -05004593static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004594{
4595 int i;
4596 struct port_info *pi;
4597
4598 while (n < adap->sge.ethqsets)
4599 for_each_port(adap, i) {
4600 pi = adap2pinfo(adap, i);
4601 if (pi->nqsets > 1) {
4602 pi->nqsets--;
4603 adap->sge.ethqsets--;
4604 if (adap->sge.ethqsets <= n)
4605 break;
4606 }
4607 }
4608
4609 n = 0;
4610 for_each_port(adap, i) {
4611 pi = adap2pinfo(adap, i);
4612 pi->first_qset = n;
4613 n += pi->nqsets;
4614 }
4615}
4616
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304617static int get_msix_info(struct adapter *adap)
4618{
4619 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304620 unsigned int max_ingq = 0;
4621
4622 if (is_offload(adap))
4623 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4624 if (is_pci_uld(adap))
4625 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4626
4627 if (!max_ingq)
4628 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304629
4630 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4631 if (!msix_info)
4632 return -ENOMEM;
4633
4634 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4635 sizeof(long), GFP_KERNEL);
4636 if (!adap->msix_bmap_ulds.msix_bmap) {
4637 kfree(msix_info);
4638 return -ENOMEM;
4639 }
4640 spin_lock_init(&adap->msix_bmap_ulds.lock);
4641 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304642out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304643 return 0;
4644}
4645
4646static void free_msix_info(struct adapter *adap)
4647{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304648 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304649 return;
4650
4651 kfree(adap->msix_info_ulds);
4652 kfree(adap->msix_bmap_ulds.msix_bmap);
4653}
4654
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004655/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4656#define EXTRA_VECS 2
4657
Bill Pemberton91744942012-12-03 09:23:02 -05004658static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004659{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304660 int ofld_need = 0, uld_need = 0;
4661 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004662 struct sge *s = &adap->sge;
4663 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304664 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304665 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004666
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304667 if (is_pci_uld(adap))
4668 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4669 if (is_offload(adap))
4670 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304671 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304672 GFP_KERNEL);
4673 if (!entries)
4674 return -ENOMEM;
4675
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304676 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304677 if (get_msix_info(adap)) {
4678 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304679 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304680 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304681
4682 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004683 entries[i].entry = i;
4684
4685 want = s->max_ethqsets + EXTRA_VECS;
4686 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304687 want += adap->num_ofld_uld * s->ofldqsets;
4688 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004689 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304690 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304691 want += adap->num_uld * s->ofldqsets;
4692 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304693 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004694#ifdef CONFIG_CHELSIO_T4_DCB
4695 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4696 * each port.
4697 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304698 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004699#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304700 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004701#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304702 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4703 if (allocated < 0) {
4704 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4705 " not using MSI-X\n");
4706 kfree(entries);
4707 return allocated;
4708 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004709
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304710 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004711 * Every group gets its minimum requirement and NIC gets top
4712 * priority for leftovers.
4713 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304714 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004715 if (i < s->max_ethqsets) {
4716 s->max_ethqsets = i;
4717 if (i < s->ethqsets)
4718 reduce_ethqs(adap, i);
4719 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304720 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304721 if (allocated < want)
4722 s->nqs_per_uld = nchan;
4723 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304724 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304725 }
4726
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304727 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004728 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304729 if (is_uld(adap)) {
4730 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304731 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304732 adap->msix_info_ulds[j].idx = i;
4733 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304734 adap->msix_bmap_ulds.mapsize = j;
4735 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304736 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304737 "nic %d per uld %d\n",
4738 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004739
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304740 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004741 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004742}
4743
4744#undef EXTRA_VECS
4745
Bill Pemberton91744942012-12-03 09:23:02 -05004746static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004747{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304748 unsigned int i;
4749 int err;
4750
4751 err = t4_init_rss_mode(adap, adap->mbox);
4752 if (err)
4753 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004754
4755 for_each_port(adap, i) {
4756 struct port_info *pi = adap2pinfo(adap, i);
4757
4758 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4759 if (!pi->rss)
4760 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004761 }
4762 return 0;
4763}
4764
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304765static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4766 enum pci_bus_speed *speed,
4767 enum pcie_link_width *width)
4768{
4769 u32 lnkcap1, lnkcap2;
4770 int err1, err2;
4771
4772#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4773
4774 *speed = PCI_SPEED_UNKNOWN;
4775 *width = PCIE_LNK_WIDTH_UNKNOWN;
4776
4777 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4778 &lnkcap1);
4779 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4780 &lnkcap2);
4781 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4782 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4783 *speed = PCIE_SPEED_8_0GT;
4784 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4785 *speed = PCIE_SPEED_5_0GT;
4786 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4787 *speed = PCIE_SPEED_2_5GT;
4788 }
4789 if (!err1) {
4790 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4791 if (!lnkcap2) { /* pre-r3.0 */
4792 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4793 *speed = PCIE_SPEED_5_0GT;
4794 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4795 *speed = PCIE_SPEED_2_5GT;
4796 }
4797 }
4798
4799 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4800 return err1 ? err1 : err2 ? err2 : -EINVAL;
4801 return 0;
4802}
4803
4804static void cxgb4_check_pcie_caps(struct adapter *adap)
4805{
4806 enum pcie_link_width width, width_cap;
4807 enum pci_bus_speed speed, speed_cap;
4808
4809#define PCIE_SPEED_STR(speed) \
4810 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4811 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4812 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4813 "Unknown")
4814
4815 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4816 dev_warn(adap->pdev_dev,
4817 "Unable to determine PCIe device BW capabilities\n");
4818 return;
4819 }
4820
4821 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4822 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4823 dev_warn(adap->pdev_dev,
4824 "Unable to determine PCI Express bandwidth.\n");
4825 return;
4826 }
4827
4828 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4829 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4830 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4831 width, width_cap);
4832 if (speed < speed_cap || width < width_cap)
4833 dev_info(adap->pdev_dev,
4834 "A slot with more lanes and/or higher speed is "
4835 "suggested for optimal performance.\n");
4836}
4837
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304838/* Dump basic information about the adapter */
4839static void print_adapter_info(struct adapter *adapter)
4840{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304841 /* Hardware/Firmware/etc. Version/Revision IDs */
4842 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304843
4844 /* Software/Hardware configuration */
4845 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4846 is_offload(adapter) ? "R" : "",
4847 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4848 (adapter->flags & USING_MSI) ? "MSI" : ""),
4849 is_offload(adapter) ? "Offload" : "non-Offload");
4850}
4851
Bill Pemberton91744942012-12-03 09:23:02 -05004852static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004853{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004854 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004855 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004856 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004857 const struct port_info *pi = netdev_priv(dev);
4858 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004859
4860 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4861 spd = " 2.5 GT/s";
4862 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4863 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004864 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4865 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004866
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304867 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304868 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304869 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304870 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304871 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004872 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304873 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304874 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304875 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304876 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304877 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4878 bufp += sprintf(bufp, "50G/");
4879 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304880 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304881 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4882 bufp += sprintf(bufp, "200G/");
4883 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4884 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004885 if (bufp != buf)
4886 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304887 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004888
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304889 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4890 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004891}
4892
Dimitris Michailidis06546392010-07-11 12:01:16 +00004893/*
4894 * Free the following resources:
4895 * - memory used for tables
4896 * - MSI/MSI-X
4897 * - net devices
4898 * - resources FW is holding for us
4899 */
4900static void free_some_resources(struct adapter *adapter)
4901{
4902 unsigned int i;
4903
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05304904 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07004905 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304906 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004907 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304908 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304909 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304910 kfree(adapter->sge.egr_map);
4911 kfree(adapter->sge.ingr_map);
4912 kfree(adapter->sge.starving_fl);
4913 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304914#ifdef CONFIG_DEBUG_FS
4915 kfree(adapter->sge.blocked_fl);
4916#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004917 disable_msi(adapter);
4918
4919 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004920 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304921 struct port_info *pi = adap2pinfo(adapter, i);
4922
4923 if (pi->viid != 0)
4924 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4925 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004926 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004927 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004928 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004929 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304930 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004931}
4932
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004933#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004934#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004935 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004936#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004937
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304938static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4939{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304940 u16 device_id;
4941
4942 /* Retrieve adapter's device ID */
4943 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004944
4945 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304946 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004947 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304948 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004949 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304950 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004951 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304952 default:
4953 dev_err(&pdev->dev, "Device %d is not supported\n",
4954 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304955 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004956 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304957}
4958
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304959#ifdef CONFIG_PCI_IOV
Ganesh Goudarbaf50862018-01-16 16:17:40 +05304960static void cxgb4_mgmt_setup(struct net_device *dev)
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304961{
4962 dev->type = ARPHRD_NONE;
4963 dev->mtu = 0;
4964 dev->hard_header_len = 0;
4965 dev->addr_len = 0;
4966 dev->tx_queue_len = 0;
4967 dev->flags |= IFF_NOARP;
4968 dev->priv_flags |= IFF_NO_QUEUE;
4969
4970 /* Initialize the device structure. */
4971 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4972 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004973 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304974}
4975
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304976static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4977{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304978 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304979 int err = 0;
4980 int current_vfs = pci_num_vf(pdev);
4981 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304982
Hariprasad Shenai78294512016-08-11 21:06:23 +05304983 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304984 /* Check if cxgb4 is the MASTER and fw is initialized */
4985 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4986 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
Ganesh Goudarbaf50862018-01-16 16:17:40 +05304987 PCIE_FW_MASTER_G(pcie_fw) != CXGB4_UNIFIED_PF) {
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304988 dev_warn(&pdev->dev,
4989 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4990 return -EOPNOTSUPP;
4991 }
4992
4993 /* If any of the VF's is already assigned to Guest OS, then
4994 * SRIOV for the same cannot be modified
4995 */
4996 if (current_vfs && pci_vfs_assigned(pdev)) {
4997 dev_err(&pdev->dev,
4998 "Cannot modify SR-IOV while VFs are assigned\n");
Ganesh Goudarbaf50862018-01-16 16:17:40 +05304999 return current_vfs;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305000 }
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305001 /* Note that the upper-level code ensures that we're never called with
5002 * a non-zero "num_vfs" when we already have VFs instantiated. But
5003 * it never hurts to code defensively.
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305004 */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305005 if (num_vfs != 0 && current_vfs != 0)
5006 return -EBUSY;
5007
5008 /* Nothing to do for no change. */
5009 if (num_vfs == current_vfs)
5010 return num_vfs;
5011
5012 /* Disable SRIOV when zero is passed. */
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305013 if (!num_vfs) {
5014 pci_disable_sriov(pdev);
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305015 /* free VF Management Interface */
5016 unregister_netdev(adap->port[0]);
5017 free_netdev(adap->port[0]);
5018 adap->port[0] = NULL;
5019
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305020 /* free VF resources */
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305021 adap->num_vfs = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305022 kfree(adap->vfinfo);
5023 adap->vfinfo = NULL;
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305024 return 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305025 }
5026
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305027 if (!current_vfs) {
5028 struct fw_pfvf_cmd port_cmd, port_rpl;
5029 struct net_device *netdev;
5030 unsigned int pmask, port;
5031 struct pci_dev *pbridge;
5032 struct port_info *pi;
5033 char name[IFNAMSIZ];
5034 u32 devcap2;
5035 u16 flags;
5036 int pos;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305037
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305038 /* If we want to instantiate Virtual Functions, then our
5039 * parent bridge's PCI-E needs to support Alternative Routing
5040 * ID (ARI) because our VFs will show up at function offset 8
5041 * and above.
5042 */
5043 pbridge = pdev->bus->self;
5044 pos = pci_find_capability(pbridge, PCI_CAP_ID_EXP);
5045 pci_read_config_word(pbridge, pos + PCI_EXP_FLAGS, &flags);
5046 pci_read_config_dword(pbridge, pos + PCI_EXP_DEVCAP2, &devcap2);
5047
5048 if ((flags & PCI_EXP_FLAGS_VERS) < 2 ||
5049 !(devcap2 & PCI_EXP_DEVCAP2_ARI)) {
5050 /* Our parent bridge does not support ARI so issue a
5051 * warning and skip instantiating the VFs. They
5052 * won't be reachable.
5053 */
5054 dev_warn(&pdev->dev, "Parent bridge %02x:%02x.%x doesn't support ARI; can't instantiate Virtual Functions\n",
5055 pbridge->bus->number, PCI_SLOT(pbridge->devfn),
5056 PCI_FUNC(pbridge->devfn));
5057 return -ENOTSUPP;
5058 }
5059 memset(&port_cmd, 0, sizeof(port_cmd));
5060 port_cmd.op_to_vfn = cpu_to_be32(FW_CMD_OP_V(FW_PFVF_CMD) |
5061 FW_CMD_REQUEST_F |
5062 FW_CMD_READ_F |
5063 FW_PFVF_CMD_PFN_V(adap->pf) |
5064 FW_PFVF_CMD_VFN_V(0));
5065 port_cmd.retval_len16 = cpu_to_be32(FW_LEN16(port_cmd));
5066 err = t4_wr_mbox(adap, adap->mbox, &port_cmd, sizeof(port_cmd),
5067 &port_rpl);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305068 if (err)
5069 return err;
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305070 pmask = FW_PFVF_CMD_PMASK_G(be32_to_cpu(port_rpl.type_to_neq));
5071 port = ffs(pmask) - 1;
5072 /* Allocate VF Management Interface. */
5073 snprintf(name, IFNAMSIZ, "mgmtpf%d,%d", adap->adap_idx,
5074 adap->pf);
5075 netdev = alloc_netdev(sizeof(struct port_info),
5076 name, NET_NAME_UNKNOWN, cxgb4_mgmt_setup);
5077 if (!netdev)
5078 return -ENOMEM;
5079
5080 pi = netdev_priv(netdev);
5081 pi->adapter = adap;
5082 pi->lport = port;
5083 pi->tx_chan = port;
5084 SET_NETDEV_DEV(netdev, &pdev->dev);
5085
5086 adap->port[0] = netdev;
5087 pi->port_id = 0;
5088
5089 err = register_netdev(adap->port[0]);
5090 if (err) {
5091 pr_info("Unable to register VF mgmt netdev %s\n", name);
5092 free_netdev(adap->port[0]);
5093 adap->port[0] = NULL;
5094 return err;
5095 }
5096 /* Allocate and set up VF Information. */
5097 adap->vfinfo = kcalloc(pci_sriov_get_totalvfs(pdev),
5098 sizeof(struct vf_info), GFP_KERNEL);
5099 if (!adap->vfinfo) {
5100 unregister_netdev(adap->port[0]);
5101 free_netdev(adap->port[0]);
5102 adap->port[0] = NULL;
5103 return -ENOMEM;
5104 }
5105 cxgb4_mgmt_fill_vf_station_mac_addr(adap);
5106 }
5107 /* Instantiate the requested number of VFs. */
5108 err = pci_enable_sriov(pdev, num_vfs);
5109 if (err) {
5110 pr_info("Unable to instantiate %d VFs\n", num_vfs);
5111 if (!current_vfs) {
5112 unregister_netdev(adap->port[0]);
5113 free_netdev(adap->port[0]);
5114 adap->port[0] = NULL;
5115 kfree(adap->vfinfo);
5116 adap->vfinfo = NULL;
5117 }
5118 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305119 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305120
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305121 adap->num_vfs = num_vfs;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305122 return num_vfs;
5123}
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305124#endif /* CONFIG_PCI_IOV */
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305125
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00005126static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005127{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005128 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005129 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005130 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005131 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305132 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305133 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305134 u32 whoami, pl_rev;
5135 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305136 static int adap_idx = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005137
5138 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
5139
5140 err = pci_request_regions(pdev, KBUILD_MODNAME);
5141 if (err) {
5142 /* Just info, some other driver may have claimed the device. */
5143 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
5144 return err;
5145 }
5146
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005147 err = pci_enable_device(pdev);
5148 if (err) {
5149 dev_err(&pdev->dev, "cannot enable PCI device\n");
5150 goto out_release_regions;
5151 }
5152
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305153 regs = pci_ioremap_bar(pdev, 0);
5154 if (!regs) {
5155 dev_err(&pdev->dev, "cannot map device registers\n");
5156 err = -ENOMEM;
5157 goto out_disable_device;
5158 }
5159
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305160 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5161 if (!adapter) {
5162 err = -ENOMEM;
5163 goto out_unmap_bar0;
5164 }
5165
5166 adapter->regs = regs;
Hariprasad Shenai8203b502014-10-09 05:48:47 +05305167 err = t4_wait_dev_ready(regs);
5168 if (err < 0)
5169 goto out_unmap_bar0;
5170
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305171 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305172 whoami = readl(regs + PL_WHOAMI_A);
5173 pl_rev = REV_G(readl(regs + PL_REV_A));
5174 chip = get_chip_type(pdev, pl_rev);
5175 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
5176 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305177
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305178 adapter->pdev = pdev;
5179 adapter->pdev_dev = &pdev->dev;
5180 adapter->name = pci_name(pdev);
5181 adapter->mbox = func;
5182 adapter->pf = func;
5183 adapter->msg_enable = DFLT_MSG_ENABLE;
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305184 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5185 (sizeof(struct mbox_cmd) *
5186 T4_OS_LOG_MBOX_CMDS),
5187 GFP_KERNEL);
5188 if (!adapter->mbox_log) {
5189 err = -ENOMEM;
5190 goto out_free_adapter;
5191 }
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305192 spin_lock_init(&adapter->mbox_lock);
5193 INIT_LIST_HEAD(&adapter->mlist.list);
5194 pci_set_drvdata(pdev, adapter);
5195
5196 if (func != ent->driver_data) {
5197 pci_disable_device(pdev);
5198 pci_save_state(pdev); /* to restore SR-IOV later */
5199 return 0;
5200 }
5201
5202 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
5203 highdma = true;
5204 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
5205 if (err) {
5206 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
5207 "coherent allocations\n");
5208 goto out_free_adapter;
5209 }
5210 } else {
5211 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5212 if (err) {
5213 dev_err(&pdev->dev, "no usable DMA configuration\n");
5214 goto out_free_adapter;
5215 }
5216 }
5217
5218 pci_enable_pcie_error_reporting(pdev);
5219 pci_set_master(pdev);
5220 pci_save_state(pdev);
5221 adap_idx++;
5222 adapter->workq = create_singlethread_workqueue("cxgb4");
5223 if (!adapter->workq) {
5224 err = -ENOMEM;
5225 goto out_free_adapter;
5226 }
5227
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305228 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
5229
Gavin Shan144be3d2014-01-23 12:27:34 +08005230 /* PCI device has been enabled */
5231 adapter->flags |= DEV_ENABLED;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005232 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
5233
Casey Leedomb0ba9d52017-08-15 11:23:26 +08005234 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
5235 * Ingress Packet Data to Free List Buffers in order to allow for
5236 * chipset performance optimizations between the Root Complex and
5237 * Memory Controllers. (Messages to the associated Ingress Queue
5238 * notifying new Packet Placement in the Free Lists Buffers will be
5239 * send without the Relaxed Ordering Attribute thus guaranteeing that
5240 * all preceding PCIe Transaction Layer Packets will be processed
5241 * first.) But some Root Complexes have various issues with Upstream
5242 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
5243 * The PCIe devices which under the Root Complexes will be cleared the
5244 * Relaxed Ordering bit in the configuration space, So we check our
5245 * PCIe configuration space to see if it's flagged with advice against
5246 * using Relaxed Ordering.
5247 */
5248 if (!pcie_relaxed_ordering_enabled(pdev))
5249 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
5250
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005251 spin_lock_init(&adapter->stats_lock);
5252 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07005253 spin_lock_init(&adapter->win0_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005254
5255 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305256 INIT_WORK(&adapter->db_full_task, process_db_full);
5257 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005258
5259 err = t4_prep_adapter(adapter);
5260 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305261 goto out_free_adapter;
5262
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005263
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305264 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305265 s_qpp = (QUEUESPERPAGEPF0_S +
5266 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305267 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305268 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5269 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005270 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5271
5272 /* Each segment size is 128B. Write coalescing is enabled only
5273 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5274 * queue is less no of segments that can be accommodated in
5275 * a page size.
5276 */
5277 if (qpp > num_seg) {
5278 dev_err(&pdev->dev,
5279 "Incorrect number of egress queues per page\n");
5280 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305281 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005282 }
5283 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5284 pci_resource_len(pdev, 2));
5285 if (!adapter->bar2) {
5286 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5287 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305288 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005289 }
5290 }
5291
Vipul Pandya636f9d32012-09-26 02:39:39 +00005292 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005293 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305294#ifdef CONFIG_DEBUG_FS
5295 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5296#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005297 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005298 if (err)
5299 goto out_unmap_bar;
5300
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305301 /* configure SGE_STAT_CFG_A to read WC stats */
5302 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305303 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5304 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5305 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305306
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005307 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005308 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5309 MAX_ETH_QSETS);
5310 if (!netdev) {
5311 err = -ENOMEM;
5312 goto out_free_dev;
5313 }
5314
5315 SET_NETDEV_DEV(netdev, &pdev->dev);
5316
5317 adapter->port[i] = netdev;
5318 pi = netdev_priv(netdev);
5319 pi->adapter = adapter;
5320 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005321 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005322 netdev->irq = pdev->irq;
5323
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005324 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5325 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5326 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305327 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5328 NETIF_F_HW_TC;
Ganesh Goudard0a12992018-01-10 18:15:26 +05305329
5330 if (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5)
5331 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
5332
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005333 if (highdma)
5334 netdev->hw_features |= NETIF_F_HIGHDMA;
5335 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005336 netdev->vlan_features = netdev->features & VLAN_FEAT;
5337
Jiri Pirko01789342011-08-16 06:29:00 +00005338 netdev->priv_flags |= IFF_UNICAST_FLT;
5339
Jarod Wilsond894be52016-10-20 13:55:16 -04005340 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305341 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005342 netdev->max_mtu = MAX_MTU;
5343
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005344 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005345#ifdef CONFIG_CHELSIO_T4_DCB
5346 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5347 cxgb4_dcb_state_init(netdev);
5348#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305349 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005350 }
5351
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305352 cxgb4_init_ethtool_dump(adapter);
5353
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005354 pci_set_drvdata(pdev, adapter);
5355
5356 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005357 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005358 if (err)
5359 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305360 } else if (adapter->params.nports == 1) {
5361 /* If we don't have a connection to the firmware -- possibly
5362 * because of an error -- grab the raw VPD parameters so we
5363 * can set the proper MAC Address on the debug network
5364 * interface that we've created.
5365 */
5366 u8 hw_addr[ETH_ALEN];
5367 u8 *na = adapter->params.vpd.na;
5368
5369 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5370 if (!err) {
5371 for (i = 0; i < ETH_ALEN; i++)
5372 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5373 hex2val(na[2 * i + 1]));
5374 t4_set_hw_addr(adapter, 0, hw_addr);
5375 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005376 }
5377
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305378 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005379 * soon as the first register_netdev completes.
5380 */
5381 cfg_queues(adapter);
5382
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305383 adapter->smt = t4_init_smt();
5384 if (!adapter->smt) {
5385 /* We tolerate a lack of SMT, giving up some functionality */
5386 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5387 }
5388
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305389 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005390 if (!adapter->l2t) {
5391 /* We tolerate a lack of L2T, giving up some functionality */
5392 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5393 adapter->params.offload = 0;
5394 }
5395
Anish Bhattb5a02f52015-01-14 15:17:34 -08005396#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305397 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5398 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5399 /* CLIP functionality is not present in hardware,
5400 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005401 */
5402 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305403 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005404 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305405 } else {
5406 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5407 adapter->clipt_end);
5408 if (!adapter->clipt) {
5409 /* We tolerate a lack of clip_table, giving up
5410 * some functionality
5411 */
5412 dev_warn(&pdev->dev,
5413 "could not allocate Clip table, continuing\n");
5414 adapter->params.offload = 0;
5415 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005416 }
5417#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305418
5419 for_each_port(adapter, i) {
5420 pi = adap2pinfo(adapter, i);
5421 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5422 if (!pi->sched_tbl)
5423 dev_warn(&pdev->dev,
5424 "could not activate scheduling on port %d\n",
5425 i);
5426 }
5427
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305428 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005429 dev_warn(&pdev->dev, "could not allocate TID table, "
5430 "continuing\n");
5431 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305432 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305433 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305434 if (!adapter->tc_u32)
5435 dev_warn(&pdev->dev,
5436 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305437
Kumar Sanghvi79e6d462017-11-01 08:53:04 +05305438 if (cxgb4_init_tc_flower(adapter))
5439 dev_warn(&pdev->dev,
5440 "could not offload tc flower, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005441 }
5442
Kumar Sanghvi5c312542017-11-01 08:53:00 +05305443 if (is_offload(adapter) || is_hashfilter(adapter)) {
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305444 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5445 u32 hash_base, hash_reg;
5446
5447 if (chip <= CHELSIO_T5) {
5448 hash_reg = LE_DB_TID_HASHBASE_A;
5449 hash_base = t4_read_reg(adapter, hash_reg);
5450 adapter->tids.hash_base = hash_base / 4;
5451 } else {
5452 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5453 hash_base = t4_read_reg(adapter, hash_reg);
5454 adapter->tids.hash_base = hash_base;
5455 }
5456 }
5457 }
5458
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005459 /* See what interrupts we'll be using */
5460 if (msi > 1 && enable_msix(adapter) == 0)
5461 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305462 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005463 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305464 if (msi > 1)
5465 free_msix_info(adapter);
5466 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005467
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305468 /* check for PCI Express bandwidth capabiltites */
5469 cxgb4_check_pcie_caps(adapter);
5470
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005471 err = init_rss(adapter);
5472 if (err)
5473 goto out_free_dev;
5474
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005475 /*
5476 * The card is now ready to go. If any errors occur during device
5477 * registration we do not fail the whole card but rather proceed only
5478 * with the ports we manage to register successfully. However we must
5479 * register at least one net device.
5480 */
5481 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005482 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305483 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005484 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5485 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5486
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305487 netif_carrier_off(adapter->port[i]);
5488
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005489 err = register_netdev(adapter->port[i]);
5490 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005491 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005492 adapter->chan_map[pi->tx_chan] = i;
5493 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005494 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005495 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005496 dev_err(&pdev->dev, "could not register any net devices\n");
5497 goto out_free_dev;
5498 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005499 if (err) {
5500 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5501 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005502 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005503
5504 if (cxgb4_debugfs_root) {
5505 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5506 cxgb4_debugfs_root);
5507 setup_debugfs(adapter);
5508 }
5509
David S. Miller88c51002011-10-07 13:38:43 -04005510 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5511 pdev->needs_freset = 1;
5512
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305513 if (is_uld(adapter)) {
5514 mutex_lock(&uld_mutex);
5515 list_add_tail(&adapter->list_node, &adapter_list);
5516 mutex_unlock(&uld_mutex);
5517 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005518
Atul Gupta9c33e422017-07-04 16:46:21 +05305519 if (!is_t4(adapter->params.chip))
5520 cxgb4_ptp_init(adapter);
5521
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305522 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305523 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305524 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305525
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005526 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005527 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305528 if (adapter->flags & USING_MSIX)
5529 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305530 if (adapter->num_uld || adapter->num_ofld_uld)
5531 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005532 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305533 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005534 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005535 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005536 if (adapter->workq)
5537 destroy_workqueue(adapter->workq);
5538
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305539 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005540 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305541 out_unmap_bar0:
5542 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005543 out_disable_device:
5544 pci_disable_pcie_error_reporting(pdev);
5545 pci_disable_device(pdev);
5546 out_release_regions:
5547 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005548 return err;
5549}
5550
Bill Pemberton91744942012-12-03 09:23:02 -05005551static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005552{
5553 struct adapter *adapter = pci_get_drvdata(pdev);
5554
Hariprasad Shenai78294512016-08-11 21:06:23 +05305555 if (!adapter) {
5556 pci_release_regions(pdev);
5557 return;
5558 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005559
Ganesh Goudare1f61982017-09-21 12:50:47 +05305560 adapter->flags |= SHUTTING_DOWN;
5561
Hariprasad Shenai78294512016-08-11 21:06:23 +05305562 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005563 int i;
5564
Anish Bhatt29aaee62014-08-20 13:44:06 -07005565 /* Tear down per-adapter Work Queue first since it can contain
5566 * references to our adapter data structure.
5567 */
5568 destroy_workqueue(adapter->workq);
5569
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005570 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005571 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005572 t4_uld_clean_up(adapter);
5573 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005574
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305575 disable_interrupts(adapter);
5576
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005577 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005578 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005579 unregister_netdev(adapter->port[i]);
5580
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005581 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005582
Atul Gupta9c33e422017-07-04 16:46:21 +05305583 if (!is_t4(adapter->params.chip))
5584 cxgb4_ptp_stop(adapter);
5585
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005586 /* If we allocated filters, free up state associated with any
5587 * valid filters ...
5588 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305589 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005590
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005591 if (adapter->flags & FULL_INIT_DONE)
5592 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005593
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305594 if (adapter->flags & USING_MSIX)
5595 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305596 if (adapter->num_uld || adapter->num_ofld_uld)
5597 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005598 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005599#if IS_ENABLED(CONFIG_IPV6)
5600 t4_cleanup_clip_tbl(adapter);
5601#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005602 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305603 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005604 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005605 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005606 if ((adapter->flags & DEV_ENABLED)) {
5607 pci_disable_device(pdev);
5608 adapter->flags &= ~DEV_ENABLED;
5609 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005610 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305611 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005612 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005613 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305614 }
5615#ifdef CONFIG_PCI_IOV
5616 else {
Ganesh Goudarbaf50862018-01-16 16:17:40 +05305617 cxgb4_iov_configure(adapter->pdev, 0);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305618 }
5619#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005620}
5621
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305622/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5623 * delivery. This is essentially a stripped down version of the PCI remove()
5624 * function where we do the minimal amount of work necessary to shutdown any
5625 * further activity.
5626 */
5627static void shutdown_one(struct pci_dev *pdev)
5628{
5629 struct adapter *adapter = pci_get_drvdata(pdev);
5630
5631 /* As with remove_one() above (see extended comment), we only want do
5632 * do cleanup on PCI Devices which went all the way through init_one()
5633 * ...
5634 */
5635 if (!adapter) {
5636 pci_release_regions(pdev);
5637 return;
5638 }
5639
Ganesh Goudare1f61982017-09-21 12:50:47 +05305640 adapter->flags |= SHUTTING_DOWN;
5641
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305642 if (adapter->pf == 4) {
5643 int i;
5644
5645 for_each_port(adapter, i)
5646 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5647 cxgb_close(adapter->port[i]);
5648
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005649 if (is_uld(adapter)) {
5650 detach_ulds(adapter);
5651 t4_uld_clean_up(adapter);
5652 }
5653
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305654 disable_interrupts(adapter);
5655 disable_msi(adapter);
5656
5657 t4_sge_stop(adapter);
5658 if (adapter->flags & FW_OK)
5659 t4_fw_bye(adapter, adapter->mbox);
5660 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305661}
5662
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005663static struct pci_driver cxgb4_driver = {
5664 .name = KBUILD_MODNAME,
5665 .id_table = cxgb4_pci_tbl,
5666 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005667 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305668 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305669#ifdef CONFIG_PCI_IOV
5670 .sriov_configure = cxgb4_iov_configure,
5671#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005672 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005673};
5674
5675static int __init cxgb4_init_module(void)
5676{
5677 int ret;
5678
5679 /* Debugfs support is optional, just warn if this fails */
5680 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5681 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005682 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005683
5684 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005685 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005686 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305687
Anish Bhatt1bb60372014-10-14 20:07:22 -07005688#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005689 if (!inet6addr_registered) {
5690 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5691 inet6addr_registered = true;
5692 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005693#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305694
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005695 return ret;
5696}
5697
5698static void __exit cxgb4_cleanup_module(void)
5699{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005700#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305701 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005702 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5703 inet6addr_registered = false;
5704 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005705#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005706 pci_unregister_driver(&cxgb4_driver);
5707 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005708}
5709
5710module_init(cxgb4_init_module);
5711module_exit(cxgb4_cleanup_module);