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