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