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