]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/ethernet/chelsio/cxgb4/cxgb4_main.c
liquidio CN23XX: bitwise vs logical AND typo
[thirdparty/linux.git] / drivers / net / ethernet / chelsio / cxgb4 / cxgb4_main.c
CommitLineData
b8ff05a9
DM
1/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
b72a32da 4 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
b8ff05a9
DM
5 *
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>
01789349 44#include <linux/if.h>
b8ff05a9
DM
45#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>
01bcca68 63#include <net/addrconf.h>
1ef8019b 64#include <net/bonding.h>
b5a02f50 65#include <net/addrconf.h>
b8ff05a9 66#include <asm/uaccess.h>
c5a8c0f3 67#include <linux/crash_dump.h>
b8ff05a9
DM
68
69#include "cxgb4.h"
d57fd6ca 70#include "cxgb4_filter.h"
b8ff05a9 71#include "t4_regs.h"
f612b815 72#include "t4_values.h"
b8ff05a9
DM
73#include "t4_msg.h"
74#include "t4fw_api.h"
cd6c2f12 75#include "t4fw_version.h"
688848b1 76#include "cxgb4_dcb.h"
fd88b31a 77#include "cxgb4_debugfs.h"
b5a02f50 78#include "clip_tbl.h"
b8ff05a9 79#include "l2t.h"
b72a32da 80#include "sched.h"
d8931847 81#include "cxgb4_tc_u32.h"
b8ff05a9 82
812034f1
HS
83char cxgb4_driver_name[] = KBUILD_MODNAME;
84
01bcca68
VP
85#ifdef DRV_VERSION
86#undef DRV_VERSION
87#endif
3a7f8554 88#define DRV_VERSION "2.0.0-ko"
812034f1 89const char cxgb4_driver_version[] = DRV_VERSION;
52a5f846 90#define DRV_DESC "Chelsio T4/T5/T6 Network Driver"
b8ff05a9 91
b8ff05a9
DM
92#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
3fedeab1
HS
96/* Macros needed to support the PCI Device ID Table ...
97 */
98#define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
768ffc66 99 static const struct pci_device_id cxgb4_pci_tbl[] = {
3fedeab1 100#define CH_PCI_DEVICE_ID_FUNCTION 0x4
b8ff05a9 101
3fedeab1
HS
102/* 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"
b8ff05a9 115
16e47624 116#define FW4_FNAME "cxgb4/t4fw.bin"
0a57a536 117#define FW5_FNAME "cxgb4/t5fw.bin"
3ccc6cf7 118#define FW6_FNAME "cxgb4/t6fw.bin"
16e47624 119#define FW4_CFNAME "cxgb4/t4-config.txt"
0a57a536 120#define FW5_CFNAME "cxgb4/t5-config.txt"
3ccc6cf7 121#define FW6_CFNAME "cxgb4/t6-config.txt"
01b69614
HS
122#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
b8ff05a9
DM
126
127MODULE_DESCRIPTION(DRV_DESC);
128MODULE_AUTHOR("Chelsio Communications");
129MODULE_LICENSE("Dual BSD/GPL");
130MODULE_VERSION(DRV_VERSION);
131MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
16e47624 132MODULE_FIRMWARE(FW4_FNAME);
0a57a536 133MODULE_FIRMWARE(FW5_FNAME);
52a5f846 134MODULE_FIRMWARE(FW6_FNAME);
b8ff05a9 135
636f9d37
VP
136/*
137 * Normally we're willing to become the firmware's Master PF but will be happy
138 * if another PF has already become the Master and initialized the adapter.
139 * Setting "force_init" will cause this driver to forcibly establish itself as
140 * the Master PF and initialize the adapter.
141 */
142static uint force_init;
143
144module_param(force_init, uint, 0644);
d7d3e25f
HS
145MODULE_PARM_DESC(force_init, "Forcibly become Master PF and initialize adapter,"
146 "deprecated parameter");
13ee15d3 147
b8ff05a9
DM
148static int dflt_msg_enable = DFLT_MSG_ENABLE;
149
150module_param(dflt_msg_enable, int, 0644);
8a21ec4e
HS
151MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap, "
152 "deprecated parameter");
b8ff05a9
DM
153
154/*
155 * The driver uses the best interrupt scheme available on a platform in the
156 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
157 * of these schemes the driver may consider as follows:
158 *
159 * msi = 2: choose from among all three options
160 * msi = 1: only consider MSI and INTx interrupts
161 * msi = 0: force INTx interrupts
162 */
163static int msi = 2;
164
165module_param(msi, int, 0644);
166MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
167
636f9d37
VP
168/*
169 * Normally we tell the chip to deliver Ingress Packets into our DMA buffers
170 * offset by 2 bytes in order to have the IP headers line up on 4-byte
171 * boundaries. This is a requirement for many architectures which will throw
172 * a machine check fault if an attempt is made to access one of the 4-byte IP
173 * header fields on a non-4-byte boundary. And it's a major performance issue
174 * even on some architectures which allow it like some implementations of the
175 * x86 ISA. However, some architectures don't mind this and for some very
176 * edge-case performance sensitive applications (like forwarding large volumes
177 * of small packets), setting this DMA offset to 0 will decrease the number of
178 * PCI-E Bus transfers enough to measurably affect performance.
179 */
180static int rx_dma_offset = 2;
181
b8ff05a9 182#ifdef CONFIG_PCI_IOV
7d6727cf
SR
183/* Configure the number of PCI-E Virtual Function which are to be instantiated
184 * on SR-IOV Capable Physical Functions.
0a57a536 185 */
7d6727cf 186static unsigned int num_vf[NUM_OF_PF_WITH_SRIOV];
b8ff05a9
DM
187
188module_param_array(num_vf, uint, NULL, 0644);
b6244201 189MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3, deprecated parameter - please use the pci sysfs interface.");
b8ff05a9
DM
190#endif
191
688848b1
AB
192/* TX Queue select used to determine what algorithm to use for selecting TX
193 * queue. Select between the kernel provided function (select_queue=0) or user
194 * cxgb_select_queue function (select_queue=1)
195 *
196 * Default: select_queue=0
197 */
198static int select_queue;
199module_param(select_queue, int, 0644);
200MODULE_PARM_DESC(select_queue,
201 "Select between kernel provided method of selecting or driver method of selecting TX queue. Default is kernel method.");
202
b8ff05a9
DM
203static struct dentry *cxgb4_debugfs_root;
204
94cdb8bb
HS
205LIST_HEAD(adapter_list);
206DEFINE_MUTEX(uld_mutex);
b8ff05a9
DM
207
208static void link_report(struct net_device *dev)
209{
210 if (!netif_carrier_ok(dev))
211 netdev_info(dev, "link down\n");
212 else {
213 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
214
85412255 215 const char *s;
b8ff05a9
DM
216 const struct port_info *p = netdev_priv(dev);
217
218 switch (p->link_cfg.speed) {
e8b39015 219 case 10000:
b8ff05a9
DM
220 s = "10Gbps";
221 break;
e8b39015 222 case 1000:
b8ff05a9
DM
223 s = "1000Mbps";
224 break;
e8b39015 225 case 100:
b8ff05a9
DM
226 s = "100Mbps";
227 break;
e8b39015 228 case 40000:
72aca4bf
KS
229 s = "40Gbps";
230 break;
85412255
HS
231 default:
232 pr_info("%s: unsupported speed: %d\n",
233 dev->name, p->link_cfg.speed);
234 return;
b8ff05a9
DM
235 }
236
237 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
238 fc[p->link_cfg.fc]);
239 }
240}
241
688848b1
AB
242#ifdef CONFIG_CHELSIO_T4_DCB
243/* Set up/tear down Data Center Bridging Priority mapping for a net device. */
244static void dcb_tx_queue_prio_enable(struct net_device *dev, int enable)
245{
246 struct port_info *pi = netdev_priv(dev);
247 struct adapter *adap = pi->adapter;
248 struct sge_eth_txq *txq = &adap->sge.ethtxq[pi->first_qset];
249 int i;
250
251 /* We use a simple mapping of Port TX Queue Index to DCB
252 * Priority when we're enabling DCB.
253 */
254 for (i = 0; i < pi->nqsets; i++, txq++) {
255 u32 name, value;
256 int err;
257
5167865a
HS
258 name = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
259 FW_PARAMS_PARAM_X_V(
260 FW_PARAMS_PARAM_DMAQ_EQ_DCBPRIO_ETH) |
261 FW_PARAMS_PARAM_YZ_V(txq->q.cntxt_id));
688848b1
AB
262 value = enable ? i : 0xffffffff;
263
264 /* Since we can be called while atomic (from "interrupt
265 * level") we need to issue the Set Parameters Commannd
266 * without sleeping (timeout < 0).
267 */
b2612722 268 err = t4_set_params_timeout(adap, adap->mbox, adap->pf, 0, 1,
01b69614
HS
269 &name, &value,
270 -FW_CMD_MAX_TIMEOUT);
688848b1
AB
271
272 if (err)
273 dev_err(adap->pdev_dev,
274 "Can't %s DCB Priority on port %d, TX Queue %d: err=%d\n",
275 enable ? "set" : "unset", pi->port_id, i, -err);
10b00466
AB
276 else
277 txq->dcb_prio = value;
688848b1
AB
278 }
279}
688848b1 280
50935857 281static int cxgb4_dcb_enabled(const struct net_device *dev)
218d48e7 282{
218d48e7
HS
283 struct port_info *pi = netdev_priv(dev);
284
285 if (!pi->dcb.enabled)
286 return 0;
287
288 return ((pi->dcb.state == CXGB4_DCB_STATE_FW_ALLSYNCED) ||
289 (pi->dcb.state == CXGB4_DCB_STATE_HOST));
218d48e7 290}
7c70c4f8 291#endif /* CONFIG_CHELSIO_T4_DCB */
218d48e7 292
b8ff05a9
DM
293void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
294{
295 struct net_device *dev = adapter->port[port_id];
296
297 /* Skip changes from disabled ports. */
298 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
299 if (link_stat)
300 netif_carrier_on(dev);
688848b1
AB
301 else {
302#ifdef CONFIG_CHELSIO_T4_DCB
218d48e7
HS
303 if (cxgb4_dcb_enabled(dev)) {
304 cxgb4_dcb_state_init(dev);
305 dcb_tx_queue_prio_enable(dev, false);
306 }
688848b1 307#endif /* CONFIG_CHELSIO_T4_DCB */
b8ff05a9 308 netif_carrier_off(dev);
688848b1 309 }
b8ff05a9
DM
310
311 link_report(dev);
312 }
313}
314
315void t4_os_portmod_changed(const struct adapter *adap, int port_id)
316{
317 static const char *mod_str[] = {
a0881cab 318 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
b8ff05a9
DM
319 };
320
321 const struct net_device *dev = adap->port[port_id];
322 const struct port_info *pi = netdev_priv(dev);
323
324 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
325 netdev_info(dev, "port module unplugged\n");
a0881cab 326 else if (pi->mod_type < ARRAY_SIZE(mod_str))
b8ff05a9 327 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
be81a2de
HS
328 else if (pi->mod_type == FW_PORT_MOD_TYPE_NOTSUPPORTED)
329 netdev_info(dev, "%s: unsupported port module inserted\n",
330 dev->name);
331 else if (pi->mod_type == FW_PORT_MOD_TYPE_UNKNOWN)
332 netdev_info(dev, "%s: unknown port module inserted\n",
333 dev->name);
334 else if (pi->mod_type == FW_PORT_MOD_TYPE_ERROR)
335 netdev_info(dev, "%s: transceiver module error\n", dev->name);
336 else
337 netdev_info(dev, "%s: unknown module type %d inserted\n",
338 dev->name, pi->mod_type);
b8ff05a9
DM
339}
340
fc08a01a
HS
341int dbfifo_int_thresh = 10; /* 10 == 640 entry threshold */
342module_param(dbfifo_int_thresh, int, 0644);
343MODULE_PARM_DESC(dbfifo_int_thresh, "doorbell fifo interrupt threshold");
344
b8ff05a9 345/*
fc08a01a 346 * usecs to sleep while draining the dbfifo
b8ff05a9 347 */
fc08a01a
HS
348static int dbfifo_drain_delay = 1000;
349module_param(dbfifo_drain_delay, int, 0644);
350MODULE_PARM_DESC(dbfifo_drain_delay,
351 "usecs to sleep while draining the dbfifo");
352
353static inline int cxgb4_set_addr_hash(struct port_info *pi)
b8ff05a9 354{
fc08a01a
HS
355 struct adapter *adap = pi->adapter;
356 u64 vec = 0;
357 bool ucast = false;
358 struct hash_mac_addr *entry;
359
360 /* Calculate the hash vector for the updated list and program it */
361 list_for_each_entry(entry, &adap->mac_hlist, list) {
362 ucast |= is_unicast_ether_addr(entry->addr);
363 vec |= (1ULL << hash_mac_addr(entry->addr));
364 }
365 return t4_set_addr_hash(adap, adap->mbox, pi->viid, ucast,
366 vec, false);
367}
368
369static int cxgb4_mac_sync(struct net_device *netdev, const u8 *mac_addr)
370{
371 struct port_info *pi = netdev_priv(netdev);
372 struct adapter *adap = pi->adapter;
373 int ret;
b8ff05a9
DM
374 u64 mhash = 0;
375 u64 uhash = 0;
fc08a01a
HS
376 bool free = false;
377 bool ucast = is_unicast_ether_addr(mac_addr);
378 const u8 *maclist[1] = {mac_addr};
379 struct hash_mac_addr *new_entry;
380
381 ret = t4_alloc_mac_filt(adap, adap->mbox, pi->viid, free, 1, maclist,
382 NULL, ucast ? &uhash : &mhash, false);
383 if (ret < 0)
384 goto out;
385 /* if hash != 0, then add the addr to hash addr list
386 * so on the end we will calculate the hash for the
387 * list and program it
388 */
389 if (uhash || mhash) {
390 new_entry = kzalloc(sizeof(*new_entry), GFP_ATOMIC);
391 if (!new_entry)
392 return -ENOMEM;
393 ether_addr_copy(new_entry->addr, mac_addr);
394 list_add_tail(&new_entry->list, &adap->mac_hlist);
395 ret = cxgb4_set_addr_hash(pi);
b8ff05a9 396 }
fc08a01a
HS
397out:
398 return ret < 0 ? ret : 0;
399}
b8ff05a9 400
fc08a01a
HS
401static int cxgb4_mac_unsync(struct net_device *netdev, const u8 *mac_addr)
402{
403 struct port_info *pi = netdev_priv(netdev);
404 struct adapter *adap = pi->adapter;
405 int ret;
406 const u8 *maclist[1] = {mac_addr};
407 struct hash_mac_addr *entry, *tmp;
b8ff05a9 408
fc08a01a
HS
409 /* If the MAC address to be removed is in the hash addr
410 * list, delete it from the list and update hash vector
411 */
412 list_for_each_entry_safe(entry, tmp, &adap->mac_hlist, list) {
413 if (ether_addr_equal(entry->addr, mac_addr)) {
414 list_del(&entry->list);
415 kfree(entry);
416 return cxgb4_set_addr_hash(pi);
b8ff05a9
DM
417 }
418 }
419
fc08a01a
HS
420 ret = t4_free_mac_filt(adap, adap->mbox, pi->viid, 1, maclist, false);
421 return ret < 0 ? -EINVAL : 0;
b8ff05a9
DM
422}
423
424/*
425 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
426 * If @mtu is -1 it is left unchanged.
427 */
428static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
429{
b8ff05a9 430 struct port_info *pi = netdev_priv(dev);
fc08a01a 431 struct adapter *adapter = pi->adapter;
b8ff05a9 432
d01f7abc
HS
433 __dev_uc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
434 __dev_mc_sync(dev, cxgb4_mac_sync, cxgb4_mac_unsync);
fc08a01a
HS
435
436 return t4_set_rxmode(adapter, adapter->mbox, pi->viid, mtu,
437 (dev->flags & IFF_PROMISC) ? 1 : 0,
438 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
439 sleep_ok);
b8ff05a9
DM
440}
441
442/**
443 * link_start - enable a port
444 * @dev: the port to enable
445 *
446 * Performs the MAC and PHY actions needed to enable a port.
447 */
448static int link_start(struct net_device *dev)
449{
450 int ret;
451 struct port_info *pi = netdev_priv(dev);
b2612722 452 unsigned int mb = pi->adapter->pf;
b8ff05a9
DM
453
454 /*
455 * We do not set address filters and promiscuity here, the stack does
456 * that step explicitly.
457 */
060e0c75 458 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
f646968f 459 !!(dev->features & NETIF_F_HW_VLAN_CTAG_RX), true);
b8ff05a9 460 if (ret == 0) {
060e0c75 461 ret = t4_change_mac(pi->adapter, mb, pi->viid,
b8ff05a9 462 pi->xact_addr_filt, dev->dev_addr, true,
b6bd29e7 463 true);
b8ff05a9
DM
464 if (ret >= 0) {
465 pi->xact_addr_filt = ret;
466 ret = 0;
467 }
468 }
469 if (ret == 0)
4036da90 470 ret = t4_link_l1cfg(pi->adapter, mb, pi->tx_chan,
060e0c75 471 &pi->link_cfg);
30f00847
AB
472 if (ret == 0) {
473 local_bh_disable();
688848b1
AB
474 ret = t4_enable_vi_params(pi->adapter, mb, pi->viid, true,
475 true, CXGB4_DCB_ENABLED);
30f00847
AB
476 local_bh_enable();
477 }
688848b1 478
b8ff05a9
DM
479 return ret;
480}
481
688848b1
AB
482#ifdef CONFIG_CHELSIO_T4_DCB
483/* Handle a Data Center Bridging update message from the firmware. */
484static void dcb_rpl(struct adapter *adap, const struct fw_port_cmd *pcmd)
485{
2b5fb1f2 486 int port = FW_PORT_CMD_PORTID_G(ntohl(pcmd->op_to_portid));
134491fd 487 struct net_device *dev = adap->port[adap->chan_map[port]];
688848b1
AB
488 int old_dcb_enabled = cxgb4_dcb_enabled(dev);
489 int new_dcb_enabled;
490
491 cxgb4_dcb_handle_fw_update(adap, pcmd);
492 new_dcb_enabled = cxgb4_dcb_enabled(dev);
493
494 /* If the DCB has become enabled or disabled on the port then we're
495 * going to need to set up/tear down DCB Priority parameters for the
496 * TX Queues associated with the port.
497 */
498 if (new_dcb_enabled != old_dcb_enabled)
499 dcb_tx_queue_prio_enable(dev, new_dcb_enabled);
500}
501#endif /* CONFIG_CHELSIO_T4_DCB */
502
f2b7e78d 503/* Response queue handler for the FW event queue.
b8ff05a9
DM
504 */
505static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
506 const struct pkt_gl *gl)
507{
508 u8 opcode = ((const struct rss_header *)rsp)->opcode;
509
510 rsp++; /* skip RSS header */
b407a4a9
VP
511
512 /* FW can send EGR_UPDATEs encapsulated in a CPL_FW4_MSG.
513 */
514 if (unlikely(opcode == CPL_FW4_MSG &&
515 ((const struct cpl_fw4_msg *)rsp)->type == FW_TYPE_RSSCPL)) {
516 rsp++;
517 opcode = ((const struct rss_header *)rsp)->opcode;
518 rsp++;
519 if (opcode != CPL_SGE_EGR_UPDATE) {
520 dev_err(q->adap->pdev_dev, "unexpected FW4/CPL %#x on FW event queue\n"
521 , opcode);
522 goto out;
523 }
524 }
525
b8ff05a9
DM
526 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
527 const struct cpl_sge_egr_update *p = (void *)rsp;
bdc590b9 528 unsigned int qid = EGR_QID_G(ntohl(p->opcode_qid));
e46dab4d 529 struct sge_txq *txq;
b8ff05a9 530
e46dab4d 531 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
b8ff05a9 532 txq->restarts++;
e46dab4d 533 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
b8ff05a9
DM
534 struct sge_eth_txq *eq;
535
536 eq = container_of(txq, struct sge_eth_txq, q);
537 netif_tx_wake_queue(eq->txq);
538 } else {
539 struct sge_ofld_txq *oq;
540
541 oq = container_of(txq, struct sge_ofld_txq, q);
542 tasklet_schedule(&oq->qresume_tsk);
543 }
544 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
545 const struct cpl_fw6_msg *p = (void *)rsp;
546
688848b1
AB
547#ifdef CONFIG_CHELSIO_T4_DCB
548 const struct fw_port_cmd *pcmd = (const void *)p->data;
e2ac9628 549 unsigned int cmd = FW_CMD_OP_G(ntohl(pcmd->op_to_portid));
688848b1 550 unsigned int action =
2b5fb1f2 551 FW_PORT_CMD_ACTION_G(ntohl(pcmd->action_to_len16));
688848b1
AB
552
553 if (cmd == FW_PORT_CMD &&
554 action == FW_PORT_ACTION_GET_PORT_INFO) {
2b5fb1f2 555 int port = FW_PORT_CMD_PORTID_G(
688848b1 556 be32_to_cpu(pcmd->op_to_portid));
134491fd
HS
557 struct net_device *dev =
558 q->adap->port[q->adap->chan_map[port]];
688848b1 559 int state_input = ((pcmd->u.info.dcbxdis_pkd &
2b5fb1f2 560 FW_PORT_CMD_DCBXDIS_F)
688848b1
AB
561 ? CXGB4_DCB_INPUT_FW_DISABLED
562 : CXGB4_DCB_INPUT_FW_ENABLED);
563
564 cxgb4_dcb_state_fsm(dev, state_input);
565 }
566
567 if (cmd == FW_PORT_CMD &&
568 action == FW_PORT_ACTION_L2_DCB_CFG)
569 dcb_rpl(q->adap, pcmd);
570 else
571#endif
572 if (p->type == 0)
573 t4_handle_fw_rpl(q->adap, p->data);
b8ff05a9
DM
574 } else if (opcode == CPL_L2T_WRITE_RPL) {
575 const struct cpl_l2t_write_rpl *p = (void *)rsp;
576
577 do_l2t_write_rpl(q->adap, p);
f2b7e78d
VP
578 } else if (opcode == CPL_SET_TCB_RPL) {
579 const struct cpl_set_tcb_rpl *p = (void *)rsp;
580
581 filter_rpl(q->adap, p);
b8ff05a9
DM
582 } else
583 dev_err(q->adap->pdev_dev,
584 "unexpected CPL %#x on FW event queue\n", opcode);
b407a4a9 585out:
b8ff05a9
DM
586 return 0;
587}
588
b8ff05a9
DM
589static void disable_msi(struct adapter *adapter)
590{
591 if (adapter->flags & USING_MSIX) {
592 pci_disable_msix(adapter->pdev);
593 adapter->flags &= ~USING_MSIX;
594 } else if (adapter->flags & USING_MSI) {
595 pci_disable_msi(adapter->pdev);
596 adapter->flags &= ~USING_MSI;
597 }
598}
599
600/*
601 * Interrupt handler for non-data events used with MSI-X.
602 */
603static irqreturn_t t4_nondata_intr(int irq, void *cookie)
604{
605 struct adapter *adap = cookie;
0d804338 606 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A));
b8ff05a9 607
0d804338 608 if (v & PFSW_F) {
b8ff05a9 609 adap->swintr = 1;
0d804338 610 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE_A), v);
b8ff05a9 611 }
c3c7b121
HS
612 if (adap->flags & MASTER_PF)
613 t4_slow_intr_handler(adap);
b8ff05a9
DM
614 return IRQ_HANDLED;
615}
616
617/*
618 * Name the MSI-X interrupts.
619 */
620static void name_msix_vecs(struct adapter *adap)
621{
ba27816c 622 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc);
b8ff05a9
DM
623
624 /* non-data interrupts */
b1a3c2b6 625 snprintf(adap->msix_info[0].desc, n, "%s", adap->port[0]->name);
b8ff05a9
DM
626
627 /* FW events */
b1a3c2b6
DM
628 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq",
629 adap->port[0]->name);
b8ff05a9
DM
630
631 /* Ethernet queues */
632 for_each_port(adap, j) {
633 struct net_device *d = adap->port[j];
634 const struct port_info *pi = netdev_priv(d);
635
ba27816c 636 for (i = 0; i < pi->nqsets; i++, msi_idx++)
b8ff05a9
DM
637 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
638 d->name, i);
b8ff05a9 639 }
b8ff05a9
DM
640}
641
642static int request_msix_queue_irqs(struct adapter *adap)
643{
644 struct sge *s = &adap->sge;
0fbc81b3 645 int err, ethqidx;
cf38be6d 646 int msi_index = 2;
b8ff05a9
DM
647
648 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
649 adap->msix_info[1].desc, &s->fw_evtq);
650 if (err)
651 return err;
652
653 for_each_ethrxq(s, ethqidx) {
404d9e3f
VP
654 err = request_irq(adap->msix_info[msi_index].vec,
655 t4_sge_intr_msix, 0,
656 adap->msix_info[msi_index].desc,
b8ff05a9
DM
657 &s->ethrxq[ethqidx].rspq);
658 if (err)
659 goto unwind;
404d9e3f 660 msi_index++;
b8ff05a9 661 }
b8ff05a9
DM
662 return 0;
663
664unwind:
b8ff05a9 665 while (--ethqidx >= 0)
404d9e3f
VP
666 free_irq(adap->msix_info[--msi_index].vec,
667 &s->ethrxq[ethqidx].rspq);
b8ff05a9
DM
668 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
669 return err;
670}
671
672static void free_msix_queue_irqs(struct adapter *adap)
673{
404d9e3f 674 int i, msi_index = 2;
b8ff05a9
DM
675 struct sge *s = &adap->sge;
676
677 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
678 for_each_ethrxq(s, i)
404d9e3f 679 free_irq(adap->msix_info[msi_index++].vec, &s->ethrxq[i].rspq);
b8ff05a9
DM
680}
681
671b0060 682/**
812034f1 683 * cxgb4_write_rss - write the RSS table for a given port
671b0060
DM
684 * @pi: the port
685 * @queues: array of queue indices for RSS
686 *
687 * Sets up the portion of the HW RSS table for the port's VI to distribute
688 * packets to the Rx queues in @queues.
c035e183 689 * Should never be called before setting up sge eth rx queues
671b0060 690 */
812034f1 691int cxgb4_write_rss(const struct port_info *pi, const u16 *queues)
671b0060
DM
692{
693 u16 *rss;
694 int i, err;
c035e183
HS
695 struct adapter *adapter = pi->adapter;
696 const struct sge_eth_rxq *rxq;
671b0060 697
c035e183 698 rxq = &adapter->sge.ethrxq[pi->first_qset];
671b0060
DM
699 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
700 if (!rss)
701 return -ENOMEM;
702
703 /* map the queue indices to queue ids */
704 for (i = 0; i < pi->rss_size; i++, queues++)
c035e183 705 rss[i] = rxq[*queues].rspq.abs_id;
671b0060 706
b2612722 707 err = t4_config_rss_range(adapter, adapter->pf, pi->viid, 0,
060e0c75 708 pi->rss_size, rss, pi->rss_size);
c035e183
HS
709 /* If Tunnel All Lookup isn't specified in the global RSS
710 * Configuration, then we need to specify a default Ingress
711 * Queue for any ingress packets which aren't hashed. We'll
712 * use our first ingress queue ...
713 */
714 if (!err)
715 err = t4_config_vi_rss(adapter, adapter->mbox, pi->viid,
716 FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN_F |
717 FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN_F |
718 FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN_F |
719 FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN_F |
720 FW_RSS_VI_CONFIG_CMD_UDPEN_F,
721 rss[0]);
671b0060
DM
722 kfree(rss);
723 return err;
724}
725
b8ff05a9
DM
726/**
727 * setup_rss - configure RSS
728 * @adap: the adapter
729 *
671b0060 730 * Sets up RSS for each port.
b8ff05a9
DM
731 */
732static int setup_rss(struct adapter *adap)
733{
c035e183 734 int i, j, err;
b8ff05a9
DM
735
736 for_each_port(adap, i) {
737 const struct port_info *pi = adap2pinfo(adap, i);
b8ff05a9 738
c035e183
HS
739 /* Fill default values with equal distribution */
740 for (j = 0; j < pi->rss_size; j++)
741 pi->rss[j] = j % pi->nqsets;
742
812034f1 743 err = cxgb4_write_rss(pi, pi->rss);
b8ff05a9
DM
744 if (err)
745 return err;
746 }
747 return 0;
748}
749
e46dab4d
DM
750/*
751 * Return the channel of the ingress queue with the given qid.
752 */
753static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
754{
755 qid -= p->ingr_start;
756 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
757}
758
b8ff05a9
DM
759/*
760 * Wait until all NAPI handlers are descheduled.
761 */
762static void quiesce_rx(struct adapter *adap)
763{
764 int i;
765
4b8e27a8 766 for (i = 0; i < adap->sge.ingr_sz; i++) {
b8ff05a9
DM
767 struct sge_rspq *q = adap->sge.ingr_map[i];
768
3a336cb1 769 if (q && q->handler) {
b8ff05a9 770 napi_disable(&q->napi);
3a336cb1
HS
771 local_bh_disable();
772 while (!cxgb_poll_lock_napi(q))
773 mdelay(1);
774 local_bh_enable();
775 }
776
b8ff05a9
DM
777 }
778}
779
b37987e8
HS
780/* Disable interrupt and napi handler */
781static void disable_interrupts(struct adapter *adap)
782{
783 if (adap->flags & FULL_INIT_DONE) {
784 t4_intr_disable(adap);
785 if (adap->flags & USING_MSIX) {
786 free_msix_queue_irqs(adap);
787 free_irq(adap->msix_info[0].vec, adap);
788 } else {
789 free_irq(adap->pdev->irq, adap);
790 }
791 quiesce_rx(adap);
792 }
793}
794
b8ff05a9
DM
795/*
796 * Enable NAPI scheduling and interrupt generation for all Rx queues.
797 */
798static void enable_rx(struct adapter *adap)
799{
800 int i;
801
4b8e27a8 802 for (i = 0; i < adap->sge.ingr_sz; i++) {
b8ff05a9
DM
803 struct sge_rspq *q = adap->sge.ingr_map[i];
804
805 if (!q)
806 continue;
3a336cb1
HS
807 if (q->handler) {
808 cxgb_busy_poll_init_lock(q);
b8ff05a9 809 napi_enable(&q->napi);
3a336cb1 810 }
b8ff05a9 811 /* 0-increment GTS to start the timer and enable interrupts */
f612b815
HS
812 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS_A),
813 SEINTARM_V(q->intr_params) |
814 INGRESSQID_V(q->cntxt_id));
b8ff05a9
DM
815 }
816}
817
1c6a5b0e 818
0fbc81b3 819static int setup_fw_sge_queues(struct adapter *adap)
b8ff05a9 820{
b8ff05a9 821 struct sge *s = &adap->sge;
0fbc81b3 822 int err = 0;
b8ff05a9 823
4b8e27a8
HS
824 bitmap_zero(s->starving_fl, s->egr_sz);
825 bitmap_zero(s->txq_maperr, s->egr_sz);
b8ff05a9
DM
826
827 if (adap->flags & USING_MSIX)
94cdb8bb 828 adap->msi_idx = 1; /* vector 0 is for non-queue interrupts */
b8ff05a9
DM
829 else {
830 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
2337ba42 831 NULL, NULL, NULL, -1);
b8ff05a9
DM
832 if (err)
833 return err;
94cdb8bb 834 adap->msi_idx = -((int)s->intrq.abs_id + 1);
b8ff05a9
DM
835 }
836
837 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
94cdb8bb 838 adap->msi_idx, NULL, fwevtq_handler, NULL, -1);
0fbc81b3
HS
839 if (err)
840 t4_free_sge_resources(adap);
841 return err;
842}
843
844/**
845 * setup_sge_queues - configure SGE Tx/Rx/response queues
846 * @adap: the adapter
847 *
848 * Determines how many sets of SGE queues to use and initializes them.
849 * We support multiple queue sets per port if we have MSI-X, otherwise
850 * just one queue set per port.
851 */
852static int setup_sge_queues(struct adapter *adap)
853{
854 int err, i, j;
855 struct sge *s = &adap->sge;
856 struct sge_uld_rxq_info *rxq_info = s->uld_rxq_info[CXGB4_ULD_RDMA];
857 unsigned int cmplqid = 0;
b8ff05a9
DM
858
859 for_each_port(adap, i) {
860 struct net_device *dev = adap->port[i];
861 struct port_info *pi = netdev_priv(dev);
862 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
863 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
864
865 for (j = 0; j < pi->nqsets; j++, q++) {
94cdb8bb
HS
866 if (adap->msi_idx > 0)
867 adap->msi_idx++;
b8ff05a9 868 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
94cdb8bb 869 adap->msi_idx, &q->fl,
145ef8a5 870 t4_ethrx_handler,
2337ba42 871 NULL,
145ef8a5
HS
872 t4_get_mps_bg_map(adap,
873 pi->tx_chan));
b8ff05a9
DM
874 if (err)
875 goto freeout;
876 q->rspq.idx = j;
877 memset(&q->stats, 0, sizeof(q->stats));
878 }
879 for (j = 0; j < pi->nqsets; j++, t++) {
880 err = t4_sge_alloc_eth_txq(adap, t, dev,
881 netdev_get_tx_queue(dev, j),
882 s->fw_evtq.cntxt_id);
883 if (err)
884 goto freeout;
885 }
886 }
887
0fbc81b3
HS
888 j = s->ofldqsets / adap->params.nports; /* iscsi queues per channel */
889 for_each_ofldtxq(s, i) {
1c6a5b0e
HS
890 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i],
891 adap->port[i / j],
b8ff05a9
DM
892 s->fw_evtq.cntxt_id);
893 if (err)
894 goto freeout;
895 }
896
b8ff05a9 897 for_each_port(adap, i) {
0fbc81b3 898 /* Note that cmplqid below is 0 if we don't
b8ff05a9
DM
899 * have RDMA queues, and that's the right value.
900 */
0fbc81b3
HS
901 if (rxq_info)
902 cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
903
b8ff05a9 904 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
0fbc81b3 905 s->fw_evtq.cntxt_id, cmplqid);
b8ff05a9
DM
906 if (err)
907 goto freeout;
908 }
909
9bb59b96 910 t4_write_reg(adap, is_t4(adap->params.chip) ?
837e4a42
HS
911 MPS_TRC_RSS_CONTROL_A :
912 MPS_T5_TRC_RSS_CONTROL_A,
913 RSSCONTROL_V(netdev2pinfo(adap->port[0])->tx_chan) |
914 QUEUENUMBER_V(s->ethrxq[0].rspq.abs_id));
b8ff05a9 915 return 0;
0fbc81b3
HS
916freeout:
917 t4_free_sge_resources(adap);
918 return err;
b8ff05a9
DM
919}
920
b8ff05a9
DM
921/*
922 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
923 * The allocated memory is cleared.
924 */
925void *t4_alloc_mem(size_t size)
926{
8be04b93 927 void *p = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
b8ff05a9
DM
928
929 if (!p)
89bf67f1 930 p = vzalloc(size);
b8ff05a9
DM
931 return p;
932}
933
934/*
935 * Free memory allocated through alloc_mem().
936 */
fd88b31a 937void t4_free_mem(void *addr)
b8ff05a9 938{
d2fcb548 939 kvfree(addr);
b8ff05a9
DM
940}
941
688848b1
AB
942static u16 cxgb_select_queue(struct net_device *dev, struct sk_buff *skb,
943 void *accel_priv, select_queue_fallback_t fallback)
944{
945 int txq;
946
947#ifdef CONFIG_CHELSIO_T4_DCB
948 /* If a Data Center Bridging has been successfully negotiated on this
949 * link then we'll use the skb's priority to map it to a TX Queue.
950 * The skb's priority is determined via the VLAN Tag Priority Code
951 * Point field.
952 */
953 if (cxgb4_dcb_enabled(dev)) {
954 u16 vlan_tci;
955 int err;
956
957 err = vlan_get_tag(skb, &vlan_tci);
958 if (unlikely(err)) {
959 if (net_ratelimit())
960 netdev_warn(dev,
961 "TX Packet without VLAN Tag on DCB Link\n");
962 txq = 0;
963 } else {
964 txq = (vlan_tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
84a200b3
VP
965#ifdef CONFIG_CHELSIO_T4_FCOE
966 if (skb->protocol == htons(ETH_P_FCOE))
967 txq = skb->priority & 0x7;
968#endif /* CONFIG_CHELSIO_T4_FCOE */
688848b1
AB
969 }
970 return txq;
971 }
972#endif /* CONFIG_CHELSIO_T4_DCB */
973
974 if (select_queue) {
975 txq = (skb_rx_queue_recorded(skb)
976 ? skb_get_rx_queue(skb)
977 : smp_processor_id());
978
979 while (unlikely(txq >= dev->real_num_tx_queues))
980 txq -= dev->real_num_tx_queues;
981
982 return txq;
983 }
984
985 return fallback(dev, skb) % dev->real_num_tx_queues;
986}
987
b8ff05a9
DM
988static int closest_timer(const struct sge *s, int time)
989{
990 int i, delta, match = 0, min_delta = INT_MAX;
991
992 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
993 delta = time - s->timer_val[i];
994 if (delta < 0)
995 delta = -delta;
996 if (delta < min_delta) {
997 min_delta = delta;
998 match = i;
999 }
1000 }
1001 return match;
1002}
1003
1004static int closest_thres(const struct sge *s, int thres)
1005{
1006 int i, delta, match = 0, min_delta = INT_MAX;
1007
1008 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1009 delta = thres - s->counter_val[i];
1010 if (delta < 0)
1011 delta = -delta;
1012 if (delta < min_delta) {
1013 min_delta = delta;
1014 match = i;
1015 }
1016 }
1017 return match;
1018}
1019
b8ff05a9 1020/**
812034f1 1021 * cxgb4_set_rspq_intr_params - set a queue's interrupt holdoff parameters
b8ff05a9
DM
1022 * @q: the Rx queue
1023 * @us: the hold-off time in us, or 0 to disable timer
1024 * @cnt: the hold-off packet count, or 0 to disable counter
1025 *
1026 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1027 * one of the two needs to be enabled for the queue to generate interrupts.
1028 */
812034f1
HS
1029int cxgb4_set_rspq_intr_params(struct sge_rspq *q,
1030 unsigned int us, unsigned int cnt)
b8ff05a9 1031{
c887ad0e
HS
1032 struct adapter *adap = q->adap;
1033
b8ff05a9
DM
1034 if ((us | cnt) == 0)
1035 cnt = 1;
1036
1037 if (cnt) {
1038 int err;
1039 u32 v, new_idx;
1040
1041 new_idx = closest_thres(&adap->sge, cnt);
1042 if (q->desc && q->pktcnt_idx != new_idx) {
1043 /* the queue has already been created, update it */
5167865a
HS
1044 v = FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
1045 FW_PARAMS_PARAM_X_V(
1046 FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1047 FW_PARAMS_PARAM_YZ_V(q->cntxt_id);
b2612722
HS
1048 err = t4_set_params(adap, adap->mbox, adap->pf, 0, 1,
1049 &v, &new_idx);
b8ff05a9
DM
1050 if (err)
1051 return err;
1052 }
1053 q->pktcnt_idx = new_idx;
1054 }
1055
1056 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
1ecc7b7a 1057 q->intr_params = QINTR_TIMER_IDX_V(us) | QINTR_CNT_EN_V(cnt > 0);
b8ff05a9
DM
1058 return 0;
1059}
1060
c8f44aff 1061static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
87b6cf51 1062{
2ed28baa 1063 const struct port_info *pi = netdev_priv(dev);
c8f44aff 1064 netdev_features_t changed = dev->features ^ features;
19ecae2c 1065 int err;
19ecae2c 1066
f646968f 1067 if (!(changed & NETIF_F_HW_VLAN_CTAG_RX))
2ed28baa 1068 return 0;
19ecae2c 1069
b2612722 1070 err = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, -1,
2ed28baa 1071 -1, -1, -1,
f646968f 1072 !!(features & NETIF_F_HW_VLAN_CTAG_RX), true);
2ed28baa 1073 if (unlikely(err))
f646968f 1074 dev->features = features ^ NETIF_F_HW_VLAN_CTAG_RX;
19ecae2c 1075 return err;
87b6cf51
DM
1076}
1077
91744948 1078static int setup_debugfs(struct adapter *adap)
b8ff05a9 1079{
b8ff05a9
DM
1080 if (IS_ERR_OR_NULL(adap->debugfs_root))
1081 return -1;
1082
fd88b31a
HS
1083#ifdef CONFIG_DEBUG_FS
1084 t4_setup_debugfs(adap);
1085#endif
b8ff05a9
DM
1086 return 0;
1087}
1088
1089/*
1090 * upper-layer driver support
1091 */
1092
1093/*
1094 * Allocate an active-open TID and set it to the supplied value.
1095 */
1096int cxgb4_alloc_atid(struct tid_info *t, void *data)
1097{
1098 int atid = -1;
1099
1100 spin_lock_bh(&t->atid_lock);
1101 if (t->afree) {
1102 union aopen_entry *p = t->afree;
1103
f2b7e78d 1104 atid = (p - t->atid_tab) + t->atid_base;
b8ff05a9
DM
1105 t->afree = p->next;
1106 p->data = data;
1107 t->atids_in_use++;
1108 }
1109 spin_unlock_bh(&t->atid_lock);
1110 return atid;
1111}
1112EXPORT_SYMBOL(cxgb4_alloc_atid);
1113
1114/*
1115 * Release an active-open TID.
1116 */
1117void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
1118{
f2b7e78d 1119 union aopen_entry *p = &t->atid_tab[atid - t->atid_base];
b8ff05a9
DM
1120
1121 spin_lock_bh(&t->atid_lock);
1122 p->next = t->afree;
1123 t->afree = p;
1124 t->atids_in_use--;
1125 spin_unlock_bh(&t->atid_lock);
1126}
1127EXPORT_SYMBOL(cxgb4_free_atid);
1128
1129/*
1130 * Allocate a server TID and set it to the supplied value.
1131 */
1132int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
1133{
1134 int stid;
1135
1136 spin_lock_bh(&t->stid_lock);
1137 if (family == PF_INET) {
1138 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
1139 if (stid < t->nstids)
1140 __set_bit(stid, t->stid_bmap);
1141 else
1142 stid = -1;
1143 } else {
a99c683e 1144 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 1);
b8ff05a9
DM
1145 if (stid < 0)
1146 stid = -1;
1147 }
1148 if (stid >= 0) {
1149 t->stid_tab[stid].data = data;
1150 stid += t->stid_base;
15f63b74
KS
1151 /* IPv6 requires max of 520 bits or 16 cells in TCAM
1152 * This is equivalent to 4 TIDs. With CLIP enabled it
1153 * needs 2 TIDs.
1154 */
1155 if (family == PF_INET)
1156 t->stids_in_use++;
1157 else
a99c683e 1158 t->stids_in_use += 2;
b8ff05a9
DM
1159 }
1160 spin_unlock_bh(&t->stid_lock);
1161 return stid;
1162}
1163EXPORT_SYMBOL(cxgb4_alloc_stid);
1164
dca4faeb
VP
1165/* Allocate a server filter TID and set it to the supplied value.
1166 */
1167int cxgb4_alloc_sftid(struct tid_info *t, int family, void *data)
1168{
1169 int stid;
1170
1171 spin_lock_bh(&t->stid_lock);
1172 if (family == PF_INET) {
1173 stid = find_next_zero_bit(t->stid_bmap,
1174 t->nstids + t->nsftids, t->nstids);
1175 if (stid < (t->nstids + t->nsftids))
1176 __set_bit(stid, t->stid_bmap);
1177 else
1178 stid = -1;
1179 } else {
1180 stid = -1;
1181 }
1182 if (stid >= 0) {
1183 t->stid_tab[stid].data = data;
470c60c4
KS
1184 stid -= t->nstids;
1185 stid += t->sftid_base;
2248b293 1186 t->sftids_in_use++;
dca4faeb
VP
1187 }
1188 spin_unlock_bh(&t->stid_lock);
1189 return stid;
1190}
1191EXPORT_SYMBOL(cxgb4_alloc_sftid);
1192
1193/* Release a server TID.
b8ff05a9
DM
1194 */
1195void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
1196{
470c60c4
KS
1197 /* Is it a server filter TID? */
1198 if (t->nsftids && (stid >= t->sftid_base)) {
1199 stid -= t->sftid_base;
1200 stid += t->nstids;
1201 } else {
1202 stid -= t->stid_base;
1203 }
1204
b8ff05a9
DM
1205 spin_lock_bh(&t->stid_lock);
1206 if (family == PF_INET)
1207 __clear_bit(stid, t->stid_bmap);
1208 else
a99c683e 1209 bitmap_release_region(t->stid_bmap, stid, 1);
b8ff05a9 1210 t->stid_tab[stid].data = NULL;
2248b293
HS
1211 if (stid < t->nstids) {
1212 if (family == PF_INET)
1213 t->stids_in_use--;
1214 else
a99c683e 1215 t->stids_in_use -= 2;
2248b293
HS
1216 } else {
1217 t->sftids_in_use--;
1218 }
b8ff05a9
DM
1219 spin_unlock_bh(&t->stid_lock);
1220}
1221EXPORT_SYMBOL(cxgb4_free_stid);
1222
1223/*
1224 * Populate a TID_RELEASE WR. Caller must properly size the skb.
1225 */
1226static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
1227 unsigned int tid)
1228{
1229 struct cpl_tid_release *req;
1230
1231 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
1232 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
1233 INIT_TP_WR(req, tid);
1234 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
1235}
1236
1237/*
1238 * Queue a TID release request and if necessary schedule a work queue to
1239 * process it.
1240 */
31b9c19b 1241static void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
1242 unsigned int tid)
b8ff05a9
DM
1243{
1244 void **p = &t->tid_tab[tid];
1245 struct adapter *adap = container_of(t, struct adapter, tids);
1246
1247 spin_lock_bh(&adap->tid_release_lock);
1248 *p = adap->tid_release_head;
1249 /* Low 2 bits encode the Tx channel number */
1250 adap->tid_release_head = (void **)((uintptr_t)p | chan);
1251 if (!adap->tid_release_task_busy) {
1252 adap->tid_release_task_busy = true;
29aaee65 1253 queue_work(adap->workq, &adap->tid_release_task);
b8ff05a9
DM
1254 }
1255 spin_unlock_bh(&adap->tid_release_lock);
1256}
b8ff05a9
DM
1257
1258/*
1259 * Process the list of pending TID release requests.
1260 */
1261static void process_tid_release_list(struct work_struct *work)
1262{
1263 struct sk_buff *skb;
1264 struct adapter *adap;
1265
1266 adap = container_of(work, struct adapter, tid_release_task);
1267
1268 spin_lock_bh(&adap->tid_release_lock);
1269 while (adap->tid_release_head) {
1270 void **p = adap->tid_release_head;
1271 unsigned int chan = (uintptr_t)p & 3;
1272 p = (void *)p - chan;
1273
1274 adap->tid_release_head = *p;
1275 *p = NULL;
1276 spin_unlock_bh(&adap->tid_release_lock);
1277
1278 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
1279 GFP_KERNEL)))
1280 schedule_timeout_uninterruptible(1);
1281
1282 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
1283 t4_ofld_send(adap, skb);
1284 spin_lock_bh(&adap->tid_release_lock);
1285 }
1286 adap->tid_release_task_busy = false;
1287 spin_unlock_bh(&adap->tid_release_lock);
1288}
1289
1290/*
1291 * Release a TID and inform HW. If we are unable to allocate the release
1292 * message we defer to a work queue.
1293 */
1294void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
1295{
b8ff05a9
DM
1296 struct sk_buff *skb;
1297 struct adapter *adap = container_of(t, struct adapter, tids);
1298
9a1bb9f6
HS
1299 WARN_ON(tid >= t->ntids);
1300
1301 if (t->tid_tab[tid]) {
1302 t->tid_tab[tid] = NULL;
1303 if (t->hash_base && (tid >= t->hash_base))
1304 atomic_dec(&t->hash_tids_in_use);
1305 else
1306 atomic_dec(&t->tids_in_use);
1307 }
1308
b8ff05a9
DM
1309 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
1310 if (likely(skb)) {
b8ff05a9
DM
1311 mk_tid_release(skb, chan, tid);
1312 t4_ofld_send(adap, skb);
1313 } else
1314 cxgb4_queue_tid_release(t, chan, tid);
b8ff05a9
DM
1315}
1316EXPORT_SYMBOL(cxgb4_remove_tid);
1317
1318/*
1319 * Allocate and initialize the TID tables. Returns 0 on success.
1320 */
1321static int tid_init(struct tid_info *t)
1322{
b6f8eaec 1323 struct adapter *adap = container_of(t, struct adapter, tids);
578b46b9
RL
1324 unsigned int max_ftids = t->nftids + t->nsftids;
1325 unsigned int natids = t->natids;
1326 unsigned int stid_bmap_size;
1327 unsigned int ftid_bmap_size;
1328 size_t size;
b8ff05a9 1329
dca4faeb 1330 stid_bmap_size = BITS_TO_LONGS(t->nstids + t->nsftids);
578b46b9 1331 ftid_bmap_size = BITS_TO_LONGS(t->nftids);
f2b7e78d
VP
1332 size = t->ntids * sizeof(*t->tid_tab) +
1333 natids * sizeof(*t->atid_tab) +
b8ff05a9 1334 t->nstids * sizeof(*t->stid_tab) +
dca4faeb 1335 t->nsftids * sizeof(*t->stid_tab) +
f2b7e78d 1336 stid_bmap_size * sizeof(long) +
578b46b9
RL
1337 max_ftids * sizeof(*t->ftid_tab) +
1338 ftid_bmap_size * sizeof(long);
f2b7e78d 1339
b8ff05a9
DM
1340 t->tid_tab = t4_alloc_mem(size);
1341 if (!t->tid_tab)
1342 return -ENOMEM;
1343
1344 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
1345 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
dca4faeb 1346 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids + t->nsftids];
f2b7e78d 1347 t->ftid_tab = (struct filter_entry *)&t->stid_bmap[stid_bmap_size];
578b46b9 1348 t->ftid_bmap = (unsigned long *)&t->ftid_tab[max_ftids];
b8ff05a9
DM
1349 spin_lock_init(&t->stid_lock);
1350 spin_lock_init(&t->atid_lock);
578b46b9 1351 spin_lock_init(&t->ftid_lock);
b8ff05a9
DM
1352
1353 t->stids_in_use = 0;
2248b293 1354 t->sftids_in_use = 0;
b8ff05a9
DM
1355 t->afree = NULL;
1356 t->atids_in_use = 0;
1357 atomic_set(&t->tids_in_use, 0);
9a1bb9f6 1358 atomic_set(&t->hash_tids_in_use, 0);
b8ff05a9
DM
1359
1360 /* Setup the free list for atid_tab and clear the stid bitmap. */
1361 if (natids) {
1362 while (--natids)
1363 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1364 t->afree = t->atid_tab;
1365 }
b6f8eaec 1366
578b46b9
RL
1367 if (is_offload(adap)) {
1368 bitmap_zero(t->stid_bmap, t->nstids + t->nsftids);
1369 /* Reserve stid 0 for T4/T5 adapters */
1370 if (!t->stid_base &&
1371 CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1372 __set_bit(0, t->stid_bmap);
1373 }
1374
1375 bitmap_zero(t->ftid_bmap, t->nftids);
b8ff05a9
DM
1376 return 0;
1377}
1378
1379/**
1380 * cxgb4_create_server - create an IP server
1381 * @dev: the device
1382 * @stid: the server TID
1383 * @sip: local IP address to bind server to
1384 * @sport: the server's TCP port
1385 * @queue: queue to direct messages from this server to
1386 *
1387 * Create an IP server for the given port and address.
1388 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1389 */
1390int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
793dad94
VP
1391 __be32 sip, __be16 sport, __be16 vlan,
1392 unsigned int queue)
b8ff05a9
DM
1393{
1394 unsigned int chan;
1395 struct sk_buff *skb;
1396 struct adapter *adap;
1397 struct cpl_pass_open_req *req;
80f40c1f 1398 int ret;
b8ff05a9
DM
1399
1400 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1401 if (!skb)
1402 return -ENOMEM;
1403
1404 adap = netdev2adap(dev);
1405 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
1406 INIT_TP_WR(req, 0);
1407 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
1408 req->local_port = sport;
1409 req->peer_port = htons(0);
1410 req->local_ip = sip;
1411 req->peer_ip = htonl(0);
e46dab4d 1412 chan = rxq_to_chan(&adap->sge, queue);
d7990b0c 1413 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
6c53e938
HS
1414 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1415 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
80f40c1f
VP
1416 ret = t4_mgmt_tx(adap, skb);
1417 return net_xmit_eval(ret);
b8ff05a9
DM
1418}
1419EXPORT_SYMBOL(cxgb4_create_server);
1420
80f40c1f
VP
1421/* cxgb4_create_server6 - create an IPv6 server
1422 * @dev: the device
1423 * @stid: the server TID
1424 * @sip: local IPv6 address to bind server to
1425 * @sport: the server's TCP port
1426 * @queue: queue to direct messages from this server to
1427 *
1428 * Create an IPv6 server for the given port and address.
1429 * Returns <0 on error and one of the %NET_XMIT_* values on success.
1430 */
1431int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
1432 const struct in6_addr *sip, __be16 sport,
1433 unsigned int queue)
1434{
1435 unsigned int chan;
1436 struct sk_buff *skb;
1437 struct adapter *adap;
1438 struct cpl_pass_open_req6 *req;
1439 int ret;
1440
1441 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1442 if (!skb)
1443 return -ENOMEM;
1444
1445 adap = netdev2adap(dev);
1446 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
1447 INIT_TP_WR(req, 0);
1448 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
1449 req->local_port = sport;
1450 req->peer_port = htons(0);
1451 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
1452 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
1453 req->peer_ip_hi = cpu_to_be64(0);
1454 req->peer_ip_lo = cpu_to_be64(0);
1455 chan = rxq_to_chan(&adap->sge, queue);
d7990b0c 1456 req->opt0 = cpu_to_be64(TX_CHAN_V(chan));
6c53e938
HS
1457 req->opt1 = cpu_to_be64(CONN_POLICY_V(CPL_CONN_POLICY_ASK) |
1458 SYN_RSS_ENABLE_F | SYN_RSS_QUEUE_V(queue));
80f40c1f
VP
1459 ret = t4_mgmt_tx(adap, skb);
1460 return net_xmit_eval(ret);
1461}
1462EXPORT_SYMBOL(cxgb4_create_server6);
1463
1464int cxgb4_remove_server(const struct net_device *dev, unsigned int stid,
1465 unsigned int queue, bool ipv6)
1466{
1467 struct sk_buff *skb;
1468 struct adapter *adap;
1469 struct cpl_close_listsvr_req *req;
1470 int ret;
1471
1472 adap = netdev2adap(dev);
1473
1474 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
1475 if (!skb)
1476 return -ENOMEM;
1477
1478 req = (struct cpl_close_listsvr_req *)__skb_put(skb, sizeof(*req));
1479 INIT_TP_WR(req, 0);
1480 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ, stid));
bdc590b9
HS
1481 req->reply_ctrl = htons(NO_REPLY_V(0) | (ipv6 ? LISTSVR_IPV6_V(1) :
1482 LISTSVR_IPV6_V(0)) | QUEUENO_V(queue));
80f40c1f
VP
1483 ret = t4_mgmt_tx(adap, skb);
1484 return net_xmit_eval(ret);
1485}
1486EXPORT_SYMBOL(cxgb4_remove_server);
1487
b8ff05a9
DM
1488/**
1489 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
1490 * @mtus: the HW MTU table
1491 * @mtu: the target MTU
1492 * @idx: index of selected entry in the MTU table
1493 *
1494 * Returns the index and the value in the HW MTU table that is closest to
1495 * but does not exceed @mtu, unless @mtu is smaller than any value in the
1496 * table, in which case that smallest available value is selected.
1497 */
1498unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
1499 unsigned int *idx)
1500{
1501 unsigned int i = 0;
1502
1503 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
1504 ++i;
1505 if (idx)
1506 *idx = i;
1507 return mtus[i];
1508}
1509EXPORT_SYMBOL(cxgb4_best_mtu);
1510
92e7ae71
HS
1511/**
1512 * cxgb4_best_aligned_mtu - find best MTU, [hopefully] data size aligned
1513 * @mtus: the HW MTU table
1514 * @header_size: Header Size
1515 * @data_size_max: maximum Data Segment Size
1516 * @data_size_align: desired Data Segment Size Alignment (2^N)
1517 * @mtu_idxp: HW MTU Table Index return value pointer (possibly NULL)
1518 *
1519 * Similar to cxgb4_best_mtu() but instead of searching the Hardware
1520 * MTU Table based solely on a Maximum MTU parameter, we break that
1521 * parameter up into a Header Size and Maximum Data Segment Size, and
1522 * provide a desired Data Segment Size Alignment. If we find an MTU in
1523 * the Hardware MTU Table which will result in a Data Segment Size with
1524 * the requested alignment _and_ that MTU isn't "too far" from the
1525 * closest MTU, then we'll return that rather than the closest MTU.
1526 */
1527unsigned int cxgb4_best_aligned_mtu(const unsigned short *mtus,
1528 unsigned short header_size,
1529 unsigned short data_size_max,
1530 unsigned short data_size_align,
1531 unsigned int *mtu_idxp)
1532{
1533 unsigned short max_mtu = header_size + data_size_max;
1534 unsigned short data_size_align_mask = data_size_align - 1;
1535 int mtu_idx, aligned_mtu_idx;
1536
1537 /* Scan the MTU Table till we find an MTU which is larger than our
1538 * Maximum MTU or we reach the end of the table. Along the way,
1539 * record the last MTU found, if any, which will result in a Data
1540 * Segment Length matching the requested alignment.
1541 */
1542 for (mtu_idx = 0, aligned_mtu_idx = -1; mtu_idx < NMTUS; mtu_idx++) {
1543 unsigned short data_size = mtus[mtu_idx] - header_size;
1544
1545 /* If this MTU minus the Header Size would result in a
1546 * Data Segment Size of the desired alignment, remember it.
1547 */
1548 if ((data_size & data_size_align_mask) == 0)
1549 aligned_mtu_idx = mtu_idx;
1550
1551 /* If we're not at the end of the Hardware MTU Table and the
1552 * next element is larger than our Maximum MTU, drop out of
1553 * the loop.
1554 */
1555 if (mtu_idx+1 < NMTUS && mtus[mtu_idx+1] > max_mtu)
1556 break;
1557 }
1558
1559 /* If we fell out of the loop because we ran to the end of the table,
1560 * then we just have to use the last [largest] entry.
1561 */
1562 if (mtu_idx == NMTUS)
1563 mtu_idx--;
1564
1565 /* If we found an MTU which resulted in the requested Data Segment
1566 * Length alignment and that's "not far" from the largest MTU which is
1567 * less than or equal to the maximum MTU, then use that.
1568 */
1569 if (aligned_mtu_idx >= 0 &&
1570 mtu_idx - aligned_mtu_idx <= 1)
1571 mtu_idx = aligned_mtu_idx;
1572
1573 /* If the caller has passed in an MTU Index pointer, pass the
1574 * MTU Index back. Return the MTU value.
1575 */
1576 if (mtu_idxp)
1577 *mtu_idxp = mtu_idx;
1578 return mtus[mtu_idx];
1579}
1580EXPORT_SYMBOL(cxgb4_best_aligned_mtu);
1581
27999805
H
1582/**
1583 * cxgb4_tp_smt_idx - Get the Source Mac Table index for this VI
1584 * @chip: chip type
1585 * @viid: VI id of the given port
1586 *
1587 * Return the SMT index for this VI.
1588 */
1589unsigned int cxgb4_tp_smt_idx(enum chip_type chip, unsigned int viid)
1590{
1591 /* In T4/T5, SMT contains 256 SMAC entries organized in
1592 * 128 rows of 2 entries each.
1593 * In T6, SMT contains 256 SMAC entries in 256 rows.
1594 * TODO: The below code needs to be updated when we add support
1595 * for 256 VFs.
1596 */
1597 if (CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5)
1598 return ((viid & 0x7f) << 1);
1599 else
1600 return (viid & 0x7f);
1601}
1602EXPORT_SYMBOL(cxgb4_tp_smt_idx);
1603
b8ff05a9
DM
1604/**
1605 * cxgb4_port_chan - get the HW channel of a port
1606 * @dev: the net device for the port
1607 *
1608 * Return the HW Tx channel of the given port.
1609 */
1610unsigned int cxgb4_port_chan(const struct net_device *dev)
1611{
1612 return netdev2pinfo(dev)->tx_chan;
1613}
1614EXPORT_SYMBOL(cxgb4_port_chan);
1615
881806bc
VP
1616unsigned int cxgb4_dbfifo_count(const struct net_device *dev, int lpfifo)
1617{
1618 struct adapter *adap = netdev2adap(dev);
2cc301d2 1619 u32 v1, v2, lp_count, hp_count;
881806bc 1620
f061de42
HS
1621 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1622 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
d14807dd 1623 if (is_t4(adap->params.chip)) {
f061de42
HS
1624 lp_count = LP_COUNT_G(v1);
1625 hp_count = HP_COUNT_G(v1);
2cc301d2 1626 } else {
f061de42
HS
1627 lp_count = LP_COUNT_T5_G(v1);
1628 hp_count = HP_COUNT_T5_G(v2);
2cc301d2
SR
1629 }
1630 return lpfifo ? lp_count : hp_count;
881806bc
VP
1631}
1632EXPORT_SYMBOL(cxgb4_dbfifo_count);
1633
b8ff05a9
DM
1634/**
1635 * cxgb4_port_viid - get the VI id of a port
1636 * @dev: the net device for the port
1637 *
1638 * Return the VI id of the given port.
1639 */
1640unsigned int cxgb4_port_viid(const struct net_device *dev)
1641{
1642 return netdev2pinfo(dev)->viid;
1643}
1644EXPORT_SYMBOL(cxgb4_port_viid);
1645
1646/**
1647 * cxgb4_port_idx - get the index of a port
1648 * @dev: the net device for the port
1649 *
1650 * Return the index of the given port.
1651 */
1652unsigned int cxgb4_port_idx(const struct net_device *dev)
1653{
1654 return netdev2pinfo(dev)->port_id;
1655}
1656EXPORT_SYMBOL(cxgb4_port_idx);
1657
b8ff05a9
DM
1658void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
1659 struct tp_tcp_stats *v6)
1660{
1661 struct adapter *adap = pci_get_drvdata(pdev);
1662
1663 spin_lock(&adap->stats_lock);
1664 t4_tp_get_tcp_stats(adap, v4, v6);
1665 spin_unlock(&adap->stats_lock);
1666}
1667EXPORT_SYMBOL(cxgb4_get_tcp_stats);
1668
1669void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
1670 const unsigned int *pgsz_order)
1671{
1672 struct adapter *adap = netdev2adap(dev);
1673
0d804338
HS
1674 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK_A, tag_mask);
1675 t4_write_reg(adap, ULP_RX_ISCSI_PSZ_A, HPZ0_V(pgsz_order[0]) |
1676 HPZ1_V(pgsz_order[1]) | HPZ2_V(pgsz_order[2]) |
1677 HPZ3_V(pgsz_order[3]));
b8ff05a9
DM
1678}
1679EXPORT_SYMBOL(cxgb4_iscsi_init);
1680
3069ee9b
VP
1681int cxgb4_flush_eq_cache(struct net_device *dev)
1682{
1683 struct adapter *adap = netdev2adap(dev);
3069ee9b 1684
5d700ecb 1685 return t4_sge_ctxt_flush(adap, adap->mbox);
3069ee9b
VP
1686}
1687EXPORT_SYMBOL(cxgb4_flush_eq_cache);
1688
1689static int read_eq_indices(struct adapter *adap, u16 qid, u16 *pidx, u16 *cidx)
1690{
f061de42 1691 u32 addr = t4_read_reg(adap, SGE_DBQ_CTXT_BADDR_A) + 24 * qid + 8;
3069ee9b
VP
1692 __be64 indices;
1693 int ret;
1694
fc5ab020
HS
1695 spin_lock(&adap->win0_lock);
1696 ret = t4_memory_rw(adap, 0, MEM_EDC0, addr,
1697 sizeof(indices), (__be32 *)&indices,
1698 T4_MEMORY_READ);
1699 spin_unlock(&adap->win0_lock);
3069ee9b 1700 if (!ret) {
404d9e3f
VP
1701 *cidx = (be64_to_cpu(indices) >> 25) & 0xffff;
1702 *pidx = (be64_to_cpu(indices) >> 9) & 0xffff;
3069ee9b
VP
1703 }
1704 return ret;
1705}
1706
1707int cxgb4_sync_txq_pidx(struct net_device *dev, u16 qid, u16 pidx,
1708 u16 size)
1709{
1710 struct adapter *adap = netdev2adap(dev);
1711 u16 hw_pidx, hw_cidx;
1712 int ret;
1713
1714 ret = read_eq_indices(adap, qid, &hw_pidx, &hw_cidx);
1715 if (ret)
1716 goto out;
1717
1718 if (pidx != hw_pidx) {
1719 u16 delta;
f612b815 1720 u32 val;
3069ee9b
VP
1721
1722 if (pidx >= hw_pidx)
1723 delta = pidx - hw_pidx;
1724 else
1725 delta = size - hw_pidx + pidx;
f612b815
HS
1726
1727 if (is_t4(adap->params.chip))
1728 val = PIDX_V(delta);
1729 else
1730 val = PIDX_T5_V(delta);
3069ee9b 1731 wmb();
f612b815
HS
1732 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1733 QID_V(qid) | val);
3069ee9b
VP
1734 }
1735out:
1736 return ret;
1737}
1738EXPORT_SYMBOL(cxgb4_sync_txq_pidx);
1739
031cf476
HS
1740int cxgb4_read_tpte(struct net_device *dev, u32 stag, __be32 *tpte)
1741{
1742 struct adapter *adap;
1743 u32 offset, memtype, memaddr;
6559a7e8 1744 u32 edc0_size, edc1_size, mc0_size, mc1_size, size;
031cf476
HS
1745 u32 edc0_end, edc1_end, mc0_end, mc1_end;
1746 int ret;
1747
1748 adap = netdev2adap(dev);
1749
1750 offset = ((stag >> 8) * 32) + adap->vres.stag.start;
1751
1752 /* Figure out where the offset lands in the Memory Type/Address scheme.
1753 * This code assumes that the memory is laid out starting at offset 0
1754 * with no breaks as: EDC0, EDC1, MC0, MC1. All cards have both EDC0
1755 * and EDC1. Some cards will have neither MC0 nor MC1, most cards have
1756 * MC0, and some have both MC0 and MC1.
1757 */
6559a7e8
HS
1758 size = t4_read_reg(adap, MA_EDRAM0_BAR_A);
1759 edc0_size = EDRAM0_SIZE_G(size) << 20;
1760 size = t4_read_reg(adap, MA_EDRAM1_BAR_A);
1761 edc1_size = EDRAM1_SIZE_G(size) << 20;
1762 size = t4_read_reg(adap, MA_EXT_MEMORY0_BAR_A);
1763 mc0_size = EXT_MEM0_SIZE_G(size) << 20;
031cf476
HS
1764
1765 edc0_end = edc0_size;
1766 edc1_end = edc0_end + edc1_size;
1767 mc0_end = edc1_end + mc0_size;
1768
1769 if (offset < edc0_end) {
1770 memtype = MEM_EDC0;
1771 memaddr = offset;
1772 } else if (offset < edc1_end) {
1773 memtype = MEM_EDC1;
1774 memaddr = offset - edc0_end;
1775 } else {
1776 if (offset < mc0_end) {
1777 memtype = MEM_MC0;
1778 memaddr = offset - edc1_end;
3ccc6cf7 1779 } else if (is_t5(adap->params.chip)) {
6559a7e8
HS
1780 size = t4_read_reg(adap, MA_EXT_MEMORY1_BAR_A);
1781 mc1_size = EXT_MEM1_SIZE_G(size) << 20;
031cf476
HS
1782 mc1_end = mc0_end + mc1_size;
1783 if (offset < mc1_end) {
1784 memtype = MEM_MC1;
1785 memaddr = offset - mc0_end;
1786 } else {
1787 /* offset beyond the end of any memory */
1788 goto err;
1789 }
3ccc6cf7
HS
1790 } else {
1791 /* T4/T6 only has a single memory channel */
1792 goto err;
031cf476
HS
1793 }
1794 }
1795
1796 spin_lock(&adap->win0_lock);
1797 ret = t4_memory_rw(adap, 0, memtype, memaddr, 32, tpte, T4_MEMORY_READ);
1798 spin_unlock(&adap->win0_lock);
1799 return ret;
1800
1801err:
1802 dev_err(adap->pdev_dev, "stag %#x, offset %#x out of range\n",
1803 stag, offset);
1804 return -EINVAL;
1805}
1806EXPORT_SYMBOL(cxgb4_read_tpte);
1807
7730b4c7
HS
1808u64 cxgb4_read_sge_timestamp(struct net_device *dev)
1809{
1810 u32 hi, lo;
1811 struct adapter *adap;
1812
1813 adap = netdev2adap(dev);
f612b815
HS
1814 lo = t4_read_reg(adap, SGE_TIMESTAMP_LO_A);
1815 hi = TSVAL_G(t4_read_reg(adap, SGE_TIMESTAMP_HI_A));
7730b4c7
HS
1816
1817 return ((u64)hi << 32) | (u64)lo;
1818}
1819EXPORT_SYMBOL(cxgb4_read_sge_timestamp);
1820
df64e4d3
HS
1821int cxgb4_bar2_sge_qregs(struct net_device *dev,
1822 unsigned int qid,
1823 enum cxgb4_bar2_qtype qtype,
66cf188e 1824 int user,
df64e4d3
HS
1825 u64 *pbar2_qoffset,
1826 unsigned int *pbar2_qid)
1827{
b2612722 1828 return t4_bar2_sge_qregs(netdev2adap(dev),
df64e4d3
HS
1829 qid,
1830 (qtype == CXGB4_BAR2_QTYPE_EGRESS
1831 ? T4_BAR2_QTYPE_EGRESS
1832 : T4_BAR2_QTYPE_INGRESS),
66cf188e 1833 user,
df64e4d3
HS
1834 pbar2_qoffset,
1835 pbar2_qid);
1836}
1837EXPORT_SYMBOL(cxgb4_bar2_sge_qregs);
1838
b8ff05a9
DM
1839static struct pci_driver cxgb4_driver;
1840
1841static void check_neigh_update(struct neighbour *neigh)
1842{
1843 const struct device *parent;
1844 const struct net_device *netdev = neigh->dev;
1845
1846 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1847 netdev = vlan_dev_real_dev(netdev);
1848 parent = netdev->dev.parent;
1849 if (parent && parent->driver == &cxgb4_driver.driver)
1850 t4_l2t_update(dev_get_drvdata(parent), neigh);
1851}
1852
1853static int netevent_cb(struct notifier_block *nb, unsigned long event,
1854 void *data)
1855{
1856 switch (event) {
1857 case NETEVENT_NEIGH_UPDATE:
1858 check_neigh_update(data);
1859 break;
b8ff05a9
DM
1860 case NETEVENT_REDIRECT:
1861 default:
1862 break;
1863 }
1864 return 0;
1865}
1866
1867static bool netevent_registered;
1868static struct notifier_block cxgb4_netevent_nb = {
1869 .notifier_call = netevent_cb
1870};
1871
3069ee9b
VP
1872static void drain_db_fifo(struct adapter *adap, int usecs)
1873{
2cc301d2 1874 u32 v1, v2, lp_count, hp_count;
3069ee9b
VP
1875
1876 do {
f061de42
HS
1877 v1 = t4_read_reg(adap, SGE_DBFIFO_STATUS_A);
1878 v2 = t4_read_reg(adap, SGE_DBFIFO_STATUS2_A);
d14807dd 1879 if (is_t4(adap->params.chip)) {
f061de42
HS
1880 lp_count = LP_COUNT_G(v1);
1881 hp_count = HP_COUNT_G(v1);
2cc301d2 1882 } else {
f061de42
HS
1883 lp_count = LP_COUNT_T5_G(v1);
1884 hp_count = HP_COUNT_T5_G(v2);
2cc301d2
SR
1885 }
1886
1887 if (lp_count == 0 && hp_count == 0)
1888 break;
3069ee9b
VP
1889 set_current_state(TASK_UNINTERRUPTIBLE);
1890 schedule_timeout(usecs_to_jiffies(usecs));
3069ee9b
VP
1891 } while (1);
1892}
1893
1894static void disable_txq_db(struct sge_txq *q)
1895{
05eb2389
SW
1896 unsigned long flags;
1897
1898 spin_lock_irqsave(&q->db_lock, flags);
3069ee9b 1899 q->db_disabled = 1;
05eb2389 1900 spin_unlock_irqrestore(&q->db_lock, flags);
3069ee9b
VP
1901}
1902
05eb2389 1903static void enable_txq_db(struct adapter *adap, struct sge_txq *q)
3069ee9b
VP
1904{
1905 spin_lock_irq(&q->db_lock);
05eb2389
SW
1906 if (q->db_pidx_inc) {
1907 /* Make sure that all writes to the TX descriptors
1908 * are committed before we tell HW about them.
1909 */
1910 wmb();
f612b815
HS
1911 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1912 QID_V(q->cntxt_id) | PIDX_V(q->db_pidx_inc));
05eb2389
SW
1913 q->db_pidx_inc = 0;
1914 }
3069ee9b
VP
1915 q->db_disabled = 0;
1916 spin_unlock_irq(&q->db_lock);
1917}
1918
1919static void disable_dbs(struct adapter *adap)
1920{
1921 int i;
1922
1923 for_each_ethrxq(&adap->sge, i)
1924 disable_txq_db(&adap->sge.ethtxq[i].q);
0fbc81b3 1925 for_each_ofldtxq(&adap->sge, i)
3069ee9b
VP
1926 disable_txq_db(&adap->sge.ofldtxq[i].q);
1927 for_each_port(adap, i)
1928 disable_txq_db(&adap->sge.ctrlq[i].q);
1929}
1930
1931static void enable_dbs(struct adapter *adap)
1932{
1933 int i;
1934
1935 for_each_ethrxq(&adap->sge, i)
05eb2389 1936 enable_txq_db(adap, &adap->sge.ethtxq[i].q);
0fbc81b3 1937 for_each_ofldtxq(&adap->sge, i)
05eb2389 1938 enable_txq_db(adap, &adap->sge.ofldtxq[i].q);
3069ee9b 1939 for_each_port(adap, i)
05eb2389
SW
1940 enable_txq_db(adap, &adap->sge.ctrlq[i].q);
1941}
1942
1943static void notify_rdma_uld(struct adapter *adap, enum cxgb4_control cmd)
1944{
0fbc81b3
HS
1945 enum cxgb4_uld type = CXGB4_ULD_RDMA;
1946
1947 if (adap->uld && adap->uld[type].handle)
1948 adap->uld[type].control(adap->uld[type].handle, cmd);
05eb2389
SW
1949}
1950
1951static void process_db_full(struct work_struct *work)
1952{
1953 struct adapter *adap;
1954
1955 adap = container_of(work, struct adapter, db_full_task);
1956
1957 drain_db_fifo(adap, dbfifo_drain_delay);
1958 enable_dbs(adap);
1959 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
3ccc6cf7
HS
1960 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
1961 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1962 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F,
1963 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F);
1964 else
1965 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
1966 DBFIFO_LP_INT_F, DBFIFO_LP_INT_F);
3069ee9b
VP
1967}
1968
1969static void sync_txq_pidx(struct adapter *adap, struct sge_txq *q)
1970{
1971 u16 hw_pidx, hw_cidx;
1972 int ret;
1973
05eb2389 1974 spin_lock_irq(&q->db_lock);
3069ee9b
VP
1975 ret = read_eq_indices(adap, (u16)q->cntxt_id, &hw_pidx, &hw_cidx);
1976 if (ret)
1977 goto out;
1978 if (q->db_pidx != hw_pidx) {
1979 u16 delta;
f612b815 1980 u32 val;
3069ee9b
VP
1981
1982 if (q->db_pidx >= hw_pidx)
1983 delta = q->db_pidx - hw_pidx;
1984 else
1985 delta = q->size - hw_pidx + q->db_pidx;
f612b815
HS
1986
1987 if (is_t4(adap->params.chip))
1988 val = PIDX_V(delta);
1989 else
1990 val = PIDX_T5_V(delta);
3069ee9b 1991 wmb();
f612b815
HS
1992 t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL_A),
1993 QID_V(q->cntxt_id) | val);
3069ee9b
VP
1994 }
1995out:
1996 q->db_disabled = 0;
05eb2389
SW
1997 q->db_pidx_inc = 0;
1998 spin_unlock_irq(&q->db_lock);
3069ee9b
VP
1999 if (ret)
2000 CH_WARN(adap, "DB drop recovery failed.\n");
2001}
0fbc81b3 2002
3069ee9b
VP
2003static void recover_all_queues(struct adapter *adap)
2004{
2005 int i;
2006
2007 for_each_ethrxq(&adap->sge, i)
2008 sync_txq_pidx(adap, &adap->sge.ethtxq[i].q);
0fbc81b3 2009 for_each_ofldtxq(&adap->sge, i)
3069ee9b
VP
2010 sync_txq_pidx(adap, &adap->sge.ofldtxq[i].q);
2011 for_each_port(adap, i)
2012 sync_txq_pidx(adap, &adap->sge.ctrlq[i].q);
2013}
2014
881806bc
VP
2015static void process_db_drop(struct work_struct *work)
2016{
2017 struct adapter *adap;
881806bc 2018
3069ee9b 2019 adap = container_of(work, struct adapter, db_drop_task);
881806bc 2020
d14807dd 2021 if (is_t4(adap->params.chip)) {
05eb2389 2022 drain_db_fifo(adap, dbfifo_drain_delay);
2cc301d2 2023 notify_rdma_uld(adap, CXGB4_CONTROL_DB_DROP);
05eb2389 2024 drain_db_fifo(adap, dbfifo_drain_delay);
2cc301d2 2025 recover_all_queues(adap);
05eb2389 2026 drain_db_fifo(adap, dbfifo_drain_delay);
2cc301d2 2027 enable_dbs(adap);
05eb2389 2028 notify_rdma_uld(adap, CXGB4_CONTROL_DB_EMPTY);
3ccc6cf7 2029 } else if (is_t5(adap->params.chip)) {
2cc301d2
SR
2030 u32 dropped_db = t4_read_reg(adap, 0x010ac);
2031 u16 qid = (dropped_db >> 15) & 0x1ffff;
2032 u16 pidx_inc = dropped_db & 0x1fff;
df64e4d3
HS
2033 u64 bar2_qoffset;
2034 unsigned int bar2_qid;
2035 int ret;
2cc301d2 2036
b2612722 2037 ret = t4_bar2_sge_qregs(adap, qid, T4_BAR2_QTYPE_EGRESS,
e0456717 2038 0, &bar2_qoffset, &bar2_qid);
df64e4d3
HS
2039 if (ret)
2040 dev_err(adap->pdev_dev, "doorbell drop recovery: "
2041 "qid=%d, pidx_inc=%d\n", qid, pidx_inc);
2042 else
f612b815 2043 writel(PIDX_T5_V(pidx_inc) | QID_V(bar2_qid),
df64e4d3 2044 adap->bar2 + bar2_qoffset + SGE_UDB_KDOORBELL);
2cc301d2
SR
2045
2046 /* Re-enable BAR2 WC */
2047 t4_set_reg_field(adap, 0x10b0, 1<<15, 1<<15);
2048 }
2049
3ccc6cf7
HS
2050 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5)
2051 t4_set_reg_field(adap, SGE_DOORBELL_CONTROL_A, DROPPED_DB_F, 0);
881806bc
VP
2052}
2053
2054void t4_db_full(struct adapter *adap)
2055{
d14807dd 2056 if (is_t4(adap->params.chip)) {
05eb2389
SW
2057 disable_dbs(adap);
2058 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
f612b815
HS
2059 t4_set_reg_field(adap, SGE_INT_ENABLE3_A,
2060 DBFIFO_HP_INT_F | DBFIFO_LP_INT_F, 0);
29aaee65 2061 queue_work(adap->workq, &adap->db_full_task);
2cc301d2 2062 }
881806bc
VP
2063}
2064
2065void t4_db_dropped(struct adapter *adap)
2066{
05eb2389
SW
2067 if (is_t4(adap->params.chip)) {
2068 disable_dbs(adap);
2069 notify_rdma_uld(adap, CXGB4_CONTROL_DB_FULL);
2070 }
29aaee65 2071 queue_work(adap->workq, &adap->db_drop_task);
881806bc
VP
2072}
2073
0fbc81b3
HS
2074void t4_register_netevent_notifier(void)
2075{
b8ff05a9
DM
2076 if (!netevent_registered) {
2077 register_netevent_notifier(&cxgb4_netevent_nb);
2078 netevent_registered = true;
2079 }
b8ff05a9
DM
2080}
2081
2082static void detach_ulds(struct adapter *adap)
2083{
2084 unsigned int i;
2085
2086 mutex_lock(&uld_mutex);
2087 list_del(&adap->list_node);
2088 for (i = 0; i < CXGB4_ULD_MAX; i++)
94cdb8bb
HS
2089 if (adap->uld && adap->uld[i].handle) {
2090 adap->uld[i].state_change(adap->uld[i].handle,
2091 CXGB4_STATE_DETACH);
2092 adap->uld[i].handle = NULL;
2093 }
b8ff05a9
DM
2094 if (netevent_registered && list_empty(&adapter_list)) {
2095 unregister_netevent_notifier(&cxgb4_netevent_nb);
2096 netevent_registered = false;
2097 }
2098 mutex_unlock(&uld_mutex);
2099}
2100
2101static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2102{
2103 unsigned int i;
2104
2105 mutex_lock(&uld_mutex);
2106 for (i = 0; i < CXGB4_ULD_MAX; i++)
94cdb8bb
HS
2107 if (adap->uld && adap->uld[i].handle)
2108 adap->uld[i].state_change(adap->uld[i].handle,
2109 new_state);
b8ff05a9
DM
2110 mutex_unlock(&uld_mutex);
2111}
2112
1bb60376 2113#if IS_ENABLED(CONFIG_IPV6)
b5a02f50
AB
2114static int cxgb4_inet6addr_handler(struct notifier_block *this,
2115 unsigned long event, void *data)
01bcca68 2116{
b5a02f50
AB
2117 struct inet6_ifaddr *ifa = data;
2118 struct net_device *event_dev = ifa->idev->dev;
2119 const struct device *parent = NULL;
2120#if IS_ENABLED(CONFIG_BONDING)
01bcca68 2121 struct adapter *adap;
b5a02f50
AB
2122#endif
2123 if (event_dev->priv_flags & IFF_802_1Q_VLAN)
2124 event_dev = vlan_dev_real_dev(event_dev);
2125#if IS_ENABLED(CONFIG_BONDING)
2126 if (event_dev->flags & IFF_MASTER) {
2127 list_for_each_entry(adap, &adapter_list, list_node) {
2128 switch (event) {
2129 case NETDEV_UP:
2130 cxgb4_clip_get(adap->port[0],
2131 (const u32 *)ifa, 1);
2132 break;
2133 case NETDEV_DOWN:
2134 cxgb4_clip_release(adap->port[0],
2135 (const u32 *)ifa, 1);
2136 break;
2137 default:
2138 break;
2139 }
2140 }
2141 return NOTIFY_OK;
2142 }
2143#endif
01bcca68 2144
b5a02f50
AB
2145 if (event_dev)
2146 parent = event_dev->dev.parent;
01bcca68 2147
b5a02f50 2148 if (parent && parent->driver == &cxgb4_driver.driver) {
01bcca68
VP
2149 switch (event) {
2150 case NETDEV_UP:
b5a02f50 2151 cxgb4_clip_get(event_dev, (const u32 *)ifa, 1);
01bcca68
VP
2152 break;
2153 case NETDEV_DOWN:
b5a02f50 2154 cxgb4_clip_release(event_dev, (const u32 *)ifa, 1);
01bcca68
VP
2155 break;
2156 default:
2157 break;
2158 }
2159 }
b5a02f50 2160 return NOTIFY_OK;
01bcca68
VP
2161}
2162
b5a02f50 2163static bool inet6addr_registered;
01bcca68
VP
2164static struct notifier_block cxgb4_inet6addr_notifier = {
2165 .notifier_call = cxgb4_inet6addr_handler
2166};
2167
01bcca68
VP
2168static void update_clip(const struct adapter *adap)
2169{
2170 int i;
2171 struct net_device *dev;
2172 int ret;
2173
2174 rcu_read_lock();
2175
2176 for (i = 0; i < MAX_NPORTS; i++) {
2177 dev = adap->port[i];
2178 ret = 0;
2179
2180 if (dev)
b5a02f50 2181 ret = cxgb4_update_root_dev_clip(dev);
01bcca68
VP
2182
2183 if (ret < 0)
2184 break;
2185 }
2186 rcu_read_unlock();
2187}
1bb60376 2188#endif /* IS_ENABLED(CONFIG_IPV6) */
01bcca68 2189
b8ff05a9
DM
2190/**
2191 * cxgb_up - enable the adapter
2192 * @adap: adapter being enabled
2193 *
2194 * Called when the first port is enabled, this function performs the
2195 * actions necessary to make an adapter operational, such as completing
2196 * the initialization of HW modules, and enabling interrupts.
2197 *
2198 * Must be called with the rtnl lock held.
2199 */
2200static int cxgb_up(struct adapter *adap)
2201{
aaefae9b 2202 int err;
b8ff05a9 2203
aaefae9b
DM
2204 err = setup_sge_queues(adap);
2205 if (err)
2206 goto out;
2207 err = setup_rss(adap);
2208 if (err)
2209 goto freeq;
b8ff05a9
DM
2210
2211 if (adap->flags & USING_MSIX) {
aaefae9b 2212 name_msix_vecs(adap);
b8ff05a9
DM
2213 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2214 adap->msix_info[0].desc, adap);
2215 if (err)
2216 goto irq_err;
b8ff05a9
DM
2217 err = request_msix_queue_irqs(adap);
2218 if (err) {
2219 free_irq(adap->msix_info[0].vec, adap);
2220 goto irq_err;
2221 }
2222 } else {
2223 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2224 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
b1a3c2b6 2225 adap->port[0]->name, adap);
b8ff05a9
DM
2226 if (err)
2227 goto irq_err;
2228 }
2229 enable_rx(adap);
2230 t4_sge_start(adap);
2231 t4_intr_enable(adap);
aaefae9b 2232 adap->flags |= FULL_INIT_DONE;
b8ff05a9 2233 notify_ulds(adap, CXGB4_STATE_UP);
1bb60376 2234#if IS_ENABLED(CONFIG_IPV6)
01bcca68 2235 update_clip(adap);
1bb60376 2236#endif
fc08a01a
HS
2237 /* Initialize hash mac addr list*/
2238 INIT_LIST_HEAD(&adap->mac_hlist);
b8ff05a9
DM
2239 out:
2240 return err;
2241 irq_err:
2242 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
aaefae9b
DM
2243 freeq:
2244 t4_free_sge_resources(adap);
b8ff05a9
DM
2245 goto out;
2246}
2247
2248static void cxgb_down(struct adapter *adapter)
2249{
b8ff05a9 2250 cancel_work_sync(&adapter->tid_release_task);
881806bc
VP
2251 cancel_work_sync(&adapter->db_full_task);
2252 cancel_work_sync(&adapter->db_drop_task);
b8ff05a9 2253 adapter->tid_release_task_busy = false;
204dc3c0 2254 adapter->tid_release_head = NULL;
b8ff05a9 2255
aaefae9b
DM
2256 t4_sge_stop(adapter);
2257 t4_free_sge_resources(adapter);
2258 adapter->flags &= ~FULL_INIT_DONE;
b8ff05a9
DM
2259}
2260
2261/*
2262 * net_device operations
2263 */
2264static int cxgb_open(struct net_device *dev)
2265{
2266 int err;
2267 struct port_info *pi = netdev_priv(dev);
2268 struct adapter *adapter = pi->adapter;
2269
6a3c869a
DM
2270 netif_carrier_off(dev);
2271
aaefae9b
DM
2272 if (!(adapter->flags & FULL_INIT_DONE)) {
2273 err = cxgb_up(adapter);
2274 if (err < 0)
2275 return err;
2276 }
b8ff05a9 2277
f68707b8
DM
2278 err = link_start(dev);
2279 if (!err)
2280 netif_tx_start_all_queues(dev);
2281 return err;
b8ff05a9
DM
2282}
2283
2284static int cxgb_close(struct net_device *dev)
2285{
b8ff05a9
DM
2286 struct port_info *pi = netdev_priv(dev);
2287 struct adapter *adapter = pi->adapter;
2288
2289 netif_tx_stop_all_queues(dev);
2290 netif_carrier_off(dev);
b2612722 2291 return t4_enable_vi(adapter, adapter->pf, pi->viid, false, false);
b8ff05a9
DM
2292}
2293
dca4faeb 2294int cxgb4_create_server_filter(const struct net_device *dev, unsigned int stid,
793dad94
VP
2295 __be32 sip, __be16 sport, __be16 vlan,
2296 unsigned int queue, unsigned char port, unsigned char mask)
dca4faeb
VP
2297{
2298 int ret;
2299 struct filter_entry *f;
2300 struct adapter *adap;
2301 int i;
2302 u8 *val;
2303
2304 adap = netdev2adap(dev);
2305
1cab775c 2306 /* Adjust stid to correct filter index */
470c60c4 2307 stid -= adap->tids.sftid_base;
1cab775c
VP
2308 stid += adap->tids.nftids;
2309
dca4faeb
VP
2310 /* Check to make sure the filter requested is writable ...
2311 */
2312 f = &adap->tids.ftid_tab[stid];
2313 ret = writable_filter(f);
2314 if (ret)
2315 return ret;
2316
2317 /* Clear out any old resources being used by the filter before
2318 * we start constructing the new filter.
2319 */
2320 if (f->valid)
2321 clear_filter(adap, f);
2322
2323 /* Clear out filter specifications */
2324 memset(&f->fs, 0, sizeof(struct ch_filter_specification));
2325 f->fs.val.lport = cpu_to_be16(sport);
2326 f->fs.mask.lport = ~0;
2327 val = (u8 *)&sip;
793dad94 2328 if ((val[0] | val[1] | val[2] | val[3]) != 0) {
dca4faeb
VP
2329 for (i = 0; i < 4; i++) {
2330 f->fs.val.lip[i] = val[i];
2331 f->fs.mask.lip[i] = ~0;
2332 }
0d804338 2333 if (adap->params.tp.vlan_pri_map & PORT_F) {
793dad94
VP
2334 f->fs.val.iport = port;
2335 f->fs.mask.iport = mask;
2336 }
2337 }
dca4faeb 2338
0d804338 2339 if (adap->params.tp.vlan_pri_map & PROTOCOL_F) {
7c89e555
KS
2340 f->fs.val.proto = IPPROTO_TCP;
2341 f->fs.mask.proto = ~0;
2342 }
2343
dca4faeb
VP
2344 f->fs.dirsteer = 1;
2345 f->fs.iq = queue;
2346 /* Mark filter as locked */
2347 f->locked = 1;
2348 f->fs.rpttid = 1;
2349
2350 ret = set_filter_wr(adap, stid);
2351 if (ret) {
2352 clear_filter(adap, f);
2353 return ret;
2354 }
2355
2356 return 0;
2357}
2358EXPORT_SYMBOL(cxgb4_create_server_filter);
2359
2360int cxgb4_remove_server_filter(const struct net_device *dev, unsigned int stid,
2361 unsigned int queue, bool ipv6)
2362{
dca4faeb
VP
2363 struct filter_entry *f;
2364 struct adapter *adap;
2365
2366 adap = netdev2adap(dev);
1cab775c
VP
2367
2368 /* Adjust stid to correct filter index */
470c60c4 2369 stid -= adap->tids.sftid_base;
1cab775c
VP
2370 stid += adap->tids.nftids;
2371
dca4faeb
VP
2372 f = &adap->tids.ftid_tab[stid];
2373 /* Unlock the filter */
2374 f->locked = 0;
2375
8c14846d 2376 return delete_filter(adap, stid);
dca4faeb
VP
2377}
2378EXPORT_SYMBOL(cxgb4_remove_server_filter);
2379
f5152c90
DM
2380static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2381 struct rtnl_link_stats64 *ns)
b8ff05a9
DM
2382{
2383 struct port_stats stats;
2384 struct port_info *p = netdev_priv(dev);
2385 struct adapter *adapter = p->adapter;
b8ff05a9 2386
9fe6cb58
GS
2387 /* Block retrieving statistics during EEH error
2388 * recovery. Otherwise, the recovery might fail
2389 * and the PCI device will be removed permanently
2390 */
b8ff05a9 2391 spin_lock(&adapter->stats_lock);
9fe6cb58
GS
2392 if (!netif_device_present(dev)) {
2393 spin_unlock(&adapter->stats_lock);
2394 return ns;
2395 }
a4cfd929
HS
2396 t4_get_port_stats_offset(adapter, p->tx_chan, &stats,
2397 &p->stats_base);
b8ff05a9
DM
2398 spin_unlock(&adapter->stats_lock);
2399
2400 ns->tx_bytes = stats.tx_octets;
2401 ns->tx_packets = stats.tx_frames;
2402 ns->rx_bytes = stats.rx_octets;
2403 ns->rx_packets = stats.rx_frames;
2404 ns->multicast = stats.rx_mcast_frames;
2405
2406 /* detailed rx_errors */
2407 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2408 stats.rx_runt;
2409 ns->rx_over_errors = 0;
2410 ns->rx_crc_errors = stats.rx_fcs_err;
2411 ns->rx_frame_errors = stats.rx_symbol_err;
2412 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2413 stats.rx_ovflow2 + stats.rx_ovflow3 +
2414 stats.rx_trunc0 + stats.rx_trunc1 +
2415 stats.rx_trunc2 + stats.rx_trunc3;
2416 ns->rx_missed_errors = 0;
2417
2418 /* detailed tx_errors */
2419 ns->tx_aborted_errors = 0;
2420 ns->tx_carrier_errors = 0;
2421 ns->tx_fifo_errors = 0;
2422 ns->tx_heartbeat_errors = 0;
2423 ns->tx_window_errors = 0;
2424
2425 ns->tx_errors = stats.tx_error_frames;
2426 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2427 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2428 return ns;
2429}
2430
2431static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2432{
060e0c75 2433 unsigned int mbox;
b8ff05a9
DM
2434 int ret = 0, prtad, devad;
2435 struct port_info *pi = netdev_priv(dev);
2436 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2437
2438 switch (cmd) {
2439 case SIOCGMIIPHY:
2440 if (pi->mdio_addr < 0)
2441 return -EOPNOTSUPP;
2442 data->phy_id = pi->mdio_addr;
2443 break;
2444 case SIOCGMIIREG:
2445 case SIOCSMIIREG:
2446 if (mdio_phy_id_is_c45(data->phy_id)) {
2447 prtad = mdio_phy_id_prtad(data->phy_id);
2448 devad = mdio_phy_id_devad(data->phy_id);
2449 } else if (data->phy_id < 32) {
2450 prtad = data->phy_id;
2451 devad = 0;
2452 data->reg_num &= 0x1f;
2453 } else
2454 return -EINVAL;
2455
b2612722 2456 mbox = pi->adapter->pf;
b8ff05a9 2457 if (cmd == SIOCGMIIREG)
060e0c75 2458 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
b8ff05a9
DM
2459 data->reg_num, &data->val_out);
2460 else
060e0c75 2461 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
b8ff05a9
DM
2462 data->reg_num, data->val_in);
2463 break;
5e2a5ebc
HS
2464 case SIOCGHWTSTAMP:
2465 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2466 sizeof(pi->tstamp_config)) ?
2467 -EFAULT : 0;
2468 case SIOCSHWTSTAMP:
2469 if (copy_from_user(&pi->tstamp_config, req->ifr_data,
2470 sizeof(pi->tstamp_config)))
2471 return -EFAULT;
2472
2473 switch (pi->tstamp_config.rx_filter) {
2474 case HWTSTAMP_FILTER_NONE:
2475 pi->rxtstamp = false;
2476 break;
2477 case HWTSTAMP_FILTER_ALL:
2478 pi->rxtstamp = true;
2479 break;
2480 default:
2481 pi->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2482 return -ERANGE;
2483 }
2484
2485 return copy_to_user(req->ifr_data, &pi->tstamp_config,
2486 sizeof(pi->tstamp_config)) ?
2487 -EFAULT : 0;
b8ff05a9
DM
2488 default:
2489 return -EOPNOTSUPP;
2490 }
2491 return ret;
2492}
2493
2494static void cxgb_set_rxmode(struct net_device *dev)
2495{
2496 /* unfortunately we can't return errors to the stack */
2497 set_rxmode(dev, -1, false);
2498}
2499
2500static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2501{
2502 int ret;
2503 struct port_info *pi = netdev_priv(dev);
2504
b2612722 2505 ret = t4_set_rxmode(pi->adapter, pi->adapter->pf, pi->viid, new_mtu, -1,
060e0c75 2506 -1, -1, -1, true);
b8ff05a9
DM
2507 if (!ret)
2508 dev->mtu = new_mtu;
2509 return ret;
2510}
2511
858aa65c 2512#ifdef CONFIG_PCI_IOV
e7b48a32
HS
2513static int dummy_open(struct net_device *dev)
2514{
2515 /* Turn carrier off since we don't have to transmit anything on this
2516 * interface.
2517 */
2518 netif_carrier_off(dev);
2519 return 0;
2520}
2521
661dbeb9
HS
2522/* Fill MAC address that will be assigned by the FW */
2523static void fill_vf_station_mac_addr(struct adapter *adap)
2524{
2525 unsigned int i;
2526 u8 hw_addr[ETH_ALEN], macaddr[ETH_ALEN];
2527 int err;
2528 u8 *na;
2529 u16 a, b;
2530
2531 err = t4_get_raw_vpd_params(adap, &adap->params.vpd);
2532 if (!err) {
2533 na = adap->params.vpd.na;
2534 for (i = 0; i < ETH_ALEN; i++)
2535 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
2536 hex2val(na[2 * i + 1]));
2537 a = (hw_addr[0] << 8) | hw_addr[1];
2538 b = (hw_addr[1] << 8) | hw_addr[2];
2539 a ^= b;
2540 a |= 0x0200; /* locally assigned Ethernet MAC address */
2541 a &= ~0x0100; /* not a multicast Ethernet MAC address */
2542 macaddr[0] = a >> 8;
2543 macaddr[1] = a & 0xff;
2544
2545 for (i = 2; i < 5; i++)
2546 macaddr[i] = hw_addr[i + 1];
2547
2548 for (i = 0; i < adap->num_vfs; i++) {
2549 macaddr[5] = adap->pf * 16 + i;
2550 ether_addr_copy(adap->vfinfo[i].vf_mac_addr, macaddr);
2551 }
2552 }
2553}
2554
858aa65c
HS
2555static int cxgb_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2556{
2557 struct port_info *pi = netdev_priv(dev);
2558 struct adapter *adap = pi->adapter;
661dbeb9 2559 int ret;
858aa65c
HS
2560
2561 /* verify MAC addr is valid */
2562 if (!is_valid_ether_addr(mac)) {
2563 dev_err(pi->adapter->pdev_dev,
2564 "Invalid Ethernet address %pM for VF %d\n",
2565 mac, vf);
2566 return -EINVAL;
2567 }
2568
2569 dev_info(pi->adapter->pdev_dev,
2570 "Setting MAC %pM on VF %d\n", mac, vf);
661dbeb9
HS
2571 ret = t4_set_vf_mac_acl(adap, vf + 1, 1, mac);
2572 if (!ret)
2573 ether_addr_copy(adap->vfinfo[vf].vf_mac_addr, mac);
2574 return ret;
2575}
2576
2577static int cxgb_get_vf_config(struct net_device *dev,
2578 int vf, struct ifla_vf_info *ivi)
2579{
2580 struct port_info *pi = netdev_priv(dev);
2581 struct adapter *adap = pi->adapter;
2582
2583 if (vf >= adap->num_vfs)
2584 return -EINVAL;
2585 ivi->vf = vf;
2586 ether_addr_copy(ivi->mac, adap->vfinfo[vf].vf_mac_addr);
2587 return 0;
858aa65c
HS
2588}
2589#endif
2590
b8ff05a9
DM
2591static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2592{
2593 int ret;
2594 struct sockaddr *addr = p;
2595 struct port_info *pi = netdev_priv(dev);
2596
2597 if (!is_valid_ether_addr(addr->sa_data))
504f9b5a 2598 return -EADDRNOTAVAIL;
b8ff05a9 2599
b2612722 2600 ret = t4_change_mac(pi->adapter, pi->adapter->pf, pi->viid,
060e0c75 2601 pi->xact_addr_filt, addr->sa_data, true, true);
b8ff05a9
DM
2602 if (ret < 0)
2603 return ret;
2604
2605 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2606 pi->xact_addr_filt = ret;
2607 return 0;
2608}
2609
b8ff05a9
DM
2610#ifdef CONFIG_NET_POLL_CONTROLLER
2611static void cxgb_netpoll(struct net_device *dev)
2612{
2613 struct port_info *pi = netdev_priv(dev);
2614 struct adapter *adap = pi->adapter;
2615
2616 if (adap->flags & USING_MSIX) {
2617 int i;
2618 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2619
2620 for (i = pi->nqsets; i; i--, rx++)
2621 t4_sge_intr_msix(0, &rx->rspq);
2622 } else
2623 t4_intr_handler(adap)(0, adap);
2624}
2625#endif
2626
10a2604e
RL
2627static int cxgb_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
2628{
2629 struct port_info *pi = netdev_priv(dev);
2630 struct adapter *adap = pi->adapter;
2631 struct sched_class *e;
2632 struct ch_sched_params p;
2633 struct ch_sched_queue qe;
2634 u32 req_rate;
2635 int err = 0;
2636
2637 if (!can_sched(dev))
2638 return -ENOTSUPP;
2639
2640 if (index < 0 || index > pi->nqsets - 1)
2641 return -EINVAL;
2642
2643 if (!(adap->flags & FULL_INIT_DONE)) {
2644 dev_err(adap->pdev_dev,
2645 "Failed to rate limit on queue %d. Link Down?\n",
2646 index);
2647 return -EINVAL;
2648 }
2649
2650 /* Convert from Mbps to Kbps */
2651 req_rate = rate << 10;
2652
2653 /* Max rate is 10 Gbps */
2654 if (req_rate >= SCHED_MAX_RATE_KBPS) {
2655 dev_err(adap->pdev_dev,
2656 "Invalid rate %u Mbps, Max rate is %u Gbps\n",
2657 rate, SCHED_MAX_RATE_KBPS);
2658 return -ERANGE;
2659 }
2660
2661 /* First unbind the queue from any existing class */
2662 memset(&qe, 0, sizeof(qe));
2663 qe.queue = index;
2664 qe.class = SCHED_CLS_NONE;
2665
2666 err = cxgb4_sched_class_unbind(dev, (void *)(&qe), SCHED_QUEUE);
2667 if (err) {
2668 dev_err(adap->pdev_dev,
2669 "Unbinding Queue %d on port %d fail. Err: %d\n",
2670 index, pi->port_id, err);
2671 return err;
2672 }
2673
2674 /* Queue already unbound */
2675 if (!req_rate)
2676 return 0;
2677
2678 /* Fetch any available unused or matching scheduling class */
2679 memset(&p, 0, sizeof(p));
2680 p.type = SCHED_CLASS_TYPE_PACKET;
2681 p.u.params.level = SCHED_CLASS_LEVEL_CL_RL;
2682 p.u.params.mode = SCHED_CLASS_MODE_CLASS;
2683 p.u.params.rateunit = SCHED_CLASS_RATEUNIT_BITS;
2684 p.u.params.ratemode = SCHED_CLASS_RATEMODE_ABS;
2685 p.u.params.channel = pi->tx_chan;
2686 p.u.params.class = SCHED_CLS_NONE;
2687 p.u.params.minrate = 0;
2688 p.u.params.maxrate = req_rate;
2689 p.u.params.weight = 0;
2690 p.u.params.pktsize = dev->mtu;
2691
2692 e = cxgb4_sched_class_alloc(dev, &p);
2693 if (!e)
2694 return -ENOMEM;
2695
2696 /* Bind the queue to a scheduling class */
2697 memset(&qe, 0, sizeof(qe));
2698 qe.queue = index;
2699 qe.class = e->idx;
2700
2701 err = cxgb4_sched_class_bind(dev, (void *)(&qe), SCHED_QUEUE);
2702 if (err)
2703 dev_err(adap->pdev_dev,
2704 "Queue rate limiting failed. Err: %d\n", err);
2705 return err;
2706}
2707
8efebd6e
BX
2708static int cxgb_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
2709 struct tc_to_netdev *tc)
d8931847
RL
2710{
2711 struct port_info *pi = netdev2pinfo(dev);
2712 struct adapter *adap = netdev2adap(dev);
2713
2714 if (!(adap->flags & FULL_INIT_DONE)) {
2715 dev_err(adap->pdev_dev,
2716 "Failed to setup tc on port %d. Link Down?\n",
2717 pi->port_id);
2718 return -EINVAL;
2719 }
2720
2721 if (TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS) &&
2722 tc->type == TC_SETUP_CLSU32) {
2723 switch (tc->cls_u32->command) {
2724 case TC_CLSU32_NEW_KNODE:
2725 case TC_CLSU32_REPLACE_KNODE:
2726 return cxgb4_config_knode(dev, proto, tc->cls_u32);
2727 case TC_CLSU32_DELETE_KNODE:
2728 return cxgb4_delete_knode(dev, proto, tc->cls_u32);
2729 default:
2730 return -EOPNOTSUPP;
2731 }
2732 }
2733
2734 return -EOPNOTSUPP;
2735}
2736
b8ff05a9
DM
2737static const struct net_device_ops cxgb4_netdev_ops = {
2738 .ndo_open = cxgb_open,
2739 .ndo_stop = cxgb_close,
2740 .ndo_start_xmit = t4_eth_xmit,
688848b1 2741 .ndo_select_queue = cxgb_select_queue,
9be793bf 2742 .ndo_get_stats64 = cxgb_get_stats,
b8ff05a9
DM
2743 .ndo_set_rx_mode = cxgb_set_rxmode,
2744 .ndo_set_mac_address = cxgb_set_mac_addr,
2ed28baa 2745 .ndo_set_features = cxgb_set_features,
b8ff05a9
DM
2746 .ndo_validate_addr = eth_validate_addr,
2747 .ndo_do_ioctl = cxgb_ioctl,
2748 .ndo_change_mtu = cxgb_change_mtu,
b8ff05a9
DM
2749#ifdef CONFIG_NET_POLL_CONTROLLER
2750 .ndo_poll_controller = cxgb_netpoll,
2751#endif
84a200b3
VP
2752#ifdef CONFIG_CHELSIO_T4_FCOE
2753 .ndo_fcoe_enable = cxgb_fcoe_enable,
2754 .ndo_fcoe_disable = cxgb_fcoe_disable,
2755#endif /* CONFIG_CHELSIO_T4_FCOE */
3a336cb1
HS
2756#ifdef CONFIG_NET_RX_BUSY_POLL
2757 .ndo_busy_poll = cxgb_busy_poll,
2758#endif
10a2604e 2759 .ndo_set_tx_maxrate = cxgb_set_tx_maxrate,
d8931847 2760 .ndo_setup_tc = cxgb_setup_tc,
b8ff05a9
DM
2761};
2762
858aa65c 2763#ifdef CONFIG_PCI_IOV
e7b48a32
HS
2764static const struct net_device_ops cxgb4_mgmt_netdev_ops = {
2765 .ndo_open = dummy_open,
858aa65c 2766 .ndo_set_vf_mac = cxgb_set_vf_mac,
661dbeb9 2767 .ndo_get_vf_config = cxgb_get_vf_config,
7829451c 2768};
e7b48a32 2769#endif
7829451c
HS
2770
2771static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2772{
2773 struct adapter *adapter = netdev2adap(dev);
2774
2775 strlcpy(info->driver, cxgb4_driver_name, sizeof(info->driver));
2776 strlcpy(info->version, cxgb4_driver_version,
2777 sizeof(info->version));
2778 strlcpy(info->bus_info, pci_name(adapter->pdev),
2779 sizeof(info->bus_info));
2780}
2781
2782static const struct ethtool_ops cxgb4_mgmt_ethtool_ops = {
2783 .get_drvinfo = get_drvinfo,
2784};
2785
b8ff05a9
DM
2786void t4_fatal_err(struct adapter *adap)
2787{
f612b815 2788 t4_set_reg_field(adap, SGE_CONTROL_A, GLOBALENABLE_F, 0);
b8ff05a9
DM
2789 t4_intr_disable(adap);
2790 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
2791}
2792
2793static void setup_memwin(struct adapter *adap)
2794{
b562fc37 2795 u32 nic_win_base = t4_get_util_window(adap);
b8ff05a9 2796
b562fc37 2797 t4_setup_memwin(adap, nic_win_base, MEMWIN_NIC);
636f9d37
VP
2798}
2799
2800static void setup_memwin_rdma(struct adapter *adap)
2801{
1ae970e0 2802 if (adap->vres.ocq.size) {
0abfd152
HS
2803 u32 start;
2804 unsigned int sz_kb;
1ae970e0 2805
0abfd152
HS
2806 start = t4_read_pcie_cfg4(adap, PCI_BASE_ADDRESS_2);
2807 start &= PCI_BASE_ADDRESS_MEM_MASK;
2808 start += OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
1ae970e0
DM
2809 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
2810 t4_write_reg(adap,
f061de42
HS
2811 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN_A, 3),
2812 start | BIR_V(1) | WINDOW_V(ilog2(sz_kb)));
1ae970e0 2813 t4_write_reg(adap,
f061de42 2814 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3),
1ae970e0
DM
2815 adap->vres.ocq.start);
2816 t4_read_reg(adap,
f061de42 2817 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET_A, 3));
1ae970e0 2818 }
b8ff05a9
DM
2819}
2820
02b5fb8e
DM
2821static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
2822{
2823 u32 v;
2824 int ret;
2825
2826 /* get device capabilities */
2827 memset(c, 0, sizeof(*c));
e2ac9628
HS
2828 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2829 FW_CMD_REQUEST_F | FW_CMD_READ_F);
ce91a923 2830 c->cfvalid_to_len16 = htonl(FW_LEN16(*c));
b2612722 2831 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), c);
02b5fb8e
DM
2832 if (ret < 0)
2833 return ret;
2834
e2ac9628
HS
2835 c->op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
2836 FW_CMD_REQUEST_F | FW_CMD_WRITE_F);
b2612722 2837 ret = t4_wr_mbox(adap, adap->mbox, c, sizeof(*c), NULL);
02b5fb8e
DM
2838 if (ret < 0)
2839 return ret;
2840
b2612722 2841 ret = t4_config_glbl_rss(adap, adap->pf,
02b5fb8e 2842 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
b2e1a3f0
HS
2843 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN_F |
2844 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP_F);
02b5fb8e
DM
2845 if (ret < 0)
2846 return ret;
2847
b2612722 2848 ret = t4_cfg_pfvf(adap, adap->mbox, adap->pf, 0, adap->sge.egr_sz, 64,
4b8e27a8
HS
2849 MAX_INGQ, 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF,
2850 FW_CMD_CAP_PF);
02b5fb8e
DM
2851 if (ret < 0)
2852 return ret;
2853
2854 t4_sge_init(adap);
2855
02b5fb8e 2856 /* tweak some settings */
837e4a42 2857 t4_write_reg(adap, TP_SHIFT_CNT_A, 0x64f8849);
0d804338 2858 t4_write_reg(adap, ULP_RX_TDDP_PSZ_A, HPZ0_V(PAGE_SHIFT - 12));
837e4a42
HS
2859 t4_write_reg(adap, TP_PIO_ADDR_A, TP_INGRESS_CONFIG_A);
2860 v = t4_read_reg(adap, TP_PIO_DATA_A);
2861 t4_write_reg(adap, TP_PIO_DATA_A, v & ~CSUM_HAS_PSEUDO_HDR_F);
060e0c75 2862
dca4faeb
VP
2863 /* first 4 Tx modulation queues point to consecutive Tx channels */
2864 adap->params.tp.tx_modq_map = 0xE4;
0d804338
HS
2865 t4_write_reg(adap, TP_TX_MOD_QUEUE_REQ_MAP_A,
2866 TX_MOD_QUEUE_REQ_MAP_V(adap->params.tp.tx_modq_map));
dca4faeb
VP
2867
2868 /* associate each Tx modulation queue with consecutive Tx channels */
2869 v = 0x84218421;
837e4a42 2870 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
0d804338 2871 &v, 1, TP_TX_SCHED_HDR_A);
837e4a42 2872 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
0d804338 2873 &v, 1, TP_TX_SCHED_FIFO_A);
837e4a42 2874 t4_write_indirect(adap, TP_PIO_ADDR_A, TP_PIO_DATA_A,
0d804338 2875 &v, 1, TP_TX_SCHED_PCMD_A);
dca4faeb
VP
2876
2877#define T4_TX_MODQ_10G_WEIGHT_DEFAULT 16 /* in KB units */
2878 if (is_offload(adap)) {
0d804338
HS
2879 t4_write_reg(adap, TP_TX_MOD_QUEUE_WEIGHT0_A,
2880 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2881 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2882 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2883 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
2884 t4_write_reg(adap, TP_TX_MOD_CHANNEL_WEIGHT_A,
2885 TX_MODQ_WEIGHT0_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2886 TX_MODQ_WEIGHT1_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2887 TX_MODQ_WEIGHT2_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT) |
2888 TX_MODQ_WEIGHT3_V(T4_TX_MODQ_10G_WEIGHT_DEFAULT));
dca4faeb
VP
2889 }
2890
060e0c75 2891 /* get basic stuff going */
b2612722 2892 return t4_early_init(adap, adap->pf);
02b5fb8e
DM
2893}
2894
b8ff05a9
DM
2895/*
2896 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
2897 */
2898#define MAX_ATIDS 8192U
2899
636f9d37
VP
2900/*
2901 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
2902 *
2903 * If the firmware we're dealing with has Configuration File support, then
2904 * we use that to perform all configuration
2905 */
2906
2907/*
2908 * Tweak configuration based on module parameters, etc. Most of these have
2909 * defaults assigned to them by Firmware Configuration Files (if we're using
2910 * them) but need to be explicitly set if we're using hard-coded
2911 * initialization. But even in the case of using Firmware Configuration
2912 * Files, we'd like to expose the ability to change these via module
2913 * parameters so these are essentially common tweaks/settings for
2914 * Configuration Files and hard-coded initialization ...
2915 */
2916static int adap_init0_tweaks(struct adapter *adapter)
2917{
2918 /*
2919 * Fix up various Host-Dependent Parameters like Page Size, Cache
2920 * Line Size, etc. The firmware default is for a 4KB Page Size and
2921 * 64B Cache Line Size ...
2922 */
2923 t4_fixup_host_params(adapter, PAGE_SIZE, L1_CACHE_BYTES);
2924
2925 /*
2926 * Process module parameters which affect early initialization.
2927 */
2928 if (rx_dma_offset != 2 && rx_dma_offset != 0) {
2929 dev_err(&adapter->pdev->dev,
2930 "Ignoring illegal rx_dma_offset=%d, using 2\n",
2931 rx_dma_offset);
2932 rx_dma_offset = 2;
2933 }
f612b815
HS
2934 t4_set_reg_field(adapter, SGE_CONTROL_A,
2935 PKTSHIFT_V(PKTSHIFT_M),
2936 PKTSHIFT_V(rx_dma_offset));
636f9d37
VP
2937
2938 /*
2939 * Don't include the "IP Pseudo Header" in CPL_RX_PKT checksums: Linux
2940 * adds the pseudo header itself.
2941 */
837e4a42
HS
2942 t4_tp_wr_bits_indirect(adapter, TP_INGRESS_CONFIG_A,
2943 CSUM_HAS_PSEUDO_HDR_F, 0);
636f9d37
VP
2944
2945 return 0;
2946}
2947
01b69614
HS
2948/* 10Gb/s-BT PHY Support. chip-external 10Gb/s-BT PHYs are complex chips
2949 * unto themselves and they contain their own firmware to perform their
2950 * tasks ...
2951 */
2952static int phy_aq1202_version(const u8 *phy_fw_data,
2953 size_t phy_fw_size)
2954{
2955 int offset;
2956
2957 /* At offset 0x8 you're looking for the primary image's
2958 * starting offset which is 3 Bytes wide
2959 *
2960 * At offset 0xa of the primary image, you look for the offset
2961 * of the DRAM segment which is 3 Bytes wide.
2962 *
2963 * The FW version is at offset 0x27e of the DRAM and is 2 Bytes
2964 * wide
2965 */
2966 #define be16(__p) (((__p)[0] << 8) | (__p)[1])
2967 #define le16(__p) ((__p)[0] | ((__p)[1] << 8))
2968 #define le24(__p) (le16(__p) | ((__p)[2] << 16))
2969
2970 offset = le24(phy_fw_data + 0x8) << 12;
2971 offset = le24(phy_fw_data + offset + 0xa);
2972 return be16(phy_fw_data + offset + 0x27e);
2973
2974 #undef be16
2975 #undef le16
2976 #undef le24
2977}
2978
2979static struct info_10gbt_phy_fw {
2980 unsigned int phy_fw_id; /* PCI Device ID */
2981 char *phy_fw_file; /* /lib/firmware/ PHY Firmware file */
2982 int (*phy_fw_version)(const u8 *phy_fw_data, size_t phy_fw_size);
2983 int phy_flash; /* Has FLASH for PHY Firmware */
2984} phy_info_array[] = {
2985 {
2986 PHY_AQ1202_DEVICEID,
2987 PHY_AQ1202_FIRMWARE,
2988 phy_aq1202_version,
2989 1,
2990 },
2991 {
2992 PHY_BCM84834_DEVICEID,
2993 PHY_BCM84834_FIRMWARE,
2994 NULL,
2995 0,
2996 },
2997 { 0, NULL, NULL },
2998};
2999
3000static struct info_10gbt_phy_fw *find_phy_info(int devid)
3001{
3002 int i;
3003
3004 for (i = 0; i < ARRAY_SIZE(phy_info_array); i++) {
3005 if (phy_info_array[i].phy_fw_id == devid)
3006 return &phy_info_array[i];
3007 }
3008 return NULL;
3009}
3010
3011/* Handle updating of chip-external 10Gb/s-BT PHY firmware. This needs to
3012 * happen after the FW_RESET_CMD but before the FW_INITIALIZE_CMD. On error
3013 * we return a negative error number. If we transfer new firmware we return 1
3014 * (from t4_load_phy_fw()). If we don't do anything we return 0.
3015 */
3016static int adap_init0_phy(struct adapter *adap)
3017{
3018 const struct firmware *phyf;
3019 int ret;
3020 struct info_10gbt_phy_fw *phy_info;
3021
3022 /* Use the device ID to determine which PHY file to flash.
3023 */
3024 phy_info = find_phy_info(adap->pdev->device);
3025 if (!phy_info) {
3026 dev_warn(adap->pdev_dev,
3027 "No PHY Firmware file found for this PHY\n");
3028 return -EOPNOTSUPP;
3029 }
3030
3031 /* If we have a T4 PHY firmware file under /lib/firmware/cxgb4/, then
3032 * use that. The adapter firmware provides us with a memory buffer
3033 * where we can load a PHY firmware file from the host if we want to
3034 * override the PHY firmware File in flash.
3035 */
3036 ret = request_firmware_direct(&phyf, phy_info->phy_fw_file,
3037 adap->pdev_dev);
3038 if (ret < 0) {
3039 /* For adapters without FLASH attached to PHY for their
3040 * firmware, it's obviously a fatal error if we can't get the
3041 * firmware to the adapter. For adapters with PHY firmware
3042 * FLASH storage, it's worth a warning if we can't find the
3043 * PHY Firmware but we'll neuter the error ...
3044 */
3045 dev_err(adap->pdev_dev, "unable to find PHY Firmware image "
3046 "/lib/firmware/%s, error %d\n",
3047 phy_info->phy_fw_file, -ret);
3048 if (phy_info->phy_flash) {
3049 int cur_phy_fw_ver = 0;
3050
3051 t4_phy_fw_ver(adap, &cur_phy_fw_ver);
3052 dev_warn(adap->pdev_dev, "continuing with, on-adapter "
3053 "FLASH copy, version %#x\n", cur_phy_fw_ver);
3054 ret = 0;
3055 }
3056
3057 return ret;
3058 }
3059
3060 /* Load PHY Firmware onto adapter.
3061 */
3062 ret = t4_load_phy_fw(adap, MEMWIN_NIC, &adap->win0_lock,
3063 phy_info->phy_fw_version,
3064 (u8 *)phyf->data, phyf->size);
3065 if (ret < 0)
3066 dev_err(adap->pdev_dev, "PHY Firmware transfer error %d\n",
3067 -ret);
3068 else if (ret > 0) {
3069 int new_phy_fw_ver = 0;
3070
3071 if (phy_info->phy_fw_version)
3072 new_phy_fw_ver = phy_info->phy_fw_version(phyf->data,
3073 phyf->size);
3074 dev_info(adap->pdev_dev, "Successfully transferred PHY "
3075 "Firmware /lib/firmware/%s, version %#x\n",
3076 phy_info->phy_fw_file, new_phy_fw_ver);
3077 }
3078
3079 release_firmware(phyf);
3080
3081 return ret;
3082}
3083
636f9d37
VP
3084/*
3085 * Attempt to initialize the adapter via a Firmware Configuration File.
3086 */
3087static int adap_init0_config(struct adapter *adapter, int reset)
3088{
3089 struct fw_caps_config_cmd caps_cmd;
3090 const struct firmware *cf;
3091 unsigned long mtype = 0, maddr = 0;
3092 u32 finiver, finicsum, cfcsum;
16e47624
HS
3093 int ret;
3094 int config_issued = 0;
0a57a536 3095 char *fw_config_file, fw_config_file_path[256];
16e47624 3096 char *config_name = NULL;
636f9d37
VP
3097
3098 /*
3099 * Reset device if necessary.
3100 */
3101 if (reset) {
3102 ret = t4_fw_reset(adapter, adapter->mbox,
0d804338 3103 PIORSTMODE_F | PIORST_F);
636f9d37
VP
3104 if (ret < 0)
3105 goto bye;
3106 }
3107
01b69614
HS
3108 /* If this is a 10Gb/s-BT adapter make sure the chip-external
3109 * 10Gb/s-BT PHYs have up-to-date firmware. Note that this step needs
3110 * to be performed after any global adapter RESET above since some
3111 * PHYs only have local RAM copies of the PHY firmware.
3112 */
3113 if (is_10gbt_device(adapter->pdev->device)) {
3114 ret = adap_init0_phy(adapter);
3115 if (ret < 0)
3116 goto bye;
3117 }
636f9d37
VP
3118 /*
3119 * If we have a T4 configuration file under /lib/firmware/cxgb4/,
3120 * then use that. Otherwise, use the configuration file stored
3121 * in the adapter flash ...
3122 */
d14807dd 3123 switch (CHELSIO_CHIP_VERSION(adapter->params.chip)) {
0a57a536 3124 case CHELSIO_T4:
16e47624 3125 fw_config_file = FW4_CFNAME;
0a57a536
SR
3126 break;
3127 case CHELSIO_T5:
3128 fw_config_file = FW5_CFNAME;
3129 break;
3ccc6cf7
HS
3130 case CHELSIO_T6:
3131 fw_config_file = FW6_CFNAME;
3132 break;
0a57a536
SR
3133 default:
3134 dev_err(adapter->pdev_dev, "Device %d is not supported\n",
3135 adapter->pdev->device);
3136 ret = -EINVAL;
3137 goto bye;
3138 }
3139
3140 ret = request_firmware(&cf, fw_config_file, adapter->pdev_dev);
636f9d37 3141 if (ret < 0) {
16e47624 3142 config_name = "On FLASH";
636f9d37
VP
3143 mtype = FW_MEMTYPE_CF_FLASH;
3144 maddr = t4_flash_cfg_addr(adapter);
3145 } else {
3146 u32 params[7], val[7];
3147
16e47624
HS
3148 sprintf(fw_config_file_path,
3149 "/lib/firmware/%s", fw_config_file);
3150 config_name = fw_config_file_path;
3151
636f9d37
VP
3152 if (cf->size >= FLASH_CFG_MAX_SIZE)
3153 ret = -ENOMEM;
3154 else {
5167865a
HS
3155 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3156 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
636f9d37 3157 ret = t4_query_params(adapter, adapter->mbox,
b2612722 3158 adapter->pf, 0, 1, params, val);
636f9d37
VP
3159 if (ret == 0) {
3160 /*
fc5ab020 3161 * For t4_memory_rw() below addresses and
636f9d37
VP
3162 * sizes have to be in terms of multiples of 4
3163 * bytes. So, if the Configuration File isn't
3164 * a multiple of 4 bytes in length we'll have
3165 * to write that out separately since we can't
3166 * guarantee that the bytes following the
3167 * residual byte in the buffer returned by
3168 * request_firmware() are zeroed out ...
3169 */
3170 size_t resid = cf->size & 0x3;
3171 size_t size = cf->size & ~0x3;
3172 __be32 *data = (__be32 *)cf->data;
3173
5167865a
HS
3174 mtype = FW_PARAMS_PARAM_Y_G(val[0]);
3175 maddr = FW_PARAMS_PARAM_Z_G(val[0]) << 16;
636f9d37 3176
fc5ab020
HS
3177 spin_lock(&adapter->win0_lock);
3178 ret = t4_memory_rw(adapter, 0, mtype, maddr,
3179 size, data, T4_MEMORY_WRITE);
636f9d37
VP
3180 if (ret == 0 && resid != 0) {
3181 union {
3182 __be32 word;
3183 char buf[4];
3184 } last;
3185 int i;
3186
3187 last.word = data[size >> 2];
3188 for (i = resid; i < 4; i++)
3189 last.buf[i] = 0;
fc5ab020
HS
3190 ret = t4_memory_rw(adapter, 0, mtype,
3191 maddr + size,
3192 4, &last.word,
3193 T4_MEMORY_WRITE);
636f9d37 3194 }
fc5ab020 3195 spin_unlock(&adapter->win0_lock);
636f9d37
VP
3196 }
3197 }
3198
3199 release_firmware(cf);
3200 if (ret)
3201 goto bye;
3202 }
3203
3204 /*
3205 * Issue a Capability Configuration command to the firmware to get it
3206 * to parse the Configuration File. We don't use t4_fw_config_file()
3207 * because we want the ability to modify various features after we've
3208 * processed the configuration file ...
3209 */
3210 memset(&caps_cmd, 0, sizeof(caps_cmd));
3211 caps_cmd.op_to_write =
e2ac9628
HS
3212 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3213 FW_CMD_REQUEST_F |
3214 FW_CMD_READ_F);
ce91a923 3215 caps_cmd.cfvalid_to_len16 =
5167865a
HS
3216 htonl(FW_CAPS_CONFIG_CMD_CFVALID_F |
3217 FW_CAPS_CONFIG_CMD_MEMTYPE_CF_V(mtype) |
3218 FW_CAPS_CONFIG_CMD_MEMADDR64K_CF_V(maddr >> 16) |
636f9d37
VP
3219 FW_LEN16(caps_cmd));
3220 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3221 &caps_cmd);
16e47624
HS
3222
3223 /* If the CAPS_CONFIG failed with an ENOENT (for a Firmware
3224 * Configuration File in FLASH), our last gasp effort is to use the
3225 * Firmware Configuration File which is embedded in the firmware. A
3226 * very few early versions of the firmware didn't have one embedded
3227 * but we can ignore those.
3228 */
3229 if (ret == -ENOENT) {
3230 memset(&caps_cmd, 0, sizeof(caps_cmd));
3231 caps_cmd.op_to_write =
e2ac9628
HS
3232 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3233 FW_CMD_REQUEST_F |
3234 FW_CMD_READ_F);
16e47624
HS
3235 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
3236 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd,
3237 sizeof(caps_cmd), &caps_cmd);
3238 config_name = "Firmware Default";
3239 }
3240
3241 config_issued = 1;
636f9d37
VP
3242 if (ret < 0)
3243 goto bye;
3244
3245 finiver = ntohl(caps_cmd.finiver);
3246 finicsum = ntohl(caps_cmd.finicsum);
3247 cfcsum = ntohl(caps_cmd.cfcsum);
3248 if (finicsum != cfcsum)
3249 dev_warn(adapter->pdev_dev, "Configuration File checksum "\
3250 "mismatch: [fini] csum=%#x, computed csum=%#x\n",
3251 finicsum, cfcsum);
3252
636f9d37
VP
3253 /*
3254 * And now tell the firmware to use the configuration we just loaded.
3255 */
3256 caps_cmd.op_to_write =
e2ac9628
HS
3257 htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3258 FW_CMD_REQUEST_F |
3259 FW_CMD_WRITE_F);
ce91a923 3260 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
636f9d37
VP
3261 ret = t4_wr_mbox(adapter, adapter->mbox, &caps_cmd, sizeof(caps_cmd),
3262 NULL);
3263 if (ret < 0)
3264 goto bye;
3265
3266 /*
3267 * Tweak configuration based on system architecture, module
3268 * parameters, etc.
3269 */
3270 ret = adap_init0_tweaks(adapter);
3271 if (ret < 0)
3272 goto bye;
3273
3274 /*
3275 * And finally tell the firmware to initialize itself using the
3276 * parameters from the Configuration File.
3277 */
3278 ret = t4_fw_initialize(adapter, adapter->mbox);
3279 if (ret < 0)
3280 goto bye;
3281
06640310
HS
3282 /* Emit Firmware Configuration File information and return
3283 * successfully.
636f9d37 3284 */
636f9d37 3285 dev_info(adapter->pdev_dev, "Successfully configured using Firmware "\
16e47624
HS
3286 "Configuration File \"%s\", version %#x, computed checksum %#x\n",
3287 config_name, finiver, cfcsum);
636f9d37
VP
3288 return 0;
3289
3290 /*
3291 * Something bad happened. Return the error ... (If the "error"
3292 * is that there's no Configuration File on the adapter we don't
3293 * want to issue a warning since this is fairly common.)
3294 */
3295bye:
16e47624
HS
3296 if (config_issued && ret != -ENOENT)
3297 dev_warn(adapter->pdev_dev, "\"%s\" configuration file error %d\n",
3298 config_name, -ret);
636f9d37
VP
3299 return ret;
3300}
3301
16e47624
HS
3302static struct fw_info fw_info_array[] = {
3303 {
3304 .chip = CHELSIO_T4,
3305 .fs_name = FW4_CFNAME,
3306 .fw_mod_name = FW4_FNAME,
3307 .fw_hdr = {
3308 .chip = FW_HDR_CHIP_T4,
3309 .fw_ver = __cpu_to_be32(FW_VERSION(T4)),
3310 .intfver_nic = FW_INTFVER(T4, NIC),
3311 .intfver_vnic = FW_INTFVER(T4, VNIC),
3312 .intfver_ri = FW_INTFVER(T4, RI),
3313 .intfver_iscsi = FW_INTFVER(T4, ISCSI),
3314 .intfver_fcoe = FW_INTFVER(T4, FCOE),
3315 },
3316 }, {
3317 .chip = CHELSIO_T5,
3318 .fs_name = FW5_CFNAME,
3319 .fw_mod_name = FW5_FNAME,
3320 .fw_hdr = {
3321 .chip = FW_HDR_CHIP_T5,
3322 .fw_ver = __cpu_to_be32(FW_VERSION(T5)),
3323 .intfver_nic = FW_INTFVER(T5, NIC),
3324 .intfver_vnic = FW_INTFVER(T5, VNIC),
3325 .intfver_ri = FW_INTFVER(T5, RI),
3326 .intfver_iscsi = FW_INTFVER(T5, ISCSI),
3327 .intfver_fcoe = FW_INTFVER(T5, FCOE),
3328 },
3ccc6cf7
HS
3329 }, {
3330 .chip = CHELSIO_T6,
3331 .fs_name = FW6_CFNAME,
3332 .fw_mod_name = FW6_FNAME,
3333 .fw_hdr = {
3334 .chip = FW_HDR_CHIP_T6,
3335 .fw_ver = __cpu_to_be32(FW_VERSION(T6)),
3336 .intfver_nic = FW_INTFVER(T6, NIC),
3337 .intfver_vnic = FW_INTFVER(T6, VNIC),
3338 .intfver_ofld = FW_INTFVER(T6, OFLD),
3339 .intfver_ri = FW_INTFVER(T6, RI),
3340 .intfver_iscsipdu = FW_INTFVER(T6, ISCSIPDU),
3341 .intfver_iscsi = FW_INTFVER(T6, ISCSI),
3342 .intfver_fcoepdu = FW_INTFVER(T6, FCOEPDU),
3343 .intfver_fcoe = FW_INTFVER(T6, FCOE),
3344 },
16e47624 3345 }
3ccc6cf7 3346
16e47624
HS
3347};
3348
3349static struct fw_info *find_fw_info(int chip)
3350{
3351 int i;
3352
3353 for (i = 0; i < ARRAY_SIZE(fw_info_array); i++) {
3354 if (fw_info_array[i].chip == chip)
3355 return &fw_info_array[i];
3356 }
3357 return NULL;
3358}
3359
b8ff05a9
DM
3360/*
3361 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
3362 */
3363static int adap_init0(struct adapter *adap)
3364{
3365 int ret;
3366 u32 v, port_vec;
3367 enum dev_state state;
3368 u32 params[7], val[7];
9a4da2cd 3369 struct fw_caps_config_cmd caps_cmd;
dcf7b6f5 3370 int reset = 1;
b8ff05a9 3371
ae469b68
HS
3372 /* Grab Firmware Device Log parameters as early as possible so we have
3373 * access to it for debugging, etc.
3374 */
3375 ret = t4_init_devlog_params(adap);
3376 if (ret < 0)
3377 return ret;
3378
666224d4 3379 /* Contact FW, advertising Master capability */
c5a8c0f3
HS
3380 ret = t4_fw_hello(adap, adap->mbox, adap->mbox,
3381 is_kdump_kernel() ? MASTER_MUST : MASTER_MAY, &state);
b8ff05a9
DM
3382 if (ret < 0) {
3383 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3384 ret);
3385 return ret;
3386 }
636f9d37
VP
3387 if (ret == adap->mbox)
3388 adap->flags |= MASTER_PF;
b8ff05a9 3389
636f9d37
VP
3390 /*
3391 * If we're the Master PF Driver and the device is uninitialized,
3392 * then let's consider upgrading the firmware ... (We always want
3393 * to check the firmware version number in order to A. get it for
3394 * later reporting and B. to warn if the currently loaded firmware
3395 * is excessively mismatched relative to the driver.)
3396 */
16e47624 3397 t4_get_fw_version(adap, &adap->params.fw_vers);
0de72738 3398 t4_get_bs_version(adap, &adap->params.bs_vers);
16e47624 3399 t4_get_tp_version(adap, &adap->params.tp_vers);
0de72738
HS
3400 t4_get_exprom_version(adap, &adap->params.er_vers);
3401
a69265e9
HS
3402 ret = t4_check_fw_version(adap);
3403 /* If firmware is too old (not supported by driver) force an update. */
21d11bd6 3404 if (ret)
a69265e9 3405 state = DEV_STATE_UNINIT;
636f9d37 3406 if ((adap->flags & MASTER_PF) && state != DEV_STATE_INIT) {
16e47624
HS
3407 struct fw_info *fw_info;
3408 struct fw_hdr *card_fw;
3409 const struct firmware *fw;
3410 const u8 *fw_data = NULL;
3411 unsigned int fw_size = 0;
3412
3413 /* This is the firmware whose headers the driver was compiled
3414 * against
3415 */
3416 fw_info = find_fw_info(CHELSIO_CHIP_VERSION(adap->params.chip));
3417 if (fw_info == NULL) {
3418 dev_err(adap->pdev_dev,
3419 "unable to get firmware info for chip %d.\n",
3420 CHELSIO_CHIP_VERSION(adap->params.chip));
3421 return -EINVAL;
636f9d37 3422 }
16e47624
HS
3423
3424 /* allocate memory to read the header of the firmware on the
3425 * card
3426 */
3427 card_fw = t4_alloc_mem(sizeof(*card_fw));
3428
3429 /* Get FW from from /lib/firmware/ */
3430 ret = request_firmware(&fw, fw_info->fw_mod_name,
3431 adap->pdev_dev);
3432 if (ret < 0) {
3433 dev_err(adap->pdev_dev,
3434 "unable to load firmware image %s, error %d\n",
3435 fw_info->fw_mod_name, ret);
3436 } else {
3437 fw_data = fw->data;
3438 fw_size = fw->size;
3439 }
3440
3441 /* upgrade FW logic */
3442 ret = t4_prep_fw(adap, fw_info, fw_data, fw_size, card_fw,
3443 state, &reset);
3444
3445 /* Cleaning up */
0b5b6bee 3446 release_firmware(fw);
16e47624
HS
3447 t4_free_mem(card_fw);
3448
636f9d37 3449 if (ret < 0)
16e47624 3450 goto bye;
636f9d37 3451 }
b8ff05a9 3452
636f9d37
VP
3453 /*
3454 * Grab VPD parameters. This should be done after we establish a
3455 * connection to the firmware since some of the VPD parameters
3456 * (notably the Core Clock frequency) are retrieved via requests to
3457 * the firmware. On the other hand, we need these fairly early on
3458 * so we do this right after getting ahold of the firmware.
3459 */
098ef6c2 3460 ret = t4_get_vpd_params(adap, &adap->params.vpd);
a0881cab
DM
3461 if (ret < 0)
3462 goto bye;
a0881cab 3463
636f9d37 3464 /*
13ee15d3
VP
3465 * Find out what ports are available to us. Note that we need to do
3466 * this before calling adap_init0_no_config() since it needs nports
3467 * and portvec ...
636f9d37
VP
3468 */
3469 v =
5167865a
HS
3470 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3471 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_PORTVEC);
b2612722 3472 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1, &v, &port_vec);
a0881cab
DM
3473 if (ret < 0)
3474 goto bye;
3475
636f9d37
VP
3476 adap->params.nports = hweight32(port_vec);
3477 adap->params.portvec = port_vec;
3478
06640310
HS
3479 /* If the firmware is initialized already, emit a simply note to that
3480 * effect. Otherwise, it's time to try initializing the adapter.
636f9d37
VP
3481 */
3482 if (state == DEV_STATE_INIT) {
3483 dev_info(adap->pdev_dev, "Coming up as %s: "\
3484 "Adapter already initialized\n",
3485 adap->flags & MASTER_PF ? "MASTER" : "SLAVE");
636f9d37
VP
3486 } else {
3487 dev_info(adap->pdev_dev, "Coming up as MASTER: "\
3488 "Initializing adapter\n");
06640310
HS
3489
3490 /* Find out whether we're dealing with a version of the
3491 * firmware which has configuration file support.
636f9d37 3492 */
06640310
HS
3493 params[0] = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
3494 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_CF));
b2612722 3495 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 1,
06640310 3496 params, val);
13ee15d3 3497
06640310
HS
3498 /* If the firmware doesn't support Configuration Files,
3499 * return an error.
3500 */
3501 if (ret < 0) {
3502 dev_err(adap->pdev_dev, "firmware doesn't support "
3503 "Firmware Configuration Files\n");
3504 goto bye;
3505 }
3506
3507 /* The firmware provides us with a memory buffer where we can
3508 * load a Configuration File from the host if we want to
3509 * override the Configuration File in flash.
3510 */
3511 ret = adap_init0_config(adap, reset);
3512 if (ret == -ENOENT) {
3513 dev_err(adap->pdev_dev, "no Configuration File "
3514 "present on adapter.\n");
3515 goto bye;
636f9d37
VP
3516 }
3517 if (ret < 0) {
06640310
HS
3518 dev_err(adap->pdev_dev, "could not initialize "
3519 "adapter, error %d\n", -ret);
636f9d37
VP
3520 goto bye;
3521 }
3522 }
3523
06640310
HS
3524 /* Give the SGE code a chance to pull in anything that it needs ...
3525 * Note that this must be called after we retrieve our VPD parameters
3526 * in order to know how to convert core ticks to seconds, etc.
636f9d37 3527 */
06640310
HS
3528 ret = t4_sge_init(adap);
3529 if (ret < 0)
3530 goto bye;
636f9d37 3531
9a4da2cd
VP
3532 if (is_bypass_device(adap->pdev->device))
3533 adap->params.bypass = 1;
3534
636f9d37
VP
3535 /*
3536 * Grab some of our basic fundamental operating parameters.
3537 */
3538#define FW_PARAM_DEV(param) \
5167865a
HS
3539 (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) | \
3540 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_##param))
636f9d37 3541
b8ff05a9 3542#define FW_PARAM_PFVF(param) \
5167865a
HS
3543 FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_PFVF) | \
3544 FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_PFVF_##param)| \
3545 FW_PARAMS_PARAM_Y_V(0) | \
3546 FW_PARAMS_PARAM_Z_V(0)
b8ff05a9 3547
636f9d37 3548 params[0] = FW_PARAM_PFVF(EQ_START);
b8ff05a9
DM
3549 params[1] = FW_PARAM_PFVF(L2T_START);
3550 params[2] = FW_PARAM_PFVF(L2T_END);
3551 params[3] = FW_PARAM_PFVF(FILTER_START);
3552 params[4] = FW_PARAM_PFVF(FILTER_END);
e46dab4d 3553 params[5] = FW_PARAM_PFVF(IQFLINT_START);
b2612722 3554 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params, val);
b8ff05a9
DM
3555 if (ret < 0)
3556 goto bye;
636f9d37
VP
3557 adap->sge.egr_start = val[0];
3558 adap->l2t_start = val[1];
3559 adap->l2t_end = val[2];
b8ff05a9
DM
3560 adap->tids.ftid_base = val[3];
3561 adap->tids.nftids = val[4] - val[3] + 1;
e46dab4d 3562 adap->sge.ingr_start = val[5];
b8ff05a9 3563
4b8e27a8
HS
3564 /* qids (ingress/egress) returned from firmware can be anywhere
3565 * in the range from EQ(IQFLINT)_START to EQ(IQFLINT)_END.
3566 * Hence driver needs to allocate memory for this range to
3567 * store the queue info. Get the highest IQFLINT/EQ index returned
3568 * in FW_EQ_*_CMD.alloc command.
3569 */
3570 params[0] = FW_PARAM_PFVF(EQ_END);
3571 params[1] = FW_PARAM_PFVF(IQFLINT_END);
b2612722 3572 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
4b8e27a8
HS
3573 if (ret < 0)
3574 goto bye;
3575 adap->sge.egr_sz = val[0] - adap->sge.egr_start + 1;
3576 adap->sge.ingr_sz = val[1] - adap->sge.ingr_start + 1;
3577
3578 adap->sge.egr_map = kcalloc(adap->sge.egr_sz,
3579 sizeof(*adap->sge.egr_map), GFP_KERNEL);
3580 if (!adap->sge.egr_map) {
3581 ret = -ENOMEM;
3582 goto bye;
3583 }
3584
3585 adap->sge.ingr_map = kcalloc(adap->sge.ingr_sz,
3586 sizeof(*adap->sge.ingr_map), GFP_KERNEL);
3587 if (!adap->sge.ingr_map) {
3588 ret = -ENOMEM;
3589 goto bye;
3590 }
3591
3592 /* Allocate the memory for the vaious egress queue bitmaps
5b377d11 3593 * ie starving_fl, txq_maperr and blocked_fl.
4b8e27a8
HS
3594 */
3595 adap->sge.starving_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3596 sizeof(long), GFP_KERNEL);
3597 if (!adap->sge.starving_fl) {
3598 ret = -ENOMEM;
3599 goto bye;
3600 }
3601
3602 adap->sge.txq_maperr = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3603 sizeof(long), GFP_KERNEL);
3604 if (!adap->sge.txq_maperr) {
3605 ret = -ENOMEM;
3606 goto bye;
3607 }
3608
5b377d11
HS
3609#ifdef CONFIG_DEBUG_FS
3610 adap->sge.blocked_fl = kcalloc(BITS_TO_LONGS(adap->sge.egr_sz),
3611 sizeof(long), GFP_KERNEL);
3612 if (!adap->sge.blocked_fl) {
3613 ret = -ENOMEM;
3614 goto bye;
3615 }
3616#endif
3617
b5a02f50
AB
3618 params[0] = FW_PARAM_PFVF(CLIP_START);
3619 params[1] = FW_PARAM_PFVF(CLIP_END);
b2612722 3620 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
b5a02f50
AB
3621 if (ret < 0)
3622 goto bye;
3623 adap->clipt_start = val[0];
3624 adap->clipt_end = val[1];
3625
b72a32da
RL
3626 /* We don't yet have a PARAMs calls to retrieve the number of Traffic
3627 * Classes supported by the hardware/firmware so we hard code it here
3628 * for now.
3629 */
3630 adap->params.nsched_cls = is_t4(adap->params.chip) ? 15 : 16;
3631
636f9d37
VP
3632 /* query params related to active filter region */
3633 params[0] = FW_PARAM_PFVF(ACTIVE_FILTER_START);
3634 params[1] = FW_PARAM_PFVF(ACTIVE_FILTER_END);
b2612722 3635 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params, val);
636f9d37
VP
3636 /* If Active filter size is set we enable establishing
3637 * offload connection through firmware work request
3638 */
3639 if ((val[0] != val[1]) && (ret >= 0)) {
3640 adap->flags |= FW_OFLD_CONN;
3641 adap->tids.aftid_base = val[0];
3642 adap->tids.aftid_end = val[1];
3643 }
3644
b407a4a9
VP
3645 /* If we're running on newer firmware, let it know that we're
3646 * prepared to deal with encapsulated CPL messages. Older
3647 * firmware won't understand this and we'll just get
3648 * unencapsulated messages ...
3649 */
3650 params[0] = FW_PARAM_PFVF(CPLFW4MSG_ENCAP);
3651 val[0] = 1;
b2612722 3652 (void)t4_set_params(adap, adap->mbox, adap->pf, 0, 1, params, val);
b407a4a9 3653
1ac0f095
KS
3654 /*
3655 * Find out whether we're allowed to use the T5+ ULPTX MEMWRITE DSGL
3656 * capability. Earlier versions of the firmware didn't have the
3657 * ULPTX_MEMWRITE_DSGL so we'll interpret a query failure as no
3658 * permission to use ULPTX MEMWRITE DSGL.
3659 */
3660 if (is_t4(adap->params.chip)) {
3661 adap->params.ulptx_memwrite_dsgl = false;
3662 } else {
3663 params[0] = FW_PARAM_DEV(ULPTX_MEMWRITE_DSGL);
b2612722 3664 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
1ac0f095
KS
3665 1, params, val);
3666 adap->params.ulptx_memwrite_dsgl = (ret == 0 && val[0] != 0);
3667 }
3668
086de575
SW
3669 /* See if FW supports FW_RI_FR_NSMR_TPTE_WR work request */
3670 params[0] = FW_PARAM_DEV(RI_FR_NSMR_TPTE_WR);
3671 ret = t4_query_params(adap, adap->mbox, adap->pf, 0,
3672 1, params, val);
3673 adap->params.fr_nsmr_tpte_wr_support = (ret == 0 && val[0] != 0);
3674
636f9d37
VP
3675 /*
3676 * Get device capabilities so we can determine what resources we need
3677 * to manage.
3678 */
3679 memset(&caps_cmd, 0, sizeof(caps_cmd));
e2ac9628
HS
3680 caps_cmd.op_to_write = htonl(FW_CMD_OP_V(FW_CAPS_CONFIG_CMD) |
3681 FW_CMD_REQUEST_F | FW_CMD_READ_F);
ce91a923 3682 caps_cmd.cfvalid_to_len16 = htonl(FW_LEN16(caps_cmd));
636f9d37
VP
3683 ret = t4_wr_mbox(adap, adap->mbox, &caps_cmd, sizeof(caps_cmd),
3684 &caps_cmd);
3685 if (ret < 0)
3686 goto bye;
3687
13ee15d3 3688 if (caps_cmd.ofldcaps) {
b8ff05a9
DM
3689 /* query offload-related parameters */
3690 params[0] = FW_PARAM_DEV(NTID);
3691 params[1] = FW_PARAM_PFVF(SERVER_START);
3692 params[2] = FW_PARAM_PFVF(SERVER_END);
3693 params[3] = FW_PARAM_PFVF(TDDP_START);
3694 params[4] = FW_PARAM_PFVF(TDDP_END);
3695 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
b2612722 3696 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
636f9d37 3697 params, val);
b8ff05a9
DM
3698 if (ret < 0)
3699 goto bye;
3700 adap->tids.ntids = val[0];
3701 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3702 adap->tids.stid_base = val[1];
3703 adap->tids.nstids = val[2] - val[1] + 1;
636f9d37 3704 /*
dbedd44e 3705 * Setup server filter region. Divide the available filter
636f9d37
VP
3706 * region into two parts. Regular filters get 1/3rd and server
3707 * filters get 2/3rd part. This is only enabled if workarond
3708 * path is enabled.
3709 * 1. For regular filters.
3710 * 2. Server filter: This are special filters which are used
3711 * to redirect SYN packets to offload queue.
3712 */
3713 if (adap->flags & FW_OFLD_CONN && !is_bypass(adap)) {
3714 adap->tids.sftid_base = adap->tids.ftid_base +
3715 DIV_ROUND_UP(adap->tids.nftids, 3);
3716 adap->tids.nsftids = adap->tids.nftids -
3717 DIV_ROUND_UP(adap->tids.nftids, 3);
3718 adap->tids.nftids = adap->tids.sftid_base -
3719 adap->tids.ftid_base;
3720 }
b8ff05a9
DM
3721 adap->vres.ddp.start = val[3];
3722 adap->vres.ddp.size = val[4] - val[3] + 1;
3723 adap->params.ofldq_wr_cred = val[5];
636f9d37 3724
b8ff05a9 3725 adap->params.offload = 1;
0fbc81b3 3726 adap->num_ofld_uld += 1;
b8ff05a9 3727 }
636f9d37 3728 if (caps_cmd.rdmacaps) {
b8ff05a9
DM
3729 params[0] = FW_PARAM_PFVF(STAG_START);
3730 params[1] = FW_PARAM_PFVF(STAG_END);
3731 params[2] = FW_PARAM_PFVF(RQ_START);
3732 params[3] = FW_PARAM_PFVF(RQ_END);
3733 params[4] = FW_PARAM_PFVF(PBL_START);
3734 params[5] = FW_PARAM_PFVF(PBL_END);
b2612722 3735 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6,
636f9d37 3736 params, val);
b8ff05a9
DM
3737 if (ret < 0)
3738 goto bye;
3739 adap->vres.stag.start = val[0];
3740 adap->vres.stag.size = val[1] - val[0] + 1;
3741 adap->vres.rq.start = val[2];
3742 adap->vres.rq.size = val[3] - val[2] + 1;
3743 adap->vres.pbl.start = val[4];
3744 adap->vres.pbl.size = val[5] - val[4] + 1;
a0881cab
DM
3745
3746 params[0] = FW_PARAM_PFVF(SQRQ_START);
3747 params[1] = FW_PARAM_PFVF(SQRQ_END);
3748 params[2] = FW_PARAM_PFVF(CQ_START);
3749 params[3] = FW_PARAM_PFVF(CQ_END);
1ae970e0
DM
3750 params[4] = FW_PARAM_PFVF(OCQ_START);
3751 params[5] = FW_PARAM_PFVF(OCQ_END);
b2612722 3752 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 6, params,
5c937dd3 3753 val);
a0881cab
DM
3754 if (ret < 0)
3755 goto bye;
3756 adap->vres.qp.start = val[0];
3757 adap->vres.qp.size = val[1] - val[0] + 1;
3758 adap->vres.cq.start = val[2];
3759 adap->vres.cq.size = val[3] - val[2] + 1;
1ae970e0
DM
3760 adap->vres.ocq.start = val[4];
3761 adap->vres.ocq.size = val[5] - val[4] + 1;
4c2c5763
HS
3762
3763 params[0] = FW_PARAM_DEV(MAXORDIRD_QP);
3764 params[1] = FW_PARAM_DEV(MAXIRD_ADAPTER);
b2612722 3765 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2, params,
5c937dd3 3766 val);
4c2c5763
HS
3767 if (ret < 0) {
3768 adap->params.max_ordird_qp = 8;
3769 adap->params.max_ird_adapter = 32 * adap->tids.ntids;
3770 ret = 0;
3771 } else {
3772 adap->params.max_ordird_qp = val[0];
3773 adap->params.max_ird_adapter = val[1];
3774 }
3775 dev_info(adap->pdev_dev,
3776 "max_ordird_qp %d max_ird_adapter %d\n",
3777 adap->params.max_ordird_qp,
3778 adap->params.max_ird_adapter);
0fbc81b3 3779 adap->num_ofld_uld += 2;
b8ff05a9 3780 }
636f9d37 3781 if (caps_cmd.iscsicaps) {
b8ff05a9
DM
3782 params[0] = FW_PARAM_PFVF(ISCSI_START);
3783 params[1] = FW_PARAM_PFVF(ISCSI_END);
b2612722 3784 ret = t4_query_params(adap, adap->mbox, adap->pf, 0, 2,
636f9d37 3785 params, val);
b8ff05a9
DM
3786 if (ret < 0)
3787 goto bye;
3788 adap->vres.iscsi.start = val[0];
3789 adap->vres.iscsi.size = val[1] - val[0] + 1;
0fbc81b3
HS
3790 /* LIO target and cxgb4i initiaitor */
3791 adap->num_ofld_uld += 2;
b8ff05a9 3792 }
94cdb8bb
HS
3793 if (caps_cmd.cryptocaps) {
3794 /* Should query params here...TODO */
3795 adap->params.crypto |= ULP_CRYPTO_LOOKASIDE;
3796 adap->num_uld += 1;
3797 }
b8ff05a9
DM
3798#undef FW_PARAM_PFVF
3799#undef FW_PARAM_DEV
3800
92e7ae71
HS
3801 /* The MTU/MSS Table is initialized by now, so load their values. If
3802 * we're initializing the adapter, then we'll make any modifications
3803 * we want to the MTU/MSS Table and also initialize the congestion
3804 * parameters.
636f9d37 3805 */
b8ff05a9 3806 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
92e7ae71
HS
3807 if (state != DEV_STATE_INIT) {
3808 int i;
3809
3810 /* The default MTU Table contains values 1492 and 1500.
3811 * However, for TCP, it's better to have two values which are
3812 * a multiple of 8 +/- 4 bytes apart near this popular MTU.
3813 * This allows us to have a TCP Data Payload which is a
3814 * multiple of 8 regardless of what combination of TCP Options
3815 * are in use (always a multiple of 4 bytes) which is
3816 * important for performance reasons. For instance, if no
3817 * options are in use, then we have a 20-byte IP header and a
3818 * 20-byte TCP header. In this case, a 1500-byte MSS would
3819 * result in a TCP Data Payload of 1500 - 40 == 1460 bytes
3820 * which is not a multiple of 8. So using an MSS of 1488 in
3821 * this case results in a TCP Data Payload of 1448 bytes which
3822 * is a multiple of 8. On the other hand, if 12-byte TCP Time
3823 * Stamps have been negotiated, then an MTU of 1500 bytes
3824 * results in a TCP Data Payload of 1448 bytes which, as
3825 * above, is a multiple of 8 bytes ...
3826 */
3827 for (i = 0; i < NMTUS; i++)
3828 if (adap->params.mtus[i] == 1492) {
3829 adap->params.mtus[i] = 1488;
3830 break;
3831 }
7ee9ff94 3832
92e7ae71
HS
3833 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3834 adap->params.b_wnd);
3835 }
df64e4d3 3836 t4_init_sge_params(adap);
636f9d37 3837 adap->flags |= FW_OK;
c1e9af0c 3838 t4_init_tp_params(adap);
b8ff05a9
DM
3839 return 0;
3840
3841 /*
636f9d37
VP
3842 * Something bad happened. If a command timed out or failed with EIO
3843 * FW does not operate within its spec or something catastrophic
3844 * happened to HW/FW, stop issuing commands.
b8ff05a9 3845 */
636f9d37 3846bye:
4b8e27a8
HS
3847 kfree(adap->sge.egr_map);
3848 kfree(adap->sge.ingr_map);
3849 kfree(adap->sge.starving_fl);
3850 kfree(adap->sge.txq_maperr);
5b377d11
HS
3851#ifdef CONFIG_DEBUG_FS
3852 kfree(adap->sge.blocked_fl);
3853#endif
636f9d37
VP
3854 if (ret != -ETIMEDOUT && ret != -EIO)
3855 t4_fw_bye(adap, adap->mbox);
b8ff05a9
DM
3856 return ret;
3857}
3858
204dc3c0
DM
3859/* EEH callbacks */
3860
3861static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
3862 pci_channel_state_t state)
3863{
3864 int i;
3865 struct adapter *adap = pci_get_drvdata(pdev);
3866
3867 if (!adap)
3868 goto out;
3869
3870 rtnl_lock();
3871 adap->flags &= ~FW_OK;
3872 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
9fe6cb58 3873 spin_lock(&adap->stats_lock);
204dc3c0
DM
3874 for_each_port(adap, i) {
3875 struct net_device *dev = adap->port[i];
3876
3877 netif_device_detach(dev);
3878 netif_carrier_off(dev);
3879 }
9fe6cb58 3880 spin_unlock(&adap->stats_lock);
b37987e8 3881 disable_interrupts(adap);
204dc3c0
DM
3882 if (adap->flags & FULL_INIT_DONE)
3883 cxgb_down(adap);
3884 rtnl_unlock();
144be3d9
GS
3885 if ((adap->flags & DEV_ENABLED)) {
3886 pci_disable_device(pdev);
3887 adap->flags &= ~DEV_ENABLED;
3888 }
204dc3c0
DM
3889out: return state == pci_channel_io_perm_failure ?
3890 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
3891}
3892
3893static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
3894{
3895 int i, ret;
3896 struct fw_caps_config_cmd c;
3897 struct adapter *adap = pci_get_drvdata(pdev);
3898
3899 if (!adap) {
3900 pci_restore_state(pdev);
3901 pci_save_state(pdev);
3902 return PCI_ERS_RESULT_RECOVERED;
3903 }
3904
144be3d9
GS
3905 if (!(adap->flags & DEV_ENABLED)) {
3906 if (pci_enable_device(pdev)) {
3907 dev_err(&pdev->dev, "Cannot reenable PCI "
3908 "device after reset\n");
3909 return PCI_ERS_RESULT_DISCONNECT;
3910 }
3911 adap->flags |= DEV_ENABLED;
204dc3c0
DM
3912 }
3913
3914 pci_set_master(pdev);
3915 pci_restore_state(pdev);
3916 pci_save_state(pdev);
3917 pci_cleanup_aer_uncorrect_error_status(pdev);
3918
8203b509 3919 if (t4_wait_dev_ready(adap->regs) < 0)
204dc3c0 3920 return PCI_ERS_RESULT_DISCONNECT;
b2612722 3921 if (t4_fw_hello(adap, adap->mbox, adap->pf, MASTER_MUST, NULL) < 0)
204dc3c0
DM
3922 return PCI_ERS_RESULT_DISCONNECT;
3923 adap->flags |= FW_OK;
3924 if (adap_init1(adap, &c))
3925 return PCI_ERS_RESULT_DISCONNECT;
3926
3927 for_each_port(adap, i) {
3928 struct port_info *p = adap2pinfo(adap, i);
3929
b2612722 3930 ret = t4_alloc_vi(adap, adap->mbox, p->tx_chan, adap->pf, 0, 1,
060e0c75 3931 NULL, NULL);
204dc3c0
DM
3932 if (ret < 0)
3933 return PCI_ERS_RESULT_DISCONNECT;
3934 p->viid = ret;
3935 p->xact_addr_filt = -1;
3936 }
3937
3938 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3939 adap->params.b_wnd);
1ae970e0 3940 setup_memwin(adap);
204dc3c0
DM
3941 if (cxgb_up(adap))
3942 return PCI_ERS_RESULT_DISCONNECT;
3943 return PCI_ERS_RESULT_RECOVERED;
3944}
3945
3946static void eeh_resume(struct pci_dev *pdev)
3947{
3948 int i;
3949 struct adapter *adap = pci_get_drvdata(pdev);
3950
3951 if (!adap)
3952 return;
3953
3954 rtnl_lock();
3955 for_each_port(adap, i) {
3956 struct net_device *dev = adap->port[i];
3957
3958 if (netif_running(dev)) {
3959 link_start(dev);
3960 cxgb_set_rxmode(dev);
3961 }
3962 netif_device_attach(dev);
3963 }
3964 rtnl_unlock();
3965}
3966
3646f0e5 3967static const struct pci_error_handlers cxgb4_eeh = {
204dc3c0
DM
3968 .error_detected = eeh_err_detected,
3969 .slot_reset = eeh_slot_reset,
3970 .resume = eeh_resume,
3971};
3972
9b86a8d1
HS
3973/* Return true if the Link Configuration supports "High Speeds" (those greater
3974 * than 1Gb/s).
3975 */
57d8b764 3976static inline bool is_x_10g_port(const struct link_config *lc)
b8ff05a9 3977{
9b86a8d1
HS
3978 unsigned int speeds, high_speeds;
3979
3980 speeds = FW_PORT_CAP_SPEED_V(FW_PORT_CAP_SPEED_G(lc->supported));
3981 high_speeds = speeds & ~(FW_PORT_CAP_SPEED_100M | FW_PORT_CAP_SPEED_1G);
3982
3983 return high_speeds != 0;
b8ff05a9
DM
3984}
3985
b8ff05a9
DM
3986/*
3987 * Perform default configuration of DMA queues depending on the number and type
3988 * of ports we found and the number of available CPUs. Most settings can be
3989 * modified by the admin prior to actual use.
3990 */
91744948 3991static void cfg_queues(struct adapter *adap)
b8ff05a9
DM
3992{
3993 struct sge *s = &adap->sge;
688848b1
AB
3994 int i, n10g = 0, qidx = 0;
3995#ifndef CONFIG_CHELSIO_T4_DCB
3996 int q10g = 0;
3997#endif
b8ff05a9 3998
94cdb8bb
HS
3999 /* Reduce memory usage in kdump environment, disable all offload.
4000 */
4001 if (is_kdump_kernel()) {
4002 adap->params.offload = 0;
4003 adap->params.crypto = 0;
0fbc81b3
HS
4004 } else if (is_uld(adap) && t4_uld_mem_alloc(adap)) {
4005 adap->params.offload = 0;
94cdb8bb
HS
4006 adap->params.crypto = 0;
4007 }
4008
b8ff05a9 4009 for_each_port(adap, i)
57d8b764 4010 n10g += is_x_10g_port(&adap2pinfo(adap, i)->link_cfg);
688848b1
AB
4011#ifdef CONFIG_CHELSIO_T4_DCB
4012 /* For Data Center Bridging support we need to be able to support up
4013 * to 8 Traffic Priorities; each of which will be assigned to its
4014 * own TX Queue in order to prevent Head-Of-Line Blocking.
4015 */
4016 if (adap->params.nports * 8 > MAX_ETH_QSETS) {
4017 dev_err(adap->pdev_dev, "MAX_ETH_QSETS=%d < %d!\n",
4018 MAX_ETH_QSETS, adap->params.nports * 8);
4019 BUG_ON(1);
4020 }
b8ff05a9 4021
688848b1
AB
4022 for_each_port(adap, i) {
4023 struct port_info *pi = adap2pinfo(adap, i);
4024
4025 pi->first_qset = qidx;
4026 pi->nqsets = 8;
4027 qidx += pi->nqsets;
4028 }
4029#else /* !CONFIG_CHELSIO_T4_DCB */
b8ff05a9
DM
4030 /*
4031 * We default to 1 queue per non-10G port and up to # of cores queues
4032 * per 10G port.
4033 */
4034 if (n10g)
4035 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
5952dde7
YM
4036 if (q10g > netif_get_num_default_rss_queues())
4037 q10g = netif_get_num_default_rss_queues();
b8ff05a9
DM
4038
4039 for_each_port(adap, i) {
4040 struct port_info *pi = adap2pinfo(adap, i);
4041
4042 pi->first_qset = qidx;
57d8b764 4043 pi->nqsets = is_x_10g_port(&pi->link_cfg) ? q10g : 1;
b8ff05a9
DM
4044 qidx += pi->nqsets;
4045 }
688848b1 4046#endif /* !CONFIG_CHELSIO_T4_DCB */
b8ff05a9
DM
4047
4048 s->ethqsets = qidx;
4049 s->max_ethqsets = qidx; /* MSI-X may lower it later */
4050
0fbc81b3 4051 if (is_uld(adap)) {
b8ff05a9
DM
4052 /*
4053 * For offload we use 1 queue/channel if all ports are up to 1G,
4054 * otherwise we divide all available queues amongst the channels
4055 * capped by the number of available cores.
4056 */
4057 if (n10g) {
a56177e1 4058 i = min_t(int, MAX_OFLD_QSETS, num_online_cpus());
0fbc81b3
HS
4059 s->ofldqsets = roundup(i, adap->params.nports);
4060 } else {
4061 s->ofldqsets = adap->params.nports;
4062 }
b8ff05a9
DM
4063 }
4064
4065 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
4066 struct sge_eth_rxq *r = &s->ethrxq[i];
4067
c887ad0e 4068 init_rspq(adap, &r->rspq, 5, 10, 1024, 64);
b8ff05a9
DM
4069 r->fl.size = 72;
4070 }
4071
4072 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
4073 s->ethtxq[i].q.size = 1024;
4074
4075 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
4076 s->ctrlq[i].q.size = 512;
4077
4078 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
4079 s->ofldtxq[i].q.size = 1024;
4080
c887ad0e 4081 init_rspq(adap, &s->fw_evtq, 0, 1, 1024, 64);
0fbc81b3 4082 init_rspq(adap, &s->intrq, 0, 1, 512, 64);
b8ff05a9
DM
4083}
4084
4085/*
4086 * Reduce the number of Ethernet queues across all ports to at most n.
4087 * n provides at least one queue per port.
4088 */
91744948 4089static void reduce_ethqs(struct adapter *adap, int n)
b8ff05a9
DM
4090{
4091 int i;
4092 struct port_info *pi;
4093
4094 while (n < adap->sge.ethqsets)
4095 for_each_port(adap, i) {
4096 pi = adap2pinfo(adap, i);
4097 if (pi->nqsets > 1) {
4098 pi->nqsets--;
4099 adap->sge.ethqsets--;
4100 if (adap->sge.ethqsets <= n)
4101 break;
4102 }
4103 }
4104
4105 n = 0;
4106 for_each_port(adap, i) {
4107 pi = adap2pinfo(adap, i);
4108 pi->first_qset = n;
4109 n += pi->nqsets;
4110 }
4111}
4112
94cdb8bb
HS
4113static int get_msix_info(struct adapter *adap)
4114{
4115 struct uld_msix_info *msix_info;
0fbc81b3
HS
4116 unsigned int max_ingq = 0;
4117
4118 if (is_offload(adap))
4119 max_ingq += MAX_OFLD_QSETS * adap->num_ofld_uld;
4120 if (is_pci_uld(adap))
4121 max_ingq += MAX_OFLD_QSETS * adap->num_uld;
4122
4123 if (!max_ingq)
4124 goto out;
94cdb8bb
HS
4125
4126 msix_info = kcalloc(max_ingq, sizeof(*msix_info), GFP_KERNEL);
4127 if (!msix_info)
4128 return -ENOMEM;
4129
4130 adap->msix_bmap_ulds.msix_bmap = kcalloc(BITS_TO_LONGS(max_ingq),
4131 sizeof(long), GFP_KERNEL);
4132 if (!adap->msix_bmap_ulds.msix_bmap) {
4133 kfree(msix_info);
4134 return -ENOMEM;
4135 }
4136 spin_lock_init(&adap->msix_bmap_ulds.lock);
4137 adap->msix_info_ulds = msix_info;
0fbc81b3 4138out:
94cdb8bb
HS
4139 return 0;
4140}
4141
4142static void free_msix_info(struct adapter *adap)
4143{
0fbc81b3 4144 if (!(adap->num_uld && adap->num_ofld_uld))
94cdb8bb
HS
4145 return;
4146
4147 kfree(adap->msix_info_ulds);
4148 kfree(adap->msix_bmap_ulds.msix_bmap);
4149}
4150
b8ff05a9
DM
4151/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
4152#define EXTRA_VECS 2
4153
91744948 4154static int enable_msix(struct adapter *adap)
b8ff05a9 4155{
94cdb8bb
HS
4156 int ofld_need = 0, uld_need = 0;
4157 int i, j, want, need, allocated;
b8ff05a9
DM
4158 struct sge *s = &adap->sge;
4159 unsigned int nchan = adap->params.nports;
f36e58e5 4160 struct msix_entry *entries;
94cdb8bb 4161 int max_ingq = MAX_INGQ;
f36e58e5 4162
0fbc81b3
HS
4163 if (is_pci_uld(adap))
4164 max_ingq += (MAX_OFLD_QSETS * adap->num_uld);
4165 if (is_offload(adap))
4166 max_ingq += (MAX_OFLD_QSETS * adap->num_ofld_uld);
94cdb8bb 4167 entries = kmalloc(sizeof(*entries) * (max_ingq + 1),
f36e58e5
HS
4168 GFP_KERNEL);
4169 if (!entries)
4170 return -ENOMEM;
b8ff05a9 4171
94cdb8bb 4172 /* map for msix */
0fbc81b3
HS
4173 if (get_msix_info(adap)) {
4174 adap->params.offload = 0;
94cdb8bb 4175 adap->params.crypto = 0;
0fbc81b3 4176 }
94cdb8bb
HS
4177
4178 for (i = 0; i < max_ingq + 1; ++i)
b8ff05a9
DM
4179 entries[i].entry = i;
4180
4181 want = s->max_ethqsets + EXTRA_VECS;
4182 if (is_offload(adap)) {
0fbc81b3
HS
4183 want += adap->num_ofld_uld * s->ofldqsets;
4184 ofld_need = adap->num_ofld_uld * nchan;
b8ff05a9 4185 }
94cdb8bb 4186 if (is_pci_uld(adap)) {
0fbc81b3
HS
4187 want += adap->num_uld * s->ofldqsets;
4188 uld_need = adap->num_uld * nchan;
94cdb8bb 4189 }
688848b1
AB
4190#ifdef CONFIG_CHELSIO_T4_DCB
4191 /* For Data Center Bridging we need 8 Ethernet TX Priority Queues for
4192 * each port.
4193 */
94cdb8bb 4194 need = 8 * adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
688848b1 4195#else
94cdb8bb 4196 need = adap->params.nports + EXTRA_VECS + ofld_need + uld_need;
688848b1 4197#endif
f36e58e5
HS
4198 allocated = pci_enable_msix_range(adap->pdev, entries, need, want);
4199 if (allocated < 0) {
4200 dev_info(adap->pdev_dev, "not enough MSI-X vectors left,"
4201 " not using MSI-X\n");
4202 kfree(entries);
4203 return allocated;
4204 }
b8ff05a9 4205
f36e58e5 4206 /* Distribute available vectors to the various queue groups.
c32ad224
AG
4207 * Every group gets its minimum requirement and NIC gets top
4208 * priority for leftovers.
4209 */
94cdb8bb 4210 i = allocated - EXTRA_VECS - ofld_need - uld_need;
c32ad224
AG
4211 if (i < s->max_ethqsets) {
4212 s->max_ethqsets = i;
4213 if (i < s->ethqsets)
4214 reduce_ethqs(adap, i);
4215 }
0fbc81b3 4216 if (is_uld(adap)) {
94cdb8bb
HS
4217 if (allocated < want)
4218 s->nqs_per_uld = nchan;
4219 else
0fbc81b3 4220 s->nqs_per_uld = s->ofldqsets;
94cdb8bb
HS
4221 }
4222
0fbc81b3 4223 for (i = 0; i < (s->max_ethqsets + EXTRA_VECS); ++i)
c32ad224 4224 adap->msix_info[i].vec = entries[i].vector;
0fbc81b3
HS
4225 if (is_uld(adap)) {
4226 for (j = 0 ; i < allocated; ++i, j++) {
94cdb8bb 4227 adap->msix_info_ulds[j].vec = entries[i].vector;
0fbc81b3
HS
4228 adap->msix_info_ulds[j].idx = i;
4229 }
94cdb8bb
HS
4230 adap->msix_bmap_ulds.mapsize = j;
4231 }
43eb4e82 4232 dev_info(adap->pdev_dev, "%d MSI-X vectors allocated, "
0fbc81b3
HS
4233 "nic %d per uld %d\n",
4234 allocated, s->max_ethqsets, s->nqs_per_uld);
c32ad224 4235
f36e58e5 4236 kfree(entries);
c32ad224 4237 return 0;
b8ff05a9
DM
4238}
4239
4240#undef EXTRA_VECS
4241
91744948 4242static int init_rss(struct adapter *adap)
671b0060 4243{
c035e183
HS
4244 unsigned int i;
4245 int err;
4246
4247 err = t4_init_rss_mode(adap, adap->mbox);
4248 if (err)
4249 return err;
671b0060
DM
4250
4251 for_each_port(adap, i) {
4252 struct port_info *pi = adap2pinfo(adap, i);
4253
4254 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
4255 if (!pi->rss)
4256 return -ENOMEM;
671b0060
DM
4257 }
4258 return 0;
4259}
4260
547fd272
HS
4261static int cxgb4_get_pcie_dev_link_caps(struct adapter *adap,
4262 enum pci_bus_speed *speed,
4263 enum pcie_link_width *width)
4264{
4265 u32 lnkcap1, lnkcap2;
4266 int err1, err2;
4267
4268#define PCIE_MLW_CAP_SHIFT 4 /* start of MLW mask in link capabilities */
4269
4270 *speed = PCI_SPEED_UNKNOWN;
4271 *width = PCIE_LNK_WIDTH_UNKNOWN;
4272
4273 err1 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP,
4274 &lnkcap1);
4275 err2 = pcie_capability_read_dword(adap->pdev, PCI_EXP_LNKCAP2,
4276 &lnkcap2);
4277 if (!err2 && lnkcap2) { /* PCIe r3.0-compliant */
4278 if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_8_0GB)
4279 *speed = PCIE_SPEED_8_0GT;
4280 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_5_0GB)
4281 *speed = PCIE_SPEED_5_0GT;
4282 else if (lnkcap2 & PCI_EXP_LNKCAP2_SLS_2_5GB)
4283 *speed = PCIE_SPEED_2_5GT;
4284 }
4285 if (!err1) {
4286 *width = (lnkcap1 & PCI_EXP_LNKCAP_MLW) >> PCIE_MLW_CAP_SHIFT;
4287 if (!lnkcap2) { /* pre-r3.0 */
4288 if (lnkcap1 & PCI_EXP_LNKCAP_SLS_5_0GB)
4289 *speed = PCIE_SPEED_5_0GT;
4290 else if (lnkcap1 & PCI_EXP_LNKCAP_SLS_2_5GB)
4291 *speed = PCIE_SPEED_2_5GT;
4292 }
4293 }
4294
4295 if (*speed == PCI_SPEED_UNKNOWN || *width == PCIE_LNK_WIDTH_UNKNOWN)
4296 return err1 ? err1 : err2 ? err2 : -EINVAL;
4297 return 0;
4298}
4299
4300static void cxgb4_check_pcie_caps(struct adapter *adap)
4301{
4302 enum pcie_link_width width, width_cap;
4303 enum pci_bus_speed speed, speed_cap;
4304
4305#define PCIE_SPEED_STR(speed) \
4306 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" : \
4307 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" : \
4308 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" : \
4309 "Unknown")
4310
4311 if (cxgb4_get_pcie_dev_link_caps(adap, &speed_cap, &width_cap)) {
4312 dev_warn(adap->pdev_dev,
4313 "Unable to determine PCIe device BW capabilities\n");
4314 return;
4315 }
4316
4317 if (pcie_get_minimum_link(adap->pdev, &speed, &width) ||
4318 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
4319 dev_warn(adap->pdev_dev,
4320 "Unable to determine PCI Express bandwidth.\n");
4321 return;
4322 }
4323
4324 dev_info(adap->pdev_dev, "PCIe link speed is %s, device supports %s\n",
4325 PCIE_SPEED_STR(speed), PCIE_SPEED_STR(speed_cap));
4326 dev_info(adap->pdev_dev, "PCIe link width is x%d, device supports x%d\n",
4327 width, width_cap);
4328 if (speed < speed_cap || width < width_cap)
4329 dev_info(adap->pdev_dev,
4330 "A slot with more lanes and/or higher speed is "
4331 "suggested for optimal performance.\n");
4332}
4333
0de72738
HS
4334/* Dump basic information about the adapter */
4335static void print_adapter_info(struct adapter *adapter)
4336{
4337 /* Device information */
4338 dev_info(adapter->pdev_dev, "Chelsio %s rev %d\n",
4339 adapter->params.vpd.id,
4340 CHELSIO_CHIP_RELEASE(adapter->params.chip));
4341 dev_info(adapter->pdev_dev, "S/N: %s, P/N: %s\n",
4342 adapter->params.vpd.sn, adapter->params.vpd.pn);
4343
4344 /* Firmware Version */
4345 if (!adapter->params.fw_vers)
4346 dev_warn(adapter->pdev_dev, "No firmware loaded\n");
4347 else
4348 dev_info(adapter->pdev_dev, "Firmware version: %u.%u.%u.%u\n",
4349 FW_HDR_FW_VER_MAJOR_G(adapter->params.fw_vers),
4350 FW_HDR_FW_VER_MINOR_G(adapter->params.fw_vers),
4351 FW_HDR_FW_VER_MICRO_G(adapter->params.fw_vers),
4352 FW_HDR_FW_VER_BUILD_G(adapter->params.fw_vers));
4353
4354 /* Bootstrap Firmware Version. (Some adapters don't have Bootstrap
4355 * Firmware, so dev_info() is more appropriate here.)
4356 */
4357 if (!adapter->params.bs_vers)
4358 dev_info(adapter->pdev_dev, "No bootstrap loaded\n");
4359 else
4360 dev_info(adapter->pdev_dev, "Bootstrap version: %u.%u.%u.%u\n",
4361 FW_HDR_FW_VER_MAJOR_G(adapter->params.bs_vers),
4362 FW_HDR_FW_VER_MINOR_G(adapter->params.bs_vers),
4363 FW_HDR_FW_VER_MICRO_G(adapter->params.bs_vers),
4364 FW_HDR_FW_VER_BUILD_G(adapter->params.bs_vers));
4365
4366 /* TP Microcode Version */
4367 if (!adapter->params.tp_vers)
4368 dev_warn(adapter->pdev_dev, "No TP Microcode loaded\n");
4369 else
4370 dev_info(adapter->pdev_dev,
4371 "TP Microcode version: %u.%u.%u.%u\n",
4372 FW_HDR_FW_VER_MAJOR_G(adapter->params.tp_vers),
4373 FW_HDR_FW_VER_MINOR_G(adapter->params.tp_vers),
4374 FW_HDR_FW_VER_MICRO_G(adapter->params.tp_vers),
4375 FW_HDR_FW_VER_BUILD_G(adapter->params.tp_vers));
4376
4377 /* Expansion ROM version */
4378 if (!adapter->params.er_vers)
4379 dev_info(adapter->pdev_dev, "No Expansion ROM loaded\n");
4380 else
4381 dev_info(adapter->pdev_dev,
4382 "Expansion ROM version: %u.%u.%u.%u\n",
4383 FW_HDR_FW_VER_MAJOR_G(adapter->params.er_vers),
4384 FW_HDR_FW_VER_MINOR_G(adapter->params.er_vers),
4385 FW_HDR_FW_VER_MICRO_G(adapter->params.er_vers),
4386 FW_HDR_FW_VER_BUILD_G(adapter->params.er_vers));
4387
4388 /* Software/Hardware configuration */
4389 dev_info(adapter->pdev_dev, "Configuration: %sNIC %s, %s capable\n",
4390 is_offload(adapter) ? "R" : "",
4391 ((adapter->flags & USING_MSIX) ? "MSI-X" :
4392 (adapter->flags & USING_MSI) ? "MSI" : ""),
4393 is_offload(adapter) ? "Offload" : "non-Offload");
4394}
4395
91744948 4396static void print_port_info(const struct net_device *dev)
b8ff05a9 4397{
b8ff05a9 4398 char buf[80];
118969ed 4399 char *bufp = buf;
f1a051b9 4400 const char *spd = "";
118969ed
DM
4401 const struct port_info *pi = netdev_priv(dev);
4402 const struct adapter *adap = pi->adapter;
f1a051b9
DM
4403
4404 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
4405 spd = " 2.5 GT/s";
4406 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
4407 spd = " 5 GT/s";
d2e752db
RD
4408 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_8_0GB)
4409 spd = " 8 GT/s";
b8ff05a9 4410
118969ed
DM
4411 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
4412 bufp += sprintf(bufp, "100/");
4413 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
4414 bufp += sprintf(bufp, "1000/");
4415 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
4416 bufp += sprintf(bufp, "10G/");
9b86a8d1
HS
4417 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_25G)
4418 bufp += sprintf(bufp, "25G/");
72aca4bf
KS
4419 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_40G)
4420 bufp += sprintf(bufp, "40G/");
9b86a8d1
HS
4421 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100G)
4422 bufp += sprintf(bufp, "100G/");
118969ed
DM
4423 if (bufp != buf)
4424 --bufp;
72aca4bf 4425 sprintf(bufp, "BASE-%s", t4_get_port_type_description(pi->port_type));
118969ed 4426
0de72738
HS
4427 netdev_info(dev, "%s: Chelsio %s (%s) %s\n",
4428 dev->name, adap->params.vpd.id, adap->name, buf);
b8ff05a9
DM
4429}
4430
91744948 4431static void enable_pcie_relaxed_ordering(struct pci_dev *dev)
ef306b50 4432{
e5c8ae5f 4433 pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
ef306b50
DM
4434}
4435
06546391
DM
4436/*
4437 * Free the following resources:
4438 * - memory used for tables
4439 * - MSI/MSI-X
4440 * - net devices
4441 * - resources FW is holding for us
4442 */
4443static void free_some_resources(struct adapter *adapter)
4444{
4445 unsigned int i;
4446
4447 t4_free_mem(adapter->l2t);
b72a32da 4448 t4_cleanup_sched(adapter);
06546391 4449 t4_free_mem(adapter->tids.tid_tab);
d8931847 4450 cxgb4_cleanup_tc_u32(adapter);
4b8e27a8
HS
4451 kfree(adapter->sge.egr_map);
4452 kfree(adapter->sge.ingr_map);
4453 kfree(adapter->sge.starving_fl);
4454 kfree(adapter->sge.txq_maperr);
5b377d11
HS
4455#ifdef CONFIG_DEBUG_FS
4456 kfree(adapter->sge.blocked_fl);
4457#endif
06546391
DM
4458 disable_msi(adapter);
4459
4460 for_each_port(adapter, i)
671b0060 4461 if (adapter->port[i]) {
4f3a0fcf
HS
4462 struct port_info *pi = adap2pinfo(adapter, i);
4463
4464 if (pi->viid != 0)
4465 t4_free_vi(adapter, adapter->mbox, adapter->pf,
4466 0, pi->viid);
671b0060 4467 kfree(adap2pinfo(adapter, i)->rss);
06546391 4468 free_netdev(adapter->port[i]);
671b0060 4469 }
06546391 4470 if (adapter->flags & FW_OK)
b2612722 4471 t4_fw_bye(adapter, adapter->pf);
06546391
DM
4472}
4473
2ed28baa 4474#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
35d35682 4475#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
b8ff05a9 4476 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
22adfe0a 4477#define SEGMENT_SIZE 128
b8ff05a9 4478
d86bd29e
HS
4479static int get_chip_type(struct pci_dev *pdev, u32 pl_rev)
4480{
d86bd29e
HS
4481 u16 device_id;
4482
4483 /* Retrieve adapter's device ID */
4484 pci_read_config_word(pdev, PCI_DEVICE_ID, &device_id);
46cdc9be 4485
4486 switch (device_id >> 12) {
d86bd29e 4487 case CHELSIO_T4:
46cdc9be 4488 return CHELSIO_CHIP_CODE(CHELSIO_T4, pl_rev);
d86bd29e 4489 case CHELSIO_T5:
46cdc9be 4490 return CHELSIO_CHIP_CODE(CHELSIO_T5, pl_rev);
d86bd29e 4491 case CHELSIO_T6:
46cdc9be 4492 return CHELSIO_CHIP_CODE(CHELSIO_T6, pl_rev);
d86bd29e
HS
4493 default:
4494 dev_err(&pdev->dev, "Device %d is not supported\n",
4495 device_id);
d86bd29e 4496 }
46cdc9be 4497 return -EINVAL;
d86bd29e
HS
4498}
4499
b6244201 4500#ifdef CONFIG_PCI_IOV
e7b48a32
HS
4501static void dummy_setup(struct net_device *dev)
4502{
4503 dev->type = ARPHRD_NONE;
4504 dev->mtu = 0;
4505 dev->hard_header_len = 0;
4506 dev->addr_len = 0;
4507 dev->tx_queue_len = 0;
4508 dev->flags |= IFF_NOARP;
4509 dev->priv_flags |= IFF_NO_QUEUE;
4510
4511 /* Initialize the device structure. */
4512 dev->netdev_ops = &cxgb4_mgmt_netdev_ops;
4513 dev->ethtool_ops = &cxgb4_mgmt_ethtool_ops;
4514 dev->destructor = free_netdev;
4515}
4516
4517static int config_mgmt_dev(struct pci_dev *pdev)
4518{
4519 struct adapter *adap = pci_get_drvdata(pdev);
4520 struct net_device *netdev;
4521 struct port_info *pi;
4522 char name[IFNAMSIZ];
4523 int err;
4524
4525 snprintf(name, IFNAMSIZ, "mgmtpf%d%d", adap->adap_idx, adap->pf);
4526 netdev = alloc_netdev(0, name, NET_NAME_UNKNOWN, dummy_setup);
4527 if (!netdev)
4528 return -ENOMEM;
4529
4530 pi = netdev_priv(netdev);
4531 pi->adapter = adap;
4532 SET_NETDEV_DEV(netdev, &pdev->dev);
4533
4534 adap->port[0] = netdev;
4535
4536 err = register_netdev(adap->port[0]);
4537 if (err) {
4538 pr_info("Unable to register VF mgmt netdev %s\n", name);
4539 free_netdev(adap->port[0]);
4540 adap->port[0] = NULL;
4541 return err;
4542 }
4543 return 0;
4544}
4545
b6244201
HS
4546static int cxgb4_iov_configure(struct pci_dev *pdev, int num_vfs)
4547{
7829451c 4548 struct adapter *adap = pci_get_drvdata(pdev);
b6244201
HS
4549 int err = 0;
4550 int current_vfs = pci_num_vf(pdev);
4551 u32 pcie_fw;
b6244201 4552
7829451c 4553 pcie_fw = readl(adap->regs + PCIE_FW_A);
b6244201
HS
4554 /* Check if cxgb4 is the MASTER and fw is initialized */
4555 if (!(pcie_fw & PCIE_FW_INIT_F) ||
4556 !(pcie_fw & PCIE_FW_MASTER_VLD_F) ||
4557 PCIE_FW_MASTER_G(pcie_fw) != 4) {
4558 dev_warn(&pdev->dev,
4559 "cxgb4 driver needs to be MASTER to support SRIOV\n");
4560 return -EOPNOTSUPP;
4561 }
4562
4563 /* If any of the VF's is already assigned to Guest OS, then
4564 * SRIOV for the same cannot be modified
4565 */
4566 if (current_vfs && pci_vfs_assigned(pdev)) {
4567 dev_err(&pdev->dev,
4568 "Cannot modify SR-IOV while VFs are assigned\n");
4569 num_vfs = current_vfs;
4570 return num_vfs;
4571 }
4572
4573 /* Disable SRIOV when zero is passed.
4574 * One needs to disable SRIOV before modifying it, else
4575 * stack throws the below warning:
4576 * " 'n' VFs already enabled. Disable before enabling 'm' VFs."
4577 */
4578 if (!num_vfs) {
4579 pci_disable_sriov(pdev);
e7b48a32 4580 if (adap->port[0]) {
7829451c 4581 unregister_netdev(adap->port[0]);
e7b48a32
HS
4582 adap->port[0] = NULL;
4583 }
661dbeb9
HS
4584 /* free VF resources */
4585 kfree(adap->vfinfo);
4586 adap->vfinfo = NULL;
4587 adap->num_vfs = 0;
b6244201
HS
4588 return num_vfs;
4589 }
4590
4591 if (num_vfs != current_vfs) {
4592 err = pci_enable_sriov(pdev, num_vfs);
4593 if (err)
4594 return err;
7829451c 4595
661dbeb9 4596 adap->num_vfs = num_vfs;
e7b48a32
HS
4597 err = config_mgmt_dev(pdev);
4598 if (err)
4599 return err;
b6244201 4600 }
661dbeb9
HS
4601
4602 adap->vfinfo = kcalloc(adap->num_vfs,
4603 sizeof(struct vf_info), GFP_KERNEL);
4604 if (adap->vfinfo)
4605 fill_vf_station_mac_addr(adap);
b6244201
HS
4606 return num_vfs;
4607}
4608#endif
4609
1dd06ae8 4610static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
b8ff05a9 4611{
22adfe0a 4612 int func, i, err, s_qpp, qpp, num_seg;
b8ff05a9 4613 struct port_info *pi;
c8f44aff 4614 bool highdma = false;
b8ff05a9 4615 struct adapter *adapter = NULL;
7829451c 4616 struct net_device *netdev;
d6ce2628 4617 void __iomem *regs;
d86bd29e
HS
4618 u32 whoami, pl_rev;
4619 enum chip_type chip;
7829451c 4620 static int adap_idx = 1;
b8ff05a9
DM
4621
4622 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
4623
4624 err = pci_request_regions(pdev, KBUILD_MODNAME);
4625 if (err) {
4626 /* Just info, some other driver may have claimed the device. */
4627 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
4628 return err;
4629 }
4630
b8ff05a9
DM
4631 err = pci_enable_device(pdev);
4632 if (err) {
4633 dev_err(&pdev->dev, "cannot enable PCI device\n");
4634 goto out_release_regions;
4635 }
4636
d6ce2628
HS
4637 regs = pci_ioremap_bar(pdev, 0);
4638 if (!regs) {
4639 dev_err(&pdev->dev, "cannot map device registers\n");
4640 err = -ENOMEM;
4641 goto out_disable_device;
4642 }
4643
8203b509
HS
4644 err = t4_wait_dev_ready(regs);
4645 if (err < 0)
4646 goto out_unmap_bar0;
4647
d6ce2628 4648 /* We control everything through one PF */
d86bd29e
HS
4649 whoami = readl(regs + PL_WHOAMI_A);
4650 pl_rev = REV_G(readl(regs + PL_REV_A));
4651 chip = get_chip_type(pdev, pl_rev);
4652 func = CHELSIO_CHIP_VERSION(chip) <= CHELSIO_T5 ?
4653 SOURCEPF_G(whoami) : T6_SOURCEPF_G(whoami);
d6ce2628 4654 if (func != ent->driver_data) {
7829451c 4655#ifndef CONFIG_PCI_IOV
d6ce2628 4656 iounmap(regs);
7829451c 4657#endif
d6ce2628
HS
4658 pci_disable_device(pdev);
4659 pci_save_state(pdev); /* to restore SR-IOV later */
4660 goto sriov;
4661 }
4662
b8ff05a9 4663 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
c8f44aff 4664 highdma = true;
b8ff05a9
DM
4665 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4666 if (err) {
4667 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
4668 "coherent allocations\n");
d6ce2628 4669 goto out_unmap_bar0;
b8ff05a9
DM
4670 }
4671 } else {
4672 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4673 if (err) {
4674 dev_err(&pdev->dev, "no usable DMA configuration\n");
d6ce2628 4675 goto out_unmap_bar0;
b8ff05a9
DM
4676 }
4677 }
4678
4679 pci_enable_pcie_error_reporting(pdev);
ef306b50 4680 enable_pcie_relaxed_ordering(pdev);
b8ff05a9
DM
4681 pci_set_master(pdev);
4682 pci_save_state(pdev);
4683
4684 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4685 if (!adapter) {
4686 err = -ENOMEM;
d6ce2628 4687 goto out_unmap_bar0;
b8ff05a9 4688 }
7829451c 4689 adap_idx++;
b8ff05a9 4690
29aaee65
AB
4691 adapter->workq = create_singlethread_workqueue("cxgb4");
4692 if (!adapter->workq) {
4693 err = -ENOMEM;
4694 goto out_free_adapter;
4695 }
4696
7f080c3f
HS
4697 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
4698 (sizeof(struct mbox_cmd) *
4699 T4_OS_LOG_MBOX_CMDS),
4700 GFP_KERNEL);
4701 if (!adapter->mbox_log) {
4702 err = -ENOMEM;
4703 goto out_free_adapter;
4704 }
4705 adapter->mbox_log->size = T4_OS_LOG_MBOX_CMDS;
4706
144be3d9
GS
4707 /* PCI device has been enabled */
4708 adapter->flags |= DEV_ENABLED;
4709
d6ce2628 4710 adapter->regs = regs;
b8ff05a9
DM
4711 adapter->pdev = pdev;
4712 adapter->pdev_dev = &pdev->dev;
0de72738 4713 adapter->name = pci_name(pdev);
3069ee9b 4714 adapter->mbox = func;
b2612722 4715 adapter->pf = func;
b8ff05a9
DM
4716 adapter->msg_enable = dflt_msg_enable;
4717 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
4718
4719 spin_lock_init(&adapter->stats_lock);
4720 spin_lock_init(&adapter->tid_release_lock);
e327c225 4721 spin_lock_init(&adapter->win0_lock);
b8ff05a9
DM
4722
4723 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
881806bc
VP
4724 INIT_WORK(&adapter->db_full_task, process_db_full);
4725 INIT_WORK(&adapter->db_drop_task, process_db_drop);
b8ff05a9
DM
4726
4727 err = t4_prep_adapter(adapter);
4728 if (err)
d6ce2628
HS
4729 goto out_free_adapter;
4730
22adfe0a 4731
d14807dd 4732 if (!is_t4(adapter->params.chip)) {
f612b815
HS
4733 s_qpp = (QUEUESPERPAGEPF0_S +
4734 (QUEUESPERPAGEPF1_S - QUEUESPERPAGEPF0_S) *
b2612722 4735 adapter->pf);
f612b815
HS
4736 qpp = 1 << QUEUESPERPAGEPF0_G(t4_read_reg(adapter,
4737 SGE_EGRESS_QUEUES_PER_PAGE_PF_A) >> s_qpp);
22adfe0a
SR
4738 num_seg = PAGE_SIZE / SEGMENT_SIZE;
4739
4740 /* Each segment size is 128B. Write coalescing is enabled only
4741 * when SGE_EGRESS_QUEUES_PER_PAGE_PF reg value for the
4742 * queue is less no of segments that can be accommodated in
4743 * a page size.
4744 */
4745 if (qpp > num_seg) {
4746 dev_err(&pdev->dev,
4747 "Incorrect number of egress queues per page\n");
4748 err = -EINVAL;
d6ce2628 4749 goto out_free_adapter;
22adfe0a
SR
4750 }
4751 adapter->bar2 = ioremap_wc(pci_resource_start(pdev, 2),
4752 pci_resource_len(pdev, 2));
4753 if (!adapter->bar2) {
4754 dev_err(&pdev->dev, "cannot map device bar2 region\n");
4755 err = -ENOMEM;
d6ce2628 4756 goto out_free_adapter;
22adfe0a
SR
4757 }
4758 }
4759
636f9d37 4760 setup_memwin(adapter);
b8ff05a9 4761 err = adap_init0(adapter);
5b377d11
HS
4762#ifdef CONFIG_DEBUG_FS
4763 bitmap_zero(adapter->sge.blocked_fl, adapter->sge.egr_sz);
4764#endif
636f9d37 4765 setup_memwin_rdma(adapter);
b8ff05a9
DM
4766 if (err)
4767 goto out_unmap_bar;
4768
2a485cf7
HS
4769 /* configure SGE_STAT_CFG_A to read WC stats */
4770 if (!is_t4(adapter->params.chip))
676d6a75
HS
4771 t4_write_reg(adapter, SGE_STAT_CFG_A, STATSOURCE_T5_V(7) |
4772 (is_t5(adapter->params.chip) ? STATMODE_V(0) :
4773 T6_STATMODE_V(0)));
2a485cf7 4774
b8ff05a9 4775 for_each_port(adapter, i) {
b8ff05a9
DM
4776 netdev = alloc_etherdev_mq(sizeof(struct port_info),
4777 MAX_ETH_QSETS);
4778 if (!netdev) {
4779 err = -ENOMEM;
4780 goto out_free_dev;
4781 }
4782
4783 SET_NETDEV_DEV(netdev, &pdev->dev);
4784
4785 adapter->port[i] = netdev;
4786 pi = netdev_priv(netdev);
4787 pi->adapter = adapter;
4788 pi->xact_addr_filt = -1;
b8ff05a9 4789 pi->port_id = i;
b8ff05a9
DM
4790 netdev->irq = pdev->irq;
4791
2ed28baa
MM
4792 netdev->hw_features = NETIF_F_SG | TSO_FLAGS |
4793 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4794 NETIF_F_RXCSUM | NETIF_F_RXHASH |
d8931847
RL
4795 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
4796 NETIF_F_HW_TC;
c8f44aff
MM
4797 if (highdma)
4798 netdev->hw_features |= NETIF_F_HIGHDMA;
4799 netdev->features |= netdev->hw_features;
b8ff05a9
DM
4800 netdev->vlan_features = netdev->features & VLAN_FEAT;
4801
01789349
JP
4802 netdev->priv_flags |= IFF_UNICAST_FLT;
4803
d894be57
JW
4804 /* MTU range: 81 - 9600 */
4805 netdev->min_mtu = 81;
4806 netdev->max_mtu = MAX_MTU;
4807
b8ff05a9 4808 netdev->netdev_ops = &cxgb4_netdev_ops;
688848b1
AB
4809#ifdef CONFIG_CHELSIO_T4_DCB
4810 netdev->dcbnl_ops = &cxgb4_dcb_ops;
4811 cxgb4_dcb_state_init(netdev);
4812#endif
812034f1 4813 cxgb4_set_ethtool_ops(netdev);
b8ff05a9
DM
4814 }
4815
4816 pci_set_drvdata(pdev, adapter);
4817
4818 if (adapter->flags & FW_OK) {
060e0c75 4819 err = t4_port_init(adapter, func, func, 0);
b8ff05a9
DM
4820 if (err)
4821 goto out_free_dev;
098ef6c2
HS
4822 } else if (adapter->params.nports == 1) {
4823 /* If we don't have a connection to the firmware -- possibly
4824 * because of an error -- grab the raw VPD parameters so we
4825 * can set the proper MAC Address on the debug network
4826 * interface that we've created.
4827 */
4828 u8 hw_addr[ETH_ALEN];
4829 u8 *na = adapter->params.vpd.na;
4830
4831 err = t4_get_raw_vpd_params(adapter, &adapter->params.vpd);
4832 if (!err) {
4833 for (i = 0; i < ETH_ALEN; i++)
4834 hw_addr[i] = (hex2val(na[2 * i + 0]) * 16 +
4835 hex2val(na[2 * i + 1]));
4836 t4_set_hw_addr(adapter, 0, hw_addr);
4837 }
b8ff05a9
DM
4838 }
4839
098ef6c2 4840 /* Configure queues and allocate tables now, they can be needed as
b8ff05a9
DM
4841 * soon as the first register_netdev completes.
4842 */
4843 cfg_queues(adapter);
4844
5be9ed8d 4845 adapter->l2t = t4_init_l2t(adapter->l2t_start, adapter->l2t_end);
b8ff05a9
DM
4846 if (!adapter->l2t) {
4847 /* We tolerate a lack of L2T, giving up some functionality */
4848 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
4849 adapter->params.offload = 0;
4850 }
4851
b5a02f50 4852#if IS_ENABLED(CONFIG_IPV6)
eb72f74f
HS
4853 if ((CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) &&
4854 (!(t4_read_reg(adapter, LE_DB_CONFIG_A) & ASLIPCOMPEN_F))) {
4855 /* CLIP functionality is not present in hardware,
4856 * hence disable all offload features
b5a02f50
AB
4857 */
4858 dev_warn(&pdev->dev,
eb72f74f 4859 "CLIP not enabled in hardware, continuing\n");
b5a02f50 4860 adapter->params.offload = 0;
eb72f74f
HS
4861 } else {
4862 adapter->clipt = t4_init_clip_tbl(adapter->clipt_start,
4863 adapter->clipt_end);
4864 if (!adapter->clipt) {
4865 /* We tolerate a lack of clip_table, giving up
4866 * some functionality
4867 */
4868 dev_warn(&pdev->dev,
4869 "could not allocate Clip table, continuing\n");
4870 adapter->params.offload = 0;
4871 }
b5a02f50
AB
4872 }
4873#endif
b72a32da
RL
4874
4875 for_each_port(adapter, i) {
4876 pi = adap2pinfo(adapter, i);
4877 pi->sched_tbl = t4_init_sched(adapter->params.nsched_cls);
4878 if (!pi->sched_tbl)
4879 dev_warn(&pdev->dev,
4880 "could not activate scheduling on port %d\n",
4881 i);
4882 }
4883
578b46b9 4884 if (tid_init(&adapter->tids) < 0) {
b8ff05a9
DM
4885 dev_warn(&pdev->dev, "could not allocate TID table, "
4886 "continuing\n");
4887 adapter->params.offload = 0;
d8931847
RL
4888 } else {
4889 adapter->tc_u32 = cxgb4_init_tc_u32(adapter,
4890 CXGB4_MAX_LINK_HANDLE);
4891 if (!adapter->tc_u32)
4892 dev_warn(&pdev->dev,
4893 "could not offload tc u32, continuing\n");
b8ff05a9
DM
4894 }
4895
9a1bb9f6
HS
4896 if (is_offload(adapter)) {
4897 if (t4_read_reg(adapter, LE_DB_CONFIG_A) & HASHEN_F) {
4898 u32 hash_base, hash_reg;
4899
4900 if (chip <= CHELSIO_T5) {
4901 hash_reg = LE_DB_TID_HASHBASE_A;
4902 hash_base = t4_read_reg(adapter, hash_reg);
4903 adapter->tids.hash_base = hash_base / 4;
4904 } else {
4905 hash_reg = T6_LE_DB_HASH_TID_BASE_A;
4906 hash_base = t4_read_reg(adapter, hash_reg);
4907 adapter->tids.hash_base = hash_base;
4908 }
4909 }
4910 }
4911
f7cabcdd
DM
4912 /* See what interrupts we'll be using */
4913 if (msi > 1 && enable_msix(adapter) == 0)
4914 adapter->flags |= USING_MSIX;
94cdb8bb 4915 else if (msi > 0 && pci_enable_msi(pdev) == 0) {
f7cabcdd 4916 adapter->flags |= USING_MSI;
94cdb8bb
HS
4917 if (msi > 1)
4918 free_msix_info(adapter);
4919 }
f7cabcdd 4920
547fd272
HS
4921 /* check for PCI Express bandwidth capabiltites */
4922 cxgb4_check_pcie_caps(adapter);
4923
671b0060
DM
4924 err = init_rss(adapter);
4925 if (err)
4926 goto out_free_dev;
4927
b8ff05a9
DM
4928 /*
4929 * The card is now ready to go. If any errors occur during device
4930 * registration we do not fail the whole card but rather proceed only
4931 * with the ports we manage to register successfully. However we must
4932 * register at least one net device.
4933 */
4934 for_each_port(adapter, i) {
a57cabe0
DM
4935 pi = adap2pinfo(adapter, i);
4936 netif_set_real_num_tx_queues(adapter->port[i], pi->nqsets);
4937 netif_set_real_num_rx_queues(adapter->port[i], pi->nqsets);
4938
b8ff05a9
DM
4939 err = register_netdev(adapter->port[i]);
4940 if (err)
b1a3c2b6 4941 break;
b1a3c2b6
DM
4942 adapter->chan_map[pi->tx_chan] = i;
4943 print_port_info(adapter->port[i]);
b8ff05a9 4944 }
b1a3c2b6 4945 if (i == 0) {
b8ff05a9
DM
4946 dev_err(&pdev->dev, "could not register any net devices\n");
4947 goto out_free_dev;
4948 }
b1a3c2b6
DM
4949 if (err) {
4950 dev_warn(&pdev->dev, "only %d net devices registered\n", i);
4951 err = 0;
6403eab1 4952 }
b8ff05a9
DM
4953
4954 if (cxgb4_debugfs_root) {
4955 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
4956 cxgb4_debugfs_root);
4957 setup_debugfs(adapter);
4958 }
4959
6482aa7c
DLR
4960 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
4961 pdev->needs_freset = 1;
4962
0fbc81b3
HS
4963 if (is_uld(adapter)) {
4964 mutex_lock(&uld_mutex);
4965 list_add_tail(&adapter->list_node, &adapter_list);
4966 mutex_unlock(&uld_mutex);
4967 }
b8ff05a9 4968
0de72738 4969 print_adapter_info(adapter);
0fbc81b3 4970 setup_fw_sge_queues(adapter);
7829451c 4971 return 0;
0de72738 4972
8e1e6059 4973sriov:
b8ff05a9 4974#ifdef CONFIG_PCI_IOV
b6244201
HS
4975 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0) {
4976 dev_warn(&pdev->dev,
4977 "Enabling SR-IOV VFs using the num_vf module "
4978 "parameter is deprecated - please use the pci sysfs "
4979 "interface instead.\n");
b8ff05a9
DM
4980 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
4981 dev_info(&pdev->dev,
4982 "instantiated %u virtual functions\n",
4983 num_vf[func]);
b6244201 4984 }
7829451c
HS
4985
4986 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
4987 if (!adapter) {
4988 err = -ENOMEM;
4989 goto free_pci_region;
4990 }
4991
7829451c
HS
4992 adapter->pdev = pdev;
4993 adapter->pdev_dev = &pdev->dev;
4994 adapter->name = pci_name(pdev);
4995 adapter->mbox = func;
4996 adapter->pf = func;
4997 adapter->regs = regs;
e7b48a32 4998 adapter->adap_idx = adap_idx;
7829451c
HS
4999 adapter->mbox_log = kzalloc(sizeof(*adapter->mbox_log) +
5000 (sizeof(struct mbox_cmd) *
5001 T4_OS_LOG_MBOX_CMDS),
5002 GFP_KERNEL);
5003 if (!adapter->mbox_log) {
5004 err = -ENOMEM;
e7b48a32 5005 goto free_adapter;
7829451c 5006 }
7829451c 5007 pci_set_drvdata(pdev, adapter);
7829451c
HS
5008 return 0;
5009
7829451c
HS
5010 free_adapter:
5011 kfree(adapter);
5012 free_pci_region:
5013 iounmap(regs);
5014 pci_disable_sriov(pdev);
5015 pci_release_regions(pdev);
5016 return err;
5017#else
b8ff05a9 5018 return 0;
7829451c 5019#endif
b8ff05a9
DM
5020
5021 out_free_dev:
06546391 5022 free_some_resources(adapter);
94cdb8bb
HS
5023 if (adapter->flags & USING_MSIX)
5024 free_msix_info(adapter);
0fbc81b3
HS
5025 if (adapter->num_uld || adapter->num_ofld_uld)
5026 t4_uld_mem_free(adapter);
b8ff05a9 5027 out_unmap_bar:
d14807dd 5028 if (!is_t4(adapter->params.chip))
22adfe0a 5029 iounmap(adapter->bar2);
b8ff05a9 5030 out_free_adapter:
29aaee65
AB
5031 if (adapter->workq)
5032 destroy_workqueue(adapter->workq);
5033
7f080c3f 5034 kfree(adapter->mbox_log);
b8ff05a9 5035 kfree(adapter);
d6ce2628
HS
5036 out_unmap_bar0:
5037 iounmap(regs);
b8ff05a9
DM
5038 out_disable_device:
5039 pci_disable_pcie_error_reporting(pdev);
5040 pci_disable_device(pdev);
5041 out_release_regions:
5042 pci_release_regions(pdev);
b8ff05a9
DM
5043 return err;
5044}
5045
91744948 5046static void remove_one(struct pci_dev *pdev)
b8ff05a9
DM
5047{
5048 struct adapter *adapter = pci_get_drvdata(pdev);
5049
7829451c
HS
5050 if (!adapter) {
5051 pci_release_regions(pdev);
5052 return;
5053 }
636f9d37 5054
7829451c 5055 if (adapter->pf == 4) {
b8ff05a9
DM
5056 int i;
5057
29aaee65
AB
5058 /* Tear down per-adapter Work Queue first since it can contain
5059 * references to our adapter data structure.
5060 */
5061 destroy_workqueue(adapter->workq);
5062
0fbc81b3 5063 if (is_uld(adapter))
b8ff05a9
DM
5064 detach_ulds(adapter);
5065
b37987e8
HS
5066 disable_interrupts(adapter);
5067
b8ff05a9 5068 for_each_port(adapter, i)
8f3a7676 5069 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
b8ff05a9
DM
5070 unregister_netdev(adapter->port[i]);
5071
9f16dc2e 5072 debugfs_remove_recursive(adapter->debugfs_root);
b8ff05a9 5073
f2b7e78d
VP
5074 /* If we allocated filters, free up state associated with any
5075 * valid filters ...
5076 */
578b46b9 5077 clear_all_filters(adapter);
f2b7e78d 5078
aaefae9b
DM
5079 if (adapter->flags & FULL_INIT_DONE)
5080 cxgb_down(adapter);
b8ff05a9 5081
94cdb8bb
HS
5082 if (adapter->flags & USING_MSIX)
5083 free_msix_info(adapter);
0fbc81b3
HS
5084 if (adapter->num_uld || adapter->num_ofld_uld)
5085 t4_uld_mem_free(adapter);
06546391 5086 free_some_resources(adapter);
b5a02f50
AB
5087#if IS_ENABLED(CONFIG_IPV6)
5088 t4_cleanup_clip_tbl(adapter);
5089#endif
b8ff05a9 5090 iounmap(adapter->regs);
d14807dd 5091 if (!is_t4(adapter->params.chip))
22adfe0a 5092 iounmap(adapter->bar2);
b8ff05a9 5093 pci_disable_pcie_error_reporting(pdev);
144be3d9
GS
5094 if ((adapter->flags & DEV_ENABLED)) {
5095 pci_disable_device(pdev);
5096 adapter->flags &= ~DEV_ENABLED;
5097 }
b8ff05a9 5098 pci_release_regions(pdev);
7f080c3f 5099 kfree(adapter->mbox_log);
ee9a33b2 5100 synchronize_rcu();
8b662fe7 5101 kfree(adapter);
7829451c
HS
5102 }
5103#ifdef CONFIG_PCI_IOV
5104 else {
e7b48a32 5105 if (adapter->port[0])
7829451c 5106 unregister_netdev(adapter->port[0]);
7829451c 5107 iounmap(adapter->regs);
661dbeb9 5108 kfree(adapter->vfinfo);
7829451c
HS
5109 kfree(adapter);
5110 pci_disable_sriov(pdev);
b8ff05a9 5111 pci_release_regions(pdev);
7829451c
HS
5112 }
5113#endif
b8ff05a9
DM
5114}
5115
0fbc81b3
HS
5116/* "Shutdown" quiesces the device, stopping Ingress Packet and Interrupt
5117 * delivery. This is essentially a stripped down version of the PCI remove()
5118 * function where we do the minimal amount of work necessary to shutdown any
5119 * further activity.
5120 */
5121static void shutdown_one(struct pci_dev *pdev)
5122{
5123 struct adapter *adapter = pci_get_drvdata(pdev);
5124
5125 /* As with remove_one() above (see extended comment), we only want do
5126 * do cleanup on PCI Devices which went all the way through init_one()
5127 * ...
5128 */
5129 if (!adapter) {
5130 pci_release_regions(pdev);
5131 return;
5132 }
5133
5134 if (adapter->pf == 4) {
5135 int i;
5136
5137 for_each_port(adapter, i)
5138 if (adapter->port[i]->reg_state == NETREG_REGISTERED)
5139 cxgb_close(adapter->port[i]);
5140
5141 t4_uld_clean_up(adapter);
5142 disable_interrupts(adapter);
5143 disable_msi(adapter);
5144
5145 t4_sge_stop(adapter);
5146 if (adapter->flags & FW_OK)
5147 t4_fw_bye(adapter, adapter->mbox);
5148 }
5149#ifdef CONFIG_PCI_IOV
5150 else {
5151 if (adapter->port[0])
5152 unregister_netdev(adapter->port[0]);
5153 iounmap(adapter->regs);
5154 kfree(adapter->vfinfo);
5155 kfree(adapter);
5156 pci_disable_sriov(pdev);
5157 pci_release_regions(pdev);
5158 }
5159#endif
5160}
5161
b8ff05a9
DM
5162static struct pci_driver cxgb4_driver = {
5163 .name = KBUILD_MODNAME,
5164 .id_table = cxgb4_pci_tbl,
5165 .probe = init_one,
91744948 5166 .remove = remove_one,
0fbc81b3 5167 .shutdown = shutdown_one,
b6244201
HS
5168#ifdef CONFIG_PCI_IOV
5169 .sriov_configure = cxgb4_iov_configure,
5170#endif
204dc3c0 5171 .err_handler = &cxgb4_eeh,
b8ff05a9
DM
5172};
5173
5174static int __init cxgb4_init_module(void)
5175{
5176 int ret;
5177
5178 /* Debugfs support is optional, just warn if this fails */
5179 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
5180 if (!cxgb4_debugfs_root)
428ac43f 5181 pr_warn("could not create debugfs entry, continuing\n");
b8ff05a9
DM
5182
5183 ret = pci_register_driver(&cxgb4_driver);
29aaee65 5184 if (ret < 0)
b8ff05a9 5185 debugfs_remove(cxgb4_debugfs_root);
01bcca68 5186
1bb60376 5187#if IS_ENABLED(CONFIG_IPV6)
b5a02f50
AB
5188 if (!inet6addr_registered) {
5189 register_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5190 inet6addr_registered = true;
5191 }
1bb60376 5192#endif
01bcca68 5193
b8ff05a9
DM
5194 return ret;
5195}
5196
5197static void __exit cxgb4_cleanup_module(void)
5198{
1bb60376 5199#if IS_ENABLED(CONFIG_IPV6)
1793c798 5200 if (inet6addr_registered) {
b5a02f50
AB
5201 unregister_inet6addr_notifier(&cxgb4_inet6addr_notifier);
5202 inet6addr_registered = false;
5203 }
1bb60376 5204#endif
b8ff05a9
DM
5205 pci_unregister_driver(&cxgb4_driver);
5206 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
5207}
5208
5209module_init(cxgb4_init_module);
5210module_exit(cxgb4_cleanup_module);