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