blob: 823bb6d35334acfcd3548b0a79f14a480b5f7779 [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 Goglind399cf82008-05-28 09:10:01 +020078#define MYRI10GE_VERSION_STR "1.3.99-1.347"
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 Goglinfa0a90d2008-05-09 02:20:25 +0200197 int max_intr_slots;
Brice Goglin0da34b62006-05-23 06:10:15 -0400198 struct net_device *dev;
199 struct net_device_stats stats;
Brice Goglinb53bef82008-05-09 02:20:03 +0200200 spinlock_t stats_lock;
Brice Goglin0da34b62006-05-23 06:10:15 -0400201 u8 __iomem *sram;
202 int sram_size;
203 unsigned long board_span;
204 unsigned long iomem_base;
Al Viro40f6cff2006-11-20 13:48:32 -0500205 __be32 __iomem *irq_deassert;
Brice Goglin0da34b62006-05-23 06:10:15 -0400206 char *mac_addr_string;
207 struct mcp_cmd_response *cmd;
208 dma_addr_t cmd_bus;
Brice Goglin0da34b62006-05-23 06:10:15 -0400209 struct pci_dev *pdev;
210 int msi_enabled;
Al Viro66341ff2007-12-22 18:56:43 +0000211 u32 link_state;
Brice Goglin0da34b62006-05-23 06:10:15 -0400212 unsigned int rdma_tags_available;
213 int intr_coal_delay;
Al Viro40f6cff2006-11-20 13:48:32 -0500214 __be32 __iomem *intr_coal_delay_ptr;
Brice Goglin0da34b62006-05-23 06:10:15 -0400215 int mtrr;
Brice Goglin276e26c2007-03-07 20:02:32 +0100216 int wc_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -0400217 int down_cnt;
218 wait_queue_head_t down_wq;
219 struct work_struct watchdog_work;
220 struct timer_list watchdog_timer;
Brice Goglin0da34b62006-05-23 06:10:15 -0400221 int watchdog_resets;
Brice Goglinb53bef82008-05-09 02:20:03 +0200222 int watchdog_pause;
Brice Goglin0da34b62006-05-23 06:10:15 -0400223 int pause;
224 char *fw_name;
225 char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
Brice Goglinc0bf8802008-05-09 02:18:24 +0200226 char *product_code_string;
Brice Goglin0da34b62006-05-23 06:10:15 -0400227 char fw_version[128];
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100228 int fw_ver_major;
229 int fw_ver_minor;
230 int fw_ver_tiny;
231 int adopted_rx_filter_bug;
Brice Goglin0da34b62006-05-23 06:10:15 -0400232 u8 mac_addr[6]; /* eeprom mac address */
233 unsigned long serial_number;
234 int vendor_specific_offset;
Brice Goglin85a7ea12006-08-21 17:36:56 -0400235 int fw_multicast_support;
Brice Goglin4f93fde2007-10-13 12:34:01 +0200236 unsigned long features;
237 u32 max_tso6;
Brice Goglin0da34b62006-05-23 06:10:15 -0400238 u32 read_dma;
239 u32 write_dma;
240 u32 read_write_dma;
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400241 u32 link_changes;
242 u32 msg_enable;
Brice Goglin0da34b62006-05-23 06:10:15 -0400243};
244
245static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
246static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
247
248static char *myri10ge_fw_name = NULL;
249module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200250MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
Brice Goglin0da34b62006-05-23 06:10:15 -0400251
252static int myri10ge_ecrc_enable = 1;
253module_param(myri10ge_ecrc_enable, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200254MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
Brice Goglin0da34b62006-05-23 06:10:15 -0400255
Brice Goglin0da34b62006-05-23 06:10:15 -0400256static int myri10ge_small_bytes = -1; /* -1 == auto */
257module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200258MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
Brice Goglin0da34b62006-05-23 06:10:15 -0400259
260static int myri10ge_msi = 1; /* enable msi by default */
Brice Goglin3621cec2006-12-18 11:51:22 +0100261module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200262MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
Brice Goglin0da34b62006-05-23 06:10:15 -0400263
Brice Goglinf761fae2007-03-21 19:45:56 +0100264static int myri10ge_intr_coal_delay = 75;
Brice Goglin0da34b62006-05-23 06:10:15 -0400265module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200266MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
Brice Goglin0da34b62006-05-23 06:10:15 -0400267
268static int myri10ge_flow_control = 1;
269module_param(myri10ge_flow_control, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200270MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
Brice Goglin0da34b62006-05-23 06:10:15 -0400271
272static int myri10ge_deassert_wait = 1;
273module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
274MODULE_PARM_DESC(myri10ge_deassert_wait,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200275 "Wait when deasserting legacy interrupts");
Brice Goglin0da34b62006-05-23 06:10:15 -0400276
277static int myri10ge_force_firmware = 0;
278module_param(myri10ge_force_firmware, int, S_IRUGO);
279MODULE_PARM_DESC(myri10ge_force_firmware,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200280 "Force firmware to assume aligned completions");
Brice Goglin0da34b62006-05-23 06:10:15 -0400281
Brice Goglin0da34b62006-05-23 06:10:15 -0400282static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
283module_param(myri10ge_initial_mtu, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200284MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
Brice Goglin0da34b62006-05-23 06:10:15 -0400285
286static int myri10ge_napi_weight = 64;
287module_param(myri10ge_napi_weight, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200288MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
Brice Goglin0da34b62006-05-23 06:10:15 -0400289
290static int myri10ge_watchdog_timeout = 1;
291module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200292MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
Brice Goglin0da34b62006-05-23 06:10:15 -0400293
294static int myri10ge_max_irq_loops = 1048576;
295module_param(myri10ge_max_irq_loops, int, S_IRUGO);
296MODULE_PARM_DESC(myri10ge_max_irq_loops,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200297 "Set stuck legacy IRQ detection threshold");
Brice Goglin0da34b62006-05-23 06:10:15 -0400298
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400299#define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
300
301static int myri10ge_debug = -1; /* defaults above */
302module_param(myri10ge_debug, int, 0);
303MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
304
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700305static int myri10ge_lro = 1;
306module_param(myri10ge_lro, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200307MODULE_PARM_DESC(myri10ge_lro, "Enable large receive offload");
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700308
309static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
310module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200311MODULE_PARM_DESC(myri10ge_lro_max_pkts,
312 "Number of LRO packets to be aggregated");
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700313
Brice Goglindd50f332006-12-11 11:25:09 +0100314static int myri10ge_fill_thresh = 256;
315module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200316MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
Brice Goglindd50f332006-12-11 11:25:09 +0100317
Brice Goglinf1811372007-06-11 20:26:31 +0200318static int myri10ge_reset_recover = 1;
319
Brice Goglinf761fae2007-03-21 19:45:56 +0100320static int myri10ge_wcfifo = 0;
Brice Goglin6ebc0872007-01-09 21:04:25 +0100321module_param(myri10ge_wcfifo, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200322MODULE_PARM_DESC(myri10ge_wcfifo, "Enable WC Fifo when WC is enabled");
Brice Goglin6ebc0872007-01-09 21:04:25 +0100323
Brice Goglin0da34b62006-05-23 06:10:15 -0400324#define MYRI10GE_FW_OFFSET 1024*1024
325#define MYRI10GE_HIGHPART_TO_U32(X) \
326(sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
327#define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
328
329#define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
330
Brice Goglin2f762162007-05-07 23:50:37 +0200331static void myri10ge_set_multicast_list(struct net_device *dev);
Brice Goglin4f93fde2007-10-13 12:34:01 +0200332static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev);
Brice Goglin2f762162007-05-07 23:50:37 +0200333
Brice Goglin62502232006-12-11 11:24:37 +0100334static inline void put_be32(__be32 val, __be32 __iomem * p)
Al Viro40f6cff2006-11-20 13:48:32 -0500335{
Brice Goglin62502232006-12-11 11:24:37 +0100336 __raw_writel((__force __u32) val, (__force void __iomem *)p);
Al Viro40f6cff2006-11-20 13:48:32 -0500337}
338
Brice Goglin0da34b62006-05-23 06:10:15 -0400339static int
340myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
341 struct myri10ge_cmd *data, int atomic)
342{
343 struct mcp_cmd *buf;
344 char buf_bytes[sizeof(*buf) + 8];
345 struct mcp_cmd_response *response = mgp->cmd;
Brice Gogline700f9f2006-08-14 17:52:54 -0400346 char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
Brice Goglin0da34b62006-05-23 06:10:15 -0400347 u32 dma_low, dma_high, result, value;
348 int sleep_total = 0;
349
350 /* ensure buf is aligned to 8 bytes */
351 buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
352
353 buf->data0 = htonl(data->data0);
354 buf->data1 = htonl(data->data1);
355 buf->data2 = htonl(data->data2);
356 buf->cmd = htonl(cmd);
357 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
358 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
359
360 buf->response_addr.low = htonl(dma_low);
361 buf->response_addr.high = htonl(dma_high);
Al Viro40f6cff2006-11-20 13:48:32 -0500362 response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglin0da34b62006-05-23 06:10:15 -0400363 mb();
364 myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
365
366 /* wait up to 15ms. Longest command is the DMA benchmark,
367 * which is capped at 5ms, but runs from a timeout handler
368 * that runs every 7.8ms. So a 15ms timeout leaves us with
369 * a 2.2ms margin
370 */
371 if (atomic) {
372 /* if atomic is set, do not sleep,
373 * and try to get the completion quickly
374 * (1ms will be enough for those commands) */
375 for (sleep_total = 0;
376 sleep_total < 1000
Al Viro40f6cff2006-11-20 13:48:32 -0500377 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglinbd2db0c2008-05-09 02:18:45 +0200378 sleep_total += 10) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400379 udelay(10);
Brice Goglinbd2db0c2008-05-09 02:18:45 +0200380 mb();
381 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400382 } else {
383 /* use msleep for most command */
384 for (sleep_total = 0;
385 sleep_total < 15
Al Viro40f6cff2006-11-20 13:48:32 -0500386 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglin0da34b62006-05-23 06:10:15 -0400387 sleep_total++)
388 msleep(1);
389 }
390
391 result = ntohl(response->result);
392 value = ntohl(response->data);
393 if (result != MYRI10GE_NO_RESPONSE_RESULT) {
394 if (result == 0) {
395 data->data0 = value;
396 return 0;
Brice Goglin85a7ea12006-08-21 17:36:56 -0400397 } else if (result == MXGEFW_CMD_UNKNOWN) {
398 return -ENOSYS;
Brice Goglin5443e9e2007-05-07 23:52:22 +0200399 } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
400 return -E2BIG;
Brice Goglin0da34b62006-05-23 06:10:15 -0400401 } else {
402 dev_err(&mgp->pdev->dev,
403 "command %d failed, result = %d\n",
404 cmd, result);
405 return -ENXIO;
406 }
407 }
408
409 dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
410 cmd, result);
411 return -EAGAIN;
412}
413
414/*
415 * The eeprom strings on the lanaiX have the format
416 * SN=x\0
417 * MAC=x:x:x:x:x:x\0
418 * PT:ddd mmm xx xx:xx:xx xx\0
419 * PV:ddd mmm xx xx:xx:xx xx\0
420 */
421static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
422{
423 char *ptr, *limit;
424 int i;
425
426 ptr = mgp->eeprom_strings;
427 limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
428
429 while (*ptr != '\0' && ptr < limit) {
430 if (memcmp(ptr, "MAC=", 4) == 0) {
431 ptr += 4;
432 mgp->mac_addr_string = ptr;
433 for (i = 0; i < 6; i++) {
434 if ((ptr + 2) > limit)
435 goto abort;
436 mgp->mac_addr[i] =
437 simple_strtoul(ptr, &ptr, 16);
438 ptr += 1;
439 }
440 }
Brice Goglinc0bf8802008-05-09 02:18:24 +0200441 if (memcmp(ptr, "PC=", 3) == 0) {
442 ptr += 3;
443 mgp->product_code_string = ptr;
444 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400445 if (memcmp((const void *)ptr, "SN=", 3) == 0) {
446 ptr += 3;
447 mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
448 }
449 while (ptr < limit && *ptr++) ;
450 }
451
452 return 0;
453
454abort:
455 dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
456 return -ENXIO;
457}
458
459/*
460 * Enable or disable periodic RDMAs from the host to make certain
461 * chipsets resend dropped PCIe messages
462 */
463
464static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
465{
466 char __iomem *submit;
Brice Goglinf8fd57c2008-05-09 02:17:37 +0200467 __be32 buf[16] __attribute__ ((__aligned__(8)));
Brice Goglin0da34b62006-05-23 06:10:15 -0400468 u32 dma_low, dma_high;
469 int i;
470
471 /* clear confirmation addr */
472 mgp->cmd->data = 0;
473 mb();
474
475 /* send a rdma command to the PCIe engine, and wait for the
476 * response in the confirmation address. The firmware should
477 * write a -1 there to indicate it is alive and well
478 */
479 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
480 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
481
482 buf[0] = htonl(dma_high); /* confirm addr MSW */
483 buf[1] = htonl(dma_low); /* confirm addr LSW */
Al Viro40f6cff2006-11-20 13:48:32 -0500484 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
Brice Goglin0da34b62006-05-23 06:10:15 -0400485 buf[3] = htonl(dma_high); /* dummy addr MSW */
486 buf[4] = htonl(dma_low); /* dummy addr LSW */
487 buf[5] = htonl(enable); /* enable? */
488
Brice Gogline700f9f2006-08-14 17:52:54 -0400489 submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
Brice Goglin0da34b62006-05-23 06:10:15 -0400490
491 myri10ge_pio_copy(submit, &buf, sizeof(buf));
492 for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
493 msleep(1);
494 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
495 dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
496 (enable ? "enable" : "disable"));
497}
498
499static int
500myri10ge_validate_firmware(struct myri10ge_priv *mgp,
501 struct mcp_gen_header *hdr)
502{
503 struct device *dev = &mgp->pdev->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -0400504
505 /* check firmware type */
506 if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
507 dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
508 return -EINVAL;
509 }
510
511 /* save firmware version for ethtool */
512 strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
513
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100514 sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
515 &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
Brice Goglin0da34b62006-05-23 06:10:15 -0400516
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100517 if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR
518 && mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400519 dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
520 dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
521 MXGEFW_VERSION_MINOR);
522 return -EINVAL;
523 }
524 return 0;
525}
526
527static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
528{
529 unsigned crc, reread_crc;
530 const struct firmware *fw;
531 struct device *dev = &mgp->pdev->dev;
David Woodhouseb0d31d62008-05-24 00:00:07 +0100532 unsigned char *fw_readback;
Brice Goglin0da34b62006-05-23 06:10:15 -0400533 struct mcp_gen_header *hdr;
534 size_t hdr_offset;
535 int status;
Brice Gogline4543582006-07-30 00:14:09 -0400536 unsigned i;
Brice Goglin0da34b62006-05-23 06:10:15 -0400537
538 if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
539 dev_err(dev, "Unable to load %s firmware image via hotplug\n",
540 mgp->fw_name);
541 status = -EINVAL;
542 goto abort_with_nothing;
543 }
544
545 /* check size */
546
547 if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
548 fw->size < MCP_HEADER_PTR_OFFSET + 4) {
549 dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
550 status = -EINVAL;
551 goto abort_with_fw;
552 }
553
554 /* check id */
Al Viro40f6cff2006-11-20 13:48:32 -0500555 hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
Brice Goglin0da34b62006-05-23 06:10:15 -0400556 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
557 dev_err(dev, "Bad firmware file\n");
558 status = -EINVAL;
559 goto abort_with_fw;
560 }
561 hdr = (void *)(fw->data + hdr_offset);
562
563 status = myri10ge_validate_firmware(mgp, hdr);
564 if (status != 0)
565 goto abort_with_fw;
566
567 crc = crc32(~0, fw->data, fw->size);
Brice Gogline4543582006-07-30 00:14:09 -0400568 for (i = 0; i < fw->size; i += 256) {
569 myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
570 fw->data + i,
571 min(256U, (unsigned)(fw->size - i)));
572 mb();
573 readb(mgp->sram);
Brice Goglinb10c0662006-06-08 10:25:00 -0400574 }
David Woodhouseb0d31d62008-05-24 00:00:07 +0100575 fw_readback = vmalloc(fw->size);
576 if (!fw_readback) {
577 status = -ENOMEM;
578 goto abort_with_fw;
579 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400580 /* corruption checking is good for parity recovery and buggy chipset */
David Woodhouseb0d31d62008-05-24 00:00:07 +0100581 memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
582 reread_crc = crc32(~0, fw_readback, fw->size);
583 vfree(fw_readback);
Brice Goglin0da34b62006-05-23 06:10:15 -0400584 if (crc != reread_crc) {
585 dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
586 (unsigned)fw->size, reread_crc, crc);
587 status = -EIO;
588 goto abort_with_fw;
589 }
590 *size = (u32) fw->size;
591
592abort_with_fw:
593 release_firmware(fw);
594
595abort_with_nothing:
596 return status;
597}
598
599static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
600{
601 struct mcp_gen_header *hdr;
602 struct device *dev = &mgp->pdev->dev;
603 const size_t bytes = sizeof(struct mcp_gen_header);
604 size_t hdr_offset;
605 int status;
606
607 /* find running firmware header */
Al Viro66341ff2007-12-22 18:56:43 +0000608 hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
Brice Goglin0da34b62006-05-23 06:10:15 -0400609
610 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
611 dev_err(dev, "Running firmware has bad header offset (%d)\n",
612 (int)hdr_offset);
613 return -EIO;
614 }
615
616 /* copy header of running firmware from SRAM to host memory to
617 * validate firmware */
618 hdr = kmalloc(bytes, GFP_KERNEL);
619 if (hdr == NULL) {
620 dev_err(dev, "could not malloc firmware hdr\n");
621 return -ENOMEM;
622 }
623 memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
624 status = myri10ge_validate_firmware(mgp, hdr);
625 kfree(hdr);
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100626
627 /* check to see if adopted firmware has bug where adopting
628 * it will cause broadcasts to be filtered unless the NIC
629 * is kept in ALLMULTI mode */
630 if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
631 mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
632 mgp->adopted_rx_filter_bug = 1;
633 dev_warn(dev, "Adopting fw %d.%d.%d: "
634 "working around rx filter bug\n",
635 mgp->fw_ver_major, mgp->fw_ver_minor,
636 mgp->fw_ver_tiny);
637 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400638 return status;
639}
640
Adrian Bunk0178ec32008-05-20 00:53:00 +0300641static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200642{
643 struct myri10ge_cmd cmd;
644 int status;
645
646 /* probe for IPv6 TSO support */
647 mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
648 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
649 &cmd, 0);
650 if (status == 0) {
651 mgp->max_tso6 = cmd.data0;
652 mgp->features |= NETIF_F_TSO6;
653 }
654
655 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
656 if (status != 0) {
657 dev_err(&mgp->pdev->dev,
658 "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
659 return -ENXIO;
660 }
661
662 mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
663
664 return 0;
665}
666
Brice Goglin0da34b62006-05-23 06:10:15 -0400667static int myri10ge_load_firmware(struct myri10ge_priv *mgp)
668{
669 char __iomem *submit;
Brice Goglinf8fd57c2008-05-09 02:17:37 +0200670 __be32 buf[16] __attribute__ ((__aligned__(8)));
Brice Goglin0da34b62006-05-23 06:10:15 -0400671 u32 dma_low, dma_high, size;
672 int status, i;
673
Brice Goglinb10c0662006-06-08 10:25:00 -0400674 size = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -0400675 status = myri10ge_load_hotplug_firmware(mgp, &size);
676 if (status) {
677 dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
678
679 /* Do not attempt to adopt firmware if there
680 * was a bad crc */
681 if (status == -EIO)
682 return status;
683
684 status = myri10ge_adopt_running_firmware(mgp);
685 if (status != 0) {
686 dev_err(&mgp->pdev->dev,
687 "failed to adopt running firmware\n");
688 return status;
689 }
690 dev_info(&mgp->pdev->dev,
691 "Successfully adopted running firmware\n");
Brice Goglinb53bef82008-05-09 02:20:03 +0200692 if (mgp->tx_boundary == 4096) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400693 dev_warn(&mgp->pdev->dev,
694 "Using firmware currently running on NIC"
695 ". For optimal\n");
696 dev_warn(&mgp->pdev->dev,
697 "performance consider loading optimized "
698 "firmware\n");
699 dev_warn(&mgp->pdev->dev, "via hotplug\n");
700 }
701
702 mgp->fw_name = "adopted";
Brice Goglinb53bef82008-05-09 02:20:03 +0200703 mgp->tx_boundary = 2048;
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200704 myri10ge_dummy_rdma(mgp, 1);
705 status = myri10ge_get_firmware_capabilities(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -0400706 return status;
707 }
708
709 /* clear confirmation addr */
710 mgp->cmd->data = 0;
711 mb();
712
713 /* send a reload command to the bootstrap MCP, and wait for the
714 * response in the confirmation address. The firmware should
715 * write a -1 there to indicate it is alive and well
716 */
717 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
718 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
719
720 buf[0] = htonl(dma_high); /* confirm addr MSW */
721 buf[1] = htonl(dma_low); /* confirm addr LSW */
Al Viro40f6cff2006-11-20 13:48:32 -0500722 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
Brice Goglin0da34b62006-05-23 06:10:15 -0400723
724 /* FIX: All newest firmware should un-protect the bottom of
725 * the sram before handoff. However, the very first interfaces
726 * do not. Therefore the handoff copy must skip the first 8 bytes
727 */
728 buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
729 buf[4] = htonl(size - 8); /* length of code */
730 buf[5] = htonl(8); /* where to copy to */
731 buf[6] = htonl(0); /* where to jump to */
732
Brice Gogline700f9f2006-08-14 17:52:54 -0400733 submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
Brice Goglin0da34b62006-05-23 06:10:15 -0400734
735 myri10ge_pio_copy(submit, &buf, sizeof(buf));
736 mb();
737 msleep(1);
738 mb();
739 i = 0;
Brice Goglind93ca2a2008-05-09 02:17:16 +0200740 while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
741 msleep(1 << i);
Brice Goglin0da34b62006-05-23 06:10:15 -0400742 i++;
743 }
744 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
745 dev_err(&mgp->pdev->dev, "handoff failed\n");
746 return -ENXIO;
747 }
Brice Goglin9a71db72006-07-21 15:49:32 -0400748 myri10ge_dummy_rdma(mgp, 1);
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200749 status = myri10ge_get_firmware_capabilities(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -0400750
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200751 return status;
Brice Goglin0da34b62006-05-23 06:10:15 -0400752}
753
754static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
755{
756 struct myri10ge_cmd cmd;
757 int status;
758
759 cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
760 | (addr[2] << 8) | addr[3]);
761
762 cmd.data1 = ((addr[4] << 8) | (addr[5]));
763
764 status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
765 return status;
766}
767
768static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
769{
770 struct myri10ge_cmd cmd;
771 int status, ctl;
772
773 ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
774 status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
775
776 if (status) {
777 printk(KERN_ERR
778 "myri10ge: %s: Failed to set flow control mode\n",
779 mgp->dev->name);
780 return status;
781 }
782 mgp->pause = pause;
783 return 0;
784}
785
786static void
787myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
788{
789 struct myri10ge_cmd cmd;
790 int status, ctl;
791
792 ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
793 status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
794 if (status)
795 printk(KERN_ERR "myri10ge: %s: Failed to set promisc mode\n",
796 mgp->dev->name);
797}
798
Brice Goglin0d6ac252007-05-07 23:51:45 +0200799static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
800{
801 struct myri10ge_cmd cmd;
802 int status;
803 u32 len;
804 struct page *dmatest_page;
805 dma_addr_t dmatest_bus;
806 char *test = " ";
807
808 dmatest_page = alloc_page(GFP_KERNEL);
809 if (!dmatest_page)
810 return -ENOMEM;
811 dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
812 DMA_BIDIRECTIONAL);
813
814 /* Run a small DMA test.
815 * The magic multipliers to the length tell the firmware
816 * to do DMA read, write, or read+write tests. The
817 * results are returned in cmd.data0. The upper 16
818 * bits or the return is the number of transfers completed.
819 * The lower 16 bits is the time in 0.5us ticks that the
820 * transfers took to complete.
821 */
822
Brice Goglinb53bef82008-05-09 02:20:03 +0200823 len = mgp->tx_boundary;
Brice Goglin0d6ac252007-05-07 23:51:45 +0200824
825 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
826 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
827 cmd.data2 = len * 0x10000;
828 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
829 if (status != 0) {
830 test = "read";
831 goto abort;
832 }
833 mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
834 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
835 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
836 cmd.data2 = len * 0x1;
837 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
838 if (status != 0) {
839 test = "write";
840 goto abort;
841 }
842 mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
843
844 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
845 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
846 cmd.data2 = len * 0x10001;
847 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
848 if (status != 0) {
849 test = "read/write";
850 goto abort;
851 }
852 mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
853 (cmd.data0 & 0xffff);
854
855abort:
856 pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
857 put_page(dmatest_page);
858
859 if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
860 dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
861 test, status);
862
863 return status;
864}
865
Brice Goglin0da34b62006-05-23 06:10:15 -0400866static int myri10ge_reset(struct myri10ge_priv *mgp)
867{
868 struct myri10ge_cmd cmd;
869 int status;
870 size_t bytes;
Brice Goglin0da34b62006-05-23 06:10:15 -0400871
872 /* try to send a reset command to the card to see if it
873 * is alive */
874 memset(&cmd, 0, sizeof(cmd));
875 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
876 if (status != 0) {
877 dev_err(&mgp->pdev->dev, "failed reset\n");
878 return -ENXIO;
879 }
Brice Goglin0d6ac252007-05-07 23:51:45 +0200880
881 (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
Brice Goglin0da34b62006-05-23 06:10:15 -0400882
883 /* Now exchange information about interrupts */
884
Brice Goglin014377a2008-05-09 02:20:47 +0200885 bytes = mgp->max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
Brice Goglinb53bef82008-05-09 02:20:03 +0200886 memset(mgp->ss.rx_done.entry, 0, bytes);
Brice Goglin0da34b62006-05-23 06:10:15 -0400887 cmd.data0 = (u32) bytes;
888 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +0200889 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->ss.rx_done.bus);
890 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->ss.rx_done.bus);
Brice Goglin0da34b62006-05-23 06:10:15 -0400891 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA, &cmd, 0);
892
893 status |=
894 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +0200895 mgp->ss.irq_claim = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglindf30a742006-12-18 11:50:40 +0100896 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
897 &cmd, 0);
898 mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglin0da34b62006-05-23 06:10:15 -0400899
Brice Goglin0da34b62006-05-23 06:10:15 -0400900 status |= myri10ge_send_cmd
901 (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
Al Viro40f6cff2006-11-20 13:48:32 -0500902 mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglin0da34b62006-05-23 06:10:15 -0400903 if (status != 0) {
904 dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
905 return status;
906 }
Al Viro40f6cff2006-11-20 13:48:32 -0500907 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
Brice Goglin0da34b62006-05-23 06:10:15 -0400908
Brice Goglinb53bef82008-05-09 02:20:03 +0200909 memset(mgp->ss.rx_done.entry, 0, bytes);
Brice Goglin0da34b62006-05-23 06:10:15 -0400910
911 /* reset mcp/driver shared state back to 0 */
Brice Goglinb53bef82008-05-09 02:20:03 +0200912 mgp->ss.tx.req = 0;
913 mgp->ss.tx.done = 0;
914 mgp->ss.tx.pkt_start = 0;
915 mgp->ss.tx.pkt_done = 0;
916 mgp->ss.rx_big.cnt = 0;
917 mgp->ss.rx_small.cnt = 0;
918 mgp->ss.rx_done.idx = 0;
919 mgp->ss.rx_done.cnt = 0;
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400920 mgp->link_changes = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -0400921 status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
Brice Goglin0da34b62006-05-23 06:10:15 -0400922 myri10ge_change_pause(mgp, mgp->pause);
Brice Goglin2f762162007-05-07 23:50:37 +0200923 myri10ge_set_multicast_list(mgp->dev);
Brice Goglin0da34b62006-05-23 06:10:15 -0400924 return status;
925}
926
927static inline void
928myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
929 struct mcp_kreq_ether_recv *src)
930{
Al Viro40f6cff2006-11-20 13:48:32 -0500931 __be32 low;
Brice Goglin0da34b62006-05-23 06:10:15 -0400932
933 low = src->addr_low;
Al Viro40f6cff2006-11-20 13:48:32 -0500934 src->addr_low = htonl(DMA_32BIT_MASK);
Brice Gogline67bda52006-12-05 17:26:27 +0100935 myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
936 mb();
937 myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
Brice Goglin0da34b62006-05-23 06:10:15 -0400938 mb();
939 src->addr_low = low;
Al Viro40f6cff2006-11-20 13:48:32 -0500940 put_be32(low, &dst->addr_low);
Brice Goglin0da34b62006-05-23 06:10:15 -0400941 mb();
942}
943
Al Viro40f6cff2006-11-20 13:48:32 -0500944static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
Brice Goglin0da34b62006-05-23 06:10:15 -0400945{
946 struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
947
Al Viro40f6cff2006-11-20 13:48:32 -0500948 if ((skb->protocol == htons(ETH_P_8021Q)) &&
Brice Goglin0da34b62006-05-23 06:10:15 -0400949 (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
950 vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
951 skb->csum = hw_csum;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700952 skb->ip_summed = CHECKSUM_COMPLETE;
Brice Goglin0da34b62006-05-23 06:10:15 -0400953 }
954}
955
Brice Goglindd50f332006-12-11 11:25:09 +0100956static inline void
957myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
958 struct skb_frag_struct *rx_frags, int len, int hlen)
959{
960 struct skb_frag_struct *skb_frags;
961
962 skb->len = skb->data_len = len;
963 skb->truesize = len + sizeof(struct sk_buff);
964 /* attach the page(s) */
965
966 skb_frags = skb_shinfo(skb)->frags;
967 while (len > 0) {
968 memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
969 len -= rx_frags->size;
970 skb_frags++;
971 rx_frags++;
972 skb_shinfo(skb)->nr_frags++;
973 }
974
975 /* pskb_may_pull is not available in irq context, but
976 * skb_pull() (for ether_pad and eth_type_trans()) requires
977 * the beginning of the packet in skb_headlen(), move it
978 * manually */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300979 skb_copy_to_linear_data(skb, va, hlen);
Brice Goglindd50f332006-12-11 11:25:09 +0100980 skb_shinfo(skb)->frags[0].page_offset += hlen;
981 skb_shinfo(skb)->frags[0].size -= hlen;
982 skb->data_len -= hlen;
983 skb->tail += hlen;
984 skb_pull(skb, MXGEFW_PAD);
985}
986
987static void
988myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
989 int bytes, int watchdog)
990{
991 struct page *page;
992 int idx;
993
994 if (unlikely(rx->watchdog_needed && !watchdog))
995 return;
996
997 /* try to refill entire ring */
998 while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
999 idx = rx->fill_cnt & rx->mask;
Brice Goglinae8509b2007-04-10 21:21:08 +02001000 if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
Brice Goglindd50f332006-12-11 11:25:09 +01001001 /* we can use part of previous page */
1002 get_page(rx->page);
1003 } else {
1004 /* we need a new page */
1005 page =
1006 alloc_pages(GFP_ATOMIC | __GFP_COMP,
1007 MYRI10GE_ALLOC_ORDER);
1008 if (unlikely(page == NULL)) {
1009 if (rx->fill_cnt - rx->cnt < 16)
1010 rx->watchdog_needed = 1;
1011 return;
1012 }
1013 rx->page = page;
1014 rx->page_offset = 0;
1015 rx->bus = pci_map_page(mgp->pdev, page, 0,
1016 MYRI10GE_ALLOC_SIZE,
1017 PCI_DMA_FROMDEVICE);
1018 }
1019 rx->info[idx].page = rx->page;
1020 rx->info[idx].page_offset = rx->page_offset;
1021 /* note that this is the address of the start of the
1022 * page */
1023 pci_unmap_addr_set(&rx->info[idx], bus, rx->bus);
1024 rx->shadow[idx].addr_low =
1025 htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
1026 rx->shadow[idx].addr_high =
1027 htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
1028
1029 /* start next packet on a cacheline boundary */
1030 rx->page_offset += SKB_DATA_ALIGN(bytes);
Brice Goglinae8509b2007-04-10 21:21:08 +02001031
1032#if MYRI10GE_ALLOC_SIZE > 4096
1033 /* don't cross a 4KB boundary */
1034 if ((rx->page_offset >> 12) !=
1035 ((rx->page_offset + bytes - 1) >> 12))
1036 rx->page_offset = (rx->page_offset + 4096) & ~4095;
1037#endif
Brice Goglindd50f332006-12-11 11:25:09 +01001038 rx->fill_cnt++;
1039
1040 /* copy 8 descriptors to the firmware at a time */
1041 if ((idx & 7) == 7) {
1042 if (rx->wc_fifo == NULL)
1043 myri10ge_submit_8rx(&rx->lanai[idx - 7],
1044 &rx->shadow[idx - 7]);
1045 else {
1046 mb();
1047 myri10ge_pio_copy(rx->wc_fifo,
1048 &rx->shadow[idx - 7], 64);
1049 }
1050 }
1051 }
1052}
1053
1054static inline void
1055myri10ge_unmap_rx_page(struct pci_dev *pdev,
1056 struct myri10ge_rx_buffer_state *info, int bytes)
1057{
1058 /* unmap the recvd page if we're the only or last user of it */
1059 if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
1060 (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
1061 pci_unmap_page(pdev, (pci_unmap_addr(info, bus)
1062 & ~(MYRI10GE_ALLOC_SIZE - 1)),
1063 MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
1064 }
1065}
1066
1067#define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
1068 * page into an skb */
1069
1070static inline int
Brice Goglinb53bef82008-05-09 02:20:03 +02001071myri10ge_rx_done(struct myri10ge_slice_state *ss, struct myri10ge_rx_buf *rx,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001072 int bytes, int len, __wsum csum)
Brice Goglindd50f332006-12-11 11:25:09 +01001073{
Brice Goglinb53bef82008-05-09 02:20:03 +02001074 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglindd50f332006-12-11 11:25:09 +01001075 struct sk_buff *skb;
1076 struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
1077 int i, idx, hlen, remainder;
1078 struct pci_dev *pdev = mgp->pdev;
1079 struct net_device *dev = mgp->dev;
1080 u8 *va;
1081
1082 len += MXGEFW_PAD;
1083 idx = rx->cnt & rx->mask;
1084 va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
1085 prefetch(va);
1086 /* Fill skb_frag_struct(s) with data from our receive */
1087 for (i = 0, remainder = len; remainder > 0; i++) {
1088 myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1089 rx_frags[i].page = rx->info[idx].page;
1090 rx_frags[i].page_offset = rx->info[idx].page_offset;
1091 if (remainder < MYRI10GE_ALLOC_SIZE)
1092 rx_frags[i].size = remainder;
1093 else
1094 rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
1095 rx->cnt++;
1096 idx = rx->cnt & rx->mask;
1097 remainder -= MYRI10GE_ALLOC_SIZE;
1098 }
1099
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001100 if (mgp->csum_flag && myri10ge_lro) {
1101 rx_frags[0].page_offset += MXGEFW_PAD;
1102 rx_frags[0].size -= MXGEFW_PAD;
1103 len -= MXGEFW_PAD;
Brice Goglinb53bef82008-05-09 02:20:03 +02001104 lro_receive_frags(&ss->rx_done.lro_mgr, rx_frags,
Al Viro66341ff2007-12-22 18:56:43 +00001105 len, len,
Brice Goglinb53bef82008-05-09 02:20:03 +02001106 /* opaque, will come back in get_frag_header */
1107 (void *)(__force unsigned long)csum, csum);
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001108 return 1;
1109 }
1110
Brice Goglindd50f332006-12-11 11:25:09 +01001111 hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
1112
Brice Gogline636b2e2007-10-13 12:32:21 +02001113 /* allocate an skb to attach the page(s) to. This is done
1114 * after trying LRO, so as to avoid skb allocation overheads */
Brice Goglindd50f332006-12-11 11:25:09 +01001115
1116 skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
1117 if (unlikely(skb == NULL)) {
1118 mgp->stats.rx_dropped++;
1119 do {
1120 i--;
1121 put_page(rx_frags[i].page);
1122 } while (i != 0);
1123 return 0;
1124 }
1125
1126 /* Attach the pages to the skb, and trim off any padding */
1127 myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
1128 if (skb_shinfo(skb)->frags[0].size <= 0) {
1129 put_page(skb_shinfo(skb)->frags[0].page);
1130 skb_shinfo(skb)->nr_frags = 0;
1131 }
1132 skb->protocol = eth_type_trans(skb, dev);
Brice Goglindd50f332006-12-11 11:25:09 +01001133
1134 if (mgp->csum_flag) {
1135 if ((skb->protocol == htons(ETH_P_IP)) ||
1136 (skb->protocol == htons(ETH_P_IPV6))) {
1137 skb->csum = csum;
1138 skb->ip_summed = CHECKSUM_COMPLETE;
1139 } else
1140 myri10ge_vlan_ip_csum(skb, csum);
1141 }
1142 netif_receive_skb(skb);
1143 dev->last_rx = jiffies;
1144 return 1;
1145}
1146
Brice Goglinb53bef82008-05-09 02:20:03 +02001147static inline void
1148myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
Brice Goglin0da34b62006-05-23 06:10:15 -04001149{
Brice Goglinb53bef82008-05-09 02:20:03 +02001150 struct pci_dev *pdev = ss->mgp->pdev;
1151 struct myri10ge_tx_buf *tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001152 struct sk_buff *skb;
1153 int idx, len;
Brice Goglin0da34b62006-05-23 06:10:15 -04001154
1155 while (tx->pkt_done != mcp_index) {
1156 idx = tx->done & tx->mask;
1157 skb = tx->info[idx].skb;
1158
1159 /* Mark as free */
1160 tx->info[idx].skb = NULL;
1161 if (tx->info[idx].last) {
1162 tx->pkt_done++;
1163 tx->info[idx].last = 0;
1164 }
1165 tx->done++;
1166 len = pci_unmap_len(&tx->info[idx], len);
1167 pci_unmap_len_set(&tx->info[idx], len, 0);
1168 if (skb) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001169 ss->stats.tx_bytes += skb->len;
1170 ss->stats.tx_packets++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001171 dev_kfree_skb_irq(skb);
1172 if (len)
1173 pci_unmap_single(pdev,
1174 pci_unmap_addr(&tx->info[idx],
1175 bus), len,
1176 PCI_DMA_TODEVICE);
1177 } else {
1178 if (len)
1179 pci_unmap_page(pdev,
1180 pci_unmap_addr(&tx->info[idx],
1181 bus), len,
1182 PCI_DMA_TODEVICE);
1183 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001184 }
1185 /* start the queue if we've stopped it */
Brice Goglinb53bef82008-05-09 02:20:03 +02001186 if (netif_queue_stopped(ss->dev)
Brice Goglin0da34b62006-05-23 06:10:15 -04001187 && tx->req - tx->done < (tx->mask >> 1)) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001188 tx->wake_queue++;
1189 netif_wake_queue(ss->dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04001190 }
1191}
1192
Brice Goglinb53bef82008-05-09 02:20:03 +02001193static inline int
1194myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
Brice Goglin0da34b62006-05-23 06:10:15 -04001195{
Brice Goglinb53bef82008-05-09 02:20:03 +02001196 struct myri10ge_rx_done *rx_done = &ss->rx_done;
1197 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001198 unsigned long rx_bytes = 0;
1199 unsigned long rx_packets = 0;
1200 unsigned long rx_ok;
1201
1202 int idx = rx_done->idx;
1203 int cnt = rx_done->cnt;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001204 int work_done = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04001205 u16 length;
Al Viro40f6cff2006-11-20 13:48:32 -05001206 __wsum checksum;
Brice Goglin0da34b62006-05-23 06:10:15 -04001207
Andrew Gallatinc956a242007-10-31 17:40:06 -04001208 while (rx_done->entry[idx].length != 0 && work_done < budget) {
Brice Goglin0da34b62006-05-23 06:10:15 -04001209 length = ntohs(rx_done->entry[idx].length);
1210 rx_done->entry[idx].length = 0;
Al Viro40f6cff2006-11-20 13:48:32 -05001211 checksum = csum_unfold(rx_done->entry[idx].checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001212 if (length <= mgp->small_bytes)
Brice Goglinb53bef82008-05-09 02:20:03 +02001213 rx_ok = myri10ge_rx_done(ss, &ss->rx_small,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001214 mgp->small_bytes,
1215 length, checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001216 else
Brice Goglinb53bef82008-05-09 02:20:03 +02001217 rx_ok = myri10ge_rx_done(ss, &ss->rx_big,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001218 mgp->big_bytes,
1219 length, checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001220 rx_packets += rx_ok;
1221 rx_bytes += rx_ok * (unsigned long)length;
1222 cnt++;
Brice Goglin014377a2008-05-09 02:20:47 +02001223 idx = cnt & (mgp->max_intr_slots - 1);
Andrew Gallatinc956a242007-10-31 17:40:06 -04001224 work_done++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001225 }
1226 rx_done->idx = idx;
1227 rx_done->cnt = cnt;
Brice Goglinb53bef82008-05-09 02:20:03 +02001228 ss->stats.rx_packets += rx_packets;
1229 ss->stats.rx_bytes += rx_bytes;
Brice Goglinc7dab992006-12-11 11:25:42 +01001230
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001231 if (myri10ge_lro)
1232 lro_flush_all(&rx_done->lro_mgr);
1233
Brice Goglinc7dab992006-12-11 11:25:42 +01001234 /* restock receive rings if needed */
Brice Goglinb53bef82008-05-09 02:20:03 +02001235 if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
1236 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
Brice Goglinc7dab992006-12-11 11:25:42 +01001237 mgp->small_bytes + MXGEFW_PAD, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001238 if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
1239 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
Brice Goglinc7dab992006-12-11 11:25:42 +01001240
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001241 return work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001242}
1243
1244static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
1245{
Brice Goglinb53bef82008-05-09 02:20:03 +02001246 struct mcp_irq_data *stats = mgp->ss.fw_stats;
Brice Goglin0da34b62006-05-23 06:10:15 -04001247
1248 if (unlikely(stats->stats_updated)) {
Brice Goglin798a95d2007-06-11 20:26:50 +02001249 unsigned link_up = ntohl(stats->link_up);
1250 if (mgp->link_state != link_up) {
1251 mgp->link_state = link_up;
1252
1253 if (mgp->link_state == MXGEFW_LINK_UP) {
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001254 if (netif_msg_link(mgp))
1255 printk(KERN_INFO
1256 "myri10ge: %s: link up\n",
1257 mgp->dev->name);
Brice Goglin0da34b62006-05-23 06:10:15 -04001258 netif_carrier_on(mgp->dev);
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001259 mgp->link_changes++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001260 } else {
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001261 if (netif_msg_link(mgp))
1262 printk(KERN_INFO
Brice Goglin798a95d2007-06-11 20:26:50 +02001263 "myri10ge: %s: link %s\n",
1264 mgp->dev->name,
1265 (link_up == MXGEFW_LINK_MYRINET ?
1266 "mismatch (Myrinet detected)" :
1267 "down"));
Brice Goglin0da34b62006-05-23 06:10:15 -04001268 netif_carrier_off(mgp->dev);
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001269 mgp->link_changes++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001270 }
1271 }
1272 if (mgp->rdma_tags_available !=
Brice Goglinb53bef82008-05-09 02:20:03 +02001273 ntohl(stats->rdma_tags_available)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04001274 mgp->rdma_tags_available =
Brice Goglinb53bef82008-05-09 02:20:03 +02001275 ntohl(stats->rdma_tags_available);
Brice Goglin0da34b62006-05-23 06:10:15 -04001276 printk(KERN_WARNING "myri10ge: %s: RDMA timed out! "
1277 "%d tags left\n", mgp->dev->name,
1278 mgp->rdma_tags_available);
1279 }
1280 mgp->down_cnt += stats->link_down;
1281 if (stats->link_down)
1282 wake_up(&mgp->down_wq);
1283 }
1284}
1285
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001286static int myri10ge_poll(struct napi_struct *napi, int budget)
Brice Goglin0da34b62006-05-23 06:10:15 -04001287{
Brice Goglinb53bef82008-05-09 02:20:03 +02001288 struct myri10ge_slice_state *ss =
1289 container_of(napi, struct myri10ge_slice_state, napi);
1290 struct net_device *netdev = ss->mgp->dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001291 int work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001292
1293 /* process as many rx events as NAPI will allow */
Brice Goglinb53bef82008-05-09 02:20:03 +02001294 work_done = myri10ge_clean_rx_done(ss, budget);
Brice Goglin0da34b62006-05-23 06:10:15 -04001295
David S. Miller4ec24112008-01-07 20:48:21 -08001296 if (work_done < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001297 netif_rx_complete(netdev, napi);
Brice Goglinb53bef82008-05-09 02:20:03 +02001298 put_be32(htonl(3), ss->irq_claim);
Brice Goglin0da34b62006-05-23 06:10:15 -04001299 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001300 return work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001301}
1302
David Howells7d12e782006-10-05 14:55:46 +01001303static irqreturn_t myri10ge_intr(int irq, void *arg)
Brice Goglin0da34b62006-05-23 06:10:15 -04001304{
Brice Goglinb53bef82008-05-09 02:20:03 +02001305 struct myri10ge_slice_state *ss = arg;
1306 struct myri10ge_priv *mgp = ss->mgp;
1307 struct mcp_irq_data *stats = ss->fw_stats;
1308 struct myri10ge_tx_buf *tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001309 u32 send_done_count;
1310 int i;
1311
1312 /* make sure it is our IRQ, and that the DMA has finished */
1313 if (unlikely(!stats->valid))
1314 return (IRQ_NONE);
1315
1316 /* low bit indicates receives are present, so schedule
1317 * napi poll handler */
1318 if (stats->valid & 1)
Brice Goglinb53bef82008-05-09 02:20:03 +02001319 netif_rx_schedule(ss->dev, &ss->napi);
Brice Goglin0da34b62006-05-23 06:10:15 -04001320
1321 if (!mgp->msi_enabled) {
Al Viro40f6cff2006-11-20 13:48:32 -05001322 put_be32(0, mgp->irq_deassert);
Brice Goglin0da34b62006-05-23 06:10:15 -04001323 if (!myri10ge_deassert_wait)
1324 stats->valid = 0;
1325 mb();
1326 } else
1327 stats->valid = 0;
1328
1329 /* Wait for IRQ line to go low, if using INTx */
1330 i = 0;
1331 while (1) {
1332 i++;
1333 /* check for transmit completes and receives */
1334 send_done_count = ntohl(stats->send_done_count);
1335 if (send_done_count != tx->pkt_done)
Brice Goglinb53bef82008-05-09 02:20:03 +02001336 myri10ge_tx_done(ss, (int)send_done_count);
Brice Goglin0da34b62006-05-23 06:10:15 -04001337 if (unlikely(i > myri10ge_max_irq_loops)) {
1338 printk(KERN_WARNING "myri10ge: %s: irq stuck?\n",
1339 mgp->dev->name);
1340 stats->valid = 0;
1341 schedule_work(&mgp->watchdog_work);
1342 }
1343 if (likely(stats->valid == 0))
1344 break;
1345 cpu_relax();
1346 barrier();
1347 }
1348
1349 myri10ge_check_statblock(mgp);
1350
Brice Goglinb53bef82008-05-09 02:20:03 +02001351 put_be32(htonl(3), ss->irq_claim + 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04001352 return (IRQ_HANDLED);
1353}
1354
1355static int
1356myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1357{
Brice Goglinc0bf8802008-05-09 02:18:24 +02001358 struct myri10ge_priv *mgp = netdev_priv(netdev);
1359 char *ptr;
1360 int i;
1361
Brice Goglin0da34b62006-05-23 06:10:15 -04001362 cmd->autoneg = AUTONEG_DISABLE;
1363 cmd->speed = SPEED_10000;
1364 cmd->duplex = DUPLEX_FULL;
Brice Goglinc0bf8802008-05-09 02:18:24 +02001365
1366 /*
1367 * parse the product code to deterimine the interface type
1368 * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
1369 * after the 3rd dash in the driver's cached copy of the
1370 * EEPROM's product code string.
1371 */
1372 ptr = mgp->product_code_string;
1373 if (ptr == NULL) {
1374 printk(KERN_ERR "myri10ge: %s: Missing product code\n",
Brice Goglin99f5f872008-05-09 02:19:08 +02001375 netdev->name);
Brice Goglinc0bf8802008-05-09 02:18:24 +02001376 return 0;
1377 }
1378 for (i = 0; i < 3; i++, ptr++) {
1379 ptr = strchr(ptr, '-');
1380 if (ptr == NULL) {
1381 printk(KERN_ERR "myri10ge: %s: Invalid product "
1382 "code %s\n", netdev->name,
1383 mgp->product_code_string);
1384 return 0;
1385 }
1386 }
1387 if (*ptr == 'R' || *ptr == 'Q') {
1388 /* We've found either an XFP or quad ribbon fiber */
1389 cmd->port = PORT_FIBRE;
1390 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001391 return 0;
1392}
1393
1394static void
1395myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
1396{
1397 struct myri10ge_priv *mgp = netdev_priv(netdev);
1398
1399 strlcpy(info->driver, "myri10ge", sizeof(info->driver));
1400 strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
1401 strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
1402 strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
1403}
1404
1405static int
1406myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1407{
1408 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001409
Brice Goglin0da34b62006-05-23 06:10:15 -04001410 coal->rx_coalesce_usecs = mgp->intr_coal_delay;
1411 return 0;
1412}
1413
1414static int
1415myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1416{
1417 struct myri10ge_priv *mgp = netdev_priv(netdev);
1418
1419 mgp->intr_coal_delay = coal->rx_coalesce_usecs;
Al Viro40f6cff2006-11-20 13:48:32 -05001420 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
Brice Goglin0da34b62006-05-23 06:10:15 -04001421 return 0;
1422}
1423
1424static void
1425myri10ge_get_pauseparam(struct net_device *netdev,
1426 struct ethtool_pauseparam *pause)
1427{
1428 struct myri10ge_priv *mgp = netdev_priv(netdev);
1429
1430 pause->autoneg = 0;
1431 pause->rx_pause = mgp->pause;
1432 pause->tx_pause = mgp->pause;
1433}
1434
1435static int
1436myri10ge_set_pauseparam(struct net_device *netdev,
1437 struct ethtool_pauseparam *pause)
1438{
1439 struct myri10ge_priv *mgp = netdev_priv(netdev);
1440
1441 if (pause->tx_pause != mgp->pause)
1442 return myri10ge_change_pause(mgp, pause->tx_pause);
1443 if (pause->rx_pause != mgp->pause)
1444 return myri10ge_change_pause(mgp, pause->tx_pause);
1445 if (pause->autoneg != 0)
1446 return -EINVAL;
1447 return 0;
1448}
1449
1450static void
1451myri10ge_get_ringparam(struct net_device *netdev,
1452 struct ethtool_ringparam *ring)
1453{
1454 struct myri10ge_priv *mgp = netdev_priv(netdev);
1455
Brice Goglinb53bef82008-05-09 02:20:03 +02001456 ring->rx_mini_max_pending = mgp->ss.rx_small.mask + 1;
1457 ring->rx_max_pending = mgp->ss.rx_big.mask + 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001458 ring->rx_jumbo_max_pending = 0;
Brice Goglinb53bef82008-05-09 02:20:03 +02001459 ring->tx_max_pending = mgp->ss.rx_small.mask + 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001460 ring->rx_mini_pending = ring->rx_mini_max_pending;
1461 ring->rx_pending = ring->rx_max_pending;
1462 ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
1463 ring->tx_pending = ring->tx_max_pending;
1464}
1465
1466static u32 myri10ge_get_rx_csum(struct net_device *netdev)
1467{
1468 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001469
Brice Goglin0da34b62006-05-23 06:10:15 -04001470 if (mgp->csum_flag)
1471 return 1;
1472 else
1473 return 0;
1474}
1475
1476static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
1477{
1478 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001479
Brice Goglin0da34b62006-05-23 06:10:15 -04001480 if (csum_enabled)
1481 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
1482 else
1483 mgp->csum_flag = 0;
1484 return 0;
1485}
1486
Brice Goglin4f93fde2007-10-13 12:34:01 +02001487static int myri10ge_set_tso(struct net_device *netdev, u32 tso_enabled)
1488{
1489 struct myri10ge_priv *mgp = netdev_priv(netdev);
1490 unsigned long flags = mgp->features & (NETIF_F_TSO6 | NETIF_F_TSO);
1491
1492 if (tso_enabled)
1493 netdev->features |= flags;
1494 else
1495 netdev->features &= ~flags;
1496 return 0;
1497}
1498
Brice Goglinb53bef82008-05-09 02:20:03 +02001499static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
Brice Goglin0da34b62006-05-23 06:10:15 -04001500 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
1501 "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
1502 "rx_length_errors", "rx_over_errors", "rx_crc_errors",
1503 "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
1504 "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
1505 "tx_heartbeat_errors", "tx_window_errors",
1506 /* device-specific stats */
Brice Goglin2c1a1082006-07-03 18:16:46 -04001507 "tx_boundary", "WC", "irq", "MSI",
Brice Goglin0da34b62006-05-23 06:10:15 -04001508 "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
Brice Goglinb53bef82008-05-09 02:20:03 +02001509 "serial_number", "watchdog_resets",
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001510 "link_changes", "link_up", "dropped_link_overflow",
Brice Goglincee505d2007-05-07 23:49:25 +02001511 "dropped_link_error_or_filtered",
1512 "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
1513 "dropped_unicast_filtered", "dropped_multicast_filtered",
Brice Goglin0da34b62006-05-23 06:10:15 -04001514 "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
Brice Goglinb53bef82008-05-09 02:20:03 +02001515 "dropped_no_big_buffer"
1516};
1517
1518static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
1519 "----------- slice ---------",
1520 "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
1521 "rx_small_cnt", "rx_big_cnt",
1522 "wake_queue", "stop_queue", "tx_linearized", "LRO aggregated",
1523 "LRO flushed",
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001524 "LRO avg aggr", "LRO no_desc"
Brice Goglin0da34b62006-05-23 06:10:15 -04001525};
1526
1527#define MYRI10GE_NET_STATS_LEN 21
Brice Goglinb53bef82008-05-09 02:20:03 +02001528#define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
1529#define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
Brice Goglin0da34b62006-05-23 06:10:15 -04001530
1531static void
1532myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
1533{
1534 switch (stringset) {
1535 case ETH_SS_STATS:
Brice Goglinb53bef82008-05-09 02:20:03 +02001536 memcpy(data, *myri10ge_gstrings_main_stats,
1537 sizeof(myri10ge_gstrings_main_stats));
1538 data += sizeof(myri10ge_gstrings_main_stats);
1539 memcpy(data, *myri10ge_gstrings_slice_stats,
1540 sizeof(myri10ge_gstrings_slice_stats));
1541 data += sizeof(myri10ge_gstrings_slice_stats);
Brice Goglin0da34b62006-05-23 06:10:15 -04001542 break;
1543 }
1544}
1545
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001546static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
Brice Goglin0da34b62006-05-23 06:10:15 -04001547{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001548 switch (sset) {
1549 case ETH_SS_STATS:
Brice Goglinb53bef82008-05-09 02:20:03 +02001550 return MYRI10GE_MAIN_STATS_LEN + MYRI10GE_SLICE_STATS_LEN;
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001551 default:
1552 return -EOPNOTSUPP;
1553 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001554}
1555
1556static void
1557myri10ge_get_ethtool_stats(struct net_device *netdev,
1558 struct ethtool_stats *stats, u64 * data)
1559{
1560 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglinb53bef82008-05-09 02:20:03 +02001561 struct myri10ge_slice_state *ss;
Brice Goglin0da34b62006-05-23 06:10:15 -04001562 int i;
1563
1564 for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1565 data[i] = ((unsigned long *)&mgp->stats)[i];
1566
Brice Goglinb53bef82008-05-09 02:20:03 +02001567 data[i++] = (unsigned int)mgp->tx_boundary;
Brice Goglin276e26c2007-03-07 20:02:32 +01001568 data[i++] = (unsigned int)mgp->wc_enabled;
Brice Goglin2c1a1082006-07-03 18:16:46 -04001569 data[i++] = (unsigned int)mgp->pdev->irq;
1570 data[i++] = (unsigned int)mgp->msi_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04001571 data[i++] = (unsigned int)mgp->read_dma;
1572 data[i++] = (unsigned int)mgp->write_dma;
1573 data[i++] = (unsigned int)mgp->read_write_dma;
1574 data[i++] = (unsigned int)mgp->serial_number;
Brice Goglin0da34b62006-05-23 06:10:15 -04001575 data[i++] = (unsigned int)mgp->watchdog_resets;
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001576 data[i++] = (unsigned int)mgp->link_changes;
Brice Goglinb53bef82008-05-09 02:20:03 +02001577
1578 /* firmware stats are useful only in the first slice */
1579 ss = &mgp->ss;
1580 data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
1581 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001582 data[i++] =
Brice Goglinb53bef82008-05-09 02:20:03 +02001583 (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
1584 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
1585 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
1586 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
1587 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
Brice Goglincee505d2007-05-07 23:49:25 +02001588 data[i++] =
Brice Goglinb53bef82008-05-09 02:20:03 +02001589 (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
1590 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
1591 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
1592 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
1593 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
1594
1595 data[i++] = 0;
1596 data[i++] = (unsigned int)ss->tx.pkt_start;
1597 data[i++] = (unsigned int)ss->tx.pkt_done;
1598 data[i++] = (unsigned int)ss->tx.req;
1599 data[i++] = (unsigned int)ss->tx.done;
1600 data[i++] = (unsigned int)ss->rx_small.cnt;
1601 data[i++] = (unsigned int)ss->rx_big.cnt;
1602 data[i++] = (unsigned int)ss->tx.wake_queue;
1603 data[i++] = (unsigned int)ss->tx.stop_queue;
1604 data[i++] = (unsigned int)ss->tx.linearized;
1605 data[i++] = ss->rx_done.lro_mgr.stats.aggregated;
1606 data[i++] = ss->rx_done.lro_mgr.stats.flushed;
1607 if (ss->rx_done.lro_mgr.stats.flushed)
1608 data[i++] = ss->rx_done.lro_mgr.stats.aggregated /
1609 ss->rx_done.lro_mgr.stats.flushed;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001610 else
1611 data[i++] = 0;
Brice Goglinb53bef82008-05-09 02:20:03 +02001612 data[i++] = ss->rx_done.lro_mgr.stats.no_desc;
Brice Goglin0da34b62006-05-23 06:10:15 -04001613}
1614
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001615static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
1616{
1617 struct myri10ge_priv *mgp = netdev_priv(netdev);
1618 mgp->msg_enable = value;
1619}
1620
1621static u32 myri10ge_get_msglevel(struct net_device *netdev)
1622{
1623 struct myri10ge_priv *mgp = netdev_priv(netdev);
1624 return mgp->msg_enable;
1625}
1626
Jeff Garzik7282d492006-09-13 14:30:00 -04001627static const struct ethtool_ops myri10ge_ethtool_ops = {
Brice Goglin0da34b62006-05-23 06:10:15 -04001628 .get_settings = myri10ge_get_settings,
1629 .get_drvinfo = myri10ge_get_drvinfo,
1630 .get_coalesce = myri10ge_get_coalesce,
1631 .set_coalesce = myri10ge_set_coalesce,
1632 .get_pauseparam = myri10ge_get_pauseparam,
1633 .set_pauseparam = myri10ge_set_pauseparam,
1634 .get_ringparam = myri10ge_get_ringparam,
1635 .get_rx_csum = myri10ge_get_rx_csum,
1636 .set_rx_csum = myri10ge_set_rx_csum,
Brice Goglinb10c0662006-06-08 10:25:00 -04001637 .set_tx_csum = ethtool_op_set_tx_hw_csum,
Brice Goglin0da34b62006-05-23 06:10:15 -04001638 .set_sg = ethtool_op_set_sg,
Brice Goglin4f93fde2007-10-13 12:34:01 +02001639 .set_tso = myri10ge_set_tso,
Brice Goglin6ffdd072007-05-30 21:13:59 +02001640 .get_link = ethtool_op_get_link,
Brice Goglin0da34b62006-05-23 06:10:15 -04001641 .get_strings = myri10ge_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001642 .get_sset_count = myri10ge_get_sset_count,
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001643 .get_ethtool_stats = myri10ge_get_ethtool_stats,
1644 .set_msglevel = myri10ge_set_msglevel,
1645 .get_msglevel = myri10ge_get_msglevel
Brice Goglin0da34b62006-05-23 06:10:15 -04001646};
1647
Brice Goglinb53bef82008-05-09 02:20:03 +02001648static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
Brice Goglin0da34b62006-05-23 06:10:15 -04001649{
Brice Goglinb53bef82008-05-09 02:20:03 +02001650 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001651 struct myri10ge_cmd cmd;
Brice Goglinb53bef82008-05-09 02:20:03 +02001652 struct net_device *dev = mgp->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -04001653 int tx_ring_size, rx_ring_size;
1654 int tx_ring_entries, rx_ring_entries;
1655 int i, status;
1656 size_t bytes;
1657
Brice Goglin0da34b62006-05-23 06:10:15 -04001658 /* get ring sizes */
Brice Goglin0da34b62006-05-23 06:10:15 -04001659 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
1660 tx_ring_size = cmd.data0;
1661 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
Brice Goglin355c7262007-03-07 19:59:52 +01001662 if (status != 0)
1663 return status;
Brice Goglin0da34b62006-05-23 06:10:15 -04001664 rx_ring_size = cmd.data0;
1665
1666 tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
1667 rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
Brice Goglinb53bef82008-05-09 02:20:03 +02001668 ss->tx.mask = tx_ring_entries - 1;
1669 ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001670
Brice Goglin355c7262007-03-07 19:59:52 +01001671 status = -ENOMEM;
1672
Brice Goglin0da34b62006-05-23 06:10:15 -04001673 /* allocate the host shadow rings */
1674
1675 bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
Brice Goglinb53bef82008-05-09 02:20:03 +02001676 * sizeof(*ss->tx.req_list);
1677 ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
1678 if (ss->tx.req_bytes == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001679 goto abort_with_nothing;
1680
1681 /* ensure req_list entries are aligned to 8 bytes */
Brice Goglinb53bef82008-05-09 02:20:03 +02001682 ss->tx.req_list = (struct mcp_kreq_ether_send *)
1683 ALIGN((unsigned long)ss->tx.req_bytes, 8);
Brice Goglin0da34b62006-05-23 06:10:15 -04001684
Brice Goglinb53bef82008-05-09 02:20:03 +02001685 bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
1686 ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
1687 if (ss->rx_small.shadow == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001688 goto abort_with_tx_req_bytes;
1689
Brice Goglinb53bef82008-05-09 02:20:03 +02001690 bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
1691 ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
1692 if (ss->rx_big.shadow == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001693 goto abort_with_rx_small_shadow;
1694
1695 /* allocate the host info rings */
1696
Brice Goglinb53bef82008-05-09 02:20:03 +02001697 bytes = tx_ring_entries * sizeof(*ss->tx.info);
1698 ss->tx.info = kzalloc(bytes, GFP_KERNEL);
1699 if (ss->tx.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001700 goto abort_with_rx_big_shadow;
1701
Brice Goglinb53bef82008-05-09 02:20:03 +02001702 bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
1703 ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
1704 if (ss->rx_small.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001705 goto abort_with_tx_info;
1706
Brice Goglinb53bef82008-05-09 02:20:03 +02001707 bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
1708 ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
1709 if (ss->rx_big.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001710 goto abort_with_rx_small_info;
1711
1712 /* Fill the receive rings */
Brice Goglinb53bef82008-05-09 02:20:03 +02001713 ss->rx_big.cnt = 0;
1714 ss->rx_small.cnt = 0;
1715 ss->rx_big.fill_cnt = 0;
1716 ss->rx_small.fill_cnt = 0;
1717 ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
1718 ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
1719 ss->rx_small.watchdog_needed = 0;
1720 ss->rx_big.watchdog_needed = 0;
1721 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
Brice Goglinc7dab992006-12-11 11:25:42 +01001722 mgp->small_bytes + MXGEFW_PAD, 0);
Brice Goglin0da34b62006-05-23 06:10:15 -04001723
Brice Goglinb53bef82008-05-09 02:20:03 +02001724 if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
Brice Goglinc7dab992006-12-11 11:25:42 +01001725 printk(KERN_ERR "myri10ge: %s: alloced only %d small bufs\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02001726 dev->name, ss->rx_small.fill_cnt);
Brice Goglinc7dab992006-12-11 11:25:42 +01001727 goto abort_with_rx_small_ring;
Brice Goglin0da34b62006-05-23 06:10:15 -04001728 }
1729
Brice Goglinb53bef82008-05-09 02:20:03 +02001730 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
1731 if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
Brice Goglinc7dab992006-12-11 11:25:42 +01001732 printk(KERN_ERR "myri10ge: %s: alloced only %d big bufs\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02001733 dev->name, ss->rx_big.fill_cnt);
Brice Goglinc7dab992006-12-11 11:25:42 +01001734 goto abort_with_rx_big_ring;
Brice Goglin0da34b62006-05-23 06:10:15 -04001735 }
1736
1737 return 0;
1738
1739abort_with_rx_big_ring:
Brice Goglinb53bef82008-05-09 02:20:03 +02001740 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
1741 int idx = i & ss->rx_big.mask;
1742 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001743 mgp->big_bytes);
Brice Goglinb53bef82008-05-09 02:20:03 +02001744 put_page(ss->rx_big.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001745 }
1746
1747abort_with_rx_small_ring:
Brice Goglinb53bef82008-05-09 02:20:03 +02001748 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
1749 int idx = i & ss->rx_small.mask;
1750 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001751 mgp->small_bytes + MXGEFW_PAD);
Brice Goglinb53bef82008-05-09 02:20:03 +02001752 put_page(ss->rx_small.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001753 }
Brice Goglinc7dab992006-12-11 11:25:42 +01001754
Brice Goglinb53bef82008-05-09 02:20:03 +02001755 kfree(ss->rx_big.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001756
1757abort_with_rx_small_info:
Brice Goglinb53bef82008-05-09 02:20:03 +02001758 kfree(ss->rx_small.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001759
1760abort_with_tx_info:
Brice Goglinb53bef82008-05-09 02:20:03 +02001761 kfree(ss->tx.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001762
1763abort_with_rx_big_shadow:
Brice Goglinb53bef82008-05-09 02:20:03 +02001764 kfree(ss->rx_big.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001765
1766abort_with_rx_small_shadow:
Brice Goglinb53bef82008-05-09 02:20:03 +02001767 kfree(ss->rx_small.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001768
1769abort_with_tx_req_bytes:
Brice Goglinb53bef82008-05-09 02:20:03 +02001770 kfree(ss->tx.req_bytes);
1771 ss->tx.req_bytes = NULL;
1772 ss->tx.req_list = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04001773
1774abort_with_nothing:
1775 return status;
1776}
1777
Brice Goglinb53bef82008-05-09 02:20:03 +02001778static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
Brice Goglin0da34b62006-05-23 06:10:15 -04001779{
Brice Goglinb53bef82008-05-09 02:20:03 +02001780 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001781 struct sk_buff *skb;
1782 struct myri10ge_tx_buf *tx;
1783 int i, len, idx;
1784
Brice Goglinb53bef82008-05-09 02:20:03 +02001785 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
1786 idx = i & ss->rx_big.mask;
1787 if (i == ss->rx_big.fill_cnt - 1)
1788 ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
1789 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001790 mgp->big_bytes);
Brice Goglinb53bef82008-05-09 02:20:03 +02001791 put_page(ss->rx_big.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001792 }
1793
Brice Goglinb53bef82008-05-09 02:20:03 +02001794 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
1795 idx = i & ss->rx_small.mask;
1796 if (i == ss->rx_small.fill_cnt - 1)
1797 ss->rx_small.info[idx].page_offset =
Brice Goglinc7dab992006-12-11 11:25:42 +01001798 MYRI10GE_ALLOC_SIZE;
Brice Goglinb53bef82008-05-09 02:20:03 +02001799 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01001800 mgp->small_bytes + MXGEFW_PAD);
Brice Goglinb53bef82008-05-09 02:20:03 +02001801 put_page(ss->rx_small.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04001802 }
Brice Goglinb53bef82008-05-09 02:20:03 +02001803 tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001804 while (tx->done != tx->req) {
1805 idx = tx->done & tx->mask;
1806 skb = tx->info[idx].skb;
1807
1808 /* Mark as free */
1809 tx->info[idx].skb = NULL;
1810 tx->done++;
1811 len = pci_unmap_len(&tx->info[idx], len);
1812 pci_unmap_len_set(&tx->info[idx], len, 0);
1813 if (skb) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001814 ss->stats.tx_dropped++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001815 dev_kfree_skb_any(skb);
1816 if (len)
1817 pci_unmap_single(mgp->pdev,
1818 pci_unmap_addr(&tx->info[idx],
1819 bus), len,
1820 PCI_DMA_TODEVICE);
1821 } else {
1822 if (len)
1823 pci_unmap_page(mgp->pdev,
1824 pci_unmap_addr(&tx->info[idx],
1825 bus), len,
1826 PCI_DMA_TODEVICE);
1827 }
1828 }
Brice Goglinb53bef82008-05-09 02:20:03 +02001829 kfree(ss->rx_big.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001830
Brice Goglinb53bef82008-05-09 02:20:03 +02001831 kfree(ss->rx_small.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001832
Brice Goglinb53bef82008-05-09 02:20:03 +02001833 kfree(ss->tx.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04001834
Brice Goglinb53bef82008-05-09 02:20:03 +02001835 kfree(ss->rx_big.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001836
Brice Goglinb53bef82008-05-09 02:20:03 +02001837 kfree(ss->rx_small.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001838
Brice Goglinb53bef82008-05-09 02:20:03 +02001839 kfree(ss->tx.req_bytes);
1840 ss->tx.req_bytes = NULL;
1841 ss->tx.req_list = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04001842}
1843
Brice Goglindf30a742006-12-18 11:50:40 +01001844static int myri10ge_request_irq(struct myri10ge_priv *mgp)
1845{
1846 struct pci_dev *pdev = mgp->pdev;
1847 int status;
1848
1849 if (myri10ge_msi) {
1850 status = pci_enable_msi(pdev);
1851 if (status != 0)
1852 dev_err(&pdev->dev,
1853 "Error %d setting up MSI; falling back to xPIC\n",
1854 status);
1855 else
1856 mgp->msi_enabled = 1;
1857 } else {
1858 mgp->msi_enabled = 0;
1859 }
1860 status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
1861 mgp->dev->name, mgp);
1862 if (status != 0) {
1863 dev_err(&pdev->dev, "failed to allocate IRQ\n");
1864 if (mgp->msi_enabled)
1865 pci_disable_msi(pdev);
1866 }
1867 return status;
1868}
1869
1870static void myri10ge_free_irq(struct myri10ge_priv *mgp)
1871{
1872 struct pci_dev *pdev = mgp->pdev;
1873
1874 free_irq(pdev->irq, mgp);
1875 if (mgp->msi_enabled)
1876 pci_disable_msi(pdev);
1877}
1878
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001879static int
1880myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
1881 void **ip_hdr, void **tcpudp_hdr,
1882 u64 * hdr_flags, void *priv)
1883{
1884 struct ethhdr *eh;
1885 struct vlan_ethhdr *veh;
1886 struct iphdr *iph;
1887 u8 *va = page_address(frag->page) + frag->page_offset;
1888 unsigned long ll_hlen;
Al Viro66341ff2007-12-22 18:56:43 +00001889 /* passed opaque through lro_receive_frags() */
1890 __wsum csum = (__force __wsum) (unsigned long)priv;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001891
1892 /* find the mac header, aborting if not IPv4 */
1893
1894 eh = (struct ethhdr *)va;
1895 *mac_hdr = eh;
1896 ll_hlen = ETH_HLEN;
1897 if (eh->h_proto != htons(ETH_P_IP)) {
1898 if (eh->h_proto == htons(ETH_P_8021Q)) {
1899 veh = (struct vlan_ethhdr *)va;
1900 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
1901 return -1;
1902
1903 ll_hlen += VLAN_HLEN;
1904
1905 /*
1906 * HW checksum starts ETH_HLEN bytes into
1907 * frame, so we must subtract off the VLAN
1908 * header's checksum before csum can be used
1909 */
1910 csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
1911 VLAN_HLEN, 0));
1912 } else {
1913 return -1;
1914 }
1915 }
1916 *hdr_flags = LRO_IPV4;
1917
1918 iph = (struct iphdr *)(va + ll_hlen);
1919 *ip_hdr = iph;
1920 if (iph->protocol != IPPROTO_TCP)
1921 return -1;
1922 *hdr_flags |= LRO_TCP;
1923 *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
1924
1925 /* verify the IP checksum */
1926 if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
1927 return -1;
1928
1929 /* verify the checksum */
1930 if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
1931 ntohs(iph->tot_len) - (iph->ihl << 2),
1932 IPPROTO_TCP, csum)))
1933 return -1;
1934
1935 return 0;
1936}
1937
Brice Goglin0da34b62006-05-23 06:10:15 -04001938static int myri10ge_open(struct net_device *dev)
1939{
Brice Goglinb53bef82008-05-09 02:20:03 +02001940 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04001941 struct myri10ge_cmd cmd;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001942 struct net_lro_mgr *lro_mgr;
Brice Goglin0da34b62006-05-23 06:10:15 -04001943 int status, big_pow2;
1944
Brice Goglin0da34b62006-05-23 06:10:15 -04001945 if (mgp->running != MYRI10GE_ETH_STOPPED)
1946 return -EBUSY;
1947
1948 mgp->running = MYRI10GE_ETH_STARTING;
1949 status = myri10ge_reset(mgp);
1950 if (status != 0) {
1951 printk(KERN_ERR "myri10ge: %s: failed reset\n", dev->name);
Brice Goglindf30a742006-12-18 11:50:40 +01001952 goto abort_with_nothing;
Brice Goglin0da34b62006-05-23 06:10:15 -04001953 }
1954
Brice Goglindf30a742006-12-18 11:50:40 +01001955 status = myri10ge_request_irq(mgp);
1956 if (status != 0)
1957 goto abort_with_nothing;
1958
Brice Goglin0da34b62006-05-23 06:10:15 -04001959 /* decide what small buffer size to use. For good TCP rx
1960 * performance, it is important to not receive 1514 byte
1961 * frames into jumbo buffers, as it confuses the socket buffer
1962 * accounting code, leading to drops and erratic performance.
1963 */
1964
1965 if (dev->mtu <= ETH_DATA_LEN)
Brice Goglinc7dab992006-12-11 11:25:42 +01001966 /* enough for a TCP header */
1967 mgp->small_bytes = (128 > SMP_CACHE_BYTES)
1968 ? (128 - MXGEFW_PAD)
1969 : (SMP_CACHE_BYTES - MXGEFW_PAD);
Brice Goglin0da34b62006-05-23 06:10:15 -04001970 else
Brice Goglinde3c4502006-12-11 11:26:38 +01001971 /* enough for a vlan encapsulated ETH_DATA_LEN frame */
1972 mgp->small_bytes = VLAN_ETH_FRAME_LEN;
Brice Goglin0da34b62006-05-23 06:10:15 -04001973
1974 /* Override the small buffer size? */
1975 if (myri10ge_small_bytes > 0)
1976 mgp->small_bytes = myri10ge_small_bytes;
1977
Brice Goglin0da34b62006-05-23 06:10:15 -04001978 /* get the lanai pointers to the send and receive rings */
1979
1980 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001981 mgp->ss.tx.lanai =
Brice Goglin0da34b62006-05-23 06:10:15 -04001982 (struct mcp_kreq_ether_send __iomem *)(mgp->sram + cmd.data0);
1983
1984 status |=
1985 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001986 mgp->ss.rx_small.lanai =
Brice Goglin0da34b62006-05-23 06:10:15 -04001987 (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
1988
1989 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001990 mgp->ss.rx_big.lanai =
Brice Goglin0da34b62006-05-23 06:10:15 -04001991 (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);
1992
1993 if (status != 0) {
1994 printk(KERN_ERR
1995 "myri10ge: %s: failed to get ring sizes or locations\n",
1996 dev->name);
1997 mgp->running = MYRI10GE_ETH_STOPPED;
Brice Goglindf30a742006-12-18 11:50:40 +01001998 goto abort_with_irq;
Brice Goglin0da34b62006-05-23 06:10:15 -04001999 }
2000
Brice Goglin276e26c2007-03-07 20:02:32 +01002001 if (myri10ge_wcfifo && mgp->wc_enabled) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002002 mgp->ss.tx.wc_fifo = (u8 __iomem *) mgp->sram + MXGEFW_ETH_SEND_4;
2003 mgp->ss.rx_small.wc_fifo =
Brice Gogline700f9f2006-08-14 17:52:54 -04002004 (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_SMALL;
Brice Goglinb53bef82008-05-09 02:20:03 +02002005 mgp->ss.rx_big.wc_fifo =
Brice Gogline700f9f2006-08-14 17:52:54 -04002006 (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_BIG;
Brice Goglin0da34b62006-05-23 06:10:15 -04002007 } else {
Brice Goglinb53bef82008-05-09 02:20:03 +02002008 mgp->ss.tx.wc_fifo = NULL;
2009 mgp->ss.rx_small.wc_fifo = NULL;
2010 mgp->ss.rx_big.wc_fifo = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04002011 }
2012
Brice Goglin0da34b62006-05-23 06:10:15 -04002013 /* Firmware needs the big buff size as a power of 2. Lie and
2014 * tell him the buffer is larger, because we only use 1
2015 * buffer/pkt, and the mtu will prevent overruns.
2016 */
Brice Goglin13348be2006-12-11 11:27:19 +01002017 big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
Brice Goglinc7dab992006-12-11 11:25:42 +01002018 if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
vignesh babu199126a2007-07-09 11:50:22 -07002019 while (!is_power_of_2(big_pow2))
Brice Goglinc7dab992006-12-11 11:25:42 +01002020 big_pow2++;
Brice Goglin13348be2006-12-11 11:27:19 +01002021 mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
Brice Goglinc7dab992006-12-11 11:25:42 +01002022 } else {
2023 big_pow2 = MYRI10GE_ALLOC_SIZE;
2024 mgp->big_bytes = big_pow2;
2025 }
2026
Brice Goglinb53bef82008-05-09 02:20:03 +02002027 status = myri10ge_allocate_rings(&mgp->ss);
Brice Goglinc7dab992006-12-11 11:25:42 +01002028 if (status != 0)
Brice Goglindf30a742006-12-18 11:50:40 +01002029 goto abort_with_irq;
Brice Goglin0da34b62006-05-23 06:10:15 -04002030
2031 /* now give firmware buffers sizes, and MTU */
2032 cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
2033 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
2034 cmd.data0 = mgp->small_bytes;
2035 status |=
2036 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
2037 cmd.data0 = big_pow2;
2038 status |=
2039 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
2040 if (status) {
2041 printk(KERN_ERR "myri10ge: %s: Couldn't set buffer sizes\n",
2042 dev->name);
2043 goto abort_with_rings;
2044 }
2045
Brice Goglinb53bef82008-05-09 02:20:03 +02002046 cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->ss.fw_stats_bus);
2047 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->ss.fw_stats_bus);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002048 cmd.data2 = sizeof(struct mcp_irq_data);
2049 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
2050 if (status == -ENOSYS) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002051 dma_addr_t bus = mgp->ss.fw_stats_bus;
Brice Goglin85a7ea12006-08-21 17:36:56 -04002052 bus += offsetof(struct mcp_irq_data, send_done_count);
2053 cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
2054 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
2055 status = myri10ge_send_cmd(mgp,
2056 MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
2057 &cmd, 0);
2058 /* Firmware cannot support multicast without STATS_DMA_V2 */
2059 mgp->fw_multicast_support = 0;
2060 } else {
2061 mgp->fw_multicast_support = 1;
2062 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002063 if (status) {
2064 printk(KERN_ERR "myri10ge: %s: Couldn't set stats DMA\n",
2065 dev->name);
2066 goto abort_with_rings;
2067 }
2068
Al Viro66341ff2007-12-22 18:56:43 +00002069 mgp->link_state = ~0U;
Brice Goglin0da34b62006-05-23 06:10:15 -04002070 mgp->rdma_tags_available = 15;
2071
Brice Goglinb53bef82008-05-09 02:20:03 +02002072 lro_mgr = &mgp->ss.rx_done.lro_mgr;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002073 lro_mgr->dev = dev;
2074 lro_mgr->features = LRO_F_NAPI;
2075 lro_mgr->ip_summed = CHECKSUM_COMPLETE;
2076 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
2077 lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
Brice Goglinb53bef82008-05-09 02:20:03 +02002078 lro_mgr->lro_arr = mgp->ss.rx_done.lro_desc;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002079 lro_mgr->get_frag_header = myri10ge_get_frag_header;
2080 lro_mgr->max_aggr = myri10ge_lro_max_pkts;
Andrew Gallatin621544e2007-12-05 02:31:42 -08002081 lro_mgr->frag_align_pad = 2;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002082 if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
2083 lro_mgr->max_aggr = MAX_SKB_FRAGS;
2084
Brice Goglinb53bef82008-05-09 02:20:03 +02002085 napi_enable(&mgp->ss.napi); /* must happen prior to any irq */
Brice Goglin0da34b62006-05-23 06:10:15 -04002086
2087 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
2088 if (status) {
2089 printk(KERN_ERR "myri10ge: %s: Couldn't bring up link\n",
2090 dev->name);
2091 goto abort_with_rings;
2092 }
2093
Brice Goglinb53bef82008-05-09 02:20:03 +02002094 mgp->ss.tx.wake_queue = 0;
2095 mgp->ss.tx.stop_queue = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04002096 mgp->running = MYRI10GE_ETH_RUNNING;
2097 mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
2098 add_timer(&mgp->watchdog_timer);
2099 netif_wake_queue(dev);
2100 return 0;
2101
2102abort_with_rings:
Brice Goglinb53bef82008-05-09 02:20:03 +02002103 myri10ge_free_rings(&mgp->ss);
Brice Goglin0da34b62006-05-23 06:10:15 -04002104
Brice Goglindf30a742006-12-18 11:50:40 +01002105abort_with_irq:
2106 myri10ge_free_irq(mgp);
2107
Brice Goglin0da34b62006-05-23 06:10:15 -04002108abort_with_nothing:
2109 mgp->running = MYRI10GE_ETH_STOPPED;
2110 return -ENOMEM;
2111}
2112
2113static int myri10ge_close(struct net_device *dev)
2114{
Brice Goglinb53bef82008-05-09 02:20:03 +02002115 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002116 struct myri10ge_cmd cmd;
2117 int status, old_down_cnt;
2118
Brice Goglin0da34b62006-05-23 06:10:15 -04002119 if (mgp->running != MYRI10GE_ETH_RUNNING)
2120 return 0;
2121
Brice Goglinb53bef82008-05-09 02:20:03 +02002122 if (mgp->ss.tx.req_bytes == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04002123 return 0;
2124
2125 del_timer_sync(&mgp->watchdog_timer);
2126 mgp->running = MYRI10GE_ETH_STOPPING;
Brice Goglinb53bef82008-05-09 02:20:03 +02002127 napi_disable(&mgp->ss.napi);
Brice Goglin0da34b62006-05-23 06:10:15 -04002128 netif_carrier_off(dev);
2129 netif_stop_queue(dev);
2130 old_down_cnt = mgp->down_cnt;
2131 mb();
2132 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
2133 if (status)
2134 printk(KERN_ERR "myri10ge: %s: Couldn't bring down link\n",
2135 dev->name);
2136
2137 wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt, HZ);
2138 if (old_down_cnt == mgp->down_cnt)
2139 printk(KERN_ERR "myri10ge: %s never got down irq\n", dev->name);
2140
2141 netif_tx_disable(dev);
Brice Goglindf30a742006-12-18 11:50:40 +01002142 myri10ge_free_irq(mgp);
Brice Goglinb53bef82008-05-09 02:20:03 +02002143 myri10ge_free_rings(&mgp->ss);
Brice Goglin0da34b62006-05-23 06:10:15 -04002144
2145 mgp->running = MYRI10GE_ETH_STOPPED;
2146 return 0;
2147}
2148
2149/* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2150 * backwards one at a time and handle ring wraps */
2151
2152static inline void
2153myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
2154 struct mcp_kreq_ether_send *src, int cnt)
2155{
2156 int idx, starting_slot;
2157 starting_slot = tx->req;
2158 while (cnt > 1) {
2159 cnt--;
2160 idx = (starting_slot + cnt) & tx->mask;
2161 myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
2162 mb();
2163 }
2164}
2165
2166/*
2167 * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2168 * at most 32 bytes at a time, so as to avoid involving the software
2169 * pio handler in the nic. We re-write the first segment's flags
2170 * to mark them valid only after writing the entire chain.
2171 */
2172
2173static inline void
2174myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
2175 int cnt)
2176{
2177 int idx, i;
2178 struct mcp_kreq_ether_send __iomem *dstp, *dst;
2179 struct mcp_kreq_ether_send *srcp;
2180 u8 last_flags;
2181
2182 idx = tx->req & tx->mask;
2183
2184 last_flags = src->flags;
2185 src->flags = 0;
2186 mb();
2187 dst = dstp = &tx->lanai[idx];
2188 srcp = src;
2189
2190 if ((idx + cnt) < tx->mask) {
2191 for (i = 0; i < (cnt - 1); i += 2) {
2192 myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
2193 mb(); /* force write every 32 bytes */
2194 srcp += 2;
2195 dstp += 2;
2196 }
2197 } else {
2198 /* submit all but the first request, and ensure
2199 * that it is submitted below */
2200 myri10ge_submit_req_backwards(tx, src, cnt);
2201 i = 0;
2202 }
2203 if (i < cnt) {
2204 /* submit the first request */
2205 myri10ge_pio_copy(dstp, srcp, sizeof(*src));
2206 mb(); /* barrier before setting valid flag */
2207 }
2208
2209 /* re-write the last 32-bits with the valid flags */
2210 src->flags = last_flags;
Al Viro40f6cff2006-11-20 13:48:32 -05002211 put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
Brice Goglin0da34b62006-05-23 06:10:15 -04002212 tx->req += cnt;
2213 mb();
2214}
2215
2216static inline void
2217myri10ge_submit_req_wc(struct myri10ge_tx_buf *tx,
2218 struct mcp_kreq_ether_send *src, int cnt)
2219{
2220 tx->req += cnt;
2221 mb();
2222 while (cnt >= 4) {
2223 myri10ge_pio_copy(tx->wc_fifo, src, 64);
2224 mb();
2225 src += 4;
2226 cnt -= 4;
2227 }
2228 if (cnt > 0) {
2229 /* pad it to 64 bytes. The src is 64 bytes bigger than it
2230 * needs to be so that we don't overrun it */
Brice Gogline700f9f2006-08-14 17:52:54 -04002231 myri10ge_pio_copy(tx->wc_fifo + MXGEFW_ETH_SEND_OFFSET(cnt),
2232 src, 64);
Brice Goglin0da34b62006-05-23 06:10:15 -04002233 mb();
2234 }
2235}
2236
2237/*
2238 * Transmit a packet. We need to split the packet so that a single
Brice Goglinb53bef82008-05-09 02:20:03 +02002239 * segment does not cross myri10ge->tx_boundary, so this makes segment
Brice Goglin0da34b62006-05-23 06:10:15 -04002240 * counting tricky. So rather than try to count segments up front, we
2241 * just give up if there are too few segments to hold a reasonably
2242 * fragmented packet currently available. If we run
2243 * out of segments while preparing a packet for DMA, we just linearize
2244 * it and try again.
2245 */
2246
2247static int myri10ge_xmit(struct sk_buff *skb, struct net_device *dev)
2248{
2249 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglinb53bef82008-05-09 02:20:03 +02002250 struct myri10ge_slice_state *ss;
Brice Goglin0da34b62006-05-23 06:10:15 -04002251 struct mcp_kreq_ether_send *req;
Brice Goglinb53bef82008-05-09 02:20:03 +02002252 struct myri10ge_tx_buf *tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002253 struct skb_frag_struct *frag;
2254 dma_addr_t bus;
Al Viro40f6cff2006-11-20 13:48:32 -05002255 u32 low;
2256 __be32 high_swapped;
Brice Goglin0da34b62006-05-23 06:10:15 -04002257 unsigned int len;
2258 int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
2259 u16 pseudo_hdr_offset, cksum_offset;
2260 int cum_len, seglen, boundary, rdma_count;
2261 u8 flags, odd_flag;
2262
Brice Goglinb53bef82008-05-09 02:20:03 +02002263 /* always transmit through slot 0 */
2264 ss = &mgp->ss;
2265 tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002266again:
2267 req = tx->req_list;
2268 avail = tx->mask - 1 - (tx->req - tx->done);
2269
2270 mss = 0;
2271 max_segments = MXGEFW_MAX_SEND_DESC;
2272
Brice Goglin917690c2007-03-27 21:54:53 +02002273 if (skb_is_gso(skb)) {
Herbert Xu79671682006-06-22 02:40:14 -07002274 mss = skb_shinfo(skb)->gso_size;
Brice Goglin917690c2007-03-27 21:54:53 +02002275 max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
Brice Goglin0da34b62006-05-23 06:10:15 -04002276 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002277
2278 if ((unlikely(avail < max_segments))) {
2279 /* we are out of transmit resources */
Brice Goglinb53bef82008-05-09 02:20:03 +02002280 tx->stop_queue++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002281 netif_stop_queue(dev);
2282 return 1;
2283 }
2284
2285 /* Setup checksum offloading, if needed */
2286 cksum_offset = 0;
2287 pseudo_hdr_offset = 0;
2288 odd_flag = 0;
2289 flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
Patrick McHardy84fa7932006-08-29 16:44:56 -07002290 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002291 cksum_offset = skb_transport_offset(skb);
Al Viroff1dcad2006-11-20 18:07:29 -08002292 pseudo_hdr_offset = cksum_offset + skb->csum_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -04002293 /* If the headers are excessively large, then we must
2294 * fall back to a software checksum */
Brice Goglin4f93fde2007-10-13 12:34:01 +02002295 if (unlikely(!mss && (cksum_offset > 255 ||
2296 pseudo_hdr_offset > 127))) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07002297 if (skb_checksum_help(skb))
Brice Goglin0da34b62006-05-23 06:10:15 -04002298 goto drop;
2299 cksum_offset = 0;
2300 pseudo_hdr_offset = 0;
2301 } else {
Brice Goglin0da34b62006-05-23 06:10:15 -04002302 odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
2303 flags |= MXGEFW_FLAGS_CKSUM;
2304 }
2305 }
2306
2307 cum_len = 0;
2308
Brice Goglin0da34b62006-05-23 06:10:15 -04002309 if (mss) { /* TSO */
2310 /* this removes any CKSUM flag from before */
2311 flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
2312
2313 /* negative cum_len signifies to the
2314 * send loop that we are still in the
2315 * header portion of the TSO packet.
Brice Goglin4f93fde2007-10-13 12:34:01 +02002316 * TSO header can be at most 1KB long */
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002317 cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
Brice Goglin0da34b62006-05-23 06:10:15 -04002318
Brice Goglin4f93fde2007-10-13 12:34:01 +02002319 /* for IPv6 TSO, the checksum offset stores the
2320 * TCP header length, to save the firmware from
2321 * the need to parse the headers */
2322 if (skb_is_gso_v6(skb)) {
2323 cksum_offset = tcp_hdrlen(skb);
2324 /* Can only handle headers <= max_tso6 long */
2325 if (unlikely(-cum_len > mgp->max_tso6))
2326 return myri10ge_sw_tso(skb, dev);
2327 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002328 /* for TSO, pseudo_hdr_offset holds mss.
2329 * The firmware figures out where to put
2330 * the checksum by parsing the header. */
Al Viro40f6cff2006-11-20 13:48:32 -05002331 pseudo_hdr_offset = mss;
Brice Goglin0da34b62006-05-23 06:10:15 -04002332 } else
Brice Goglin0da34b62006-05-23 06:10:15 -04002333 /* Mark small packets, and pad out tiny packets */
2334 if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
2335 flags |= MXGEFW_FLAGS_SMALL;
2336
2337 /* pad frames to at least ETH_ZLEN bytes */
2338 if (unlikely(skb->len < ETH_ZLEN)) {
Herbert Xu5b057c62006-06-23 02:06:41 -07002339 if (skb_padto(skb, ETH_ZLEN)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002340 /* The packet is gone, so we must
2341 * return 0 */
Brice Goglinb53bef82008-05-09 02:20:03 +02002342 ss->stats.tx_dropped += 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04002343 return 0;
2344 }
2345 /* adjust the len to account for the zero pad
2346 * so that the nic can know how long it is */
2347 skb->len = ETH_ZLEN;
2348 }
2349 }
2350
2351 /* map the skb for DMA */
2352 len = skb->len - skb->data_len;
2353 idx = tx->req & tx->mask;
2354 tx->info[idx].skb = skb;
2355 bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
2356 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2357 pci_unmap_len_set(&tx->info[idx], len, len);
2358
2359 frag_cnt = skb_shinfo(skb)->nr_frags;
2360 frag_idx = 0;
2361 count = 0;
2362 rdma_count = 0;
2363
2364 /* "rdma_count" is the number of RDMAs belonging to the
2365 * current packet BEFORE the current send request. For
2366 * non-TSO packets, this is equal to "count".
2367 * For TSO packets, rdma_count needs to be reset
2368 * to 0 after a segment cut.
2369 *
2370 * The rdma_count field of the send request is
2371 * the number of RDMAs of the packet starting at
2372 * that request. For TSO send requests with one ore more cuts
2373 * in the middle, this is the number of RDMAs starting
2374 * after the last cut in the request. All previous
2375 * segments before the last cut implicitly have 1 RDMA.
2376 *
2377 * Since the number of RDMAs is not known beforehand,
2378 * it must be filled-in retroactively - after each
2379 * segmentation cut or at the end of the entire packet.
2380 */
2381
2382 while (1) {
2383 /* Break the SKB or Fragment up into pieces which
Brice Goglinb53bef82008-05-09 02:20:03 +02002384 * do not cross mgp->tx_boundary */
Brice Goglin0da34b62006-05-23 06:10:15 -04002385 low = MYRI10GE_LOWPART_TO_U32(bus);
2386 high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
2387 while (len) {
2388 u8 flags_next;
2389 int cum_len_next;
2390
2391 if (unlikely(count == max_segments))
2392 goto abort_linearize;
2393
Brice Goglinb53bef82008-05-09 02:20:03 +02002394 boundary =
2395 (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04002396 seglen = boundary - low;
2397 if (seglen > len)
2398 seglen = len;
2399 flags_next = flags & ~MXGEFW_FLAGS_FIRST;
2400 cum_len_next = cum_len + seglen;
Brice Goglin0da34b62006-05-23 06:10:15 -04002401 if (mss) { /* TSO */
2402 (req - rdma_count)->rdma_count = rdma_count + 1;
2403
2404 if (likely(cum_len >= 0)) { /* payload */
2405 int next_is_first, chop;
2406
2407 chop = (cum_len_next > mss);
2408 cum_len_next = cum_len_next % mss;
2409 next_is_first = (cum_len_next == 0);
2410 flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
2411 flags_next |= next_is_first *
2412 MXGEFW_FLAGS_FIRST;
2413 rdma_count |= -(chop | next_is_first);
2414 rdma_count += chop & !next_is_first;
2415 } else if (likely(cum_len_next >= 0)) { /* header ends */
2416 int small;
2417
2418 rdma_count = -1;
2419 cum_len_next = 0;
2420 seglen = -cum_len;
2421 small = (mss <= MXGEFW_SEND_SMALL_SIZE);
2422 flags_next = MXGEFW_FLAGS_TSO_PLD |
2423 MXGEFW_FLAGS_FIRST |
2424 (small * MXGEFW_FLAGS_SMALL);
2425 }
2426 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002427 req->addr_high = high_swapped;
2428 req->addr_low = htonl(low);
Al Viro40f6cff2006-11-20 13:48:32 -05002429 req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
Brice Goglin0da34b62006-05-23 06:10:15 -04002430 req->pad = 0; /* complete solid 16-byte block; does this matter? */
2431 req->rdma_count = 1;
2432 req->length = htons(seglen);
2433 req->cksum_offset = cksum_offset;
2434 req->flags = flags | ((cum_len & 1) * odd_flag);
2435
2436 low += seglen;
2437 len -= seglen;
2438 cum_len = cum_len_next;
2439 flags = flags_next;
2440 req++;
2441 count++;
2442 rdma_count++;
Brice Goglin4f93fde2007-10-13 12:34:01 +02002443 if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
2444 if (unlikely(cksum_offset > seglen))
2445 cksum_offset -= seglen;
2446 else
2447 cksum_offset = 0;
2448 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002449 }
2450 if (frag_idx == frag_cnt)
2451 break;
2452
2453 /* map next fragment for DMA */
2454 idx = (count + tx->req) & tx->mask;
2455 frag = &skb_shinfo(skb)->frags[frag_idx];
2456 frag_idx++;
2457 len = frag->size;
2458 bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
2459 len, PCI_DMA_TODEVICE);
2460 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2461 pci_unmap_len_set(&tx->info[idx], len, len);
2462 }
2463
2464 (req - rdma_count)->rdma_count = rdma_count;
Brice Goglin0da34b62006-05-23 06:10:15 -04002465 if (mss)
2466 do {
2467 req--;
2468 req->flags |= MXGEFW_FLAGS_TSO_LAST;
2469 } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
2470 MXGEFW_FLAGS_FIRST)));
Brice Goglin0da34b62006-05-23 06:10:15 -04002471 idx = ((count - 1) + tx->req) & tx->mask;
2472 tx->info[idx].last = 1;
2473 if (tx->wc_fifo == NULL)
2474 myri10ge_submit_req(tx, tx->req_list, count);
2475 else
2476 myri10ge_submit_req_wc(tx, tx->req_list, count);
2477 tx->pkt_start++;
2478 if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002479 tx->stop_queue++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002480 netif_stop_queue(dev);
2481 }
2482 dev->trans_start = jiffies;
2483 return 0;
2484
2485abort_linearize:
2486 /* Free any DMA resources we've alloced and clear out the skb
2487 * slot so as to not trip up assertions, and to avoid a
2488 * double-free if linearizing fails */
2489
2490 last_idx = (idx + 1) & tx->mask;
2491 idx = tx->req & tx->mask;
2492 tx->info[idx].skb = NULL;
2493 do {
2494 len = pci_unmap_len(&tx->info[idx], len);
2495 if (len) {
2496 if (tx->info[idx].skb != NULL)
2497 pci_unmap_single(mgp->pdev,
2498 pci_unmap_addr(&tx->info[idx],
2499 bus), len,
2500 PCI_DMA_TODEVICE);
2501 else
2502 pci_unmap_page(mgp->pdev,
2503 pci_unmap_addr(&tx->info[idx],
2504 bus), len,
2505 PCI_DMA_TODEVICE);
2506 pci_unmap_len_set(&tx->info[idx], len, 0);
2507 tx->info[idx].skb = NULL;
2508 }
2509 idx = (idx + 1) & tx->mask;
2510 } while (idx != last_idx);
Herbert Xu89114af2006-07-08 13:34:32 -07002511 if (skb_is_gso(skb)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002512 printk(KERN_ERR
2513 "myri10ge: %s: TSO but wanted to linearize?!?!?\n",
2514 mgp->dev->name);
2515 goto drop;
2516 }
2517
Andrew Mortonbec0e852006-06-22 14:47:19 -07002518 if (skb_linearize(skb))
Brice Goglin0da34b62006-05-23 06:10:15 -04002519 goto drop;
2520
Brice Goglinb53bef82008-05-09 02:20:03 +02002521 tx->linearized++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002522 goto again;
2523
2524drop:
2525 dev_kfree_skb_any(skb);
Brice Goglinb53bef82008-05-09 02:20:03 +02002526 ss->stats.tx_dropped += 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04002527 return 0;
2528
2529}
2530
Brice Goglin4f93fde2007-10-13 12:34:01 +02002531static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev)
2532{
2533 struct sk_buff *segs, *curr;
Brice Goglinb53bef82008-05-09 02:20:03 +02002534 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin4f93fde2007-10-13 12:34:01 +02002535 int status;
2536
2537 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07002538 if (IS_ERR(segs))
Brice Goglin4f93fde2007-10-13 12:34:01 +02002539 goto drop;
2540
2541 while (segs) {
2542 curr = segs;
2543 segs = segs->next;
2544 curr->next = NULL;
2545 status = myri10ge_xmit(curr, dev);
2546 if (status != 0) {
2547 dev_kfree_skb_any(curr);
2548 if (segs != NULL) {
2549 curr = segs;
2550 segs = segs->next;
2551 curr->next = NULL;
2552 dev_kfree_skb_any(segs);
2553 }
2554 goto drop;
2555 }
2556 }
2557 dev_kfree_skb_any(skb);
2558 return 0;
2559
2560drop:
2561 dev_kfree_skb_any(skb);
2562 mgp->stats.tx_dropped += 1;
2563 return 0;
2564}
2565
Brice Goglin0da34b62006-05-23 06:10:15 -04002566static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
2567{
2568 struct myri10ge_priv *mgp = netdev_priv(dev);
2569 return &mgp->stats;
2570}
2571
2572static void myri10ge_set_multicast_list(struct net_device *dev)
2573{
Brice Goglinb53bef82008-05-09 02:20:03 +02002574 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002575 struct myri10ge_cmd cmd;
Brice Goglin85a7ea12006-08-21 17:36:56 -04002576 struct dev_mc_list *mc_list;
Brice Goglin62502232006-12-11 11:24:37 +01002577 __be32 data[2] = { 0, 0 };
Brice Goglin85a7ea12006-08-21 17:36:56 -04002578 int err;
Joe Perches0795af52007-10-03 17:59:30 -07002579 DECLARE_MAC_BUF(mac);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002580
Brice Goglin0da34b62006-05-23 06:10:15 -04002581 /* can be called from atomic contexts,
2582 * pass 1 to force atomicity in myri10ge_send_cmd() */
Brice Goglin85a7ea12006-08-21 17:36:56 -04002583 myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
2584
2585 /* This firmware is known to not support multicast */
Brice Goglin2f762162007-05-07 23:50:37 +02002586 if (!mgp->fw_multicast_support)
Brice Goglin85a7ea12006-08-21 17:36:56 -04002587 return;
2588
2589 /* Disable multicast filtering */
2590
2591 err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
2592 if (err != 0) {
2593 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_ENABLE_ALLMULTI,"
2594 " error status: %d\n", dev->name, err);
2595 goto abort;
2596 }
2597
Brice Goglin2f762162007-05-07 23:50:37 +02002598 if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
Brice Goglin85a7ea12006-08-21 17:36:56 -04002599 /* request to disable multicast filtering, so quit here */
2600 return;
2601 }
2602
2603 /* Flush the filters */
2604
2605 err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
2606 &cmd, 1);
2607 if (err != 0) {
2608 printk(KERN_ERR
2609 "myri10ge: %s: Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS"
2610 ", error status: %d\n", dev->name, err);
2611 goto abort;
2612 }
2613
2614 /* Walk the multicast list, and add each address */
2615 for (mc_list = dev->mc_list; mc_list != NULL; mc_list = mc_list->next) {
Al Viro40f6cff2006-11-20 13:48:32 -05002616 memcpy(data, &mc_list->dmi_addr, 6);
2617 cmd.data0 = ntohl(data[0]);
2618 cmd.data1 = ntohl(data[1]);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002619 err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
2620 &cmd, 1);
2621
2622 if (err != 0) {
2623 printk(KERN_ERR "myri10ge: %s: Failed "
2624 "MXGEFW_JOIN_MULTICAST_GROUP, error status:"
2625 "%d\t", dev->name, err);
Joe Perches0795af52007-10-03 17:59:30 -07002626 printk(KERN_ERR "MAC %s\n",
2627 print_mac(mac, mc_list->dmi_addr));
Brice Goglin85a7ea12006-08-21 17:36:56 -04002628 goto abort;
2629 }
2630 }
2631 /* Enable multicast filtering */
2632 err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
2633 if (err != 0) {
2634 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_DISABLE_ALLMULTI,"
2635 "error status: %d\n", dev->name, err);
2636 goto abort;
2637 }
2638
2639 return;
2640
2641abort:
2642 return;
Brice Goglin0da34b62006-05-23 06:10:15 -04002643}
2644
2645static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
2646{
2647 struct sockaddr *sa = addr;
2648 struct myri10ge_priv *mgp = netdev_priv(dev);
2649 int status;
2650
2651 if (!is_valid_ether_addr(sa->sa_data))
2652 return -EADDRNOTAVAIL;
2653
2654 status = myri10ge_update_mac_address(mgp, sa->sa_data);
2655 if (status != 0) {
2656 printk(KERN_ERR
2657 "myri10ge: %s: changing mac address failed with %d\n",
2658 dev->name, status);
2659 return status;
2660 }
2661
2662 /* change the dev structure */
2663 memcpy(dev->dev_addr, sa->sa_data, 6);
2664 return 0;
2665}
2666
2667static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
2668{
2669 struct myri10ge_priv *mgp = netdev_priv(dev);
2670 int error = 0;
2671
2672 if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
2673 printk(KERN_ERR "myri10ge: %s: new mtu (%d) is not valid\n",
2674 dev->name, new_mtu);
2675 return -EINVAL;
2676 }
2677 printk(KERN_INFO "%s: changing mtu from %d to %d\n",
2678 dev->name, dev->mtu, new_mtu);
2679 if (mgp->running) {
2680 /* if we change the mtu on an active device, we must
2681 * reset the device so the firmware sees the change */
2682 myri10ge_close(dev);
2683 dev->mtu = new_mtu;
2684 myri10ge_open(dev);
2685 } else
2686 dev->mtu = new_mtu;
2687
2688 return error;
2689}
2690
2691/*
2692 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
2693 * Only do it if the bridge is a root port since we don't want to disturb
2694 * any other device, except if forced with myri10ge_ecrc_enable > 1.
2695 */
2696
Brice Goglin0da34b62006-05-23 06:10:15 -04002697static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
2698{
2699 struct pci_dev *bridge = mgp->pdev->bus->self;
2700 struct device *dev = &mgp->pdev->dev;
2701 unsigned cap;
2702 unsigned err_cap;
2703 u16 val;
2704 u8 ext_type;
2705 int ret;
2706
2707 if (!myri10ge_ecrc_enable || !bridge)
2708 return;
2709
2710 /* check that the bridge is a root port */
2711 cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
2712 pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
2713 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
2714 if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
2715 if (myri10ge_ecrc_enable > 1) {
Brice Goglineca3fd82008-05-09 02:19:29 +02002716 struct pci_dev *prev_bridge, *old_bridge = bridge;
Brice Goglin0da34b62006-05-23 06:10:15 -04002717
2718 /* Walk the hierarchy up to the root port
2719 * where ECRC has to be enabled */
2720 do {
Brice Goglineca3fd82008-05-09 02:19:29 +02002721 prev_bridge = bridge;
Brice Goglin0da34b62006-05-23 06:10:15 -04002722 bridge = bridge->bus->self;
Brice Goglineca3fd82008-05-09 02:19:29 +02002723 if (!bridge || prev_bridge == bridge) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002724 dev_err(dev,
2725 "Failed to find root port"
2726 " to force ECRC\n");
2727 return;
2728 }
2729 cap =
2730 pci_find_capability(bridge, PCI_CAP_ID_EXP);
2731 pci_read_config_word(bridge,
2732 cap + PCI_CAP_FLAGS, &val);
2733 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
2734 } while (ext_type != PCI_EXP_TYPE_ROOT_PORT);
2735
2736 dev_info(dev,
2737 "Forcing ECRC on non-root port %s"
2738 " (enabling on root port %s)\n",
2739 pci_name(old_bridge), pci_name(bridge));
2740 } else {
2741 dev_err(dev,
2742 "Not enabling ECRC on non-root port %s\n",
2743 pci_name(bridge));
2744 return;
2745 }
2746 }
2747
2748 cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
Brice Goglin0da34b62006-05-23 06:10:15 -04002749 if (!cap)
2750 return;
2751
2752 ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
2753 if (ret) {
2754 dev_err(dev, "failed reading ext-conf-space of %s\n",
2755 pci_name(bridge));
2756 dev_err(dev, "\t pci=nommconf in use? "
2757 "or buggy/incomplete/absent ACPI MCFG attr?\n");
2758 return;
2759 }
2760 if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
2761 return;
2762
2763 err_cap |= PCI_ERR_CAP_ECRC_GENE;
2764 pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
2765 dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
Brice Goglin0da34b62006-05-23 06:10:15 -04002766}
2767
2768/*
2769 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
2770 * when the PCI-E Completion packets are aligned on an 8-byte
2771 * boundary. Some PCI-E chip sets always align Completion packets; on
2772 * the ones that do not, the alignment can be enforced by enabling
2773 * ECRC generation (if supported).
2774 *
2775 * When PCI-E Completion packets are not aligned, it is actually more
2776 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
2777 *
2778 * If the driver can neither enable ECRC nor verify that it has
2779 * already been enabled, then it must use a firmware image which works
2780 * around unaligned completion packets (myri10ge_ethp_z8e.dat), and it
2781 * should also ensure that it never gives the device a Read-DMA which is
Brice Goglinb53bef82008-05-09 02:20:03 +02002782 * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
Brice Goglin0da34b62006-05-23 06:10:15 -04002783 * enabled, then the driver should use the aligned (myri10ge_eth_z8e.dat)
Brice Goglinb53bef82008-05-09 02:20:03 +02002784 * firmware image, and set tx_boundary to 4KB.
Brice Goglin0da34b62006-05-23 06:10:15 -04002785 */
2786
Brice Goglin5443e9e2007-05-07 23:52:22 +02002787static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
Brice Goglin0da34b62006-05-23 06:10:15 -04002788{
Brice Goglin5443e9e2007-05-07 23:52:22 +02002789 struct pci_dev *pdev = mgp->pdev;
2790 struct device *dev = &pdev->dev;
Brice Goglin302d2422007-08-24 08:57:17 +02002791 int status;
Brice Goglin0da34b62006-05-23 06:10:15 -04002792
Brice Goglinb53bef82008-05-09 02:20:03 +02002793 mgp->tx_boundary = 4096;
Brice Goglin5443e9e2007-05-07 23:52:22 +02002794 /*
2795 * Verify the max read request size was set to 4KB
2796 * before trying the test with 4KB.
2797 */
Brice Goglin302d2422007-08-24 08:57:17 +02002798 status = pcie_get_readrq(pdev);
2799 if (status < 0) {
Brice Goglin5443e9e2007-05-07 23:52:22 +02002800 dev_err(dev, "Couldn't read max read req size: %d\n", status);
2801 goto abort;
2802 }
Brice Goglin302d2422007-08-24 08:57:17 +02002803 if (status != 4096) {
2804 dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
Brice Goglinb53bef82008-05-09 02:20:03 +02002805 mgp->tx_boundary = 2048;
Brice Goglin5443e9e2007-05-07 23:52:22 +02002806 }
2807 /*
2808 * load the optimized firmware (which assumes aligned PCIe
2809 * completions) in order to see if it works on this host.
2810 */
2811 mgp->fw_name = myri10ge_fw_aligned;
2812 status = myri10ge_load_firmware(mgp);
2813 if (status != 0) {
2814 goto abort;
2815 }
2816
2817 /*
2818 * Enable ECRC if possible
2819 */
2820 myri10ge_enable_ecrc(mgp);
2821
2822 /*
2823 * Run a DMA test which watches for unaligned completions and
2824 * aborts on the first one seen.
2825 */
2826
2827 status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
2828 if (status == 0)
2829 return; /* keep the aligned firmware */
2830
2831 if (status != -E2BIG)
2832 dev_warn(dev, "DMA test failed: %d\n", status);
2833 if (status == -ENOSYS)
2834 dev_warn(dev, "Falling back to ethp! "
2835 "Please install up to date fw\n");
2836abort:
2837 /* fall back to using the unaligned firmware */
Brice Goglinb53bef82008-05-09 02:20:03 +02002838 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -04002839 mgp->fw_name = myri10ge_fw_unaligned;
2840
Brice Goglin5443e9e2007-05-07 23:52:22 +02002841}
2842
2843static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
2844{
Brice Goglin0da34b62006-05-23 06:10:15 -04002845 if (myri10ge_force_firmware == 0) {
Brice Goglince7f9362006-08-31 01:32:59 -04002846 int link_width, exp_cap;
2847 u16 lnk;
2848
2849 exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
2850 pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
2851 link_width = (lnk >> 4) & 0x3f;
2852
Brice Goglince7f9362006-08-31 01:32:59 -04002853 /* Check to see if Link is less than 8 or if the
2854 * upstream bridge is known to provide aligned
2855 * completions */
2856 if (link_width < 8) {
2857 dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
2858 link_width);
Brice Goglinb53bef82008-05-09 02:20:03 +02002859 mgp->tx_boundary = 4096;
Brice Goglince7f9362006-08-31 01:32:59 -04002860 mgp->fw_name = myri10ge_fw_aligned;
Brice Goglin5443e9e2007-05-07 23:52:22 +02002861 } else {
2862 myri10ge_firmware_probe(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -04002863 }
2864 } else {
2865 if (myri10ge_force_firmware == 1) {
2866 dev_info(&mgp->pdev->dev,
2867 "Assuming aligned completions (forced)\n");
Brice Goglinb53bef82008-05-09 02:20:03 +02002868 mgp->tx_boundary = 4096;
Brice Goglin0da34b62006-05-23 06:10:15 -04002869 mgp->fw_name = myri10ge_fw_aligned;
2870 } else {
2871 dev_info(&mgp->pdev->dev,
2872 "Assuming unaligned completions (forced)\n");
Brice Goglinb53bef82008-05-09 02:20:03 +02002873 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -04002874 mgp->fw_name = myri10ge_fw_unaligned;
2875 }
2876 }
2877 if (myri10ge_fw_name != NULL) {
2878 dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
2879 myri10ge_fw_name);
2880 mgp->fw_name = myri10ge_fw_name;
2881 }
2882}
2883
Brice Goglin0da34b62006-05-23 06:10:15 -04002884#ifdef CONFIG_PM
Brice Goglin0da34b62006-05-23 06:10:15 -04002885static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
2886{
2887 struct myri10ge_priv *mgp;
2888 struct net_device *netdev;
2889
2890 mgp = pci_get_drvdata(pdev);
2891 if (mgp == NULL)
2892 return -EINVAL;
2893 netdev = mgp->dev;
2894
2895 netif_device_detach(netdev);
2896 if (netif_running(netdev)) {
2897 printk(KERN_INFO "myri10ge: closing %s\n", netdev->name);
2898 rtnl_lock();
2899 myri10ge_close(netdev);
2900 rtnl_unlock();
2901 }
2902 myri10ge_dummy_rdma(mgp, 0);
Brice Goglin83f6e152006-12-18 11:52:02 +01002903 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002904 pci_disable_device(pdev);
Brice Goglin1a63e842006-12-18 11:52:34 +01002905
2906 return pci_set_power_state(pdev, pci_choose_state(pdev, state));
Brice Goglin0da34b62006-05-23 06:10:15 -04002907}
2908
2909static int myri10ge_resume(struct pci_dev *pdev)
2910{
2911 struct myri10ge_priv *mgp;
2912 struct net_device *netdev;
2913 int status;
2914 u16 vendor;
2915
2916 mgp = pci_get_drvdata(pdev);
2917 if (mgp == NULL)
2918 return -EINVAL;
2919 netdev = mgp->dev;
2920 pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
2921 msleep(5); /* give card time to respond */
2922 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
2923 if (vendor == 0xffff) {
2924 printk(KERN_ERR "myri10ge: %s: device disappeared!\n",
2925 mgp->dev->name);
2926 return -EIO;
2927 }
Brice Goglin83f6e152006-12-18 11:52:02 +01002928
Brice Goglin1a63e842006-12-18 11:52:34 +01002929 status = pci_restore_state(pdev);
2930 if (status)
2931 return status;
Brice Goglin4c2248c2006-07-09 21:10:18 -04002932
2933 status = pci_enable_device(pdev);
Brice Goglin1a63e842006-12-18 11:52:34 +01002934 if (status) {
Brice Goglin4c2248c2006-07-09 21:10:18 -04002935 dev_err(&pdev->dev, "failed to enable device\n");
Brice Goglin1a63e842006-12-18 11:52:34 +01002936 return status;
Brice Goglin4c2248c2006-07-09 21:10:18 -04002937 }
2938
Brice Goglin0da34b62006-05-23 06:10:15 -04002939 pci_set_master(pdev);
2940
Brice Goglin0da34b62006-05-23 06:10:15 -04002941 myri10ge_reset(mgp);
Brice Goglin013b68b2006-08-09 00:07:53 -04002942 myri10ge_dummy_rdma(mgp, 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04002943
2944 /* Save configuration space to be restored if the
2945 * nic resets due to a parity error */
Brice Goglin83f6e152006-12-18 11:52:02 +01002946 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002947
2948 if (netif_running(netdev)) {
2949 rtnl_lock();
Brice Goglindf30a742006-12-18 11:50:40 +01002950 status = myri10ge_open(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002951 rtnl_unlock();
Brice Goglindf30a742006-12-18 11:50:40 +01002952 if (status != 0)
2953 goto abort_with_enabled;
2954
Brice Goglin0da34b62006-05-23 06:10:15 -04002955 }
2956 netif_device_attach(netdev);
2957
2958 return 0;
2959
Brice Goglin4c2248c2006-07-09 21:10:18 -04002960abort_with_enabled:
2961 pci_disable_device(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002962 return -EIO;
2963
2964}
Brice Goglin0da34b62006-05-23 06:10:15 -04002965#endif /* CONFIG_PM */
2966
2967static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
2968{
2969 struct pci_dev *pdev = mgp->pdev;
2970 int vs = mgp->vendor_specific_offset;
2971 u32 reboot;
2972
2973 /*enter read32 mode */
2974 pci_write_config_byte(pdev, vs + 0x10, 0x3);
2975
2976 /*read REBOOT_STATUS (0xfffffff0) */
2977 pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
2978 pci_read_config_dword(pdev, vs + 0x14, &reboot);
2979 return reboot;
2980}
2981
2982/*
2983 * This watchdog is used to check whether the board has suffered
2984 * from a parity error and needs to be recovered.
2985 */
David Howellsc4028952006-11-22 14:57:56 +00002986static void myri10ge_watchdog(struct work_struct *work)
Brice Goglin0da34b62006-05-23 06:10:15 -04002987{
David Howellsc4028952006-11-22 14:57:56 +00002988 struct myri10ge_priv *mgp =
Brice Goglin62502232006-12-11 11:24:37 +01002989 container_of(work, struct myri10ge_priv, watchdog_work);
Brice Goglinb53bef82008-05-09 02:20:03 +02002990 struct myri10ge_tx_buf *tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002991 u32 reboot;
2992 int status;
2993 u16 cmd, vendor;
2994
2995 mgp->watchdog_resets++;
2996 pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
2997 if ((cmd & PCI_COMMAND_MASTER) == 0) {
2998 /* Bus master DMA disabled? Check to see
2999 * if the card rebooted due to a parity error
3000 * For now, just report it */
3001 reboot = myri10ge_read_reboot(mgp);
3002 printk(KERN_ERR
Brice Goglinf1811372007-06-11 20:26:31 +02003003 "myri10ge: %s: NIC rebooted (0x%x),%s resetting\n",
3004 mgp->dev->name, reboot,
3005 myri10ge_reset_recover ? " " : " not");
3006 if (myri10ge_reset_recover == 0)
3007 return;
3008
3009 myri10ge_reset_recover--;
3010
Brice Goglin0da34b62006-05-23 06:10:15 -04003011 /*
3012 * A rebooted nic will come back with config space as
3013 * it was after power was applied to PCIe bus.
3014 * Attempt to restore config space which was saved
3015 * when the driver was loaded, or the last time the
3016 * nic was resumed from power saving mode.
3017 */
Brice Goglin83f6e152006-12-18 11:52:02 +01003018 pci_restore_state(mgp->pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003019
3020 /* save state again for accounting reasons */
Brice Goglin83f6e152006-12-18 11:52:02 +01003021 pci_save_state(mgp->pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003022
Brice Goglin0da34b62006-05-23 06:10:15 -04003023 } else {
3024 /* if we get back -1's from our slot, perhaps somebody
3025 * powered off our card. Don't try to reset it in
3026 * this case */
3027 if (cmd == 0xffff) {
3028 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3029 if (vendor == 0xffff) {
3030 printk(KERN_ERR
3031 "myri10ge: %s: device disappeared!\n",
3032 mgp->dev->name);
3033 return;
3034 }
3035 }
3036 /* Perhaps it is a software error. Try to reset */
3037
3038 printk(KERN_ERR "myri10ge: %s: device timeout, resetting\n",
3039 mgp->dev->name);
Brice Goglinb53bef82008-05-09 02:20:03 +02003040 tx = &mgp->ss.tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04003041 printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02003042 mgp->dev->name, tx->req, tx->done,
3043 tx->pkt_start, tx->pkt_done,
3044 (int)ntohl(mgp->ss.fw_stats->send_done_count));
Brice Goglin0da34b62006-05-23 06:10:15 -04003045 msleep(2000);
3046 printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
Brice Goglinb53bef82008-05-09 02:20:03 +02003047 mgp->dev->name, tx->req, tx->done,
3048 tx->pkt_start, tx->pkt_done,
3049 (int)ntohl(mgp->ss.fw_stats->send_done_count));
Brice Goglin0da34b62006-05-23 06:10:15 -04003050 }
3051 rtnl_lock();
3052 myri10ge_close(mgp->dev);
3053 status = myri10ge_load_firmware(mgp);
3054 if (status != 0)
3055 printk(KERN_ERR "myri10ge: %s: failed to load firmware\n",
3056 mgp->dev->name);
3057 else
3058 myri10ge_open(mgp->dev);
3059 rtnl_unlock();
3060}
3061
3062/*
3063 * We use our own timer routine rather than relying upon
3064 * netdev->tx_timeout because we have a very large hardware transmit
3065 * queue. Due to the large queue, the netdev->tx_timeout function
3066 * cannot detect a NIC with a parity error in a timely fashion if the
3067 * NIC is lightly loaded.
3068 */
3069static void myri10ge_watchdog_timer(unsigned long arg)
3070{
3071 struct myri10ge_priv *mgp;
Brice Goglinb53bef82008-05-09 02:20:03 +02003072 struct myri10ge_slice_state *ss;
Brice Goglin626fda92007-08-09 09:02:14 +02003073 u32 rx_pause_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -04003074
3075 mgp = (struct myri10ge_priv *)arg;
Brice Goglinc7dab992006-12-11 11:25:42 +01003076
Brice Goglinb53bef82008-05-09 02:20:03 +02003077 rx_pause_cnt = ntohl(mgp->ss.fw_stats->dropped_pause);
Brice Goglinc7dab992006-12-11 11:25:42 +01003078
Brice Goglinb53bef82008-05-09 02:20:03 +02003079 ss = &mgp->ss;
3080 if (ss->rx_small.watchdog_needed) {
3081 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
3082 mgp->small_bytes + MXGEFW_PAD, 1);
3083 if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
3084 myri10ge_fill_thresh)
3085 ss->rx_small.watchdog_needed = 0;
3086 }
3087 if (ss->rx_big.watchdog_needed) {
3088 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 1);
3089 if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
3090 myri10ge_fill_thresh)
3091 ss->rx_big.watchdog_needed = 0;
3092 }
3093
3094 if (ss->tx.req != ss->tx.done &&
3095 ss->tx.done == ss->watchdog_tx_done &&
3096 ss->watchdog_tx_req != ss->watchdog_tx_done) {
Brice Goglin0da34b62006-05-23 06:10:15 -04003097 /* nic seems like it might be stuck.. */
Brice Goglin626fda92007-08-09 09:02:14 +02003098 if (rx_pause_cnt != mgp->watchdog_pause) {
3099 if (net_ratelimit())
3100 printk(KERN_WARNING "myri10ge %s:"
3101 "TX paused, check link partner\n",
3102 mgp->dev->name);
3103 } else {
3104 schedule_work(&mgp->watchdog_work);
3105 return;
3106 }
3107 }
3108 /* rearm timer */
3109 mod_timer(&mgp->watchdog_timer,
3110 jiffies + myri10ge_watchdog_timeout * HZ);
Brice Goglinb53bef82008-05-09 02:20:03 +02003111 ss->watchdog_tx_done = ss->tx.done;
3112 ss->watchdog_tx_req = ss->tx.req;
Brice Goglin626fda92007-08-09 09:02:14 +02003113 mgp->watchdog_pause = rx_pause_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -04003114}
3115
3116static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3117{
3118 struct net_device *netdev;
3119 struct myri10ge_priv *mgp;
3120 struct device *dev = &pdev->dev;
3121 size_t bytes;
3122 int i;
3123 int status = -ENXIO;
Brice Goglin0da34b62006-05-23 06:10:15 -04003124 int dac_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04003125
3126 netdev = alloc_etherdev(sizeof(*mgp));
3127 if (netdev == NULL) {
3128 dev_err(dev, "Could not allocate ethernet device\n");
3129 return -ENOMEM;
3130 }
3131
Maik Hampelb245fb62007-06-28 17:07:26 +02003132 SET_NETDEV_DEV(netdev, &pdev->dev);
3133
Brice Goglin0da34b62006-05-23 06:10:15 -04003134 mgp = netdev_priv(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003135 mgp->dev = netdev;
Brice Goglinb53bef82008-05-09 02:20:03 +02003136 netif_napi_add(netdev, &mgp->ss.napi, myri10ge_poll, myri10ge_napi_weight);
Brice Goglin0da34b62006-05-23 06:10:15 -04003137 mgp->pdev = pdev;
3138 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
3139 mgp->pause = myri10ge_flow_control;
3140 mgp->intr_coal_delay = myri10ge_intr_coal_delay;
Brice Goglinc58ac5c2006-08-21 17:36:49 -04003141 mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
Brice Goglin0da34b62006-05-23 06:10:15 -04003142 init_waitqueue_head(&mgp->down_wq);
3143
3144 if (pci_enable_device(pdev)) {
3145 dev_err(&pdev->dev, "pci_enable_device call failed\n");
3146 status = -ENODEV;
3147 goto abort_with_netdev;
3148 }
Brice Goglin0da34b62006-05-23 06:10:15 -04003149
3150 /* Find the vendor-specific cap so we can check
3151 * the reboot register later on */
3152 mgp->vendor_specific_offset
3153 = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
3154
3155 /* Set our max read request to 4KB */
Brice Goglin302d2422007-08-24 08:57:17 +02003156 status = pcie_set_readrq(pdev, 4096);
Brice Goglin0da34b62006-05-23 06:10:15 -04003157 if (status != 0) {
3158 dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
3159 status);
3160 goto abort_with_netdev;
3161 }
3162
3163 pci_set_master(pdev);
3164 dac_enabled = 1;
3165 status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
3166 if (status != 0) {
3167 dac_enabled = 0;
3168 dev_err(&pdev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07003169 "64-bit pci address mask was refused, "
3170 "trying 32-bit\n");
Brice Goglin0da34b62006-05-23 06:10:15 -04003171 status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3172 }
3173 if (status != 0) {
3174 dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
3175 goto abort_with_netdev;
3176 }
Brice Goglinb10c0662006-06-08 10:25:00 -04003177 mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
3178 &mgp->cmd_bus, GFP_KERNEL);
Brice Goglin0da34b62006-05-23 06:10:15 -04003179 if (mgp->cmd == NULL)
3180 goto abort_with_netdev;
3181
Brice Goglinb53bef82008-05-09 02:20:03 +02003182 mgp->ss.fw_stats = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
3183 &mgp->ss.fw_stats_bus, GFP_KERNEL);
3184 if (mgp->ss.fw_stats == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04003185 goto abort_with_cmd;
3186
3187 mgp->board_span = pci_resource_len(pdev, 0);
3188 mgp->iomem_base = pci_resource_start(pdev, 0);
3189 mgp->mtrr = -1;
Brice Goglin276e26c2007-03-07 20:02:32 +01003190 mgp->wc_enabled = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04003191#ifdef CONFIG_MTRR
3192 mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
3193 MTRR_TYPE_WRCOMB, 1);
Brice Goglin276e26c2007-03-07 20:02:32 +01003194 if (mgp->mtrr >= 0)
3195 mgp->wc_enabled = 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04003196#endif
3197 /* Hack. need to get rid of these magic numbers */
3198 mgp->sram_size =
3199 2 * 1024 * 1024 - (2 * (48 * 1024) + (32 * 1024)) - 0x100;
3200 if (mgp->sram_size > mgp->board_span) {
3201 dev_err(&pdev->dev, "board span %ld bytes too small\n",
3202 mgp->board_span);
3203 goto abort_with_wc;
3204 }
3205 mgp->sram = ioremap(mgp->iomem_base, mgp->board_span);
3206 if (mgp->sram == NULL) {
3207 dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
3208 mgp->board_span, mgp->iomem_base);
3209 status = -ENXIO;
3210 goto abort_with_wc;
3211 }
3212 memcpy_fromio(mgp->eeprom_strings,
3213 mgp->sram + mgp->sram_size - MYRI10GE_EEPROM_STRINGS_SIZE,
3214 MYRI10GE_EEPROM_STRINGS_SIZE);
3215 memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
3216 status = myri10ge_read_mac_addr(mgp);
3217 if (status)
3218 goto abort_with_ioremap;
3219
3220 for (i = 0; i < ETH_ALEN; i++)
3221 netdev->dev_addr[i] = mgp->mac_addr[i];
3222
3223 /* allocate rx done ring */
Brice Goglin014377a2008-05-09 02:20:47 +02003224 bytes = mgp->max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
Brice Goglinb53bef82008-05-09 02:20:03 +02003225 mgp->ss.rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
3226 &mgp->ss.rx_done.bus, GFP_KERNEL);
3227 if (mgp->ss.rx_done.entry == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04003228 goto abort_with_ioremap;
Brice Goglinb53bef82008-05-09 02:20:03 +02003229 memset(mgp->ss.rx_done.entry, 0, bytes);
Brice Goglin0da34b62006-05-23 06:10:15 -04003230
Brice Goglin5443e9e2007-05-07 23:52:22 +02003231 myri10ge_select_firmware(mgp);
3232
Brice Goglin0da34b62006-05-23 06:10:15 -04003233 status = myri10ge_load_firmware(mgp);
3234 if (status != 0) {
3235 dev_err(&pdev->dev, "failed to load firmware\n");
3236 goto abort_with_rx_done;
3237 }
3238
3239 status = myri10ge_reset(mgp);
3240 if (status != 0) {
3241 dev_err(&pdev->dev, "failed reset\n");
3242 goto abort_with_firmware;
3243 }
3244
Brice Goglin0da34b62006-05-23 06:10:15 -04003245 pci_set_drvdata(pdev, mgp);
3246 if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
3247 myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
3248 if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
3249 myri10ge_initial_mtu = 68;
3250 netdev->mtu = myri10ge_initial_mtu;
3251 netdev->open = myri10ge_open;
3252 netdev->stop = myri10ge_close;
3253 netdev->hard_start_xmit = myri10ge_xmit;
3254 netdev->get_stats = myri10ge_get_stats;
3255 netdev->base_addr = mgp->iomem_base;
Brice Goglin0da34b62006-05-23 06:10:15 -04003256 netdev->change_mtu = myri10ge_change_mtu;
3257 netdev->set_multicast_list = myri10ge_set_multicast_list;
3258 netdev->set_mac_address = myri10ge_set_mac_address;
Brice Goglin4f93fde2007-10-13 12:34:01 +02003259 netdev->features = mgp->features;
Brice Goglin0da34b62006-05-23 06:10:15 -04003260 if (dac_enabled)
3261 netdev->features |= NETIF_F_HIGHDMA;
Brice Goglin0da34b62006-05-23 06:10:15 -04003262
Brice Goglin21d05db2007-01-09 21:05:04 +01003263 /* make sure we can get an irq, and that MSI can be
3264 * setup (if available). Also ensure netdev->irq
3265 * is set to correct value if MSI is enabled */
3266 status = myri10ge_request_irq(mgp);
3267 if (status != 0)
3268 goto abort_with_firmware;
3269 netdev->irq = pdev->irq;
3270 myri10ge_free_irq(mgp);
3271
Brice Goglin0da34b62006-05-23 06:10:15 -04003272 /* Save configuration space to be restored if the
3273 * nic resets due to a parity error */
Brice Goglin83f6e152006-12-18 11:52:02 +01003274 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003275
3276 /* Setup the watchdog timer */
3277 setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
3278 (unsigned long)mgp);
3279
3280 SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
David Howellsc4028952006-11-22 14:57:56 +00003281 INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
Brice Goglin0da34b62006-05-23 06:10:15 -04003282 status = register_netdev(netdev);
3283 if (status != 0) {
3284 dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
Brice Goglin7adda302006-12-18 11:50:00 +01003285 goto abort_with_state;
Brice Goglin0da34b62006-05-23 06:10:15 -04003286 }
Brice Goglin21d05db2007-01-09 21:05:04 +01003287 dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
3288 (mgp->msi_enabled ? "MSI" : "xPIC"),
Brice Goglinb53bef82008-05-09 02:20:03 +02003289 netdev->irq, mgp->tx_boundary, mgp->fw_name,
Brice Goglin276e26c2007-03-07 20:02:32 +01003290 (mgp->wc_enabled ? "Enabled" : "Disabled"));
Brice Goglin0da34b62006-05-23 06:10:15 -04003291
3292 return 0;
3293
Brice Goglin7adda302006-12-18 11:50:00 +01003294abort_with_state:
Brice Goglin83f6e152006-12-18 11:52:02 +01003295 pci_restore_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003296
3297abort_with_firmware:
3298 myri10ge_dummy_rdma(mgp, 0);
3299
3300abort_with_rx_done:
Brice Goglin014377a2008-05-09 02:20:47 +02003301 bytes = mgp->max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
Brice Goglinb10c0662006-06-08 10:25:00 -04003302 dma_free_coherent(&pdev->dev, bytes,
Brice Goglinb53bef82008-05-09 02:20:03 +02003303 mgp->ss.rx_done.entry, mgp->ss.rx_done.bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003304
3305abort_with_ioremap:
3306 iounmap(mgp->sram);
3307
3308abort_with_wc:
3309#ifdef CONFIG_MTRR
3310 if (mgp->mtrr >= 0)
3311 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3312#endif
Brice Goglinb53bef82008-05-09 02:20:03 +02003313 dma_free_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
3314 mgp->ss.fw_stats, mgp->ss.fw_stats_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003315
3316abort_with_cmd:
Brice Goglinb10c0662006-06-08 10:25:00 -04003317 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3318 mgp->cmd, mgp->cmd_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003319
3320abort_with_netdev:
3321
3322 free_netdev(netdev);
3323 return status;
3324}
3325
3326/*
3327 * myri10ge_remove
3328 *
3329 * Does what is necessary to shutdown one Myrinet device. Called
3330 * once for each Myrinet card by the kernel when a module is
3331 * unloaded.
3332 */
3333static void myri10ge_remove(struct pci_dev *pdev)
3334{
3335 struct myri10ge_priv *mgp;
3336 struct net_device *netdev;
3337 size_t bytes;
3338
3339 mgp = pci_get_drvdata(pdev);
3340 if (mgp == NULL)
3341 return;
3342
3343 flush_scheduled_work();
3344 netdev = mgp->dev;
3345 unregister_netdev(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003346
3347 myri10ge_dummy_rdma(mgp, 0);
3348
Brice Goglin7adda302006-12-18 11:50:00 +01003349 /* avoid a memory leak */
Brice Goglin83f6e152006-12-18 11:52:02 +01003350 pci_restore_state(pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003351
Brice Goglin014377a2008-05-09 02:20:47 +02003352 bytes = mgp->max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
Brice Goglinb10c0662006-06-08 10:25:00 -04003353 dma_free_coherent(&pdev->dev, bytes,
Brice Goglinb53bef82008-05-09 02:20:03 +02003354 mgp->ss.rx_done.entry, mgp->ss.rx_done.bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003355
3356 iounmap(mgp->sram);
3357
3358#ifdef CONFIG_MTRR
3359 if (mgp->mtrr >= 0)
3360 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3361#endif
Brice Goglinb53bef82008-05-09 02:20:03 +02003362 dma_free_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
3363 mgp->ss.fw_stats, mgp->ss.fw_stats_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003364
Brice Goglinb10c0662006-06-08 10:25:00 -04003365 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3366 mgp->cmd, mgp->cmd_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003367
3368 free_netdev(netdev);
3369 pci_set_drvdata(pdev, NULL);
3370}
3371
Brice Goglinb10c0662006-06-08 10:25:00 -04003372#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
Brice Goglina07bc1f2007-09-14 00:40:14 +02003373#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
Brice Goglin0da34b62006-05-23 06:10:15 -04003374
3375static struct pci_device_id myri10ge_pci_tbl[] = {
Brice Goglinb10c0662006-06-08 10:25:00 -04003376 {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
Brice Goglina07bc1f2007-09-14 00:40:14 +02003377 {PCI_DEVICE
3378 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
Brice Goglin0da34b62006-05-23 06:10:15 -04003379 {0},
3380};
3381
3382static struct pci_driver myri10ge_driver = {
3383 .name = "myri10ge",
3384 .probe = myri10ge_probe,
3385 .remove = myri10ge_remove,
3386 .id_table = myri10ge_pci_tbl,
3387#ifdef CONFIG_PM
3388 .suspend = myri10ge_suspend,
3389 .resume = myri10ge_resume,
3390#endif
3391};
3392
3393static __init int myri10ge_init_module(void)
3394{
3395 printk(KERN_INFO "%s: Version %s\n", myri10ge_driver.name,
3396 MYRI10GE_VERSION_STR);
3397 return pci_register_driver(&myri10ge_driver);
3398}
3399
3400module_init(myri10ge_init_module);
3401
3402static __exit void myri10ge_cleanup_module(void)
3403{
3404 pci_unregister_driver(&myri10ge_driver);
3405}
3406
3407module_exit(myri10ge_cleanup_module);