blob: 7552d8df55edef15d1aa30cf48b1a85fd9d5c2b2 [file] [log] [blame]
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001/*
2 * This file is part of the Chelsio T4 Ethernet driver for Linux.
3 *
4 * Copyright (c) 2003-2010 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_vlan.h>
45#include <linux/init.h>
46#include <linux/log2.h>
47#include <linux/mdio.h>
48#include <linux/module.h>
49#include <linux/moduleparam.h>
50#include <linux/mutex.h>
51#include <linux/netdevice.h>
52#include <linux/pci.h>
53#include <linux/aer.h>
54#include <linux/rtnetlink.h>
55#include <linux/sched.h>
56#include <linux/seq_file.h>
57#include <linux/sockios.h>
58#include <linux/vmalloc.h>
59#include <linux/workqueue.h>
60#include <net/neighbour.h>
61#include <net/netevent.h>
62#include <asm/uaccess.h>
63
64#include "cxgb4.h"
65#include "t4_regs.h"
66#include "t4_msg.h"
67#include "t4fw_api.h"
68#include "l2t.h"
69
Dimitris Michailidis99e6d062010-08-02 13:19:24 +000070#define DRV_VERSION "1.3.0-ko"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +000071#define DRV_DESC "Chelsio T4 Network Driver"
72
73/*
74 * Max interrupt hold-off timer value in us. Queues fall back to this value
75 * under extreme memory pressure so it's largish to give the system time to
76 * recover.
77 */
78#define MAX_SGE_TIMERVAL 200U
79
Casey Leedom7ee9ff92010-06-25 12:11:46 +000080#ifdef CONFIG_PCI_IOV
81/*
82 * Virtual Function provisioning constants. We need two extra Ingress Queues
83 * with Interrupt capability to serve as the VF's Firmware Event Queue and
84 * Forwarded Interrupt Queue (when using MSI mode) -- neither will have Free
85 * Lists associated with them). For each Ethernet/Control Egress Queue and
86 * for each Free List, we need an Egress Context.
87 */
88enum {
89 VFRES_NPORTS = 1, /* # of "ports" per VF */
90 VFRES_NQSETS = 2, /* # of "Queue Sets" per VF */
91
92 VFRES_NVI = VFRES_NPORTS, /* # of Virtual Interfaces */
93 VFRES_NETHCTRL = VFRES_NQSETS, /* # of EQs used for ETH or CTRL Qs */
94 VFRES_NIQFLINT = VFRES_NQSETS+2,/* # of ingress Qs/w Free List(s)/intr */
95 VFRES_NIQ = 0, /* # of non-fl/int ingress queues */
96 VFRES_NEQ = VFRES_NQSETS*2, /* # of egress queues */
97 VFRES_TC = 0, /* PCI-E traffic class */
98 VFRES_NEXACTF = 16, /* # of exact MPS filters */
99
100 VFRES_R_CAPS = FW_CMD_CAP_DMAQ|FW_CMD_CAP_VF|FW_CMD_CAP_PORT,
101 VFRES_WX_CAPS = FW_CMD_CAP_DMAQ|FW_CMD_CAP_VF,
102};
103
104/*
105 * Provide a Port Access Rights Mask for the specified PF/VF. This is very
106 * static and likely not to be useful in the long run. We really need to
107 * implement some form of persistent configuration which the firmware
108 * controls.
109 */
110static unsigned int pfvfres_pmask(struct adapter *adapter,
111 unsigned int pf, unsigned int vf)
112{
113 unsigned int portn, portvec;
114
115 /*
116 * Give PF's access to all of the ports.
117 */
118 if (vf == 0)
119 return FW_PFVF_CMD_PMASK_MASK;
120
121 /*
122 * For VFs, we'll assign them access to the ports based purely on the
123 * PF. We assign active ports in order, wrapping around if there are
124 * fewer active ports than PFs: e.g. active port[pf % nports].
125 * Unfortunately the adapter's port_info structs haven't been
126 * initialized yet so we have to compute this.
127 */
128 if (adapter->params.nports == 0)
129 return 0;
130
131 portn = pf % adapter->params.nports;
132 portvec = adapter->params.portvec;
133 for (;;) {
134 /*
135 * Isolate the lowest set bit in the port vector. If we're at
136 * the port number that we want, return that as the pmask.
137 * otherwise mask that bit out of the port vector and
138 * decrement our port number ...
139 */
140 unsigned int pmask = portvec ^ (portvec & (portvec-1));
141 if (portn == 0)
142 return pmask;
143 portn--;
144 portvec &= ~pmask;
145 }
146 /*NOTREACHED*/
147}
148#endif
149
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000150enum {
151 MEMWIN0_APERTURE = 65536,
152 MEMWIN0_BASE = 0x30000,
153 MEMWIN1_APERTURE = 32768,
154 MEMWIN1_BASE = 0x28000,
155 MEMWIN2_APERTURE = 2048,
156 MEMWIN2_BASE = 0x1b800,
157};
158
159enum {
160 MAX_TXQ_ENTRIES = 16384,
161 MAX_CTRL_TXQ_ENTRIES = 1024,
162 MAX_RSPQ_ENTRIES = 16384,
163 MAX_RX_BUFFERS = 16384,
164 MIN_TXQ_ENTRIES = 32,
165 MIN_CTRL_TXQ_ENTRIES = 32,
166 MIN_RSPQ_ENTRIES = 128,
167 MIN_FL_ENTRIES = 16
168};
169
170#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
171 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
172 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
173
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000174#define CH_DEVICE(devid, data) { PCI_VDEVICE(CHELSIO, devid), (data) }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000175
176static DEFINE_PCI_DEVICE_TABLE(cxgb4_pci_tbl) = {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000177 CH_DEVICE(0xa000, 0), /* PE10K */
Dimitris Michailidisac50bed2010-08-02 13:19:23 +0000178 CH_DEVICE(0x4001, 0),
179 CH_DEVICE(0x4002, 0),
180 CH_DEVICE(0x4003, 0),
181 CH_DEVICE(0x4004, 0),
182 CH_DEVICE(0x4005, 0),
183 CH_DEVICE(0x4006, 0),
184 CH_DEVICE(0x4007, 0),
185 CH_DEVICE(0x4008, 0),
186 CH_DEVICE(0x4009, 0),
187 CH_DEVICE(0x400a, 0),
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000188 { 0, }
189};
190
191#define FW_FNAME "cxgb4/t4fw.bin"
192
193MODULE_DESCRIPTION(DRV_DESC);
194MODULE_AUTHOR("Chelsio Communications");
195MODULE_LICENSE("Dual BSD/GPL");
196MODULE_VERSION(DRV_VERSION);
197MODULE_DEVICE_TABLE(pci, cxgb4_pci_tbl);
198MODULE_FIRMWARE(FW_FNAME);
199
200static int dflt_msg_enable = DFLT_MSG_ENABLE;
201
202module_param(dflt_msg_enable, int, 0644);
203MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T4 default message enable bitmap");
204
205/*
206 * The driver uses the best interrupt scheme available on a platform in the
207 * order MSI-X, MSI, legacy INTx interrupts. This parameter determines which
208 * of these schemes the driver may consider as follows:
209 *
210 * msi = 2: choose from among all three options
211 * msi = 1: only consider MSI and INTx interrupts
212 * msi = 0: force INTx interrupts
213 */
214static int msi = 2;
215
216module_param(msi, int, 0644);
217MODULE_PARM_DESC(msi, "whether to use INTx (0), MSI (1) or MSI-X (2)");
218
219/*
220 * Queue interrupt hold-off timer values. Queues default to the first of these
221 * upon creation.
222 */
223static unsigned int intr_holdoff[SGE_NTIMERS - 1] = { 5, 10, 20, 50, 100 };
224
225module_param_array(intr_holdoff, uint, NULL, 0644);
226MODULE_PARM_DESC(intr_holdoff, "values for queue interrupt hold-off timers "
227 "0..4 in microseconds");
228
229static unsigned int intr_cnt[SGE_NCOUNTERS - 1] = { 4, 8, 16 };
230
231module_param_array(intr_cnt, uint, NULL, 0644);
232MODULE_PARM_DESC(intr_cnt,
233 "thresholds 1..3 for queue interrupt packet counters");
234
235static int vf_acls;
236
237#ifdef CONFIG_PCI_IOV
238module_param(vf_acls, bool, 0644);
239MODULE_PARM_DESC(vf_acls, "if set enable virtualization L2 ACL enforcement");
240
241static unsigned int num_vf[4];
242
243module_param_array(num_vf, uint, NULL, 0644);
244MODULE_PARM_DESC(num_vf, "number of VFs for each of PFs 0-3");
245#endif
246
247static struct dentry *cxgb4_debugfs_root;
248
249static LIST_HEAD(adapter_list);
250static DEFINE_MUTEX(uld_mutex);
251static struct cxgb4_uld_info ulds[CXGB4_ULD_MAX];
252static const char *uld_str[] = { "RDMA", "iSCSI" };
253
254static void link_report(struct net_device *dev)
255{
256 if (!netif_carrier_ok(dev))
257 netdev_info(dev, "link down\n");
258 else {
259 static const char *fc[] = { "no", "Rx", "Tx", "Tx/Rx" };
260
261 const char *s = "10Mbps";
262 const struct port_info *p = netdev_priv(dev);
263
264 switch (p->link_cfg.speed) {
265 case SPEED_10000:
266 s = "10Gbps";
267 break;
268 case SPEED_1000:
269 s = "1000Mbps";
270 break;
271 case SPEED_100:
272 s = "100Mbps";
273 break;
274 }
275
276 netdev_info(dev, "link up, %s, full-duplex, %s PAUSE\n", s,
277 fc[p->link_cfg.fc]);
278 }
279}
280
281void t4_os_link_changed(struct adapter *adapter, int port_id, int link_stat)
282{
283 struct net_device *dev = adapter->port[port_id];
284
285 /* Skip changes from disabled ports. */
286 if (netif_running(dev) && link_stat != netif_carrier_ok(dev)) {
287 if (link_stat)
288 netif_carrier_on(dev);
289 else
290 netif_carrier_off(dev);
291
292 link_report(dev);
293 }
294}
295
296void t4_os_portmod_changed(const struct adapter *adap, int port_id)
297{
298 static const char *mod_str[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000299 NULL, "LR", "SR", "ER", "passive DA", "active DA", "LRM"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000300 };
301
302 const struct net_device *dev = adap->port[port_id];
303 const struct port_info *pi = netdev_priv(dev);
304
305 if (pi->mod_type == FW_PORT_MOD_TYPE_NONE)
306 netdev_info(dev, "port module unplugged\n");
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +0000307 else if (pi->mod_type < ARRAY_SIZE(mod_str))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000308 netdev_info(dev, "%s module inserted\n", mod_str[pi->mod_type]);
309}
310
311/*
312 * Configure the exact and hash address filters to handle a port's multicast
313 * and secondary unicast MAC addresses.
314 */
315static int set_addr_filters(const struct net_device *dev, bool sleep)
316{
317 u64 mhash = 0;
318 u64 uhash = 0;
319 bool free = true;
320 u16 filt_idx[7];
321 const u8 *addr[7];
322 int ret, naddr = 0;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000323 const struct netdev_hw_addr *ha;
324 int uc_cnt = netdev_uc_count(dev);
David S. Miller4a35ecf2010-04-06 23:53:30 -0700325 int mc_cnt = netdev_mc_count(dev);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000326 const struct port_info *pi = netdev_priv(dev);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000327 unsigned int mb = pi->adapter->fn;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000328
329 /* first do the secondary unicast addresses */
330 netdev_for_each_uc_addr(ha, dev) {
331 addr[naddr++] = ha->addr;
332 if (--uc_cnt == 0 || naddr >= ARRAY_SIZE(addr)) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000333 ret = t4_alloc_mac_filt(pi->adapter, mb, pi->viid, free,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000334 naddr, addr, filt_idx, &uhash, sleep);
335 if (ret < 0)
336 return ret;
337
338 free = false;
339 naddr = 0;
340 }
341 }
342
343 /* next set up the multicast addresses */
David S. Miller4a35ecf2010-04-06 23:53:30 -0700344 netdev_for_each_mc_addr(ha, dev) {
345 addr[naddr++] = ha->addr;
346 if (--mc_cnt == 0 || naddr >= ARRAY_SIZE(addr)) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000347 ret = t4_alloc_mac_filt(pi->adapter, mb, pi->viid, free,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000348 naddr, addr, filt_idx, &mhash, sleep);
349 if (ret < 0)
350 return ret;
351
352 free = false;
353 naddr = 0;
354 }
355 }
356
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000357 return t4_set_addr_hash(pi->adapter, mb, pi->viid, uhash != 0,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000358 uhash | mhash, sleep);
359}
360
361/*
362 * Set Rx properties of a port, such as promiscruity, address filters, and MTU.
363 * If @mtu is -1 it is left unchanged.
364 */
365static int set_rxmode(struct net_device *dev, int mtu, bool sleep_ok)
366{
367 int ret;
368 struct port_info *pi = netdev_priv(dev);
369
370 ret = set_addr_filters(dev, sleep_ok);
371 if (ret == 0)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000372 ret = t4_set_rxmode(pi->adapter, pi->adapter->fn, pi->viid, mtu,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000373 (dev->flags & IFF_PROMISC) ? 1 : 0,
Dimitris Michailidisf8f5aaf2010-05-10 15:58:07 +0000374 (dev->flags & IFF_ALLMULTI) ? 1 : 0, 1, -1,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000375 sleep_ok);
376 return ret;
377}
378
379/**
380 * link_start - enable a port
381 * @dev: the port to enable
382 *
383 * Performs the MAC and PHY actions needed to enable a port.
384 */
385static int link_start(struct net_device *dev)
386{
387 int ret;
388 struct port_info *pi = netdev_priv(dev);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000389 unsigned int mb = pi->adapter->fn;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000390
391 /*
392 * We do not set address filters and promiscuity here, the stack does
393 * that step explicitly.
394 */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000395 ret = t4_set_rxmode(pi->adapter, mb, pi->viid, dev->mtu, -1, -1, -1,
Dimitris Michailidisf8f5aaf2010-05-10 15:58:07 +0000396 pi->vlan_grp != NULL, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000397 if (ret == 0) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000398 ret = t4_change_mac(pi->adapter, mb, pi->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000399 pi->xact_addr_filt, dev->dev_addr, true,
Dimitris Michailidisb6bd29e2010-05-18 10:07:11 +0000400 true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000401 if (ret >= 0) {
402 pi->xact_addr_filt = ret;
403 ret = 0;
404 }
405 }
406 if (ret == 0)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000407 ret = t4_link_start(pi->adapter, mb, pi->tx_chan,
408 &pi->link_cfg);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000409 if (ret == 0)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000410 ret = t4_enable_vi(pi->adapter, mb, pi->viid, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000411 return ret;
412}
413
414/*
415 * Response queue handler for the FW event queue.
416 */
417static int fwevtq_handler(struct sge_rspq *q, const __be64 *rsp,
418 const struct pkt_gl *gl)
419{
420 u8 opcode = ((const struct rss_header *)rsp)->opcode;
421
422 rsp++; /* skip RSS header */
423 if (likely(opcode == CPL_SGE_EGR_UPDATE)) {
424 const struct cpl_sge_egr_update *p = (void *)rsp;
425 unsigned int qid = EGR_QID(ntohl(p->opcode_qid));
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000426 struct sge_txq *txq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000427
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000428 txq = q->adap->sge.egr_map[qid - q->adap->sge.egr_start];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000429 txq->restarts++;
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000430 if ((u8 *)txq < (u8 *)q->adap->sge.ofldtxq) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000431 struct sge_eth_txq *eq;
432
433 eq = container_of(txq, struct sge_eth_txq, q);
434 netif_tx_wake_queue(eq->txq);
435 } else {
436 struct sge_ofld_txq *oq;
437
438 oq = container_of(txq, struct sge_ofld_txq, q);
439 tasklet_schedule(&oq->qresume_tsk);
440 }
441 } else if (opcode == CPL_FW6_MSG || opcode == CPL_FW4_MSG) {
442 const struct cpl_fw6_msg *p = (void *)rsp;
443
444 if (p->type == 0)
445 t4_handle_fw_rpl(q->adap, p->data);
446 } else if (opcode == CPL_L2T_WRITE_RPL) {
447 const struct cpl_l2t_write_rpl *p = (void *)rsp;
448
449 do_l2t_write_rpl(q->adap, p);
450 } else
451 dev_err(q->adap->pdev_dev,
452 "unexpected CPL %#x on FW event queue\n", opcode);
453 return 0;
454}
455
456/**
457 * uldrx_handler - response queue handler for ULD queues
458 * @q: the response queue that received the packet
459 * @rsp: the response queue descriptor holding the offload message
460 * @gl: the gather list of packet fragments
461 *
462 * Deliver an ingress offload packet to a ULD. All processing is done by
463 * the ULD, we just maintain statistics.
464 */
465static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
466 const struct pkt_gl *gl)
467{
468 struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
469
470 if (ulds[q->uld].rx_handler(q->adap->uld_handle[q->uld], rsp, gl)) {
471 rxq->stats.nomem++;
472 return -1;
473 }
474 if (gl == NULL)
475 rxq->stats.imm++;
476 else if (gl == CXGB4_MSG_AN)
477 rxq->stats.an++;
478 else
479 rxq->stats.pkts++;
480 return 0;
481}
482
483static void disable_msi(struct adapter *adapter)
484{
485 if (adapter->flags & USING_MSIX) {
486 pci_disable_msix(adapter->pdev);
487 adapter->flags &= ~USING_MSIX;
488 } else if (adapter->flags & USING_MSI) {
489 pci_disable_msi(adapter->pdev);
490 adapter->flags &= ~USING_MSI;
491 }
492}
493
494/*
495 * Interrupt handler for non-data events used with MSI-X.
496 */
497static irqreturn_t t4_nondata_intr(int irq, void *cookie)
498{
499 struct adapter *adap = cookie;
500
501 u32 v = t4_read_reg(adap, MYPF_REG(PL_PF_INT_CAUSE));
502 if (v & PFSW) {
503 adap->swintr = 1;
504 t4_write_reg(adap, MYPF_REG(PL_PF_INT_CAUSE), v);
505 }
506 t4_slow_intr_handler(adap);
507 return IRQ_HANDLED;
508}
509
510/*
511 * Name the MSI-X interrupts.
512 */
513static void name_msix_vecs(struct adapter *adap)
514{
515 int i, j, msi_idx = 2, n = sizeof(adap->msix_info[0].desc) - 1;
516
517 /* non-data interrupts */
518 snprintf(adap->msix_info[0].desc, n, "%s", adap->name);
519 adap->msix_info[0].desc[n] = 0;
520
521 /* FW events */
522 snprintf(adap->msix_info[1].desc, n, "%s-FWeventq", adap->name);
523 adap->msix_info[1].desc[n] = 0;
524
525 /* Ethernet queues */
526 for_each_port(adap, j) {
527 struct net_device *d = adap->port[j];
528 const struct port_info *pi = netdev_priv(d);
529
530 for (i = 0; i < pi->nqsets; i++, msi_idx++) {
531 snprintf(adap->msix_info[msi_idx].desc, n, "%s-Rx%d",
532 d->name, i);
533 adap->msix_info[msi_idx].desc[n] = 0;
534 }
535 }
536
537 /* offload queues */
538 for_each_ofldrxq(&adap->sge, i) {
539 snprintf(adap->msix_info[msi_idx].desc, n, "%s-ofld%d",
540 adap->name, i);
541 adap->msix_info[msi_idx++].desc[n] = 0;
542 }
543 for_each_rdmarxq(&adap->sge, i) {
544 snprintf(adap->msix_info[msi_idx].desc, n, "%s-rdma%d",
545 adap->name, i);
546 adap->msix_info[msi_idx++].desc[n] = 0;
547 }
548}
549
550static int request_msix_queue_irqs(struct adapter *adap)
551{
552 struct sge *s = &adap->sge;
553 int err, ethqidx, ofldqidx = 0, rdmaqidx = 0, msi = 2;
554
555 err = request_irq(adap->msix_info[1].vec, t4_sge_intr_msix, 0,
556 adap->msix_info[1].desc, &s->fw_evtq);
557 if (err)
558 return err;
559
560 for_each_ethrxq(s, ethqidx) {
561 err = request_irq(adap->msix_info[msi].vec, t4_sge_intr_msix, 0,
562 adap->msix_info[msi].desc,
563 &s->ethrxq[ethqidx].rspq);
564 if (err)
565 goto unwind;
566 msi++;
567 }
568 for_each_ofldrxq(s, ofldqidx) {
569 err = request_irq(adap->msix_info[msi].vec, t4_sge_intr_msix, 0,
570 adap->msix_info[msi].desc,
571 &s->ofldrxq[ofldqidx].rspq);
572 if (err)
573 goto unwind;
574 msi++;
575 }
576 for_each_rdmarxq(s, rdmaqidx) {
577 err = request_irq(adap->msix_info[msi].vec, t4_sge_intr_msix, 0,
578 adap->msix_info[msi].desc,
579 &s->rdmarxq[rdmaqidx].rspq);
580 if (err)
581 goto unwind;
582 msi++;
583 }
584 return 0;
585
586unwind:
587 while (--rdmaqidx >= 0)
588 free_irq(adap->msix_info[--msi].vec,
589 &s->rdmarxq[rdmaqidx].rspq);
590 while (--ofldqidx >= 0)
591 free_irq(adap->msix_info[--msi].vec,
592 &s->ofldrxq[ofldqidx].rspq);
593 while (--ethqidx >= 0)
594 free_irq(adap->msix_info[--msi].vec, &s->ethrxq[ethqidx].rspq);
595 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
596 return err;
597}
598
599static void free_msix_queue_irqs(struct adapter *adap)
600{
601 int i, msi = 2;
602 struct sge *s = &adap->sge;
603
604 free_irq(adap->msix_info[1].vec, &s->fw_evtq);
605 for_each_ethrxq(s, i)
606 free_irq(adap->msix_info[msi++].vec, &s->ethrxq[i].rspq);
607 for_each_ofldrxq(s, i)
608 free_irq(adap->msix_info[msi++].vec, &s->ofldrxq[i].rspq);
609 for_each_rdmarxq(s, i)
610 free_irq(adap->msix_info[msi++].vec, &s->rdmarxq[i].rspq);
611}
612
613/**
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000614 * write_rss - write the RSS table for a given port
615 * @pi: the port
616 * @queues: array of queue indices for RSS
617 *
618 * Sets up the portion of the HW RSS table for the port's VI to distribute
619 * packets to the Rx queues in @queues.
620 */
621static int write_rss(const struct port_info *pi, const u16 *queues)
622{
623 u16 *rss;
624 int i, err;
625 const struct sge_eth_rxq *q = &pi->adapter->sge.ethrxq[pi->first_qset];
626
627 rss = kmalloc(pi->rss_size * sizeof(u16), GFP_KERNEL);
628 if (!rss)
629 return -ENOMEM;
630
631 /* map the queue indices to queue ids */
632 for (i = 0; i < pi->rss_size; i++, queues++)
633 rss[i] = q[*queues].rspq.abs_id;
634
Dimitris Michailidis060e0c72010-08-02 13:19:21 +0000635 err = t4_config_rss_range(pi->adapter, pi->adapter->fn, pi->viid, 0,
636 pi->rss_size, rss, pi->rss_size);
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000637 kfree(rss);
638 return err;
639}
640
641/**
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000642 * setup_rss - configure RSS
643 * @adap: the adapter
644 *
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000645 * Sets up RSS for each port.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000646 */
647static int setup_rss(struct adapter *adap)
648{
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000649 int i, err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000650
651 for_each_port(adap, i) {
652 const struct port_info *pi = adap2pinfo(adap, i);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000653
Dimitris Michailidis671b0062010-07-11 12:01:17 +0000654 err = write_rss(pi, pi->rss);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000655 if (err)
656 return err;
657 }
658 return 0;
659}
660
661/*
Dimitris Michailidise46dab42010-08-23 17:20:58 +0000662 * Return the channel of the ingress queue with the given qid.
663 */
664static unsigned int rxq_to_chan(const struct sge *p, unsigned int qid)
665{
666 qid -= p->ingr_start;
667 return netdev2pinfo(p->ingr_map[qid]->netdev)->tx_chan;
668}
669
670/*
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000671 * Wait until all NAPI handlers are descheduled.
672 */
673static void quiesce_rx(struct adapter *adap)
674{
675 int i;
676
677 for (i = 0; i < ARRAY_SIZE(adap->sge.ingr_map); i++) {
678 struct sge_rspq *q = adap->sge.ingr_map[i];
679
680 if (q && q->handler)
681 napi_disable(&q->napi);
682 }
683}
684
685/*
686 * Enable NAPI scheduling and interrupt generation for all Rx queues.
687 */
688static void enable_rx(struct adapter *adap)
689{
690 int i;
691
692 for (i = 0; i < ARRAY_SIZE(adap->sge.ingr_map); i++) {
693 struct sge_rspq *q = adap->sge.ingr_map[i];
694
695 if (!q)
696 continue;
697 if (q->handler)
698 napi_enable(&q->napi);
699 /* 0-increment GTS to start the timer and enable interrupts */
700 t4_write_reg(adap, MYPF_REG(SGE_PF_GTS),
701 SEINTARM(q->intr_params) |
702 INGRESSQID(q->cntxt_id));
703 }
704}
705
706/**
707 * setup_sge_queues - configure SGE Tx/Rx/response queues
708 * @adap: the adapter
709 *
710 * Determines how many sets of SGE queues to use and initializes them.
711 * We support multiple queue sets per port if we have MSI-X, otherwise
712 * just one queue set per port.
713 */
714static int setup_sge_queues(struct adapter *adap)
715{
716 int err, msi_idx, i, j;
717 struct sge *s = &adap->sge;
718
719 bitmap_zero(s->starving_fl, MAX_EGRQ);
720 bitmap_zero(s->txq_maperr, MAX_EGRQ);
721
722 if (adap->flags & USING_MSIX)
723 msi_idx = 1; /* vector 0 is for non-queue interrupts */
724 else {
725 err = t4_sge_alloc_rxq(adap, &s->intrq, false, adap->port[0], 0,
726 NULL, NULL);
727 if (err)
728 return err;
729 msi_idx = -((int)s->intrq.abs_id + 1);
730 }
731
732 err = t4_sge_alloc_rxq(adap, &s->fw_evtq, true, adap->port[0],
733 msi_idx, NULL, fwevtq_handler);
734 if (err) {
735freeout: t4_free_sge_resources(adap);
736 return err;
737 }
738
739 for_each_port(adap, i) {
740 struct net_device *dev = adap->port[i];
741 struct port_info *pi = netdev_priv(dev);
742 struct sge_eth_rxq *q = &s->ethrxq[pi->first_qset];
743 struct sge_eth_txq *t = &s->ethtxq[pi->first_qset];
744
745 for (j = 0; j < pi->nqsets; j++, q++) {
746 if (msi_idx > 0)
747 msi_idx++;
748 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev,
749 msi_idx, &q->fl,
750 t4_ethrx_handler);
751 if (err)
752 goto freeout;
753 q->rspq.idx = j;
754 memset(&q->stats, 0, sizeof(q->stats));
755 }
756 for (j = 0; j < pi->nqsets; j++, t++) {
757 err = t4_sge_alloc_eth_txq(adap, t, dev,
758 netdev_get_tx_queue(dev, j),
759 s->fw_evtq.cntxt_id);
760 if (err)
761 goto freeout;
762 }
763 }
764
765 j = s->ofldqsets / adap->params.nports; /* ofld queues per channel */
766 for_each_ofldrxq(s, i) {
767 struct sge_ofld_rxq *q = &s->ofldrxq[i];
768 struct net_device *dev = adap->port[i / j];
769
770 if (msi_idx > 0)
771 msi_idx++;
772 err = t4_sge_alloc_rxq(adap, &q->rspq, false, dev, msi_idx,
773 &q->fl, uldrx_handler);
774 if (err)
775 goto freeout;
776 memset(&q->stats, 0, sizeof(q->stats));
777 s->ofld_rxq[i] = q->rspq.abs_id;
778 err = t4_sge_alloc_ofld_txq(adap, &s->ofldtxq[i], dev,
779 s->fw_evtq.cntxt_id);
780 if (err)
781 goto freeout;
782 }
783
784 for_each_rdmarxq(s, i) {
785 struct sge_ofld_rxq *q = &s->rdmarxq[i];
786
787 if (msi_idx > 0)
788 msi_idx++;
789 err = t4_sge_alloc_rxq(adap, &q->rspq, false, adap->port[i],
790 msi_idx, &q->fl, uldrx_handler);
791 if (err)
792 goto freeout;
793 memset(&q->stats, 0, sizeof(q->stats));
794 s->rdma_rxq[i] = q->rspq.abs_id;
795 }
796
797 for_each_port(adap, i) {
798 /*
799 * Note that ->rdmarxq[i].rspq.cntxt_id below is 0 if we don't
800 * have RDMA queues, and that's the right value.
801 */
802 err = t4_sge_alloc_ctrl_txq(adap, &s->ctrlq[i], adap->port[i],
803 s->fw_evtq.cntxt_id,
804 s->rdmarxq[i].rspq.cntxt_id);
805 if (err)
806 goto freeout;
807 }
808
809 t4_write_reg(adap, MPS_TRC_RSS_CONTROL,
810 RSSCONTROL(netdev2pinfo(adap->port[0])->tx_chan) |
811 QUEUENUMBER(s->ethrxq[0].rspq.abs_id));
812 return 0;
813}
814
815/*
816 * Returns 0 if new FW was successfully loaded, a positive errno if a load was
817 * started but failed, and a negative errno if flash load couldn't start.
818 */
819static int upgrade_fw(struct adapter *adap)
820{
821 int ret;
822 u32 vers;
823 const struct fw_hdr *hdr;
824 const struct firmware *fw;
825 struct device *dev = adap->pdev_dev;
826
827 ret = request_firmware(&fw, FW_FNAME, dev);
828 if (ret < 0) {
829 dev_err(dev, "unable to load firmware image " FW_FNAME
830 ", error %d\n", ret);
831 return ret;
832 }
833
834 hdr = (const struct fw_hdr *)fw->data;
835 vers = ntohl(hdr->fw_ver);
836 if (FW_HDR_FW_VER_MAJOR_GET(vers) != FW_VERSION_MAJOR) {
837 ret = -EINVAL; /* wrong major version, won't do */
838 goto out;
839 }
840
841 /*
842 * If the flash FW is unusable or we found something newer, load it.
843 */
844 if (FW_HDR_FW_VER_MAJOR_GET(adap->params.fw_vers) != FW_VERSION_MAJOR ||
845 vers > adap->params.fw_vers) {
846 ret = -t4_load_fw(adap, fw->data, fw->size);
847 if (!ret)
848 dev_info(dev, "firmware upgraded to version %pI4 from "
849 FW_FNAME "\n", &hdr->fw_ver);
850 }
851out: release_firmware(fw);
852 return ret;
853}
854
855/*
856 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
857 * The allocated memory is cleared.
858 */
859void *t4_alloc_mem(size_t size)
860{
861 void *p = kmalloc(size, GFP_KERNEL);
862
863 if (!p)
864 p = vmalloc(size);
865 if (p)
866 memset(p, 0, size);
867 return p;
868}
869
870/*
871 * Free memory allocated through alloc_mem().
872 */
873void t4_free_mem(void *addr)
874{
875 if (is_vmalloc_addr(addr))
876 vfree(addr);
877 else
878 kfree(addr);
879}
880
881static inline int is_offload(const struct adapter *adap)
882{
883 return adap->params.offload;
884}
885
886/*
887 * Implementation of ethtool operations.
888 */
889
890static u32 get_msglevel(struct net_device *dev)
891{
892 return netdev2adap(dev)->msg_enable;
893}
894
895static void set_msglevel(struct net_device *dev, u32 val)
896{
897 netdev2adap(dev)->msg_enable = val;
898}
899
900static char stats_strings[][ETH_GSTRING_LEN] = {
901 "TxOctetsOK ",
902 "TxFramesOK ",
903 "TxBroadcastFrames ",
904 "TxMulticastFrames ",
905 "TxUnicastFrames ",
906 "TxErrorFrames ",
907
908 "TxFrames64 ",
909 "TxFrames65To127 ",
910 "TxFrames128To255 ",
911 "TxFrames256To511 ",
912 "TxFrames512To1023 ",
913 "TxFrames1024To1518 ",
914 "TxFrames1519ToMax ",
915
916 "TxFramesDropped ",
917 "TxPauseFrames ",
918 "TxPPP0Frames ",
919 "TxPPP1Frames ",
920 "TxPPP2Frames ",
921 "TxPPP3Frames ",
922 "TxPPP4Frames ",
923 "TxPPP5Frames ",
924 "TxPPP6Frames ",
925 "TxPPP7Frames ",
926
927 "RxOctetsOK ",
928 "RxFramesOK ",
929 "RxBroadcastFrames ",
930 "RxMulticastFrames ",
931 "RxUnicastFrames ",
932
933 "RxFramesTooLong ",
934 "RxJabberErrors ",
935 "RxFCSErrors ",
936 "RxLengthErrors ",
937 "RxSymbolErrors ",
938 "RxRuntFrames ",
939
940 "RxFrames64 ",
941 "RxFrames65To127 ",
942 "RxFrames128To255 ",
943 "RxFrames256To511 ",
944 "RxFrames512To1023 ",
945 "RxFrames1024To1518 ",
946 "RxFrames1519ToMax ",
947
948 "RxPauseFrames ",
949 "RxPPP0Frames ",
950 "RxPPP1Frames ",
951 "RxPPP2Frames ",
952 "RxPPP3Frames ",
953 "RxPPP4Frames ",
954 "RxPPP5Frames ",
955 "RxPPP6Frames ",
956 "RxPPP7Frames ",
957
958 "RxBG0FramesDropped ",
959 "RxBG1FramesDropped ",
960 "RxBG2FramesDropped ",
961 "RxBG3FramesDropped ",
962 "RxBG0FramesTrunc ",
963 "RxBG1FramesTrunc ",
964 "RxBG2FramesTrunc ",
965 "RxBG3FramesTrunc ",
966
967 "TSO ",
968 "TxCsumOffload ",
969 "RxCsumGood ",
970 "VLANextractions ",
971 "VLANinsertions ",
Dimitris Michailidis4a6346d2010-05-10 15:58:09 +0000972 "GROpackets ",
973 "GROmerged ",
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +0000974};
975
976static int get_sset_count(struct net_device *dev, int sset)
977{
978 switch (sset) {
979 case ETH_SS_STATS:
980 return ARRAY_SIZE(stats_strings);
981 default:
982 return -EOPNOTSUPP;
983 }
984}
985
986#define T4_REGMAP_SIZE (160 * 1024)
987
988static int get_regs_len(struct net_device *dev)
989{
990 return T4_REGMAP_SIZE;
991}
992
993static int get_eeprom_len(struct net_device *dev)
994{
995 return EEPROMSIZE;
996}
997
998static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
999{
1000 struct adapter *adapter = netdev2adap(dev);
1001
1002 strcpy(info->driver, KBUILD_MODNAME);
1003 strcpy(info->version, DRV_VERSION);
1004 strcpy(info->bus_info, pci_name(adapter->pdev));
1005
1006 if (!adapter->params.fw_vers)
1007 strcpy(info->fw_version, "N/A");
1008 else
1009 snprintf(info->fw_version, sizeof(info->fw_version),
1010 "%u.%u.%u.%u, TP %u.%u.%u.%u",
1011 FW_HDR_FW_VER_MAJOR_GET(adapter->params.fw_vers),
1012 FW_HDR_FW_VER_MINOR_GET(adapter->params.fw_vers),
1013 FW_HDR_FW_VER_MICRO_GET(adapter->params.fw_vers),
1014 FW_HDR_FW_VER_BUILD_GET(adapter->params.fw_vers),
1015 FW_HDR_FW_VER_MAJOR_GET(adapter->params.tp_vers),
1016 FW_HDR_FW_VER_MINOR_GET(adapter->params.tp_vers),
1017 FW_HDR_FW_VER_MICRO_GET(adapter->params.tp_vers),
1018 FW_HDR_FW_VER_BUILD_GET(adapter->params.tp_vers));
1019}
1020
1021static void get_strings(struct net_device *dev, u32 stringset, u8 *data)
1022{
1023 if (stringset == ETH_SS_STATS)
1024 memcpy(data, stats_strings, sizeof(stats_strings));
1025}
1026
1027/*
1028 * port stats maintained per queue of the port. They should be in the same
1029 * order as in stats_strings above.
1030 */
1031struct queue_port_stats {
1032 u64 tso;
1033 u64 tx_csum;
1034 u64 rx_csum;
1035 u64 vlan_ex;
1036 u64 vlan_ins;
Dimitris Michailidis4a6346d2010-05-10 15:58:09 +00001037 u64 gro_pkts;
1038 u64 gro_merged;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001039};
1040
1041static void collect_sge_port_stats(const struct adapter *adap,
1042 const struct port_info *p, struct queue_port_stats *s)
1043{
1044 int i;
1045 const struct sge_eth_txq *tx = &adap->sge.ethtxq[p->first_qset];
1046 const struct sge_eth_rxq *rx = &adap->sge.ethrxq[p->first_qset];
1047
1048 memset(s, 0, sizeof(*s));
1049 for (i = 0; i < p->nqsets; i++, rx++, tx++) {
1050 s->tso += tx->tso;
1051 s->tx_csum += tx->tx_cso;
1052 s->rx_csum += rx->stats.rx_cso;
1053 s->vlan_ex += rx->stats.vlan_ex;
1054 s->vlan_ins += tx->vlan_ins;
Dimitris Michailidis4a6346d2010-05-10 15:58:09 +00001055 s->gro_pkts += rx->stats.lro_pkts;
1056 s->gro_merged += rx->stats.lro_merged;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001057 }
1058}
1059
1060static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
1061 u64 *data)
1062{
1063 struct port_info *pi = netdev_priv(dev);
1064 struct adapter *adapter = pi->adapter;
1065
1066 t4_get_port_stats(adapter, pi->tx_chan, (struct port_stats *)data);
1067
1068 data += sizeof(struct port_stats) / sizeof(u64);
1069 collect_sge_port_stats(adapter, pi, (struct queue_port_stats *)data);
1070}
1071
1072/*
1073 * Return a version number to identify the type of adapter. The scheme is:
1074 * - bits 0..9: chip version
1075 * - bits 10..15: chip revision
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001076 * - bits 16..23: register dump version
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001077 */
1078static inline unsigned int mk_adap_vers(const struct adapter *ap)
1079{
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001080 return 4 | (ap->params.rev << 10) | (1 << 16);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001081}
1082
1083static void reg_block_dump(struct adapter *ap, void *buf, unsigned int start,
1084 unsigned int end)
1085{
1086 u32 *p = buf + start;
1087
1088 for ( ; start <= end; start += sizeof(u32))
1089 *p++ = t4_read_reg(ap, start);
1090}
1091
1092static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
1093 void *buf)
1094{
1095 static const unsigned int reg_ranges[] = {
1096 0x1008, 0x1108,
1097 0x1180, 0x11b4,
1098 0x11fc, 0x123c,
1099 0x1300, 0x173c,
1100 0x1800, 0x18fc,
1101 0x3000, 0x30d8,
1102 0x30e0, 0x5924,
1103 0x5960, 0x59d4,
1104 0x5a00, 0x5af8,
1105 0x6000, 0x6098,
1106 0x6100, 0x6150,
1107 0x6200, 0x6208,
1108 0x6240, 0x6248,
1109 0x6280, 0x6338,
1110 0x6370, 0x638c,
1111 0x6400, 0x643c,
1112 0x6500, 0x6524,
1113 0x6a00, 0x6a38,
1114 0x6a60, 0x6a78,
1115 0x6b00, 0x6b84,
1116 0x6bf0, 0x6c84,
1117 0x6cf0, 0x6d84,
1118 0x6df0, 0x6e84,
1119 0x6ef0, 0x6f84,
1120 0x6ff0, 0x7084,
1121 0x70f0, 0x7184,
1122 0x71f0, 0x7284,
1123 0x72f0, 0x7384,
1124 0x73f0, 0x7450,
1125 0x7500, 0x7530,
1126 0x7600, 0x761c,
1127 0x7680, 0x76cc,
1128 0x7700, 0x7798,
1129 0x77c0, 0x77fc,
1130 0x7900, 0x79fc,
1131 0x7b00, 0x7c38,
1132 0x7d00, 0x7efc,
1133 0x8dc0, 0x8e1c,
1134 0x8e30, 0x8e78,
1135 0x8ea0, 0x8f6c,
1136 0x8fc0, 0x9074,
1137 0x90fc, 0x90fc,
1138 0x9400, 0x9458,
1139 0x9600, 0x96bc,
1140 0x9800, 0x9808,
1141 0x9820, 0x983c,
1142 0x9850, 0x9864,
1143 0x9c00, 0x9c6c,
1144 0x9c80, 0x9cec,
1145 0x9d00, 0x9d6c,
1146 0x9d80, 0x9dec,
1147 0x9e00, 0x9e6c,
1148 0x9e80, 0x9eec,
1149 0x9f00, 0x9f6c,
1150 0x9f80, 0x9fec,
1151 0xd004, 0xd03c,
1152 0xdfc0, 0xdfe0,
1153 0xe000, 0xea7c,
1154 0xf000, 0x11190,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001155 0x19040, 0x1906c,
1156 0x19078, 0x19080,
1157 0x1908c, 0x19124,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001158 0x19150, 0x191b0,
1159 0x191d0, 0x191e8,
1160 0x19238, 0x1924c,
1161 0x193f8, 0x19474,
1162 0x19490, 0x194f8,
1163 0x19800, 0x19f30,
1164 0x1a000, 0x1a06c,
1165 0x1a0b0, 0x1a120,
1166 0x1a128, 0x1a138,
1167 0x1a190, 0x1a1c4,
1168 0x1a1fc, 0x1a1fc,
1169 0x1e040, 0x1e04c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001170 0x1e284, 0x1e28c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001171 0x1e2c0, 0x1e2c0,
1172 0x1e2e0, 0x1e2e0,
1173 0x1e300, 0x1e384,
1174 0x1e3c0, 0x1e3c8,
1175 0x1e440, 0x1e44c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001176 0x1e684, 0x1e68c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001177 0x1e6c0, 0x1e6c0,
1178 0x1e6e0, 0x1e6e0,
1179 0x1e700, 0x1e784,
1180 0x1e7c0, 0x1e7c8,
1181 0x1e840, 0x1e84c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001182 0x1ea84, 0x1ea8c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001183 0x1eac0, 0x1eac0,
1184 0x1eae0, 0x1eae0,
1185 0x1eb00, 0x1eb84,
1186 0x1ebc0, 0x1ebc8,
1187 0x1ec40, 0x1ec4c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001188 0x1ee84, 0x1ee8c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001189 0x1eec0, 0x1eec0,
1190 0x1eee0, 0x1eee0,
1191 0x1ef00, 0x1ef84,
1192 0x1efc0, 0x1efc8,
1193 0x1f040, 0x1f04c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001194 0x1f284, 0x1f28c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001195 0x1f2c0, 0x1f2c0,
1196 0x1f2e0, 0x1f2e0,
1197 0x1f300, 0x1f384,
1198 0x1f3c0, 0x1f3c8,
1199 0x1f440, 0x1f44c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001200 0x1f684, 0x1f68c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001201 0x1f6c0, 0x1f6c0,
1202 0x1f6e0, 0x1f6e0,
1203 0x1f700, 0x1f784,
1204 0x1f7c0, 0x1f7c8,
1205 0x1f840, 0x1f84c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001206 0x1fa84, 0x1fa8c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001207 0x1fac0, 0x1fac0,
1208 0x1fae0, 0x1fae0,
1209 0x1fb00, 0x1fb84,
1210 0x1fbc0, 0x1fbc8,
1211 0x1fc40, 0x1fc4c,
Dimitris Michailidis835bb602010-07-11 17:33:48 -07001212 0x1fe84, 0x1fe8c,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001213 0x1fec0, 0x1fec0,
1214 0x1fee0, 0x1fee0,
1215 0x1ff00, 0x1ff84,
1216 0x1ffc0, 0x1ffc8,
1217 0x20000, 0x2002c,
1218 0x20100, 0x2013c,
1219 0x20190, 0x201c8,
1220 0x20200, 0x20318,
1221 0x20400, 0x20528,
1222 0x20540, 0x20614,
1223 0x21000, 0x21040,
1224 0x2104c, 0x21060,
1225 0x210c0, 0x210ec,
1226 0x21200, 0x21268,
1227 0x21270, 0x21284,
1228 0x212fc, 0x21388,
1229 0x21400, 0x21404,
1230 0x21500, 0x21518,
1231 0x2152c, 0x2153c,
1232 0x21550, 0x21554,
1233 0x21600, 0x21600,
1234 0x21608, 0x21628,
1235 0x21630, 0x2163c,
1236 0x21700, 0x2171c,
1237 0x21780, 0x2178c,
1238 0x21800, 0x21c38,
1239 0x21c80, 0x21d7c,
1240 0x21e00, 0x21e04,
1241 0x22000, 0x2202c,
1242 0x22100, 0x2213c,
1243 0x22190, 0x221c8,
1244 0x22200, 0x22318,
1245 0x22400, 0x22528,
1246 0x22540, 0x22614,
1247 0x23000, 0x23040,
1248 0x2304c, 0x23060,
1249 0x230c0, 0x230ec,
1250 0x23200, 0x23268,
1251 0x23270, 0x23284,
1252 0x232fc, 0x23388,
1253 0x23400, 0x23404,
1254 0x23500, 0x23518,
1255 0x2352c, 0x2353c,
1256 0x23550, 0x23554,
1257 0x23600, 0x23600,
1258 0x23608, 0x23628,
1259 0x23630, 0x2363c,
1260 0x23700, 0x2371c,
1261 0x23780, 0x2378c,
1262 0x23800, 0x23c38,
1263 0x23c80, 0x23d7c,
1264 0x23e00, 0x23e04,
1265 0x24000, 0x2402c,
1266 0x24100, 0x2413c,
1267 0x24190, 0x241c8,
1268 0x24200, 0x24318,
1269 0x24400, 0x24528,
1270 0x24540, 0x24614,
1271 0x25000, 0x25040,
1272 0x2504c, 0x25060,
1273 0x250c0, 0x250ec,
1274 0x25200, 0x25268,
1275 0x25270, 0x25284,
1276 0x252fc, 0x25388,
1277 0x25400, 0x25404,
1278 0x25500, 0x25518,
1279 0x2552c, 0x2553c,
1280 0x25550, 0x25554,
1281 0x25600, 0x25600,
1282 0x25608, 0x25628,
1283 0x25630, 0x2563c,
1284 0x25700, 0x2571c,
1285 0x25780, 0x2578c,
1286 0x25800, 0x25c38,
1287 0x25c80, 0x25d7c,
1288 0x25e00, 0x25e04,
1289 0x26000, 0x2602c,
1290 0x26100, 0x2613c,
1291 0x26190, 0x261c8,
1292 0x26200, 0x26318,
1293 0x26400, 0x26528,
1294 0x26540, 0x26614,
1295 0x27000, 0x27040,
1296 0x2704c, 0x27060,
1297 0x270c0, 0x270ec,
1298 0x27200, 0x27268,
1299 0x27270, 0x27284,
1300 0x272fc, 0x27388,
1301 0x27400, 0x27404,
1302 0x27500, 0x27518,
1303 0x2752c, 0x2753c,
1304 0x27550, 0x27554,
1305 0x27600, 0x27600,
1306 0x27608, 0x27628,
1307 0x27630, 0x2763c,
1308 0x27700, 0x2771c,
1309 0x27780, 0x2778c,
1310 0x27800, 0x27c38,
1311 0x27c80, 0x27d7c,
1312 0x27e00, 0x27e04
1313 };
1314
1315 int i;
1316 struct adapter *ap = netdev2adap(dev);
1317
1318 regs->version = mk_adap_vers(ap);
1319
1320 memset(buf, 0, T4_REGMAP_SIZE);
1321 for (i = 0; i < ARRAY_SIZE(reg_ranges); i += 2)
1322 reg_block_dump(ap, buf, reg_ranges[i], reg_ranges[i + 1]);
1323}
1324
1325static int restart_autoneg(struct net_device *dev)
1326{
1327 struct port_info *p = netdev_priv(dev);
1328
1329 if (!netif_running(dev))
1330 return -EAGAIN;
1331 if (p->link_cfg.autoneg != AUTONEG_ENABLE)
1332 return -EINVAL;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00001333 t4_restart_aneg(p->adapter, p->adapter->fn, p->tx_chan);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001334 return 0;
1335}
1336
1337static int identify_port(struct net_device *dev, u32 data)
1338{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00001339 struct adapter *adap = netdev2adap(dev);
1340
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001341 if (data == 0)
1342 data = 2; /* default to 2 seconds */
1343
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00001344 return t4_identify_port(adap, adap->fn, netdev2pinfo(dev)->viid,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001345 data * 5);
1346}
1347
1348static unsigned int from_fw_linkcaps(unsigned int type, unsigned int caps)
1349{
1350 unsigned int v = 0;
1351
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00001352 if (type == FW_PORT_TYPE_BT_SGMII || type == FW_PORT_TYPE_BT_XFI ||
1353 type == FW_PORT_TYPE_BT_XAUI) {
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001354 v |= SUPPORTED_TP;
1355 if (caps & FW_PORT_CAP_SPEED_100M)
1356 v |= SUPPORTED_100baseT_Full;
1357 if (caps & FW_PORT_CAP_SPEED_1G)
1358 v |= SUPPORTED_1000baseT_Full;
1359 if (caps & FW_PORT_CAP_SPEED_10G)
1360 v |= SUPPORTED_10000baseT_Full;
1361 } else if (type == FW_PORT_TYPE_KX4 || type == FW_PORT_TYPE_KX) {
1362 v |= SUPPORTED_Backplane;
1363 if (caps & FW_PORT_CAP_SPEED_1G)
1364 v |= SUPPORTED_1000baseKX_Full;
1365 if (caps & FW_PORT_CAP_SPEED_10G)
1366 v |= SUPPORTED_10000baseKX4_Full;
1367 } else if (type == FW_PORT_TYPE_KR)
1368 v |= SUPPORTED_Backplane | SUPPORTED_10000baseKR_Full;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00001369 else if (type == FW_PORT_TYPE_BP_AP)
1370 v |= SUPPORTED_Backplane | SUPPORTED_10000baseR_FEC;
1371 else if (type == FW_PORT_TYPE_FIBER_XFI ||
1372 type == FW_PORT_TYPE_FIBER_XAUI || type == FW_PORT_TYPE_SFP)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001373 v |= SUPPORTED_FIBRE;
1374
1375 if (caps & FW_PORT_CAP_ANEG)
1376 v |= SUPPORTED_Autoneg;
1377 return v;
1378}
1379
1380static unsigned int to_fw_linkcaps(unsigned int caps)
1381{
1382 unsigned int v = 0;
1383
1384 if (caps & ADVERTISED_100baseT_Full)
1385 v |= FW_PORT_CAP_SPEED_100M;
1386 if (caps & ADVERTISED_1000baseT_Full)
1387 v |= FW_PORT_CAP_SPEED_1G;
1388 if (caps & ADVERTISED_10000baseT_Full)
1389 v |= FW_PORT_CAP_SPEED_10G;
1390 return v;
1391}
1392
1393static int get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1394{
1395 const struct port_info *p = netdev_priv(dev);
1396
1397 if (p->port_type == FW_PORT_TYPE_BT_SGMII ||
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00001398 p->port_type == FW_PORT_TYPE_BT_XFI ||
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001399 p->port_type == FW_PORT_TYPE_BT_XAUI)
1400 cmd->port = PORT_TP;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00001401 else if (p->port_type == FW_PORT_TYPE_FIBER_XFI ||
1402 p->port_type == FW_PORT_TYPE_FIBER_XAUI)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001403 cmd->port = PORT_FIBRE;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00001404 else if (p->port_type == FW_PORT_TYPE_SFP) {
1405 if (p->mod_type == FW_PORT_MOD_TYPE_TWINAX_PASSIVE ||
1406 p->mod_type == FW_PORT_MOD_TYPE_TWINAX_ACTIVE)
1407 cmd->port = PORT_DA;
1408 else
1409 cmd->port = PORT_FIBRE;
1410 } else
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001411 cmd->port = PORT_OTHER;
1412
1413 if (p->mdio_addr >= 0) {
1414 cmd->phy_address = p->mdio_addr;
1415 cmd->transceiver = XCVR_EXTERNAL;
1416 cmd->mdio_support = p->port_type == FW_PORT_TYPE_BT_SGMII ?
1417 MDIO_SUPPORTS_C22 : MDIO_SUPPORTS_C45;
1418 } else {
1419 cmd->phy_address = 0; /* not really, but no better option */
1420 cmd->transceiver = XCVR_INTERNAL;
1421 cmd->mdio_support = 0;
1422 }
1423
1424 cmd->supported = from_fw_linkcaps(p->port_type, p->link_cfg.supported);
1425 cmd->advertising = from_fw_linkcaps(p->port_type,
1426 p->link_cfg.advertising);
1427 cmd->speed = netif_carrier_ok(dev) ? p->link_cfg.speed : 0;
1428 cmd->duplex = DUPLEX_FULL;
1429 cmd->autoneg = p->link_cfg.autoneg;
1430 cmd->maxtxpkt = 0;
1431 cmd->maxrxpkt = 0;
1432 return 0;
1433}
1434
1435static unsigned int speed_to_caps(int speed)
1436{
1437 if (speed == SPEED_100)
1438 return FW_PORT_CAP_SPEED_100M;
1439 if (speed == SPEED_1000)
1440 return FW_PORT_CAP_SPEED_1G;
1441 if (speed == SPEED_10000)
1442 return FW_PORT_CAP_SPEED_10G;
1443 return 0;
1444}
1445
1446static int set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1447{
1448 unsigned int cap;
1449 struct port_info *p = netdev_priv(dev);
1450 struct link_config *lc = &p->link_cfg;
1451
1452 if (cmd->duplex != DUPLEX_FULL) /* only full-duplex supported */
1453 return -EINVAL;
1454
1455 if (!(lc->supported & FW_PORT_CAP_ANEG)) {
1456 /*
1457 * PHY offers a single speed. See if that's what's
1458 * being requested.
1459 */
1460 if (cmd->autoneg == AUTONEG_DISABLE &&
1461 (lc->supported & speed_to_caps(cmd->speed)))
1462 return 0;
1463 return -EINVAL;
1464 }
1465
1466 if (cmd->autoneg == AUTONEG_DISABLE) {
1467 cap = speed_to_caps(cmd->speed);
1468
1469 if (!(lc->supported & cap) || cmd->speed == SPEED_1000 ||
1470 cmd->speed == SPEED_10000)
1471 return -EINVAL;
1472 lc->requested_speed = cap;
1473 lc->advertising = 0;
1474 } else {
1475 cap = to_fw_linkcaps(cmd->advertising);
1476 if (!(lc->supported & cap))
1477 return -EINVAL;
1478 lc->requested_speed = 0;
1479 lc->advertising = cap | FW_PORT_CAP_ANEG;
1480 }
1481 lc->autoneg = cmd->autoneg;
1482
1483 if (netif_running(dev))
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00001484 return t4_link_start(p->adapter, p->adapter->fn, p->tx_chan,
1485 lc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001486 return 0;
1487}
1488
1489static void get_pauseparam(struct net_device *dev,
1490 struct ethtool_pauseparam *epause)
1491{
1492 struct port_info *p = netdev_priv(dev);
1493
1494 epause->autoneg = (p->link_cfg.requested_fc & PAUSE_AUTONEG) != 0;
1495 epause->rx_pause = (p->link_cfg.fc & PAUSE_RX) != 0;
1496 epause->tx_pause = (p->link_cfg.fc & PAUSE_TX) != 0;
1497}
1498
1499static int set_pauseparam(struct net_device *dev,
1500 struct ethtool_pauseparam *epause)
1501{
1502 struct port_info *p = netdev_priv(dev);
1503 struct link_config *lc = &p->link_cfg;
1504
1505 if (epause->autoneg == AUTONEG_DISABLE)
1506 lc->requested_fc = 0;
1507 else if (lc->supported & FW_PORT_CAP_ANEG)
1508 lc->requested_fc = PAUSE_AUTONEG;
1509 else
1510 return -EINVAL;
1511
1512 if (epause->rx_pause)
1513 lc->requested_fc |= PAUSE_RX;
1514 if (epause->tx_pause)
1515 lc->requested_fc |= PAUSE_TX;
1516 if (netif_running(dev))
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00001517 return t4_link_start(p->adapter, p->adapter->fn, p->tx_chan,
1518 lc);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001519 return 0;
1520}
1521
1522static u32 get_rx_csum(struct net_device *dev)
1523{
1524 struct port_info *p = netdev_priv(dev);
1525
1526 return p->rx_offload & RX_CSO;
1527}
1528
1529static int set_rx_csum(struct net_device *dev, u32 data)
1530{
1531 struct port_info *p = netdev_priv(dev);
1532
1533 if (data)
1534 p->rx_offload |= RX_CSO;
1535 else
1536 p->rx_offload &= ~RX_CSO;
1537 return 0;
1538}
1539
1540static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
1541{
1542 const struct port_info *pi = netdev_priv(dev);
1543 const struct sge *s = &pi->adapter->sge;
1544
1545 e->rx_max_pending = MAX_RX_BUFFERS;
1546 e->rx_mini_max_pending = MAX_RSPQ_ENTRIES;
1547 e->rx_jumbo_max_pending = 0;
1548 e->tx_max_pending = MAX_TXQ_ENTRIES;
1549
1550 e->rx_pending = s->ethrxq[pi->first_qset].fl.size - 8;
1551 e->rx_mini_pending = s->ethrxq[pi->first_qset].rspq.size;
1552 e->rx_jumbo_pending = 0;
1553 e->tx_pending = s->ethtxq[pi->first_qset].q.size;
1554}
1555
1556static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
1557{
1558 int i;
1559 const struct port_info *pi = netdev_priv(dev);
1560 struct adapter *adapter = pi->adapter;
1561 struct sge *s = &adapter->sge;
1562
1563 if (e->rx_pending > MAX_RX_BUFFERS || e->rx_jumbo_pending ||
1564 e->tx_pending > MAX_TXQ_ENTRIES ||
1565 e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
1566 e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
1567 e->rx_pending < MIN_FL_ENTRIES || e->tx_pending < MIN_TXQ_ENTRIES)
1568 return -EINVAL;
1569
1570 if (adapter->flags & FULL_INIT_DONE)
1571 return -EBUSY;
1572
1573 for (i = 0; i < pi->nqsets; ++i) {
1574 s->ethtxq[pi->first_qset + i].q.size = e->tx_pending;
1575 s->ethrxq[pi->first_qset + i].fl.size = e->rx_pending + 8;
1576 s->ethrxq[pi->first_qset + i].rspq.size = e->rx_mini_pending;
1577 }
1578 return 0;
1579}
1580
1581static int closest_timer(const struct sge *s, int time)
1582{
1583 int i, delta, match = 0, min_delta = INT_MAX;
1584
1585 for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
1586 delta = time - s->timer_val[i];
1587 if (delta < 0)
1588 delta = -delta;
1589 if (delta < min_delta) {
1590 min_delta = delta;
1591 match = i;
1592 }
1593 }
1594 return match;
1595}
1596
1597static int closest_thres(const struct sge *s, int thres)
1598{
1599 int i, delta, match = 0, min_delta = INT_MAX;
1600
1601 for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
1602 delta = thres - s->counter_val[i];
1603 if (delta < 0)
1604 delta = -delta;
1605 if (delta < min_delta) {
1606 min_delta = delta;
1607 match = i;
1608 }
1609 }
1610 return match;
1611}
1612
1613/*
1614 * Return a queue's interrupt hold-off time in us. 0 means no timer.
1615 */
1616static unsigned int qtimer_val(const struct adapter *adap,
1617 const struct sge_rspq *q)
1618{
1619 unsigned int idx = q->intr_params >> 1;
1620
1621 return idx < SGE_NTIMERS ? adap->sge.timer_val[idx] : 0;
1622}
1623
1624/**
1625 * set_rxq_intr_params - set a queue's interrupt holdoff parameters
1626 * @adap: the adapter
1627 * @q: the Rx queue
1628 * @us: the hold-off time in us, or 0 to disable timer
1629 * @cnt: the hold-off packet count, or 0 to disable counter
1630 *
1631 * Sets an Rx queue's interrupt hold-off time and packet count. At least
1632 * one of the two needs to be enabled for the queue to generate interrupts.
1633 */
1634static int set_rxq_intr_params(struct adapter *adap, struct sge_rspq *q,
1635 unsigned int us, unsigned int cnt)
1636{
1637 if ((us | cnt) == 0)
1638 cnt = 1;
1639
1640 if (cnt) {
1641 int err;
1642 u32 v, new_idx;
1643
1644 new_idx = closest_thres(&adap->sge, cnt);
1645 if (q->desc && q->pktcnt_idx != new_idx) {
1646 /* the queue has already been created, update it */
1647 v = FW_PARAMS_MNEM(FW_PARAMS_MNEM_DMAQ) |
1648 FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DMAQ_IQ_INTCNTTHRESH) |
1649 FW_PARAMS_PARAM_YZ(q->cntxt_id);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00001650 err = t4_set_params(adap, adap->fn, adap->fn, 0, 1, &v,
1651 &new_idx);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001652 if (err)
1653 return err;
1654 }
1655 q->pktcnt_idx = new_idx;
1656 }
1657
1658 us = us == 0 ? 6 : closest_timer(&adap->sge, us);
1659 q->intr_params = QINTR_TIMER_IDX(us) | (cnt > 0 ? QINTR_CNT_EN : 0);
1660 return 0;
1661}
1662
1663static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1664{
1665 const struct port_info *pi = netdev_priv(dev);
1666 struct adapter *adap = pi->adapter;
1667
1668 return set_rxq_intr_params(adap, &adap->sge.ethrxq[pi->first_qset].rspq,
1669 c->rx_coalesce_usecs, c->rx_max_coalesced_frames);
1670}
1671
1672static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1673{
1674 const struct port_info *pi = netdev_priv(dev);
1675 const struct adapter *adap = pi->adapter;
1676 const struct sge_rspq *rq = &adap->sge.ethrxq[pi->first_qset].rspq;
1677
1678 c->rx_coalesce_usecs = qtimer_val(adap, rq);
1679 c->rx_max_coalesced_frames = (rq->intr_params & QINTR_CNT_EN) ?
1680 adap->sge.counter_val[rq->pktcnt_idx] : 0;
1681 return 0;
1682}
1683
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001684/**
1685 * eeprom_ptov - translate a physical EEPROM address to virtual
1686 * @phys_addr: the physical EEPROM address
1687 * @fn: the PCI function number
1688 * @sz: size of function-specific area
1689 *
1690 * Translate a physical EEPROM address to virtual. The first 1K is
1691 * accessed through virtual addresses starting at 31K, the rest is
1692 * accessed through virtual addresses starting at 0.
1693 *
1694 * The mapping is as follows:
1695 * [0..1K) -> [31K..32K)
1696 * [1K..1K+A) -> [31K-A..31K)
1697 * [1K+A..ES) -> [0..ES-A-1K)
1698 *
1699 * where A = @fn * @sz, and ES = EEPROM size.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001700 */
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001701static int eeprom_ptov(unsigned int phys_addr, unsigned int fn, unsigned int sz)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001702{
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001703 fn *= sz;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001704 if (phys_addr < 1024)
1705 return phys_addr + (31 << 10);
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001706 if (phys_addr < 1024 + fn)
1707 return 31744 - fn + phys_addr - 1024;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001708 if (phys_addr < EEPROMSIZE)
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001709 return phys_addr - 1024 - fn;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001710 return -EINVAL;
1711}
1712
1713/*
1714 * The next two routines implement eeprom read/write from physical addresses.
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001715 */
1716static int eeprom_rd_phys(struct adapter *adap, unsigned int phys_addr, u32 *v)
1717{
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001718 int vaddr = eeprom_ptov(phys_addr, adap->fn, EEPROMPFSIZE);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001719
1720 if (vaddr >= 0)
1721 vaddr = pci_read_vpd(adap->pdev, vaddr, sizeof(u32), v);
1722 return vaddr < 0 ? vaddr : 0;
1723}
1724
1725static int eeprom_wr_phys(struct adapter *adap, unsigned int phys_addr, u32 v)
1726{
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001727 int vaddr = eeprom_ptov(phys_addr, adap->fn, EEPROMPFSIZE);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001728
1729 if (vaddr >= 0)
1730 vaddr = pci_write_vpd(adap->pdev, vaddr, sizeof(u32), &v);
1731 return vaddr < 0 ? vaddr : 0;
1732}
1733
1734#define EEPROM_MAGIC 0x38E2F10C
1735
1736static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
1737 u8 *data)
1738{
1739 int i, err = 0;
1740 struct adapter *adapter = netdev2adap(dev);
1741
1742 u8 *buf = kmalloc(EEPROMSIZE, GFP_KERNEL);
1743 if (!buf)
1744 return -ENOMEM;
1745
1746 e->magic = EEPROM_MAGIC;
1747 for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
1748 err = eeprom_rd_phys(adapter, i, (u32 *)&buf[i]);
1749
1750 if (!err)
1751 memcpy(data, buf + e->offset, e->len);
1752 kfree(buf);
1753 return err;
1754}
1755
1756static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
1757 u8 *data)
1758{
1759 u8 *buf;
1760 int err = 0;
1761 u32 aligned_offset, aligned_len, *p;
1762 struct adapter *adapter = netdev2adap(dev);
1763
1764 if (eeprom->magic != EEPROM_MAGIC)
1765 return -EINVAL;
1766
1767 aligned_offset = eeprom->offset & ~3;
1768 aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
1769
Dimitris Michailidis1478b3e2010-08-23 17:20:59 +00001770 if (adapter->fn > 0) {
1771 u32 start = 1024 + adapter->fn * EEPROMPFSIZE;
1772
1773 if (aligned_offset < start ||
1774 aligned_offset + aligned_len > start + EEPROMPFSIZE)
1775 return -EPERM;
1776 }
1777
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001778 if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
1779 /*
1780 * RMW possibly needed for first or last words.
1781 */
1782 buf = kmalloc(aligned_len, GFP_KERNEL);
1783 if (!buf)
1784 return -ENOMEM;
1785 err = eeprom_rd_phys(adapter, aligned_offset, (u32 *)buf);
1786 if (!err && aligned_len > 4)
1787 err = eeprom_rd_phys(adapter,
1788 aligned_offset + aligned_len - 4,
1789 (u32 *)&buf[aligned_len - 4]);
1790 if (err)
1791 goto out;
1792 memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
1793 } else
1794 buf = data;
1795
1796 err = t4_seeprom_wp(adapter, false);
1797 if (err)
1798 goto out;
1799
1800 for (p = (u32 *)buf; !err && aligned_len; aligned_len -= 4, p++) {
1801 err = eeprom_wr_phys(adapter, aligned_offset, *p);
1802 aligned_offset += 4;
1803 }
1804
1805 if (!err)
1806 err = t4_seeprom_wp(adapter, true);
1807out:
1808 if (buf != data)
1809 kfree(buf);
1810 return err;
1811}
1812
1813static int set_flash(struct net_device *netdev, struct ethtool_flash *ef)
1814{
1815 int ret;
1816 const struct firmware *fw;
1817 struct adapter *adap = netdev2adap(netdev);
1818
1819 ef->data[sizeof(ef->data) - 1] = '\0';
1820 ret = request_firmware(&fw, ef->data, adap->pdev_dev);
1821 if (ret < 0)
1822 return ret;
1823
1824 ret = t4_load_fw(adap, fw->data, fw->size);
1825 release_firmware(fw);
1826 if (!ret)
1827 dev_info(adap->pdev_dev, "loaded firmware %s\n", ef->data);
1828 return ret;
1829}
1830
1831#define WOL_SUPPORTED (WAKE_BCAST | WAKE_MAGIC)
1832#define BCAST_CRC 0xa0ccc1a6
1833
1834static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1835{
1836 wol->supported = WAKE_BCAST | WAKE_MAGIC;
1837 wol->wolopts = netdev2adap(dev)->wol;
1838 memset(&wol->sopass, 0, sizeof(wol->sopass));
1839}
1840
1841static int set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1842{
1843 int err = 0;
1844 struct port_info *pi = netdev_priv(dev);
1845
1846 if (wol->wolopts & ~WOL_SUPPORTED)
1847 return -EINVAL;
1848 t4_wol_magic_enable(pi->adapter, pi->tx_chan,
1849 (wol->wolopts & WAKE_MAGIC) ? dev->dev_addr : NULL);
1850 if (wol->wolopts & WAKE_BCAST) {
1851 err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0xfe, ~0ULL,
1852 ~0ULL, 0, false);
1853 if (!err)
1854 err = t4_wol_pat_enable(pi->adapter, pi->tx_chan, 1,
1855 ~6ULL, ~0ULL, BCAST_CRC, true);
1856 } else
1857 t4_wol_pat_enable(pi->adapter, pi->tx_chan, 0, 0, 0, 0, false);
1858 return err;
1859}
1860
Dimitris Michailidis35d35682010-08-02 13:19:20 +00001861#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
1862
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001863static int set_tso(struct net_device *dev, u32 value)
1864{
1865 if (value)
Dimitris Michailidis35d35682010-08-02 13:19:20 +00001866 dev->features |= TSO_FLAGS;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001867 else
Dimitris Michailidis35d35682010-08-02 13:19:20 +00001868 dev->features &= ~TSO_FLAGS;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001869 return 0;
1870}
1871
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001872static int set_flags(struct net_device *dev, u32 flags)
1873{
Ben Hutchings1437ce32010-06-30 02:44:32 +00001874 return ethtool_op_set_flags(dev, flags, ETH_FLAG_RXHASH);
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001875}
1876
Dimitris Michailidis671b0062010-07-11 12:01:17 +00001877static int get_rss_table(struct net_device *dev, struct ethtool_rxfh_indir *p)
1878{
1879 const struct port_info *pi = netdev_priv(dev);
1880 unsigned int n = min_t(unsigned int, p->size, pi->rss_size);
1881
1882 p->size = pi->rss_size;
1883 while (n--)
1884 p->ring_index[n] = pi->rss[n];
1885 return 0;
1886}
1887
1888static int set_rss_table(struct net_device *dev,
1889 const struct ethtool_rxfh_indir *p)
1890{
1891 unsigned int i;
1892 struct port_info *pi = netdev_priv(dev);
1893
1894 if (p->size != pi->rss_size)
1895 return -EINVAL;
1896 for (i = 0; i < p->size; i++)
1897 if (p->ring_index[i] >= pi->nqsets)
1898 return -EINVAL;
1899 for (i = 0; i < p->size; i++)
1900 pi->rss[i] = p->ring_index[i];
1901 if (pi->adapter->flags & FULL_INIT_DONE)
1902 return write_rss(pi, pi->rss);
1903 return 0;
1904}
1905
1906static int get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info,
1907 void *rules)
1908{
Dimitris Michailidisf7965642010-07-11 12:01:18 +00001909 const struct port_info *pi = netdev_priv(dev);
1910
Dimitris Michailidis671b0062010-07-11 12:01:17 +00001911 switch (info->cmd) {
Dimitris Michailidisf7965642010-07-11 12:01:18 +00001912 case ETHTOOL_GRXFH: {
1913 unsigned int v = pi->rss_mode;
1914
1915 info->data = 0;
1916 switch (info->flow_type) {
1917 case TCP_V4_FLOW:
1918 if (v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN)
1919 info->data = RXH_IP_SRC | RXH_IP_DST |
1920 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1921 else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
1922 info->data = RXH_IP_SRC | RXH_IP_DST;
1923 break;
1924 case UDP_V4_FLOW:
1925 if ((v & FW_RSS_VI_CONFIG_CMD_IP4FOURTUPEN) &&
1926 (v & FW_RSS_VI_CONFIG_CMD_UDPEN))
1927 info->data = RXH_IP_SRC | RXH_IP_DST |
1928 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1929 else if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
1930 info->data = RXH_IP_SRC | RXH_IP_DST;
1931 break;
1932 case SCTP_V4_FLOW:
1933 case AH_ESP_V4_FLOW:
1934 case IPV4_FLOW:
1935 if (v & FW_RSS_VI_CONFIG_CMD_IP4TWOTUPEN)
1936 info->data = RXH_IP_SRC | RXH_IP_DST;
1937 break;
1938 case TCP_V6_FLOW:
1939 if (v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN)
1940 info->data = RXH_IP_SRC | RXH_IP_DST |
1941 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1942 else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
1943 info->data = RXH_IP_SRC | RXH_IP_DST;
1944 break;
1945 case UDP_V6_FLOW:
1946 if ((v & FW_RSS_VI_CONFIG_CMD_IP6FOURTUPEN) &&
1947 (v & FW_RSS_VI_CONFIG_CMD_UDPEN))
1948 info->data = RXH_IP_SRC | RXH_IP_DST |
1949 RXH_L4_B_0_1 | RXH_L4_B_2_3;
1950 else if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
1951 info->data = RXH_IP_SRC | RXH_IP_DST;
1952 break;
1953 case SCTP_V6_FLOW:
1954 case AH_ESP_V6_FLOW:
1955 case IPV6_FLOW:
1956 if (v & FW_RSS_VI_CONFIG_CMD_IP6TWOTUPEN)
1957 info->data = RXH_IP_SRC | RXH_IP_DST;
1958 break;
1959 }
1960 return 0;
1961 }
Dimitris Michailidis671b0062010-07-11 12:01:17 +00001962 case ETHTOOL_GRXRINGS:
Dimitris Michailidisf7965642010-07-11 12:01:18 +00001963 info->data = pi->nqsets;
Dimitris Michailidis671b0062010-07-11 12:01:17 +00001964 return 0;
1965 }
1966 return -EOPNOTSUPP;
1967}
1968
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00001969static struct ethtool_ops cxgb_ethtool_ops = {
1970 .get_settings = get_settings,
1971 .set_settings = set_settings,
1972 .get_drvinfo = get_drvinfo,
1973 .get_msglevel = get_msglevel,
1974 .set_msglevel = set_msglevel,
1975 .get_ringparam = get_sge_param,
1976 .set_ringparam = set_sge_param,
1977 .get_coalesce = get_coalesce,
1978 .set_coalesce = set_coalesce,
1979 .get_eeprom_len = get_eeprom_len,
1980 .get_eeprom = get_eeprom,
1981 .set_eeprom = set_eeprom,
1982 .get_pauseparam = get_pauseparam,
1983 .set_pauseparam = set_pauseparam,
1984 .get_rx_csum = get_rx_csum,
1985 .set_rx_csum = set_rx_csum,
1986 .set_tx_csum = ethtool_op_set_tx_ipv6_csum,
1987 .set_sg = ethtool_op_set_sg,
1988 .get_link = ethtool_op_get_link,
1989 .get_strings = get_strings,
1990 .phys_id = identify_port,
1991 .nway_reset = restart_autoneg,
1992 .get_sset_count = get_sset_count,
1993 .get_ethtool_stats = get_stats,
1994 .get_regs_len = get_regs_len,
1995 .get_regs = get_regs,
1996 .get_wol = get_wol,
1997 .set_wol = set_wol,
1998 .set_tso = set_tso,
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07001999 .set_flags = set_flags,
Dimitris Michailidis671b0062010-07-11 12:01:17 +00002000 .get_rxnfc = get_rxnfc,
2001 .get_rxfh_indir = get_rss_table,
2002 .set_rxfh_indir = set_rss_table,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002003 .flash_device = set_flash,
2004};
2005
2006/*
2007 * debugfs support
2008 */
2009
2010static int mem_open(struct inode *inode, struct file *file)
2011{
2012 file->private_data = inode->i_private;
2013 return 0;
2014}
2015
2016static ssize_t mem_read(struct file *file, char __user *buf, size_t count,
2017 loff_t *ppos)
2018{
2019 loff_t pos = *ppos;
2020 loff_t avail = file->f_path.dentry->d_inode->i_size;
2021 unsigned int mem = (uintptr_t)file->private_data & 3;
2022 struct adapter *adap = file->private_data - mem;
2023
2024 if (pos < 0)
2025 return -EINVAL;
2026 if (pos >= avail)
2027 return 0;
2028 if (count > avail - pos)
2029 count = avail - pos;
2030
2031 while (count) {
2032 size_t len;
2033 int ret, ofst;
2034 __be32 data[16];
2035
2036 if (mem == MEM_MC)
2037 ret = t4_mc_read(adap, pos, data, NULL);
2038 else
2039 ret = t4_edc_read(adap, mem, pos, data, NULL);
2040 if (ret)
2041 return ret;
2042
2043 ofst = pos % sizeof(data);
2044 len = min(count, sizeof(data) - ofst);
2045 if (copy_to_user(buf, (u8 *)data + ofst, len))
2046 return -EFAULT;
2047
2048 buf += len;
2049 pos += len;
2050 count -= len;
2051 }
2052 count = pos - *ppos;
2053 *ppos = pos;
2054 return count;
2055}
2056
2057static const struct file_operations mem_debugfs_fops = {
2058 .owner = THIS_MODULE,
2059 .open = mem_open,
2060 .read = mem_read,
2061};
2062
2063static void __devinit add_debugfs_mem(struct adapter *adap, const char *name,
2064 unsigned int idx, unsigned int size_mb)
2065{
2066 struct dentry *de;
2067
2068 de = debugfs_create_file(name, S_IRUSR, adap->debugfs_root,
2069 (void *)adap + idx, &mem_debugfs_fops);
2070 if (de && de->d_inode)
2071 de->d_inode->i_size = size_mb << 20;
2072}
2073
2074static int __devinit setup_debugfs(struct adapter *adap)
2075{
2076 int i;
2077
2078 if (IS_ERR_OR_NULL(adap->debugfs_root))
2079 return -1;
2080
2081 i = t4_read_reg(adap, MA_TARGET_MEM_ENABLE);
2082 if (i & EDRAM0_ENABLE)
2083 add_debugfs_mem(adap, "edc0", MEM_EDC0, 5);
2084 if (i & EDRAM1_ENABLE)
2085 add_debugfs_mem(adap, "edc1", MEM_EDC1, 5);
2086 if (i & EXT_MEM_ENABLE)
2087 add_debugfs_mem(adap, "mc", MEM_MC,
2088 EXT_MEM_SIZE_GET(t4_read_reg(adap, MA_EXT_MEMORY_BAR)));
2089 if (adap->l2t)
2090 debugfs_create_file("l2t", S_IRUSR, adap->debugfs_root, adap,
2091 &t4_l2t_fops);
2092 return 0;
2093}
2094
2095/*
2096 * upper-layer driver support
2097 */
2098
2099/*
2100 * Allocate an active-open TID and set it to the supplied value.
2101 */
2102int cxgb4_alloc_atid(struct tid_info *t, void *data)
2103{
2104 int atid = -1;
2105
2106 spin_lock_bh(&t->atid_lock);
2107 if (t->afree) {
2108 union aopen_entry *p = t->afree;
2109
2110 atid = p - t->atid_tab;
2111 t->afree = p->next;
2112 p->data = data;
2113 t->atids_in_use++;
2114 }
2115 spin_unlock_bh(&t->atid_lock);
2116 return atid;
2117}
2118EXPORT_SYMBOL(cxgb4_alloc_atid);
2119
2120/*
2121 * Release an active-open TID.
2122 */
2123void cxgb4_free_atid(struct tid_info *t, unsigned int atid)
2124{
2125 union aopen_entry *p = &t->atid_tab[atid];
2126
2127 spin_lock_bh(&t->atid_lock);
2128 p->next = t->afree;
2129 t->afree = p;
2130 t->atids_in_use--;
2131 spin_unlock_bh(&t->atid_lock);
2132}
2133EXPORT_SYMBOL(cxgb4_free_atid);
2134
2135/*
2136 * Allocate a server TID and set it to the supplied value.
2137 */
2138int cxgb4_alloc_stid(struct tid_info *t, int family, void *data)
2139{
2140 int stid;
2141
2142 spin_lock_bh(&t->stid_lock);
2143 if (family == PF_INET) {
2144 stid = find_first_zero_bit(t->stid_bmap, t->nstids);
2145 if (stid < t->nstids)
2146 __set_bit(stid, t->stid_bmap);
2147 else
2148 stid = -1;
2149 } else {
2150 stid = bitmap_find_free_region(t->stid_bmap, t->nstids, 2);
2151 if (stid < 0)
2152 stid = -1;
2153 }
2154 if (stid >= 0) {
2155 t->stid_tab[stid].data = data;
2156 stid += t->stid_base;
2157 t->stids_in_use++;
2158 }
2159 spin_unlock_bh(&t->stid_lock);
2160 return stid;
2161}
2162EXPORT_SYMBOL(cxgb4_alloc_stid);
2163
2164/*
2165 * Release a server TID.
2166 */
2167void cxgb4_free_stid(struct tid_info *t, unsigned int stid, int family)
2168{
2169 stid -= t->stid_base;
2170 spin_lock_bh(&t->stid_lock);
2171 if (family == PF_INET)
2172 __clear_bit(stid, t->stid_bmap);
2173 else
2174 bitmap_release_region(t->stid_bmap, stid, 2);
2175 t->stid_tab[stid].data = NULL;
2176 t->stids_in_use--;
2177 spin_unlock_bh(&t->stid_lock);
2178}
2179EXPORT_SYMBOL(cxgb4_free_stid);
2180
2181/*
2182 * Populate a TID_RELEASE WR. Caller must properly size the skb.
2183 */
2184static void mk_tid_release(struct sk_buff *skb, unsigned int chan,
2185 unsigned int tid)
2186{
2187 struct cpl_tid_release *req;
2188
2189 set_wr_txq(skb, CPL_PRIORITY_SETUP, chan);
2190 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
2191 INIT_TP_WR(req, tid);
2192 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
2193}
2194
2195/*
2196 * Queue a TID release request and if necessary schedule a work queue to
2197 * process it.
2198 */
2199void cxgb4_queue_tid_release(struct tid_info *t, unsigned int chan,
2200 unsigned int tid)
2201{
2202 void **p = &t->tid_tab[tid];
2203 struct adapter *adap = container_of(t, struct adapter, tids);
2204
2205 spin_lock_bh(&adap->tid_release_lock);
2206 *p = adap->tid_release_head;
2207 /* Low 2 bits encode the Tx channel number */
2208 adap->tid_release_head = (void **)((uintptr_t)p | chan);
2209 if (!adap->tid_release_task_busy) {
2210 adap->tid_release_task_busy = true;
2211 schedule_work(&adap->tid_release_task);
2212 }
2213 spin_unlock_bh(&adap->tid_release_lock);
2214}
2215EXPORT_SYMBOL(cxgb4_queue_tid_release);
2216
2217/*
2218 * Process the list of pending TID release requests.
2219 */
2220static void process_tid_release_list(struct work_struct *work)
2221{
2222 struct sk_buff *skb;
2223 struct adapter *adap;
2224
2225 adap = container_of(work, struct adapter, tid_release_task);
2226
2227 spin_lock_bh(&adap->tid_release_lock);
2228 while (adap->tid_release_head) {
2229 void **p = adap->tid_release_head;
2230 unsigned int chan = (uintptr_t)p & 3;
2231 p = (void *)p - chan;
2232
2233 adap->tid_release_head = *p;
2234 *p = NULL;
2235 spin_unlock_bh(&adap->tid_release_lock);
2236
2237 while (!(skb = alloc_skb(sizeof(struct cpl_tid_release),
2238 GFP_KERNEL)))
2239 schedule_timeout_uninterruptible(1);
2240
2241 mk_tid_release(skb, chan, p - adap->tids.tid_tab);
2242 t4_ofld_send(adap, skb);
2243 spin_lock_bh(&adap->tid_release_lock);
2244 }
2245 adap->tid_release_task_busy = false;
2246 spin_unlock_bh(&adap->tid_release_lock);
2247}
2248
2249/*
2250 * Release a TID and inform HW. If we are unable to allocate the release
2251 * message we defer to a work queue.
2252 */
2253void cxgb4_remove_tid(struct tid_info *t, unsigned int chan, unsigned int tid)
2254{
2255 void *old;
2256 struct sk_buff *skb;
2257 struct adapter *adap = container_of(t, struct adapter, tids);
2258
2259 old = t->tid_tab[tid];
2260 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
2261 if (likely(skb)) {
2262 t->tid_tab[tid] = NULL;
2263 mk_tid_release(skb, chan, tid);
2264 t4_ofld_send(adap, skb);
2265 } else
2266 cxgb4_queue_tid_release(t, chan, tid);
2267 if (old)
2268 atomic_dec(&t->tids_in_use);
2269}
2270EXPORT_SYMBOL(cxgb4_remove_tid);
2271
2272/*
2273 * Allocate and initialize the TID tables. Returns 0 on success.
2274 */
2275static int tid_init(struct tid_info *t)
2276{
2277 size_t size;
2278 unsigned int natids = t->natids;
2279
2280 size = t->ntids * sizeof(*t->tid_tab) + natids * sizeof(*t->atid_tab) +
2281 t->nstids * sizeof(*t->stid_tab) +
2282 BITS_TO_LONGS(t->nstids) * sizeof(long);
2283 t->tid_tab = t4_alloc_mem(size);
2284 if (!t->tid_tab)
2285 return -ENOMEM;
2286
2287 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
2288 t->stid_tab = (struct serv_entry *)&t->atid_tab[natids];
2289 t->stid_bmap = (unsigned long *)&t->stid_tab[t->nstids];
2290 spin_lock_init(&t->stid_lock);
2291 spin_lock_init(&t->atid_lock);
2292
2293 t->stids_in_use = 0;
2294 t->afree = NULL;
2295 t->atids_in_use = 0;
2296 atomic_set(&t->tids_in_use, 0);
2297
2298 /* Setup the free list for atid_tab and clear the stid bitmap. */
2299 if (natids) {
2300 while (--natids)
2301 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
2302 t->afree = t->atid_tab;
2303 }
2304 bitmap_zero(t->stid_bmap, t->nstids);
2305 return 0;
2306}
2307
2308/**
2309 * cxgb4_create_server - create an IP server
2310 * @dev: the device
2311 * @stid: the server TID
2312 * @sip: local IP address to bind server to
2313 * @sport: the server's TCP port
2314 * @queue: queue to direct messages from this server to
2315 *
2316 * Create an IP server for the given port and address.
2317 * Returns <0 on error and one of the %NET_XMIT_* values on success.
2318 */
2319int cxgb4_create_server(const struct net_device *dev, unsigned int stid,
2320 __be32 sip, __be16 sport, unsigned int queue)
2321{
2322 unsigned int chan;
2323 struct sk_buff *skb;
2324 struct adapter *adap;
2325 struct cpl_pass_open_req *req;
2326
2327 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
2328 if (!skb)
2329 return -ENOMEM;
2330
2331 adap = netdev2adap(dev);
2332 req = (struct cpl_pass_open_req *)__skb_put(skb, sizeof(*req));
2333 INIT_TP_WR(req, 0);
2334 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, stid));
2335 req->local_port = sport;
2336 req->peer_port = htons(0);
2337 req->local_ip = sip;
2338 req->peer_ip = htonl(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002339 chan = rxq_to_chan(&adap->sge, queue);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002340 req->opt0 = cpu_to_be64(TX_CHAN(chan));
2341 req->opt1 = cpu_to_be64(CONN_POLICY_ASK |
2342 SYN_RSS_ENABLE | SYN_RSS_QUEUE(queue));
2343 return t4_mgmt_tx(adap, skb);
2344}
2345EXPORT_SYMBOL(cxgb4_create_server);
2346
2347/**
2348 * cxgb4_create_server6 - create an IPv6 server
2349 * @dev: the device
2350 * @stid: the server TID
2351 * @sip: local IPv6 address to bind server to
2352 * @sport: the server's TCP port
2353 * @queue: queue to direct messages from this server to
2354 *
2355 * Create an IPv6 server for the given port and address.
2356 * Returns <0 on error and one of the %NET_XMIT_* values on success.
2357 */
2358int cxgb4_create_server6(const struct net_device *dev, unsigned int stid,
2359 const struct in6_addr *sip, __be16 sport,
2360 unsigned int queue)
2361{
2362 unsigned int chan;
2363 struct sk_buff *skb;
2364 struct adapter *adap;
2365 struct cpl_pass_open_req6 *req;
2366
2367 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
2368 if (!skb)
2369 return -ENOMEM;
2370
2371 adap = netdev2adap(dev);
2372 req = (struct cpl_pass_open_req6 *)__skb_put(skb, sizeof(*req));
2373 INIT_TP_WR(req, 0);
2374 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, stid));
2375 req->local_port = sport;
2376 req->peer_port = htons(0);
2377 req->local_ip_hi = *(__be64 *)(sip->s6_addr);
2378 req->local_ip_lo = *(__be64 *)(sip->s6_addr + 8);
2379 req->peer_ip_hi = cpu_to_be64(0);
2380 req->peer_ip_lo = cpu_to_be64(0);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00002381 chan = rxq_to_chan(&adap->sge, queue);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002382 req->opt0 = cpu_to_be64(TX_CHAN(chan));
2383 req->opt1 = cpu_to_be64(CONN_POLICY_ASK |
2384 SYN_RSS_ENABLE | SYN_RSS_QUEUE(queue));
2385 return t4_mgmt_tx(adap, skb);
2386}
2387EXPORT_SYMBOL(cxgb4_create_server6);
2388
2389/**
2390 * cxgb4_best_mtu - find the entry in the MTU table closest to an MTU
2391 * @mtus: the HW MTU table
2392 * @mtu: the target MTU
2393 * @idx: index of selected entry in the MTU table
2394 *
2395 * Returns the index and the value in the HW MTU table that is closest to
2396 * but does not exceed @mtu, unless @mtu is smaller than any value in the
2397 * table, in which case that smallest available value is selected.
2398 */
2399unsigned int cxgb4_best_mtu(const unsigned short *mtus, unsigned short mtu,
2400 unsigned int *idx)
2401{
2402 unsigned int i = 0;
2403
2404 while (i < NMTUS - 1 && mtus[i + 1] <= mtu)
2405 ++i;
2406 if (idx)
2407 *idx = i;
2408 return mtus[i];
2409}
2410EXPORT_SYMBOL(cxgb4_best_mtu);
2411
2412/**
2413 * cxgb4_port_chan - get the HW channel of a port
2414 * @dev: the net device for the port
2415 *
2416 * Return the HW Tx channel of the given port.
2417 */
2418unsigned int cxgb4_port_chan(const struct net_device *dev)
2419{
2420 return netdev2pinfo(dev)->tx_chan;
2421}
2422EXPORT_SYMBOL(cxgb4_port_chan);
2423
2424/**
2425 * cxgb4_port_viid - get the VI id of a port
2426 * @dev: the net device for the port
2427 *
2428 * Return the VI id of the given port.
2429 */
2430unsigned int cxgb4_port_viid(const struct net_device *dev)
2431{
2432 return netdev2pinfo(dev)->viid;
2433}
2434EXPORT_SYMBOL(cxgb4_port_viid);
2435
2436/**
2437 * cxgb4_port_idx - get the index of a port
2438 * @dev: the net device for the port
2439 *
2440 * Return the index of the given port.
2441 */
2442unsigned int cxgb4_port_idx(const struct net_device *dev)
2443{
2444 return netdev2pinfo(dev)->port_id;
2445}
2446EXPORT_SYMBOL(cxgb4_port_idx);
2447
2448/**
2449 * cxgb4_netdev_by_hwid - return the net device of a HW port
2450 * @pdev: identifies the adapter
2451 * @id: the HW port id
2452 *
2453 * Return the net device associated with the interface with the given HW
2454 * id.
2455 */
2456struct net_device *cxgb4_netdev_by_hwid(struct pci_dev *pdev, unsigned int id)
2457{
2458 const struct adapter *adap = pci_get_drvdata(pdev);
2459
2460 if (!adap || id >= NCHAN)
2461 return NULL;
2462 id = adap->chan_map[id];
2463 return id < MAX_NPORTS ? adap->port[id] : NULL;
2464}
2465EXPORT_SYMBOL(cxgb4_netdev_by_hwid);
2466
2467void cxgb4_get_tcp_stats(struct pci_dev *pdev, struct tp_tcp_stats *v4,
2468 struct tp_tcp_stats *v6)
2469{
2470 struct adapter *adap = pci_get_drvdata(pdev);
2471
2472 spin_lock(&adap->stats_lock);
2473 t4_tp_get_tcp_stats(adap, v4, v6);
2474 spin_unlock(&adap->stats_lock);
2475}
2476EXPORT_SYMBOL(cxgb4_get_tcp_stats);
2477
2478void cxgb4_iscsi_init(struct net_device *dev, unsigned int tag_mask,
2479 const unsigned int *pgsz_order)
2480{
2481 struct adapter *adap = netdev2adap(dev);
2482
2483 t4_write_reg(adap, ULP_RX_ISCSI_TAGMASK, tag_mask);
2484 t4_write_reg(adap, ULP_RX_ISCSI_PSZ, HPZ0(pgsz_order[0]) |
2485 HPZ1(pgsz_order[1]) | HPZ2(pgsz_order[2]) |
2486 HPZ3(pgsz_order[3]));
2487}
2488EXPORT_SYMBOL(cxgb4_iscsi_init);
2489
2490static struct pci_driver cxgb4_driver;
2491
2492static void check_neigh_update(struct neighbour *neigh)
2493{
2494 const struct device *parent;
2495 const struct net_device *netdev = neigh->dev;
2496
2497 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2498 netdev = vlan_dev_real_dev(netdev);
2499 parent = netdev->dev.parent;
2500 if (parent && parent->driver == &cxgb4_driver.driver)
2501 t4_l2t_update(dev_get_drvdata(parent), neigh);
2502}
2503
2504static int netevent_cb(struct notifier_block *nb, unsigned long event,
2505 void *data)
2506{
2507 switch (event) {
2508 case NETEVENT_NEIGH_UPDATE:
2509 check_neigh_update(data);
2510 break;
2511 case NETEVENT_PMTU_UPDATE:
2512 case NETEVENT_REDIRECT:
2513 default:
2514 break;
2515 }
2516 return 0;
2517}
2518
2519static bool netevent_registered;
2520static struct notifier_block cxgb4_netevent_nb = {
2521 .notifier_call = netevent_cb
2522};
2523
2524static void uld_attach(struct adapter *adap, unsigned int uld)
2525{
2526 void *handle;
2527 struct cxgb4_lld_info lli;
2528
2529 lli.pdev = adap->pdev;
2530 lli.l2t = adap->l2t;
2531 lli.tids = &adap->tids;
2532 lli.ports = adap->port;
2533 lli.vr = &adap->vres;
2534 lli.mtus = adap->params.mtus;
2535 if (uld == CXGB4_ULD_RDMA) {
2536 lli.rxq_ids = adap->sge.rdma_rxq;
2537 lli.nrxq = adap->sge.rdmaqs;
2538 } else if (uld == CXGB4_ULD_ISCSI) {
2539 lli.rxq_ids = adap->sge.ofld_rxq;
2540 lli.nrxq = adap->sge.ofldqsets;
2541 }
2542 lli.ntxq = adap->sge.ofldqsets;
2543 lli.nchan = adap->params.nports;
2544 lli.nports = adap->params.nports;
2545 lli.wr_cred = adap->params.ofldq_wr_cred;
2546 lli.adapter_type = adap->params.rev;
2547 lli.iscsi_iolen = MAXRXDATA_GET(t4_read_reg(adap, TP_PARA_REG2));
2548 lli.udb_density = 1 << QUEUESPERPAGEPF0_GET(
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002549 t4_read_reg(adap, SGE_EGRESS_QUEUES_PER_PAGE_PF) >>
2550 (adap->fn * 4));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002551 lli.ucq_density = 1 << QUEUESPERPAGEPF0_GET(
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002552 t4_read_reg(adap, SGE_INGRESS_QUEUES_PER_PAGE_PF) >>
2553 (adap->fn * 4));
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002554 lli.gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS);
2555 lli.db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL);
2556 lli.fw_vers = adap->params.fw_vers;
2557
2558 handle = ulds[uld].add(&lli);
2559 if (IS_ERR(handle)) {
2560 dev_warn(adap->pdev_dev,
2561 "could not attach to the %s driver, error %ld\n",
2562 uld_str[uld], PTR_ERR(handle));
2563 return;
2564 }
2565
2566 adap->uld_handle[uld] = handle;
2567
2568 if (!netevent_registered) {
2569 register_netevent_notifier(&cxgb4_netevent_nb);
2570 netevent_registered = true;
2571 }
Dimitris Michailidise29f5db2010-05-18 10:07:13 +00002572
2573 if (adap->flags & FULL_INIT_DONE)
2574 ulds[uld].state_change(handle, CXGB4_STATE_UP);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002575}
2576
2577static void attach_ulds(struct adapter *adap)
2578{
2579 unsigned int i;
2580
2581 mutex_lock(&uld_mutex);
2582 list_add_tail(&adap->list_node, &adapter_list);
2583 for (i = 0; i < CXGB4_ULD_MAX; i++)
2584 if (ulds[i].add)
2585 uld_attach(adap, i);
2586 mutex_unlock(&uld_mutex);
2587}
2588
2589static void detach_ulds(struct adapter *adap)
2590{
2591 unsigned int i;
2592
2593 mutex_lock(&uld_mutex);
2594 list_del(&adap->list_node);
2595 for (i = 0; i < CXGB4_ULD_MAX; i++)
2596 if (adap->uld_handle[i]) {
2597 ulds[i].state_change(adap->uld_handle[i],
2598 CXGB4_STATE_DETACH);
2599 adap->uld_handle[i] = NULL;
2600 }
2601 if (netevent_registered && list_empty(&adapter_list)) {
2602 unregister_netevent_notifier(&cxgb4_netevent_nb);
2603 netevent_registered = false;
2604 }
2605 mutex_unlock(&uld_mutex);
2606}
2607
2608static void notify_ulds(struct adapter *adap, enum cxgb4_state new_state)
2609{
2610 unsigned int i;
2611
2612 mutex_lock(&uld_mutex);
2613 for (i = 0; i < CXGB4_ULD_MAX; i++)
2614 if (adap->uld_handle[i])
2615 ulds[i].state_change(adap->uld_handle[i], new_state);
2616 mutex_unlock(&uld_mutex);
2617}
2618
2619/**
2620 * cxgb4_register_uld - register an upper-layer driver
2621 * @type: the ULD type
2622 * @p: the ULD methods
2623 *
2624 * Registers an upper-layer driver with this driver and notifies the ULD
2625 * about any presently available devices that support its type. Returns
2626 * %-EBUSY if a ULD of the same type is already registered.
2627 */
2628int cxgb4_register_uld(enum cxgb4_uld type, const struct cxgb4_uld_info *p)
2629{
2630 int ret = 0;
2631 struct adapter *adap;
2632
2633 if (type >= CXGB4_ULD_MAX)
2634 return -EINVAL;
2635 mutex_lock(&uld_mutex);
2636 if (ulds[type].add) {
2637 ret = -EBUSY;
2638 goto out;
2639 }
2640 ulds[type] = *p;
2641 list_for_each_entry(adap, &adapter_list, list_node)
2642 uld_attach(adap, type);
2643out: mutex_unlock(&uld_mutex);
2644 return ret;
2645}
2646EXPORT_SYMBOL(cxgb4_register_uld);
2647
2648/**
2649 * cxgb4_unregister_uld - unregister an upper-layer driver
2650 * @type: the ULD type
2651 *
2652 * Unregisters an existing upper-layer driver.
2653 */
2654int cxgb4_unregister_uld(enum cxgb4_uld type)
2655{
2656 struct adapter *adap;
2657
2658 if (type >= CXGB4_ULD_MAX)
2659 return -EINVAL;
2660 mutex_lock(&uld_mutex);
2661 list_for_each_entry(adap, &adapter_list, list_node)
2662 adap->uld_handle[type] = NULL;
2663 ulds[type].add = NULL;
2664 mutex_unlock(&uld_mutex);
2665 return 0;
2666}
2667EXPORT_SYMBOL(cxgb4_unregister_uld);
2668
2669/**
2670 * cxgb_up - enable the adapter
2671 * @adap: adapter being enabled
2672 *
2673 * Called when the first port is enabled, this function performs the
2674 * actions necessary to make an adapter operational, such as completing
2675 * the initialization of HW modules, and enabling interrupts.
2676 *
2677 * Must be called with the rtnl lock held.
2678 */
2679static int cxgb_up(struct adapter *adap)
2680{
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002681 int err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002682
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002683 err = setup_sge_queues(adap);
2684 if (err)
2685 goto out;
2686 err = setup_rss(adap);
2687 if (err)
2688 goto freeq;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002689
2690 if (adap->flags & USING_MSIX) {
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002691 name_msix_vecs(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002692 err = request_irq(adap->msix_info[0].vec, t4_nondata_intr, 0,
2693 adap->msix_info[0].desc, adap);
2694 if (err)
2695 goto irq_err;
2696
2697 err = request_msix_queue_irqs(adap);
2698 if (err) {
2699 free_irq(adap->msix_info[0].vec, adap);
2700 goto irq_err;
2701 }
2702 } else {
2703 err = request_irq(adap->pdev->irq, t4_intr_handler(adap),
2704 (adap->flags & USING_MSI) ? 0 : IRQF_SHARED,
2705 adap->name, adap);
2706 if (err)
2707 goto irq_err;
2708 }
2709 enable_rx(adap);
2710 t4_sge_start(adap);
2711 t4_intr_enable(adap);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002712 adap->flags |= FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002713 notify_ulds(adap, CXGB4_STATE_UP);
2714 out:
2715 return err;
2716 irq_err:
2717 dev_err(adap->pdev_dev, "request_irq failed, err %d\n", err);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002718 freeq:
2719 t4_free_sge_resources(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002720 goto out;
2721}
2722
2723static void cxgb_down(struct adapter *adapter)
2724{
2725 t4_intr_disable(adapter);
2726 cancel_work_sync(&adapter->tid_release_task);
2727 adapter->tid_release_task_busy = false;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00002728 adapter->tid_release_head = NULL;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002729
2730 if (adapter->flags & USING_MSIX) {
2731 free_msix_queue_irqs(adapter);
2732 free_irq(adapter->msix_info[0].vec, adapter);
2733 } else
2734 free_irq(adapter->pdev->irq, adapter);
2735 quiesce_rx(adapter);
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002736 t4_sge_stop(adapter);
2737 t4_free_sge_resources(adapter);
2738 adapter->flags &= ~FULL_INIT_DONE;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002739}
2740
2741/*
2742 * net_device operations
2743 */
2744static int cxgb_open(struct net_device *dev)
2745{
2746 int err;
2747 struct port_info *pi = netdev_priv(dev);
2748 struct adapter *adapter = pi->adapter;
2749
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00002750 if (!(adapter->flags & FULL_INIT_DONE)) {
2751 err = cxgb_up(adapter);
2752 if (err < 0)
2753 return err;
2754 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002755
2756 dev->real_num_tx_queues = pi->nqsets;
Dimitris Michailidisf68707b2010-06-18 10:05:32 +00002757 err = link_start(dev);
2758 if (!err)
2759 netif_tx_start_all_queues(dev);
2760 return err;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002761}
2762
2763static int cxgb_close(struct net_device *dev)
2764{
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002765 struct port_info *pi = netdev_priv(dev);
2766 struct adapter *adapter = pi->adapter;
2767
2768 netif_tx_stop_all_queues(dev);
2769 netif_carrier_off(dev);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002770 return t4_enable_vi(adapter, adapter->fn, pi->viid, false, false);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002771}
2772
Dimitris Michailidisf5152c92010-07-07 16:11:25 +00002773static struct rtnl_link_stats64 *cxgb_get_stats(struct net_device *dev,
2774 struct rtnl_link_stats64 *ns)
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002775{
2776 struct port_stats stats;
2777 struct port_info *p = netdev_priv(dev);
2778 struct adapter *adapter = p->adapter;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002779
2780 spin_lock(&adapter->stats_lock);
2781 t4_get_port_stats(adapter, p->tx_chan, &stats);
2782 spin_unlock(&adapter->stats_lock);
2783
2784 ns->tx_bytes = stats.tx_octets;
2785 ns->tx_packets = stats.tx_frames;
2786 ns->rx_bytes = stats.rx_octets;
2787 ns->rx_packets = stats.rx_frames;
2788 ns->multicast = stats.rx_mcast_frames;
2789
2790 /* detailed rx_errors */
2791 ns->rx_length_errors = stats.rx_jabber + stats.rx_too_long +
2792 stats.rx_runt;
2793 ns->rx_over_errors = 0;
2794 ns->rx_crc_errors = stats.rx_fcs_err;
2795 ns->rx_frame_errors = stats.rx_symbol_err;
2796 ns->rx_fifo_errors = stats.rx_ovflow0 + stats.rx_ovflow1 +
2797 stats.rx_ovflow2 + stats.rx_ovflow3 +
2798 stats.rx_trunc0 + stats.rx_trunc1 +
2799 stats.rx_trunc2 + stats.rx_trunc3;
2800 ns->rx_missed_errors = 0;
2801
2802 /* detailed tx_errors */
2803 ns->tx_aborted_errors = 0;
2804 ns->tx_carrier_errors = 0;
2805 ns->tx_fifo_errors = 0;
2806 ns->tx_heartbeat_errors = 0;
2807 ns->tx_window_errors = 0;
2808
2809 ns->tx_errors = stats.tx_error_frames;
2810 ns->rx_errors = stats.rx_symbol_err + stats.rx_fcs_err +
2811 ns->rx_length_errors + stats.rx_len_err + ns->rx_fifo_errors;
2812 return ns;
2813}
2814
2815static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2816{
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002817 unsigned int mbox;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002818 int ret = 0, prtad, devad;
2819 struct port_info *pi = netdev_priv(dev);
2820 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&req->ifr_data;
2821
2822 switch (cmd) {
2823 case SIOCGMIIPHY:
2824 if (pi->mdio_addr < 0)
2825 return -EOPNOTSUPP;
2826 data->phy_id = pi->mdio_addr;
2827 break;
2828 case SIOCGMIIREG:
2829 case SIOCSMIIREG:
2830 if (mdio_phy_id_is_c45(data->phy_id)) {
2831 prtad = mdio_phy_id_prtad(data->phy_id);
2832 devad = mdio_phy_id_devad(data->phy_id);
2833 } else if (data->phy_id < 32) {
2834 prtad = data->phy_id;
2835 devad = 0;
2836 data->reg_num &= 0x1f;
2837 } else
2838 return -EINVAL;
2839
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002840 mbox = pi->adapter->fn;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002841 if (cmd == SIOCGMIIREG)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002842 ret = t4_mdio_rd(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002843 data->reg_num, &data->val_out);
2844 else
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002845 ret = t4_mdio_wr(pi->adapter, mbox, prtad, devad,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002846 data->reg_num, data->val_in);
2847 break;
2848 default:
2849 return -EOPNOTSUPP;
2850 }
2851 return ret;
2852}
2853
2854static void cxgb_set_rxmode(struct net_device *dev)
2855{
2856 /* unfortunately we can't return errors to the stack */
2857 set_rxmode(dev, -1, false);
2858}
2859
2860static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2861{
2862 int ret;
2863 struct port_info *pi = netdev_priv(dev);
2864
2865 if (new_mtu < 81 || new_mtu > MAX_MTU) /* accommodate SACK */
2866 return -EINVAL;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002867 ret = t4_set_rxmode(pi->adapter, pi->adapter->fn, pi->viid, new_mtu, -1,
2868 -1, -1, -1, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002869 if (!ret)
2870 dev->mtu = new_mtu;
2871 return ret;
2872}
2873
2874static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2875{
2876 int ret;
2877 struct sockaddr *addr = p;
2878 struct port_info *pi = netdev_priv(dev);
2879
2880 if (!is_valid_ether_addr(addr->sa_data))
2881 return -EINVAL;
2882
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002883 ret = t4_change_mac(pi->adapter, pi->adapter->fn, pi->viid,
2884 pi->xact_addr_filt, addr->sa_data, true, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002885 if (ret < 0)
2886 return ret;
2887
2888 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2889 pi->xact_addr_filt = ret;
2890 return 0;
2891}
2892
2893static void vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
2894{
2895 struct port_info *pi = netdev_priv(dev);
2896
2897 pi->vlan_grp = grp;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002898 t4_set_rxmode(pi->adapter, pi->adapter->fn, pi->viid, -1, -1, -1, -1,
2899 grp != NULL, true);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002900}
2901
2902#ifdef CONFIG_NET_POLL_CONTROLLER
2903static void cxgb_netpoll(struct net_device *dev)
2904{
2905 struct port_info *pi = netdev_priv(dev);
2906 struct adapter *adap = pi->adapter;
2907
2908 if (adap->flags & USING_MSIX) {
2909 int i;
2910 struct sge_eth_rxq *rx = &adap->sge.ethrxq[pi->first_qset];
2911
2912 for (i = pi->nqsets; i; i--, rx++)
2913 t4_sge_intr_msix(0, &rx->rspq);
2914 } else
2915 t4_intr_handler(adap)(0, adap);
2916}
2917#endif
2918
2919static const struct net_device_ops cxgb4_netdev_ops = {
2920 .ndo_open = cxgb_open,
2921 .ndo_stop = cxgb_close,
2922 .ndo_start_xmit = t4_eth_xmit,
Dimitris Michailidis9be793b2010-06-18 10:05:31 +00002923 .ndo_get_stats64 = cxgb_get_stats,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002924 .ndo_set_rx_mode = cxgb_set_rxmode,
2925 .ndo_set_mac_address = cxgb_set_mac_addr,
2926 .ndo_validate_addr = eth_validate_addr,
2927 .ndo_do_ioctl = cxgb_ioctl,
2928 .ndo_change_mtu = cxgb_change_mtu,
2929 .ndo_vlan_rx_register = vlan_rx_register,
2930#ifdef CONFIG_NET_POLL_CONTROLLER
2931 .ndo_poll_controller = cxgb_netpoll,
2932#endif
2933};
2934
2935void t4_fatal_err(struct adapter *adap)
2936{
2937 t4_set_reg_field(adap, SGE_CONTROL, GLOBALENABLE, 0);
2938 t4_intr_disable(adap);
2939 dev_alert(adap->pdev_dev, "encountered fatal error, adapter stopped\n");
2940}
2941
2942static void setup_memwin(struct adapter *adap)
2943{
2944 u32 bar0;
2945
2946 bar0 = pci_resource_start(adap->pdev, 0); /* truncation intentional */
2947 t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 0),
2948 (bar0 + MEMWIN0_BASE) | BIR(0) |
2949 WINDOW(ilog2(MEMWIN0_APERTURE) - 10));
2950 t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 1),
2951 (bar0 + MEMWIN1_BASE) | BIR(0) |
2952 WINDOW(ilog2(MEMWIN1_APERTURE) - 10));
2953 t4_write_reg(adap, PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 2),
2954 (bar0 + MEMWIN2_BASE) | BIR(0) |
2955 WINDOW(ilog2(MEMWIN2_APERTURE) - 10));
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00002956 if (adap->vres.ocq.size) {
2957 unsigned int start, sz_kb;
2958
2959 start = pci_resource_start(adap->pdev, 2) +
2960 OCQ_WIN_OFFSET(adap->pdev, &adap->vres);
2961 sz_kb = roundup_pow_of_two(adap->vres.ocq.size) >> 10;
2962 t4_write_reg(adap,
2963 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_BASE_WIN, 3),
2964 start | BIR(1) | WINDOW(ilog2(sz_kb)));
2965 t4_write_reg(adap,
2966 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET, 3),
2967 adap->vres.ocq.start);
2968 t4_read_reg(adap,
2969 PCIE_MEM_ACCESS_REG(PCIE_MEM_ACCESS_OFFSET, 3));
2970 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00002971}
2972
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002973static int adap_init1(struct adapter *adap, struct fw_caps_config_cmd *c)
2974{
2975 u32 v;
2976 int ret;
2977
2978 /* get device capabilities */
2979 memset(c, 0, sizeof(*c));
2980 c->op_to_write = htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
2981 FW_CMD_REQUEST | FW_CMD_READ);
2982 c->retval_len16 = htonl(FW_LEN16(*c));
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002983 ret = t4_wr_mbox(adap, adap->fn, c, sizeof(*c), c);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00002984 if (ret < 0)
2985 return ret;
2986
2987 /* select capabilities we'll be using */
2988 if (c->niccaps & htons(FW_CAPS_CONFIG_NIC_VM)) {
2989 if (!vf_acls)
2990 c->niccaps ^= htons(FW_CAPS_CONFIG_NIC_VM);
2991 else
2992 c->niccaps = htons(FW_CAPS_CONFIG_NIC_VM);
2993 } else if (vf_acls) {
2994 dev_err(adap->pdev_dev, "virtualization ACLs not supported");
2995 return ret;
2996 }
2997 c->op_to_write = htonl(FW_CMD_OP(FW_CAPS_CONFIG_CMD) |
2998 FW_CMD_REQUEST | FW_CMD_WRITE);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00002999 ret = t4_wr_mbox(adap, adap->fn, c, sizeof(*c), NULL);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003000 if (ret < 0)
3001 return ret;
3002
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003003 ret = t4_config_glbl_rss(adap, adap->fn,
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003004 FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL,
3005 FW_RSS_GLB_CONFIG_CMD_TNLMAPEN |
3006 FW_RSS_GLB_CONFIG_CMD_TNLALLLKP);
3007 if (ret < 0)
3008 return ret;
3009
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003010 ret = t4_cfg_pfvf(adap, adap->fn, adap->fn, 0, MAX_EGRQ, 64, MAX_INGQ,
3011 0, 0, 4, 0xf, 0xf, 16, FW_CMD_CAP_PF, FW_CMD_CAP_PF);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003012 if (ret < 0)
3013 return ret;
3014
3015 t4_sge_init(adap);
3016
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003017 /* tweak some settings */
3018 t4_write_reg(adap, TP_SHIFT_CNT, 0x64f8849);
3019 t4_write_reg(adap, ULP_RX_TDDP_PSZ, HPZ0(PAGE_SHIFT - 12));
3020 t4_write_reg(adap, TP_PIO_ADDR, TP_INGRESS_CONFIG);
3021 v = t4_read_reg(adap, TP_PIO_DATA);
3022 t4_write_reg(adap, TP_PIO_DATA, v & ~CSUM_HAS_PSEUDO_HDR);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003023
3024 /* get basic stuff going */
3025 return t4_early_init(adap, adap->fn);
Dimitris Michailidis02b5fb82010-06-18 10:05:28 +00003026}
3027
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003028/*
3029 * Max # of ATIDs. The absolute HW max is 16K but we keep it lower.
3030 */
3031#define MAX_ATIDS 8192U
3032
3033/*
3034 * Phase 0 of initialization: contact FW, obtain config, perform basic init.
3035 */
3036static int adap_init0(struct adapter *adap)
3037{
3038 int ret;
3039 u32 v, port_vec;
3040 enum dev_state state;
3041 u32 params[7], val[7];
3042 struct fw_caps_config_cmd c;
3043
3044 ret = t4_check_fw_version(adap);
3045 if (ret == -EINVAL || ret > 0) {
3046 if (upgrade_fw(adap) >= 0) /* recache FW version */
3047 ret = t4_check_fw_version(adap);
3048 }
3049 if (ret < 0)
3050 return ret;
3051
3052 /* contact FW, request master */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003053 ret = t4_fw_hello(adap, adap->fn, adap->fn, MASTER_MUST, &state);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003054 if (ret < 0) {
3055 dev_err(adap->pdev_dev, "could not connect to FW, error %d\n",
3056 ret);
3057 return ret;
3058 }
3059
3060 /* reset device */
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003061 ret = t4_fw_reset(adap, adap->fn, PIORSTMODE | PIORST);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003062 if (ret < 0)
3063 goto bye;
3064
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003065 for (v = 0; v < SGE_NTIMERS - 1; v++)
3066 adap->sge.timer_val[v] = min(intr_holdoff[v], MAX_SGE_TIMERVAL);
3067 adap->sge.timer_val[SGE_NTIMERS - 1] = MAX_SGE_TIMERVAL;
3068 adap->sge.counter_val[0] = 1;
3069 for (v = 1; v < SGE_NCOUNTERS; v++)
3070 adap->sge.counter_val[v] = min(intr_cnt[v - 1],
3071 THRESHOLD_3_MASK);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003072#define FW_PARAM_DEV(param) \
3073 (FW_PARAMS_MNEM(FW_PARAMS_MNEM_DEV) | \
3074 FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_DEV_##param))
3075
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003076 params[0] = FW_PARAM_DEV(CCLK);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003077 ret = t4_query_params(adap, adap->fn, adap->fn, 0, 1, params, val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003078 if (ret < 0)
3079 goto bye;
3080 adap->params.vpd.cclk = val[0];
3081
3082 ret = adap_init1(adap, &c);
3083 if (ret < 0)
3084 goto bye;
3085
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003086#define FW_PARAM_PFVF(param) \
3087 (FW_PARAMS_MNEM(FW_PARAMS_MNEM_PFVF) | \
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003088 FW_PARAMS_PARAM_X(FW_PARAMS_PARAM_PFVF_##param) | \
3089 FW_PARAMS_PARAM_Y(adap->fn))
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003090
3091 params[0] = FW_PARAM_DEV(PORTVEC);
3092 params[1] = FW_PARAM_PFVF(L2T_START);
3093 params[2] = FW_PARAM_PFVF(L2T_END);
3094 params[3] = FW_PARAM_PFVF(FILTER_START);
3095 params[4] = FW_PARAM_PFVF(FILTER_END);
Dimitris Michailidise46dab42010-08-23 17:20:58 +00003096 params[5] = FW_PARAM_PFVF(IQFLINT_START);
3097 params[6] = FW_PARAM_PFVF(EQ_START);
3098 ret = t4_query_params(adap, adap->fn, adap->fn, 0, 7, params, val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003099 if (ret < 0)
3100 goto bye;
3101 port_vec = val[0];
3102 adap->tids.ftid_base = val[3];
3103 adap->tids.nftids = val[4] - val[3] + 1;
Dimitris Michailidise46dab42010-08-23 17:20:58 +00003104 adap->sge.ingr_start = val[5];
3105 adap->sge.egr_start = val[6];
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003106
3107 if (c.ofldcaps) {
3108 /* query offload-related parameters */
3109 params[0] = FW_PARAM_DEV(NTID);
3110 params[1] = FW_PARAM_PFVF(SERVER_START);
3111 params[2] = FW_PARAM_PFVF(SERVER_END);
3112 params[3] = FW_PARAM_PFVF(TDDP_START);
3113 params[4] = FW_PARAM_PFVF(TDDP_END);
3114 params[5] = FW_PARAM_DEV(FLOWC_BUFFIFO_SZ);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003115 ret = t4_query_params(adap, adap->fn, adap->fn, 0, 6, params,
3116 val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003117 if (ret < 0)
3118 goto bye;
3119 adap->tids.ntids = val[0];
3120 adap->tids.natids = min(adap->tids.ntids / 2, MAX_ATIDS);
3121 adap->tids.stid_base = val[1];
3122 adap->tids.nstids = val[2] - val[1] + 1;
3123 adap->vres.ddp.start = val[3];
3124 adap->vres.ddp.size = val[4] - val[3] + 1;
3125 adap->params.ofldq_wr_cred = val[5];
3126 adap->params.offload = 1;
3127 }
3128 if (c.rdmacaps) {
3129 params[0] = FW_PARAM_PFVF(STAG_START);
3130 params[1] = FW_PARAM_PFVF(STAG_END);
3131 params[2] = FW_PARAM_PFVF(RQ_START);
3132 params[3] = FW_PARAM_PFVF(RQ_END);
3133 params[4] = FW_PARAM_PFVF(PBL_START);
3134 params[5] = FW_PARAM_PFVF(PBL_END);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003135 ret = t4_query_params(adap, adap->fn, adap->fn, 0, 6, params,
3136 val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003137 if (ret < 0)
3138 goto bye;
3139 adap->vres.stag.start = val[0];
3140 adap->vres.stag.size = val[1] - val[0] + 1;
3141 adap->vres.rq.start = val[2];
3142 adap->vres.rq.size = val[3] - val[2] + 1;
3143 adap->vres.pbl.start = val[4];
3144 adap->vres.pbl.size = val[5] - val[4] + 1;
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003145
3146 params[0] = FW_PARAM_PFVF(SQRQ_START);
3147 params[1] = FW_PARAM_PFVF(SQRQ_END);
3148 params[2] = FW_PARAM_PFVF(CQ_START);
3149 params[3] = FW_PARAM_PFVF(CQ_END);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003150 params[4] = FW_PARAM_PFVF(OCQ_START);
3151 params[5] = FW_PARAM_PFVF(OCQ_END);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003152 ret = t4_query_params(adap, adap->fn, adap->fn, 0, 6, params,
3153 val);
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003154 if (ret < 0)
3155 goto bye;
3156 adap->vres.qp.start = val[0];
3157 adap->vres.qp.size = val[1] - val[0] + 1;
3158 adap->vres.cq.start = val[2];
3159 adap->vres.cq.size = val[3] - val[2] + 1;
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003160 adap->vres.ocq.start = val[4];
3161 adap->vres.ocq.size = val[5] - val[4] + 1;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003162 }
3163 if (c.iscsicaps) {
3164 params[0] = FW_PARAM_PFVF(ISCSI_START);
3165 params[1] = FW_PARAM_PFVF(ISCSI_END);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003166 ret = t4_query_params(adap, adap->fn, adap->fn, 0, 2, params,
3167 val);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003168 if (ret < 0)
3169 goto bye;
3170 adap->vres.iscsi.start = val[0];
3171 adap->vres.iscsi.size = val[1] - val[0] + 1;
3172 }
3173#undef FW_PARAM_PFVF
3174#undef FW_PARAM_DEV
3175
3176 adap->params.nports = hweight32(port_vec);
3177 adap->params.portvec = port_vec;
3178 adap->flags |= FW_OK;
3179
3180 /* These are finalized by FW initialization, load their values now */
3181 v = t4_read_reg(adap, TP_TIMER_RESOLUTION);
3182 adap->params.tp.tre = TIMERRESOLUTION_GET(v);
3183 t4_read_mtu_tbl(adap, adap->params.mtus, NULL);
3184 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3185 adap->params.b_wnd);
Casey Leedom7ee9ff92010-06-25 12:11:46 +00003186
3187#ifdef CONFIG_PCI_IOV
3188 /*
3189 * Provision resource limits for Virtual Functions. We currently
3190 * grant them all the same static resource limits except for the Port
3191 * Access Rights Mask which we're assigning based on the PF. All of
3192 * the static provisioning stuff for both the PF and VF really needs
3193 * to be managed in a persistent manner for each device which the
3194 * firmware controls.
3195 */
3196 {
3197 int pf, vf;
3198
3199 for (pf = 0; pf < ARRAY_SIZE(num_vf); pf++) {
3200 if (num_vf[pf] <= 0)
3201 continue;
3202
3203 /* VF numbering starts at 1! */
3204 for (vf = 1; vf <= num_vf[pf]; vf++) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003205 ret = t4_cfg_pfvf(adap, adap->fn, pf, vf,
Casey Leedom7ee9ff92010-06-25 12:11:46 +00003206 VFRES_NEQ, VFRES_NETHCTRL,
3207 VFRES_NIQFLINT, VFRES_NIQ,
3208 VFRES_TC, VFRES_NVI,
3209 FW_PFVF_CMD_CMASK_MASK,
3210 pfvfres_pmask(adap, pf, vf),
3211 VFRES_NEXACTF,
3212 VFRES_R_CAPS, VFRES_WX_CAPS);
3213 if (ret < 0)
3214 dev_warn(adap->pdev_dev, "failed to "
3215 "provision pf/vf=%d/%d; "
3216 "err=%d\n", pf, vf, ret);
3217 }
3218 }
3219 }
3220#endif
3221
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003222 setup_memwin(adap);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003223 return 0;
3224
3225 /*
3226 * If a command timed out or failed with EIO FW does not operate within
3227 * its spec or something catastrophic happened to HW/FW, stop issuing
3228 * commands.
3229 */
3230bye: if (ret != -ETIMEDOUT && ret != -EIO)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003231 t4_fw_bye(adap, adap->fn);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003232 return ret;
3233}
3234
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003235/* EEH callbacks */
3236
3237static pci_ers_result_t eeh_err_detected(struct pci_dev *pdev,
3238 pci_channel_state_t state)
3239{
3240 int i;
3241 struct adapter *adap = pci_get_drvdata(pdev);
3242
3243 if (!adap)
3244 goto out;
3245
3246 rtnl_lock();
3247 adap->flags &= ~FW_OK;
3248 notify_ulds(adap, CXGB4_STATE_START_RECOVERY);
3249 for_each_port(adap, i) {
3250 struct net_device *dev = adap->port[i];
3251
3252 netif_device_detach(dev);
3253 netif_carrier_off(dev);
3254 }
3255 if (adap->flags & FULL_INIT_DONE)
3256 cxgb_down(adap);
3257 rtnl_unlock();
3258 pci_disable_device(pdev);
3259out: return state == pci_channel_io_perm_failure ?
3260 PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
3261}
3262
3263static pci_ers_result_t eeh_slot_reset(struct pci_dev *pdev)
3264{
3265 int i, ret;
3266 struct fw_caps_config_cmd c;
3267 struct adapter *adap = pci_get_drvdata(pdev);
3268
3269 if (!adap) {
3270 pci_restore_state(pdev);
3271 pci_save_state(pdev);
3272 return PCI_ERS_RESULT_RECOVERED;
3273 }
3274
3275 if (pci_enable_device(pdev)) {
3276 dev_err(&pdev->dev, "cannot reenable PCI device after reset\n");
3277 return PCI_ERS_RESULT_DISCONNECT;
3278 }
3279
3280 pci_set_master(pdev);
3281 pci_restore_state(pdev);
3282 pci_save_state(pdev);
3283 pci_cleanup_aer_uncorrect_error_status(pdev);
3284
3285 if (t4_wait_dev_ready(adap) < 0)
3286 return PCI_ERS_RESULT_DISCONNECT;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003287 if (t4_fw_hello(adap, adap->fn, adap->fn, MASTER_MUST, NULL))
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003288 return PCI_ERS_RESULT_DISCONNECT;
3289 adap->flags |= FW_OK;
3290 if (adap_init1(adap, &c))
3291 return PCI_ERS_RESULT_DISCONNECT;
3292
3293 for_each_port(adap, i) {
3294 struct port_info *p = adap2pinfo(adap, i);
3295
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003296 ret = t4_alloc_vi(adap, adap->fn, p->tx_chan, adap->fn, 0, 1,
3297 NULL, NULL);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003298 if (ret < 0)
3299 return PCI_ERS_RESULT_DISCONNECT;
3300 p->viid = ret;
3301 p->xact_addr_filt = -1;
3302 }
3303
3304 t4_load_mtus(adap, adap->params.mtus, adap->params.a_wnd,
3305 adap->params.b_wnd);
Dimitris Michailidis1ae970e2010-08-02 13:19:19 +00003306 setup_memwin(adap);
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003307 if (cxgb_up(adap))
3308 return PCI_ERS_RESULT_DISCONNECT;
3309 return PCI_ERS_RESULT_RECOVERED;
3310}
3311
3312static void eeh_resume(struct pci_dev *pdev)
3313{
3314 int i;
3315 struct adapter *adap = pci_get_drvdata(pdev);
3316
3317 if (!adap)
3318 return;
3319
3320 rtnl_lock();
3321 for_each_port(adap, i) {
3322 struct net_device *dev = adap->port[i];
3323
3324 if (netif_running(dev)) {
3325 link_start(dev);
3326 cxgb_set_rxmode(dev);
3327 }
3328 netif_device_attach(dev);
3329 }
3330 rtnl_unlock();
3331}
3332
3333static struct pci_error_handlers cxgb4_eeh = {
3334 .error_detected = eeh_err_detected,
3335 .slot_reset = eeh_slot_reset,
3336 .resume = eeh_resume,
3337};
3338
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003339static inline bool is_10g_port(const struct link_config *lc)
3340{
3341 return (lc->supported & FW_PORT_CAP_SPEED_10G) != 0;
3342}
3343
3344static inline void init_rspq(struct sge_rspq *q, u8 timer_idx, u8 pkt_cnt_idx,
3345 unsigned int size, unsigned int iqe_size)
3346{
3347 q->intr_params = QINTR_TIMER_IDX(timer_idx) |
3348 (pkt_cnt_idx < SGE_NCOUNTERS ? QINTR_CNT_EN : 0);
3349 q->pktcnt_idx = pkt_cnt_idx < SGE_NCOUNTERS ? pkt_cnt_idx : 0;
3350 q->iqe_len = iqe_size;
3351 q->size = size;
3352}
3353
3354/*
3355 * Perform default configuration of DMA queues depending on the number and type
3356 * of ports we found and the number of available CPUs. Most settings can be
3357 * modified by the admin prior to actual use.
3358 */
3359static void __devinit cfg_queues(struct adapter *adap)
3360{
3361 struct sge *s = &adap->sge;
3362 int i, q10g = 0, n10g = 0, qidx = 0;
3363
3364 for_each_port(adap, i)
3365 n10g += is_10g_port(&adap2pinfo(adap, i)->link_cfg);
3366
3367 /*
3368 * We default to 1 queue per non-10G port and up to # of cores queues
3369 * per 10G port.
3370 */
3371 if (n10g)
3372 q10g = (MAX_ETH_QSETS - (adap->params.nports - n10g)) / n10g;
3373 if (q10g > num_online_cpus())
3374 q10g = num_online_cpus();
3375
3376 for_each_port(adap, i) {
3377 struct port_info *pi = adap2pinfo(adap, i);
3378
3379 pi->first_qset = qidx;
3380 pi->nqsets = is_10g_port(&pi->link_cfg) ? q10g : 1;
3381 qidx += pi->nqsets;
3382 }
3383
3384 s->ethqsets = qidx;
3385 s->max_ethqsets = qidx; /* MSI-X may lower it later */
3386
3387 if (is_offload(adap)) {
3388 /*
3389 * For offload we use 1 queue/channel if all ports are up to 1G,
3390 * otherwise we divide all available queues amongst the channels
3391 * capped by the number of available cores.
3392 */
3393 if (n10g) {
3394 i = min_t(int, ARRAY_SIZE(s->ofldrxq),
3395 num_online_cpus());
3396 s->ofldqsets = roundup(i, adap->params.nports);
3397 } else
3398 s->ofldqsets = adap->params.nports;
3399 /* For RDMA one Rx queue per channel suffices */
3400 s->rdmaqs = adap->params.nports;
3401 }
3402
3403 for (i = 0; i < ARRAY_SIZE(s->ethrxq); i++) {
3404 struct sge_eth_rxq *r = &s->ethrxq[i];
3405
3406 init_rspq(&r->rspq, 0, 0, 1024, 64);
3407 r->fl.size = 72;
3408 }
3409
3410 for (i = 0; i < ARRAY_SIZE(s->ethtxq); i++)
3411 s->ethtxq[i].q.size = 1024;
3412
3413 for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++)
3414 s->ctrlq[i].q.size = 512;
3415
3416 for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++)
3417 s->ofldtxq[i].q.size = 1024;
3418
3419 for (i = 0; i < ARRAY_SIZE(s->ofldrxq); i++) {
3420 struct sge_ofld_rxq *r = &s->ofldrxq[i];
3421
3422 init_rspq(&r->rspq, 0, 0, 1024, 64);
3423 r->rspq.uld = CXGB4_ULD_ISCSI;
3424 r->fl.size = 72;
3425 }
3426
3427 for (i = 0; i < ARRAY_SIZE(s->rdmarxq); i++) {
3428 struct sge_ofld_rxq *r = &s->rdmarxq[i];
3429
3430 init_rspq(&r->rspq, 0, 0, 511, 64);
3431 r->rspq.uld = CXGB4_ULD_RDMA;
3432 r->fl.size = 72;
3433 }
3434
3435 init_rspq(&s->fw_evtq, 6, 0, 512, 64);
3436 init_rspq(&s->intrq, 6, 0, 2 * MAX_INGQ, 64);
3437}
3438
3439/*
3440 * Reduce the number of Ethernet queues across all ports to at most n.
3441 * n provides at least one queue per port.
3442 */
3443static void __devinit reduce_ethqs(struct adapter *adap, int n)
3444{
3445 int i;
3446 struct port_info *pi;
3447
3448 while (n < adap->sge.ethqsets)
3449 for_each_port(adap, i) {
3450 pi = adap2pinfo(adap, i);
3451 if (pi->nqsets > 1) {
3452 pi->nqsets--;
3453 adap->sge.ethqsets--;
3454 if (adap->sge.ethqsets <= n)
3455 break;
3456 }
3457 }
3458
3459 n = 0;
3460 for_each_port(adap, i) {
3461 pi = adap2pinfo(adap, i);
3462 pi->first_qset = n;
3463 n += pi->nqsets;
3464 }
3465}
3466
3467/* 2 MSI-X vectors needed for the FW queue and non-data interrupts */
3468#define EXTRA_VECS 2
3469
3470static int __devinit enable_msix(struct adapter *adap)
3471{
3472 int ofld_need = 0;
3473 int i, err, want, need;
3474 struct sge *s = &adap->sge;
3475 unsigned int nchan = adap->params.nports;
3476 struct msix_entry entries[MAX_INGQ + 1];
3477
3478 for (i = 0; i < ARRAY_SIZE(entries); ++i)
3479 entries[i].entry = i;
3480
3481 want = s->max_ethqsets + EXTRA_VECS;
3482 if (is_offload(adap)) {
3483 want += s->rdmaqs + s->ofldqsets;
3484 /* need nchan for each possible ULD */
3485 ofld_need = 2 * nchan;
3486 }
3487 need = adap->params.nports + EXTRA_VECS + ofld_need;
3488
3489 while ((err = pci_enable_msix(adap->pdev, entries, want)) >= need)
3490 want = err;
3491
3492 if (!err) {
3493 /*
3494 * Distribute available vectors to the various queue groups.
3495 * Every group gets its minimum requirement and NIC gets top
3496 * priority for leftovers.
3497 */
3498 i = want - EXTRA_VECS - ofld_need;
3499 if (i < s->max_ethqsets) {
3500 s->max_ethqsets = i;
3501 if (i < s->ethqsets)
3502 reduce_ethqs(adap, i);
3503 }
3504 if (is_offload(adap)) {
3505 i = want - EXTRA_VECS - s->max_ethqsets;
3506 i -= ofld_need - nchan;
3507 s->ofldqsets = (i / nchan) * nchan; /* round down */
3508 }
3509 for (i = 0; i < want; ++i)
3510 adap->msix_info[i].vec = entries[i].vector;
3511 } else if (err > 0)
3512 dev_info(adap->pdev_dev,
3513 "only %d MSI-X vectors left, not using MSI-X\n", err);
3514 return err;
3515}
3516
3517#undef EXTRA_VECS
3518
Dimitris Michailidis671b0062010-07-11 12:01:17 +00003519static int __devinit init_rss(struct adapter *adap)
3520{
3521 unsigned int i, j;
3522
3523 for_each_port(adap, i) {
3524 struct port_info *pi = adap2pinfo(adap, i);
3525
3526 pi->rss = kcalloc(pi->rss_size, sizeof(u16), GFP_KERNEL);
3527 if (!pi->rss)
3528 return -ENOMEM;
3529 for (j = 0; j < pi->rss_size; j++)
3530 pi->rss[j] = j % pi->nqsets;
3531 }
3532 return 0;
3533}
3534
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003535static void __devinit print_port_info(struct adapter *adap)
3536{
3537 static const char *base[] = {
Dimitris Michailidisa0881ca2010-06-18 10:05:34 +00003538 "R XFI", "R XAUI", "T SGMII", "T XFI", "T XAUI", "KX4", "CX4",
3539 "KX", "KR", "KR SFP+", "KR FEC"
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003540 };
3541
3542 int i;
3543 char buf[80];
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00003544 const char *spd = "";
3545
3546 if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_2_5GB)
3547 spd = " 2.5 GT/s";
3548 else if (adap->params.pci.speed == PCI_EXP_LNKSTA_CLS_5_0GB)
3549 spd = " 5 GT/s";
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003550
3551 for_each_port(adap, i) {
3552 struct net_device *dev = adap->port[i];
3553 const struct port_info *pi = netdev_priv(dev);
3554 char *bufp = buf;
3555
3556 if (!test_bit(i, &adap->registered_device_map))
3557 continue;
3558
3559 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_100M)
3560 bufp += sprintf(bufp, "100/");
3561 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_1G)
3562 bufp += sprintf(bufp, "1000/");
3563 if (pi->link_cfg.supported & FW_PORT_CAP_SPEED_10G)
3564 bufp += sprintf(bufp, "10G/");
3565 if (bufp != buf)
3566 --bufp;
3567 sprintf(bufp, "BASE-%s", base[pi->port_type]);
3568
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00003569 netdev_info(dev, "Chelsio %s rev %d %s %sNIC PCIe x%d%s%s\n",
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003570 adap->params.vpd.id, adap->params.rev,
3571 buf, is_offload(adap) ? "R" : "",
Dimitris Michailidisf1a051b2010-05-10 15:58:08 +00003572 adap->params.pci.width, spd,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003573 (adap->flags & USING_MSIX) ? " MSI-X" :
3574 (adap->flags & USING_MSI) ? " MSI" : "");
3575 if (adap->name == dev->name)
3576 netdev_info(dev, "S/N: %s, E/C: %s\n",
3577 adap->params.vpd.sn, adap->params.vpd.ec);
3578 }
3579}
3580
Dimitris Michailidis06546392010-07-11 12:01:16 +00003581/*
3582 * Free the following resources:
3583 * - memory used for tables
3584 * - MSI/MSI-X
3585 * - net devices
3586 * - resources FW is holding for us
3587 */
3588static void free_some_resources(struct adapter *adapter)
3589{
3590 unsigned int i;
3591
3592 t4_free_mem(adapter->l2t);
3593 t4_free_mem(adapter->tids.tid_tab);
3594 disable_msi(adapter);
3595
3596 for_each_port(adapter, i)
Dimitris Michailidis671b0062010-07-11 12:01:17 +00003597 if (adapter->port[i]) {
3598 kfree(adap2pinfo(adapter, i)->rss);
Dimitris Michailidis06546392010-07-11 12:01:16 +00003599 free_netdev(adapter->port[i]);
Dimitris Michailidis671b0062010-07-11 12:01:17 +00003600 }
Dimitris Michailidis06546392010-07-11 12:01:16 +00003601 if (adapter->flags & FW_OK)
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003602 t4_fw_bye(adapter, adapter->fn);
Dimitris Michailidis06546392010-07-11 12:01:16 +00003603}
3604
Dimitris Michailidis35d35682010-08-02 13:19:20 +00003605#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003606 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
3607
3608static int __devinit init_one(struct pci_dev *pdev,
3609 const struct pci_device_id *ent)
3610{
3611 int func, i, err;
3612 struct port_info *pi;
3613 unsigned int highdma = 0;
3614 struct adapter *adapter = NULL;
3615
3616 printk_once(KERN_INFO "%s - version %s\n", DRV_DESC, DRV_VERSION);
3617
3618 err = pci_request_regions(pdev, KBUILD_MODNAME);
3619 if (err) {
3620 /* Just info, some other driver may have claimed the device. */
3621 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
3622 return err;
3623 }
3624
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003625 /* We control everything through one PF */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003626 func = PCI_FUNC(pdev->devfn);
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003627 if (func != ent->driver_data) {
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003628 pci_save_state(pdev); /* to restore SR-IOV later */
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003629 goto sriov;
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003630 }
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003631
3632 err = pci_enable_device(pdev);
3633 if (err) {
3634 dev_err(&pdev->dev, "cannot enable PCI device\n");
3635 goto out_release_regions;
3636 }
3637
3638 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3639 highdma = NETIF_F_HIGHDMA;
3640 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3641 if (err) {
3642 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
3643 "coherent allocations\n");
3644 goto out_disable_device;
3645 }
3646 } else {
3647 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3648 if (err) {
3649 dev_err(&pdev->dev, "no usable DMA configuration\n");
3650 goto out_disable_device;
3651 }
3652 }
3653
3654 pci_enable_pcie_error_reporting(pdev);
3655 pci_set_master(pdev);
3656 pci_save_state(pdev);
3657
3658 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3659 if (!adapter) {
3660 err = -ENOMEM;
3661 goto out_disable_device;
3662 }
3663
3664 adapter->regs = pci_ioremap_bar(pdev, 0);
3665 if (!adapter->regs) {
3666 dev_err(&pdev->dev, "cannot map device registers\n");
3667 err = -ENOMEM;
3668 goto out_free_adapter;
3669 }
3670
3671 adapter->pdev = pdev;
3672 adapter->pdev_dev = &pdev->dev;
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003673 adapter->fn = func;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003674 adapter->name = pci_name(pdev);
3675 adapter->msg_enable = dflt_msg_enable;
3676 memset(adapter->chan_map, 0xff, sizeof(adapter->chan_map));
3677
3678 spin_lock_init(&adapter->stats_lock);
3679 spin_lock_init(&adapter->tid_release_lock);
3680
3681 INIT_WORK(&adapter->tid_release_task, process_tid_release_list);
3682
3683 err = t4_prep_adapter(adapter);
3684 if (err)
3685 goto out_unmap_bar;
3686 err = adap_init0(adapter);
3687 if (err)
3688 goto out_unmap_bar;
3689
3690 for_each_port(adapter, i) {
3691 struct net_device *netdev;
3692
3693 netdev = alloc_etherdev_mq(sizeof(struct port_info),
3694 MAX_ETH_QSETS);
3695 if (!netdev) {
3696 err = -ENOMEM;
3697 goto out_free_dev;
3698 }
3699
3700 SET_NETDEV_DEV(netdev, &pdev->dev);
3701
3702 adapter->port[i] = netdev;
3703 pi = netdev_priv(netdev);
3704 pi->adapter = adapter;
3705 pi->xact_addr_filt = -1;
3706 pi->rx_offload = RX_CSO;
3707 pi->port_id = i;
3708 netif_carrier_off(netdev);
3709 netif_tx_stop_all_queues(netdev);
3710 netdev->irq = pdev->irq;
3711
Dimitris Michailidis35d35682010-08-02 13:19:20 +00003712 netdev->features |= NETIF_F_SG | TSO_FLAGS;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003713 netdev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Dimitris Michailidis87b6cf52010-04-27 16:22:42 -07003714 netdev->features |= NETIF_F_GRO | NETIF_F_RXHASH | highdma;
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003715 netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3716 netdev->vlan_features = netdev->features & VLAN_FEAT;
3717
3718 netdev->netdev_ops = &cxgb4_netdev_ops;
3719 SET_ETHTOOL_OPS(netdev, &cxgb_ethtool_ops);
3720 }
3721
3722 pci_set_drvdata(pdev, adapter);
3723
3724 if (adapter->flags & FW_OK) {
Dimitris Michailidis060e0c72010-08-02 13:19:21 +00003725 err = t4_port_init(adapter, func, func, 0);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003726 if (err)
3727 goto out_free_dev;
3728 }
3729
3730 /*
3731 * Configure queues and allocate tables now, they can be needed as
3732 * soon as the first register_netdev completes.
3733 */
3734 cfg_queues(adapter);
3735
3736 adapter->l2t = t4_init_l2t();
3737 if (!adapter->l2t) {
3738 /* We tolerate a lack of L2T, giving up some functionality */
3739 dev_warn(&pdev->dev, "could not allocate L2T, continuing\n");
3740 adapter->params.offload = 0;
3741 }
3742
3743 if (is_offload(adapter) && tid_init(&adapter->tids) < 0) {
3744 dev_warn(&pdev->dev, "could not allocate TID table, "
3745 "continuing\n");
3746 adapter->params.offload = 0;
3747 }
3748
Dimitris Michailidisf7cabcd2010-07-11 12:01:15 +00003749 /* See what interrupts we'll be using */
3750 if (msi > 1 && enable_msix(adapter) == 0)
3751 adapter->flags |= USING_MSIX;
3752 else if (msi > 0 && pci_enable_msi(pdev) == 0)
3753 adapter->flags |= USING_MSI;
3754
Dimitris Michailidis671b0062010-07-11 12:01:17 +00003755 err = init_rss(adapter);
3756 if (err)
3757 goto out_free_dev;
3758
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003759 /*
3760 * The card is now ready to go. If any errors occur during device
3761 * registration we do not fail the whole card but rather proceed only
3762 * with the ports we manage to register successfully. However we must
3763 * register at least one net device.
3764 */
3765 for_each_port(adapter, i) {
3766 err = register_netdev(adapter->port[i]);
3767 if (err)
3768 dev_warn(&pdev->dev,
3769 "cannot register net device %s, skipping\n",
3770 adapter->port[i]->name);
3771 else {
3772 /*
3773 * Change the name we use for messages to the name of
3774 * the first successfully registered interface.
3775 */
3776 if (!adapter->registered_device_map)
3777 adapter->name = adapter->port[i]->name;
3778
3779 __set_bit(i, &adapter->registered_device_map);
3780 adapter->chan_map[adap2pinfo(adapter, i)->tx_chan] = i;
3781 }
3782 }
3783 if (!adapter->registered_device_map) {
3784 dev_err(&pdev->dev, "could not register any net devices\n");
3785 goto out_free_dev;
3786 }
3787
3788 if (cxgb4_debugfs_root) {
3789 adapter->debugfs_root = debugfs_create_dir(pci_name(pdev),
3790 cxgb4_debugfs_root);
3791 setup_debugfs(adapter);
3792 }
3793
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003794 if (is_offload(adapter))
3795 attach_ulds(adapter);
3796
3797 print_port_info(adapter);
3798
3799sriov:
3800#ifdef CONFIG_PCI_IOV
3801 if (func < ARRAY_SIZE(num_vf) && num_vf[func] > 0)
3802 if (pci_enable_sriov(pdev, num_vf[func]) == 0)
3803 dev_info(&pdev->dev,
3804 "instantiated %u virtual functions\n",
3805 num_vf[func]);
3806#endif
3807 return 0;
3808
3809 out_free_dev:
Dimitris Michailidis06546392010-07-11 12:01:16 +00003810 free_some_resources(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003811 out_unmap_bar:
3812 iounmap(adapter->regs);
3813 out_free_adapter:
3814 kfree(adapter);
3815 out_disable_device:
3816 pci_disable_pcie_error_reporting(pdev);
3817 pci_disable_device(pdev);
3818 out_release_regions:
3819 pci_release_regions(pdev);
3820 pci_set_drvdata(pdev, NULL);
3821 return err;
3822}
3823
3824static void __devexit remove_one(struct pci_dev *pdev)
3825{
3826 struct adapter *adapter = pci_get_drvdata(pdev);
3827
3828 pci_disable_sriov(pdev);
3829
3830 if (adapter) {
3831 int i;
3832
3833 if (is_offload(adapter))
3834 detach_ulds(adapter);
3835
3836 for_each_port(adapter, i)
3837 if (test_bit(i, &adapter->registered_device_map))
3838 unregister_netdev(adapter->port[i]);
3839
3840 if (adapter->debugfs_root)
3841 debugfs_remove_recursive(adapter->debugfs_root);
3842
Dimitris Michailidisaaefae92010-05-18 10:07:12 +00003843 if (adapter->flags & FULL_INIT_DONE)
3844 cxgb_down(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003845
Dimitris Michailidis06546392010-07-11 12:01:16 +00003846 free_some_resources(adapter);
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003847 iounmap(adapter->regs);
3848 kfree(adapter);
3849 pci_disable_pcie_error_reporting(pdev);
3850 pci_disable_device(pdev);
3851 pci_release_regions(pdev);
3852 pci_set_drvdata(pdev, NULL);
3853 } else if (PCI_FUNC(pdev->devfn) > 0)
3854 pci_release_regions(pdev);
3855}
3856
3857static struct pci_driver cxgb4_driver = {
3858 .name = KBUILD_MODNAME,
3859 .id_table = cxgb4_pci_tbl,
3860 .probe = init_one,
3861 .remove = __devexit_p(remove_one),
Dimitris Michailidis204dc3c2010-06-18 10:05:29 +00003862 .err_handler = &cxgb4_eeh,
Dimitris Michailidisb8ff05a2010-04-01 15:28:26 +00003863};
3864
3865static int __init cxgb4_init_module(void)
3866{
3867 int ret;
3868
3869 /* Debugfs support is optional, just warn if this fails */
3870 cxgb4_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
3871 if (!cxgb4_debugfs_root)
3872 pr_warning("could not create debugfs entry, continuing\n");
3873
3874 ret = pci_register_driver(&cxgb4_driver);
3875 if (ret < 0)
3876 debugfs_remove(cxgb4_debugfs_root);
3877 return ret;
3878}
3879
3880static void __exit cxgb4_cleanup_module(void)
3881{
3882 pci_unregister_driver(&cxgb4_driver);
3883 debugfs_remove(cxgb4_debugfs_root); /* NULL ok */
3884}
3885
3886module_init(cxgb4_init_module);
3887module_exit(cxgb4_cleanup_module);