blob: 3293980ce6e01e6b714d0c24d2d5714b3282448a [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[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530105#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000106
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530107/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
108 * called for both.
109 */
110#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
111
112#define CH_PCI_ID_TABLE_ENTRY(devid) \
113 {PCI_VDEVICE(CHELSIO, (devid)), 4}
114
115#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
116 { 0, } \
117 }
118
119#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000120
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530121#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000122#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530123#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530124#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000125#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530126#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530127#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
128#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
129#define PHY_AQ1202_DEVICEID 0x4409
130#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000131
132MODULE_DESCRIPTION(DRV_DESC);
133MODULE_AUTHOR("Chelsio Communications");
134MODULE_LICENSE("Dual BSD/GPL");
135MODULE_VERSION(DRV_VERSION);
136MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530137MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000138MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530139MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000140
Vipul Pandya636f9d32012-09-26 02:39:39 +0000141/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000142 * The driver uses the best interrupt scheme available on a platform in the
143 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
144 * of these schemes the driver may consider as follows:
145 *
146 * msi = 2: choose from among all three options
147 * msi = 1: only consider MSI and INTx interrupts
148 * msi = 0: force INTx interrupts
149 */
150static int msi = 2;
151
152module_param(msi, int, 0644);
153MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
154
155/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000156 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
157 * offset by 2 bytes in order to have the IP headers line up on 4-byte
158 * boundaries. This is a requirement for many architectures which will throw
159 * a machine check fault if an attempt is made to access one of the 4-byte IP
160 * header fields on a non-4-byte boundary. And it's a major performance issue
161 * even on some architectures which allow it like some implementations of the
162 * x86 ISA. However, some architectures don't mind this and for some very
163 * edge-case performance sensitive applications (like forwarding large volumes
164 * of small packets), setting this DMA offset to 0 will decrease the number of
165 * PCI-E Bus transfers enough to measurably affect performance.
166 */
167static int rx_dma_offset = 2;
168
Anish Bhatt688848b2014-06-19 21:37:13 -0700169/* TX Queue select used to determine what algorithm to use for selecting TX
170 * queue. Select between the kernel provided function (select_queue=0) or user
171 * cxgb_select_queue function (select_queue=1)
172 *
173 * Default: select_queue=0
174 */
175static int select_queue;
176module_param(select_queue, int, 0644);
177MODULE_PARM_DESC(select_queue,
178 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
179
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000180static struct dentry *cxgb4_debugfs_root;
181
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530182LIST_HEAD(adapter_list);
183DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000184
185static void link_report(struct net_device *dev)
186{
187 if (!netif_carrier_ok(dev))
188 netdev_info(dev, "link down\n");
189 else {
190 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
191
Hariprasad Shenai85412252015-10-01 13:48:48 +0530192 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000193 const struct port_info *p = netdev_priv(dev);
194
195 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000196 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000197 s = "100Mbps";
198 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530199 case 1000:
200 s = "1Gbps";
201 break;
202 case 10000:
203 s = "10Gbps";
204 break;
205 case 25000:
206 s = "25Gbps";
207 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000208 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530209 s = "40Gbps";
210 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530211 case 100000:
212 s = "100Gbps";
213 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530214 default:
215 pr_info("%s: unsupported speed: %d\n",
216 dev->name, p->link_cfg.speed);
217 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000218 }
219
220 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
221 fc[p->link_cfg.fc]);
222 }
223}
224
Anish Bhatt688848b2014-06-19 21:37:13 -0700225#ifdef CONFIG_CHELSIO_T4_DCB
226/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
227static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
228{
229 struct port_info *pi = netdev_priv(dev);
230 struct adapter *adap = pi->adapter;
231 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
232 int i;
233
234 /* We use a simple mapping of Port TX Queue Index to DCB
235 * Priority when we're enabling DCB.
236 */
237 for (i = 0; i < pi->nqsets; i++, txq++) {
238 u32 name, value;
239 int err;
240
Hariprasad Shenai51678652014-11-21 12:52:02 +0530241 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
242 FW_PARAMS_PARAM_X_V(
243 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
244 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700245 value = enable ? i : 0xffffffff;
246
247 /* Since we can be called while atomic (from "interrupt
248 * level") we need to issue the Set Parameters Commannd
249 * without sleeping (timeout < 0).
250 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530251 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530252 &name, &value,
253 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700254
255 if (err)
256 dev_err(adap->pdev_dev,
257 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
258 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700259 else
260 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700261 }
262}
Anish Bhatt688848b2014-06-19 21:37:13 -0700263
Baoyou Xie50935852016-09-25 14:10:09 +0800264static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530265{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530266 struct port_info *pi = netdev_priv(dev);
267
268 if (!pi->dcb.enabled)
269 return 0;
270
271 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
272 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530273}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200274#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530275
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000276void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
277{
278 struct net_device *dev = adapter->port[port_id];
279
280 /* Skip changes from disabled ports. */
281 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
282 if (link_stat)
283 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700284 else {
285#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530286 if (cxgb4_dcb_enabled(dev)) {
Ganesh Goudarba581f72017-09-23 16:07:28 +0530287 cxgb4_dcb_reset(dev);
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530288 dcb_tx_queue_prio_enable(dev, false);
289 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700290#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000291 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700292 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000293
294 link_report(dev);
295 }
296}
297
298void t4_os_portmod_changed(const struct adapter *adap, int port_id)
299{
300 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000301 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000302 };
303
304 const struct net_device *dev = adap->port[port_id];
305 const struct port_info *pi = netdev_priv(dev);
306
307 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
308 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000309 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000310 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530311 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
312 netdev_info(dev, "%s: unsupported port module inserted\n",
313 dev->name);
314 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
315 netdev_info(dev, "%s: unknown port module inserted\n",
316 dev->name);
317 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
318 netdev_info(dev, "%s: transceiver module error\n", dev->name);
319 else
320 netdev_info(dev, "%s: unknown module type %d inserted\n",
321 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000322}
323
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530324int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
325module_param(dbfifo_int_thresh, int, 0644);
326MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
327
Vipul Pandya404d9e32012-10-08 02:59:43 +0000328/*
329 * usecs to sleep while draining the dbfifo
330 */
331static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530332module_param(dbfifo_drain_delay, int, 0644);
333MODULE_PARM_DESC(dbfifo_drain_delay,
334 "usecs to sleep while draining the dbfifo");
335
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530336static inline int cxgb4_set_addr_hash(struct port_info *pi)
337{
338 struct adapter *adap = pi->adapter;
339 u64 vec = 0;
340 bool ucast = false;
341 struct hash_mac_addr *entry;
342
343 /* Calculate the hash vector for the updated list and program it */
344 list_for_each_entry(entry, &adap->mac_hlist, list) {
345 ucast |= is_unicast_ether_addr(entry->addr);
346 vec |= (1ULL << hash_mac_addr(entry->addr));
347 }
348 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
349 vec, false);
350}
351
352static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
353{
354 struct port_info *pi = netdev_priv(netdev);
355 struct adapter *adap = pi->adapter;
356 int ret;
357 u64 mhash = 0;
358 u64 uhash = 0;
359 bool free = false;
360 bool ucast = is_unicast_ether_addr(mac_addr);
361 const u8 *maclist[1] = {mac_addr};
362 struct hash_mac_addr *new_entry;
363
364 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
365 NULL, ucast ? &uhash : &mhash, false);
366 if (ret < 0)
367 goto out;
368 /* if hash != 0, then add the addr to hash addr list
369 * so on the end we will calculate the hash for the
370 * list and program it
371 */
372 if (uhash || mhash) {
373 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
374 if (!new_entry)
375 return -ENOMEM;
376 ether_addr_copy(new_entry->addr, mac_addr);
377 list_add_tail(&new_entry->list, &adap->mac_hlist);
378 ret = cxgb4_set_addr_hash(pi);
379 }
380out:
381 return ret < 0 ? ret : 0;
382}
383
384static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
385{
386 struct port_info *pi = netdev_priv(netdev);
387 struct adapter *adap = pi->adapter;
388 int ret;
389 const u8 *maclist[1] = {mac_addr};
390 struct hash_mac_addr *entry, *tmp;
391
392 /* If the MAC address to be removed is in the hash addr
393 * list, delete it from the list and update hash vector
394 */
395 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
396 if (ether_addr_equal(entry->addr, mac_addr)) {
397 list_del(&entry->list);
398 kfree(entry);
399 return cxgb4_set_addr_hash(pi);
400 }
401 }
402
403 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
404 return ret < 0 ? -EINVAL : 0;
405}
406
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000407/*
408 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
409 * If @mtu is -1 it is left unchanged.
410 */
411static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
412{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000413 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530414 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000415
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530416 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
417 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530418
419 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
420 (dev->flags & IFF_PROMISC) ? 1 : 0,
421 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
422 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000423}
424
425/**
426 * link_start - enable a port
427 * @dev: the port to enable
428 *
429 * Performs the MAC and PHY actions needed to enable a port.
430 */
431static int link_start(struct net_device *dev)
432{
433 int ret;
434 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530435 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000436
437 /*
438 * We do not set address filters and promiscuity here, the stack does
439 * that step explicitly.
440 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000441 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000442 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000443 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000444 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000445 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000446 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000447 if (ret >= 0) {
448 pi->xact_addr_filt = ret;
449 ret = 0;
450 }
451 }
452 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530453 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000454 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700455 if (ret == 0) {
456 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700457 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
458 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700459 local_bh_enable();
460 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700461
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000462 return ret;
463}
464
Anish Bhatt688848b2014-06-19 21:37:13 -0700465#ifdef CONFIG_CHELSIO_T4_DCB
466/* Handle a Data Center Bridging update message from the firmware. */
467static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
468{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530469 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530470 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700471 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
472 int new_dcb_enabled;
473
474 cxgb4_dcb_handle_fw_update(adap, pcmd);
475 new_dcb_enabled = cxgb4_dcb_enabled(dev);
476
477 /* If the DCB has become enabled or disabled on the port then we're
478 * going to need to set up/tear down DCB Priority parameters for the
479 * TX Queues associated with the port.
480 */
481 if (new_dcb_enabled != old_dcb_enabled)
482 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
483}
484#endif /* CONFIG_CHELSIO_T4_DCB */
485
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000486/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000487 */
488static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
489 const struct pkt_gl *gl)
490{
491 u8 opcode = ((const struct rss_header *)rsp)->opcode;
492
493 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000494
495 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
496 */
497 if (unlikely(opcode == CPL_FW4_MSG &&
498 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
499 rsp++;
500 opcode = ((const struct rss_header *)rsp)->opcode;
501 rsp++;
502 if (opcode != CPL_SGE_EGR_UPDATE) {
503 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
504 , opcode);
505 goto out;
506 }
507 }
508
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000509 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
510 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800511 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000512 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000513
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000514 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000515 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530516 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000517 struct sge_eth_txq *eq;
518
519 eq = container_of(txq, struct sge_eth_txq, q);
520 netif_tx_wake_queue(eq->txq);
521 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530522 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000523
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530524 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000525 tasklet_schedule(&oq->qresume_tsk);
526 }
527 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
528 const struct cpl_fw6_msg *p = (void *)rsp;
529
Anish Bhatt688848b2014-06-19 21:37:13 -0700530#ifdef CONFIG_CHELSIO_T4_DCB
531 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530532 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700533 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530534 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700535
536 if (cmd == FW_PORT_CMD &&
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530537 (action == FW_PORT_ACTION_GET_PORT_INFO ||
538 action == FW_PORT_ACTION_GET_PORT_INFO32)) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530539 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700540 be32_to_cpu(pcmd->op_to_portid));
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530541 struct net_device *dev;
542 int dcbxdis, state_input;
543
544 dev = q->adap->port[q->adap->chan_map[port]];
545 dcbxdis = (action == FW_PORT_ACTION_GET_PORT_INFO
546 ? !!(pcmd->u.info.dcbxdis_pkd &
547 FW_PORT_CMD_DCBXDIS_F)
548 : !!(pcmd->u.info32.lstatus32_to_cbllen32 &
549 FW_PORT_CMD_DCBXDIS32_F));
550 state_input = (dcbxdis
551 ? CXGB4_DCB_INPUT_FW_DISABLED
552 : CXGB4_DCB_INPUT_FW_ENABLED);
Anish Bhatt688848b2014-06-19 21:37:13 -0700553
554 cxgb4_dcb_state_fsm(dev, state_input);
555 }
556
557 if (cmd == FW_PORT_CMD &&
558 action == FW_PORT_ACTION_L2_DCB_CFG)
559 dcb_rpl(q->adap, pcmd);
560 else
561#endif
562 if (p->type == 0)
563 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000564 } else if (opcode == CPL_L2T_WRITE_RPL) {
565 const struct cpl_l2t_write_rpl *p = (void *)rsp;
566
567 do_l2t_write_rpl(q->adap, p);
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +0530568 } else if (opcode == CPL_SMT_WRITE_RPL) {
569 const struct cpl_smt_write_rpl *p = (void *)rsp;
570
571 do_smt_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000572 } else if (opcode == CPL_SET_TCB_RPL) {
573 const struct cpl_set_tcb_rpl *p = (void *)rsp;
574
575 filter_rpl(q->adap, p);
Kumar Sanghvi12b276f2017-11-01 08:53:01 +0530576 } else if (opcode == CPL_ACT_OPEN_RPL) {
577 const struct cpl_act_open_rpl *p = (void *)rsp;
578
579 hash_filter_rpl(q->adap, p);
Kumar Sanghvi3b0b3be2017-11-01 08:53:02 +0530580 } else if (opcode == CPL_ABORT_RPL_RSS) {
581 const struct cpl_abort_rpl_rss *p = (void *)rsp;
582
583 hash_del_filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000584 } else
585 dev_err(q->adap->pdev_dev,
586 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000587out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000588 return 0;
589}
590
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000591static void disable_msi(struct adapter *adapter)
592{
593 if (adapter->flags & USING_MSIX) {
594 pci_disable_msix(adapter->pdev);
595 adapter->flags &= ~USING_MSIX;
596 } else if (adapter->flags & USING_MSI) {
597 pci_disable_msi(adapter->pdev);
598 adapter->flags &= ~USING_MSI;
599 }
600}
601
602/*
603 * Interrupt handler for non-data events used with MSI-X.
604 */
605static irqreturn_t t4_nondata_intr(int irq, void *cookie)
606{
607 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530608 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000609
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530610 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000611 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530612 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000613 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530614 if (adap->flags & MASTER_PF)
615 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000616 return IRQ_HANDLED;
617}
618
619/*
620 * Name the MSI-X interrupts.
621 */
622static void name_msix_vecs(struct adapter *adap)
623{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000624 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000625
626 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000627 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000628
629 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000630 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
631 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000632
633 /* Ethernet queues */
634 for_each_port(adap, j) {
635 struct net_device *d = adap->port[j];
636 const struct port_info *pi = netdev_priv(d);
637
Dimitris Michailidisba278162010-12-14 21:36:50 +0000638 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000639 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
640 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000641 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000642}
643
644static int request_msix_queue_irqs(struct adapter *adap)
645{
646 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530647 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530648 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000649
650 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
651 adap->msix_info[1].desc, &s->fw_evtq);
652 if (err)
653 return err;
654
655 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000656 err = request_irq(adap->msix_info[msi_index].vec,
657 t4_sge_intr_msix, 0,
658 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000659 &s->ethrxq[ethqidx].rspq);
660 if (err)
661 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000662 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000663 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000664 return 0;
665
666unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000667 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000668 free_irq(adap->msix_info[--msi_index].vec,
669 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000670 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
671 return err;
672}
673
674static void free_msix_queue_irqs(struct adapter *adap)
675{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000676 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000677 struct sge *s = &adap->sge;
678
679 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
680 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000681 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000682}
683
684/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530685 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000686 * @pi: the port
687 * @queues: array of queue indices for RSS
688 *
689 * Sets up the portion of the HW RSS table for the port's VI to distribute
690 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530691 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000692 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530693int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000694{
695 u16 *rss;
696 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530697 struct adapter *adapter = pi->adapter;
698 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000699
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530700 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000701 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
702 if (!rss)
703 return -ENOMEM;
704
705 /* map the queue indices to queue ids */
706 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530707 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000708
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530709 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000710 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530711 /* If Tunnel All Lookup isn't specified in the global RSS
712 * Configuration, then we need to specify a default Ingress
713 * Queue for any ingress packets which aren't hashed. We'll
714 * use our first ingress queue ...
715 */
716 if (!err)
717 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
718 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
719 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
720 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
721 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
722 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
723 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000724 kfree(rss);
725 return err;
726}
727
728/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000729 * setup_rss - configure RSS
730 * @adap: the adapter
731 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000732 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000733 */
734static int setup_rss(struct adapter *adap)
735{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530736 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000737
738 for_each_port(adap, i) {
739 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000740
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530741 /* Fill default values with equal distribution */
742 for (j = 0; j < pi->rss_size; j++)
743 pi->rss[j] = j % pi->nqsets;
744
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530745 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000746 if (err)
747 return err;
748 }
749 return 0;
750}
751
752/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000753 * Return the channel of the ingress queue with the given qid.
754 */
755static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
756{
757 qid -= p->ingr_start;
758 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
759}
760
761/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000762 * Wait until all NAPI handlers are descheduled.
763 */
764static void quiesce_rx(struct adapter *adap)
765{
766 int i;
767
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530768 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000769 struct sge_rspq *q = adap->sge.ingr_map[i];
770
Eric Dumazet5226b7912017-02-02 11:44:27 -0800771 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000772 napi_disable(&q->napi);
773 }
774}
775
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530776/* Disable interrupt and napi handler */
777static void disable_interrupts(struct adapter *adap)
778{
779 if (adap->flags & FULL_INIT_DONE) {
780 t4_intr_disable(adap);
781 if (adap->flags & USING_MSIX) {
782 free_msix_queue_irqs(adap);
783 free_irq(adap->msix_info[0].vec, adap);
784 } else {
785 free_irq(adap->pdev->irq, adap);
786 }
787 quiesce_rx(adap);
788 }
789}
790
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000791/*
792 * Enable NAPI scheduling and interrupt generation for all Rx queues.
793 */
794static void enable_rx(struct adapter *adap)
795{
796 int i;
797
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530798 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000799 struct sge_rspq *q = adap->sge.ingr_map[i];
800
801 if (!q)
802 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800803 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000804 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800805
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000806 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530807 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
808 SEINTARM_V(q->intr_params) |
809 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000810 }
811}
812
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530813
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530814static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000816 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530817 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000818
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530819 bitmap_zero(s->starving_fl, s->egr_sz);
820 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000821
822 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530823 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000824 else {
825 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530826 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000827 if (err)
828 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530829 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000830 }
831
832 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530833 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530834 if (err)
835 t4_free_sge_resources(adap);
836 return err;
837}
838
839/**
840 * setup_sge_queues - configure SGE Tx/Rx/response queues
841 * @adap: the adapter
842 *
843 * Determines how many sets of SGE queues to use and initializes them.
844 * We support multiple queue sets per port if we have MSI-X, otherwise
845 * just one queue set per port.
846 */
847static int setup_sge_queues(struct adapter *adap)
848{
849 int err, i, j;
850 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530851 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530852 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000853
Ganesh Goudard427cae2017-06-16 15:36:09 +0530854 if (is_uld(adap))
855 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
856
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000857 for_each_port(adap, i) {
858 struct net_device *dev = adap->port[i];
859 struct port_info *pi = netdev_priv(dev);
860 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
861 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
862
863 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530864 if (adap->msi_idx > 0)
865 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000866 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530867 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530868 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530869 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530870 t4_get_tp_ch_map(adap,
871 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000872 if (err)
873 goto freeout;
874 q->rspq.idx = j;
875 memset(&q->stats, 0, sizeof(q->stats));
876 }
877 for (j = 0; j < pi->nqsets; j++, t++) {
878 err = t4_sge_alloc_eth_txq(adap, t, dev,
879 netdev_get_tx_queue(dev, j),
880 s->fw_evtq.cntxt_id);
881 if (err)
882 goto freeout;
883 }
884 }
885
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000886 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530887 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000888 * have RDMA queues, and that's the right value.
889 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530890 if (rxq_info)
891 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
892
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000893 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530894 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000895 if (err)
896 goto freeout;
897 }
898
Atul Guptaa45695042017-07-04 16:46:20 +0530899 if (!is_t4(adap->params.chip)) {
900 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
901 netdev_get_tx_queue(adap->port[0], 0)
902 , s->fw_evtq.cntxt_id);
903 if (err)
904 goto freeout;
905 }
906
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530907 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530908 MPS_TRC_RSS_CONTROL_A :
909 MPS_T5_TRC_RSS_CONTROL_A,
910 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
911 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000912 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530913freeout:
914 t4_free_sge_resources(adap);
915 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000916}
917
Anish Bhatt688848b2014-06-19 21:37:13 -0700918static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
919 void *accel_priv, select_queue_fallback_t fallback)
920{
921 int txq;
922
923#ifdef CONFIG_CHELSIO_T4_DCB
924 /* If a Data Center Bridging has been successfully negotiated on this
925 * link then we'll use the skb's priority to map it to a TX Queue.
926 * The skb's priority is determined via the VLAN Tag Priority Code
927 * Point field.
928 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530929 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700930 u16 vlan_tci;
931 int err;
932
933 err = vlan_get_tag(skb, &vlan_tci);
934 if (unlikely(err)) {
935 if (net_ratelimit())
936 netdev_warn(dev,
937 "TX Packet without VLAN Tag on DCB Link\n");
938 txq = 0;
939 } else {
940 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530941#ifdef CONFIG_CHELSIO_T4_FCOE
942 if (skb->protocol == htons(ETH_P_FCOE))
943 txq = skb->priority & 0x7;
944#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700945 }
946 return txq;
947 }
948#endif /* CONFIG_CHELSIO_T4_DCB */
949
950 if (select_queue) {
951 txq = (skb_rx_queue_recorded(skb)
952 ? skb_get_rx_queue(skb)
953 : smp_processor_id());
954
955 while (unlikely(txq >= dev->real_num_tx_queues))
956 txq -= dev->real_num_tx_queues;
957
958 return txq;
959 }
960
961 return fallback(dev, skb) % dev->real_num_tx_queues;
962}
963
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000964static int closest_timer(const struct sge *s, int time)
965{
966 int i, delta, match = 0, min_delta = INT_MAX;
967
968 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
969 delta = time - s->timer_val[i];
970 if (delta < 0)
971 delta = -delta;
972 if (delta < min_delta) {
973 min_delta = delta;
974 match = i;
975 }
976 }
977 return match;
978}
979
980static int closest_thres(const struct sge *s, int thres)
981{
982 int i, delta, match = 0, min_delta = INT_MAX;
983
984 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
985 delta = thres - s->counter_val[i];
986 if (delta < 0)
987 delta = -delta;
988 if (delta < min_delta) {
989 min_delta = delta;
990 match = i;
991 }
992 }
993 return match;
994}
995
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000996/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530997 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000998 * @q: the Rx queue
999 * @us: the hold-off time in us, or 0 to disable timer
1000 * @cnt: the hold-off packet count, or 0 to disable counter
1001 *
1002 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1003 * one of the two needs to be enabled for the queue to generate interrupts.
1004 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +05301005int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1006 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001007{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05301008 struct adapter *adap = q->adap;
1009
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001010 if ((us | cnt) == 0)
1011 cnt = 1;
1012
1013 if (cnt) {
1014 int err;
1015 u32 v, new_idx;
1016
1017 new_idx = closest_thres(&adap->sge, cnt);
1018 if (q->desc && q->pktcnt_idx != new_idx) {
1019 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301020 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1021 FW_PARAMS_PARAM_X_V(
1022 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1023 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301024 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1025 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001026 if (err)
1027 return err;
1028 }
1029 q->pktcnt_idx = new_idx;
1030 }
1031
1032 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301033 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001034 return 0;
1035}
1036
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001037static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001038{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001039 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001040 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001041 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001042
Patrick McHardyf6469682013-04-19 02:04:27 +00001043 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001044 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001045
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301046 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001047 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001048 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001049 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001050 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001051 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001052}
1053
Bill Pemberton91744942012-12-03 09:23:02 -05001054static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001055{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001056 if (IS_ERR_OR_NULL(adap->debugfs_root))
1057 return -1;
1058
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301059#ifdef CONFIG_DEBUG_FS
1060 t4_setup_debugfs(adap);
1061#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001062 return 0;
1063}
1064
1065/*
1066 * upper-layer driver support
1067 */
1068
1069/*
1070 * Allocate an active-open TID and set it to the supplied value.
1071 */
1072int cxgb4_alloc_atid(struct tid_info *t, void *data)
1073{
1074 int atid = -1;
1075
1076 spin_lock_bh(&t->atid_lock);
1077 if (t->afree) {
1078 union aopen_entry *p = t->afree;
1079
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001080 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001081 t->afree = p->next;
1082 p->data = data;
1083 t->atids_in_use++;
1084 }
1085 spin_unlock_bh(&t->atid_lock);
1086 return atid;
1087}
1088EXPORT_SYMBOL(cxgb4_alloc_atid);
1089
1090/*
1091 * Release an active-open TID.
1092 */
1093void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1094{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001095 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001096
1097 spin_lock_bh(&t->atid_lock);
1098 p->next = t->afree;
1099 t->afree = p;
1100 t->atids_in_use--;
1101 spin_unlock_bh(&t->atid_lock);
1102}
1103EXPORT_SYMBOL(cxgb4_free_atid);
1104
1105/*
1106 * Allocate a server TID and set it to the supplied value.
1107 */
1108int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1109{
1110 int stid;
1111
1112 spin_lock_bh(&t->stid_lock);
1113 if (family == PF_INET) {
1114 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1115 if (stid < t->nstids)
1116 __set_bit(stid, t->stid_bmap);
1117 else
1118 stid = -1;
1119 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301120 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001121 if (stid < 0)
1122 stid = -1;
1123 }
1124 if (stid >= 0) {
1125 t->stid_tab[stid].data = data;
1126 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301127 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1128 * This is equivalent to 4 TIDs. With CLIP enabled it
1129 * needs 2 TIDs.
1130 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301131 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301132 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301133 t->v6_stids_in_use += 2;
1134 } else {
1135 t->stids_in_use++;
1136 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001137 }
1138 spin_unlock_bh(&t->stid_lock);
1139 return stid;
1140}
1141EXPORT_SYMBOL(cxgb4_alloc_stid);
1142
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001143/* Allocate a server filter TID and set it to the supplied value.
1144 */
1145int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1146{
1147 int stid;
1148
1149 spin_lock_bh(&t->stid_lock);
1150 if (family == PF_INET) {
1151 stid = find_next_zero_bit(t->stid_bmap,
1152 t->nstids + t->nsftids, t->nstids);
1153 if (stid < (t->nstids + t->nsftids))
1154 __set_bit(stid, t->stid_bmap);
1155 else
1156 stid = -1;
1157 } else {
1158 stid = -1;
1159 }
1160 if (stid >= 0) {
1161 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301162 stid -= t->nstids;
1163 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301164 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001165 }
1166 spin_unlock_bh(&t->stid_lock);
1167 return stid;
1168}
1169EXPORT_SYMBOL(cxgb4_alloc_sftid);
1170
1171/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001172 */
1173void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1174{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301175 /* Is it a server filter TID? */
1176 if (t->nsftids && (stid >= t->sftid_base)) {
1177 stid -= t->sftid_base;
1178 stid += t->nstids;
1179 } else {
1180 stid -= t->stid_base;
1181 }
1182
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001183 spin_lock_bh(&t->stid_lock);
1184 if (family == PF_INET)
1185 __clear_bit(stid, t->stid_bmap);
1186 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301187 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001188 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301189 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301190 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301191 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301192 t->v6_stids_in_use -= 2;
1193 } else {
1194 t->stids_in_use--;
1195 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301196 } else {
1197 t->sftids_in_use--;
1198 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301199
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001200 spin_unlock_bh(&t->stid_lock);
1201}
1202EXPORT_SYMBOL(cxgb4_free_stid);
1203
1204/*
1205 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1206 */
1207static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1208 unsigned int tid)
1209{
1210 struct cpl_tid_release *req;
1211
1212 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001213 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001214 INIT_TP_WR(req, tid);
1215 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1216}
1217
1218/*
1219 * Queue a TID release request and if necessary schedule a work queue to
1220 * process it.
1221 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001222static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1223 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001224{
1225 void **p = &t->tid_tab[tid];
1226 struct adapter *adap = container_of(t, struct adapter, tids);
1227
1228 spin_lock_bh(&adap->tid_release_lock);
1229 *p = adap->tid_release_head;
1230 /* Low 2 bits encode the Tx channel number */
1231 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1232 if (!adap->tid_release_task_busy) {
1233 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001234 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001235 }
1236 spin_unlock_bh(&adap->tid_release_lock);
1237}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001238
1239/*
1240 * Process the list of pending TID release requests.
1241 */
1242static void process_tid_release_list(struct work_struct *work)
1243{
1244 struct sk_buff *skb;
1245 struct adapter *adap;
1246
1247 adap = container_of(work, struct adapter, tid_release_task);
1248
1249 spin_lock_bh(&adap->tid_release_lock);
1250 while (adap->tid_release_head) {
1251 void **p = adap->tid_release_head;
1252 unsigned int chan = (uintptr_t)p & 3;
1253 p = (void *)p - chan;
1254
1255 adap->tid_release_head = *p;
1256 *p = NULL;
1257 spin_unlock_bh(&adap->tid_release_lock);
1258
1259 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1260 GFP_KERNEL)))
1261 schedule_timeout_uninterruptible(1);
1262
1263 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1264 t4_ofld_send(adap, skb);
1265 spin_lock_bh(&adap->tid_release_lock);
1266 }
1267 adap->tid_release_task_busy = false;
1268 spin_unlock_bh(&adap->tid_release_lock);
1269}
1270
1271/*
1272 * Release a TID and inform HW. If we are unable to allocate the release
1273 * message we defer to a work queue.
1274 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301275void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1276 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001277{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001278 struct sk_buff *skb;
1279 struct adapter *adap = container_of(t, struct adapter, tids);
1280
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301281 WARN_ON(tid >= t->ntids);
1282
1283 if (t->tid_tab[tid]) {
1284 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301285 atomic_dec(&t->conns_in_use);
1286 if (t->hash_base && (tid >= t->hash_base)) {
1287 if (family == AF_INET6)
1288 atomic_sub(2, &t->hash_tids_in_use);
1289 else
1290 atomic_dec(&t->hash_tids_in_use);
1291 } else {
1292 if (family == AF_INET6)
1293 atomic_sub(2, &t->tids_in_use);
1294 else
1295 atomic_dec(&t->tids_in_use);
1296 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301297 }
1298
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001299 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1300 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001301 mk_tid_release(skb, chan, tid);
1302 t4_ofld_send(adap, skb);
1303 } else
1304 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001305}
1306EXPORT_SYMBOL(cxgb4_remove_tid);
1307
1308/*
1309 * Allocate and initialize the TID tables. Returns 0 on success.
1310 */
1311static int tid_init(struct tid_info *t)
1312{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301313 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301314 unsigned int max_ftids = t->nftids + t->nsftids;
1315 unsigned int natids = t->natids;
1316 unsigned int stid_bmap_size;
1317 unsigned int ftid_bmap_size;
1318 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001319
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001320 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301321 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001322 size = t->ntids * sizeof(*t->tid_tab) +
1323 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001324 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001325 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001326 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301327 max_ftids * sizeof(*t->ftid_tab) +
1328 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001329
Michal Hocko752ade62017-05-08 15:57:27 -07001330 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001331 if (!t->tid_tab)
1332 return -ENOMEM;
1333
1334 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1335 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001336 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001337 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301338 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001339 spin_lock_init(&t->stid_lock);
1340 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301341 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001342
1343 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301344 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301345 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001346 t->afree = NULL;
1347 t->atids_in_use = 0;
1348 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301349 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301350 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001351
1352 /* Setup the free list for atid_tab and clear the stid bitmap. */
1353 if (natids) {
1354 while (--natids)
1355 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1356 t->afree = t->atid_tab;
1357 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301358
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301359 if (is_offload(adap)) {
1360 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1361 /* Reserve stid 0 for T4/T5 adapters */
1362 if (!t->stid_base &&
1363 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1364 __set_bit(0, t->stid_bmap);
1365 }
1366
1367 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001368 return 0;
1369}
1370
1371/**
1372 * cxgb4_create_server - create an IP server
1373 * @dev: the device
1374 * @stid: the server TID
1375 * @sip: local IP address to bind server to
1376 * @sport: the server's TCP port
1377 * @queue: queue to direct messages from this server to
1378 *
1379 * Create an IP server for the given port and address.
1380 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1381 */
1382int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001383 __be32 sip, __be16 sport, __be16 vlan,
1384 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001385{
1386 unsigned int chan;
1387 struct sk_buff *skb;
1388 struct adapter *adap;
1389 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301390 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001391
1392 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1393 if (!skb)
1394 return -ENOMEM;
1395
1396 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001397 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001398 INIT_TP_WR(req, 0);
1399 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1400 req->local_port = sport;
1401 req->peer_port = htons(0);
1402 req->local_ip = sip;
1403 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001404 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001405 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001406 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1407 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301408 ret = t4_mgmt_tx(adap, skb);
1409 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001410}
1411EXPORT_SYMBOL(cxgb4_create_server);
1412
Vipul Pandya80f40c12013-07-04 16:10:45 +05301413/* cxgb4_create_server6 - create an IPv6 server
1414 * @dev: the device
1415 * @stid: the server TID
1416 * @sip: local IPv6 address to bind server to
1417 * @sport: the server's TCP port
1418 * @queue: queue to direct messages from this server to
1419 *
1420 * Create an IPv6 server for the given port and address.
1421 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1422 */
1423int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1424 const struct in6_addr *sip, __be16 sport,
1425 unsigned int queue)
1426{
1427 unsigned int chan;
1428 struct sk_buff *skb;
1429 struct adapter *adap;
1430 struct cpl_pass_open_req6 *req;
1431 int ret;
1432
1433 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1434 if (!skb)
1435 return -ENOMEM;
1436
1437 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001438 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301439 INIT_TP_WR(req, 0);
1440 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1441 req->local_port = sport;
1442 req->peer_port = htons(0);
1443 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1444 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1445 req->peer_ip_hi = cpu_to_be64(0);
1446 req->peer_ip_lo = cpu_to_be64(0);
1447 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001448 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001449 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1450 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301451 ret = t4_mgmt_tx(adap, skb);
1452 return net_xmit_eval(ret);
1453}
1454EXPORT_SYMBOL(cxgb4_create_server6);
1455
1456int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1457 unsigned int queue, bool ipv6)
1458{
1459 struct sk_buff *skb;
1460 struct adapter *adap;
1461 struct cpl_close_listsvr_req *req;
1462 int ret;
1463
1464 adap = netdev2adap(dev);
1465
1466 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1467 if (!skb)
1468 return -ENOMEM;
1469
Johannes Berg4df864c2017-06-16 14:29:21 +02001470 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301471 INIT_TP_WR(req, 0);
1472 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001473 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1474 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301475 ret = t4_mgmt_tx(adap, skb);
1476 return net_xmit_eval(ret);
1477}
1478EXPORT_SYMBOL(cxgb4_remove_server);
1479
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001480/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001481 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1482 * @mtus: the HW MTU table
1483 * @mtu: the target MTU
1484 * @idx: index of selected entry in the MTU table
1485 *
1486 * Returns the index and the value in the HW MTU table that is closest to
1487 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1488 * table, in which case that smallest available value is selected.
1489 */
1490unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1491 unsigned int *idx)
1492{
1493 unsigned int i = 0;
1494
1495 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1496 ++i;
1497 if (idx)
1498 *idx = i;
1499 return mtus[i];
1500}
1501EXPORT_SYMBOL(cxgb4_best_mtu);
1502
1503/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301504 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1505 * @mtus: the HW MTU table
1506 * @header_size: Header Size
1507 * @data_size_max: maximum Data Segment Size
1508 * @data_size_align: desired Data Segment Size Alignment (2^N)
1509 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1510 *
1511 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1512 * MTU Table based solely on a Maximum MTU parameter, we break that
1513 * parameter up into a Header Size and Maximum Data Segment Size, and
1514 * provide a desired Data Segment Size Alignment. If we find an MTU in
1515 * the Hardware MTU Table which will result in a Data Segment Size with
1516 * the requested alignment _and_ that MTU isn't "too far" from the
1517 * closest MTU, then we'll return that rather than the closest MTU.
1518 */
1519unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1520 unsigned short header_size,
1521 unsigned short data_size_max,
1522 unsigned short data_size_align,
1523 unsigned int *mtu_idxp)
1524{
1525 unsigned short max_mtu = header_size + data_size_max;
1526 unsigned short data_size_align_mask = data_size_align - 1;
1527 int mtu_idx, aligned_mtu_idx;
1528
1529 /* Scan the MTU Table till we find an MTU which is larger than our
1530 * Maximum MTU or we reach the end of the table. Along the way,
1531 * record the last MTU found, if any, which will result in a Data
1532 * Segment Length matching the requested alignment.
1533 */
1534 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1535 unsigned short data_size = mtus[mtu_idx] - header_size;
1536
1537 /* If this MTU minus the Header Size would result in a
1538 * Data Segment Size of the desired alignment, remember it.
1539 */
1540 if ((data_size & data_size_align_mask) == 0)
1541 aligned_mtu_idx = mtu_idx;
1542
1543 /* If we're not at the end of the Hardware MTU Table and the
1544 * next element is larger than our Maximum MTU, drop out of
1545 * the loop.
1546 */
1547 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1548 break;
1549 }
1550
1551 /* If we fell out of the loop because we ran to the end of the table,
1552 * then we just have to use the last [largest] entry.
1553 */
1554 if (mtu_idx == NMTUS)
1555 mtu_idx--;
1556
1557 /* If we found an MTU which resulted in the requested Data Segment
1558 * Length alignment and that's "not far" from the largest MTU which is
1559 * less than or equal to the maximum MTU, then use that.
1560 */
1561 if (aligned_mtu_idx >= 0 &&
1562 mtu_idx - aligned_mtu_idx <= 1)
1563 mtu_idx = aligned_mtu_idx;
1564
1565 /* If the caller has passed in an MTU Index pointer, pass the
1566 * MTU Index back. Return the MTU value.
1567 */
1568 if (mtu_idxp)
1569 *mtu_idxp = mtu_idx;
1570 return mtus[mtu_idx];
1571}
1572EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1573
1574/**
Hariprasad S27999802015-09-23 17:19:26 +05301575 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1576 * @chip: chip type
1577 * @viid: VI id of the given port
1578 *
1579 * Return the SMT index for this VI.
1580 */
1581unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1582{
1583 /* In T4/T5, SMT contains 256 SMAC entries organized in
1584 * 128 rows of 2 entries each.
1585 * In T6, SMT contains 256 SMAC entries in 256 rows.
1586 * TODO: The below code needs to be updated when we add support
1587 * for 256 VFs.
1588 */
1589 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1590 return ((viid & 0x7f) << 1);
1591 else
1592 return (viid & 0x7f);
1593}
1594EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1595
1596/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001597 * cxgb4_port_chan - get the HW channel of a port
1598 * @dev: the net device for the port
1599 *
1600 * Return the HW Tx channel of the given port.
1601 */
1602unsigned int cxgb4_port_chan(const struct net_device *dev)
1603{
1604 return netdev2pinfo(dev)->tx_chan;
1605}
1606EXPORT_SYMBOL(cxgb4_port_chan);
1607
Vipul Pandya881806b2012-05-18 15:29:24 +05301608unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1609{
1610 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001611 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301612
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301613 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1614 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301615 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301616 lp_count = LP_COUNT_G(v1);
1617 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001618 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301619 lp_count = LP_COUNT_T5_G(v1);
1620 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001621 }
1622 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301623}
1624EXPORT_SYMBOL(cxgb4_dbfifo_count);
1625
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001626/**
1627 * cxgb4_port_viid - get the VI id of a port
1628 * @dev: the net device for the port
1629 *
1630 * Return the VI id of the given port.
1631 */
1632unsigned int cxgb4_port_viid(const struct net_device *dev)
1633{
1634 return netdev2pinfo(dev)->viid;
1635}
1636EXPORT_SYMBOL(cxgb4_port_viid);
1637
1638/**
1639 * cxgb4_port_idx - get the index of a port
1640 * @dev: the net device for the port
1641 *
1642 * Return the index of the given port.
1643 */
1644unsigned int cxgb4_port_idx(const struct net_device *dev)
1645{
1646 return netdev2pinfo(dev)->port_id;
1647}
1648EXPORT_SYMBOL(cxgb4_port_idx);
1649
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001650void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1651 struct tp_tcp_stats *v6)
1652{
1653 struct adapter *adap = pci_get_drvdata(pdev);
1654
1655 spin_lock(&adap->stats_lock);
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05301656 t4_tp_get_tcp_stats(adap, v4, v6, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001657 spin_unlock(&adap->stats_lock);
1658}
1659EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1660
1661void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1662 const unsigned int *pgsz_order)
1663{
1664 struct adapter *adap = netdev2adap(dev);
1665
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301666 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1667 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1668 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1669 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001670}
1671EXPORT_SYMBOL(cxgb4_iscsi_init);
1672
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301673int cxgb4_flush_eq_cache(struct net_device *dev)
1674{
1675 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301676
Rahul Lakkireddy736c3b92017-12-08 09:48:40 +05301677 return t4_sge_ctxt_flush(adap, adap->mbox, CTXT_EGRESS);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301678}
1679EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1680
1681static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1682{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301683 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301684 __be64 indices;
1685 int ret;
1686
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301687 spin_lock(&adap->win0_lock);
1688 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1689 sizeof(indices), (__be32 *)&indices,
1690 T4_MEMORY_READ);
1691 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301692 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001693 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1694 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301695 }
1696 return ret;
1697}
1698
1699int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1700 u16 size)
1701{
1702 struct adapter *adap = netdev2adap(dev);
1703 u16 hw_pidx, hw_cidx;
1704 int ret;
1705
1706 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1707 if (ret)
1708 goto out;
1709
1710 if (pidx != hw_pidx) {
1711 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301712 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301713
1714 if (pidx >= hw_pidx)
1715 delta = pidx - hw_pidx;
1716 else
1717 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301718
1719 if (is_t4(adap->params.chip))
1720 val = PIDX_V(delta);
1721 else
1722 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301723 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301724 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1725 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301726 }
1727out:
1728 return ret;
1729}
1730EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1731
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301732int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1733{
1734 struct adapter *adap;
1735 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301736 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301737 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1738 int ret;
1739
1740 adap = netdev2adap(dev);
1741
1742 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1743
1744 /* Figure out where the offset lands in the Memory Type/Address scheme.
1745 * This code assumes that the memory is laid out starting at offset 0
1746 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1747 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1748 * MC0, and some have both MC0 and MC1.
1749 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301750 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1751 edc0_size = EDRAM0_SIZE_G(size) << 20;
1752 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1753 edc1_size = EDRAM1_SIZE_G(size) << 20;
1754 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1755 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301756
1757 edc0_end = edc0_size;
1758 edc1_end = edc0_end + edc1_size;
1759 mc0_end = edc1_end + mc0_size;
1760
1761 if (offset < edc0_end) {
1762 memtype = MEM_EDC0;
1763 memaddr = offset;
1764 } else if (offset < edc1_end) {
1765 memtype = MEM_EDC1;
1766 memaddr = offset - edc0_end;
1767 } else {
1768 if (offset < mc0_end) {
1769 memtype = MEM_MC0;
1770 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301771 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301772 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1773 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301774 mc1_end = mc0_end + mc1_size;
1775 if (offset < mc1_end) {
1776 memtype = MEM_MC1;
1777 memaddr = offset - mc0_end;
1778 } else {
1779 /* offset beyond the end of any memory */
1780 goto err;
1781 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301782 } else {
1783 /* T4/T6 only has a single memory channel */
1784 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301785 }
1786 }
1787
1788 spin_lock(&adap->win0_lock);
1789 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1790 spin_unlock(&adap->win0_lock);
1791 return ret;
1792
1793err:
1794 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1795 stag, offset);
1796 return -EINVAL;
1797}
1798EXPORT_SYMBOL(cxgb4_read_tpte);
1799
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301800u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1801{
1802 u32 hi, lo;
1803 struct adapter *adap;
1804
1805 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301806 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1807 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301808
1809 return ((u64)hi << 32) | (u64)lo;
1810}
1811EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1812
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301813int cxgb4_bar2_sge_qregs(struct net_device *dev,
1814 unsigned int qid,
1815 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301816 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301817 u64 *pbar2_qoffset,
1818 unsigned int *pbar2_qid)
1819{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301820 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301821 qid,
1822 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1823 ? T4_BAR2_QTYPE_EGRESS
1824 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301825 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301826 pbar2_qoffset,
1827 pbar2_qid);
1828}
1829EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1830
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001831static struct pci_driver cxgb4_driver;
1832
1833static void check_neigh_update(struct neighbour *neigh)
1834{
1835 const struct device *parent;
1836 const struct net_device *netdev = neigh->dev;
1837
Parav Panditd0d7b102017-02-04 11:00:49 -06001838 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001839 netdev = vlan_dev_real_dev(netdev);
1840 parent = netdev->dev.parent;
1841 if (parent && parent->driver == &cxgb4_driver.driver)
1842 t4_l2t_update(dev_get_drvdata(parent), neigh);
1843}
1844
1845static int netevent_cb(struct notifier_block *nb, unsigned long event,
1846 void *data)
1847{
1848 switch (event) {
1849 case NETEVENT_NEIGH_UPDATE:
1850 check_neigh_update(data);
1851 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001852 case NETEVENT_REDIRECT:
1853 default:
1854 break;
1855 }
1856 return 0;
1857}
1858
1859static bool netevent_registered;
1860static struct notifier_block cxgb4_netevent_nb = {
1861 .notifier_call = netevent_cb
1862};
1863
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301864static void drain_db_fifo(struct adapter *adap, int usecs)
1865{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001866 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301867
1868 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301869 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1870 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301871 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301872 lp_count = LP_COUNT_G(v1);
1873 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001874 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301875 lp_count = LP_COUNT_T5_G(v1);
1876 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001877 }
1878
1879 if (lp_count == 0 && hp_count == 0)
1880 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301881 set_current_state(TASK_UNINTERRUPTIBLE);
1882 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301883 } while (1);
1884}
1885
1886static void disable_txq_db(struct sge_txq *q)
1887{
Steve Wise05eb2382014-03-14 21:52:08 +05301888 unsigned long flags;
1889
1890 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301891 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301892 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301893}
1894
Steve Wise05eb2382014-03-14 21:52:08 +05301895static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301896{
1897 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301898 if (q->db_pidx_inc) {
1899 /* Make sure that all writes to the TX descriptors
1900 * are committed before we tell HW about them.
1901 */
1902 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301903 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1904 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301905 q->db_pidx_inc = 0;
1906 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301907 q->db_disabled = 0;
1908 spin_unlock_irq(&q->db_lock);
1909}
1910
1911static void disable_dbs(struct adapter *adap)
1912{
1913 int i;
1914
1915 for_each_ethrxq(&adap->sge, i)
1916 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301917 if (is_offload(adap)) {
1918 struct sge_uld_txq_info *txq_info =
1919 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1920
1921 if (txq_info) {
1922 for_each_ofldtxq(&adap->sge, i) {
1923 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1924
1925 disable_txq_db(&txq->q);
1926 }
1927 }
1928 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301929 for_each_port(adap, i)
1930 disable_txq_db(&adap->sge.ctrlq[i].q);
1931}
1932
1933static void enable_dbs(struct adapter *adap)
1934{
1935 int i;
1936
1937 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301938 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301939 if (is_offload(adap)) {
1940 struct sge_uld_txq_info *txq_info =
1941 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1942
1943 if (txq_info) {
1944 for_each_ofldtxq(&adap->sge, i) {
1945 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1946
1947 enable_txq_db(adap, &txq->q);
1948 }
1949 }
1950 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301951 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301952 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1953}
1954
1955static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1956{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301957 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1958
1959 if (adap->uld && adap->uld[type].handle)
1960 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301961}
1962
1963static void process_db_full(struct work_struct *work)
1964{
1965 struct adapter *adap;
1966
1967 adap = container_of(work, struct adapter, db_full_task);
1968
1969 drain_db_fifo(adap, dbfifo_drain_delay);
1970 enable_dbs(adap);
1971 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301972 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1973 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1974 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1975 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1976 else
1977 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1978 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301979}
1980
1981static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1982{
1983 u16 hw_pidx, hw_cidx;
1984 int ret;
1985
Steve Wise05eb2382014-03-14 21:52:08 +05301986 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301987 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1988 if (ret)
1989 goto out;
1990 if (q->db_pidx != hw_pidx) {
1991 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301992 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301993
1994 if (q->db_pidx >= hw_pidx)
1995 delta = q->db_pidx - hw_pidx;
1996 else
1997 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301998
1999 if (is_t4(adap->params.chip))
2000 val = PIDX_V(delta);
2001 else
2002 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302003 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302004 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
2005 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302006 }
2007out:
2008 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05302009 q->db_pidx_inc = 0;
2010 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302011 if (ret)
2012 CH_WARN(adap, "DB drop recovery failed.\n");
2013}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302014
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302015static void recover_all_queues(struct adapter *adap)
2016{
2017 int i;
2018
2019 for_each_ethrxq(&adap->sge, i)
2020 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302021 if (is_offload(adap)) {
2022 struct sge_uld_txq_info *txq_info =
2023 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2024 if (txq_info) {
2025 for_each_ofldtxq(&adap->sge, i) {
2026 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2027
2028 sync_txq_pidx(adap, &txq->q);
2029 }
2030 }
2031 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302032 for_each_port(adap, i)
2033 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2034}
2035
Vipul Pandya881806b2012-05-18 15:29:24 +05302036static void process_db_drop(struct work_struct *work)
2037{
2038 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302039
Vipul Pandya881806b2012-05-18 15:29:24 +05302040 adap = container_of(work, struct adapter, db_drop_task);
2041
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302042 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302043 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002044 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302045 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002046 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302047 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002048 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302049 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302050 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002051 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2052 u16 qid = (dropped_db >> 15) & 0x1ffff;
2053 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302054 u64 bar2_qoffset;
2055 unsigned int bar2_qid;
2056 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002057
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302058 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002059 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302060 if (ret)
2061 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2062 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2063 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302064 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302065 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002066
2067 /* Re-enable BAR2 WC */
2068 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2069 }
2070
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302071 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2072 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302073}
2074
2075void t4_db_full(struct adapter *adap)
2076{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302077 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302078 disable_dbs(adap);
2079 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302080 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2081 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002082 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002083 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302084}
2085
2086void t4_db_dropped(struct adapter *adap)
2087{
Steve Wise05eb2382014-03-14 21:52:08 +05302088 if (is_t4(adap->params.chip)) {
2089 disable_dbs(adap);
2090 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2091 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002092 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302093}
2094
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302095void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002096{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002097 if (!netevent_registered) {
2098 register_netevent_notifier(&cxgb4_netevent_nb);
2099 netevent_registered = true;
2100 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002101}
2102
2103static void detach_ulds(struct adapter *adap)
2104{
2105 unsigned int i;
2106
2107 mutex_lock(&uld_mutex);
2108 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002109
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002110 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002111 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302112 adap->uld[i].state_change(adap->uld[i].handle,
2113 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002114
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002115 if (netevent_registered && list_empty(&adapter_list)) {
2116 unregister_netevent_notifier(&cxgb4_netevent_nb);
2117 netevent_registered = false;
2118 }
2119 mutex_unlock(&uld_mutex);
2120}
2121
2122static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2123{
2124 unsigned int i;
2125
2126 mutex_lock(&uld_mutex);
2127 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302128 if (adap->uld && adap->uld[i].handle)
2129 adap->uld[i].state_change(adap->uld[i].handle,
2130 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002131 mutex_unlock(&uld_mutex);
2132}
2133
Anish Bhatt1bb60372014-10-14 20:07:22 -07002134#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002135static int cxgb4_inet6addr_handler(struct notifier_block *this,
2136 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302137{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002138 struct inet6_ifaddr *ifa = data;
2139 struct net_device *event_dev = ifa->idev->dev;
2140 const struct device *parent = NULL;
2141#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302142 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002143#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002144 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002145 event_dev = vlan_dev_real_dev(event_dev);
2146#if IS_ENABLED(CONFIG_BONDING)
2147 if (event_dev->flags & IFF_MASTER) {
2148 list_for_each_entry(adap, &adapter_list, list_node) {
2149 switch (event) {
2150 case NETDEV_UP:
2151 cxgb4_clip_get(adap->port[0],
2152 (const u32 *)ifa, 1);
2153 break;
2154 case NETDEV_DOWN:
2155 cxgb4_clip_release(adap->port[0],
2156 (const u32 *)ifa, 1);
2157 break;
2158 default:
2159 break;
2160 }
2161 }
2162 return NOTIFY_OK;
2163 }
2164#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302165
Anish Bhattb5a02f52015-01-14 15:17:34 -08002166 if (event_dev)
2167 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302168
Anish Bhattb5a02f52015-01-14 15:17:34 -08002169 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302170 switch (event) {
2171 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002172 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302173 break;
2174 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002175 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302176 break;
2177 default:
2178 break;
2179 }
2180 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002181 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302182}
2183
Anish Bhattb5a02f52015-01-14 15:17:34 -08002184static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302185static struct notifier_block cxgb4_inet6addr_notifier = {
2186 .notifier_call = cxgb4_inet6addr_handler
2187};
2188
Vipul Pandya01bcca62013-07-04 16:10:46 +05302189static void update_clip(const struct adapter *adap)
2190{
2191 int i;
2192 struct net_device *dev;
2193 int ret;
2194
2195 rcu_read_lock();
2196
2197 for (i = 0; i < MAX_NPORTS; i++) {
2198 dev = adap->port[i];
2199 ret = 0;
2200
2201 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002202 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302203
2204 if (ret < 0)
2205 break;
2206 }
2207 rcu_read_unlock();
2208}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002209#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302210
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002211/**
2212 * cxgb_up - enable the adapter
2213 * @adap: adapter being enabled
2214 *
2215 * Called when the first port is enabled, this function performs the
2216 * actions necessary to make an adapter operational, such as completing
2217 * the initialization of HW modules, and enabling interrupts.
2218 *
2219 * Must be called with the rtnl lock held.
2220 */
2221static int cxgb_up(struct adapter *adap)
2222{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002223 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002224
Raju Rangoju91060382017-06-19 17:40:48 +05302225 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002226 err = setup_sge_queues(adap);
2227 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302228 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002229 err = setup_rss(adap);
2230 if (err)
2231 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002232
2233 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002234 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002235 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2236 adap->msix_info[0].desc, adap);
2237 if (err)
2238 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002239 err = request_msix_queue_irqs(adap);
2240 if (err) {
2241 free_irq(adap->msix_info[0].vec, adap);
2242 goto irq_err;
2243 }
2244 } else {
2245 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2246 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002247 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002248 if (err)
2249 goto irq_err;
2250 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302251
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002252 enable_rx(adap);
2253 t4_sge_start(adap);
2254 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002255 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302256 mutex_unlock(&uld_mutex);
2257
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002258 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002259#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302260 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002261#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302262 /* Initialize hash mac addr list*/
2263 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002264 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302265
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002266 irq_err:
2267 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002268 freeq:
2269 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302270 rel_lock:
2271 mutex_unlock(&uld_mutex);
2272 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002273}
2274
2275static void cxgb_down(struct adapter *adapter)
2276{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002277 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302278 cancel_work_sync(&adapter->db_full_task);
2279 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002280 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002281 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002282
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002283 t4_sge_stop(adapter);
2284 t4_free_sge_resources(adapter);
2285 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002286}
2287
2288/*
2289 * net_device operations
2290 */
2291static int cxgb_open(struct net_device *dev)
2292{
2293 int err;
2294 struct port_info *pi = netdev_priv(dev);
2295 struct adapter *adapter = pi->adapter;
2296
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002297 netif_carrier_off(dev);
2298
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002299 if (!(adapter->flags & FULL_INIT_DONE)) {
2300 err = cxgb_up(adapter);
2301 if (err < 0)
2302 return err;
2303 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002304
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302305 /* It's possible that the basic port information could have
2306 * changed since we first read it.
2307 */
2308 err = t4_update_port_info(pi);
2309 if (err < 0)
2310 return err;
2311
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002312 err = link_start(dev);
2313 if (!err)
2314 netif_tx_start_all_queues(dev);
2315 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002316}
2317
2318static int cxgb_close(struct net_device *dev)
2319{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002320 struct port_info *pi = netdev_priv(dev);
2321 struct adapter *adapter = pi->adapter;
Ganesh Goudarba581f72017-09-23 16:07:28 +05302322 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002323
2324 netif_tx_stop_all_queues(dev);
2325 netif_carrier_off(dev);
Ganesh Goudarba581f72017-09-23 16:07:28 +05302326 ret = t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
2327#ifdef CONFIG_CHELSIO_T4_DCB
2328 cxgb4_dcb_reset(dev);
2329 dcb_tx_queue_prio_enable(dev, false);
2330#endif
2331 return ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002332}
2333
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002334int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002335 __be32 sip, __be16 sport, __be16 vlan,
2336 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002337{
2338 int ret;
2339 struct filter_entry *f;
2340 struct adapter *adap;
2341 int i;
2342 u8 *val;
2343
2344 adap = netdev2adap(dev);
2345
Vipul Pandya1cab7752012-12-10 09:30:55 +00002346 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302347 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002348 stid += adap->tids.nftids;
2349
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002350 /* Check to make sure the filter requested is writable ...
2351 */
2352 f = &adap->tids.ftid_tab[stid];
2353 ret = writable_filter(f);
2354 if (ret)
2355 return ret;
2356
2357 /* Clear out any old resources being used by the filter before
2358 * we start constructing the new filter.
2359 */
2360 if (f->valid)
2361 clear_filter(adap, f);
2362
2363 /* Clear out filter specifications */
2364 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2365 f->fs.val.lport = cpu_to_be16(sport);
2366 f->fs.mask.lport = ~0;
2367 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002368 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002369 for (i = 0; i < 4; i++) {
2370 f->fs.val.lip[i] = val[i];
2371 f->fs.mask.lip[i] = ~0;
2372 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302373 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002374 f->fs.val.iport = port;
2375 f->fs.mask.iport = mask;
2376 }
2377 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002378
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302379 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302380 f->fs.val.proto = IPPROTO_TCP;
2381 f->fs.mask.proto = ~0;
2382 }
2383
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002384 f->fs.dirsteer = 1;
2385 f->fs.iq = queue;
2386 /* Mark filter as locked */
2387 f->locked = 1;
2388 f->fs.rpttid = 1;
2389
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302390 /* Save the actual tid. We need this to get the corresponding
2391 * filter entry structure in filter_rpl.
2392 */
2393 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002394 ret = set_filter_wr(adap, stid);
2395 if (ret) {
2396 clear_filter(adap, f);
2397 return ret;
2398 }
2399
2400 return 0;
2401}
2402EXPORT_SYMBOL(cxgb4_create_server_filter);
2403
2404int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2405 unsigned int queue, bool ipv6)
2406{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002407 struct filter_entry *f;
2408 struct adapter *adap;
2409
2410 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002411
2412 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302413 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002414 stid += adap->tids.nftids;
2415
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002416 f = &adap->tids.ftid_tab[stid];
2417 /* Unlock the filter */
2418 f->locked = 0;
2419
Wei Yongjun8c148462016-08-20 15:32:41 +00002420 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002421}
2422EXPORT_SYMBOL(cxgb4_remove_server_filter);
2423
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002424static void cxgb_get_stats(struct net_device *dev,
2425 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002426{
2427 struct port_stats stats;
2428 struct port_info *p = netdev_priv(dev);
2429 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002430
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002431 /* Block retrieving statistics during EEH error
2432 * recovery. Otherwise, the recovery might fail
2433 * and the PCI device will be removed permanently
2434 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002435 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002436 if (!netif_device_present(dev)) {
2437 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002438 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002439 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302440 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2441 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002442 spin_unlock(&adapter->stats_lock);
2443
2444 ns->tx_bytes = stats.tx_octets;
2445 ns->tx_packets = stats.tx_frames;
2446 ns->rx_bytes = stats.rx_octets;
2447 ns->rx_packets = stats.rx_frames;
2448 ns->multicast = stats.rx_mcast_frames;
2449
2450 /* detailed rx_errors */
2451 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2452 stats.rx_runt;
2453 ns->rx_over_errors = 0;
2454 ns->rx_crc_errors = stats.rx_fcs_err;
2455 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302456 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002457 stats.rx_ovflow2 + stats.rx_ovflow3 +
2458 stats.rx_trunc0 + stats.rx_trunc1 +
2459 stats.rx_trunc2 + stats.rx_trunc3;
2460 ns->rx_missed_errors = 0;
2461
2462 /* detailed tx_errors */
2463 ns->tx_aborted_errors = 0;
2464 ns->tx_carrier_errors = 0;
2465 ns->tx_fifo_errors = 0;
2466 ns->tx_heartbeat_errors = 0;
2467 ns->tx_window_errors = 0;
2468
2469 ns->tx_errors = stats.tx_error_frames;
2470 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2471 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002472}
2473
2474static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2475{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002476 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002477 int ret = 0, prtad, devad;
2478 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302479 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002480 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2481
2482 switch (cmd) {
2483 case SIOCGMIIPHY:
2484 if (pi->mdio_addr < 0)
2485 return -EOPNOTSUPP;
2486 data->phy_id = pi->mdio_addr;
2487 break;
2488 case SIOCGMIIREG:
2489 case SIOCSMIIREG:
2490 if (mdio_phy_id_is_c45(data->phy_id)) {
2491 prtad = mdio_phy_id_prtad(data->phy_id);
2492 devad = mdio_phy_id_devad(data->phy_id);
2493 } else if (data->phy_id < 32) {
2494 prtad = data->phy_id;
2495 devad = 0;
2496 data->reg_num &= 0x1f;
2497 } else
2498 return -EINVAL;
2499
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302500 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002501 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002502 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002503 data->reg_num, &data->val_out);
2504 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002505 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002506 data->reg_num, data->val_in);
2507 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302508 case SIOCGHWTSTAMP:
2509 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2510 sizeof(pi->tstamp_config)) ?
2511 -EFAULT : 0;
2512 case SIOCSHWTSTAMP:
2513 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2514 sizeof(pi->tstamp_config)))
2515 return -EFAULT;
2516
Atul Guptaa45695042017-07-04 16:46:20 +05302517 if (!is_t4(adapter->params.chip)) {
2518 switch (pi->tstamp_config.tx_type) {
2519 case HWTSTAMP_TX_OFF:
2520 case HWTSTAMP_TX_ON:
2521 break;
2522 default:
2523 return -ERANGE;
2524 }
2525
2526 switch (pi->tstamp_config.rx_filter) {
2527 case HWTSTAMP_FILTER_NONE:
2528 pi->rxtstamp = false;
2529 break;
2530 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2531 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2532 cxgb4_ptprx_timestamping(pi, pi->port_id,
2533 PTP_TS_L4);
2534 break;
2535 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2536 cxgb4_ptprx_timestamping(pi, pi->port_id,
2537 PTP_TS_L2_L4);
2538 break;
2539 case HWTSTAMP_FILTER_ALL:
2540 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2541 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2542 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2543 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2544 pi->rxtstamp = true;
2545 break;
2546 default:
2547 pi->tstamp_config.rx_filter =
2548 HWTSTAMP_FILTER_NONE;
2549 return -ERANGE;
2550 }
2551
2552 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2553 (pi->tstamp_config.rx_filter ==
2554 HWTSTAMP_FILTER_NONE)) {
2555 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2556 pi->ptp_enable = false;
2557 }
2558
2559 if (pi->tstamp_config.rx_filter !=
2560 HWTSTAMP_FILTER_NONE) {
2561 if (cxgb4_ptp_redirect_rx_packet(adapter,
2562 pi) >= 0)
2563 pi->ptp_enable = true;
2564 }
2565 } else {
2566 /* For T4 Adapters */
2567 switch (pi->tstamp_config.rx_filter) {
2568 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302569 pi->rxtstamp = false;
2570 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302571 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302572 pi->rxtstamp = true;
2573 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302574 default:
2575 pi->tstamp_config.rx_filter =
2576 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302577 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302578 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302579 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302580 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2581 sizeof(pi->tstamp_config)) ?
2582 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002583 default:
2584 return -EOPNOTSUPP;
2585 }
2586 return ret;
2587}
2588
2589static void cxgb_set_rxmode(struct net_device *dev)
2590{
2591 /* unfortunately we can't return errors to the stack */
2592 set_rxmode(dev, -1, false);
2593}
2594
2595static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2596{
2597 int ret;
2598 struct port_info *pi = netdev_priv(dev);
2599
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302600 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002601 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002602 if (!ret)
2603 dev->mtu = new_mtu;
2604 return ret;
2605}
2606
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302607#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302608static int dummy_open(struct net_device *dev)
2609{
2610 /* Turn carrier off since we don't have to transmit anything on this
2611 * interface.
2612 */
2613 netif_carrier_off(dev);
2614 return 0;
2615}
2616
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302617/* Fill MAC address that will be assigned by the FW */
2618static void fill_vf_station_mac_addr(struct adapter *adap)
2619{
2620 unsigned int i;
2621 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2622 int err;
2623 u8 *na;
2624 u16 a, b;
2625
2626 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2627 if (!err) {
2628 na = adap->params.vpd.na;
2629 for (i = 0; i < ETH_ALEN; i++)
2630 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2631 hex2val(na[2 * i + 1]));
2632 a = (hw_addr[0] << 8) | hw_addr[1];
2633 b = (hw_addr[1] << 8) | hw_addr[2];
2634 a ^= b;
2635 a |= 0x0200; /* locally assigned Ethernet MAC address */
2636 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2637 macaddr[0] = a >> 8;
2638 macaddr[1] = a & 0xff;
2639
2640 for (i = 2; i < 5; i++)
2641 macaddr[i] = hw_addr[i + 1];
2642
2643 for (i = 0; i < adap->num_vfs; i++) {
2644 macaddr[5] = adap->pf * 16 + i;
2645 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2646 }
2647 }
2648}
2649
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302650static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2651{
2652 struct port_info *pi = netdev_priv(dev);
2653 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302654 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302655
2656 /* verify MAC addr is valid */
2657 if (!is_valid_ether_addr(mac)) {
2658 dev_err(pi->adapter->pdev_dev,
2659 "Invalid Ethernet address %pM for VF %d\n",
2660 mac, vf);
2661 return -EINVAL;
2662 }
2663
2664 dev_info(pi->adapter->pdev_dev,
2665 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302666 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2667 if (!ret)
2668 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2669 return ret;
2670}
2671
2672static int cxgb_get_vf_config(struct net_device *dev,
2673 int vf, struct ifla_vf_info *ivi)
2674{
2675 struct port_info *pi = netdev_priv(dev);
2676 struct adapter *adap = pi->adapter;
2677
2678 if (vf >= adap->num_vfs)
2679 return -EINVAL;
2680 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302681 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2682 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302683 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2684 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302685}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302686
2687static int cxgb_get_phys_port_id(struct net_device *dev,
2688 struct netdev_phys_item_id *ppid)
2689{
2690 struct port_info *pi = netdev_priv(dev);
2691 unsigned int phy_port_id;
2692
2693 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2694 ppid->id_len = sizeof(phy_port_id);
2695 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2696 return 0;
2697}
2698
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302699static int cxgb_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2700 int max_tx_rate)
2701{
2702 struct port_info *pi = netdev_priv(dev);
2703 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302704 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302705 u32 fw_pfvf, fw_class;
2706 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302707 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302708 u16 pktsize;
2709
2710 if (vf >= adap->num_vfs)
2711 return -EINVAL;
2712
2713 if (min_tx_rate) {
2714 dev_err(adap->pdev_dev,
2715 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2716 min_tx_rate, vf);
2717 return -EINVAL;
2718 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302719
2720 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302721 if (ret != FW_SUCCESS) {
2722 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302723 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302724 return -EINVAL;
2725 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302726
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302727 if (!link_ok) {
2728 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2729 return -EINVAL;
2730 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302731
2732 if (max_tx_rate > speed) {
2733 dev_err(adap->pdev_dev,
2734 "Max tx rate %d for VF %d can't be > link-speed %u",
2735 max_tx_rate, vf, speed);
2736 return -EINVAL;
2737 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302738
2739 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302740 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2741 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2742 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2743 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2744 /* configure Traffic Class for rate-limiting */
2745 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2746 SCHED_CLASS_LEVEL_CL_RL,
2747 SCHED_CLASS_MODE_CLASS,
2748 SCHED_CLASS_RATEUNIT_BITS,
2749 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302750 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302751 max_tx_rate * 1000, 0, pktsize);
2752 if (ret) {
2753 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2754 ret);
2755 return -EINVAL;
2756 }
2757 dev_info(adap->pdev_dev,
2758 "Class %d with MSS %u configured with rate %u\n",
2759 class_id, pktsize, max_tx_rate);
2760
2761 /* bind VF to configured Traffic Class */
2762 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2763 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2764 fw_class = class_id;
2765 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2766 &fw_class);
2767 if (ret) {
2768 dev_err(adap->pdev_dev,
2769 "Err %d in binding VF %d to Traffic Class %d\n",
2770 ret, vf, class_id);
2771 return -EINVAL;
2772 }
2773 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2774 adap->pf, vf, class_id);
2775 adap->vfinfo[vf].tx_rate = max_tx_rate;
2776 return 0;
2777}
2778
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302779#endif
2780
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002781static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2782{
2783 int ret;
2784 struct sockaddr *addr = p;
2785 struct port_info *pi = netdev_priv(dev);
2786
2787 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002788 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002789
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302790 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002791 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002792 if (ret < 0)
2793 return ret;
2794
2795 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2796 pi->xact_addr_filt = ret;
2797 return 0;
2798}
2799
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002800#ifdef CONFIG_NET_POLL_CONTROLLER
2801static void cxgb_netpoll(struct net_device *dev)
2802{
2803 struct port_info *pi = netdev_priv(dev);
2804 struct adapter *adap = pi->adapter;
2805
2806 if (adap->flags & USING_MSIX) {
2807 int i;
2808 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2809
2810 for (i = pi->nqsets; i; i--, rx++)
2811 t4_sge_intr_msix(0, &rx->rspq);
2812 } else
2813 t4_intr_handler(adap)(0, adap);
2814}
2815#endif
2816
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302817static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2818{
2819 struct port_info *pi = netdev_priv(dev);
2820 struct adapter *adap = pi->adapter;
2821 struct sched_class *e;
2822 struct ch_sched_params p;
2823 struct ch_sched_queue qe;
2824 u32 req_rate;
2825 int err = 0;
2826
2827 if (!can_sched(dev))
2828 return -ENOTSUPP;
2829
2830 if (index < 0 || index > pi->nqsets - 1)
2831 return -EINVAL;
2832
2833 if (!(adap->flags & FULL_INIT_DONE)) {
2834 dev_err(adap->pdev_dev,
2835 "Failed to rate limit on queue %d. Link Down?\n",
2836 index);
2837 return -EINVAL;
2838 }
2839
2840 /* Convert from Mbps to Kbps */
2841 req_rate = rate << 10;
2842
2843 /* Max rate is 10 Gbps */
2844 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2845 dev_err(adap->pdev_dev,
2846 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2847 rate, SCHED_MAX_RATE_KBPS);
2848 return -ERANGE;
2849 }
2850
2851 /* First unbind the queue from any existing class */
2852 memset(&qe, 0, sizeof(qe));
2853 qe.queue = index;
2854 qe.class = SCHED_CLS_NONE;
2855
2856 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2857 if (err) {
2858 dev_err(adap->pdev_dev,
2859 "Unbinding Queue %d on port %d fail. Err: %d\n",
2860 index, pi->port_id, err);
2861 return err;
2862 }
2863
2864 /* Queue already unbound */
2865 if (!req_rate)
2866 return 0;
2867
2868 /* Fetch any available unused or matching scheduling class */
2869 memset(&p, 0, sizeof(p));
2870 p.type = SCHED_CLASS_TYPE_PACKET;
2871 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2872 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2873 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2874 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2875 p.u.params.channel = pi->tx_chan;
2876 p.u.params.class = SCHED_CLS_NONE;
2877 p.u.params.minrate = 0;
2878 p.u.params.maxrate = req_rate;
2879 p.u.params.weight = 0;
2880 p.u.params.pktsize = dev->mtu;
2881
2882 e = cxgb4_sched_class_alloc(dev, &p);
2883 if (!e)
2884 return -ENOMEM;
2885
2886 /* Bind the queue to a scheduling class */
2887 memset(&qe, 0, sizeof(qe));
2888 qe.queue = index;
2889 qe.class = e->idx;
2890
2891 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2892 if (err)
2893 dev_err(adap->pdev_dev,
2894 "Queue rate limiting failed. Err: %d\n", err);
2895 return err;
2896}
2897
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302898static int cxgb_setup_tc_flower(struct net_device *dev,
2899 struct tc_cls_flower_offload *cls_flower)
2900{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002901 if (cls_flower->common.chain_index)
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302902 return -EOPNOTSUPP;
2903
2904 switch (cls_flower->command) {
2905 case TC_CLSFLOWER_REPLACE:
2906 return cxgb4_tc_flower_replace(dev, cls_flower);
2907 case TC_CLSFLOWER_DESTROY:
2908 return cxgb4_tc_flower_destroy(dev, cls_flower);
2909 case TC_CLSFLOWER_STATS:
2910 return cxgb4_tc_flower_stats(dev, cls_flower);
2911 default:
2912 return -EOPNOTSUPP;
2913 }
2914}
2915
Jiri Pirkof7323042017-08-07 10:15:20 +02002916static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002917 struct tc_cls_u32_offload *cls_u32)
2918{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002919 if (cls_u32->common.chain_index)
Jiri Pirkof7323042017-08-07 10:15:20 +02002920 return -EOPNOTSUPP;
2921
2922 switch (cls_u32->command) {
2923 case TC_CLSU32_NEW_KNODE:
2924 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002925 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002926 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002927 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002928 default:
2929 return -EOPNOTSUPP;
2930 }
2931}
2932
Jiri Pirkocd019e92017-10-19 15:50:40 +02002933static int cxgb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
2934 void *cb_priv)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302935{
Jiri Pirkocd019e92017-10-19 15:50:40 +02002936 struct net_device *dev = cb_priv;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302937 struct port_info *pi = netdev2pinfo(dev);
2938 struct adapter *adap = netdev2adap(dev);
2939
2940 if (!(adap->flags & FULL_INIT_DONE)) {
2941 dev_err(adap->pdev_dev,
2942 "Failed to setup tc on port %d. Link Down?\n",
2943 pi->port_id);
2944 return -EINVAL;
2945 }
2946
Jiri Pirko44ae12a2017-11-01 11:47:39 +01002947 if (!tc_can_offload(dev))
2948 return -EOPNOTSUPP;
2949
Jiri Pirkof7323042017-08-07 10:15:20 +02002950 switch (type) {
2951 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002952 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302953 case TC_SETUP_CLSFLOWER:
2954 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002955 default:
2956 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302957 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302958}
2959
Jiri Pirkocd019e92017-10-19 15:50:40 +02002960static int cxgb_setup_tc_block(struct net_device *dev,
2961 struct tc_block_offload *f)
2962{
2963 struct port_info *pi = netdev2pinfo(dev);
2964
2965 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
2966 return -EOPNOTSUPP;
2967
2968 switch (f->command) {
2969 case TC_BLOCK_BIND:
2970 return tcf_block_cb_register(f->block, cxgb_setup_tc_block_cb,
2971 pi, dev);
2972 case TC_BLOCK_UNBIND:
2973 tcf_block_cb_unregister(f->block, cxgb_setup_tc_block_cb, pi);
2974 return 0;
2975 default:
2976 return -EOPNOTSUPP;
2977 }
2978}
2979
2980static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
2981 void *type_data)
2982{
2983 switch (type) {
Jiri Pirkocd019e92017-10-19 15:50:40 +02002984 case TC_SETUP_BLOCK:
2985 return cxgb_setup_tc_block(dev, type_data);
2986 default:
2987 return -EOPNOTSUPP;
2988 }
2989}
2990
Ganesh Goudar846eac32018-01-10 18:15:08 +05302991static void cxgb_del_udp_tunnel(struct net_device *netdev,
2992 struct udp_tunnel_info *ti)
2993{
2994 struct port_info *pi = netdev_priv(netdev);
2995 struct adapter *adapter = pi->adapter;
2996 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
2997 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
2998 int ret = 0, i;
2999
3000 if (chip_ver < CHELSIO_T6)
3001 return;
3002
3003 switch (ti->type) {
3004 case UDP_TUNNEL_TYPE_VXLAN:
3005 if (!adapter->vxlan_port_cnt ||
3006 adapter->vxlan_port != ti->port)
3007 return; /* Invalid VxLAN destination port */
3008
3009 adapter->vxlan_port_cnt--;
3010 if (adapter->vxlan_port_cnt)
3011 return;
3012
3013 adapter->vxlan_port = 0;
3014 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A, 0);
3015 break;
3016 default:
3017 return;
3018 }
3019
3020 /* Matchall mac entries can be deleted only after all tunnel ports
3021 * are brought down or removed.
3022 */
3023 if (!adapter->rawf_cnt)
3024 return;
3025 for_each_port(adapter, i) {
3026 pi = adap2pinfo(adapter, i);
3027 ret = t4_free_raw_mac_filt(adapter, pi->viid,
3028 match_all_mac, match_all_mac,
3029 adapter->rawf_start +
3030 pi->port_id,
3031 1, pi->port_id, true);
3032 if (ret < 0) {
3033 netdev_info(netdev, "Failed to free mac filter entry, for port %d\n",
3034 i);
3035 return;
3036 }
3037 atomic_dec(&adapter->mps_encap[adapter->rawf_start +
3038 pi->port_id].refcnt);
3039 }
3040}
3041
3042static void cxgb_add_udp_tunnel(struct net_device *netdev,
3043 struct udp_tunnel_info *ti)
3044{
3045 struct port_info *pi = netdev_priv(netdev);
3046 struct adapter *adapter = pi->adapter;
3047 unsigned int chip_ver = CHELSIO_CHIP_VERSION(adapter->params.chip);
3048 u8 match_all_mac[] = { 0, 0, 0, 0, 0, 0 };
3049 int i, ret;
3050
3051 if (chip_ver < CHELSIO_T6)
3052 return;
3053
3054 switch (ti->type) {
3055 case UDP_TUNNEL_TYPE_VXLAN:
3056 /* For T6 fw reserves last 2 entries for
3057 * storing match all mac filter (config file entry).
3058 */
3059 if (!adapter->rawf_cnt)
3060 return;
3061
3062 /* Callback for adding vxlan port can be called with the same
3063 * port for both IPv4 and IPv6. We should not disable the
3064 * offloading when the same port for both protocols is added
3065 * and later one of them is removed.
3066 */
3067 if (adapter->vxlan_port_cnt &&
3068 adapter->vxlan_port == ti->port) {
3069 adapter->vxlan_port_cnt++;
3070 return;
3071 }
3072
3073 /* We will support only one VxLAN port */
3074 if (adapter->vxlan_port_cnt) {
3075 netdev_info(netdev, "UDP port %d already offloaded, not adding port %d\n",
3076 be16_to_cpu(adapter->vxlan_port),
3077 be16_to_cpu(ti->port));
3078 return;
3079 }
3080
3081 adapter->vxlan_port = ti->port;
3082 adapter->vxlan_port_cnt = 1;
3083
3084 t4_write_reg(adapter, MPS_RX_VXLAN_TYPE_A,
3085 VXLAN_V(be16_to_cpu(ti->port)) | VXLAN_EN_F);
3086 break;
3087 default:
3088 return;
3089 }
3090
3091 /* Create a 'match all' mac filter entry for inner mac,
3092 * if raw mac interface is supported. Once the linux kernel provides
3093 * driver entry points for adding/deleting the inner mac addresses,
3094 * we will remove this 'match all' entry and fallback to adding
3095 * exact match filters.
3096 */
3097 if (adapter->rawf_cnt) {
3098 for_each_port(adapter, i) {
3099 pi = adap2pinfo(adapter, i);
3100
3101 ret = t4_alloc_raw_mac_filt(adapter, pi->viid,
3102 match_all_mac,
3103 match_all_mac,
3104 adapter->rawf_start +
3105 pi->port_id,
3106 1, pi->port_id, true);
3107 if (ret < 0) {
3108 netdev_info(netdev, "Failed to allocate a mac filter entry, not adding port %d\n",
3109 be16_to_cpu(ti->port));
3110 cxgb_del_udp_tunnel(netdev, ti);
3111 return;
3112 }
3113 atomic_inc(&adapter->mps_encap[ret].refcnt);
3114 }
3115 }
3116}
3117
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303118static netdev_features_t cxgb_features_check(struct sk_buff *skb,
3119 struct net_device *dev,
3120 netdev_features_t features)
3121{
3122 struct port_info *pi = netdev_priv(dev);
3123 struct adapter *adapter = pi->adapter;
3124
3125 if (CHELSIO_CHIP_VERSION(adapter->params.chip) < CHELSIO_T6)
3126 return features;
3127
3128 /* Check if hw supports offload for this packet */
3129 if (!skb->encapsulation || cxgb_encap_offload_supported(skb))
3130 return features;
3131
3132 /* Offload is not supported for this encapsulated packet */
3133 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3134}
3135
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303136static netdev_features_t cxgb_fix_features(struct net_device *dev,
3137 netdev_features_t features)
3138{
3139 /* Disable GRO, if RX_CSUM is disabled */
3140 if (!(features & NETIF_F_RXCSUM))
3141 features &= ~NETIF_F_GRO;
3142
3143 return features;
3144}
3145
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003146static const struct net_device_ops cxgb4_netdev_ops = {
3147 .ndo_open = cxgb_open,
3148 .ndo_stop = cxgb_close,
3149 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07003150 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00003151 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003152 .ndo_set_rx_mode = cxgb_set_rxmode,
3153 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00003154 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003155 .ndo_validate_addr = eth_validate_addr,
3156 .ndo_do_ioctl = cxgb_ioctl,
3157 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003158#ifdef CONFIG_NET_POLL_CONTROLLER
3159 .ndo_poll_controller = cxgb_netpoll,
3160#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05303161#ifdef CONFIG_CHELSIO_T4_FCOE
3162 .ndo_fcoe_enable = cxgb_fcoe_enable,
3163 .ndo_fcoe_disable = cxgb_fcoe_disable,
3164#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05303165 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05303166 .ndo_setup_tc = cxgb_setup_tc,
Ganesh Goudar846eac32018-01-10 18:15:08 +05303167 .ndo_udp_tunnel_add = cxgb_add_udp_tunnel,
3168 .ndo_udp_tunnel_del = cxgb_del_udp_tunnel,
Ganesh Goudar4621ffd2018-01-10 18:15:47 +05303169 .ndo_features_check = cxgb_features_check,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05303170 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003171};
3172
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303173#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303174static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
3175 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05303176 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05303177 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05303178 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05303179 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05303180};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05303181#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05303182
3183static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
3184{
3185 struct adapter *adapter = netdev2adap(dev);
3186
3187 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
3188 strlcpy(info->version, cxgb4_driver_version,
3189 sizeof(info->version));
3190 strlcpy(info->bus_info, pci_name(adapter->pdev),
3191 sizeof(info->bus_info));
3192}
3193
3194static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
3195 .get_drvinfo = get_drvinfo,
3196};
3197
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003198void t4_fatal_err(struct adapter *adap)
3199{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303200 int port;
3201
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003202 if (pci_channel_offline(adap->pdev))
3203 return;
3204
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303205 /* Disable the SGE since ULDs are going to free resources that
3206 * could be exposed to the adapter. RDMA MWs for example...
3207 */
3208 t4_shutdown_adapter(adap);
3209 for_each_port(adap, port) {
3210 struct net_device *dev = adap->port[port];
3211
3212 /* If we get here in very early initialization the network
3213 * devices may not have been set up yet.
3214 */
3215 if (!dev)
3216 continue;
3217
3218 netif_tx_stop_all_queues(dev);
3219 netif_carrier_off(dev);
3220 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003221 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3222}
3223
3224static void setup_memwin(struct adapter *adap)
3225{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303226 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003227
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303228 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003229}
3230
3231static void setup_memwin_rdma(struct adapter *adap)
3232{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003233 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303234 u32 start;
3235 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003236
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303237 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3238 start &= PCI_BASE_ADDRESS_MEM_MASK;
3239 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003240 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3241 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303242 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3243 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003244 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303245 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003246 adap->vres.ocq.start);
3247 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303248 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003249 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003250}
3251
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003252static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3253{
3254 u32 v;
3255 int ret;
3256
3257 /* get device capabilities */
3258 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303259 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3260 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303261 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303262 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003263 if (ret < 0)
3264 return ret;
3265
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303266 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3267 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303268 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003269 if (ret < 0)
3270 return ret;
3271
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303272 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003273 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303274 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3275 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003276 if (ret < 0)
3277 return ret;
3278
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303279 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303280 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3281 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003282 if (ret < 0)
3283 return ret;
3284
3285 t4_sge_init(adap);
3286
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003287 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303288 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303289 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303290 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3291 v = t4_read_reg(adap, TP_PIO_DATA_A);
3292 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003293
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003294 /* first 4 Tx modulation queues point to consecutive Tx channels */
3295 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303296 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3297 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003298
3299 /* associate each Tx modulation queue with consecutive Tx channels */
3300 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303301 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303302 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303303 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303304 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303305 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303306 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003307
3308#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3309 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303310 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3311 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3312 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3313 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3314 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3315 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3316 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3317 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3318 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3319 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003320 }
3321
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003322 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303323 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003324}
3325
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003326/*
3327 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3328 */
3329#define MAX_ATIDS 8192U
3330
3331/*
3332 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003333 *
3334 * If the firmware we're dealing with has Configuration File support, then
3335 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003336 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003337
3338/*
3339 * Tweak configuration based on module parameters, etc. Most of these have
3340 * defaults assigned to them by Firmware Configuration Files (if we're using
3341 * them) but need to be explicitly set if we're using hard-coded
3342 * initialization. But even in the case of using Firmware Configuration
3343 * Files, we'd like to expose the ability to change these via module
3344 * parameters so these are essentially common tweaks/settings for
3345 * Configuration Files and hard-coded initialization ...
3346 */
3347static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003348{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003349 /*
3350 * Fix up various Host-Dependent Parameters like Page Size, Cache
3351 * Line Size, etc. The firmware default is for a 4KB Page Size and
3352 * 64B Cache Line Size ...
3353 */
3354 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003355
Vipul Pandya636f9d32012-09-26 02:39:39 +00003356 /*
3357 * Process module parameters which affect early initialization.
3358 */
3359 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3360 dev_err(&adapter->pdev->dev,
3361 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3362 rx_dma_offset);
3363 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003364 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303365 t4_set_reg_field(adapter, SGE_CONTROL_A,
3366 PKTSHIFT_V(PKTSHIFT_M),
3367 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003368
Vipul Pandya636f9d32012-09-26 02:39:39 +00003369 /*
3370 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3371 * adds the pseudo header itself.
3372 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303373 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3374 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003375
3376 return 0;
3377}
3378
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303379/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3380 * unto themselves and they contain their own firmware to perform their
3381 * tasks ...
3382 */
3383static int phy_aq1202_version(const u8 *phy_fw_data,
3384 size_t phy_fw_size)
3385{
3386 int offset;
3387
3388 /* At offset 0x8 you're looking for the primary image's
3389 * starting offset which is 3 Bytes wide
3390 *
3391 * At offset 0xa of the primary image, you look for the offset
3392 * of the DRAM segment which is 3 Bytes wide.
3393 *
3394 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3395 * wide
3396 */
3397 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3398 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3399 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3400
3401 offset = le24(phy_fw_data + 0x8) << 12;
3402 offset = le24(phy_fw_data + offset + 0xa);
3403 return be16(phy_fw_data + offset + 0x27e);
3404
3405 #undef be16
3406 #undef le16
3407 #undef le24
3408}
3409
3410static struct info_10gbt_phy_fw {
3411 unsigned int phy_fw_id; /* PCI Device ID */
3412 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3413 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3414 int phy_flash; /* Has FLASH for PHY Firmware */
3415} phy_info_array[] = {
3416 {
3417 PHY_AQ1202_DEVICEID,
3418 PHY_AQ1202_FIRMWARE,
3419 phy_aq1202_version,
3420 1,
3421 },
3422 {
3423 PHY_BCM84834_DEVICEID,
3424 PHY_BCM84834_FIRMWARE,
3425 NULL,
3426 0,
3427 },
3428 { 0, NULL, NULL },
3429};
3430
3431static struct info_10gbt_phy_fw *find_phy_info(int devid)
3432{
3433 int i;
3434
3435 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3436 if (phy_info_array[i].phy_fw_id == devid)
3437 return &phy_info_array[i];
3438 }
3439 return NULL;
3440}
3441
3442/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3443 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3444 * we return a negative error number. If we transfer new firmware we return 1
3445 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3446 */
3447static int adap_init0_phy(struct adapter *adap)
3448{
3449 const struct firmware *phyf;
3450 int ret;
3451 struct info_10gbt_phy_fw *phy_info;
3452
3453 /* Use the device ID to determine which PHY file to flash.
3454 */
3455 phy_info = find_phy_info(adap->pdev->device);
3456 if (!phy_info) {
3457 dev_warn(adap->pdev_dev,
3458 "No PHY Firmware file found for this PHY\n");
3459 return -EOPNOTSUPP;
3460 }
3461
3462 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3463 * use that. The adapter firmware provides us with a memory buffer
3464 * where we can load a PHY firmware file from the host if we want to
3465 * override the PHY firmware File in flash.
3466 */
3467 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3468 adap->pdev_dev);
3469 if (ret < 0) {
3470 /* For adapters without FLASH attached to PHY for their
3471 * firmware, it's obviously a fatal error if we can't get the
3472 * firmware to the adapter. For adapters with PHY firmware
3473 * FLASH storage, it's worth a warning if we can't find the
3474 * PHY Firmware but we'll neuter the error ...
3475 */
3476 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3477 "/lib/firmware/%s, error %d\n",
3478 phy_info->phy_fw_file, -ret);
3479 if (phy_info->phy_flash) {
3480 int cur_phy_fw_ver = 0;
3481
3482 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3483 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3484 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3485 ret = 0;
3486 }
3487
3488 return ret;
3489 }
3490
3491 /* Load PHY Firmware onto adapter.
3492 */
3493 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3494 phy_info->phy_fw_version,
3495 (u8 *)phyf->data, phyf->size);
3496 if (ret < 0)
3497 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3498 -ret);
3499 else if (ret > 0) {
3500 int new_phy_fw_ver = 0;
3501
3502 if (phy_info->phy_fw_version)
3503 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3504 phyf->size);
3505 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3506 "Firmware /lib/firmware/%s, version %#x\n",
3507 phy_info->phy_fw_file, new_phy_fw_ver);
3508 }
3509
3510 release_firmware(phyf);
3511
3512 return ret;
3513}
3514
Vipul Pandya636f9d32012-09-26 02:39:39 +00003515/*
3516 * Attempt to initialize the adapter via a Firmware Configuration File.
3517 */
3518static int adap_init0_config(struct adapter *adapter, int reset)
3519{
3520 struct fw_caps_config_cmd caps_cmd;
3521 const struct firmware *cf;
3522 unsigned long mtype = 0, maddr = 0;
3523 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303524 int ret;
3525 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003526 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303527 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003528
3529 /*
3530 * Reset device if necessary.
3531 */
3532 if (reset) {
3533 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303534 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003535 if (ret < 0)
3536 goto bye;
3537 }
3538
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303539 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3540 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3541 * to be performed after any global adapter RESET above since some
3542 * PHYs only have local RAM copies of the PHY firmware.
3543 */
3544 if (is_10gbt_device(adapter->pdev->device)) {
3545 ret = adap_init0_phy(adapter);
3546 if (ret < 0)
3547 goto bye;
3548 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003549 /*
3550 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3551 * then use that. Otherwise, use the configuration file stored
3552 * in the adapter flash ...
3553 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303554 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003555 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303556 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003557 break;
3558 case CHELSIO_T5:
3559 fw_config_file = FW5_CFNAME;
3560 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303561 case CHELSIO_T6:
3562 fw_config_file = FW6_CFNAME;
3563 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003564 default:
3565 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3566 adapter->pdev->device);
3567 ret = -EINVAL;
3568 goto bye;
3569 }
3570
3571 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003572 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303573 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003574 mtype = FW_MEMTYPE_CF_FLASH;
3575 maddr = t4_flash_cfg_addr(adapter);
3576 } else {
3577 u32 params[7], val[7];
3578
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303579 sprintf(fw_config_file_path,
3580 "/lib/firmware/%s", fw_config_file);
3581 config_name = fw_config_file_path;
3582
Vipul Pandya636f9d32012-09-26 02:39:39 +00003583 if (cf->size >= FLASH_CFG_MAX_SIZE)
3584 ret = -ENOMEM;
3585 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303586 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3587 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003588 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303589 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003590 if (ret == 0) {
3591 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303592 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003593 * sizes have to be in terms of multiples of 4
3594 * bytes. So, if the Configuration File isn't
3595 * a multiple of 4 bytes in length we'll have
3596 * to write that out separately since we can't
3597 * guarantee that the bytes following the
3598 * residual byte in the buffer returned by
3599 * request_firmware() are zeroed out ...
3600 */
3601 size_t resid = cf->size & 0x3;
3602 size_t size = cf->size & ~0x3;
3603 __be32 *data = (__be32 *)cf->data;
3604
Hariprasad Shenai51678652014-11-21 12:52:02 +05303605 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3606 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003607
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303608 spin_lock(&adapter->win0_lock);
3609 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3610 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003611 if (ret == 0 && resid != 0) {
3612 union {
3613 __be32 word;
3614 char buf[4];
3615 } last;
3616 int i;
3617
3618 last.word = data[size >> 2];
3619 for (i = resid; i < 4; i++)
3620 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303621 ret = t4_memory_rw(adapter, 0, mtype,
3622 maddr + size,
3623 4, &last.word,
3624 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003625 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303626 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003627 }
3628 }
3629
3630 release_firmware(cf);
3631 if (ret)
3632 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003633 }
3634
Vipul Pandya636f9d32012-09-26 02:39:39 +00003635 /*
3636 * Issue a Capability Configuration command to the firmware to get it
3637 * to parse the Configuration File. We don't use t4_fw_config_file()
3638 * because we want the ability to modify various features after we've
3639 * processed the configuration file ...
3640 */
3641 memset(&caps_cmd, 0, sizeof(caps_cmd));
3642 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303643 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3644 FW_CMD_REQUEST_F |
3645 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303646 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303647 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3648 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3649 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003650 FW_LEN16(caps_cmd));
3651 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3652 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303653
3654 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3655 * Configuration File in FLASH), our last gasp effort is to use the
3656 * Firmware Configuration File which is embedded in the firmware. A
3657 * very few early versions of the firmware didn't have one embedded
3658 * but we can ignore those.
3659 */
3660 if (ret == -ENOENT) {
3661 memset(&caps_cmd, 0, sizeof(caps_cmd));
3662 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303663 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3664 FW_CMD_REQUEST_F |
3665 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303666 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3667 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3668 sizeof(caps_cmd), &caps_cmd);
3669 config_name = "Firmware Default";
3670 }
3671
3672 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003673 if (ret < 0)
3674 goto bye;
3675
Vipul Pandya636f9d32012-09-26 02:39:39 +00003676 finiver = ntohl(caps_cmd.finiver);
3677 finicsum = ntohl(caps_cmd.finicsum);
3678 cfcsum = ntohl(caps_cmd.cfcsum);
3679 if (finicsum != cfcsum)
3680 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3681 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3682 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003683
Vipul Pandya636f9d32012-09-26 02:39:39 +00003684 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003685 * And now tell the firmware to use the configuration we just loaded.
3686 */
3687 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303688 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3689 FW_CMD_REQUEST_F |
3690 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303691 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003692 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3693 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003694 if (ret < 0)
3695 goto bye;
3696
Vipul Pandya636f9d32012-09-26 02:39:39 +00003697 /*
3698 * Tweak configuration based on system architecture, module
3699 * parameters, etc.
3700 */
3701 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003702 if (ret < 0)
3703 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003704
Vipul Pandya636f9d32012-09-26 02:39:39 +00003705 /*
3706 * And finally tell the firmware to initialize itself using the
3707 * parameters from the Configuration File.
3708 */
3709 ret = t4_fw_initialize(adapter, adapter->mbox);
3710 if (ret < 0)
3711 goto bye;
3712
Hariprasad Shenai06640312015-01-13 15:19:25 +05303713 /* Emit Firmware Configuration File information and return
3714 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003715 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003716 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303717 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3718 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003719 return 0;
3720
3721 /*
3722 * Something bad happened. Return the error ... (If the "error"
3723 * is that there's no Configuration File on the adapter we don't
3724 * want to issue a warning since this is fairly common.)
3725 */
3726bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303727 if (config_issued && ret != -ENOENT)
3728 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3729 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003730 return ret;
3731}
3732
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303733static struct fw_info fw_info_array[] = {
3734 {
3735 .chip = CHELSIO_T4,
3736 .fs_name = FW4_CFNAME,
3737 .fw_mod_name = FW4_FNAME,
3738 .fw_hdr = {
3739 .chip = FW_HDR_CHIP_T4,
3740 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3741 .intfver_nic = FW_INTFVER(T4, NIC),
3742 .intfver_vnic = FW_INTFVER(T4, VNIC),
3743 .intfver_ri = FW_INTFVER(T4, RI),
3744 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3745 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3746 },
3747 }, {
3748 .chip = CHELSIO_T5,
3749 .fs_name = FW5_CFNAME,
3750 .fw_mod_name = FW5_FNAME,
3751 .fw_hdr = {
3752 .chip = FW_HDR_CHIP_T5,
3753 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3754 .intfver_nic = FW_INTFVER(T5, NIC),
3755 .intfver_vnic = FW_INTFVER(T5, VNIC),
3756 .intfver_ri = FW_INTFVER(T5, RI),
3757 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3758 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3759 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303760 }, {
3761 .chip = CHELSIO_T6,
3762 .fs_name = FW6_CFNAME,
3763 .fw_mod_name = FW6_FNAME,
3764 .fw_hdr = {
3765 .chip = FW_HDR_CHIP_T6,
3766 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3767 .intfver_nic = FW_INTFVER(T6, NIC),
3768 .intfver_vnic = FW_INTFVER(T6, VNIC),
3769 .intfver_ofld = FW_INTFVER(T6, OFLD),
3770 .intfver_ri = FW_INTFVER(T6, RI),
3771 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3772 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3773 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3774 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3775 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303776 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303777
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303778};
3779
3780static struct fw_info *find_fw_info(int chip)
3781{
3782 int i;
3783
3784 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3785 if (fw_info_array[i].chip == chip)
3786 return &fw_info_array[i];
3787 }
3788 return NULL;
3789}
3790
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003791/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003792 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003793 */
3794static int adap_init0(struct adapter *adap)
3795{
3796 int ret;
3797 u32 v, port_vec;
3798 enum dev_state state;
3799 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003800 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303801 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003802
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303803 /* Grab Firmware Device Log parameters as early as possible so we have
3804 * access to it for debugging, etc.
3805 */
3806 ret = t4_init_devlog_params(adap);
3807 if (ret < 0)
3808 return ret;
3809
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303810 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303811 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3812 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003813 if (ret < 0) {
3814 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3815 ret);
3816 return ret;
3817 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003818 if (ret == adap->mbox)
3819 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003820
Vipul Pandya636f9d32012-09-26 02:39:39 +00003821 /*
3822 * If we're the Master PF Driver and the device is uninitialized,
3823 * then let's consider upgrading the firmware ... (We always want
3824 * to check the firmware version number in order to A. get it for
3825 * later reporting and B. to warn if the currently loaded firmware
3826 * is excessively mismatched relative to the driver.)
3827 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303828
Ganesh Goudar760446f2017-07-20 18:28:48 +05303829 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303830 ret = t4_check_fw_version(adap);
3831 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303832 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303833 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003834 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303835 struct fw_info *fw_info;
3836 struct fw_hdr *card_fw;
3837 const struct firmware *fw;
3838 const u8 *fw_data = NULL;
3839 unsigned int fw_size = 0;
3840
3841 /* This is the firmware whose headers the driver was compiled
3842 * against
3843 */
3844 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3845 if (fw_info == NULL) {
3846 dev_err(adap->pdev_dev,
3847 "unable to get firmware info for chip %d.\n",
3848 CHELSIO_CHIP_VERSION(adap->params.chip));
3849 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003850 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303851
3852 /* allocate memory to read the header of the firmware on the
3853 * card
3854 */
Michal Hocko752ade62017-05-08 15:57:27 -07003855 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303856
3857 /* Get FW from from /lib/firmware/ */
3858 ret = request_firmware(&fw, fw_info->fw_mod_name,
3859 adap->pdev_dev);
3860 if (ret < 0) {
3861 dev_err(adap->pdev_dev,
3862 "unable to load firmware image %s, error %d\n",
3863 fw_info->fw_mod_name, ret);
3864 } else {
3865 fw_data = fw->data;
3866 fw_size = fw->size;
3867 }
3868
3869 /* upgrade FW logic */
3870 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3871 state, &reset);
3872
3873 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003874 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003875 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303876
Vipul Pandya636f9d32012-09-26 02:39:39 +00003877 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303878 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003879 }
3880
3881 /*
3882 * Grab VPD parameters. This should be done after we establish a
3883 * connection to the firmware since some of the VPD parameters
3884 * (notably the Core Clock frequency) are retrieved via requests to
3885 * the firmware. On the other hand, we need these fairly early on
3886 * so we do this right after getting ahold of the firmware.
3887 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303888 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003889 if (ret < 0)
3890 goto bye;
3891
Vipul Pandya636f9d32012-09-26 02:39:39 +00003892 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003893 * Find out what ports are available to us. Note that we need to do
3894 * this before calling adap_init0_no_config() since it needs nports
3895 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003896 */
3897 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303898 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3899 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303900 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003901 if (ret < 0)
3902 goto bye;
3903
3904 adap->params.nports = hweight32(port_vec);
3905 adap->params.portvec = port_vec;
3906
Hariprasad Shenai06640312015-01-13 15:19:25 +05303907 /* If the firmware is initialized already, emit a simply note to that
3908 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003909 */
3910 if (state == DEV_STATE_INIT) {
3911 dev_info(adap->pdev_dev, "Coming up as %s: "\
3912 "Adapter already initialized\n",
3913 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003914 } else {
3915 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3916 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303917
3918 /* Find out whether we're dealing with a version of the
3919 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003920 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303921 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3922 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303923 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303924 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003925
Hariprasad Shenai06640312015-01-13 15:19:25 +05303926 /* If the firmware doesn't support Configuration Files,
3927 * return an error.
3928 */
3929 if (ret < 0) {
3930 dev_err(adap->pdev_dev, "firmware doesn't support "
3931 "Firmware Configuration Files\n");
3932 goto bye;
3933 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003934
Hariprasad Shenai06640312015-01-13 15:19:25 +05303935 /* The firmware provides us with a memory buffer where we can
3936 * load a Configuration File from the host if we want to
3937 * override the Configuration File in flash.
3938 */
3939 ret = adap_init0_config(adap, reset);
3940 if (ret == -ENOENT) {
3941 dev_err(adap->pdev_dev, "no Configuration File "
3942 "present on adapter.\n");
3943 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003944 }
3945 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303946 dev_err(adap->pdev_dev, "could not initialize "
3947 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003948 goto bye;
3949 }
3950 }
3951
Hariprasad Shenai06640312015-01-13 15:19:25 +05303952 /* Give the SGE code a chance to pull in anything that it needs ...
3953 * Note that this must be called after we retrieve our VPD parameters
3954 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003955 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303956 ret = t4_sge_init(adap);
3957 if (ret < 0)
3958 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003959
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003960 if (is_bypass_device(adap->pdev->device))
3961 adap->params.bypass = 1;
3962
Vipul Pandya636f9d32012-09-26 02:39:39 +00003963 /*
3964 * Grab some of our basic fundamental operating parameters.
3965 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003966#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303967 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3968 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003969
3970#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303971 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3972 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3973 FW_PARAMS_PARAM_Y_V(0) | \
3974 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003975
Vipul Pandya636f9d32012-09-26 02:39:39 +00003976 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003977 params[1] = FW_PARAM_PFVF(L2T_START);
3978 params[2] = FW_PARAM_PFVF(L2T_END);
3979 params[3] = FW_PARAM_PFVF(FILTER_START);
3980 params[4] = FW_PARAM_PFVF(FILTER_END);
3981 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303982 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003983 if (ret < 0)
3984 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003985 adap->sge.egr_start = val[0];
3986 adap->l2t_start = val[1];
3987 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003988 adap->tids.ftid_base = val[3];
3989 adap->tids.nftids = val[4] - val[3] + 1;
3990 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003991
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303992 /* qids (ingress/egress) returned from firmware can be anywhere
3993 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3994 * Hence driver needs to allocate memory for this range to
3995 * store the queue info. Get the highest IQFLINT/EQ index returned
3996 * in FW_EQ_*_CMD.alloc command.
3997 */
3998 params[0] = FW_PARAM_PFVF(EQ_END);
3999 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304000 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304001 if (ret < 0)
4002 goto bye;
4003 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
4004 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
4005
4006 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
4007 sizeof(*adap->sge.egr_map), GFP_KERNEL);
4008 if (!adap->sge.egr_map) {
4009 ret = -ENOMEM;
4010 goto bye;
4011 }
4012
4013 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
4014 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
4015 if (!adap->sge.ingr_map) {
4016 ret = -ENOMEM;
4017 goto bye;
4018 }
4019
4020 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304021 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304022 */
4023 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4024 sizeof(long), GFP_KERNEL);
4025 if (!adap->sge.starving_fl) {
4026 ret = -ENOMEM;
4027 goto bye;
4028 }
4029
4030 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4031 sizeof(long), GFP_KERNEL);
4032 if (!adap->sge.txq_maperr) {
4033 ret = -ENOMEM;
4034 goto bye;
4035 }
4036
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304037#ifdef CONFIG_DEBUG_FS
4038 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
4039 sizeof(long), GFP_KERNEL);
4040 if (!adap->sge.blocked_fl) {
4041 ret = -ENOMEM;
4042 goto bye;
4043 }
4044#endif
4045
Anish Bhattb5a02f52015-01-14 15:17:34 -08004046 params[0] = FW_PARAM_PFVF(CLIP_START);
4047 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304048 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08004049 if (ret < 0)
4050 goto bye;
4051 adap->clipt_start = val[0];
4052 adap->clipt_end = val[1];
4053
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304054 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
4055 * Classes supported by the hardware/firmware so we hard code it here
4056 * for now.
4057 */
4058 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
4059
Vipul Pandya636f9d32012-09-26 02:39:39 +00004060 /* query params related to active filter region */
4061 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
4062 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304063 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004064 /* If Active filter size is set we enable establishing
4065 * offload connection through firmware work request
4066 */
4067 if ((val[0] != val[1]) && (ret >= 0)) {
4068 adap->flags |= FW_OFLD_CONN;
4069 adap->tids.aftid_base = val[0];
4070 adap->tids.aftid_end = val[1];
4071 }
4072
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004073 /* If we're running on newer firmware, let it know that we're
4074 * prepared to deal with encapsulated CPL messages. Older
4075 * firmware won't understand this and we'll just get
4076 * unencapsulated messages ...
4077 */
4078 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
4079 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304080 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00004081
Vipul Pandya636f9d32012-09-26 02:39:39 +00004082 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304083 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
4084 * capability. Earlier versions of the firmware didn't have the
4085 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
4086 * permission to use ULPTX MEMWRITE DSGL.
4087 */
4088 if (is_t4(adap->params.chip)) {
4089 adap->params.ulptx_memwrite_dsgl = false;
4090 } else {
4091 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304092 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304093 1, params, val);
4094 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
4095 }
4096
Steve Wise086de572016-09-16 07:54:49 -07004097 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
4098 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
4099 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4100 1, params, val);
4101 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
4102
Kumar Sanghvi0ff90992017-10-18 20:49:13 +05304103 /* See if FW supports FW_FILTER2 work request */
4104 if (is_t4(adap->params.chip)) {
4105 adap->params.filter2_wr_support = 0;
4106 } else {
4107 params[0] = FW_PARAM_DEV(FILTER2_WR);
4108 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
4109 1, params, val);
4110 adap->params.filter2_wr_support = (ret == 0 && val[0] != 0);
4111 }
4112
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05304113 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004114 * Get device capabilities so we can determine what resources we need
4115 * to manage.
4116 */
4117 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05304118 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
4119 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05304120 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00004121 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
4122 &caps_cmd);
4123 if (ret < 0)
4124 goto bye;
4125
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304126 if (caps_cmd.ofldcaps ||
4127 (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER))) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004128 /* query offload-related parameters */
4129 params[0] = FW_PARAM_DEV(NTID);
4130 params[1] = FW_PARAM_PFVF(SERVER_START);
4131 params[2] = FW_PARAM_PFVF(SERVER_END);
4132 params[3] = FW_PARAM_PFVF(TDDP_START);
4133 params[4] = FW_PARAM_PFVF(TDDP_END);
4134 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304135 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004136 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004137 if (ret < 0)
4138 goto bye;
4139 adap->tids.ntids = val[0];
4140 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
4141 adap->tids.stid_base = val[1];
4142 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00004143 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08004144 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00004145 * region into two parts. Regular filters get 1/3rd and server
4146 * filters get 2/3rd part. This is only enabled if workarond
4147 * path is enabled.
4148 * 1. For regular filters.
4149 * 2. Server filter: This are special filters which are used
4150 * to redirect SYN packets to offload queue.
4151 */
4152 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
4153 adap->tids.sftid_base = adap->tids.ftid_base +
4154 DIV_ROUND_UP(adap->tids.nftids, 3);
4155 adap->tids.nsftids = adap->tids.nftids -
4156 DIV_ROUND_UP(adap->tids.nftids, 3);
4157 adap->tids.nftids = adap->tids.sftid_base -
4158 adap->tids.ftid_base;
4159 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004160 adap->vres.ddp.start = val[3];
4161 adap->vres.ddp.size = val[4] - val[3] + 1;
4162 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00004163
Kumar Sanghvi5c312542017-11-01 08:53:00 +05304164 if (caps_cmd.niccaps & htons(FW_CAPS_CONFIG_NIC_HASHFILTER)) {
4165 if (init_hash_filter(adap) < 0)
4166 goto bye;
4167 } else {
4168 adap->params.offload = 1;
4169 adap->num_ofld_uld += 1;
4170 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004171 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004172 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004173 params[0] = FW_PARAM_PFVF(STAG_START);
4174 params[1] = FW_PARAM_PFVF(STAG_END);
4175 params[2] = FW_PARAM_PFVF(RQ_START);
4176 params[3] = FW_PARAM_PFVF(RQ_END);
4177 params[4] = FW_PARAM_PFVF(PBL_START);
4178 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304179 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004180 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004181 if (ret < 0)
4182 goto bye;
4183 adap->vres.stag.start = val[0];
4184 adap->vres.stag.size = val[1] - val[0] + 1;
4185 adap->vres.rq.start = val[2];
4186 adap->vres.rq.size = val[3] - val[2] + 1;
4187 adap->vres.pbl.start = val[4];
4188 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004189
4190 params[0] = FW_PARAM_PFVF(SQRQ_START);
4191 params[1] = FW_PARAM_PFVF(SQRQ_END);
4192 params[2] = FW_PARAM_PFVF(CQ_START);
4193 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004194 params[4] = FW_PARAM_PFVF(OCQ_START);
4195 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304196 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304197 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00004198 if (ret < 0)
4199 goto bye;
4200 adap->vres.qp.start = val[0];
4201 adap->vres.qp.size = val[1] - val[0] + 1;
4202 adap->vres.cq.start = val[2];
4203 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004204 adap->vres.ocq.start = val[4];
4205 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304206
4207 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
4208 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304209 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05304210 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05304211 if (ret < 0) {
4212 adap->params.max_ordird_qp = 8;
4213 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
4214 ret = 0;
4215 } else {
4216 adap->params.max_ordird_qp = val[0];
4217 adap->params.max_ird_adapter = val[1];
4218 }
4219 dev_info(adap->pdev_dev,
4220 "max_ordird_qp %d max_ird_adapter %d\n",
4221 adap->params.max_ordird_qp,
4222 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304223 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004224 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004225 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004226 params[0] = FW_PARAM_PFVF(ISCSI_START);
4227 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304228 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
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.iscsi.start = val[0];
4233 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304234 /* LIO target and cxgb4i initiaitor */
4235 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004236 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304237 if (caps_cmd.cryptocaps) {
4238 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304239 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4240 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4241 params, val);
4242 if (ret < 0) {
4243 if (ret != -EINVAL)
4244 goto bye;
4245 } else {
4246 adap->vres.ncrypto_fc = val[0];
4247 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304248 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4249 adap->num_uld += 1;
4250 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004251#undef FW_PARAM_PFVF
4252#undef FW_PARAM_DEV
4253
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304254 /* The MTU/MSS Table is initialized by now, so load their values. If
4255 * we're initializing the adapter, then we'll make any modifications
4256 * we want to the MTU/MSS Table and also initialize the congestion
4257 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004258 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004259 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304260 if (state != DEV_STATE_INIT) {
4261 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004262
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304263 /* The default MTU Table contains values 1492 and 1500.
4264 * However, for TCP, it's better to have two values which are
4265 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4266 * This allows us to have a TCP Data Payload which is a
4267 * multiple of 8 regardless of what combination of TCP Options
4268 * are in use (always a multiple of 4 bytes) which is
4269 * important for performance reasons. For instance, if no
4270 * options are in use, then we have a 20-byte IP header and a
4271 * 20-byte TCP header. In this case, a 1500-byte MSS would
4272 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4273 * which is not a multiple of 8. So using an MSS of 1488 in
4274 * this case results in a TCP Data Payload of 1448 bytes which
4275 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4276 * Stamps have been negotiated, then an MTU of 1500 bytes
4277 * results in a TCP Data Payload of 1448 bytes which, as
4278 * above, is a multiple of 8 bytes ...
4279 */
4280 for (i = 0; i < NMTUS; i++)
4281 if (adap->params.mtus[i] == 1492) {
4282 adap->params.mtus[i] = 1488;
4283 break;
4284 }
4285
4286 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4287 adap->params.b_wnd);
4288 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304289 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004290 adap->flags |= FW_OK;
Rahul Lakkireddy5ccf9d02017-10-13 18:48:17 +05304291 t4_init_tp_params(adap, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004292 return 0;
4293
4294 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004295 * Something bad happened. If a command timed out or failed with EIO
4296 * FW does not operate within its spec or something catastrophic
4297 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004298 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004299bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304300 kfree(adap->sge.egr_map);
4301 kfree(adap->sge.ingr_map);
4302 kfree(adap->sge.starving_fl);
4303 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304304#ifdef CONFIG_DEBUG_FS
4305 kfree(adap->sge.blocked_fl);
4306#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004307 if (ret != -ETIMEDOUT && ret != -EIO)
4308 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004309 return ret;
4310}
4311
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004312/* EEH callbacks */
4313
4314static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4315 pci_channel_state_t state)
4316{
4317 int i;
4318 struct adapter *adap = pci_get_drvdata(pdev);
4319
4320 if (!adap)
4321 goto out;
4322
4323 rtnl_lock();
4324 adap->flags &= ~FW_OK;
4325 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004326 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004327 for_each_port(adap, i) {
4328 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004329 if (dev) {
4330 netif_device_detach(dev);
4331 netif_carrier_off(dev);
4332 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004333 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004334 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304335 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004336 if (adap->flags & FULL_INIT_DONE)
4337 cxgb_down(adap);
4338 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004339 if ((adap->flags & DEV_ENABLED)) {
4340 pci_disable_device(pdev);
4341 adap->flags &= ~DEV_ENABLED;
4342 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004343out: return state == pci_channel_io_perm_failure ?
4344 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4345}
4346
4347static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4348{
4349 int i, ret;
4350 struct fw_caps_config_cmd c;
4351 struct adapter *adap = pci_get_drvdata(pdev);
4352
4353 if (!adap) {
4354 pci_restore_state(pdev);
4355 pci_save_state(pdev);
4356 return PCI_ERS_RESULT_RECOVERED;
4357 }
4358
Gavin Shan144be3d2014-01-23 12:27:34 +08004359 if (!(adap->flags & DEV_ENABLED)) {
4360 if (pci_enable_device(pdev)) {
4361 dev_err(&pdev->dev, "Cannot reenable PCI "
4362 "device after reset\n");
4363 return PCI_ERS_RESULT_DISCONNECT;
4364 }
4365 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004366 }
4367
4368 pci_set_master(pdev);
4369 pci_restore_state(pdev);
4370 pci_save_state(pdev);
4371 pci_cleanup_aer_uncorrect_error_status(pdev);
4372
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304373 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004374 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304375 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004376 return PCI_ERS_RESULT_DISCONNECT;
4377 adap->flags |= FW_OK;
4378 if (adap_init1(adap, &c))
4379 return PCI_ERS_RESULT_DISCONNECT;
4380
4381 for_each_port(adap, i) {
4382 struct port_info *p = adap2pinfo(adap, i);
4383
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304384 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004385 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004386 if (ret < 0)
4387 return PCI_ERS_RESULT_DISCONNECT;
4388 p->viid = ret;
4389 p->xact_addr_filt = -1;
4390 }
4391
4392 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4393 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004394 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004395 if (cxgb_up(adap))
4396 return PCI_ERS_RESULT_DISCONNECT;
4397 return PCI_ERS_RESULT_RECOVERED;
4398}
4399
4400static void eeh_resume(struct pci_dev *pdev)
4401{
4402 int i;
4403 struct adapter *adap = pci_get_drvdata(pdev);
4404
4405 if (!adap)
4406 return;
4407
4408 rtnl_lock();
4409 for_each_port(adap, i) {
4410 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004411 if (dev) {
4412 if (netif_running(dev)) {
4413 link_start(dev);
4414 cxgb_set_rxmode(dev);
4415 }
4416 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004417 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004418 }
4419 rtnl_unlock();
4420}
4421
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004422static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004423 .error_detected = eeh_err_detected,
4424 .slot_reset = eeh_slot_reset,
4425 .resume = eeh_resume,
4426};
4427
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304428/* Return true if the Link Configuration supports "High Speeds" (those greater
4429 * than 1Gb/s).
4430 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304431static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004432{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304433 unsigned int speeds, high_speeds;
4434
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304435 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4436 high_speeds = speeds &
4437 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304438
4439 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004440}
4441
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004442/*
4443 * Perform default configuration of DMA queues depending on the number and type
4444 * of ports we found and the number of available CPUs. Most settings can be
4445 * modified by the admin prior to actual use.
4446 */
Bill Pemberton91744942012-12-03 09:23:02 -05004447static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004448{
4449 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304450 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004451#ifndef CONFIG_CHELSIO_T4_DCB
4452 int q10g = 0;
4453#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004454
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304455 /* Reduce memory usage in kdump environment, disable all offload.
4456 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304457 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304458 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304459 adap->params.crypto = 0;
4460 }
4461
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304462 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004463#ifdef CONFIG_CHELSIO_T4_DCB
4464 /* For Data Center Bridging support we need to be able to support up
4465 * to 8 Traffic Priorities; each of which will be assigned to its
4466 * own TX Queue in order to prevent Head-Of-Line Blocking.
4467 */
4468 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4469 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4470 MAX_ETH_QSETS, adap->params.nports * 8);
4471 BUG_ON(1);
4472 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004473
Anish Bhatt688848b2014-06-19 21:37:13 -07004474 for_each_port(adap, i) {
4475 struct port_info *pi = adap2pinfo(adap, i);
4476
4477 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304478 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004479 qidx += pi->nqsets;
4480 }
4481#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004482 /*
4483 * We default to 1 queue per non-10G port and up to # of cores queues
4484 * per 10G port.
4485 */
4486 if (n10g)
4487 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004488 if (q10g > netif_get_num_default_rss_queues())
4489 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004490
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304491 if (is_kdump_kernel())
4492 q10g = 1;
4493
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004494 for_each_port(adap, i) {
4495 struct port_info *pi = adap2pinfo(adap, i);
4496
4497 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304498 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004499 qidx += pi->nqsets;
4500 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004501#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004502
4503 s->ethqsets = qidx;
4504 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4505
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304506 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004507 /*
4508 * For offload we use 1 queue/channel if all ports are up to 1G,
4509 * otherwise we divide all available queues amongst the channels
4510 * capped by the number of available cores.
4511 */
4512 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304513 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304514 s->ofldqsets = roundup(i, adap->params.nports);
4515 } else {
4516 s->ofldqsets = adap->params.nports;
4517 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004518 }
4519
4520 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4521 struct sge_eth_rxq *r = &s->ethrxq[i];
4522
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304523 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004524 r->fl.size = 72;
4525 }
4526
4527 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4528 s->ethtxq[i].q.size = 1024;
4529
4530 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4531 s->ctrlq[i].q.size = 512;
4532
Atul Guptaa45695042017-07-04 16:46:20 +05304533 if (!is_t4(adap->params.chip))
4534 s->ptptxq.q.size = 8;
4535
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304536 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304537 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004538}
4539
4540/*
4541 * Reduce the number of Ethernet queues across all ports to at most n.
4542 * n provides at least one queue per port.
4543 */
Bill Pemberton91744942012-12-03 09:23:02 -05004544static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004545{
4546 int i;
4547 struct port_info *pi;
4548
4549 while (n < adap->sge.ethqsets)
4550 for_each_port(adap, i) {
4551 pi = adap2pinfo(adap, i);
4552 if (pi->nqsets > 1) {
4553 pi->nqsets--;
4554 adap->sge.ethqsets--;
4555 if (adap->sge.ethqsets <= n)
4556 break;
4557 }
4558 }
4559
4560 n = 0;
4561 for_each_port(adap, i) {
4562 pi = adap2pinfo(adap, i);
4563 pi->first_qset = n;
4564 n += pi->nqsets;
4565 }
4566}
4567
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304568static int get_msix_info(struct adapter *adap)
4569{
4570 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304571 unsigned int max_ingq = 0;
4572
4573 if (is_offload(adap))
4574 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4575 if (is_pci_uld(adap))
4576 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4577
4578 if (!max_ingq)
4579 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304580
4581 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4582 if (!msix_info)
4583 return -ENOMEM;
4584
4585 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4586 sizeof(long), GFP_KERNEL);
4587 if (!adap->msix_bmap_ulds.msix_bmap) {
4588 kfree(msix_info);
4589 return -ENOMEM;
4590 }
4591 spin_lock_init(&adap->msix_bmap_ulds.lock);
4592 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304593out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304594 return 0;
4595}
4596
4597static void free_msix_info(struct adapter *adap)
4598{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304599 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304600 return;
4601
4602 kfree(adap->msix_info_ulds);
4603 kfree(adap->msix_bmap_ulds.msix_bmap);
4604}
4605
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004606/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4607#define EXTRA_VECS 2
4608
Bill Pemberton91744942012-12-03 09:23:02 -05004609static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004610{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304611 int ofld_need = 0, uld_need = 0;
4612 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004613 struct sge *s = &adap->sge;
4614 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304615 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304616 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004617
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304618 if (is_pci_uld(adap))
4619 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4620 if (is_offload(adap))
4621 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304622 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304623 GFP_KERNEL);
4624 if (!entries)
4625 return -ENOMEM;
4626
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304627 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304628 if (get_msix_info(adap)) {
4629 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304630 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304631 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304632
4633 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004634 entries[i].entry = i;
4635
4636 want = s->max_ethqsets + EXTRA_VECS;
4637 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304638 want += adap->num_ofld_uld * s->ofldqsets;
4639 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004640 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304641 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304642 want += adap->num_uld * s->ofldqsets;
4643 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304644 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004645#ifdef CONFIG_CHELSIO_T4_DCB
4646 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4647 * each port.
4648 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304649 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004650#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304651 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004652#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304653 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4654 if (allocated < 0) {
4655 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4656 " not using MSI-X\n");
4657 kfree(entries);
4658 return allocated;
4659 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004660
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304661 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004662 * Every group gets its minimum requirement and NIC gets top
4663 * priority for leftovers.
4664 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304665 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004666 if (i < s->max_ethqsets) {
4667 s->max_ethqsets = i;
4668 if (i < s->ethqsets)
4669 reduce_ethqs(adap, i);
4670 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304671 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304672 if (allocated < want)
4673 s->nqs_per_uld = nchan;
4674 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304675 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304676 }
4677
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304678 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004679 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304680 if (is_uld(adap)) {
4681 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304682 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304683 adap->msix_info_ulds[j].idx = i;
4684 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304685 adap->msix_bmap_ulds.mapsize = j;
4686 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304687 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304688 "nic %d per uld %d\n",
4689 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004690
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304691 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004692 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004693}
4694
4695#undef EXTRA_VECS
4696
Bill Pemberton91744942012-12-03 09:23:02 -05004697static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004698{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304699 unsigned int i;
4700 int err;
4701
4702 err = t4_init_rss_mode(adap, adap->mbox);
4703 if (err)
4704 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004705
4706 for_each_port(adap, i) {
4707 struct port_info *pi = adap2pinfo(adap, i);
4708
4709 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4710 if (!pi->rss)
4711 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004712 }
4713 return 0;
4714}
4715
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304716static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4717 enum pci_bus_speed *speed,
4718 enum pcie_link_width *width)
4719{
4720 u32 lnkcap1, lnkcap2;
4721 int err1, err2;
4722
4723#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4724
4725 *speed = PCI_SPEED_UNKNOWN;
4726 *width = PCIE_LNK_WIDTH_UNKNOWN;
4727
4728 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4729 &lnkcap1);
4730 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4731 &lnkcap2);
4732 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4733 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4734 *speed = PCIE_SPEED_8_0GT;
4735 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4736 *speed = PCIE_SPEED_5_0GT;
4737 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4738 *speed = PCIE_SPEED_2_5GT;
4739 }
4740 if (!err1) {
4741 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4742 if (!lnkcap2) { /* pre-r3.0 */
4743 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4744 *speed = PCIE_SPEED_5_0GT;
4745 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4746 *speed = PCIE_SPEED_2_5GT;
4747 }
4748 }
4749
4750 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4751 return err1 ? err1 : err2 ? err2 : -EINVAL;
4752 return 0;
4753}
4754
4755static void cxgb4_check_pcie_caps(struct adapter *adap)
4756{
4757 enum pcie_link_width width, width_cap;
4758 enum pci_bus_speed speed, speed_cap;
4759
4760#define PCIE_SPEED_STR(speed) \
4761 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4762 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4763 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4764 "Unknown")
4765
4766 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4767 dev_warn(adap->pdev_dev,
4768 "Unable to determine PCIe device BW capabilities\n");
4769 return;
4770 }
4771
4772 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4773 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4774 dev_warn(adap->pdev_dev,
4775 "Unable to determine PCI Express bandwidth.\n");
4776 return;
4777 }
4778
4779 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4780 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4781 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4782 width, width_cap);
4783 if (speed < speed_cap || width < width_cap)
4784 dev_info(adap->pdev_dev,
4785 "A slot with more lanes and/or higher speed is "
4786 "suggested for optimal performance.\n");
4787}
4788
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304789/* Dump basic information about the adapter */
4790static void print_adapter_info(struct adapter *adapter)
4791{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304792 /* Hardware/Firmware/etc. Version/Revision IDs */
4793 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304794
4795 /* Software/Hardware configuration */
4796 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4797 is_offload(adapter) ? "R" : "",
4798 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4799 (adapter->flags & USING_MSI) ? "MSI" : ""),
4800 is_offload(adapter) ? "Offload" : "non-Offload");
4801}
4802
Bill Pemberton91744942012-12-03 09:23:02 -05004803static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004804{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004805 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004806 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004807 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004808 const struct port_info *pi = netdev_priv(dev);
4809 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004810
4811 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4812 spd = " 2.5 GT/s";
4813 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4814 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004815 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4816 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004817
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304818 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304819 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304820 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304821 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304822 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004823 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304824 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304825 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304826 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304827 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304828 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4829 bufp += sprintf(bufp, "50G/");
4830 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304831 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304832 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4833 bufp += sprintf(bufp, "200G/");
4834 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4835 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004836 if (bufp != buf)
4837 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304838 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004839
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304840 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4841 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004842}
4843
Dimitris Michailidis06546392010-07-11 12:01:16 +00004844/*
4845 * Free the following resources:
4846 * - memory used for tables
4847 * - MSI/MSI-X
4848 * - net devices
4849 * - resources FW is holding for us
4850 */
4851static void free_some_resources(struct adapter *adapter)
4852{
4853 unsigned int i;
4854
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05304855 kvfree(adapter->smt);
Michal Hocko752ade62017-05-08 15:57:27 -07004856 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304857 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004858 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304859 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304860 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304861 kfree(adapter->sge.egr_map);
4862 kfree(adapter->sge.ingr_map);
4863 kfree(adapter->sge.starving_fl);
4864 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304865#ifdef CONFIG_DEBUG_FS
4866 kfree(adapter->sge.blocked_fl);
4867#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004868 disable_msi(adapter);
4869
4870 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004871 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304872 struct port_info *pi = adap2pinfo(adapter, i);
4873
4874 if (pi->viid != 0)
4875 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4876 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004877 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004878 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004879 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004880 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304881 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004882}
4883
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004884#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004885#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004886 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004887#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004888
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304889static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4890{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304891 u16 device_id;
4892
4893 /* Retrieve adapter's device ID */
4894 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004895
4896 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304897 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004898 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304899 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004900 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304901 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004902 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304903 default:
4904 dev_err(&pdev->dev, "Device %d is not supported\n",
4905 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304906 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004907 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304908}
4909
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304910#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304911static void dummy_setup(struct net_device *dev)
4912{
4913 dev->type = ARPHRD_NONE;
4914 dev->mtu = 0;
4915 dev->hard_header_len = 0;
4916 dev->addr_len = 0;
4917 dev->tx_queue_len = 0;
4918 dev->flags |= IFF_NOARP;
4919 dev->priv_flags |= IFF_NO_QUEUE;
4920
4921 /* Initialize the device structure. */
4922 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4923 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004924 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304925}
4926
4927static int config_mgmt_dev(struct pci_dev *pdev)
4928{
4929 struct adapter *adap = pci_get_drvdata(pdev);
4930 struct net_device *netdev;
4931 struct port_info *pi;
4932 char name[IFNAMSIZ];
4933 int err;
4934
4935 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304936 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4937 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304938 if (!netdev)
4939 return -ENOMEM;
4940
4941 pi = netdev_priv(netdev);
4942 pi->adapter = adap;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304943 pi->tx_chan = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304944 SET_NETDEV_DEV(netdev, &pdev->dev);
4945
4946 adap->port[0] = netdev;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304947 pi->port_id = 0;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304948
4949 err = register_netdev(adap->port[0]);
4950 if (err) {
4951 pr_info("Unable to register VF mgmt netdev %s\n", name);
4952 free_netdev(adap->port[0]);
4953 adap->port[0] = NULL;
4954 return err;
4955 }
4956 return 0;
4957}
4958
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304959static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4960{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304961 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304962 int err = 0;
4963 int current_vfs = pci_num_vf(pdev);
4964 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304965
Hariprasad Shenai78294512016-08-11 21:06:23 +05304966 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304967 /* Check if cxgb4 is the MASTER and fw is initialized */
4968 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4969 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4970 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4971 dev_warn(&pdev->dev,
4972 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4973 return -EOPNOTSUPP;
4974 }
4975
4976 /* If any of the VF's is already assigned to Guest OS, then
4977 * SRIOV for the same cannot be modified
4978 */
4979 if (current_vfs && pci_vfs_assigned(pdev)) {
4980 dev_err(&pdev->dev,
4981 "Cannot modify SR-IOV while VFs are assigned\n");
4982 num_vfs = current_vfs;
4983 return num_vfs;
4984 }
4985
4986 /* Disable SRIOV when zero is passed.
4987 * One needs to disable SRIOV before modifying it, else
4988 * stack throws the below warning:
4989 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4990 */
4991 if (!num_vfs) {
4992 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304993 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304994 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304995 adap->port[0] = NULL;
4996 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304997 /* free VF resources */
4998 kfree(adap->vfinfo);
4999 adap->vfinfo = NULL;
5000 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305001 return num_vfs;
5002 }
5003
5004 if (num_vfs != current_vfs) {
5005 err = pci_enable_sriov(pdev, num_vfs);
5006 if (err)
5007 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305008
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305009 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305010 err = config_mgmt_dev(pdev);
5011 if (err)
5012 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305013 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305014
5015 adap->vfinfo = kcalloc(adap->num_vfs,
5016 sizeof(struct vf_info), GFP_KERNEL);
5017 if (adap->vfinfo)
5018 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305019 return num_vfs;
5020}
5021#endif
5022
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00005023static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005024{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005025 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005026 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005027 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005028 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305029 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305030 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305031 u32 whoami, pl_rev;
5032 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305033 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01005034#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305035 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01005036#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005037
5038 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
5039
5040 err = pci_request_regions(pdev, KBUILD_MODNAME);
5041 if (err) {
5042 /* Just info, some other driver may have claimed the device. */
5043 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
5044 return err;
5045 }
5046
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005047 err = pci_enable_device(pdev);
5048 if (err) {
5049 dev_err(&pdev->dev, "cannot enable PCI device\n");
5050 goto out_release_regions;
5051 }
5052
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305053 regs = pci_ioremap_bar(pdev, 0);
5054 if (!regs) {
5055 dev_err(&pdev->dev, "cannot map device registers\n");
5056 err = -ENOMEM;
5057 goto out_disable_device;
5058 }
5059
Hariprasad Shenai8203b502014-10-09 05:48:47 +05305060 err = t4_wait_dev_ready(regs);
5061 if (err < 0)
5062 goto out_unmap_bar0;
5063
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305064 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05305065 whoami = readl(regs + PL_WHOAMI_A);
5066 pl_rev = REV_G(readl(regs + PL_REV_A));
5067 chip = get_chip_type(pdev, pl_rev);
5068 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
5069 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305070 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05305071#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305072 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305073#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305074 pci_disable_device(pdev);
5075 pci_save_state(pdev); /* to restore SR-IOV later */
5076 goto sriov;
5077 }
5078
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005079 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005080 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005081 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
5082 if (err) {
5083 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
5084 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305085 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005086 }
5087 } else {
5088 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5089 if (err) {
5090 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305091 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005092 }
5093 }
5094
5095 pci_enable_pcie_error_reporting(pdev);
5096 pci_set_master(pdev);
5097 pci_save_state(pdev);
5098
5099 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5100 if (!adapter) {
5101 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305102 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005103 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05305104 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005105
Anish Bhatt29aaee62014-08-20 13:44:06 -07005106 adapter->workq = create_singlethread_workqueue("cxgb4");
5107 if (!adapter->workq) {
5108 err = -ENOMEM;
5109 goto out_free_adapter;
5110 }
5111
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305112 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5113 (sizeof(struct mbox_cmd) *
5114 T4_OS_LOG_MBOX_CMDS),
5115 GFP_KERNEL);
5116 if (!adapter->mbox_log) {
5117 err = -ENOMEM;
5118 goto out_free_adapter;
5119 }
5120 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
5121
Gavin Shan144be3d2014-01-23 12:27:34 +08005122 /* PCI device has been enabled */
5123 adapter->flags |= DEV_ENABLED;
5124
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305125 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005126 adapter->pdev = pdev;
5127 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305128 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05305129 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305130 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05305131 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005132 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
5133
Casey Leedomb0ba9d52017-08-15 11:23:26 +08005134 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
5135 * Ingress Packet Data to Free List Buffers in order to allow for
5136 * chipset performance optimizations between the Root Complex and
5137 * Memory Controllers. (Messages to the associated Ingress Queue
5138 * notifying new Packet Placement in the Free Lists Buffers will be
5139 * send without the Relaxed Ordering Attribute thus guaranteeing that
5140 * all preceding PCIe Transaction Layer Packets will be processed
5141 * first.) But some Root Complexes have various issues with Upstream
5142 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
5143 * The PCIe devices which under the Root Complexes will be cleared the
5144 * Relaxed Ordering bit in the configuration space, So we check our
5145 * PCIe configuration space to see if it's flagged with advice against
5146 * using Relaxed Ordering.
5147 */
5148 if (!pcie_relaxed_ordering_enabled(pdev))
5149 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
5150
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005151 spin_lock_init(&adapter->stats_lock);
5152 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07005153 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05305154 spin_lock_init(&adapter->mbox_lock);
5155
5156 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005157
5158 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05305159 INIT_WORK(&adapter->db_full_task, process_db_full);
5160 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005161
5162 err = t4_prep_adapter(adapter);
5163 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305164 goto out_free_adapter;
5165
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005166
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305167 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305168 s_qpp = (QUEUESPERPAGEPF0_S +
5169 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05305170 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05305171 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
5172 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005173 num_seg = PAGE_SIZE / SEGMENT_SIZE;
5174
5175 /* Each segment size is 128B. Write coalescing is enabled only
5176 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
5177 * queue is less no of segments that can be accommodated in
5178 * a page size.
5179 */
5180 if (qpp > num_seg) {
5181 dev_err(&pdev->dev,
5182 "Incorrect number of egress queues per page\n");
5183 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305184 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005185 }
5186 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
5187 pci_resource_len(pdev, 2));
5188 if (!adapter->bar2) {
5189 dev_err(&pdev->dev, "cannot map device bar2 region\n");
5190 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305191 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005192 }
5193 }
5194
Vipul Pandya636f9d32012-09-26 02:39:39 +00005195 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005196 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05305197#ifdef CONFIG_DEBUG_FS
5198 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
5199#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00005200 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005201 if (err)
5202 goto out_unmap_bar;
5203
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305204 /* configure SGE_STAT_CFG_A to read WC stats */
5205 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05305206 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
5207 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
5208 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05305209
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005210 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005211 netdev = alloc_etherdev_mq(sizeof(struct port_info),
5212 MAX_ETH_QSETS);
5213 if (!netdev) {
5214 err = -ENOMEM;
5215 goto out_free_dev;
5216 }
5217
5218 SET_NETDEV_DEV(netdev, &pdev->dev);
5219
5220 adapter->port[i] = netdev;
5221 pi = netdev_priv(netdev);
5222 pi->adapter = adapter;
5223 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005224 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005225 netdev->irq = pdev->irq;
5226
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005227 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5228 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5229 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305230 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5231 NETIF_F_HW_TC;
Ganesh Goudard0a12992018-01-10 18:15:26 +05305232
5233 if (CHELSIO_CHIP_VERSION(chip) > CHELSIO_T5)
5234 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
5235
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005236 if (highdma)
5237 netdev->hw_features |= NETIF_F_HIGHDMA;
5238 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005239 netdev->vlan_features = netdev->features & VLAN_FEAT;
5240
Jiri Pirko01789342011-08-16 06:29:00 +00005241 netdev->priv_flags |= IFF_UNICAST_FLT;
5242
Jarod Wilsond894be52016-10-20 13:55:16 -04005243 /* MTU range: 81 - 9600 */
Arjun Vynipadatha047fba2017-10-03 11:43:05 +05305244 netdev->min_mtu = 81; /* accommodate SACK */
Jarod Wilsond894be52016-10-20 13:55:16 -04005245 netdev->max_mtu = MAX_MTU;
5246
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005247 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005248#ifdef CONFIG_CHELSIO_T4_DCB
5249 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5250 cxgb4_dcb_state_init(netdev);
5251#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305252 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005253 }
5254
Rahul Lakkireddyad75b7d2017-10-13 18:48:13 +05305255 cxgb4_init_ethtool_dump(adapter);
5256
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005257 pci_set_drvdata(pdev, adapter);
5258
5259 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005260 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005261 if (err)
5262 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305263 } else if (adapter->params.nports == 1) {
5264 /* If we don't have a connection to the firmware -- possibly
5265 * because of an error -- grab the raw VPD parameters so we
5266 * can set the proper MAC Address on the debug network
5267 * interface that we've created.
5268 */
5269 u8 hw_addr[ETH_ALEN];
5270 u8 *na = adapter->params.vpd.na;
5271
5272 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5273 if (!err) {
5274 for (i = 0; i < ETH_ALEN; i++)
5275 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5276 hex2val(na[2 * i + 1]));
5277 t4_set_hw_addr(adapter, 0, hw_addr);
5278 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005279 }
5280
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305281 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005282 * soon as the first register_netdev completes.
5283 */
5284 cfg_queues(adapter);
5285
Kumar Sanghvi3bdb3762017-10-18 20:49:11 +05305286 adapter->smt = t4_init_smt();
5287 if (!adapter->smt) {
5288 /* We tolerate a lack of SMT, giving up some functionality */
5289 dev_warn(&pdev->dev, "could not allocate SMT, continuing\n");
5290 }
5291
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305292 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005293 if (!adapter->l2t) {
5294 /* We tolerate a lack of L2T, giving up some functionality */
5295 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5296 adapter->params.offload = 0;
5297 }
5298
Anish Bhattb5a02f52015-01-14 15:17:34 -08005299#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305300 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5301 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5302 /* CLIP functionality is not present in hardware,
5303 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005304 */
5305 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305306 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005307 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305308 } else {
5309 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5310 adapter->clipt_end);
5311 if (!adapter->clipt) {
5312 /* We tolerate a lack of clip_table, giving up
5313 * some functionality
5314 */
5315 dev_warn(&pdev->dev,
5316 "could not allocate Clip table, continuing\n");
5317 adapter->params.offload = 0;
5318 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005319 }
5320#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305321
5322 for_each_port(adapter, i) {
5323 pi = adap2pinfo(adapter, i);
5324 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5325 if (!pi->sched_tbl)
5326 dev_warn(&pdev->dev,
5327 "could not activate scheduling on port %d\n",
5328 i);
5329 }
5330
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305331 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005332 dev_warn(&pdev->dev, "could not allocate TID table, "
5333 "continuing\n");
5334 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305335 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305336 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305337 if (!adapter->tc_u32)
5338 dev_warn(&pdev->dev,
5339 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305340
Kumar Sanghvi79e6d462017-11-01 08:53:04 +05305341 if (cxgb4_init_tc_flower(adapter))
5342 dev_warn(&pdev->dev,
5343 "could not offload tc flower, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005344 }
5345
Kumar Sanghvi5c312542017-11-01 08:53:00 +05305346 if (is_offload(adapter) || is_hashfilter(adapter)) {
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305347 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5348 u32 hash_base, hash_reg;
5349
5350 if (chip <= CHELSIO_T5) {
5351 hash_reg = LE_DB_TID_HASHBASE_A;
5352 hash_base = t4_read_reg(adapter, hash_reg);
5353 adapter->tids.hash_base = hash_base / 4;
5354 } else {
5355 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5356 hash_base = t4_read_reg(adapter, hash_reg);
5357 adapter->tids.hash_base = hash_base;
5358 }
5359 }
5360 }
5361
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005362 /* See what interrupts we'll be using */
5363 if (msi > 1 && enable_msix(adapter) == 0)
5364 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305365 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005366 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305367 if (msi > 1)
5368 free_msix_info(adapter);
5369 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005370
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305371 /* check for PCI Express bandwidth capabiltites */
5372 cxgb4_check_pcie_caps(adapter);
5373
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005374 err = init_rss(adapter);
5375 if (err)
5376 goto out_free_dev;
5377
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005378 /*
5379 * The card is now ready to go. If any errors occur during device
5380 * registration we do not fail the whole card but rather proceed only
5381 * with the ports we manage to register successfully. However we must
5382 * register at least one net device.
5383 */
5384 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005385 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305386 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005387 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5388 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5389
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305390 netif_carrier_off(adapter->port[i]);
5391
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005392 err = register_netdev(adapter->port[i]);
5393 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005394 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005395 adapter->chan_map[pi->tx_chan] = i;
5396 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005397 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005398 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005399 dev_err(&pdev->dev, "could not register any net devices\n");
5400 goto out_free_dev;
5401 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005402 if (err) {
5403 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5404 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005405 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005406
5407 if (cxgb4_debugfs_root) {
5408 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5409 cxgb4_debugfs_root);
5410 setup_debugfs(adapter);
5411 }
5412
David S. Miller88c51002011-10-07 13:38:43 -04005413 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5414 pdev->needs_freset = 1;
5415
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305416 if (is_uld(adapter)) {
5417 mutex_lock(&uld_mutex);
5418 list_add_tail(&adapter->list_node, &adapter_list);
5419 mutex_unlock(&uld_mutex);
5420 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005421
Atul Gupta9c33e422017-07-04 16:46:21 +05305422 if (!is_t4(adapter->params.chip))
5423 cxgb4_ptp_init(adapter);
5424
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305425 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305426 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305427 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305428
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305429sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005430#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305431 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5432 if (!adapter) {
5433 err = -ENOMEM;
5434 goto free_pci_region;
5435 }
5436
Hariprasad Shenai78294512016-08-11 21:06:23 +05305437 adapter->pdev = pdev;
5438 adapter->pdev_dev = &pdev->dev;
5439 adapter->name = pci_name(pdev);
5440 adapter->mbox = func;
5441 adapter->pf = func;
5442 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305443 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305444 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5445 (sizeof(struct mbox_cmd) *
5446 T4_OS_LOG_MBOX_CMDS),
5447 GFP_KERNEL);
5448 if (!adapter->mbox_log) {
5449 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305450 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305451 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305452 spin_lock_init(&adapter->mbox_lock);
5453 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305454
5455 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5456 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5457 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5458 &v, &port_vec);
5459 if (err < 0) {
5460 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
Ganesh Goudard0417842017-06-09 19:26:24 +05305461 goto free_mbox_log;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305462 }
5463
5464 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305465 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005466 return 0;
5467
Ganesh Goudard0417842017-06-09 19:26:24 +05305468free_mbox_log:
5469 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305470 free_adapter:
5471 kfree(adapter);
5472 free_pci_region:
5473 iounmap(regs);
5474 pci_disable_sriov(pdev);
5475 pci_release_regions(pdev);
5476 return err;
5477#else
5478 return 0;
5479#endif
5480
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005481 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005482 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305483 if (adapter->flags & USING_MSIX)
5484 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305485 if (adapter->num_uld || adapter->num_ofld_uld)
5486 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005487 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305488 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005489 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005490 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005491 if (adapter->workq)
5492 destroy_workqueue(adapter->workq);
5493
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305494 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005495 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305496 out_unmap_bar0:
5497 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005498 out_disable_device:
5499 pci_disable_pcie_error_reporting(pdev);
5500 pci_disable_device(pdev);
5501 out_release_regions:
5502 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005503 return err;
5504}
5505
Bill Pemberton91744942012-12-03 09:23:02 -05005506static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005507{
5508 struct adapter *adapter = pci_get_drvdata(pdev);
5509
Hariprasad Shenai78294512016-08-11 21:06:23 +05305510 if (!adapter) {
5511 pci_release_regions(pdev);
5512 return;
5513 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005514
Ganesh Goudare1f61982017-09-21 12:50:47 +05305515 adapter->flags |= SHUTTING_DOWN;
5516
Hariprasad Shenai78294512016-08-11 21:06:23 +05305517 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005518 int i;
5519
Anish Bhatt29aaee62014-08-20 13:44:06 -07005520 /* Tear down per-adapter Work Queue first since it can contain
5521 * references to our adapter data structure.
5522 */
5523 destroy_workqueue(adapter->workq);
5524
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005525 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005526 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005527 t4_uld_clean_up(adapter);
5528 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005529
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305530 disable_interrupts(adapter);
5531
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005532 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005533 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005534 unregister_netdev(adapter->port[i]);
5535
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005536 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005537
Atul Gupta9c33e422017-07-04 16:46:21 +05305538 if (!is_t4(adapter->params.chip))
5539 cxgb4_ptp_stop(adapter);
5540
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005541 /* If we allocated filters, free up state associated with any
5542 * valid filters ...
5543 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305544 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005545
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005546 if (adapter->flags & FULL_INIT_DONE)
5547 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005548
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305549 if (adapter->flags & USING_MSIX)
5550 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305551 if (adapter->num_uld || adapter->num_ofld_uld)
5552 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005553 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005554#if IS_ENABLED(CONFIG_IPV6)
5555 t4_cleanup_clip_tbl(adapter);
5556#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005557 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305558 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005559 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005560 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005561 if ((adapter->flags & DEV_ENABLED)) {
5562 pci_disable_device(pdev);
5563 adapter->flags &= ~DEV_ENABLED;
5564 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005565 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305566 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005567 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005568 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305569 }
5570#ifdef CONFIG_PCI_IOV
5571 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305572 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305573 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305574 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305575 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305576 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305577 kfree(adapter);
5578 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005579 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305580 }
5581#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005582}
5583
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305584/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5585 * delivery. This is essentially a stripped down version of the PCI remove()
5586 * function where we do the minimal amount of work necessary to shutdown any
5587 * further activity.
5588 */
5589static void shutdown_one(struct pci_dev *pdev)
5590{
5591 struct adapter *adapter = pci_get_drvdata(pdev);
5592
5593 /* As with remove_one() above (see extended comment), we only want do
5594 * do cleanup on PCI Devices which went all the way through init_one()
5595 * ...
5596 */
5597 if (!adapter) {
5598 pci_release_regions(pdev);
5599 return;
5600 }
5601
Ganesh Goudare1f61982017-09-21 12:50:47 +05305602 adapter->flags |= SHUTTING_DOWN;
5603
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305604 if (adapter->pf == 4) {
5605 int i;
5606
5607 for_each_port(adapter, i)
5608 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5609 cxgb_close(adapter->port[i]);
5610
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005611 if (is_uld(adapter)) {
5612 detach_ulds(adapter);
5613 t4_uld_clean_up(adapter);
5614 }
5615
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305616 disable_interrupts(adapter);
5617 disable_msi(adapter);
5618
5619 t4_sge_stop(adapter);
5620 if (adapter->flags & FW_OK)
5621 t4_fw_bye(adapter, adapter->mbox);
5622 }
5623#ifdef CONFIG_PCI_IOV
5624 else {
5625 if (adapter->port[0])
5626 unregister_netdev(adapter->port[0]);
5627 iounmap(adapter->regs);
5628 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305629 kfree(adapter->mbox_log);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305630 kfree(adapter);
5631 pci_disable_sriov(pdev);
5632 pci_release_regions(pdev);
5633 }
5634#endif
5635}
5636
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005637static struct pci_driver cxgb4_driver = {
5638 .name = KBUILD_MODNAME,
5639 .id_table = cxgb4_pci_tbl,
5640 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005641 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305642 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305643#ifdef CONFIG_PCI_IOV
5644 .sriov_configure = cxgb4_iov_configure,
5645#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005646 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005647};
5648
5649static int __init cxgb4_init_module(void)
5650{
5651 int ret;
5652
5653 /* Debugfs support is optional, just warn if this fails */
5654 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5655 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005656 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005657
5658 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005659 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005660 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305661
Anish Bhatt1bb60372014-10-14 20:07:22 -07005662#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005663 if (!inet6addr_registered) {
5664 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5665 inet6addr_registered = true;
5666 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005667#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305668
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005669 return ret;
5670}
5671
5672static void __exit cxgb4_cleanup_module(void)
5673{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005674#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305675 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005676 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5677 inet6addr_registered = false;
5678 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005679#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005680 pci_unregister_driver(&cxgb4_driver);
5681 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005682}
5683
5684module_init(cxgb4_init_module);
5685module_exit(cxgb4_cleanup_module);