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