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