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