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