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