myri10ge: fix error checking and return value in myri10ge_allocate_rings
[linux-2.6-block.git] / drivers / net / myri10ge / myri10ge.c
CommitLineData
0da34b6d
BG
1/*************************************************************************
2 * myri10ge.c: Myricom Myri-10G Ethernet driver.
3 *
4a2e612a 4 * Copyright (C) 2005 - 2007 Myricom, Inc.
0da34b6d
BG
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Myricom, Inc. nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
4a2e612a
BG
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0da34b6d 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4a2e612a
BG
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
0da34b6d
BG
30 *
31 *
32 * If the eeprom on your board is not recent enough, you will need to get a
33 * newer firmware image at:
34 * http://www.myri.com/scs/download-Myri10GE.html
35 *
36 * Contact Information:
37 * <help@myri.com>
38 * Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
39 *************************************************************************/
40
41#include <linux/tcp.h>
42#include <linux/netdevice.h>
43#include <linux/skbuff.h>
44#include <linux/string.h>
45#include <linux/module.h>
46#include <linux/pci.h>
b10c0668 47#include <linux/dma-mapping.h>
0da34b6d
BG
48#include <linux/etherdevice.h>
49#include <linux/if_ether.h>
50#include <linux/if_vlan.h>
51#include <linux/ip.h>
52#include <linux/inet.h>
53#include <linux/in.h>
54#include <linux/ethtool.h>
55#include <linux/firmware.h>
56#include <linux/delay.h>
57#include <linux/version.h>
58#include <linux/timer.h>
59#include <linux/vmalloc.h>
60#include <linux/crc32.h>
61#include <linux/moduleparam.h>
62#include <linux/io.h>
63#include <net/checksum.h>
64#include <asm/byteorder.h>
65#include <asm/io.h>
0da34b6d
BG
66#include <asm/processor.h>
67#ifdef CONFIG_MTRR
68#include <asm/mtrr.h>
69#endif
70
71#include "myri10ge_mcp.h"
72#include "myri10ge_mcp_gen_header.h"
73
b2db8dd4 74#define MYRI10GE_VERSION_STR "1.2.0"
0da34b6d
BG
75
76MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
77MODULE_AUTHOR("Maintainer: help@myri.com");
78MODULE_VERSION(MYRI10GE_VERSION_STR);
79MODULE_LICENSE("Dual BSD/GPL");
80
81#define MYRI10GE_MAX_ETHER_MTU 9014
82
83#define MYRI10GE_ETH_STOPPED 0
84#define MYRI10GE_ETH_STOPPING 1
85#define MYRI10GE_ETH_STARTING 2
86#define MYRI10GE_ETH_RUNNING 3
87#define MYRI10GE_ETH_OPEN_FAILED 4
88
89#define MYRI10GE_EEPROM_STRINGS_SIZE 256
90#define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
91
40f6cff5 92#define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
0da34b6d
BG
93#define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
94
dd50f336
BG
95#define MYRI10GE_ALLOC_ORDER 0
96#define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
97#define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
98
0da34b6d 99struct myri10ge_rx_buffer_state {
dd50f336
BG
100 struct page *page;
101 int page_offset;
0da34b6d
BG
102 DECLARE_PCI_UNMAP_ADDR(bus)
103 DECLARE_PCI_UNMAP_LEN(len)
104};
105
106struct myri10ge_tx_buffer_state {
107 struct sk_buff *skb;
108 int last;
109 DECLARE_PCI_UNMAP_ADDR(bus)
110 DECLARE_PCI_UNMAP_LEN(len)
111};
112
113struct myri10ge_cmd {
114 u32 data0;
115 u32 data1;
116 u32 data2;
117};
118
119struct myri10ge_rx_buf {
120 struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
121 u8 __iomem *wc_fifo; /* w/c rx dma addr fifo address */
122 struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
123 struct myri10ge_rx_buffer_state *info;
dd50f336
BG
124 struct page *page;
125 dma_addr_t bus;
126 int page_offset;
0da34b6d 127 int cnt;
dd50f336 128 int fill_cnt;
0da34b6d
BG
129 int alloc_fail;
130 int mask; /* number of rx slots -1 */
dd50f336 131 int watchdog_needed;
0da34b6d
BG
132};
133
134struct myri10ge_tx_buf {
135 struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
136 u8 __iomem *wc_fifo; /* w/c send fifo address */
137 struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
138 char *req_bytes;
139 struct myri10ge_tx_buffer_state *info;
140 int mask; /* number of transmit slots -1 */
141 int boundary; /* boundary transmits cannot cross */
142 int req ____cacheline_aligned; /* transmit slots submitted */
143 int pkt_start; /* packets started */
144 int done ____cacheline_aligned; /* transmit slots completed */
145 int pkt_done; /* packets completed */
146};
147
148struct myri10ge_rx_done {
149 struct mcp_slot *entry;
150 dma_addr_t bus;
151 int cnt;
152 int idx;
153};
154
155struct myri10ge_priv {
156 int running; /* running? */
157 int csum_flag; /* rx_csums? */
158 struct myri10ge_tx_buf tx; /* transmit ring */
159 struct myri10ge_rx_buf rx_small;
160 struct myri10ge_rx_buf rx_big;
161 struct myri10ge_rx_done rx_done;
162 int small_bytes;
dd50f336 163 int big_bytes;
0da34b6d
BG
164 struct net_device *dev;
165 struct net_device_stats stats;
166 u8 __iomem *sram;
167 int sram_size;
168 unsigned long board_span;
169 unsigned long iomem_base;
40f6cff5
AV
170 __be32 __iomem *irq_claim;
171 __be32 __iomem *irq_deassert;
0da34b6d
BG
172 char *mac_addr_string;
173 struct mcp_cmd_response *cmd;
174 dma_addr_t cmd_bus;
175 struct mcp_irq_data *fw_stats;
176 dma_addr_t fw_stats_bus;
177 struct pci_dev *pdev;
178 int msi_enabled;
40f6cff5 179 __be32 link_state;
0da34b6d
BG
180 unsigned int rdma_tags_available;
181 int intr_coal_delay;
40f6cff5 182 __be32 __iomem *intr_coal_delay_ptr;
0da34b6d
BG
183 int mtrr;
184 int wake_queue;
185 int stop_queue;
186 int down_cnt;
187 wait_queue_head_t down_wq;
188 struct work_struct watchdog_work;
189 struct timer_list watchdog_timer;
190 int watchdog_tx_done;
c54772e7 191 int watchdog_tx_req;
0da34b6d
BG
192 int watchdog_resets;
193 int tx_linearized;
194 int pause;
195 char *fw_name;
196 char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
197 char fw_version[128];
9dc6f0e7
BG
198 int fw_ver_major;
199 int fw_ver_minor;
200 int fw_ver_tiny;
201 int adopted_rx_filter_bug;
0da34b6d
BG
202 u8 mac_addr[6]; /* eeprom mac address */
203 unsigned long serial_number;
204 int vendor_specific_offset;
85a7ea1b 205 int fw_multicast_support;
0da34b6d
BG
206 u32 read_dma;
207 u32 write_dma;
208 u32 read_write_dma;
c58ac5ca
BG
209 u32 link_changes;
210 u32 msg_enable;
0da34b6d
BG
211};
212
213static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
214static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
215
216static char *myri10ge_fw_name = NULL;
217module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
218MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name\n");
219
220static int myri10ge_ecrc_enable = 1;
221module_param(myri10ge_ecrc_enable, int, S_IRUGO);
222MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E\n");
223
224static int myri10ge_max_intr_slots = 1024;
225module_param(myri10ge_max_intr_slots, int, S_IRUGO);
226MODULE_PARM_DESC(myri10ge_max_intr_slots, "Interrupt queue slots\n");
227
228static int myri10ge_small_bytes = -1; /* -1 == auto */
229module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
230MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets\n");
231
232static int myri10ge_msi = 1; /* enable msi by default */
3621cec5 233module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
0da34b6d
BG
234MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts\n");
235
236static int myri10ge_intr_coal_delay = 25;
237module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
238MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay\n");
239
240static int myri10ge_flow_control = 1;
241module_param(myri10ge_flow_control, int, S_IRUGO);
242MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter\n");
243
244static int myri10ge_deassert_wait = 1;
245module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
246MODULE_PARM_DESC(myri10ge_deassert_wait,
247 "Wait when deasserting legacy interrupts\n");
248
249static int myri10ge_force_firmware = 0;
250module_param(myri10ge_force_firmware, int, S_IRUGO);
251MODULE_PARM_DESC(myri10ge_force_firmware,
252 "Force firmware to assume aligned completions\n");
253
0da34b6d
BG
254static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
255module_param(myri10ge_initial_mtu, int, S_IRUGO);
256MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU\n");
257
258static int myri10ge_napi_weight = 64;
259module_param(myri10ge_napi_weight, int, S_IRUGO);
260MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight\n");
261
262static int myri10ge_watchdog_timeout = 1;
263module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
264MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout\n");
265
266static int myri10ge_max_irq_loops = 1048576;
267module_param(myri10ge_max_irq_loops, int, S_IRUGO);
268MODULE_PARM_DESC(myri10ge_max_irq_loops,
269 "Set stuck legacy IRQ detection threshold\n");
270
c58ac5ca
BG
271#define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
272
273static int myri10ge_debug = -1; /* defaults above */
274module_param(myri10ge_debug, int, 0);
275MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
276
dd50f336
BG
277static int myri10ge_fill_thresh = 256;
278module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
279MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed\n");
280
6ebc087a
BG
281static int myri10ge_wcfifo = 1;
282module_param(myri10ge_wcfifo, int, S_IRUGO);
283MODULE_PARM_DESC(myri10ge_wcfifo, "Enable WC Fifo when WC is enabled\n");
284
0da34b6d
BG
285#define MYRI10GE_FW_OFFSET 1024*1024
286#define MYRI10GE_HIGHPART_TO_U32(X) \
287(sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
288#define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
289
290#define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
291
6250223e 292static inline void put_be32(__be32 val, __be32 __iomem * p)
40f6cff5 293{
6250223e 294 __raw_writel((__force __u32) val, (__force void __iomem *)p);
40f6cff5
AV
295}
296
0da34b6d
BG
297static int
298myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
299 struct myri10ge_cmd *data, int atomic)
300{
301 struct mcp_cmd *buf;
302 char buf_bytes[sizeof(*buf) + 8];
303 struct mcp_cmd_response *response = mgp->cmd;
e700f9f4 304 char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
0da34b6d
BG
305 u32 dma_low, dma_high, result, value;
306 int sleep_total = 0;
307
308 /* ensure buf is aligned to 8 bytes */
309 buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
310
311 buf->data0 = htonl(data->data0);
312 buf->data1 = htonl(data->data1);
313 buf->data2 = htonl(data->data2);
314 buf->cmd = htonl(cmd);
315 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
316 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
317
318 buf->response_addr.low = htonl(dma_low);
319 buf->response_addr.high = htonl(dma_high);
40f6cff5 320 response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
0da34b6d
BG
321 mb();
322 myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
323
324 /* wait up to 15ms. Longest command is the DMA benchmark,
325 * which is capped at 5ms, but runs from a timeout handler
326 * that runs every 7.8ms. So a 15ms timeout leaves us with
327 * a 2.2ms margin
328 */
329 if (atomic) {
330 /* if atomic is set, do not sleep,
331 * and try to get the completion quickly
332 * (1ms will be enough for those commands) */
333 for (sleep_total = 0;
334 sleep_total < 1000
40f6cff5 335 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
0da34b6d
BG
336 sleep_total += 10)
337 udelay(10);
338 } else {
339 /* use msleep for most command */
340 for (sleep_total = 0;
341 sleep_total < 15
40f6cff5 342 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
0da34b6d
BG
343 sleep_total++)
344 msleep(1);
345 }
346
347 result = ntohl(response->result);
348 value = ntohl(response->data);
349 if (result != MYRI10GE_NO_RESPONSE_RESULT) {
350 if (result == 0) {
351 data->data0 = value;
352 return 0;
85a7ea1b
BG
353 } else if (result == MXGEFW_CMD_UNKNOWN) {
354 return -ENOSYS;
0da34b6d
BG
355 } else {
356 dev_err(&mgp->pdev->dev,
357 "command %d failed, result = %d\n",
358 cmd, result);
359 return -ENXIO;
360 }
361 }
362
363 dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
364 cmd, result);
365 return -EAGAIN;
366}
367
368/*
369 * The eeprom strings on the lanaiX have the format
370 * SN=x\0
371 * MAC=x:x:x:x:x:x\0
372 * PT:ddd mmm xx xx:xx:xx xx\0
373 * PV:ddd mmm xx xx:xx:xx xx\0
374 */
375static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
376{
377 char *ptr, *limit;
378 int i;
379
380 ptr = mgp->eeprom_strings;
381 limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
382
383 while (*ptr != '\0' && ptr < limit) {
384 if (memcmp(ptr, "MAC=", 4) == 0) {
385 ptr += 4;
386 mgp->mac_addr_string = ptr;
387 for (i = 0; i < 6; i++) {
388 if ((ptr + 2) > limit)
389 goto abort;
390 mgp->mac_addr[i] =
391 simple_strtoul(ptr, &ptr, 16);
392 ptr += 1;
393 }
394 }
395 if (memcmp((const void *)ptr, "SN=", 3) == 0) {
396 ptr += 3;
397 mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
398 }
399 while (ptr < limit && *ptr++) ;
400 }
401
402 return 0;
403
404abort:
405 dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
406 return -ENXIO;
407}
408
409/*
410 * Enable or disable periodic RDMAs from the host to make certain
411 * chipsets resend dropped PCIe messages
412 */
413
414static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
415{
416 char __iomem *submit;
40f6cff5 417 __be32 buf[16];
0da34b6d
BG
418 u32 dma_low, dma_high;
419 int i;
420
421 /* clear confirmation addr */
422 mgp->cmd->data = 0;
423 mb();
424
425 /* send a rdma command to the PCIe engine, and wait for the
426 * response in the confirmation address. The firmware should
427 * write a -1 there to indicate it is alive and well
428 */
429 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
430 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
431
432 buf[0] = htonl(dma_high); /* confirm addr MSW */
433 buf[1] = htonl(dma_low); /* confirm addr LSW */
40f6cff5 434 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
0da34b6d
BG
435 buf[3] = htonl(dma_high); /* dummy addr MSW */
436 buf[4] = htonl(dma_low); /* dummy addr LSW */
437 buf[5] = htonl(enable); /* enable? */
438
e700f9f4 439 submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
0da34b6d
BG
440
441 myri10ge_pio_copy(submit, &buf, sizeof(buf));
442 for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
443 msleep(1);
444 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
445 dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
446 (enable ? "enable" : "disable"));
447}
448
449static int
450myri10ge_validate_firmware(struct myri10ge_priv *mgp,
451 struct mcp_gen_header *hdr)
452{
453 struct device *dev = &mgp->pdev->dev;
0da34b6d
BG
454
455 /* check firmware type */
456 if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
457 dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
458 return -EINVAL;
459 }
460
461 /* save firmware version for ethtool */
462 strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
463
9dc6f0e7
BG
464 sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
465 &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
0da34b6d 466
9dc6f0e7
BG
467 if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR
468 && mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
0da34b6d
BG
469 dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
470 dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
471 MXGEFW_VERSION_MINOR);
472 return -EINVAL;
473 }
474 return 0;
475}
476
477static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
478{
479 unsigned crc, reread_crc;
480 const struct firmware *fw;
481 struct device *dev = &mgp->pdev->dev;
482 struct mcp_gen_header *hdr;
483 size_t hdr_offset;
484 int status;
e454358a 485 unsigned i;
0da34b6d
BG
486
487 if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
488 dev_err(dev, "Unable to load %s firmware image via hotplug\n",
489 mgp->fw_name);
490 status = -EINVAL;
491 goto abort_with_nothing;
492 }
493
494 /* check size */
495
496 if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
497 fw->size < MCP_HEADER_PTR_OFFSET + 4) {
498 dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
499 status = -EINVAL;
500 goto abort_with_fw;
501 }
502
503 /* check id */
40f6cff5 504 hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
0da34b6d
BG
505 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
506 dev_err(dev, "Bad firmware file\n");
507 status = -EINVAL;
508 goto abort_with_fw;
509 }
510 hdr = (void *)(fw->data + hdr_offset);
511
512 status = myri10ge_validate_firmware(mgp, hdr);
513 if (status != 0)
514 goto abort_with_fw;
515
516 crc = crc32(~0, fw->data, fw->size);
e454358a
BG
517 for (i = 0; i < fw->size; i += 256) {
518 myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
519 fw->data + i,
520 min(256U, (unsigned)(fw->size - i)));
521 mb();
522 readb(mgp->sram);
b10c0668 523 }
0da34b6d
BG
524 /* corruption checking is good for parity recovery and buggy chipset */
525 memcpy_fromio(fw->data, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
526 reread_crc = crc32(~0, fw->data, fw->size);
527 if (crc != reread_crc) {
528 dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
529 (unsigned)fw->size, reread_crc, crc);
530 status = -EIO;
531 goto abort_with_fw;
532 }
533 *size = (u32) fw->size;
534
535abort_with_fw:
536 release_firmware(fw);
537
538abort_with_nothing:
539 return status;
540}
541
542static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
543{
544 struct mcp_gen_header *hdr;
545 struct device *dev = &mgp->pdev->dev;
546 const size_t bytes = sizeof(struct mcp_gen_header);
547 size_t hdr_offset;
548 int status;
549
550 /* find running firmware header */
551 hdr_offset = ntohl(__raw_readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
552
553 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
554 dev_err(dev, "Running firmware has bad header offset (%d)\n",
555 (int)hdr_offset);
556 return -EIO;
557 }
558
559 /* copy header of running firmware from SRAM to host memory to
560 * validate firmware */
561 hdr = kmalloc(bytes, GFP_KERNEL);
562 if (hdr == NULL) {
563 dev_err(dev, "could not malloc firmware hdr\n");
564 return -ENOMEM;
565 }
566 memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
567 status = myri10ge_validate_firmware(mgp, hdr);
568 kfree(hdr);
9dc6f0e7
BG
569
570 /* check to see if adopted firmware has bug where adopting
571 * it will cause broadcasts to be filtered unless the NIC
572 * is kept in ALLMULTI mode */
573 if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
574 mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
575 mgp->adopted_rx_filter_bug = 1;
576 dev_warn(dev, "Adopting fw %d.%d.%d: "
577 "working around rx filter bug\n",
578 mgp->fw_ver_major, mgp->fw_ver_minor,
579 mgp->fw_ver_tiny);
580 }
0da34b6d
BG
581 return status;
582}
583
584static int myri10ge_load_firmware(struct myri10ge_priv *mgp)
585{
586 char __iomem *submit;
40f6cff5 587 __be32 buf[16];
0da34b6d
BG
588 u32 dma_low, dma_high, size;
589 int status, i;
590
b10c0668 591 size = 0;
0da34b6d
BG
592 status = myri10ge_load_hotplug_firmware(mgp, &size);
593 if (status) {
594 dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
595
596 /* Do not attempt to adopt firmware if there
597 * was a bad crc */
598 if (status == -EIO)
599 return status;
600
601 status = myri10ge_adopt_running_firmware(mgp);
602 if (status != 0) {
603 dev_err(&mgp->pdev->dev,
604 "failed to adopt running firmware\n");
605 return status;
606 }
607 dev_info(&mgp->pdev->dev,
608 "Successfully adopted running firmware\n");
609 if (mgp->tx.boundary == 4096) {
610 dev_warn(&mgp->pdev->dev,
611 "Using firmware currently running on NIC"
612 ". For optimal\n");
613 dev_warn(&mgp->pdev->dev,
614 "performance consider loading optimized "
615 "firmware\n");
616 dev_warn(&mgp->pdev->dev, "via hotplug\n");
617 }
618
619 mgp->fw_name = "adopted";
620 mgp->tx.boundary = 2048;
621 return status;
622 }
623
624 /* clear confirmation addr */
625 mgp->cmd->data = 0;
626 mb();
627
628 /* send a reload command to the bootstrap MCP, and wait for the
629 * response in the confirmation address. The firmware should
630 * write a -1 there to indicate it is alive and well
631 */
632 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
633 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
634
635 buf[0] = htonl(dma_high); /* confirm addr MSW */
636 buf[1] = htonl(dma_low); /* confirm addr LSW */
40f6cff5 637 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
0da34b6d
BG
638
639 /* FIX: All newest firmware should un-protect the bottom of
640 * the sram before handoff. However, the very first interfaces
641 * do not. Therefore the handoff copy must skip the first 8 bytes
642 */
643 buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
644 buf[4] = htonl(size - 8); /* length of code */
645 buf[5] = htonl(8); /* where to copy to */
646 buf[6] = htonl(0); /* where to jump to */
647
e700f9f4 648 submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
0da34b6d
BG
649
650 myri10ge_pio_copy(submit, &buf, sizeof(buf));
651 mb();
652 msleep(1);
653 mb();
654 i = 0;
655 while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20) {
656 msleep(1);
657 i++;
658 }
659 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
660 dev_err(&mgp->pdev->dev, "handoff failed\n");
661 return -ENXIO;
662 }
663 dev_info(&mgp->pdev->dev, "handoff confirmed\n");
9a71db72 664 myri10ge_dummy_rdma(mgp, 1);
0da34b6d
BG
665
666 return 0;
667}
668
669static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
670{
671 struct myri10ge_cmd cmd;
672 int status;
673
674 cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
675 | (addr[2] << 8) | addr[3]);
676
677 cmd.data1 = ((addr[4] << 8) | (addr[5]));
678
679 status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
680 return status;
681}
682
683static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
684{
685 struct myri10ge_cmd cmd;
686 int status, ctl;
687
688 ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
689 status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
690
691 if (status) {
692 printk(KERN_ERR
693 "myri10ge: %s: Failed to set flow control mode\n",
694 mgp->dev->name);
695 return status;
696 }
697 mgp->pause = pause;
698 return 0;
699}
700
701static void
702myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
703{
704 struct myri10ge_cmd cmd;
705 int status, ctl;
706
707 ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
708 status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
709 if (status)
710 printk(KERN_ERR "myri10ge: %s: Failed to set promisc mode\n",
711 mgp->dev->name);
712}
713
714static int myri10ge_reset(struct myri10ge_priv *mgp)
715{
716 struct myri10ge_cmd cmd;
717 int status;
718 size_t bytes;
719 u32 len;
720
721 /* try to send a reset command to the card to see if it
722 * is alive */
723 memset(&cmd, 0, sizeof(cmd));
724 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
725 if (status != 0) {
726 dev_err(&mgp->pdev->dev, "failed reset\n");
727 return -ENXIO;
728 }
729
730 /* Now exchange information about interrupts */
731
732 bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
733 memset(mgp->rx_done.entry, 0, bytes);
734 cmd.data0 = (u32) bytes;
735 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
736 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->rx_done.bus);
737 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->rx_done.bus);
738 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA, &cmd, 0);
739
740 status |=
741 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
40f6cff5 742 mgp->irq_claim = (__iomem __be32 *) (mgp->sram + cmd.data0);
df30a740
BG
743 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
744 &cmd, 0);
745 mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
0da34b6d 746
0da34b6d
BG
747 status |= myri10ge_send_cmd
748 (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
40f6cff5 749 mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
0da34b6d
BG
750 if (status != 0) {
751 dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
752 return status;
753 }
40f6cff5 754 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
0da34b6d
BG
755
756 /* Run a small DMA test.
757 * The magic multipliers to the length tell the firmware
758 * to do DMA read, write, or read+write tests. The
759 * results are returned in cmd.data0. The upper 16
760 * bits or the return is the number of transfers completed.
761 * The lower 16 bits is the time in 0.5us ticks that the
762 * transfers took to complete.
763 */
764
765 len = mgp->tx.boundary;
766
767 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->rx_done.bus);
768 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->rx_done.bus);
769 cmd.data2 = len * 0x10000;
770 status = myri10ge_send_cmd(mgp, MXGEFW_DMA_TEST, &cmd, 0);
771 if (status == 0)
772 mgp->read_dma = ((cmd.data0 >> 16) * len * 2) /
773 (cmd.data0 & 0xffff);
774 else
775 dev_warn(&mgp->pdev->dev, "DMA read benchmark failed: %d\n",
776 status);
777 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->rx_done.bus);
778 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->rx_done.bus);
779 cmd.data2 = len * 0x1;
780 status = myri10ge_send_cmd(mgp, MXGEFW_DMA_TEST, &cmd, 0);
781 if (status == 0)
782 mgp->write_dma = ((cmd.data0 >> 16) * len * 2) /
783 (cmd.data0 & 0xffff);
784 else
785 dev_warn(&mgp->pdev->dev, "DMA write benchmark failed: %d\n",
786 status);
787
788 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->rx_done.bus);
789 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->rx_done.bus);
790 cmd.data2 = len * 0x10001;
791 status = myri10ge_send_cmd(mgp, MXGEFW_DMA_TEST, &cmd, 0);
792 if (status == 0)
793 mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
794 (cmd.data0 & 0xffff);
795 else
796 dev_warn(&mgp->pdev->dev,
797 "DMA read/write benchmark failed: %d\n", status);
798
799 memset(mgp->rx_done.entry, 0, bytes);
800
801 /* reset mcp/driver shared state back to 0 */
802 mgp->tx.req = 0;
803 mgp->tx.done = 0;
804 mgp->tx.pkt_start = 0;
805 mgp->tx.pkt_done = 0;
806 mgp->rx_big.cnt = 0;
807 mgp->rx_small.cnt = 0;
808 mgp->rx_done.idx = 0;
809 mgp->rx_done.cnt = 0;
c58ac5ca 810 mgp->link_changes = 0;
0da34b6d
BG
811 status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
812 myri10ge_change_promisc(mgp, 0, 0);
813 myri10ge_change_pause(mgp, mgp->pause);
9dc6f0e7
BG
814 if (mgp->adopted_rx_filter_bug)
815 (void)myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
0da34b6d
BG
816 return status;
817}
818
819static inline void
820myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
821 struct mcp_kreq_ether_recv *src)
822{
40f6cff5 823 __be32 low;
0da34b6d
BG
824
825 low = src->addr_low;
40f6cff5 826 src->addr_low = htonl(DMA_32BIT_MASK);
e67bda55
BG
827 myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
828 mb();
829 myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
0da34b6d
BG
830 mb();
831 src->addr_low = low;
40f6cff5 832 put_be32(low, &dst->addr_low);
0da34b6d
BG
833 mb();
834}
835
40f6cff5 836static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
0da34b6d
BG
837{
838 struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
839
40f6cff5 840 if ((skb->protocol == htons(ETH_P_8021Q)) &&
0da34b6d
BG
841 (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
842 vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
843 skb->csum = hw_csum;
84fa7933 844 skb->ip_summed = CHECKSUM_COMPLETE;
0da34b6d
BG
845 }
846}
847
dd50f336
BG
848static inline void
849myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
850 struct skb_frag_struct *rx_frags, int len, int hlen)
851{
852 struct skb_frag_struct *skb_frags;
853
854 skb->len = skb->data_len = len;
855 skb->truesize = len + sizeof(struct sk_buff);
856 /* attach the page(s) */
857
858 skb_frags = skb_shinfo(skb)->frags;
859 while (len > 0) {
860 memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
861 len -= rx_frags->size;
862 skb_frags++;
863 rx_frags++;
864 skb_shinfo(skb)->nr_frags++;
865 }
866
867 /* pskb_may_pull is not available in irq context, but
868 * skb_pull() (for ether_pad and eth_type_trans()) requires
869 * the beginning of the packet in skb_headlen(), move it
870 * manually */
871 memcpy(skb->data, va, hlen);
872 skb_shinfo(skb)->frags[0].page_offset += hlen;
873 skb_shinfo(skb)->frags[0].size -= hlen;
874 skb->data_len -= hlen;
875 skb->tail += hlen;
876 skb_pull(skb, MXGEFW_PAD);
877}
878
879static void
880myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
881 int bytes, int watchdog)
882{
883 struct page *page;
884 int idx;
885
886 if (unlikely(rx->watchdog_needed && !watchdog))
887 return;
888
889 /* try to refill entire ring */
890 while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
891 idx = rx->fill_cnt & rx->mask;
892
893 if ((bytes < MYRI10GE_ALLOC_SIZE / 2) &&
894 (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE)) {
895 /* we can use part of previous page */
896 get_page(rx->page);
897 } else {
898 /* we need a new page */
899 page =
900 alloc_pages(GFP_ATOMIC | __GFP_COMP,
901 MYRI10GE_ALLOC_ORDER);
902 if (unlikely(page == NULL)) {
903 if (rx->fill_cnt - rx->cnt < 16)
904 rx->watchdog_needed = 1;
905 return;
906 }
907 rx->page = page;
908 rx->page_offset = 0;
909 rx->bus = pci_map_page(mgp->pdev, page, 0,
910 MYRI10GE_ALLOC_SIZE,
911 PCI_DMA_FROMDEVICE);
912 }
913 rx->info[idx].page = rx->page;
914 rx->info[idx].page_offset = rx->page_offset;
915 /* note that this is the address of the start of the
916 * page */
917 pci_unmap_addr_set(&rx->info[idx], bus, rx->bus);
918 rx->shadow[idx].addr_low =
919 htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
920 rx->shadow[idx].addr_high =
921 htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
922
923 /* start next packet on a cacheline boundary */
924 rx->page_offset += SKB_DATA_ALIGN(bytes);
925 rx->fill_cnt++;
926
927 /* copy 8 descriptors to the firmware at a time */
928 if ((idx & 7) == 7) {
929 if (rx->wc_fifo == NULL)
930 myri10ge_submit_8rx(&rx->lanai[idx - 7],
931 &rx->shadow[idx - 7]);
932 else {
933 mb();
934 myri10ge_pio_copy(rx->wc_fifo,
935 &rx->shadow[idx - 7], 64);
936 }
937 }
938 }
939}
940
941static inline void
942myri10ge_unmap_rx_page(struct pci_dev *pdev,
943 struct myri10ge_rx_buffer_state *info, int bytes)
944{
945 /* unmap the recvd page if we're the only or last user of it */
946 if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
947 (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
948 pci_unmap_page(pdev, (pci_unmap_addr(info, bus)
949 & ~(MYRI10GE_ALLOC_SIZE - 1)),
950 MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
951 }
952}
953
954#define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
955 * page into an skb */
956
957static inline int
52ea6fb3
BG
958myri10ge_rx_done(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
959 int bytes, int len, __wsum csum)
dd50f336
BG
960{
961 struct sk_buff *skb;
962 struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
963 int i, idx, hlen, remainder;
964 struct pci_dev *pdev = mgp->pdev;
965 struct net_device *dev = mgp->dev;
966 u8 *va;
967
968 len += MXGEFW_PAD;
969 idx = rx->cnt & rx->mask;
970 va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
971 prefetch(va);
972 /* Fill skb_frag_struct(s) with data from our receive */
973 for (i = 0, remainder = len; remainder > 0; i++) {
974 myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
975 rx_frags[i].page = rx->info[idx].page;
976 rx_frags[i].page_offset = rx->info[idx].page_offset;
977 if (remainder < MYRI10GE_ALLOC_SIZE)
978 rx_frags[i].size = remainder;
979 else
980 rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
981 rx->cnt++;
982 idx = rx->cnt & rx->mask;
983 remainder -= MYRI10GE_ALLOC_SIZE;
984 }
985
986 hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
987
988 /* allocate an skb to attach the page(s) to. */
989
990 skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
991 if (unlikely(skb == NULL)) {
992 mgp->stats.rx_dropped++;
993 do {
994 i--;
995 put_page(rx_frags[i].page);
996 } while (i != 0);
997 return 0;
998 }
999
1000 /* Attach the pages to the skb, and trim off any padding */
1001 myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
1002 if (skb_shinfo(skb)->frags[0].size <= 0) {
1003 put_page(skb_shinfo(skb)->frags[0].page);
1004 skb_shinfo(skb)->nr_frags = 0;
1005 }
1006 skb->protocol = eth_type_trans(skb, dev);
1007 skb->dev = dev;
1008
1009 if (mgp->csum_flag) {
1010 if ((skb->protocol == htons(ETH_P_IP)) ||
1011 (skb->protocol == htons(ETH_P_IPV6))) {
1012 skb->csum = csum;
1013 skb->ip_summed = CHECKSUM_COMPLETE;
1014 } else
1015 myri10ge_vlan_ip_csum(skb, csum);
1016 }
1017 netif_receive_skb(skb);
1018 dev->last_rx = jiffies;
1019 return 1;
1020}
1021
0da34b6d
BG
1022static inline void myri10ge_tx_done(struct myri10ge_priv *mgp, int mcp_index)
1023{
1024 struct pci_dev *pdev = mgp->pdev;
1025 struct myri10ge_tx_buf *tx = &mgp->tx;
1026 struct sk_buff *skb;
1027 int idx, len;
1028 int limit = 0;
1029
1030 while (tx->pkt_done != mcp_index) {
1031 idx = tx->done & tx->mask;
1032 skb = tx->info[idx].skb;
1033
1034 /* Mark as free */
1035 tx->info[idx].skb = NULL;
1036 if (tx->info[idx].last) {
1037 tx->pkt_done++;
1038 tx->info[idx].last = 0;
1039 }
1040 tx->done++;
1041 len = pci_unmap_len(&tx->info[idx], len);
1042 pci_unmap_len_set(&tx->info[idx], len, 0);
1043 if (skb) {
1044 mgp->stats.tx_bytes += skb->len;
1045 mgp->stats.tx_packets++;
1046 dev_kfree_skb_irq(skb);
1047 if (len)
1048 pci_unmap_single(pdev,
1049 pci_unmap_addr(&tx->info[idx],
1050 bus), len,
1051 PCI_DMA_TODEVICE);
1052 } else {
1053 if (len)
1054 pci_unmap_page(pdev,
1055 pci_unmap_addr(&tx->info[idx],
1056 bus), len,
1057 PCI_DMA_TODEVICE);
1058 }
1059
1060 /* limit potential for livelock by only handling
1061 * 2 full tx rings per call */
1062 if (unlikely(++limit > 2 * tx->mask))
1063 break;
1064 }
1065 /* start the queue if we've stopped it */
1066 if (netif_queue_stopped(mgp->dev)
1067 && tx->req - tx->done < (tx->mask >> 1)) {
1068 mgp->wake_queue++;
1069 netif_wake_queue(mgp->dev);
1070 }
1071}
1072
1073static inline void myri10ge_clean_rx_done(struct myri10ge_priv *mgp, int *limit)
1074{
1075 struct myri10ge_rx_done *rx_done = &mgp->rx_done;
1076 unsigned long rx_bytes = 0;
1077 unsigned long rx_packets = 0;
1078 unsigned long rx_ok;
1079
1080 int idx = rx_done->idx;
1081 int cnt = rx_done->cnt;
1082 u16 length;
40f6cff5 1083 __wsum checksum;
0da34b6d
BG
1084
1085 while (rx_done->entry[idx].length != 0 && *limit != 0) {
1086 length = ntohs(rx_done->entry[idx].length);
1087 rx_done->entry[idx].length = 0;
40f6cff5 1088 checksum = csum_unfold(rx_done->entry[idx].checksum);
0da34b6d 1089 if (length <= mgp->small_bytes)
52ea6fb3
BG
1090 rx_ok = myri10ge_rx_done(mgp, &mgp->rx_small,
1091 mgp->small_bytes,
1092 length, checksum);
0da34b6d 1093 else
52ea6fb3
BG
1094 rx_ok = myri10ge_rx_done(mgp, &mgp->rx_big,
1095 mgp->big_bytes,
1096 length, checksum);
0da34b6d
BG
1097 rx_packets += rx_ok;
1098 rx_bytes += rx_ok * (unsigned long)length;
1099 cnt++;
1100 idx = cnt & (myri10ge_max_intr_slots - 1);
1101
1102 /* limit potential for livelock by only handling a
1103 * limited number of frames. */
1104 (*limit)--;
1105 }
1106 rx_done->idx = idx;
1107 rx_done->cnt = cnt;
1108 mgp->stats.rx_packets += rx_packets;
1109 mgp->stats.rx_bytes += rx_bytes;
c7dab99b
BG
1110
1111 /* restock receive rings if needed */
1112 if (mgp->rx_small.fill_cnt - mgp->rx_small.cnt < myri10ge_fill_thresh)
1113 myri10ge_alloc_rx_pages(mgp, &mgp->rx_small,
1114 mgp->small_bytes + MXGEFW_PAD, 0);
1115 if (mgp->rx_big.fill_cnt - mgp->rx_big.cnt < myri10ge_fill_thresh)
1116 myri10ge_alloc_rx_pages(mgp, &mgp->rx_big, mgp->big_bytes, 0);
1117
0da34b6d
BG
1118}
1119
1120static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
1121{
1122 struct mcp_irq_data *stats = mgp->fw_stats;
1123
1124 if (unlikely(stats->stats_updated)) {
1125 if (mgp->link_state != stats->link_up) {
1126 mgp->link_state = stats->link_up;
1127 if (mgp->link_state) {
c58ac5ca
BG
1128 if (netif_msg_link(mgp))
1129 printk(KERN_INFO
1130 "myri10ge: %s: link up\n",
1131 mgp->dev->name);
0da34b6d 1132 netif_carrier_on(mgp->dev);
c58ac5ca 1133 mgp->link_changes++;
0da34b6d 1134 } else {
c58ac5ca
BG
1135 if (netif_msg_link(mgp))
1136 printk(KERN_INFO
1137 "myri10ge: %s: link down\n",
1138 mgp->dev->name);
0da34b6d 1139 netif_carrier_off(mgp->dev);
c58ac5ca 1140 mgp->link_changes++;
0da34b6d
BG
1141 }
1142 }
1143 if (mgp->rdma_tags_available !=
1144 ntohl(mgp->fw_stats->rdma_tags_available)) {
1145 mgp->rdma_tags_available =
1146 ntohl(mgp->fw_stats->rdma_tags_available);
1147 printk(KERN_WARNING "myri10ge: %s: RDMA timed out! "
1148 "%d tags left\n", mgp->dev->name,
1149 mgp->rdma_tags_available);
1150 }
1151 mgp->down_cnt += stats->link_down;
1152 if (stats->link_down)
1153 wake_up(&mgp->down_wq);
1154 }
1155}
1156
1157static int myri10ge_poll(struct net_device *netdev, int *budget)
1158{
1159 struct myri10ge_priv *mgp = netdev_priv(netdev);
1160 struct myri10ge_rx_done *rx_done = &mgp->rx_done;
1161 int limit, orig_limit, work_done;
1162
1163 /* process as many rx events as NAPI will allow */
1164 limit = min(*budget, netdev->quota);
1165 orig_limit = limit;
1166 myri10ge_clean_rx_done(mgp, &limit);
1167 work_done = orig_limit - limit;
1168 *budget -= work_done;
1169 netdev->quota -= work_done;
1170
1171 if (rx_done->entry[rx_done->idx].length == 0 || !netif_running(netdev)) {
1172 netif_rx_complete(netdev);
40f6cff5 1173 put_be32(htonl(3), mgp->irq_claim);
0da34b6d
BG
1174 return 0;
1175 }
1176 return 1;
1177}
1178
7d12e780 1179static irqreturn_t myri10ge_intr(int irq, void *arg)
0da34b6d
BG
1180{
1181 struct myri10ge_priv *mgp = arg;
1182 struct mcp_irq_data *stats = mgp->fw_stats;
1183 struct myri10ge_tx_buf *tx = &mgp->tx;
1184 u32 send_done_count;
1185 int i;
1186
1187 /* make sure it is our IRQ, and that the DMA has finished */
1188 if (unlikely(!stats->valid))
1189 return (IRQ_NONE);
1190
1191 /* low bit indicates receives are present, so schedule
1192 * napi poll handler */
1193 if (stats->valid & 1)
1194 netif_rx_schedule(mgp->dev);
1195
1196 if (!mgp->msi_enabled) {
40f6cff5 1197 put_be32(0, mgp->irq_deassert);
0da34b6d
BG
1198 if (!myri10ge_deassert_wait)
1199 stats->valid = 0;
1200 mb();
1201 } else
1202 stats->valid = 0;
1203
1204 /* Wait for IRQ line to go low, if using INTx */
1205 i = 0;
1206 while (1) {
1207 i++;
1208 /* check for transmit completes and receives */
1209 send_done_count = ntohl(stats->send_done_count);
1210 if (send_done_count != tx->pkt_done)
1211 myri10ge_tx_done(mgp, (int)send_done_count);
1212 if (unlikely(i > myri10ge_max_irq_loops)) {
1213 printk(KERN_WARNING "myri10ge: %s: irq stuck?\n",
1214 mgp->dev->name);
1215 stats->valid = 0;
1216 schedule_work(&mgp->watchdog_work);
1217 }
1218 if (likely(stats->valid == 0))
1219 break;
1220 cpu_relax();
1221 barrier();
1222 }
1223
1224 myri10ge_check_statblock(mgp);
1225
40f6cff5 1226 put_be32(htonl(3), mgp->irq_claim + 1);
0da34b6d
BG
1227 return (IRQ_HANDLED);
1228}
1229
1230static int
1231myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1232{
1233 cmd->autoneg = AUTONEG_DISABLE;
1234 cmd->speed = SPEED_10000;
1235 cmd->duplex = DUPLEX_FULL;
1236 return 0;
1237}
1238
1239static void
1240myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
1241{
1242 struct myri10ge_priv *mgp = netdev_priv(netdev);
1243
1244 strlcpy(info->driver, "myri10ge", sizeof(info->driver));
1245 strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
1246 strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
1247 strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
1248}
1249
1250static int
1251myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1252{
1253 struct myri10ge_priv *mgp = netdev_priv(netdev);
1254 coal->rx_coalesce_usecs = mgp->intr_coal_delay;
1255 return 0;
1256}
1257
1258static int
1259myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1260{
1261 struct myri10ge_priv *mgp = netdev_priv(netdev);
1262
1263 mgp->intr_coal_delay = coal->rx_coalesce_usecs;
40f6cff5 1264 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
0da34b6d
BG
1265 return 0;
1266}
1267
1268static void
1269myri10ge_get_pauseparam(struct net_device *netdev,
1270 struct ethtool_pauseparam *pause)
1271{
1272 struct myri10ge_priv *mgp = netdev_priv(netdev);
1273
1274 pause->autoneg = 0;
1275 pause->rx_pause = mgp->pause;
1276 pause->tx_pause = mgp->pause;
1277}
1278
1279static int
1280myri10ge_set_pauseparam(struct net_device *netdev,
1281 struct ethtool_pauseparam *pause)
1282{
1283 struct myri10ge_priv *mgp = netdev_priv(netdev);
1284
1285 if (pause->tx_pause != mgp->pause)
1286 return myri10ge_change_pause(mgp, pause->tx_pause);
1287 if (pause->rx_pause != mgp->pause)
1288 return myri10ge_change_pause(mgp, pause->tx_pause);
1289 if (pause->autoneg != 0)
1290 return -EINVAL;
1291 return 0;
1292}
1293
1294static void
1295myri10ge_get_ringparam(struct net_device *netdev,
1296 struct ethtool_ringparam *ring)
1297{
1298 struct myri10ge_priv *mgp = netdev_priv(netdev);
1299
1300 ring->rx_mini_max_pending = mgp->rx_small.mask + 1;
1301 ring->rx_max_pending = mgp->rx_big.mask + 1;
1302 ring->rx_jumbo_max_pending = 0;
1303 ring->tx_max_pending = mgp->rx_small.mask + 1;
1304 ring->rx_mini_pending = ring->rx_mini_max_pending;
1305 ring->rx_pending = ring->rx_max_pending;
1306 ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
1307 ring->tx_pending = ring->tx_max_pending;
1308}
1309
1310static u32 myri10ge_get_rx_csum(struct net_device *netdev)
1311{
1312 struct myri10ge_priv *mgp = netdev_priv(netdev);
1313 if (mgp->csum_flag)
1314 return 1;
1315 else
1316 return 0;
1317}
1318
1319static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
1320{
1321 struct myri10ge_priv *mgp = netdev_priv(netdev);
1322 if (csum_enabled)
1323 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
1324 else
1325 mgp->csum_flag = 0;
1326 return 0;
1327}
1328
1329static const char myri10ge_gstrings_stats[][ETH_GSTRING_LEN] = {
1330 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
1331 "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
1332 "rx_length_errors", "rx_over_errors", "rx_crc_errors",
1333 "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
1334 "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
1335 "tx_heartbeat_errors", "tx_window_errors",
1336 /* device-specific stats */
2c1a1088 1337 "tx_boundary", "WC", "irq", "MSI",
0da34b6d
BG
1338 "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
1339 "serial_number", "tx_pkt_start", "tx_pkt_done",
1340 "tx_req", "tx_done", "rx_small_cnt", "rx_big_cnt",
1341 "wake_queue", "stop_queue", "watchdog_resets", "tx_linearized",
c58ac5ca 1342 "link_changes", "link_up", "dropped_link_overflow",
85a7ea1b 1343 "dropped_link_error_or_filtered", "dropped_multicast_filtered",
0da34b6d
BG
1344 "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
1345 "dropped_no_big_buffer"
1346};
1347
1348#define MYRI10GE_NET_STATS_LEN 21
1349#define MYRI10GE_STATS_LEN sizeof(myri10ge_gstrings_stats) / ETH_GSTRING_LEN
1350
1351static void
1352myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
1353{
1354 switch (stringset) {
1355 case ETH_SS_STATS:
1356 memcpy(data, *myri10ge_gstrings_stats,
1357 sizeof(myri10ge_gstrings_stats));
1358 break;
1359 }
1360}
1361
1362static int myri10ge_get_stats_count(struct net_device *netdev)
1363{
1364 return MYRI10GE_STATS_LEN;
1365}
1366
1367static void
1368myri10ge_get_ethtool_stats(struct net_device *netdev,
1369 struct ethtool_stats *stats, u64 * data)
1370{
1371 struct myri10ge_priv *mgp = netdev_priv(netdev);
1372 int i;
1373
1374 for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1375 data[i] = ((unsigned long *)&mgp->stats)[i];
1376
2c1a1088
BG
1377 data[i++] = (unsigned int)mgp->tx.boundary;
1378 data[i++] = (unsigned int)(mgp->mtrr >= 0);
1379 data[i++] = (unsigned int)mgp->pdev->irq;
1380 data[i++] = (unsigned int)mgp->msi_enabled;
0da34b6d
BG
1381 data[i++] = (unsigned int)mgp->read_dma;
1382 data[i++] = (unsigned int)mgp->write_dma;
1383 data[i++] = (unsigned int)mgp->read_write_dma;
1384 data[i++] = (unsigned int)mgp->serial_number;
1385 data[i++] = (unsigned int)mgp->tx.pkt_start;
1386 data[i++] = (unsigned int)mgp->tx.pkt_done;
1387 data[i++] = (unsigned int)mgp->tx.req;
1388 data[i++] = (unsigned int)mgp->tx.done;
1389 data[i++] = (unsigned int)mgp->rx_small.cnt;
1390 data[i++] = (unsigned int)mgp->rx_big.cnt;
1391 data[i++] = (unsigned int)mgp->wake_queue;
1392 data[i++] = (unsigned int)mgp->stop_queue;
1393 data[i++] = (unsigned int)mgp->watchdog_resets;
1394 data[i++] = (unsigned int)mgp->tx_linearized;
c58ac5ca 1395 data[i++] = (unsigned int)mgp->link_changes;
0da34b6d
BG
1396 data[i++] = (unsigned int)ntohl(mgp->fw_stats->link_up);
1397 data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_link_overflow);
1398 data[i++] =
1399 (unsigned int)ntohl(mgp->fw_stats->dropped_link_error_or_filtered);
85a7ea1b
BG
1400 data[i++] =
1401 (unsigned int)ntohl(mgp->fw_stats->dropped_multicast_filtered);
0da34b6d
BG
1402 data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_runt);
1403 data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_overrun);
1404 data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_no_small_buffer);
1405 data[i++] = (unsigned int)ntohl(mgp->fw_stats->dropped_no_big_buffer);
1406}
1407
c58ac5ca
BG
1408static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
1409{
1410 struct myri10ge_priv *mgp = netdev_priv(netdev);
1411 mgp->msg_enable = value;
1412}
1413
1414static u32 myri10ge_get_msglevel(struct net_device *netdev)
1415{
1416 struct myri10ge_priv *mgp = netdev_priv(netdev);
1417 return mgp->msg_enable;
1418}
1419
7282d491 1420static const struct ethtool_ops myri10ge_ethtool_ops = {
0da34b6d
BG
1421 .get_settings = myri10ge_get_settings,
1422 .get_drvinfo = myri10ge_get_drvinfo,
1423 .get_coalesce = myri10ge_get_coalesce,
1424 .set_coalesce = myri10ge_set_coalesce,
1425 .get_pauseparam = myri10ge_get_pauseparam,
1426 .set_pauseparam = myri10ge_set_pauseparam,
1427 .get_ringparam = myri10ge_get_ringparam,
1428 .get_rx_csum = myri10ge_get_rx_csum,
1429 .set_rx_csum = myri10ge_set_rx_csum,
1430 .get_tx_csum = ethtool_op_get_tx_csum,
b10c0668 1431 .set_tx_csum = ethtool_op_set_tx_hw_csum,
0da34b6d
BG
1432 .get_sg = ethtool_op_get_sg,
1433 .set_sg = ethtool_op_set_sg,
0da34b6d
BG
1434 .get_tso = ethtool_op_get_tso,
1435 .set_tso = ethtool_op_set_tso,
0da34b6d
BG
1436 .get_strings = myri10ge_get_strings,
1437 .get_stats_count = myri10ge_get_stats_count,
c58ac5ca
BG
1438 .get_ethtool_stats = myri10ge_get_ethtool_stats,
1439 .set_msglevel = myri10ge_set_msglevel,
1440 .get_msglevel = myri10ge_get_msglevel
0da34b6d
BG
1441};
1442
1443static int myri10ge_allocate_rings(struct net_device *dev)
1444{
1445 struct myri10ge_priv *mgp;
1446 struct myri10ge_cmd cmd;
1447 int tx_ring_size, rx_ring_size;
1448 int tx_ring_entries, rx_ring_entries;
1449 int i, status;
1450 size_t bytes;
1451
1452 mgp = netdev_priv(dev);
1453
1454 /* get ring sizes */
1455
1456 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
1457 tx_ring_size = cmd.data0;
1458 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
355c7265
BG
1459 if (status != 0)
1460 return status;
0da34b6d
BG
1461 rx_ring_size = cmd.data0;
1462
1463 tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
1464 rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
1465 mgp->tx.mask = tx_ring_entries - 1;
1466 mgp->rx_small.mask = mgp->rx_big.mask = rx_ring_entries - 1;
1467
355c7265
BG
1468 status = -ENOMEM;
1469
0da34b6d
BG
1470 /* allocate the host shadow rings */
1471
1472 bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
1473 * sizeof(*mgp->tx.req_list);
1474 mgp->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
1475 if (mgp->tx.req_bytes == NULL)
1476 goto abort_with_nothing;
1477
1478 /* ensure req_list entries are aligned to 8 bytes */
1479 mgp->tx.req_list = (struct mcp_kreq_ether_send *)
1480 ALIGN((unsigned long)mgp->tx.req_bytes, 8);
1481
1482 bytes = rx_ring_entries * sizeof(*mgp->rx_small.shadow);
1483 mgp->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
1484 if (mgp->rx_small.shadow == NULL)
1485 goto abort_with_tx_req_bytes;
1486
1487 bytes = rx_ring_entries * sizeof(*mgp->rx_big.shadow);
1488 mgp->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
1489 if (mgp->rx_big.shadow == NULL)
1490 goto abort_with_rx_small_shadow;
1491
1492 /* allocate the host info rings */
1493
1494 bytes = tx_ring_entries * sizeof(*mgp->tx.info);
1495 mgp->tx.info = kzalloc(bytes, GFP_KERNEL);
1496 if (mgp->tx.info == NULL)
1497 goto abort_with_rx_big_shadow;
1498
1499 bytes = rx_ring_entries * sizeof(*mgp->rx_small.info);
1500 mgp->rx_small.info = kzalloc(bytes, GFP_KERNEL);
1501 if (mgp->rx_small.info == NULL)
1502 goto abort_with_tx_info;
1503
1504 bytes = rx_ring_entries * sizeof(*mgp->rx_big.info);
1505 mgp->rx_big.info = kzalloc(bytes, GFP_KERNEL);
1506 if (mgp->rx_big.info == NULL)
1507 goto abort_with_rx_small_info;
1508
1509 /* Fill the receive rings */
c7dab99b
BG
1510 mgp->rx_big.cnt = 0;
1511 mgp->rx_small.cnt = 0;
1512 mgp->rx_big.fill_cnt = 0;
1513 mgp->rx_small.fill_cnt = 0;
1514 mgp->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
1515 mgp->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
1516 mgp->rx_small.watchdog_needed = 0;
1517 mgp->rx_big.watchdog_needed = 0;
1518 myri10ge_alloc_rx_pages(mgp, &mgp->rx_small,
1519 mgp->small_bytes + MXGEFW_PAD, 0);
0da34b6d 1520
c7dab99b
BG
1521 if (mgp->rx_small.fill_cnt < mgp->rx_small.mask + 1) {
1522 printk(KERN_ERR "myri10ge: %s: alloced only %d small bufs\n",
1523 dev->name, mgp->rx_small.fill_cnt);
1524 goto abort_with_rx_small_ring;
0da34b6d
BG
1525 }
1526
c7dab99b
BG
1527 myri10ge_alloc_rx_pages(mgp, &mgp->rx_big, mgp->big_bytes, 0);
1528 if (mgp->rx_big.fill_cnt < mgp->rx_big.mask + 1) {
1529 printk(KERN_ERR "myri10ge: %s: alloced only %d big bufs\n",
1530 dev->name, mgp->rx_big.fill_cnt);
1531 goto abort_with_rx_big_ring;
0da34b6d
BG
1532 }
1533
1534 return 0;
1535
1536abort_with_rx_big_ring:
c7dab99b
BG
1537 for (i = mgp->rx_big.cnt; i < mgp->rx_big.fill_cnt; i++) {
1538 int idx = i & mgp->rx_big.mask;
1539 myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_big.info[idx],
1540 mgp->big_bytes);
1541 put_page(mgp->rx_big.info[idx].page);
0da34b6d
BG
1542 }
1543
1544abort_with_rx_small_ring:
c7dab99b
BG
1545 for (i = mgp->rx_small.cnt; i < mgp->rx_small.fill_cnt; i++) {
1546 int idx = i & mgp->rx_small.mask;
1547 myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_small.info[idx],
1548 mgp->small_bytes + MXGEFW_PAD);
1549 put_page(mgp->rx_small.info[idx].page);
0da34b6d 1550 }
c7dab99b 1551
0da34b6d
BG
1552 kfree(mgp->rx_big.info);
1553
1554abort_with_rx_small_info:
1555 kfree(mgp->rx_small.info);
1556
1557abort_with_tx_info:
1558 kfree(mgp->tx.info);
1559
1560abort_with_rx_big_shadow:
1561 kfree(mgp->rx_big.shadow);
1562
1563abort_with_rx_small_shadow:
1564 kfree(mgp->rx_small.shadow);
1565
1566abort_with_tx_req_bytes:
1567 kfree(mgp->tx.req_bytes);
1568 mgp->tx.req_bytes = NULL;
1569 mgp->tx.req_list = NULL;
1570
1571abort_with_nothing:
1572 return status;
1573}
1574
1575static void myri10ge_free_rings(struct net_device *dev)
1576{
1577 struct myri10ge_priv *mgp;
1578 struct sk_buff *skb;
1579 struct myri10ge_tx_buf *tx;
1580 int i, len, idx;
1581
1582 mgp = netdev_priv(dev);
1583
c7dab99b
BG
1584 for (i = mgp->rx_big.cnt; i < mgp->rx_big.fill_cnt; i++) {
1585 idx = i & mgp->rx_big.mask;
1586 if (i == mgp->rx_big.fill_cnt - 1)
1587 mgp->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
1588 myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_big.info[idx],
1589 mgp->big_bytes);
1590 put_page(mgp->rx_big.info[idx].page);
0da34b6d
BG
1591 }
1592
c7dab99b
BG
1593 for (i = mgp->rx_small.cnt; i < mgp->rx_small.fill_cnt; i++) {
1594 idx = i & mgp->rx_small.mask;
1595 if (i == mgp->rx_small.fill_cnt - 1)
1596 mgp->rx_small.info[idx].page_offset =
1597 MYRI10GE_ALLOC_SIZE;
1598 myri10ge_unmap_rx_page(mgp->pdev, &mgp->rx_small.info[idx],
1599 mgp->small_bytes + MXGEFW_PAD);
1600 put_page(mgp->rx_small.info[idx].page);
1601 }
0da34b6d
BG
1602 tx = &mgp->tx;
1603 while (tx->done != tx->req) {
1604 idx = tx->done & tx->mask;
1605 skb = tx->info[idx].skb;
1606
1607 /* Mark as free */
1608 tx->info[idx].skb = NULL;
1609 tx->done++;
1610 len = pci_unmap_len(&tx->info[idx], len);
1611 pci_unmap_len_set(&tx->info[idx], len, 0);
1612 if (skb) {
1613 mgp->stats.tx_dropped++;
1614 dev_kfree_skb_any(skb);
1615 if (len)
1616 pci_unmap_single(mgp->pdev,
1617 pci_unmap_addr(&tx->info[idx],
1618 bus), len,
1619 PCI_DMA_TODEVICE);
1620 } else {
1621 if (len)
1622 pci_unmap_page(mgp->pdev,
1623 pci_unmap_addr(&tx->info[idx],
1624 bus), len,
1625 PCI_DMA_TODEVICE);
1626 }
1627 }
1628 kfree(mgp->rx_big.info);
1629
1630 kfree(mgp->rx_small.info);
1631
1632 kfree(mgp->tx.info);
1633
1634 kfree(mgp->rx_big.shadow);
1635
1636 kfree(mgp->rx_small.shadow);
1637
1638 kfree(mgp->tx.req_bytes);
1639 mgp->tx.req_bytes = NULL;
1640 mgp->tx.req_list = NULL;
1641}
1642
df30a740
BG
1643static int myri10ge_request_irq(struct myri10ge_priv *mgp)
1644{
1645 struct pci_dev *pdev = mgp->pdev;
1646 int status;
1647
1648 if (myri10ge_msi) {
1649 status = pci_enable_msi(pdev);
1650 if (status != 0)
1651 dev_err(&pdev->dev,
1652 "Error %d setting up MSI; falling back to xPIC\n",
1653 status);
1654 else
1655 mgp->msi_enabled = 1;
1656 } else {
1657 mgp->msi_enabled = 0;
1658 }
1659 status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
1660 mgp->dev->name, mgp);
1661 if (status != 0) {
1662 dev_err(&pdev->dev, "failed to allocate IRQ\n");
1663 if (mgp->msi_enabled)
1664 pci_disable_msi(pdev);
1665 }
1666 return status;
1667}
1668
1669static void myri10ge_free_irq(struct myri10ge_priv *mgp)
1670{
1671 struct pci_dev *pdev = mgp->pdev;
1672
1673 free_irq(pdev->irq, mgp);
1674 if (mgp->msi_enabled)
1675 pci_disable_msi(pdev);
1676}
1677
0da34b6d
BG
1678static int myri10ge_open(struct net_device *dev)
1679{
1680 struct myri10ge_priv *mgp;
1681 struct myri10ge_cmd cmd;
1682 int status, big_pow2;
1683
1684 mgp = netdev_priv(dev);
1685
1686 if (mgp->running != MYRI10GE_ETH_STOPPED)
1687 return -EBUSY;
1688
1689 mgp->running = MYRI10GE_ETH_STARTING;
1690 status = myri10ge_reset(mgp);
1691 if (status != 0) {
1692 printk(KERN_ERR "myri10ge: %s: failed reset\n", dev->name);
df30a740 1693 goto abort_with_nothing;
0da34b6d
BG
1694 }
1695
df30a740
BG
1696 status = myri10ge_request_irq(mgp);
1697 if (status != 0)
1698 goto abort_with_nothing;
1699
0da34b6d
BG
1700 /* decide what small buffer size to use. For good TCP rx
1701 * performance, it is important to not receive 1514 byte
1702 * frames into jumbo buffers, as it confuses the socket buffer
1703 * accounting code, leading to drops and erratic performance.
1704 */
1705
1706 if (dev->mtu <= ETH_DATA_LEN)
c7dab99b
BG
1707 /* enough for a TCP header */
1708 mgp->small_bytes = (128 > SMP_CACHE_BYTES)
1709 ? (128 - MXGEFW_PAD)
1710 : (SMP_CACHE_BYTES - MXGEFW_PAD);
0da34b6d 1711 else
de3c4507
BG
1712 /* enough for a vlan encapsulated ETH_DATA_LEN frame */
1713 mgp->small_bytes = VLAN_ETH_FRAME_LEN;
0da34b6d
BG
1714
1715 /* Override the small buffer size? */
1716 if (myri10ge_small_bytes > 0)
1717 mgp->small_bytes = myri10ge_small_bytes;
1718
0da34b6d
BG
1719 /* get the lanai pointers to the send and receive rings */
1720
1721 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET, &cmd, 0);
1722 mgp->tx.lanai =
1723 (struct mcp_kreq_ether_send __iomem *)(mgp->sram + cmd.data0);
1724
1725 status |=
1726 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd, 0);
1727 mgp->rx_small.lanai =
1728 (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
1729
1730 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
1731 mgp->rx_big.lanai =
1732 (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
1733
1734 if (status != 0) {
1735 printk(KERN_ERR
1736 "myri10ge: %s: failed to get ring sizes or locations\n",
1737 dev->name);
1738 mgp->running = MYRI10GE_ETH_STOPPED;
df30a740 1739 goto abort_with_irq;
0da34b6d
BG
1740 }
1741
6ebc087a 1742 if (myri10ge_wcfifo && mgp->mtrr >= 0) {
e700f9f4
BG
1743 mgp->tx.wc_fifo = (u8 __iomem *) mgp->sram + MXGEFW_ETH_SEND_4;
1744 mgp->rx_small.wc_fifo =
1745 (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_SMALL;
1746 mgp->rx_big.wc_fifo =
1747 (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_BIG;
0da34b6d
BG
1748 } else {
1749 mgp->tx.wc_fifo = NULL;
1750 mgp->rx_small.wc_fifo = NULL;
1751 mgp->rx_big.wc_fifo = NULL;
1752 }
1753
0da34b6d
BG
1754 /* Firmware needs the big buff size as a power of 2. Lie and
1755 * tell him the buffer is larger, because we only use 1
1756 * buffer/pkt, and the mtu will prevent overruns.
1757 */
13348bee 1758 big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
c7dab99b
BG
1759 if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
1760 while ((big_pow2 & (big_pow2 - 1)) != 0)
1761 big_pow2++;
13348bee 1762 mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
c7dab99b
BG
1763 } else {
1764 big_pow2 = MYRI10GE_ALLOC_SIZE;
1765 mgp->big_bytes = big_pow2;
1766 }
1767
1768 status = myri10ge_allocate_rings(dev);
1769 if (status != 0)
df30a740 1770 goto abort_with_irq;
0da34b6d
BG
1771
1772 /* now give firmware buffers sizes, and MTU */
1773 cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
1774 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
1775 cmd.data0 = mgp->small_bytes;
1776 status |=
1777 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
1778 cmd.data0 = big_pow2;
1779 status |=
1780 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
1781 if (status) {
1782 printk(KERN_ERR "myri10ge: %s: Couldn't set buffer sizes\n",
1783 dev->name);
1784 goto abort_with_rings;
1785 }
1786
1787 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->fw_stats_bus);
1788 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->fw_stats_bus);
85a7ea1b
BG
1789 cmd.data2 = sizeof(struct mcp_irq_data);
1790 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
1791 if (status == -ENOSYS) {
1792 dma_addr_t bus = mgp->fw_stats_bus;
1793 bus += offsetof(struct mcp_irq_data, send_done_count);
1794 cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
1795 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
1796 status = myri10ge_send_cmd(mgp,
1797 MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
1798 &cmd, 0);
1799 /* Firmware cannot support multicast without STATS_DMA_V2 */
1800 mgp->fw_multicast_support = 0;
1801 } else {
1802 mgp->fw_multicast_support = 1;
1803 }
0da34b6d
BG
1804 if (status) {
1805 printk(KERN_ERR "myri10ge: %s: Couldn't set stats DMA\n",
1806 dev->name);
1807 goto abort_with_rings;
1808 }
1809
40f6cff5 1810 mgp->link_state = htonl(~0U);
0da34b6d
BG
1811 mgp->rdma_tags_available = 15;
1812
1813 netif_poll_enable(mgp->dev); /* must happen prior to any irq */
1814
1815 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
1816 if (status) {
1817 printk(KERN_ERR "myri10ge: %s: Couldn't bring up link\n",
1818 dev->name);
1819 goto abort_with_rings;
1820 }
1821
1822 mgp->wake_queue = 0;
1823 mgp->stop_queue = 0;
1824 mgp->running = MYRI10GE_ETH_RUNNING;
1825 mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
1826 add_timer(&mgp->watchdog_timer);
1827 netif_wake_queue(dev);
1828 return 0;
1829
1830abort_with_rings:
1831 myri10ge_free_rings(dev);
1832
df30a740
BG
1833abort_with_irq:
1834 myri10ge_free_irq(mgp);
1835
0da34b6d
BG
1836abort_with_nothing:
1837 mgp->running = MYRI10GE_ETH_STOPPED;
1838 return -ENOMEM;
1839}
1840
1841static int myri10ge_close(struct net_device *dev)
1842{
1843 struct myri10ge_priv *mgp;
1844 struct myri10ge_cmd cmd;
1845 int status, old_down_cnt;
1846
1847 mgp = netdev_priv(dev);
1848
1849 if (mgp->running != MYRI10GE_ETH_RUNNING)
1850 return 0;
1851
1852 if (mgp->tx.req_bytes == NULL)
1853 return 0;
1854
1855 del_timer_sync(&mgp->watchdog_timer);
1856 mgp->running = MYRI10GE_ETH_STOPPING;
1857 netif_poll_disable(mgp->dev);
1858 netif_carrier_off(dev);
1859 netif_stop_queue(dev);
1860 old_down_cnt = mgp->down_cnt;
1861 mb();
1862 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
1863 if (status)
1864 printk(KERN_ERR "myri10ge: %s: Couldn't bring down link\n",
1865 dev->name);
1866
1867 wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt, HZ);
1868 if (old_down_cnt == mgp->down_cnt)
1869 printk(KERN_ERR "myri10ge: %s never got down irq\n", dev->name);
1870
1871 netif_tx_disable(dev);
df30a740 1872 myri10ge_free_irq(mgp);
0da34b6d
BG
1873 myri10ge_free_rings(dev);
1874
1875 mgp->running = MYRI10GE_ETH_STOPPED;
1876 return 0;
1877}
1878
1879/* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
1880 * backwards one at a time and handle ring wraps */
1881
1882static inline void
1883myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
1884 struct mcp_kreq_ether_send *src, int cnt)
1885{
1886 int idx, starting_slot;
1887 starting_slot = tx->req;
1888 while (cnt > 1) {
1889 cnt--;
1890 idx = (starting_slot + cnt) & tx->mask;
1891 myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
1892 mb();
1893 }
1894}
1895
1896/*
1897 * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
1898 * at most 32 bytes at a time, so as to avoid involving the software
1899 * pio handler in the nic. We re-write the first segment's flags
1900 * to mark them valid only after writing the entire chain.
1901 */
1902
1903static inline void
1904myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
1905 int cnt)
1906{
1907 int idx, i;
1908 struct mcp_kreq_ether_send __iomem *dstp, *dst;
1909 struct mcp_kreq_ether_send *srcp;
1910 u8 last_flags;
1911
1912 idx = tx->req & tx->mask;
1913
1914 last_flags = src->flags;
1915 src->flags = 0;
1916 mb();
1917 dst = dstp = &tx->lanai[idx];
1918 srcp = src;
1919
1920 if ((idx + cnt) < tx->mask) {
1921 for (i = 0; i < (cnt - 1); i += 2) {
1922 myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
1923 mb(); /* force write every 32 bytes */
1924 srcp += 2;
1925 dstp += 2;
1926 }
1927 } else {
1928 /* submit all but the first request, and ensure
1929 * that it is submitted below */
1930 myri10ge_submit_req_backwards(tx, src, cnt);
1931 i = 0;
1932 }
1933 if (i < cnt) {
1934 /* submit the first request */
1935 myri10ge_pio_copy(dstp, srcp, sizeof(*src));
1936 mb(); /* barrier before setting valid flag */
1937 }
1938
1939 /* re-write the last 32-bits with the valid flags */
1940 src->flags = last_flags;
40f6cff5 1941 put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
0da34b6d
BG
1942 tx->req += cnt;
1943 mb();
1944}
1945
1946static inline void
1947myri10ge_submit_req_wc(struct myri10ge_tx_buf *tx,
1948 struct mcp_kreq_ether_send *src, int cnt)
1949{
1950 tx->req += cnt;
1951 mb();
1952 while (cnt >= 4) {
1953 myri10ge_pio_copy(tx->wc_fifo, src, 64);
1954 mb();
1955 src += 4;
1956 cnt -= 4;
1957 }
1958 if (cnt > 0) {
1959 /* pad it to 64 bytes. The src is 64 bytes bigger than it
1960 * needs to be so that we don't overrun it */
e700f9f4
BG
1961 myri10ge_pio_copy(tx->wc_fifo + MXGEFW_ETH_SEND_OFFSET(cnt),
1962 src, 64);
0da34b6d
BG
1963 mb();
1964 }
1965}
1966
1967/*
1968 * Transmit a packet. We need to split the packet so that a single
1969 * segment does not cross myri10ge->tx.boundary, so this makes segment
1970 * counting tricky. So rather than try to count segments up front, we
1971 * just give up if there are too few segments to hold a reasonably
1972 * fragmented packet currently available. If we run
1973 * out of segments while preparing a packet for DMA, we just linearize
1974 * it and try again.
1975 */
1976
1977static int myri10ge_xmit(struct sk_buff *skb, struct net_device *dev)
1978{
1979 struct myri10ge_priv *mgp = netdev_priv(dev);
1980 struct mcp_kreq_ether_send *req;
1981 struct myri10ge_tx_buf *tx = &mgp->tx;
1982 struct skb_frag_struct *frag;
1983 dma_addr_t bus;
40f6cff5
AV
1984 u32 low;
1985 __be32 high_swapped;
0da34b6d
BG
1986 unsigned int len;
1987 int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
1988 u16 pseudo_hdr_offset, cksum_offset;
1989 int cum_len, seglen, boundary, rdma_count;
1990 u8 flags, odd_flag;
1991
1992again:
1993 req = tx->req_list;
1994 avail = tx->mask - 1 - (tx->req - tx->done);
1995
1996 mss = 0;
1997 max_segments = MXGEFW_MAX_SEND_DESC;
1998
0da34b6d 1999 if (skb->len > (dev->mtu + ETH_HLEN)) {
7967168c 2000 mss = skb_shinfo(skb)->gso_size;
0da34b6d
BG
2001 if (mss != 0)
2002 max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
2003 }
0da34b6d
BG
2004
2005 if ((unlikely(avail < max_segments))) {
2006 /* we are out of transmit resources */
2007 mgp->stop_queue++;
2008 netif_stop_queue(dev);
2009 return 1;
2010 }
2011
2012 /* Setup checksum offloading, if needed */
2013 cksum_offset = 0;
2014 pseudo_hdr_offset = 0;
2015 odd_flag = 0;
2016 flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
84fa7933 2017 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
0da34b6d 2018 cksum_offset = (skb->h.raw - skb->data);
ff1dcadb 2019 pseudo_hdr_offset = cksum_offset + skb->csum_offset;
0da34b6d
BG
2020 /* If the headers are excessively large, then we must
2021 * fall back to a software checksum */
2022 if (unlikely(cksum_offset > 255 || pseudo_hdr_offset > 127)) {
84fa7933 2023 if (skb_checksum_help(skb))
0da34b6d
BG
2024 goto drop;
2025 cksum_offset = 0;
2026 pseudo_hdr_offset = 0;
2027 } else {
0da34b6d
BG
2028 odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
2029 flags |= MXGEFW_FLAGS_CKSUM;
2030 }
2031 }
2032
2033 cum_len = 0;
2034
0da34b6d
BG
2035 if (mss) { /* TSO */
2036 /* this removes any CKSUM flag from before */
2037 flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
2038
2039 /* negative cum_len signifies to the
2040 * send loop that we are still in the
2041 * header portion of the TSO packet.
2042 * TSO header must be at most 134 bytes long */
2043 cum_len = -((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2044
2045 /* for TSO, pseudo_hdr_offset holds mss.
2046 * The firmware figures out where to put
2047 * the checksum by parsing the header. */
40f6cff5 2048 pseudo_hdr_offset = mss;
0da34b6d 2049 } else
0da34b6d
BG
2050 /* Mark small packets, and pad out tiny packets */
2051 if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
2052 flags |= MXGEFW_FLAGS_SMALL;
2053
2054 /* pad frames to at least ETH_ZLEN bytes */
2055 if (unlikely(skb->len < ETH_ZLEN)) {
5b057c6b 2056 if (skb_padto(skb, ETH_ZLEN)) {
0da34b6d
BG
2057 /* The packet is gone, so we must
2058 * return 0 */
2059 mgp->stats.tx_dropped += 1;
2060 return 0;
2061 }
2062 /* adjust the len to account for the zero pad
2063 * so that the nic can know how long it is */
2064 skb->len = ETH_ZLEN;
2065 }
2066 }
2067
2068 /* map the skb for DMA */
2069 len = skb->len - skb->data_len;
2070 idx = tx->req & tx->mask;
2071 tx->info[idx].skb = skb;
2072 bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
2073 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2074 pci_unmap_len_set(&tx->info[idx], len, len);
2075
2076 frag_cnt = skb_shinfo(skb)->nr_frags;
2077 frag_idx = 0;
2078 count = 0;
2079 rdma_count = 0;
2080
2081 /* "rdma_count" is the number of RDMAs belonging to the
2082 * current packet BEFORE the current send request. For
2083 * non-TSO packets, this is equal to "count".
2084 * For TSO packets, rdma_count needs to be reset
2085 * to 0 after a segment cut.
2086 *
2087 * The rdma_count field of the send request is
2088 * the number of RDMAs of the packet starting at
2089 * that request. For TSO send requests with one ore more cuts
2090 * in the middle, this is the number of RDMAs starting
2091 * after the last cut in the request. All previous
2092 * segments before the last cut implicitly have 1 RDMA.
2093 *
2094 * Since the number of RDMAs is not known beforehand,
2095 * it must be filled-in retroactively - after each
2096 * segmentation cut or at the end of the entire packet.
2097 */
2098
2099 while (1) {
2100 /* Break the SKB or Fragment up into pieces which
2101 * do not cross mgp->tx.boundary */
2102 low = MYRI10GE_LOWPART_TO_U32(bus);
2103 high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
2104 while (len) {
2105 u8 flags_next;
2106 int cum_len_next;
2107
2108 if (unlikely(count == max_segments))
2109 goto abort_linearize;
2110
2111 boundary = (low + tx->boundary) & ~(tx->boundary - 1);
2112 seglen = boundary - low;
2113 if (seglen > len)
2114 seglen = len;
2115 flags_next = flags & ~MXGEFW_FLAGS_FIRST;
2116 cum_len_next = cum_len + seglen;
0da34b6d
BG
2117 if (mss) { /* TSO */
2118 (req - rdma_count)->rdma_count = rdma_count + 1;
2119
2120 if (likely(cum_len >= 0)) { /* payload */
2121 int next_is_first, chop;
2122
2123 chop = (cum_len_next > mss);
2124 cum_len_next = cum_len_next % mss;
2125 next_is_first = (cum_len_next == 0);
2126 flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
2127 flags_next |= next_is_first *
2128 MXGEFW_FLAGS_FIRST;
2129 rdma_count |= -(chop | next_is_first);
2130 rdma_count += chop & !next_is_first;
2131 } else if (likely(cum_len_next >= 0)) { /* header ends */
2132 int small;
2133
2134 rdma_count = -1;
2135 cum_len_next = 0;
2136 seglen = -cum_len;
2137 small = (mss <= MXGEFW_SEND_SMALL_SIZE);
2138 flags_next = MXGEFW_FLAGS_TSO_PLD |
2139 MXGEFW_FLAGS_FIRST |
2140 (small * MXGEFW_FLAGS_SMALL);
2141 }
2142 }
0da34b6d
BG
2143 req->addr_high = high_swapped;
2144 req->addr_low = htonl(low);
40f6cff5 2145 req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
0da34b6d
BG
2146 req->pad = 0; /* complete solid 16-byte block; does this matter? */
2147 req->rdma_count = 1;
2148 req->length = htons(seglen);
2149 req->cksum_offset = cksum_offset;
2150 req->flags = flags | ((cum_len & 1) * odd_flag);
2151
2152 low += seglen;
2153 len -= seglen;
2154 cum_len = cum_len_next;
2155 flags = flags_next;
2156 req++;
2157 count++;
2158 rdma_count++;
2159 if (unlikely(cksum_offset > seglen))
2160 cksum_offset -= seglen;
2161 else
2162 cksum_offset = 0;
2163 }
2164 if (frag_idx == frag_cnt)
2165 break;
2166
2167 /* map next fragment for DMA */
2168 idx = (count + tx->req) & tx->mask;
2169 frag = &skb_shinfo(skb)->frags[frag_idx];
2170 frag_idx++;
2171 len = frag->size;
2172 bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
2173 len, PCI_DMA_TODEVICE);
2174 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2175 pci_unmap_len_set(&tx->info[idx], len, len);
2176 }
2177
2178 (req - rdma_count)->rdma_count = rdma_count;
0da34b6d
BG
2179 if (mss)
2180 do {
2181 req--;
2182 req->flags |= MXGEFW_FLAGS_TSO_LAST;
2183 } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
2184 MXGEFW_FLAGS_FIRST)));
0da34b6d
BG
2185 idx = ((count - 1) + tx->req) & tx->mask;
2186 tx->info[idx].last = 1;
2187 if (tx->wc_fifo == NULL)
2188 myri10ge_submit_req(tx, tx->req_list, count);
2189 else
2190 myri10ge_submit_req_wc(tx, tx->req_list, count);
2191 tx->pkt_start++;
2192 if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
2193 mgp->stop_queue++;
2194 netif_stop_queue(dev);
2195 }
2196 dev->trans_start = jiffies;
2197 return 0;
2198
2199abort_linearize:
2200 /* Free any DMA resources we've alloced and clear out the skb
2201 * slot so as to not trip up assertions, and to avoid a
2202 * double-free if linearizing fails */
2203
2204 last_idx = (idx + 1) & tx->mask;
2205 idx = tx->req & tx->mask;
2206 tx->info[idx].skb = NULL;
2207 do {
2208 len = pci_unmap_len(&tx->info[idx], len);
2209 if (len) {
2210 if (tx->info[idx].skb != NULL)
2211 pci_unmap_single(mgp->pdev,
2212 pci_unmap_addr(&tx->info[idx],
2213 bus), len,
2214 PCI_DMA_TODEVICE);
2215 else
2216 pci_unmap_page(mgp->pdev,
2217 pci_unmap_addr(&tx->info[idx],
2218 bus), len,
2219 PCI_DMA_TODEVICE);
2220 pci_unmap_len_set(&tx->info[idx], len, 0);
2221 tx->info[idx].skb = NULL;
2222 }
2223 idx = (idx + 1) & tx->mask;
2224 } while (idx != last_idx);
89114afd 2225 if (skb_is_gso(skb)) {
0da34b6d
BG
2226 printk(KERN_ERR
2227 "myri10ge: %s: TSO but wanted to linearize?!?!?\n",
2228 mgp->dev->name);
2229 goto drop;
2230 }
2231
bec0e859 2232 if (skb_linearize(skb))
0da34b6d
BG
2233 goto drop;
2234
2235 mgp->tx_linearized++;
2236 goto again;
2237
2238drop:
2239 dev_kfree_skb_any(skb);
2240 mgp->stats.tx_dropped += 1;
2241 return 0;
2242
2243}
2244
2245static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
2246{
2247 struct myri10ge_priv *mgp = netdev_priv(dev);
2248 return &mgp->stats;
2249}
2250
2251static void myri10ge_set_multicast_list(struct net_device *dev)
2252{
85a7ea1b
BG
2253 struct myri10ge_cmd cmd;
2254 struct myri10ge_priv *mgp;
2255 struct dev_mc_list *mc_list;
6250223e 2256 __be32 data[2] = { 0, 0 };
85a7ea1b
BG
2257 int err;
2258
2259 mgp = netdev_priv(dev);
0da34b6d
BG
2260 /* can be called from atomic contexts,
2261 * pass 1 to force atomicity in myri10ge_send_cmd() */
85a7ea1b
BG
2262 myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
2263
2264 /* This firmware is known to not support multicast */
9dc6f0e7 2265 if (!mgp->fw_multicast_support || mgp->adopted_rx_filter_bug)
85a7ea1b
BG
2266 return;
2267
2268 /* Disable multicast filtering */
2269
2270 err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
2271 if (err != 0) {
2272 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_ENABLE_ALLMULTI,"
2273 " error status: %d\n", dev->name, err);
2274 goto abort;
2275 }
2276
2277 if (dev->flags & IFF_ALLMULTI) {
2278 /* request to disable multicast filtering, so quit here */
2279 return;
2280 }
2281
2282 /* Flush the filters */
2283
2284 err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
2285 &cmd, 1);
2286 if (err != 0) {
2287 printk(KERN_ERR
2288 "myri10ge: %s: Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS"
2289 ", error status: %d\n", dev->name, err);
2290 goto abort;
2291 }
2292
2293 /* Walk the multicast list, and add each address */
2294 for (mc_list = dev->mc_list; mc_list != NULL; mc_list = mc_list->next) {
40f6cff5
AV
2295 memcpy(data, &mc_list->dmi_addr, 6);
2296 cmd.data0 = ntohl(data[0]);
2297 cmd.data1 = ntohl(data[1]);
85a7ea1b
BG
2298 err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
2299 &cmd, 1);
2300
2301 if (err != 0) {
2302 printk(KERN_ERR "myri10ge: %s: Failed "
2303 "MXGEFW_JOIN_MULTICAST_GROUP, error status:"
2304 "%d\t", dev->name, err);
2305 printk(KERN_ERR "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
2306 ((unsigned char *)&mc_list->dmi_addr)[0],
2307 ((unsigned char *)&mc_list->dmi_addr)[1],
2308 ((unsigned char *)&mc_list->dmi_addr)[2],
2309 ((unsigned char *)&mc_list->dmi_addr)[3],
2310 ((unsigned char *)&mc_list->dmi_addr)[4],
2311 ((unsigned char *)&mc_list->dmi_addr)[5]
2312 );
2313 goto abort;
2314 }
2315 }
2316 /* Enable multicast filtering */
2317 err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
2318 if (err != 0) {
2319 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_DISABLE_ALLMULTI,"
2320 "error status: %d\n", dev->name, err);
2321 goto abort;
2322 }
2323
2324 return;
2325
2326abort:
2327 return;
0da34b6d
BG
2328}
2329
2330static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
2331{
2332 struct sockaddr *sa = addr;
2333 struct myri10ge_priv *mgp = netdev_priv(dev);
2334 int status;
2335
2336 if (!is_valid_ether_addr(sa->sa_data))
2337 return -EADDRNOTAVAIL;
2338
2339 status = myri10ge_update_mac_address(mgp, sa->sa_data);
2340 if (status != 0) {
2341 printk(KERN_ERR
2342 "myri10ge: %s: changing mac address failed with %d\n",
2343 dev->name, status);
2344 return status;
2345 }
2346
2347 /* change the dev structure */
2348 memcpy(dev->dev_addr, sa->sa_data, 6);
2349 return 0;
2350}
2351
2352static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
2353{
2354 struct myri10ge_priv *mgp = netdev_priv(dev);
2355 int error = 0;
2356
2357 if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
2358 printk(KERN_ERR "myri10ge: %s: new mtu (%d) is not valid\n",
2359 dev->name, new_mtu);
2360 return -EINVAL;
2361 }
2362 printk(KERN_INFO "%s: changing mtu from %d to %d\n",
2363 dev->name, dev->mtu, new_mtu);
2364 if (mgp->running) {
2365 /* if we change the mtu on an active device, we must
2366 * reset the device so the firmware sees the change */
2367 myri10ge_close(dev);
2368 dev->mtu = new_mtu;
2369 myri10ge_open(dev);
2370 } else
2371 dev->mtu = new_mtu;
2372
2373 return error;
2374}
2375
2376/*
2377 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
2378 * Only do it if the bridge is a root port since we don't want to disturb
2379 * any other device, except if forced with myri10ge_ecrc_enable > 1.
2380 */
2381
0da34b6d
BG
2382static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
2383{
2384 struct pci_dev *bridge = mgp->pdev->bus->self;
2385 struct device *dev = &mgp->pdev->dev;
2386 unsigned cap;
2387 unsigned err_cap;
2388 u16 val;
2389 u8 ext_type;
2390 int ret;
2391
2392 if (!myri10ge_ecrc_enable || !bridge)
2393 return;
2394
2395 /* check that the bridge is a root port */
2396 cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
2397 pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
2398 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
2399 if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
2400 if (myri10ge_ecrc_enable > 1) {
2401 struct pci_dev *old_bridge = bridge;
2402
2403 /* Walk the hierarchy up to the root port
2404 * where ECRC has to be enabled */
2405 do {
2406 bridge = bridge->bus->self;
2407 if (!bridge) {
2408 dev_err(dev,
2409 "Failed to find root port"
2410 " to force ECRC\n");
2411 return;
2412 }
2413 cap =
2414 pci_find_capability(bridge, PCI_CAP_ID_EXP);
2415 pci_read_config_word(bridge,
2416 cap + PCI_CAP_FLAGS, &val);
2417 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
2418 } while (ext_type != PCI_EXP_TYPE_ROOT_PORT);
2419
2420 dev_info(dev,
2421 "Forcing ECRC on non-root port %s"
2422 " (enabling on root port %s)\n",
2423 pci_name(old_bridge), pci_name(bridge));
2424 } else {
2425 dev_err(dev,
2426 "Not enabling ECRC on non-root port %s\n",
2427 pci_name(bridge));
2428 return;
2429 }
2430 }
2431
2432 cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
0da34b6d
BG
2433 if (!cap)
2434 return;
2435
2436 ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
2437 if (ret) {
2438 dev_err(dev, "failed reading ext-conf-space of %s\n",
2439 pci_name(bridge));
2440 dev_err(dev, "\t pci=nommconf in use? "
2441 "or buggy/incomplete/absent ACPI MCFG attr?\n");
2442 return;
2443 }
2444 if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
2445 return;
2446
2447 err_cap |= PCI_ERR_CAP_ECRC_GENE;
2448 pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
2449 dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
2450 mgp->tx.boundary = 4096;
2451 mgp->fw_name = myri10ge_fw_aligned;
2452}
2453
2454/*
2455 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
2456 * when the PCI-E Completion packets are aligned on an 8-byte
2457 * boundary. Some PCI-E chip sets always align Completion packets; on
2458 * the ones that do not, the alignment can be enforced by enabling
2459 * ECRC generation (if supported).
2460 *
2461 * When PCI-E Completion packets are not aligned, it is actually more
2462 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
2463 *
2464 * If the driver can neither enable ECRC nor verify that it has
2465 * already been enabled, then it must use a firmware image which works
2466 * around unaligned completion packets (myri10ge_ethp_z8e.dat), and it
2467 * should also ensure that it never gives the device a Read-DMA which is
2468 * larger than 2KB by setting the tx.boundary to 2KB. If ECRC is
2469 * enabled, then the driver should use the aligned (myri10ge_eth_z8e.dat)
2470 * firmware image, and set tx.boundary to 4KB.
2471 */
2472
ce7f9368
BG
2473#define PCI_DEVICE_ID_INTEL_E5000_PCIE23 0x25f7
2474#define PCI_DEVICE_ID_INTEL_E5000_PCIE47 0x25fa
0da34b6d
BG
2475
2476static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
2477{
2478 struct pci_dev *bridge = mgp->pdev->bus->self;
2479
2480 mgp->tx.boundary = 2048;
2481 mgp->fw_name = myri10ge_fw_unaligned;
2482
2483 if (myri10ge_force_firmware == 0) {
ce7f9368
BG
2484 int link_width, exp_cap;
2485 u16 lnk;
2486
2487 exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
2488 pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
2489 link_width = (lnk >> 4) & 0x3f;
2490
0da34b6d
BG
2491 myri10ge_enable_ecrc(mgp);
2492
ce7f9368
BG
2493 /* Check to see if Link is less than 8 or if the
2494 * upstream bridge is known to provide aligned
2495 * completions */
2496 if (link_width < 8) {
2497 dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
2498 link_width);
2499 mgp->tx.boundary = 4096;
2500 mgp->fw_name = myri10ge_fw_aligned;
2501 } else if (bridge &&
2502 /* ServerWorks HT2000/HT1000 */
2503 ((bridge->vendor == PCI_VENDOR_ID_SERVERWORKS
2504 && bridge->device ==
2505 PCI_DEVICE_ID_SERVERWORKS_HT2000_PCIE)
2506 /* All Intel E5000 PCIE ports */
2507 || (bridge->vendor == PCI_VENDOR_ID_INTEL
2508 && bridge->device >=
2509 PCI_DEVICE_ID_INTEL_E5000_PCIE23
2510 && bridge->device <=
2511 PCI_DEVICE_ID_INTEL_E5000_PCIE47))) {
0da34b6d
BG
2512 dev_info(&mgp->pdev->dev,
2513 "Assuming aligned completions (0x%x:0x%x)\n",
2514 bridge->vendor, bridge->device);
2515 mgp->tx.boundary = 4096;
2516 mgp->fw_name = myri10ge_fw_aligned;
2517 }
2518 } else {
2519 if (myri10ge_force_firmware == 1) {
2520 dev_info(&mgp->pdev->dev,
2521 "Assuming aligned completions (forced)\n");
2522 mgp->tx.boundary = 4096;
2523 mgp->fw_name = myri10ge_fw_aligned;
2524 } else {
2525 dev_info(&mgp->pdev->dev,
2526 "Assuming unaligned completions (forced)\n");
2527 mgp->tx.boundary = 2048;
2528 mgp->fw_name = myri10ge_fw_unaligned;
2529 }
2530 }
2531 if (myri10ge_fw_name != NULL) {
2532 dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
2533 myri10ge_fw_name);
2534 mgp->fw_name = myri10ge_fw_name;
2535 }
2536}
2537
0da34b6d
BG
2538#ifdef CONFIG_PM
2539
2540static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
2541{
2542 struct myri10ge_priv *mgp;
2543 struct net_device *netdev;
2544
2545 mgp = pci_get_drvdata(pdev);
2546 if (mgp == NULL)
2547 return -EINVAL;
2548 netdev = mgp->dev;
2549
2550 netif_device_detach(netdev);
2551 if (netif_running(netdev)) {
2552 printk(KERN_INFO "myri10ge: closing %s\n", netdev->name);
2553 rtnl_lock();
2554 myri10ge_close(netdev);
2555 rtnl_unlock();
2556 }
2557 myri10ge_dummy_rdma(mgp, 0);
83f6e152 2558 pci_save_state(pdev);
0da34b6d 2559 pci_disable_device(pdev);
1a63e846
BG
2560
2561 return pci_set_power_state(pdev, pci_choose_state(pdev, state));
0da34b6d
BG
2562}
2563
2564static int myri10ge_resume(struct pci_dev *pdev)
2565{
2566 struct myri10ge_priv *mgp;
2567 struct net_device *netdev;
2568 int status;
2569 u16 vendor;
2570
2571 mgp = pci_get_drvdata(pdev);
2572 if (mgp == NULL)
2573 return -EINVAL;
2574 netdev = mgp->dev;
2575 pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
2576 msleep(5); /* give card time to respond */
2577 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
2578 if (vendor == 0xffff) {
2579 printk(KERN_ERR "myri10ge: %s: device disappeared!\n",
2580 mgp->dev->name);
2581 return -EIO;
2582 }
83f6e152 2583
1a63e846
BG
2584 status = pci_restore_state(pdev);
2585 if (status)
2586 return status;
4c2248cc
BG
2587
2588 status = pci_enable_device(pdev);
1a63e846 2589 if (status) {
4c2248cc 2590 dev_err(&pdev->dev, "failed to enable device\n");
1a63e846 2591 return status;
4c2248cc
BG
2592 }
2593
0da34b6d
BG
2594 pci_set_master(pdev);
2595
0da34b6d 2596 myri10ge_reset(mgp);
013b68bf 2597 myri10ge_dummy_rdma(mgp, 1);
0da34b6d
BG
2598
2599 /* Save configuration space to be restored if the
2600 * nic resets due to a parity error */
83f6e152 2601 pci_save_state(pdev);
0da34b6d
BG
2602
2603 if (netif_running(netdev)) {
2604 rtnl_lock();
df30a740 2605 status = myri10ge_open(netdev);
0da34b6d 2606 rtnl_unlock();
df30a740
BG
2607 if (status != 0)
2608 goto abort_with_enabled;
2609
0da34b6d
BG
2610 }
2611 netif_device_attach(netdev);
2612
2613 return 0;
2614
4c2248cc
BG
2615abort_with_enabled:
2616 pci_disable_device(pdev);
0da34b6d
BG
2617 return -EIO;
2618
2619}
2620
2621#endif /* CONFIG_PM */
2622
2623static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
2624{
2625 struct pci_dev *pdev = mgp->pdev;
2626 int vs = mgp->vendor_specific_offset;
2627 u32 reboot;
2628
2629 /*enter read32 mode */
2630 pci_write_config_byte(pdev, vs + 0x10, 0x3);
2631
2632 /*read REBOOT_STATUS (0xfffffff0) */
2633 pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
2634 pci_read_config_dword(pdev, vs + 0x14, &reboot);
2635 return reboot;
2636}
2637
2638/*
2639 * This watchdog is used to check whether the board has suffered
2640 * from a parity error and needs to be recovered.
2641 */
c4028958 2642static void myri10ge_watchdog(struct work_struct *work)
0da34b6d 2643{
c4028958 2644 struct myri10ge_priv *mgp =
6250223e 2645 container_of(work, struct myri10ge_priv, watchdog_work);
0da34b6d
BG
2646 u32 reboot;
2647 int status;
2648 u16 cmd, vendor;
2649
2650 mgp->watchdog_resets++;
2651 pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
2652 if ((cmd & PCI_COMMAND_MASTER) == 0) {
2653 /* Bus master DMA disabled? Check to see
2654 * if the card rebooted due to a parity error
2655 * For now, just report it */
2656 reboot = myri10ge_read_reboot(mgp);
2657 printk(KERN_ERR
2658 "myri10ge: %s: NIC rebooted (0x%x), resetting\n",
2659 mgp->dev->name, reboot);
2660 /*
2661 * A rebooted nic will come back with config space as
2662 * it was after power was applied to PCIe bus.
2663 * Attempt to restore config space which was saved
2664 * when the driver was loaded, or the last time the
2665 * nic was resumed from power saving mode.
2666 */
83f6e152 2667 pci_restore_state(mgp->pdev);
7adda30c
BG
2668
2669 /* save state again for accounting reasons */
83f6e152 2670 pci_save_state(mgp->pdev);
7adda30c 2671
0da34b6d
BG
2672 } else {
2673 /* if we get back -1's from our slot, perhaps somebody
2674 * powered off our card. Don't try to reset it in
2675 * this case */
2676 if (cmd == 0xffff) {
2677 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
2678 if (vendor == 0xffff) {
2679 printk(KERN_ERR
2680 "myri10ge: %s: device disappeared!\n",
2681 mgp->dev->name);
2682 return;
2683 }
2684 }
2685 /* Perhaps it is a software error. Try to reset */
2686
2687 printk(KERN_ERR "myri10ge: %s: device timeout, resetting\n",
2688 mgp->dev->name);
2689 printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
2690 mgp->dev->name, mgp->tx.req, mgp->tx.done,
2691 mgp->tx.pkt_start, mgp->tx.pkt_done,
2692 (int)ntohl(mgp->fw_stats->send_done_count));
2693 msleep(2000);
2694 printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
2695 mgp->dev->name, mgp->tx.req, mgp->tx.done,
2696 mgp->tx.pkt_start, mgp->tx.pkt_done,
2697 (int)ntohl(mgp->fw_stats->send_done_count));
2698 }
2699 rtnl_lock();
2700 myri10ge_close(mgp->dev);
2701 status = myri10ge_load_firmware(mgp);
2702 if (status != 0)
2703 printk(KERN_ERR "myri10ge: %s: failed to load firmware\n",
2704 mgp->dev->name);
2705 else
2706 myri10ge_open(mgp->dev);
2707 rtnl_unlock();
2708}
2709
2710/*
2711 * We use our own timer routine rather than relying upon
2712 * netdev->tx_timeout because we have a very large hardware transmit
2713 * queue. Due to the large queue, the netdev->tx_timeout function
2714 * cannot detect a NIC with a parity error in a timely fashion if the
2715 * NIC is lightly loaded.
2716 */
2717static void myri10ge_watchdog_timer(unsigned long arg)
2718{
2719 struct myri10ge_priv *mgp;
2720
2721 mgp = (struct myri10ge_priv *)arg;
c7dab99b
BG
2722
2723 if (mgp->rx_small.watchdog_needed) {
2724 myri10ge_alloc_rx_pages(mgp, &mgp->rx_small,
2725 mgp->small_bytes + MXGEFW_PAD, 1);
2726 if (mgp->rx_small.fill_cnt - mgp->rx_small.cnt >=
2727 myri10ge_fill_thresh)
2728 mgp->rx_small.watchdog_needed = 0;
2729 }
2730 if (mgp->rx_big.watchdog_needed) {
2731 myri10ge_alloc_rx_pages(mgp, &mgp->rx_big, mgp->big_bytes, 1);
2732 if (mgp->rx_big.fill_cnt - mgp->rx_big.cnt >=
2733 myri10ge_fill_thresh)
2734 mgp->rx_big.watchdog_needed = 0;
2735 }
2736
0da34b6d 2737 if (mgp->tx.req != mgp->tx.done &&
c54772e7
BG
2738 mgp->tx.done == mgp->watchdog_tx_done &&
2739 mgp->watchdog_tx_req != mgp->watchdog_tx_done)
0da34b6d
BG
2740 /* nic seems like it might be stuck.. */
2741 schedule_work(&mgp->watchdog_work);
2742 else
2743 /* rearm timer */
2744 mod_timer(&mgp->watchdog_timer,
2745 jiffies + myri10ge_watchdog_timeout * HZ);
2746
2747 mgp->watchdog_tx_done = mgp->tx.done;
c54772e7 2748 mgp->watchdog_tx_req = mgp->tx.req;
0da34b6d
BG
2749}
2750
2751static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2752{
2753 struct net_device *netdev;
2754 struct myri10ge_priv *mgp;
2755 struct device *dev = &pdev->dev;
2756 size_t bytes;
2757 int i;
2758 int status = -ENXIO;
2759 int cap;
2760 int dac_enabled;
2761 u16 val;
2762
2763 netdev = alloc_etherdev(sizeof(*mgp));
2764 if (netdev == NULL) {
2765 dev_err(dev, "Could not allocate ethernet device\n");
2766 return -ENOMEM;
2767 }
2768
2769 mgp = netdev_priv(netdev);
2770 memset(mgp, 0, sizeof(*mgp));
2771 mgp->dev = netdev;
2772 mgp->pdev = pdev;
2773 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
2774 mgp->pause = myri10ge_flow_control;
2775 mgp->intr_coal_delay = myri10ge_intr_coal_delay;
c58ac5ca 2776 mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
0da34b6d
BG
2777 init_waitqueue_head(&mgp->down_wq);
2778
2779 if (pci_enable_device(pdev)) {
2780 dev_err(&pdev->dev, "pci_enable_device call failed\n");
2781 status = -ENODEV;
2782 goto abort_with_netdev;
2783 }
2784 myri10ge_select_firmware(mgp);
2785
2786 /* Find the vendor-specific cap so we can check
2787 * the reboot register later on */
2788 mgp->vendor_specific_offset
2789 = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
2790
2791 /* Set our max read request to 4KB */
2792 cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
2793 if (cap < 64) {
2794 dev_err(&pdev->dev, "Bad PCI_CAP_ID_EXP location %d\n", cap);
2795 goto abort_with_netdev;
2796 }
2797 status = pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &val);
2798 if (status != 0) {
2799 dev_err(&pdev->dev, "Error %d reading PCI_EXP_DEVCTL\n",
2800 status);
2801 goto abort_with_netdev;
2802 }
2803 val = (val & ~PCI_EXP_DEVCTL_READRQ) | (5 << 12);
2804 status = pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, val);
2805 if (status != 0) {
2806 dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
2807 status);
2808 goto abort_with_netdev;
2809 }
2810
2811 pci_set_master(pdev);
2812 dac_enabled = 1;
2813 status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
2814 if (status != 0) {
2815 dac_enabled = 0;
2816 dev_err(&pdev->dev,
2817 "64-bit pci address mask was refused, trying 32-bit");
2818 status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2819 }
2820 if (status != 0) {
2821 dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
2822 goto abort_with_netdev;
2823 }
b10c0668
BG
2824 mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
2825 &mgp->cmd_bus, GFP_KERNEL);
0da34b6d
BG
2826 if (mgp->cmd == NULL)
2827 goto abort_with_netdev;
2828
b10c0668
BG
2829 mgp->fw_stats = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->fw_stats),
2830 &mgp->fw_stats_bus, GFP_KERNEL);
0da34b6d
BG
2831 if (mgp->fw_stats == NULL)
2832 goto abort_with_cmd;
2833
2834 mgp->board_span = pci_resource_len(pdev, 0);
2835 mgp->iomem_base = pci_resource_start(pdev, 0);
2836 mgp->mtrr = -1;
2837#ifdef CONFIG_MTRR
2838 mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
2839 MTRR_TYPE_WRCOMB, 1);
2840#endif
2841 /* Hack. need to get rid of these magic numbers */
2842 mgp->sram_size =
2843 2 * 1024 * 1024 - (2 * (48 * 1024) + (32 * 1024)) - 0x100;
2844 if (mgp->sram_size > mgp->board_span) {
2845 dev_err(&pdev->dev, "board span %ld bytes too small\n",
2846 mgp->board_span);
2847 goto abort_with_wc;
2848 }
2849 mgp->sram = ioremap(mgp->iomem_base, mgp->board_span);
2850 if (mgp->sram == NULL) {
2851 dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
2852 mgp->board_span, mgp->iomem_base);
2853 status = -ENXIO;
2854 goto abort_with_wc;
2855 }
2856 memcpy_fromio(mgp->eeprom_strings,
2857 mgp->sram + mgp->sram_size - MYRI10GE_EEPROM_STRINGS_SIZE,
2858 MYRI10GE_EEPROM_STRINGS_SIZE);
2859 memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
2860 status = myri10ge_read_mac_addr(mgp);
2861 if (status)
2862 goto abort_with_ioremap;
2863
2864 for (i = 0; i < ETH_ALEN; i++)
2865 netdev->dev_addr[i] = mgp->mac_addr[i];
2866
2867 /* allocate rx done ring */
2868 bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
b10c0668
BG
2869 mgp->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
2870 &mgp->rx_done.bus, GFP_KERNEL);
0da34b6d
BG
2871 if (mgp->rx_done.entry == NULL)
2872 goto abort_with_ioremap;
2873 memset(mgp->rx_done.entry, 0, bytes);
2874
2875 status = myri10ge_load_firmware(mgp);
2876 if (status != 0) {
2877 dev_err(&pdev->dev, "failed to load firmware\n");
2878 goto abort_with_rx_done;
2879 }
2880
2881 status = myri10ge_reset(mgp);
2882 if (status != 0) {
2883 dev_err(&pdev->dev, "failed reset\n");
2884 goto abort_with_firmware;
2885 }
2886
0da34b6d
BG
2887 pci_set_drvdata(pdev, mgp);
2888 if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
2889 myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
2890 if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
2891 myri10ge_initial_mtu = 68;
2892 netdev->mtu = myri10ge_initial_mtu;
2893 netdev->open = myri10ge_open;
2894 netdev->stop = myri10ge_close;
2895 netdev->hard_start_xmit = myri10ge_xmit;
2896 netdev->get_stats = myri10ge_get_stats;
2897 netdev->base_addr = mgp->iomem_base;
0da34b6d
BG
2898 netdev->change_mtu = myri10ge_change_mtu;
2899 netdev->set_multicast_list = myri10ge_set_multicast_list;
2900 netdev->set_mac_address = myri10ge_set_mac_address;
2901 netdev->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
2902 if (dac_enabled)
2903 netdev->features |= NETIF_F_HIGHDMA;
2904 netdev->poll = myri10ge_poll;
2905 netdev->weight = myri10ge_napi_weight;
2906
21d05db1
BG
2907 /* make sure we can get an irq, and that MSI can be
2908 * setup (if available). Also ensure netdev->irq
2909 * is set to correct value if MSI is enabled */
2910 status = myri10ge_request_irq(mgp);
2911 if (status != 0)
2912 goto abort_with_firmware;
2913 netdev->irq = pdev->irq;
2914 myri10ge_free_irq(mgp);
2915
0da34b6d
BG
2916 /* Save configuration space to be restored if the
2917 * nic resets due to a parity error */
83f6e152 2918 pci_save_state(pdev);
0da34b6d
BG
2919
2920 /* Setup the watchdog timer */
2921 setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
2922 (unsigned long)mgp);
2923
2924 SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
c4028958 2925 INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
0da34b6d
BG
2926 status = register_netdev(netdev);
2927 if (status != 0) {
2928 dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
7adda30c 2929 goto abort_with_state;
0da34b6d 2930 }
21d05db1
BG
2931 dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
2932 (mgp->msi_enabled ? "MSI" : "xPIC"),
2933 netdev->irq, mgp->tx.boundary, mgp->fw_name,
d6020787 2934 (mgp->mtrr >= 0 ? "Enabled" : "Disabled"));
0da34b6d
BG
2935
2936 return 0;
2937
7adda30c 2938abort_with_state:
83f6e152 2939 pci_restore_state(pdev);
0da34b6d
BG
2940
2941abort_with_firmware:
2942 myri10ge_dummy_rdma(mgp, 0);
2943
2944abort_with_rx_done:
2945 bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
b10c0668
BG
2946 dma_free_coherent(&pdev->dev, bytes,
2947 mgp->rx_done.entry, mgp->rx_done.bus);
0da34b6d
BG
2948
2949abort_with_ioremap:
2950 iounmap(mgp->sram);
2951
2952abort_with_wc:
2953#ifdef CONFIG_MTRR
2954 if (mgp->mtrr >= 0)
2955 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
2956#endif
b10c0668
BG
2957 dma_free_coherent(&pdev->dev, sizeof(*mgp->fw_stats),
2958 mgp->fw_stats, mgp->fw_stats_bus);
0da34b6d
BG
2959
2960abort_with_cmd:
b10c0668
BG
2961 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
2962 mgp->cmd, mgp->cmd_bus);
0da34b6d
BG
2963
2964abort_with_netdev:
2965
2966 free_netdev(netdev);
2967 return status;
2968}
2969
2970/*
2971 * myri10ge_remove
2972 *
2973 * Does what is necessary to shutdown one Myrinet device. Called
2974 * once for each Myrinet card by the kernel when a module is
2975 * unloaded.
2976 */
2977static void myri10ge_remove(struct pci_dev *pdev)
2978{
2979 struct myri10ge_priv *mgp;
2980 struct net_device *netdev;
2981 size_t bytes;
2982
2983 mgp = pci_get_drvdata(pdev);
2984 if (mgp == NULL)
2985 return;
2986
2987 flush_scheduled_work();
2988 netdev = mgp->dev;
2989 unregister_netdev(netdev);
0da34b6d
BG
2990
2991 myri10ge_dummy_rdma(mgp, 0);
2992
7adda30c 2993 /* avoid a memory leak */
83f6e152 2994 pci_restore_state(pdev);
7adda30c 2995
0da34b6d 2996 bytes = myri10ge_max_intr_slots * sizeof(*mgp->rx_done.entry);
b10c0668
BG
2997 dma_free_coherent(&pdev->dev, bytes,
2998 mgp->rx_done.entry, mgp->rx_done.bus);
0da34b6d
BG
2999
3000 iounmap(mgp->sram);
3001
3002#ifdef CONFIG_MTRR
3003 if (mgp->mtrr >= 0)
3004 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3005#endif
b10c0668
BG
3006 dma_free_coherent(&pdev->dev, sizeof(*mgp->fw_stats),
3007 mgp->fw_stats, mgp->fw_stats_bus);
0da34b6d 3008
b10c0668
BG
3009 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3010 mgp->cmd, mgp->cmd_bus);
0da34b6d
BG
3011
3012 free_netdev(netdev);
3013 pci_set_drvdata(pdev, NULL);
3014}
3015
b10c0668 3016#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
0da34b6d
BG
3017
3018static struct pci_device_id myri10ge_pci_tbl[] = {
b10c0668 3019 {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
0da34b6d
BG
3020 {0},
3021};
3022
3023static struct pci_driver myri10ge_driver = {
3024 .name = "myri10ge",
3025 .probe = myri10ge_probe,
3026 .remove = myri10ge_remove,
3027 .id_table = myri10ge_pci_tbl,
3028#ifdef CONFIG_PM
3029 .suspend = myri10ge_suspend,
3030 .resume = myri10ge_resume,
3031#endif
3032};
3033
3034static __init int myri10ge_init_module(void)
3035{
3036 printk(KERN_INFO "%s: Version %s\n", myri10ge_driver.name,
3037 MYRI10GE_VERSION_STR);
3038 return pci_register_driver(&myri10ge_driver);
3039}
3040
3041module_init(myri10ge_init_module);
3042
3043static __exit void myri10ge_cleanup_module(void)
3044{
3045 pci_unregister_driver(&myri10ge_driver);
3046}
3047
3048module_exit(myri10ge_cleanup_module);