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