blob: 6c463703e072d853b829f60863fb8d57e56538d9 [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"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000082
Hariprasad Shenai812034f2015-04-06 20:23:23 +053083char cxgb4_driver_name[] = KBUILD_MODNAME;
84
Vipul Pandya01bcca62013-07-04 16:10:46 +053085#ifdef DRV_VERSION
86#undef DRV_VERSION
87#endif
Santosh Rastapur3a7f8552013-03-14 05:08:55 +000088#define DRV_VERSION "2.0.0-ko"
Hariprasad Shenai812034f2015-04-06 20:23:23 +053089const char cxgb4_driver_version[] = DRV_VERSION;
Hariprasad Shenai52a5f842015-10-21 14:39:54 +053090#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000091
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000092#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
93 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
94 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
95
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +053096/* Macros needed to support the PCI Device ID Table ...
97 */
98#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
Hariprasad Shenai768ffc62015-03-19 22:27:36 +053099 static const struct pci_device_id cxgb4_pci_tbl[] = {
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530100#define CH_PCI_DEVICE_ID_FUNCTION 0x4
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000101
Hariprasad Shenai3fedeab2014-11-25 08:33:58 +0530102/* Include PCI Device IDs for both PF4 and PF0-3 so our PCI probe() routine is
103 * called for both.
104 */
105#define CH_PCI_DEVICE_ID_FUNCTION2 0x0
106
107#define CH_PCI_ID_TABLE_ENTRY(devid) \
108 {PCI_VDEVICE(CHELSIO, (devid)), 4}
109
110#define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
111 { 0, } \
112 }
113
114#include "t4_pci_id_tbl.h"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000115
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530116#define FW4_FNAME "cxgb4/t4fw.bin"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000117#define FW5_FNAME "cxgb4/t5fw.bin"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530118#define FW6_FNAME "cxgb4/t6fw.bin"
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530119#define FW4_CFNAME "cxgb4/t4-config.txt"
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000120#define FW5_CFNAME "cxgb4/t5-config.txt"
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +0530121#define FW6_CFNAME "cxgb4/t6-config.txt"
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530122#define PHY_AQ1202_FIRMWARE "cxgb4/aq1202_fw.cld"
123#define PHY_BCM84834_FIRMWARE "cxgb4/bcm8483.bin"
124#define PHY_AQ1202_DEVICEID 0x4409
125#define PHY_BCM84834_DEVICEID 0x4486
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000126
127MODULE_DESCRIPTION(DRV_DESC);
128MODULE_AUTHOR("Chelsio Communications");
129MODULE_LICENSE("Dual BSD/GPL");
130MODULE_VERSION(DRV_VERSION);
131MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
Hariprasad Shenai16e47622013-12-03 17:05:58 +0530132MODULE_FIRMWARE(FW4_FNAME);
Santosh Rastapur0a57a532013-03-14 05:08:49 +0000133MODULE_FIRMWARE(FW5_FNAME);
Hariprasad Shenai52a5f842015-10-21 14:39:54 +0530134MODULE_FIRMWARE(FW6_FNAME);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000135
Vipul Pandya636f9d32012-09-26 02:39:39 +0000136/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000137 * The driver uses the best interrupt scheme available on a platform in the
138 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
139 * of these schemes the driver may consider as follows:
140 *
141 * msi = 2: choose from among all three options
142 * msi = 1: only consider MSI and INTx interrupts
143 * msi = 0: force INTx interrupts
144 */
145static int msi = 2;
146
147module_param(msi, int, 0644);
148MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
149
150/*
Vipul Pandya636f9d32012-09-26 02:39:39 +0000151 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
152 * offset by 2 bytes in order to have the IP headers line up on 4-byte
153 * boundaries. This is a requirement for many architectures which will throw
154 * a machine check fault if an attempt is made to access one of the 4-byte IP
155 * header fields on a non-4-byte boundary. And it's a major performance issue
156 * even on some architectures which allow it like some implementations of the
157 * x86 ISA. However, some architectures don't mind this and for some very
158 * edge-case performance sensitive applications (like forwarding large volumes
159 * of small packets), setting this DMA offset to 0 will decrease the number of
160 * PCI-E Bus transfers enough to measurably affect performance.
161 */
162static int rx_dma_offset = 2;
163
Anish Bhatt688848b2014-06-19 21:37:13 -0700164/* TX Queue select used to determine what algorithm to use for selecting TX
165 * queue. Select between the kernel provided function (select_queue=0) or user
166 * cxgb_select_queue function (select_queue=1)
167 *
168 * Default: select_queue=0
169 */
170static int select_queue;
171module_param(select_queue, int, 0644);
172MODULE_PARM_DESC(select_queue,
173 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
174
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000175static struct dentry *cxgb4_debugfs_root;
176
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530177LIST_HEAD(adapter_list);
178DEFINE_MUTEX(uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000179
180static void link_report(struct net_device *dev)
181{
182 if (!netif_carrier_ok(dev))
183 netdev_info(dev, "link down\n");
184 else {
185 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
186
Hariprasad Shenai85412252015-10-01 13:48:48 +0530187 const char *s;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000188 const struct port_info *p = netdev_priv(dev);
189
190 switch (p->link_cfg.speed) {
Ben Hutchingse8b39012014-02-23 00:03:24 +0000191 case 100:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000192 s = "100Mbps";
193 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530194 case 1000:
195 s = "1Gbps";
196 break;
197 case 10000:
198 s = "10Gbps";
199 break;
200 case 25000:
201 s = "25Gbps";
202 break;
Ben Hutchingse8b39012014-02-23 00:03:24 +0000203 case 40000:
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +0530204 s = "40Gbps";
205 break;
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +0530206 case 100000:
207 s = "100Gbps";
208 break;
Hariprasad Shenai85412252015-10-01 13:48:48 +0530209 default:
210 pr_info("%s: unsupported speed: %d\n",
211 dev->name, p->link_cfg.speed);
212 return;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000213 }
214
215 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
216 fc[p->link_cfg.fc]);
217 }
218}
219
Anish Bhatt688848b2014-06-19 21:37:13 -0700220#ifdef CONFIG_CHELSIO_T4_DCB
221/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
222static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
223{
224 struct port_info *pi = netdev_priv(dev);
225 struct adapter *adap = pi->adapter;
226 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
227 int i;
228
229 /* We use a simple mapping of Port TX Queue Index to DCB
230 * Priority when we're enabling DCB.
231 */
232 for (i = 0; i < pi->nqsets; i++, txq++) {
233 u32 name, value;
234 int err;
235
Hariprasad Shenai51678652014-11-21 12:52:02 +0530236 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
237 FW_PARAMS_PARAM_X_V(
238 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
239 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
Anish Bhatt688848b2014-06-19 21:37:13 -0700240 value = enable ? i : 0xffffffff;
241
242 /* Since we can be called while atomic (from "interrupt
243 * level") we need to issue the Set Parameters Commannd
244 * without sleeping (timeout < 0).
245 */
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530246 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai01b69612015-05-22 21:58:21 +0530247 &name, &value,
248 -FW_CMD_MAX_TIMEOUT);
Anish Bhatt688848b2014-06-19 21:37:13 -0700249
250 if (err)
251 dev_err(adap->pdev_dev,
252 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
253 enable ? "set" : "unset", pi->port_id, i, -err);
Anish Bhatt10b00462014-08-07 16:14:03 -0700254 else
255 txq->dcb_prio = value;
Anish Bhatt688848b2014-06-19 21:37:13 -0700256 }
257}
Anish Bhatt688848b2014-06-19 21:37:13 -0700258
Baoyou Xie50935852016-09-25 14:10:09 +0800259static int cxgb4_dcb_enabled(const struct net_device *dev)
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530260{
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530261 struct port_info *pi = netdev_priv(dev);
262
263 if (!pi->dcb.enabled)
264 return 0;
265
266 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
267 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530268}
Arnd Bergmann7c70c4f2016-09-30 18:15:33 +0200269#endif /* CONFIG_CHELSIO_T4_DCB */
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530270
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000271void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
272{
273 struct net_device *dev = adapter->port[port_id];
274
275 /* Skip changes from disabled ports. */
276 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
277 if (link_stat)
278 netif_carrier_on(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700279 else {
280#ifdef CONFIG_CHELSIO_T4_DCB
Hariprasad Shenai218d48e2016-05-05 11:05:39 +0530281 if (cxgb4_dcb_enabled(dev)) {
282 cxgb4_dcb_state_init(dev);
283 dcb_tx_queue_prio_enable(dev, false);
284 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700285#endif /* CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000286 netif_carrier_off(dev);
Anish Bhatt688848b2014-06-19 21:37:13 -0700287 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000288
289 link_report(dev);
290 }
291}
292
293void t4_os_portmod_changed(const struct adapter *adap, int port_id)
294{
295 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000296 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000297 };
298
299 const struct net_device *dev = adap->port[port_id];
300 const struct port_info *pi = netdev_priv(dev);
301
302 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
303 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000304 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000305 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
Hariprasad Shenaibe81a2d2016-04-26 20:10:25 +0530306 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
307 netdev_info(dev, "%s: unsupported port module inserted\n",
308 dev->name);
309 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
310 netdev_info(dev, "%s: unknown port module inserted\n",
311 dev->name);
312 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
313 netdev_info(dev, "%s: transceiver module error\n", dev->name);
314 else
315 netdev_info(dev, "%s: unknown module type %d inserted\n",
316 dev->name, pi->mod_type);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000317}
318
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530319int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
320module_param(dbfifo_int_thresh, int, 0644);
321MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
322
Vipul Pandya404d9e32012-10-08 02:59:43 +0000323/*
324 * usecs to sleep while draining the dbfifo
325 */
326static int dbfifo_drain_delay = 1000;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +0530327module_param(dbfifo_drain_delay, int, 0644);
328MODULE_PARM_DESC(dbfifo_drain_delay,
329 "usecs to sleep while draining the dbfifo");
330
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530331static inline int cxgb4_set_addr_hash(struct port_info *pi)
332{
333 struct adapter *adap = pi->adapter;
334 u64 vec = 0;
335 bool ucast = false;
336 struct hash_mac_addr *entry;
337
338 /* Calculate the hash vector for the updated list and program it */
339 list_for_each_entry(entry, &adap->mac_hlist, list) {
340 ucast |= is_unicast_ether_addr(entry->addr);
341 vec |= (1ULL << hash_mac_addr(entry->addr));
342 }
343 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
344 vec, false);
345}
346
347static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
348{
349 struct port_info *pi = netdev_priv(netdev);
350 struct adapter *adap = pi->adapter;
351 int ret;
352 u64 mhash = 0;
353 u64 uhash = 0;
354 bool free = false;
355 bool ucast = is_unicast_ether_addr(mac_addr);
356 const u8 *maclist[1] = {mac_addr};
357 struct hash_mac_addr *new_entry;
358
359 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
360 NULL, ucast ? &uhash : &mhash, false);
361 if (ret < 0)
362 goto out;
363 /* if hash != 0, then add the addr to hash addr list
364 * so on the end we will calculate the hash for the
365 * list and program it
366 */
367 if (uhash || mhash) {
368 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
369 if (!new_entry)
370 return -ENOMEM;
371 ether_addr_copy(new_entry->addr, mac_addr);
372 list_add_tail(&new_entry->list, &adap->mac_hlist);
373 ret = cxgb4_set_addr_hash(pi);
374 }
375out:
376 return ret < 0 ? ret : 0;
377}
378
379static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
380{
381 struct port_info *pi = netdev_priv(netdev);
382 struct adapter *adap = pi->adapter;
383 int ret;
384 const u8 *maclist[1] = {mac_addr};
385 struct hash_mac_addr *entry, *tmp;
386
387 /* If the MAC address to be removed is in the hash addr
388 * list, delete it from the list and update hash vector
389 */
390 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
391 if (ether_addr_equal(entry->addr, mac_addr)) {
392 list_del(&entry->list);
393 kfree(entry);
394 return cxgb4_set_addr_hash(pi);
395 }
396 }
397
398 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
399 return ret < 0 ? -EINVAL : 0;
400}
401
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000402/*
403 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
404 * If @mtu is -1 it is left unchanged.
405 */
406static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
407{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000408 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530409 struct adapter *adapter = pi->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000410
Hariprasad Shenaid01f7ab2016-06-14 14:39:32 +0530411 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
412 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
Hariprasad Shenaifc08a012016-02-16 10:07:09 +0530413
414 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
415 (dev->flags & IFF_PROMISC) ? 1 : 0,
416 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
417 sleep_ok);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000418}
419
420/**
421 * link_start - enable a port
422 * @dev: the port to enable
423 *
424 * Performs the MAC and PHY actions needed to enable a port.
425 */
426static int link_start(struct net_device *dev)
427{
428 int ret;
429 struct port_info *pi = netdev_priv(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530430 unsigned int mb = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000431
432 /*
433 * We do not set address filters and promiscuity here, the stack does
434 * that step explicitly.
435 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000436 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +0000437 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000438 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000439 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000440 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000441 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000442 if (ret >= 0) {
443 pi->xact_addr_filt = ret;
444 ret = 0;
445 }
446 }
447 if (ret == 0)
Hariprasad Shenai4036da92015-06-05 14:24:49 +0530448 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000449 &pi->link_cfg);
Anish Bhatt30f00842014-08-05 16:05:23 -0700450 if (ret == 0) {
451 local_bh_disable();
Anish Bhatt688848b2014-06-19 21:37:13 -0700452 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
453 true, CXGB4_DCB_ENABLED);
Anish Bhatt30f00842014-08-05 16:05:23 -0700454 local_bh_enable();
455 }
Anish Bhatt688848b2014-06-19 21:37:13 -0700456
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000457 return ret;
458}
459
Anish Bhatt688848b2014-06-19 21:37:13 -0700460#ifdef CONFIG_CHELSIO_T4_DCB
461/* Handle a Data Center Bridging update message from the firmware. */
462static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
463{
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530464 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530465 struct net_device *dev = adap->port[adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700466 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
467 int new_dcb_enabled;
468
469 cxgb4_dcb_handle_fw_update(adap, pcmd);
470 new_dcb_enabled = cxgb4_dcb_enabled(dev);
471
472 /* If the DCB has become enabled or disabled on the port then we're
473 * going to need to set up/tear down DCB Priority parameters for the
474 * TX Queues associated with the port.
475 */
476 if (new_dcb_enabled != old_dcb_enabled)
477 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
478}
479#endif /* CONFIG_CHELSIO_T4_DCB */
480
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000481/* Response queue handler for the FW event queue.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000482 */
483static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
484 const struct pkt_gl *gl)
485{
486 u8 opcode = ((const struct rss_header *)rsp)->opcode;
487
488 rsp++; /* skip RSS header */
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000489
490 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
491 */
492 if (unlikely(opcode == CPL_FW4_MSG &&
493 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
494 rsp++;
495 opcode = ((const struct rss_header *)rsp)->opcode;
496 rsp++;
497 if (opcode != CPL_SGE_EGR_UPDATE) {
498 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
499 , opcode);
500 goto out;
501 }
502 }
503
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000504 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
505 const struct cpl_sge_egr_update *p = (void *)rsp;
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -0800506 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000507 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000508
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000509 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000510 txq->restarts++;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530511 if (txq->q_type == CXGB4_TXQ_ETH) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000512 struct sge_eth_txq *eq;
513
514 eq = container_of(txq, struct sge_eth_txq, q);
515 netif_tx_wake_queue(eq->txq);
516 } else {
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530517 struct sge_uld_txq *oq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000518
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +0530519 oq = container_of(txq, struct sge_uld_txq, q);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000520 tasklet_schedule(&oq->qresume_tsk);
521 }
522 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
523 const struct cpl_fw6_msg *p = (void *)rsp;
524
Anish Bhatt688848b2014-06-19 21:37:13 -0700525#ifdef CONFIG_CHELSIO_T4_DCB
526 const struct fw_port_cmd *pcmd = (const void *)p->data;
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +0530527 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
Anish Bhatt688848b2014-06-19 21:37:13 -0700528 unsigned int action =
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530529 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
Anish Bhatt688848b2014-06-19 21:37:13 -0700530
531 if (cmd == FW_PORT_CMD &&
532 action == FW_PORT_ACTION_GET_PORT_INFO) {
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530533 int port = FW_PORT_CMD_PORTID_G(
Anish Bhatt688848b2014-06-19 21:37:13 -0700534 be32_to_cpu(pcmd->op_to_portid));
Hariprasad Shenai134491f2016-04-26 20:10:27 +0530535 struct net_device *dev =
536 q->adap->port[q->adap->chan_map[port]];
Anish Bhatt688848b2014-06-19 21:37:13 -0700537 int state_input = ((pcmd->u.info.dcbxdis_pkd &
Hariprasad Shenai2b5fb1f2014-11-21 12:52:04 +0530538 FW_PORT_CMD_DCBXDIS_F)
Anish Bhatt688848b2014-06-19 21:37:13 -0700539 ? CXGB4_DCB_INPUT_FW_DISABLED
540 : CXGB4_DCB_INPUT_FW_ENABLED);
541
542 cxgb4_dcb_state_fsm(dev, state_input);
543 }
544
545 if (cmd == FW_PORT_CMD &&
546 action == FW_PORT_ACTION_L2_DCB_CFG)
547 dcb_rpl(q->adap, pcmd);
548 else
549#endif
550 if (p->type == 0)
551 t4_handle_fw_rpl(q->adap, p->data);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000552 } else if (opcode == CPL_L2T_WRITE_RPL) {
553 const struct cpl_l2t_write_rpl *p = (void *)rsp;
554
555 do_l2t_write_rpl(q->adap, p);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +0000556 } else if (opcode == CPL_SET_TCB_RPL) {
557 const struct cpl_set_tcb_rpl *p = (void *)rsp;
558
559 filter_rpl(q->adap, p);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000560 } else
561 dev_err(q->adap->pdev_dev,
562 "unexpected CPL %#x on FW event queue\n", opcode);
Vipul Pandyab407a4a2013-04-29 04:04:40 +0000563out:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000564 return 0;
565}
566
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000567static void disable_msi(struct adapter *adapter)
568{
569 if (adapter->flags & USING_MSIX) {
570 pci_disable_msix(adapter->pdev);
571 adapter->flags &= ~USING_MSIX;
572 } else if (adapter->flags & USING_MSI) {
573 pci_disable_msi(adapter->pdev);
574 adapter->flags &= ~USING_MSI;
575 }
576}
577
578/*
579 * Interrupt handler for non-data events used with MSI-X.
580 */
581static irqreturn_t t4_nondata_intr(int irq, void *cookie)
582{
583 struct adapter *adap = cookie;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530584 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000585
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530586 if (v & PFSW_F) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000587 adap->swintr = 1;
Hariprasad Shenai0d804332015-01-05 16:30:47 +0530588 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000589 }
Hariprasad Shenaic3c7b122015-04-15 02:02:34 +0530590 if (adap->flags & MASTER_PF)
591 t4_slow_intr_handler(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000592 return IRQ_HANDLED;
593}
594
595/*
596 * Name the MSI-X interrupts.
597 */
598static void name_msix_vecs(struct adapter *adap)
599{
Dimitris Michailidisba278162010-12-14 21:36:50 +0000600 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000601
602 /* non-data interrupts */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000603 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000604
605 /* FW events */
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +0000606 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
607 adap->port[0]->name);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000608
609 /* Ethernet queues */
610 for_each_port(adap, j) {
611 struct net_device *d = adap->port[j];
612 const struct port_info *pi = netdev_priv(d);
613
Dimitris Michailidisba278162010-12-14 21:36:50 +0000614 for (i = 0; i < pi->nqsets; i++, msi_idx++)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000615 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
616 d->name, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000617 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000618}
619
620static int request_msix_queue_irqs(struct adapter *adap)
621{
622 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530623 int err, ethqidx;
Hariprasad Shenaicf38be62014-06-06 21:40:42 +0530624 int msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000625
626 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
627 adap->msix_info[1].desc, &s->fw_evtq);
628 if (err)
629 return err;
630
631 for_each_ethrxq(s, ethqidx) {
Vipul Pandya404d9e32012-10-08 02:59:43 +0000632 err = request_irq(adap->msix_info[msi_index].vec,
633 t4_sge_intr_msix, 0,
634 adap->msix_info[msi_index].desc,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000635 &s->ethrxq[ethqidx].rspq);
636 if (err)
637 goto unwind;
Vipul Pandya404d9e32012-10-08 02:59:43 +0000638 msi_index++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000639 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000640 return 0;
641
642unwind:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000643 while (--ethqidx >= 0)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000644 free_irq(adap->msix_info[--msi_index].vec,
645 &s->ethrxq[ethqidx].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000646 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
647 return err;
648}
649
650static void free_msix_queue_irqs(struct adapter *adap)
651{
Vipul Pandya404d9e32012-10-08 02:59:43 +0000652 int i, msi_index = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000653 struct sge *s = &adap->sge;
654
655 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
656 for_each_ethrxq(s, i)
Vipul Pandya404d9e32012-10-08 02:59:43 +0000657 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000658}
659
660/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530661 * cxgb4_write_rss - write the RSS table for a given port
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000662 * @pi: the port
663 * @queues: array of queue indices for RSS
664 *
665 * Sets up the portion of the HW RSS table for the port's VI to distribute
666 * packets to the Rx queues in @queues.
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530667 * Should never be called before setting up sge eth rx queues
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000668 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530669int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000670{
671 u16 *rss;
672 int i, err;
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530673 struct adapter *adapter = pi->adapter;
674 const struct sge_eth_rxq *rxq;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000675
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530676 rxq = &adapter->sge.ethrxq[pi->first_qset];
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000677 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
678 if (!rss)
679 return -ENOMEM;
680
681 /* map the queue indices to queue ids */
682 for (i = 0; i < pi->rss_size; i++, queues++)
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530683 rss[i] = rxq[*queues].rspq.abs_id;
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000684
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530685 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000686 pi->rss_size, rss, pi->rss_size);
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530687 /* If Tunnel All Lookup isn't specified in the global RSS
688 * Configuration, then we need to specify a default Ingress
689 * Queue for any ingress packets which aren't hashed. We'll
690 * use our first ingress queue ...
691 */
692 if (!err)
693 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
694 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
695 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
696 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
697 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
698 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
699 rss[0]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000700 kfree(rss);
701 return err;
702}
703
704/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000705 * setup_rss - configure RSS
706 * @adap: the adapter
707 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000708 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000709 */
710static int setup_rss(struct adapter *adap)
711{
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530712 int i, j, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000713
714 for_each_port(adap, i) {
715 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000716
Hariprasad Shenaic035e182015-05-06 19:48:37 +0530717 /* Fill default values with equal distribution */
718 for (j = 0; j < pi->rss_size; j++)
719 pi->rss[j] = j % pi->nqsets;
720
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530721 err = cxgb4_write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000722 if (err)
723 return err;
724 }
725 return 0;
726}
727
728/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000729 * Return the channel of the ingress queue with the given qid.
730 */
731static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
732{
733 qid -= p->ingr_start;
734 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
735}
736
737/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000738 * Wait until all NAPI handlers are descheduled.
739 */
740static void quiesce_rx(struct adapter *adap)
741{
742 int i;
743
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530744 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000745 struct sge_rspq *q = adap->sge.ingr_map[i];
746
Eric Dumazet5226b7912017-02-02 11:44:27 -0800747 if (q && q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000748 napi_disable(&q->napi);
749 }
750}
751
Hariprasad Shenaib37987e2015-03-26 10:04:26 +0530752/* Disable interrupt and napi handler */
753static void disable_interrupts(struct adapter *adap)
754{
755 if (adap->flags & FULL_INIT_DONE) {
756 t4_intr_disable(adap);
757 if (adap->flags & USING_MSIX) {
758 free_msix_queue_irqs(adap);
759 free_irq(adap->msix_info[0].vec, adap);
760 } else {
761 free_irq(adap->pdev->irq, adap);
762 }
763 quiesce_rx(adap);
764 }
765}
766
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000767/*
768 * Enable NAPI scheduling and interrupt generation for all Rx queues.
769 */
770static void enable_rx(struct adapter *adap)
771{
772 int i;
773
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530774 for (i = 0; i < adap->sge.ingr_sz; i++) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000775 struct sge_rspq *q = adap->sge.ingr_map[i];
776
777 if (!q)
778 continue;
Eric Dumazet5226b7912017-02-02 11:44:27 -0800779 if (q->handler)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000780 napi_enable(&q->napi);
Eric Dumazet5226b7912017-02-02 11:44:27 -0800781
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000782 /* 0-increment GTS to start the timer and enable interrupts */
Hariprasad Shenaif612b812015-01-05 16:30:43 +0530783 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
784 SEINTARM_V(q->intr_params) |
785 INGRESSQID_V(q->cntxt_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000786 }
787}
788
Hariprasad Shenai1c6a5b02015-03-04 18:16:27 +0530789
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530790static int setup_fw_sge_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000791{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000792 struct sge *s = &adap->sge;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530793 int err = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000794
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +0530795 bitmap_zero(s->starving_fl, s->egr_sz);
796 bitmap_zero(s->txq_maperr, s->egr_sz);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000797
798 if (adap->flags & USING_MSIX)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530799 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000800 else {
801 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
Varun Prakash2337ba42016-02-14 23:02:41 +0530802 NULL, NULL, NULL, -1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000803 if (err)
804 return err;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530805 adap->msi_idx = -((int)s->intrq.abs_id + 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000806 }
807
808 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530809 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530810 if (err)
811 t4_free_sge_resources(adap);
812 return err;
813}
814
815/**
816 * setup_sge_queues - configure SGE Tx/Rx/response queues
817 * @adap: the adapter
818 *
819 * Determines how many sets of SGE queues to use and initializes them.
820 * We support multiple queue sets per port if we have MSI-X, otherwise
821 * just one queue set per port.
822 */
823static int setup_sge_queues(struct adapter *adap)
824{
825 int err, i, j;
826 struct sge *s = &adap->sge;
827 struct sge_uld_rxq_info *rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
828 unsigned int cmplqid = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000829
830 for_each_port(adap, i) {
831 struct net_device *dev = adap->port[i];
832 struct port_info *pi = netdev_priv(dev);
833 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
834 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
835
836 for (j = 0; j < pi->nqsets; j++, q++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530837 if (adap->msi_idx > 0)
838 adap->msi_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000839 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +0530840 adap->msi_idx, &q->fl,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530841 t4_ethrx_handler,
Varun Prakash2337ba42016-02-14 23:02:41 +0530842 NULL,
Hariprasad Shenai145ef8a2015-05-05 14:59:52 +0530843 t4_get_mps_bg_map(adap,
844 pi->tx_chan));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000845 if (err)
846 goto freeout;
847 q->rspq.idx = j;
848 memset(&q->stats, 0, sizeof(q->stats));
849 }
850 for (j = 0; j < pi->nqsets; j++, t++) {
851 err = t4_sge_alloc_eth_txq(adap, t, dev,
852 netdev_get_tx_queue(dev, j),
853 s->fw_evtq.cntxt_id);
854 if (err)
855 goto freeout;
856 }
857 }
858
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000859 for_each_port(adap, i) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530860 /* Note that cmplqid below is 0 if we don't
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000861 * have RDMA queues, and that's the right value.
862 */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530863 if (rxq_info)
864 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
865
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000866 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530867 s->fw_evtq.cntxt_id, cmplqid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000868 if (err)
869 goto freeout;
870 }
871
Hariprasad Shenai9bb59b92014-09-01 19:54:57 +0530872 t4_write_reg(adap, is_t4(adap->params.chip) ?
Hariprasad Shenai837e4a42015-01-05 16:30:46 +0530873 MPS_TRC_RSS_CONTROL_A :
874 MPS_T5_TRC_RSS_CONTROL_A,
875 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
876 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000877 return 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +0530878freeout:
879 t4_free_sge_resources(adap);
880 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000881}
882
Anish Bhatt688848b2014-06-19 21:37:13 -0700883static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
884 void *accel_priv, select_queue_fallback_t fallback)
885{
886 int txq;
887
888#ifdef CONFIG_CHELSIO_T4_DCB
889 /* If a Data Center Bridging has been successfully negotiated on this
890 * link then we'll use the skb's priority to map it to a TX Queue.
891 * The skb's priority is determined via the VLAN Tag Priority Code
892 * Point field.
893 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +0530894 if (cxgb4_dcb_enabled(dev) && !is_kdump_kernel()) {
Anish Bhatt688848b2014-06-19 21:37:13 -0700895 u16 vlan_tci;
896 int err;
897
898 err = vlan_get_tag(skb, &vlan_tci);
899 if (unlikely(err)) {
900 if (net_ratelimit())
901 netdev_warn(dev,
902 "TX Packet without VLAN Tag on DCB Link\n");
903 txq = 0;
904 } else {
905 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
Varun Prakash84a200b2015-03-24 19:14:46 +0530906#ifdef CONFIG_CHELSIO_T4_FCOE
907 if (skb->protocol == htons(ETH_P_FCOE))
908 txq = skb->priority & 0x7;
909#endif /* CONFIG_CHELSIO_T4_FCOE */
Anish Bhatt688848b2014-06-19 21:37:13 -0700910 }
911 return txq;
912 }
913#endif /* CONFIG_CHELSIO_T4_DCB */
914
915 if (select_queue) {
916 txq = (skb_rx_queue_recorded(skb)
917 ? skb_get_rx_queue(skb)
918 : smp_processor_id());
919
920 while (unlikely(txq >= dev->real_num_tx_queues))
921 txq -= dev->real_num_tx_queues;
922
923 return txq;
924 }
925
926 return fallback(dev, skb) % dev->real_num_tx_queues;
927}
928
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000929static int closest_timer(const struct sge *s, int time)
930{
931 int i, delta, match = 0, min_delta = INT_MAX;
932
933 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
934 delta = time - s->timer_val[i];
935 if (delta < 0)
936 delta = -delta;
937 if (delta < min_delta) {
938 min_delta = delta;
939 match = i;
940 }
941 }
942 return match;
943}
944
945static int closest_thres(const struct sge *s, int thres)
946{
947 int i, delta, match = 0, min_delta = INT_MAX;
948
949 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
950 delta = thres - s->counter_val[i];
951 if (delta < 0)
952 delta = -delta;
953 if (delta < min_delta) {
954 min_delta = delta;
955 match = i;
956 }
957 }
958 return match;
959}
960
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000961/**
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530962 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000963 * @q: the Rx queue
964 * @us: the hold-off time in us, or 0 to disable timer
965 * @cnt: the hold-off packet count, or 0 to disable counter
966 *
967 * Sets an Rx queue's interrupt hold-off time and packet count. At least
968 * one of the two needs to be enabled for the queue to generate interrupts.
969 */
Hariprasad Shenai812034f2015-04-06 20:23:23 +0530970int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
971 unsigned int us, unsigned int cnt)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000972{
Hariprasad Shenaic887ad02014-06-06 21:40:45 +0530973 struct adapter *adap = q->adap;
974
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000975 if ((us | cnt) == 0)
976 cnt = 1;
977
978 if (cnt) {
979 int err;
980 u32 v, new_idx;
981
982 new_idx = closest_thres(&adap->sge, cnt);
983 if (q->desc && q->pktcnt_idx != new_idx) {
984 /* the queue has already been created, update it */
Hariprasad Shenai51678652014-11-21 12:52:02 +0530985 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
986 FW_PARAMS_PARAM_X_V(
987 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
988 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
Hariprasad Shenaib2612722015-05-27 22:30:24 +0530989 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
990 &v, &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000991 if (err)
992 return err;
993 }
994 q->pktcnt_idx = new_idx;
995 }
996
997 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
Hariprasad Shenai1ecc7b72015-05-12 04:43:43 +0530998 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000999 return 0;
1000}
1001
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001002static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001003{
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001004 const struct port_info *pi = netdev_priv(dev);
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001005 netdev_features_t changed = dev->features ^ features;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001006 int err;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001007
Patrick McHardyf6469682013-04-19 02:04:27 +00001008 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001009 return 0;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001010
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301011 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001012 -1, -1, -1,
Patrick McHardyf6469682013-04-19 02:04:27 +00001013 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00001014 if (unlikely(err))
Patrick McHardyf6469682013-04-19 02:04:27 +00001015 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
Dimitris Michailidis19ecae22010-10-21 11:29:56 +00001016 return err;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001017}
1018
Bill Pemberton91744942012-12-03 09:23:02 -05001019static int setup_debugfs(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001020{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001021 if (IS_ERR_OR_NULL(adap->debugfs_root))
1022 return -1;
1023
Hariprasad Shenaifd88b312014-11-07 09:35:23 +05301024#ifdef CONFIG_DEBUG_FS
1025 t4_setup_debugfs(adap);
1026#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001027 return 0;
1028}
1029
1030/*
1031 * upper-layer driver support
1032 */
1033
1034/*
1035 * Allocate an active-open TID and set it to the supplied value.
1036 */
1037int cxgb4_alloc_atid(struct tid_info *t, void *data)
1038{
1039 int atid = -1;
1040
1041 spin_lock_bh(&t->atid_lock);
1042 if (t->afree) {
1043 union aopen_entry *p = t->afree;
1044
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001045 atid = (p - t->atid_tab) + t->atid_base;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001046 t->afree = p->next;
1047 p->data = data;
1048 t->atids_in_use++;
1049 }
1050 spin_unlock_bh(&t->atid_lock);
1051 return atid;
1052}
1053EXPORT_SYMBOL(cxgb4_alloc_atid);
1054
1055/*
1056 * Release an active-open TID.
1057 */
1058void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1059{
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001060 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001061
1062 spin_lock_bh(&t->atid_lock);
1063 p->next = t->afree;
1064 t->afree = p;
1065 t->atids_in_use--;
1066 spin_unlock_bh(&t->atid_lock);
1067}
1068EXPORT_SYMBOL(cxgb4_free_atid);
1069
1070/*
1071 * Allocate a server TID and set it to the supplied value.
1072 */
1073int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1074{
1075 int stid;
1076
1077 spin_lock_bh(&t->stid_lock);
1078 if (family == PF_INET) {
1079 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1080 if (stid < t->nstids)
1081 __set_bit(stid, t->stid_bmap);
1082 else
1083 stid = -1;
1084 } else {
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301085 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001086 if (stid < 0)
1087 stid = -1;
1088 }
1089 if (stid >= 0) {
1090 t->stid_tab[stid].data = data;
1091 stid += t->stid_base;
Kumar Sanghvi15f63b72013-12-18 16:38:22 +05301092 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1093 * This is equivalent to 4 TIDs. With CLIP enabled it
1094 * needs 2 TIDs.
1095 */
1096 if (family == PF_INET)
1097 t->stids_in_use++;
1098 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301099 t->stids_in_use += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001100 }
1101 spin_unlock_bh(&t->stid_lock);
1102 return stid;
1103}
1104EXPORT_SYMBOL(cxgb4_alloc_stid);
1105
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001106/* Allocate a server filter TID and set it to the supplied value.
1107 */
1108int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1109{
1110 int stid;
1111
1112 spin_lock_bh(&t->stid_lock);
1113 if (family == PF_INET) {
1114 stid = find_next_zero_bit(t->stid_bmap,
1115 t->nstids + t->nsftids, t->nstids);
1116 if (stid < (t->nstids + t->nsftids))
1117 __set_bit(stid, t->stid_bmap);
1118 else
1119 stid = -1;
1120 } else {
1121 stid = -1;
1122 }
1123 if (stid >= 0) {
1124 t->stid_tab[stid].data = data;
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301125 stid -= t->nstids;
1126 stid += t->sftid_base;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301127 t->sftids_in_use++;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001128 }
1129 spin_unlock_bh(&t->stid_lock);
1130 return stid;
1131}
1132EXPORT_SYMBOL(cxgb4_alloc_sftid);
1133
1134/* Release a server TID.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001135 */
1136void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1137{
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05301138 /* Is it a server filter TID? */
1139 if (t->nsftids && (stid >= t->sftid_base)) {
1140 stid -= t->sftid_base;
1141 stid += t->nstids;
1142 } else {
1143 stid -= t->stid_base;
1144 }
1145
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001146 spin_lock_bh(&t->stid_lock);
1147 if (family == PF_INET)
1148 __clear_bit(stid, t->stid_bmap);
1149 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301150 bitmap_release_region(t->stid_bmap, stid, 1);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001151 t->stid_tab[stid].data = NULL;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301152 if (stid < t->nstids) {
1153 if (family == PF_INET)
1154 t->stids_in_use--;
1155 else
Hariprasad Shenaia99c6832015-12-24 16:15:17 +05301156 t->stids_in_use -= 2;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301157 } else {
1158 t->sftids_in_use--;
1159 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001160 spin_unlock_bh(&t->stid_lock);
1161}
1162EXPORT_SYMBOL(cxgb4_free_stid);
1163
1164/*
1165 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1166 */
1167static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1168 unsigned int tid)
1169{
1170 struct cpl_tid_release *req;
1171
1172 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1173 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1174 INIT_TP_WR(req, tid);
1175 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1176}
1177
1178/*
1179 * Queue a TID release request and if necessary schedule a work queue to
1180 * process it.
1181 */
stephen hemminger31b9c192010-10-18 05:39:18 +00001182static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1183 unsigned int tid)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001184{
1185 void **p = &t->tid_tab[tid];
1186 struct adapter *adap = container_of(t, struct adapter, tids);
1187
1188 spin_lock_bh(&adap->tid_release_lock);
1189 *p = adap->tid_release_head;
1190 /* Low 2 bits encode the Tx channel number */
1191 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1192 if (!adap->tid_release_task_busy) {
1193 adap->tid_release_task_busy = true;
Anish Bhatt29aaee62014-08-20 13:44:06 -07001194 queue_work(adap->workq, &adap->tid_release_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001195 }
1196 spin_unlock_bh(&adap->tid_release_lock);
1197}
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001198
1199/*
1200 * Process the list of pending TID release requests.
1201 */
1202static void process_tid_release_list(struct work_struct *work)
1203{
1204 struct sk_buff *skb;
1205 struct adapter *adap;
1206
1207 adap = container_of(work, struct adapter, tid_release_task);
1208
1209 spin_lock_bh(&adap->tid_release_lock);
1210 while (adap->tid_release_head) {
1211 void **p = adap->tid_release_head;
1212 unsigned int chan = (uintptr_t)p & 3;
1213 p = (void *)p - chan;
1214
1215 adap->tid_release_head = *p;
1216 *p = NULL;
1217 spin_unlock_bh(&adap->tid_release_lock);
1218
1219 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1220 GFP_KERNEL)))
1221 schedule_timeout_uninterruptible(1);
1222
1223 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1224 t4_ofld_send(adap, skb);
1225 spin_lock_bh(&adap->tid_release_lock);
1226 }
1227 adap->tid_release_task_busy = false;
1228 spin_unlock_bh(&adap->tid_release_lock);
1229}
1230
1231/*
1232 * Release a TID and inform HW. If we are unable to allocate the release
1233 * message we defer to a work queue.
1234 */
1235void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1236{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001237 struct sk_buff *skb;
1238 struct adapter *adap = container_of(t, struct adapter, tids);
1239
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301240 WARN_ON(tid >= t->ntids);
1241
1242 if (t->tid_tab[tid]) {
1243 t->tid_tab[tid] = NULL;
1244 if (t->hash_base && (tid >= t->hash_base))
1245 atomic_dec(&t->hash_tids_in_use);
1246 else
1247 atomic_dec(&t->tids_in_use);
1248 }
1249
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001250 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1251 if (likely(skb)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001252 mk_tid_release(skb, chan, tid);
1253 t4_ofld_send(adap, skb);
1254 } else
1255 cxgb4_queue_tid_release(t, chan, tid);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001256}
1257EXPORT_SYMBOL(cxgb4_remove_tid);
1258
1259/*
1260 * Allocate and initialize the TID tables. Returns 0 on success.
1261 */
1262static int tid_init(struct tid_info *t)
1263{
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301264 struct adapter *adap = container_of(t, struct adapter, tids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301265 unsigned int max_ftids = t->nftids + t->nsftids;
1266 unsigned int natids = t->natids;
1267 unsigned int stid_bmap_size;
1268 unsigned int ftid_bmap_size;
1269 size_t size;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001270
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001271 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301272 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001273 size = t->ntids * sizeof(*t->tid_tab) +
1274 natids * sizeof(*t->atid_tab) +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001275 t->nstids * sizeof(*t->stid_tab) +
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001276 t->nsftids * sizeof(*t->stid_tab) +
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001277 stid_bmap_size * sizeof(long) +
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301278 max_ftids * sizeof(*t->ftid_tab) +
1279 ftid_bmap_size * sizeof(long);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001280
Michal Hocko752ade62017-05-08 15:57:27 -07001281 t->tid_tab = kvzalloc(size, GFP_KERNEL);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001282 if (!t->tid_tab)
1283 return -ENOMEM;
1284
1285 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1286 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
Vipul Pandyadca4fae2012-12-10 09:30:53 +00001287 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00001288 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301289 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001290 spin_lock_init(&t->stid_lock);
1291 spin_lock_init(&t->atid_lock);
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301292 spin_lock_init(&t->ftid_lock);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001293
1294 t->stids_in_use = 0;
Hariprasad Shenai2248b292015-08-12 16:55:06 +05301295 t->sftids_in_use = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001296 t->afree = NULL;
1297 t->atids_in_use = 0;
1298 atomic_set(&t->tids_in_use, 0);
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05301299 atomic_set(&t->hash_tids_in_use, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001300
1301 /* Setup the free list for atid_tab and clear the stid bitmap. */
1302 if (natids) {
1303 while (--natids)
1304 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1305 t->afree = t->atid_tab;
1306 }
Kumar Sanghvib6f8eae2013-12-18 16:38:19 +05301307
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05301308 if (is_offload(adap)) {
1309 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1310 /* Reserve stid 0 for T4/T5 adapters */
1311 if (!t->stid_base &&
1312 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1313 __set_bit(0, t->stid_bmap);
1314 }
1315
1316 bitmap_zero(t->ftid_bmap, t->nftids);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001317 return 0;
1318}
1319
1320/**
1321 * cxgb4_create_server - create an IP server
1322 * @dev: the device
1323 * @stid: the server TID
1324 * @sip: local IP address to bind server to
1325 * @sport: the server's TCP port
1326 * @queue: queue to direct messages from this server to
1327 *
1328 * Create an IP server for the given port and address.
1329 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1330 */
1331int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00001332 __be32 sip, __be16 sport, __be16 vlan,
1333 unsigned int queue)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001334{
1335 unsigned int chan;
1336 struct sk_buff *skb;
1337 struct adapter *adap;
1338 struct cpl_pass_open_req *req;
Vipul Pandya80f40c12013-07-04 16:10:45 +05301339 int ret;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001340
1341 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1342 if (!skb)
1343 return -ENOMEM;
1344
1345 adap = netdev2adap(dev);
1346 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1347 INIT_TP_WR(req, 0);
1348 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1349 req->local_port = sport;
1350 req->peer_port = htons(0);
1351 req->local_ip = sip;
1352 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00001353 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001354 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001355 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1356 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301357 ret = t4_mgmt_tx(adap, skb);
1358 return net_xmit_eval(ret);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001359}
1360EXPORT_SYMBOL(cxgb4_create_server);
1361
Vipul Pandya80f40c12013-07-04 16:10:45 +05301362/* cxgb4_create_server6 - create an IPv6 server
1363 * @dev: the device
1364 * @stid: the server TID
1365 * @sip: local IPv6 address to bind server to
1366 * @sport: the server's TCP port
1367 * @queue: queue to direct messages from this server to
1368 *
1369 * Create an IPv6 server for the given port and address.
1370 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1371 */
1372int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1373 const struct in6_addr *sip, __be16 sport,
1374 unsigned int queue)
1375{
1376 unsigned int chan;
1377 struct sk_buff *skb;
1378 struct adapter *adap;
1379 struct cpl_pass_open_req6 *req;
1380 int ret;
1381
1382 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1383 if (!skb)
1384 return -ENOMEM;
1385
1386 adap = netdev2adap(dev);
1387 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1388 INIT_TP_WR(req, 0);
1389 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1390 req->local_port = sport;
1391 req->peer_port = htons(0);
1392 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1393 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1394 req->peer_ip_hi = cpu_to_be64(0);
1395 req->peer_ip_lo = cpu_to_be64(0);
1396 chan = rxq_to_chan(&adap->sge, queue);
Anish Bhattd7990b02014-11-12 17:15:57 -08001397 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
Hariprasad Shenai6c53e932015-01-08 21:38:15 -08001398 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1399 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301400 ret = t4_mgmt_tx(adap, skb);
1401 return net_xmit_eval(ret);
1402}
1403EXPORT_SYMBOL(cxgb4_create_server6);
1404
1405int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1406 unsigned int queue, bool ipv6)
1407{
1408 struct sk_buff *skb;
1409 struct adapter *adap;
1410 struct cpl_close_listsvr_req *req;
1411 int ret;
1412
1413 adap = netdev2adap(dev);
1414
1415 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1416 if (!skb)
1417 return -ENOMEM;
1418
1419 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1420 INIT_TP_WR(req, 0);
1421 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
Hariprasad Shenaibdc590b2015-01-08 21:38:16 -08001422 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1423 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
Vipul Pandya80f40c12013-07-04 16:10:45 +05301424 ret = t4_mgmt_tx(adap, skb);
1425 return net_xmit_eval(ret);
1426}
1427EXPORT_SYMBOL(cxgb4_remove_server);
1428
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001429/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001430 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1431 * @mtus: the HW MTU table
1432 * @mtu: the target MTU
1433 * @idx: index of selected entry in the MTU table
1434 *
1435 * Returns the index and the value in the HW MTU table that is closest to
1436 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1437 * table, in which case that smallest available value is selected.
1438 */
1439unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1440 unsigned int *idx)
1441{
1442 unsigned int i = 0;
1443
1444 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1445 ++i;
1446 if (idx)
1447 *idx = i;
1448 return mtus[i];
1449}
1450EXPORT_SYMBOL(cxgb4_best_mtu);
1451
1452/**
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05301453 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1454 * @mtus: the HW MTU table
1455 * @header_size: Header Size
1456 * @data_size_max: maximum Data Segment Size
1457 * @data_size_align: desired Data Segment Size Alignment (2^N)
1458 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1459 *
1460 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1461 * MTU Table based solely on a Maximum MTU parameter, we break that
1462 * parameter up into a Header Size and Maximum Data Segment Size, and
1463 * provide a desired Data Segment Size Alignment. If we find an MTU in
1464 * the Hardware MTU Table which will result in a Data Segment Size with
1465 * the requested alignment _and_ that MTU isn't "too far" from the
1466 * closest MTU, then we'll return that rather than the closest MTU.
1467 */
1468unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1469 unsigned short header_size,
1470 unsigned short data_size_max,
1471 unsigned short data_size_align,
1472 unsigned int *mtu_idxp)
1473{
1474 unsigned short max_mtu = header_size + data_size_max;
1475 unsigned short data_size_align_mask = data_size_align - 1;
1476 int mtu_idx, aligned_mtu_idx;
1477
1478 /* Scan the MTU Table till we find an MTU which is larger than our
1479 * Maximum MTU or we reach the end of the table. Along the way,
1480 * record the last MTU found, if any, which will result in a Data
1481 * Segment Length matching the requested alignment.
1482 */
1483 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1484 unsigned short data_size = mtus[mtu_idx] - header_size;
1485
1486 /* If this MTU minus the Header Size would result in a
1487 * Data Segment Size of the desired alignment, remember it.
1488 */
1489 if ((data_size & data_size_align_mask) == 0)
1490 aligned_mtu_idx = mtu_idx;
1491
1492 /* If we're not at the end of the Hardware MTU Table and the
1493 * next element is larger than our Maximum MTU, drop out of
1494 * the loop.
1495 */
1496 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1497 break;
1498 }
1499
1500 /* If we fell out of the loop because we ran to the end of the table,
1501 * then we just have to use the last [largest] entry.
1502 */
1503 if (mtu_idx == NMTUS)
1504 mtu_idx--;
1505
1506 /* If we found an MTU which resulted in the requested Data Segment
1507 * Length alignment and that's "not far" from the largest MTU which is
1508 * less than or equal to the maximum MTU, then use that.
1509 */
1510 if (aligned_mtu_idx >= 0 &&
1511 mtu_idx - aligned_mtu_idx <= 1)
1512 mtu_idx = aligned_mtu_idx;
1513
1514 /* If the caller has passed in an MTU Index pointer, pass the
1515 * MTU Index back. Return the MTU value.
1516 */
1517 if (mtu_idxp)
1518 *mtu_idxp = mtu_idx;
1519 return mtus[mtu_idx];
1520}
1521EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1522
1523/**
Hariprasad S27999802015-09-23 17:19:26 +05301524 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1525 * @chip: chip type
1526 * @viid: VI id of the given port
1527 *
1528 * Return the SMT index for this VI.
1529 */
1530unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1531{
1532 /* In T4/T5, SMT contains 256 SMAC entries organized in
1533 * 128 rows of 2 entries each.
1534 * In T6, SMT contains 256 SMAC entries in 256 rows.
1535 * TODO: The below code needs to be updated when we add support
1536 * for 256 VFs.
1537 */
1538 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1539 return ((viid & 0x7f) << 1);
1540 else
1541 return (viid & 0x7f);
1542}
1543EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1544
1545/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001546 * cxgb4_port_chan - get the HW channel of a port
1547 * @dev: the net device for the port
1548 *
1549 * Return the HW Tx channel of the given port.
1550 */
1551unsigned int cxgb4_port_chan(const struct net_device *dev)
1552{
1553 return netdev2pinfo(dev)->tx_chan;
1554}
1555EXPORT_SYMBOL(cxgb4_port_chan);
1556
Vipul Pandya881806b2012-05-18 15:29:24 +05301557unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1558{
1559 struct adapter *adap = netdev2adap(dev);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001560 u32 v1, v2, lp_count, hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301561
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301562 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1563 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301564 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301565 lp_count = LP_COUNT_G(v1);
1566 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001567 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301568 lp_count = LP_COUNT_T5_G(v1);
1569 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001570 }
1571 return lpfifo ? lp_count : hp_count;
Vipul Pandya881806b2012-05-18 15:29:24 +05301572}
1573EXPORT_SYMBOL(cxgb4_dbfifo_count);
1574
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001575/**
1576 * cxgb4_port_viid - get the VI id of a port
1577 * @dev: the net device for the port
1578 *
1579 * Return the VI id of the given port.
1580 */
1581unsigned int cxgb4_port_viid(const struct net_device *dev)
1582{
1583 return netdev2pinfo(dev)->viid;
1584}
1585EXPORT_SYMBOL(cxgb4_port_viid);
1586
1587/**
1588 * cxgb4_port_idx - get the index of a port
1589 * @dev: the net device for the port
1590 *
1591 * Return the index of the given port.
1592 */
1593unsigned int cxgb4_port_idx(const struct net_device *dev)
1594{
1595 return netdev2pinfo(dev)->port_id;
1596}
1597EXPORT_SYMBOL(cxgb4_port_idx);
1598
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001599void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1600 struct tp_tcp_stats *v6)
1601{
1602 struct adapter *adap = pci_get_drvdata(pdev);
1603
1604 spin_lock(&adap->stats_lock);
1605 t4_tp_get_tcp_stats(adap, v4, v6);
1606 spin_unlock(&adap->stats_lock);
1607}
1608EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1609
1610void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1611 const unsigned int *pgsz_order)
1612{
1613 struct adapter *adap = netdev2adap(dev);
1614
Hariprasad Shenai0d804332015-01-05 16:30:47 +05301615 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1616 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1617 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1618 HPZ3_V(pgsz_order[3]));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001619}
1620EXPORT_SYMBOL(cxgb4_iscsi_init);
1621
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301622int cxgb4_flush_eq_cache(struct net_device *dev)
1623{
1624 struct adapter *adap = netdev2adap(dev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301625
Hariprasad Shenai5d700ec2015-06-05 14:24:48 +05301626 return t4_sge_ctxt_flush(adap, adap->mbox);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301627}
1628EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1629
1630static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1631{
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301632 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301633 __be64 indices;
1634 int ret;
1635
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05301636 spin_lock(&adap->win0_lock);
1637 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1638 sizeof(indices), (__be32 *)&indices,
1639 T4_MEMORY_READ);
1640 spin_unlock(&adap->win0_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301641 if (!ret) {
Vipul Pandya404d9e32012-10-08 02:59:43 +00001642 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1643 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301644 }
1645 return ret;
1646}
1647
1648int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1649 u16 size)
1650{
1651 struct adapter *adap = netdev2adap(dev);
1652 u16 hw_pidx, hw_cidx;
1653 int ret;
1654
1655 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1656 if (ret)
1657 goto out;
1658
1659 if (pidx != hw_pidx) {
1660 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301661 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301662
1663 if (pidx >= hw_pidx)
1664 delta = pidx - hw_pidx;
1665 else
1666 delta = size - hw_pidx + pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301667
1668 if (is_t4(adap->params.chip))
1669 val = PIDX_V(delta);
1670 else
1671 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301672 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301673 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1674 QID_V(qid) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301675 }
1676out:
1677 return ret;
1678}
1679EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1680
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301681int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1682{
1683 struct adapter *adap;
1684 u32 offset, memtype, memaddr;
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301685 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301686 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1687 int ret;
1688
1689 adap = netdev2adap(dev);
1690
1691 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1692
1693 /* Figure out where the offset lands in the Memory Type/Address scheme.
1694 * This code assumes that the memory is laid out starting at offset 0
1695 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1696 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1697 * MC0, and some have both MC0 and MC1.
1698 */
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301699 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1700 edc0_size = EDRAM0_SIZE_G(size) << 20;
1701 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1702 edc1_size = EDRAM1_SIZE_G(size) << 20;
1703 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1704 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301705
1706 edc0_end = edc0_size;
1707 edc1_end = edc0_end + edc1_size;
1708 mc0_end = edc1_end + mc0_size;
1709
1710 if (offset < edc0_end) {
1711 memtype = MEM_EDC0;
1712 memaddr = offset;
1713 } else if (offset < edc1_end) {
1714 memtype = MEM_EDC1;
1715 memaddr = offset - edc0_end;
1716 } else {
1717 if (offset < mc0_end) {
1718 memtype = MEM_MC0;
1719 memaddr = offset - edc1_end;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301720 } else if (is_t5(adap->params.chip)) {
Hariprasad Shenai6559a7e2014-11-07 09:35:24 +05301721 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1722 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301723 mc1_end = mc0_end + mc1_size;
1724 if (offset < mc1_end) {
1725 memtype = MEM_MC1;
1726 memaddr = offset - mc0_end;
1727 } else {
1728 /* offset beyond the end of any memory */
1729 goto err;
1730 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301731 } else {
1732 /* T4/T6 only has a single memory channel */
1733 goto err;
Hariprasad Shenai031cf472014-07-14 21:34:53 +05301734 }
1735 }
1736
1737 spin_lock(&adap->win0_lock);
1738 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1739 spin_unlock(&adap->win0_lock);
1740 return ret;
1741
1742err:
1743 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1744 stag, offset);
1745 return -EINVAL;
1746}
1747EXPORT_SYMBOL(cxgb4_read_tpte);
1748
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301749u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1750{
1751 u32 hi, lo;
1752 struct adapter *adap;
1753
1754 adap = netdev2adap(dev);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301755 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1756 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
Hariprasad Shenai7730b4c2014-07-14 21:34:54 +05301757
1758 return ((u64)hi << 32) | (u64)lo;
1759}
1760EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1761
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301762int cxgb4_bar2_sge_qregs(struct net_device *dev,
1763 unsigned int qid,
1764 enum cxgb4_bar2_qtype qtype,
Hariprasad S66cf1882015-06-09 18:23:11 +05301765 int user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301766 u64 *pbar2_qoffset,
1767 unsigned int *pbar2_qid)
1768{
Hariprasad Shenaib2612722015-05-27 22:30:24 +05301769 return t4_bar2_sge_qregs(netdev2adap(dev),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301770 qid,
1771 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1772 ? T4_BAR2_QTYPE_EGRESS
1773 : T4_BAR2_QTYPE_INGRESS),
Hariprasad S66cf1882015-06-09 18:23:11 +05301774 user,
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05301775 pbar2_qoffset,
1776 pbar2_qid);
1777}
1778EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1779
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001780static struct pci_driver cxgb4_driver;
1781
1782static void check_neigh_update(struct neighbour *neigh)
1783{
1784 const struct device *parent;
1785 const struct net_device *netdev = neigh->dev;
1786
Parav Panditd0d7b102017-02-04 11:00:49 -06001787 if (is_vlan_dev(netdev))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001788 netdev = vlan_dev_real_dev(netdev);
1789 parent = netdev->dev.parent;
1790 if (parent && parent->driver == &cxgb4_driver.driver)
1791 t4_l2t_update(dev_get_drvdata(parent), neigh);
1792}
1793
1794static int netevent_cb(struct notifier_block *nb, unsigned long event,
1795 void *data)
1796{
1797 switch (event) {
1798 case NETEVENT_NEIGH_UPDATE:
1799 check_neigh_update(data);
1800 break;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001801 case NETEVENT_REDIRECT:
1802 default:
1803 break;
1804 }
1805 return 0;
1806}
1807
1808static bool netevent_registered;
1809static struct notifier_block cxgb4_netevent_nb = {
1810 .notifier_call = netevent_cb
1811};
1812
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301813static void drain_db_fifo(struct adapter *adap, int usecs)
1814{
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001815 u32 v1, v2, lp_count, hp_count;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301816
1817 do {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301818 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1819 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301820 if (is_t4(adap->params.chip)) {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301821 lp_count = LP_COUNT_G(v1);
1822 hp_count = HP_COUNT_G(v1);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001823 } else {
Hariprasad Shenaif061de422015-01-05 16:30:44 +05301824 lp_count = LP_COUNT_T5_G(v1);
1825 hp_count = HP_COUNT_T5_G(v2);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001826 }
1827
1828 if (lp_count == 0 && hp_count == 0)
1829 break;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301830 set_current_state(TASK_UNINTERRUPTIBLE);
1831 schedule_timeout(usecs_to_jiffies(usecs));
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301832 } while (1);
1833}
1834
1835static void disable_txq_db(struct sge_txq *q)
1836{
Steve Wise05eb2382014-03-14 21:52:08 +05301837 unsigned long flags;
1838
1839 spin_lock_irqsave(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301840 q->db_disabled = 1;
Steve Wise05eb2382014-03-14 21:52:08 +05301841 spin_unlock_irqrestore(&q->db_lock, flags);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301842}
1843
Steve Wise05eb2382014-03-14 21:52:08 +05301844static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301845{
1846 spin_lock_irq(&q->db_lock);
Steve Wise05eb2382014-03-14 21:52:08 +05301847 if (q->db_pidx_inc) {
1848 /* Make sure that all writes to the TX descriptors
1849 * are committed before we tell HW about them.
1850 */
1851 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301852 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1853 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
Steve Wise05eb2382014-03-14 21:52:08 +05301854 q->db_pidx_inc = 0;
1855 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301856 q->db_disabled = 0;
1857 spin_unlock_irq(&q->db_lock);
1858}
1859
1860static void disable_dbs(struct adapter *adap)
1861{
1862 int i;
1863
1864 for_each_ethrxq(&adap->sge, i)
1865 disable_txq_db(&adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301866 if (is_offload(adap)) {
1867 struct sge_uld_txq_info *txq_info =
1868 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1869
1870 if (txq_info) {
1871 for_each_ofldtxq(&adap->sge, i) {
1872 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1873
1874 disable_txq_db(&txq->q);
1875 }
1876 }
1877 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301878 for_each_port(adap, i)
1879 disable_txq_db(&adap->sge.ctrlq[i].q);
1880}
1881
1882static void enable_dbs(struct adapter *adap)
1883{
1884 int i;
1885
1886 for_each_ethrxq(&adap->sge, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301887 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301888 if (is_offload(adap)) {
1889 struct sge_uld_txq_info *txq_info =
1890 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1891
1892 if (txq_info) {
1893 for_each_ofldtxq(&adap->sge, i) {
1894 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1895
1896 enable_txq_db(adap, &txq->q);
1897 }
1898 }
1899 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301900 for_each_port(adap, i)
Steve Wise05eb2382014-03-14 21:52:08 +05301901 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1902}
1903
1904static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1905{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301906 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1907
1908 if (adap->uld && adap->uld[type].handle)
1909 adap->uld[type].control(adap->uld[type].handle, cmd);
Steve Wise05eb2382014-03-14 21:52:08 +05301910}
1911
1912static void process_db_full(struct work_struct *work)
1913{
1914 struct adapter *adap;
1915
1916 adap = container_of(work, struct adapter, db_full_task);
1917
1918 drain_db_fifo(adap, dbfifo_drain_delay);
1919 enable_dbs(adap);
1920 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301921 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1922 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1923 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1924 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1925 else
1926 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1927 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301928}
1929
1930static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1931{
1932 u16 hw_pidx, hw_cidx;
1933 int ret;
1934
Steve Wise05eb2382014-03-14 21:52:08 +05301935 spin_lock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301936 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1937 if (ret)
1938 goto out;
1939 if (q->db_pidx != hw_pidx) {
1940 u16 delta;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301941 u32 val;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301942
1943 if (q->db_pidx >= hw_pidx)
1944 delta = q->db_pidx - hw_pidx;
1945 else
1946 delta = q->size - hw_pidx + q->db_pidx;
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301947
1948 if (is_t4(adap->params.chip))
1949 val = PIDX_V(delta);
1950 else
1951 val = PIDX_T5_V(delta);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301952 wmb();
Hariprasad Shenaif612b812015-01-05 16:30:43 +05301953 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1954 QID_V(q->cntxt_id) | val);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301955 }
1956out:
1957 q->db_disabled = 0;
Steve Wise05eb2382014-03-14 21:52:08 +05301958 q->db_pidx_inc = 0;
1959 spin_unlock_irq(&q->db_lock);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301960 if (ret)
1961 CH_WARN(adap, "DB drop recovery failed.\n");
1962}
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05301963
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301964static void recover_all_queues(struct adapter *adap)
1965{
1966 int i;
1967
1968 for_each_ethrxq(&adap->sge, i)
1969 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05301970 if (is_offload(adap)) {
1971 struct sge_uld_txq_info *txq_info =
1972 adap->sge.uld_txq_info[CXGB4_TX_OFLD];
1973 if (txq_info) {
1974 for_each_ofldtxq(&adap->sge, i) {
1975 struct sge_uld_txq *txq = &txq_info->uldtxq[i];
1976
1977 sync_txq_pidx(adap, &txq->q);
1978 }
1979 }
1980 }
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301981 for_each_port(adap, i)
1982 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
1983}
1984
Vipul Pandya881806b2012-05-18 15:29:24 +05301985static void process_db_drop(struct work_struct *work)
1986{
1987 struct adapter *adap;
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05301988
Vipul Pandya881806b2012-05-18 15:29:24 +05301989 adap = container_of(work, struct adapter, db_drop_task);
1990
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05301991 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05301992 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001993 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
Steve Wise05eb2382014-03-14 21:52:08 +05301994 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001995 recover_all_queues(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05301996 drain_db_fifo(adap, dbfifo_drain_delay);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00001997 enable_dbs(adap);
Steve Wise05eb2382014-03-14 21:52:08 +05301998 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05301999 } else if (is_t5(adap->params.chip)) {
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002000 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2001 u16 qid = (dropped_db >> 15) & 0x1ffff;
2002 u16 pidx_inc = dropped_db & 0x1fff;
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302003 u64 bar2_qoffset;
2004 unsigned int bar2_qid;
2005 int ret;
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002006
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302007 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
Linus Torvaldse0456712015-06-24 16:49:49 -07002008 0, &bar2_qoffset, &bar2_qid);
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302009 if (ret)
2010 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2011 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2012 else
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302013 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05302014 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002015
2016 /* Re-enable BAR2 WC */
2017 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2018 }
2019
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05302020 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2021 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
Vipul Pandya881806b2012-05-18 15:29:24 +05302022}
2023
2024void t4_db_full(struct adapter *adap)
2025{
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05302026 if (is_t4(adap->params.chip)) {
Steve Wise05eb2382014-03-14 21:52:08 +05302027 disable_dbs(adap);
2028 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05302029 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2030 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
Anish Bhatt29aaee62014-08-20 13:44:06 -07002031 queue_work(adap->workq, &adap->db_full_task);
Santosh Rastapur2cc301d2013-03-14 05:08:52 +00002032 }
Vipul Pandya881806b2012-05-18 15:29:24 +05302033}
2034
2035void t4_db_dropped(struct adapter *adap)
2036{
Steve Wise05eb2382014-03-14 21:52:08 +05302037 if (is_t4(adap->params.chip)) {
2038 disable_dbs(adap);
2039 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2040 }
Anish Bhatt29aaee62014-08-20 13:44:06 -07002041 queue_work(adap->workq, &adap->db_drop_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302042}
2043
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05302044void t4_register_netevent_notifier(void)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002045{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002046 if (!netevent_registered) {
2047 register_netevent_notifier(&cxgb4_netevent_nb);
2048 netevent_registered = true;
2049 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002050}
2051
2052static void detach_ulds(struct adapter *adap)
2053{
2054 unsigned int i;
2055
2056 mutex_lock(&uld_mutex);
2057 list_del(&adap->list_node);
2058 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302059 if (adap->uld && adap->uld[i].handle) {
2060 adap->uld[i].state_change(adap->uld[i].handle,
2061 CXGB4_STATE_DETACH);
2062 adap->uld[i].handle = NULL;
2063 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002064 if (netevent_registered && list_empty(&adapter_list)) {
2065 unregister_netevent_notifier(&cxgb4_netevent_nb);
2066 netevent_registered = false;
2067 }
2068 mutex_unlock(&uld_mutex);
2069}
2070
2071static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2072{
2073 unsigned int i;
2074
2075 mutex_lock(&uld_mutex);
2076 for (i = 0; i < CXGB4_ULD_MAX; i++)
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05302077 if (adap->uld && adap->uld[i].handle)
2078 adap->uld[i].state_change(adap->uld[i].handle,
2079 new_state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002080 mutex_unlock(&uld_mutex);
2081}
2082
Anish Bhatt1bb60372014-10-14 20:07:22 -07002083#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002084static int cxgb4_inet6addr_handler(struct notifier_block *this,
2085 unsigned long event, void *data)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302086{
Anish Bhattb5a02f52015-01-14 15:17:34 -08002087 struct inet6_ifaddr *ifa = data;
2088 struct net_device *event_dev = ifa->idev->dev;
2089 const struct device *parent = NULL;
2090#if IS_ENABLED(CONFIG_BONDING)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302091 struct adapter *adap;
Anish Bhattb5a02f52015-01-14 15:17:34 -08002092#endif
Parav Panditd0d7b102017-02-04 11:00:49 -06002093 if (is_vlan_dev(event_dev))
Anish Bhattb5a02f52015-01-14 15:17:34 -08002094 event_dev = vlan_dev_real_dev(event_dev);
2095#if IS_ENABLED(CONFIG_BONDING)
2096 if (event_dev->flags & IFF_MASTER) {
2097 list_for_each_entry(adap, &adapter_list, list_node) {
2098 switch (event) {
2099 case NETDEV_UP:
2100 cxgb4_clip_get(adap->port[0],
2101 (const u32 *)ifa, 1);
2102 break;
2103 case NETDEV_DOWN:
2104 cxgb4_clip_release(adap->port[0],
2105 (const u32 *)ifa, 1);
2106 break;
2107 default:
2108 break;
2109 }
2110 }
2111 return NOTIFY_OK;
2112 }
2113#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05302114
Anish Bhattb5a02f52015-01-14 15:17:34 -08002115 if (event_dev)
2116 parent = event_dev->dev.parent;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302117
Anish Bhattb5a02f52015-01-14 15:17:34 -08002118 if (parent && parent->driver == &cxgb4_driver.driver) {
Vipul Pandya01bcca62013-07-04 16:10:46 +05302119 switch (event) {
2120 case NETDEV_UP:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002121 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302122 break;
2123 case NETDEV_DOWN:
Anish Bhattb5a02f52015-01-14 15:17:34 -08002124 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302125 break;
2126 default:
2127 break;
2128 }
2129 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08002130 return NOTIFY_OK;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302131}
2132
Anish Bhattb5a02f52015-01-14 15:17:34 -08002133static bool inet6addr_registered;
Vipul Pandya01bcca62013-07-04 16:10:46 +05302134static struct notifier_block cxgb4_inet6addr_notifier = {
2135 .notifier_call = cxgb4_inet6addr_handler
2136};
2137
Vipul Pandya01bcca62013-07-04 16:10:46 +05302138static void update_clip(const struct adapter *adap)
2139{
2140 int i;
2141 struct net_device *dev;
2142 int ret;
2143
2144 rcu_read_lock();
2145
2146 for (i = 0; i < MAX_NPORTS; i++) {
2147 dev = adap->port[i];
2148 ret = 0;
2149
2150 if (dev)
Anish Bhattb5a02f52015-01-14 15:17:34 -08002151 ret = cxgb4_update_root_dev_clip(dev);
Vipul Pandya01bcca62013-07-04 16:10:46 +05302152
2153 if (ret < 0)
2154 break;
2155 }
2156 rcu_read_unlock();
2157}
Anish Bhatt1bb60372014-10-14 20:07:22 -07002158#endif /* IS_ENABLED(CONFIG_IPV6) */
Vipul Pandya01bcca62013-07-04 16:10:46 +05302159
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002160/**
2161 * cxgb_up - enable the adapter
2162 * @adap: adapter being enabled
2163 *
2164 * Called when the first port is enabled, this function performs the
2165 * actions necessary to make an adapter operational, such as completing
2166 * the initialization of HW modules, and enabling interrupts.
2167 *
2168 * Must be called with the rtnl lock held.
2169 */
2170static int cxgb_up(struct adapter *adap)
2171{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002172 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002173
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002174 err = setup_sge_queues(adap);
2175 if (err)
2176 goto out;
2177 err = setup_rss(adap);
2178 if (err)
2179 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002180
2181 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002182 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002183 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2184 adap->msix_info[0].desc, adap);
2185 if (err)
2186 goto irq_err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002187 err = request_msix_queue_irqs(adap);
2188 if (err) {
2189 free_irq(adap->msix_info[0].vec, adap);
2190 goto irq_err;
2191 }
2192 } else {
2193 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2194 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00002195 adap->port[0]->name, adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002196 if (err)
2197 goto irq_err;
2198 }
Ganesh Goudare7519f92017-05-31 18:26:28 +05302199
2200 mutex_lock(&uld_mutex);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002201 enable_rx(adap);
2202 t4_sge_start(adap);
2203 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002204 adap->flags |= FULL_INIT_DONE;
Ganesh Goudare7519f92017-05-31 18:26:28 +05302205 mutex_unlock(&uld_mutex);
2206
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002207 notify_ulds(adap, CXGB4_STATE_UP);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002208#if IS_ENABLED(CONFIG_IPV6)
Vipul Pandya01bcca62013-07-04 16:10:46 +05302209 update_clip(adap);
Anish Bhatt1bb60372014-10-14 20:07:22 -07002210#endif
Hariprasad Shenaifc08a012016-02-16 10:07:09 +05302211 /* Initialize hash mac addr list*/
2212 INIT_LIST_HEAD(&adap->mac_hlist);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002213 out:
2214 return err;
2215 irq_err:
2216 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002217 freeq:
2218 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002219 goto out;
2220}
2221
2222static void cxgb_down(struct adapter *adapter)
2223{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002224 cancel_work_sync(&adapter->tid_release_task);
Vipul Pandya881806b2012-05-18 15:29:24 +05302225 cancel_work_sync(&adapter->db_full_task);
2226 cancel_work_sync(&adapter->db_drop_task);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002227 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002228 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002229
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002230 t4_sge_stop(adapter);
2231 t4_free_sge_resources(adapter);
2232 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002233}
2234
2235/*
2236 * net_device operations
2237 */
2238static int cxgb_open(struct net_device *dev)
2239{
2240 int err;
2241 struct port_info *pi = netdev_priv(dev);
2242 struct adapter *adapter = pi->adapter;
2243
Dimitris Michailidis6a3c8692011-01-19 15:29:05 +00002244 netif_carrier_off(dev);
2245
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002246 if (!(adapter->flags & FULL_INIT_DONE)) {
2247 err = cxgb_up(adapter);
2248 if (err < 0)
2249 return err;
2250 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002251
Ganesh Goudar2061ec32017-05-19 17:50:15 +05302252 /* It's possible that the basic port information could have
2253 * changed since we first read it.
2254 */
2255 err = t4_update_port_info(pi);
2256 if (err < 0)
2257 return err;
2258
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002259 err = link_start(dev);
2260 if (!err)
2261 netif_tx_start_all_queues(dev);
2262 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002263}
2264
2265static int cxgb_close(struct net_device *dev)
2266{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002267 struct port_info *pi = netdev_priv(dev);
2268 struct adapter *adapter = pi->adapter;
2269
2270 netif_tx_stop_all_queues(dev);
2271 netif_carrier_off(dev);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302272 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002273}
2274
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002275int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
Vipul Pandya793dad92012-12-10 09:30:56 +00002276 __be32 sip, __be16 sport, __be16 vlan,
2277 unsigned int queue, unsigned char port, unsigned char mask)
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002278{
2279 int ret;
2280 struct filter_entry *f;
2281 struct adapter *adap;
2282 int i;
2283 u8 *val;
2284
2285 adap = netdev2adap(dev);
2286
Vipul Pandya1cab7752012-12-10 09:30:55 +00002287 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302288 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002289 stid += adap->tids.nftids;
2290
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002291 /* Check to make sure the filter requested is writable ...
2292 */
2293 f = &adap->tids.ftid_tab[stid];
2294 ret = writable_filter(f);
2295 if (ret)
2296 return ret;
2297
2298 /* Clear out any old resources being used by the filter before
2299 * we start constructing the new filter.
2300 */
2301 if (f->valid)
2302 clear_filter(adap, f);
2303
2304 /* Clear out filter specifications */
2305 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2306 f->fs.val.lport = cpu_to_be16(sport);
2307 f->fs.mask.lport = ~0;
2308 val = (u8 *)&sip;
Vipul Pandya793dad92012-12-10 09:30:56 +00002309 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002310 for (i = 0; i < 4; i++) {
2311 f->fs.val.lip[i] = val[i];
2312 f->fs.mask.lip[i] = ~0;
2313 }
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302314 if (adap->params.tp.vlan_pri_map & PORT_F) {
Vipul Pandya793dad92012-12-10 09:30:56 +00002315 f->fs.val.iport = port;
2316 f->fs.mask.iport = mask;
2317 }
2318 }
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002319
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302320 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
Kumar Sanghvi7c89e552013-12-18 16:38:20 +05302321 f->fs.val.proto = IPPROTO_TCP;
2322 f->fs.mask.proto = ~0;
2323 }
2324
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002325 f->fs.dirsteer = 1;
2326 f->fs.iq = queue;
2327 /* Mark filter as locked */
2328 f->locked = 1;
2329 f->fs.rpttid = 1;
2330
Ganesh Goudar6b254af2017-04-10 21:26:18 +05302331 /* Save the actual tid. We need this to get the corresponding
2332 * filter entry structure in filter_rpl.
2333 */
2334 f->tid = stid + adap->tids.ftid_base;
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002335 ret = set_filter_wr(adap, stid);
2336 if (ret) {
2337 clear_filter(adap, f);
2338 return ret;
2339 }
2340
2341 return 0;
2342}
2343EXPORT_SYMBOL(cxgb4_create_server_filter);
2344
2345int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2346 unsigned int queue, bool ipv6)
2347{
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002348 struct filter_entry *f;
2349 struct adapter *adap;
2350
2351 adap = netdev2adap(dev);
Vipul Pandya1cab7752012-12-10 09:30:55 +00002352
2353 /* Adjust stid to correct filter index */
Kumar Sanghvi470c60c2013-12-18 16:38:21 +05302354 stid -= adap->tids.sftid_base;
Vipul Pandya1cab7752012-12-10 09:30:55 +00002355 stid += adap->tids.nftids;
2356
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002357 f = &adap->tids.ftid_tab[stid];
2358 /* Unlock the filter */
2359 f->locked = 0;
2360
Wei Yongjun8c148462016-08-20 15:32:41 +00002361 return delete_filter(adap, stid);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002362}
2363EXPORT_SYMBOL(cxgb4_remove_server_filter);
2364
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002365static void cxgb_get_stats(struct net_device *dev,
2366 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002367{
2368 struct port_stats stats;
2369 struct port_info *p = netdev_priv(dev);
2370 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002371
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002372 /* Block retrieving statistics during EEH error
2373 * recovery. Otherwise, the recovery might fail
2374 * and the PCI device will be removed permanently
2375 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002376 spin_lock(&adapter->stats_lock);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002377 if (!netif_device_present(dev)) {
2378 spin_unlock(&adapter->stats_lock);
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002379 return;
Gavin Shan9fe6cb52014-01-23 12:27:35 +08002380 }
Hariprasad Shenaia4cfd922015-06-03 21:04:39 +05302381 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2382 &p->stats_base);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002383 spin_unlock(&adapter->stats_lock);
2384
2385 ns->tx_bytes = stats.tx_octets;
2386 ns->tx_packets = stats.tx_frames;
2387 ns->rx_bytes = stats.rx_octets;
2388 ns->rx_packets = stats.rx_frames;
2389 ns->multicast = stats.rx_mcast_frames;
2390
2391 /* detailed rx_errors */
2392 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2393 stats.rx_runt;
2394 ns->rx_over_errors = 0;
2395 ns->rx_crc_errors = stats.rx_fcs_err;
2396 ns->rx_frame_errors = stats.rx_symbol_err;
Ganesh Goudarb93f79b2017-02-15 11:45:25 +05302397 ns->rx_dropped = stats.rx_ovflow0 + stats.rx_ovflow1 +
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002398 stats.rx_ovflow2 + stats.rx_ovflow3 +
2399 stats.rx_trunc0 + stats.rx_trunc1 +
2400 stats.rx_trunc2 + stats.rx_trunc3;
2401 ns->rx_missed_errors = 0;
2402
2403 /* detailed tx_errors */
2404 ns->tx_aborted_errors = 0;
2405 ns->tx_carrier_errors = 0;
2406 ns->tx_fifo_errors = 0;
2407 ns->tx_heartbeat_errors = 0;
2408 ns->tx_window_errors = 0;
2409
2410 ns->tx_errors = stats.tx_error_frames;
2411 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2412 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002413}
2414
2415static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2416{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002417 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002418 int ret = 0, prtad, devad;
2419 struct port_info *pi = netdev_priv(dev);
2420 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2421
2422 switch (cmd) {
2423 case SIOCGMIIPHY:
2424 if (pi->mdio_addr < 0)
2425 return -EOPNOTSUPP;
2426 data->phy_id = pi->mdio_addr;
2427 break;
2428 case SIOCGMIIREG:
2429 case SIOCSMIIREG:
2430 if (mdio_phy_id_is_c45(data->phy_id)) {
2431 prtad = mdio_phy_id_prtad(data->phy_id);
2432 devad = mdio_phy_id_devad(data->phy_id);
2433 } else if (data->phy_id < 32) {
2434 prtad = data->phy_id;
2435 devad = 0;
2436 data->reg_num &= 0x1f;
2437 } else
2438 return -EINVAL;
2439
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302440 mbox = pi->adapter->pf;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002441 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002442 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002443 data->reg_num, &data->val_out);
2444 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002445 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002446 data->reg_num, data->val_in);
2447 break;
Hariprasad Shenai5e2a5eb2015-09-28 10:26:53 +05302448 case SIOCGHWTSTAMP:
2449 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2450 sizeof(pi->tstamp_config)) ?
2451 -EFAULT : 0;
2452 case SIOCSHWTSTAMP:
2453 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2454 sizeof(pi->tstamp_config)))
2455 return -EFAULT;
2456
2457 switch (pi->tstamp_config.rx_filter) {
2458 case HWTSTAMP_FILTER_NONE:
2459 pi->rxtstamp = false;
2460 break;
2461 case HWTSTAMP_FILTER_ALL:
2462 pi->rxtstamp = true;
2463 break;
2464 default:
2465 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2466 return -ERANGE;
2467 }
2468
2469 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2470 sizeof(pi->tstamp_config)) ?
2471 -EFAULT : 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002472 default:
2473 return -EOPNOTSUPP;
2474 }
2475 return ret;
2476}
2477
2478static void cxgb_set_rxmode(struct net_device *dev)
2479{
2480 /* unfortunately we can't return errors to the stack */
2481 set_rxmode(dev, -1, false);
2482}
2483
2484static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2485{
2486 int ret;
2487 struct port_info *pi = netdev_priv(dev);
2488
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302489 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002490 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002491 if (!ret)
2492 dev->mtu = new_mtu;
2493 return ret;
2494}
2495
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302496#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302497static int dummy_open(struct net_device *dev)
2498{
2499 /* Turn carrier off since we don't have to transmit anything on this
2500 * interface.
2501 */
2502 netif_carrier_off(dev);
2503 return 0;
2504}
2505
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302506/* Fill MAC address that will be assigned by the FW */
2507static void fill_vf_station_mac_addr(struct adapter *adap)
2508{
2509 unsigned int i;
2510 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2511 int err;
2512 u8 *na;
2513 u16 a, b;
2514
2515 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2516 if (!err) {
2517 na = adap->params.vpd.na;
2518 for (i = 0; i < ETH_ALEN; i++)
2519 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2520 hex2val(na[2 * i + 1]));
2521 a = (hw_addr[0] << 8) | hw_addr[1];
2522 b = (hw_addr[1] << 8) | hw_addr[2];
2523 a ^= b;
2524 a |= 0x0200; /* locally assigned Ethernet MAC address */
2525 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2526 macaddr[0] = a >> 8;
2527 macaddr[1] = a & 0xff;
2528
2529 for (i = 2; i < 5; i++)
2530 macaddr[i] = hw_addr[i + 1];
2531
2532 for (i = 0; i < adap->num_vfs; i++) {
2533 macaddr[5] = adap->pf * 16 + i;
2534 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2535 }
2536 }
2537}
2538
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302539static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2540{
2541 struct port_info *pi = netdev_priv(dev);
2542 struct adapter *adap = pi->adapter;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302543 int ret;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302544
2545 /* verify MAC addr is valid */
2546 if (!is_valid_ether_addr(mac)) {
2547 dev_err(pi->adapter->pdev_dev,
2548 "Invalid Ethernet address %pM for VF %d\n",
2549 mac, vf);
2550 return -EINVAL;
2551 }
2552
2553 dev_info(pi->adapter->pdev_dev,
2554 "Setting MAC %pM on VF %d\n", mac, vf);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302555 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2556 if (!ret)
2557 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2558 return ret;
2559}
2560
2561static int cxgb_get_vf_config(struct net_device *dev,
2562 int vf, struct ifla_vf_info *ivi)
2563{
2564 struct port_info *pi = netdev_priv(dev);
2565 struct adapter *adap = pi->adapter;
2566
2567 if (vf >= adap->num_vfs)
2568 return -EINVAL;
2569 ivi->vf = vf;
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302570 ivi->max_tx_rate = adap->vfinfo[vf].tx_rate;
2571 ivi->min_tx_rate = 0;
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302572 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2573 return 0;
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302574}
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302575
2576static int cxgb_get_phys_port_id(struct net_device *dev,
2577 struct netdev_phys_item_id *ppid)
2578{
2579 struct port_info *pi = netdev_priv(dev);
2580 unsigned int phy_port_id;
2581
2582 phy_port_id = pi->adapter->adap_idx * 10 + pi->port_id;
2583 ppid->id_len = sizeof(phy_port_id);
2584 memcpy(ppid->id, &phy_port_id, ppid->id_len);
2585 return 0;
2586}
2587
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302588static int cxgb_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2589 int max_tx_rate)
2590{
2591 struct port_info *pi = netdev_priv(dev);
2592 struct adapter *adap = pi->adapter;
2593 struct fw_port_cmd port_cmd, port_rpl;
2594 u32 link_status, speed = 0;
2595 u32 fw_pfvf, fw_class;
2596 int class_id = vf;
2597 int link_ok, ret;
2598 u16 pktsize;
2599
2600 if (vf >= adap->num_vfs)
2601 return -EINVAL;
2602
2603 if (min_tx_rate) {
2604 dev_err(adap->pdev_dev,
2605 "Min tx rate (%d) (> 0) for VF %d is Invalid.\n",
2606 min_tx_rate, vf);
2607 return -EINVAL;
2608 }
2609 /* Retrieve link details for VF port */
2610 memset(&port_cmd, 0, sizeof(port_cmd));
2611 port_cmd.op_to_portid = cpu_to_be32(FW_CMD_OP_V(FW_PORT_CMD) |
2612 FW_CMD_REQUEST_F |
2613 FW_CMD_READ_F |
2614 FW_PORT_CMD_PORTID_V(pi->port_id));
2615 port_cmd.action_to_len16 =
2616 cpu_to_be32(FW_PORT_CMD_ACTION_V(FW_PORT_ACTION_GET_PORT_INFO) |
2617 FW_LEN16(port_cmd));
2618 ret = t4_wr_mbox(adap, adap->mbox, &port_cmd, sizeof(port_cmd),
2619 &port_rpl);
2620 if (ret != FW_SUCCESS) {
2621 dev_err(adap->pdev_dev,
2622 "Failed to get link status for VF %d\n", vf);
2623 return -EINVAL;
2624 }
2625 link_status = be32_to_cpu(port_rpl.u.info.lstatus_to_modtype);
2626 link_ok = (link_status & FW_PORT_CMD_LSTATUS_F) != 0;
2627 if (!link_ok) {
2628 dev_err(adap->pdev_dev, "Link down for VF %d\n", vf);
2629 return -EINVAL;
2630 }
2631 /* Determine link speed */
2632 if (link_status & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100M))
2633 speed = 100;
2634 else if (link_status & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_1G))
2635 speed = 1000;
2636 else if (link_status & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_10G))
2637 speed = 10000;
2638 else if (link_status & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_25G))
2639 speed = 25000;
2640 else if (link_status & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_40G))
2641 speed = 40000;
2642 else if (link_status & FW_PORT_CMD_LSPEED_V(FW_PORT_CAP_SPEED_100G))
2643 speed = 100000;
2644
2645 if (max_tx_rate > speed) {
2646 dev_err(adap->pdev_dev,
2647 "Max tx rate %d for VF %d can't be > link-speed %u",
2648 max_tx_rate, vf, speed);
2649 return -EINVAL;
2650 }
2651 pktsize = be16_to_cpu(port_rpl.u.info.mtu);
2652 /* subtract ethhdr size and 4 bytes crc since, f/w appends it */
2653 pktsize = pktsize - sizeof(struct ethhdr) - 4;
2654 /* subtract ipv4 hdr size, tcp hdr size to get typical IPv4 MSS size */
2655 pktsize = pktsize - sizeof(struct iphdr) - sizeof(struct tcphdr);
2656 /* configure Traffic Class for rate-limiting */
2657 ret = t4_sched_params(adap, SCHED_CLASS_TYPE_PACKET,
2658 SCHED_CLASS_LEVEL_CL_RL,
2659 SCHED_CLASS_MODE_CLASS,
2660 SCHED_CLASS_RATEUNIT_BITS,
2661 SCHED_CLASS_RATEMODE_ABS,
2662 pi->port_id, class_id, 0,
2663 max_tx_rate * 1000, 0, pktsize);
2664 if (ret) {
2665 dev_err(adap->pdev_dev, "Err %d for Traffic Class config\n",
2666 ret);
2667 return -EINVAL;
2668 }
2669 dev_info(adap->pdev_dev,
2670 "Class %d with MSS %u configured with rate %u\n",
2671 class_id, pktsize, max_tx_rate);
2672
2673 /* bind VF to configured Traffic Class */
2674 fw_pfvf = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) |
2675 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_SCHEDCLASS_ETH));
2676 fw_class = class_id;
2677 ret = t4_set_params(adap, adap->mbox, adap->pf, vf + 1, 1, &fw_pfvf,
2678 &fw_class);
2679 if (ret) {
2680 dev_err(adap->pdev_dev,
2681 "Err %d in binding VF %d to Traffic Class %d\n",
2682 ret, vf, class_id);
2683 return -EINVAL;
2684 }
2685 dev_info(adap->pdev_dev, "PF %d VF %d is bound to Class %d\n",
2686 adap->pf, vf, class_id);
2687 adap->vfinfo[vf].tx_rate = max_tx_rate;
2688 return 0;
2689}
2690
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302691#endif
2692
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002693static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2694{
2695 int ret;
2696 struct sockaddr *addr = p;
2697 struct port_info *pi = netdev_priv(dev);
2698
2699 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002700 return -EADDRNOTAVAIL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002701
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302702 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002703 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002704 if (ret < 0)
2705 return ret;
2706
2707 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2708 pi->xact_addr_filt = ret;
2709 return 0;
2710}
2711
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002712#ifdef CONFIG_NET_POLL_CONTROLLER
2713static void cxgb_netpoll(struct net_device *dev)
2714{
2715 struct port_info *pi = netdev_priv(dev);
2716 struct adapter *adap = pi->adapter;
2717
2718 if (adap->flags & USING_MSIX) {
2719 int i;
2720 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2721
2722 for (i = pi->nqsets; i; i--, rx++)
2723 t4_sge_intr_msix(0, &rx->rspq);
2724 } else
2725 t4_intr_handler(adap)(0, adap);
2726}
2727#endif
2728
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302729static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2730{
2731 struct port_info *pi = netdev_priv(dev);
2732 struct adapter *adap = pi->adapter;
2733 struct sched_class *e;
2734 struct ch_sched_params p;
2735 struct ch_sched_queue qe;
2736 u32 req_rate;
2737 int err = 0;
2738
2739 if (!can_sched(dev))
2740 return -ENOTSUPP;
2741
2742 if (index < 0 || index > pi->nqsets - 1)
2743 return -EINVAL;
2744
2745 if (!(adap->flags & FULL_INIT_DONE)) {
2746 dev_err(adap->pdev_dev,
2747 "Failed to rate limit on queue %d. Link Down?\n",
2748 index);
2749 return -EINVAL;
2750 }
2751
2752 /* Convert from Mbps to Kbps */
2753 req_rate = rate << 10;
2754
2755 /* Max rate is 10 Gbps */
2756 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2757 dev_err(adap->pdev_dev,
2758 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2759 rate, SCHED_MAX_RATE_KBPS);
2760 return -ERANGE;
2761 }
2762
2763 /* First unbind the queue from any existing class */
2764 memset(&qe, 0, sizeof(qe));
2765 qe.queue = index;
2766 qe.class = SCHED_CLS_NONE;
2767
2768 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2769 if (err) {
2770 dev_err(adap->pdev_dev,
2771 "Unbinding Queue %d on port %d fail. Err: %d\n",
2772 index, pi->port_id, err);
2773 return err;
2774 }
2775
2776 /* Queue already unbound */
2777 if (!req_rate)
2778 return 0;
2779
2780 /* Fetch any available unused or matching scheduling class */
2781 memset(&p, 0, sizeof(p));
2782 p.type = SCHED_CLASS_TYPE_PACKET;
2783 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2784 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2785 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2786 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2787 p.u.params.channel = pi->tx_chan;
2788 p.u.params.class = SCHED_CLS_NONE;
2789 p.u.params.minrate = 0;
2790 p.u.params.maxrate = req_rate;
2791 p.u.params.weight = 0;
2792 p.u.params.pktsize = dev->mtu;
2793
2794 e = cxgb4_sched_class_alloc(dev, &p);
2795 if (!e)
2796 return -ENOMEM;
2797
2798 /* Bind the queue to a scheduling class */
2799 memset(&qe, 0, sizeof(qe));
2800 qe.queue = index;
2801 qe.class = e->idx;
2802
2803 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2804 if (err)
2805 dev_err(adap->pdev_dev,
2806 "Queue rate limiting failed. Err: %d\n", err);
2807 return err;
2808}
2809
Baoyou Xie8efebd62016-09-30 15:34:25 +08002810static int cxgb_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
2811 struct tc_to_netdev *tc)
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302812{
2813 struct port_info *pi = netdev2pinfo(dev);
2814 struct adapter *adap = netdev2adap(dev);
2815
2816 if (!(adap->flags & FULL_INIT_DONE)) {
2817 dev_err(adap->pdev_dev,
2818 "Failed to setup tc on port %d. Link Down?\n",
2819 pi->port_id);
2820 return -EINVAL;
2821 }
2822
2823 if (TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS) &&
2824 tc->type == TC_SETUP_CLSU32) {
2825 switch (tc->cls_u32->command) {
2826 case TC_CLSU32_NEW_KNODE:
2827 case TC_CLSU32_REPLACE_KNODE:
2828 return cxgb4_config_knode(dev, proto, tc->cls_u32);
2829 case TC_CLSU32_DELETE_KNODE:
2830 return cxgb4_delete_knode(dev, proto, tc->cls_u32);
2831 default:
2832 return -EOPNOTSUPP;
2833 }
2834 }
2835
2836 return -EOPNOTSUPP;
2837}
2838
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302839static netdev_features_t cxgb_fix_features(struct net_device *dev,
2840 netdev_features_t features)
2841{
2842 /* Disable GRO, if RX_CSUM is disabled */
2843 if (!(features & NETIF_F_RXCSUM))
2844 features &= ~NETIF_F_GRO;
2845
2846 return features;
2847}
2848
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002849static const struct net_device_ops cxgb4_netdev_ops = {
2850 .ndo_open = cxgb_open,
2851 .ndo_stop = cxgb_close,
2852 .ndo_start_xmit = t4_eth_xmit,
Anish Bhatt688848b2014-06-19 21:37:13 -07002853 .ndo_select_queue = cxgb_select_queue,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002854 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002855 .ndo_set_rx_mode = cxgb_set_rxmode,
2856 .ndo_set_mac_address = cxgb_set_mac_addr,
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00002857 .ndo_set_features = cxgb_set_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002858 .ndo_validate_addr = eth_validate_addr,
2859 .ndo_do_ioctl = cxgb_ioctl,
2860 .ndo_change_mtu = cxgb_change_mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002861#ifdef CONFIG_NET_POLL_CONTROLLER
2862 .ndo_poll_controller = cxgb_netpoll,
2863#endif
Varun Prakash84a200b2015-03-24 19:14:46 +05302864#ifdef CONFIG_CHELSIO_T4_FCOE
2865 .ndo_fcoe_enable = cxgb_fcoe_enable,
2866 .ndo_fcoe_disable = cxgb_fcoe_disable,
2867#endif /* CONFIG_CHELSIO_T4_FCOE */
Rahul Lakkireddy10a26042016-08-22 16:29:08 +05302868 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05302869 .ndo_setup_tc = cxgb_setup_tc,
Arjun Vynipadath90592b92017-05-30 13:30:24 +05302870 .ndo_fix_features = cxgb_fix_features,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002871};
2872
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302873#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302874static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2875 .ndo_open = dummy_open,
Hariprasad Shenai858aa652016-08-11 21:06:24 +05302876 .ndo_set_vf_mac = cxgb_set_vf_mac,
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05302877 .ndo_get_vf_config = cxgb_get_vf_config,
Ganesh Goudar8ea4fae2017-06-05 18:34:20 +05302878 .ndo_set_vf_rate = cxgb_set_vf_rate,
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05302879 .ndo_get_phys_port_id = cxgb_get_phys_port_id,
Hariprasad Shenai78294512016-08-11 21:06:23 +05302880};
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05302881#endif
Hariprasad Shenai78294512016-08-11 21:06:23 +05302882
2883static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2884{
2885 struct adapter *adapter = netdev2adap(dev);
2886
2887 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2888 strlcpy(info->version, cxgb4_driver_version,
2889 sizeof(info->version));
2890 strlcpy(info->bus_info, pci_name(adapter->pdev),
2891 sizeof(info->bus_info));
2892}
2893
2894static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
2895 .get_drvinfo = get_drvinfo,
2896};
2897
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002898void t4_fatal_err(struct adapter *adap)
2899{
Hariprasad Shenai3be06792017-01-13 21:55:26 +05302900 int port;
2901
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03002902 if (pci_channel_offline(adap->pdev))
2903 return;
2904
Hariprasad Shenai3be06792017-01-13 21:55:26 +05302905 /* Disable the SGE since ULDs are going to free resources that
2906 * could be exposed to the adapter. RDMA MWs for example...
2907 */
2908 t4_shutdown_adapter(adap);
2909 for_each_port(adap, port) {
2910 struct net_device *dev = adap->port[port];
2911
2912 /* If we get here in very early initialization the network
2913 * devices may not have been set up yet.
2914 */
2915 if (!dev)
2916 continue;
2917
2918 netif_tx_stop_all_queues(dev);
2919 netif_carrier_off(dev);
2920 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002921 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
2922}
2923
2924static void setup_memwin(struct adapter *adap)
2925{
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05302926 u32 nic_win_base = t4_get_util_window(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002927
Hariprasad Shenaib562fc32015-05-20 17:53:45 +05302928 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
Vipul Pandya636f9d32012-09-26 02:39:39 +00002929}
2930
2931static void setup_memwin_rdma(struct adapter *adap)
2932{
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002933 if (adap->vres.ocq.size) {
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05302934 u32 start;
2935 unsigned int sz_kb;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002936
Hariprasad Shenai0abfd152014-06-27 19:23:48 +05302937 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
2938 start &= PCI_BASE_ADDRESS_MEM_MASK;
2939 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002940 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
2941 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302942 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
2943 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002944 t4_write_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302945 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002946 adap->vres.ocq.start);
2947 t4_read_reg(adap,
Hariprasad Shenaif061de422015-01-05 16:30:44 +05302948 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002949 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002950}
2951
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002952static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
2953{
2954 u32 v;
2955 int ret;
2956
2957 /* get device capabilities */
2958 memset(c, 0, sizeof(*c));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302959 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2960 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05302961 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302962 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002963 if (ret < 0)
2964 return ret;
2965
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05302966 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2967 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302968 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002969 if (ret < 0)
2970 return ret;
2971
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302972 ret = t4_config_glbl_rss(adap, adap->pf,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002973 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
Hariprasad Shenaib2e1a3f2014-11-21 12:52:05 +05302974 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
2975 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002976 if (ret < 0)
2977 return ret;
2978
Hariprasad Shenaib2612722015-05-27 22:30:24 +05302979 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05302980 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
2981 FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002982 if (ret < 0)
2983 return ret;
2984
2985 t4_sge_init(adap);
2986
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002987 /* tweak some settings */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302988 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302989 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05302990 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
2991 v = t4_read_reg(adap, TP_PIO_DATA_A);
2992 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002993
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002994 /* first 4 Tx modulation queues point to consecutive Tx channels */
2995 adap->params.tp.tx_modq_map = 0xE4;
Hariprasad Shenai0d804332015-01-05 16:30:47 +05302996 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
2997 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00002998
2999 /* associate each Tx modulation queue with consecutive Tx channels */
3000 v = 0x84218421;
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303001 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303002 &v, 1, TP_TX_SCHED_HDR_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303003 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303004 &v, 1, TP_TX_SCHED_FIFO_A);
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303005 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303006 &v, 1, TP_TX_SCHED_PCMD_A);
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003007
3008#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
3009 if (is_offload(adap)) {
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303010 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
3011 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3012 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3013 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3014 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
3015 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
3016 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3017 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3018 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
3019 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
Vipul Pandyadca4fae2012-12-10 09:30:53 +00003020 }
3021
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003022 /* get basic stuff going */
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303023 return t4_early_init(adap, adap->pf);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003024}
3025
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003026/*
3027 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3028 */
3029#define MAX_ATIDS 8192U
3030
3031/*
3032 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003033 *
3034 * If the firmware we're dealing with has Configuration File support, then
3035 * we use that to perform all configuration
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003036 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003037
3038/*
3039 * Tweak configuration based on module parameters, etc. Most of these have
3040 * defaults assigned to them by Firmware Configuration Files (if we're using
3041 * them) but need to be explicitly set if we're using hard-coded
3042 * initialization. But even in the case of using Firmware Configuration
3043 * Files, we'd like to expose the ability to change these via module
3044 * parameters so these are essentially common tweaks/settings for
3045 * Configuration Files and hard-coded initialization ...
3046 */
3047static int adap_init0_tweaks(struct adapter *adapter)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003048{
Vipul Pandya636f9d32012-09-26 02:39:39 +00003049 /*
3050 * Fix up various Host-Dependent Parameters like Page Size, Cache
3051 * Line Size, etc. The firmware default is for a 4KB Page Size and
3052 * 64B Cache Line Size ...
3053 */
3054 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003055
Vipul Pandya636f9d32012-09-26 02:39:39 +00003056 /*
3057 * Process module parameters which affect early initialization.
3058 */
3059 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
3060 dev_err(&adapter->pdev->dev,
3061 "Ignoring illegal rx_dma_offset=%d, using 2\n",
3062 rx_dma_offset);
3063 rx_dma_offset = 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003064 }
Hariprasad Shenaif612b812015-01-05 16:30:43 +05303065 t4_set_reg_field(adapter, SGE_CONTROL_A,
3066 PKTSHIFT_V(PKTSHIFT_M),
3067 PKTSHIFT_V(rx_dma_offset));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003068
Vipul Pandya636f9d32012-09-26 02:39:39 +00003069 /*
3070 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
3071 * adds the pseudo header itself.
3072 */
Hariprasad Shenai837e4a42015-01-05 16:30:46 +05303073 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
3074 CSUM_HAS_PSEUDO_HDR_F, 0);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003075
3076 return 0;
3077}
3078
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303079/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
3080 * unto themselves and they contain their own firmware to perform their
3081 * tasks ...
3082 */
3083static int phy_aq1202_version(const u8 *phy_fw_data,
3084 size_t phy_fw_size)
3085{
3086 int offset;
3087
3088 /* At offset 0x8 you're looking for the primary image's
3089 * starting offset which is 3 Bytes wide
3090 *
3091 * At offset 0xa of the primary image, you look for the offset
3092 * of the DRAM segment which is 3 Bytes wide.
3093 *
3094 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
3095 * wide
3096 */
3097 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
3098 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
3099 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
3100
3101 offset = le24(phy_fw_data + 0x8) << 12;
3102 offset = le24(phy_fw_data + offset + 0xa);
3103 return be16(phy_fw_data + offset + 0x27e);
3104
3105 #undef be16
3106 #undef le16
3107 #undef le24
3108}
3109
3110static struct info_10gbt_phy_fw {
3111 unsigned int phy_fw_id; /* PCI Device ID */
3112 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
3113 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
3114 int phy_flash; /* Has FLASH for PHY Firmware */
3115} phy_info_array[] = {
3116 {
3117 PHY_AQ1202_DEVICEID,
3118 PHY_AQ1202_FIRMWARE,
3119 phy_aq1202_version,
3120 1,
3121 },
3122 {
3123 PHY_BCM84834_DEVICEID,
3124 PHY_BCM84834_FIRMWARE,
3125 NULL,
3126 0,
3127 },
3128 { 0, NULL, NULL },
3129};
3130
3131static struct info_10gbt_phy_fw *find_phy_info(int devid)
3132{
3133 int i;
3134
3135 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3136 if (phy_info_array[i].phy_fw_id == devid)
3137 return &phy_info_array[i];
3138 }
3139 return NULL;
3140}
3141
3142/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3143 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3144 * we return a negative error number. If we transfer new firmware we return 1
3145 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3146 */
3147static int adap_init0_phy(struct adapter *adap)
3148{
3149 const struct firmware *phyf;
3150 int ret;
3151 struct info_10gbt_phy_fw *phy_info;
3152
3153 /* Use the device ID to determine which PHY file to flash.
3154 */
3155 phy_info = find_phy_info(adap->pdev->device);
3156 if (!phy_info) {
3157 dev_warn(adap->pdev_dev,
3158 "No PHY Firmware file found for this PHY\n");
3159 return -EOPNOTSUPP;
3160 }
3161
3162 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3163 * use that. The adapter firmware provides us with a memory buffer
3164 * where we can load a PHY firmware file from the host if we want to
3165 * override the PHY firmware File in flash.
3166 */
3167 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3168 adap->pdev_dev);
3169 if (ret < 0) {
3170 /* For adapters without FLASH attached to PHY for their
3171 * firmware, it's obviously a fatal error if we can't get the
3172 * firmware to the adapter. For adapters with PHY firmware
3173 * FLASH storage, it's worth a warning if we can't find the
3174 * PHY Firmware but we'll neuter the error ...
3175 */
3176 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3177 "/lib/firmware/%s, error %d\n",
3178 phy_info->phy_fw_file, -ret);
3179 if (phy_info->phy_flash) {
3180 int cur_phy_fw_ver = 0;
3181
3182 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3183 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3184 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3185 ret = 0;
3186 }
3187
3188 return ret;
3189 }
3190
3191 /* Load PHY Firmware onto adapter.
3192 */
3193 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3194 phy_info->phy_fw_version,
3195 (u8 *)phyf->data, phyf->size);
3196 if (ret < 0)
3197 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3198 -ret);
3199 else if (ret > 0) {
3200 int new_phy_fw_ver = 0;
3201
3202 if (phy_info->phy_fw_version)
3203 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3204 phyf->size);
3205 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3206 "Firmware /lib/firmware/%s, version %#x\n",
3207 phy_info->phy_fw_file, new_phy_fw_ver);
3208 }
3209
3210 release_firmware(phyf);
3211
3212 return ret;
3213}
3214
Vipul Pandya636f9d32012-09-26 02:39:39 +00003215/*
3216 * Attempt to initialize the adapter via a Firmware Configuration File.
3217 */
3218static int adap_init0_config(struct adapter *adapter, int reset)
3219{
3220 struct fw_caps_config_cmd caps_cmd;
3221 const struct firmware *cf;
3222 unsigned long mtype = 0, maddr = 0;
3223 u32 finiver, finicsum, cfcsum;
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303224 int ret;
3225 int config_issued = 0;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003226 char *fw_config_file, fw_config_file_path[256];
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303227 char *config_name = NULL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003228
3229 /*
3230 * Reset device if necessary.
3231 */
3232 if (reset) {
3233 ret = t4_fw_reset(adapter, adapter->mbox,
Hariprasad Shenai0d804332015-01-05 16:30:47 +05303234 PIORSTMODE_F | PIORST_F);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003235 if (ret < 0)
3236 goto bye;
3237 }
3238
Hariprasad Shenai01b69612015-05-22 21:58:21 +05303239 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3240 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3241 * to be performed after any global adapter RESET above since some
3242 * PHYs only have local RAM copies of the PHY firmware.
3243 */
3244 if (is_10gbt_device(adapter->pdev->device)) {
3245 ret = adap_init0_phy(adapter);
3246 if (ret < 0)
3247 goto bye;
3248 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003249 /*
3250 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3251 * then use that. Otherwise, use the configuration file stored
3252 * in the adapter flash ...
3253 */
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05303254 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003255 case CHELSIO_T4:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303256 fw_config_file = FW4_CFNAME;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003257 break;
3258 case CHELSIO_T5:
3259 fw_config_file = FW5_CFNAME;
3260 break;
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303261 case CHELSIO_T6:
3262 fw_config_file = FW6_CFNAME;
3263 break;
Santosh Rastapur0a57a532013-03-14 05:08:49 +00003264 default:
3265 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3266 adapter->pdev->device);
3267 ret = -EINVAL;
3268 goto bye;
3269 }
3270
3271 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003272 if (ret < 0) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303273 config_name = "On FLASH";
Vipul Pandya636f9d32012-09-26 02:39:39 +00003274 mtype = FW_MEMTYPE_CF_FLASH;
3275 maddr = t4_flash_cfg_addr(adapter);
3276 } else {
3277 u32 params[7], val[7];
3278
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303279 sprintf(fw_config_file_path,
3280 "/lib/firmware/%s", fw_config_file);
3281 config_name = fw_config_file_path;
3282
Vipul Pandya636f9d32012-09-26 02:39:39 +00003283 if (cf->size >= FLASH_CFG_MAX_SIZE)
3284 ret = -ENOMEM;
3285 else {
Hariprasad Shenai51678652014-11-21 12:52:02 +05303286 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3287 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003288 ret = t4_query_params(adapter, adapter->mbox,
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303289 adapter->pf, 0, 1, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003290 if (ret == 0) {
3291 /*
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303292 * For t4_memory_rw() below addresses and
Vipul Pandya636f9d32012-09-26 02:39:39 +00003293 * sizes have to be in terms of multiples of 4
3294 * bytes. So, if the Configuration File isn't
3295 * a multiple of 4 bytes in length we'll have
3296 * to write that out separately since we can't
3297 * guarantee that the bytes following the
3298 * residual byte in the buffer returned by
3299 * request_firmware() are zeroed out ...
3300 */
3301 size_t resid = cf->size & 0x3;
3302 size_t size = cf->size & ~0x3;
3303 __be32 *data = (__be32 *)cf->data;
3304
Hariprasad Shenai51678652014-11-21 12:52:02 +05303305 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3306 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003307
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303308 spin_lock(&adapter->win0_lock);
3309 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3310 size, data, T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003311 if (ret == 0 && resid != 0) {
3312 union {
3313 __be32 word;
3314 char buf[4];
3315 } last;
3316 int i;
3317
3318 last.word = data[size >> 2];
3319 for (i = resid; i < 4; i++)
3320 last.buf[i] = 0;
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303321 ret = t4_memory_rw(adapter, 0, mtype,
3322 maddr + size,
3323 4, &last.word,
3324 T4_MEMORY_WRITE);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003325 }
Hariprasad Shenaifc5ab022014-06-27 19:23:49 +05303326 spin_unlock(&adapter->win0_lock);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003327 }
3328 }
3329
3330 release_firmware(cf);
3331 if (ret)
3332 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003333 }
3334
Vipul Pandya636f9d32012-09-26 02:39:39 +00003335 /*
3336 * Issue a Capability Configuration command to the firmware to get it
3337 * to parse the Configuration File. We don't use t4_fw_config_file()
3338 * because we want the ability to modify various features after we've
3339 * processed the configuration file ...
3340 */
3341 memset(&caps_cmd, 0, sizeof(caps_cmd));
3342 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303343 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3344 FW_CMD_REQUEST_F |
3345 FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303346 caps_cmd.cfvalid_to_len16 =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303347 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3348 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3349 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
Vipul Pandya636f9d32012-09-26 02:39:39 +00003350 FW_LEN16(caps_cmd));
3351 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3352 &caps_cmd);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303353
3354 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3355 * Configuration File in FLASH), our last gasp effort is to use the
3356 * Firmware Configuration File which is embedded in the firmware. A
3357 * very few early versions of the firmware didn't have one embedded
3358 * but we can ignore those.
3359 */
3360 if (ret == -ENOENT) {
3361 memset(&caps_cmd, 0, sizeof(caps_cmd));
3362 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303363 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3364 FW_CMD_REQUEST_F |
3365 FW_CMD_READ_F);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303366 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3367 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3368 sizeof(caps_cmd), &caps_cmd);
3369 config_name = "Firmware Default";
3370 }
3371
3372 config_issued = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003373 if (ret < 0)
3374 goto bye;
3375
Vipul Pandya636f9d32012-09-26 02:39:39 +00003376 finiver = ntohl(caps_cmd.finiver);
3377 finicsum = ntohl(caps_cmd.finicsum);
3378 cfcsum = ntohl(caps_cmd.cfcsum);
3379 if (finicsum != cfcsum)
3380 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3381 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3382 finicsum, cfcsum);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003383
Vipul Pandya636f9d32012-09-26 02:39:39 +00003384 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003385 * And now tell the firmware to use the configuration we just loaded.
3386 */
3387 caps_cmd.op_to_write =
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303388 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3389 FW_CMD_REQUEST_F |
3390 FW_CMD_WRITE_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303391 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003392 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3393 NULL);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003394 if (ret < 0)
3395 goto bye;
3396
Vipul Pandya636f9d32012-09-26 02:39:39 +00003397 /*
3398 * Tweak configuration based on system architecture, module
3399 * parameters, etc.
3400 */
3401 ret = adap_init0_tweaks(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003402 if (ret < 0)
3403 goto bye;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003404
Vipul Pandya636f9d32012-09-26 02:39:39 +00003405 /*
3406 * And finally tell the firmware to initialize itself using the
3407 * parameters from the Configuration File.
3408 */
3409 ret = t4_fw_initialize(adapter, adapter->mbox);
3410 if (ret < 0)
3411 goto bye;
3412
Hariprasad Shenai06640312015-01-13 15:19:25 +05303413 /* Emit Firmware Configuration File information and return
3414 * successfully.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003415 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003416 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303417 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3418 config_name, finiver, cfcsum);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003419 return 0;
3420
3421 /*
3422 * Something bad happened. Return the error ... (If the "error"
3423 * is that there's no Configuration File on the adapter we don't
3424 * want to issue a warning since this is fairly common.)
3425 */
3426bye:
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303427 if (config_issued && ret != -ENOENT)
3428 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3429 config_name, -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003430 return ret;
3431}
3432
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303433static struct fw_info fw_info_array[] = {
3434 {
3435 .chip = CHELSIO_T4,
3436 .fs_name = FW4_CFNAME,
3437 .fw_mod_name = FW4_FNAME,
3438 .fw_hdr = {
3439 .chip = FW_HDR_CHIP_T4,
3440 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3441 .intfver_nic = FW_INTFVER(T4, NIC),
3442 .intfver_vnic = FW_INTFVER(T4, VNIC),
3443 .intfver_ri = FW_INTFVER(T4, RI),
3444 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3445 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3446 },
3447 }, {
3448 .chip = CHELSIO_T5,
3449 .fs_name = FW5_CFNAME,
3450 .fw_mod_name = FW5_FNAME,
3451 .fw_hdr = {
3452 .chip = FW_HDR_CHIP_T5,
3453 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3454 .intfver_nic = FW_INTFVER(T5, NIC),
3455 .intfver_vnic = FW_INTFVER(T5, VNIC),
3456 .intfver_ri = FW_INTFVER(T5, RI),
3457 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3458 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3459 },
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303460 }, {
3461 .chip = CHELSIO_T6,
3462 .fs_name = FW6_CFNAME,
3463 .fw_mod_name = FW6_FNAME,
3464 .fw_hdr = {
3465 .chip = FW_HDR_CHIP_T6,
3466 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3467 .intfver_nic = FW_INTFVER(T6, NIC),
3468 .intfver_vnic = FW_INTFVER(T6, VNIC),
3469 .intfver_ofld = FW_INTFVER(T6, OFLD),
3470 .intfver_ri = FW_INTFVER(T6, RI),
3471 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3472 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3473 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3474 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3475 },
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303476 }
Hariprasad Shenai3ccc6cf2015-06-02 13:59:39 +05303477
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303478};
3479
3480static struct fw_info *find_fw_info(int chip)
3481{
3482 int i;
3483
3484 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3485 if (fw_info_array[i].chip == chip)
3486 return &fw_info_array[i];
3487 }
3488 return NULL;
3489}
3490
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003491/*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003492 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003493 */
3494static int adap_init0(struct adapter *adap)
3495{
3496 int ret;
3497 u32 v, port_vec;
3498 enum dev_state state;
3499 u32 params[7], val[7];
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003500 struct fw_caps_config_cmd caps_cmd;
Kumar Sanghvidcf7b6f2013-12-18 16:38:23 +05303501 int reset = 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003502
Hariprasad Shenaiae469b62015-04-01 21:41:16 +05303503 /* Grab Firmware Device Log parameters as early as possible so we have
3504 * access to it for debugging, etc.
3505 */
3506 ret = t4_init_devlog_params(adap);
3507 if (ret < 0)
3508 return ret;
3509
Hariprasad Shenai666224d2014-12-11 11:11:43 +05303510 /* Contact FW, advertising Master capability */
Hariprasad Shenaic5a8c0f2016-06-14 14:39:30 +05303511 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3512 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003513 if (ret < 0) {
3514 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3515 ret);
3516 return ret;
3517 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003518 if (ret == adap->mbox)
3519 adap->flags |= MASTER_PF;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003520
Vipul Pandya636f9d32012-09-26 02:39:39 +00003521 /*
3522 * If we're the Master PF Driver and the device is uninitialized,
3523 * then let's consider upgrading the firmware ... (We always want
3524 * to check the firmware version number in order to A. get it for
3525 * later reporting and B. to warn if the currently loaded firmware
3526 * is excessively mismatched relative to the driver.)
3527 */
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303528 t4_get_fw_version(adap, &adap->params.fw_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303529 t4_get_bs_version(adap, &adap->params.bs_vers);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303530 t4_get_tp_version(adap, &adap->params.tp_vers);
Hariprasad Shenai0de72732016-04-26 20:10:22 +05303531 t4_get_exprom_version(adap, &adap->params.er_vers);
3532
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303533 ret = t4_check_fw_version(adap);
3534 /* If firmware is too old (not supported by driver) force an update. */
Hariprasad Shenai21d11bd2015-10-08 10:08:23 +05303535 if (ret)
Hariprasad Shenaia69265e2015-08-28 11:17:12 +05303536 state = DEV_STATE_UNINIT;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003537 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303538 struct fw_info *fw_info;
3539 struct fw_hdr *card_fw;
3540 const struct firmware *fw;
3541 const u8 *fw_data = NULL;
3542 unsigned int fw_size = 0;
3543
3544 /* This is the firmware whose headers the driver was compiled
3545 * against
3546 */
3547 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3548 if (fw_info == NULL) {
3549 dev_err(adap->pdev_dev,
3550 "unable to get firmware info for chip %d.\n",
3551 CHELSIO_CHIP_VERSION(adap->params.chip));
3552 return -EINVAL;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003553 }
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303554
3555 /* allocate memory to read the header of the firmware on the
3556 * card
3557 */
Michal Hocko752ade62017-05-08 15:57:27 -07003558 card_fw = kvzalloc(sizeof(*card_fw), GFP_KERNEL);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303559
3560 /* Get FW from from /lib/firmware/ */
3561 ret = request_firmware(&fw, fw_info->fw_mod_name,
3562 adap->pdev_dev);
3563 if (ret < 0) {
3564 dev_err(adap->pdev_dev,
3565 "unable to load firmware image %s, error %d\n",
3566 fw_info->fw_mod_name, ret);
3567 } else {
3568 fw_data = fw->data;
3569 fw_size = fw->size;
3570 }
3571
3572 /* upgrade FW logic */
3573 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3574 state, &reset);
3575
3576 /* Cleaning up */
Markus Elfring0b5b6be2015-02-04 11:28:43 +01003577 release_firmware(fw);
Michal Hocko752ade62017-05-08 15:57:27 -07003578 kvfree(card_fw);
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303579
Vipul Pandya636f9d32012-09-26 02:39:39 +00003580 if (ret < 0)
Hariprasad Shenai16e47622013-12-03 17:05:58 +05303581 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003582 }
3583
3584 /*
3585 * Grab VPD parameters. This should be done after we establish a
3586 * connection to the firmware since some of the VPD parameters
3587 * (notably the Core Clock frequency) are retrieved via requests to
3588 * the firmware. On the other hand, we need these fairly early on
3589 * so we do this right after getting ahold of the firmware.
3590 */
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05303591 ret = t4_get_vpd_params(adap, &adap->params.vpd);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003592 if (ret < 0)
3593 goto bye;
3594
Vipul Pandya636f9d32012-09-26 02:39:39 +00003595 /*
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003596 * Find out what ports are available to us. Note that we need to do
3597 * this before calling adap_init0_no_config() since it needs nports
3598 * and portvec ...
Vipul Pandya636f9d32012-09-26 02:39:39 +00003599 */
3600 v =
Hariprasad Shenai51678652014-11-21 12:52:02 +05303601 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3602 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303603 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003604 if (ret < 0)
3605 goto bye;
3606
3607 adap->params.nports = hweight32(port_vec);
3608 adap->params.portvec = port_vec;
3609
Hariprasad Shenai06640312015-01-13 15:19:25 +05303610 /* If the firmware is initialized already, emit a simply note to that
3611 * effect. Otherwise, it's time to try initializing the adapter.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003612 */
3613 if (state == DEV_STATE_INIT) {
3614 dev_info(adap->pdev_dev, "Coming up as %s: "\
3615 "Adapter already initialized\n",
3616 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
Vipul Pandya636f9d32012-09-26 02:39:39 +00003617 } else {
3618 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3619 "Initializing adapter\n");
Hariprasad Shenai06640312015-01-13 15:19:25 +05303620
3621 /* Find out whether we're dealing with a version of the
3622 * firmware which has configuration file support.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003623 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303624 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3625 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303626 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
Hariprasad Shenai06640312015-01-13 15:19:25 +05303627 params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003628
Hariprasad Shenai06640312015-01-13 15:19:25 +05303629 /* If the firmware doesn't support Configuration Files,
3630 * return an error.
3631 */
3632 if (ret < 0) {
3633 dev_err(adap->pdev_dev, "firmware doesn't support "
3634 "Firmware Configuration Files\n");
3635 goto bye;
3636 }
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003637
Hariprasad Shenai06640312015-01-13 15:19:25 +05303638 /* The firmware provides us with a memory buffer where we can
3639 * load a Configuration File from the host if we want to
3640 * override the Configuration File in flash.
3641 */
3642 ret = adap_init0_config(adap, reset);
3643 if (ret == -ENOENT) {
3644 dev_err(adap->pdev_dev, "no Configuration File "
3645 "present on adapter.\n");
3646 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003647 }
3648 if (ret < 0) {
Hariprasad Shenai06640312015-01-13 15:19:25 +05303649 dev_err(adap->pdev_dev, "could not initialize "
3650 "adapter, error %d\n", -ret);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003651 goto bye;
3652 }
3653 }
3654
Hariprasad Shenai06640312015-01-13 15:19:25 +05303655 /* Give the SGE code a chance to pull in anything that it needs ...
3656 * Note that this must be called after we retrieve our VPD parameters
3657 * in order to know how to convert core ticks to seconds, etc.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003658 */
Hariprasad Shenai06640312015-01-13 15:19:25 +05303659 ret = t4_sge_init(adap);
3660 if (ret < 0)
3661 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003662
Vipul Pandya9a4da2c2012-10-19 02:09:53 +00003663 if (is_bypass_device(adap->pdev->device))
3664 adap->params.bypass = 1;
3665
Vipul Pandya636f9d32012-09-26 02:39:39 +00003666 /*
3667 * Grab some of our basic fundamental operating parameters.
3668 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003669#define FW_PARAM_DEV(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303670 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3671 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003672
3673#define FW_PARAM_PFVF(param) \
Hariprasad Shenai51678652014-11-21 12:52:02 +05303674 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3675 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3676 FW_PARAMS_PARAM_Y_V(0) | \
3677 FW_PARAMS_PARAM_Z_V(0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003678
Vipul Pandya636f9d32012-09-26 02:39:39 +00003679 params[0] = FW_PARAM_PFVF(EQ_START);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003680 params[1] = FW_PARAM_PFVF(L2T_START);
3681 params[2] = FW_PARAM_PFVF(L2T_END);
3682 params[3] = FW_PARAM_PFVF(FILTER_START);
3683 params[4] = FW_PARAM_PFVF(FILTER_END);
3684 params[5] = FW_PARAM_PFVF(IQFLINT_START);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303685 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003686 if (ret < 0)
3687 goto bye;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003688 adap->sge.egr_start = val[0];
3689 adap->l2t_start = val[1];
3690 adap->l2t_end = val[2];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003691 adap->tids.ftid_base = val[3];
3692 adap->tids.nftids = val[4] - val[3] + 1;
3693 adap->sge.ingr_start = val[5];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003694
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303695 /* qids (ingress/egress) returned from firmware can be anywhere
3696 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3697 * Hence driver needs to allocate memory for this range to
3698 * store the queue info. Get the highest IQFLINT/EQ index returned
3699 * in FW_EQ_*_CMD.alloc command.
3700 */
3701 params[0] = FW_PARAM_PFVF(EQ_END);
3702 params[1] = FW_PARAM_PFVF(IQFLINT_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303703 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303704 if (ret < 0)
3705 goto bye;
3706 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3707 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3708
3709 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3710 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3711 if (!adap->sge.egr_map) {
3712 ret = -ENOMEM;
3713 goto bye;
3714 }
3715
3716 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3717 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3718 if (!adap->sge.ingr_map) {
3719 ret = -ENOMEM;
3720 goto bye;
3721 }
3722
3723 /* Allocate the memory for the vaious egress queue bitmaps
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303724 * ie starving_fl, txq_maperr and blocked_fl.
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303725 */
3726 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3727 sizeof(long), GFP_KERNEL);
3728 if (!adap->sge.starving_fl) {
3729 ret = -ENOMEM;
3730 goto bye;
3731 }
3732
3733 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3734 sizeof(long), GFP_KERNEL);
3735 if (!adap->sge.txq_maperr) {
3736 ret = -ENOMEM;
3737 goto bye;
3738 }
3739
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303740#ifdef CONFIG_DEBUG_FS
3741 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3742 sizeof(long), GFP_KERNEL);
3743 if (!adap->sge.blocked_fl) {
3744 ret = -ENOMEM;
3745 goto bye;
3746 }
3747#endif
3748
Anish Bhattb5a02f52015-01-14 15:17:34 -08003749 params[0] = FW_PARAM_PFVF(CLIP_START);
3750 params[1] = FW_PARAM_PFVF(CLIP_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303751 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Anish Bhattb5a02f52015-01-14 15:17:34 -08003752 if (ret < 0)
3753 goto bye;
3754 adap->clipt_start = val[0];
3755 adap->clipt_end = val[1];
3756
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05303757 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3758 * Classes supported by the hardware/firmware so we hard code it here
3759 * for now.
3760 */
3761 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3762
Vipul Pandya636f9d32012-09-26 02:39:39 +00003763 /* query params related to active filter region */
3764 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3765 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303766 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003767 /* If Active filter size is set we enable establishing
3768 * offload connection through firmware work request
3769 */
3770 if ((val[0] != val[1]) && (ret >= 0)) {
3771 adap->flags |= FW_OFLD_CONN;
3772 adap->tids.aftid_base = val[0];
3773 adap->tids.aftid_end = val[1];
3774 }
3775
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003776 /* If we're running on newer firmware, let it know that we're
3777 * prepared to deal with encapsulated CPL messages. Older
3778 * firmware won't understand this and we'll just get
3779 * unencapsulated messages ...
3780 */
3781 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3782 val[0] = 1;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303783 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
Vipul Pandyab407a4a2013-04-29 04:04:40 +00003784
Vipul Pandya636f9d32012-09-26 02:39:39 +00003785 /*
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303786 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3787 * capability. Earlier versions of the firmware didn't have the
3788 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3789 * permission to use ULPTX MEMWRITE DSGL.
3790 */
3791 if (is_t4(adap->params.chip)) {
3792 adap->params.ulptx_memwrite_dsgl = false;
3793 } else {
3794 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303795 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303796 1, params, val);
3797 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3798 }
3799
Steve Wise086de572016-09-16 07:54:49 -07003800 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3801 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3802 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3803 1, params, val);
3804 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3805
Kumar Sanghvi1ac0f092014-02-18 17:56:12 +05303806 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003807 * Get device capabilities so we can determine what resources we need
3808 * to manage.
3809 */
3810 memset(&caps_cmd, 0, sizeof(caps_cmd));
Hariprasad Shenaie2ac9622014-11-07 09:35:25 +05303811 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3812 FW_CMD_REQUEST_F | FW_CMD_READ_F);
Naresh Kumar Innace91a922012-11-15 22:41:17 +05303813 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
Vipul Pandya636f9d32012-09-26 02:39:39 +00003814 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3815 &caps_cmd);
3816 if (ret < 0)
3817 goto bye;
3818
Vipul Pandya13ee15d2012-09-26 02:39:40 +00003819 if (caps_cmd.ofldcaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003820 /* query offload-related parameters */
3821 params[0] = FW_PARAM_DEV(NTID);
3822 params[1] = FW_PARAM_PFVF(SERVER_START);
3823 params[2] = FW_PARAM_PFVF(SERVER_END);
3824 params[3] = FW_PARAM_PFVF(TDDP_START);
3825 params[4] = FW_PARAM_PFVF(TDDP_END);
3826 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303827 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003828 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003829 if (ret < 0)
3830 goto bye;
3831 adap->tids.ntids = val[0];
3832 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3833 adap->tids.stid_base = val[1];
3834 adap->tids.nstids = val[2] - val[1] + 1;
Vipul Pandya636f9d32012-09-26 02:39:39 +00003835 /*
Joe Perchesdbedd442015-03-06 20:49:12 -08003836 * Setup server filter region. Divide the available filter
Vipul Pandya636f9d32012-09-26 02:39:39 +00003837 * region into two parts. Regular filters get 1/3rd and server
3838 * filters get 2/3rd part. This is only enabled if workarond
3839 * path is enabled.
3840 * 1. For regular filters.
3841 * 2. Server filter: This are special filters which are used
3842 * to redirect SYN packets to offload queue.
3843 */
3844 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3845 adap->tids.sftid_base = adap->tids.ftid_base +
3846 DIV_ROUND_UP(adap->tids.nftids, 3);
3847 adap->tids.nsftids = adap->tids.nftids -
3848 DIV_ROUND_UP(adap->tids.nftids, 3);
3849 adap->tids.nftids = adap->tids.sftid_base -
3850 adap->tids.ftid_base;
3851 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003852 adap->vres.ddp.start = val[3];
3853 adap->vres.ddp.size = val[4] - val[3] + 1;
3854 adap->params.ofldq_wr_cred = val[5];
Vipul Pandya636f9d32012-09-26 02:39:39 +00003855
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003856 adap->params.offload = 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303857 adap->num_ofld_uld += 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003858 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003859 if (caps_cmd.rdmacaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003860 params[0] = FW_PARAM_PFVF(STAG_START);
3861 params[1] = FW_PARAM_PFVF(STAG_END);
3862 params[2] = FW_PARAM_PFVF(RQ_START);
3863 params[3] = FW_PARAM_PFVF(RQ_END);
3864 params[4] = FW_PARAM_PFVF(PBL_START);
3865 params[5] = FW_PARAM_PFVF(PBL_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303866 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003867 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003868 if (ret < 0)
3869 goto bye;
3870 adap->vres.stag.start = val[0];
3871 adap->vres.stag.size = val[1] - val[0] + 1;
3872 adap->vres.rq.start = val[2];
3873 adap->vres.rq.size = val[3] - val[2] + 1;
3874 adap->vres.pbl.start = val[4];
3875 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003876
3877 params[0] = FW_PARAM_PFVF(SQRQ_START);
3878 params[1] = FW_PARAM_PFVF(SQRQ_END);
3879 params[2] = FW_PARAM_PFVF(CQ_START);
3880 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003881 params[4] = FW_PARAM_PFVF(OCQ_START);
3882 params[5] = FW_PARAM_PFVF(OCQ_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303883 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303884 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003885 if (ret < 0)
3886 goto bye;
3887 adap->vres.qp.start = val[0];
3888 adap->vres.qp.size = val[1] - val[0] + 1;
3889 adap->vres.cq.start = val[2];
3890 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003891 adap->vres.ocq.start = val[4];
3892 adap->vres.ocq.size = val[5] - val[4] + 1;
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303893
3894 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3895 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303896 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
Hariprasad Shenai5c937dd2014-09-01 19:55:00 +05303897 val);
Hariprasad Shenai4c2c5762014-07-14 21:34:52 +05303898 if (ret < 0) {
3899 adap->params.max_ordird_qp = 8;
3900 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3901 ret = 0;
3902 } else {
3903 adap->params.max_ordird_qp = val[0];
3904 adap->params.max_ird_adapter = val[1];
3905 }
3906 dev_info(adap->pdev_dev,
3907 "max_ordird_qp %d max_ird_adapter %d\n",
3908 adap->params.max_ordird_qp,
3909 adap->params.max_ird_adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303910 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003911 }
Vipul Pandya636f9d32012-09-26 02:39:39 +00003912 if (caps_cmd.iscsicaps) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003913 params[0] = FW_PARAM_PFVF(ISCSI_START);
3914 params[1] = FW_PARAM_PFVF(ISCSI_END);
Hariprasad Shenaib2612722015-05-27 22:30:24 +05303915 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
Vipul Pandya636f9d32012-09-26 02:39:39 +00003916 params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003917 if (ret < 0)
3918 goto bye;
3919 adap->vres.iscsi.start = val[0];
3920 adap->vres.iscsi.size = val[1] - val[0] + 1;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05303921 /* LIO target and cxgb4i initiaitor */
3922 adap->num_ofld_uld += 2;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003923 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05303924 if (caps_cmd.cryptocaps) {
3925 /* Should query params here...TODO */
Harsh Jain72a56ca2017-04-10 18:24:00 +05303926 params[0] = FW_PARAM_PFVF(NCRYPTO_LOOKASIDE);
3927 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
3928 params, val);
3929 if (ret < 0) {
3930 if (ret != -EINVAL)
3931 goto bye;
3932 } else {
3933 adap->vres.ncrypto_fc = val[0];
3934 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05303935 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
3936 adap->num_uld += 1;
3937 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003938#undef FW_PARAM_PFVF
3939#undef FW_PARAM_DEV
3940
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303941 /* The MTU/MSS Table is initialized by now, so load their values. If
3942 * we're initializing the adapter, then we'll make any modifications
3943 * we want to the MTU/MSS Table and also initialize the congestion
3944 * parameters.
Vipul Pandya636f9d32012-09-26 02:39:39 +00003945 */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003946 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303947 if (state != DEV_STATE_INIT) {
3948 int i;
Casey Leedom7ee9ff92010-06-25 12:11:46 +00003949
Hariprasad Shenai92e7ae72014-06-06 21:40:43 +05303950 /* The default MTU Table contains values 1492 and 1500.
3951 * However, for TCP, it's better to have two values which are
3952 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
3953 * This allows us to have a TCP Data Payload which is a
3954 * multiple of 8 regardless of what combination of TCP Options
3955 * are in use (always a multiple of 4 bytes) which is
3956 * important for performance reasons. For instance, if no
3957 * options are in use, then we have a 20-byte IP header and a
3958 * 20-byte TCP header. In this case, a 1500-byte MSS would
3959 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
3960 * which is not a multiple of 8. So using an MSS of 1488 in
3961 * this case results in a TCP Data Payload of 1448 bytes which
3962 * is a multiple of 8. On the other hand, if 12-byte TCP Time
3963 * Stamps have been negotiated, then an MTU of 1500 bytes
3964 * results in a TCP Data Payload of 1448 bytes which, as
3965 * above, is a multiple of 8 bytes ...
3966 */
3967 for (i = 0; i < NMTUS; i++)
3968 if (adap->params.mtus[i] == 1492) {
3969 adap->params.mtus[i] = 1488;
3970 break;
3971 }
3972
3973 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3974 adap->params.b_wnd);
3975 }
Hariprasad Shenaidf64e4d2014-12-03 19:32:53 +05303976 t4_init_sge_params(adap);
Vipul Pandya636f9d32012-09-26 02:39:39 +00003977 adap->flags |= FW_OK;
Hariprasad Shenaic1e9af02015-06-05 14:24:52 +05303978 t4_init_tp_params(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003979 return 0;
3980
3981 /*
Vipul Pandya636f9d32012-09-26 02:39:39 +00003982 * Something bad happened. If a command timed out or failed with EIO
3983 * FW does not operate within its spec or something catastrophic
3984 * happened to HW/FW, stop issuing commands.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003985 */
Vipul Pandya636f9d32012-09-26 02:39:39 +00003986bye:
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05303987 kfree(adap->sge.egr_map);
3988 kfree(adap->sge.ingr_map);
3989 kfree(adap->sge.starving_fl);
3990 kfree(adap->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05303991#ifdef CONFIG_DEBUG_FS
3992 kfree(adap->sge.blocked_fl);
3993#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00003994 if (ret != -ETIMEDOUT && ret != -EIO)
3995 t4_fw_bye(adap, adap->mbox);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003996 return ret;
3997}
3998
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003999/* EEH callbacks */
4000
4001static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
4002 pci_channel_state_t state)
4003{
4004 int i;
4005 struct adapter *adap = pci_get_drvdata(pdev);
4006
4007 if (!adap)
4008 goto out;
4009
4010 rtnl_lock();
4011 adap->flags &= ~FW_OK;
4012 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004013 spin_lock(&adap->stats_lock);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004014 for_each_port(adap, i) {
4015 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004016 if (dev) {
4017 netif_device_detach(dev);
4018 netif_carrier_off(dev);
4019 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004020 }
Gavin Shan9fe6cb52014-01-23 12:27:35 +08004021 spin_unlock(&adap->stats_lock);
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05304022 disable_interrupts(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004023 if (adap->flags & FULL_INIT_DONE)
4024 cxgb_down(adap);
4025 rtnl_unlock();
Gavin Shan144be3d2014-01-23 12:27:34 +08004026 if ((adap->flags & DEV_ENABLED)) {
4027 pci_disable_device(pdev);
4028 adap->flags &= ~DEV_ENABLED;
4029 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004030out: return state == pci_channel_io_perm_failure ?
4031 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
4032}
4033
4034static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
4035{
4036 int i, ret;
4037 struct fw_caps_config_cmd c;
4038 struct adapter *adap = pci_get_drvdata(pdev);
4039
4040 if (!adap) {
4041 pci_restore_state(pdev);
4042 pci_save_state(pdev);
4043 return PCI_ERS_RESULT_RECOVERED;
4044 }
4045
Gavin Shan144be3d2014-01-23 12:27:34 +08004046 if (!(adap->flags & DEV_ENABLED)) {
4047 if (pci_enable_device(pdev)) {
4048 dev_err(&pdev->dev, "Cannot reenable PCI "
4049 "device after reset\n");
4050 return PCI_ERS_RESULT_DISCONNECT;
4051 }
4052 adap->flags |= DEV_ENABLED;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004053 }
4054
4055 pci_set_master(pdev);
4056 pci_restore_state(pdev);
4057 pci_save_state(pdev);
4058 pci_cleanup_aer_uncorrect_error_status(pdev);
4059
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304060 if (t4_wait_dev_ready(adap->regs) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004061 return PCI_ERS_RESULT_DISCONNECT;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304062 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004063 return PCI_ERS_RESULT_DISCONNECT;
4064 adap->flags |= FW_OK;
4065 if (adap_init1(adap, &c))
4066 return PCI_ERS_RESULT_DISCONNECT;
4067
4068 for_each_port(adap, i) {
4069 struct port_info *p = adap2pinfo(adap, i);
4070
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304071 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004072 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004073 if (ret < 0)
4074 return PCI_ERS_RESULT_DISCONNECT;
4075 p->viid = ret;
4076 p->xact_addr_filt = -1;
4077 }
4078
4079 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
4080 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00004081 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004082 if (cxgb_up(adap))
4083 return PCI_ERS_RESULT_DISCONNECT;
4084 return PCI_ERS_RESULT_RECOVERED;
4085}
4086
4087static void eeh_resume(struct pci_dev *pdev)
4088{
4089 int i;
4090 struct adapter *adap = pci_get_drvdata(pdev);
4091
4092 if (!adap)
4093 return;
4094
4095 rtnl_lock();
4096 for_each_port(adap, i) {
4097 struct net_device *dev = adap->port[i];
Guilherme G. Piccoli025d0972017-05-28 23:07:01 -03004098 if (dev) {
4099 if (netif_running(dev)) {
4100 link_start(dev);
4101 cxgb_set_rxmode(dev);
4102 }
4103 netif_device_attach(dev);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004104 }
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004105 }
4106 rtnl_unlock();
4107}
4108
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07004109static const struct pci_error_handlers cxgb4_eeh = {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00004110 .error_detected = eeh_err_detected,
4111 .slot_reset = eeh_slot_reset,
4112 .resume = eeh_resume,
4113};
4114
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304115/* Return true if the Link Configuration supports "High Speeds" (those greater
4116 * than 1Gb/s).
4117 */
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304118static inline bool is_x_10g_port(const struct link_config *lc)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004119{
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304120 unsigned int speeds, high_speeds;
4121
4122 speeds = FW_PORT_CAP_SPEED_V(FW_PORT_CAP_SPEED_G(lc->supported));
4123 high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
4124
4125 return high_speeds != 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004126}
4127
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004128/*
4129 * Perform default configuration of DMA queues depending on the number and type
4130 * of ports we found and the number of available CPUs. Most settings can be
4131 * modified by the admin prior to actual use.
4132 */
Bill Pemberton91744942012-12-03 09:23:02 -05004133static void cfg_queues(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004134{
4135 struct sge *s = &adap->sge;
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304136 int i = 0, n10g = 0, qidx = 0;
Anish Bhatt688848b2014-06-19 21:37:13 -07004137#ifndef CONFIG_CHELSIO_T4_DCB
4138 int q10g = 0;
4139#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004140
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304141 /* Reduce memory usage in kdump environment, disable all offload.
4142 */
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304143 if (is_kdump_kernel() || (is_uld(adap) && t4_uld_mem_alloc(adap))) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304144 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304145 adap->params.crypto = 0;
4146 }
4147
Hariprasad Shenaiab677ff2016-11-18 16:37:40 +05304148 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
Anish Bhatt688848b2014-06-19 21:37:13 -07004149#ifdef CONFIG_CHELSIO_T4_DCB
4150 /* For Data Center Bridging support we need to be able to support up
4151 * to 8 Traffic Priorities; each of which will be assigned to its
4152 * own TX Queue in order to prevent Head-Of-Line Blocking.
4153 */
4154 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4155 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4156 MAX_ETH_QSETS, adap->params.nports * 8);
4157 BUG_ON(1);
4158 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004159
Anish Bhatt688848b2014-06-19 21:37:13 -07004160 for_each_port(adap, i) {
4161 struct port_info *pi = adap2pinfo(adap, i);
4162
4163 pi->first_qset = qidx;
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304164 pi->nqsets = is_kdump_kernel() ? 1 : 8;
Anish Bhatt688848b2014-06-19 21:37:13 -07004165 qidx += pi->nqsets;
4166 }
4167#else /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004168 /*
4169 * We default to 1 queue per non-10G port and up to # of cores queues
4170 * per 10G port.
4171 */
4172 if (n10g)
4173 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
Yuval Mintz5952dde2012-07-01 03:18:55 +00004174 if (q10g > netif_get_num_default_rss_queues())
4175 q10g = netif_get_num_default_rss_queues();
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004176
Ganesh Goudar85eacf32017-05-16 21:17:42 +05304177 if (is_kdump_kernel())
4178 q10g = 1;
4179
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004180 for_each_port(adap, i) {
4181 struct port_info *pi = adap2pinfo(adap, i);
4182
4183 pi->first_qset = qidx;
Kumar Sanghvi57d8b762014-02-18 17:56:10 +05304184 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004185 qidx += pi->nqsets;
4186 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004187#endif /* !CONFIG_CHELSIO_T4_DCB */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004188
4189 s->ethqsets = qidx;
4190 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4191
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304192 if (is_uld(adap)) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004193 /*
4194 * For offload we use 1 queue/channel if all ports are up to 1G,
4195 * otherwise we divide all available queues amongst the channels
4196 * capped by the number of available cores.
4197 */
4198 if (n10g) {
Ganesh Goudara56177e2016-10-18 14:21:25 +05304199 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304200 s->ofldqsets = roundup(i, adap->params.nports);
4201 } else {
4202 s->ofldqsets = adap->params.nports;
4203 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004204 }
4205
4206 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4207 struct sge_eth_rxq *r = &s->ethrxq[i];
4208
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304209 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004210 r->fl.size = 72;
4211 }
4212
4213 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4214 s->ethtxq[i].q.size = 1024;
4215
4216 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4217 s->ctrlq[i].q.size = 512;
4218
Hariprasad Shenaic887ad02014-06-06 21:40:45 +05304219 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304220 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004221}
4222
4223/*
4224 * Reduce the number of Ethernet queues across all ports to at most n.
4225 * n provides at least one queue per port.
4226 */
Bill Pemberton91744942012-12-03 09:23:02 -05004227static void reduce_ethqs(struct adapter *adap, int n)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004228{
4229 int i;
4230 struct port_info *pi;
4231
4232 while (n < adap->sge.ethqsets)
4233 for_each_port(adap, i) {
4234 pi = adap2pinfo(adap, i);
4235 if (pi->nqsets > 1) {
4236 pi->nqsets--;
4237 adap->sge.ethqsets--;
4238 if (adap->sge.ethqsets <= n)
4239 break;
4240 }
4241 }
4242
4243 n = 0;
4244 for_each_port(adap, i) {
4245 pi = adap2pinfo(adap, i);
4246 pi->first_qset = n;
4247 n += pi->nqsets;
4248 }
4249}
4250
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304251static int get_msix_info(struct adapter *adap)
4252{
4253 struct uld_msix_info *msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304254 unsigned int max_ingq = 0;
4255
4256 if (is_offload(adap))
4257 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4258 if (is_pci_uld(adap))
4259 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4260
4261 if (!max_ingq)
4262 goto out;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304263
4264 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4265 if (!msix_info)
4266 return -ENOMEM;
4267
4268 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4269 sizeof(long), GFP_KERNEL);
4270 if (!adap->msix_bmap_ulds.msix_bmap) {
4271 kfree(msix_info);
4272 return -ENOMEM;
4273 }
4274 spin_lock_init(&adap->msix_bmap_ulds.lock);
4275 adap->msix_info_ulds = msix_info;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304276out:
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304277 return 0;
4278}
4279
4280static void free_msix_info(struct adapter *adap)
4281{
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304282 if (!(adap->num_uld && adap->num_ofld_uld))
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304283 return;
4284
4285 kfree(adap->msix_info_ulds);
4286 kfree(adap->msix_bmap_ulds.msix_bmap);
4287}
4288
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004289/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4290#define EXTRA_VECS 2
4291
Bill Pemberton91744942012-12-03 09:23:02 -05004292static int enable_msix(struct adapter *adap)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004293{
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304294 int ofld_need = 0, uld_need = 0;
4295 int i, j, want, need, allocated;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004296 struct sge *s = &adap->sge;
4297 unsigned int nchan = adap->params.nports;
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304298 struct msix_entry *entries;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304299 int max_ingq = MAX_INGQ;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004300
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304301 if (is_pci_uld(adap))
4302 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4303 if (is_offload(adap))
4304 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304305 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304306 GFP_KERNEL);
4307 if (!entries)
4308 return -ENOMEM;
4309
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304310 /* map for msix */
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304311 if (get_msix_info(adap)) {
4312 adap->params.offload = 0;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304313 adap->params.crypto = 0;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304314 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304315
4316 for (i = 0; i < max_ingq + 1; ++i)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004317 entries[i].entry = i;
4318
4319 want = s->max_ethqsets + EXTRA_VECS;
4320 if (is_offload(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304321 want += adap->num_ofld_uld * s->ofldqsets;
4322 ofld_need = adap->num_ofld_uld * nchan;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004323 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304324 if (is_pci_uld(adap)) {
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304325 want += adap->num_uld * s->ofldqsets;
4326 uld_need = adap->num_uld * nchan;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304327 }
Anish Bhatt688848b2014-06-19 21:37:13 -07004328#ifdef CONFIG_CHELSIO_T4_DCB
4329 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4330 * each port.
4331 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304332 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004333#else
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304334 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
Anish Bhatt688848b2014-06-19 21:37:13 -07004335#endif
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304336 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4337 if (allocated < 0) {
4338 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4339 " not using MSI-X\n");
4340 kfree(entries);
4341 return allocated;
4342 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004343
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304344 /* Distribute available vectors to the various queue groups.
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004345 * Every group gets its minimum requirement and NIC gets top
4346 * priority for leftovers.
4347 */
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304348 i = allocated - EXTRA_VECS - ofld_need - uld_need;
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004349 if (i < s->max_ethqsets) {
4350 s->max_ethqsets = i;
4351 if (i < s->ethqsets)
4352 reduce_ethqs(adap, i);
4353 }
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304354 if (is_uld(adap)) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304355 if (allocated < want)
4356 s->nqs_per_uld = nchan;
4357 else
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304358 s->nqs_per_uld = s->ofldqsets;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304359 }
4360
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304361 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004362 adap->msix_info[i].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304363 if (is_uld(adap)) {
4364 for (j = 0 ; i < allocated; ++i, j++) {
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304365 adap->msix_info_ulds[j].vec = entries[i].vector;
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304366 adap->msix_info_ulds[j].idx = i;
4367 }
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05304368 adap->msix_bmap_ulds.mapsize = j;
4369 }
Hariprasad Shenai43eb4e82015-10-21 14:39:53 +05304370 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05304371 "nic %d per uld %d\n",
4372 allocated, s->max_ethqsets, s->nqs_per_uld);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004373
Hariprasad Shenaif36e58e2015-03-04 18:16:28 +05304374 kfree(entries);
Alexander Gordeevc32ad222014-02-18 11:07:59 +01004375 return 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004376}
4377
4378#undef EXTRA_VECS
4379
Bill Pemberton91744942012-12-03 09:23:02 -05004380static int init_rss(struct adapter *adap)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004381{
Hariprasad Shenaic035e182015-05-06 19:48:37 +05304382 unsigned int i;
4383 int err;
4384
4385 err = t4_init_rss_mode(adap, adap->mbox);
4386 if (err)
4387 return err;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004388
4389 for_each_port(adap, i) {
4390 struct port_info *pi = adap2pinfo(adap, i);
4391
4392 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4393 if (!pi->rss)
4394 return -ENOMEM;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004395 }
4396 return 0;
4397}
4398
Hariprasad Shenai547fd272015-12-23 11:29:53 +05304399static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4400 enum pci_bus_speed *speed,
4401 enum pcie_link_width *width)
4402{
4403 u32 lnkcap1, lnkcap2;
4404 int err1, err2;
4405
4406#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4407
4408 *speed = PCI_SPEED_UNKNOWN;
4409 *width = PCIE_LNK_WIDTH_UNKNOWN;
4410
4411 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4412 &lnkcap1);
4413 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4414 &lnkcap2);
4415 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4416 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4417 *speed = PCIE_SPEED_8_0GT;
4418 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4419 *speed = PCIE_SPEED_5_0GT;
4420 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4421 *speed = PCIE_SPEED_2_5GT;
4422 }
4423 if (!err1) {
4424 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4425 if (!lnkcap2) { /* pre-r3.0 */
4426 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4427 *speed = PCIE_SPEED_5_0GT;
4428 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4429 *speed = PCIE_SPEED_2_5GT;
4430 }
4431 }
4432
4433 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4434 return err1 ? err1 : err2 ? err2 : -EINVAL;
4435 return 0;
4436}
4437
4438static void cxgb4_check_pcie_caps(struct adapter *adap)
4439{
4440 enum pcie_link_width width, width_cap;
4441 enum pci_bus_speed speed, speed_cap;
4442
4443#define PCIE_SPEED_STR(speed) \
4444 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4445 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4446 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4447 "Unknown")
4448
4449 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4450 dev_warn(adap->pdev_dev,
4451 "Unable to determine PCIe device BW capabilities\n");
4452 return;
4453 }
4454
4455 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4456 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4457 dev_warn(adap->pdev_dev,
4458 "Unable to determine PCI Express bandwidth.\n");
4459 return;
4460 }
4461
4462 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4463 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4464 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4465 width, width_cap);
4466 if (speed < speed_cap || width < width_cap)
4467 dev_info(adap->pdev_dev,
4468 "A slot with more lanes and/or higher speed is "
4469 "suggested for optimal performance.\n");
4470}
4471
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304472/* Dump basic information about the adapter */
4473static void print_adapter_info(struct adapter *adapter)
4474{
4475 /* Device information */
4476 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4477 adapter->params.vpd.id,
4478 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4479 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4480 adapter->params.vpd.sn, adapter->params.vpd.pn);
4481
4482 /* Firmware Version */
4483 if (!adapter->params.fw_vers)
4484 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4485 else
4486 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4487 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4488 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4489 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4490 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4491
4492 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4493 * Firmware, so dev_info() is more appropriate here.)
4494 */
4495 if (!adapter->params.bs_vers)
4496 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4497 else
4498 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4499 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4500 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4501 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4502 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4503
4504 /* TP Microcode Version */
4505 if (!adapter->params.tp_vers)
4506 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4507 else
4508 dev_info(adapter->pdev_dev,
4509 "TP Microcode version: %u.%u.%u.%u\n",
4510 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4511 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4512 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4513 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4514
4515 /* Expansion ROM version */
4516 if (!adapter->params.er_vers)
4517 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4518 else
4519 dev_info(adapter->pdev_dev,
4520 "Expansion ROM version: %u.%u.%u.%u\n",
4521 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4522 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4523 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4524 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4525
4526 /* Software/Hardware configuration */
4527 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4528 is_offload(adapter) ? "R" : "",
4529 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4530 (adapter->flags & USING_MSI) ? "MSI" : ""),
4531 is_offload(adapter) ? "Offload" : "non-Offload");
4532}
4533
Bill Pemberton91744942012-12-03 09:23:02 -05004534static void print_port_info(const struct net_device *dev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004535{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004536 char buf[80];
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004537 char *bufp = buf;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004538 const char *spd = "";
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004539 const struct port_info *pi = netdev_priv(dev);
4540 const struct adapter *adap = pi->adapter;
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00004541
4542 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4543 spd = " 2.5 GT/s";
4544 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4545 spd = " 5 GT/s";
Roland Dreierd2e752d2014-04-28 17:36:20 -07004546 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4547 spd = " 8 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004548
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004549 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304550 bufp += sprintf(bufp, "100M/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004551 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
Ganesh Goudar5e78f7f2017-01-06 16:51:46 +05304552 bufp += sprintf(bufp, "1G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004553 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4554 bufp += sprintf(bufp, "10G/");
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304555 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
4556 bufp += sprintf(bufp, "25G/");
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304557 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4558 bufp += sprintf(bufp, "40G/");
Hariprasad Shenai9b86a8d2016-09-20 12:00:52 +05304559 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
4560 bufp += sprintf(bufp, "100G/");
Dimitris Michailidis118969e2010-12-14 21:36:48 +00004561 if (bufp != buf)
4562 --bufp;
Kumar Sanghvi72aca4b2014-02-18 17:56:08 +05304563 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004564
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304565 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4566 dev->name, adap->params.vpd.id, adap->name, buf);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004567}
4568
Bill Pemberton91744942012-12-03 09:23:02 -05004569static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004570{
Jiang Liue5c8ae52012-08-20 13:53:19 -06004571 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004572}
4573
Dimitris Michailidis06546392010-07-11 12:01:16 +00004574/*
4575 * Free the following resources:
4576 * - memory used for tables
4577 * - MSI/MSI-X
4578 * - net devices
4579 * - resources FW is holding for us
4580 */
4581static void free_some_resources(struct adapter *adapter)
4582{
4583 unsigned int i;
4584
Michal Hocko752ade62017-05-08 15:57:27 -07004585 kvfree(adapter->l2t);
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05304586 t4_cleanup_sched(adapter);
Michal Hocko752ade62017-05-08 15:57:27 -07004587 kvfree(adapter->tids.tid_tab);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304588 cxgb4_cleanup_tc_u32(adapter);
Hariprasad Shenai4b8e27a2015-03-26 10:04:25 +05304589 kfree(adapter->sge.egr_map);
4590 kfree(adapter->sge.ingr_map);
4591 kfree(adapter->sge.starving_fl);
4592 kfree(adapter->sge.txq_maperr);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304593#ifdef CONFIG_DEBUG_FS
4594 kfree(adapter->sge.blocked_fl);
4595#endif
Dimitris Michailidis06546392010-07-11 12:01:16 +00004596 disable_msi(adapter);
4597
4598 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004599 if (adapter->port[i]) {
Hariprasad Shenai4f3a0fc2015-06-05 14:24:47 +05304600 struct port_info *pi = adap2pinfo(adapter, i);
4601
4602 if (pi->viid != 0)
4603 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4604 0, pi->viid);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004605 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004606 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00004607 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00004608 if (adapter->flags & FW_OK)
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304609 t4_fw_bye(adapter, adapter->pf);
Dimitris Michailidis06546392010-07-11 12:01:16 +00004610}
4611
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004612#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00004613#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004614 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004615#define SEGMENT_SIZE 128
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004616
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304617static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4618{
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304619 u16 device_id;
4620
4621 /* Retrieve adapter's device ID */
4622 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
françois romieu46cdc9b2015-09-04 23:05:42 +02004623
4624 switch (device_id >> 12) {
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304625 case CHELSIO_T4:
françois romieu46cdc9b2015-09-04 23:05:42 +02004626 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304627 case CHELSIO_T5:
françois romieu46cdc9b2015-09-04 23:05:42 +02004628 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304629 case CHELSIO_T6:
françois romieu46cdc9b2015-09-04 23:05:42 +02004630 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304631 default:
4632 dev_err(&pdev->dev, "Device %d is not supported\n",
4633 device_id);
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304634 }
françois romieu46cdc9b2015-09-04 23:05:42 +02004635 return -EINVAL;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304636}
4637
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304638#ifdef CONFIG_PCI_IOV
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304639static void dummy_setup(struct net_device *dev)
4640{
4641 dev->type = ARPHRD_NONE;
4642 dev->mtu = 0;
4643 dev->hard_header_len = 0;
4644 dev->addr_len = 0;
4645 dev->tx_queue_len = 0;
4646 dev->flags |= IFF_NOARP;
4647 dev->priv_flags |= IFF_NO_QUEUE;
4648
4649 /* Initialize the device structure. */
4650 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4651 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
4652 dev->destructor = free_netdev;
4653}
4654
4655static int config_mgmt_dev(struct pci_dev *pdev)
4656{
4657 struct adapter *adap = pci_get_drvdata(pdev);
4658 struct net_device *netdev;
4659 struct port_info *pi;
4660 char name[IFNAMSIZ];
4661 int err;
4662
4663 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
Ganesh Goudar038c35a2017-01-12 12:23:21 +05304664 netdev = alloc_netdev(sizeof(struct port_info), name, NET_NAME_UNKNOWN,
4665 dummy_setup);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304666 if (!netdev)
4667 return -ENOMEM;
4668
4669 pi = netdev_priv(netdev);
4670 pi->adapter = adap;
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304671 pi->port_id = adap->pf % adap->params.nports;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304672 SET_NETDEV_DEV(netdev, &pdev->dev);
4673
4674 adap->port[0] = netdev;
4675
4676 err = register_netdev(adap->port[0]);
4677 if (err) {
4678 pr_info("Unable to register VF mgmt netdev %s\n", name);
4679 free_netdev(adap->port[0]);
4680 adap->port[0] = NULL;
4681 return err;
4682 }
4683 return 0;
4684}
4685
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304686static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4687{
Hariprasad Shenai78294512016-08-11 21:06:23 +05304688 struct adapter *adap = pci_get_drvdata(pdev);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304689 int err = 0;
4690 int current_vfs = pci_num_vf(pdev);
4691 u32 pcie_fw;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304692
Hariprasad Shenai78294512016-08-11 21:06:23 +05304693 pcie_fw = readl(adap->regs + PCIE_FW_A);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304694 /* Check if cxgb4 is the MASTER and fw is initialized */
4695 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4696 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4697 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4698 dev_warn(&pdev->dev,
4699 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4700 return -EOPNOTSUPP;
4701 }
4702
4703 /* If any of the VF's is already assigned to Guest OS, then
4704 * SRIOV for the same cannot be modified
4705 */
4706 if (current_vfs && pci_vfs_assigned(pdev)) {
4707 dev_err(&pdev->dev,
4708 "Cannot modify SR-IOV while VFs are assigned\n");
4709 num_vfs = current_vfs;
4710 return num_vfs;
4711 }
4712
4713 /* Disable SRIOV when zero is passed.
4714 * One needs to disable SRIOV before modifying it, else
4715 * stack throws the below warning:
4716 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4717 */
4718 if (!num_vfs) {
4719 pci_disable_sriov(pdev);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304720 if (adap->port[0]) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304721 unregister_netdev(adap->port[0]);
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304722 adap->port[0] = NULL;
4723 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304724 /* free VF resources */
4725 kfree(adap->vfinfo);
4726 adap->vfinfo = NULL;
4727 adap->num_vfs = 0;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304728 return num_vfs;
4729 }
4730
4731 if (num_vfs != current_vfs) {
4732 err = pci_enable_sriov(pdev, num_vfs);
4733 if (err)
4734 return err;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304735
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304736 adap->num_vfs = num_vfs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05304737 err = config_mgmt_dev(pdev);
4738 if (err)
4739 return err;
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304740 }
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05304741
4742 adap->vfinfo = kcalloc(adap->num_vfs,
4743 sizeof(struct vf_info), GFP_KERNEL);
4744 if (adap->vfinfo)
4745 fill_vf_station_mac_addr(adap);
Hariprasad Shenaib6244202016-06-14 14:39:31 +05304746 return num_vfs;
4747}
4748#endif
4749
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00004750static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004751{
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004752 int func, i, err, s_qpp, qpp, num_seg;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004753 struct port_info *pi;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004754 bool highdma = false;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004755 struct adapter *adapter = NULL;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304756 struct net_device *netdev;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304757 void __iomem *regs;
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304758 u32 whoami, pl_rev;
4759 enum chip_type chip;
Hariprasad Shenai78294512016-08-11 21:06:23 +05304760 static int adap_idx = 1;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004761#ifdef CONFIG_PCI_IOV
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05304762 u32 v, port_vec;
Arnd Bergmann0a327882017-01-18 15:52:51 +01004763#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004764
4765 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4766
4767 err = pci_request_regions(pdev, KBUILD_MODNAME);
4768 if (err) {
4769 /* Just info, some other driver may have claimed the device. */
4770 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4771 return err;
4772 }
4773
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004774 err = pci_enable_device(pdev);
4775 if (err) {
4776 dev_err(&pdev->dev, "cannot enable PCI device\n");
4777 goto out_release_regions;
4778 }
4779
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304780 regs = pci_ioremap_bar(pdev, 0);
4781 if (!regs) {
4782 dev_err(&pdev->dev, "cannot map device registers\n");
4783 err = -ENOMEM;
4784 goto out_disable_device;
4785 }
4786
Hariprasad Shenai8203b502014-10-09 05:48:47 +05304787 err = t4_wait_dev_ready(regs);
4788 if (err < 0)
4789 goto out_unmap_bar0;
4790
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304791 /* We control everything through one PF */
Hariprasad Shenaid86bd292015-08-04 14:36:19 +05304792 whoami = readl(regs + PL_WHOAMI_A);
4793 pl_rev = REV_G(readl(regs + PL_REV_A));
4794 chip = get_chip_type(pdev, pl_rev);
4795 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4796 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304797 if (func != ent->driver_data) {
Hariprasad Shenai78294512016-08-11 21:06:23 +05304798#ifndef CONFIG_PCI_IOV
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304799 iounmap(regs);
Hariprasad Shenai78294512016-08-11 21:06:23 +05304800#endif
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304801 pci_disable_device(pdev);
4802 pci_save_state(pdev); /* to restore SR-IOV later */
4803 goto sriov;
4804 }
4805
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004806 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004807 highdma = true;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004808 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4809 if (err) {
4810 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4811 "coherent allocations\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304812 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004813 }
4814 } else {
4815 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4816 if (err) {
4817 dev_err(&pdev->dev, "no usable DMA configuration\n");
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304818 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004819 }
4820 }
4821
4822 pci_enable_pcie_error_reporting(pdev);
Dimitris Michailidisef306b52010-12-14 21:36:44 +00004823 enable_pcie_relaxed_ordering(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004824 pci_set_master(pdev);
4825 pci_save_state(pdev);
4826
4827 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4828 if (!adapter) {
4829 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304830 goto out_unmap_bar0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004831 }
Hariprasad Shenai78294512016-08-11 21:06:23 +05304832 adap_idx++;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004833
Anish Bhatt29aaee62014-08-20 13:44:06 -07004834 adapter->workq = create_singlethread_workqueue("cxgb4");
4835 if (!adapter->workq) {
4836 err = -ENOMEM;
4837 goto out_free_adapter;
4838 }
4839
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05304840 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4841 (sizeof(struct mbox_cmd) *
4842 T4_OS_LOG_MBOX_CMDS),
4843 GFP_KERNEL);
4844 if (!adapter->mbox_log) {
4845 err = -ENOMEM;
4846 goto out_free_adapter;
4847 }
4848 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4849
Gavin Shan144be3d2014-01-23 12:27:34 +08004850 /* PCI device has been enabled */
4851 adapter->flags |= DEV_ENABLED;
4852
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304853 adapter->regs = regs;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004854 adapter->pdev = pdev;
4855 adapter->pdev_dev = &pdev->dev;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05304856 adapter->name = pci_name(pdev);
Vipul Pandya3069ee9b2012-05-18 15:29:26 +05304857 adapter->mbox = func;
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304858 adapter->pf = func;
Ganesh Goudarea1e76f2016-12-08 13:16:25 +05304859 adapter->msg_enable = DFLT_MSG_ENABLE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004860 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4861
4862 spin_lock_init(&adapter->stats_lock);
4863 spin_lock_init(&adapter->tid_release_lock);
Anish Bhatte327c222014-10-29 17:54:03 -07004864 spin_lock_init(&adapter->win0_lock);
Hariprasad Shenai4055ae52017-01-06 08:47:20 +05304865 spin_lock_init(&adapter->mbox_lock);
4866
4867 INIT_LIST_HEAD(&adapter->mlist.list);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004868
4869 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
Vipul Pandya881806b2012-05-18 15:29:24 +05304870 INIT_WORK(&adapter->db_full_task, process_db_full);
4871 INIT_WORK(&adapter->db_drop_task, process_db_drop);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004872
4873 err = t4_prep_adapter(adapter);
4874 if (err)
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304875 goto out_free_adapter;
4876
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004877
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05304878 if (!is_t4(adapter->params.chip)) {
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304879 s_qpp = (QUEUESPERPAGEPF0_S +
4880 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
Hariprasad Shenaib2612722015-05-27 22:30:24 +05304881 adapter->pf);
Hariprasad Shenaif612b812015-01-05 16:30:43 +05304882 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4883 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004884 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4885
4886 /* Each segment size is 128B. Write coalescing is enabled only
4887 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4888 * queue is less no of segments that can be accommodated in
4889 * a page size.
4890 */
4891 if (qpp > num_seg) {
4892 dev_err(&pdev->dev,
4893 "Incorrect number of egress queues per page\n");
4894 err = -EINVAL;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304895 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004896 }
4897 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4898 pci_resource_len(pdev, 2));
4899 if (!adapter->bar2) {
4900 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4901 err = -ENOMEM;
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05304902 goto out_free_adapter;
Santosh Rastapur22adfe02013-03-14 05:08:51 +00004903 }
4904 }
4905
Vipul Pandya636f9d32012-09-26 02:39:39 +00004906 setup_memwin(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004907 err = adap_init0(adapter);
Hariprasad Shenai5b377d12015-05-27 22:30:23 +05304908#ifdef CONFIG_DEBUG_FS
4909 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4910#endif
Vipul Pandya636f9d32012-09-26 02:39:39 +00004911 setup_memwin_rdma(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004912 if (err)
4913 goto out_unmap_bar;
4914
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304915 /* configure SGE_STAT_CFG_A to read WC stats */
4916 if (!is_t4(adapter->params.chip))
Hariprasad Shenai676d6a72015-12-23 22:47:14 +05304917 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4918 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4919 T6_STATMODE_V(0)));
Hariprasad Shenai2a485cf2015-09-08 16:25:40 +05304920
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004921 for_each_port(adapter, i) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004922 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4923 MAX_ETH_QSETS);
4924 if (!netdev) {
4925 err = -ENOMEM;
4926 goto out_free_dev;
4927 }
4928
4929 SET_NETDEV_DEV(netdev, &pdev->dev);
4930
4931 adapter->port[i] = netdev;
4932 pi = netdev_priv(netdev);
4933 pi->adapter = adapter;
4934 pi->xact_addr_filt = -1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004935 pi->port_id = i;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004936 netdev->irq = pdev->irq;
4937
Michał Mirosław2ed28ba2011-04-16 13:05:08 +00004938 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4939 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4940 NETIF_F_RXCSUM | NETIF_F_RXHASH |
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05304941 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
4942 NETIF_F_HW_TC;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00004943 if (highdma)
4944 netdev->hw_features |= NETIF_F_HIGHDMA;
4945 netdev->features |= netdev->hw_features;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004946 netdev->vlan_features = netdev->features & VLAN_FEAT;
4947
Jiri Pirko01789342011-08-16 06:29:00 +00004948 netdev->priv_flags |= IFF_UNICAST_FLT;
4949
Jarod Wilsond894be52016-10-20 13:55:16 -04004950 /* MTU range: 81 - 9600 */
4951 netdev->min_mtu = 81;
4952 netdev->max_mtu = MAX_MTU;
4953
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004954 netdev->netdev_ops = &cxgb4_netdev_ops;
Anish Bhatt688848b2014-06-19 21:37:13 -07004955#ifdef CONFIG_CHELSIO_T4_DCB
4956 netdev->dcbnl_ops = &cxgb4_dcb_ops;
4957 cxgb4_dcb_state_init(netdev);
4958#endif
Hariprasad Shenai812034f2015-04-06 20:23:23 +05304959 cxgb4_set_ethtool_ops(netdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004960 }
4961
4962 pci_set_drvdata(pdev, adapter);
4963
4964 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00004965 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004966 if (err)
4967 goto out_free_dev;
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304968 } else if (adapter->params.nports == 1) {
4969 /* If we don't have a connection to the firmware -- possibly
4970 * because of an error -- grab the raw VPD parameters so we
4971 * can set the proper MAC Address on the debug network
4972 * interface that we've created.
4973 */
4974 u8 hw_addr[ETH_ALEN];
4975 u8 *na = adapter->params.vpd.na;
4976
4977 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
4978 if (!err) {
4979 for (i = 0; i < ETH_ALEN; i++)
4980 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
4981 hex2val(na[2 * i + 1]));
4982 t4_set_hw_addr(adapter, 0, hw_addr);
4983 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004984 }
4985
Hariprasad Shenai098ef6c2015-06-05 14:24:50 +05304986 /* Configure queues and allocate tables now, they can be needed as
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004987 * soon as the first register_netdev completes.
4988 */
4989 cfg_queues(adapter);
4990
Hariprasad Shenai5be9ed82015-07-07 21:49:18 +05304991 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00004992 if (!adapter->l2t) {
4993 /* We tolerate a lack of L2T, giving up some functionality */
4994 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
4995 adapter->params.offload = 0;
4996 }
4997
Anish Bhattb5a02f52015-01-14 15:17:34 -08004998#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05304999 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
5000 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
5001 /* CLIP functionality is not present in hardware,
5002 * hence disable all offload features
Anish Bhattb5a02f52015-01-14 15:17:34 -08005003 */
5004 dev_warn(&pdev->dev,
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305005 "CLIP not enabled in hardware, continuing\n");
Anish Bhattb5a02f52015-01-14 15:17:34 -08005006 adapter->params.offload = 0;
Hariprasad Shenaieb72f742015-12-09 17:16:35 +05305007 } else {
5008 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
5009 adapter->clipt_end);
5010 if (!adapter->clipt) {
5011 /* We tolerate a lack of clip_table, giving up
5012 * some functionality
5013 */
5014 dev_warn(&pdev->dev,
5015 "could not allocate Clip table, continuing\n");
5016 adapter->params.offload = 0;
5017 }
Anish Bhattb5a02f52015-01-14 15:17:34 -08005018 }
5019#endif
Rahul Lakkireddyb72a32d2016-08-22 16:29:06 +05305020
5021 for_each_port(adapter, i) {
5022 pi = adap2pinfo(adapter, i);
5023 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
5024 if (!pi->sched_tbl)
5025 dev_warn(&pdev->dev,
5026 "could not activate scheduling on port %d\n",
5027 i);
5028 }
5029
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305030 if (tid_init(&adapter->tids) < 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005031 dev_warn(&pdev->dev, "could not allocate TID table, "
5032 "continuing\n");
5033 adapter->params.offload = 0;
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305034 } else {
Arjun V45da1ca2017-02-16 12:22:45 +05305035 adapter->tc_u32 = cxgb4_init_tc_u32(adapter);
Rahul Lakkireddyd8931842016-09-20 17:13:09 +05305036 if (!adapter->tc_u32)
5037 dev_warn(&pdev->dev,
5038 "could not offload tc u32, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005039 }
5040
Hariprasad Shenai9a1bb9f2015-08-12 16:55:05 +05305041 if (is_offload(adapter)) {
5042 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
5043 u32 hash_base, hash_reg;
5044
5045 if (chip <= CHELSIO_T5) {
5046 hash_reg = LE_DB_TID_HASHBASE_A;
5047 hash_base = t4_read_reg(adapter, hash_reg);
5048 adapter->tids.hash_base = hash_base / 4;
5049 } else {
5050 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
5051 hash_base = t4_read_reg(adapter, hash_reg);
5052 adapter->tids.hash_base = hash_base;
5053 }
5054 }
5055 }
5056
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005057 /* See what interrupts we'll be using */
5058 if (msi > 1 && enable_msix(adapter) == 0)
5059 adapter->flags |= USING_MSIX;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305060 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005061 adapter->flags |= USING_MSI;
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305062 if (msi > 1)
5063 free_msix_info(adapter);
5064 }
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00005065
Hariprasad Shenai547fd272015-12-23 11:29:53 +05305066 /* check for PCI Express bandwidth capabiltites */
5067 cxgb4_check_pcie_caps(adapter);
5068
Dimitris Michailidis671b0062010-07-11 12:01:17 +00005069 err = init_rss(adapter);
5070 if (err)
5071 goto out_free_dev;
5072
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005073 /*
5074 * The card is now ready to go. If any errors occur during device
5075 * registration we do not fail the whole card but rather proceed only
5076 * with the ports we manage to register successfully. However we must
5077 * register at least one net device.
5078 */
5079 for_each_port(adapter, i) {
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005080 pi = adap2pinfo(adapter, i);
Arjun Vd2a007ab2016-12-08 18:09:23 +05305081 adapter->port[i]->dev_port = pi->lport;
Dimitris Michailidisa57cabe2010-12-14 21:36:46 +00005082 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
5083 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
5084
Surendra Mobiyab1a73af2017-05-30 11:32:06 +05305085 netif_carrier_off(adapter->port[i]);
5086
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005087 err = register_netdev(adapter->port[i]);
5088 if (err)
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005089 break;
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005090 adapter->chan_map[pi->tx_chan] = i;
5091 print_port_info(adapter->port[i]);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005092 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005093 if (i == 0) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005094 dev_err(&pdev->dev, "could not register any net devices\n");
5095 goto out_free_dev;
5096 }
Dimitris Michailidisb1a3c2b2010-12-14 21:36:51 +00005097 if (err) {
5098 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
5099 err = 0;
Joe Perches6403eab2011-06-03 11:51:20 +00005100 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005101
5102 if (cxgb4_debugfs_root) {
5103 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
5104 cxgb4_debugfs_root);
5105 setup_debugfs(adapter);
5106 }
5107
David S. Miller88c51002011-10-07 13:38:43 -04005108 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
5109 pdev->needs_freset = 1;
5110
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305111 if (is_uld(adapter)) {
5112 mutex_lock(&uld_mutex);
5113 list_add_tail(&adapter->list_node, &adapter_list);
5114 mutex_unlock(&uld_mutex);
5115 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005116
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305117 print_adapter_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305118 setup_fw_sge_queues(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305119 return 0;
Hariprasad Shenai0de72732016-04-26 20:10:22 +05305120
Hariprasad Shenai8e1e6052014-08-06 17:10:59 +05305121sriov:
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005122#ifdef CONFIG_PCI_IOV
Hariprasad Shenai78294512016-08-11 21:06:23 +05305123 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
5124 if (!adapter) {
5125 err = -ENOMEM;
5126 goto free_pci_region;
5127 }
5128
Hariprasad Shenai78294512016-08-11 21:06:23 +05305129 adapter->pdev = pdev;
5130 adapter->pdev_dev = &pdev->dev;
5131 adapter->name = pci_name(pdev);
5132 adapter->mbox = func;
5133 adapter->pf = func;
5134 adapter->regs = regs;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305135 adapter->adap_idx = adap_idx;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305136 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5137 (sizeof(struct mbox_cmd) *
5138 T4_OS_LOG_MBOX_CMDS),
5139 GFP_KERNEL);
5140 if (!adapter->mbox_log) {
5141 err = -ENOMEM;
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305142 goto free_adapter;
Hariprasad Shenai78294512016-08-11 21:06:23 +05305143 }
Ganesh Goudar038c35a2017-01-12 12:23:21 +05305144 spin_lock_init(&adapter->mbox_lock);
5145 INIT_LIST_HEAD(&adapter->mlist.list);
Ganesh Goudar96fe11f2017-01-17 14:09:38 +05305146
5147 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
5148 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
5149 err = t4_query_params(adapter, adapter->mbox, adapter->pf, 0, 1,
5150 &v, &port_vec);
5151 if (err < 0) {
5152 dev_err(adapter->pdev_dev, "Could not fetch port params\n");
5153 goto free_adapter;
5154 }
5155
5156 adapter->params.nports = hweight32(port_vec);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305157 pci_set_drvdata(pdev, adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005158 return 0;
5159
Hariprasad Shenai78294512016-08-11 21:06:23 +05305160 free_adapter:
5161 kfree(adapter);
5162 free_pci_region:
5163 iounmap(regs);
5164 pci_disable_sriov(pdev);
5165 pci_release_regions(pdev);
5166 return err;
5167#else
5168 return 0;
5169#endif
5170
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005171 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00005172 free_some_resources(adapter);
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305173 if (adapter->flags & USING_MSIX)
5174 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305175 if (adapter->num_uld || adapter->num_ofld_uld)
5176 t4_uld_mem_free(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005177 out_unmap_bar:
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305178 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005179 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005180 out_free_adapter:
Anish Bhatt29aaee62014-08-20 13:44:06 -07005181 if (adapter->workq)
5182 destroy_workqueue(adapter->workq);
5183
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305184 kfree(adapter->mbox_log);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005185 kfree(adapter);
Hariprasad Shenaid6ce2622014-09-16 02:58:46 +05305186 out_unmap_bar0:
5187 iounmap(regs);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005188 out_disable_device:
5189 pci_disable_pcie_error_reporting(pdev);
5190 pci_disable_device(pdev);
5191 out_release_regions:
5192 pci_release_regions(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005193 return err;
5194}
5195
Bill Pemberton91744942012-12-03 09:23:02 -05005196static void remove_one(struct pci_dev *pdev)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005197{
5198 struct adapter *adapter = pci_get_drvdata(pdev);
5199
Hariprasad Shenai78294512016-08-11 21:06:23 +05305200 if (!adapter) {
5201 pci_release_regions(pdev);
5202 return;
5203 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005204
Hariprasad Shenai78294512016-08-11 21:06:23 +05305205 if (adapter->pf == 4) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005206 int i;
5207
Anish Bhatt29aaee62014-08-20 13:44:06 -07005208 /* Tear down per-adapter Work Queue first since it can contain
5209 * references to our adapter data structure.
5210 */
5211 destroy_workqueue(adapter->workq);
5212
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305213 if (is_uld(adapter))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005214 detach_ulds(adapter);
5215
Hariprasad Shenaib37987e2015-03-26 10:04:26 +05305216 disable_interrupts(adapter);
5217
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005218 for_each_port(adapter, i)
Dimitris Michailidis8f3a7672010-12-14 21:36:52 +00005219 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005220 unregister_netdev(adapter->port[i]);
5221
Fabian Frederick9f16dc22014-06-27 22:51:52 +02005222 debugfs_remove_recursive(adapter->debugfs_root);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005223
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005224 /* If we allocated filters, free up state associated with any
5225 * valid filters ...
5226 */
Rahul Lakkireddy578b46b2016-09-20 17:13:07 +05305227 clear_all_filters(adapter);
Vipul Pandyaf2b7e782012-12-10 09:30:52 +00005228
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00005229 if (adapter->flags & FULL_INIT_DONE)
5230 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005231
Hariprasad Shenai94cdb8b2016-08-17 12:33:03 +05305232 if (adapter->flags & USING_MSIX)
5233 free_msix_info(adapter);
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305234 if (adapter->num_uld || adapter->num_ofld_uld)
5235 t4_uld_mem_free(adapter);
Dimitris Michailidis06546392010-07-11 12:01:16 +00005236 free_some_resources(adapter);
Anish Bhattb5a02f52015-01-14 15:17:34 -08005237#if IS_ENABLED(CONFIG_IPV6)
5238 t4_cleanup_clip_tbl(adapter);
5239#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005240 iounmap(adapter->regs);
Hariprasad Shenaid14807d2013-12-03 17:05:56 +05305241 if (!is_t4(adapter->params.chip))
Santosh Rastapur22adfe02013-03-14 05:08:51 +00005242 iounmap(adapter->bar2);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005243 pci_disable_pcie_error_reporting(pdev);
Gavin Shan144be3d2014-01-23 12:27:34 +08005244 if ((adapter->flags & DEV_ENABLED)) {
5245 pci_disable_device(pdev);
5246 adapter->flags &= ~DEV_ENABLED;
5247 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005248 pci_release_regions(pdev);
Hariprasad Shenai7f080c32016-04-28 13:23:18 +05305249 kfree(adapter->mbox_log);
Li RongQingee9a33b2014-06-20 17:32:36 +08005250 synchronize_rcu();
Gavin Shan8b662fe2014-01-24 17:12:03 +08005251 kfree(adapter);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305252 }
5253#ifdef CONFIG_PCI_IOV
5254 else {
Hariprasad Shenaie7b48a32016-08-23 11:35:32 +05305255 if (adapter->port[0])
Hariprasad Shenai78294512016-08-11 21:06:23 +05305256 unregister_netdev(adapter->port[0]);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305257 iounmap(adapter->regs);
Hariprasad Shenai661dbeb2016-09-02 19:13:53 +05305258 kfree(adapter->vfinfo);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305259 kfree(adapter);
5260 pci_disable_sriov(pdev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005261 pci_release_regions(pdev);
Hariprasad Shenai78294512016-08-11 21:06:23 +05305262 }
5263#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005264}
5265
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305266/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5267 * delivery. This is essentially a stripped down version of the PCI remove()
5268 * function where we do the minimal amount of work necessary to shutdown any
5269 * further activity.
5270 */
5271static void shutdown_one(struct pci_dev *pdev)
5272{
5273 struct adapter *adapter = pci_get_drvdata(pdev);
5274
5275 /* As with remove_one() above (see extended comment), we only want do
5276 * do cleanup on PCI Devices which went all the way through init_one()
5277 * ...
5278 */
5279 if (!adapter) {
5280 pci_release_regions(pdev);
5281 return;
5282 }
5283
5284 if (adapter->pf == 4) {
5285 int i;
5286
5287 for_each_port(adapter, i)
5288 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5289 cxgb_close(adapter->port[i]);
5290
5291 t4_uld_clean_up(adapter);
5292 disable_interrupts(adapter);
5293 disable_msi(adapter);
5294
5295 t4_sge_stop(adapter);
5296 if (adapter->flags & FW_OK)
5297 t4_fw_bye(adapter, adapter->mbox);
5298 }
5299#ifdef CONFIG_PCI_IOV
5300 else {
5301 if (adapter->port[0])
5302 unregister_netdev(adapter->port[0]);
5303 iounmap(adapter->regs);
5304 kfree(adapter->vfinfo);
5305 kfree(adapter);
5306 pci_disable_sriov(pdev);
5307 pci_release_regions(pdev);
5308 }
5309#endif
5310}
5311
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005312static struct pci_driver cxgb4_driver = {
5313 .name = KBUILD_MODNAME,
5314 .id_table = cxgb4_pci_tbl,
5315 .probe = init_one,
Bill Pemberton91744942012-12-03 09:23:02 -05005316 .remove = remove_one,
Hariprasad Shenai0fbc81b2016-09-17 08:12:39 +05305317 .shutdown = shutdown_one,
Hariprasad Shenaib6244202016-06-14 14:39:31 +05305318#ifdef CONFIG_PCI_IOV
5319 .sriov_configure = cxgb4_iov_configure,
5320#endif
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00005321 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005322};
5323
5324static int __init cxgb4_init_module(void)
5325{
5326 int ret;
5327
5328 /* Debugfs support is optional, just warn if this fails */
5329 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5330 if (!cxgb4_debugfs_root)
Joe Perches428ac432013-01-06 13:34:49 +00005331 pr_warn("could not create debugfs entry, continuing\n");
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005332
5333 ret = pci_register_driver(&cxgb4_driver);
Anish Bhatt29aaee62014-08-20 13:44:06 -07005334 if (ret < 0)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005335 debugfs_remove(cxgb4_debugfs_root);
Vipul Pandya01bcca62013-07-04 16:10:46 +05305336
Anish Bhatt1bb60372014-10-14 20:07:22 -07005337#if IS_ENABLED(CONFIG_IPV6)
Anish Bhattb5a02f52015-01-14 15:17:34 -08005338 if (!inet6addr_registered) {
5339 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5340 inet6addr_registered = true;
5341 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005342#endif
Vipul Pandya01bcca62013-07-04 16:10:46 +05305343
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005344 return ret;
5345}
5346
5347static void __exit cxgb4_cleanup_module(void)
5348{
Anish Bhatt1bb60372014-10-14 20:07:22 -07005349#if IS_ENABLED(CONFIG_IPV6)
Hariprasad Shenai1793c792015-01-21 20:57:52 +05305350 if (inet6addr_registered) {
Anish Bhattb5a02f52015-01-14 15:17:34 -08005351 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5352 inet6addr_registered = false;
5353 }
Anish Bhatt1bb60372014-10-14 20:07:22 -07005354#endif
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005355 pci_unregister_driver(&cxgb4_driver);
5356 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00005357}
5358
5359module_init(cxgb4_init_module);
5360module_exit(cxgb4_cleanup_module);