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