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