blob: 5edcbfe93065edd2584d65c9d3551c6c7becf919 [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>
Andrew Gallatin1e6e9342007-09-17 11:37:42 -070051#include <linux/inet_lro.h>
Brice Goglin0da34b62006-05-23 06:10:15 -040052#include <linux/ip.h>
53#include <linux/inet.h>
54#include <linux/in.h>
55#include <linux/ethtool.h>
56#include <linux/firmware.h>
57#include <linux/delay.h>
58#include <linux/version.h>
59#include <linux/timer.h>
60#include <linux/vmalloc.h>
61#include <linux/crc32.h>
62#include <linux/moduleparam.h>
63#include <linux/io.h>
vignesh babu199126a2007-07-09 11:50:22 -070064#include <linux/log2.h>
Brice Goglin0da34b62006-05-23 06:10:15 -040065#include <net/checksum.h>
Andrew Gallatin1e6e9342007-09-17 11:37:42 -070066#include <net/ip.h>
67#include <net/tcp.h>
Brice Goglin0da34b62006-05-23 06:10:15 -040068#include <asm/byteorder.h>
69#include <asm/io.h>
Brice Goglin0da34b62006-05-23 06:10:15 -040070#include <asm/processor.h>
71#ifdef CONFIG_MTRR
72#include <asm/mtrr.h>
73#endif
74
75#include "myri10ge_mcp.h"
76#include "myri10ge_mcp_gen_header.h"
77
Brice Gogline0d7c522007-10-13 12:34:36 +020078#define MYRI10GE_VERSION_STR "1.3.2-1.287"
Brice Goglin0da34b62006-05-23 06:10:15 -040079
80MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
81MODULE_AUTHOR("Maintainer: help@myri.com");
82MODULE_VERSION(MYRI10GE_VERSION_STR);
83MODULE_LICENSE("Dual BSD/GPL");
84
85#define MYRI10GE_MAX_ETHER_MTU 9014
86
87#define MYRI10GE_ETH_STOPPED 0
88#define MYRI10GE_ETH_STOPPING 1
89#define MYRI10GE_ETH_STARTING 2
90#define MYRI10GE_ETH_RUNNING 3
91#define MYRI10GE_ETH_OPEN_FAILED 4
92
93#define MYRI10GE_EEPROM_STRINGS_SIZE 256
94#define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
Andrew Gallatin1e6e9342007-09-17 11:37:42 -070095#define MYRI10GE_MAX_LRO_DESCRIPTORS 8
96#define MYRI10GE_LRO_MAX_PKTS 64
Brice Goglin0da34b62006-05-23 06:10:15 -040097
Al Viro40f6cff2006-11-20 13:48:32 -050098#define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
Brice Goglin0da34b62006-05-23 06:10:15 -040099#define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
100
Brice Goglindd50f332006-12-11 11:25:09 +0100101#define MYRI10GE_ALLOC_ORDER 0
102#define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
103#define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
104
Brice Goglin0da34b62006-05-23 06:10:15 -0400105struct myri10ge_rx_buffer_state {
Brice Goglindd50f332006-12-11 11:25:09 +0100106 struct page *page;
107 int page_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -0400108 DECLARE_PCI_UNMAP_ADDR(bus)
109 DECLARE_PCI_UNMAP_LEN(len)
110};
111
112struct myri10ge_tx_buffer_state {
113 struct sk_buff *skb;
114 int last;
115 DECLARE_PCI_UNMAP_ADDR(bus)
116 DECLARE_PCI_UNMAP_LEN(len)
117};
118
119struct myri10ge_cmd {
120 u32 data0;
121 u32 data1;
122 u32 data2;
123};
124
125struct myri10ge_rx_buf {
126 struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
127 u8 __iomem *wc_fifo; /* w/c rx dma addr fifo address */
128 struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
129 struct myri10ge_rx_buffer_state *info;
Brice Goglindd50f332006-12-11 11:25:09 +0100130 struct page *page;
131 dma_addr_t bus;
132 int page_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -0400133 int cnt;
Brice Goglindd50f332006-12-11 11:25:09 +0100134 int fill_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -0400135 int alloc_fail;
136 int mask; /* number of rx slots -1 */
Brice Goglindd50f332006-12-11 11:25:09 +0100137 int watchdog_needed;
Brice Goglin0da34b62006-05-23 06:10:15 -0400138};
139
140struct myri10ge_tx_buf {
141 struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
142 u8 __iomem *wc_fifo; /* w/c send fifo address */
143 struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
144 char *req_bytes;
145 struct myri10ge_tx_buffer_state *info;
146 int mask; /* number of transmit slots -1 */
Brice Goglin0da34b62006-05-23 06:10:15 -0400147 int req ____cacheline_aligned; /* transmit slots submitted */
148 int pkt_start; /* packets started */
Brice Goglinb53bef82008-05-09 02:20:03 +0200149 int stop_queue;
150 int linearized;
Brice Goglin0da34b62006-05-23 06:10:15 -0400151 int done ____cacheline_aligned; /* transmit slots completed */
152 int pkt_done; /* packets completed */
Brice Goglinb53bef82008-05-09 02:20:03 +0200153 int wake_queue;
Brice Goglin0da34b62006-05-23 06:10:15 -0400154};
155
156struct myri10ge_rx_done {
157 struct mcp_slot *entry;
158 dma_addr_t bus;
159 int cnt;
160 int idx;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700161 struct net_lro_mgr lro_mgr;
162 struct net_lro_desc lro_desc[MYRI10GE_MAX_LRO_DESCRIPTORS];
Brice Goglin0da34b62006-05-23 06:10:15 -0400163};
164
Brice Goglinb53bef82008-05-09 02:20:03 +0200165struct myri10ge_slice_netstats {
166 unsigned long rx_packets;
167 unsigned long tx_packets;
168 unsigned long rx_bytes;
169 unsigned long tx_bytes;
170 unsigned long rx_dropped;
171 unsigned long tx_dropped;
172};
173
174struct myri10ge_slice_state {
Brice Goglin0da34b62006-05-23 06:10:15 -0400175 struct myri10ge_tx_buf tx; /* transmit ring */
176 struct myri10ge_rx_buf rx_small;
177 struct myri10ge_rx_buf rx_big;
178 struct myri10ge_rx_done rx_done;
Brice Goglinb53bef82008-05-09 02:20:03 +0200179 struct net_device *dev;
180 struct napi_struct napi;
181 struct myri10ge_priv *mgp;
182 struct myri10ge_slice_netstats stats;
183 __be32 __iomem *irq_claim;
184 struct mcp_irq_data *fw_stats;
185 dma_addr_t fw_stats_bus;
186 int watchdog_tx_done;
187 int watchdog_tx_req;
188};
189
190struct myri10ge_priv {
191 struct myri10ge_slice_state ss;
192 int tx_boundary; /* boundary transmits cannot cross */
193 int running; /* running? */
194 int csum_flag; /* rx_csums? */
Brice Goglin0da34b62006-05-23 06:10:15 -0400195 int small_bytes;
Brice Goglindd50f332006-12-11 11:25:09 +0100196 int big_bytes;
Brice Goglin0da34b62006-05-23 06:10:15 -0400197 struct net_device *dev;
198 struct net_device_stats stats;
Brice Goglinb53bef82008-05-09 02:20:03 +0200199 spinlock_t stats_lock;
Brice Goglin0da34b62006-05-23 06:10:15 -0400200 u8 __iomem *sram;
201 int sram_size;
202 unsigned long board_span;
203 unsigned long iomem_base;
Al Viro40f6cff2006-11-20 13:48:32 -0500204 __be32 __iomem *irq_deassert;
Brice Goglin0da34b62006-05-23 06:10:15 -0400205 char *mac_addr_string;
206 struct mcp_cmd_response *cmd;
207 dma_addr_t cmd_bus;
Brice Goglin0da34b62006-05-23 06:10:15 -0400208 struct pci_dev *pdev;
209 int msi_enabled;
Al Viro66341ff2007-12-22 18:56:43 +0000210 u32 link_state;
Brice Goglin0da34b62006-05-23 06:10:15 -0400211 unsigned int rdma_tags_available;
212 int intr_coal_delay;
Al Viro40f6cff2006-11-20 13:48:32 -0500213 __be32 __iomem *intr_coal_delay_ptr;
Brice Goglin0da34b62006-05-23 06:10:15 -0400214 int mtrr;
Brice Goglin276e26c2007-03-07 20:02:32 +0100215 int wc_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -0400216 int down_cnt;
217 wait_queue_head_t down_wq;
218 struct work_struct watchdog_work;
219 struct timer_list watchdog_timer;
Brice Goglin0da34b62006-05-23 06:10:15 -0400220 int watchdog_resets;
Brice Goglinb53bef82008-05-09 02:20:03 +0200221 int watchdog_pause;
Brice Goglin0da34b62006-05-23 06:10:15 -0400222 int pause;
223 char *fw_name;
224 char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
Brice Goglinc0bf8802008-05-09 02:18:24 +0200225 char *product_code_string;
Brice Goglin0da34b62006-05-23 06:10:15 -0400226 char fw_version[128];
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100227 int fw_ver_major;
228 int fw_ver_minor;
229 int fw_ver_tiny;
230 int adopted_rx_filter_bug;
Brice Goglin0da34b62006-05-23 06:10:15 -0400231 u8 mac_addr[6]; /* eeprom mac address */
232 unsigned long serial_number;
233 int vendor_specific_offset;
Brice Goglin85a7ea12006-08-21 17:36:56 -0400234 int fw_multicast_support;
Brice Goglin4f93fde2007-10-13 12:34:01 +0200235 unsigned long features;
236 u32 max_tso6;
Brice Goglin0da34b62006-05-23 06:10:15 -0400237 u32 read_dma;
238 u32 write_dma;
239 u32 read_write_dma;
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400240 u32 link_changes;
241 u32 msg_enable;
Brice Goglin0da34b62006-05-23 06:10:15 -0400242};
243
244static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
245static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
246
247static char *myri10ge_fw_name = NULL;
248module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200249MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
Brice Goglin0da34b62006-05-23 06:10:15 -0400250
251static int myri10ge_ecrc_enable = 1;
252module_param(myri10ge_ecrc_enable, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200253MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
Brice Goglin0da34b62006-05-23 06:10:15 -0400254
255static int myri10ge_max_intr_slots = 1024;
256module_param(myri10ge_max_intr_slots, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200257MODULE_PARM_DESC(myri10ge_max_intr_slots, "Interrupt queue slots");
Brice Goglin0da34b62006-05-23 06:10:15 -0400258
259static int myri10ge_small_bytes = -1; /* -1 == auto */
260module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200261MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
Brice Goglin0da34b62006-05-23 06:10:15 -0400262
263static int myri10ge_msi = 1; /* enable msi by default */
Brice Goglin3621cec2006-12-18 11:51:22 +0100264module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200265MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
Brice Goglin0da34b62006-05-23 06:10:15 -0400266
Brice Goglinf761fae2007-03-21 19:45:56 +0100267static int myri10ge_intr_coal_delay = 75;
Brice Goglin0da34b62006-05-23 06:10:15 -0400268module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200269MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
Brice Goglin0da34b62006-05-23 06:10:15 -0400270
271static int myri10ge_flow_control = 1;
272module_param(myri10ge_flow_control, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200273MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
Brice Goglin0da34b62006-05-23 06:10:15 -0400274
275static int myri10ge_deassert_wait = 1;
276module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
277MODULE_PARM_DESC(myri10ge_deassert_wait,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200278 "Wait when deasserting legacy interrupts");
Brice Goglin0da34b62006-05-23 06:10:15 -0400279
280static int myri10ge_force_firmware = 0;
281module_param(myri10ge_force_firmware, int, S_IRUGO);
282MODULE_PARM_DESC(myri10ge_force_firmware,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200283 "Force firmware to assume aligned completions");
Brice Goglin0da34b62006-05-23 06:10:15 -0400284
Brice Goglin0da34b62006-05-23 06:10:15 -0400285static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
286module_param(myri10ge_initial_mtu, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200287MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
Brice Goglin0da34b62006-05-23 06:10:15 -0400288
289static int myri10ge_napi_weight = 64;
290module_param(myri10ge_napi_weight, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200291MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
Brice Goglin0da34b62006-05-23 06:10:15 -0400292
293static int myri10ge_watchdog_timeout = 1;
294module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200295MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
Brice Goglin0da34b62006-05-23 06:10:15 -0400296
297static int myri10ge_max_irq_loops = 1048576;
298module_param(myri10ge_max_irq_loops, int, S_IRUGO);
299MODULE_PARM_DESC(myri10ge_max_irq_loops,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200300 "Set stuck legacy IRQ detection threshold");
Brice Goglin0da34b62006-05-23 06:10:15 -0400301
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400302#define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
303
304static int myri10ge_debug = -1; /* defaults above */
305module_param(myri10ge_debug, int, 0);
306MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
307
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700308static int myri10ge_lro = 1;
309module_param(myri10ge_lro, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200310MODULE_PARM_DESC(myri10ge_lro, "Enable large receive offload");
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700311
312static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
313module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200314MODULE_PARM_DESC(myri10ge_lro_max_pkts,
315 "Number of LRO packets to be aggregated");
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700316
Brice Goglindd50f332006-12-11 11:25:09 +0100317static int myri10ge_fill_thresh = 256;
318module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200319MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
Brice Goglindd50f332006-12-11 11:25:09 +0100320
Brice Goglinf1811372007-06-11 20:26:31 +0200321static int myri10ge_reset_recover = 1;
322
Brice Goglinf761fae2007-03-21 19:45:56 +0100323static int myri10ge_wcfifo = 0;
Brice Goglin6ebc0872007-01-09 21:04:25 +0100324module_param(myri10ge_wcfifo, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200325MODULE_PARM_DESC(myri10ge_wcfifo, "Enable WC Fifo when WC is enabled");
Brice Goglin6ebc0872007-01-09 21:04:25 +0100326
Brice Goglin0da34b62006-05-23 06:10:15 -0400327#define MYRI10GE_FW_OFFSET 1024*1024
328#define MYRI10GE_HIGHPART_TO_U32(X) \
329(sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
330#define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
331
332#define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
333
Brice Goglin2f762162007-05-07 23:50:37 +0200334static void myri10ge_set_multicast_list(struct net_device *dev);
Brice Goglin4f93fde2007-10-13 12:34:01 +0200335static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev);
Brice Goglin2f762162007-05-07 23:50:37 +0200336
Brice Goglin62502232006-12-11 11:24:37 +0100337static inline void put_be32(__be32 val, __be32 __iomem * p)
Al Viro40f6cff2006-11-20 13:48:32 -0500338{
Brice Goglin62502232006-12-11 11:24:37 +0100339 __raw_writel((__force __u32) val, (__force void __iomem *)p);
Al Viro40f6cff2006-11-20 13:48:32 -0500340}
341
Brice Goglin0da34b62006-05-23 06:10:15 -0400342static int
343myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
344 struct myri10ge_cmd *data, int atomic)
345{
346 struct mcp_cmd *buf;
347 char buf_bytes[sizeof(*buf) + 8];
348 struct mcp_cmd_response *response = mgp->cmd;
Brice Gogline700f9f2006-08-14 17:52:54 -0400349 char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
Brice Goglin0da34b62006-05-23 06:10:15 -0400350 u32 dma_low, dma_high, result, value;
351 int sleep_total = 0;
352
353 /* ensure buf is aligned to 8 bytes */
354 buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
355
356 buf->data0 = htonl(data->data0);
357 buf->data1 = htonl(data->data1);
358 buf->data2 = htonl(data->data2);
359 buf->cmd = htonl(cmd);
360 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
361 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
362
363 buf->response_addr.low = htonl(dma_low);
364 buf->response_addr.high = htonl(dma_high);
Al Viro40f6cff2006-11-20 13:48:32 -0500365 response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglin0da34b62006-05-23 06:10:15 -0400366 mb();
367 myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
368
369 /* wait up to 15ms. Longest command is the DMA benchmark,
370 * which is capped at 5ms, but runs from a timeout handler
371 * that runs every 7.8ms. So a 15ms timeout leaves us with
372 * a 2.2ms margin
373 */
374 if (atomic) {
375 /* if atomic is set, do not sleep,
376 * and try to get the completion quickly
377 * (1ms will be enough for those commands) */
378 for (sleep_total = 0;
379 sleep_total < 1000
Al Viro40f6cff2006-11-20 13:48:32 -0500380 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglinbd2db0c2008-05-09 02:18:45 +0200381 sleep_total += 10) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400382 udelay(10);
Brice Goglinbd2db0c2008-05-09 02:18:45 +0200383 mb();
384 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400385 } else {
386 /* use msleep for most command */
387 for (sleep_total = 0;
388 sleep_total < 15
Al Viro40f6cff2006-11-20 13:48:32 -0500389 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglin0da34b62006-05-23 06:10:15 -0400390 sleep_total++)
391 msleep(1);
392 }
393
394 result = ntohl(response->result);
395 value = ntohl(response->data);
396 if (result != MYRI10GE_NO_RESPONSE_RESULT) {
397 if (result == 0) {
398 data->data0 = value;
399 return 0;
Brice Goglin85a7ea12006-08-21 17:36:56 -0400400 } else if (result == MXGEFW_CMD_UNKNOWN) {
401 return -ENOSYS;
Brice Goglin5443e9e2007-05-07 23:52:22 +0200402 } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
403 return -E2BIG;
Brice Goglin0da34b62006-05-23 06:10:15 -0400404 } else {
405 dev_err(&mgp->pdev->dev,
406 "command %d failed, result = %d\n",
407 cmd, result);
408 return -ENXIO;
409 }
410 }
411
412 dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
413 cmd, result);
414 return -EAGAIN;
415}
416
417/*
418 * The eeprom strings on the lanaiX have the format
419 * SN=x\0
420 * MAC=x:x:x:x:x:x\0
421 * PT:ddd mmm xx xx:xx:xx xx\0
422 * PV:ddd mmm xx xx:xx:xx xx\0
423 */
424static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
425{
426 char *ptr, *limit;
427 int i;
428
429 ptr = mgp->eeprom_strings;
430 limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
431
432 while (*ptr != '\0' && ptr < limit) {
433 if (memcmp(ptr, "MAC=", 4) == 0) {
434 ptr += 4;
435 mgp->mac_addr_string = ptr;
436 for (i = 0; i < 6; i++) {
437 if ((ptr + 2) > limit)
438 goto abort;
439 mgp->mac_addr[i] =
440 simple_strtoul(ptr, &ptr, 16);
441 ptr += 1;
442 }
443 }
Brice Goglinc0bf8802008-05-09 02:18:24 +0200444 if (memcmp(ptr, "PC=", 3) == 0) {
445 ptr += 3;
446 mgp->product_code_string = ptr;
447 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400448 if (memcmp((const void *)ptr, "SN=", 3) == 0) {
449 ptr += 3;
450 mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
451 }
452 while (ptr < limit && *ptr++) ;
453 }
454
455 return 0;
456
457abort:
458 dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
459 return -ENXIO;
460}
461
462/*
463 * Enable or disable periodic RDMAs from the host to make certain
464 * chipsets resend dropped PCIe messages
465 */
466
467static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
468{
469 char __iomem *submit;
Brice Goglinf8fd57c2008-05-09 02:17:37 +0200470 __be32 buf[16] __attribute__ ((__aligned__(8)));
Brice Goglin0da34b62006-05-23 06:10:15 -0400471 u32 dma_low, dma_high;
472 int i;
473
474 /* clear confirmation addr */
475 mgp->cmd->data = 0;
476 mb();
477
478 /* send a rdma command to the PCIe engine, and wait for the
479 * response in the confirmation address. The firmware should
480 * write a -1 there to indicate it is alive and well
481 */
482 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
483 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
484
485 buf[0] = htonl(dma_high); /* confirm addr MSW */
486 buf[1] = htonl(dma_low); /* confirm addr LSW */
Al Viro40f6cff2006-11-20 13:48:32 -0500487 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
Brice Goglin0da34b62006-05-23 06:10:15 -0400488 buf[3] = htonl(dma_high); /* dummy addr MSW */
489 buf[4] = htonl(dma_low); /* dummy addr LSW */
490 buf[5] = htonl(enable); /* enable? */
491
Brice Gogline700f9f2006-08-14 17:52:54 -0400492 submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
Brice Goglin0da34b62006-05-23 06:10:15 -0400493
494 myri10ge_pio_copy(submit, &buf, sizeof(buf));
495 for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
496 msleep(1);
497 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
498 dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
499 (enable ? "enable" : "disable"));
500}
501
502static int
503myri10ge_validate_firmware(struct myri10ge_priv *mgp,
504 struct mcp_gen_header *hdr)
505{
506 struct device *dev = &mgp->pdev->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -0400507
508 /* check firmware type */
509 if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
510 dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
511 return -EINVAL;
512 }
513
514 /* save firmware version for ethtool */
515 strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
516
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100517 sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
518 &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
Brice Goglin0da34b62006-05-23 06:10:15 -0400519
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100520 if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR
521 && mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400522 dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
523 dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
524 MXGEFW_VERSION_MINOR);
525 return -EINVAL;
526 }
527 return 0;
528}
529
530static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
531{
532 unsigned crc, reread_crc;
533 const struct firmware *fw;
534 struct device *dev = &mgp->pdev->dev;
535 struct mcp_gen_header *hdr;
536 size_t hdr_offset;
537 int status;
Brice Gogline4543582006-07-30 00:14:09 -0400538 unsigned i;
Brice Goglin0da34b62006-05-23 06:10:15 -0400539
540 if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
541 dev_err(dev, "Unable to load %s firmware image via hotplug\n",
542 mgp->fw_name);
543 status = -EINVAL;
544 goto abort_with_nothing;
545 }
546
547 /* check size */
548
549 if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
550 fw->size < MCP_HEADER_PTR_OFFSET + 4) {
551 dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
552 status = -EINVAL;
553 goto abort_with_fw;
554 }
555
556 /* check id */
Al Viro40f6cff2006-11-20 13:48:32 -0500557 hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
Brice Goglin0da34b62006-05-23 06:10:15 -0400558 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
559 dev_err(dev, "Bad firmware file\n");
560 status = -EINVAL;
561 goto abort_with_fw;
562 }
563 hdr = (void *)(fw->data + hdr_offset);
564
565 status = myri10ge_validate_firmware(mgp, hdr);
566 if (status != 0)
567 goto abort_with_fw;
568
569 crc = crc32(~0, fw->data, fw->size);
Brice Gogline4543582006-07-30 00:14:09 -0400570 for (i = 0; i < fw->size; i += 256) {
571 myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
572 fw->data + i,
573 min(256U, (unsigned)(fw->size - i)));
574 mb();
575 readb(mgp->sram);
Brice Goglinb10c0662006-06-08 10:25:00 -0400576 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400577 /* corruption checking is good for parity recovery and buggy chipset */
578 memcpy_fromio(fw->data, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
579 reread_crc = crc32(~0, fw->data, fw->size);
580 if (crc != reread_crc) {
581 dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
582 (unsigned)fw->size, reread_crc, crc);
583 status = -EIO;
584 goto abort_with_fw;
585 }
586 *size = (u32) fw->size;
587
588abort_with_fw:
589 release_firmware(fw);
590
591abort_with_nothing:
592 return status;
593}
594
595static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
596{
597 struct mcp_gen_header *hdr;
598 struct device *dev = &mgp->pdev->dev;
599 const size_t bytes = sizeof(struct mcp_gen_header);
600 size_t hdr_offset;
601 int status;
602
603 /* find running firmware header */
Al Viro66341ff2007-12-22 18:56:43 +0000604 hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
Brice Goglin0da34b62006-05-23 06:10:15 -0400605
606 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
607 dev_err(dev, "Running firmware has bad header offset (%d)\n",
608 (int)hdr_offset);
609 return -EIO;
610 }
611
612 /* copy header of running firmware from SRAM to host memory to
613 * validate firmware */
614 hdr = kmalloc(bytes, GFP_KERNEL);
615 if (hdr == NULL) {
616 dev_err(dev, "could not malloc firmware hdr\n");
617 return -ENOMEM;
618 }
619 memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
620 status = myri10ge_validate_firmware(mgp, hdr);
621 kfree(hdr);
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100622
623 /* check to see if adopted firmware has bug where adopting
624 * it will cause broadcasts to be filtered unless the NIC
625 * is kept in ALLMULTI mode */
626 if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
627 mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
628 mgp->adopted_rx_filter_bug = 1;
629 dev_warn(dev, "Adopting fw %d.%d.%d: "
630 "working around rx filter bug\n",
631 mgp->fw_ver_major, mgp->fw_ver_minor,
632 mgp->fw_ver_tiny);
633 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400634 return status;
635}
636
637static int myri10ge_load_firmware(struct myri10ge_priv *mgp)
638{
639 char __iomem *submit;
Brice Goglinf8fd57c2008-05-09 02:17:37 +0200640 __be32 buf[16] __attribute__ ((__aligned__(8)));
Brice Goglin0da34b62006-05-23 06:10:15 -0400641 u32 dma_low, dma_high, size;
642 int status, i;
Brice Goglin4f93fde2007-10-13 12:34:01 +0200643 struct myri10ge_cmd cmd;
Brice Goglin0da34b62006-05-23 06:10:15 -0400644
Brice Goglinb10c0662006-06-08 10:25:00 -0400645 size = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -0400646 status = myri10ge_load_hotplug_firmware(mgp, &size);
647 if (status) {
648 dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
649
650 /* Do not attempt to adopt firmware if there
651 * was a bad crc */
652 if (status == -EIO)
653 return status;
654
655 status = myri10ge_adopt_running_firmware(mgp);
656 if (status != 0) {
657 dev_err(&mgp->pdev->dev,
658 "failed to adopt running firmware\n");
659 return status;
660 }
661 dev_info(&mgp->pdev->dev,
662 "Successfully adopted running firmware\n");
Brice Goglinb53bef82008-05-09 02:20:03 +0200663 if (mgp->tx_boundary == 4096) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400664 dev_warn(&mgp->pdev->dev,
665 "Using firmware currently running on NIC"
666 ". For optimal\n");
667 dev_warn(&mgp->pdev->dev,
668 "performance consider loading optimized "
669 "firmware\n");
670 dev_warn(&mgp->pdev->dev, "via hotplug\n");
671 }
672
673 mgp->fw_name = "adopted";
Brice Goglinb53bef82008-05-09 02:20:03 +0200674 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -0400675 return status;
676 }
677
678 /* clear confirmation addr */
679 mgp->cmd->data = 0;
680 mb();
681
682 /* send a reload command to the bootstrap MCP, and wait for the
683 * response in the confirmation address. The firmware should
684 * write a -1 there to indicate it is alive and well
685 */
686 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
687 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
688
689 buf[0] = htonl(dma_high); /* confirm addr MSW */
690 buf[1] = htonl(dma_low); /* confirm addr LSW */
Al Viro40f6cff2006-11-20 13:48:32 -0500691 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
Brice Goglin0da34b62006-05-23 06:10:15 -0400692
693 /* FIX: All newest firmware should un-protect the bottom of
694 * the sram before handoff. However, the very first interfaces
695 * do not. Therefore the handoff copy must skip the first 8 bytes
696 */
697 buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
698 buf[4] = htonl(size - 8); /* length of code */
699 buf[5] = htonl(8); /* where to copy to */
700 buf[6] = htonl(0); /* where to jump to */
701
Brice Gogline700f9f2006-08-14 17:52:54 -0400702 submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
Brice Goglin0da34b62006-05-23 06:10:15 -0400703
704 myri10ge_pio_copy(submit, &buf, sizeof(buf));
705 mb();
706 msleep(1);
707 mb();
708 i = 0;
Brice Goglind93ca2a2008-05-09 02:17:16 +0200709 while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
710 msleep(1 << i);
Brice Goglin0da34b62006-05-23 06:10:15 -0400711 i++;
712 }
713 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
714 dev_err(&mgp->pdev->dev, "handoff failed\n");
715 return -ENXIO;
716 }
717 dev_info(&mgp->pdev->dev, "handoff confirmed\n");
Brice Goglin9a71db72006-07-21 15:49:32 -0400718 myri10ge_dummy_rdma(mgp, 1);
Brice Goglin0da34b62006-05-23 06:10:15 -0400719
Brice Goglin4f93fde2007-10-13 12:34:01 +0200720 /* probe for IPv6 TSO support */
721 mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
722 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
723 &cmd, 0);
724 if (status == 0) {
725 mgp->max_tso6 = cmd.data0;
726 mgp->features |= NETIF_F_TSO6;
727 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400728 return 0;
729}
730
731static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
732{
733 struct myri10ge_cmd cmd;
734 int status;
735
736 cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
737 | (addr[2] << 8) | addr[3]);
738
739 cmd.data1 = ((addr[4] << 8) | (addr[5]));
740
741 status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
742 return status;
743}
744
745static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
746{
747 struct myri10ge_cmd cmd;
748 int status, ctl;
749
750 ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
751 status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
752
753 if (status) {
754 printk(KERN_ERR
755 "myri10ge: %s: Failed to set flow control mode\n",
756 mgp->dev->name);
757 return status;
758 }
759 mgp->pause = pause;
760 return 0;
761}
762
763static void
764myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
765{
766 struct myri10ge_cmd cmd;
767 int status, ctl;
768
769 ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
770 status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
771 if (status)
772 printk(KERN_ERR "myri10ge: %s: Failed to set promisc mode\n",
773 mgp->dev->name);
774}
775
Brice Goglin0d6ac252007-05-07 23:51:45 +0200776static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
777{
778 struct myri10ge_cmd cmd;
779 int status;
780 u32 len;
781 struct page *dmatest_page;
782 dma_addr_t dmatest_bus;
783 char *test = " ";
784
785 dmatest_page = alloc_page(GFP_KERNEL);
786 if (!dmatest_page)
787 return -ENOMEM;
788 dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
789 DMA_BIDIRECTIONAL);
790
791 /* Run a small DMA test.
792 * The magic multipliers to the length tell the firmware
793 * to do DMA read, write, or read+write tests. The
794 * results are returned in cmd.data0. The upper 16
795 * bits or the return is the number of transfers completed.
796 * The lower 16 bits is the time in 0.5us ticks that the
797 * transfers took to complete.
798 */
799
Brice Goglinb53bef82008-05-09 02:20:03 +0200800 len = mgp->tx_boundary;
Brice Goglin0d6ac252007-05-07 23:51:45 +0200801
802 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
803 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
804 cmd.data2 = len * 0x10000;
805 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
806 if (status != 0) {
807 test = "read";
808 goto abort;
809 }
810 mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
811 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
812 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
813 cmd.data2 = len * 0x1;
814 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
815 if (status != 0) {
816 test = "write";
817 goto abort;
818 }
819 mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
820
821 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
822 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
823 cmd.data2 = len * 0x10001;
824 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
825 if (status != 0) {
826 test = "read/write";
827 goto abort;
828 }
829 mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
830 (cmd.data0 & 0xffff);
831
832abort:
833 pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
834 put_page(dmatest_page);
835
836 if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
837 dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
838 test, status);
839
840 return status;
841}
842
Brice Goglin0da34b62006-05-23 06:10:15 -0400843static int myri10ge_reset(struct myri10ge_priv *mgp)
844{
845 struct myri10ge_cmd cmd;
846 int status;
847 size_t bytes;
Brice Goglin0da34b62006-05-23 06:10:15 -0400848
849 /* try to send a reset command to the card to see if it
850 * is alive */
851 memset(&cmd, 0, sizeof(cmd));
852 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
853 if (status != 0) {
854 dev_err(&mgp->pdev->dev, "failed reset\n");
855 return -ENXIO;
856 }
Brice Goglin0d6ac252007-05-07 23:51:45 +0200857
858 (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
Brice Goglin0da34b62006-05-23 06:10:15 -0400859
860 /* Now exchange information about interrupts */
861
Brice Goglinb53bef82008-05-09 02:20:03 +0200862 bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
863 memset(mgp->ss.rx_done.entry, 0, bytes);
Brice Goglin0da34b62006-05-23 06:10:15 -0400864 cmd.data0 = (u32) bytes;
865 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +0200866 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->ss.rx_done.bus);
867 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->ss.rx_done.bus);
Brice Goglin0da34b62006-05-23 06:10:15 -0400868 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA, &cmd, 0);
869
870 status |=
871 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +0200872 mgp->ss.irq_claim = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglindf30a742006-12-18 11:50:40 +0100873 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
874 &cmd, 0);
875 mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglin0da34b62006-05-23 06:10:15 -0400876
Brice Goglin0da34b62006-05-23 06:10:15 -0400877 status |= myri10ge_send_cmd
878 (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
Al Viro40f6cff2006-11-20 13:48:32 -0500879 mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglin0da34b62006-05-23 06:10:15 -0400880 if (status != 0) {
881 dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
882 return status;
883 }
Al Viro40f6cff2006-11-20 13:48:32 -0500884 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
Brice Goglin0da34b62006-05-23 06:10:15 -0400885
Brice Goglinb53bef82008-05-09 02:20:03 +0200886 memset(mgp->ss.rx_done.entry, 0, bytes);
Brice Goglin0da34b62006-05-23 06:10:15 -0400887
888 /* reset mcp/driver shared state back to 0 */
Brice Goglinb53bef82008-05-09 02:20:03 +0200889 mgp->ss.tx.req = 0;
890 mgp->ss.tx.done = 0;
891 mgp->ss.tx.pkt_start = 0;
892 mgp->ss.tx.pkt_done = 0;
893 mgp->ss.rx_big.cnt = 0;
894 mgp->ss.rx_small.cnt = 0;
895 mgp->ss.rx_done.idx = 0;
896 mgp->ss.rx_done.cnt = 0;
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400897 mgp->link_changes = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -0400898 status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
Brice Goglin0da34b62006-05-23 06:10:15 -0400899 myri10ge_change_pause(mgp, mgp->pause);
Brice Goglin2f762162007-05-07 23:50:37 +0200900 myri10ge_set_multicast_list(mgp->dev);
Brice Goglin0da34b62006-05-23 06:10:15 -0400901 return status;
902}
903
904static inline void
905myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
906 struct mcp_kreq_ether_recv *src)
907{
Al Viro40f6cff2006-11-20 13:48:32 -0500908 __be32 low;
Brice Goglin0da34b62006-05-23 06:10:15 -0400909
910 low = src->addr_low;
Al Viro40f6cff2006-11-20 13:48:32 -0500911 src->addr_low = htonl(DMA_32BIT_MASK);
Brice Gogline67bda52006-12-05 17:26:27 +0100912 myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
913 mb();
914 myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
Brice Goglin0da34b62006-05-23 06:10:15 -0400915 mb();
916 src->addr_low = low;
Al Viro40f6cff2006-11-20 13:48:32 -0500917 put_be32(low, &dst->addr_low);
Brice Goglin0da34b62006-05-23 06:10:15 -0400918 mb();
919}
920
Al Viro40f6cff2006-11-20 13:48:32 -0500921static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
Brice Goglin0da34b62006-05-23 06:10:15 -0400922{
923 struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
924
Al Viro40f6cff2006-11-20 13:48:32 -0500925 if ((skb->protocol == htons(ETH_P_8021Q)) &&
Brice Goglin0da34b62006-05-23 06:10:15 -0400926 (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
927 vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
928 skb->csum = hw_csum;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700929 skb->ip_summed = CHECKSUM_COMPLETE;
Brice Goglin0da34b62006-05-23 06:10:15 -0400930 }
931}
932
Brice Goglindd50f332006-12-11 11:25:09 +0100933static inline void
934myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
935 struct skb_frag_struct *rx_frags, int len, int hlen)
936{
937 struct skb_frag_struct *skb_frags;
938
939 skb->len = skb->data_len = len;
940 skb->truesize = len + sizeof(struct sk_buff);
941 /* attach the page(s) */
942
943 skb_frags = skb_shinfo(skb)->frags;
944 while (len > 0) {
945 memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
946 len -= rx_frags->size;
947 skb_frags++;
948 rx_frags++;
949 skb_shinfo(skb)->nr_frags++;
950 }
951
952 /* pskb_may_pull is not available in irq context, but
953 * skb_pull() (for ether_pad and eth_type_trans()) requires
954 * the beginning of the packet in skb_headlen(), move it
955 * manually */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300956 skb_copy_to_linear_data(skb, va, hlen);
Brice Goglindd50f332006-12-11 11:25:09 +0100957 skb_shinfo(skb)->frags[0].page_offset += hlen;
958 skb_shinfo(skb)->frags[0].size -= hlen;
959 skb->data_len -= hlen;
960 skb->tail += hlen;
961 skb_pull(skb, MXGEFW_PAD);
962}
963
964static void
965myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
966 int bytes, int watchdog)
967{
968 struct page *page;
969 int idx;
970
971 if (unlikely(rx->watchdog_needed && !watchdog))
972 return;
973
974 /* try to refill entire ring */
975 while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
976 idx = rx->fill_cnt & rx->mask;
Brice Goglinae8509b2007-04-10 21:21:08 +0200977 if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
Brice Goglindd50f332006-12-11 11:25:09 +0100978 /* we can use part of previous page */
979 get_page(rx->page);
980 } else {
981 /* we need a new page */
982 page =
983 alloc_pages(GFP_ATOMIC | __GFP_COMP,
984 MYRI10GE_ALLOC_ORDER);
985 if (unlikely(page == NULL)) {
986 if (rx->fill_cnt - rx->cnt < 16)
987 rx->watchdog_needed = 1;
988 return;
989 }
990 rx->page = page;
991 rx->page_offset = 0;
992 rx->bus = pci_map_page(mgp->pdev, page, 0,
993 MYRI10GE_ALLOC_SIZE,
994 PCI_DMA_FROMDEVICE);
995 }
996 rx->info[idx].page = rx->page;
997 rx->info[idx].page_offset = rx->page_offset;
998 /* note that this is the address of the start of the
999 * page */
1000 pci_unmap_addr_set(&rx->info[idx], bus, rx->bus);
1001 rx->shadow[idx].addr_low =
1002 htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
1003 rx->shadow[idx].addr_high =
1004 htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
1005
1006 /* start next packet on a cacheline boundary */
1007 rx->page_offset += SKB_DATA_ALIGN(bytes);
Brice Goglinae8509b2007-04-10 21:21:08 +02001008
1009#if MYRI10GE_ALLOC_SIZE > 4096
1010 /* don't cross a 4KB boundary */
1011 if ((rx->page_offset >> 12) !=
1012 ((rx->page_offset + bytes - 1) >> 12))
1013 rx->page_offset = (rx->page_offset + 4096) & ~4095;
1014#endif
Brice Goglindd50f332006-12-11 11:25:09 +01001015 rx->fill_cnt++;
1016
1017 /* copy 8 descriptors to the firmware at a time */
1018 if ((idx & 7) == 7) {
1019 if (rx->wc_fifo == NULL)
1020 myri10ge_submit_8rx(&rx->lanai[idx - 7],
1021 &rx->shadow[idx - 7]);
1022 else {
1023 mb();
1024 myri10ge_pio_copy(rx->wc_fifo,
1025 &rx->shadow[idx - 7], 64);
1026 }
1027 }
1028 }
1029}
1030
1031static inline void
1032myri10ge_unmap_rx_page(struct pci_dev *pdev,
1033 struct myri10ge_rx_buffer_state *info, int bytes)
1034{
1035 /* unmap the recvd page if we're the only or last user of it */
1036 if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
1037 (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
1038 pci_unmap_page(pdev, (pci_unmap_addr(info, bus)
1039 & ~(MYRI10GE_ALLOC_SIZE - 1)),
1040 MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
1041 }
1042}
1043
1044#define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
1045 * page into an skb */
1046
1047static inline int
Brice Goglinb53bef82008-05-09 02:20:03 +02001048myri10ge_rx_done(struct myri10ge_slice_state *ss, struct myri10ge_rx_buf *rx,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001049 int bytes, int len, __wsum csum)
Brice Goglindd50f332006-12-11 11:25:09 +01001050{
Brice Goglinb53bef82008-05-09 02:20:03 +02001051 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglindd50f332006-12-11 11:25:09 +01001052 struct sk_buff *skb;
1053 struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
1054 int i, idx, hlen, remainder;
1055 struct pci_dev *pdev = mgp->pdev;
1056 struct net_device *dev = mgp->dev;
1057 u8 *va;
1058
1059 len += MXGEFW_PAD;
1060 idx = rx->cnt & rx->mask;
1061 va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
1062 prefetch(va);
1063 /* Fill skb_frag_struct(s) with data from our receive */
1064 for (i = 0, remainder = len; remainder > 0; i++) {
1065 myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1066 rx_frags[i].page = rx->info[idx].page;
1067 rx_frags[i].page_offset = rx->info[idx].page_offset;
1068 if (remainder < MYRI10GE_ALLOC_SIZE)
1069 rx_frags[i].size = remainder;
1070 else
1071 rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
1072 rx->cnt++;
1073 idx = rx->cnt & rx->mask;
1074 remainder -= MYRI10GE_ALLOC_SIZE;
1075 }
1076
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001077 if (mgp->csum_flag && myri10ge_lro) {
1078 rx_frags[0].page_offset += MXGEFW_PAD;
1079 rx_frags[0].size -= MXGEFW_PAD;
1080 len -= MXGEFW_PAD;
Brice Goglinb53bef82008-05-09 02:20:03 +02001081 lro_receive_frags(&ss->rx_done.lro_mgr, rx_frags,
Al Viro66341ff2007-12-22 18:56:43 +00001082 len, len,
Brice Goglinb53bef82008-05-09 02:20:03 +02001083 /* opaque, will come back in get_frag_header */
1084 (void *)(__force unsigned long)csum, csum);
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001085 return 1;
1086 }
1087
Brice Goglindd50f332006-12-11 11:25:09 +01001088 hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
1089
Brice Gogline636b2e2007-10-13 12:32:21 +02001090 /* allocate an skb to attach the page(s) to. This is done
1091 * after trying LRO, so as to avoid skb allocation overheads */
Brice Goglindd50f332006-12-11 11:25:09 +01001092
1093 skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
1094 if (unlikely(skb == NULL)) {
1095 mgp->stats.rx_dropped++;
1096 do {
1097 i--;
1098 put_page(rx_frags[i].page);
1099 } while (i != 0);
1100 return 0;
1101 }
1102
1103 /* Attach the pages to the skb, and trim off any padding */
1104 myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
1105 if (skb_shinfo(skb)->frags[0].size <= 0) {
1106 put_page(skb_shinfo(skb)->frags[0].page);
1107 skb_shinfo(skb)->nr_frags = 0;
1108 }
1109 skb->protocol = eth_type_trans(skb, dev);
Brice Goglindd50f332006-12-11 11:25:09 +01001110
1111 if (mgp->csum_flag) {
1112 if ((skb->protocol == htons(ETH_P_IP)) ||
1113 (skb->protocol == htons(ETH_P_IPV6))) {
1114 skb->csum = csum;
1115 skb->ip_summed = CHECKSUM_COMPLETE;
1116 } else
1117 myri10ge_vlan_ip_csum(skb, csum);
1118 }
1119 netif_receive_skb(skb);
1120 dev->last_rx = jiffies;
1121 return 1;
1122}
1123
Brice Goglinb53bef82008-05-09 02:20:03 +02001124static inline void
1125myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
Brice Goglin0da34b62006-05-23 06:10:15 -04001126{
Brice Goglinb53bef82008-05-09 02:20:03 +02001127 struct pci_dev *pdev = ss->mgp->pdev;
1128 struct myri10ge_tx_buf *tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001129 struct sk_buff *skb;
1130 int idx, len;
Brice Goglin0da34b62006-05-23 06:10:15 -04001131
1132 while (tx->pkt_done != mcp_index) {
1133 idx = tx->done & tx->mask;
1134 skb = tx->info[idx].skb;
1135
1136 /* Mark as free */
1137 tx->info[idx].skb = NULL;
1138 if (tx->info[idx].last) {
1139 tx->pkt_done++;
1140 tx->info[idx].last = 0;
1141 }
1142 tx->done++;
1143 len = pci_unmap_len(&tx->info[idx], len);
1144 pci_unmap_len_set(&tx->info[idx], len, 0);
1145 if (skb) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001146 ss->stats.tx_bytes += skb->len;
1147 ss->stats.tx_packets++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001148 dev_kfree_skb_irq(skb);
1149 if (len)
1150 pci_unmap_single(pdev,
1151 pci_unmap_addr(&tx->info[idx],
1152 bus), len,
1153 PCI_DMA_TODEVICE);
1154 } else {
1155 if (len)
1156 pci_unmap_page(pdev,
1157 pci_unmap_addr(&tx->info[idx],
1158 bus), len,
1159 PCI_DMA_TODEVICE);
1160 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001161 }
1162 /* start the queue if we've stopped it */
Brice Goglinb53bef82008-05-09 02:20:03 +02001163 if (netif_queue_stopped(ss->dev)
Brice Goglin0da34b62006-05-23 06:10:15 -04001164 && tx->req - tx->done < (tx->mask >> 1)) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001165 tx->wake_queue++;
1166 netif_wake_queue(ss->dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04001167 }
1168}
1169
Brice Goglinb53bef82008-05-09 02:20:03 +02001170static inline int
1171myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
Brice Goglin0da34b62006-05-23 06:10:15 -04001172{
Brice Goglinb53bef82008-05-09 02:20:03 +02001173 struct myri10ge_rx_done *rx_done = &ss->rx_done;
1174 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001175 unsigned long rx_bytes = 0;
1176 unsigned long rx_packets = 0;
1177 unsigned long rx_ok;
1178
1179 int idx = rx_done->idx;
1180 int cnt = rx_done->cnt;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001181 int work_done = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04001182 u16 length;
Al Viro40f6cff2006-11-20 13:48:32 -05001183 __wsum checksum;
Brice Goglin0da34b62006-05-23 06:10:15 -04001184
Andrew Gallatinc956a242007-10-31 17:40:06 -04001185 while (rx_done->entry[idx].length != 0 && work_done < budget) {
Brice Goglin0da34b62006-05-23 06:10:15 -04001186 length = ntohs(rx_done->entry[idx].length);
1187 rx_done->entry[idx].length = 0;
Al Viro40f6cff2006-11-20 13:48:32 -05001188 checksum = csum_unfold(rx_done->entry[idx].checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001189 if (length <= mgp->small_bytes)
Brice Goglinb53bef82008-05-09 02:20:03 +02001190 rx_ok = myri10ge_rx_done(ss, &ss->rx_small,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001191 mgp->small_bytes,
1192 length, checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001193 else
Brice Goglinb53bef82008-05-09 02:20:03 +02001194 rx_ok = myri10ge_rx_done(ss, &ss->rx_big,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001195 mgp->big_bytes,
1196 length, checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001197 rx_packets += rx_ok;
1198 rx_bytes += rx_ok * (unsigned long)length;
1199 cnt++;
1200 idx = cnt & (myri10ge_max_intr_slots - 1);
Andrew Gallatinc956a242007-10-31 17:40:06 -04001201 work_done++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001202 }
1203 rx_done->idx = idx;
1204 rx_done->cnt = cnt;
Brice Goglinb53bef82008-05-09 02:20:03 +02001205 ss->stats.rx_packets += rx_packets;
1206 ss->stats.rx_bytes += rx_bytes;
Brice Goglinc7dab992006-12-11 11:25:42 +01001207
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001208 if (myri10ge_lro)
1209 lro_flush_all(&rx_done->lro_mgr);
1210
Brice Goglinc7dab992006-12-11 11:25:42 +01001211 /* restock receive rings if needed */
Brice Goglinb53bef82008-05-09 02:20:03 +02001212 if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
1213 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
Brice Goglinc7dab992006-12-11 11:25:42 +01001214 mgp->small_bytes + MXGEFW_PAD, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001215 if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
1216 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
Brice Goglinc7dab992006-12-11 11:25:42 +01001217
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001218 return work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001219}
1220
1221static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
1222{
Brice Goglinb53bef82008-05-09 02:20:03 +02001223 struct mcp_irq_data *stats = mgp->ss.fw_stats;
Brice Goglin0da34b62006-05-23 06:10:15 -04001224
1225 if (unlikely(stats->stats_updated)) {
Brice Goglin798a95d2007-06-11 20:26:50 +02001226 unsigned link_up = ntohl(stats->link_up);
1227 if (mgp->link_state != link_up) {
1228 mgp->link_state = link_up;
1229
1230 if (mgp->link_state == MXGEFW_LINK_UP) {
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001231 if (netif_msg_link(mgp))
1232 printk(KERN_INFO
1233 "myri10ge: %s: link up\n",
1234 mgp->dev->name);
Brice Goglin0da34b62006-05-23 06:10:15 -04001235 netif_carrier_on(mgp->dev);
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001236 mgp->link_changes++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001237 } else {
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001238 if (netif_msg_link(mgp))
1239 printk(KERN_INFO
Brice Goglin798a95d2007-06-11 20:26:50 +02001240 "myri10ge: %s: link %s\n",
1241 mgp->dev->name,
1242 (link_up == MXGEFW_LINK_MYRINET ?
1243 "mismatch (Myrinet detected)" :
1244 "down"));
Brice Goglin0da34b62006-05-23 06:10:15 -04001245 netif_carrier_off(mgp->dev);
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001246 mgp->link_changes++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001247 }
1248 }
1249 if (mgp->rdma_tags_available !=
Brice Goglinb53bef82008-05-09 02:20:03 +02001250 ntohl(stats->rdma_tags_available)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04001251 mgp->rdma_tags_available =
Brice Goglinb53bef82008-05-09 02:20:03 +02001252 ntohl(stats->rdma_tags_available);
Brice Goglin0da34b62006-05-23 06:10:15 -04001253 printk(KERN_WARNING "myri10ge: %s: RDMA timed out! "
1254 "%d tags left\n", mgp->dev->name,
1255 mgp->rdma_tags_available);
1256 }
1257 mgp->down_cnt += stats->link_down;
1258 if (stats->link_down)
1259 wake_up(&mgp->down_wq);
1260 }
1261}
1262
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001263static int myri10ge_poll(struct napi_struct *napi, int budget)
Brice Goglin0da34b62006-05-23 06:10:15 -04001264{
Brice Goglinb53bef82008-05-09 02:20:03 +02001265 struct myri10ge_slice_state *ss =
1266 container_of(napi, struct myri10ge_slice_state, napi);
1267 struct net_device *netdev = ss->mgp->dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001268 int work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001269
1270 /* process as many rx events as NAPI will allow */
Brice Goglinb53bef82008-05-09 02:20:03 +02001271 work_done = myri10ge_clean_rx_done(ss, budget);
Brice Goglin0da34b62006-05-23 06:10:15 -04001272
David S. Miller4ec24112008-01-07 20:48:21 -08001273 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001274 netif_rx_complete(netdev, napi);
Brice Goglinb53bef82008-05-09 02:20:03 +02001275 put_be32(htonl(3), ss->irq_claim);
Brice Goglin0da34b62006-05-23 06:10:15 -04001276 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001277 return work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001278}
1279
David Howells7d12e782006-10-05 14:55:46 +01001280static irqreturn_t myri10ge_intr(int irq, void *arg)
Brice Goglin0da34b62006-05-23 06:10:15 -04001281{
Brice Goglinb53bef82008-05-09 02:20:03 +02001282 struct myri10ge_slice_state *ss = arg;
1283 struct myri10ge_priv *mgp = ss->mgp;
1284 struct mcp_irq_data *stats = ss->fw_stats;
1285 struct myri10ge_tx_buf *tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001286 u32 send_done_count;
1287 int i;
1288
1289 /* make sure it is our IRQ, and that the DMA has finished */
1290 if (unlikely(!stats->valid))
1291 return (IRQ_NONE);
1292
1293 /* low bit indicates receives are present, so schedule
1294 * napi poll handler */
1295 if (stats->valid & 1)
Brice Goglinb53bef82008-05-09 02:20:03 +02001296 netif_rx_schedule(ss->dev, &ss->napi);
Brice Goglin0da34b62006-05-23 06:10:15 -04001297
1298 if (!mgp->msi_enabled) {
Al Viro40f6cff2006-11-20 13:48:32 -05001299 put_be32(0, mgp->irq_deassert);
Brice Goglin0da34b62006-05-23 06:10:15 -04001300 if (!myri10ge_deassert_wait)
1301 stats->valid = 0;
1302 mb();
1303 } else
1304 stats->valid = 0;
1305
1306 /* Wait for IRQ line to go low, if using INTx */
1307 i = 0;
1308 while (1) {
1309 i++;
1310 /* check for transmit completes and receives */
1311 send_done_count = ntohl(stats->send_done_count);
1312 if (send_done_count != tx->pkt_done)
Brice Goglinb53bef82008-05-09 02:20:03 +02001313 myri10ge_tx_done(ss, (int)send_done_count);
Brice Goglin0da34b62006-05-23 06:10:15 -04001314 if (unlikely(i > myri10ge_max_irq_loops)) {
1315 printk(KERN_WARNING "myri10ge: %s: irq stuck?\n",
1316 mgp->dev->name);
1317 stats->valid = 0;
1318 schedule_work(&mgp->watchdog_work);
1319 }
1320 if (likely(stats->valid == 0))
1321 break;
1322 cpu_relax();
1323 barrier();
1324 }
1325
1326 myri10ge_check_statblock(mgp);
1327
Brice Goglinb53bef82008-05-09 02:20:03 +02001328 put_be32(htonl(3), ss->irq_claim + 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04001329 return (IRQ_HANDLED);
1330}
1331
1332static int
1333myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1334{
Brice Goglinc0bf8802008-05-09 02:18:24 +02001335 struct myri10ge_priv *mgp = netdev_priv(netdev);
1336 char *ptr;
1337 int i;
1338
Brice Goglin0da34b62006-05-23 06:10:15 -04001339 cmd->autoneg = AUTONEG_DISABLE;
1340 cmd->speed = SPEED_10000;
1341 cmd->duplex = DUPLEX_FULL;
Brice Goglinc0bf8802008-05-09 02:18:24 +02001342
1343 /*
1344 * parse the product code to deterimine the interface type
1345 * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
1346 * after the 3rd dash in the driver's cached copy of the
1347 * EEPROM's product code string.
1348 */
1349 ptr = mgp->product_code_string;
1350 if (ptr == NULL) {
1351 printk(KERN_ERR "myri10ge: %s: Missing product code\n",
Brice Goglin99f5f872008-05-09 02:19:08 +02001352 netdev->name);
Brice Goglinc0bf8802008-05-09 02:18:24 +02001353 return 0;
1354 }
1355 for (i = 0; i < 3; i++, ptr++) {
1356 ptr = strchr(ptr, '-');
1357 if (ptr == NULL) {
1358 printk(KERN_ERR "myri10ge: %s: Invalid product "
1359 "code %s\n", netdev->name,
1360 mgp->product_code_string);
1361 return 0;
1362 }
1363 }
1364 if (*ptr == 'R' || *ptr == 'Q') {
1365 /* We've found either an XFP or quad ribbon fiber */
1366 cmd->port = PORT_FIBRE;
1367 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001368 return 0;
1369}
1370
1371static void
1372myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
1373{
1374 struct myri10ge_priv *mgp = netdev_priv(netdev);
1375
1376 strlcpy(info->driver, "myri10ge", sizeof(info->driver));
1377 strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
1378 strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
1379 strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
1380}
1381
1382static int
1383myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1384{
1385 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001386
Brice Goglin0da34b62006-05-23 06:10:15 -04001387 coal->rx_coalesce_usecs = mgp->intr_coal_delay;
1388 return 0;
1389}
1390
1391static int
1392myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1393{
1394 struct myri10ge_priv *mgp = netdev_priv(netdev);
1395
1396 mgp->intr_coal_delay = coal->rx_coalesce_usecs;
Al Viro40f6cff2006-11-20 13:48:32 -05001397 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
Brice Goglin0da34b62006-05-23 06:10:15 -04001398 return 0;
1399}
1400
1401static void
1402myri10ge_get_pauseparam(struct net_device *netdev,
1403 struct ethtool_pauseparam *pause)
1404{
1405 struct myri10ge_priv *mgp = netdev_priv(netdev);
1406
1407 pause->autoneg = 0;
1408 pause->rx_pause = mgp->pause;
1409 pause->tx_pause = mgp->pause;
1410}
1411
1412static int
1413myri10ge_set_pauseparam(struct net_device *netdev,
1414 struct ethtool_pauseparam *pause)
1415{
1416 struct myri10ge_priv *mgp = netdev_priv(netdev);
1417
1418 if (pause->tx_pause != mgp->pause)
1419 return myri10ge_change_pause(mgp, pause->tx_pause);
1420 if (pause->rx_pause != mgp->pause)
1421 return myri10ge_change_pause(mgp, pause->tx_pause);
1422 if (pause->autoneg != 0)
1423 return -EINVAL;
1424 return 0;
1425}
1426
1427static void
1428myri10ge_get_ringparam(struct net_device *netdev,
1429 struct ethtool_ringparam *ring)
1430{
1431 struct myri10ge_priv *mgp = netdev_priv(netdev);
1432
Brice Goglinb53bef82008-05-09 02:20:03 +02001433 ring->rx_mini_max_pending = mgp->ss.rx_small.mask + 1;
1434 ring->rx_max_pending = mgp->ss.rx_big.mask + 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001435 ring->rx_jumbo_max_pending = 0;
Brice Goglinb53bef82008-05-09 02:20:03 +02001436 ring->tx_max_pending = mgp->ss.rx_small.mask + 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001437 ring->rx_mini_pending = ring->rx_mini_max_pending;
1438 ring->rx_pending = ring->rx_max_pending;
1439 ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
1440 ring->tx_pending = ring->tx_max_pending;
1441}
1442
1443static u32 myri10ge_get_rx_csum(struct net_device *netdev)
1444{
1445 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001446
Brice Goglin0da34b62006-05-23 06:10:15 -04001447 if (mgp->csum_flag)
1448 return 1;
1449 else
1450 return 0;
1451}
1452
1453static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
1454{
1455 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001456
Brice Goglin0da34b62006-05-23 06:10:15 -04001457 if (csum_enabled)
1458 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
1459 else
1460 mgp->csum_flag = 0;
1461 return 0;
1462}
1463
Brice Goglin4f93fde2007-10-13 12:34:01 +02001464static int myri10ge_set_tso(struct net_device *netdev, u32 tso_enabled)
1465{
1466 struct myri10ge_priv *mgp = netdev_priv(netdev);
1467 unsigned long flags = mgp->features & (NETIF_F_TSO6 | NETIF_F_TSO);
1468
1469 if (tso_enabled)
1470 netdev->features |= flags;
1471 else
1472 netdev->features &= ~flags;
1473 return 0;
1474}
1475
Brice Goglinb53bef82008-05-09 02:20:03 +02001476static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
Brice Goglin0da34b62006-05-23 06:10:15 -04001477 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
1478 "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
1479 "rx_length_errors", "rx_over_errors", "rx_crc_errors",
1480 "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
1481 "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
1482 "tx_heartbeat_errors", "tx_window_errors",
1483 /* device-specific stats */
Brice Goglin2c1a1082006-07-03 18:16:46 -04001484 "tx_boundary", "WC", "irq", "MSI",
Brice Goglin0da34b62006-05-23 06:10:15 -04001485 "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
Brice Goglinb53bef82008-05-09 02:20:03 +02001486 "serial_number", "watchdog_resets",
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001487 "link_changes", "link_up", "dropped_link_overflow",
Brice Goglincee505d2007-05-07 23:49:25 +02001488 "dropped_link_error_or_filtered",
1489 "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
1490 "dropped_unicast_filtered", "dropped_multicast_filtered",
Brice Goglin0da34b62006-05-23 06:10:15 -04001491 "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
Brice Goglinb53bef82008-05-09 02:20:03 +02001492 "dropped_no_big_buffer"
1493};
1494
1495static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
1496 "----------- slice ---------",
1497 "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
1498 "rx_small_cnt", "rx_big_cnt",
1499 "wake_queue", "stop_queue", "tx_linearized", "LRO aggregated",
1500 "LRO flushed",
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001501 "LRO avg aggr", "LRO no_desc"
Brice Goglin0da34b62006-05-23 06:10:15 -04001502};
1503
1504#define MYRI10GE_NET_STATS_LEN 21
Brice Goglinb53bef82008-05-09 02:20:03 +02001505#define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
1506#define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
Brice Goglin0da34b62006-05-23 06:10:15 -04001507
1508static void
1509myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
1510{
1511 switch (stringset) {
1512 case ETH_SS_STATS:
Brice Goglinb53bef82008-05-09 02:20:03 +02001513 memcpy(data, *myri10ge_gstrings_main_stats,
1514 sizeof(myri10ge_gstrings_main_stats));
1515 data += sizeof(myri10ge_gstrings_main_stats);
1516 memcpy(data, *myri10ge_gstrings_slice_stats,
1517 sizeof(myri10ge_gstrings_slice_stats));
1518 data += sizeof(myri10ge_gstrings_slice_stats);
Brice Goglin0da34b62006-05-23 06:10:15 -04001519 break;
1520 }
1521}
1522
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001523static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
Brice Goglin0da34b62006-05-23 06:10:15 -04001524{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001525 switch (sset) {
1526 case ETH_SS_STATS:
Brice Goglinb53bef82008-05-09 02:20:03 +02001527 return MYRI10GE_MAIN_STATS_LEN + MYRI10GE_SLICE_STATS_LEN;
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001528 default:
1529 return -EOPNOTSUPP;
1530 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001531}
1532
1533static void
1534myri10ge_get_ethtool_stats(struct net_device *netdev,
1535 struct ethtool_stats *stats, u64 * data)
1536{
1537 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglinb53bef82008-05-09 02:20:03 +02001538 struct myri10ge_slice_state *ss;
Brice Goglin0da34b62006-05-23 06:10:15 -04001539 int i;
1540
1541 for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1542 data[i] = ((unsigned long *)&mgp->stats)[i];
1543
Brice Goglinb53bef82008-05-09 02:20:03 +02001544 data[i++] = (unsigned int)mgp->tx_boundary;
Brice Goglin276e26c2007-03-07 20:02:32 +01001545 data[i++] = (unsigned int)mgp->wc_enabled;
Brice Goglin2c1a1082006-07-03 18:16:46 -04001546 data[i++] = (unsigned int)mgp->pdev->irq;
1547 data[i++] = (unsigned int)mgp->msi_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04001548 data[i++] = (unsigned int)mgp->read_dma;
1549 data[i++] = (unsigned int)mgp->write_dma;
1550 data[i++] = (unsigned int)mgp->read_write_dma;
1551 data[i++] = (unsigned int)mgp->serial_number;
Brice Goglin0da34b62006-05-23 06:10:15 -04001552 data[i++] = (unsigned int)mgp->watchdog_resets;
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001553 data[i++] = (unsigned int)mgp->link_changes;
Brice Goglinb53bef82008-05-09 02:20:03 +02001554
1555 /* firmware stats are useful only in the first slice */
1556 ss = &mgp->ss;
1557 data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
1558 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001559 data[i++] =
Brice Goglinb53bef82008-05-09 02:20:03 +02001560 (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
1561 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
1562 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
1563 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
1564 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
Brice Goglincee505d2007-05-07 23:49:25 +02001565 data[i++] =
Brice Goglinb53bef82008-05-09 02:20:03 +02001566 (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
1567 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
1568 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
1569 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
1570 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
1571
1572 data[i++] = 0;
1573 data[i++] = (unsigned int)ss->tx.pkt_start;
1574 data[i++] = (unsigned int)ss->tx.pkt_done;
1575 data[i++] = (unsigned int)ss->tx.req;
1576 data[i++] = (unsigned int)ss->tx.done;
1577 data[i++] = (unsigned int)ss->rx_small.cnt;
1578 data[i++] = (unsigned int)ss->rx_big.cnt;
1579 data[i++] = (unsigned int)ss->tx.wake_queue;
1580 data[i++] = (unsigned int)ss->tx.stop_queue;
1581 data[i++] = (unsigned int)ss->tx.linearized;
1582 data[i++] = ss->rx_done.lro_mgr.stats.aggregated;
1583 data[i++] = ss->rx_done.lro_mgr.stats.flushed;
1584 if (ss->rx_done.lro_mgr.stats.flushed)
1585 data[i++] = ss->rx_done.lro_mgr.stats.aggregated /
1586 ss->rx_done.lro_mgr.stats.flushed;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001587 else
1588 data[i++] = 0;
Brice Goglinb53bef82008-05-09 02:20:03 +02001589 data[i++] = ss->rx_done.lro_mgr.stats.no_desc;
Brice Goglin0da34b62006-05-23 06:10:15 -04001590}
1591
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001592static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
1593{
1594 struct myri10ge_priv *mgp = netdev_priv(netdev);
1595 mgp->msg_enable = value;
1596}
1597
1598static u32 myri10ge_get_msglevel(struct net_device *netdev)
1599{
1600 struct myri10ge_priv *mgp = netdev_priv(netdev);
1601 return mgp->msg_enable;
1602}
1603
Jeff Garzik7282d492006-09-13 14:30:00 -04001604static const struct ethtool_ops myri10ge_ethtool_ops = {
Brice Goglin0da34b62006-05-23 06:10:15 -04001605 .get_settings = myri10ge_get_settings,
1606 .get_drvinfo = myri10ge_get_drvinfo,
1607 .get_coalesce = myri10ge_get_coalesce,
1608 .set_coalesce = myri10ge_set_coalesce,
1609 .get_pauseparam = myri10ge_get_pauseparam,
1610 .set_pauseparam = myri10ge_set_pauseparam,
1611 .get_ringparam = myri10ge_get_ringparam,
1612 .get_rx_csum = myri10ge_get_rx_csum,
1613 .set_rx_csum = myri10ge_set_rx_csum,
Brice Goglinb10c0662006-06-08 10:25:00 -04001614 .set_tx_csum = ethtool_op_set_tx_hw_csum,
Brice Goglin0da34b62006-05-23 06:10:15 -04001615 .set_sg = ethtool_op_set_sg,
Brice Goglin4f93fde2007-10-13 12:34:01 +02001616 .set_tso = myri10ge_set_tso,
Brice Goglin6ffdd072007-05-30 21:13:59 +02001617 .get_link = ethtool_op_get_link,
Brice Goglin0da34b62006-05-23 06:10:15 -04001618 .get_strings = myri10ge_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001619 .get_sset_count = myri10ge_get_sset_count,
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001620 .get_ethtool_stats = myri10ge_get_ethtool_stats,
1621 .set_msglevel = myri10ge_set_msglevel,
1622 .get_msglevel = myri10ge_get_msglevel
Brice Goglin0da34b62006-05-23 06:10:15 -04001623};
1624
Brice Goglinb53bef82008-05-09 02:20:03 +02001625static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
Brice Goglin0da34b62006-05-23 06:10:15 -04001626{
Brice Goglinb53bef82008-05-09 02:20:03 +02001627 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001628 struct myri10ge_cmd cmd;
Brice Goglinb53bef82008-05-09 02:20:03 +02001629 struct net_device *dev = mgp->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -04001630 int tx_ring_size, rx_ring_size;
1631 int tx_ring_entries, rx_ring_entries;
1632 int i, status;
1633 size_t bytes;
1634
Brice Goglin0da34b62006-05-23 06:10:15 -04001635 /* get ring sizes */
Brice Goglin0da34b62006-05-23 06:10:15 -04001636 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
1637 tx_ring_size = cmd.data0;
1638 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
Brice Goglin355c7262007-03-07 19:59:52 +01001639 if (status != 0)
1640 return status;
Brice Goglin0da34b62006-05-23 06:10:15 -04001641 rx_ring_size = cmd.data0;
1642
1643 tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
1644 rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
Brice Goglinb53bef82008-05-09 02:20:03 +02001645 ss->tx.mask = tx_ring_entries - 1;
1646 ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001647
Brice Goglin355c7262007-03-07 19:59:52 +01001648 status = -ENOMEM;
1649
Brice Goglin0da34b62006-05-23 06:10:15 -04001650 /* allocate the host shadow rings */
1651
1652 bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
Brice Goglinb53bef82008-05-09 02:20:03 +02001653 * sizeof(*ss->tx.req_list);
1654 ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
1655 if (ss->tx.req_bytes == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001656 goto abort_with_nothing;
1657
1658 /* ensure req_list entries are aligned to 8 bytes */
Brice Goglinb53bef82008-05-09 02:20:03 +02001659 ss->tx.req_list = (struct mcp_kreq_ether_send *)
1660 ALIGN((unsigned long)ss->tx.req_bytes, 8);
Brice Goglin0da34b62006-05-23 06:10:15 -04001661
Brice Goglinb53bef82008-05-09 02:20:03 +02001662 bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
1663 ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
1664 if (ss->rx_small.shadow == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001665 goto abort_with_tx_req_bytes;
1666
Brice Goglinb53bef82008-05-09 02:20:03 +02001667 bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
1668 ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
1669 if (ss->rx_big.shadow == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001670 goto abort_with_rx_small_shadow;
1671
1672 /* allocate the host info rings */
1673
Brice Goglinb53bef82008-05-09 02:20:03 +02001674 bytes = tx_ring_entries * sizeof(*ss->tx.info);
1675 ss->tx.info = kzalloc(bytes, GFP_KERNEL);
1676 if (ss->tx.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001677 goto abort_with_rx_big_shadow;
1678
Brice Goglinb53bef82008-05-09 02:20:03 +02001679 bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
1680 ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
1681 if (ss->rx_small.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001682 goto abort_with_tx_info;
1683
Brice Goglinb53bef82008-05-09 02:20:03 +02001684 bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
1685 ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
1686 if (ss->rx_big.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001687 goto abort_with_rx_small_info;
1688
1689 /* Fill the receive rings */
Brice Goglinb53bef82008-05-09 02:20:03 +02001690 ss->rx_big.cnt = 0;
1691 ss->rx_small.cnt = 0;
1692 ss->rx_big.fill_cnt = 0;
1693 ss->rx_small.fill_cnt = 0;
1694 ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
1695 ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
1696 ss->rx_small.watchdog_needed = 0;
1697 ss->rx_big.watchdog_needed = 0;
1698 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
Brice Goglinc7dab992006-12-11 11:25:42 +01001699 mgp->small_bytes + MXGEFW_PAD, 0);
Brice Goglin0da34b62006-05-23 06:10:15 -04001700
Brice Goglinb53bef82008-05-09 02:20:03 +02001701 if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
Brice Goglinc7dab992006-12-11 11:25:42 +01001702 printk(KERN_ERR "myri10ge: %s: alloced only %d small bufs\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02001703 dev->name, ss->rx_small.fill_cnt);
Brice Goglinc7dab992006-12-11 11:25:42 +01001704 goto abort_with_rx_small_ring;
Brice Goglin0da34b62006-05-23 06:10:15 -04001705 }
1706
Brice Goglinb53bef82008-05-09 02:20:03 +02001707 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
1708 if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
Brice Goglinc7dab992006-12-11 11:25:42 +01001709 printk(KERN_ERR "myri10ge: %s: alloced only %d big bufs\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02001710 dev->name, ss->rx_big.fill_cnt);
Brice Goglinc7dab992006-12-11 11:25:42 +01001711 goto abort_with_rx_big_ring;
Brice Goglin0da34b62006-05-23 06:10:15 -04001712 }
1713
1714 return 0;
1715
1716abort_with_rx_big_ring:
Brice Goglinb53bef82008-05-09 02:20:03 +02001717 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
1718 int idx = i & ss->rx_big.mask;
1719 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001720 mgp->big_bytes);
Brice Goglinb53bef82008-05-09 02:20:03 +02001721 put_page(ss->rx_big.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001722 }
1723
1724abort_with_rx_small_ring:
Brice Goglinb53bef82008-05-09 02:20:03 +02001725 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
1726 int idx = i & ss->rx_small.mask;
1727 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001728 mgp->small_bytes + MXGEFW_PAD);
Brice Goglinb53bef82008-05-09 02:20:03 +02001729 put_page(ss->rx_small.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001730 }
Brice Goglinc7dab992006-12-11 11:25:42 +01001731
Brice Goglinb53bef82008-05-09 02:20:03 +02001732 kfree(ss->rx_big.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001733
1734abort_with_rx_small_info:
Brice Goglinb53bef82008-05-09 02:20:03 +02001735 kfree(ss->rx_small.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001736
1737abort_with_tx_info:
Brice Goglinb53bef82008-05-09 02:20:03 +02001738 kfree(ss->tx.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001739
1740abort_with_rx_big_shadow:
Brice Goglinb53bef82008-05-09 02:20:03 +02001741 kfree(ss->rx_big.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001742
1743abort_with_rx_small_shadow:
Brice Goglinb53bef82008-05-09 02:20:03 +02001744 kfree(ss->rx_small.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001745
1746abort_with_tx_req_bytes:
Brice Goglinb53bef82008-05-09 02:20:03 +02001747 kfree(ss->tx.req_bytes);
1748 ss->tx.req_bytes = NULL;
1749 ss->tx.req_list = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04001750
1751abort_with_nothing:
1752 return status;
1753}
1754
Brice Goglinb53bef82008-05-09 02:20:03 +02001755static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
Brice Goglin0da34b62006-05-23 06:10:15 -04001756{
Brice Goglinb53bef82008-05-09 02:20:03 +02001757 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001758 struct sk_buff *skb;
1759 struct myri10ge_tx_buf *tx;
1760 int i, len, idx;
1761
Brice Goglinb53bef82008-05-09 02:20:03 +02001762 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
1763 idx = i & ss->rx_big.mask;
1764 if (i == ss->rx_big.fill_cnt - 1)
1765 ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
1766 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001767 mgp->big_bytes);
Brice Goglinb53bef82008-05-09 02:20:03 +02001768 put_page(ss->rx_big.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001769 }
1770
Brice Goglinb53bef82008-05-09 02:20:03 +02001771 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
1772 idx = i & ss->rx_small.mask;
1773 if (i == ss->rx_small.fill_cnt - 1)
1774 ss->rx_small.info[idx].page_offset =
Brice Goglinc7dab992006-12-11 11:25:42 +01001775 MYRI10GE_ALLOC_SIZE;
Brice Goglinb53bef82008-05-09 02:20:03 +02001776 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001777 mgp->small_bytes + MXGEFW_PAD);
Brice Goglinb53bef82008-05-09 02:20:03 +02001778 put_page(ss->rx_small.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001779 }
Brice Goglinb53bef82008-05-09 02:20:03 +02001780 tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001781 while (tx->done != tx->req) {
1782 idx = tx->done & tx->mask;
1783 skb = tx->info[idx].skb;
1784
1785 /* Mark as free */
1786 tx->info[idx].skb = NULL;
1787 tx->done++;
1788 len = pci_unmap_len(&tx->info[idx], len);
1789 pci_unmap_len_set(&tx->info[idx], len, 0);
1790 if (skb) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001791 ss->stats.tx_dropped++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001792 dev_kfree_skb_any(skb);
1793 if (len)
1794 pci_unmap_single(mgp->pdev,
1795 pci_unmap_addr(&tx->info[idx],
1796 bus), len,
1797 PCI_DMA_TODEVICE);
1798 } else {
1799 if (len)
1800 pci_unmap_page(mgp->pdev,
1801 pci_unmap_addr(&tx->info[idx],
1802 bus), len,
1803 PCI_DMA_TODEVICE);
1804 }
1805 }
Brice Goglinb53bef82008-05-09 02:20:03 +02001806 kfree(ss->rx_big.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001807
Brice Goglinb53bef82008-05-09 02:20:03 +02001808 kfree(ss->rx_small.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001809
Brice Goglinb53bef82008-05-09 02:20:03 +02001810 kfree(ss->tx.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001811
Brice Goglinb53bef82008-05-09 02:20:03 +02001812 kfree(ss->rx_big.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001813
Brice Goglinb53bef82008-05-09 02:20:03 +02001814 kfree(ss->rx_small.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001815
Brice Goglinb53bef82008-05-09 02:20:03 +02001816 kfree(ss->tx.req_bytes);
1817 ss->tx.req_bytes = NULL;
1818 ss->tx.req_list = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04001819}
1820
Brice Goglindf30a742006-12-18 11:50:40 +01001821static int myri10ge_request_irq(struct myri10ge_priv *mgp)
1822{
1823 struct pci_dev *pdev = mgp->pdev;
1824 int status;
1825
1826 if (myri10ge_msi) {
1827 status = pci_enable_msi(pdev);
1828 if (status != 0)
1829 dev_err(&pdev->dev,
1830 "Error %d setting up MSI; falling back to xPIC\n",
1831 status);
1832 else
1833 mgp->msi_enabled = 1;
1834 } else {
1835 mgp->msi_enabled = 0;
1836 }
1837 status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
1838 mgp->dev->name, mgp);
1839 if (status != 0) {
1840 dev_err(&pdev->dev, "failed to allocate IRQ\n");
1841 if (mgp->msi_enabled)
1842 pci_disable_msi(pdev);
1843 }
1844 return status;
1845}
1846
1847static void myri10ge_free_irq(struct myri10ge_priv *mgp)
1848{
1849 struct pci_dev *pdev = mgp->pdev;
1850
1851 free_irq(pdev->irq, mgp);
1852 if (mgp->msi_enabled)
1853 pci_disable_msi(pdev);
1854}
1855
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001856static int
1857myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
1858 void **ip_hdr, void **tcpudp_hdr,
1859 u64 * hdr_flags, void *priv)
1860{
1861 struct ethhdr *eh;
1862 struct vlan_ethhdr *veh;
1863 struct iphdr *iph;
1864 u8 *va = page_address(frag->page) + frag->page_offset;
1865 unsigned long ll_hlen;
Al Viro66341ff2007-12-22 18:56:43 +00001866 /* passed opaque through lro_receive_frags() */
1867 __wsum csum = (__force __wsum) (unsigned long)priv;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001868
1869 /* find the mac header, aborting if not IPv4 */
1870
1871 eh = (struct ethhdr *)va;
1872 *mac_hdr = eh;
1873 ll_hlen = ETH_HLEN;
1874 if (eh->h_proto != htons(ETH_P_IP)) {
1875 if (eh->h_proto == htons(ETH_P_8021Q)) {
1876 veh = (struct vlan_ethhdr *)va;
1877 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
1878 return -1;
1879
1880 ll_hlen += VLAN_HLEN;
1881
1882 /*
1883 * HW checksum starts ETH_HLEN bytes into
1884 * frame, so we must subtract off the VLAN
1885 * header's checksum before csum can be used
1886 */
1887 csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
1888 VLAN_HLEN, 0));
1889 } else {
1890 return -1;
1891 }
1892 }
1893 *hdr_flags = LRO_IPV4;
1894
1895 iph = (struct iphdr *)(va + ll_hlen);
1896 *ip_hdr = iph;
1897 if (iph->protocol != IPPROTO_TCP)
1898 return -1;
1899 *hdr_flags |= LRO_TCP;
1900 *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
1901
1902 /* verify the IP checksum */
1903 if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
1904 return -1;
1905
1906 /* verify the checksum */
1907 if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
1908 ntohs(iph->tot_len) - (iph->ihl << 2),
1909 IPPROTO_TCP, csum)))
1910 return -1;
1911
1912 return 0;
1913}
1914
Brice Goglin0da34b62006-05-23 06:10:15 -04001915static int myri10ge_open(struct net_device *dev)
1916{
Brice Goglinb53bef82008-05-09 02:20:03 +02001917 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04001918 struct myri10ge_cmd cmd;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001919 struct net_lro_mgr *lro_mgr;
Brice Goglin0da34b62006-05-23 06:10:15 -04001920 int status, big_pow2;
1921
Brice Goglin0da34b62006-05-23 06:10:15 -04001922 if (mgp->running != MYRI10GE_ETH_STOPPED)
1923 return -EBUSY;
1924
1925 mgp->running = MYRI10GE_ETH_STARTING;
1926 status = myri10ge_reset(mgp);
1927 if (status != 0) {
1928 printk(KERN_ERR "myri10ge: %s: failed reset\n", dev->name);
Brice Goglindf30a742006-12-18 11:50:40 +01001929 goto abort_with_nothing;
Brice Goglin0da34b62006-05-23 06:10:15 -04001930 }
1931
Brice Goglindf30a742006-12-18 11:50:40 +01001932 status = myri10ge_request_irq(mgp);
1933 if (status != 0)
1934 goto abort_with_nothing;
1935
Brice Goglin0da34b62006-05-23 06:10:15 -04001936 /* decide what small buffer size to use. For good TCP rx
1937 * performance, it is important to not receive 1514 byte
1938 * frames into jumbo buffers, as it confuses the socket buffer
1939 * accounting code, leading to drops and erratic performance.
1940 */
1941
1942 if (dev->mtu <= ETH_DATA_LEN)
Brice Goglinc7dab992006-12-11 11:25:42 +01001943 /* enough for a TCP header */
1944 mgp->small_bytes = (128 > SMP_CACHE_BYTES)
1945 ? (128 - MXGEFW_PAD)
1946 : (SMP_CACHE_BYTES - MXGEFW_PAD);
Brice Goglin0da34b62006-05-23 06:10:15 -04001947 else
Brice Goglinde3c4502006-12-11 11:26:38 +01001948 /* enough for a vlan encapsulated ETH_DATA_LEN frame */
1949 mgp->small_bytes = VLAN_ETH_FRAME_LEN;
Brice Goglin0da34b62006-05-23 06:10:15 -04001950
1951 /* Override the small buffer size? */
1952 if (myri10ge_small_bytes > 0)
1953 mgp->small_bytes = myri10ge_small_bytes;
1954
Brice Goglin0da34b62006-05-23 06:10:15 -04001955 /* get the lanai pointers to the send and receive rings */
1956
1957 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001958 mgp->ss.tx.lanai =
Brice Goglin0da34b62006-05-23 06:10:15 -04001959 (struct mcp_kreq_ether_send __iomem *)(mgp->sram + cmd.data0);
1960
1961 status |=
1962 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001963 mgp->ss.rx_small.lanai =
Brice Goglin0da34b62006-05-23 06:10:15 -04001964 (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
1965
1966 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001967 mgp->ss.rx_big.lanai =
Brice Goglin0da34b62006-05-23 06:10:15 -04001968 (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
1969
1970 if (status != 0) {
1971 printk(KERN_ERR
1972 "myri10ge: %s: failed to get ring sizes or locations\n",
1973 dev->name);
1974 mgp->running = MYRI10GE_ETH_STOPPED;
Brice Goglindf30a742006-12-18 11:50:40 +01001975 goto abort_with_irq;
Brice Goglin0da34b62006-05-23 06:10:15 -04001976 }
1977
Brice Goglin276e26c2007-03-07 20:02:32 +01001978 if (myri10ge_wcfifo && mgp->wc_enabled) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001979 mgp->ss.tx.wc_fifo = (u8 __iomem *) mgp->sram + MXGEFW_ETH_SEND_4;
1980 mgp->ss.rx_small.wc_fifo =
Brice Gogline700f9f2006-08-14 17:52:54 -04001981 (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_SMALL;
Brice Goglinb53bef82008-05-09 02:20:03 +02001982 mgp->ss.rx_big.wc_fifo =
Brice Gogline700f9f2006-08-14 17:52:54 -04001983 (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_BIG;
Brice Goglin0da34b62006-05-23 06:10:15 -04001984 } else {
Brice Goglinb53bef82008-05-09 02:20:03 +02001985 mgp->ss.tx.wc_fifo = NULL;
1986 mgp->ss.rx_small.wc_fifo = NULL;
1987 mgp->ss.rx_big.wc_fifo = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04001988 }
1989
Brice Goglin0da34b62006-05-23 06:10:15 -04001990 /* Firmware needs the big buff size as a power of 2. Lie and
1991 * tell him the buffer is larger, because we only use 1
1992 * buffer/pkt, and the mtu will prevent overruns.
1993 */
Brice Goglin13348be2006-12-11 11:27:19 +01001994 big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
Brice Goglinc7dab992006-12-11 11:25:42 +01001995 if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
vignesh babu199126a2007-07-09 11:50:22 -07001996 while (!is_power_of_2(big_pow2))
Brice Goglinc7dab992006-12-11 11:25:42 +01001997 big_pow2++;
Brice Goglin13348be2006-12-11 11:27:19 +01001998 mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
Brice Goglinc7dab992006-12-11 11:25:42 +01001999 } else {
2000 big_pow2 = MYRI10GE_ALLOC_SIZE;
2001 mgp->big_bytes = big_pow2;
2002 }
2003
Brice Goglinb53bef82008-05-09 02:20:03 +02002004 status = myri10ge_allocate_rings(&mgp->ss);
Brice Goglinc7dab992006-12-11 11:25:42 +01002005 if (status != 0)
Brice Goglindf30a742006-12-18 11:50:40 +01002006 goto abort_with_irq;
Brice Goglin0da34b62006-05-23 06:10:15 -04002007
2008 /* now give firmware buffers sizes, and MTU */
2009 cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
2010 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
2011 cmd.data0 = mgp->small_bytes;
2012 status |=
2013 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
2014 cmd.data0 = big_pow2;
2015 status |=
2016 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
2017 if (status) {
2018 printk(KERN_ERR "myri10ge: %s: Couldn't set buffer sizes\n",
2019 dev->name);
2020 goto abort_with_rings;
2021 }
2022
Brice Goglinb53bef82008-05-09 02:20:03 +02002023 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->ss.fw_stats_bus);
2024 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->ss.fw_stats_bus);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002025 cmd.data2 = sizeof(struct mcp_irq_data);
2026 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
2027 if (status == -ENOSYS) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002028 dma_addr_t bus = mgp->ss.fw_stats_bus;
Brice Goglin85a7ea12006-08-21 17:36:56 -04002029 bus += offsetof(struct mcp_irq_data, send_done_count);
2030 cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
2031 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
2032 status = myri10ge_send_cmd(mgp,
2033 MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
2034 &cmd, 0);
2035 /* Firmware cannot support multicast without STATS_DMA_V2 */
2036 mgp->fw_multicast_support = 0;
2037 } else {
2038 mgp->fw_multicast_support = 1;
2039 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002040 if (status) {
2041 printk(KERN_ERR "myri10ge: %s: Couldn't set stats DMA\n",
2042 dev->name);
2043 goto abort_with_rings;
2044 }
2045
Al Viro66341ff2007-12-22 18:56:43 +00002046 mgp->link_state = ~0U;
Brice Goglin0da34b62006-05-23 06:10:15 -04002047 mgp->rdma_tags_available = 15;
2048
Brice Goglinb53bef82008-05-09 02:20:03 +02002049 lro_mgr = &mgp->ss.rx_done.lro_mgr;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002050 lro_mgr->dev = dev;
2051 lro_mgr->features = LRO_F_NAPI;
2052 lro_mgr->ip_summed = CHECKSUM_COMPLETE;
2053 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
2054 lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
Brice Goglinb53bef82008-05-09 02:20:03 +02002055 lro_mgr->lro_arr = mgp->ss.rx_done.lro_desc;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002056 lro_mgr->get_frag_header = myri10ge_get_frag_header;
2057 lro_mgr->max_aggr = myri10ge_lro_max_pkts;
Andrew Gallatin621544e2007-12-05 02:31:42 -08002058 lro_mgr->frag_align_pad = 2;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002059 if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
2060 lro_mgr->max_aggr = MAX_SKB_FRAGS;
2061
Brice Goglinb53bef82008-05-09 02:20:03 +02002062 napi_enable(&mgp->ss.napi); /* must happen prior to any irq */
Brice Goglin0da34b62006-05-23 06:10:15 -04002063
2064 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
2065 if (status) {
2066 printk(KERN_ERR "myri10ge: %s: Couldn't bring up link\n",
2067 dev->name);
2068 goto abort_with_rings;
2069 }
2070
Brice Goglinb53bef82008-05-09 02:20:03 +02002071 mgp->ss.tx.wake_queue = 0;
2072 mgp->ss.tx.stop_queue = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04002073 mgp->running = MYRI10GE_ETH_RUNNING;
2074 mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
2075 add_timer(&mgp->watchdog_timer);
2076 netif_wake_queue(dev);
2077 return 0;
2078
2079abort_with_rings:
Brice Goglinb53bef82008-05-09 02:20:03 +02002080 myri10ge_free_rings(&mgp->ss);
Brice Goglin0da34b62006-05-23 06:10:15 -04002081
Brice Goglindf30a742006-12-18 11:50:40 +01002082abort_with_irq:
2083 myri10ge_free_irq(mgp);
2084
Brice Goglin0da34b62006-05-23 06:10:15 -04002085abort_with_nothing:
2086 mgp->running = MYRI10GE_ETH_STOPPED;
2087 return -ENOMEM;
2088}
2089
2090static int myri10ge_close(struct net_device *dev)
2091{
Brice Goglinb53bef82008-05-09 02:20:03 +02002092 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002093 struct myri10ge_cmd cmd;
2094 int status, old_down_cnt;
2095
Brice Goglin0da34b62006-05-23 06:10:15 -04002096 if (mgp->running != MYRI10GE_ETH_RUNNING)
2097 return 0;
2098
Brice Goglinb53bef82008-05-09 02:20:03 +02002099 if (mgp->ss.tx.req_bytes == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04002100 return 0;
2101
2102 del_timer_sync(&mgp->watchdog_timer);
2103 mgp->running = MYRI10GE_ETH_STOPPING;
Brice Goglinb53bef82008-05-09 02:20:03 +02002104 napi_disable(&mgp->ss.napi);
Brice Goglin0da34b62006-05-23 06:10:15 -04002105 netif_carrier_off(dev);
2106 netif_stop_queue(dev);
2107 old_down_cnt = mgp->down_cnt;
2108 mb();
2109 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
2110 if (status)
2111 printk(KERN_ERR "myri10ge: %s: Couldn't bring down link\n",
2112 dev->name);
2113
2114 wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt, HZ);
2115 if (old_down_cnt == mgp->down_cnt)
2116 printk(KERN_ERR "myri10ge: %s never got down irq\n", dev->name);
2117
2118 netif_tx_disable(dev);
Brice Goglindf30a742006-12-18 11:50:40 +01002119 myri10ge_free_irq(mgp);
Brice Goglinb53bef82008-05-09 02:20:03 +02002120 myri10ge_free_rings(&mgp->ss);
Brice Goglin0da34b62006-05-23 06:10:15 -04002121
2122 mgp->running = MYRI10GE_ETH_STOPPED;
2123 return 0;
2124}
2125
2126/* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2127 * backwards one at a time and handle ring wraps */
2128
2129static inline void
2130myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
2131 struct mcp_kreq_ether_send *src, int cnt)
2132{
2133 int idx, starting_slot;
2134 starting_slot = tx->req;
2135 while (cnt > 1) {
2136 cnt--;
2137 idx = (starting_slot + cnt) & tx->mask;
2138 myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
2139 mb();
2140 }
2141}
2142
2143/*
2144 * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2145 * at most 32 bytes at a time, so as to avoid involving the software
2146 * pio handler in the nic. We re-write the first segment's flags
2147 * to mark them valid only after writing the entire chain.
2148 */
2149
2150static inline void
2151myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
2152 int cnt)
2153{
2154 int idx, i;
2155 struct mcp_kreq_ether_send __iomem *dstp, *dst;
2156 struct mcp_kreq_ether_send *srcp;
2157 u8 last_flags;
2158
2159 idx = tx->req & tx->mask;
2160
2161 last_flags = src->flags;
2162 src->flags = 0;
2163 mb();
2164 dst = dstp = &tx->lanai[idx];
2165 srcp = src;
2166
2167 if ((idx + cnt) < tx->mask) {
2168 for (i = 0; i < (cnt - 1); i += 2) {
2169 myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
2170 mb(); /* force write every 32 bytes */
2171 srcp += 2;
2172 dstp += 2;
2173 }
2174 } else {
2175 /* submit all but the first request, and ensure
2176 * that it is submitted below */
2177 myri10ge_submit_req_backwards(tx, src, cnt);
2178 i = 0;
2179 }
2180 if (i < cnt) {
2181 /* submit the first request */
2182 myri10ge_pio_copy(dstp, srcp, sizeof(*src));
2183 mb(); /* barrier before setting valid flag */
2184 }
2185
2186 /* re-write the last 32-bits with the valid flags */
2187 src->flags = last_flags;
Al Viro40f6cff2006-11-20 13:48:32 -05002188 put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
Brice Goglin0da34b62006-05-23 06:10:15 -04002189 tx->req += cnt;
2190 mb();
2191}
2192
2193static inline void
2194myri10ge_submit_req_wc(struct myri10ge_tx_buf *tx,
2195 struct mcp_kreq_ether_send *src, int cnt)
2196{
2197 tx->req += cnt;
2198 mb();
2199 while (cnt >= 4) {
2200 myri10ge_pio_copy(tx->wc_fifo, src, 64);
2201 mb();
2202 src += 4;
2203 cnt -= 4;
2204 }
2205 if (cnt > 0) {
2206 /* pad it to 64 bytes. The src is 64 bytes bigger than it
2207 * needs to be so that we don't overrun it */
Brice Gogline700f9f2006-08-14 17:52:54 -04002208 myri10ge_pio_copy(tx->wc_fifo + MXGEFW_ETH_SEND_OFFSET(cnt),
2209 src, 64);
Brice Goglin0da34b62006-05-23 06:10:15 -04002210 mb();
2211 }
2212}
2213
2214/*
2215 * Transmit a packet. We need to split the packet so that a single
Brice Goglinb53bef82008-05-09 02:20:03 +02002216 * segment does not cross myri10ge->tx_boundary, so this makes segment
Brice Goglin0da34b62006-05-23 06:10:15 -04002217 * counting tricky. So rather than try to count segments up front, we
2218 * just give up if there are too few segments to hold a reasonably
2219 * fragmented packet currently available. If we run
2220 * out of segments while preparing a packet for DMA, we just linearize
2221 * it and try again.
2222 */
2223
2224static int myri10ge_xmit(struct sk_buff *skb, struct net_device *dev)
2225{
2226 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglinb53bef82008-05-09 02:20:03 +02002227 struct myri10ge_slice_state *ss;
Brice Goglin0da34b62006-05-23 06:10:15 -04002228 struct mcp_kreq_ether_send *req;
Brice Goglinb53bef82008-05-09 02:20:03 +02002229 struct myri10ge_tx_buf *tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002230 struct skb_frag_struct *frag;
2231 dma_addr_t bus;
Al Viro40f6cff2006-11-20 13:48:32 -05002232 u32 low;
2233 __be32 high_swapped;
Brice Goglin0da34b62006-05-23 06:10:15 -04002234 unsigned int len;
2235 int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
2236 u16 pseudo_hdr_offset, cksum_offset;
2237 int cum_len, seglen, boundary, rdma_count;
2238 u8 flags, odd_flag;
2239
Brice Goglinb53bef82008-05-09 02:20:03 +02002240 /* always transmit through slot 0 */
2241 ss = &mgp->ss;
2242 tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002243again:
2244 req = tx->req_list;
2245 avail = tx->mask - 1 - (tx->req - tx->done);
2246
2247 mss = 0;
2248 max_segments = MXGEFW_MAX_SEND_DESC;
2249
Brice Goglin917690c2007-03-27 21:54:53 +02002250 if (skb_is_gso(skb)) {
Herbert Xu79671682006-06-22 02:40:14 -07002251 mss = skb_shinfo(skb)->gso_size;
Brice Goglin917690c2007-03-27 21:54:53 +02002252 max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
Brice Goglin0da34b62006-05-23 06:10:15 -04002253 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002254
2255 if ((unlikely(avail < max_segments))) {
2256 /* we are out of transmit resources */
Brice Goglinb53bef82008-05-09 02:20:03 +02002257 tx->stop_queue++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002258 netif_stop_queue(dev);
2259 return 1;
2260 }
2261
2262 /* Setup checksum offloading, if needed */
2263 cksum_offset = 0;
2264 pseudo_hdr_offset = 0;
2265 odd_flag = 0;
2266 flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
Patrick McHardy84fa7932006-08-29 16:44:56 -07002267 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002268 cksum_offset = skb_transport_offset(skb);
Al Viroff1dcad2006-11-20 18:07:29 -08002269 pseudo_hdr_offset = cksum_offset + skb->csum_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -04002270 /* If the headers are excessively large, then we must
2271 * fall back to a software checksum */
Brice Goglin4f93fde2007-10-13 12:34:01 +02002272 if (unlikely(!mss && (cksum_offset > 255 ||
2273 pseudo_hdr_offset > 127))) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07002274 if (skb_checksum_help(skb))
Brice Goglin0da34b62006-05-23 06:10:15 -04002275 goto drop;
2276 cksum_offset = 0;
2277 pseudo_hdr_offset = 0;
2278 } else {
Brice Goglin0da34b62006-05-23 06:10:15 -04002279 odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
2280 flags |= MXGEFW_FLAGS_CKSUM;
2281 }
2282 }
2283
2284 cum_len = 0;
2285
Brice Goglin0da34b62006-05-23 06:10:15 -04002286 if (mss) { /* TSO */
2287 /* this removes any CKSUM flag from before */
2288 flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
2289
2290 /* negative cum_len signifies to the
2291 * send loop that we are still in the
2292 * header portion of the TSO packet.
Brice Goglin4f93fde2007-10-13 12:34:01 +02002293 * TSO header can be at most 1KB long */
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002294 cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
Brice Goglin0da34b62006-05-23 06:10:15 -04002295
Brice Goglin4f93fde2007-10-13 12:34:01 +02002296 /* for IPv6 TSO, the checksum offset stores the
2297 * TCP header length, to save the firmware from
2298 * the need to parse the headers */
2299 if (skb_is_gso_v6(skb)) {
2300 cksum_offset = tcp_hdrlen(skb);
2301 /* Can only handle headers <= max_tso6 long */
2302 if (unlikely(-cum_len > mgp->max_tso6))
2303 return myri10ge_sw_tso(skb, dev);
2304 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002305 /* for TSO, pseudo_hdr_offset holds mss.
2306 * The firmware figures out where to put
2307 * the checksum by parsing the header. */
Al Viro40f6cff2006-11-20 13:48:32 -05002308 pseudo_hdr_offset = mss;
Brice Goglin0da34b62006-05-23 06:10:15 -04002309 } else
Brice Goglin0da34b62006-05-23 06:10:15 -04002310 /* Mark small packets, and pad out tiny packets */
2311 if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
2312 flags |= MXGEFW_FLAGS_SMALL;
2313
2314 /* pad frames to at least ETH_ZLEN bytes */
2315 if (unlikely(skb->len < ETH_ZLEN)) {
Herbert Xu5b057c62006-06-23 02:06:41 -07002316 if (skb_padto(skb, ETH_ZLEN)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002317 /* The packet is gone, so we must
2318 * return 0 */
Brice Goglinb53bef82008-05-09 02:20:03 +02002319 ss->stats.tx_dropped += 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04002320 return 0;
2321 }
2322 /* adjust the len to account for the zero pad
2323 * so that the nic can know how long it is */
2324 skb->len = ETH_ZLEN;
2325 }
2326 }
2327
2328 /* map the skb for DMA */
2329 len = skb->len - skb->data_len;
2330 idx = tx->req & tx->mask;
2331 tx->info[idx].skb = skb;
2332 bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
2333 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2334 pci_unmap_len_set(&tx->info[idx], len, len);
2335
2336 frag_cnt = skb_shinfo(skb)->nr_frags;
2337 frag_idx = 0;
2338 count = 0;
2339 rdma_count = 0;
2340
2341 /* "rdma_count" is the number of RDMAs belonging to the
2342 * current packet BEFORE the current send request. For
2343 * non-TSO packets, this is equal to "count".
2344 * For TSO packets, rdma_count needs to be reset
2345 * to 0 after a segment cut.
2346 *
2347 * The rdma_count field of the send request is
2348 * the number of RDMAs of the packet starting at
2349 * that request. For TSO send requests with one ore more cuts
2350 * in the middle, this is the number of RDMAs starting
2351 * after the last cut in the request. All previous
2352 * segments before the last cut implicitly have 1 RDMA.
2353 *
2354 * Since the number of RDMAs is not known beforehand,
2355 * it must be filled-in retroactively - after each
2356 * segmentation cut or at the end of the entire packet.
2357 */
2358
2359 while (1) {
2360 /* Break the SKB or Fragment up into pieces which
Brice Goglinb53bef82008-05-09 02:20:03 +02002361 * do not cross mgp->tx_boundary */
Brice Goglin0da34b62006-05-23 06:10:15 -04002362 low = MYRI10GE_LOWPART_TO_U32(bus);
2363 high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
2364 while (len) {
2365 u8 flags_next;
2366 int cum_len_next;
2367
2368 if (unlikely(count == max_segments))
2369 goto abort_linearize;
2370
Brice Goglinb53bef82008-05-09 02:20:03 +02002371 boundary =
2372 (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04002373 seglen = boundary - low;
2374 if (seglen > len)
2375 seglen = len;
2376 flags_next = flags & ~MXGEFW_FLAGS_FIRST;
2377 cum_len_next = cum_len + seglen;
Brice Goglin0da34b62006-05-23 06:10:15 -04002378 if (mss) { /* TSO */
2379 (req - rdma_count)->rdma_count = rdma_count + 1;
2380
2381 if (likely(cum_len >= 0)) { /* payload */
2382 int next_is_first, chop;
2383
2384 chop = (cum_len_next > mss);
2385 cum_len_next = cum_len_next % mss;
2386 next_is_first = (cum_len_next == 0);
2387 flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
2388 flags_next |= next_is_first *
2389 MXGEFW_FLAGS_FIRST;
2390 rdma_count |= -(chop | next_is_first);
2391 rdma_count += chop & !next_is_first;
2392 } else if (likely(cum_len_next >= 0)) { /* header ends */
2393 int small;
2394
2395 rdma_count = -1;
2396 cum_len_next = 0;
2397 seglen = -cum_len;
2398 small = (mss <= MXGEFW_SEND_SMALL_SIZE);
2399 flags_next = MXGEFW_FLAGS_TSO_PLD |
2400 MXGEFW_FLAGS_FIRST |
2401 (small * MXGEFW_FLAGS_SMALL);
2402 }
2403 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002404 req->addr_high = high_swapped;
2405 req->addr_low = htonl(low);
Al Viro40f6cff2006-11-20 13:48:32 -05002406 req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
Brice Goglin0da34b62006-05-23 06:10:15 -04002407 req->pad = 0; /* complete solid 16-byte block; does this matter? */
2408 req->rdma_count = 1;
2409 req->length = htons(seglen);
2410 req->cksum_offset = cksum_offset;
2411 req->flags = flags | ((cum_len & 1) * odd_flag);
2412
2413 low += seglen;
2414 len -= seglen;
2415 cum_len = cum_len_next;
2416 flags = flags_next;
2417 req++;
2418 count++;
2419 rdma_count++;
Brice Goglin4f93fde2007-10-13 12:34:01 +02002420 if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
2421 if (unlikely(cksum_offset > seglen))
2422 cksum_offset -= seglen;
2423 else
2424 cksum_offset = 0;
2425 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002426 }
2427 if (frag_idx == frag_cnt)
2428 break;
2429
2430 /* map next fragment for DMA */
2431 idx = (count + tx->req) & tx->mask;
2432 frag = &skb_shinfo(skb)->frags[frag_idx];
2433 frag_idx++;
2434 len = frag->size;
2435 bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
2436 len, PCI_DMA_TODEVICE);
2437 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2438 pci_unmap_len_set(&tx->info[idx], len, len);
2439 }
2440
2441 (req - rdma_count)->rdma_count = rdma_count;
Brice Goglin0da34b62006-05-23 06:10:15 -04002442 if (mss)
2443 do {
2444 req--;
2445 req->flags |= MXGEFW_FLAGS_TSO_LAST;
2446 } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
2447 MXGEFW_FLAGS_FIRST)));
Brice Goglin0da34b62006-05-23 06:10:15 -04002448 idx = ((count - 1) + tx->req) & tx->mask;
2449 tx->info[idx].last = 1;
2450 if (tx->wc_fifo == NULL)
2451 myri10ge_submit_req(tx, tx->req_list, count);
2452 else
2453 myri10ge_submit_req_wc(tx, tx->req_list, count);
2454 tx->pkt_start++;
2455 if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002456 tx->stop_queue++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002457 netif_stop_queue(dev);
2458 }
2459 dev->trans_start = jiffies;
2460 return 0;
2461
2462abort_linearize:
2463 /* Free any DMA resources we've alloced and clear out the skb
2464 * slot so as to not trip up assertions, and to avoid a
2465 * double-free if linearizing fails */
2466
2467 last_idx = (idx + 1) & tx->mask;
2468 idx = tx->req & tx->mask;
2469 tx->info[idx].skb = NULL;
2470 do {
2471 len = pci_unmap_len(&tx->info[idx], len);
2472 if (len) {
2473 if (tx->info[idx].skb != NULL)
2474 pci_unmap_single(mgp->pdev,
2475 pci_unmap_addr(&tx->info[idx],
2476 bus), len,
2477 PCI_DMA_TODEVICE);
2478 else
2479 pci_unmap_page(mgp->pdev,
2480 pci_unmap_addr(&tx->info[idx],
2481 bus), len,
2482 PCI_DMA_TODEVICE);
2483 pci_unmap_len_set(&tx->info[idx], len, 0);
2484 tx->info[idx].skb = NULL;
2485 }
2486 idx = (idx + 1) & tx->mask;
2487 } while (idx != last_idx);
Herbert Xu89114af2006-07-08 13:34:32 -07002488 if (skb_is_gso(skb)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002489 printk(KERN_ERR
2490 "myri10ge: %s: TSO but wanted to linearize?!?!?\n",
2491 mgp->dev->name);
2492 goto drop;
2493 }
2494
Andrew Mortonbec0e852006-06-22 14:47:19 -07002495 if (skb_linearize(skb))
Brice Goglin0da34b62006-05-23 06:10:15 -04002496 goto drop;
2497
Brice Goglinb53bef82008-05-09 02:20:03 +02002498 tx->linearized++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002499 goto again;
2500
2501drop:
2502 dev_kfree_skb_any(skb);
Brice Goglinb53bef82008-05-09 02:20:03 +02002503 ss->stats.tx_dropped += 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04002504 return 0;
2505
2506}
2507
Brice Goglin4f93fde2007-10-13 12:34:01 +02002508static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev)
2509{
2510 struct sk_buff *segs, *curr;
Brice Goglinb53bef82008-05-09 02:20:03 +02002511 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin4f93fde2007-10-13 12:34:01 +02002512 int status;
2513
2514 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07002515 if (IS_ERR(segs))
Brice Goglin4f93fde2007-10-13 12:34:01 +02002516 goto drop;
2517
2518 while (segs) {
2519 curr = segs;
2520 segs = segs->next;
2521 curr->next = NULL;
2522 status = myri10ge_xmit(curr, dev);
2523 if (status != 0) {
2524 dev_kfree_skb_any(curr);
2525 if (segs != NULL) {
2526 curr = segs;
2527 segs = segs->next;
2528 curr->next = NULL;
2529 dev_kfree_skb_any(segs);
2530 }
2531 goto drop;
2532 }
2533 }
2534 dev_kfree_skb_any(skb);
2535 return 0;
2536
2537drop:
2538 dev_kfree_skb_any(skb);
2539 mgp->stats.tx_dropped += 1;
2540 return 0;
2541}
2542
Brice Goglin0da34b62006-05-23 06:10:15 -04002543static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
2544{
2545 struct myri10ge_priv *mgp = netdev_priv(dev);
2546 return &mgp->stats;
2547}
2548
2549static void myri10ge_set_multicast_list(struct net_device *dev)
2550{
Brice Goglinb53bef82008-05-09 02:20:03 +02002551 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002552 struct myri10ge_cmd cmd;
Brice Goglin85a7ea12006-08-21 17:36:56 -04002553 struct dev_mc_list *mc_list;
Brice Goglin62502232006-12-11 11:24:37 +01002554 __be32 data[2] = { 0, 0 };
Brice Goglin85a7ea12006-08-21 17:36:56 -04002555 int err;
Joe Perches0795af52007-10-03 17:59:30 -07002556 DECLARE_MAC_BUF(mac);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002557
Brice Goglin0da34b62006-05-23 06:10:15 -04002558 /* can be called from atomic contexts,
2559 * pass 1 to force atomicity in myri10ge_send_cmd() */
Brice Goglin85a7ea12006-08-21 17:36:56 -04002560 myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
2561
2562 /* This firmware is known to not support multicast */
Brice Goglin2f762162007-05-07 23:50:37 +02002563 if (!mgp->fw_multicast_support)
Brice Goglin85a7ea12006-08-21 17:36:56 -04002564 return;
2565
2566 /* Disable multicast filtering */
2567
2568 err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
2569 if (err != 0) {
2570 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_ENABLE_ALLMULTI,"
2571 " error status: %d\n", dev->name, err);
2572 goto abort;
2573 }
2574
Brice Goglin2f762162007-05-07 23:50:37 +02002575 if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
Brice Goglin85a7ea12006-08-21 17:36:56 -04002576 /* request to disable multicast filtering, so quit here */
2577 return;
2578 }
2579
2580 /* Flush the filters */
2581
2582 err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
2583 &cmd, 1);
2584 if (err != 0) {
2585 printk(KERN_ERR
2586 "myri10ge: %s: Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS"
2587 ", error status: %d\n", dev->name, err);
2588 goto abort;
2589 }
2590
2591 /* Walk the multicast list, and add each address */
2592 for (mc_list = dev->mc_list; mc_list != NULL; mc_list = mc_list->next) {
Al Viro40f6cff2006-11-20 13:48:32 -05002593 memcpy(data, &mc_list->dmi_addr, 6);
2594 cmd.data0 = ntohl(data[0]);
2595 cmd.data1 = ntohl(data[1]);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002596 err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
2597 &cmd, 1);
2598
2599 if (err != 0) {
2600 printk(KERN_ERR "myri10ge: %s: Failed "
2601 "MXGEFW_JOIN_MULTICAST_GROUP, error status:"
2602 "%d\t", dev->name, err);
Joe Perches0795af52007-10-03 17:59:30 -07002603 printk(KERN_ERR "MAC %s\n",
2604 print_mac(mac, mc_list->dmi_addr));
Brice Goglin85a7ea12006-08-21 17:36:56 -04002605 goto abort;
2606 }
2607 }
2608 /* Enable multicast filtering */
2609 err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
2610 if (err != 0) {
2611 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_DISABLE_ALLMULTI,"
2612 "error status: %d\n", dev->name, err);
2613 goto abort;
2614 }
2615
2616 return;
2617
2618abort:
2619 return;
Brice Goglin0da34b62006-05-23 06:10:15 -04002620}
2621
2622static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
2623{
2624 struct sockaddr *sa = addr;
2625 struct myri10ge_priv *mgp = netdev_priv(dev);
2626 int status;
2627
2628 if (!is_valid_ether_addr(sa->sa_data))
2629 return -EADDRNOTAVAIL;
2630
2631 status = myri10ge_update_mac_address(mgp, sa->sa_data);
2632 if (status != 0) {
2633 printk(KERN_ERR
2634 "myri10ge: %s: changing mac address failed with %d\n",
2635 dev->name, status);
2636 return status;
2637 }
2638
2639 /* change the dev structure */
2640 memcpy(dev->dev_addr, sa->sa_data, 6);
2641 return 0;
2642}
2643
2644static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
2645{
2646 struct myri10ge_priv *mgp = netdev_priv(dev);
2647 int error = 0;
2648
2649 if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
2650 printk(KERN_ERR "myri10ge: %s: new mtu (%d) is not valid\n",
2651 dev->name, new_mtu);
2652 return -EINVAL;
2653 }
2654 printk(KERN_INFO "%s: changing mtu from %d to %d\n",
2655 dev->name, dev->mtu, new_mtu);
2656 if (mgp->running) {
2657 /* if we change the mtu on an active device, we must
2658 * reset the device so the firmware sees the change */
2659 myri10ge_close(dev);
2660 dev->mtu = new_mtu;
2661 myri10ge_open(dev);
2662 } else
2663 dev->mtu = new_mtu;
2664
2665 return error;
2666}
2667
2668/*
2669 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
2670 * Only do it if the bridge is a root port since we don't want to disturb
2671 * any other device, except if forced with myri10ge_ecrc_enable > 1.
2672 */
2673
Brice Goglin0da34b62006-05-23 06:10:15 -04002674static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
2675{
2676 struct pci_dev *bridge = mgp->pdev->bus->self;
2677 struct device *dev = &mgp->pdev->dev;
2678 unsigned cap;
2679 unsigned err_cap;
2680 u16 val;
2681 u8 ext_type;
2682 int ret;
2683
2684 if (!myri10ge_ecrc_enable || !bridge)
2685 return;
2686
2687 /* check that the bridge is a root port */
2688 cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
2689 pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
2690 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
2691 if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
2692 if (myri10ge_ecrc_enable > 1) {
Brice Goglineca3fd82008-05-09 02:19:29 +02002693 struct pci_dev *prev_bridge, *old_bridge = bridge;
Brice Goglin0da34b62006-05-23 06:10:15 -04002694
2695 /* Walk the hierarchy up to the root port
2696 * where ECRC has to be enabled */
2697 do {
Brice Goglineca3fd82008-05-09 02:19:29 +02002698 prev_bridge = bridge;
Brice Goglin0da34b62006-05-23 06:10:15 -04002699 bridge = bridge->bus->self;
Brice Goglineca3fd82008-05-09 02:19:29 +02002700 if (!bridge || prev_bridge == bridge) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002701 dev_err(dev,
2702 "Failed to find root port"
2703 " to force ECRC\n");
2704 return;
2705 }
2706 cap =
2707 pci_find_capability(bridge, PCI_CAP_ID_EXP);
2708 pci_read_config_word(bridge,
2709 cap + PCI_CAP_FLAGS, &val);
2710 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
2711 } while (ext_type != PCI_EXP_TYPE_ROOT_PORT);
2712
2713 dev_info(dev,
2714 "Forcing ECRC on non-root port %s"
2715 " (enabling on root port %s)\n",
2716 pci_name(old_bridge), pci_name(bridge));
2717 } else {
2718 dev_err(dev,
2719 "Not enabling ECRC on non-root port %s\n",
2720 pci_name(bridge));
2721 return;
2722 }
2723 }
2724
2725 cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
Brice Goglin0da34b62006-05-23 06:10:15 -04002726 if (!cap)
2727 return;
2728
2729 ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
2730 if (ret) {
2731 dev_err(dev, "failed reading ext-conf-space of %s\n",
2732 pci_name(bridge));
2733 dev_err(dev, "\t pci=nommconf in use? "
2734 "or buggy/incomplete/absent ACPI MCFG attr?\n");
2735 return;
2736 }
2737 if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
2738 return;
2739
2740 err_cap |= PCI_ERR_CAP_ECRC_GENE;
2741 pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
2742 dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
Brice Goglin0da34b62006-05-23 06:10:15 -04002743}
2744
2745/*
2746 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
2747 * when the PCI-E Completion packets are aligned on an 8-byte
2748 * boundary. Some PCI-E chip sets always align Completion packets; on
2749 * the ones that do not, the alignment can be enforced by enabling
2750 * ECRC generation (if supported).
2751 *
2752 * When PCI-E Completion packets are not aligned, it is actually more
2753 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
2754 *
2755 * If the driver can neither enable ECRC nor verify that it has
2756 * already been enabled, then it must use a firmware image which works
2757 * around unaligned completion packets (myri10ge_ethp_z8e.dat), and it
2758 * should also ensure that it never gives the device a Read-DMA which is
Brice Goglinb53bef82008-05-09 02:20:03 +02002759 * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
Brice Goglin0da34b62006-05-23 06:10:15 -04002760 * enabled, then the driver should use the aligned (myri10ge_eth_z8e.dat)
Brice Goglinb53bef82008-05-09 02:20:03 +02002761 * firmware image, and set tx_boundary to 4KB.
Brice Goglin0da34b62006-05-23 06:10:15 -04002762 */
2763
Brice Goglin5443e9e2007-05-07 23:52:22 +02002764static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
Brice Goglin0da34b62006-05-23 06:10:15 -04002765{
Brice Goglin5443e9e2007-05-07 23:52:22 +02002766 struct pci_dev *pdev = mgp->pdev;
2767 struct device *dev = &pdev->dev;
Brice Goglin302d2422007-08-24 08:57:17 +02002768 int status;
Brice Goglin0da34b62006-05-23 06:10:15 -04002769
Brice Goglinb53bef82008-05-09 02:20:03 +02002770 mgp->tx_boundary = 4096;
Brice Goglin5443e9e2007-05-07 23:52:22 +02002771 /*
2772 * Verify the max read request size was set to 4KB
2773 * before trying the test with 4KB.
2774 */
Brice Goglin302d2422007-08-24 08:57:17 +02002775 status = pcie_get_readrq(pdev);
2776 if (status < 0) {
Brice Goglin5443e9e2007-05-07 23:52:22 +02002777 dev_err(dev, "Couldn't read max read req size: %d\n", status);
2778 goto abort;
2779 }
Brice Goglin302d2422007-08-24 08:57:17 +02002780 if (status != 4096) {
2781 dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
Brice Goglinb53bef82008-05-09 02:20:03 +02002782 mgp->tx_boundary = 2048;
Brice Goglin5443e9e2007-05-07 23:52:22 +02002783 }
2784 /*
2785 * load the optimized firmware (which assumes aligned PCIe
2786 * completions) in order to see if it works on this host.
2787 */
2788 mgp->fw_name = myri10ge_fw_aligned;
2789 status = myri10ge_load_firmware(mgp);
2790 if (status != 0) {
2791 goto abort;
2792 }
2793
2794 /*
2795 * Enable ECRC if possible
2796 */
2797 myri10ge_enable_ecrc(mgp);
2798
2799 /*
2800 * Run a DMA test which watches for unaligned completions and
2801 * aborts on the first one seen.
2802 */
2803
2804 status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
2805 if (status == 0)
2806 return; /* keep the aligned firmware */
2807
2808 if (status != -E2BIG)
2809 dev_warn(dev, "DMA test failed: %d\n", status);
2810 if (status == -ENOSYS)
2811 dev_warn(dev, "Falling back to ethp! "
2812 "Please install up to date fw\n");
2813abort:
2814 /* fall back to using the unaligned firmware */
Brice Goglinb53bef82008-05-09 02:20:03 +02002815 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -04002816 mgp->fw_name = myri10ge_fw_unaligned;
2817
Brice Goglin5443e9e2007-05-07 23:52:22 +02002818}
2819
2820static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
2821{
Brice Goglin0da34b62006-05-23 06:10:15 -04002822 if (myri10ge_force_firmware == 0) {
Brice Goglince7f9362006-08-31 01:32:59 -04002823 int link_width, exp_cap;
2824 u16 lnk;
2825
2826 exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
2827 pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
2828 link_width = (lnk >> 4) & 0x3f;
2829
Brice Goglince7f9362006-08-31 01:32:59 -04002830 /* Check to see if Link is less than 8 or if the
2831 * upstream bridge is known to provide aligned
2832 * completions */
2833 if (link_width < 8) {
2834 dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
2835 link_width);
Brice Goglinb53bef82008-05-09 02:20:03 +02002836 mgp->tx_boundary = 4096;
Brice Goglince7f9362006-08-31 01:32:59 -04002837 mgp->fw_name = myri10ge_fw_aligned;
Brice Goglin5443e9e2007-05-07 23:52:22 +02002838 } else {
2839 myri10ge_firmware_probe(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -04002840 }
2841 } else {
2842 if (myri10ge_force_firmware == 1) {
2843 dev_info(&mgp->pdev->dev,
2844 "Assuming aligned completions (forced)\n");
Brice Goglinb53bef82008-05-09 02:20:03 +02002845 mgp->tx_boundary = 4096;
Brice Goglin0da34b62006-05-23 06:10:15 -04002846 mgp->fw_name = myri10ge_fw_aligned;
2847 } else {
2848 dev_info(&mgp->pdev->dev,
2849 "Assuming unaligned completions (forced)\n");
Brice Goglinb53bef82008-05-09 02:20:03 +02002850 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -04002851 mgp->fw_name = myri10ge_fw_unaligned;
2852 }
2853 }
2854 if (myri10ge_fw_name != NULL) {
2855 dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
2856 myri10ge_fw_name);
2857 mgp->fw_name = myri10ge_fw_name;
2858 }
2859}
2860
Brice Goglin0da34b62006-05-23 06:10:15 -04002861#ifdef CONFIG_PM
Brice Goglin0da34b62006-05-23 06:10:15 -04002862static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
2863{
2864 struct myri10ge_priv *mgp;
2865 struct net_device *netdev;
2866
2867 mgp = pci_get_drvdata(pdev);
2868 if (mgp == NULL)
2869 return -EINVAL;
2870 netdev = mgp->dev;
2871
2872 netif_device_detach(netdev);
2873 if (netif_running(netdev)) {
2874 printk(KERN_INFO "myri10ge: closing %s\n", netdev->name);
2875 rtnl_lock();
2876 myri10ge_close(netdev);
2877 rtnl_unlock();
2878 }
2879 myri10ge_dummy_rdma(mgp, 0);
Brice Goglin83f6e152006-12-18 11:52:02 +01002880 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002881 pci_disable_device(pdev);
Brice Goglin1a63e842006-12-18 11:52:34 +01002882
2883 return pci_set_power_state(pdev, pci_choose_state(pdev, state));
Brice Goglin0da34b62006-05-23 06:10:15 -04002884}
2885
2886static int myri10ge_resume(struct pci_dev *pdev)
2887{
2888 struct myri10ge_priv *mgp;
2889 struct net_device *netdev;
2890 int status;
2891 u16 vendor;
2892
2893 mgp = pci_get_drvdata(pdev);
2894 if (mgp == NULL)
2895 return -EINVAL;
2896 netdev = mgp->dev;
2897 pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
2898 msleep(5); /* give card time to respond */
2899 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
2900 if (vendor == 0xffff) {
2901 printk(KERN_ERR "myri10ge: %s: device disappeared!\n",
2902 mgp->dev->name);
2903 return -EIO;
2904 }
Brice Goglin83f6e152006-12-18 11:52:02 +01002905
Brice Goglin1a63e842006-12-18 11:52:34 +01002906 status = pci_restore_state(pdev);
2907 if (status)
2908 return status;
Brice Goglin4c2248c2006-07-09 21:10:18 -04002909
2910 status = pci_enable_device(pdev);
Brice Goglin1a63e842006-12-18 11:52:34 +01002911 if (status) {
Brice Goglin4c2248c2006-07-09 21:10:18 -04002912 dev_err(&pdev->dev, "failed to enable device\n");
Brice Goglin1a63e842006-12-18 11:52:34 +01002913 return status;
Brice Goglin4c2248c2006-07-09 21:10:18 -04002914 }
2915
Brice Goglin0da34b62006-05-23 06:10:15 -04002916 pci_set_master(pdev);
2917
Brice Goglin0da34b62006-05-23 06:10:15 -04002918 myri10ge_reset(mgp);
Brice Goglin013b68b2006-08-09 00:07:53 -04002919 myri10ge_dummy_rdma(mgp, 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04002920
2921 /* Save configuration space to be restored if the
2922 * nic resets due to a parity error */
Brice Goglin83f6e152006-12-18 11:52:02 +01002923 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002924
2925 if (netif_running(netdev)) {
2926 rtnl_lock();
Brice Goglindf30a742006-12-18 11:50:40 +01002927 status = myri10ge_open(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002928 rtnl_unlock();
Brice Goglindf30a742006-12-18 11:50:40 +01002929 if (status != 0)
2930 goto abort_with_enabled;
2931
Brice Goglin0da34b62006-05-23 06:10:15 -04002932 }
2933 netif_device_attach(netdev);
2934
2935 return 0;
2936
Brice Goglin4c2248c2006-07-09 21:10:18 -04002937abort_with_enabled:
2938 pci_disable_device(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002939 return -EIO;
2940
2941}
Brice Goglin0da34b62006-05-23 06:10:15 -04002942#endif /* CONFIG_PM */
2943
2944static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
2945{
2946 struct pci_dev *pdev = mgp->pdev;
2947 int vs = mgp->vendor_specific_offset;
2948 u32 reboot;
2949
2950 /*enter read32 mode */
2951 pci_write_config_byte(pdev, vs + 0x10, 0x3);
2952
2953 /*read REBOOT_STATUS (0xfffffff0) */
2954 pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
2955 pci_read_config_dword(pdev, vs + 0x14, &reboot);
2956 return reboot;
2957}
2958
2959/*
2960 * This watchdog is used to check whether the board has suffered
2961 * from a parity error and needs to be recovered.
2962 */
David Howellsc4028952006-11-22 14:57:56 +00002963static void myri10ge_watchdog(struct work_struct *work)
Brice Goglin0da34b62006-05-23 06:10:15 -04002964{
David Howellsc4028952006-11-22 14:57:56 +00002965 struct myri10ge_priv *mgp =
Brice Goglin62502232006-12-11 11:24:37 +01002966 container_of(work, struct myri10ge_priv, watchdog_work);
Brice Goglinb53bef82008-05-09 02:20:03 +02002967 struct myri10ge_tx_buf *tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002968 u32 reboot;
2969 int status;
2970 u16 cmd, vendor;
2971
2972 mgp->watchdog_resets++;
2973 pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
2974 if ((cmd & PCI_COMMAND_MASTER) == 0) {
2975 /* Bus master DMA disabled? Check to see
2976 * if the card rebooted due to a parity error
2977 * For now, just report it */
2978 reboot = myri10ge_read_reboot(mgp);
2979 printk(KERN_ERR
Brice Goglinf1811372007-06-11 20:26:31 +02002980 "myri10ge: %s: NIC rebooted (0x%x),%s resetting\n",
2981 mgp->dev->name, reboot,
2982 myri10ge_reset_recover ? " " : " not");
2983 if (myri10ge_reset_recover == 0)
2984 return;
2985
2986 myri10ge_reset_recover--;
2987
Brice Goglin0da34b62006-05-23 06:10:15 -04002988 /*
2989 * A rebooted nic will come back with config space as
2990 * it was after power was applied to PCIe bus.
2991 * Attempt to restore config space which was saved
2992 * when the driver was loaded, or the last time the
2993 * nic was resumed from power saving mode.
2994 */
Brice Goglin83f6e152006-12-18 11:52:02 +01002995 pci_restore_state(mgp->pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01002996
2997 /* save state again for accounting reasons */
Brice Goglin83f6e152006-12-18 11:52:02 +01002998 pci_save_state(mgp->pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01002999
Brice Goglin0da34b62006-05-23 06:10:15 -04003000 } else {
3001 /* if we get back -1's from our slot, perhaps somebody
3002 * powered off our card. Don't try to reset it in
3003 * this case */
3004 if (cmd == 0xffff) {
3005 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3006 if (vendor == 0xffff) {
3007 printk(KERN_ERR
3008 "myri10ge: %s: device disappeared!\n",
3009 mgp->dev->name);
3010 return;
3011 }
3012 }
3013 /* Perhaps it is a software error. Try to reset */
3014
3015 printk(KERN_ERR "myri10ge: %s: device timeout, resetting\n",
3016 mgp->dev->name);
Brice Goglinb53bef82008-05-09 02:20:03 +02003017 tx = &mgp->ss.tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04003018 printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02003019 mgp->dev->name, tx->req, tx->done,
3020 tx->pkt_start, tx->pkt_done,
3021 (int)ntohl(mgp->ss.fw_stats->send_done_count));
Brice Goglin0da34b62006-05-23 06:10:15 -04003022 msleep(2000);
3023 printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02003024 mgp->dev->name, tx->req, tx->done,
3025 tx->pkt_start, tx->pkt_done,
3026 (int)ntohl(mgp->ss.fw_stats->send_done_count));
Brice Goglin0da34b62006-05-23 06:10:15 -04003027 }
3028 rtnl_lock();
3029 myri10ge_close(mgp->dev);
3030 status = myri10ge_load_firmware(mgp);
3031 if (status != 0)
3032 printk(KERN_ERR "myri10ge: %s: failed to load firmware\n",
3033 mgp->dev->name);
3034 else
3035 myri10ge_open(mgp->dev);
3036 rtnl_unlock();
3037}
3038
3039/*
3040 * We use our own timer routine rather than relying upon
3041 * netdev->tx_timeout because we have a very large hardware transmit
3042 * queue. Due to the large queue, the netdev->tx_timeout function
3043 * cannot detect a NIC with a parity error in a timely fashion if the
3044 * NIC is lightly loaded.
3045 */
3046static void myri10ge_watchdog_timer(unsigned long arg)
3047{
3048 struct myri10ge_priv *mgp;
Brice Goglinb53bef82008-05-09 02:20:03 +02003049 struct myri10ge_slice_state *ss;
Brice Goglin626fda92007-08-09 09:02:14 +02003050 u32 rx_pause_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -04003051
3052 mgp = (struct myri10ge_priv *)arg;
Brice Goglinc7dab992006-12-11 11:25:42 +01003053
Brice Goglinb53bef82008-05-09 02:20:03 +02003054 rx_pause_cnt = ntohl(mgp->ss.fw_stats->dropped_pause);
Brice Goglinc7dab992006-12-11 11:25:42 +01003055
Brice Goglinb53bef82008-05-09 02:20:03 +02003056 ss = &mgp->ss;
3057 if (ss->rx_small.watchdog_needed) {
3058 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
3059 mgp->small_bytes + MXGEFW_PAD, 1);
3060 if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
3061 myri10ge_fill_thresh)
3062 ss->rx_small.watchdog_needed = 0;
3063 }
3064 if (ss->rx_big.watchdog_needed) {
3065 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 1);
3066 if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
3067 myri10ge_fill_thresh)
3068 ss->rx_big.watchdog_needed = 0;
3069 }
3070
3071 if (ss->tx.req != ss->tx.done &&
3072 ss->tx.done == ss->watchdog_tx_done &&
3073 ss->watchdog_tx_req != ss->watchdog_tx_done) {
Brice Goglin0da34b62006-05-23 06:10:15 -04003074 /* nic seems like it might be stuck.. */
Brice Goglin626fda92007-08-09 09:02:14 +02003075 if (rx_pause_cnt != mgp->watchdog_pause) {
3076 if (net_ratelimit())
3077 printk(KERN_WARNING "myri10ge %s:"
3078 "TX paused, check link partner\n",
3079 mgp->dev->name);
3080 } else {
3081 schedule_work(&mgp->watchdog_work);
3082 return;
3083 }
3084 }
3085 /* rearm timer */
3086 mod_timer(&mgp->watchdog_timer,
3087 jiffies + myri10ge_watchdog_timeout * HZ);
Brice Goglinb53bef82008-05-09 02:20:03 +02003088 ss->watchdog_tx_done = ss->tx.done;
3089 ss->watchdog_tx_req = ss->tx.req;
Brice Goglin626fda92007-08-09 09:02:14 +02003090 mgp->watchdog_pause = rx_pause_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -04003091}
3092
3093static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3094{
3095 struct net_device *netdev;
3096 struct myri10ge_priv *mgp;
3097 struct device *dev = &pdev->dev;
3098 size_t bytes;
3099 int i;
3100 int status = -ENXIO;
Brice Goglin0da34b62006-05-23 06:10:15 -04003101 int dac_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04003102
3103 netdev = alloc_etherdev(sizeof(*mgp));
3104 if (netdev == NULL) {
3105 dev_err(dev, "Could not allocate ethernet device\n");
3106 return -ENOMEM;
3107 }
3108
Maik Hampelb245fb62007-06-28 17:07:26 +02003109 SET_NETDEV_DEV(netdev, &pdev->dev);
3110
Brice Goglin0da34b62006-05-23 06:10:15 -04003111 mgp = netdev_priv(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003112 mgp->dev = netdev;
Brice Goglinb53bef82008-05-09 02:20:03 +02003113 netif_napi_add(netdev, &mgp->ss.napi, myri10ge_poll, myri10ge_napi_weight);
Brice Goglin0da34b62006-05-23 06:10:15 -04003114 mgp->pdev = pdev;
3115 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
3116 mgp->pause = myri10ge_flow_control;
3117 mgp->intr_coal_delay = myri10ge_intr_coal_delay;
Brice Goglinc58ac5c2006-08-21 17:36:49 -04003118 mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
Brice Goglin0da34b62006-05-23 06:10:15 -04003119 init_waitqueue_head(&mgp->down_wq);
3120
3121 if (pci_enable_device(pdev)) {
3122 dev_err(&pdev->dev, "pci_enable_device call failed\n");
3123 status = -ENODEV;
3124 goto abort_with_netdev;
3125 }
Brice Goglin0da34b62006-05-23 06:10:15 -04003126
3127 /* Find the vendor-specific cap so we can check
3128 * the reboot register later on */
3129 mgp->vendor_specific_offset
3130 = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
3131
3132 /* Set our max read request to 4KB */
Brice Goglin302d2422007-08-24 08:57:17 +02003133 status = pcie_set_readrq(pdev, 4096);
Brice Goglin0da34b62006-05-23 06:10:15 -04003134 if (status != 0) {
3135 dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
3136 status);
3137 goto abort_with_netdev;
3138 }
3139
3140 pci_set_master(pdev);
3141 dac_enabled = 1;
3142 status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
3143 if (status != 0) {
3144 dac_enabled = 0;
3145 dev_err(&pdev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07003146 "64-bit pci address mask was refused, "
3147 "trying 32-bit\n");
Brice Goglin0da34b62006-05-23 06:10:15 -04003148 status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3149 }
3150 if (status != 0) {
3151 dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
3152 goto abort_with_netdev;
3153 }
Brice Goglinb10c0662006-06-08 10:25:00 -04003154 mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
3155 &mgp->cmd_bus, GFP_KERNEL);
Brice Goglin0da34b62006-05-23 06:10:15 -04003156 if (mgp->cmd == NULL)
3157 goto abort_with_netdev;
3158
Brice Goglinb53bef82008-05-09 02:20:03 +02003159 mgp->ss.fw_stats = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
3160 &mgp->ss.fw_stats_bus, GFP_KERNEL);
3161 if (mgp->ss.fw_stats == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04003162 goto abort_with_cmd;
3163
3164 mgp->board_span = pci_resource_len(pdev, 0);
3165 mgp->iomem_base = pci_resource_start(pdev, 0);
3166 mgp->mtrr = -1;
Brice Goglin276e26c2007-03-07 20:02:32 +01003167 mgp->wc_enabled = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04003168#ifdef CONFIG_MTRR
3169 mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
3170 MTRR_TYPE_WRCOMB, 1);
Brice Goglin276e26c2007-03-07 20:02:32 +01003171 if (mgp->mtrr >= 0)
3172 mgp->wc_enabled = 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04003173#endif
3174 /* Hack. need to get rid of these magic numbers */
3175 mgp->sram_size =
3176 2 * 1024 * 1024 - (2 * (48 * 1024) + (32 * 1024)) - 0x100;
3177 if (mgp->sram_size > mgp->board_span) {
3178 dev_err(&pdev->dev, "board span %ld bytes too small\n",
3179 mgp->board_span);
3180 goto abort_with_wc;
3181 }
3182 mgp->sram = ioremap(mgp->iomem_base, mgp->board_span);
3183 if (mgp->sram == NULL) {
3184 dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
3185 mgp->board_span, mgp->iomem_base);
3186 status = -ENXIO;
3187 goto abort_with_wc;
3188 }
3189 memcpy_fromio(mgp->eeprom_strings,
3190 mgp->sram + mgp->sram_size - MYRI10GE_EEPROM_STRINGS_SIZE,
3191 MYRI10GE_EEPROM_STRINGS_SIZE);
3192 memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
3193 status = myri10ge_read_mac_addr(mgp);
3194 if (status)
3195 goto abort_with_ioremap;
3196
3197 for (i = 0; i < ETH_ALEN; i++)
3198 netdev->dev_addr[i] = mgp->mac_addr[i];
3199
3200 /* allocate rx done ring */
Brice Goglinb53bef82008-05-09 02:20:03 +02003201 bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
3202 mgp->ss.rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
3203 &mgp->ss.rx_done.bus, GFP_KERNEL);
3204 if (mgp->ss.rx_done.entry == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04003205 goto abort_with_ioremap;
Brice Goglinb53bef82008-05-09 02:20:03 +02003206 memset(mgp->ss.rx_done.entry, 0, bytes);
Brice Goglin0da34b62006-05-23 06:10:15 -04003207
Brice Goglin5443e9e2007-05-07 23:52:22 +02003208 myri10ge_select_firmware(mgp);
3209
Brice Goglin0da34b62006-05-23 06:10:15 -04003210 status = myri10ge_load_firmware(mgp);
3211 if (status != 0) {
3212 dev_err(&pdev->dev, "failed to load firmware\n");
3213 goto abort_with_rx_done;
3214 }
3215
3216 status = myri10ge_reset(mgp);
3217 if (status != 0) {
3218 dev_err(&pdev->dev, "failed reset\n");
3219 goto abort_with_firmware;
3220 }
3221
Brice Goglin0da34b62006-05-23 06:10:15 -04003222 pci_set_drvdata(pdev, mgp);
3223 if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
3224 myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
3225 if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
3226 myri10ge_initial_mtu = 68;
3227 netdev->mtu = myri10ge_initial_mtu;
3228 netdev->open = myri10ge_open;
3229 netdev->stop = myri10ge_close;
3230 netdev->hard_start_xmit = myri10ge_xmit;
3231 netdev->get_stats = myri10ge_get_stats;
3232 netdev->base_addr = mgp->iomem_base;
Brice Goglin0da34b62006-05-23 06:10:15 -04003233 netdev->change_mtu = myri10ge_change_mtu;
3234 netdev->set_multicast_list = myri10ge_set_multicast_list;
3235 netdev->set_mac_address = myri10ge_set_mac_address;
Brice Goglin4f93fde2007-10-13 12:34:01 +02003236 netdev->features = mgp->features;
Brice Goglin0da34b62006-05-23 06:10:15 -04003237 if (dac_enabled)
3238 netdev->features |= NETIF_F_HIGHDMA;
Brice Goglin0da34b62006-05-23 06:10:15 -04003239
Brice Goglin21d05db2007-01-09 21:05:04 +01003240 /* make sure we can get an irq, and that MSI can be
3241 * setup (if available). Also ensure netdev->irq
3242 * is set to correct value if MSI is enabled */
3243 status = myri10ge_request_irq(mgp);
3244 if (status != 0)
3245 goto abort_with_firmware;
3246 netdev->irq = pdev->irq;
3247 myri10ge_free_irq(mgp);
3248
Brice Goglin0da34b62006-05-23 06:10:15 -04003249 /* Save configuration space to be restored if the
3250 * nic resets due to a parity error */
Brice Goglin83f6e152006-12-18 11:52:02 +01003251 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003252
3253 /* Setup the watchdog timer */
3254 setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
3255 (unsigned long)mgp);
3256
3257 SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
David Howellsc4028952006-11-22 14:57:56 +00003258 INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
Brice Goglin0da34b62006-05-23 06:10:15 -04003259 status = register_netdev(netdev);
3260 if (status != 0) {
3261 dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
Brice Goglin7adda302006-12-18 11:50:00 +01003262 goto abort_with_state;
Brice Goglin0da34b62006-05-23 06:10:15 -04003263 }
Brice Goglin21d05db2007-01-09 21:05:04 +01003264 dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
3265 (mgp->msi_enabled ? "MSI" : "xPIC"),
Brice Goglinb53bef82008-05-09 02:20:03 +02003266 netdev->irq, mgp->tx_boundary, mgp->fw_name,
Brice Goglin276e26c2007-03-07 20:02:32 +01003267 (mgp->wc_enabled ? "Enabled" : "Disabled"));
Brice Goglin0da34b62006-05-23 06:10:15 -04003268
3269 return 0;
3270
Brice Goglin7adda302006-12-18 11:50:00 +01003271abort_with_state:
Brice Goglin83f6e152006-12-18 11:52:02 +01003272 pci_restore_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003273
3274abort_with_firmware:
3275 myri10ge_dummy_rdma(mgp, 0);
3276
3277abort_with_rx_done:
Brice Goglinb53bef82008-05-09 02:20:03 +02003278 bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
Brice Goglinb10c0662006-06-08 10:25:00 -04003279 dma_free_coherent(&pdev->dev, bytes,
Brice Goglinb53bef82008-05-09 02:20:03 +02003280 mgp->ss.rx_done.entry, mgp->ss.rx_done.bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003281
3282abort_with_ioremap:
3283 iounmap(mgp->sram);
3284
3285abort_with_wc:
3286#ifdef CONFIG_MTRR
3287 if (mgp->mtrr >= 0)
3288 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3289#endif
Brice Goglinb53bef82008-05-09 02:20:03 +02003290 dma_free_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
3291 mgp->ss.fw_stats, mgp->ss.fw_stats_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003292
3293abort_with_cmd:
Brice Goglinb10c0662006-06-08 10:25:00 -04003294 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3295 mgp->cmd, mgp->cmd_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003296
3297abort_with_netdev:
3298
3299 free_netdev(netdev);
3300 return status;
3301}
3302
3303/*
3304 * myri10ge_remove
3305 *
3306 * Does what is necessary to shutdown one Myrinet device. Called
3307 * once for each Myrinet card by the kernel when a module is
3308 * unloaded.
3309 */
3310static void myri10ge_remove(struct pci_dev *pdev)
3311{
3312 struct myri10ge_priv *mgp;
3313 struct net_device *netdev;
3314 size_t bytes;
3315
3316 mgp = pci_get_drvdata(pdev);
3317 if (mgp == NULL)
3318 return;
3319
3320 flush_scheduled_work();
3321 netdev = mgp->dev;
3322 unregister_netdev(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003323
3324 myri10ge_dummy_rdma(mgp, 0);
3325
Brice Goglin7adda302006-12-18 11:50:00 +01003326 /* avoid a memory leak */
Brice Goglin83f6e152006-12-18 11:52:02 +01003327 pci_restore_state(pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003328
Brice Goglinb53bef82008-05-09 02:20:03 +02003329 bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
Brice Goglinb10c0662006-06-08 10:25:00 -04003330 dma_free_coherent(&pdev->dev, bytes,
Brice Goglinb53bef82008-05-09 02:20:03 +02003331 mgp->ss.rx_done.entry, mgp->ss.rx_done.bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003332
3333 iounmap(mgp->sram);
3334
3335#ifdef CONFIG_MTRR
3336 if (mgp->mtrr >= 0)
3337 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3338#endif
Brice Goglinb53bef82008-05-09 02:20:03 +02003339 dma_free_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
3340 mgp->ss.fw_stats, mgp->ss.fw_stats_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003341
Brice Goglinb10c0662006-06-08 10:25:00 -04003342 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3343 mgp->cmd, mgp->cmd_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003344
3345 free_netdev(netdev);
3346 pci_set_drvdata(pdev, NULL);
3347}
3348
Brice Goglinb10c0662006-06-08 10:25:00 -04003349#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
Brice Goglina07bc1f2007-09-14 00:40:14 +02003350#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
Brice Goglin0da34b62006-05-23 06:10:15 -04003351
3352static struct pci_device_id myri10ge_pci_tbl[] = {
Brice Goglinb10c0662006-06-08 10:25:00 -04003353 {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
Brice Goglina07bc1f2007-09-14 00:40:14 +02003354 {PCI_DEVICE
3355 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
Brice Goglin0da34b62006-05-23 06:10:15 -04003356 {0},
3357};
3358
3359static struct pci_driver myri10ge_driver = {
3360 .name = "myri10ge",
3361 .probe = myri10ge_probe,
3362 .remove = myri10ge_remove,
3363 .id_table = myri10ge_pci_tbl,
3364#ifdef CONFIG_PM
3365 .suspend = myri10ge_suspend,
3366 .resume = myri10ge_resume,
3367#endif
3368};
3369
3370static __init int myri10ge_init_module(void)
3371{
3372 printk(KERN_INFO "%s: Version %s\n", myri10ge_driver.name,
3373 MYRI10GE_VERSION_STR);
3374 return pci_register_driver(&myri10ge_driver);
3375}
3376
3377module_init(myri10ge_init_module);
3378
3379static __exit void myri10ge_cleanup_module(void)
3380{
3381 pci_unregister_driver(&myri10ge_driver);
3382}
3383
3384module_exit(myri10ge_cleanup_module);