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