blob: aa93ae95d3b928cad1eb2161037f73810438174c [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>
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000068
69#include "cxgb4.h"
Rahul Lakkireddyd57fd6c2016-09-20 17:13:06 +053070#include "cxgb4_filter.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000071#include "t4_regs.h"
Hariprasad Shenaif612b812015-01-05 16:30:43 +053072#include "t4_values.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000073#include "t4_msg.h"
74#include "t4fw_api.h"
Hariprasad Shenaicd6c2f12015-01-27 20:12:52 +053075#include "t4fw_version.h"
Anish Bhatt688848b2014-06-19 21:37:13 -070076#include "cxgb4_dcb.h"
Hariprasad Shenaifd88b312014-11-07 09:35:23 +053077#include "cxgb4_debugfs.h"
Anish Bhattb5a02f52015-01-14 15:17:34 -080078#include "clip_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000079#include "l2t.h"
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +053080#include "sched.h"
Rahul Lakkireddyd8931842016-09-20 17:13:09 +053081#include "cxgb4_tc_u32.h"
Kumar Sanghvi6a345b32017-09-21 23:41:13 +053082#include "cxgb4_tc_flower.h"
Atul Guptaa45695042017-07-04 16:46:20 +053083#include "cxgb4_ptp.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000084
Hariprasad Shenai812034f2015-04-06 20:23:23 +053085char cxgb4_driver_name[] = KBUILD_MODNAME;
86
Vipul Pandya01bcca62013-07-04 16:10:46 +053087#ifdef DRV_VERSION
88#undef DRV_VERSION
89#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000090#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053091const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053092#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000093
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000094#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
95 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
96 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
97
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +053098/* Macros needed to support the PCI Device ID Table ...
99 */
100#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +0530101 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530102#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000103
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530104/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
105 * called for both.
106 */
107#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
108
109#define CH_PCI_ID_TABLE_ENTRY(devid) \
110 {PCI_VDEVICE(CHELSIO, (devid)), 4}
111
112#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
113 { 0, } \
114 }
115
116#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000117
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530118#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000119#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530120#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530121#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000122#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530123#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530124#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
125#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
126#define PHY_AQ1202_DEVICEID 0x4409
127#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000128
129MODULE_DESCRIPTION(DRV_DESC);
130MODULE_AUTHOR("Chelsio Communications");
131MODULE_LICENSE("Dual BSD/GPL");
132MODULE_VERSION(DRV_VERSION);
133MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530134MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000135MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530136MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000137
Vipul Pandya636f9d32012-09-26 02:39:39 +0000138/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000139 * The driver uses the best interrupt scheme available on a platform in the
140 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
141 * of these schemes the driver may consider as follows:
142 *
143 * msi = 2: choose from among all three options
144 * msi = 1: only consider MSI and INTx interrupts
145 * msi = 0: force INTx interrupts
146 */
147static int msi = 2;
148
149module_param(msi, int, 0644);
150MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
151
152/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000153 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
154 * offset by 2 bytes in order to have the IP headers line up on 4-byte
155 * boundaries. This is a requirement for many architectures which will throw
156 * a machine check fault if an attempt is made to access one of the 4-byte IP
157 * header fields on a non-4-byte boundary. And it's a major performance issue
158 * even on some architectures which allow it like some implementations of the
159 * x86 ISA. However, some architectures don't mind this and for some very
160 * edge-case performance sensitive applications (like forwarding large volumes
161 * of small packets), setting this DMA offset to 0 will decrease the number of
162 * PCI-E Bus transfers enough to measurably affect performance.
163 */
164static int rx_dma_offset = 2;
165
Anish Bhatt688848b2014-06-19 21:37:13 -0700166/* TX Queue select used to determine what algorithm to use for selecting TX
167 * queue. Select between the kernel provided function (select_queue=0) or user
168 * cxgb_select_queue function (select_queue=1)
169 *
170 * Default: select_queue=0
171 */
172static int select_queue;
173module_param(select_queue, int, 0644);
174MODULE_PARM_DESC(select_queue,
175 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
176
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000177static struct dentry *cxgb4_debugfs_root;
178
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530179LIST_HEAD(adapter_list);
180DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000181
182static void link_report(struct net_device *dev)
183{
184 if (!netif_carrier_ok(dev))
185 netdev_info(dev, "link down\n");
186 else {
187 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
188
Hariprasad Shenai85412252015-10-01 13:48:48 +0530189 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000190 const struct port_info *p = netdev_priv(dev);
191
192 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000193 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000194 s = "100Mbps";
195 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530196 case 1000:
197 s = "1Gbps";
198 break;
199 case 10000:
200 s = "10Gbps";
201 break;
202 case 25000:
203 s = "25Gbps";
204 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000205 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530206 s = "40Gbps";
207 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530208 case 100000:
209 s = "100Gbps";
210 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530211 default:
212 pr_info("%s: unsupported speed: %d\n",
213 dev->name, p->link_cfg.speed);
214 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000215 }
216
217 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
218 fc[p->link_cfg.fc]);
219 }
220}
221
Anish Bhatt688848b2014-06-19 21:37:13 -0700222#ifdef CONFIG_CHELSIO_T4_DCB
223/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
224static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
225{
226 struct port_info *pi = netdev_priv(dev);
227 struct adapter *adap = pi->adapter;
228 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
229 int i;
230
231 /* We use a simple mapping of Port TX Queue Index to DCB
232 * Priority when we're enabling DCB.
233 */
234 for (i = 0; i < pi->nqsets; i++, txq++) {
235 u32 name, value;
236 int err;
237
Hariprasad Shenai51678652014-11-21 12:52:02 +0530238 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
239 FW_PARAMS_PARAM_X_V(
240 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
241 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700242 value = enable ? i : 0xffffffff;
243
244 /* Since we can be called while atomic (from "interrupt
245 * level") we need to issue the Set Parameters Commannd
246 * without sleeping (timeout < 0).
247 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530248 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530249 &name, &value,
250 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700251
252 if (err)
253 dev_err(adap->pdev_dev,
254 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
255 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700256 else
257 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700258 }
259}
Anish Bhatt688848b2014-06-19 21:37:13 -0700260
Baoyou Xie50935852016-09-25 14:10:09 +0800261static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530262{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530263 struct port_info *pi = netdev_priv(dev);
264
265 if (!pi->dcb.enabled)
266 return 0;
267
268 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
269 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530270}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200271#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530272
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000273void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
274{
275 struct net_device *dev = adapter->port[port_id];
276
277 /* Skip changes from disabled ports. */
278 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
279 if (link_stat)
280 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700281 else {
282#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530283 if (cxgb4_dcb_enabled(dev)) {
284 cxgb4_dcb_state_init(dev);
285 dcb_tx_queue_prio_enable(dev, false);
286 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700287#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000288 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700289 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000290
291 link_report(dev);
292 }
293}
294
295void t4_os_portmod_changed(const struct adapter *adap, int port_id)
296{
297 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000298 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000299 };
300
301 const struct net_device *dev = adap->port[port_id];
302 const struct port_info *pi = netdev_priv(dev);
303
304 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
305 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000306 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000307 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530308 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
309 netdev_info(dev, "%s: unsupported port module inserted\n",
310 dev->name);
311 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
312 netdev_info(dev, "%s: unknown port module inserted\n",
313 dev->name);
314 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
315 netdev_info(dev, "%s: transceiver module error\n", dev->name);
316 else
317 netdev_info(dev, "%s: unknown module type %d inserted\n",
318 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000319}
320
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530321int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
322module_param(dbfifo_int_thresh, int, 0644);
323MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
324
Vipul Pandya404d9e32012-10-08 02:59:43 +0000325/*
326 * usecs to sleep while draining the dbfifo
327 */
328static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530329module_param(dbfifo_drain_delay, int, 0644);
330MODULE_PARM_DESC(dbfifo_drain_delay,
331 "usecs to sleep while draining the dbfifo");
332
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530333static inline int cxgb4_set_addr_hash(struct port_info *pi)
334{
335 struct adapter *adap = pi->adapter;
336 u64 vec = 0;
337 bool ucast = false;
338 struct hash_mac_addr *entry;
339
340 /* Calculate the hash vector for the updated list and program it */
341 list_for_each_entry(entry, &adap->mac_hlist, list) {
342 ucast |= is_unicast_ether_addr(entry->addr);
343 vec |= (1ULL << hash_mac_addr(entry->addr));
344 }
345 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
346 vec, false);
347}
348
349static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
350{
351 struct port_info *pi = netdev_priv(netdev);
352 struct adapter *adap = pi->adapter;
353 int ret;
354 u64 mhash = 0;
355 u64 uhash = 0;
356 bool free = false;
357 bool ucast = is_unicast_ether_addr(mac_addr);
358 const u8 *maclist[1] = {mac_addr};
359 struct hash_mac_addr *new_entry;
360
361 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
362 NULL, ucast ? &uhash : &mhash, false);
363 if (ret < 0)
364 goto out;
365 /* if hash != 0, then add the addr to hash addr list
366 * so on the end we will calculate the hash for the
367 * list and program it
368 */
369 if (uhash || mhash) {
370 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
371 if (!new_entry)
372 return -ENOMEM;
373 ether_addr_copy(new_entry->addr, mac_addr);
374 list_add_tail(&new_entry->list, &adap->mac_hlist);
375 ret = cxgb4_set_addr_hash(pi);
376 }
377out:
378 return ret < 0 ? ret : 0;
379}
380
381static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
382{
383 struct port_info *pi = netdev_priv(netdev);
384 struct adapter *adap = pi->adapter;
385 int ret;
386 const u8 *maclist[1] = {mac_addr};
387 struct hash_mac_addr *entry, *tmp;
388
389 /* If the MAC address to be removed is in the hash addr
390 * list, delete it from the list and update hash vector
391 */
392 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
393 if (ether_addr_equal(entry->addr, mac_addr)) {
394 list_del(&entry->list);
395 kfree(entry);
396 return cxgb4_set_addr_hash(pi);
397 }
398 }
399
400 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
401 return ret < 0 ? -EINVAL : 0;
402}
403
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000404/*
405 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
406 * If @mtu is -1 it is left unchanged.
407 */
408static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
409{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000410 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530411 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000412
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530413 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
414 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530415
416 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
417 (dev->flags & IFF_PROMISC) ? 1 : 0,
418 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
419 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000420}
421
422/**
423 * link_start - enable a port
424 * @dev: the port to enable
425 *
426 * Performs the MAC and PHY actions needed to enable a port.
427 */
428static int link_start(struct net_device *dev)
429{
430 int ret;
431 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530432 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000433
434 /*
435 * We do not set address filters and promiscuity here, the stack does
436 * that step explicitly.
437 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000438 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000439 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000440 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000441 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000442 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000443 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000444 if (ret >= 0) {
445 pi->xact_addr_filt = ret;
446 ret = 0;
447 }
448 }
449 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530450 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000451 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700452 if (ret == 0) {
453 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700454 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
455 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700456 local_bh_enable();
457 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700458
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000459 return ret;
460}
461
Anish Bhatt688848b2014-06-19 21:37:13 -0700462#ifdef CONFIG_CHELSIO_T4_DCB
463/* Handle a Data Center Bridging update message from the firmware. */
464static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
465{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530466 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530467 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700468 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
469 int new_dcb_enabled;
470
471 cxgb4_dcb_handle_fw_update(adap, pcmd);
472 new_dcb_enabled = cxgb4_dcb_enabled(dev);
473
474 /* If the DCB has become enabled or disabled on the port then we're
475 * going to need to set up/tear down DCB Priority parameters for the
476 * TX Queues associated with the port.
477 */
478 if (new_dcb_enabled != old_dcb_enabled)
479 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
480}
481#endif /* CONFIG_CHELSIO_T4_DCB */
482
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000483/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000484 */
485static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
486 const struct pkt_gl *gl)
487{
488 u8 opcode = ((const struct rss_header *)rsp)->opcode;
489
490 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000491
492 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
493 */
494 if (unlikely(opcode == CPL_FW4_MSG &&
495 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
496 rsp++;
497 opcode = ((const struct rss_header *)rsp)->opcode;
498 rsp++;
499 if (opcode != CPL_SGE_EGR_UPDATE) {
500 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
501 , opcode);
502 goto out;
503 }
504 }
505
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000506 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
507 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800508 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000509 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000510
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000511 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000512 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530513 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000514 struct sge_eth_txq *eq;
515
516 eq = container_of(txq, struct sge_eth_txq, q);
517 netif_tx_wake_queue(eq->txq);
518 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530519 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000520
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530521 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000522 tasklet_schedule(&oq->qresume_tsk);
523 }
524 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
525 const struct cpl_fw6_msg *p = (void *)rsp;
526
Anish Bhatt688848b2014-06-19 21:37:13 -0700527#ifdef CONFIG_CHELSIO_T4_DCB
528 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530529 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700530 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530531 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700532
533 if (cmd == FW_PORT_CMD &&
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530534 (action == FW_PORT_ACTION_GET_PORT_INFO ||
535 action == FW_PORT_ACTION_GET_PORT_INFO32)) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530536 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700537 be32_to_cpu(pcmd->op_to_portid));
Ganesh Goudarc3168ca2017-08-20 14:15:51 +0530538 struct net_device *dev;
539 int dcbxdis, state_input;
540
541 dev = q->adap->port[q->adap->chan_map[port]];
542 dcbxdis = (action == FW_PORT_ACTION_GET_PORT_INFO
543 ? !!(pcmd->u.info.dcbxdis_pkd &
544 FW_PORT_CMD_DCBXDIS_F)
545 : !!(pcmd->u.info32.lstatus32_to_cbllen32 &
546 FW_PORT_CMD_DCBXDIS32_F));
547 state_input = (dcbxdis
548 ? CXGB4_DCB_INPUT_FW_DISABLED
549 : CXGB4_DCB_INPUT_FW_ENABLED);
Anish Bhatt688848b2014-06-19 21:37:13 -0700550
551 cxgb4_dcb_state_fsm(dev, state_input);
552 }
553
554 if (cmd == FW_PORT_CMD &&
555 action == FW_PORT_ACTION_L2_DCB_CFG)
556 dcb_rpl(q->adap, pcmd);
557 else
558#endif
559 if (p->type == 0)
560 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000561 } else if (opcode == CPL_L2T_WRITE_RPL) {
562 const struct cpl_l2t_write_rpl *p = (void *)rsp;
563
564 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000565 } else if (opcode == CPL_SET_TCB_RPL) {
566 const struct cpl_set_tcb_rpl *p = (void *)rsp;
567
568 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000569 } else
570 dev_err(q->adap->pdev_dev,
571 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000572out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000573 return 0;
574}
575
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000576static void disable_msi(struct adapter *adapter)
577{
578 if (adapter->flags & USING_MSIX) {
579 pci_disable_msix(adapter->pdev);
580 adapter->flags &= ~USING_MSIX;
581 } else if (adapter->flags & USING_MSI) {
582 pci_disable_msi(adapter->pdev);
583 adapter->flags &= ~USING_MSI;
584 }
585}
586
587/*
588 * Interrupt handler for non-data events used with MSI-X.
589 */
590static irqreturn_t t4_nondata_intr(int irq, void *cookie)
591{
592 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530593 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000594
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530595 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000596 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530597 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000598 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530599 if (adap->flags & MASTER_PF)
600 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000601 return IRQ_HANDLED;
602}
603
604/*
605 * Name the MSI-X interrupts.
606 */
607static void name_msix_vecs(struct adapter *adap)
608{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000609 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000610
611 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000612 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000613
614 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000615 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
616 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000617
618 /* Ethernet queues */
619 for_each_port(adap, j) {
620 struct net_device *d = adap->port[j];
621 const struct port_info *pi = netdev_priv(d);
622
Dimitris Michailidisba278162010-12-14 21:36:50 +0000623 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000624 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
625 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000626 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000627}
628
629static int request_msix_queue_irqs(struct adapter *adap)
630{
631 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530632 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530633 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000634
635 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
636 adap->msix_info[1].desc, &s->fw_evtq);
637 if (err)
638 return err;
639
640 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000641 err = request_irq(adap->msix_info[msi_index].vec,
642 t4_sge_intr_msix, 0,
643 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000644 &s->ethrxq[ethqidx].rspq);
645 if (err)
646 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000647 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000648 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000649 return 0;
650
651unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000652 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000653 free_irq(adap->msix_info[--msi_index].vec,
654 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000655 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
656 return err;
657}
658
659static void free_msix_queue_irqs(struct adapter *adap)
660{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000661 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000662 struct sge *s = &adap->sge;
663
664 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
665 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000666 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000667}
668
669/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530670 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000671 * @pi: the port
672 * @queues: array of queue indices for RSS
673 *
674 * Sets up the portion of the HW RSS table for the port's VI to distribute
675 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530676 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000677 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530678int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000679{
680 u16 *rss;
681 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530682 struct adapter *adapter = pi->adapter;
683 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000684
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530685 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000686 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
687 if (!rss)
688 return -ENOMEM;
689
690 /* map the queue indices to queue ids */
691 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530692 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000693
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530694 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000695 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530696 /* If Tunnel All Lookup isn't specified in the global RSS
697 * Configuration, then we need to specify a default Ingress
698 * Queue for any ingress packets which aren't hashed. We'll
699 * use our first ingress queue ...
700 */
701 if (!err)
702 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
703 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
704 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
705 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
706 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
707 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
708 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000709 kfree(rss);
710 return err;
711}
712
713/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000714 * setup_rss - configure RSS
715 * @adap: the adapter
716 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000717 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000718 */
719static int setup_rss(struct adapter *adap)
720{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530721 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000722
723 for_each_port(adap, i) {
724 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000725
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530726 /* Fill default values with equal distribution */
727 for (j = 0; j < pi->rss_size; j++)
728 pi->rss[j] = j % pi->nqsets;
729
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530730 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000731 if (err)
732 return err;
733 }
734 return 0;
735}
736
737/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000738 * Return the channel of the ingress queue with the given qid.
739 */
740static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
741{
742 qid -= p->ingr_start;
743 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
744}
745
746/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000747 * Wait until all NAPI handlers are descheduled.
748 */
749static void quiesce_rx(struct adapter *adap)
750{
751 int i;
752
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530753 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000754 struct sge_rspq *q = adap->sge.ingr_map[i];
755
Eric Dumazet5226b7912017-02-02 11:44:27 -0800756 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000757 napi_disable(&q->napi);
758 }
759}
760
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530761/* Disable interrupt and napi handler */
762static void disable_interrupts(struct adapter *adap)
763{
764 if (adap->flags & FULL_INIT_DONE) {
765 t4_intr_disable(adap);
766 if (adap->flags & USING_MSIX) {
767 free_msix_queue_irqs(adap);
768 free_irq(adap->msix_info[0].vec, adap);
769 } else {
770 free_irq(adap->pdev->irq, adap);
771 }
772 quiesce_rx(adap);
773 }
774}
775
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000776/*
777 * Enable NAPI scheduling and interrupt generation for all Rx queues.
778 */
779static void enable_rx(struct adapter *adap)
780{
781 int i;
782
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530783 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000784 struct sge_rspq *q = adap->sge.ingr_map[i];
785
786 if (!q)
787 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800788 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000789 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800790
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000791 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530792 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
793 SEINTARM_V(q->intr_params) |
794 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000795 }
796}
797
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530798
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530799static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000800{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000801 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530802 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000803
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530804 bitmap_zero(s->starving_fl, s->egr_sz);
805 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000806
807 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530808 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000809 else {
810 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530811 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000812 if (err)
813 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530814 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000815 }
816
817 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530818 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530819 if (err)
820 t4_free_sge_resources(adap);
821 return err;
822}
823
824/**
825 * setup_sge_queues - configure SGE Tx/Rx/response queues
826 * @adap: the adapter
827 *
828 * Determines how many sets of SGE queues to use and initializes them.
829 * We support multiple queue sets per port if we have MSI-X, otherwise
830 * just one queue set per port.
831 */
832static int setup_sge_queues(struct adapter *adap)
833{
834 int err, i, j;
835 struct sge *s = &adap->sge;
Ganesh Goudard427cae2017-06-16 15:36:09 +0530836 struct sge_uld_rxq_info *rxq_info = NULL;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530837 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000838
Ganesh Goudard427cae2017-06-16 15:36:09 +0530839 if (is_uld(adap))
840 rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
841
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000842 for_each_port(adap, i) {
843 struct net_device *dev = adap->port[i];
844 struct port_info *pi = netdev_priv(dev);
845 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
846 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
847
848 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530849 if (adap->msi_idx > 0)
850 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000851 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530852 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530853 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530854 NULL,
Arjun Vynipadath193c4c22017-06-23 19:14:36 +0530855 t4_get_tp_ch_map(adap,
856 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000857 if (err)
858 goto freeout;
859 q->rspq.idx = j;
860 memset(&q->stats, 0, sizeof(q->stats));
861 }
862 for (j = 0; j < pi->nqsets; j++, t++) {
863 err = t4_sge_alloc_eth_txq(adap, t, dev,
864 netdev_get_tx_queue(dev, j),
865 s->fw_evtq.cntxt_id);
866 if (err)
867 goto freeout;
868 }
869 }
870
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000871 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530872 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000873 * have RDMA queues, and that's the right value.
874 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530875 if (rxq_info)
876 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
877
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000878 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530879 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000880 if (err)
881 goto freeout;
882 }
883
Atul Guptaa45695042017-07-04 16:46:20 +0530884 if (!is_t4(adap->params.chip)) {
885 err = t4_sge_alloc_eth_txq(adap, &s->ptptxq, adap->port[0],
886 netdev_get_tx_queue(adap->port[0], 0)
887 , s->fw_evtq.cntxt_id);
888 if (err)
889 goto freeout;
890 }
891
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530892 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530893 MPS_TRC_RSS_CONTROL_A :
894 MPS_T5_TRC_RSS_CONTROL_A,
895 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
896 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000897 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530898freeout:
899 t4_free_sge_resources(adap);
900 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000901}
902
Anish Bhatt688848b2014-06-19 21:37:13 -0700903static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
904 void *accel_priv, select_queue_fallback_t fallback)
905{
906 int txq;
907
908#ifdef CONFIG_CHELSIO_T4_DCB
909 /* If a Data Center Bridging has been successfully negotiated on this
910 * link then we'll use the skb's priority to map it to a TX Queue.
911 * The skb's priority is determined via the VLAN Tag Priority Code
912 * Point field.
913 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530914 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700915 u16 vlan_tci;
916 int err;
917
918 err = vlan_get_tag(skb, &vlan_tci);
919 if (unlikely(err)) {
920 if (net_ratelimit())
921 netdev_warn(dev,
922 "TX Packet without VLAN Tag on DCB Link\n");
923 txq = 0;
924 } else {
925 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530926#ifdef CONFIG_CHELSIO_T4_FCOE
927 if (skb->protocol == htons(ETH_P_FCOE))
928 txq = skb->priority & 0x7;
929#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700930 }
931 return txq;
932 }
933#endif /* CONFIG_CHELSIO_T4_DCB */
934
935 if (select_queue) {
936 txq = (skb_rx_queue_recorded(skb)
937 ? skb_get_rx_queue(skb)
938 : smp_processor_id());
939
940 while (unlikely(txq >= dev->real_num_tx_queues))
941 txq -= dev->real_num_tx_queues;
942
943 return txq;
944 }
945
946 return fallback(dev, skb) % dev->real_num_tx_queues;
947}
948
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000949static int closest_timer(const struct sge *s, int time)
950{
951 int i, delta, match = 0, min_delta = INT_MAX;
952
953 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
954 delta = time - s->timer_val[i];
955 if (delta < 0)
956 delta = -delta;
957 if (delta < min_delta) {
958 min_delta = delta;
959 match = i;
960 }
961 }
962 return match;
963}
964
965static int closest_thres(const struct sge *s, int thres)
966{
967 int i, delta, match = 0, min_delta = INT_MAX;
968
969 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
970 delta = thres - s->counter_val[i];
971 if (delta < 0)
972 delta = -delta;
973 if (delta < min_delta) {
974 min_delta = delta;
975 match = i;
976 }
977 }
978 return match;
979}
980
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000981/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530982 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000983 * @q: the Rx queue
984 * @us: the hold-off time in us, or 0 to disable timer
985 * @cnt: the hold-off packet count, or 0 to disable counter
986 *
987 * Sets an Rx queue's interrupt hold-off time and packet count. At least
988 * one of the two needs to be enabled for the queue to generate interrupts.
989 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530990int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
991 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000992{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +0530993 struct adapter *adap = q->adap;
994
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000995 if ((us | cnt) == 0)
996 cnt = 1;
997
998 if (cnt) {
999 int err;
1000 u32 v, new_idx;
1001
1002 new_idx = closest_thres(&adap->sge, cnt);
1003 if (q->desc && q->pktcnt_idx != new_idx) {
1004 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +05301005 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1006 FW_PARAMS_PARAM_X_V(
1007 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1008 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301009 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1010 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001011 if (err)
1012 return err;
1013 }
1014 q->pktcnt_idx = new_idx;
1015 }
1016
1017 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +05301018 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001019 return 0;
1020}
1021
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001022static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001023{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001024 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001025 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001026 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001027
Patrick McHardyf6469682013-04-19 02:04:27 +00001028 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001029 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001030
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301031 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001032 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001033 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001034 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001035 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001036 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001037}
1038
Bill Pemberton91744942012-12-03 09:23:02 -05001039static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001040{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001041 if (IS_ERR_OR_NULL(adap->debugfs_root))
1042 return -1;
1043
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301044#ifdef CONFIG_DEBUG_FS
1045 t4_setup_debugfs(adap);
1046#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001047 return 0;
1048}
1049
1050/*
1051 * upper-layer driver support
1052 */
1053
1054/*
1055 * Allocate an active-open TID and set it to the supplied value.
1056 */
1057int cxgb4_alloc_atid(struct tid_info *t, void *data)
1058{
1059 int atid = -1;
1060
1061 spin_lock_bh(&t->atid_lock);
1062 if (t->afree) {
1063 union aopen_entry *p = t->afree;
1064
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001065 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001066 t->afree = p->next;
1067 p->data = data;
1068 t->atids_in_use++;
1069 }
1070 spin_unlock_bh(&t->atid_lock);
1071 return atid;
1072}
1073EXPORT_SYMBOL(cxgb4_alloc_atid);
1074
1075/*
1076 * Release an active-open TID.
1077 */
1078void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1079{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001080 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001081
1082 spin_lock_bh(&t->atid_lock);
1083 p->next = t->afree;
1084 t->afree = p;
1085 t->atids_in_use--;
1086 spin_unlock_bh(&t->atid_lock);
1087}
1088EXPORT_SYMBOL(cxgb4_free_atid);
1089
1090/*
1091 * Allocate a server TID and set it to the supplied value.
1092 */
1093int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1094{
1095 int stid;
1096
1097 spin_lock_bh(&t->stid_lock);
1098 if (family == PF_INET) {
1099 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1100 if (stid < t->nstids)
1101 __set_bit(stid, t->stid_bmap);
1102 else
1103 stid = -1;
1104 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301105 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001106 if (stid < 0)
1107 stid = -1;
1108 }
1109 if (stid >= 0) {
1110 t->stid_tab[stid].data = data;
1111 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301112 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1113 * This is equivalent to 4 TIDs. With CLIP enabled it
1114 * needs 2 TIDs.
1115 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301116 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301117 t->stids_in_use += 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301118 t->v6_stids_in_use += 2;
1119 } else {
1120 t->stids_in_use++;
1121 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001122 }
1123 spin_unlock_bh(&t->stid_lock);
1124 return stid;
1125}
1126EXPORT_SYMBOL(cxgb4_alloc_stid);
1127
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001128/* Allocate a server filter TID and set it to the supplied value.
1129 */
1130int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1131{
1132 int stid;
1133
1134 spin_lock_bh(&t->stid_lock);
1135 if (family == PF_INET) {
1136 stid = find_next_zero_bit(t->stid_bmap,
1137 t->nstids + t->nsftids, t->nstids);
1138 if (stid < (t->nstids + t->nsftids))
1139 __set_bit(stid, t->stid_bmap);
1140 else
1141 stid = -1;
1142 } else {
1143 stid = -1;
1144 }
1145 if (stid >= 0) {
1146 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301147 stid -= t->nstids;
1148 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301149 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001150 }
1151 spin_unlock_bh(&t->stid_lock);
1152 return stid;
1153}
1154EXPORT_SYMBOL(cxgb4_alloc_sftid);
1155
1156/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001157 */
1158void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1159{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301160 /* Is it a server filter TID? */
1161 if (t->nsftids && (stid >= t->sftid_base)) {
1162 stid -= t->sftid_base;
1163 stid += t->nstids;
1164 } else {
1165 stid -= t->stid_base;
1166 }
1167
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001168 spin_lock_bh(&t->stid_lock);
1169 if (family == PF_INET)
1170 __clear_bit(stid, t->stid_bmap);
1171 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301172 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001173 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301174 if (stid < t->nstids) {
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301175 if (family == PF_INET6) {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301176 t->stids_in_use -= 2;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301177 t->v6_stids_in_use -= 2;
1178 } else {
1179 t->stids_in_use--;
1180 }
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301181 } else {
1182 t->sftids_in_use--;
1183 }
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301184
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001185 spin_unlock_bh(&t->stid_lock);
1186}
1187EXPORT_SYMBOL(cxgb4_free_stid);
1188
1189/*
1190 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1191 */
1192static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1193 unsigned int tid)
1194{
1195 struct cpl_tid_release *req;
1196
1197 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
Johannes Berg4df864c2017-06-16 14:29:21 +02001198 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001199 INIT_TP_WR(req, tid);
1200 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1201}
1202
1203/*
1204 * Queue a TID release request and if necessary schedule a work queue to
1205 * process it.
1206 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001207static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1208 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001209{
1210 void **p = &t->tid_tab[tid];
1211 struct adapter *adap = container_of(t, struct adapter, tids);
1212
1213 spin_lock_bh(&adap->tid_release_lock);
1214 *p = adap->tid_release_head;
1215 /* Low 2 bits encode the Tx channel number */
1216 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1217 if (!adap->tid_release_task_busy) {
1218 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001219 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001220 }
1221 spin_unlock_bh(&adap->tid_release_lock);
1222}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001223
1224/*
1225 * Process the list of pending TID release requests.
1226 */
1227static void process_tid_release_list(struct work_struct *work)
1228{
1229 struct sk_buff *skb;
1230 struct adapter *adap;
1231
1232 adap = container_of(work, struct adapter, tid_release_task);
1233
1234 spin_lock_bh(&adap->tid_release_lock);
1235 while (adap->tid_release_head) {
1236 void **p = adap->tid_release_head;
1237 unsigned int chan = (uintptr_t)p & 3;
1238 p = (void *)p - chan;
1239
1240 adap->tid_release_head = *p;
1241 *p = NULL;
1242 spin_unlock_bh(&adap->tid_release_lock);
1243
1244 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1245 GFP_KERNEL)))
1246 schedule_timeout_uninterruptible(1);
1247
1248 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1249 t4_ofld_send(adap, skb);
1250 spin_lock_bh(&adap->tid_release_lock);
1251 }
1252 adap->tid_release_task_busy = false;
1253 spin_unlock_bh(&adap->tid_release_lock);
1254}
1255
1256/*
1257 * Release a TID and inform HW. If we are unable to allocate the release
1258 * message we defer to a work queue.
1259 */
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301260void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid,
1261 unsigned short family)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001262{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001263 struct sk_buff *skb;
1264 struct adapter *adap = container_of(t, struct adapter, tids);
1265
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301266 WARN_ON(tid >= t->ntids);
1267
1268 if (t->tid_tab[tid]) {
1269 t->tid_tab[tid] = NULL;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301270 atomic_dec(&t->conns_in_use);
1271 if (t->hash_base && (tid >= t->hash_base)) {
1272 if (family == AF_INET6)
1273 atomic_sub(2, &t->hash_tids_in_use);
1274 else
1275 atomic_dec(&t->hash_tids_in_use);
1276 } else {
1277 if (family == AF_INET6)
1278 atomic_sub(2, &t->tids_in_use);
1279 else
1280 atomic_dec(&t->tids_in_use);
1281 }
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301282 }
1283
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001284 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1285 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001286 mk_tid_release(skb, chan, tid);
1287 t4_ofld_send(adap, skb);
1288 } else
1289 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001290}
1291EXPORT_SYMBOL(cxgb4_remove_tid);
1292
1293/*
1294 * Allocate and initialize the TID tables. Returns 0 on success.
1295 */
1296static int tid_init(struct tid_info *t)
1297{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301298 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301299 unsigned int max_ftids = t->nftids + t->nsftids;
1300 unsigned int natids = t->natids;
1301 unsigned int stid_bmap_size;
1302 unsigned int ftid_bmap_size;
1303 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001304
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001305 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301306 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001307 size = t->ntids * sizeof(*t->tid_tab) +
1308 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001309 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001310 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001311 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301312 max_ftids * sizeof(*t->ftid_tab) +
1313 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001314
Michal Hocko752ade62017-05-08 15:57:27 -07001315 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001316 if (!t->tid_tab)
1317 return -ENOMEM;
1318
1319 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1320 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001321 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001322 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301323 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001324 spin_lock_init(&t->stid_lock);
1325 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301326 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001327
1328 t->stids_in_use = 0;
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301329 t->v6_stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301330 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001331 t->afree = NULL;
1332 t->atids_in_use = 0;
1333 atomic_set(&t->tids_in_use, 0);
Ganesh Goudar1dec4ce2017-06-07 15:04:51 +05301334 atomic_set(&t->conns_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301335 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001336
1337 /* Setup the free list for atid_tab and clear the stid bitmap. */
1338 if (natids) {
1339 while (--natids)
1340 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1341 t->afree = t->atid_tab;
1342 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301343
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301344 if (is_offload(adap)) {
1345 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1346 /* Reserve stid 0 for T4/T5 adapters */
1347 if (!t->stid_base &&
1348 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1349 __set_bit(0, t->stid_bmap);
1350 }
1351
1352 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001353 return 0;
1354}
1355
1356/**
1357 * cxgb4_create_server - create an IP server
1358 * @dev: the device
1359 * @stid: the server TID
1360 * @sip: local IP address to bind server to
1361 * @sport: the server's TCP port
1362 * @queue: queue to direct messages from this server to
1363 *
1364 * Create an IP server for the given port and address.
1365 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1366 */
1367int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001368 __be32 sip, __be16 sport, __be16 vlan,
1369 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001370{
1371 unsigned int chan;
1372 struct sk_buff *skb;
1373 struct adapter *adap;
1374 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301375 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001376
1377 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1378 if (!skb)
1379 return -ENOMEM;
1380
1381 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001382 req = __skb_put(skb, sizeof(*req));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001383 INIT_TP_WR(req, 0);
1384 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1385 req->local_port = sport;
1386 req->peer_port = htons(0);
1387 req->local_ip = sip;
1388 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001389 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001390 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001391 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1392 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301393 ret = t4_mgmt_tx(adap, skb);
1394 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001395}
1396EXPORT_SYMBOL(cxgb4_create_server);
1397
Vipul Pandya80f40c12013-07-04 16:10:45 +05301398/* cxgb4_create_server6 - create an IPv6 server
1399 * @dev: the device
1400 * @stid: the server TID
1401 * @sip: local IPv6 address to bind server to
1402 * @sport: the server's TCP port
1403 * @queue: queue to direct messages from this server to
1404 *
1405 * Create an IPv6 server for the given port and address.
1406 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1407 */
1408int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1409 const struct in6_addr *sip, __be16 sport,
1410 unsigned int queue)
1411{
1412 unsigned int chan;
1413 struct sk_buff *skb;
1414 struct adapter *adap;
1415 struct cpl_pass_open_req6 *req;
1416 int ret;
1417
1418 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1419 if (!skb)
1420 return -ENOMEM;
1421
1422 adap = netdev2adap(dev);
Johannes Berg4df864c2017-06-16 14:29:21 +02001423 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301424 INIT_TP_WR(req, 0);
1425 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1426 req->local_port = sport;
1427 req->peer_port = htons(0);
1428 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1429 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1430 req->peer_ip_hi = cpu_to_be64(0);
1431 req->peer_ip_lo = cpu_to_be64(0);
1432 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001433 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001434 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1435 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301436 ret = t4_mgmt_tx(adap, skb);
1437 return net_xmit_eval(ret);
1438}
1439EXPORT_SYMBOL(cxgb4_create_server6);
1440
1441int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1442 unsigned int queue, bool ipv6)
1443{
1444 struct sk_buff *skb;
1445 struct adapter *adap;
1446 struct cpl_close_listsvr_req *req;
1447 int ret;
1448
1449 adap = netdev2adap(dev);
1450
1451 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1452 if (!skb)
1453 return -ENOMEM;
1454
Johannes Berg4df864c2017-06-16 14:29:21 +02001455 req = __skb_put(skb, sizeof(*req));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301456 INIT_TP_WR(req, 0);
1457 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001458 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1459 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301460 ret = t4_mgmt_tx(adap, skb);
1461 return net_xmit_eval(ret);
1462}
1463EXPORT_SYMBOL(cxgb4_remove_server);
1464
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001465/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001466 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1467 * @mtus: the HW MTU table
1468 * @mtu: the target MTU
1469 * @idx: index of selected entry in the MTU table
1470 *
1471 * Returns the index and the value in the HW MTU table that is closest to
1472 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1473 * table, in which case that smallest available value is selected.
1474 */
1475unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1476 unsigned int *idx)
1477{
1478 unsigned int i = 0;
1479
1480 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1481 ++i;
1482 if (idx)
1483 *idx = i;
1484 return mtus[i];
1485}
1486EXPORT_SYMBOL(cxgb4_best_mtu);
1487
1488/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301489 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1490 * @mtus: the HW MTU table
1491 * @header_size: Header Size
1492 * @data_size_max: maximum Data Segment Size
1493 * @data_size_align: desired Data Segment Size Alignment (2^N)
1494 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1495 *
1496 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1497 * MTU Table based solely on a Maximum MTU parameter, we break that
1498 * parameter up into a Header Size and Maximum Data Segment Size, and
1499 * provide a desired Data Segment Size Alignment. If we find an MTU in
1500 * the Hardware MTU Table which will result in a Data Segment Size with
1501 * the requested alignment _and_ that MTU isn't "too far" from the
1502 * closest MTU, then we'll return that rather than the closest MTU.
1503 */
1504unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1505 unsigned short header_size,
1506 unsigned short data_size_max,
1507 unsigned short data_size_align,
1508 unsigned int *mtu_idxp)
1509{
1510 unsigned short max_mtu = header_size + data_size_max;
1511 unsigned short data_size_align_mask = data_size_align - 1;
1512 int mtu_idx, aligned_mtu_idx;
1513
1514 /* Scan the MTU Table till we find an MTU which is larger than our
1515 * Maximum MTU or we reach the end of the table. Along the way,
1516 * record the last MTU found, if any, which will result in a Data
1517 * Segment Length matching the requested alignment.
1518 */
1519 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1520 unsigned short data_size = mtus[mtu_idx] - header_size;
1521
1522 /* If this MTU minus the Header Size would result in a
1523 * Data Segment Size of the desired alignment, remember it.
1524 */
1525 if ((data_size & data_size_align_mask) == 0)
1526 aligned_mtu_idx = mtu_idx;
1527
1528 /* If we're not at the end of the Hardware MTU Table and the
1529 * next element is larger than our Maximum MTU, drop out of
1530 * the loop.
1531 */
1532 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1533 break;
1534 }
1535
1536 /* If we fell out of the loop because we ran to the end of the table,
1537 * then we just have to use the last [largest] entry.
1538 */
1539 if (mtu_idx == NMTUS)
1540 mtu_idx--;
1541
1542 /* If we found an MTU which resulted in the requested Data Segment
1543 * Length alignment and that's "not far" from the largest MTU which is
1544 * less than or equal to the maximum MTU, then use that.
1545 */
1546 if (aligned_mtu_idx >= 0 &&
1547 mtu_idx - aligned_mtu_idx <= 1)
1548 mtu_idx = aligned_mtu_idx;
1549
1550 /* If the caller has passed in an MTU Index pointer, pass the
1551 * MTU Index back. Return the MTU value.
1552 */
1553 if (mtu_idxp)
1554 *mtu_idxp = mtu_idx;
1555 return mtus[mtu_idx];
1556}
1557EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1558
1559/**
Hariprasad S27999802015-09-23 17:19:26 +05301560 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1561 * @chip: chip type
1562 * @viid: VI id of the given port
1563 *
1564 * Return the SMT index for this VI.
1565 */
1566unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1567{
1568 /* In T4/T5, SMT contains 256 SMAC entries organized in
1569 * 128 rows of 2 entries each.
1570 * In T6, SMT contains 256 SMAC entries in 256 rows.
1571 * TODO: The below code needs to be updated when we add support
1572 * for 256 VFs.
1573 */
1574 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1575 return ((viid & 0x7f) << 1);
1576 else
1577 return (viid & 0x7f);
1578}
1579EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1580
1581/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001582 * cxgb4_port_chan - get the HW channel of a port
1583 * @dev: the net device for the port
1584 *
1585 * Return the HW Tx channel of the given port.
1586 */
1587unsigned int cxgb4_port_chan(const struct net_device *dev)
1588{
1589 return netdev2pinfo(dev)->tx_chan;
1590}
1591EXPORT_SYMBOL(cxgb4_port_chan);
1592
Vipul Pandya881806b2012-05-18 15:29:24 +05301593unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1594{
1595 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001596 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301597
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301598 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1599 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301600 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301601 lp_count = LP_COUNT_G(v1);
1602 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001603 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301604 lp_count = LP_COUNT_T5_G(v1);
1605 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001606 }
1607 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301608}
1609EXPORT_SYMBOL(cxgb4_dbfifo_count);
1610
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001611/**
1612 * cxgb4_port_viid - get the VI id of a port
1613 * @dev: the net device for the port
1614 *
1615 * Return the VI id of the given port.
1616 */
1617unsigned int cxgb4_port_viid(const struct net_device *dev)
1618{
1619 return netdev2pinfo(dev)->viid;
1620}
1621EXPORT_SYMBOL(cxgb4_port_viid);
1622
1623/**
1624 * cxgb4_port_idx - get the index of a port
1625 * @dev: the net device for the port
1626 *
1627 * Return the index of the given port.
1628 */
1629unsigned int cxgb4_port_idx(const struct net_device *dev)
1630{
1631 return netdev2pinfo(dev)->port_id;
1632}
1633EXPORT_SYMBOL(cxgb4_port_idx);
1634
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001635void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1636 struct tp_tcp_stats *v6)
1637{
1638 struct adapter *adap = pci_get_drvdata(pdev);
1639
1640 spin_lock(&adap->stats_lock);
1641 t4_tp_get_tcp_stats(adap, v4, v6);
1642 spin_unlock(&adap->stats_lock);
1643}
1644EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1645
1646void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1647 const unsigned int *pgsz_order)
1648{
1649 struct adapter *adap = netdev2adap(dev);
1650
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301651 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1652 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1653 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1654 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001655}
1656EXPORT_SYMBOL(cxgb4_iscsi_init);
1657
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301658int cxgb4_flush_eq_cache(struct net_device *dev)
1659{
1660 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301661
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301662 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301663}
1664EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1665
1666static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1667{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301668 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301669 __be64 indices;
1670 int ret;
1671
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301672 spin_lock(&adap->win0_lock);
1673 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1674 sizeof(indices), (__be32 *)&indices,
1675 T4_MEMORY_READ);
1676 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301677 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001678 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1679 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301680 }
1681 return ret;
1682}
1683
1684int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1685 u16 size)
1686{
1687 struct adapter *adap = netdev2adap(dev);
1688 u16 hw_pidx, hw_cidx;
1689 int ret;
1690
1691 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1692 if (ret)
1693 goto out;
1694
1695 if (pidx != hw_pidx) {
1696 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301697 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301698
1699 if (pidx >= hw_pidx)
1700 delta = pidx - hw_pidx;
1701 else
1702 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301703
1704 if (is_t4(adap->params.chip))
1705 val = PIDX_V(delta);
1706 else
1707 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301708 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301709 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1710 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301711 }
1712out:
1713 return ret;
1714}
1715EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1716
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301717int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1718{
1719 struct adapter *adap;
1720 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301721 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301722 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1723 int ret;
1724
1725 adap = netdev2adap(dev);
1726
1727 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1728
1729 /* Figure out where the offset lands in the Memory Type/Address scheme.
1730 * This code assumes that the memory is laid out starting at offset 0
1731 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1732 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1733 * MC0, and some have both MC0 and MC1.
1734 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301735 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1736 edc0_size = EDRAM0_SIZE_G(size) << 20;
1737 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1738 edc1_size = EDRAM1_SIZE_G(size) << 20;
1739 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1740 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301741
1742 edc0_end = edc0_size;
1743 edc1_end = edc0_end + edc1_size;
1744 mc0_end = edc1_end + mc0_size;
1745
1746 if (offset < edc0_end) {
1747 memtype = MEM_EDC0;
1748 memaddr = offset;
1749 } else if (offset < edc1_end) {
1750 memtype = MEM_EDC1;
1751 memaddr = offset - edc0_end;
1752 } else {
1753 if (offset < mc0_end) {
1754 memtype = MEM_MC0;
1755 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301756 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301757 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1758 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301759 mc1_end = mc0_end + mc1_size;
1760 if (offset < mc1_end) {
1761 memtype = MEM_MC1;
1762 memaddr = offset - mc0_end;
1763 } else {
1764 /* offset beyond the end of any memory */
1765 goto err;
1766 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301767 } else {
1768 /* T4/T6 only has a single memory channel */
1769 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301770 }
1771 }
1772
1773 spin_lock(&adap->win0_lock);
1774 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1775 spin_unlock(&adap->win0_lock);
1776 return ret;
1777
1778err:
1779 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1780 stag, offset);
1781 return -EINVAL;
1782}
1783EXPORT_SYMBOL(cxgb4_read_tpte);
1784
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301785u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1786{
1787 u32 hi, lo;
1788 struct adapter *adap;
1789
1790 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301791 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1792 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301793
1794 return ((u64)hi << 32) | (u64)lo;
1795}
1796EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1797
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301798int cxgb4_bar2_sge_qregs(struct net_device *dev,
1799 unsigned int qid,
1800 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301801 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301802 u64 *pbar2_qoffset,
1803 unsigned int *pbar2_qid)
1804{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301805 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301806 qid,
1807 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1808 ? T4_BAR2_QTYPE_EGRESS
1809 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301810 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301811 pbar2_qoffset,
1812 pbar2_qid);
1813}
1814EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1815
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001816static struct pci_driver cxgb4_driver;
1817
1818static void check_neigh_update(struct neighbour *neigh)
1819{
1820 const struct device *parent;
1821 const struct net_device *netdev = neigh->dev;
1822
Parav Panditd0d7b102017-02-04 11:00:49 -06001823 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001824 netdev = vlan_dev_real_dev(netdev);
1825 parent = netdev->dev.parent;
1826 if (parent && parent->driver == &cxgb4_driver.driver)
1827 t4_l2t_update(dev_get_drvdata(parent), neigh);
1828}
1829
1830static int netevent_cb(struct notifier_block *nb, unsigned long event,
1831 void *data)
1832{
1833 switch (event) {
1834 case NETEVENT_NEIGH_UPDATE:
1835 check_neigh_update(data);
1836 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001837 case NETEVENT_REDIRECT:
1838 default:
1839 break;
1840 }
1841 return 0;
1842}
1843
1844static bool netevent_registered;
1845static struct notifier_block cxgb4_netevent_nb = {
1846 .notifier_call = netevent_cb
1847};
1848
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301849static void drain_db_fifo(struct adapter *adap, int usecs)
1850{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001851 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301852
1853 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301854 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1855 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301856 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301857 lp_count = LP_COUNT_G(v1);
1858 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001859 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301860 lp_count = LP_COUNT_T5_G(v1);
1861 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001862 }
1863
1864 if (lp_count == 0 && hp_count == 0)
1865 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301866 set_current_state(TASK_UNINTERRUPTIBLE);
1867 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301868 } while (1);
1869}
1870
1871static void disable_txq_db(struct sge_txq *q)
1872{
Steve Wise05eb2382014-03-14 21:52:08 +05301873 unsigned long flags;
1874
1875 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301876 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301877 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301878}
1879
Steve Wise05eb2382014-03-14 21:52:08 +05301880static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301881{
1882 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301883 if (q->db_pidx_inc) {
1884 /* Make sure that all writes to the TX descriptors
1885 * are committed before we tell HW about them.
1886 */
1887 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301888 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1889 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301890 q->db_pidx_inc = 0;
1891 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301892 q->db_disabled = 0;
1893 spin_unlock_irq(&q->db_lock);
1894}
1895
1896static void disable_dbs(struct adapter *adap)
1897{
1898 int i;
1899
1900 for_each_ethrxq(&adap->sge, i)
1901 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301902 if (is_offload(adap)) {
1903 struct sge_uld_txq_info *txq_info =
1904 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1905
1906 if (txq_info) {
1907 for_each_ofldtxq(&adap->sge, i) {
1908 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1909
1910 disable_txq_db(&txq->q);
1911 }
1912 }
1913 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301914 for_each_port(adap, i)
1915 disable_txq_db(&adap->sge.ctrlq[i].q);
1916}
1917
1918static void enable_dbs(struct adapter *adap)
1919{
1920 int i;
1921
1922 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301923 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301924 if (is_offload(adap)) {
1925 struct sge_uld_txq_info *txq_info =
1926 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1927
1928 if (txq_info) {
1929 for_each_ofldtxq(&adap->sge, i) {
1930 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1931
1932 enable_txq_db(adap, &txq->q);
1933 }
1934 }
1935 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301936 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301937 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1938}
1939
1940static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1941{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301942 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1943
1944 if (adap->uld && adap->uld[type].handle)
1945 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301946}
1947
1948static void process_db_full(struct work_struct *work)
1949{
1950 struct adapter *adap;
1951
1952 adap = container_of(work, struct adapter, db_full_task);
1953
1954 drain_db_fifo(adap, dbfifo_drain_delay);
1955 enable_dbs(adap);
1956 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301957 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1958 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1959 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1960 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1961 else
1962 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1963 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301964}
1965
1966static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1967{
1968 u16 hw_pidx, hw_cidx;
1969 int ret;
1970
Steve Wise05eb2382014-03-14 21:52:08 +05301971 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301972 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1973 if (ret)
1974 goto out;
1975 if (q->db_pidx != hw_pidx) {
1976 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301977 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301978
1979 if (q->db_pidx >= hw_pidx)
1980 delta = q->db_pidx - hw_pidx;
1981 else
1982 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301983
1984 if (is_t4(adap->params.chip))
1985 val = PIDX_V(delta);
1986 else
1987 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301988 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301989 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1990 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301991 }
1992out:
1993 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05301994 q->db_pidx_inc = 0;
1995 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301996 if (ret)
1997 CH_WARN(adap, "DB drop recovery failed.\n");
1998}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301999
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302000static void recover_all_queues(struct adapter *adap)
2001{
2002 int i;
2003
2004 for_each_ethrxq(&adap->sge, i)
2005 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05302006 if (is_offload(adap)) {
2007 struct sge_uld_txq_info *txq_info =
2008 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
2009 if (txq_info) {
2010 for_each_ofldtxq(&adap->sge, i) {
2011 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
2012
2013 sync_txq_pidx(adap, &txq->q);
2014 }
2015 }
2016 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302017 for_each_port(adap, i)
2018 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2019}
2020
Vipul Pandya881806b2012-05-18 15:29:24 +05302021static void process_db_drop(struct work_struct *work)
2022{
2023 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05302024
Vipul Pandya881806b2012-05-18 15:29:24 +05302025 adap = container_of(work, struct adapter, db_drop_task);
2026
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302027 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302028 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002029 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05302030 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002031 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302032 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002033 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05302034 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302035 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002036 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2037 u16 qid = (dropped_db >> 15) & 0x1ffff;
2038 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302039 u64 bar2_qoffset;
2040 unsigned int bar2_qid;
2041 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002042
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302043 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002044 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302045 if (ret)
2046 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2047 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2048 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302049 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302050 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002051
2052 /* Re-enable BAR2 WC */
2053 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2054 }
2055
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302056 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2057 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302058}
2059
2060void t4_db_full(struct adapter *adap)
2061{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302062 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302063 disable_dbs(adap);
2064 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302065 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2066 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002067 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002068 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302069}
2070
2071void t4_db_dropped(struct adapter *adap)
2072{
Steve Wise05eb2382014-03-14 21:52:08 +05302073 if (is_t4(adap->params.chip)) {
2074 disable_dbs(adap);
2075 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2076 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002077 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302078}
2079
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302080void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002081{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002082 if (!netevent_registered) {
2083 register_netevent_notifier(&cxgb4_netevent_nb);
2084 netevent_registered = true;
2085 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002086}
2087
2088static void detach_ulds(struct adapter *adap)
2089{
2090 unsigned int i;
2091
2092 mutex_lock(&uld_mutex);
2093 list_del(&adap->list_node);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002094
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002095 for (i = 0; i < CXGB4_ULD_MAX; i++)
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002096 if (adap->uld && adap->uld[i].handle)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302097 adap->uld[i].state_change(adap->uld[i].handle,
2098 CXGB4_STATE_DETACH);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03002099
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002100 if (netevent_registered && list_empty(&adapter_list)) {
2101 unregister_netevent_notifier(&cxgb4_netevent_nb);
2102 netevent_registered = false;
2103 }
2104 mutex_unlock(&uld_mutex);
2105}
2106
2107static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2108{
2109 unsigned int i;
2110
2111 mutex_lock(&uld_mutex);
2112 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302113 if (adap->uld && adap->uld[i].handle)
2114 adap->uld[i].state_change(adap->uld[i].handle,
2115 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002116 mutex_unlock(&uld_mutex);
2117}
2118
Anish Bhatt1bb60372014-10-14 20:07:22 -07002119#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002120static int cxgb4_inet6addr_handler(struct notifier_block *this,
2121 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302122{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002123 struct inet6_ifaddr *ifa = data;
2124 struct net_device *event_dev = ifa->idev->dev;
2125 const struct device *parent = NULL;
2126#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302127 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002128#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002129 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002130 event_dev = vlan_dev_real_dev(event_dev);
2131#if IS_ENABLED(CONFIG_BONDING)
2132 if (event_dev->flags & IFF_MASTER) {
2133 list_for_each_entry(adap, &adapter_list, list_node) {
2134 switch (event) {
2135 case NETDEV_UP:
2136 cxgb4_clip_get(adap->port[0],
2137 (const u32 *)ifa, 1);
2138 break;
2139 case NETDEV_DOWN:
2140 cxgb4_clip_release(adap->port[0],
2141 (const u32 *)ifa, 1);
2142 break;
2143 default:
2144 break;
2145 }
2146 }
2147 return NOTIFY_OK;
2148 }
2149#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302150
Anish Bhattb5a02f52015-01-14 15:17:34 -08002151 if (event_dev)
2152 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302153
Anish Bhattb5a02f52015-01-14 15:17:34 -08002154 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302155 switch (event) {
2156 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002157 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302158 break;
2159 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002160 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302161 break;
2162 default:
2163 break;
2164 }
2165 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002166 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302167}
2168
Anish Bhattb5a02f52015-01-14 15:17:34 -08002169static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302170static struct notifier_block cxgb4_inet6addr_notifier = {
2171 .notifier_call = cxgb4_inet6addr_handler
2172};
2173
Vipul Pandya01bcca62013-07-04 16:10:46 +05302174static void update_clip(const struct adapter *adap)
2175{
2176 int i;
2177 struct net_device *dev;
2178 int ret;
2179
2180 rcu_read_lock();
2181
2182 for (i = 0; i < MAX_NPORTS; i++) {
2183 dev = adap->port[i];
2184 ret = 0;
2185
2186 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002187 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302188
2189 if (ret < 0)
2190 break;
2191 }
2192 rcu_read_unlock();
2193}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002194#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302195
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002196/**
2197 * cxgb_up - enable the adapter
2198 * @adap: adapter being enabled
2199 *
2200 * Called when the first port is enabled, this function performs the
2201 * actions necessary to make an adapter operational, such as completing
2202 * the initialization of HW modules, and enabling interrupts.
2203 *
2204 * Must be called with the rtnl lock held.
2205 */
2206static int cxgb_up(struct adapter *adap)
2207{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002208 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002209
Raju Rangoju91060382017-06-19 17:40:48 +05302210 mutex_lock(&uld_mutex);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002211 err = setup_sge_queues(adap);
2212 if (err)
Raju Rangoju91060382017-06-19 17:40:48 +05302213 goto rel_lock;
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002214 err = setup_rss(adap);
2215 if (err)
2216 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002217
2218 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002219 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002220 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2221 adap->msix_info[0].desc, adap);
2222 if (err)
2223 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002224 err = request_msix_queue_irqs(adap);
2225 if (err) {
2226 free_irq(adap->msix_info[0].vec, adap);
2227 goto irq_err;
2228 }
2229 } else {
2230 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2231 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002232 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002233 if (err)
2234 goto irq_err;
2235 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302236
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002237 enable_rx(adap);
2238 t4_sge_start(adap);
2239 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002240 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302241 mutex_unlock(&uld_mutex);
2242
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002243 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002244#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302245 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002246#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302247 /* Initialize hash mac addr list*/
2248 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002249 return err;
Raju Rangoju91060382017-06-19 17:40:48 +05302250
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002251 irq_err:
2252 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002253 freeq:
2254 t4_free_sge_resources(adap);
Raju Rangoju91060382017-06-19 17:40:48 +05302255 rel_lock:
2256 mutex_unlock(&uld_mutex);
2257 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002258}
2259
2260static void cxgb_down(struct adapter *adapter)
2261{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002262 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302263 cancel_work_sync(&adapter->db_full_task);
2264 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002265 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002266 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002267
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002268 t4_sge_stop(adapter);
2269 t4_free_sge_resources(adapter);
2270 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002271}
2272
2273/*
2274 * net_device operations
2275 */
2276static int cxgb_open(struct net_device *dev)
2277{
2278 int err;
2279 struct port_info *pi = netdev_priv(dev);
2280 struct adapter *adapter = pi->adapter;
2281
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002282 netif_carrier_off(dev);
2283
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002284 if (!(adapter->flags & FULL_INIT_DONE)) {
2285 err = cxgb_up(adapter);
2286 if (err < 0)
2287 return err;
2288 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002289
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302290 /* It's possible that the basic port information could have
2291 * changed since we first read it.
2292 */
2293 err = t4_update_port_info(pi);
2294 if (err < 0)
2295 return err;
2296
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002297 err = link_start(dev);
2298 if (!err)
2299 netif_tx_start_all_queues(dev);
2300 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002301}
2302
2303static int cxgb_close(struct net_device *dev)
2304{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002305 struct port_info *pi = netdev_priv(dev);
2306 struct adapter *adapter = pi->adapter;
2307
2308 netif_tx_stop_all_queues(dev);
2309 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302310 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002311}
2312
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002313int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002314 __be32 sip, __be16 sport, __be16 vlan,
2315 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002316{
2317 int ret;
2318 struct filter_entry *f;
2319 struct adapter *adap;
2320 int i;
2321 u8 *val;
2322
2323 adap = netdev2adap(dev);
2324
Vipul Pandya1cab7752012-12-10 09:30:55 +00002325 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302326 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002327 stid += adap->tids.nftids;
2328
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002329 /* Check to make sure the filter requested is writable ...
2330 */
2331 f = &adap->tids.ftid_tab[stid];
2332 ret = writable_filter(f);
2333 if (ret)
2334 return ret;
2335
2336 /* Clear out any old resources being used by the filter before
2337 * we start constructing the new filter.
2338 */
2339 if (f->valid)
2340 clear_filter(adap, f);
2341
2342 /* Clear out filter specifications */
2343 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2344 f->fs.val.lport = cpu_to_be16(sport);
2345 f->fs.mask.lport = ~0;
2346 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002347 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002348 for (i = 0; i < 4; i++) {
2349 f->fs.val.lip[i] = val[i];
2350 f->fs.mask.lip[i] = ~0;
2351 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302352 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002353 f->fs.val.iport = port;
2354 f->fs.mask.iport = mask;
2355 }
2356 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002357
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302358 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302359 f->fs.val.proto = IPPROTO_TCP;
2360 f->fs.mask.proto = ~0;
2361 }
2362
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002363 f->fs.dirsteer = 1;
2364 f->fs.iq = queue;
2365 /* Mark filter as locked */
2366 f->locked = 1;
2367 f->fs.rpttid = 1;
2368
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302369 /* Save the actual tid. We need this to get the corresponding
2370 * filter entry structure in filter_rpl.
2371 */
2372 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002373 ret = set_filter_wr(adap, stid);
2374 if (ret) {
2375 clear_filter(adap, f);
2376 return ret;
2377 }
2378
2379 return 0;
2380}
2381EXPORT_SYMBOL(cxgb4_create_server_filter);
2382
2383int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2384 unsigned int queue, bool ipv6)
2385{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002386 struct filter_entry *f;
2387 struct adapter *adap;
2388
2389 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002390
2391 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302392 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002393 stid += adap->tids.nftids;
2394
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002395 f = &adap->tids.ftid_tab[stid];
2396 /* Unlock the filter */
2397 f->locked = 0;
2398
Wei Yongjun8c148462016-08-20 15:32:41 +00002399 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002400}
2401EXPORT_SYMBOL(cxgb4_remove_server_filter);
2402
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002403static void cxgb_get_stats(struct net_device *dev,
2404 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002405{
2406 struct port_stats stats;
2407 struct port_info *p = netdev_priv(dev);
2408 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002409
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002410 /* Block retrieving statistics during EEH error
2411 * recovery. Otherwise, the recovery might fail
2412 * and the PCI device will be removed permanently
2413 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002414 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002415 if (!netif_device_present(dev)) {
2416 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002417 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002418 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302419 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2420 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002421 spin_unlock(&adapter->stats_lock);
2422
2423 ns->tx_bytes = stats.tx_octets;
2424 ns->tx_packets = stats.tx_frames;
2425 ns->rx_bytes = stats.rx_octets;
2426 ns->rx_packets = stats.rx_frames;
2427 ns->multicast = stats.rx_mcast_frames;
2428
2429 /* detailed rx_errors */
2430 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2431 stats.rx_runt;
2432 ns->rx_over_errors = 0;
2433 ns->rx_crc_errors = stats.rx_fcs_err;
2434 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302435 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002436 stats.rx_ovflow2 + stats.rx_ovflow3 +
2437 stats.rx_trunc0 + stats.rx_trunc1 +
2438 stats.rx_trunc2 + stats.rx_trunc3;
2439 ns->rx_missed_errors = 0;
2440
2441 /* detailed tx_errors */
2442 ns->tx_aborted_errors = 0;
2443 ns->tx_carrier_errors = 0;
2444 ns->tx_fifo_errors = 0;
2445 ns->tx_heartbeat_errors = 0;
2446 ns->tx_window_errors = 0;
2447
2448 ns->tx_errors = stats.tx_error_frames;
2449 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2450 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002451}
2452
2453static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2454{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002455 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002456 int ret = 0, prtad, devad;
2457 struct port_info *pi = netdev_priv(dev);
Atul Guptaa45695042017-07-04 16:46:20 +05302458 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002459 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2460
2461 switch (cmd) {
2462 case SIOCGMIIPHY:
2463 if (pi->mdio_addr < 0)
2464 return -EOPNOTSUPP;
2465 data->phy_id = pi->mdio_addr;
2466 break;
2467 case SIOCGMIIREG:
2468 case SIOCSMIIREG:
2469 if (mdio_phy_id_is_c45(data->phy_id)) {
2470 prtad = mdio_phy_id_prtad(data->phy_id);
2471 devad = mdio_phy_id_devad(data->phy_id);
2472 } else if (data->phy_id < 32) {
2473 prtad = data->phy_id;
2474 devad = 0;
2475 data->reg_num &= 0x1f;
2476 } else
2477 return -EINVAL;
2478
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302479 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002480 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002481 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002482 data->reg_num, &data->val_out);
2483 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002484 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002485 data->reg_num, data->val_in);
2486 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302487 case SIOCGHWTSTAMP:
2488 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2489 sizeof(pi->tstamp_config)) ?
2490 -EFAULT : 0;
2491 case SIOCSHWTSTAMP:
2492 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2493 sizeof(pi->tstamp_config)))
2494 return -EFAULT;
2495
Atul Guptaa45695042017-07-04 16:46:20 +05302496 if (!is_t4(adapter->params.chip)) {
2497 switch (pi->tstamp_config.tx_type) {
2498 case HWTSTAMP_TX_OFF:
2499 case HWTSTAMP_TX_ON:
2500 break;
2501 default:
2502 return -ERANGE;
2503 }
2504
2505 switch (pi->tstamp_config.rx_filter) {
2506 case HWTSTAMP_FILTER_NONE:
2507 pi->rxtstamp = false;
2508 break;
2509 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2510 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2511 cxgb4_ptprx_timestamping(pi, pi->port_id,
2512 PTP_TS_L4);
2513 break;
2514 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2515 cxgb4_ptprx_timestamping(pi, pi->port_id,
2516 PTP_TS_L2_L4);
2517 break;
2518 case HWTSTAMP_FILTER_ALL:
2519 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2520 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2521 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2522 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2523 pi->rxtstamp = true;
2524 break;
2525 default:
2526 pi->tstamp_config.rx_filter =
2527 HWTSTAMP_FILTER_NONE;
2528 return -ERANGE;
2529 }
2530
2531 if ((pi->tstamp_config.tx_type == HWTSTAMP_TX_OFF) &&
2532 (pi->tstamp_config.rx_filter ==
2533 HWTSTAMP_FILTER_NONE)) {
2534 if (cxgb4_ptp_txtype(adapter, pi->port_id) >= 0)
2535 pi->ptp_enable = false;
2536 }
2537
2538 if (pi->tstamp_config.rx_filter !=
2539 HWTSTAMP_FILTER_NONE) {
2540 if (cxgb4_ptp_redirect_rx_packet(adapter,
2541 pi) >= 0)
2542 pi->ptp_enable = true;
2543 }
2544 } else {
2545 /* For T4 Adapters */
2546 switch (pi->tstamp_config.rx_filter) {
2547 case HWTSTAMP_FILTER_NONE:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302548 pi->rxtstamp = false;
2549 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302550 case HWTSTAMP_FILTER_ALL:
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302551 pi->rxtstamp = true;
2552 break;
Atul Guptaa45695042017-07-04 16:46:20 +05302553 default:
2554 pi->tstamp_config.rx_filter =
2555 HWTSTAMP_FILTER_NONE;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302556 return -ERANGE;
Atul Guptaa45695042017-07-04 16:46:20 +05302557 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302558 }
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302559 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2560 sizeof(pi->tstamp_config)) ?
2561 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002562 default:
2563 return -EOPNOTSUPP;
2564 }
2565 return ret;
2566}
2567
2568static void cxgb_set_rxmode(struct net_device *dev)
2569{
2570 /* unfortunately we can't return errors to the stack */
2571 set_rxmode(dev, -1, false);
2572}
2573
2574static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2575{
2576 int ret;
2577 struct port_info *pi = netdev_priv(dev);
2578
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302579 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002580 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002581 if (!ret)
2582 dev->mtu = new_mtu;
2583 return ret;
2584}
2585
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302586#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302587static int dummy_open(struct net_device *dev)
2588{
2589 /* Turn carrier off since we don't have to transmit anything on this
2590 * interface.
2591 */
2592 netif_carrier_off(dev);
2593 return 0;
2594}
2595
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302596/* Fill MAC address that will be assigned by the FW */
2597static void fill_vf_station_mac_addr(struct adapter *adap)
2598{
2599 unsigned int i;
2600 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2601 int err;
2602 u8 *na;
2603 u16 a, b;
2604
2605 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2606 if (!err) {
2607 na = adap->params.vpd.na;
2608 for (i = 0; i < ETH_ALEN; i++)
2609 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2610 hex2val(na[2 * i + 1]));
2611 a = (hw_addr[0] << 8) | hw_addr[1];
2612 b = (hw_addr[1] << 8) | hw_addr[2];
2613 a ^= b;
2614 a |= 0x0200; /* locally assigned Ethernet MAC address */
2615 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2616 macaddr[0] = a >> 8;
2617 macaddr[1] = a & 0xff;
2618
2619 for (i = 2; i < 5; i++)
2620 macaddr[i] = hw_addr[i + 1];
2621
2622 for (i = 0; i < adap->num_vfs; i++) {
2623 macaddr[5] = adap->pf * 16 + i;
2624 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2625 }
2626 }
2627}
2628
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302629static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2630{
2631 struct port_info *pi = netdev_priv(dev);
2632 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302633 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302634
2635 /* verify MAC addr is valid */
2636 if (!is_valid_ether_addr(mac)) {
2637 dev_err(pi->adapter->pdev_dev,
2638 "Invalid Ethernet address %pM for VF %d\n",
2639 mac, vf);
2640 return -EINVAL;
2641 }
2642
2643 dev_info(pi->adapter->pdev_dev,
2644 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302645 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2646 if (!ret)
2647 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2648 return ret;
2649}
2650
2651static int cxgb_get_vf_config(struct net_device *dev,
2652 int vf, struct ifla_vf_info *ivi)
2653{
2654 struct port_info *pi = netdev_priv(dev);
2655 struct adapter *adap = pi->adapter;
2656
2657 if (vf >= adap->num_vfs)
2658 return -EINVAL;
2659 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302660 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2661 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302662 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2663 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302664}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302665
2666static int cxgb_get_phys_port_id(struct net_device *dev,
2667 struct netdev_phys_item_id *ppid)
2668{
2669 struct port_info *pi = netdev_priv(dev);
2670 unsigned int phy_port_id;
2671
2672 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2673 ppid->id_len = sizeof(phy_port_id);
2674 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2675 return 0;
2676}
2677
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302678static int cxgb_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2679 int max_tx_rate)
2680{
2681 struct port_info *pi = netdev_priv(dev);
2682 struct adapter *adap = pi->adapter;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302683 unsigned int link_ok, speed, mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302684 u32 fw_pfvf, fw_class;
2685 int class_id = vf;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302686 int ret;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302687 u16 pktsize;
2688
2689 if (vf >= adap->num_vfs)
2690 return -EINVAL;
2691
2692 if (min_tx_rate) {
2693 dev_err(adap->pdev_dev,
2694 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2695 min_tx_rate, vf);
2696 return -EINVAL;
2697 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302698
2699 ret = t4_get_link_params(pi, &link_ok, &speed, &mtu);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302700 if (ret != FW_SUCCESS) {
2701 dev_err(adap->pdev_dev,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302702 "Failed to get link information for VF %d\n", vf);
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302703 return -EINVAL;
2704 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302705
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302706 if (!link_ok) {
2707 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2708 return -EINVAL;
2709 }
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302710
2711 if (max_tx_rate > speed) {
2712 dev_err(adap->pdev_dev,
2713 "Max tx rate %d for VF %d can't be > link-speed %u",
2714 max_tx_rate, vf, speed);
2715 return -EINVAL;
2716 }
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302717
2718 pktsize = mtu;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302719 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2720 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2721 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2722 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2723 /* configure Traffic Class for rate-limiting */
2724 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2725 SCHED_CLASS_LEVEL_CL_RL,
2726 SCHED_CLASS_MODE_CLASS,
2727 SCHED_CLASS_RATEUNIT_BITS,
2728 SCHED_CLASS_RATEMODE_ABS,
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05302729 pi->tx_chan, class_id, 0,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302730 max_tx_rate * 1000, 0, pktsize);
2731 if (ret) {
2732 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2733 ret);
2734 return -EINVAL;
2735 }
2736 dev_info(adap->pdev_dev,
2737 "Class %d with MSS %u configured with rate %u\n",
2738 class_id, pktsize, max_tx_rate);
2739
2740 /* bind VF to configured Traffic Class */
2741 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2742 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2743 fw_class = class_id;
2744 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2745 &fw_class);
2746 if (ret) {
2747 dev_err(adap->pdev_dev,
2748 "Err %d in binding VF %d to Traffic Class %d\n",
2749 ret, vf, class_id);
2750 return -EINVAL;
2751 }
2752 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2753 adap->pf, vf, class_id);
2754 adap->vfinfo[vf].tx_rate = max_tx_rate;
2755 return 0;
2756}
2757
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302758#endif
2759
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002760static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2761{
2762 int ret;
2763 struct sockaddr *addr = p;
2764 struct port_info *pi = netdev_priv(dev);
2765
2766 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002767 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002768
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302769 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002770 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002771 if (ret < 0)
2772 return ret;
2773
2774 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2775 pi->xact_addr_filt = ret;
2776 return 0;
2777}
2778
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002779#ifdef CONFIG_NET_POLL_CONTROLLER
2780static void cxgb_netpoll(struct net_device *dev)
2781{
2782 struct port_info *pi = netdev_priv(dev);
2783 struct adapter *adap = pi->adapter;
2784
2785 if (adap->flags & USING_MSIX) {
2786 int i;
2787 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2788
2789 for (i = pi->nqsets; i; i--, rx++)
2790 t4_sge_intr_msix(0, &rx->rspq);
2791 } else
2792 t4_intr_handler(adap)(0, adap);
2793}
2794#endif
2795
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302796static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2797{
2798 struct port_info *pi = netdev_priv(dev);
2799 struct adapter *adap = pi->adapter;
2800 struct sched_class *e;
2801 struct ch_sched_params p;
2802 struct ch_sched_queue qe;
2803 u32 req_rate;
2804 int err = 0;
2805
2806 if (!can_sched(dev))
2807 return -ENOTSUPP;
2808
2809 if (index < 0 || index > pi->nqsets - 1)
2810 return -EINVAL;
2811
2812 if (!(adap->flags & FULL_INIT_DONE)) {
2813 dev_err(adap->pdev_dev,
2814 "Failed to rate limit on queue %d. Link Down?\n",
2815 index);
2816 return -EINVAL;
2817 }
2818
2819 /* Convert from Mbps to Kbps */
2820 req_rate = rate << 10;
2821
2822 /* Max rate is 10 Gbps */
2823 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2824 dev_err(adap->pdev_dev,
2825 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2826 rate, SCHED_MAX_RATE_KBPS);
2827 return -ERANGE;
2828 }
2829
2830 /* First unbind the queue from any existing class */
2831 memset(&qe, 0, sizeof(qe));
2832 qe.queue = index;
2833 qe.class = SCHED_CLS_NONE;
2834
2835 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2836 if (err) {
2837 dev_err(adap->pdev_dev,
2838 "Unbinding Queue %d on port %d fail. Err: %d\n",
2839 index, pi->port_id, err);
2840 return err;
2841 }
2842
2843 /* Queue already unbound */
2844 if (!req_rate)
2845 return 0;
2846
2847 /* Fetch any available unused or matching scheduling class */
2848 memset(&p, 0, sizeof(p));
2849 p.type = SCHED_CLASS_TYPE_PACKET;
2850 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2851 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2852 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2853 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2854 p.u.params.channel = pi->tx_chan;
2855 p.u.params.class = SCHED_CLS_NONE;
2856 p.u.params.minrate = 0;
2857 p.u.params.maxrate = req_rate;
2858 p.u.params.weight = 0;
2859 p.u.params.pktsize = dev->mtu;
2860
2861 e = cxgb4_sched_class_alloc(dev, &p);
2862 if (!e)
2863 return -ENOMEM;
2864
2865 /* Bind the queue to a scheduling class */
2866 memset(&qe, 0, sizeof(qe));
2867 qe.queue = index;
2868 qe.class = e->idx;
2869
2870 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2871 if (err)
2872 dev_err(adap->pdev_dev,
2873 "Queue rate limiting failed. Err: %d\n", err);
2874 return err;
2875}
2876
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302877static int cxgb_setup_tc_flower(struct net_device *dev,
2878 struct tc_cls_flower_offload *cls_flower)
2879{
2880 if (!is_classid_clsact_ingress(cls_flower->common.classid) ||
2881 cls_flower->common.chain_index)
2882 return -EOPNOTSUPP;
2883
2884 switch (cls_flower->command) {
2885 case TC_CLSFLOWER_REPLACE:
2886 return cxgb4_tc_flower_replace(dev, cls_flower);
2887 case TC_CLSFLOWER_DESTROY:
2888 return cxgb4_tc_flower_destroy(dev, cls_flower);
2889 case TC_CLSFLOWER_STATS:
2890 return cxgb4_tc_flower_stats(dev, cls_flower);
2891 default:
2892 return -EOPNOTSUPP;
2893 }
2894}
2895
Jiri Pirkof7323042017-08-07 10:15:20 +02002896static int cxgb_setup_tc_cls_u32(struct net_device *dev,
Jiri Pirkof7323042017-08-07 10:15:20 +02002897 struct tc_cls_u32_offload *cls_u32)
2898{
Chris Mi7f3b39d2017-08-18 07:24:20 -04002899 if (!is_classid_clsact_ingress(cls_u32->common.classid) ||
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002900 cls_u32->common.chain_index)
Jiri Pirkof7323042017-08-07 10:15:20 +02002901 return -EOPNOTSUPP;
2902
2903 switch (cls_u32->command) {
2904 case TC_CLSU32_NEW_KNODE:
2905 case TC_CLSU32_REPLACE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002906 return cxgb4_config_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002907 case TC_CLSU32_DELETE_KNODE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02002908 return cxgb4_delete_knode(dev, cls_u32);
Jiri Pirkof7323042017-08-07 10:15:20 +02002909 default:
2910 return -EOPNOTSUPP;
2911 }
2912}
2913
Jiri Pirko2572ac52017-08-07 10:15:17 +02002914static int cxgb_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02002915 void *type_data)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302916{
2917 struct port_info *pi = netdev2pinfo(dev);
2918 struct adapter *adap = netdev2adap(dev);
2919
2920 if (!(adap->flags & FULL_INIT_DONE)) {
2921 dev_err(adap->pdev_dev,
2922 "Failed to setup tc on port %d. Link Down?\n",
2923 pi->port_id);
2924 return -EINVAL;
2925 }
2926
Jiri Pirkof7323042017-08-07 10:15:20 +02002927 switch (type) {
2928 case TC_SETUP_CLSU32:
Jiri Pirkode4784c2017-08-07 10:15:32 +02002929 return cxgb_setup_tc_cls_u32(dev, type_data);
Kumar Sanghvi6a345b32017-09-21 23:41:13 +05302930 case TC_SETUP_CLSFLOWER:
2931 return cxgb_setup_tc_flower(dev, type_data);
Jiri Pirkof7323042017-08-07 10:15:20 +02002932 default:
2933 return -EOPNOTSUPP;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302934 }
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302935}
2936
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302937static netdev_features_t cxgb_fix_features(struct net_device *dev,
2938 netdev_features_t features)
2939{
2940 /* Disable GRO, if RX_CSUM is disabled */
2941 if (!(features & NETIF_F_RXCSUM))
2942 features &= ~NETIF_F_GRO;
2943
2944 return features;
2945}
2946
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002947static const struct net_device_ops cxgb4_netdev_ops = {
2948 .ndo_open = cxgb_open,
2949 .ndo_stop = cxgb_close,
2950 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002951 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002952 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002953 .ndo_set_rx_mode = cxgb_set_rxmode,
2954 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00002955 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002956 .ndo_validate_addr = eth_validate_addr,
2957 .ndo_do_ioctl = cxgb_ioctl,
2958 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002959#ifdef CONFIG_NET_POLL_CONTROLLER
2960 .ndo_poll_controller = cxgb_netpoll,
2961#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05302962#ifdef CONFIG_CHELSIO_T4_FCOE
2963 .ndo_fcoe_enable = cxgb_fcoe_enable,
2964 .ndo_fcoe_disable = cxgb_fcoe_disable,
2965#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302966 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302967 .ndo_setup_tc = cxgb_setup_tc,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302968 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002969};
2970
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302971#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302972static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2973 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302974 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302975 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302976 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302977 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05302978};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302979#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05302980
2981static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2982{
2983 struct adapter *adapter = netdev2adap(dev);
2984
2985 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2986 strlcpy(info->version, cxgb4_driver_version,
2987 sizeof(info->version));
2988 strlcpy(info->bus_info, pci_name(adapter->pdev),
2989 sizeof(info->bus_info));
2990}
2991
2992static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
2993 .get_drvinfo = get_drvinfo,
2994};
2995
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002996void t4_fatal_err(struct adapter *adap)
2997{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05302998 int port;
2999
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03003000 if (pci_channel_offline(adap->pdev))
3001 return;
3002
Hariprasad Shenai3be06792017-01-13 21:55:26 +05303003 /* Disable the SGE since ULDs are going to free resources that
3004 * could be exposed to the adapter. RDMA MWs for example...
3005 */
3006 t4_shutdown_adapter(adap);
3007 for_each_port(adap, port) {
3008 struct net_device *dev = adap->port[port];
3009
3010 /* If we get here in very early initialization the network
3011 * devices may not have been set up yet.
3012 */
3013 if (!dev)
3014 continue;
3015
3016 netif_tx_stop_all_queues(dev);
3017 netif_carrier_off(dev);
3018 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003019 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
3020}
3021
3022static void setup_memwin(struct adapter *adap)
3023{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303024 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003025
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05303026 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003027}
3028
3029static void setup_memwin_rdma(struct adapter *adap)
3030{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003031 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303032 u32 start;
3033 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003034
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05303035 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
3036 start &= PCI_BASE_ADDRESS_MEM_MASK;
3037 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003038 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
3039 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303040 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
3041 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003042 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303043 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003044 adap->vres.ocq.start);
3045 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05303046 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003047 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003048}
3049
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003050static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
3051{
3052 u32 v;
3053 int ret;
3054
3055 /* get device capabilities */
3056 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303057 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3058 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303059 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303060 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003061 if (ret < 0)
3062 return ret;
3063
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303064 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3065 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303066 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003067 if (ret < 0)
3068 return ret;
3069
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303070 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003071 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05303072 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
3073 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003074 if (ret < 0)
3075 return ret;
3076
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303077 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303078 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
3079 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003080 if (ret < 0)
3081 return ret;
3082
3083 t4_sge_init(adap);
3084
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003085 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303086 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303087 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303088 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
3089 v = t4_read_reg(adap, TP_PIO_DATA_A);
3090 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003091
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003092 /* first 4 Tx modulation queues point to consecutive Tx channels */
3093 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303094 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
3095 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003096
3097 /* associate each Tx modulation queue with consecutive Tx channels */
3098 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303099 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303100 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303101 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303102 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303103 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303104 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003105
3106#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3107 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303108 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3109 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3110 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3111 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3112 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3113 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3114 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3115 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3116 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3117 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003118 }
3119
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003120 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303121 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003122}
3123
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003124/*
3125 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3126 */
3127#define MAX_ATIDS 8192U
3128
3129/*
3130 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003131 *
3132 * If the firmware we're dealing with has Configuration File support, then
3133 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003134 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003135
3136/*
3137 * Tweak configuration based on module parameters, etc. Most of these have
3138 * defaults assigned to them by Firmware Configuration Files (if we're using
3139 * them) but need to be explicitly set if we're using hard-coded
3140 * initialization. But even in the case of using Firmware Configuration
3141 * Files, we'd like to expose the ability to change these via module
3142 * parameters so these are essentially common tweaks/settings for
3143 * Configuration Files and hard-coded initialization ...
3144 */
3145static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003146{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003147 /*
3148 * Fix up various Host-Dependent Parameters like Page Size, Cache
3149 * Line Size, etc. The firmware default is for a 4KB Page Size and
3150 * 64B Cache Line Size ...
3151 */
3152 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003153
Vipul Pandya636f9d32012-09-26 02:39:39 +00003154 /*
3155 * Process module parameters which affect early initialization.
3156 */
3157 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3158 dev_err(&adapter->pdev->dev,
3159 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3160 rx_dma_offset);
3161 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003162 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303163 t4_set_reg_field(adapter, SGE_CONTROL_A,
3164 PKTSHIFT_V(PKTSHIFT_M),
3165 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003166
Vipul Pandya636f9d32012-09-26 02:39:39 +00003167 /*
3168 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3169 * adds the pseudo header itself.
3170 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303171 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3172 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003173
3174 return 0;
3175}
3176
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303177/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3178 * unto themselves and they contain their own firmware to perform their
3179 * tasks ...
3180 */
3181static int phy_aq1202_version(const u8 *phy_fw_data,
3182 size_t phy_fw_size)
3183{
3184 int offset;
3185
3186 /* At offset 0x8 you're looking for the primary image's
3187 * starting offset which is 3 Bytes wide
3188 *
3189 * At offset 0xa of the primary image, you look for the offset
3190 * of the DRAM segment which is 3 Bytes wide.
3191 *
3192 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3193 * wide
3194 */
3195 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3196 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3197 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3198
3199 offset = le24(phy_fw_data + 0x8) << 12;
3200 offset = le24(phy_fw_data + offset + 0xa);
3201 return be16(phy_fw_data + offset + 0x27e);
3202
3203 #undef be16
3204 #undef le16
3205 #undef le24
3206}
3207
3208static struct info_10gbt_phy_fw {
3209 unsigned int phy_fw_id; /* PCI Device ID */
3210 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3211 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3212 int phy_flash; /* Has FLASH for PHY Firmware */
3213} phy_info_array[] = {
3214 {
3215 PHY_AQ1202_DEVICEID,
3216 PHY_AQ1202_FIRMWARE,
3217 phy_aq1202_version,
3218 1,
3219 },
3220 {
3221 PHY_BCM84834_DEVICEID,
3222 PHY_BCM84834_FIRMWARE,
3223 NULL,
3224 0,
3225 },
3226 { 0, NULL, NULL },
3227};
3228
3229static struct info_10gbt_phy_fw *find_phy_info(int devid)
3230{
3231 int i;
3232
3233 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3234 if (phy_info_array[i].phy_fw_id == devid)
3235 return &phy_info_array[i];
3236 }
3237 return NULL;
3238}
3239
3240/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3241 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3242 * we return a negative error number. If we transfer new firmware we return 1
3243 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3244 */
3245static int adap_init0_phy(struct adapter *adap)
3246{
3247 const struct firmware *phyf;
3248 int ret;
3249 struct info_10gbt_phy_fw *phy_info;
3250
3251 /* Use the device ID to determine which PHY file to flash.
3252 */
3253 phy_info = find_phy_info(adap->pdev->device);
3254 if (!phy_info) {
3255 dev_warn(adap->pdev_dev,
3256 "No PHY Firmware file found for this PHY\n");
3257 return -EOPNOTSUPP;
3258 }
3259
3260 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3261 * use that. The adapter firmware provides us with a memory buffer
3262 * where we can load a PHY firmware file from the host if we want to
3263 * override the PHY firmware File in flash.
3264 */
3265 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3266 adap->pdev_dev);
3267 if (ret < 0) {
3268 /* For adapters without FLASH attached to PHY for their
3269 * firmware, it's obviously a fatal error if we can't get the
3270 * firmware to the adapter. For adapters with PHY firmware
3271 * FLASH storage, it's worth a warning if we can't find the
3272 * PHY Firmware but we'll neuter the error ...
3273 */
3274 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3275 "/lib/firmware/%s, error %d\n",
3276 phy_info->phy_fw_file, -ret);
3277 if (phy_info->phy_flash) {
3278 int cur_phy_fw_ver = 0;
3279
3280 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3281 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3282 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3283 ret = 0;
3284 }
3285
3286 return ret;
3287 }
3288
3289 /* Load PHY Firmware onto adapter.
3290 */
3291 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3292 phy_info->phy_fw_version,
3293 (u8 *)phyf->data, phyf->size);
3294 if (ret < 0)
3295 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3296 -ret);
3297 else if (ret > 0) {
3298 int new_phy_fw_ver = 0;
3299
3300 if (phy_info->phy_fw_version)
3301 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3302 phyf->size);
3303 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3304 "Firmware /lib/firmware/%s, version %#x\n",
3305 phy_info->phy_fw_file, new_phy_fw_ver);
3306 }
3307
3308 release_firmware(phyf);
3309
3310 return ret;
3311}
3312
Vipul Pandya636f9d32012-09-26 02:39:39 +00003313/*
3314 * Attempt to initialize the adapter via a Firmware Configuration File.
3315 */
3316static int adap_init0_config(struct adapter *adapter, int reset)
3317{
3318 struct fw_caps_config_cmd caps_cmd;
3319 const struct firmware *cf;
3320 unsigned long mtype = 0, maddr = 0;
3321 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303322 int ret;
3323 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003324 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303325 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003326
3327 /*
3328 * Reset device if necessary.
3329 */
3330 if (reset) {
3331 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303332 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003333 if (ret < 0)
3334 goto bye;
3335 }
3336
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303337 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3338 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3339 * to be performed after any global adapter RESET above since some
3340 * PHYs only have local RAM copies of the PHY firmware.
3341 */
3342 if (is_10gbt_device(adapter->pdev->device)) {
3343 ret = adap_init0_phy(adapter);
3344 if (ret < 0)
3345 goto bye;
3346 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003347 /*
3348 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3349 * then use that. Otherwise, use the configuration file stored
3350 * in the adapter flash ...
3351 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303352 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003353 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303354 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003355 break;
3356 case CHELSIO_T5:
3357 fw_config_file = FW5_CFNAME;
3358 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303359 case CHELSIO_T6:
3360 fw_config_file = FW6_CFNAME;
3361 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003362 default:
3363 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3364 adapter->pdev->device);
3365 ret = -EINVAL;
3366 goto bye;
3367 }
3368
3369 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003370 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303371 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003372 mtype = FW_MEMTYPE_CF_FLASH;
3373 maddr = t4_flash_cfg_addr(adapter);
3374 } else {
3375 u32 params[7], val[7];
3376
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303377 sprintf(fw_config_file_path,
3378 "/lib/firmware/%s", fw_config_file);
3379 config_name = fw_config_file_path;
3380
Vipul Pandya636f9d32012-09-26 02:39:39 +00003381 if (cf->size >= FLASH_CFG_MAX_SIZE)
3382 ret = -ENOMEM;
3383 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303384 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3385 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003386 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303387 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003388 if (ret == 0) {
3389 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303390 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003391 * sizes have to be in terms of multiples of 4
3392 * bytes. So, if the Configuration File isn't
3393 * a multiple of 4 bytes in length we'll have
3394 * to write that out separately since we can't
3395 * guarantee that the bytes following the
3396 * residual byte in the buffer returned by
3397 * request_firmware() are zeroed out ...
3398 */
3399 size_t resid = cf->size & 0x3;
3400 size_t size = cf->size & ~0x3;
3401 __be32 *data = (__be32 *)cf->data;
3402
Hariprasad Shenai51678652014-11-21 12:52:02 +05303403 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3404 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003405
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303406 spin_lock(&adapter->win0_lock);
3407 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3408 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003409 if (ret == 0 && resid != 0) {
3410 union {
3411 __be32 word;
3412 char buf[4];
3413 } last;
3414 int i;
3415
3416 last.word = data[size >> 2];
3417 for (i = resid; i < 4; i++)
3418 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303419 ret = t4_memory_rw(adapter, 0, mtype,
3420 maddr + size,
3421 4, &last.word,
3422 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003423 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303424 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003425 }
3426 }
3427
3428 release_firmware(cf);
3429 if (ret)
3430 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003431 }
3432
Vipul Pandya636f9d32012-09-26 02:39:39 +00003433 /*
3434 * Issue a Capability Configuration command to the firmware to get it
3435 * to parse the Configuration File. We don't use t4_fw_config_file()
3436 * because we want the ability to modify various features after we've
3437 * processed the configuration file ...
3438 */
3439 memset(&caps_cmd, 0, sizeof(caps_cmd));
3440 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303441 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3442 FW_CMD_REQUEST_F |
3443 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303444 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303445 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3446 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3447 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003448 FW_LEN16(caps_cmd));
3449 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3450 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303451
3452 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3453 * Configuration File in FLASH), our last gasp effort is to use the
3454 * Firmware Configuration File which is embedded in the firmware. A
3455 * very few early versions of the firmware didn't have one embedded
3456 * but we can ignore those.
3457 */
3458 if (ret == -ENOENT) {
3459 memset(&caps_cmd, 0, sizeof(caps_cmd));
3460 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303461 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3462 FW_CMD_REQUEST_F |
3463 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303464 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3465 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3466 sizeof(caps_cmd), &caps_cmd);
3467 config_name = "Firmware Default";
3468 }
3469
3470 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003471 if (ret < 0)
3472 goto bye;
3473
Vipul Pandya636f9d32012-09-26 02:39:39 +00003474 finiver = ntohl(caps_cmd.finiver);
3475 finicsum = ntohl(caps_cmd.finicsum);
3476 cfcsum = ntohl(caps_cmd.cfcsum);
3477 if (finicsum != cfcsum)
3478 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3479 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3480 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003481
Vipul Pandya636f9d32012-09-26 02:39:39 +00003482 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003483 * And now tell the firmware to use the configuration we just loaded.
3484 */
3485 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303486 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3487 FW_CMD_REQUEST_F |
3488 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303489 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003490 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3491 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003492 if (ret < 0)
3493 goto bye;
3494
Vipul Pandya636f9d32012-09-26 02:39:39 +00003495 /*
3496 * Tweak configuration based on system architecture, module
3497 * parameters, etc.
3498 */
3499 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003500 if (ret < 0)
3501 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003502
Vipul Pandya636f9d32012-09-26 02:39:39 +00003503 /*
3504 * And finally tell the firmware to initialize itself using the
3505 * parameters from the Configuration File.
3506 */
3507 ret = t4_fw_initialize(adapter, adapter->mbox);
3508 if (ret < 0)
3509 goto bye;
3510
Hariprasad Shenai06640312015-01-13 15:19:25 +05303511 /* Emit Firmware Configuration File information and return
3512 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003513 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003514 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303515 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3516 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003517 return 0;
3518
3519 /*
3520 * Something bad happened. Return the error ... (If the "error"
3521 * is that there's no Configuration File on the adapter we don't
3522 * want to issue a warning since this is fairly common.)
3523 */
3524bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303525 if (config_issued && ret != -ENOENT)
3526 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3527 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003528 return ret;
3529}
3530
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303531static struct fw_info fw_info_array[] = {
3532 {
3533 .chip = CHELSIO_T4,
3534 .fs_name = FW4_CFNAME,
3535 .fw_mod_name = FW4_FNAME,
3536 .fw_hdr = {
3537 .chip = FW_HDR_CHIP_T4,
3538 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3539 .intfver_nic = FW_INTFVER(T4, NIC),
3540 .intfver_vnic = FW_INTFVER(T4, VNIC),
3541 .intfver_ri = FW_INTFVER(T4, RI),
3542 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3543 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3544 },
3545 }, {
3546 .chip = CHELSIO_T5,
3547 .fs_name = FW5_CFNAME,
3548 .fw_mod_name = FW5_FNAME,
3549 .fw_hdr = {
3550 .chip = FW_HDR_CHIP_T5,
3551 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3552 .intfver_nic = FW_INTFVER(T5, NIC),
3553 .intfver_vnic = FW_INTFVER(T5, VNIC),
3554 .intfver_ri = FW_INTFVER(T5, RI),
3555 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3556 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3557 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303558 }, {
3559 .chip = CHELSIO_T6,
3560 .fs_name = FW6_CFNAME,
3561 .fw_mod_name = FW6_FNAME,
3562 .fw_hdr = {
3563 .chip = FW_HDR_CHIP_T6,
3564 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3565 .intfver_nic = FW_INTFVER(T6, NIC),
3566 .intfver_vnic = FW_INTFVER(T6, VNIC),
3567 .intfver_ofld = FW_INTFVER(T6, OFLD),
3568 .intfver_ri = FW_INTFVER(T6, RI),
3569 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3570 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3571 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3572 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3573 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303574 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303575
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303576};
3577
3578static struct fw_info *find_fw_info(int chip)
3579{
3580 int i;
3581
3582 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3583 if (fw_info_array[i].chip == chip)
3584 return &fw_info_array[i];
3585 }
3586 return NULL;
3587}
3588
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003589/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003590 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003591 */
3592static int adap_init0(struct adapter *adap)
3593{
3594 int ret;
3595 u32 v, port_vec;
3596 enum dev_state state;
3597 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003598 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303599 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003600
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303601 /* Grab Firmware Device Log parameters as early as possible so we have
3602 * access to it for debugging, etc.
3603 */
3604 ret = t4_init_devlog_params(adap);
3605 if (ret < 0)
3606 return ret;
3607
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303608 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303609 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3610 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003611 if (ret < 0) {
3612 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3613 ret);
3614 return ret;
3615 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003616 if (ret == adap->mbox)
3617 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003618
Vipul Pandya636f9d32012-09-26 02:39:39 +00003619 /*
3620 * If we're the Master PF Driver and the device is uninitialized,
3621 * then let's consider upgrading the firmware ... (We always want
3622 * to check the firmware version number in order to A. get it for
3623 * later reporting and B. to warn if the currently loaded firmware
3624 * is excessively mismatched relative to the driver.)
3625 */
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303626
Ganesh Goudar760446f2017-07-20 18:28:48 +05303627 t4_get_version_info(adap);
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303628 ret = t4_check_fw_version(adap);
3629 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303630 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303631 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003632 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303633 struct fw_info *fw_info;
3634 struct fw_hdr *card_fw;
3635 const struct firmware *fw;
3636 const u8 *fw_data = NULL;
3637 unsigned int fw_size = 0;
3638
3639 /* This is the firmware whose headers the driver was compiled
3640 * against
3641 */
3642 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3643 if (fw_info == NULL) {
3644 dev_err(adap->pdev_dev,
3645 "unable to get firmware info for chip %d.\n",
3646 CHELSIO_CHIP_VERSION(adap->params.chip));
3647 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003648 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303649
3650 /* allocate memory to read the header of the firmware on the
3651 * card
3652 */
Michal Hocko752ade62017-05-08 15:57:27 -07003653 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303654
3655 /* Get FW from from /lib/firmware/ */
3656 ret = request_firmware(&fw, fw_info->fw_mod_name,
3657 adap->pdev_dev);
3658 if (ret < 0) {
3659 dev_err(adap->pdev_dev,
3660 "unable to load firmware image %s, error %d\n",
3661 fw_info->fw_mod_name, ret);
3662 } else {
3663 fw_data = fw->data;
3664 fw_size = fw->size;
3665 }
3666
3667 /* upgrade FW logic */
3668 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3669 state, &reset);
3670
3671 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003672 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003673 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303674
Vipul Pandya636f9d32012-09-26 02:39:39 +00003675 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303676 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003677 }
3678
3679 /*
3680 * Grab VPD parameters. This should be done after we establish a
3681 * connection to the firmware since some of the VPD parameters
3682 * (notably the Core Clock frequency) are retrieved via requests to
3683 * the firmware. On the other hand, we need these fairly early on
3684 * so we do this right after getting ahold of the firmware.
3685 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303686 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003687 if (ret < 0)
3688 goto bye;
3689
Vipul Pandya636f9d32012-09-26 02:39:39 +00003690 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003691 * Find out what ports are available to us. Note that we need to do
3692 * this before calling adap_init0_no_config() since it needs nports
3693 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003694 */
3695 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303696 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3697 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303698 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003699 if (ret < 0)
3700 goto bye;
3701
3702 adap->params.nports = hweight32(port_vec);
3703 adap->params.portvec = port_vec;
3704
Hariprasad Shenai06640312015-01-13 15:19:25 +05303705 /* If the firmware is initialized already, emit a simply note to that
3706 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003707 */
3708 if (state == DEV_STATE_INIT) {
3709 dev_info(adap->pdev_dev, "Coming up as %s: "\
3710 "Adapter already initialized\n",
3711 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003712 } else {
3713 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3714 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303715
3716 /* Find out whether we're dealing with a version of the
3717 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003718 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303719 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3720 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303721 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303722 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003723
Hariprasad Shenai06640312015-01-13 15:19:25 +05303724 /* If the firmware doesn't support Configuration Files,
3725 * return an error.
3726 */
3727 if (ret < 0) {
3728 dev_err(adap->pdev_dev, "firmware doesn't support "
3729 "Firmware Configuration Files\n");
3730 goto bye;
3731 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003732
Hariprasad Shenai06640312015-01-13 15:19:25 +05303733 /* The firmware provides us with a memory buffer where we can
3734 * load a Configuration File from the host if we want to
3735 * override the Configuration File in flash.
3736 */
3737 ret = adap_init0_config(adap, reset);
3738 if (ret == -ENOENT) {
3739 dev_err(adap->pdev_dev, "no Configuration File "
3740 "present on adapter.\n");
3741 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003742 }
3743 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303744 dev_err(adap->pdev_dev, "could not initialize "
3745 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003746 goto bye;
3747 }
3748 }
3749
Hariprasad Shenai06640312015-01-13 15:19:25 +05303750 /* Give the SGE code a chance to pull in anything that it needs ...
3751 * Note that this must be called after we retrieve our VPD parameters
3752 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003753 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303754 ret = t4_sge_init(adap);
3755 if (ret < 0)
3756 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003757
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003758 if (is_bypass_device(adap->pdev->device))
3759 adap->params.bypass = 1;
3760
Vipul Pandya636f9d32012-09-26 02:39:39 +00003761 /*
3762 * Grab some of our basic fundamental operating parameters.
3763 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003764#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303765 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3766 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003767
3768#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303769 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3770 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3771 FW_PARAMS_PARAM_Y_V(0) | \
3772 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003773
Vipul Pandya636f9d32012-09-26 02:39:39 +00003774 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003775 params[1] = FW_PARAM_PFVF(L2T_START);
3776 params[2] = FW_PARAM_PFVF(L2T_END);
3777 params[3] = FW_PARAM_PFVF(FILTER_START);
3778 params[4] = FW_PARAM_PFVF(FILTER_END);
3779 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303780 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003781 if (ret < 0)
3782 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003783 adap->sge.egr_start = val[0];
3784 adap->l2t_start = val[1];
3785 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003786 adap->tids.ftid_base = val[3];
3787 adap->tids.nftids = val[4] - val[3] + 1;
3788 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003789
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303790 /* qids (ingress/egress) returned from firmware can be anywhere
3791 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3792 * Hence driver needs to allocate memory for this range to
3793 * store the queue info. Get the highest IQFLINT/EQ index returned
3794 * in FW_EQ_*_CMD.alloc command.
3795 */
3796 params[0] = FW_PARAM_PFVF(EQ_END);
3797 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303798 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303799 if (ret < 0)
3800 goto bye;
3801 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3802 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3803
3804 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3805 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3806 if (!adap->sge.egr_map) {
3807 ret = -ENOMEM;
3808 goto bye;
3809 }
3810
3811 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3812 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3813 if (!adap->sge.ingr_map) {
3814 ret = -ENOMEM;
3815 goto bye;
3816 }
3817
3818 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303819 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303820 */
3821 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3822 sizeof(long), GFP_KERNEL);
3823 if (!adap->sge.starving_fl) {
3824 ret = -ENOMEM;
3825 goto bye;
3826 }
3827
3828 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3829 sizeof(long), GFP_KERNEL);
3830 if (!adap->sge.txq_maperr) {
3831 ret = -ENOMEM;
3832 goto bye;
3833 }
3834
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303835#ifdef CONFIG_DEBUG_FS
3836 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3837 sizeof(long), GFP_KERNEL);
3838 if (!adap->sge.blocked_fl) {
3839 ret = -ENOMEM;
3840 goto bye;
3841 }
3842#endif
3843
Anish Bhattb5a02f52015-01-14 15:17:34 -08003844 params[0] = FW_PARAM_PFVF(CLIP_START);
3845 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303846 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003847 if (ret < 0)
3848 goto bye;
3849 adap->clipt_start = val[0];
3850 adap->clipt_end = val[1];
3851
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303852 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3853 * Classes supported by the hardware/firmware so we hard code it here
3854 * for now.
3855 */
3856 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3857
Vipul Pandya636f9d32012-09-26 02:39:39 +00003858 /* query params related to active filter region */
3859 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3860 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303861 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003862 /* If Active filter size is set we enable establishing
3863 * offload connection through firmware work request
3864 */
3865 if ((val[0] != val[1]) && (ret >= 0)) {
3866 adap->flags |= FW_OFLD_CONN;
3867 adap->tids.aftid_base = val[0];
3868 adap->tids.aftid_end = val[1];
3869 }
3870
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003871 /* If we're running on newer firmware, let it know that we're
3872 * prepared to deal with encapsulated CPL messages. Older
3873 * firmware won't understand this and we'll just get
3874 * unencapsulated messages ...
3875 */
3876 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3877 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303878 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003879
Vipul Pandya636f9d32012-09-26 02:39:39 +00003880 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303881 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3882 * capability. Earlier versions of the firmware didn't have the
3883 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3884 * permission to use ULPTX MEMWRITE DSGL.
3885 */
3886 if (is_t4(adap->params.chip)) {
3887 adap->params.ulptx_memwrite_dsgl = false;
3888 } else {
3889 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303890 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303891 1, params, val);
3892 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3893 }
3894
Steve Wise086de572016-09-16 07:54:49 -07003895 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3896 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3897 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3898 1, params, val);
3899 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3900
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303901 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003902 * Get device capabilities so we can determine what resources we need
3903 * to manage.
3904 */
3905 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303906 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3907 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303908 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003909 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3910 &caps_cmd);
3911 if (ret < 0)
3912 goto bye;
3913
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003914 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003915 /* query offload-related parameters */
3916 params[0] = FW_PARAM_DEV(NTID);
3917 params[1] = FW_PARAM_PFVF(SERVER_START);
3918 params[2] = FW_PARAM_PFVF(SERVER_END);
3919 params[3] = FW_PARAM_PFVF(TDDP_START);
3920 params[4] = FW_PARAM_PFVF(TDDP_END);
3921 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303922 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003923 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003924 if (ret < 0)
3925 goto bye;
3926 adap->tids.ntids = val[0];
3927 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3928 adap->tids.stid_base = val[1];
3929 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003930 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003931 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003932 * region into two parts. Regular filters get 1/3rd and server
3933 * filters get 2/3rd part. This is only enabled if workarond
3934 * path is enabled.
3935 * 1. For regular filters.
3936 * 2. Server filter: This are special filters which are used
3937 * to redirect SYN packets to offload queue.
3938 */
3939 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3940 adap->tids.sftid_base = adap->tids.ftid_base +
3941 DIV_ROUND_UP(adap->tids.nftids, 3);
3942 adap->tids.nsftids = adap->tids.nftids -
3943 DIV_ROUND_UP(adap->tids.nftids, 3);
3944 adap->tids.nftids = adap->tids.sftid_base -
3945 adap->tids.ftid_base;
3946 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003947 adap->vres.ddp.start = val[3];
3948 adap->vres.ddp.size = val[4] - val[3] + 1;
3949 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00003950
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003951 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303952 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003953 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003954 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003955 params[0] = FW_PARAM_PFVF(STAG_START);
3956 params[1] = FW_PARAM_PFVF(STAG_END);
3957 params[2] = FW_PARAM_PFVF(RQ_START);
3958 params[3] = FW_PARAM_PFVF(RQ_END);
3959 params[4] = FW_PARAM_PFVF(PBL_START);
3960 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303961 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003962 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003963 if (ret < 0)
3964 goto bye;
3965 adap->vres.stag.start = val[0];
3966 adap->vres.stag.size = val[1] - val[0] + 1;
3967 adap->vres.rq.start = val[2];
3968 adap->vres.rq.size = val[3] - val[2] + 1;
3969 adap->vres.pbl.start = val[4];
3970 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003971
3972 params[0] = FW_PARAM_PFVF(SQRQ_START);
3973 params[1] = FW_PARAM_PFVF(SQRQ_END);
3974 params[2] = FW_PARAM_PFVF(CQ_START);
3975 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003976 params[4] = FW_PARAM_PFVF(OCQ_START);
3977 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303978 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303979 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003980 if (ret < 0)
3981 goto bye;
3982 adap->vres.qp.start = val[0];
3983 adap->vres.qp.size = val[1] - val[0] + 1;
3984 adap->vres.cq.start = val[2];
3985 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003986 adap->vres.ocq.start = val[4];
3987 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303988
3989 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3990 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303991 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303992 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303993 if (ret < 0) {
3994 adap->params.max_ordird_qp = 8;
3995 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3996 ret = 0;
3997 } else {
3998 adap->params.max_ordird_qp = val[0];
3999 adap->params.max_ird_adapter = val[1];
4000 }
4001 dev_info(adap->pdev_dev,
4002 "max_ordird_qp %d max_ird_adapter %d\n",
4003 adap->params.max_ordird_qp,
4004 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304005 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004006 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00004007 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004008 params[0] = FW_PARAM_PFVF(ISCSI_START);
4009 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304010 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00004011 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004012 if (ret < 0)
4013 goto bye;
4014 adap->vres.iscsi.start = val[0];
4015 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304016 /* LIO target and cxgb4i initiaitor */
4017 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004018 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304019 if (caps_cmd.cryptocaps) {
4020 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05304021 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
4022 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
4023 params, val);
4024 if (ret < 0) {
4025 if (ret != -EINVAL)
4026 goto bye;
4027 } else {
4028 adap->vres.ncrypto_fc = val[0];
4029 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304030 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
4031 adap->num_uld += 1;
4032 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004033#undef FW_PARAM_PFVF
4034#undef FW_PARAM_DEV
4035
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304036 /* The MTU/MSS Table is initialized by now, so load their values. If
4037 * we're initializing the adapter, then we'll make any modifications
4038 * we want to the MTU/MSS Table and also initialize the congestion
4039 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00004040 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004041 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304042 if (state != DEV_STATE_INIT) {
4043 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00004044
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05304045 /* The default MTU Table contains values 1492 and 1500.
4046 * However, for TCP, it's better to have two values which are
4047 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
4048 * This allows us to have a TCP Data Payload which is a
4049 * multiple of 8 regardless of what combination of TCP Options
4050 * are in use (always a multiple of 4 bytes) which is
4051 * important for performance reasons. For instance, if no
4052 * options are in use, then we have a 20-byte IP header and a
4053 * 20-byte TCP header. In this case, a 1500-byte MSS would
4054 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
4055 * which is not a multiple of 8. So using an MSS of 1488 in
4056 * this case results in a TCP Data Payload of 1448 bytes which
4057 * is a multiple of 8. On the other hand, if 12-byte TCP Time
4058 * Stamps have been negotiated, then an MTU of 1500 bytes
4059 * results in a TCP Data Payload of 1448 bytes which, as
4060 * above, is a multiple of 8 bytes ...
4061 */
4062 for (i = 0; i < NMTUS; i++)
4063 if (adap->params.mtus[i] == 1492) {
4064 adap->params.mtus[i] = 1488;
4065 break;
4066 }
4067
4068 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4069 adap->params.b_wnd);
4070 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05304071 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00004072 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05304073 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004074 return 0;
4075
4076 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00004077 * Something bad happened. If a command timed out or failed with EIO
4078 * FW does not operate within its spec or something catastrophic
4079 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004080 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00004081bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304082 kfree(adap->sge.egr_map);
4083 kfree(adap->sge.ingr_map);
4084 kfree(adap->sge.starving_fl);
4085 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304086#ifdef CONFIG_DEBUG_FS
4087 kfree(adap->sge.blocked_fl);
4088#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004089 if (ret != -ETIMEDOUT && ret != -EIO)
4090 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004091 return ret;
4092}
4093
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004094/* EEH callbacks */
4095
4096static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4097 pci_channel_state_t state)
4098{
4099 int i;
4100 struct adapter *adap = pci_get_drvdata(pdev);
4101
4102 if (!adap)
4103 goto out;
4104
4105 rtnl_lock();
4106 adap->flags &= ~FW_OK;
4107 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004108 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004109 for_each_port(adap, i) {
4110 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004111 if (dev) {
4112 netif_device_detach(dev);
4113 netif_carrier_off(dev);
4114 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004115 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004116 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304117 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004118 if (adap->flags & FULL_INIT_DONE)
4119 cxgb_down(adap);
4120 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004121 if ((adap->flags & DEV_ENABLED)) {
4122 pci_disable_device(pdev);
4123 adap->flags &= ~DEV_ENABLED;
4124 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004125out: return state == pci_channel_io_perm_failure ?
4126 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4127}
4128
4129static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4130{
4131 int i, ret;
4132 struct fw_caps_config_cmd c;
4133 struct adapter *adap = pci_get_drvdata(pdev);
4134
4135 if (!adap) {
4136 pci_restore_state(pdev);
4137 pci_save_state(pdev);
4138 return PCI_ERS_RESULT_RECOVERED;
4139 }
4140
Gavin Shan144be3d2014-01-23 12:27:34 +08004141 if (!(adap->flags & DEV_ENABLED)) {
4142 if (pci_enable_device(pdev)) {
4143 dev_err(&pdev->dev, "Cannot reenable PCI "
4144 "device after reset\n");
4145 return PCI_ERS_RESULT_DISCONNECT;
4146 }
4147 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004148 }
4149
4150 pci_set_master(pdev);
4151 pci_restore_state(pdev);
4152 pci_save_state(pdev);
4153 pci_cleanup_aer_uncorrect_error_status(pdev);
4154
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304155 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004156 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304157 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004158 return PCI_ERS_RESULT_DISCONNECT;
4159 adap->flags |= FW_OK;
4160 if (adap_init1(adap, &c))
4161 return PCI_ERS_RESULT_DISCONNECT;
4162
4163 for_each_port(adap, i) {
4164 struct port_info *p = adap2pinfo(adap, i);
4165
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304166 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004167 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004168 if (ret < 0)
4169 return PCI_ERS_RESULT_DISCONNECT;
4170 p->viid = ret;
4171 p->xact_addr_filt = -1;
4172 }
4173
4174 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4175 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004176 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004177 if (cxgb_up(adap))
4178 return PCI_ERS_RESULT_DISCONNECT;
4179 return PCI_ERS_RESULT_RECOVERED;
4180}
4181
4182static void eeh_resume(struct pci_dev *pdev)
4183{
4184 int i;
4185 struct adapter *adap = pci_get_drvdata(pdev);
4186
4187 if (!adap)
4188 return;
4189
4190 rtnl_lock();
4191 for_each_port(adap, i) {
4192 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004193 if (dev) {
4194 if (netif_running(dev)) {
4195 link_start(dev);
4196 cxgb_set_rxmode(dev);
4197 }
4198 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004199 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004200 }
4201 rtnl_unlock();
4202}
4203
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004204static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004205 .error_detected = eeh_err_detected,
4206 .slot_reset = eeh_slot_reset,
4207 .resume = eeh_resume,
4208};
4209
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304210/* Return true if the Link Configuration supports "High Speeds" (those greater
4211 * than 1Gb/s).
4212 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304213static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004214{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304215 unsigned int speeds, high_speeds;
4216
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304217 speeds = FW_PORT_CAP32_SPEED_V(FW_PORT_CAP32_SPEED_G(lc->pcaps));
4218 high_speeds = speeds &
4219 ~(FW_PORT_CAP32_SPEED_100M | FW_PORT_CAP32_SPEED_1G);
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304220
4221 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004222}
4223
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004224/*
4225 * Perform default configuration of DMA queues depending on the number and type
4226 * of ports we found and the number of available CPUs. Most settings can be
4227 * modified by the admin prior to actual use.
4228 */
Bill Pemberton91744942012-12-03 09:23:02 -05004229static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004230{
4231 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304232 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004233#ifndef CONFIG_CHELSIO_T4_DCB
4234 int q10g = 0;
4235#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004236
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304237 /* Reduce memory usage in kdump environment, disable all offload.
4238 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304239 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304240 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304241 adap->params.crypto = 0;
4242 }
4243
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304244 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004245#ifdef CONFIG_CHELSIO_T4_DCB
4246 /* For Data Center Bridging support we need to be able to support up
4247 * to 8 Traffic Priorities; each of which will be assigned to its
4248 * own TX Queue in order to prevent Head-Of-Line Blocking.
4249 */
4250 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4251 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4252 MAX_ETH_QSETS, adap->params.nports * 8);
4253 BUG_ON(1);
4254 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004255
Anish Bhatt688848b2014-06-19 21:37:13 -07004256 for_each_port(adap, i) {
4257 struct port_info *pi = adap2pinfo(adap, i);
4258
4259 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304260 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004261 qidx += pi->nqsets;
4262 }
4263#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004264 /*
4265 * We default to 1 queue per non-10G port and up to # of cores queues
4266 * per 10G port.
4267 */
4268 if (n10g)
4269 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004270 if (q10g > netif_get_num_default_rss_queues())
4271 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004272
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304273 if (is_kdump_kernel())
4274 q10g = 1;
4275
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004276 for_each_port(adap, i) {
4277 struct port_info *pi = adap2pinfo(adap, i);
4278
4279 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304280 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004281 qidx += pi->nqsets;
4282 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004283#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004284
4285 s->ethqsets = qidx;
4286 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4287
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304288 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004289 /*
4290 * For offload we use 1 queue/channel if all ports are up to 1G,
4291 * otherwise we divide all available queues amongst the channels
4292 * capped by the number of available cores.
4293 */
4294 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304295 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304296 s->ofldqsets = roundup(i, adap->params.nports);
4297 } else {
4298 s->ofldqsets = adap->params.nports;
4299 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004300 }
4301
4302 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4303 struct sge_eth_rxq *r = &s->ethrxq[i];
4304
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304305 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004306 r->fl.size = 72;
4307 }
4308
4309 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4310 s->ethtxq[i].q.size = 1024;
4311
4312 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4313 s->ctrlq[i].q.size = 512;
4314
Atul Guptaa45695042017-07-04 16:46:20 +05304315 if (!is_t4(adap->params.chip))
4316 s->ptptxq.q.size = 8;
4317
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304318 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304319 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004320}
4321
4322/*
4323 * Reduce the number of Ethernet queues across all ports to at most n.
4324 * n provides at least one queue per port.
4325 */
Bill Pemberton91744942012-12-03 09:23:02 -05004326static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004327{
4328 int i;
4329 struct port_info *pi;
4330
4331 while (n < adap->sge.ethqsets)
4332 for_each_port(adap, i) {
4333 pi = adap2pinfo(adap, i);
4334 if (pi->nqsets > 1) {
4335 pi->nqsets--;
4336 adap->sge.ethqsets--;
4337 if (adap->sge.ethqsets <= n)
4338 break;
4339 }
4340 }
4341
4342 n = 0;
4343 for_each_port(adap, i) {
4344 pi = adap2pinfo(adap, i);
4345 pi->first_qset = n;
4346 n += pi->nqsets;
4347 }
4348}
4349
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304350static int get_msix_info(struct adapter *adap)
4351{
4352 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304353 unsigned int max_ingq = 0;
4354
4355 if (is_offload(adap))
4356 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4357 if (is_pci_uld(adap))
4358 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4359
4360 if (!max_ingq)
4361 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304362
4363 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4364 if (!msix_info)
4365 return -ENOMEM;
4366
4367 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4368 sizeof(long), GFP_KERNEL);
4369 if (!adap->msix_bmap_ulds.msix_bmap) {
4370 kfree(msix_info);
4371 return -ENOMEM;
4372 }
4373 spin_lock_init(&adap->msix_bmap_ulds.lock);
4374 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304375out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304376 return 0;
4377}
4378
4379static void free_msix_info(struct adapter *adap)
4380{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304381 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304382 return;
4383
4384 kfree(adap->msix_info_ulds);
4385 kfree(adap->msix_bmap_ulds.msix_bmap);
4386}
4387
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004388/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4389#define EXTRA_VECS 2
4390
Bill Pemberton91744942012-12-03 09:23:02 -05004391static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004392{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304393 int ofld_need = 0, uld_need = 0;
4394 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004395 struct sge *s = &adap->sge;
4396 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304397 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304398 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004399
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304400 if (is_pci_uld(adap))
4401 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4402 if (is_offload(adap))
4403 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304404 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304405 GFP_KERNEL);
4406 if (!entries)
4407 return -ENOMEM;
4408
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304409 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304410 if (get_msix_info(adap)) {
4411 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304412 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304413 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304414
4415 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004416 entries[i].entry = i;
4417
4418 want = s->max_ethqsets + EXTRA_VECS;
4419 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304420 want += adap->num_ofld_uld * s->ofldqsets;
4421 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004422 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304423 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304424 want += adap->num_uld * s->ofldqsets;
4425 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304426 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004427#ifdef CONFIG_CHELSIO_T4_DCB
4428 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4429 * each port.
4430 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304431 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004432#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304433 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004434#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304435 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4436 if (allocated < 0) {
4437 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4438 " not using MSI-X\n");
4439 kfree(entries);
4440 return allocated;
4441 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004442
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304443 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004444 * Every group gets its minimum requirement and NIC gets top
4445 * priority for leftovers.
4446 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304447 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004448 if (i < s->max_ethqsets) {
4449 s->max_ethqsets = i;
4450 if (i < s->ethqsets)
4451 reduce_ethqs(adap, i);
4452 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304453 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304454 if (allocated < want)
4455 s->nqs_per_uld = nchan;
4456 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304457 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304458 }
4459
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304460 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004461 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304462 if (is_uld(adap)) {
4463 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304464 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304465 adap->msix_info_ulds[j].idx = i;
4466 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304467 adap->msix_bmap_ulds.mapsize = j;
4468 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304469 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304470 "nic %d per uld %d\n",
4471 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004472
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304473 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004474 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004475}
4476
4477#undef EXTRA_VECS
4478
Bill Pemberton91744942012-12-03 09:23:02 -05004479static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004480{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304481 unsigned int i;
4482 int err;
4483
4484 err = t4_init_rss_mode(adap, adap->mbox);
4485 if (err)
4486 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004487
4488 for_each_port(adap, i) {
4489 struct port_info *pi = adap2pinfo(adap, i);
4490
4491 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4492 if (!pi->rss)
4493 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004494 }
4495 return 0;
4496}
4497
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304498static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4499 enum pci_bus_speed *speed,
4500 enum pcie_link_width *width)
4501{
4502 u32 lnkcap1, lnkcap2;
4503 int err1, err2;
4504
4505#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4506
4507 *speed = PCI_SPEED_UNKNOWN;
4508 *width = PCIE_LNK_WIDTH_UNKNOWN;
4509
4510 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4511 &lnkcap1);
4512 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4513 &lnkcap2);
4514 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4515 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4516 *speed = PCIE_SPEED_8_0GT;
4517 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4518 *speed = PCIE_SPEED_5_0GT;
4519 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4520 *speed = PCIE_SPEED_2_5GT;
4521 }
4522 if (!err1) {
4523 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4524 if (!lnkcap2) { /* pre-r3.0 */
4525 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4526 *speed = PCIE_SPEED_5_0GT;
4527 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4528 *speed = PCIE_SPEED_2_5GT;
4529 }
4530 }
4531
4532 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4533 return err1 ? err1 : err2 ? err2 : -EINVAL;
4534 return 0;
4535}
4536
4537static void cxgb4_check_pcie_caps(struct adapter *adap)
4538{
4539 enum pcie_link_width width, width_cap;
4540 enum pci_bus_speed speed, speed_cap;
4541
4542#define PCIE_SPEED_STR(speed) \
4543 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4544 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4545 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4546 "Unknown")
4547
4548 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4549 dev_warn(adap->pdev_dev,
4550 "Unable to determine PCIe device BW capabilities\n");
4551 return;
4552 }
4553
4554 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4555 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4556 dev_warn(adap->pdev_dev,
4557 "Unable to determine PCI Express bandwidth.\n");
4558 return;
4559 }
4560
4561 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4562 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4563 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4564 width, width_cap);
4565 if (speed < speed_cap || width < width_cap)
4566 dev_info(adap->pdev_dev,
4567 "A slot with more lanes and/or higher speed is "
4568 "suggested for optimal performance.\n");
4569}
4570
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304571/* Dump basic information about the adapter */
4572static void print_adapter_info(struct adapter *adapter)
4573{
Ganesh Goudar760446f2017-07-20 18:28:48 +05304574 /* Hardware/Firmware/etc. Version/Revision IDs */
4575 t4_dump_version_info(adapter);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304576
4577 /* Software/Hardware configuration */
4578 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4579 is_offload(adapter) ? "R" : "",
4580 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4581 (adapter->flags & USING_MSI) ? "MSI" : ""),
4582 is_offload(adapter) ? "Offload" : "non-Offload");
4583}
4584
Bill Pemberton91744942012-12-03 09:23:02 -05004585static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004586{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004587 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004588 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004589 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004590 const struct port_info *pi = netdev_priv(dev);
4591 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004592
4593 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4594 spd = " 2.5 GT/s";
4595 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4596 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004597 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4598 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004599
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304600 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304601 bufp += sprintf(bufp, "100M/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304602 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304603 bufp += sprintf(bufp, "1G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304604 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_10G)
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004605 bufp += sprintf(bufp, "10G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304606 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_25G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304607 bufp += sprintf(bufp, "25G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304608 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_40G)
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304609 bufp += sprintf(bufp, "40G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304610 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_50G)
4611 bufp += sprintf(bufp, "50G/");
4612 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_100G)
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304613 bufp += sprintf(bufp, "100G/");
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304614 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_200G)
4615 bufp += sprintf(bufp, "200G/");
4616 if (pi->link_cfg.pcaps & FW_PORT_CAP32_SPEED_400G)
4617 bufp += sprintf(bufp, "400G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004618 if (bufp != buf)
4619 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304620 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004621
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304622 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4623 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004624}
4625
Dimitris Michailidis06546392010-07-11 12:01:16 +00004626/*
4627 * Free the following resources:
4628 * - memory used for tables
4629 * - MSI/MSI-X
4630 * - net devices
4631 * - resources FW is holding for us
4632 */
4633static void free_some_resources(struct adapter *adapter)
4634{
4635 unsigned int i;
4636
Michal Hocko752ade62017-05-08 15:57:27 -07004637 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304638 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004639 kvfree(adapter->tids.tid_tab);
Kumar Sanghvie0f911c2017-09-21 23:41:16 +05304640 cxgb4_cleanup_tc_flower(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304641 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304642 kfree(adapter->sge.egr_map);
4643 kfree(adapter->sge.ingr_map);
4644 kfree(adapter->sge.starving_fl);
4645 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304646#ifdef CONFIG_DEBUG_FS
4647 kfree(adapter->sge.blocked_fl);
4648#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004649 disable_msi(adapter);
4650
4651 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004652 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304653 struct port_info *pi = adap2pinfo(adapter, i);
4654
4655 if (pi->viid != 0)
4656 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4657 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004658 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004659 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004660 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004661 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304662 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004663}
4664
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004665#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004666#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004667 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004668#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004669
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304670static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4671{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304672 u16 device_id;
4673
4674 /* Retrieve adapter's device ID */
4675 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004676
4677 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304678 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004679 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304680 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004681 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304682 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004683 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304684 default:
4685 dev_err(&pdev->dev, "Device %d is not supported\n",
4686 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304687 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004688 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304689}
4690
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304691#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304692static void dummy_setup(struct net_device *dev)
4693{
4694 dev->type = ARPHRD_NONE;
4695 dev->mtu = 0;
4696 dev->hard_header_len = 0;
4697 dev->addr_len = 0;
4698 dev->tx_queue_len = 0;
4699 dev->flags |= IFF_NOARP;
4700 dev->priv_flags |= IFF_NO_QUEUE;
4701
4702 /* Initialize the device structure. */
4703 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4704 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
David S. Millercf124db2017-05-08 12:52:56 -04004705 dev->needs_free_netdev = true;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304706}
4707
4708static int config_mgmt_dev(struct pci_dev *pdev)
4709{
4710 struct adapter *adap = pci_get_drvdata(pdev);
4711 struct net_device *netdev;
4712 struct port_info *pi;
4713 char name[IFNAMSIZ];
4714 int err;
4715
4716 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304717 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4718 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304719 if (!netdev)
4720 return -ENOMEM;
4721
4722 pi = netdev_priv(netdev);
4723 pi->adapter = adap;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304724 pi->tx_chan = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304725 SET_NETDEV_DEV(netdev, &pdev->dev);
4726
4727 adap->port[0] = netdev;
Ganesh Goudarc3168ca2017-08-20 14:15:51 +05304728 pi->port_id = 0;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304729
4730 err = register_netdev(adap->port[0]);
4731 if (err) {
4732 pr_info("Unable to register VF mgmt netdev %s\n", name);
4733 free_netdev(adap->port[0]);
4734 adap->port[0] = NULL;
4735 return err;
4736 }
4737 return 0;
4738}
4739
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304740static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4741{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304742 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304743 int err = 0;
4744 int current_vfs = pci_num_vf(pdev);
4745 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304746
Hariprasad Shenai78294512016-08-11 21:06:23 +05304747 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304748 /* Check if cxgb4 is the MASTER and fw is initialized */
4749 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4750 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4751 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4752 dev_warn(&pdev->dev,
4753 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4754 return -EOPNOTSUPP;
4755 }
4756
4757 /* If any of the VF's is already assigned to Guest OS, then
4758 * SRIOV for the same cannot be modified
4759 */
4760 if (current_vfs && pci_vfs_assigned(pdev)) {
4761 dev_err(&pdev->dev,
4762 "Cannot modify SR-IOV while VFs are assigned\n");
4763 num_vfs = current_vfs;
4764 return num_vfs;
4765 }
4766
4767 /* Disable SRIOV when zero is passed.
4768 * One needs to disable SRIOV before modifying it, else
4769 * stack throws the below warning:
4770 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4771 */
4772 if (!num_vfs) {
4773 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304774 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304775 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304776 adap->port[0] = NULL;
4777 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304778 /* free VF resources */
4779 kfree(adap->vfinfo);
4780 adap->vfinfo = NULL;
4781 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304782 return num_vfs;
4783 }
4784
4785 if (num_vfs != current_vfs) {
4786 err = pci_enable_sriov(pdev, num_vfs);
4787 if (err)
4788 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304789
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304790 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304791 err = config_mgmt_dev(pdev);
4792 if (err)
4793 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304794 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304795
4796 adap->vfinfo = kcalloc(adap->num_vfs,
4797 sizeof(struct vf_info), GFP_KERNEL);
4798 if (adap->vfinfo)
4799 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304800 return num_vfs;
4801}
4802#endif
4803
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004804static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004805{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004806 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004807 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004808 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004809 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304810 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304811 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304812 u32 whoami, pl_rev;
4813 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304814 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004815#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304816 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004817#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004818
4819 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4820
4821 err = pci_request_regions(pdev, KBUILD_MODNAME);
4822 if (err) {
4823 /* Just info, some other driver may have claimed the device. */
4824 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4825 return err;
4826 }
4827
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004828 err = pci_enable_device(pdev);
4829 if (err) {
4830 dev_err(&pdev->dev, "cannot enable PCI device\n");
4831 goto out_release_regions;
4832 }
4833
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304834 regs = pci_ioremap_bar(pdev, 0);
4835 if (!regs) {
4836 dev_err(&pdev->dev, "cannot map device registers\n");
4837 err = -ENOMEM;
4838 goto out_disable_device;
4839 }
4840
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304841 err = t4_wait_dev_ready(regs);
4842 if (err < 0)
4843 goto out_unmap_bar0;
4844
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304845 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304846 whoami = readl(regs + PL_WHOAMI_A);
4847 pl_rev = REV_G(readl(regs + PL_REV_A));
4848 chip = get_chip_type(pdev, pl_rev);
4849 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4850 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304851 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304852#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304853 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304854#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304855 pci_disable_device(pdev);
4856 pci_save_state(pdev); /* to restore SR-IOV later */
4857 goto sriov;
4858 }
4859
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004860 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004861 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004862 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4863 if (err) {
4864 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4865 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304866 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004867 }
4868 } else {
4869 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4870 if (err) {
4871 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304872 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004873 }
4874 }
4875
4876 pci_enable_pcie_error_reporting(pdev);
4877 pci_set_master(pdev);
4878 pci_save_state(pdev);
4879
4880 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4881 if (!adapter) {
4882 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304883 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004884 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304885 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004886
Anish Bhatt29aaee62014-08-20 13:44:06 -07004887 adapter->workq = create_singlethread_workqueue("cxgb4");
4888 if (!adapter->workq) {
4889 err = -ENOMEM;
4890 goto out_free_adapter;
4891 }
4892
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304893 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4894 (sizeof(struct mbox_cmd) *
4895 T4_OS_LOG_MBOX_CMDS),
4896 GFP_KERNEL);
4897 if (!adapter->mbox_log) {
4898 err = -ENOMEM;
4899 goto out_free_adapter;
4900 }
4901 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4902
Gavin Shan144be3d2014-01-23 12:27:34 +08004903 /* PCI device has been enabled */
4904 adapter->flags |= DEV_ENABLED;
4905
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304906 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004907 adapter->pdev = pdev;
4908 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304909 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304910 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304911 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05304912 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004913 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4914
Casey Leedomb0ba9d52017-08-15 11:23:26 +08004915 /* If possible, we use PCIe Relaxed Ordering Attribute to deliver
4916 * Ingress Packet Data to Free List Buffers in order to allow for
4917 * chipset performance optimizations between the Root Complex and
4918 * Memory Controllers. (Messages to the associated Ingress Queue
4919 * notifying new Packet Placement in the Free Lists Buffers will be
4920 * send without the Relaxed Ordering Attribute thus guaranteeing that
4921 * all preceding PCIe Transaction Layer Packets will be processed
4922 * first.) But some Root Complexes have various issues with Upstream
4923 * Transaction Layer Packets with the Relaxed Ordering Attribute set.
4924 * The PCIe devices which under the Root Complexes will be cleared the
4925 * Relaxed Ordering bit in the configuration space, So we check our
4926 * PCIe configuration space to see if it's flagged with advice against
4927 * using Relaxed Ordering.
4928 */
4929 if (!pcie_relaxed_ordering_enabled(pdev))
4930 adapter->flags |= ROOT_NO_RELAXED_ORDERING;
4931
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004932 spin_lock_init(&adapter->stats_lock);
4933 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004934 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05304935 spin_lock_init(&adapter->mbox_lock);
4936
4937 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004938
4939 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304940 INIT_WORK(&adapter->db_full_task, process_db_full);
4941 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004942
4943 err = t4_prep_adapter(adapter);
4944 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304945 goto out_free_adapter;
4946
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004947
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304948 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304949 s_qpp = (QUEUESPERPAGEPF0_S +
4950 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304951 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304952 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4953 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004954 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4955
4956 /* Each segment size is 128B. Write coalescing is enabled only
4957 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4958 * queue is less no of segments that can be accommodated in
4959 * a page size.
4960 */
4961 if (qpp > num_seg) {
4962 dev_err(&pdev->dev,
4963 "Incorrect number of egress queues per page\n");
4964 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304965 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004966 }
4967 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4968 pci_resource_len(pdev, 2));
4969 if (!adapter->bar2) {
4970 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4971 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304972 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004973 }
4974 }
4975
Vipul Pandya636f9d32012-09-26 02:39:39 +00004976 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004977 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304978#ifdef CONFIG_DEBUG_FS
4979 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4980#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004981 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004982 if (err)
4983 goto out_unmap_bar;
4984
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304985 /* configure SGE_STAT_CFG_A to read WC stats */
4986 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304987 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4988 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4989 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304990
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004991 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004992 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4993 MAX_ETH_QSETS);
4994 if (!netdev) {
4995 err = -ENOMEM;
4996 goto out_free_dev;
4997 }
4998
4999 SET_NETDEV_DEV(netdev, &pdev->dev);
5000
5001 adapter->port[i] = netdev;
5002 pi = netdev_priv(netdev);
5003 pi->adapter = adapter;
5004 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005005 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005006 netdev->irq = pdev->irq;
5007
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00005008 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
5009 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5010 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305011 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
5012 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00005013 if (highdma)
5014 netdev->hw_features |= NETIF_F_HIGHDMA;
5015 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005016 netdev->vlan_features = netdev->features & VLAN_FEAT;
5017
Jiri Pirko01789342011-08-16 06:29:00 +00005018 netdev->priv_flags |= IFF_UNICAST_FLT;
5019
Jarod Wilsond894be52016-10-20 13:55:16 -04005020 /* MTU range: 81 - 9600 */
5021 netdev->min_mtu = 81;
5022 netdev->max_mtu = MAX_MTU;
5023
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005024 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07005025#ifdef CONFIG_CHELSIO_T4_DCB
5026 netdev->dcbnl_ops = &cxgb4_dcb_ops;
5027 cxgb4_dcb_state_init(netdev);
5028#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05305029 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005030 }
5031
5032 pci_set_drvdata(pdev, adapter);
5033
5034 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00005035 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005036 if (err)
5037 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305038 } else if (adapter->params.nports == 1) {
5039 /* If we don't have a connection to the firmware -- possibly
5040 * because of an error -- grab the raw VPD parameters so we
5041 * can set the proper MAC Address on the debug network
5042 * interface that we've created.
5043 */
5044 u8 hw_addr[ETH_ALEN];
5045 u8 *na = adapter->params.vpd.na;
5046
5047 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
5048 if (!err) {
5049 for (i = 0; i < ETH_ALEN; i++)
5050 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
5051 hex2val(na[2 * i + 1]));
5052 t4_set_hw_addr(adapter, 0, hw_addr);
5053 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005054 }
5055
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05305056 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005057 * soon as the first register_netdev completes.
5058 */
5059 cfg_queues(adapter);
5060
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05305061 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005062 if (!adapter->l2t) {
5063 /* We tolerate a lack of L2T, giving up some functionality */
5064 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
5065 adapter->params.offload = 0;
5066 }
5067
Anish Bhattb5a02f52015-01-14 15:17:34 -08005068#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305069 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5070 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5071 /* CLIP functionality is not present in hardware,
5072 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005073 */
5074 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305075 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005076 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305077 } else {
5078 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5079 adapter->clipt_end);
5080 if (!adapter->clipt) {
5081 /* We tolerate a lack of clip_table, giving up
5082 * some functionality
5083 */
5084 dev_warn(&pdev->dev,
5085 "could not allocate Clip table, continuing\n");
5086 adapter->params.offload = 0;
5087 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005088 }
5089#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305090
5091 for_each_port(adapter, i) {
5092 pi = adap2pinfo(adapter, i);
5093 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5094 if (!pi->sched_tbl)
5095 dev_warn(&pdev->dev,
5096 "could not activate scheduling on port %d\n",
5097 i);
5098 }
5099
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305100 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005101 dev_warn(&pdev->dev, "could not allocate TID table, "
5102 "continuing\n");
5103 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305104 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305105 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305106 if (!adapter->tc_u32)
5107 dev_warn(&pdev->dev,
5108 "could not offload tc u32, continuing\n");
Kumar Sanghvi62488e42017-09-21 23:41:14 +05305109
5110 cxgb4_init_tc_flower(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005111 }
5112
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305113 if (is_offload(adapter)) {
5114 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5115 u32 hash_base, hash_reg;
5116
5117 if (chip <= CHELSIO_T5) {
5118 hash_reg = LE_DB_TID_HASHBASE_A;
5119 hash_base = t4_read_reg(adapter, hash_reg);
5120 adapter->tids.hash_base = hash_base / 4;
5121 } else {
5122 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5123 hash_base = t4_read_reg(adapter, hash_reg);
5124 adapter->tids.hash_base = hash_base;
5125 }
5126 }
5127 }
5128
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005129 /* See what interrupts we'll be using */
5130 if (msi > 1 && enable_msix(adapter) == 0)
5131 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305132 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005133 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305134 if (msi > 1)
5135 free_msix_info(adapter);
5136 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005137
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305138 /* check for PCI Express bandwidth capabiltites */
5139 cxgb4_check_pcie_caps(adapter);
5140
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005141 err = init_rss(adapter);
5142 if (err)
5143 goto out_free_dev;
5144
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005145 /*
5146 * The card is now ready to go. If any errors occur during device
5147 * registration we do not fail the whole card but rather proceed only
5148 * with the ports we manage to register successfully. However we must
5149 * register at least one net device.
5150 */
5151 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005152 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305153 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005154 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5155 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5156
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305157 netif_carrier_off(adapter->port[i]);
5158
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005159 err = register_netdev(adapter->port[i]);
5160 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005161 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005162 adapter->chan_map[pi->tx_chan] = i;
5163 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005164 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005165 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005166 dev_err(&pdev->dev, "could not register any net devices\n");
5167 goto out_free_dev;
5168 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005169 if (err) {
5170 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5171 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005172 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005173
5174 if (cxgb4_debugfs_root) {
5175 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5176 cxgb4_debugfs_root);
5177 setup_debugfs(adapter);
5178 }
5179
David S. Miller88c51002011-10-07 13:38:43 -04005180 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5181 pdev->needs_freset = 1;
5182
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305183 if (is_uld(adapter)) {
5184 mutex_lock(&uld_mutex);
5185 list_add_tail(&adapter->list_node, &adapter_list);
5186 mutex_unlock(&uld_mutex);
5187 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005188
Atul Gupta9c33e422017-07-04 16:46:21 +05305189 if (!is_t4(adapter->params.chip))
5190 cxgb4_ptp_init(adapter);
5191
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305192 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305193 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305194 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305195
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305196sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005197#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305198 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5199 if (!adapter) {
5200 err = -ENOMEM;
5201 goto free_pci_region;
5202 }
5203
Hariprasad Shenai78294512016-08-11 21:06:23 +05305204 adapter->pdev = pdev;
5205 adapter->pdev_dev = &pdev->dev;
5206 adapter->name = pci_name(pdev);
5207 adapter->mbox = func;
5208 adapter->pf = func;
5209 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305210 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305211 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5212 (sizeof(struct mbox_cmd) *
5213 T4_OS_LOG_MBOX_CMDS),
5214 GFP_KERNEL);
5215 if (!adapter->mbox_log) {
5216 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305217 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305218 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305219 spin_lock_init(&adapter->mbox_lock);
5220 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305221
5222 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5223 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5224 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5225 &v, &port_vec);
5226 if (err < 0) {
5227 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
Ganesh Goudard0417842017-06-09 19:26:24 +05305228 goto free_mbox_log;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305229 }
5230
5231 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305232 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005233 return 0;
5234
Ganesh Goudard0417842017-06-09 19:26:24 +05305235free_mbox_log:
5236 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305237 free_adapter:
5238 kfree(adapter);
5239 free_pci_region:
5240 iounmap(regs);
5241 pci_disable_sriov(pdev);
5242 pci_release_regions(pdev);
5243 return err;
5244#else
5245 return 0;
5246#endif
5247
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005248 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005249 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305250 if (adapter->flags & USING_MSIX)
5251 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305252 if (adapter->num_uld || adapter->num_ofld_uld)
5253 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005254 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305255 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005256 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005257 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005258 if (adapter->workq)
5259 destroy_workqueue(adapter->workq);
5260
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305261 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005262 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305263 out_unmap_bar0:
5264 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005265 out_disable_device:
5266 pci_disable_pcie_error_reporting(pdev);
5267 pci_disable_device(pdev);
5268 out_release_regions:
5269 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005270 return err;
5271}
5272
Bill Pemberton91744942012-12-03 09:23:02 -05005273static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005274{
5275 struct adapter *adapter = pci_get_drvdata(pdev);
5276
Hariprasad Shenai78294512016-08-11 21:06:23 +05305277 if (!adapter) {
5278 pci_release_regions(pdev);
5279 return;
5280 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005281
Ganesh Goudare1f61982017-09-21 12:50:47 +05305282 adapter->flags |= SHUTTING_DOWN;
5283
Hariprasad Shenai78294512016-08-11 21:06:23 +05305284 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005285 int i;
5286
Anish Bhatt29aaee62014-08-20 13:44:06 -07005287 /* Tear down per-adapter Work Queue first since it can contain
5288 * references to our adapter data structure.
5289 */
5290 destroy_workqueue(adapter->workq);
5291
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005292 if (is_uld(adapter)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005293 detach_ulds(adapter);
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005294 t4_uld_clean_up(adapter);
5295 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005296
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305297 disable_interrupts(adapter);
5298
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005299 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005300 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005301 unregister_netdev(adapter->port[i]);
5302
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005303 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005304
Atul Gupta9c33e422017-07-04 16:46:21 +05305305 if (!is_t4(adapter->params.chip))
5306 cxgb4_ptp_stop(adapter);
5307
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005308 /* If we allocated filters, free up state associated with any
5309 * valid filters ...
5310 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305311 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005312
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005313 if (adapter->flags & FULL_INIT_DONE)
5314 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005315
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305316 if (adapter->flags & USING_MSIX)
5317 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305318 if (adapter->num_uld || adapter->num_ofld_uld)
5319 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005320 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005321#if IS_ENABLED(CONFIG_IPV6)
5322 t4_cleanup_clip_tbl(adapter);
5323#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005324 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305325 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005326 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005327 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005328 if ((adapter->flags & DEV_ENABLED)) {
5329 pci_disable_device(pdev);
5330 adapter->flags &= ~DEV_ENABLED;
5331 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005332 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305333 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005334 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005335 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305336 }
5337#ifdef CONFIG_PCI_IOV
5338 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305339 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305340 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305341 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305342 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305343 kfree(adapter->mbox_log);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305344 kfree(adapter);
5345 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005346 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305347 }
5348#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005349}
5350
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305351/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5352 * delivery. This is essentially a stripped down version of the PCI remove()
5353 * function where we do the minimal amount of work necessary to shutdown any
5354 * further activity.
5355 */
5356static void shutdown_one(struct pci_dev *pdev)
5357{
5358 struct adapter *adapter = pci_get_drvdata(pdev);
5359
5360 /* As with remove_one() above (see extended comment), we only want do
5361 * do cleanup on PCI Devices which went all the way through init_one()
5362 * ...
5363 */
5364 if (!adapter) {
5365 pci_release_regions(pdev);
5366 return;
5367 }
5368
Ganesh Goudare1f61982017-09-21 12:50:47 +05305369 adapter->flags |= SHUTTING_DOWN;
5370
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305371 if (adapter->pf == 4) {
5372 int i;
5373
5374 for_each_port(adapter, i)
5375 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5376 cxgb_close(adapter->port[i]);
5377
Guilherme G. Piccoli6a146f32017-07-10 10:55:46 -03005378 if (is_uld(adapter)) {
5379 detach_ulds(adapter);
5380 t4_uld_clean_up(adapter);
5381 }
5382
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305383 disable_interrupts(adapter);
5384 disable_msi(adapter);
5385
5386 t4_sge_stop(adapter);
5387 if (adapter->flags & FW_OK)
5388 t4_fw_bye(adapter, adapter->mbox);
5389 }
5390#ifdef CONFIG_PCI_IOV
5391 else {
5392 if (adapter->port[0])
5393 unregister_netdev(adapter->port[0]);
5394 iounmap(adapter->regs);
5395 kfree(adapter->vfinfo);
Ganesh Goudard0417842017-06-09 19:26:24 +05305396 kfree(adapter->mbox_log);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305397 kfree(adapter);
5398 pci_disable_sriov(pdev);
5399 pci_release_regions(pdev);
5400 }
5401#endif
5402}
5403
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005404static struct pci_driver cxgb4_driver = {
5405 .name = KBUILD_MODNAME,
5406 .id_table = cxgb4_pci_tbl,
5407 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005408 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305409 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305410#ifdef CONFIG_PCI_IOV
5411 .sriov_configure = cxgb4_iov_configure,
5412#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005413 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005414};
5415
5416static int __init cxgb4_init_module(void)
5417{
5418 int ret;
5419
5420 /* Debugfs support is optional, just warn if this fails */
5421 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5422 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005423 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005424
5425 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005426 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005427 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305428
Anish Bhatt1bb60372014-10-14 20:07:22 -07005429#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005430 if (!inet6addr_registered) {
5431 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5432 inet6addr_registered = true;
5433 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005434#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305435
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005436 return ret;
5437}
5438
5439static void __exit cxgb4_cleanup_module(void)
5440{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005441#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305442 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005443 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5444 inet6addr_registered = false;
5445 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005446#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005447 pci_unregister_driver(&cxgb4_driver);
5448 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005449}
5450
5451module_init(cxgb4_init_module);
5452module_exit(cxgb4_cleanup_module);