blob: 0bc6a4ffce30e07e31d98deae5efe4f778800739 [file] [log] [blame]
Divy Le Ray4d22de32007-01-18 22:04:14 -05001/*
Divy Le Raya02d44a2008-10-13 18:47:30 -07002 * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
Divy Le Ray4d22de32007-01-18 22:04:14 -05003 *
Divy Le Ray1d68e932007-01-30 19:44:35 -08004 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
Divy Le Ray4d22de32007-01-18 22:04:14 -05009 *
Divy Le Ray1d68e932007-01-30 19:44:35 -080010 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
Divy Le Ray4d22de32007-01-18 22:04:14 -050031 */
Joe Perches428ac432013-01-06 13:34:49 +000032
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Divy Le Ray4d22de32007-01-18 22:04:14 -050035#include <linux/module.h>
36#include <linux/moduleparam.h>
37#include <linux/init.h>
38#include <linux/pci.h>
39#include <linux/dma-mapping.h>
40#include <linux/netdevice.h>
41#include <linux/etherdevice.h>
42#include <linux/if_vlan.h>
Ben Hutchings0f07c4e2009-04-29 08:07:20 +000043#include <linux/mdio.h>
Divy Le Ray4d22de32007-01-18 22:04:14 -050044#include <linux/sockios.h>
45#include <linux/workqueue.h>
46#include <linux/proc_fs.h>
47#include <linux/rtnetlink.h>
Divy Le Ray2e283962007-03-18 13:10:06 -070048#include <linux/firmware.h>
vignesh babud9da4662007-07-09 11:50:22 -070049#include <linux/log2.h>
Ben Hutchings34336ec2009-11-07 11:53:52 +000050#include <linux/stringify.h>
Steve Wisee998f242010-01-27 17:03:34 +000051#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080053#include <linux/uaccess.h>
Divy Le Ray4d22de32007-01-18 22:04:14 -050054
55#include "common.h"
56#include "cxgb3_ioctl.h"
57#include "regs.h"
58#include "cxgb3_offload.h"
59#include "version.h"
60
61#include "cxgb3_ctl_defs.h"
62#include "t3_cpl.h"
63#include "firmware_exports.h"
64
65enum {
66 MAX_TXQ_ENTRIES = 16384,
67 MAX_CTRL_TXQ_ENTRIES = 1024,
68 MAX_RSPQ_ENTRIES = 16384,
69 MAX_RX_BUFFERS = 16384,
70 MAX_RX_JUMBO_BUFFERS = 16384,
71 MIN_TXQ_ENTRIES = 4,
72 MIN_CTRL_TXQ_ENTRIES = 4,
73 MIN_RSPQ_ENTRIES = 32,
74 MIN_FL_ENTRIES = 32
75};
76
77#define PORT_MASK ((1 << MAX_NPORTS) - 1)
78
79#define DFLT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
80 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP |\
81 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
82
83#define EEPROM_MAGIC 0x38E2F10C
84
Divy Le Ray678771d2007-11-16 14:26:44 -080085#define CH_DEVICE(devid, idx) \
86 { PCI_VENDOR_ID_CHELSIO, devid, PCI_ANY_ID, PCI_ANY_ID, 0, 0, idx }
Divy Le Ray4d22de32007-01-18 22:04:14 -050087
Benoit Taine9baa3c32014-08-08 15:56:03 +020088static const struct pci_device_id cxgb3_pci_tbl[] = {
Divy Le Ray678771d2007-11-16 14:26:44 -080089 CH_DEVICE(0x20, 0), /* PE9000 */
90 CH_DEVICE(0x21, 1), /* T302E */
91 CH_DEVICE(0x22, 2), /* T310E */
92 CH_DEVICE(0x23, 3), /* T320X */
93 CH_DEVICE(0x24, 1), /* T302X */
94 CH_DEVICE(0x25, 3), /* T320E */
95 CH_DEVICE(0x26, 2), /* T310X */
96 CH_DEVICE(0x30, 2), /* T3B10 */
97 CH_DEVICE(0x31, 3), /* T3B20 */
98 CH_DEVICE(0x32, 1), /* T3B02 */
Divy Le Rayce03aad2009-02-18 17:47:57 -080099 CH_DEVICE(0x35, 6), /* T3C20-derived T3C10 */
Divy Le Ray74451422009-05-29 12:52:44 +0000100 CH_DEVICE(0x36, 3), /* S320E-CR */
101 CH_DEVICE(0x37, 7), /* N320E-G2 */
Divy Le Ray4d22de32007-01-18 22:04:14 -0500102 {0,}
103};
104
105MODULE_DESCRIPTION(DRV_DESC);
106MODULE_AUTHOR("Chelsio Communications");
Divy Le Ray1d68e932007-01-30 19:44:35 -0800107MODULE_LICENSE("Dual BSD/GPL");
Divy Le Ray4d22de32007-01-18 22:04:14 -0500108MODULE_VERSION(DRV_VERSION);
109MODULE_DEVICE_TABLE(pci, cxgb3_pci_tbl);
110
111static int dflt_msg_enable = DFLT_MSG_ENABLE;
112
113module_param(dflt_msg_enable, int, 0644);
114MODULE_PARM_DESC(dflt_msg_enable, "Chelsio T3 default message enable bitmap");
115
116/*
117 * The driver uses the best interrupt scheme available on a platform in the
118 * order MSI-X, MSI, legacy pin interrupts. This parameter determines which
119 * of these schemes the driver may consider as follows:
120 *
121 * msi = 2: choose from among all three options
122 * msi = 1: only consider MSI and pin interrupts
123 * msi = 0: force pin interrupts
124 */
125static int msi = 2;
126
127module_param(msi, int, 0644);
128MODULE_PARM_DESC(msi, "whether to use MSI or MSI-X");
129
130/*
131 * The driver enables offload as a default.
132 * To disable it, use ofld_disable = 1.
133 */
134
135static int ofld_disable = 0;
136
137module_param(ofld_disable, int, 0644);
138MODULE_PARM_DESC(ofld_disable, "whether to enable offload at init time or not");
139
140/*
141 * We have work elements that we need to cancel when an interface is taken
142 * down. Normally the work elements would be executed by keventd but that
143 * can deadlock because of linkwatch. If our close method takes the rtnl
144 * lock and linkwatch is ahead of our work elements in keventd, linkwatch
145 * will block keventd as it needs the rtnl lock, and we'll deadlock waiting
146 * for our work to complete. Get our own work queue to solve this.
147 */
Steve Wisee998f242010-01-27 17:03:34 +0000148struct workqueue_struct *cxgb3_wq;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500149
150/**
151 * link_report - show link status and link speed/duplex
152 * @p: the port whose settings are to be reported
153 *
154 * Shows the link status, speed, and duplex of a port.
155 */
156static void link_report(struct net_device *dev)
157{
158 if (!netif_carrier_ok(dev))
Joe Perches428ac432013-01-06 13:34:49 +0000159 netdev_info(dev, "link down\n");
Divy Le Ray4d22de32007-01-18 22:04:14 -0500160 else {
161 const char *s = "10Mbps";
162 const struct port_info *p = netdev_priv(dev);
163
164 switch (p->link_config.speed) {
165 case SPEED_10000:
166 s = "10Gbps";
167 break;
168 case SPEED_1000:
169 s = "1000Mbps";
170 break;
171 case SPEED_100:
172 s = "100Mbps";
173 break;
174 }
175
Joe Perches428ac432013-01-06 13:34:49 +0000176 netdev_info(dev, "link up, %s, %s-duplex\n",
177 s, p->link_config.duplex == DUPLEX_FULL
178 ? "full" : "half");
Divy Le Ray4d22de32007-01-18 22:04:14 -0500179 }
180}
181
Divy Le Ray34701fd2009-07-07 19:48:32 +0000182static void enable_tx_fifo_drain(struct adapter *adapter,
183 struct port_info *pi)
184{
185 t3_set_reg_field(adapter, A_XGM_TXFIFO_CFG + pi->mac.offset, 0,
186 F_ENDROPPKT);
187 t3_write_reg(adapter, A_XGM_RX_CTRL + pi->mac.offset, 0);
188 t3_write_reg(adapter, A_XGM_TX_CTRL + pi->mac.offset, F_TXEN);
189 t3_write_reg(adapter, A_XGM_RX_CTRL + pi->mac.offset, F_RXEN);
190}
191
192static void disable_tx_fifo_drain(struct adapter *adapter,
193 struct port_info *pi)
194{
195 t3_set_reg_field(adapter, A_XGM_TXFIFO_CFG + pi->mac.offset,
196 F_ENDROPPKT, 0);
197}
198
Divy Le Raybf792092009-03-12 21:14:19 +0000199void t3_os_link_fault(struct adapter *adap, int port_id, int state)
200{
201 struct net_device *dev = adap->port[port_id];
202 struct port_info *pi = netdev_priv(dev);
203
204 if (state == netif_carrier_ok(dev))
205 return;
206
207 if (state) {
208 struct cmac *mac = &pi->mac;
209
210 netif_carrier_on(dev);
211
Divy Le Ray34701fd2009-07-07 19:48:32 +0000212 disable_tx_fifo_drain(adap, pi);
213
Divy Le Raybf792092009-03-12 21:14:19 +0000214 /* Clear local faults */
215 t3_xgm_intr_disable(adap, pi->port_id);
216 t3_read_reg(adap, A_XGM_INT_STATUS +
217 pi->mac.offset);
218 t3_write_reg(adap,
219 A_XGM_INT_CAUSE + pi->mac.offset,
220 F_XGM_INT);
221
222 t3_set_reg_field(adap,
223 A_XGM_INT_ENABLE +
224 pi->mac.offset,
225 F_XGM_INT, F_XGM_INT);
226 t3_xgm_intr_enable(adap, pi->port_id);
227
228 t3_mac_enable(mac, MAC_DIRECTION_TX);
Divy Le Ray34701fd2009-07-07 19:48:32 +0000229 } else {
Divy Le Raybf792092009-03-12 21:14:19 +0000230 netif_carrier_off(dev);
231
Divy Le Ray34701fd2009-07-07 19:48:32 +0000232 /* Flush TX FIFO */
233 enable_tx_fifo_drain(adap, pi);
234 }
Divy Le Raybf792092009-03-12 21:14:19 +0000235 link_report(dev);
236}
237
Divy Le Ray4d22de32007-01-18 22:04:14 -0500238/**
239 * t3_os_link_changed - handle link status changes
240 * @adapter: the adapter associated with the link change
241 * @port_id: the port index whose limk status has changed
242 * @link_stat: the new status of the link
243 * @speed: the new speed setting
244 * @duplex: the new duplex setting
245 * @pause: the new flow-control setting
246 *
247 * This is the OS-dependent handler for link status changes. The OS
248 * neutral handler takes care of most of the processing for these events,
249 * then calls this handler for any OS-specific processing.
250 */
251void t3_os_link_changed(struct adapter *adapter, int port_id, int link_stat,
252 int speed, int duplex, int pause)
253{
254 struct net_device *dev = adapter->port[port_id];
Divy Le Ray6d6daba2007-03-31 00:23:24 -0700255 struct port_info *pi = netdev_priv(dev);
256 struct cmac *mac = &pi->mac;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500257
258 /* Skip changes from disabled ports. */
259 if (!netif_running(dev))
260 return;
261
262 if (link_stat != netif_carrier_ok(dev)) {
Divy Le Ray6d6daba2007-03-31 00:23:24 -0700263 if (link_stat) {
Divy Le Ray34701fd2009-07-07 19:48:32 +0000264 disable_tx_fifo_drain(adapter, pi);
265
Divy Le Ray59cf8102007-04-09 20:10:27 -0700266 t3_mac_enable(mac, MAC_DIRECTION_RX);
Divy Le Raybf792092009-03-12 21:14:19 +0000267
268 /* Clear local faults */
269 t3_xgm_intr_disable(adapter, pi->port_id);
270 t3_read_reg(adapter, A_XGM_INT_STATUS +
271 pi->mac.offset);
272 t3_write_reg(adapter,
273 A_XGM_INT_CAUSE + pi->mac.offset,
274 F_XGM_INT);
275
276 t3_set_reg_field(adapter,
277 A_XGM_INT_ENABLE + pi->mac.offset,
278 F_XGM_INT, F_XGM_INT);
279 t3_xgm_intr_enable(adapter, pi->port_id);
280
Divy Le Ray4d22de32007-01-18 22:04:14 -0500281 netif_carrier_on(dev);
Divy Le Ray6d6daba2007-03-31 00:23:24 -0700282 } else {
Divy Le Ray4d22de32007-01-18 22:04:14 -0500283 netif_carrier_off(dev);
Divy Le Raybf792092009-03-12 21:14:19 +0000284
285 t3_xgm_intr_disable(adapter, pi->port_id);
286 t3_read_reg(adapter, A_XGM_INT_STATUS + pi->mac.offset);
287 t3_set_reg_field(adapter,
288 A_XGM_INT_ENABLE + pi->mac.offset,
289 F_XGM_INT, 0);
290
291 if (is_10G(adapter))
292 pi->phy.ops->power_down(&pi->phy, 1);
293
294 t3_read_reg(adapter, A_XGM_INT_STATUS + pi->mac.offset);
Divy Le Ray59cf8102007-04-09 20:10:27 -0700295 t3_mac_disable(mac, MAC_DIRECTION_RX);
296 t3_link_start(&pi->phy, mac, &pi->link_config);
Divy Le Ray34701fd2009-07-07 19:48:32 +0000297
298 /* Flush TX FIFO */
299 enable_tx_fifo_drain(adapter, pi);
Divy Le Ray6d6daba2007-03-31 00:23:24 -0700300 }
301
Divy Le Ray4d22de32007-01-18 22:04:14 -0500302 link_report(dev);
303 }
304}
305
Divy Le Ray1e882022008-10-08 17:40:07 -0700306/**
307 * t3_os_phymod_changed - handle PHY module changes
308 * @phy: the PHY reporting the module change
309 * @mod_type: new module type
310 *
311 * This is the OS-dependent handler for PHY module changes. It is
312 * invoked when a PHY module is removed or inserted for any OS-specific
313 * processing.
314 */
315void t3_os_phymod_changed(struct adapter *adap, int port_id)
316{
317 static const char *mod_str[] = {
318 NULL, "SR", "LR", "LRM", "TWINAX", "TWINAX", "unknown"
319 };
320
321 const struct net_device *dev = adap->port[port_id];
322 const struct port_info *pi = netdev_priv(dev);
323
324 if (pi->phy.modtype == phy_modtype_none)
Joe Perches428ac432013-01-06 13:34:49 +0000325 netdev_info(dev, "PHY module unplugged\n");
Divy Le Ray1e882022008-10-08 17:40:07 -0700326 else
Joe Perches428ac432013-01-06 13:34:49 +0000327 netdev_info(dev, "%s PHY module inserted\n",
328 mod_str[pi->phy.modtype]);
Divy Le Ray1e882022008-10-08 17:40:07 -0700329}
330
Divy Le Ray4d22de32007-01-18 22:04:14 -0500331static void cxgb_set_rxmode(struct net_device *dev)
332{
Divy Le Ray4d22de32007-01-18 22:04:14 -0500333 struct port_info *pi = netdev_priv(dev);
334
Jiri Pirko0988d262010-02-17 12:27:14 +0000335 t3_mac_set_rx_mode(&pi->mac, dev);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500336}
337
338/**
339 * link_start - enable a port
340 * @dev: the device to enable
341 *
342 * Performs the MAC and PHY actions needed to enable a port.
343 */
344static void link_start(struct net_device *dev)
345{
Divy Le Ray4d22de32007-01-18 22:04:14 -0500346 struct port_info *pi = netdev_priv(dev);
347 struct cmac *mac = &pi->mac;
348
Divy Le Ray4d22de32007-01-18 22:04:14 -0500349 t3_mac_reset(mac);
Karen Xief14d42f2009-10-08 09:11:05 +0000350 t3_mac_set_num_ucast(mac, MAX_MAC_IDX);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500351 t3_mac_set_mtu(mac, dev->mtu);
Karen Xief14d42f2009-10-08 09:11:05 +0000352 t3_mac_set_address(mac, LAN_MAC_IDX, dev->dev_addr);
353 t3_mac_set_address(mac, SAN_MAC_IDX, pi->iscsic.mac_addr);
Jiri Pirko0988d262010-02-17 12:27:14 +0000354 t3_mac_set_rx_mode(mac, dev);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500355 t3_link_start(&pi->phy, mac, &pi->link_config);
356 t3_mac_enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
357}
358
359static inline void cxgb_disable_msi(struct adapter *adapter)
360{
361 if (adapter->flags & USING_MSIX) {
362 pci_disable_msix(adapter->pdev);
363 adapter->flags &= ~USING_MSIX;
364 } else if (adapter->flags & USING_MSI) {
365 pci_disable_msi(adapter->pdev);
366 adapter->flags &= ~USING_MSI;
367 }
368}
369
370/*
371 * Interrupt handler for asynchronous events used with MSI-X.
372 */
373static irqreturn_t t3_async_intr_handler(int irq, void *cookie)
374{
375 t3_slow_intr_handler(cookie);
376 return IRQ_HANDLED;
377}
378
379/*
380 * Name the MSI-X interrupts.
381 */
382static void name_msix_vecs(struct adapter *adap)
383{
384 int i, j, msi_idx = 1, n = sizeof(adap->msix_info[0].desc) - 1;
385
386 snprintf(adap->msix_info[0].desc, n, "%s", adap->name);
387 adap->msix_info[0].desc[n] = 0;
388
389 for_each_port(adap, j) {
390 struct net_device *d = adap->port[j];
391 const struct port_info *pi = netdev_priv(d);
392
393 for (i = 0; i < pi->nqsets; i++, msi_idx++) {
394 snprintf(adap->msix_info[msi_idx].desc, n,
Divy Le Ray8c263762008-10-08 17:37:33 -0700395 "%s-%d", d->name, pi->first_qset + i);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500396 adap->msix_info[msi_idx].desc[n] = 0;
397 }
Divy Le Ray8c263762008-10-08 17:37:33 -0700398 }
Divy Le Ray4d22de32007-01-18 22:04:14 -0500399}
400
401static int request_msix_data_irqs(struct adapter *adap)
402{
403 int i, j, err, qidx = 0;
404
405 for_each_port(adap, i) {
406 int nqsets = adap2pinfo(adap, i)->nqsets;
407
408 for (j = 0; j < nqsets; ++j) {
409 err = request_irq(adap->msix_info[qidx + 1].vec,
410 t3_intr_handler(adap,
411 adap->sge.qs[qidx].
412 rspq.polling), 0,
413 adap->msix_info[qidx + 1].desc,
414 &adap->sge.qs[qidx]);
415 if (err) {
416 while (--qidx >= 0)
417 free_irq(adap->msix_info[qidx + 1].vec,
418 &adap->sge.qs[qidx]);
419 return err;
420 }
421 qidx++;
422 }
423 }
424 return 0;
425}
426
Divy Le Ray8c263762008-10-08 17:37:33 -0700427static void free_irq_resources(struct adapter *adapter)
428{
429 if (adapter->flags & USING_MSIX) {
430 int i, n = 0;
431
432 free_irq(adapter->msix_info[0].vec, adapter);
433 for_each_port(adapter, i)
Divy Le Ray5cda9362009-01-18 21:29:40 -0800434 n += adap2pinfo(adapter, i)->nqsets;
Divy Le Ray8c263762008-10-08 17:37:33 -0700435
436 for (i = 0; i < n; ++i)
437 free_irq(adapter->msix_info[i + 1].vec,
438 &adapter->sge.qs[i]);
439 } else
440 free_irq(adapter->pdev->irq, adapter);
441}
442
Divy Le Rayb8819552007-12-17 18:47:31 -0800443static int await_mgmt_replies(struct adapter *adap, unsigned long init_cnt,
444 unsigned long n)
445{
Andre Detsche95ef5d2010-04-26 05:38:27 +0000446 int attempts = 10;
Divy Le Rayb8819552007-12-17 18:47:31 -0800447
448 while (adap->sge.qs[0].rspq.offload_pkts < init_cnt + n) {
449 if (!--attempts)
450 return -ETIMEDOUT;
451 msleep(10);
452 }
453 return 0;
454}
455
456static int init_tp_parity(struct adapter *adap)
457{
458 int i;
459 struct sk_buff *skb;
460 struct cpl_set_tcb_field *greq;
461 unsigned long cnt = adap->sge.qs[0].rspq.offload_pkts;
462
463 t3_tp_set_offload_mode(adap, 1);
464
465 for (i = 0; i < 16; i++) {
466 struct cpl_smt_write_req *req;
467
Divy Le Ray74b793e2009-06-09 23:25:21 +0000468 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
469 if (!skb)
470 skb = adap->nofail_skb;
471 if (!skb)
472 goto alloc_skb_fail;
473
yuan linyude77b962017-06-18 22:48:17 +0800474 req = __skb_put_zero(skb, sizeof(*req));
Divy Le Rayb8819552007-12-17 18:47:31 -0800475 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
476 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, i));
Divy Le Raydce7d1d2009-07-07 19:48:59 +0000477 req->mtu_idx = NMTUS - 1;
Divy Le Rayb8819552007-12-17 18:47:31 -0800478 req->iff = i;
479 t3_mgmt_tx(adap, skb);
Divy Le Ray74b793e2009-06-09 23:25:21 +0000480 if (skb == adap->nofail_skb) {
481 await_mgmt_replies(adap, cnt, i + 1);
482 adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
483 if (!adap->nofail_skb)
484 goto alloc_skb_fail;
485 }
Divy Le Rayb8819552007-12-17 18:47:31 -0800486 }
487
488 for (i = 0; i < 2048; i++) {
489 struct cpl_l2t_write_req *req;
490
Divy Le Ray74b793e2009-06-09 23:25:21 +0000491 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
492 if (!skb)
493 skb = adap->nofail_skb;
494 if (!skb)
495 goto alloc_skb_fail;
496
yuan linyude77b962017-06-18 22:48:17 +0800497 req = __skb_put_zero(skb, sizeof(*req));
Divy Le Rayb8819552007-12-17 18:47:31 -0800498 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
499 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, i));
500 req->params = htonl(V_L2T_W_IDX(i));
501 t3_mgmt_tx(adap, skb);
Divy Le Ray74b793e2009-06-09 23:25:21 +0000502 if (skb == adap->nofail_skb) {
503 await_mgmt_replies(adap, cnt, 16 + i + 1);
504 adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
505 if (!adap->nofail_skb)
506 goto alloc_skb_fail;
507 }
Divy Le Rayb8819552007-12-17 18:47:31 -0800508 }
509
510 for (i = 0; i < 2048; i++) {
511 struct cpl_rte_write_req *req;
512
Divy Le Ray74b793e2009-06-09 23:25:21 +0000513 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
514 if (!skb)
515 skb = adap->nofail_skb;
516 if (!skb)
517 goto alloc_skb_fail;
518
yuan linyude77b962017-06-18 22:48:17 +0800519 req = __skb_put_zero(skb, sizeof(*req));
Divy Le Rayb8819552007-12-17 18:47:31 -0800520 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
521 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_RTE_WRITE_REQ, i));
522 req->l2t_idx = htonl(V_L2T_W_IDX(i));
523 t3_mgmt_tx(adap, skb);
Divy Le Ray74b793e2009-06-09 23:25:21 +0000524 if (skb == adap->nofail_skb) {
525 await_mgmt_replies(adap, cnt, 16 + 2048 + i + 1);
526 adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
527 if (!adap->nofail_skb)
528 goto alloc_skb_fail;
529 }
Divy Le Rayb8819552007-12-17 18:47:31 -0800530 }
531
Divy Le Ray74b793e2009-06-09 23:25:21 +0000532 skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
533 if (!skb)
534 skb = adap->nofail_skb;
535 if (!skb)
536 goto alloc_skb_fail;
537
yuan linyude77b962017-06-18 22:48:17 +0800538 greq = __skb_put_zero(skb, sizeof(*greq));
Divy Le Rayb8819552007-12-17 18:47:31 -0800539 greq->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
540 OPCODE_TID(greq) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, 0));
541 greq->mask = cpu_to_be64(1);
542 t3_mgmt_tx(adap, skb);
543
544 i = await_mgmt_replies(adap, cnt, 16 + 2048 + 2048 + 1);
Divy Le Ray74b793e2009-06-09 23:25:21 +0000545 if (skb == adap->nofail_skb) {
546 i = await_mgmt_replies(adap, cnt, 16 + 2048 + 2048 + 1);
547 adap->nofail_skb = alloc_skb(sizeof(*greq), GFP_KERNEL);
548 }
549
Divy Le Rayb8819552007-12-17 18:47:31 -0800550 t3_tp_set_offload_mode(adap, 0);
551 return i;
Divy Le Ray74b793e2009-06-09 23:25:21 +0000552
553alloc_skb_fail:
554 t3_tp_set_offload_mode(adap, 0);
555 return -ENOMEM;
Divy Le Rayb8819552007-12-17 18:47:31 -0800556}
557
Divy Le Ray4d22de32007-01-18 22:04:14 -0500558/**
559 * setup_rss - configure RSS
560 * @adap: the adapter
561 *
562 * Sets up RSS to distribute packets to multiple receive queues. We
563 * configure the RSS CPU lookup table to distribute to the number of HW
564 * receive queues, and the response queue lookup table to narrow that
565 * down to the response queues actually configured for each port.
566 * We always configure the RSS mapping for two ports since the mapping
567 * table has plenty of entries.
568 */
569static void setup_rss(struct adapter *adap)
570{
571 int i;
572 unsigned int nq0 = adap2pinfo(adap, 0)->nqsets;
573 unsigned int nq1 = adap->port[1] ? adap2pinfo(adap, 1)->nqsets : 1;
574 u8 cpus[SGE_QSETS + 1];
Michal Schmidt0b86a2a2016-04-29 11:06:50 +0200575 u16 rspq_map[RSS_TABLE_SIZE + 1];
Divy Le Ray4d22de32007-01-18 22:04:14 -0500576
577 for (i = 0; i < SGE_QSETS; ++i)
578 cpus[i] = i;
579 cpus[SGE_QSETS] = 0xff; /* terminator */
580
581 for (i = 0; i < RSS_TABLE_SIZE / 2; ++i) {
582 rspq_map[i] = i % nq0;
583 rspq_map[i + RSS_TABLE_SIZE / 2] = (i % nq1) + nq0;
584 }
Michal Schmidt0b86a2a2016-04-29 11:06:50 +0200585 rspq_map[RSS_TABLE_SIZE] = 0xffff; /* terminator */
Divy Le Ray4d22de32007-01-18 22:04:14 -0500586
587 t3_config_rss(adap, F_RQFEEDBACKENABLE | F_TNLLKPEN | F_TNLMAPEN |
588 F_TNLPRTEN | F_TNL2TUPEN | F_TNL4TUPEN |
Divy Le Raya2604be2007-11-16 11:22:16 -0800589 V_RRCPLCPUSIZE(6) | F_HASHTOEPLITZ, cpus, rspq_map);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500590}
591
Steve Wisee998f242010-01-27 17:03:34 +0000592static void ring_dbs(struct adapter *adap)
593{
594 int i, j;
595
596 for (i = 0; i < SGE_QSETS; i++) {
597 struct sge_qset *qs = &adap->sge.qs[i];
598
599 if (qs->adap)
600 for (j = 0; j < SGE_TXQ_PER_SET; j++)
601 t3_write_reg(adap, A_SG_KDOORBELL, F_SELEGRCNTX | V_EGRCNTX(qs->txq[j].cntxt_id));
602 }
603}
604
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700605static void init_napi(struct adapter *adap)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500606{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700607 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500608
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700609 for (i = 0; i < SGE_QSETS; i++) {
610 struct sge_qset *qs = &adap->sge.qs[i];
Divy Le Ray4d22de32007-01-18 22:04:14 -0500611
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700612 if (qs->adap)
613 netif_napi_add(qs->netdev, &qs->napi, qs->napi.poll,
614 64);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500615 }
Divy Le Ray48c4b6d2008-05-06 19:25:56 -0700616
617 /*
618 * netif_napi_add() can be called only once per napi_struct because it
619 * adds each new napi_struct to a list. Be careful not to call it a
620 * second time, e.g., during EEH recovery, by making a note of it.
621 */
622 adap->flags |= NAPI_INIT;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500623}
624
625/*
626 * Wait until all NAPI handlers are descheduled. This includes the handlers of
627 * both netdevices representing interfaces and the dummy ones for the extra
628 * queues.
629 */
630static void quiesce_rx(struct adapter *adap)
631{
632 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500633
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700634 for (i = 0; i < SGE_QSETS; i++)
635 if (adap->sge.qs[i].adap)
636 napi_disable(&adap->sge.qs[i].napi);
637}
Divy Le Ray4d22de32007-01-18 22:04:14 -0500638
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700639static void enable_all_napi(struct adapter *adap)
640{
641 int i;
642 for (i = 0; i < SGE_QSETS; i++)
643 if (adap->sge.qs[i].adap)
644 napi_enable(&adap->sge.qs[i].napi);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500645}
646
647/**
648 * setup_sge_qsets - configure SGE Tx/Rx/response queues
649 * @adap: the adapter
650 *
651 * Determines how many sets of SGE queues to use and initializes them.
652 * We support multiple queue sets per port if we have MSI-X, otherwise
653 * just one queue set per port.
654 */
655static int setup_sge_qsets(struct adapter *adap)
656{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700657 int i, j, err, irq_idx = 0, qset_idx = 0;
Divy Le Ray8ac3ba62007-03-31 00:23:19 -0700658 unsigned int ntxq = SGE_TXQ_PER_SET;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500659
660 if (adap->params.rev > 0 && !(adap->flags & USING_MSI))
661 irq_idx = -1;
662
663 for_each_port(adap, i) {
664 struct net_device *dev = adap->port[i];
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700665 struct port_info *pi = netdev_priv(dev);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500666
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700667 pi->qs = &adap->sge.qs[pi->first_qset];
Roland Dreiere594e962009-07-09 09:30:25 +0000668 for (j = 0; j < pi->nqsets; ++j, ++qset_idx) {
Divy Le Ray4d22de32007-01-18 22:04:14 -0500669 err = t3_sge_alloc_qset(adap, qset_idx, 1,
670 (adap->flags & USING_MSIX) ? qset_idx + 1 :
671 irq_idx,
Divy Le Ray82ad3322008-12-16 01:09:39 -0800672 &adap->params.sge.qset[qset_idx], ntxq, dev,
673 netdev_get_tx_queue(dev, j));
Divy Le Ray4d22de32007-01-18 22:04:14 -0500674 if (err) {
675 t3_free_sge_resources(adap);
676 return err;
677 }
678 }
679 }
680
681 return 0;
682}
683
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800684static ssize_t attr_show(struct device *d, char *buf,
Divy Le Ray896392e2007-02-24 16:43:50 -0800685 ssize_t(*format) (struct net_device *, char *))
Divy Le Ray4d22de32007-01-18 22:04:14 -0500686{
687 ssize_t len;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500688
689 /* Synchronize with ioctls that may shut down the device */
690 rtnl_lock();
Divy Le Ray896392e2007-02-24 16:43:50 -0800691 len = (*format) (to_net_dev(d), buf);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500692 rtnl_unlock();
693 return len;
694}
695
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800696static ssize_t attr_store(struct device *d,
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800697 const char *buf, size_t len,
Divy Le Ray896392e2007-02-24 16:43:50 -0800698 ssize_t(*set) (struct net_device *, unsigned int),
Divy Le Ray4d22de32007-01-18 22:04:14 -0500699 unsigned int min_val, unsigned int max_val)
700{
Divy Le Ray4d22de32007-01-18 22:04:14 -0500701 ssize_t ret;
702 unsigned int val;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500703
704 if (!capable(CAP_NET_ADMIN))
705 return -EPERM;
706
LABBE Corentine72c9322015-12-07 14:11:33 +0100707 ret = kstrtouint(buf, 0, &val);
708 if (ret)
709 return ret;
710 if (val < min_val || val > max_val)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500711 return -EINVAL;
712
713 rtnl_lock();
Divy Le Ray896392e2007-02-24 16:43:50 -0800714 ret = (*set) (to_net_dev(d), val);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500715 if (!ret)
716 ret = len;
717 rtnl_unlock();
718 return ret;
719}
720
721#define CXGB3_SHOW(name, val_expr) \
Divy Le Ray896392e2007-02-24 16:43:50 -0800722static ssize_t format_##name(struct net_device *dev, char *buf) \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500723{ \
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700724 struct port_info *pi = netdev_priv(dev); \
725 struct adapter *adap = pi->adapter; \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500726 return sprintf(buf, "%u\n", val_expr); \
727} \
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800728static ssize_t show_##name(struct device *d, struct device_attribute *attr, \
729 char *buf) \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500730{ \
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800731 return attr_show(d, buf, format_##name); \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500732}
733
Divy Le Ray896392e2007-02-24 16:43:50 -0800734static ssize_t set_nfilters(struct net_device *dev, unsigned int val)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500735{
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700736 struct port_info *pi = netdev_priv(dev);
737 struct adapter *adap = pi->adapter;
Divy Le Ray9f238482007-03-31 00:23:13 -0700738 int min_tids = is_offload(adap) ? MC5_MIN_TIDS : 0;
Divy Le Ray896392e2007-02-24 16:43:50 -0800739
Divy Le Ray4d22de32007-01-18 22:04:14 -0500740 if (adap->flags & FULL_INIT_DONE)
741 return -EBUSY;
742 if (val && adap->params.rev == 0)
743 return -EINVAL;
Divy Le Ray9f238482007-03-31 00:23:13 -0700744 if (val > t3_mc5_size(&adap->mc5) - adap->params.mc5.nservers -
745 min_tids)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500746 return -EINVAL;
747 adap->params.mc5.nfilters = val;
748 return 0;
749}
750
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800751static ssize_t store_nfilters(struct device *d, struct device_attribute *attr,
752 const char *buf, size_t len)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500753{
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800754 return attr_store(d, buf, len, set_nfilters, 0, ~0);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500755}
756
Divy Le Ray896392e2007-02-24 16:43:50 -0800757static ssize_t set_nservers(struct net_device *dev, unsigned int val)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500758{
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700759 struct port_info *pi = netdev_priv(dev);
760 struct adapter *adap = pi->adapter;
Divy Le Ray896392e2007-02-24 16:43:50 -0800761
Divy Le Ray4d22de32007-01-18 22:04:14 -0500762 if (adap->flags & FULL_INIT_DONE)
763 return -EBUSY;
Divy Le Ray9f238482007-03-31 00:23:13 -0700764 if (val > t3_mc5_size(&adap->mc5) - adap->params.mc5.nfilters -
765 MC5_MIN_TIDS)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500766 return -EINVAL;
767 adap->params.mc5.nservers = val;
768 return 0;
769}
770
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800771static ssize_t store_nservers(struct device *d, struct device_attribute *attr,
772 const char *buf, size_t len)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500773{
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800774 return attr_store(d, buf, len, set_nservers, 0, ~0);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500775}
776
777#define CXGB3_ATTR_R(name, val_expr) \
778CXGB3_SHOW(name, val_expr) \
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800779static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500780
781#define CXGB3_ATTR_RW(name, val_expr, store_method) \
782CXGB3_SHOW(name, val_expr) \
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800783static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_##name, store_method)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500784
785CXGB3_ATTR_R(cam_size, t3_mc5_size(&adap->mc5));
786CXGB3_ATTR_RW(nfilters, adap->params.mc5.nfilters, store_nfilters);
787CXGB3_ATTR_RW(nservers, adap->params.mc5.nservers, store_nservers);
788
789static struct attribute *cxgb3_attrs[] = {
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800790 &dev_attr_cam_size.attr,
791 &dev_attr_nfilters.attr,
792 &dev_attr_nservers.attr,
Divy Le Ray4d22de32007-01-18 22:04:14 -0500793 NULL
794};
795
796static struct attribute_group cxgb3_attr_group = {.attrs = cxgb3_attrs };
797
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800798static ssize_t tm_attr_show(struct device *d,
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800799 char *buf, int sched)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500800{
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700801 struct port_info *pi = netdev_priv(to_net_dev(d));
802 struct adapter *adap = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500803 unsigned int v, addr, bpt, cpt;
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700804 ssize_t len;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500805
806 addr = A_TP_TX_MOD_Q1_Q0_RATE_LIMIT - sched / 2;
807 rtnl_lock();
808 t3_write_reg(adap, A_TP_TM_PIO_ADDR, addr);
809 v = t3_read_reg(adap, A_TP_TM_PIO_DATA);
810 if (sched & 1)
811 v >>= 16;
812 bpt = (v >> 8) & 0xff;
813 cpt = v & 0xff;
814 if (!cpt)
815 len = sprintf(buf, "disabled\n");
816 else {
817 v = (adap->params.vpd.cclk * 1000) / cpt;
818 len = sprintf(buf, "%u Kbps\n", (v * bpt) / 125);
819 }
820 rtnl_unlock();
821 return len;
822}
823
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800824static ssize_t tm_attr_store(struct device *d,
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800825 const char *buf, size_t len, int sched)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500826{
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700827 struct port_info *pi = netdev_priv(to_net_dev(d));
828 struct adapter *adap = pi->adapter;
829 unsigned int val;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500830 ssize_t ret;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500831
832 if (!capable(CAP_NET_ADMIN))
833 return -EPERM;
834
LABBE Corentine72c9322015-12-07 14:11:33 +0100835 ret = kstrtouint(buf, 0, &val);
836 if (ret)
837 return ret;
838 if (val > 10000000)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500839 return -EINVAL;
840
841 rtnl_lock();
842 ret = t3_config_sched(adap, val, sched);
843 if (!ret)
844 ret = len;
845 rtnl_unlock();
846 return ret;
847}
848
849#define TM_ATTR(name, sched) \
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800850static ssize_t show_##name(struct device *d, struct device_attribute *attr, \
851 char *buf) \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500852{ \
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800853 return tm_attr_show(d, buf, sched); \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500854} \
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800855static ssize_t store_##name(struct device *d, struct device_attribute *attr, \
856 const char *buf, size_t len) \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500857{ \
Divy Le Ray3e5192e2007-11-16 11:22:10 -0800858 return tm_attr_store(d, buf, len, sched); \
Divy Le Ray4d22de32007-01-18 22:04:14 -0500859} \
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800860static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_##name, store_##name)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500861
862TM_ATTR(sched0, 0);
863TM_ATTR(sched1, 1);
864TM_ATTR(sched2, 2);
865TM_ATTR(sched3, 3);
866TM_ATTR(sched4, 4);
867TM_ATTR(sched5, 5);
868TM_ATTR(sched6, 6);
869TM_ATTR(sched7, 7);
870
871static struct attribute *offload_attrs[] = {
Divy Le Ray0ee8d332007-02-08 16:55:59 -0800872 &dev_attr_sched0.attr,
873 &dev_attr_sched1.attr,
874 &dev_attr_sched2.attr,
875 &dev_attr_sched3.attr,
876 &dev_attr_sched4.attr,
877 &dev_attr_sched5.attr,
878 &dev_attr_sched6.attr,
879 &dev_attr_sched7.attr,
Divy Le Ray4d22de32007-01-18 22:04:14 -0500880 NULL
881};
882
883static struct attribute_group offload_attr_group = {.attrs = offload_attrs };
884
885/*
886 * Sends an sk_buff to an offload queue driver
887 * after dealing with any active network taps.
888 */
889static inline int offload_tx(struct t3cdev *tdev, struct sk_buff *skb)
890{
891 int ret;
892
893 local_bh_disable();
894 ret = t3_offload_tx(tdev, skb);
895 local_bh_enable();
896 return ret;
897}
898
899static int write_smt_entry(struct adapter *adapter, int idx)
900{
901 struct cpl_smt_write_req *req;
Karen Xief14d42f2009-10-08 09:11:05 +0000902 struct port_info *pi = netdev_priv(adapter->port[idx]);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500903 struct sk_buff *skb = alloc_skb(sizeof(*req), GFP_KERNEL);
904
905 if (!skb)
906 return -ENOMEM;
907
Johannes Berg4df864c2017-06-16 14:29:21 +0200908 req = __skb_put(skb, sizeof(*req));
Divy Le Ray4d22de32007-01-18 22:04:14 -0500909 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
910 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, idx));
911 req->mtu_idx = NMTUS - 1; /* should be 0 but there's a T3 bug */
912 req->iff = idx;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500913 memcpy(req->src_mac0, adapter->port[idx]->dev_addr, ETH_ALEN);
Karen Xief14d42f2009-10-08 09:11:05 +0000914 memcpy(req->src_mac1, pi->iscsic.mac_addr, ETH_ALEN);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500915 skb->priority = 1;
916 offload_tx(&adapter->tdev, skb);
917 return 0;
918}
919
920static int init_smt(struct adapter *adapter)
921{
922 int i;
923
924 for_each_port(adapter, i)
925 write_smt_entry(adapter, i);
926 return 0;
927}
928
929static void init_port_mtus(struct adapter *adapter)
930{
931 unsigned int mtus = adapter->port[0]->mtu;
932
933 if (adapter->port[1])
934 mtus |= adapter->port[1]->mtu << 16;
935 t3_write_reg(adapter, A_TP_MTU_PORT_TABLE, mtus);
936}
937
Divy Le Ray8c263762008-10-08 17:37:33 -0700938static int send_pktsched_cmd(struct adapter *adap, int sched, int qidx, int lo,
Divy Le Ray14ab9892007-01-30 19:43:50 -0800939 int hi, int port)
940{
941 struct sk_buff *skb;
942 struct mngt_pktsched_wr *req;
Divy Le Ray8c263762008-10-08 17:37:33 -0700943 int ret;
Divy Le Ray14ab9892007-01-30 19:43:50 -0800944
Divy Le Ray74b793e2009-06-09 23:25:21 +0000945 skb = alloc_skb(sizeof(*req), GFP_KERNEL);
946 if (!skb)
947 skb = adap->nofail_skb;
948 if (!skb)
949 return -ENOMEM;
950
Johannes Berg4df864c2017-06-16 14:29:21 +0200951 req = skb_put(skb, sizeof(*req));
Divy Le Ray14ab9892007-01-30 19:43:50 -0800952 req->wr_hi = htonl(V_WR_OP(FW_WROPCODE_MNGT));
953 req->mngt_opcode = FW_MNGTOPCODE_PKTSCHED_SET;
954 req->sched = sched;
955 req->idx = qidx;
956 req->min = lo;
957 req->max = hi;
958 req->binding = port;
Divy Le Ray8c263762008-10-08 17:37:33 -0700959 ret = t3_mgmt_tx(adap, skb);
Divy Le Ray74b793e2009-06-09 23:25:21 +0000960 if (skb == adap->nofail_skb) {
961 adap->nofail_skb = alloc_skb(sizeof(struct cpl_set_tcb_field),
962 GFP_KERNEL);
963 if (!adap->nofail_skb)
964 ret = -ENOMEM;
965 }
Divy Le Ray8c263762008-10-08 17:37:33 -0700966
967 return ret;
Divy Le Ray14ab9892007-01-30 19:43:50 -0800968}
969
Divy Le Ray8c263762008-10-08 17:37:33 -0700970static int bind_qsets(struct adapter *adap)
Divy Le Ray14ab9892007-01-30 19:43:50 -0800971{
Divy Le Ray8c263762008-10-08 17:37:33 -0700972 int i, j, err = 0;
Divy Le Ray14ab9892007-01-30 19:43:50 -0800973
974 for_each_port(adap, i) {
975 const struct port_info *pi = adap2pinfo(adap, i);
976
Divy Le Ray8c263762008-10-08 17:37:33 -0700977 for (j = 0; j < pi->nqsets; ++j) {
978 int ret = send_pktsched_cmd(adap, 1,
979 pi->first_qset + j, -1,
980 -1, i);
981 if (ret)
982 err = ret;
983 }
Divy Le Ray14ab9892007-01-30 19:43:50 -0800984 }
Divy Le Ray8c263762008-10-08 17:37:33 -0700985
986 return err;
Divy Le Ray14ab9892007-01-30 19:43:50 -0800987}
988
Ben Hutchings34336ec2009-11-07 11:53:52 +0000989#define FW_VERSION __stringify(FW_VERSION_MAJOR) "." \
990 __stringify(FW_VERSION_MINOR) "." __stringify(FW_VERSION_MICRO)
991#define FW_FNAME "cxgb3/t3fw-" FW_VERSION ".bin"
992#define TPSRAM_VERSION __stringify(TP_VERSION_MAJOR) "." \
993 __stringify(TP_VERSION_MINOR) "." __stringify(TP_VERSION_MICRO)
994#define TPSRAM_NAME "cxgb3/t3%c_psram-" TPSRAM_VERSION ".bin"
Divy Le Ray2e8c07c2009-07-07 19:49:09 +0000995#define AEL2005_OPT_EDC_NAME "cxgb3/ael2005_opt_edc.bin"
996#define AEL2005_TWX_EDC_NAME "cxgb3/ael2005_twx_edc.bin"
Divy Le Ray94505262009-07-30 21:23:34 +0000997#define AEL2020_TWX_EDC_NAME "cxgb3/ael2020_twx_edc.bin"
Ben Hutchings34336ec2009-11-07 11:53:52 +0000998MODULE_FIRMWARE(FW_FNAME);
999MODULE_FIRMWARE("cxgb3/t3b_psram-" TPSRAM_VERSION ".bin");
1000MODULE_FIRMWARE("cxgb3/t3c_psram-" TPSRAM_VERSION ".bin");
1001MODULE_FIRMWARE(AEL2005_OPT_EDC_NAME);
1002MODULE_FIRMWARE(AEL2005_TWX_EDC_NAME);
1003MODULE_FIRMWARE(AEL2020_TWX_EDC_NAME);
Divy Le Ray2e8c07c2009-07-07 19:49:09 +00001004
1005static inline const char *get_edc_fw_name(int edc_idx)
1006{
1007 const char *fw_name = NULL;
1008
1009 switch (edc_idx) {
1010 case EDC_OPT_AEL2005:
1011 fw_name = AEL2005_OPT_EDC_NAME;
1012 break;
1013 case EDC_TWX_AEL2005:
1014 fw_name = AEL2005_TWX_EDC_NAME;
1015 break;
1016 case EDC_TWX_AEL2020:
1017 fw_name = AEL2020_TWX_EDC_NAME;
1018 break;
1019 }
1020 return fw_name;
1021}
1022
1023int t3_get_edc_fw(struct cphy *phy, int edc_idx, int size)
1024{
1025 struct adapter *adapter = phy->adapter;
1026 const struct firmware *fw;
Kees Cook92a486c2015-06-16 15:36:17 -07001027 const char *fw_name;
Divy Le Ray2e8c07c2009-07-07 19:49:09 +00001028 u32 csum;
1029 const __be32 *p;
1030 u16 *cache = phy->phy_cache;
Kees Cook92a486c2015-06-16 15:36:17 -07001031 int i, ret = -EINVAL;
Divy Le Ray2e8c07c2009-07-07 19:49:09 +00001032
Kees Cook92a486c2015-06-16 15:36:17 -07001033 fw_name = get_edc_fw_name(edc_idx);
1034 if (fw_name)
1035 ret = request_firmware(&fw, fw_name, &adapter->pdev->dev);
Divy Le Ray2e8c07c2009-07-07 19:49:09 +00001036 if (ret < 0) {
1037 dev_err(&adapter->pdev->dev,
1038 "could not upgrade firmware: unable to load %s\n",
Kees Cook92a486c2015-06-16 15:36:17 -07001039 fw_name);
Divy Le Ray2e8c07c2009-07-07 19:49:09 +00001040 return ret;
1041 }
1042
1043 /* check size, take checksum in account */
1044 if (fw->size > size + 4) {
1045 CH_ERR(adapter, "firmware image too large %u, expected %d\n",
1046 (unsigned int)fw->size, size + 4);
1047 ret = -EINVAL;
1048 }
1049
1050 /* compute checksum */
1051 p = (const __be32 *)fw->data;
1052 for (csum = 0, i = 0; i < fw->size / sizeof(csum); i++)
1053 csum += ntohl(p[i]);
1054
1055 if (csum != 0xffffffff) {
1056 CH_ERR(adapter, "corrupted firmware image, checksum %u\n",
1057 csum);
1058 ret = -EINVAL;
1059 }
1060
1061 for (i = 0; i < size / 4 ; i++) {
1062 *cache++ = (be32_to_cpu(p[i]) & 0xffff0000) >> 16;
1063 *cache++ = be32_to_cpu(p[i]) & 0xffff;
1064 }
1065
1066 release_firmware(fw);
1067
1068 return ret;
1069}
Divy Le Ray2e283962007-03-18 13:10:06 -07001070
1071static int upgrade_fw(struct adapter *adap)
1072{
1073 int ret;
Divy Le Ray2e283962007-03-18 13:10:06 -07001074 const struct firmware *fw;
1075 struct device *dev = &adap->pdev->dev;
1076
Ben Hutchings34336ec2009-11-07 11:53:52 +00001077 ret = request_firmware(&fw, FW_FNAME, dev);
Divy Le Ray2e283962007-03-18 13:10:06 -07001078 if (ret < 0) {
1079 dev_err(dev, "could not upgrade firmware: unable to load %s\n",
Ben Hutchings34336ec2009-11-07 11:53:52 +00001080 FW_FNAME);
Divy Le Ray2e283962007-03-18 13:10:06 -07001081 return ret;
1082 }
1083 ret = t3_load_fw(adap, fw->data, fw->size);
1084 release_firmware(fw);
Divy Le Ray47330072007-08-29 19:15:52 -07001085
1086 if (ret == 0)
1087 dev_info(dev, "successful upgrade to firmware %d.%d.%d\n",
1088 FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_MICRO);
1089 else
1090 dev_err(dev, "failed to upgrade to firmware %d.%d.%d\n",
1091 FW_VERSION_MAJOR, FW_VERSION_MINOR, FW_VERSION_MICRO);
Jeff Garzik2eab17a2007-11-23 21:59:45 -05001092
Divy Le Ray47330072007-08-29 19:15:52 -07001093 return ret;
1094}
1095
1096static inline char t3rev2char(struct adapter *adapter)
1097{
1098 char rev = 0;
1099
1100 switch(adapter->params.rev) {
1101 case T3_REV_B:
1102 case T3_REV_B2:
1103 rev = 'b';
1104 break;
Divy Le Ray1aafee22007-09-05 15:58:36 -07001105 case T3_REV_C:
1106 rev = 'c';
1107 break;
Divy Le Ray47330072007-08-29 19:15:52 -07001108 }
1109 return rev;
1110}
1111
Stephen Hemminger9265fab2007-10-08 16:22:29 -07001112static int update_tpsram(struct adapter *adap)
Divy Le Ray47330072007-08-29 19:15:52 -07001113{
1114 const struct firmware *tpsram;
1115 char buf[64];
1116 struct device *dev = &adap->pdev->dev;
1117 int ret;
1118 char rev;
Jeff Garzik2eab17a2007-11-23 21:59:45 -05001119
Divy Le Ray47330072007-08-29 19:15:52 -07001120 rev = t3rev2char(adap);
1121 if (!rev)
1122 return 0;
1123
Ben Hutchings34336ec2009-11-07 11:53:52 +00001124 snprintf(buf, sizeof(buf), TPSRAM_NAME, rev);
Divy Le Ray47330072007-08-29 19:15:52 -07001125
1126 ret = request_firmware(&tpsram, buf, dev);
1127 if (ret < 0) {
1128 dev_err(dev, "could not load TP SRAM: unable to load %s\n",
1129 buf);
1130 return ret;
1131 }
Jeff Garzik2eab17a2007-11-23 21:59:45 -05001132
Divy Le Ray47330072007-08-29 19:15:52 -07001133 ret = t3_check_tpsram(adap, tpsram->data, tpsram->size);
1134 if (ret)
Jeff Garzik2eab17a2007-11-23 21:59:45 -05001135 goto release_tpsram;
Divy Le Ray47330072007-08-29 19:15:52 -07001136
1137 ret = t3_set_proto_sram(adap, tpsram->data);
1138 if (ret == 0)
1139 dev_info(dev,
1140 "successful update of protocol engine "
1141 "to %d.%d.%d\n",
1142 TP_VERSION_MAJOR, TP_VERSION_MINOR, TP_VERSION_MICRO);
1143 else
1144 dev_err(dev, "failed to update of protocol engine %d.%d.%d\n",
1145 TP_VERSION_MAJOR, TP_VERSION_MINOR, TP_VERSION_MICRO);
1146 if (ret)
1147 dev_err(dev, "loading protocol SRAM failed\n");
1148
1149release_tpsram:
1150 release_firmware(tpsram);
Jeff Garzik2eab17a2007-11-23 21:59:45 -05001151
Divy Le Ray2e283962007-03-18 13:10:06 -07001152 return ret;
1153}
1154
Divy Le Ray4d22de32007-01-18 22:04:14 -05001155/**
Roland Dreier60158e62012-04-30 07:15:47 +00001156 * t3_synchronize_rx - wait for current Rx processing on a port to complete
1157 * @adap: the adapter
1158 * @p: the port
1159 *
1160 * Ensures that current Rx processing on any of the queues associated with
1161 * the given port completes before returning. We do this by acquiring and
1162 * releasing the locks of the response queues associated with the port.
1163 */
1164static void t3_synchronize_rx(struct adapter *adap, const struct port_info *p)
1165{
1166 int i;
1167
1168 for (i = p->first_qset; i < p->first_qset + p->nqsets; i++) {
1169 struct sge_rspq *q = &adap->sge.qs[i].rspq;
1170
1171 spin_lock_irq(&q->lock);
1172 spin_unlock_irq(&q->lock);
1173 }
1174}
1175
1176static void cxgb_vlan_mode(struct net_device *dev, netdev_features_t features)
1177{
1178 struct port_info *pi = netdev_priv(dev);
1179 struct adapter *adapter = pi->adapter;
1180
1181 if (adapter->params.rev > 0) {
1182 t3_set_vlan_accel(adapter, 1 << pi->port_id,
Patrick McHardyf6469682013-04-19 02:04:27 +00001183 features & NETIF_F_HW_VLAN_CTAG_RX);
Roland Dreier60158e62012-04-30 07:15:47 +00001184 } else {
1185 /* single control for all ports */
Patrick McHardyf6469682013-04-19 02:04:27 +00001186 unsigned int i, have_vlans = features & NETIF_F_HW_VLAN_CTAG_RX;
Roland Dreier60158e62012-04-30 07:15:47 +00001187
1188 for_each_port(adapter, i)
1189 have_vlans |=
Patrick McHardyf6469682013-04-19 02:04:27 +00001190 adapter->port[i]->features &
1191 NETIF_F_HW_VLAN_CTAG_RX;
Roland Dreier60158e62012-04-30 07:15:47 +00001192
1193 t3_set_vlan_accel(adapter, 1, have_vlans);
1194 }
1195 t3_synchronize_rx(adapter, pi);
1196}
1197
1198/**
Divy Le Ray4d22de32007-01-18 22:04:14 -05001199 * cxgb_up - enable the adapter
1200 * @adapter: adapter being enabled
1201 *
1202 * Called when the first port is enabled, this function performs the
1203 * actions necessary to make an adapter operational, such as completing
1204 * the initialization of HW modules, and enabling interrupts.
1205 *
1206 * Must be called with the rtnl lock held.
1207 */
1208static int cxgb_up(struct adapter *adap)
1209{
Roland Dreier60158e62012-04-30 07:15:47 +00001210 int i, err;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001211
1212 if (!(adap->flags & FULL_INIT_DONE)) {
Divy Le Ray8207bef2008-12-16 01:51:47 -08001213 err = t3_check_fw_version(adap);
Divy Le Raya5a3b462007-09-05 15:58:09 -07001214 if (err == -EINVAL) {
Divy Le Ray2e283962007-03-18 13:10:06 -07001215 err = upgrade_fw(adap);
Divy Le Ray8207bef2008-12-16 01:51:47 -08001216 CH_WARN(adap, "FW upgrade to %d.%d.%d %s\n",
1217 FW_VERSION_MAJOR, FW_VERSION_MINOR,
1218 FW_VERSION_MICRO, err ? "failed" : "succeeded");
Divy Le Raya5a3b462007-09-05 15:58:09 -07001219 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05001220
Divy Le Ray8207bef2008-12-16 01:51:47 -08001221 err = t3_check_tpsram_version(adap);
Divy Le Ray47330072007-08-29 19:15:52 -07001222 if (err == -EINVAL) {
1223 err = update_tpsram(adap);
Divy Le Ray8207bef2008-12-16 01:51:47 -08001224 CH_WARN(adap, "TP upgrade to %d.%d.%d %s\n",
1225 TP_VERSION_MAJOR, TP_VERSION_MINOR,
1226 TP_VERSION_MICRO, err ? "failed" : "succeeded");
Divy Le Ray47330072007-08-29 19:15:52 -07001227 }
1228
Divy Le Ray20d3fc12008-10-08 17:36:03 -07001229 /*
1230 * Clear interrupts now to catch errors if t3_init_hw fails.
1231 * We clear them again later as initialization may trigger
1232 * conditions that can interrupt.
1233 */
1234 t3_intr_clear(adap);
1235
Divy Le Ray4d22de32007-01-18 22:04:14 -05001236 err = t3_init_hw(adap, 0);
1237 if (err)
1238 goto out;
1239
Divy Le Rayb8819552007-12-17 18:47:31 -08001240 t3_set_reg_field(adap, A_TP_PARA_REG5, 0, F_RXDDPOFFINIT);
Divy Le Ray6cdbd772007-04-09 20:10:33 -07001241 t3_write_reg(adap, A_ULPRX_TDDP_PSZ, V_HPZ0(PAGE_SHIFT - 12));
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001242
Divy Le Ray4d22de32007-01-18 22:04:14 -05001243 err = setup_sge_qsets(adap);
1244 if (err)
1245 goto out;
1246
Roland Dreier60158e62012-04-30 07:15:47 +00001247 for_each_port(adap, i)
1248 cxgb_vlan_mode(adap->port[i], adap->port[i]->features);
1249
Divy Le Ray4d22de32007-01-18 22:04:14 -05001250 setup_rss(adap);
Divy Le Ray48c4b6d2008-05-06 19:25:56 -07001251 if (!(adap->flags & NAPI_INIT))
1252 init_napi(adap);
Divy Le Ray31563782009-03-26 16:39:09 +00001253
1254 t3_start_sge_timers(adap);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001255 adap->flags |= FULL_INIT_DONE;
1256 }
1257
1258 t3_intr_clear(adap);
1259
1260 if (adap->flags & USING_MSIX) {
1261 name_msix_vecs(adap);
1262 err = request_irq(adap->msix_info[0].vec,
1263 t3_async_intr_handler, 0,
1264 adap->msix_info[0].desc, adap);
1265 if (err)
1266 goto irq_err;
1267
Divy Le Ray42256f52007-11-16 11:21:39 -08001268 err = request_msix_data_irqs(adap);
1269 if (err) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05001270 free_irq(adap->msix_info[0].vec, adap);
1271 goto irq_err;
1272 }
1273 } else if ((err = request_irq(adap->pdev->irq,
1274 t3_intr_handler(adap,
1275 adap->sge.qs[0].rspq.
1276 polling),
Thomas Gleixner2db63462007-02-14 00:33:20 -08001277 (adap->flags & USING_MSI) ?
1278 0 : IRQF_SHARED,
Divy Le Ray4d22de32007-01-18 22:04:14 -05001279 adap->name, adap)))
1280 goto irq_err;
1281
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001282 enable_all_napi(adap);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001283 t3_sge_start(adap);
1284 t3_intr_enable(adap);
Divy Le Ray14ab9892007-01-30 19:43:50 -08001285
Divy Le Rayb8819552007-12-17 18:47:31 -08001286 if (adap->params.rev >= T3_REV_C && !(adap->flags & TP_PARITY_INIT) &&
1287 is_offload(adap) && init_tp_parity(adap) == 0)
1288 adap->flags |= TP_PARITY_INIT;
1289
1290 if (adap->flags & TP_PARITY_INIT) {
1291 t3_write_reg(adap, A_TP_INT_CAUSE,
1292 F_CMCACHEPERR | F_ARPLUTPERR);
1293 t3_write_reg(adap, A_TP_INT_ENABLE, 0x7fbfffff);
1294 }
1295
Divy Le Ray8c263762008-10-08 17:37:33 -07001296 if (!(adap->flags & QUEUES_BOUND)) {
Divy Le Ray18edc842010-10-25 07:35:02 +00001297 int ret = bind_qsets(adap);
1298
1299 if (ret < 0) {
1300 CH_ERR(adap, "failed to bind qsets, err %d\n", ret);
Divy Le Ray8c263762008-10-08 17:37:33 -07001301 t3_intr_disable(adap);
1302 free_irq_resources(adap);
Divy Le Ray18edc842010-10-25 07:35:02 +00001303 err = ret;
Divy Le Ray8c263762008-10-08 17:37:33 -07001304 goto out;
1305 }
1306 adap->flags |= QUEUES_BOUND;
1307 }
Divy Le Ray14ab9892007-01-30 19:43:50 -08001308
Divy Le Ray4d22de32007-01-18 22:04:14 -05001309out:
1310 return err;
1311irq_err:
1312 CH_ERR(adap, "request_irq failed, err %d\n", err);
1313 goto out;
1314}
1315
1316/*
1317 * Release resources when all the ports and offloading have been stopped.
1318 */
Casey Leedom55bc3222010-09-02 13:07:32 +00001319static void cxgb_down(struct adapter *adapter, int on_wq)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001320{
1321 t3_sge_stop(adapter);
1322 spin_lock_irq(&adapter->work_lock); /* sync with PHY intr task */
1323 t3_intr_disable(adapter);
1324 spin_unlock_irq(&adapter->work_lock);
1325
Divy Le Ray8c263762008-10-08 17:37:33 -07001326 free_irq_resources(adapter);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001327 quiesce_rx(adapter);
Divy Le Raya6f018e2010-03-03 09:49:47 +00001328 t3_sge_stop(adapter);
Casey Leedom55bc3222010-09-02 13:07:32 +00001329 if (!on_wq)
1330 flush_workqueue(cxgb3_wq);/* wait for external IRQ handler */
Divy Le Ray4d22de32007-01-18 22:04:14 -05001331}
1332
1333static void schedule_chk_task(struct adapter *adap)
1334{
1335 unsigned int timeo;
1336
1337 timeo = adap->params.linkpoll_period ?
1338 (HZ * adap->params.linkpoll_period) / 10 :
1339 adap->params.stats_update_period * HZ;
1340 if (timeo)
1341 queue_delayed_work(cxgb3_wq, &adap->adap_check_task, timeo);
1342}
1343
1344static int offload_open(struct net_device *dev)
1345{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001346 struct port_info *pi = netdev_priv(dev);
1347 struct adapter *adapter = pi->adapter;
1348 struct t3cdev *tdev = dev2t3cdev(dev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001349 int adap_up = adapter->open_device_map & PORT_MASK;
Denis Chengc54f5c22007-07-18 15:24:49 +08001350 int err;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001351
1352 if (test_and_set_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map))
1353 return 0;
1354
1355 if (!adap_up && (err = cxgb_up(adapter)) < 0)
Divy Le Ray48c4b6d2008-05-06 19:25:56 -07001356 goto out;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001357
1358 t3_tp_set_offload_mode(adapter, 1);
1359 tdev->lldev = adapter->port[0];
1360 err = cxgb3_offload_activate(adapter);
1361 if (err)
1362 goto out;
1363
1364 init_port_mtus(adapter);
1365 t3_load_mtus(adapter, adapter->params.mtus, adapter->params.a_wnd,
1366 adapter->params.b_wnd,
1367 adapter->params.rev == 0 ?
1368 adapter->port[0]->mtu : 0xffff);
1369 init_smt(adapter);
1370
Dan Noed96a51f2008-04-12 22:34:38 -04001371 if (sysfs_create_group(&tdev->lldev->dev.kobj, &offload_attr_group))
1372 dev_dbg(&dev->dev, "cannot create sysfs group\n");
Divy Le Ray4d22de32007-01-18 22:04:14 -05001373
1374 /* Call back all registered clients */
1375 cxgb3_add_clients(tdev);
1376
1377out:
1378 /* restore them in case the offload module has changed them */
1379 if (err) {
1380 t3_tp_set_offload_mode(adapter, 0);
1381 clear_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map);
1382 cxgb3_set_dummy_ops(tdev);
1383 }
1384 return err;
1385}
1386
1387static int offload_close(struct t3cdev *tdev)
1388{
1389 struct adapter *adapter = tdev2adap(tdev);
Tejun Heo23f333a2010-12-12 16:45:14 +01001390 struct t3c_data *td = T3C_DATA(tdev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001391
1392 if (!test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map))
1393 return 0;
1394
1395 /* Call back all registered clients */
1396 cxgb3_remove_clients(tdev);
1397
Divy Le Ray0ee8d332007-02-08 16:55:59 -08001398 sysfs_remove_group(&tdev->lldev->dev.kobj, &offload_attr_group);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001399
Divy Le Rayc80b0c22009-04-17 12:21:17 +00001400 /* Flush work scheduled while releasing TIDs */
Tejun Heo43829732012-08-20 14:51:24 -07001401 flush_work(&td->tid_release_task);
Divy Le Rayc80b0c22009-04-17 12:21:17 +00001402
Divy Le Ray4d22de32007-01-18 22:04:14 -05001403 tdev->lldev = NULL;
1404 cxgb3_set_dummy_ops(tdev);
1405 t3_tp_set_offload_mode(adapter, 0);
1406 clear_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map);
1407
1408 if (!adapter->open_device_map)
Casey Leedom55bc3222010-09-02 13:07:32 +00001409 cxgb_down(adapter, 0);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001410
1411 cxgb3_offload_deactivate(adapter);
1412 return 0;
1413}
1414
1415static int cxgb_open(struct net_device *dev)
1416{
Divy Le Ray4d22de32007-01-18 22:04:14 -05001417 struct port_info *pi = netdev_priv(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001418 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001419 int other_ports = adapter->open_device_map & PORT_MASK;
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001420 int err;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001421
Divy Le Ray48c4b6d2008-05-06 19:25:56 -07001422 if (!adapter->open_device_map && (err = cxgb_up(adapter)) < 0)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001423 return err;
1424
1425 set_bit(pi->port_id, &adapter->open_device_map);
Divy Le Ray8ac3ba62007-03-31 00:23:19 -07001426 if (is_offload(adapter) && !ofld_disable) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05001427 err = offload_open(dev);
1428 if (err)
Joe Perches428ac432013-01-06 13:34:49 +00001429 pr_warn("Could not initialize offload capabilities\n");
Divy Le Ray4d22de32007-01-18 22:04:14 -05001430 }
1431
Ben Hutchings19221e72010-09-27 08:25:44 +00001432 netif_set_real_num_tx_queues(dev, pi->nqsets);
1433 err = netif_set_real_num_rx_queues(dev, pi->nqsets);
1434 if (err)
1435 return err;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001436 link_start(dev);
1437 t3_port_intr_enable(adapter, pi->port_id);
Divy Le Ray82ad3322008-12-16 01:09:39 -08001438 netif_tx_start_all_queues(dev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001439 if (!other_ports)
1440 schedule_chk_task(adapter);
1441
Steve Wisefa0d4c12009-09-05 20:22:38 -07001442 cxgb3_event_notify(&adapter->tdev, OFFLOAD_PORT_UP, pi->port_id);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001443 return 0;
1444}
1445
Casey Leedom55bc3222010-09-02 13:07:32 +00001446static int __cxgb_close(struct net_device *dev, int on_wq)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001447{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001448 struct port_info *pi = netdev_priv(dev);
1449 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001450
Divy Le Raye8d19372009-04-17 12:21:27 +00001451
1452 if (!adapter->open_device_map)
1453 return 0;
1454
Divy Le Raybf792092009-03-12 21:14:19 +00001455 /* Stop link fault interrupts */
1456 t3_xgm_intr_disable(adapter, pi->port_id);
1457 t3_read_reg(adapter, A_XGM_INT_STATUS + pi->mac.offset);
1458
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001459 t3_port_intr_disable(adapter, pi->port_id);
Divy Le Ray82ad3322008-12-16 01:09:39 -08001460 netif_tx_stop_all_queues(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001461 pi->phy.ops->power_down(&pi->phy, 1);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001462 netif_carrier_off(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001463 t3_mac_disable(&pi->mac, MAC_DIRECTION_TX | MAC_DIRECTION_RX);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001464
Divy Le Ray20d3fc12008-10-08 17:36:03 -07001465 spin_lock_irq(&adapter->work_lock); /* sync with update task */
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001466 clear_bit(pi->port_id, &adapter->open_device_map);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07001467 spin_unlock_irq(&adapter->work_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001468
1469 if (!(adapter->open_device_map & PORT_MASK))
Divy Le Rayc80b0c22009-04-17 12:21:17 +00001470 cancel_delayed_work_sync(&adapter->adap_check_task);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001471
1472 if (!adapter->open_device_map)
Casey Leedom55bc3222010-09-02 13:07:32 +00001473 cxgb_down(adapter, on_wq);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001474
Steve Wisefa0d4c12009-09-05 20:22:38 -07001475 cxgb3_event_notify(&adapter->tdev, OFFLOAD_PORT_DOWN, pi->port_id);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001476 return 0;
1477}
1478
Casey Leedom55bc3222010-09-02 13:07:32 +00001479static int cxgb_close(struct net_device *dev)
1480{
1481 return __cxgb_close(dev, 0);
1482}
1483
Divy Le Ray4d22de32007-01-18 22:04:14 -05001484static struct net_device_stats *cxgb_get_stats(struct net_device *dev)
1485{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001486 struct port_info *pi = netdev_priv(dev);
1487 struct adapter *adapter = pi->adapter;
Tobias Klausera73be7f2017-04-07 10:17:27 +02001488 struct net_device_stats *ns = &dev->stats;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001489 const struct mac_stats *pstats;
1490
1491 spin_lock(&adapter->stats_lock);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001492 pstats = t3_mac_update_stats(&pi->mac);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001493 spin_unlock(&adapter->stats_lock);
1494
1495 ns->tx_bytes = pstats->tx_octets;
1496 ns->tx_packets = pstats->tx_frames;
1497 ns->rx_bytes = pstats->rx_octets;
1498 ns->rx_packets = pstats->rx_frames;
1499 ns->multicast = pstats->rx_mcast_frames;
1500
1501 ns->tx_errors = pstats->tx_underrun;
1502 ns->rx_errors = pstats->rx_symbol_errs + pstats->rx_fcs_errs +
1503 pstats->rx_too_long + pstats->rx_jabber + pstats->rx_short +
1504 pstats->rx_fifo_ovfl;
1505
1506 /* detailed rx_errors */
1507 ns->rx_length_errors = pstats->rx_jabber + pstats->rx_too_long;
1508 ns->rx_over_errors = 0;
1509 ns->rx_crc_errors = pstats->rx_fcs_errs;
1510 ns->rx_frame_errors = pstats->rx_symbol_errs;
1511 ns->rx_fifo_errors = pstats->rx_fifo_ovfl;
1512 ns->rx_missed_errors = pstats->rx_cong_drops;
1513
1514 /* detailed tx_errors */
1515 ns->tx_aborted_errors = 0;
1516 ns->tx_carrier_errors = 0;
1517 ns->tx_fifo_errors = pstats->tx_underrun;
1518 ns->tx_heartbeat_errors = 0;
1519 ns->tx_window_errors = 0;
1520 return ns;
1521}
1522
1523static u32 get_msglevel(struct net_device *dev)
1524{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001525 struct port_info *pi = netdev_priv(dev);
1526 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001527
1528 return adapter->msg_enable;
1529}
1530
1531static void set_msglevel(struct net_device *dev, u32 val)
1532{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001533 struct port_info *pi = netdev_priv(dev);
1534 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001535
1536 adapter->msg_enable = val;
1537}
1538
Joe Perches9ca683c2015-04-01 08:49:24 -07001539static const char stats_strings[][ETH_GSTRING_LEN] = {
Divy Le Ray4d22de32007-01-18 22:04:14 -05001540 "TxOctetsOK ",
1541 "TxFramesOK ",
1542 "TxMulticastFramesOK",
1543 "TxBroadcastFramesOK",
1544 "TxPauseFrames ",
1545 "TxUnderrun ",
1546 "TxExtUnderrun ",
1547
1548 "TxFrames64 ",
1549 "TxFrames65To127 ",
1550 "TxFrames128To255 ",
1551 "TxFrames256To511 ",
1552 "TxFrames512To1023 ",
1553 "TxFrames1024To1518 ",
1554 "TxFrames1519ToMax ",
1555
1556 "RxOctetsOK ",
1557 "RxFramesOK ",
1558 "RxMulticastFramesOK",
1559 "RxBroadcastFramesOK",
1560 "RxPauseFrames ",
1561 "RxFCSErrors ",
1562 "RxSymbolErrors ",
1563 "RxShortErrors ",
1564 "RxJabberErrors ",
1565 "RxLengthErrors ",
1566 "RxFIFOoverflow ",
1567
1568 "RxFrames64 ",
1569 "RxFrames65To127 ",
1570 "RxFrames128To255 ",
1571 "RxFrames256To511 ",
1572 "RxFrames512To1023 ",
1573 "RxFrames1024To1518 ",
1574 "RxFrames1519ToMax ",
1575
1576 "PhyFIFOErrors ",
1577 "TSO ",
1578 "VLANextractions ",
1579 "VLANinsertions ",
1580 "TxCsumOffload ",
1581 "RxCsumGood ",
Divy Le Rayb47385b2008-05-21 18:56:26 -07001582 "LroAggregated ",
1583 "LroFlushed ",
1584 "LroNoDesc ",
Divy Le Rayfc906642007-03-18 13:10:12 -07001585 "RxDrops ",
1586
1587 "CheckTXEnToggled ",
1588 "CheckResets ",
1589
Divy Le Raybf792092009-03-12 21:14:19 +00001590 "LinkFaults ",
Divy Le Ray4d22de32007-01-18 22:04:14 -05001591};
1592
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001593static int get_sset_count(struct net_device *dev, int sset)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001594{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001595 switch (sset) {
1596 case ETH_SS_STATS:
1597 return ARRAY_SIZE(stats_strings);
1598 default:
1599 return -EOPNOTSUPP;
1600 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05001601}
1602
1603#define T3_REGMAP_SIZE (3 * 1024)
1604
1605static int get_regs_len(struct net_device *dev)
1606{
1607 return T3_REGMAP_SIZE;
1608}
1609
1610static int get_eeprom_len(struct net_device *dev)
1611{
1612 return EEPROMSIZE;
1613}
1614
1615static void get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1616{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001617 struct port_info *pi = netdev_priv(dev);
1618 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001619 u32 fw_vers = 0;
Divy Le Ray47330072007-08-29 19:15:52 -07001620 u32 tp_vers = 0;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001621
Steve Wisecf3760d2008-11-06 17:06:42 -06001622 spin_lock(&adapter->stats_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001623 t3_get_fw_version(adapter, &fw_vers);
Divy Le Ray47330072007-08-29 19:15:52 -07001624 t3_get_tp_version(adapter, &tp_vers);
Steve Wisecf3760d2008-11-06 17:06:42 -06001625 spin_unlock(&adapter->stats_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001626
Rick Jones23020ab2011-11-09 09:58:07 +00001627 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1628 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1629 strlcpy(info->bus_info, pci_name(adapter->pdev),
1630 sizeof(info->bus_info));
Rick Jones84b40502011-11-21 10:54:05 +00001631 if (fw_vers)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001632 snprintf(info->fw_version, sizeof(info->fw_version),
Divy Le Ray47330072007-08-29 19:15:52 -07001633 "%s %u.%u.%u TP %u.%u.%u",
Divy Le Ray4aac3892007-01-30 19:43:45 -08001634 G_FW_VERSION_TYPE(fw_vers) ? "T" : "N",
1635 G_FW_VERSION_MAJOR(fw_vers),
1636 G_FW_VERSION_MINOR(fw_vers),
Divy Le Ray47330072007-08-29 19:15:52 -07001637 G_FW_VERSION_MICRO(fw_vers),
1638 G_TP_VERSION_MAJOR(tp_vers),
1639 G_TP_VERSION_MINOR(tp_vers),
1640 G_TP_VERSION_MICRO(tp_vers));
Divy Le Ray4d22de32007-01-18 22:04:14 -05001641}
1642
1643static void get_strings(struct net_device *dev, u32 stringset, u8 * data)
1644{
1645 if (stringset == ETH_SS_STATS)
1646 memcpy(data, stats_strings, sizeof(stats_strings));
1647}
1648
1649static unsigned long collect_sge_port_stats(struct adapter *adapter,
1650 struct port_info *p, int idx)
1651{
1652 int i;
1653 unsigned long tot = 0;
1654
Divy Le Ray8c263762008-10-08 17:37:33 -07001655 for (i = p->first_qset; i < p->first_qset + p->nqsets; ++i)
1656 tot += adapter->sge.qs[i].port_stats[idx];
Divy Le Ray4d22de32007-01-18 22:04:14 -05001657 return tot;
1658}
1659
1660static void get_stats(struct net_device *dev, struct ethtool_stats *stats,
1661 u64 *data)
1662{
Divy Le Ray4d22de32007-01-18 22:04:14 -05001663 struct port_info *pi = netdev_priv(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001664 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001665 const struct mac_stats *s;
1666
1667 spin_lock(&adapter->stats_lock);
1668 s = t3_mac_update_stats(&pi->mac);
1669 spin_unlock(&adapter->stats_lock);
1670
1671 *data++ = s->tx_octets;
1672 *data++ = s->tx_frames;
1673 *data++ = s->tx_mcast_frames;
1674 *data++ = s->tx_bcast_frames;
1675 *data++ = s->tx_pause;
1676 *data++ = s->tx_underrun;
1677 *data++ = s->tx_fifo_urun;
1678
1679 *data++ = s->tx_frames_64;
1680 *data++ = s->tx_frames_65_127;
1681 *data++ = s->tx_frames_128_255;
1682 *data++ = s->tx_frames_256_511;
1683 *data++ = s->tx_frames_512_1023;
1684 *data++ = s->tx_frames_1024_1518;
1685 *data++ = s->tx_frames_1519_max;
1686
1687 *data++ = s->rx_octets;
1688 *data++ = s->rx_frames;
1689 *data++ = s->rx_mcast_frames;
1690 *data++ = s->rx_bcast_frames;
1691 *data++ = s->rx_pause;
1692 *data++ = s->rx_fcs_errs;
1693 *data++ = s->rx_symbol_errs;
1694 *data++ = s->rx_short;
1695 *data++ = s->rx_jabber;
1696 *data++ = s->rx_too_long;
1697 *data++ = s->rx_fifo_ovfl;
1698
1699 *data++ = s->rx_frames_64;
1700 *data++ = s->rx_frames_65_127;
1701 *data++ = s->rx_frames_128_255;
1702 *data++ = s->rx_frames_256_511;
1703 *data++ = s->rx_frames_512_1023;
1704 *data++ = s->rx_frames_1024_1518;
1705 *data++ = s->rx_frames_1519_max;
1706
1707 *data++ = pi->phy.fifo_errors;
1708
1709 *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_TSO);
1710 *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_VLANEX);
1711 *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_VLANINS);
1712 *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_TX_CSUM);
1713 *data++ = collect_sge_port_stats(adapter, pi, SGE_PSTAT_RX_CSUM_GOOD);
Herbert Xu7be2df42009-01-21 14:39:13 -08001714 *data++ = 0;
1715 *data++ = 0;
1716 *data++ = 0;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001717 *data++ = s->rx_cong_drops;
Divy Le Rayfc906642007-03-18 13:10:12 -07001718
1719 *data++ = s->num_toggled;
1720 *data++ = s->num_resets;
Divy Le Raybf792092009-03-12 21:14:19 +00001721
1722 *data++ = s->link_faults;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001723}
1724
1725static inline void reg_block_dump(struct adapter *ap, void *buf,
1726 unsigned int start, unsigned int end)
1727{
1728 u32 *p = buf + start;
1729
1730 for (; start <= end; start += sizeof(u32))
1731 *p++ = t3_read_reg(ap, start);
1732}
1733
1734static void get_regs(struct net_device *dev, struct ethtool_regs *regs,
1735 void *buf)
1736{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001737 struct port_info *pi = netdev_priv(dev);
1738 struct adapter *ap = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001739
1740 /*
1741 * Version scheme:
1742 * bits 0..9: chip version
1743 * bits 10..15: chip revision
1744 * bit 31: set for PCIe cards
1745 */
1746 regs->version = 3 | (ap->params.rev << 10) | (is_pcie(ap) << 31);
1747
1748 /*
1749 * We skip the MAC statistics registers because they are clear-on-read.
1750 * Also reading multi-register stats would need to synchronize with the
1751 * periodic mac stats accumulation. Hard to justify the complexity.
1752 */
1753 memset(buf, 0, T3_REGMAP_SIZE);
1754 reg_block_dump(ap, buf, 0, A_SG_RSPQ_CREDIT_RETURN);
1755 reg_block_dump(ap, buf, A_SG_HI_DRB_HI_THRSH, A_ULPRX_PBL_ULIMIT);
1756 reg_block_dump(ap, buf, A_ULPTX_CONFIG, A_MPS_INT_CAUSE);
1757 reg_block_dump(ap, buf, A_CPL_SWITCH_CNTRL, A_CPL_MAP_TBL_DATA);
1758 reg_block_dump(ap, buf, A_SMB_GLOBAL_TIME_CFG, A_XGM_SERDES_STAT3);
1759 reg_block_dump(ap, buf, A_XGM_SERDES_STATUS0,
1760 XGM_REG(A_XGM_SERDES_STAT3, 1));
1761 reg_block_dump(ap, buf, XGM_REG(A_XGM_SERDES_STATUS0, 1),
1762 XGM_REG(A_XGM_RX_SPI4_SOP_EOP_CNT, 1));
1763}
1764
1765static int restart_autoneg(struct net_device *dev)
1766{
1767 struct port_info *p = netdev_priv(dev);
1768
1769 if (!netif_running(dev))
1770 return -EAGAIN;
1771 if (p->link_config.autoneg != AUTONEG_ENABLE)
1772 return -EINVAL;
1773 p->phy.ops->autoneg_restart(&p->phy);
1774 return 0;
1775}
1776
stephen hemminger12fcf942011-04-04 08:43:51 +00001777static int set_phys_id(struct net_device *dev,
1778 enum ethtool_phys_id_state state)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001779{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001780 struct port_info *pi = netdev_priv(dev);
1781 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001782
stephen hemminger12fcf942011-04-04 08:43:51 +00001783 switch (state) {
1784 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001785 return 1; /* cycle on/off once per second */
Divy Le Ray4d22de32007-01-18 22:04:14 -05001786
stephen hemminger12fcf942011-04-04 08:43:51 +00001787 case ETHTOOL_ID_OFF:
1788 t3_set_reg_field(adapter, A_T3DBG_GPIO_EN, F_GPIO0_OUT_VAL, 0);
1789 break;
1790
1791 case ETHTOOL_ID_ON:
1792 case ETHTOOL_ID_INACTIVE:
Divy Le Ray4d22de32007-01-18 22:04:14 -05001793 t3_set_reg_field(adapter, A_T3DBG_GPIO_EN, F_GPIO0_OUT_VAL,
Divy Le Ray4d22de32007-01-18 22:04:14 -05001794 F_GPIO0_OUT_VAL);
stephen hemminger12fcf942011-04-04 08:43:51 +00001795 }
1796
Divy Le Ray4d22de32007-01-18 22:04:14 -05001797 return 0;
1798}
1799
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001800static int get_link_ksettings(struct net_device *dev,
1801 struct ethtool_link_ksettings *cmd)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001802{
1803 struct port_info *p = netdev_priv(dev);
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001804 u32 supported;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001805
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001806 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1807 p->link_config.supported);
1808 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1809 p->link_config.advertising);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001810
1811 if (netif_carrier_ok(dev)) {
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001812 cmd->base.speed = p->link_config.speed;
1813 cmd->base.duplex = p->link_config.duplex;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001814 } else {
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001815 cmd->base.speed = SPEED_UNKNOWN;
1816 cmd->base.duplex = DUPLEX_UNKNOWN;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001817 }
1818
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001819 ethtool_convert_link_mode_to_legacy_u32(&supported,
1820 cmd->link_modes.supported);
1821
1822 cmd->base.port = (supported & SUPPORTED_TP) ? PORT_TP : PORT_FIBRE;
1823 cmd->base.phy_address = p->phy.mdio.prtad;
1824 cmd->base.autoneg = p->link_config.autoneg;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001825 return 0;
1826}
1827
1828static int speed_duplex_to_caps(int speed, int duplex)
1829{
1830 int cap = 0;
1831
1832 switch (speed) {
1833 case SPEED_10:
1834 if (duplex == DUPLEX_FULL)
1835 cap = SUPPORTED_10baseT_Full;
1836 else
1837 cap = SUPPORTED_10baseT_Half;
1838 break;
1839 case SPEED_100:
1840 if (duplex == DUPLEX_FULL)
1841 cap = SUPPORTED_100baseT_Full;
1842 else
1843 cap = SUPPORTED_100baseT_Half;
1844 break;
1845 case SPEED_1000:
1846 if (duplex == DUPLEX_FULL)
1847 cap = SUPPORTED_1000baseT_Full;
1848 else
1849 cap = SUPPORTED_1000baseT_Half;
1850 break;
1851 case SPEED_10000:
1852 if (duplex == DUPLEX_FULL)
1853 cap = SUPPORTED_10000baseT_Full;
1854 }
1855 return cap;
1856}
1857
1858#define ADVERTISED_MASK (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | \
1859 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | \
1860 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full | \
1861 ADVERTISED_10000baseT_Full)
1862
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001863static int set_link_ksettings(struct net_device *dev,
1864 const struct ethtool_link_ksettings *cmd)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001865{
1866 struct port_info *p = netdev_priv(dev);
1867 struct link_config *lc = &p->link_config;
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001868 u32 advertising;
1869
1870 ethtool_convert_link_mode_to_legacy_u32(&advertising,
1871 cmd->link_modes.advertising);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001872
Divy Le Ray9b1e3652008-10-08 17:39:31 -07001873 if (!(lc->supported & SUPPORTED_Autoneg)) {
1874 /*
1875 * PHY offers a single speed/duplex. See if that's what's
1876 * being requested.
1877 */
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001878 if (cmd->base.autoneg == AUTONEG_DISABLE) {
1879 u32 speed = cmd->base.speed;
1880 int cap = speed_duplex_to_caps(speed, cmd->base.duplex);
Divy Le Ray9b1e3652008-10-08 17:39:31 -07001881 if (lc->supported & cap)
1882 return 0;
1883 }
1884 return -EINVAL;
1885 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05001886
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001887 if (cmd->base.autoneg == AUTONEG_DISABLE) {
1888 u32 speed = cmd->base.speed;
1889 int cap = speed_duplex_to_caps(speed, cmd->base.duplex);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001890
David Decotigny25db0332011-04-27 18:32:39 +00001891 if (!(lc->supported & cap) || (speed == SPEED_1000))
Divy Le Ray4d22de32007-01-18 22:04:14 -05001892 return -EINVAL;
David Decotigny25db0332011-04-27 18:32:39 +00001893 lc->requested_speed = speed;
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001894 lc->requested_duplex = cmd->base.duplex;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001895 lc->advertising = 0;
1896 } else {
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001897 advertising &= ADVERTISED_MASK;
1898 advertising &= lc->supported;
1899 if (!advertising)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001900 return -EINVAL;
1901 lc->requested_speed = SPEED_INVALID;
1902 lc->requested_duplex = DUPLEX_INVALID;
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001903 lc->advertising = advertising | ADVERTISED_Autoneg;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001904 }
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01001905 lc->autoneg = cmd->base.autoneg;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001906 if (netif_running(dev))
1907 t3_link_start(&p->phy, &p->mac, lc);
1908 return 0;
1909}
1910
1911static void get_pauseparam(struct net_device *dev,
1912 struct ethtool_pauseparam *epause)
1913{
1914 struct port_info *p = netdev_priv(dev);
1915
1916 epause->autoneg = (p->link_config.requested_fc & PAUSE_AUTONEG) != 0;
1917 epause->rx_pause = (p->link_config.fc & PAUSE_RX) != 0;
1918 epause->tx_pause = (p->link_config.fc & PAUSE_TX) != 0;
1919}
1920
1921static int set_pauseparam(struct net_device *dev,
1922 struct ethtool_pauseparam *epause)
1923{
1924 struct port_info *p = netdev_priv(dev);
1925 struct link_config *lc = &p->link_config;
1926
1927 if (epause->autoneg == AUTONEG_DISABLE)
1928 lc->requested_fc = 0;
1929 else if (lc->supported & SUPPORTED_Autoneg)
1930 lc->requested_fc = PAUSE_AUTONEG;
1931 else
1932 return -EINVAL;
1933
1934 if (epause->rx_pause)
1935 lc->requested_fc |= PAUSE_RX;
1936 if (epause->tx_pause)
1937 lc->requested_fc |= PAUSE_TX;
1938 if (lc->autoneg == AUTONEG_ENABLE) {
1939 if (netif_running(dev))
1940 t3_link_start(&p->phy, &p->mac, lc);
1941 } else {
1942 lc->fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
1943 if (netif_running(dev))
1944 t3_mac_set_speed_duplex_fc(&p->mac, -1, -1, lc->fc);
1945 }
1946 return 0;
1947}
1948
Divy Le Ray4d22de32007-01-18 22:04:14 -05001949static void get_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
1950{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001951 struct port_info *pi = netdev_priv(dev);
1952 struct adapter *adapter = pi->adapter;
Divy Le Ray05b97b32007-03-18 13:10:01 -07001953 const struct qset_params *q = &adapter->params.sge.qset[pi->first_qset];
Divy Le Ray4d22de32007-01-18 22:04:14 -05001954
1955 e->rx_max_pending = MAX_RX_BUFFERS;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001956 e->rx_jumbo_max_pending = MAX_RX_JUMBO_BUFFERS;
1957 e->tx_max_pending = MAX_TXQ_ENTRIES;
1958
Divy Le Ray05b97b32007-03-18 13:10:01 -07001959 e->rx_pending = q->fl_size;
1960 e->rx_mini_pending = q->rspq_size;
1961 e->rx_jumbo_pending = q->jumbo_size;
1962 e->tx_pending = q->txq_size[0];
Divy Le Ray4d22de32007-01-18 22:04:14 -05001963}
1964
1965static int set_sge_param(struct net_device *dev, struct ethtool_ringparam *e)
1966{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001967 struct port_info *pi = netdev_priv(dev);
1968 struct adapter *adapter = pi->adapter;
Divy Le Ray05b97b32007-03-18 13:10:01 -07001969 struct qset_params *q;
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001970 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001971
1972 if (e->rx_pending > MAX_RX_BUFFERS ||
1973 e->rx_jumbo_pending > MAX_RX_JUMBO_BUFFERS ||
1974 e->tx_pending > MAX_TXQ_ENTRIES ||
1975 e->rx_mini_pending > MAX_RSPQ_ENTRIES ||
1976 e->rx_mini_pending < MIN_RSPQ_ENTRIES ||
1977 e->rx_pending < MIN_FL_ENTRIES ||
1978 e->rx_jumbo_pending < MIN_FL_ENTRIES ||
1979 e->tx_pending < adapter->params.nports * MIN_TXQ_ENTRIES)
1980 return -EINVAL;
1981
1982 if (adapter->flags & FULL_INIT_DONE)
1983 return -EBUSY;
1984
Divy Le Ray05b97b32007-03-18 13:10:01 -07001985 q = &adapter->params.sge.qset[pi->first_qset];
1986 for (i = 0; i < pi->nqsets; ++i, ++q) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05001987 q->rspq_size = e->rx_mini_pending;
1988 q->fl_size = e->rx_pending;
1989 q->jumbo_size = e->rx_jumbo_pending;
1990 q->txq_size[0] = e->tx_pending;
1991 q->txq_size[1] = e->tx_pending;
1992 q->txq_size[2] = e->tx_pending;
1993 }
1994 return 0;
1995}
1996
1997static int set_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
1998{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001999 struct port_info *pi = netdev_priv(dev);
2000 struct adapter *adapter = pi->adapter;
Anton Blanchardc211c962011-03-27 16:50:41 +00002001 struct qset_params *qsp;
2002 struct sge_qset *qs;
2003 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002004
2005 if (c->rx_coalesce_usecs * 10 > M_NEWTIMER)
2006 return -EINVAL;
2007
Anton Blanchardc211c962011-03-27 16:50:41 +00002008 for (i = 0; i < pi->nqsets; i++) {
2009 qsp = &adapter->params.sge.qset[i];
2010 qs = &adapter->sge.qs[i];
2011 qsp->coalesce_usecs = c->rx_coalesce_usecs;
2012 t3_update_qset_coalesce(qs, qsp);
2013 }
2014
Divy Le Ray4d22de32007-01-18 22:04:14 -05002015 return 0;
2016}
2017
2018static int get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
2019{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002020 struct port_info *pi = netdev_priv(dev);
2021 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002022 struct qset_params *q = adapter->params.sge.qset;
2023
2024 c->rx_coalesce_usecs = q->coalesce_usecs;
2025 return 0;
2026}
2027
2028static int get_eeprom(struct net_device *dev, struct ethtool_eeprom *e,
2029 u8 * data)
2030{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002031 struct port_info *pi = netdev_priv(dev);
2032 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002033 int i, err = 0;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002034
2035 u8 *buf = kmalloc(EEPROMSIZE, GFP_KERNEL);
2036 if (!buf)
2037 return -ENOMEM;
2038
2039 e->magic = EEPROM_MAGIC;
2040 for (i = e->offset & ~3; !err && i < e->offset + e->len; i += 4)
Al Viro05e5c112007-12-22 18:56:23 +00002041 err = t3_seeprom_read(adapter, i, (__le32 *) & buf[i]);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002042
2043 if (!err)
2044 memcpy(data, buf + e->offset, e->len);
2045 kfree(buf);
2046 return err;
2047}
2048
2049static int set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
2050 u8 * data)
2051{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002052 struct port_info *pi = netdev_priv(dev);
2053 struct adapter *adapter = pi->adapter;
Al Viro05e5c112007-12-22 18:56:23 +00002054 u32 aligned_offset, aligned_len;
2055 __le32 *p;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002056 u8 *buf;
Denis Chengc54f5c22007-07-18 15:24:49 +08002057 int err;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002058
2059 if (eeprom->magic != EEPROM_MAGIC)
2060 return -EINVAL;
2061
2062 aligned_offset = eeprom->offset & ~3;
2063 aligned_len = (eeprom->len + (eeprom->offset & 3) + 3) & ~3;
2064
2065 if (aligned_offset != eeprom->offset || aligned_len != eeprom->len) {
2066 buf = kmalloc(aligned_len, GFP_KERNEL);
2067 if (!buf)
2068 return -ENOMEM;
Al Viro05e5c112007-12-22 18:56:23 +00002069 err = t3_seeprom_read(adapter, aligned_offset, (__le32 *) buf);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002070 if (!err && aligned_len > 4)
2071 err = t3_seeprom_read(adapter,
2072 aligned_offset + aligned_len - 4,
Al Viro05e5c112007-12-22 18:56:23 +00002073 (__le32 *) & buf[aligned_len - 4]);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002074 if (err)
2075 goto out;
2076 memcpy(buf + (eeprom->offset & 3), data, eeprom->len);
2077 } else
2078 buf = data;
2079
2080 err = t3_seeprom_wp(adapter, 0);
2081 if (err)
2082 goto out;
2083
Al Viro05e5c112007-12-22 18:56:23 +00002084 for (p = (__le32 *) buf; !err && aligned_len; aligned_len -= 4, p++) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05002085 err = t3_seeprom_write(adapter, aligned_offset, *p);
2086 aligned_offset += 4;
2087 }
2088
2089 if (!err)
2090 err = t3_seeprom_wp(adapter, 1);
2091out:
2092 if (buf != data)
2093 kfree(buf);
2094 return err;
2095}
2096
2097static void get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2098{
2099 wol->supported = 0;
2100 wol->wolopts = 0;
2101 memset(&wol->sopass, 0, sizeof(wol->sopass));
2102}
2103
2104static const struct ethtool_ops cxgb_ethtool_ops = {
Divy Le Ray4d22de32007-01-18 22:04:14 -05002105 .get_drvinfo = get_drvinfo,
2106 .get_msglevel = get_msglevel,
2107 .set_msglevel = set_msglevel,
2108 .get_ringparam = get_sge_param,
2109 .set_ringparam = set_sge_param,
2110 .get_coalesce = get_coalesce,
2111 .set_coalesce = set_coalesce,
2112 .get_eeprom_len = get_eeprom_len,
2113 .get_eeprom = get_eeprom,
2114 .set_eeprom = set_eeprom,
2115 .get_pauseparam = get_pauseparam,
2116 .set_pauseparam = set_pauseparam,
Divy Le Ray4d22de32007-01-18 22:04:14 -05002117 .get_link = ethtool_op_get_link,
2118 .get_strings = get_strings,
stephen hemminger12fcf942011-04-04 08:43:51 +00002119 .set_phys_id = set_phys_id,
Divy Le Ray4d22de32007-01-18 22:04:14 -05002120 .nway_reset = restart_autoneg,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07002121 .get_sset_count = get_sset_count,
Divy Le Ray4d22de32007-01-18 22:04:14 -05002122 .get_ethtool_stats = get_stats,
2123 .get_regs_len = get_regs_len,
2124 .get_regs = get_regs,
2125 .get_wol = get_wol,
Philippe Reynesb7b44fd2016-12-12 00:27:49 +01002126 .get_link_ksettings = get_link_ksettings,
2127 .set_link_ksettings = set_link_ksettings,
Divy Le Ray4d22de32007-01-18 22:04:14 -05002128};
2129
2130static int in_range(int val, int lo, int hi)
2131{
2132 return val < 0 || (val <= hi && val >= lo);
2133}
2134
2135static int cxgb_extension_ioctl(struct net_device *dev, void __user *useraddr)
2136{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002137 struct port_info *pi = netdev_priv(dev);
2138 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002139 u32 cmd;
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002140 int ret;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002141
2142 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
2143 return -EFAULT;
2144
2145 switch (cmd) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05002146 case CHELSIO_SET_QSET_PARAMS:{
2147 int i;
2148 struct qset_params *q;
2149 struct ch_qset_params t;
Divy Le Ray8c263762008-10-08 17:37:33 -07002150 int q1 = pi->first_qset;
2151 int nqsets = pi->nqsets;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002152
2153 if (!capable(CAP_NET_ADMIN))
2154 return -EPERM;
2155 if (copy_from_user(&t, useraddr, sizeof(t)))
2156 return -EFAULT;
2157 if (t.qset_idx >= SGE_QSETS)
2158 return -EINVAL;
2159 if (!in_range(t.intr_lat, 0, M_NEWTIMER) ||
Joe Perches8e95a202009-12-03 07:58:21 +00002160 !in_range(t.cong_thres, 0, 255) ||
2161 !in_range(t.txq_size[0], MIN_TXQ_ENTRIES,
2162 MAX_TXQ_ENTRIES) ||
2163 !in_range(t.txq_size[1], MIN_TXQ_ENTRIES,
2164 MAX_TXQ_ENTRIES) ||
2165 !in_range(t.txq_size[2], MIN_CTRL_TXQ_ENTRIES,
2166 MAX_CTRL_TXQ_ENTRIES) ||
2167 !in_range(t.fl_size[0], MIN_FL_ENTRIES,
2168 MAX_RX_BUFFERS) ||
2169 !in_range(t.fl_size[1], MIN_FL_ENTRIES,
2170 MAX_RX_JUMBO_BUFFERS) ||
2171 !in_range(t.rspq_size, MIN_RSPQ_ENTRIES,
2172 MAX_RSPQ_ENTRIES))
Divy Le Ray4d22de32007-01-18 22:04:14 -05002173 return -EINVAL;
Divy Le Ray8c263762008-10-08 17:37:33 -07002174
Divy Le Ray4d22de32007-01-18 22:04:14 -05002175 if ((adapter->flags & FULL_INIT_DONE) &&
2176 (t.rspq_size >= 0 || t.fl_size[0] >= 0 ||
2177 t.fl_size[1] >= 0 || t.txq_size[0] >= 0 ||
2178 t.txq_size[1] >= 0 || t.txq_size[2] >= 0 ||
2179 t.polling >= 0 || t.cong_thres >= 0))
2180 return -EBUSY;
2181
Divy Le Ray8c263762008-10-08 17:37:33 -07002182 /* Allow setting of any available qset when offload enabled */
2183 if (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map)) {
2184 q1 = 0;
2185 for_each_port(adapter, i) {
2186 pi = adap2pinfo(adapter, i);
2187 nqsets += pi->first_qset + pi->nqsets;
2188 }
2189 }
2190
2191 if (t.qset_idx < q1)
2192 return -EINVAL;
2193 if (t.qset_idx > q1 + nqsets - 1)
2194 return -EINVAL;
2195
Divy Le Ray4d22de32007-01-18 22:04:14 -05002196 q = &adapter->params.sge.qset[t.qset_idx];
2197
2198 if (t.rspq_size >= 0)
2199 q->rspq_size = t.rspq_size;
2200 if (t.fl_size[0] >= 0)
2201 q->fl_size = t.fl_size[0];
2202 if (t.fl_size[1] >= 0)
2203 q->jumbo_size = t.fl_size[1];
2204 if (t.txq_size[0] >= 0)
2205 q->txq_size[0] = t.txq_size[0];
2206 if (t.txq_size[1] >= 0)
2207 q->txq_size[1] = t.txq_size[1];
2208 if (t.txq_size[2] >= 0)
2209 q->txq_size[2] = t.txq_size[2];
2210 if (t.cong_thres >= 0)
2211 q->cong_thres = t.cong_thres;
2212 if (t.intr_lat >= 0) {
2213 struct sge_qset *qs =
2214 &adapter->sge.qs[t.qset_idx];
2215
2216 q->coalesce_usecs = t.intr_lat;
2217 t3_update_qset_coalesce(qs, q);
2218 }
2219 if (t.polling >= 0) {
2220 if (adapter->flags & USING_MSIX)
2221 q->polling = t.polling;
2222 else {
2223 /* No polling with INTx for T3A */
2224 if (adapter->params.rev == 0 &&
2225 !(adapter->flags & USING_MSI))
2226 t.polling = 0;
2227
2228 for (i = 0; i < SGE_QSETS; i++) {
2229 q = &adapter->params.sge.
2230 qset[i];
2231 q->polling = t.polling;
2232 }
2233 }
2234 }
Michał Mirosławd2fe2752011-04-17 00:15:46 +00002235
2236 if (t.lro >= 0) {
2237 if (t.lro)
2238 dev->wanted_features |= NETIF_F_GRO;
2239 else
2240 dev->wanted_features &= ~NETIF_F_GRO;
2241 netdev_update_features(dev);
2242 }
Divy Le Ray04ecb072008-10-28 22:40:32 -07002243
Divy Le Ray4d22de32007-01-18 22:04:14 -05002244 break;
2245 }
2246 case CHELSIO_GET_QSET_PARAMS:{
2247 struct qset_params *q;
2248 struct ch_qset_params t;
Divy Le Ray8c263762008-10-08 17:37:33 -07002249 int q1 = pi->first_qset;
2250 int nqsets = pi->nqsets;
2251 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002252
2253 if (copy_from_user(&t, useraddr, sizeof(t)))
2254 return -EFAULT;
Divy Le Ray8c263762008-10-08 17:37:33 -07002255
2256 /* Display qsets for all ports when offload enabled */
2257 if (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map)) {
2258 q1 = 0;
2259 for_each_port(adapter, i) {
2260 pi = adap2pinfo(adapter, i);
2261 nqsets = pi->first_qset + pi->nqsets;
2262 }
2263 }
2264
2265 if (t.qset_idx >= nqsets)
Divy Le Ray4d22de32007-01-18 22:04:14 -05002266 return -EINVAL;
2267
Divy Le Ray8c263762008-10-08 17:37:33 -07002268 q = &adapter->params.sge.qset[q1 + t.qset_idx];
Divy Le Ray4d22de32007-01-18 22:04:14 -05002269 t.rspq_size = q->rspq_size;
2270 t.txq_size[0] = q->txq_size[0];
2271 t.txq_size[1] = q->txq_size[1];
2272 t.txq_size[2] = q->txq_size[2];
2273 t.fl_size[0] = q->fl_size;
2274 t.fl_size[1] = q->jumbo_size;
2275 t.polling = q->polling;
Michał Mirosławd2fe2752011-04-17 00:15:46 +00002276 t.lro = !!(dev->features & NETIF_F_GRO);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002277 t.intr_lat = q->coalesce_usecs;
2278 t.cong_thres = q->cong_thres;
Divy Le Ray8c263762008-10-08 17:37:33 -07002279 t.qnum = q1;
2280
2281 if (adapter->flags & USING_MSIX)
2282 t.vector = adapter->msix_info[q1 + t.qset_idx + 1].vec;
2283 else
2284 t.vector = adapter->pdev->irq;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002285
2286 if (copy_to_user(useraddr, &t, sizeof(t)))
2287 return -EFAULT;
2288 break;
2289 }
2290 case CHELSIO_SET_QSET_NUM:{
2291 struct ch_reg edata;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002292 unsigned int i, first_qset = 0, other_qsets = 0;
2293
2294 if (!capable(CAP_NET_ADMIN))
2295 return -EPERM;
2296 if (adapter->flags & FULL_INIT_DONE)
2297 return -EBUSY;
2298 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2299 return -EFAULT;
2300 if (edata.val < 1 ||
2301 (edata.val > 1 && !(adapter->flags & USING_MSIX)))
2302 return -EINVAL;
2303
2304 for_each_port(adapter, i)
2305 if (adapter->port[i] && adapter->port[i] != dev)
2306 other_qsets += adap2pinfo(adapter, i)->nqsets;
2307
2308 if (edata.val + other_qsets > SGE_QSETS)
2309 return -EINVAL;
2310
2311 pi->nqsets = edata.val;
2312
2313 for_each_port(adapter, i)
2314 if (adapter->port[i]) {
2315 pi = adap2pinfo(adapter, i);
2316 pi->first_qset = first_qset;
2317 first_qset += pi->nqsets;
2318 }
2319 break;
2320 }
2321 case CHELSIO_GET_QSET_NUM:{
2322 struct ch_reg edata;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002323
Dan Rosenberg49c37c02010-09-15 11:43:12 +00002324 memset(&edata, 0, sizeof(struct ch_reg));
2325
Divy Le Ray4d22de32007-01-18 22:04:14 -05002326 edata.cmd = CHELSIO_GET_QSET_NUM;
2327 edata.val = pi->nqsets;
2328 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2329 return -EFAULT;
2330 break;
2331 }
2332 case CHELSIO_LOAD_FW:{
2333 u8 *fw_data;
2334 struct ch_mem_range t;
2335
Alan Cox1b3aa7a2008-04-29 14:29:30 +01002336 if (!capable(CAP_SYS_RAWIO))
Divy Le Ray4d22de32007-01-18 22:04:14 -05002337 return -EPERM;
2338 if (copy_from_user(&t, useraddr, sizeof(t)))
2339 return -EFAULT;
Alan Cox1b3aa7a2008-04-29 14:29:30 +01002340 /* Check t.len sanity ? */
Julia Lawallc5dc9a32010-05-21 22:20:10 +00002341 fw_data = memdup_user(useraddr + sizeof(t), t.len);
2342 if (IS_ERR(fw_data))
2343 return PTR_ERR(fw_data);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002344
2345 ret = t3_load_fw(adapter, fw_data, t.len);
2346 kfree(fw_data);
2347 if (ret)
2348 return ret;
2349 break;
2350 }
2351 case CHELSIO_SETMTUTAB:{
2352 struct ch_mtus m;
2353 int i;
2354
2355 if (!is_offload(adapter))
2356 return -EOPNOTSUPP;
2357 if (!capable(CAP_NET_ADMIN))
2358 return -EPERM;
2359 if (offload_running(adapter))
2360 return -EBUSY;
2361 if (copy_from_user(&m, useraddr, sizeof(m)))
2362 return -EFAULT;
2363 if (m.nmtus != NMTUS)
2364 return -EINVAL;
2365 if (m.mtus[0] < 81) /* accommodate SACK */
2366 return -EINVAL;
2367
2368 /* MTUs must be in ascending order */
2369 for (i = 1; i < NMTUS; ++i)
2370 if (m.mtus[i] < m.mtus[i - 1])
2371 return -EINVAL;
2372
2373 memcpy(adapter->params.mtus, m.mtus,
2374 sizeof(adapter->params.mtus));
2375 break;
2376 }
2377 case CHELSIO_GET_PM:{
2378 struct tp_params *p = &adapter->params.tp;
2379 struct ch_pm m = {.cmd = CHELSIO_GET_PM };
2380
2381 if (!is_offload(adapter))
2382 return -EOPNOTSUPP;
2383 m.tx_pg_sz = p->tx_pg_size;
2384 m.tx_num_pg = p->tx_num_pgs;
2385 m.rx_pg_sz = p->rx_pg_size;
2386 m.rx_num_pg = p->rx_num_pgs;
2387 m.pm_total = p->pmtx_size + p->chan_rx_size * p->nchan;
2388 if (copy_to_user(useraddr, &m, sizeof(m)))
2389 return -EFAULT;
2390 break;
2391 }
2392 case CHELSIO_SET_PM:{
2393 struct ch_pm m;
2394 struct tp_params *p = &adapter->params.tp;
2395
2396 if (!is_offload(adapter))
2397 return -EOPNOTSUPP;
2398 if (!capable(CAP_NET_ADMIN))
2399 return -EPERM;
2400 if (adapter->flags & FULL_INIT_DONE)
2401 return -EBUSY;
2402 if (copy_from_user(&m, useraddr, sizeof(m)))
2403 return -EFAULT;
vignesh babud9da4662007-07-09 11:50:22 -07002404 if (!is_power_of_2(m.rx_pg_sz) ||
2405 !is_power_of_2(m.tx_pg_sz))
Divy Le Ray4d22de32007-01-18 22:04:14 -05002406 return -EINVAL; /* not power of 2 */
2407 if (!(m.rx_pg_sz & 0x14000))
2408 return -EINVAL; /* not 16KB or 64KB */
2409 if (!(m.tx_pg_sz & 0x1554000))
2410 return -EINVAL;
2411 if (m.tx_num_pg == -1)
2412 m.tx_num_pg = p->tx_num_pgs;
2413 if (m.rx_num_pg == -1)
2414 m.rx_num_pg = p->rx_num_pgs;
2415 if (m.tx_num_pg % 24 || m.rx_num_pg % 24)
2416 return -EINVAL;
2417 if (m.rx_num_pg * m.rx_pg_sz > p->chan_rx_size ||
2418 m.tx_num_pg * m.tx_pg_sz > p->chan_tx_size)
2419 return -EINVAL;
2420 p->rx_pg_size = m.rx_pg_sz;
2421 p->tx_pg_size = m.tx_pg_sz;
2422 p->rx_num_pgs = m.rx_num_pg;
2423 p->tx_num_pgs = m.tx_num_pg;
2424 break;
2425 }
2426 case CHELSIO_GET_MEM:{
2427 struct ch_mem_range t;
2428 struct mc7 *mem;
2429 u64 buf[32];
2430
2431 if (!is_offload(adapter))
2432 return -EOPNOTSUPP;
2433 if (!(adapter->flags & FULL_INIT_DONE))
2434 return -EIO; /* need the memory controllers */
2435 if (copy_from_user(&t, useraddr, sizeof(t)))
2436 return -EFAULT;
2437 if ((t.addr & 7) || (t.len & 7))
2438 return -EINVAL;
2439 if (t.mem_id == MEM_CM)
2440 mem = &adapter->cm;
2441 else if (t.mem_id == MEM_PMRX)
2442 mem = &adapter->pmrx;
2443 else if (t.mem_id == MEM_PMTX)
2444 mem = &adapter->pmtx;
2445 else
2446 return -EINVAL;
2447
2448 /*
Divy Le Ray18254942007-02-24 16:43:56 -08002449 * Version scheme:
2450 * bits 0..9: chip version
2451 * bits 10..15: chip revision
2452 */
Divy Le Ray4d22de32007-01-18 22:04:14 -05002453 t.version = 3 | (adapter->params.rev << 10);
2454 if (copy_to_user(useraddr, &t, sizeof(t)))
2455 return -EFAULT;
2456
2457 /*
2458 * Read 256 bytes at a time as len can be large and we don't
2459 * want to use huge intermediate buffers.
2460 */
2461 useraddr += sizeof(t); /* advance to start of buffer */
2462 while (t.len) {
2463 unsigned int chunk =
2464 min_t(unsigned int, t.len, sizeof(buf));
2465
2466 ret =
2467 t3_mc7_bd_read(mem, t.addr / 8, chunk / 8,
2468 buf);
2469 if (ret)
2470 return ret;
2471 if (copy_to_user(useraddr, buf, chunk))
2472 return -EFAULT;
2473 useraddr += chunk;
2474 t.addr += chunk;
2475 t.len -= chunk;
2476 }
2477 break;
2478 }
2479 case CHELSIO_SET_TRACE_FILTER:{
2480 struct ch_trace t;
2481 const struct trace_params *tp;
2482
2483 if (!capable(CAP_NET_ADMIN))
2484 return -EPERM;
2485 if (!offload_running(adapter))
2486 return -EAGAIN;
2487 if (copy_from_user(&t, useraddr, sizeof(t)))
2488 return -EFAULT;
2489
2490 tp = (const struct trace_params *)&t.sip;
2491 if (t.config_tx)
2492 t3_config_trace_filter(adapter, tp, 0,
2493 t.invert_match,
2494 t.trace_tx);
2495 if (t.config_rx)
2496 t3_config_trace_filter(adapter, tp, 1,
2497 t.invert_match,
2498 t.trace_rx);
2499 break;
2500 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05002501 default:
2502 return -EOPNOTSUPP;
2503 }
2504 return 0;
2505}
2506
2507static int cxgb_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2508{
Divy Le Ray4d22de32007-01-18 22:04:14 -05002509 struct mii_ioctl_data *data = if_mii(req);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002510 struct port_info *pi = netdev_priv(dev);
2511 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002512
2513 switch (cmd) {
Ben Hutchings0f07c4e2009-04-29 08:07:20 +00002514 case SIOCGMIIREG:
2515 case SIOCSMIIREG:
2516 /* Convert phy_id from older PRTAD/DEVAD format */
2517 if (is_10G(adapter) &&
2518 !mdio_phy_id_is_c45(data->phy_id) &&
2519 (data->phy_id & 0x1f00) &&
2520 !(data->phy_id & 0xe0e0))
2521 data->phy_id = mdio_phy_id_c45(data->phy_id >> 8,
2522 data->phy_id & 0x1f);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002523 /* FALLTHRU */
Ben Hutchings0f07c4e2009-04-29 08:07:20 +00002524 case SIOCGMIIPHY:
2525 return mdio_mii_ioctl(&pi->phy.mdio, data, cmd);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002526 case SIOCCHIOCTL:
2527 return cxgb_extension_ioctl(dev, req->ifr_data);
2528 default:
2529 return -EOPNOTSUPP;
2530 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05002531}
2532
2533static int cxgb_change_mtu(struct net_device *dev, int new_mtu)
2534{
Divy Le Ray4d22de32007-01-18 22:04:14 -05002535 struct port_info *pi = netdev_priv(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002536 struct adapter *adapter = pi->adapter;
2537 int ret;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002538
Divy Le Ray4d22de32007-01-18 22:04:14 -05002539 if ((ret = t3_mac_set_mtu(&pi->mac, new_mtu)))
2540 return ret;
2541 dev->mtu = new_mtu;
2542 init_port_mtus(adapter);
2543 if (adapter->params.rev == 0 && offload_running(adapter))
2544 t3_load_mtus(adapter, adapter->params.mtus,
2545 adapter->params.a_wnd, adapter->params.b_wnd,
2546 adapter->port[0]->mtu);
2547 return 0;
2548}
2549
2550static int cxgb_set_mac_addr(struct net_device *dev, void *p)
2551{
Divy Le Ray4d22de32007-01-18 22:04:14 -05002552 struct port_info *pi = netdev_priv(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002553 struct adapter *adapter = pi->adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002554 struct sockaddr *addr = p;
2555
2556 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00002557 return -EADDRNOTAVAIL;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002558
2559 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Karen Xief14d42f2009-10-08 09:11:05 +00002560 t3_mac_set_address(&pi->mac, LAN_MAC_IDX, dev->dev_addr);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002561 if (offload_running(adapter))
2562 write_smt_entry(adapter, pi->port_id);
2563 return 0;
2564}
2565
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002566static netdev_features_t cxgb_fix_features(struct net_device *dev,
2567 netdev_features_t features)
Jiri Pirko892ef5d2011-07-20 04:54:35 +00002568{
2569 /*
2570 * Since there is no support for separate rx/tx vlan accel
2571 * enable/disable make sure tx flag is always in same state as rx.
2572 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002573 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2574 features |= NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko892ef5d2011-07-20 04:54:35 +00002575 else
Patrick McHardyf6469682013-04-19 02:04:27 +00002576 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
Jiri Pirko892ef5d2011-07-20 04:54:35 +00002577
2578 return features;
2579}
2580
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002581static int cxgb_set_features(struct net_device *dev, netdev_features_t features)
Jiri Pirko892ef5d2011-07-20 04:54:35 +00002582{
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002583 netdev_features_t changed = dev->features ^ features;
Jiri Pirko892ef5d2011-07-20 04:54:35 +00002584
Patrick McHardyf6469682013-04-19 02:04:27 +00002585 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
Jiri Pirko892ef5d2011-07-20 04:54:35 +00002586 cxgb_vlan_mode(dev, features);
2587
2588 return 0;
2589}
2590
Divy Le Ray4d22de32007-01-18 22:04:14 -05002591#ifdef CONFIG_NET_POLL_CONTROLLER
2592static void cxgb_netpoll(struct net_device *dev)
2593{
Divy Le Ray890de332007-05-30 10:01:34 -07002594 struct port_info *pi = netdev_priv(dev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07002595 struct adapter *adapter = pi->adapter;
Divy Le Ray890de332007-05-30 10:01:34 -07002596 int qidx;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002597
Divy Le Ray890de332007-05-30 10:01:34 -07002598 for (qidx = pi->first_qset; qidx < pi->first_qset + pi->nqsets; qidx++) {
2599 struct sge_qset *qs = &adapter->sge.qs[qidx];
2600 void *source;
Jeff Garzik2eab17a2007-11-23 21:59:45 -05002601
Divy Le Ray890de332007-05-30 10:01:34 -07002602 if (adapter->flags & USING_MSIX)
2603 source = qs;
2604 else
2605 source = adapter;
2606
2607 t3_intr_handler(adapter, qs->rspq.polling) (0, source);
2608 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05002609}
2610#endif
2611
2612/*
2613 * Periodic accumulation of MAC statistics.
2614 */
2615static void mac_stats_update(struct adapter *adapter)
2616{
2617 int i;
2618
2619 for_each_port(adapter, i) {
2620 struct net_device *dev = adapter->port[i];
2621 struct port_info *p = netdev_priv(dev);
2622
2623 if (netif_running(dev)) {
2624 spin_lock(&adapter->stats_lock);
2625 t3_mac_update_stats(&p->mac);
2626 spin_unlock(&adapter->stats_lock);
2627 }
2628 }
2629}
2630
2631static void check_link_status(struct adapter *adapter)
2632{
2633 int i;
2634
2635 for_each_port(adapter, i) {
2636 struct net_device *dev = adapter->port[i];
2637 struct port_info *p = netdev_priv(dev);
Divy Le Rayc22c8142009-05-28 11:23:08 +00002638 int link_fault;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002639
Divy Le Raybf792092009-03-12 21:14:19 +00002640 spin_lock_irq(&adapter->work_lock);
Divy Le Rayc22c8142009-05-28 11:23:08 +00002641 link_fault = p->link_fault;
2642 spin_unlock_irq(&adapter->work_lock);
2643
2644 if (link_fault) {
Divy Le Ray3851c662009-04-17 12:21:11 +00002645 t3_link_fault(adapter, i);
Divy Le Raybf792092009-03-12 21:14:19 +00002646 continue;
2647 }
Divy Le Raybf792092009-03-12 21:14:19 +00002648
2649 if (!(p->phy.caps & SUPPORTED_IRQ) && netif_running(dev)) {
2650 t3_xgm_intr_disable(adapter, i);
2651 t3_read_reg(adapter, A_XGM_INT_STATUS + p->mac.offset);
2652
Divy Le Ray4d22de32007-01-18 22:04:14 -05002653 t3_link_changed(adapter, i);
Divy Le Raybf792092009-03-12 21:14:19 +00002654 t3_xgm_intr_enable(adapter, i);
2655 }
Divy Le Ray4d22de32007-01-18 22:04:14 -05002656 }
2657}
2658
Divy Le Rayfc906642007-03-18 13:10:12 -07002659static void check_t3b2_mac(struct adapter *adapter)
2660{
2661 int i;
2662
Divy Le Rayf2d961c2007-04-09 20:10:22 -07002663 if (!rtnl_trylock()) /* synchronize with ifdown */
2664 return;
2665
Divy Le Rayfc906642007-03-18 13:10:12 -07002666 for_each_port(adapter, i) {
2667 struct net_device *dev = adapter->port[i];
2668 struct port_info *p = netdev_priv(dev);
2669 int status;
2670
2671 if (!netif_running(dev))
2672 continue;
2673
2674 status = 0;
Divy Le Ray6d6daba2007-03-31 00:23:24 -07002675 if (netif_running(dev) && netif_carrier_ok(dev))
Divy Le Rayfc906642007-03-18 13:10:12 -07002676 status = t3b2_mac_watchdog_task(&p->mac);
2677 if (status == 1)
2678 p->mac.stats.num_toggled++;
2679 else if (status == 2) {
2680 struct cmac *mac = &p->mac;
2681
2682 t3_mac_set_mtu(mac, dev->mtu);
Karen Xief14d42f2009-10-08 09:11:05 +00002683 t3_mac_set_address(mac, LAN_MAC_IDX, dev->dev_addr);
Divy Le Rayfc906642007-03-18 13:10:12 -07002684 cxgb_set_rxmode(dev);
2685 t3_link_start(&p->phy, mac, &p->link_config);
2686 t3_mac_enable(mac, MAC_DIRECTION_RX | MAC_DIRECTION_TX);
2687 t3_port_intr_enable(adapter, p->port_id);
2688 p->mac.stats.num_resets++;
2689 }
2690 }
2691 rtnl_unlock();
2692}
2693
2694
Divy Le Ray4d22de32007-01-18 22:04:14 -05002695static void t3_adap_check_task(struct work_struct *work)
2696{
2697 struct adapter *adapter = container_of(work, struct adapter,
2698 adap_check_task.work);
2699 const struct adapter_params *p = &adapter->params;
Divy Le Rayfc882192009-03-12 21:14:09 +00002700 int port;
2701 unsigned int v, status, reset;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002702
2703 adapter->check_task_cnt++;
2704
Divy Le Ray3851c662009-04-17 12:21:11 +00002705 check_link_status(adapter);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002706
2707 /* Accumulate MAC stats if needed */
2708 if (!p->linkpoll_period ||
2709 (adapter->check_task_cnt * p->linkpoll_period) / 10 >=
2710 p->stats_update_period) {
2711 mac_stats_update(adapter);
2712 adapter->check_task_cnt = 0;
2713 }
2714
Divy Le Rayfc906642007-03-18 13:10:12 -07002715 if (p->rev == T3_REV_B2)
2716 check_t3b2_mac(adapter);
2717
Divy Le Rayfc882192009-03-12 21:14:09 +00002718 /*
2719 * Scan the XGMAC's to check for various conditions which we want to
2720 * monitor in a periodic polling manner rather than via an interrupt
2721 * condition. This is used for conditions which would otherwise flood
2722 * the system with interrupts and we only really need to know that the
2723 * conditions are "happening" ... For each condition we count the
2724 * detection of the condition and reset it for the next polling loop.
2725 */
2726 for_each_port(adapter, port) {
2727 struct cmac *mac = &adap2pinfo(adapter, port)->mac;
2728 u32 cause;
2729
2730 cause = t3_read_reg(adapter, A_XGM_INT_CAUSE + mac->offset);
2731 reset = 0;
2732 if (cause & F_RXFIFO_OVERFLOW) {
2733 mac->stats.rx_fifo_ovfl++;
2734 reset |= F_RXFIFO_OVERFLOW;
2735 }
2736
2737 t3_write_reg(adapter, A_XGM_INT_CAUSE + mac->offset, reset);
2738 }
2739
2740 /*
2741 * We do the same as above for FL_EMPTY interrupts.
2742 */
2743 status = t3_read_reg(adapter, A_SG_INT_CAUSE);
2744 reset = 0;
2745
2746 if (status & F_FLEMPTY) {
2747 struct sge_qset *qs = &adapter->sge.qs[0];
2748 int i = 0;
2749
2750 reset |= F_FLEMPTY;
2751
2752 v = (t3_read_reg(adapter, A_SG_RSPQ_FL_STATUS) >> S_FL0EMPTY) &
2753 0xffff;
2754
2755 while (v) {
2756 qs->fl[i].empty += (v & 1);
2757 if (i)
2758 qs++;
2759 i ^= 1;
2760 v >>= 1;
2761 }
2762 }
2763
2764 t3_write_reg(adapter, A_SG_INT_CAUSE, reset);
2765
Divy Le Ray4d22de32007-01-18 22:04:14 -05002766 /* Schedule the next check update if any port is active. */
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002767 spin_lock_irq(&adapter->work_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002768 if (adapter->open_device_map & PORT_MASK)
2769 schedule_chk_task(adapter);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002770 spin_unlock_irq(&adapter->work_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002771}
2772
Steve Wisee998f242010-01-27 17:03:34 +00002773static void db_full_task(struct work_struct *work)
2774{
2775 struct adapter *adapter = container_of(work, struct adapter,
2776 db_full_task);
2777
2778 cxgb3_event_notify(&adapter->tdev, OFFLOAD_DB_FULL, 0);
2779}
2780
2781static void db_empty_task(struct work_struct *work)
2782{
2783 struct adapter *adapter = container_of(work, struct adapter,
2784 db_empty_task);
2785
2786 cxgb3_event_notify(&adapter->tdev, OFFLOAD_DB_EMPTY, 0);
2787}
2788
2789static void db_drop_task(struct work_struct *work)
2790{
2791 struct adapter *adapter = container_of(work, struct adapter,
2792 db_drop_task);
2793 unsigned long delay = 1000;
2794 unsigned short r;
2795
2796 cxgb3_event_notify(&adapter->tdev, OFFLOAD_DB_DROP, 0);
2797
2798 /*
2799 * Sleep a while before ringing the driver qset dbs.
2800 * The delay is between 1000-2023 usecs.
2801 */
2802 get_random_bytes(&r, 2);
2803 delay += r & 1023;
2804 set_current_state(TASK_UNINTERRUPTIBLE);
2805 schedule_timeout(usecs_to_jiffies(delay));
2806 ring_dbs(adapter);
2807}
2808
Divy Le Ray4d22de32007-01-18 22:04:14 -05002809/*
2810 * Processes external (PHY) interrupts in process context.
2811 */
2812static void ext_intr_task(struct work_struct *work)
2813{
2814 struct adapter *adapter = container_of(work, struct adapter,
2815 ext_intr_handler_task);
Divy Le Raybf792092009-03-12 21:14:19 +00002816 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -05002817
Divy Le Raybf792092009-03-12 21:14:19 +00002818 /* Disable link fault interrupts */
2819 for_each_port(adapter, i) {
2820 struct net_device *dev = adapter->port[i];
2821 struct port_info *p = netdev_priv(dev);
2822
2823 t3_xgm_intr_disable(adapter, i);
2824 t3_read_reg(adapter, A_XGM_INT_STATUS + p->mac.offset);
2825 }
2826
2827 /* Re-enable link fault interrupts */
Divy Le Ray4d22de32007-01-18 22:04:14 -05002828 t3_phy_intr_handler(adapter);
2829
Divy Le Raybf792092009-03-12 21:14:19 +00002830 for_each_port(adapter, i)
2831 t3_xgm_intr_enable(adapter, i);
2832
Divy Le Ray4d22de32007-01-18 22:04:14 -05002833 /* Now reenable external interrupts */
2834 spin_lock_irq(&adapter->work_lock);
2835 if (adapter->slow_intr_mask) {
2836 adapter->slow_intr_mask |= F_T3DBG;
2837 t3_write_reg(adapter, A_PL_INT_CAUSE0, F_T3DBG);
2838 t3_write_reg(adapter, A_PL_INT_ENABLE0,
2839 adapter->slow_intr_mask);
2840 }
2841 spin_unlock_irq(&adapter->work_lock);
2842}
2843
2844/*
2845 * Interrupt-context handler for external (PHY) interrupts.
2846 */
2847void t3_os_ext_intr_handler(struct adapter *adapter)
2848{
2849 /*
2850 * Schedule a task to handle external interrupts as they may be slow
2851 * and we use a mutex to protect MDIO registers. We disable PHY
2852 * interrupts in the meantime and let the task reenable them when
2853 * it's done.
2854 */
2855 spin_lock(&adapter->work_lock);
2856 if (adapter->slow_intr_mask) {
2857 adapter->slow_intr_mask &= ~F_T3DBG;
2858 t3_write_reg(adapter, A_PL_INT_ENABLE0,
2859 adapter->slow_intr_mask);
2860 queue_work(cxgb3_wq, &adapter->ext_intr_handler_task);
2861 }
2862 spin_unlock(&adapter->work_lock);
2863}
2864
Divy Le Raybf792092009-03-12 21:14:19 +00002865void t3_os_link_fault_handler(struct adapter *adapter, int port_id)
2866{
2867 struct net_device *netdev = adapter->port[port_id];
2868 struct port_info *pi = netdev_priv(netdev);
2869
2870 spin_lock(&adapter->work_lock);
2871 pi->link_fault = 1;
Divy Le Raybf792092009-03-12 21:14:19 +00002872 spin_unlock(&adapter->work_lock);
2873}
2874
Casey Leedom55bc3222010-09-02 13:07:32 +00002875static int t3_adapter_error(struct adapter *adapter, int reset, int on_wq)
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002876{
2877 int i, ret = 0;
2878
Divy Le Raycb0bc202009-01-26 22:21:59 -08002879 if (is_offload(adapter) &&
2880 test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map)) {
Steve Wisefa0d4c12009-09-05 20:22:38 -07002881 cxgb3_event_notify(&adapter->tdev, OFFLOAD_STATUS_DOWN, 0);
Divy Le Raycb0bc202009-01-26 22:21:59 -08002882 offload_close(&adapter->tdev);
2883 }
2884
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002885 /* Stop all ports */
2886 for_each_port(adapter, i) {
2887 struct net_device *netdev = adapter->port[i];
2888
2889 if (netif_running(netdev))
Casey Leedom55bc3222010-09-02 13:07:32 +00002890 __cxgb_close(netdev, on_wq);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002891 }
2892
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002893 /* Stop SGE timers */
2894 t3_stop_sge_timers(adapter);
2895
2896 adapter->flags &= ~FULL_INIT_DONE;
2897
2898 if (reset)
2899 ret = t3_reset_adapter(adapter);
2900
2901 pci_disable_device(adapter->pdev);
2902
2903 return ret;
2904}
2905
2906static int t3_reenable_adapter(struct adapter *adapter)
2907{
2908 if (pci_enable_device(adapter->pdev)) {
2909 dev_err(&adapter->pdev->dev,
2910 "Cannot re-enable PCI device after reset.\n");
2911 goto err;
2912 }
2913 pci_set_master(adapter->pdev);
2914 pci_restore_state(adapter->pdev);
Breno Leitaoccdddf52009-12-10 09:03:37 +00002915 pci_save_state(adapter->pdev);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002916
2917 /* Free sge resources */
2918 t3_free_sge_resources(adapter);
2919
2920 if (t3_replay_prep_adapter(adapter))
2921 goto err;
2922
2923 return 0;
2924err:
2925 return -1;
2926}
2927
2928static void t3_resume_ports(struct adapter *adapter)
2929{
2930 int i;
2931
2932 /* Restart the ports */
2933 for_each_port(adapter, i) {
2934 struct net_device *netdev = adapter->port[i];
2935
2936 if (netif_running(netdev)) {
2937 if (cxgb_open(netdev)) {
2938 dev_err(&adapter->pdev->dev,
2939 "can't bring device back up"
2940 " after reset\n");
2941 continue;
2942 }
2943 }
2944 }
Divy Le Raycb0bc202009-01-26 22:21:59 -08002945
2946 if (is_offload(adapter) && !ofld_disable)
Steve Wisefa0d4c12009-09-05 20:22:38 -07002947 cxgb3_event_notify(&adapter->tdev, OFFLOAD_STATUS_UP, 0);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002948}
2949
2950/*
2951 * processes a fatal error.
2952 * Bring the ports down, reset the chip, bring the ports back up.
2953 */
2954static void fatal_error_task(struct work_struct *work)
2955{
2956 struct adapter *adapter = container_of(work, struct adapter,
2957 fatal_error_handler_task);
2958 int err = 0;
2959
2960 rtnl_lock();
Casey Leedom55bc3222010-09-02 13:07:32 +00002961 err = t3_adapter_error(adapter, 1, 1);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002962 if (!err)
2963 err = t3_reenable_adapter(adapter);
2964 if (!err)
2965 t3_resume_ports(adapter);
2966
2967 CH_ALERT(adapter, "adapter reset %s\n", err ? "failed" : "succeeded");
2968 rtnl_unlock();
2969}
2970
Divy Le Ray4d22de32007-01-18 22:04:14 -05002971void t3_fatal_err(struct adapter *adapter)
2972{
2973 unsigned int fw_status[4];
2974
2975 if (adapter->flags & FULL_INIT_DONE) {
2976 t3_sge_stop(adapter);
Divy Le Rayc64c2ea2007-08-21 20:49:31 -07002977 t3_write_reg(adapter, A_XGM_TX_CTRL, 0);
2978 t3_write_reg(adapter, A_XGM_RX_CTRL, 0);
2979 t3_write_reg(adapter, XGM_REG(A_XGM_TX_CTRL, 1), 0);
2980 t3_write_reg(adapter, XGM_REG(A_XGM_RX_CTRL, 1), 0);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002981
2982 spin_lock(&adapter->work_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002983 t3_intr_disable(adapter);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07002984 queue_work(cxgb3_wq, &adapter->fatal_error_handler_task);
2985 spin_unlock(&adapter->work_lock);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002986 }
2987 CH_ALERT(adapter, "encountered fatal error, operation suspended\n");
2988 if (!t3_cim_ctl_blk_read(adapter, 0xa0, 4, fw_status))
2989 CH_ALERT(adapter, "FW status: 0x%x, 0x%x, 0x%x, 0x%x\n",
2990 fw_status[0], fw_status[1],
2991 fw_status[2], fw_status[3]);
Divy Le Ray4d22de32007-01-18 22:04:14 -05002992}
2993
Divy Le Ray91a6b502007-11-16 11:21:55 -08002994/**
2995 * t3_io_error_detected - called when PCI error is detected
2996 * @pdev: Pointer to PCI device
2997 * @state: The current pci connection state
2998 *
2999 * This function is called after a PCI bus error affecting
3000 * this device has been detected.
3001 */
3002static pci_ers_result_t t3_io_error_detected(struct pci_dev *pdev,
3003 pci_channel_state_t state)
3004{
Divy Le Raybc4b6b522007-12-17 18:47:41 -08003005 struct adapter *adapter = pci_get_drvdata(pdev);
Divy Le Ray91a6b502007-11-16 11:21:55 -08003006
Divy Le Raye8d19372009-04-17 12:21:27 +00003007 if (state == pci_channel_io_perm_failure)
3008 return PCI_ERS_RESULT_DISCONNECT;
3009
Breno Leitaoc661c4a2010-11-25 07:53:55 +00003010 t3_adapter_error(adapter, 0, 0);
Divy Le Ray91a6b502007-11-16 11:21:55 -08003011
Divy Le Ray48c4b6d2008-05-06 19:25:56 -07003012 /* Request a slot reset. */
Divy Le Ray91a6b502007-11-16 11:21:55 -08003013 return PCI_ERS_RESULT_NEED_RESET;
3014}
3015
3016/**
3017 * t3_io_slot_reset - called after the pci bus has been reset.
3018 * @pdev: Pointer to PCI device
3019 *
3020 * Restart the card from scratch, as if from a cold-boot.
3021 */
3022static pci_ers_result_t t3_io_slot_reset(struct pci_dev *pdev)
3023{
Divy Le Raybc4b6b522007-12-17 18:47:41 -08003024 struct adapter *adapter = pci_get_drvdata(pdev);
Divy Le Ray91a6b502007-11-16 11:21:55 -08003025
Divy Le Ray20d3fc12008-10-08 17:36:03 -07003026 if (!t3_reenable_adapter(adapter))
3027 return PCI_ERS_RESULT_RECOVERED;
Divy Le Ray91a6b502007-11-16 11:21:55 -08003028
Divy Le Ray48c4b6d2008-05-06 19:25:56 -07003029 return PCI_ERS_RESULT_DISCONNECT;
Divy Le Ray91a6b502007-11-16 11:21:55 -08003030}
3031
3032/**
3033 * t3_io_resume - called when traffic can start flowing again.
3034 * @pdev: Pointer to PCI device
3035 *
3036 * This callback is called when the error recovery driver tells us that
3037 * its OK to resume normal operation.
3038 */
3039static void t3_io_resume(struct pci_dev *pdev)
3040{
Divy Le Raybc4b6b522007-12-17 18:47:41 -08003041 struct adapter *adapter = pci_get_drvdata(pdev);
Divy Le Ray91a6b502007-11-16 11:21:55 -08003042
Divy Le Ray68f40c12009-03-26 16:39:19 +00003043 CH_ALERT(adapter, "adapter recovering, PEX ERR 0x%x\n",
3044 t3_read_reg(adapter, A_PCIE_PEX_ERR));
3045
Benjamin Herrenschmidt7cc47d12013-06-30 14:37:11 +10003046 rtnl_lock();
Divy Le Ray20d3fc12008-10-08 17:36:03 -07003047 t3_resume_ports(adapter);
Benjamin Herrenschmidt7cc47d12013-06-30 14:37:11 +10003048 rtnl_unlock();
Divy Le Ray91a6b502007-11-16 11:21:55 -08003049}
3050
Stephen Hemminger3646f0e2012-09-07 09:33:15 -07003051static const struct pci_error_handlers t3_err_handler = {
Divy Le Ray91a6b502007-11-16 11:21:55 -08003052 .error_detected = t3_io_error_detected,
3053 .slot_reset = t3_io_slot_reset,
3054 .resume = t3_io_resume,
3055};
3056
Divy Le Ray8c263762008-10-08 17:37:33 -07003057/*
3058 * Set the number of qsets based on the number of CPUs and the number of ports,
3059 * not to exceed the number of available qsets, assuming there are enough qsets
3060 * per port in HW.
3061 */
3062static void set_nqsets(struct adapter *adap)
3063{
3064 int i, j = 0;
Yuval Mintzdbfa6002012-07-01 03:18:54 +00003065 int num_cpus = netif_get_num_default_rss_queues();
Divy Le Ray8c263762008-10-08 17:37:33 -07003066 int hwports = adap->params.nports;
Divy Le Ray5cda9362009-01-18 21:29:40 -08003067 int nqsets = adap->msix_nvectors - 1;
Divy Le Ray8c263762008-10-08 17:37:33 -07003068
Divy Le Rayf9ee3882008-11-09 00:55:33 -08003069 if (adap->params.rev > 0 && adap->flags & USING_MSIX) {
Divy Le Ray8c263762008-10-08 17:37:33 -07003070 if (hwports == 2 &&
3071 (hwports * nqsets > SGE_QSETS ||
3072 num_cpus >= nqsets / hwports))
3073 nqsets /= hwports;
3074 if (nqsets > num_cpus)
3075 nqsets = num_cpus;
3076 if (nqsets < 1 || hwports == 4)
3077 nqsets = 1;
3078 } else
3079 nqsets = 1;
3080
3081 for_each_port(adap, i) {
3082 struct port_info *pi = adap2pinfo(adap, i);
3083
3084 pi->first_qset = j;
3085 pi->nqsets = nqsets;
3086 j = pi->first_qset + nqsets;
3087
3088 dev_info(&adap->pdev->dev,
3089 "Port %d using %d queue sets.\n", i, nqsets);
3090 }
3091}
3092
Bill Pemberton2109eaa2012-12-03 09:23:01 -05003093static int cxgb_enable_msix(struct adapter *adap)
Divy Le Ray4d22de32007-01-18 22:04:14 -05003094{
3095 struct msix_entry entries[SGE_QSETS + 1];
Divy Le Ray5cda9362009-01-18 21:29:40 -08003096 int vectors;
Alexander Gordeevfc1d0bf2014-02-18 11:07:58 +01003097 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003098
Divy Le Ray5cda9362009-01-18 21:29:40 -08003099 vectors = ARRAY_SIZE(entries);
3100 for (i = 0; i < vectors; ++i)
Divy Le Ray4d22de32007-01-18 22:04:14 -05003101 entries[i].entry = i;
3102
Alexander Gordeevfc1d0bf2014-02-18 11:07:58 +01003103 vectors = pci_enable_msix_range(adap->pdev, entries,
3104 adap->params.nports + 1, vectors);
3105 if (vectors < 0)
3106 return vectors;
Divy Le Ray5cda9362009-01-18 21:29:40 -08003107
Alexander Gordeevfc1d0bf2014-02-18 11:07:58 +01003108 for (i = 0; i < vectors; ++i)
3109 adap->msix_info[i].vec = entries[i].vector;
3110 adap->msix_nvectors = vectors;
Divy Le Ray5cda9362009-01-18 21:29:40 -08003111
Alexander Gordeevfc1d0bf2014-02-18 11:07:58 +01003112 return 0;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003113}
3114
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003115static void print_port_info(struct adapter *adap, const struct adapter_info *ai)
Divy Le Ray4d22de32007-01-18 22:04:14 -05003116{
3117 static const char *pci_variant[] = {
3118 "PCI", "PCI-X", "PCI-X ECC", "PCI-X 266", "PCI Express"
3119 };
3120
3121 int i;
3122 char buf[80];
3123
3124 if (is_pcie(adap))
3125 snprintf(buf, sizeof(buf), "%s x%d",
3126 pci_variant[adap->params.pci.variant],
3127 adap->params.pci.width);
3128 else
3129 snprintf(buf, sizeof(buf), "%s %dMHz/%d-bit",
3130 pci_variant[adap->params.pci.variant],
3131 adap->params.pci.speed, adap->params.pci.width);
3132
3133 for_each_port(adap, i) {
3134 struct net_device *dev = adap->port[i];
3135 const struct port_info *pi = netdev_priv(dev);
3136
3137 if (!test_bit(i, &adap->registered_device_map))
3138 continue;
Joe Perches428ac432013-01-06 13:34:49 +00003139 netdev_info(dev, "%s %s %sNIC (rev %d) %s%s\n",
3140 ai->desc, pi->phy.desc,
3141 is_offload(adap) ? "R" : "", adap->params.rev, buf,
3142 (adap->flags & USING_MSIX) ? " MSI-X" :
3143 (adap->flags & USING_MSI) ? " MSI" : "");
Divy Le Ray4d22de32007-01-18 22:04:14 -05003144 if (adap->name == dev->name && adap->params.vpd.mclk)
Joe Perches428ac432013-01-06 13:34:49 +00003145 pr_info("%s: %uMB CM, %uMB PMTX, %uMB PMRX, S/N: %s\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -05003146 adap->name, t3_mc7_size(&adap->cm) >> 20,
3147 t3_mc7_size(&adap->pmtx) >> 20,
Divy Le Ray167cdf52007-08-21 20:49:36 -07003148 t3_mc7_size(&adap->pmrx) >> 20,
3149 adap->params.vpd.sn);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003150 }
3151}
3152
Stephen Hemmingerdd752692008-11-19 22:15:39 -08003153static const struct net_device_ops cxgb_netdev_ops = {
3154 .ndo_open = cxgb_open,
3155 .ndo_stop = cxgb_close,
Divy Le Ray43a944f2008-11-26 15:35:26 -08003156 .ndo_start_xmit = t3_eth_xmit,
Stephen Hemmingerdd752692008-11-19 22:15:39 -08003157 .ndo_get_stats = cxgb_get_stats,
3158 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00003159 .ndo_set_rx_mode = cxgb_set_rxmode,
Stephen Hemmingerdd752692008-11-19 22:15:39 -08003160 .ndo_do_ioctl = cxgb_ioctl,
3161 .ndo_change_mtu = cxgb_change_mtu,
3162 .ndo_set_mac_address = cxgb_set_mac_addr,
Jiri Pirko892ef5d2011-07-20 04:54:35 +00003163 .ndo_fix_features = cxgb_fix_features,
3164 .ndo_set_features = cxgb_set_features,
Stephen Hemmingerdd752692008-11-19 22:15:39 -08003165#ifdef CONFIG_NET_POLL_CONTROLLER
3166 .ndo_poll_controller = cxgb_netpoll,
3167#endif
3168};
3169
Bill Pemberton2109eaa2012-12-03 09:23:01 -05003170static void cxgb3_init_iscsi_mac(struct net_device *dev)
Karen Xief14d42f2009-10-08 09:11:05 +00003171{
3172 struct port_info *pi = netdev_priv(dev);
3173
3174 memcpy(pi->iscsic.mac_addr, dev->dev_addr, ETH_ALEN);
3175 pi->iscsic.mac_addr[3] |= 0x80;
3176}
3177
brenohl@br.ibm.com1d962ec2012-07-18 09:29:08 +00003178#define TSO_FLAGS (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN)
3179#define VLAN_FEAT (NETIF_F_SG | NETIF_F_IP_CSUM | TSO_FLAGS | \
3180 NETIF_F_IPV6_CSUM | NETIF_F_HIGHDMA)
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003181static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Divy Le Ray4d22de32007-01-18 22:04:14 -05003182{
Divy Le Ray4d22de32007-01-18 22:04:14 -05003183 int i, err, pci_using_dac = 0;
Divy Le Ray68f40c12009-03-26 16:39:19 +00003184 resource_size_t mmio_start, mmio_len;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003185 const struct adapter_info *ai;
3186 struct adapter *adapter = NULL;
3187 struct port_info *pi;
3188
Joe Perches428ac432013-01-06 13:34:49 +00003189 pr_info_once("%s - version %s\n", DRV_DESC, DRV_VERSION);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003190
3191 if (!cxgb3_wq) {
3192 cxgb3_wq = create_singlethread_workqueue(DRV_NAME);
3193 if (!cxgb3_wq) {
Joe Perches428ac432013-01-06 13:34:49 +00003194 pr_err("cannot initialize work queue\n");
Divy Le Ray4d22de32007-01-18 22:04:14 -05003195 return -ENOMEM;
3196 }
3197 }
3198
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003199 err = pci_enable_device(pdev);
3200 if (err) {
3201 dev_err(&pdev->dev, "cannot enable PCI device\n");
3202 goto out;
3203 }
3204
Divy Le Ray4d22de32007-01-18 22:04:14 -05003205 err = pci_request_regions(pdev, DRV_NAME);
3206 if (err) {
3207 /* Just info, some other driver may have claimed the device. */
3208 dev_info(&pdev->dev, "cannot obtain PCI resources\n");
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003209 goto out_disable_device;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003210 }
3211
Yang Hongyang6a355282009-04-06 19:01:13 -07003212 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05003213 pci_using_dac = 1;
Yang Hongyang6a355282009-04-06 19:01:13 -07003214 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Divy Le Ray4d22de32007-01-18 22:04:14 -05003215 if (err) {
3216 dev_err(&pdev->dev, "unable to obtain 64-bit DMA for "
3217 "coherent allocations\n");
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003218 goto out_release_regions;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003219 }
Yang Hongyang284901a2009-04-06 19:01:15 -07003220 } else if ((err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) != 0) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05003221 dev_err(&pdev->dev, "no usable DMA configuration\n");
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003222 goto out_release_regions;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003223 }
3224
3225 pci_set_master(pdev);
Divy Le Ray204e2f92008-05-06 19:26:01 -07003226 pci_save_state(pdev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003227
3228 mmio_start = pci_resource_start(pdev, 0);
3229 mmio_len = pci_resource_len(pdev, 0);
3230 ai = t3_get_adapter_info(ent->driver_data);
3231
3232 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
3233 if (!adapter) {
3234 err = -ENOMEM;
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003235 goto out_release_regions;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003236 }
3237
Divy Le Ray74b793e2009-06-09 23:25:21 +00003238 adapter->nofail_skb =
3239 alloc_skb(sizeof(struct cpl_set_tcb_field), GFP_KERNEL);
3240 if (!adapter->nofail_skb) {
3241 dev_err(&pdev->dev, "cannot allocate nofail buffer\n");
3242 err = -ENOMEM;
3243 goto out_free_adapter;
3244 }
3245
Divy Le Ray4d22de32007-01-18 22:04:14 -05003246 adapter->regs = ioremap_nocache(mmio_start, mmio_len);
3247 if (!adapter->regs) {
3248 dev_err(&pdev->dev, "cannot map device registers\n");
3249 err = -ENOMEM;
3250 goto out_free_adapter;
3251 }
3252
3253 adapter->pdev = pdev;
3254 adapter->name = pci_name(pdev);
3255 adapter->msg_enable = dflt_msg_enable;
3256 adapter->mmio_len = mmio_len;
3257
3258 mutex_init(&adapter->mdio_lock);
3259 spin_lock_init(&adapter->work_lock);
3260 spin_lock_init(&adapter->stats_lock);
3261
3262 INIT_LIST_HEAD(&adapter->adapter_list);
3263 INIT_WORK(&adapter->ext_intr_handler_task, ext_intr_task);
Divy Le Ray20d3fc12008-10-08 17:36:03 -07003264 INIT_WORK(&adapter->fatal_error_handler_task, fatal_error_task);
Steve Wisee998f242010-01-27 17:03:34 +00003265
3266 INIT_WORK(&adapter->db_full_task, db_full_task);
3267 INIT_WORK(&adapter->db_empty_task, db_empty_task);
3268 INIT_WORK(&adapter->db_drop_task, db_drop_task);
3269
Divy Le Ray4d22de32007-01-18 22:04:14 -05003270 INIT_DELAYED_WORK(&adapter->adap_check_task, t3_adap_check_task);
3271
Divy Le Ray952cdf32009-03-26 16:39:24 +00003272 for (i = 0; i < ai->nports0 + ai->nports1; ++i) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05003273 struct net_device *netdev;
3274
Divy Le Ray82ad3322008-12-16 01:09:39 -08003275 netdev = alloc_etherdev_mq(sizeof(struct port_info), SGE_QSETS);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003276 if (!netdev) {
3277 err = -ENOMEM;
3278 goto out_free_dev;
3279 }
3280
Divy Le Ray4d22de32007-01-18 22:04:14 -05003281 SET_NETDEV_DEV(netdev, &pdev->dev);
3282
3283 adapter->port[i] = netdev;
3284 pi = netdev_priv(netdev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07003285 pi->adapter = adapter;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003286 pi->port_id = i;
3287 netif_carrier_off(netdev);
3288 netdev->irq = pdev->irq;
3289 netdev->mem_start = mmio_start;
3290 netdev->mem_end = mmio_start + mmio_len - 1;
Michał Mirosławd2fe2752011-04-17 00:15:46 +00003291 netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
Patrick McHardyf6469682013-04-19 02:04:27 +00003292 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX;
3293 netdev->features |= netdev->hw_features |
3294 NETIF_F_HW_VLAN_CTAG_TX;
brenohl@br.ibm.com1d962ec2012-07-18 09:29:08 +00003295 netdev->vlan_features |= netdev->features & VLAN_FEAT;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003296 if (pci_using_dac)
3297 netdev->features |= NETIF_F_HIGHDMA;
3298
Stephen Hemmingerdd752692008-11-19 22:15:39 -08003299 netdev->netdev_ops = &cxgb_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003300 netdev->ethtool_ops = &cxgb_ethtool_ops;
Jarod Wilsond894be52016-10-20 13:55:16 -04003301 netdev->min_mtu = 81;
3302 netdev->max_mtu = ETH_MAX_MTU;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003303 }
3304
Divy Le Ray5fbf8162007-08-29 19:15:47 -07003305 pci_set_drvdata(pdev, adapter);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003306 if (t3_prep_adapter(adapter, ai, 1) < 0) {
3307 err = -ENODEV;
3308 goto out_free_dev;
3309 }
Jeff Garzik2eab17a2007-11-23 21:59:45 -05003310
Divy Le Ray4d22de32007-01-18 22:04:14 -05003311 /*
3312 * The card is now ready to go. If any errors occur during device
3313 * registration we do not fail the whole card but rather proceed only
3314 * with the ports we manage to register successfully. However we must
3315 * register at least one net device.
3316 */
3317 for_each_port(adapter, i) {
3318 err = register_netdev(adapter->port[i]);
3319 if (err)
3320 dev_warn(&pdev->dev,
3321 "cannot register net device %s, skipping\n",
3322 adapter->port[i]->name);
3323 else {
3324 /*
3325 * Change the name we use for messages to the name of
3326 * the first successfully registered interface.
3327 */
3328 if (!adapter->registered_device_map)
3329 adapter->name = adapter->port[i]->name;
3330
3331 __set_bit(i, &adapter->registered_device_map);
3332 }
3333 }
3334 if (!adapter->registered_device_map) {
3335 dev_err(&pdev->dev, "could not register any net devices\n");
3336 goto out_free_dev;
3337 }
3338
Karen Xief14d42f2009-10-08 09:11:05 +00003339 for_each_port(adapter, i)
3340 cxgb3_init_iscsi_mac(adapter->port[i]);
3341
Divy Le Ray4d22de32007-01-18 22:04:14 -05003342 /* Driver's ready. Reflect it on LEDs */
3343 t3_led_ready(adapter);
3344
3345 if (is_offload(adapter)) {
3346 __set_bit(OFFLOAD_DEVMAP_BIT, &adapter->registered_device_map);
3347 cxgb3_adapter_ofld(adapter);
3348 }
3349
3350 /* See what interrupts we'll be using */
3351 if (msi > 1 && cxgb_enable_msix(adapter) == 0)
3352 adapter->flags |= USING_MSIX;
3353 else if (msi > 0 && pci_enable_msi(pdev) == 0)
3354 adapter->flags |= USING_MSI;
3355
Divy Le Ray8c263762008-10-08 17:37:33 -07003356 set_nqsets(adapter);
3357
Divy Le Ray0ee8d332007-02-08 16:55:59 -08003358 err = sysfs_create_group(&adapter->port[0]->dev.kobj,
Divy Le Ray4d22de32007-01-18 22:04:14 -05003359 &cxgb3_attr_group);
3360
3361 print_port_info(adapter, ai);
3362 return 0;
3363
3364out_free_dev:
3365 iounmap(adapter->regs);
Divy Le Ray952cdf32009-03-26 16:39:24 +00003366 for (i = ai->nports0 + ai->nports1 - 1; i >= 0; --i)
Divy Le Ray4d22de32007-01-18 22:04:14 -05003367 if (adapter->port[i])
3368 free_netdev(adapter->port[i]);
3369
3370out_free_adapter:
3371 kfree(adapter);
3372
Divy Le Ray4d22de32007-01-18 22:04:14 -05003373out_release_regions:
3374 pci_release_regions(pdev);
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003375out_disable_device:
3376 pci_disable_device(pdev);
Kulikov Vasiliy7aaaaa12010-08-03 05:43:11 +00003377out:
Divy Le Ray4d22de32007-01-18 22:04:14 -05003378 return err;
3379}
3380
Bill Pemberton2109eaa2012-12-03 09:23:01 -05003381static void remove_one(struct pci_dev *pdev)
Divy Le Ray4d22de32007-01-18 22:04:14 -05003382{
Divy Le Ray5fbf8162007-08-29 19:15:47 -07003383 struct adapter *adapter = pci_get_drvdata(pdev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003384
Divy Le Ray5fbf8162007-08-29 19:15:47 -07003385 if (adapter) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05003386 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -05003387
3388 t3_sge_stop(adapter);
Divy Le Ray0ee8d332007-02-08 16:55:59 -08003389 sysfs_remove_group(&adapter->port[0]->dev.kobj,
Divy Le Ray4d22de32007-01-18 22:04:14 -05003390 &cxgb3_attr_group);
3391
Divy Le Ray4d22de32007-01-18 22:04:14 -05003392 if (is_offload(adapter)) {
3393 cxgb3_adapter_unofld(adapter);
3394 if (test_bit(OFFLOAD_DEVMAP_BIT,
3395 &adapter->open_device_map))
3396 offload_close(&adapter->tdev);
3397 }
3398
Divy Le Ray67d92ab2007-11-16 11:21:50 -08003399 for_each_port(adapter, i)
3400 if (test_bit(i, &adapter->registered_device_map))
3401 unregister_netdev(adapter->port[i]);
3402
Divy Le Ray0ca41c02008-09-25 14:05:28 +00003403 t3_stop_sge_timers(adapter);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003404 t3_free_sge_resources(adapter);
3405 cxgb_disable_msi(adapter);
3406
Divy Le Ray4d22de32007-01-18 22:04:14 -05003407 for_each_port(adapter, i)
3408 if (adapter->port[i])
3409 free_netdev(adapter->port[i]);
3410
3411 iounmap(adapter->regs);
Divy Le Ray74b793e2009-06-09 23:25:21 +00003412 if (adapter->nofail_skb)
3413 kfree_skb(adapter->nofail_skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003414 kfree(adapter);
3415 pci_release_regions(pdev);
3416 pci_disable_device(pdev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05003417 }
3418}
3419
3420static struct pci_driver driver = {
3421 .name = DRV_NAME,
3422 .id_table = cxgb3_pci_tbl,
3423 .probe = init_one,
Bill Pemberton2109eaa2012-12-03 09:23:01 -05003424 .remove = remove_one,
Divy Le Ray91a6b502007-11-16 11:21:55 -08003425 .err_handler = &t3_err_handler,
Divy Le Ray4d22de32007-01-18 22:04:14 -05003426};
3427
3428static int __init cxgb3_init_module(void)
3429{
3430 int ret;
3431
3432 cxgb3_offload_init();
3433
3434 ret = pci_register_driver(&driver);
3435 return ret;
3436}
3437
3438static void __exit cxgb3_cleanup_module(void)
3439{
3440 pci_unregister_driver(&driver);
3441 if (cxgb3_wq)
3442 destroy_workqueue(cxgb3_wq);
3443}
3444
3445module_init(cxgb3_init_module);
3446module_exit(cxgb3_cleanup_module);