blob: a833cdd85466894a2ec21b399f1eb251dd4c1453 [file] [log] [blame]
Brice Goglin0da34b62006-05-23 06:10:15 -04001/*************************************************************************
2 * myri10ge.c: Myricom Myri-10G Ethernet driver.
3 *
Brice Gogline3fd5532009-01-17 08:27:19 +00004 * Copyright (C) 2005 - 2009 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 Goglin981813d2008-05-09 02:22:16 +020052#include <linux/dca.h>
Brice Goglin0da34b62006-05-23 06:10:15 -040053#include <linux/ip.h>
54#include <linux/inet.h>
55#include <linux/in.h>
56#include <linux/ethtool.h>
57#include <linux/firmware.h>
58#include <linux/delay.h>
Brice Goglin0da34b62006-05-23 06:10:15 -040059#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 Gogline3fd5532009-01-17 08:27:19 +000078#define MYRI10GE_VERSION_STR "1.4.4-1.401"
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 Goglin236bb5e2008-09-28 15:34:21 +0000105#define MYRI10GE_MAX_SLICES 32
106
Brice Goglin0da34b62006-05-23 06:10:15 -0400107struct myri10ge_rx_buffer_state {
Brice Goglindd50f332006-12-11 11:25:09 +0100108 struct page *page;
109 int page_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -0400110 DECLARE_PCI_UNMAP_ADDR(bus)
111 DECLARE_PCI_UNMAP_LEN(len)
112};
113
114struct myri10ge_tx_buffer_state {
115 struct sk_buff *skb;
116 int last;
117 DECLARE_PCI_UNMAP_ADDR(bus)
118 DECLARE_PCI_UNMAP_LEN(len)
119};
120
121struct myri10ge_cmd {
122 u32 data0;
123 u32 data1;
124 u32 data2;
125};
126
127struct myri10ge_rx_buf {
128 struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
Brice Goglin0da34b62006-05-23 06:10:15 -0400129 struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
130 struct myri10ge_rx_buffer_state *info;
Brice Goglindd50f332006-12-11 11:25:09 +0100131 struct page *page;
132 dma_addr_t bus;
133 int page_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -0400134 int cnt;
Brice Goglindd50f332006-12-11 11:25:09 +0100135 int fill_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -0400136 int alloc_fail;
137 int mask; /* number of rx slots -1 */
Brice Goglindd50f332006-12-11 11:25:09 +0100138 int watchdog_needed;
Brice Goglin0da34b62006-05-23 06:10:15 -0400139};
140
141struct myri10ge_tx_buf {
142 struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
Brice Goglin236bb5e2008-09-28 15:34:21 +0000143 __be32 __iomem *send_go; /* "go" doorbell ptr */
144 __be32 __iomem *send_stop; /* "stop" doorbell ptr */
Brice Goglin0da34b62006-05-23 06:10:15 -0400145 struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
146 char *req_bytes;
147 struct myri10ge_tx_buffer_state *info;
148 int mask; /* number of transmit slots -1 */
Brice Goglin0da34b62006-05-23 06:10:15 -0400149 int req ____cacheline_aligned; /* transmit slots submitted */
150 int pkt_start; /* packets started */
Brice Goglinb53bef82008-05-09 02:20:03 +0200151 int stop_queue;
152 int linearized;
Brice Goglin0da34b62006-05-23 06:10:15 -0400153 int done ____cacheline_aligned; /* transmit slots completed */
154 int pkt_done; /* packets completed */
Brice Goglinb53bef82008-05-09 02:20:03 +0200155 int wake_queue;
Brice Goglin236bb5e2008-09-28 15:34:21 +0000156 int queue_active;
Brice Goglin0da34b62006-05-23 06:10:15 -0400157};
158
159struct myri10ge_rx_done {
160 struct mcp_slot *entry;
161 dma_addr_t bus;
162 int cnt;
163 int idx;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700164 struct net_lro_mgr lro_mgr;
165 struct net_lro_desc lro_desc[MYRI10GE_MAX_LRO_DESCRIPTORS];
Brice Goglin0da34b62006-05-23 06:10:15 -0400166};
167
Brice Goglinb53bef82008-05-09 02:20:03 +0200168struct myri10ge_slice_netstats {
169 unsigned long rx_packets;
170 unsigned long tx_packets;
171 unsigned long rx_bytes;
172 unsigned long tx_bytes;
173 unsigned long rx_dropped;
174 unsigned long tx_dropped;
175};
176
177struct myri10ge_slice_state {
Brice Goglin0da34b62006-05-23 06:10:15 -0400178 struct myri10ge_tx_buf tx; /* transmit ring */
179 struct myri10ge_rx_buf rx_small;
180 struct myri10ge_rx_buf rx_big;
181 struct myri10ge_rx_done rx_done;
Brice Goglinb53bef82008-05-09 02:20:03 +0200182 struct net_device *dev;
183 struct napi_struct napi;
184 struct myri10ge_priv *mgp;
185 struct myri10ge_slice_netstats stats;
186 __be32 __iomem *irq_claim;
187 struct mcp_irq_data *fw_stats;
188 dma_addr_t fw_stats_bus;
189 int watchdog_tx_done;
190 int watchdog_tx_req;
Jeff Garzik5dd2d332008-10-16 05:09:31 -0400191#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +0200192 int cached_dca_tag;
193 int cpu;
194 __be32 __iomem *dca_tag;
195#endif
Brice Goglin0dcffac2008-05-09 02:21:49 +0200196 char irq_desc[32];
Brice Goglinb53bef82008-05-09 02:20:03 +0200197};
198
199struct myri10ge_priv {
Brice Goglin0dcffac2008-05-09 02:21:49 +0200200 struct myri10ge_slice_state *ss;
Brice Goglinb53bef82008-05-09 02:20:03 +0200201 int tx_boundary; /* boundary transmits cannot cross */
Brice Goglin0dcffac2008-05-09 02:21:49 +0200202 int num_slices;
Brice Goglinb53bef82008-05-09 02:20:03 +0200203 int running; /* running? */
204 int csum_flag; /* rx_csums? */
Brice Goglin0da34b62006-05-23 06:10:15 -0400205 int small_bytes;
Brice Goglindd50f332006-12-11 11:25:09 +0100206 int big_bytes;
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200207 int max_intr_slots;
Brice Goglin0da34b62006-05-23 06:10:15 -0400208 struct net_device *dev;
209 struct net_device_stats stats;
Brice Goglinb53bef82008-05-09 02:20:03 +0200210 spinlock_t stats_lock;
Brice Goglin0da34b62006-05-23 06:10:15 -0400211 u8 __iomem *sram;
212 int sram_size;
213 unsigned long board_span;
214 unsigned long iomem_base;
Al Viro40f6cff2006-11-20 13:48:32 -0500215 __be32 __iomem *irq_deassert;
Brice Goglin0da34b62006-05-23 06:10:15 -0400216 char *mac_addr_string;
217 struct mcp_cmd_response *cmd;
218 dma_addr_t cmd_bus;
Brice Goglin0da34b62006-05-23 06:10:15 -0400219 struct pci_dev *pdev;
220 int msi_enabled;
Brice Goglin0dcffac2008-05-09 02:21:49 +0200221 int msix_enabled;
222 struct msix_entry *msix_vectors;
Jeff Garzik5dd2d332008-10-16 05:09:31 -0400223#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +0200224 int dca_enabled;
225#endif
Al Viro66341ff2007-12-22 18:56:43 +0000226 u32 link_state;
Brice Goglin0da34b62006-05-23 06:10:15 -0400227 unsigned int rdma_tags_available;
228 int intr_coal_delay;
Al Viro40f6cff2006-11-20 13:48:32 -0500229 __be32 __iomem *intr_coal_delay_ptr;
Brice Goglin0da34b62006-05-23 06:10:15 -0400230 int mtrr;
Brice Goglin276e26c2007-03-07 20:02:32 +0100231 int wc_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -0400232 int down_cnt;
233 wait_queue_head_t down_wq;
234 struct work_struct watchdog_work;
235 struct timer_list watchdog_timer;
Brice Goglin0da34b62006-05-23 06:10:15 -0400236 int watchdog_resets;
Brice Goglinb53bef82008-05-09 02:20:03 +0200237 int watchdog_pause;
Brice Goglin0da34b62006-05-23 06:10:15 -0400238 int pause;
239 char *fw_name;
240 char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
Brice Goglinc0bf8802008-05-09 02:18:24 +0200241 char *product_code_string;
Brice Goglin0da34b62006-05-23 06:10:15 -0400242 char fw_version[128];
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100243 int fw_ver_major;
244 int fw_ver_minor;
245 int fw_ver_tiny;
246 int adopted_rx_filter_bug;
Brice Goglin0da34b62006-05-23 06:10:15 -0400247 u8 mac_addr[6]; /* eeprom mac address */
248 unsigned long serial_number;
249 int vendor_specific_offset;
Brice Goglin85a7ea12006-08-21 17:36:56 -0400250 int fw_multicast_support;
Brice Goglin4f93fde2007-10-13 12:34:01 +0200251 unsigned long features;
252 u32 max_tso6;
Brice Goglin0da34b62006-05-23 06:10:15 -0400253 u32 read_dma;
254 u32 write_dma;
255 u32 read_write_dma;
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400256 u32 link_changes;
257 u32 msg_enable;
Brice Goglin0da34b62006-05-23 06:10:15 -0400258};
259
260static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
261static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
Brice Goglin0dcffac2008-05-09 02:21:49 +0200262static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
263static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
Brice Goglin0da34b62006-05-23 06:10:15 -0400264
265static char *myri10ge_fw_name = NULL;
266module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200267MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
Brice Goglin0da34b62006-05-23 06:10:15 -0400268
269static int myri10ge_ecrc_enable = 1;
270module_param(myri10ge_ecrc_enable, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200271MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
Brice Goglin0da34b62006-05-23 06:10:15 -0400272
Brice Goglin0da34b62006-05-23 06:10:15 -0400273static int myri10ge_small_bytes = -1; /* -1 == auto */
274module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200275MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
Brice Goglin0da34b62006-05-23 06:10:15 -0400276
277static int myri10ge_msi = 1; /* enable msi by default */
Brice Goglin3621cec2006-12-18 11:51:22 +0100278module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200279MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
Brice Goglin0da34b62006-05-23 06:10:15 -0400280
Brice Goglinf761fae2007-03-21 19:45:56 +0100281static int myri10ge_intr_coal_delay = 75;
Brice Goglin0da34b62006-05-23 06:10:15 -0400282module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200283MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
Brice Goglin0da34b62006-05-23 06:10:15 -0400284
285static int myri10ge_flow_control = 1;
286module_param(myri10ge_flow_control, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200287MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
Brice Goglin0da34b62006-05-23 06:10:15 -0400288
289static int myri10ge_deassert_wait = 1;
290module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
291MODULE_PARM_DESC(myri10ge_deassert_wait,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200292 "Wait when deasserting legacy interrupts");
Brice Goglin0da34b62006-05-23 06:10:15 -0400293
294static int myri10ge_force_firmware = 0;
295module_param(myri10ge_force_firmware, int, S_IRUGO);
296MODULE_PARM_DESC(myri10ge_force_firmware,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200297 "Force firmware to assume aligned completions");
Brice Goglin0da34b62006-05-23 06:10:15 -0400298
Brice Goglin0da34b62006-05-23 06:10:15 -0400299static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
300module_param(myri10ge_initial_mtu, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200301MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
Brice Goglin0da34b62006-05-23 06:10:15 -0400302
303static int myri10ge_napi_weight = 64;
304module_param(myri10ge_napi_weight, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200305MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
Brice Goglin0da34b62006-05-23 06:10:15 -0400306
307static int myri10ge_watchdog_timeout = 1;
308module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200309MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
Brice Goglin0da34b62006-05-23 06:10:15 -0400310
311static int myri10ge_max_irq_loops = 1048576;
312module_param(myri10ge_max_irq_loops, int, S_IRUGO);
313MODULE_PARM_DESC(myri10ge_max_irq_loops,
Brice Goglind1ce3a02008-05-09 02:16:53 +0200314 "Set stuck legacy IRQ detection threshold");
Brice Goglin0da34b62006-05-23 06:10:15 -0400315
Brice Goglinc58ac5c2006-08-21 17:36:49 -0400316#define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
317
318static int myri10ge_debug = -1; /* defaults above */
319module_param(myri10ge_debug, int, 0);
320MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
321
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700322static int myri10ge_lro = 1;
323module_param(myri10ge_lro, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200324MODULE_PARM_DESC(myri10ge_lro, "Enable large receive offload");
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700325
326static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
327module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200328MODULE_PARM_DESC(myri10ge_lro_max_pkts,
329 "Number of LRO packets to be aggregated");
Andrew Gallatin1e6e9342007-09-17 11:37:42 -0700330
Brice Goglindd50f332006-12-11 11:25:09 +0100331static int myri10ge_fill_thresh = 256;
332module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
Brice Goglind1ce3a02008-05-09 02:16:53 +0200333MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
Brice Goglindd50f332006-12-11 11:25:09 +0100334
Brice Goglinf1811372007-06-11 20:26:31 +0200335static int myri10ge_reset_recover = 1;
336
Brice Goglin0dcffac2008-05-09 02:21:49 +0200337static int myri10ge_max_slices = 1;
338module_param(myri10ge_max_slices, int, S_IRUGO);
339MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
340
341static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
342module_param(myri10ge_rss_hash, int, S_IRUGO);
343MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
344
Brice Goglin981813d2008-05-09 02:22:16 +0200345static int myri10ge_dca = 1;
346module_param(myri10ge_dca, int, S_IRUGO);
347MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
348
Brice Goglin0da34b62006-05-23 06:10:15 -0400349#define MYRI10GE_FW_OFFSET 1024*1024
350#define MYRI10GE_HIGHPART_TO_U32(X) \
351(sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
352#define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
353
354#define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
355
Brice Goglin2f762162007-05-07 23:50:37 +0200356static void myri10ge_set_multicast_list(struct net_device *dev);
Brice Goglin4f93fde2007-10-13 12:34:01 +0200357static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev);
Brice Goglin2f762162007-05-07 23:50:37 +0200358
Brice Goglin62502232006-12-11 11:24:37 +0100359static inline void put_be32(__be32 val, __be32 __iomem * p)
Al Viro40f6cff2006-11-20 13:48:32 -0500360{
Brice Goglin62502232006-12-11 11:24:37 +0100361 __raw_writel((__force __u32) val, (__force void __iomem *)p);
Al Viro40f6cff2006-11-20 13:48:32 -0500362}
363
Brice Goglin59081822009-04-16 02:23:56 +0000364static struct net_device_stats *myri10ge_get_stats(struct net_device *dev);
365
Brice Goglin0da34b62006-05-23 06:10:15 -0400366static int
367myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
368 struct myri10ge_cmd *data, int atomic)
369{
370 struct mcp_cmd *buf;
371 char buf_bytes[sizeof(*buf) + 8];
372 struct mcp_cmd_response *response = mgp->cmd;
Brice Gogline700f9f2006-08-14 17:52:54 -0400373 char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
Brice Goglin0da34b62006-05-23 06:10:15 -0400374 u32 dma_low, dma_high, result, value;
375 int sleep_total = 0;
376
377 /* ensure buf is aligned to 8 bytes */
378 buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
379
380 buf->data0 = htonl(data->data0);
381 buf->data1 = htonl(data->data1);
382 buf->data2 = htonl(data->data2);
383 buf->cmd = htonl(cmd);
384 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
385 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
386
387 buf->response_addr.low = htonl(dma_low);
388 buf->response_addr.high = htonl(dma_high);
Al Viro40f6cff2006-11-20 13:48:32 -0500389 response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglin0da34b62006-05-23 06:10:15 -0400390 mb();
391 myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
392
393 /* wait up to 15ms. Longest command is the DMA benchmark,
394 * which is capped at 5ms, but runs from a timeout handler
395 * that runs every 7.8ms. So a 15ms timeout leaves us with
396 * a 2.2ms margin
397 */
398 if (atomic) {
399 /* if atomic is set, do not sleep,
400 * and try to get the completion quickly
401 * (1ms will be enough for those commands) */
402 for (sleep_total = 0;
403 sleep_total < 1000
Al Viro40f6cff2006-11-20 13:48:32 -0500404 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglinbd2db0c2008-05-09 02:18:45 +0200405 sleep_total += 10) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400406 udelay(10);
Brice Goglinbd2db0c2008-05-09 02:18:45 +0200407 mb();
408 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400409 } else {
410 /* use msleep for most command */
411 for (sleep_total = 0;
412 sleep_total < 15
Al Viro40f6cff2006-11-20 13:48:32 -0500413 && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
Brice Goglin0da34b62006-05-23 06:10:15 -0400414 sleep_total++)
415 msleep(1);
416 }
417
418 result = ntohl(response->result);
419 value = ntohl(response->data);
420 if (result != MYRI10GE_NO_RESPONSE_RESULT) {
421 if (result == 0) {
422 data->data0 = value;
423 return 0;
Brice Goglin85a7ea12006-08-21 17:36:56 -0400424 } else if (result == MXGEFW_CMD_UNKNOWN) {
425 return -ENOSYS;
Brice Goglin5443e9e2007-05-07 23:52:22 +0200426 } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
427 return -E2BIG;
Brice Goglin236bb5e2008-09-28 15:34:21 +0000428 } else if (result == MXGEFW_CMD_ERROR_RANGE &&
429 cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
430 (data->
431 data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
432 0) {
433 return -ERANGE;
Brice Goglin0da34b62006-05-23 06:10:15 -0400434 } else {
435 dev_err(&mgp->pdev->dev,
436 "command %d failed, result = %d\n",
437 cmd, result);
438 return -ENXIO;
439 }
440 }
441
442 dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
443 cmd, result);
444 return -EAGAIN;
445}
446
447/*
448 * The eeprom strings on the lanaiX have the format
449 * SN=x\0
450 * MAC=x:x:x:x:x:x\0
451 * PT:ddd mmm xx xx:xx:xx xx\0
452 * PV:ddd mmm xx xx:xx:xx xx\0
453 */
454static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
455{
456 char *ptr, *limit;
457 int i;
458
459 ptr = mgp->eeprom_strings;
460 limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
461
462 while (*ptr != '\0' && ptr < limit) {
463 if (memcmp(ptr, "MAC=", 4) == 0) {
464 ptr += 4;
465 mgp->mac_addr_string = ptr;
466 for (i = 0; i < 6; i++) {
467 if ((ptr + 2) > limit)
468 goto abort;
469 mgp->mac_addr[i] =
470 simple_strtoul(ptr, &ptr, 16);
471 ptr += 1;
472 }
473 }
Brice Goglinc0bf8802008-05-09 02:18:24 +0200474 if (memcmp(ptr, "PC=", 3) == 0) {
475 ptr += 3;
476 mgp->product_code_string = ptr;
477 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400478 if (memcmp((const void *)ptr, "SN=", 3) == 0) {
479 ptr += 3;
480 mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
481 }
482 while (ptr < limit && *ptr++) ;
483 }
484
485 return 0;
486
487abort:
488 dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
489 return -ENXIO;
490}
491
492/*
493 * Enable or disable periodic RDMAs from the host to make certain
494 * chipsets resend dropped PCIe messages
495 */
496
497static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
498{
499 char __iomem *submit;
Brice Goglinf8fd57c2008-05-09 02:17:37 +0200500 __be32 buf[16] __attribute__ ((__aligned__(8)));
Brice Goglin0da34b62006-05-23 06:10:15 -0400501 u32 dma_low, dma_high;
502 int i;
503
504 /* clear confirmation addr */
505 mgp->cmd->data = 0;
506 mb();
507
508 /* send a rdma command to the PCIe engine, and wait for the
509 * response in the confirmation address. The firmware should
510 * write a -1 there to indicate it is alive and well
511 */
512 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
513 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
514
515 buf[0] = htonl(dma_high); /* confirm addr MSW */
516 buf[1] = htonl(dma_low); /* confirm addr LSW */
Al Viro40f6cff2006-11-20 13:48:32 -0500517 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
Brice Goglin0da34b62006-05-23 06:10:15 -0400518 buf[3] = htonl(dma_high); /* dummy addr MSW */
519 buf[4] = htonl(dma_low); /* dummy addr LSW */
520 buf[5] = htonl(enable); /* enable? */
521
Brice Gogline700f9f2006-08-14 17:52:54 -0400522 submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
Brice Goglin0da34b62006-05-23 06:10:15 -0400523
524 myri10ge_pio_copy(submit, &buf, sizeof(buf));
525 for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
526 msleep(1);
527 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
528 dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
529 (enable ? "enable" : "disable"));
530}
531
532static int
533myri10ge_validate_firmware(struct myri10ge_priv *mgp,
534 struct mcp_gen_header *hdr)
535{
536 struct device *dev = &mgp->pdev->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -0400537
538 /* check firmware type */
539 if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
540 dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
541 return -EINVAL;
542 }
543
544 /* save firmware version for ethtool */
545 strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
546
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100547 sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
548 &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
Brice Goglin0da34b62006-05-23 06:10:15 -0400549
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100550 if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR
551 && mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400552 dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
553 dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
554 MXGEFW_VERSION_MINOR);
555 return -EINVAL;
556 }
557 return 0;
558}
559
560static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
561{
562 unsigned crc, reread_crc;
563 const struct firmware *fw;
564 struct device *dev = &mgp->pdev->dev;
David Woodhouseb0d31d62008-05-24 00:00:07 +0100565 unsigned char *fw_readback;
Brice Goglin0da34b62006-05-23 06:10:15 -0400566 struct mcp_gen_header *hdr;
567 size_t hdr_offset;
568 int status;
Brice Gogline4543582006-07-30 00:14:09 -0400569 unsigned i;
Brice Goglin0da34b62006-05-23 06:10:15 -0400570
571 if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
572 dev_err(dev, "Unable to load %s firmware image via hotplug\n",
573 mgp->fw_name);
574 status = -EINVAL;
575 goto abort_with_nothing;
576 }
577
578 /* check size */
579
580 if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
581 fw->size < MCP_HEADER_PTR_OFFSET + 4) {
582 dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
583 status = -EINVAL;
584 goto abort_with_fw;
585 }
586
587 /* check id */
Al Viro40f6cff2006-11-20 13:48:32 -0500588 hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
Brice Goglin0da34b62006-05-23 06:10:15 -0400589 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
590 dev_err(dev, "Bad firmware file\n");
591 status = -EINVAL;
592 goto abort_with_fw;
593 }
594 hdr = (void *)(fw->data + hdr_offset);
595
596 status = myri10ge_validate_firmware(mgp, hdr);
597 if (status != 0)
598 goto abort_with_fw;
599
600 crc = crc32(~0, fw->data, fw->size);
Brice Gogline4543582006-07-30 00:14:09 -0400601 for (i = 0; i < fw->size; i += 256) {
602 myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
603 fw->data + i,
604 min(256U, (unsigned)(fw->size - i)));
605 mb();
606 readb(mgp->sram);
Brice Goglinb10c0662006-06-08 10:25:00 -0400607 }
David Woodhouseb0d31d62008-05-24 00:00:07 +0100608 fw_readback = vmalloc(fw->size);
609 if (!fw_readback) {
610 status = -ENOMEM;
611 goto abort_with_fw;
612 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400613 /* corruption checking is good for parity recovery and buggy chipset */
David Woodhouseb0d31d62008-05-24 00:00:07 +0100614 memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
615 reread_crc = crc32(~0, fw_readback, fw->size);
616 vfree(fw_readback);
Brice Goglin0da34b62006-05-23 06:10:15 -0400617 if (crc != reread_crc) {
618 dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
619 (unsigned)fw->size, reread_crc, crc);
620 status = -EIO;
621 goto abort_with_fw;
622 }
623 *size = (u32) fw->size;
624
625abort_with_fw:
626 release_firmware(fw);
627
628abort_with_nothing:
629 return status;
630}
631
632static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
633{
634 struct mcp_gen_header *hdr;
635 struct device *dev = &mgp->pdev->dev;
636 const size_t bytes = sizeof(struct mcp_gen_header);
637 size_t hdr_offset;
638 int status;
639
640 /* find running firmware header */
Al Viro66341ff2007-12-22 18:56:43 +0000641 hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
Brice Goglin0da34b62006-05-23 06:10:15 -0400642
643 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
644 dev_err(dev, "Running firmware has bad header offset (%d)\n",
645 (int)hdr_offset);
646 return -EIO;
647 }
648
649 /* copy header of running firmware from SRAM to host memory to
650 * validate firmware */
651 hdr = kmalloc(bytes, GFP_KERNEL);
652 if (hdr == NULL) {
653 dev_err(dev, "could not malloc firmware hdr\n");
654 return -ENOMEM;
655 }
656 memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
657 status = myri10ge_validate_firmware(mgp, hdr);
658 kfree(hdr);
Brice Goglin9dc6f0e2007-02-21 18:05:17 +0100659
660 /* check to see if adopted firmware has bug where adopting
661 * it will cause broadcasts to be filtered unless the NIC
662 * is kept in ALLMULTI mode */
663 if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
664 mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
665 mgp->adopted_rx_filter_bug = 1;
666 dev_warn(dev, "Adopting fw %d.%d.%d: "
667 "working around rx filter bug\n",
668 mgp->fw_ver_major, mgp->fw_ver_minor,
669 mgp->fw_ver_tiny);
670 }
Brice Goglin0da34b62006-05-23 06:10:15 -0400671 return status;
672}
673
Adrian Bunk0178ec32008-05-20 00:53:00 +0300674static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200675{
676 struct myri10ge_cmd cmd;
677 int status;
678
679 /* probe for IPv6 TSO support */
680 mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
681 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
682 &cmd, 0);
683 if (status == 0) {
684 mgp->max_tso6 = cmd.data0;
685 mgp->features |= NETIF_F_TSO6;
686 }
687
688 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
689 if (status != 0) {
690 dev_err(&mgp->pdev->dev,
691 "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
692 return -ENXIO;
693 }
694
695 mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
696
697 return 0;
698}
699
Brice Goglin0dcffac2008-05-09 02:21:49 +0200700static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
Brice Goglin0da34b62006-05-23 06:10:15 -0400701{
702 char __iomem *submit;
Brice Goglinf8fd57c2008-05-09 02:17:37 +0200703 __be32 buf[16] __attribute__ ((__aligned__(8)));
Brice Goglin0da34b62006-05-23 06:10:15 -0400704 u32 dma_low, dma_high, size;
705 int status, i;
706
Brice Goglinb10c0662006-06-08 10:25:00 -0400707 size = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -0400708 status = myri10ge_load_hotplug_firmware(mgp, &size);
709 if (status) {
Brice Goglin0dcffac2008-05-09 02:21:49 +0200710 if (!adopt)
711 return status;
Brice Goglin0da34b62006-05-23 06:10:15 -0400712 dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
713
714 /* Do not attempt to adopt firmware if there
715 * was a bad crc */
716 if (status == -EIO)
717 return status;
718
719 status = myri10ge_adopt_running_firmware(mgp);
720 if (status != 0) {
721 dev_err(&mgp->pdev->dev,
722 "failed to adopt running firmware\n");
723 return status;
724 }
725 dev_info(&mgp->pdev->dev,
726 "Successfully adopted running firmware\n");
Brice Goglinb53bef82008-05-09 02:20:03 +0200727 if (mgp->tx_boundary == 4096) {
Brice Goglin0da34b62006-05-23 06:10:15 -0400728 dev_warn(&mgp->pdev->dev,
729 "Using firmware currently running on NIC"
730 ". For optimal\n");
731 dev_warn(&mgp->pdev->dev,
732 "performance consider loading optimized "
733 "firmware\n");
734 dev_warn(&mgp->pdev->dev, "via hotplug\n");
735 }
736
737 mgp->fw_name = "adopted";
Brice Goglinb53bef82008-05-09 02:20:03 +0200738 mgp->tx_boundary = 2048;
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200739 myri10ge_dummy_rdma(mgp, 1);
740 status = myri10ge_get_firmware_capabilities(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -0400741 return status;
742 }
743
744 /* clear confirmation addr */
745 mgp->cmd->data = 0;
746 mb();
747
748 /* send a reload command to the bootstrap MCP, and wait for the
749 * response in the confirmation address. The firmware should
750 * write a -1 there to indicate it is alive and well
751 */
752 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
753 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
754
755 buf[0] = htonl(dma_high); /* confirm addr MSW */
756 buf[1] = htonl(dma_low); /* confirm addr LSW */
Al Viro40f6cff2006-11-20 13:48:32 -0500757 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
Brice Goglin0da34b62006-05-23 06:10:15 -0400758
759 /* FIX: All newest firmware should un-protect the bottom of
760 * the sram before handoff. However, the very first interfaces
761 * do not. Therefore the handoff copy must skip the first 8 bytes
762 */
763 buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
764 buf[4] = htonl(size - 8); /* length of code */
765 buf[5] = htonl(8); /* where to copy to */
766 buf[6] = htonl(0); /* where to jump to */
767
Brice Gogline700f9f2006-08-14 17:52:54 -0400768 submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
Brice Goglin0da34b62006-05-23 06:10:15 -0400769
770 myri10ge_pio_copy(submit, &buf, sizeof(buf));
771 mb();
772 msleep(1);
773 mb();
774 i = 0;
Brice Goglind93ca2a2008-05-09 02:17:16 +0200775 while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
776 msleep(1 << i);
Brice Goglin0da34b62006-05-23 06:10:15 -0400777 i++;
778 }
779 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
780 dev_err(&mgp->pdev->dev, "handoff failed\n");
781 return -ENXIO;
782 }
Brice Goglin9a71db72006-07-21 15:49:32 -0400783 myri10ge_dummy_rdma(mgp, 1);
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200784 status = myri10ge_get_firmware_capabilities(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -0400785
Brice Goglinfa0a90d2008-05-09 02:20:25 +0200786 return status;
Brice Goglin0da34b62006-05-23 06:10:15 -0400787}
788
789static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
790{
791 struct myri10ge_cmd cmd;
792 int status;
793
794 cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
795 | (addr[2] << 8) | addr[3]);
796
797 cmd.data1 = ((addr[4] << 8) | (addr[5]));
798
799 status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
800 return status;
801}
802
803static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
804{
805 struct myri10ge_cmd cmd;
806 int status, ctl;
807
808 ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
809 status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
810
811 if (status) {
812 printk(KERN_ERR
813 "myri10ge: %s: Failed to set flow control mode\n",
814 mgp->dev->name);
815 return status;
816 }
817 mgp->pause = pause;
818 return 0;
819}
820
821static void
822myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
823{
824 struct myri10ge_cmd cmd;
825 int status, ctl;
826
827 ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
828 status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
829 if (status)
830 printk(KERN_ERR "myri10ge: %s: Failed to set promisc mode\n",
831 mgp->dev->name);
832}
833
Brice Goglin0d6ac252007-05-07 23:51:45 +0200834static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
835{
836 struct myri10ge_cmd cmd;
837 int status;
838 u32 len;
839 struct page *dmatest_page;
840 dma_addr_t dmatest_bus;
841 char *test = " ";
842
843 dmatest_page = alloc_page(GFP_KERNEL);
844 if (!dmatest_page)
845 return -ENOMEM;
846 dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
847 DMA_BIDIRECTIONAL);
848
849 /* Run a small DMA test.
850 * The magic multipliers to the length tell the firmware
851 * to do DMA read, write, or read+write tests. The
852 * results are returned in cmd.data0. The upper 16
853 * bits or the return is the number of transfers completed.
854 * The lower 16 bits is the time in 0.5us ticks that the
855 * transfers took to complete.
856 */
857
Brice Goglinb53bef82008-05-09 02:20:03 +0200858 len = mgp->tx_boundary;
Brice Goglin0d6ac252007-05-07 23:51:45 +0200859
860 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
861 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
862 cmd.data2 = len * 0x10000;
863 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
864 if (status != 0) {
865 test = "read";
866 goto abort;
867 }
868 mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
869 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
870 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
871 cmd.data2 = len * 0x1;
872 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
873 if (status != 0) {
874 test = "write";
875 goto abort;
876 }
877 mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
878
879 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
880 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
881 cmd.data2 = len * 0x10001;
882 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
883 if (status != 0) {
884 test = "read/write";
885 goto abort;
886 }
887 mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
888 (cmd.data0 & 0xffff);
889
890abort:
891 pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
892 put_page(dmatest_page);
893
894 if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
895 dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
896 test, status);
897
898 return status;
899}
900
Brice Goglin0da34b62006-05-23 06:10:15 -0400901static int myri10ge_reset(struct myri10ge_priv *mgp)
902{
903 struct myri10ge_cmd cmd;
Brice Goglin0dcffac2008-05-09 02:21:49 +0200904 struct myri10ge_slice_state *ss;
905 int i, status;
Brice Goglin0da34b62006-05-23 06:10:15 -0400906 size_t bytes;
Jeff Garzik5dd2d332008-10-16 05:09:31 -0400907#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +0200908 unsigned long dca_tag_off;
909#endif
Brice Goglin0da34b62006-05-23 06:10:15 -0400910
911 /* try to send a reset command to the card to see if it
912 * is alive */
913 memset(&cmd, 0, sizeof(cmd));
914 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
915 if (status != 0) {
916 dev_err(&mgp->pdev->dev, "failed reset\n");
917 return -ENXIO;
918 }
Brice Goglin0d6ac252007-05-07 23:51:45 +0200919
920 (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
Brice Goglin0dcffac2008-05-09 02:21:49 +0200921 /*
922 * Use non-ndis mcp_slot (eg, 4 bytes total,
923 * no toeplitz hash value returned. Older firmware will
924 * not understand this command, but will use the correct
925 * sized mcp_slot, so we ignore error returns
926 */
927 cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
928 (void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
Brice Goglin0da34b62006-05-23 06:10:15 -0400929
930 /* Now exchange information about interrupts */
931
Brice Goglin0dcffac2008-05-09 02:21:49 +0200932 bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
Brice Goglin0da34b62006-05-23 06:10:15 -0400933 cmd.data0 = (u32) bytes;
934 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
Brice Goglin0dcffac2008-05-09 02:21:49 +0200935
936 /*
937 * Even though we already know how many slices are supported
938 * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
939 * has magic side effects, and must be called after a reset.
940 * It must be called prior to calling any RSS related cmds,
941 * including assigning an interrupt queue for anything but
942 * slice 0. It must also be called *after*
943 * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
944 * the firmware to compute offsets.
945 */
946
947 if (mgp->num_slices > 1) {
948
949 /* ask the maximum number of slices it supports */
950 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
951 &cmd, 0);
952 if (status != 0) {
953 dev_err(&mgp->pdev->dev,
954 "failed to get number of slices\n");
955 }
956
957 /*
958 * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
959 * to setting up the interrupt queue DMA
960 */
961
962 cmd.data0 = mgp->num_slices;
Brice Goglin236bb5e2008-09-28 15:34:21 +0000963 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
964 if (mgp->dev->real_num_tx_queues > 1)
965 cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
Brice Goglin0dcffac2008-05-09 02:21:49 +0200966 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
967 &cmd, 0);
Brice Goglin236bb5e2008-09-28 15:34:21 +0000968
969 /* Firmware older than 1.4.32 only supports multiple
970 * RX queues, so if we get an error, first retry using a
971 * single TX queue before giving up */
972 if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
973 mgp->dev->real_num_tx_queues = 1;
974 cmd.data0 = mgp->num_slices;
975 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
976 status = myri10ge_send_cmd(mgp,
977 MXGEFW_CMD_ENABLE_RSS_QUEUES,
978 &cmd, 0);
979 }
980
Brice Goglin0dcffac2008-05-09 02:21:49 +0200981 if (status != 0) {
982 dev_err(&mgp->pdev->dev,
983 "failed to set number of slices\n");
984
985 return status;
986 }
987 }
988 for (i = 0; i < mgp->num_slices; i++) {
989 ss = &mgp->ss[i];
990 cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
991 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
992 cmd.data2 = i;
993 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
994 &cmd, 0);
995 };
Brice Goglin0da34b62006-05-23 06:10:15 -0400996
997 status |=
998 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
Brice Goglin0dcffac2008-05-09 02:21:49 +0200999 for (i = 0; i < mgp->num_slices; i++) {
1000 ss = &mgp->ss[i];
1001 ss->irq_claim =
1002 (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
1003 }
Brice Goglindf30a742006-12-18 11:50:40 +01001004 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
1005 &cmd, 0);
1006 mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglin0da34b62006-05-23 06:10:15 -04001007
Brice Goglin0da34b62006-05-23 06:10:15 -04001008 status |= myri10ge_send_cmd
1009 (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
Al Viro40f6cff2006-11-20 13:48:32 -05001010 mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
Brice Goglin0da34b62006-05-23 06:10:15 -04001011 if (status != 0) {
1012 dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
1013 return status;
1014 }
Al Viro40f6cff2006-11-20 13:48:32 -05001015 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
Brice Goglin0da34b62006-05-23 06:10:15 -04001016
Jeff Garzik5dd2d332008-10-16 05:09:31 -04001017#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02001018 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
1019 dca_tag_off = cmd.data0;
1020 for (i = 0; i < mgp->num_slices; i++) {
1021 ss = &mgp->ss[i];
1022 if (status == 0) {
1023 ss->dca_tag = (__iomem __be32 *)
1024 (mgp->sram + dca_tag_off + 4 * i);
1025 } else {
1026 ss->dca_tag = NULL;
1027 }
1028 }
Brice Goglin4ee2ac52008-11-23 15:49:28 -08001029#endif /* CONFIG_MYRI10GE_DCA */
Brice Goglin981813d2008-05-09 02:22:16 +02001030
Brice Goglin0da34b62006-05-23 06:10:15 -04001031 /* reset mcp/driver shared state back to 0 */
Brice Goglin0dcffac2008-05-09 02:21:49 +02001032
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001033 mgp->link_changes = 0;
Brice Goglin0dcffac2008-05-09 02:21:49 +02001034 for (i = 0; i < mgp->num_slices; i++) {
1035 ss = &mgp->ss[i];
1036
1037 memset(ss->rx_done.entry, 0, bytes);
1038 ss->tx.req = 0;
1039 ss->tx.done = 0;
1040 ss->tx.pkt_start = 0;
1041 ss->tx.pkt_done = 0;
1042 ss->rx_big.cnt = 0;
1043 ss->rx_small.cnt = 0;
1044 ss->rx_done.idx = 0;
1045 ss->rx_done.cnt = 0;
1046 ss->tx.wake_queue = 0;
1047 ss->tx.stop_queue = 0;
1048 }
1049
Brice Goglin0da34b62006-05-23 06:10:15 -04001050 status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
Brice Goglin0da34b62006-05-23 06:10:15 -04001051 myri10ge_change_pause(mgp, mgp->pause);
Brice Goglin2f762162007-05-07 23:50:37 +02001052 myri10ge_set_multicast_list(mgp->dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04001053 return status;
1054}
1055
Jeff Garzik5dd2d332008-10-16 05:09:31 -04001056#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02001057static void
1058myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
1059{
1060 ss->cpu = cpu;
1061 ss->cached_dca_tag = tag;
1062 put_be32(htonl(tag), ss->dca_tag);
1063}
1064
1065static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
1066{
1067 int cpu = get_cpu();
1068 int tag;
1069
1070 if (cpu != ss->cpu) {
1071 tag = dca_get_tag(cpu);
1072 if (ss->cached_dca_tag != tag)
1073 myri10ge_write_dca(ss, cpu, tag);
1074 }
1075 put_cpu();
1076}
1077
1078static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
1079{
1080 int err, i;
1081 struct pci_dev *pdev = mgp->pdev;
1082
1083 if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
1084 return;
1085 if (!myri10ge_dca) {
1086 dev_err(&pdev->dev, "dca disabled by administrator\n");
1087 return;
1088 }
1089 err = dca_add_requester(&pdev->dev);
1090 if (err) {
Brice Goglin330554c2008-09-12 19:47:26 +02001091 if (err != -ENODEV)
1092 dev_err(&pdev->dev,
1093 "dca_add_requester() failed, err=%d\n", err);
Brice Goglin981813d2008-05-09 02:22:16 +02001094 return;
1095 }
1096 mgp->dca_enabled = 1;
1097 for (i = 0; i < mgp->num_slices; i++)
1098 myri10ge_write_dca(&mgp->ss[i], -1, 0);
1099}
1100
1101static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
1102{
1103 struct pci_dev *pdev = mgp->pdev;
1104 int err;
1105
1106 if (!mgp->dca_enabled)
1107 return;
1108 mgp->dca_enabled = 0;
1109 err = dca_remove_requester(&pdev->dev);
1110}
1111
1112static int myri10ge_notify_dca_device(struct device *dev, void *data)
1113{
1114 struct myri10ge_priv *mgp;
1115 unsigned long event;
1116
1117 mgp = dev_get_drvdata(dev);
1118 event = *(unsigned long *)data;
1119
1120 if (event == DCA_PROVIDER_ADD)
1121 myri10ge_setup_dca(mgp);
1122 else if (event == DCA_PROVIDER_REMOVE)
1123 myri10ge_teardown_dca(mgp);
1124 return 0;
1125}
Brice Goglin4ee2ac52008-11-23 15:49:28 -08001126#endif /* CONFIG_MYRI10GE_DCA */
Brice Goglin981813d2008-05-09 02:22:16 +02001127
Brice Goglin0da34b62006-05-23 06:10:15 -04001128static inline void
1129myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
1130 struct mcp_kreq_ether_recv *src)
1131{
Al Viro40f6cff2006-11-20 13:48:32 -05001132 __be32 low;
Brice Goglin0da34b62006-05-23 06:10:15 -04001133
1134 low = src->addr_low;
Yang Hongyang284901a2009-04-06 19:01:15 -07001135 src->addr_low = htonl(DMA_BIT_MASK(32));
Brice Gogline67bda52006-12-05 17:26:27 +01001136 myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
1137 mb();
1138 myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
Brice Goglin0da34b62006-05-23 06:10:15 -04001139 mb();
1140 src->addr_low = low;
Al Viro40f6cff2006-11-20 13:48:32 -05001141 put_be32(low, &dst->addr_low);
Brice Goglin0da34b62006-05-23 06:10:15 -04001142 mb();
1143}
1144
Al Viro40f6cff2006-11-20 13:48:32 -05001145static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
Brice Goglin0da34b62006-05-23 06:10:15 -04001146{
1147 struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
1148
Al Viro40f6cff2006-11-20 13:48:32 -05001149 if ((skb->protocol == htons(ETH_P_8021Q)) &&
Brice Goglin0da34b62006-05-23 06:10:15 -04001150 (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
1151 vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
1152 skb->csum = hw_csum;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001153 skb->ip_summed = CHECKSUM_COMPLETE;
Brice Goglin0da34b62006-05-23 06:10:15 -04001154 }
1155}
1156
Brice Goglindd50f332006-12-11 11:25:09 +01001157static inline void
1158myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
1159 struct skb_frag_struct *rx_frags, int len, int hlen)
1160{
1161 struct skb_frag_struct *skb_frags;
1162
1163 skb->len = skb->data_len = len;
1164 skb->truesize = len + sizeof(struct sk_buff);
1165 /* attach the page(s) */
1166
1167 skb_frags = skb_shinfo(skb)->frags;
1168 while (len > 0) {
1169 memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
1170 len -= rx_frags->size;
1171 skb_frags++;
1172 rx_frags++;
1173 skb_shinfo(skb)->nr_frags++;
1174 }
1175
1176 /* pskb_may_pull is not available in irq context, but
1177 * skb_pull() (for ether_pad and eth_type_trans()) requires
1178 * the beginning of the packet in skb_headlen(), move it
1179 * manually */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001180 skb_copy_to_linear_data(skb, va, hlen);
Brice Goglindd50f332006-12-11 11:25:09 +01001181 skb_shinfo(skb)->frags[0].page_offset += hlen;
1182 skb_shinfo(skb)->frags[0].size -= hlen;
1183 skb->data_len -= hlen;
1184 skb->tail += hlen;
1185 skb_pull(skb, MXGEFW_PAD);
1186}
1187
1188static void
1189myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
1190 int bytes, int watchdog)
1191{
1192 struct page *page;
1193 int idx;
1194
1195 if (unlikely(rx->watchdog_needed && !watchdog))
1196 return;
1197
1198 /* try to refill entire ring */
1199 while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
1200 idx = rx->fill_cnt & rx->mask;
Brice Goglinae8509b2007-04-10 21:21:08 +02001201 if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
Brice Goglindd50f332006-12-11 11:25:09 +01001202 /* we can use part of previous page */
1203 get_page(rx->page);
1204 } else {
1205 /* we need a new page */
1206 page =
1207 alloc_pages(GFP_ATOMIC | __GFP_COMP,
1208 MYRI10GE_ALLOC_ORDER);
1209 if (unlikely(page == NULL)) {
1210 if (rx->fill_cnt - rx->cnt < 16)
1211 rx->watchdog_needed = 1;
1212 return;
1213 }
1214 rx->page = page;
1215 rx->page_offset = 0;
1216 rx->bus = pci_map_page(mgp->pdev, page, 0,
1217 MYRI10GE_ALLOC_SIZE,
1218 PCI_DMA_FROMDEVICE);
1219 }
1220 rx->info[idx].page = rx->page;
1221 rx->info[idx].page_offset = rx->page_offset;
1222 /* note that this is the address of the start of the
1223 * page */
1224 pci_unmap_addr_set(&rx->info[idx], bus, rx->bus);
1225 rx->shadow[idx].addr_low =
1226 htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
1227 rx->shadow[idx].addr_high =
1228 htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
1229
1230 /* start next packet on a cacheline boundary */
1231 rx->page_offset += SKB_DATA_ALIGN(bytes);
Brice Goglinae8509b2007-04-10 21:21:08 +02001232
1233#if MYRI10GE_ALLOC_SIZE > 4096
1234 /* don't cross a 4KB boundary */
1235 if ((rx->page_offset >> 12) !=
1236 ((rx->page_offset + bytes - 1) >> 12))
1237 rx->page_offset = (rx->page_offset + 4096) & ~4095;
1238#endif
Brice Goglindd50f332006-12-11 11:25:09 +01001239 rx->fill_cnt++;
1240
1241 /* copy 8 descriptors to the firmware at a time */
1242 if ((idx & 7) == 7) {
Brice Gogline454e7e2008-07-21 10:25:50 +02001243 myri10ge_submit_8rx(&rx->lanai[idx - 7],
1244 &rx->shadow[idx - 7]);
Brice Goglindd50f332006-12-11 11:25:09 +01001245 }
1246 }
1247}
1248
1249static inline void
1250myri10ge_unmap_rx_page(struct pci_dev *pdev,
1251 struct myri10ge_rx_buffer_state *info, int bytes)
1252{
1253 /* unmap the recvd page if we're the only or last user of it */
1254 if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
1255 (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
1256 pci_unmap_page(pdev, (pci_unmap_addr(info, bus)
1257 & ~(MYRI10GE_ALLOC_SIZE - 1)),
1258 MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
1259 }
1260}
1261
1262#define MYRI10GE_HLEN 64 /* The number of bytes to copy from a
1263 * page into an skb */
1264
1265static inline int
Brice Goglinb53bef82008-05-09 02:20:03 +02001266myri10ge_rx_done(struct myri10ge_slice_state *ss, struct myri10ge_rx_buf *rx,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001267 int bytes, int len, __wsum csum)
Brice Goglindd50f332006-12-11 11:25:09 +01001268{
Brice Goglinb53bef82008-05-09 02:20:03 +02001269 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglindd50f332006-12-11 11:25:09 +01001270 struct sk_buff *skb;
1271 struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
1272 int i, idx, hlen, remainder;
1273 struct pci_dev *pdev = mgp->pdev;
1274 struct net_device *dev = mgp->dev;
1275 u8 *va;
1276
1277 len += MXGEFW_PAD;
1278 idx = rx->cnt & rx->mask;
1279 va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
1280 prefetch(va);
1281 /* Fill skb_frag_struct(s) with data from our receive */
1282 for (i = 0, remainder = len; remainder > 0; i++) {
1283 myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1284 rx_frags[i].page = rx->info[idx].page;
1285 rx_frags[i].page_offset = rx->info[idx].page_offset;
1286 if (remainder < MYRI10GE_ALLOC_SIZE)
1287 rx_frags[i].size = remainder;
1288 else
1289 rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
1290 rx->cnt++;
1291 idx = rx->cnt & rx->mask;
1292 remainder -= MYRI10GE_ALLOC_SIZE;
1293 }
1294
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001295 if (mgp->csum_flag && myri10ge_lro) {
1296 rx_frags[0].page_offset += MXGEFW_PAD;
1297 rx_frags[0].size -= MXGEFW_PAD;
1298 len -= MXGEFW_PAD;
Brice Goglinb53bef82008-05-09 02:20:03 +02001299 lro_receive_frags(&ss->rx_done.lro_mgr, rx_frags,
Brice Goglinb53bef82008-05-09 02:20:03 +02001300 /* opaque, will come back in get_frag_header */
Brice Goglin0dcffac2008-05-09 02:21:49 +02001301 len, len,
Brice Goglinb53bef82008-05-09 02:20:03 +02001302 (void *)(__force unsigned long)csum, csum);
Brice Goglin0dcffac2008-05-09 02:21:49 +02001303
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001304 return 1;
1305 }
1306
Brice Goglindd50f332006-12-11 11:25:09 +01001307 hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;
1308
Brice Gogline636b2e2007-10-13 12:32:21 +02001309 /* allocate an skb to attach the page(s) to. This is done
1310 * after trying LRO, so as to avoid skb allocation overheads */
Brice Goglindd50f332006-12-11 11:25:09 +01001311
1312 skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
1313 if (unlikely(skb == NULL)) {
Brice Goglind6279c82008-11-20 01:50:04 -08001314 ss->stats.rx_dropped++;
Brice Goglindd50f332006-12-11 11:25:09 +01001315 do {
1316 i--;
1317 put_page(rx_frags[i].page);
1318 } while (i != 0);
1319 return 0;
1320 }
1321
1322 /* Attach the pages to the skb, and trim off any padding */
1323 myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
1324 if (skb_shinfo(skb)->frags[0].size <= 0) {
1325 put_page(skb_shinfo(skb)->frags[0].page);
1326 skb_shinfo(skb)->nr_frags = 0;
1327 }
1328 skb->protocol = eth_type_trans(skb, dev);
David S. Miller0c8dfc82009-01-27 16:22:32 -08001329 skb_record_rx_queue(skb, ss - &mgp->ss[0]);
Brice Goglindd50f332006-12-11 11:25:09 +01001330
1331 if (mgp->csum_flag) {
1332 if ((skb->protocol == htons(ETH_P_IP)) ||
1333 (skb->protocol == htons(ETH_P_IPV6))) {
1334 skb->csum = csum;
1335 skb->ip_summed = CHECKSUM_COMPLETE;
1336 } else
1337 myri10ge_vlan_ip_csum(skb, csum);
1338 }
1339 netif_receive_skb(skb);
Brice Goglindd50f332006-12-11 11:25:09 +01001340 return 1;
1341}
1342
Brice Goglinb53bef82008-05-09 02:20:03 +02001343static inline void
1344myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
Brice Goglin0da34b62006-05-23 06:10:15 -04001345{
Brice Goglinb53bef82008-05-09 02:20:03 +02001346 struct pci_dev *pdev = ss->mgp->pdev;
1347 struct myri10ge_tx_buf *tx = &ss->tx;
Brice Goglin236bb5e2008-09-28 15:34:21 +00001348 struct netdev_queue *dev_queue;
Brice Goglin0da34b62006-05-23 06:10:15 -04001349 struct sk_buff *skb;
1350 int idx, len;
Brice Goglin0da34b62006-05-23 06:10:15 -04001351
1352 while (tx->pkt_done != mcp_index) {
1353 idx = tx->done & tx->mask;
1354 skb = tx->info[idx].skb;
1355
1356 /* Mark as free */
1357 tx->info[idx].skb = NULL;
1358 if (tx->info[idx].last) {
1359 tx->pkt_done++;
1360 tx->info[idx].last = 0;
1361 }
1362 tx->done++;
1363 len = pci_unmap_len(&tx->info[idx], len);
1364 pci_unmap_len_set(&tx->info[idx], len, 0);
1365 if (skb) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001366 ss->stats.tx_bytes += skb->len;
1367 ss->stats.tx_packets++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001368 dev_kfree_skb_irq(skb);
1369 if (len)
1370 pci_unmap_single(pdev,
1371 pci_unmap_addr(&tx->info[idx],
1372 bus), len,
1373 PCI_DMA_TODEVICE);
1374 } else {
1375 if (len)
1376 pci_unmap_page(pdev,
1377 pci_unmap_addr(&tx->info[idx],
1378 bus), len,
1379 PCI_DMA_TODEVICE);
1380 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001381 }
Brice Goglin236bb5e2008-09-28 15:34:21 +00001382
1383 dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
1384 /*
1385 * Make a minimal effort to prevent the NIC from polling an
1386 * idle tx queue. If we can't get the lock we leave the queue
1387 * active. In this case, either a thread was about to start
1388 * using the queue anyway, or we lost a race and the NIC will
1389 * waste some of its resources polling an inactive queue for a
1390 * while.
1391 */
1392
1393 if ((ss->mgp->dev->real_num_tx_queues > 1) &&
1394 __netif_tx_trylock(dev_queue)) {
1395 if (tx->req == tx->done) {
1396 tx->queue_active = 0;
1397 put_be32(htonl(1), tx->send_stop);
Brice Goglin8c2f5fa2008-11-10 13:58:41 +01001398 mb();
Brice Goglin6824a102008-10-30 08:59:33 +01001399 mmiowb();
Brice Goglin236bb5e2008-09-28 15:34:21 +00001400 }
1401 __netif_tx_unlock(dev_queue);
1402 }
1403
Brice Goglin0da34b62006-05-23 06:10:15 -04001404 /* start the queue if we've stopped it */
Brice Goglin236bb5e2008-09-28 15:34:21 +00001405 if (netif_tx_queue_stopped(dev_queue)
Brice Goglin0da34b62006-05-23 06:10:15 -04001406 && tx->req - tx->done < (tx->mask >> 1)) {
Brice Goglinb53bef82008-05-09 02:20:03 +02001407 tx->wake_queue++;
Brice Goglin236bb5e2008-09-28 15:34:21 +00001408 netif_tx_wake_queue(dev_queue);
Brice Goglin0da34b62006-05-23 06:10:15 -04001409 }
1410}
1411
Brice Goglinb53bef82008-05-09 02:20:03 +02001412static inline int
1413myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
Brice Goglin0da34b62006-05-23 06:10:15 -04001414{
Brice Goglinb53bef82008-05-09 02:20:03 +02001415 struct myri10ge_rx_done *rx_done = &ss->rx_done;
1416 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001417 unsigned long rx_bytes = 0;
1418 unsigned long rx_packets = 0;
1419 unsigned long rx_ok;
1420
1421 int idx = rx_done->idx;
1422 int cnt = rx_done->cnt;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001423 int work_done = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04001424 u16 length;
Al Viro40f6cff2006-11-20 13:48:32 -05001425 __wsum checksum;
Brice Goglin0da34b62006-05-23 06:10:15 -04001426
Andrew Gallatinc956a242007-10-31 17:40:06 -04001427 while (rx_done->entry[idx].length != 0 && work_done < budget) {
Brice Goglin0da34b62006-05-23 06:10:15 -04001428 length = ntohs(rx_done->entry[idx].length);
1429 rx_done->entry[idx].length = 0;
Al Viro40f6cff2006-11-20 13:48:32 -05001430 checksum = csum_unfold(rx_done->entry[idx].checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001431 if (length <= mgp->small_bytes)
Brice Goglinb53bef82008-05-09 02:20:03 +02001432 rx_ok = myri10ge_rx_done(ss, &ss->rx_small,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001433 mgp->small_bytes,
1434 length, checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001435 else
Brice Goglinb53bef82008-05-09 02:20:03 +02001436 rx_ok = myri10ge_rx_done(ss, &ss->rx_big,
Brice Goglin52ea6fb2006-12-11 11:26:12 +01001437 mgp->big_bytes,
1438 length, checksum);
Brice Goglin0da34b62006-05-23 06:10:15 -04001439 rx_packets += rx_ok;
1440 rx_bytes += rx_ok * (unsigned long)length;
1441 cnt++;
Brice Goglin014377a2008-05-09 02:20:47 +02001442 idx = cnt & (mgp->max_intr_slots - 1);
Andrew Gallatinc956a242007-10-31 17:40:06 -04001443 work_done++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001444 }
1445 rx_done->idx = idx;
1446 rx_done->cnt = cnt;
Brice Goglinb53bef82008-05-09 02:20:03 +02001447 ss->stats.rx_packets += rx_packets;
1448 ss->stats.rx_bytes += rx_bytes;
Brice Goglinc7dab992006-12-11 11:25:42 +01001449
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001450 if (myri10ge_lro)
1451 lro_flush_all(&rx_done->lro_mgr);
1452
Brice Goglinc7dab992006-12-11 11:25:42 +01001453 /* restock receive rings if needed */
Brice Goglinb53bef82008-05-09 02:20:03 +02001454 if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
1455 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
Brice Goglinc7dab992006-12-11 11:25:42 +01001456 mgp->small_bytes + MXGEFW_PAD, 0);
Brice Goglinb53bef82008-05-09 02:20:03 +02001457 if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
1458 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
Brice Goglinc7dab992006-12-11 11:25:42 +01001459
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001460 return work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001461}
1462
1463static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
1464{
Brice Goglin0dcffac2008-05-09 02:21:49 +02001465 struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
Brice Goglin0da34b62006-05-23 06:10:15 -04001466
1467 if (unlikely(stats->stats_updated)) {
Brice Goglin798a95d2007-06-11 20:26:50 +02001468 unsigned link_up = ntohl(stats->link_up);
1469 if (mgp->link_state != link_up) {
1470 mgp->link_state = link_up;
1471
1472 if (mgp->link_state == MXGEFW_LINK_UP) {
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001473 if (netif_msg_link(mgp))
1474 printk(KERN_INFO
1475 "myri10ge: %s: link up\n",
1476 mgp->dev->name);
Brice Goglin0da34b62006-05-23 06:10:15 -04001477 netif_carrier_on(mgp->dev);
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001478 mgp->link_changes++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001479 } else {
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001480 if (netif_msg_link(mgp))
1481 printk(KERN_INFO
Brice Goglin798a95d2007-06-11 20:26:50 +02001482 "myri10ge: %s: link %s\n",
1483 mgp->dev->name,
1484 (link_up == MXGEFW_LINK_MYRINET ?
1485 "mismatch (Myrinet detected)" :
1486 "down"));
Brice Goglin0da34b62006-05-23 06:10:15 -04001487 netif_carrier_off(mgp->dev);
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001488 mgp->link_changes++;
Brice Goglin0da34b62006-05-23 06:10:15 -04001489 }
1490 }
1491 if (mgp->rdma_tags_available !=
Brice Goglinb53bef82008-05-09 02:20:03 +02001492 ntohl(stats->rdma_tags_available)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04001493 mgp->rdma_tags_available =
Brice Goglinb53bef82008-05-09 02:20:03 +02001494 ntohl(stats->rdma_tags_available);
Brice Goglin0da34b62006-05-23 06:10:15 -04001495 printk(KERN_WARNING "myri10ge: %s: RDMA timed out! "
1496 "%d tags left\n", mgp->dev->name,
1497 mgp->rdma_tags_available);
1498 }
1499 mgp->down_cnt += stats->link_down;
1500 if (stats->link_down)
1501 wake_up(&mgp->down_wq);
1502 }
1503}
1504
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001505static int myri10ge_poll(struct napi_struct *napi, int budget)
Brice Goglin0da34b62006-05-23 06:10:15 -04001506{
Brice Goglinb53bef82008-05-09 02:20:03 +02001507 struct myri10ge_slice_state *ss =
1508 container_of(napi, struct myri10ge_slice_state, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001509 int work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001510
Jeff Garzik5dd2d332008-10-16 05:09:31 -04001511#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02001512 if (ss->mgp->dca_enabled)
1513 myri10ge_update_dca(ss);
1514#endif
1515
Brice Goglin0da34b62006-05-23 06:10:15 -04001516 /* process as many rx events as NAPI will allow */
Brice Goglinb53bef82008-05-09 02:20:03 +02001517 work_done = myri10ge_clean_rx_done(ss, budget);
Brice Goglin0da34b62006-05-23 06:10:15 -04001518
David S. Miller4ec24112008-01-07 20:48:21 -08001519 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001520 napi_complete(napi);
Brice Goglinb53bef82008-05-09 02:20:03 +02001521 put_be32(htonl(3), ss->irq_claim);
Brice Goglin0da34b62006-05-23 06:10:15 -04001522 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001523 return work_done;
Brice Goglin0da34b62006-05-23 06:10:15 -04001524}
1525
David Howells7d12e782006-10-05 14:55:46 +01001526static irqreturn_t myri10ge_intr(int irq, void *arg)
Brice Goglin0da34b62006-05-23 06:10:15 -04001527{
Brice Goglinb53bef82008-05-09 02:20:03 +02001528 struct myri10ge_slice_state *ss = arg;
1529 struct myri10ge_priv *mgp = ss->mgp;
1530 struct mcp_irq_data *stats = ss->fw_stats;
1531 struct myri10ge_tx_buf *tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04001532 u32 send_done_count;
1533 int i;
1534
Brice Goglin236bb5e2008-09-28 15:34:21 +00001535 /* an interrupt on a non-zero receive-only slice is implicitly
1536 * valid since MSI-X irqs are not shared */
1537 if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001538 napi_schedule(&ss->napi);
Brice Goglin0dcffac2008-05-09 02:21:49 +02001539 return (IRQ_HANDLED);
1540 }
1541
Brice Goglin0da34b62006-05-23 06:10:15 -04001542 /* make sure it is our IRQ, and that the DMA has finished */
1543 if (unlikely(!stats->valid))
1544 return (IRQ_NONE);
1545
1546 /* low bit indicates receives are present, so schedule
1547 * napi poll handler */
1548 if (stats->valid & 1)
Ben Hutchings288379f2009-01-19 16:43:59 -08001549 napi_schedule(&ss->napi);
Brice Goglin0da34b62006-05-23 06:10:15 -04001550
Brice Goglin0dcffac2008-05-09 02:21:49 +02001551 if (!mgp->msi_enabled && !mgp->msix_enabled) {
Al Viro40f6cff2006-11-20 13:48:32 -05001552 put_be32(0, mgp->irq_deassert);
Brice Goglin0da34b62006-05-23 06:10:15 -04001553 if (!myri10ge_deassert_wait)
1554 stats->valid = 0;
1555 mb();
1556 } else
1557 stats->valid = 0;
1558
1559 /* Wait for IRQ line to go low, if using INTx */
1560 i = 0;
1561 while (1) {
1562 i++;
1563 /* check for transmit completes and receives */
1564 send_done_count = ntohl(stats->send_done_count);
1565 if (send_done_count != tx->pkt_done)
Brice Goglinb53bef82008-05-09 02:20:03 +02001566 myri10ge_tx_done(ss, (int)send_done_count);
Brice Goglin0da34b62006-05-23 06:10:15 -04001567 if (unlikely(i > myri10ge_max_irq_loops)) {
1568 printk(KERN_WARNING "myri10ge: %s: irq stuck?\n",
1569 mgp->dev->name);
1570 stats->valid = 0;
1571 schedule_work(&mgp->watchdog_work);
1572 }
1573 if (likely(stats->valid == 0))
1574 break;
1575 cpu_relax();
1576 barrier();
1577 }
1578
Brice Goglin236bb5e2008-09-28 15:34:21 +00001579 /* Only slice 0 updates stats */
1580 if (ss == mgp->ss)
1581 myri10ge_check_statblock(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -04001582
Brice Goglinb53bef82008-05-09 02:20:03 +02001583 put_be32(htonl(3), ss->irq_claim + 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04001584 return (IRQ_HANDLED);
1585}
1586
1587static int
1588myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1589{
Brice Goglinc0bf8802008-05-09 02:18:24 +02001590 struct myri10ge_priv *mgp = netdev_priv(netdev);
1591 char *ptr;
1592 int i;
1593
Brice Goglin0da34b62006-05-23 06:10:15 -04001594 cmd->autoneg = AUTONEG_DISABLE;
1595 cmd->speed = SPEED_10000;
1596 cmd->duplex = DUPLEX_FULL;
Brice Goglinc0bf8802008-05-09 02:18:24 +02001597
1598 /*
1599 * parse the product code to deterimine the interface type
1600 * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
1601 * after the 3rd dash in the driver's cached copy of the
1602 * EEPROM's product code string.
1603 */
1604 ptr = mgp->product_code_string;
1605 if (ptr == NULL) {
1606 printk(KERN_ERR "myri10ge: %s: Missing product code\n",
Brice Goglin99f5f872008-05-09 02:19:08 +02001607 netdev->name);
Brice Goglinc0bf8802008-05-09 02:18:24 +02001608 return 0;
1609 }
1610 for (i = 0; i < 3; i++, ptr++) {
1611 ptr = strchr(ptr, '-');
1612 if (ptr == NULL) {
1613 printk(KERN_ERR "myri10ge: %s: Invalid product "
1614 "code %s\n", netdev->name,
1615 mgp->product_code_string);
1616 return 0;
1617 }
1618 }
1619 if (*ptr == 'R' || *ptr == 'Q') {
1620 /* We've found either an XFP or quad ribbon fiber */
1621 cmd->port = PORT_FIBRE;
1622 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001623 return 0;
1624}
1625
1626static void
1627myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
1628{
1629 struct myri10ge_priv *mgp = netdev_priv(netdev);
1630
1631 strlcpy(info->driver, "myri10ge", sizeof(info->driver));
1632 strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
1633 strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
1634 strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
1635}
1636
1637static int
1638myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1639{
1640 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001641
Brice Goglin0da34b62006-05-23 06:10:15 -04001642 coal->rx_coalesce_usecs = mgp->intr_coal_delay;
1643 return 0;
1644}
1645
1646static int
1647myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1648{
1649 struct myri10ge_priv *mgp = netdev_priv(netdev);
1650
1651 mgp->intr_coal_delay = coal->rx_coalesce_usecs;
Al Viro40f6cff2006-11-20 13:48:32 -05001652 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
Brice Goglin0da34b62006-05-23 06:10:15 -04001653 return 0;
1654}
1655
1656static void
1657myri10ge_get_pauseparam(struct net_device *netdev,
1658 struct ethtool_pauseparam *pause)
1659{
1660 struct myri10ge_priv *mgp = netdev_priv(netdev);
1661
1662 pause->autoneg = 0;
1663 pause->rx_pause = mgp->pause;
1664 pause->tx_pause = mgp->pause;
1665}
1666
1667static int
1668myri10ge_set_pauseparam(struct net_device *netdev,
1669 struct ethtool_pauseparam *pause)
1670{
1671 struct myri10ge_priv *mgp = netdev_priv(netdev);
1672
1673 if (pause->tx_pause != mgp->pause)
1674 return myri10ge_change_pause(mgp, pause->tx_pause);
1675 if (pause->rx_pause != mgp->pause)
1676 return myri10ge_change_pause(mgp, pause->tx_pause);
1677 if (pause->autoneg != 0)
1678 return -EINVAL;
1679 return 0;
1680}
1681
1682static void
1683myri10ge_get_ringparam(struct net_device *netdev,
1684 struct ethtool_ringparam *ring)
1685{
1686 struct myri10ge_priv *mgp = netdev_priv(netdev);
1687
Brice Goglin0dcffac2008-05-09 02:21:49 +02001688 ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
1689 ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001690 ring->rx_jumbo_max_pending = 0;
Brice Goglin0dcffac2008-05-09 02:21:49 +02001691 ring->tx_max_pending = mgp->ss[0].rx_small.mask + 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001692 ring->rx_mini_pending = ring->rx_mini_max_pending;
1693 ring->rx_pending = ring->rx_max_pending;
1694 ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
1695 ring->tx_pending = ring->tx_max_pending;
1696}
1697
1698static u32 myri10ge_get_rx_csum(struct net_device *netdev)
1699{
1700 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001701
Brice Goglin0da34b62006-05-23 06:10:15 -04001702 if (mgp->csum_flag)
1703 return 1;
1704 else
1705 return 0;
1706}
1707
1708static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
1709{
1710 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglin99f5f872008-05-09 02:19:08 +02001711
Brice Goglin0da34b62006-05-23 06:10:15 -04001712 if (csum_enabled)
1713 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
1714 else
1715 mgp->csum_flag = 0;
1716 return 0;
1717}
1718
Brice Goglin4f93fde2007-10-13 12:34:01 +02001719static int myri10ge_set_tso(struct net_device *netdev, u32 tso_enabled)
1720{
1721 struct myri10ge_priv *mgp = netdev_priv(netdev);
1722 unsigned long flags = mgp->features & (NETIF_F_TSO6 | NETIF_F_TSO);
1723
1724 if (tso_enabled)
1725 netdev->features |= flags;
1726 else
1727 netdev->features &= ~flags;
1728 return 0;
1729}
1730
Brice Goglinb53bef82008-05-09 02:20:03 +02001731static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
Brice Goglin0da34b62006-05-23 06:10:15 -04001732 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
1733 "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
1734 "rx_length_errors", "rx_over_errors", "rx_crc_errors",
1735 "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
1736 "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
1737 "tx_heartbeat_errors", "tx_window_errors",
1738 /* device-specific stats */
Brice Goglin0dcffac2008-05-09 02:21:49 +02001739 "tx_boundary", "WC", "irq", "MSI", "MSIX",
Brice Goglin0da34b62006-05-23 06:10:15 -04001740 "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
Brice Goglinb53bef82008-05-09 02:20:03 +02001741 "serial_number", "watchdog_resets",
Jeff Garzik5dd2d332008-10-16 05:09:31 -04001742#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin9a6b3b52008-09-12 19:48:06 +02001743 "dca_capable_firmware", "dca_device_present",
Brice Goglin981813d2008-05-09 02:22:16 +02001744#endif
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001745 "link_changes", "link_up", "dropped_link_overflow",
Brice Goglincee505d2007-05-07 23:49:25 +02001746 "dropped_link_error_or_filtered",
1747 "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
1748 "dropped_unicast_filtered", "dropped_multicast_filtered",
Brice Goglin0da34b62006-05-23 06:10:15 -04001749 "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
Brice Goglinb53bef82008-05-09 02:20:03 +02001750 "dropped_no_big_buffer"
1751};
1752
1753static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
1754 "----------- slice ---------",
1755 "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
1756 "rx_small_cnt", "rx_big_cnt",
1757 "wake_queue", "stop_queue", "tx_linearized", "LRO aggregated",
1758 "LRO flushed",
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07001759 "LRO avg aggr", "LRO no_desc"
Brice Goglin0da34b62006-05-23 06:10:15 -04001760};
1761
1762#define MYRI10GE_NET_STATS_LEN 21
Brice Goglinb53bef82008-05-09 02:20:03 +02001763#define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
1764#define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
Brice Goglin0da34b62006-05-23 06:10:15 -04001765
1766static void
1767myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
1768{
Brice Goglin0dcffac2008-05-09 02:21:49 +02001769 struct myri10ge_priv *mgp = netdev_priv(netdev);
1770 int i;
1771
Brice Goglin0da34b62006-05-23 06:10:15 -04001772 switch (stringset) {
1773 case ETH_SS_STATS:
Brice Goglinb53bef82008-05-09 02:20:03 +02001774 memcpy(data, *myri10ge_gstrings_main_stats,
1775 sizeof(myri10ge_gstrings_main_stats));
1776 data += sizeof(myri10ge_gstrings_main_stats);
Brice Goglin0dcffac2008-05-09 02:21:49 +02001777 for (i = 0; i < mgp->num_slices; i++) {
1778 memcpy(data, *myri10ge_gstrings_slice_stats,
1779 sizeof(myri10ge_gstrings_slice_stats));
1780 data += sizeof(myri10ge_gstrings_slice_stats);
1781 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001782 break;
1783 }
1784}
1785
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001786static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
Brice Goglin0da34b62006-05-23 06:10:15 -04001787{
Brice Goglin0dcffac2008-05-09 02:21:49 +02001788 struct myri10ge_priv *mgp = netdev_priv(netdev);
1789
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001790 switch (sset) {
1791 case ETH_SS_STATS:
Brice Goglin0dcffac2008-05-09 02:21:49 +02001792 return MYRI10GE_MAIN_STATS_LEN +
1793 mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001794 default:
1795 return -EOPNOTSUPP;
1796 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001797}
1798
1799static void
1800myri10ge_get_ethtool_stats(struct net_device *netdev,
1801 struct ethtool_stats *stats, u64 * data)
1802{
1803 struct myri10ge_priv *mgp = netdev_priv(netdev);
Brice Goglinb53bef82008-05-09 02:20:03 +02001804 struct myri10ge_slice_state *ss;
Brice Goglin0dcffac2008-05-09 02:21:49 +02001805 int slice;
Brice Goglin0da34b62006-05-23 06:10:15 -04001806 int i;
1807
Brice Goglin59081822009-04-16 02:23:56 +00001808 /* force stats update */
1809 (void)myri10ge_get_stats(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04001810 for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1811 data[i] = ((unsigned long *)&mgp->stats)[i];
1812
Brice Goglinb53bef82008-05-09 02:20:03 +02001813 data[i++] = (unsigned int)mgp->tx_boundary;
Brice Goglin276e26c2007-03-07 20:02:32 +01001814 data[i++] = (unsigned int)mgp->wc_enabled;
Brice Goglin2c1a1082006-07-03 18:16:46 -04001815 data[i++] = (unsigned int)mgp->pdev->irq;
1816 data[i++] = (unsigned int)mgp->msi_enabled;
Brice Goglin0dcffac2008-05-09 02:21:49 +02001817 data[i++] = (unsigned int)mgp->msix_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04001818 data[i++] = (unsigned int)mgp->read_dma;
1819 data[i++] = (unsigned int)mgp->write_dma;
1820 data[i++] = (unsigned int)mgp->read_write_dma;
1821 data[i++] = (unsigned int)mgp->serial_number;
Brice Goglin0da34b62006-05-23 06:10:15 -04001822 data[i++] = (unsigned int)mgp->watchdog_resets;
Jeff Garzik5dd2d332008-10-16 05:09:31 -04001823#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02001824 data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
1825 data[i++] = (unsigned int)(mgp->dca_enabled);
1826#endif
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001827 data[i++] = (unsigned int)mgp->link_changes;
Brice Goglinb53bef82008-05-09 02:20:03 +02001828
1829 /* firmware stats are useful only in the first slice */
Brice Goglin0dcffac2008-05-09 02:21:49 +02001830 ss = &mgp->ss[0];
Brice Goglinb53bef82008-05-09 02:20:03 +02001831 data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
1832 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
Brice Goglin0da34b62006-05-23 06:10:15 -04001833 data[i++] =
Brice Goglinb53bef82008-05-09 02:20:03 +02001834 (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
1835 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
1836 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
1837 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
1838 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
Brice Goglincee505d2007-05-07 23:49:25 +02001839 data[i++] =
Brice Goglinb53bef82008-05-09 02:20:03 +02001840 (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
1841 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
1842 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
1843 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
1844 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
1845
Brice Goglin0dcffac2008-05-09 02:21:49 +02001846 for (slice = 0; slice < mgp->num_slices; slice++) {
1847 ss = &mgp->ss[slice];
1848 data[i++] = slice;
1849 data[i++] = (unsigned int)ss->tx.pkt_start;
1850 data[i++] = (unsigned int)ss->tx.pkt_done;
1851 data[i++] = (unsigned int)ss->tx.req;
1852 data[i++] = (unsigned int)ss->tx.done;
1853 data[i++] = (unsigned int)ss->rx_small.cnt;
1854 data[i++] = (unsigned int)ss->rx_big.cnt;
1855 data[i++] = (unsigned int)ss->tx.wake_queue;
1856 data[i++] = (unsigned int)ss->tx.stop_queue;
1857 data[i++] = (unsigned int)ss->tx.linearized;
1858 data[i++] = ss->rx_done.lro_mgr.stats.aggregated;
1859 data[i++] = ss->rx_done.lro_mgr.stats.flushed;
1860 if (ss->rx_done.lro_mgr.stats.flushed)
1861 data[i++] = ss->rx_done.lro_mgr.stats.aggregated /
1862 ss->rx_done.lro_mgr.stats.flushed;
1863 else
1864 data[i++] = 0;
1865 data[i++] = ss->rx_done.lro_mgr.stats.no_desc;
1866 }
Brice Goglin0da34b62006-05-23 06:10:15 -04001867}
1868
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001869static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
1870{
1871 struct myri10ge_priv *mgp = netdev_priv(netdev);
1872 mgp->msg_enable = value;
1873}
1874
1875static u32 myri10ge_get_msglevel(struct net_device *netdev)
1876{
1877 struct myri10ge_priv *mgp = netdev_priv(netdev);
1878 return mgp->msg_enable;
1879}
1880
Jeff Garzik7282d492006-09-13 14:30:00 -04001881static const struct ethtool_ops myri10ge_ethtool_ops = {
Brice Goglin0da34b62006-05-23 06:10:15 -04001882 .get_settings = myri10ge_get_settings,
1883 .get_drvinfo = myri10ge_get_drvinfo,
1884 .get_coalesce = myri10ge_get_coalesce,
1885 .set_coalesce = myri10ge_set_coalesce,
1886 .get_pauseparam = myri10ge_get_pauseparam,
1887 .set_pauseparam = myri10ge_set_pauseparam,
1888 .get_ringparam = myri10ge_get_ringparam,
1889 .get_rx_csum = myri10ge_get_rx_csum,
1890 .set_rx_csum = myri10ge_set_rx_csum,
Brice Goglinb10c0662006-06-08 10:25:00 -04001891 .set_tx_csum = ethtool_op_set_tx_hw_csum,
Brice Goglin0da34b62006-05-23 06:10:15 -04001892 .set_sg = ethtool_op_set_sg,
Brice Goglin4f93fde2007-10-13 12:34:01 +02001893 .set_tso = myri10ge_set_tso,
Brice Goglin6ffdd072007-05-30 21:13:59 +02001894 .get_link = ethtool_op_get_link,
Brice Goglin0da34b62006-05-23 06:10:15 -04001895 .get_strings = myri10ge_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001896 .get_sset_count = myri10ge_get_sset_count,
Brice Goglinc58ac5c2006-08-21 17:36:49 -04001897 .get_ethtool_stats = myri10ge_get_ethtool_stats,
1898 .set_msglevel = myri10ge_set_msglevel,
1899 .get_msglevel = myri10ge_get_msglevel
Brice Goglin0da34b62006-05-23 06:10:15 -04001900};
1901
Brice Goglinb53bef82008-05-09 02:20:03 +02001902static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
Brice Goglin0da34b62006-05-23 06:10:15 -04001903{
Brice Goglinb53bef82008-05-09 02:20:03 +02001904 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04001905 struct myri10ge_cmd cmd;
Brice Goglinb53bef82008-05-09 02:20:03 +02001906 struct net_device *dev = mgp->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -04001907 int tx_ring_size, rx_ring_size;
1908 int tx_ring_entries, rx_ring_entries;
Brice Goglin0dcffac2008-05-09 02:21:49 +02001909 int i, slice, status;
Brice Goglin0da34b62006-05-23 06:10:15 -04001910 size_t bytes;
1911
Brice Goglin0da34b62006-05-23 06:10:15 -04001912 /* get ring sizes */
Brice Goglin0dcffac2008-05-09 02:21:49 +02001913 slice = ss - mgp->ss;
1914 cmd.data0 = slice;
Brice Goglin0da34b62006-05-23 06:10:15 -04001915 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
1916 tx_ring_size = cmd.data0;
Brice Goglin0dcffac2008-05-09 02:21:49 +02001917 cmd.data0 = slice;
Brice Goglin0da34b62006-05-23 06:10:15 -04001918 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
Brice Goglin355c7262007-03-07 19:59:52 +01001919 if (status != 0)
1920 return status;
Brice Goglin0da34b62006-05-23 06:10:15 -04001921 rx_ring_size = cmd.data0;
1922
1923 tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
1924 rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
Brice Goglinb53bef82008-05-09 02:20:03 +02001925 ss->tx.mask = tx_ring_entries - 1;
1926 ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04001927
Brice Goglin355c7262007-03-07 19:59:52 +01001928 status = -ENOMEM;
1929
Brice Goglin0da34b62006-05-23 06:10:15 -04001930 /* allocate the host shadow rings */
1931
1932 bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
Brice Goglinb53bef82008-05-09 02:20:03 +02001933 * sizeof(*ss->tx.req_list);
1934 ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
1935 if (ss->tx.req_bytes == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001936 goto abort_with_nothing;
1937
1938 /* ensure req_list entries are aligned to 8 bytes */
Brice Goglinb53bef82008-05-09 02:20:03 +02001939 ss->tx.req_list = (struct mcp_kreq_ether_send *)
1940 ALIGN((unsigned long)ss->tx.req_bytes, 8);
Brice Goglin236bb5e2008-09-28 15:34:21 +00001941 ss->tx.queue_active = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04001942
Brice Goglinb53bef82008-05-09 02:20:03 +02001943 bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
1944 ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
1945 if (ss->rx_small.shadow == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001946 goto abort_with_tx_req_bytes;
1947
Brice Goglinb53bef82008-05-09 02:20:03 +02001948 bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
1949 ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
1950 if (ss->rx_big.shadow == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001951 goto abort_with_rx_small_shadow;
1952
1953 /* allocate the host info rings */
1954
Brice Goglinb53bef82008-05-09 02:20:03 +02001955 bytes = tx_ring_entries * sizeof(*ss->tx.info);
1956 ss->tx.info = kzalloc(bytes, GFP_KERNEL);
1957 if (ss->tx.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001958 goto abort_with_rx_big_shadow;
1959
Brice Goglinb53bef82008-05-09 02:20:03 +02001960 bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
1961 ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
1962 if (ss->rx_small.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001963 goto abort_with_tx_info;
1964
Brice Goglinb53bef82008-05-09 02:20:03 +02001965 bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
1966 ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
1967 if (ss->rx_big.info == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04001968 goto abort_with_rx_small_info;
1969
1970 /* Fill the receive rings */
Brice Goglinb53bef82008-05-09 02:20:03 +02001971 ss->rx_big.cnt = 0;
1972 ss->rx_small.cnt = 0;
1973 ss->rx_big.fill_cnt = 0;
1974 ss->rx_small.fill_cnt = 0;
1975 ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
1976 ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
1977 ss->rx_small.watchdog_needed = 0;
1978 ss->rx_big.watchdog_needed = 0;
1979 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
Brice Goglinc7dab992006-12-11 11:25:42 +01001980 mgp->small_bytes + MXGEFW_PAD, 0);
Brice Goglin0da34b62006-05-23 06:10:15 -04001981
Brice Goglinb53bef82008-05-09 02:20:03 +02001982 if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
Brice Goglin0dcffac2008-05-09 02:21:49 +02001983 printk(KERN_ERR
1984 "myri10ge: %s:slice-%d: alloced only %d small bufs\n",
1985 dev->name, slice, ss->rx_small.fill_cnt);
Brice Goglinc7dab992006-12-11 11:25:42 +01001986 goto abort_with_rx_small_ring;
Brice Goglin0da34b62006-05-23 06:10:15 -04001987 }
1988
Brice Goglinb53bef82008-05-09 02:20:03 +02001989 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
1990 if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
Brice Goglin0dcffac2008-05-09 02:21:49 +02001991 printk(KERN_ERR
1992 "myri10ge: %s:slice-%d: alloced only %d big bufs\n",
1993 dev->name, slice, ss->rx_big.fill_cnt);
Brice Goglinc7dab992006-12-11 11:25:42 +01001994 goto abort_with_rx_big_ring;
Brice Goglin0da34b62006-05-23 06:10:15 -04001995 }
1996
1997 return 0;
1998
1999abort_with_rx_big_ring:
Brice Goglinb53bef82008-05-09 02:20:03 +02002000 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
2001 int idx = i & ss->rx_big.mask;
2002 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01002003 mgp->big_bytes);
Brice Goglinb53bef82008-05-09 02:20:03 +02002004 put_page(ss->rx_big.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04002005 }
2006
2007abort_with_rx_small_ring:
Brice Goglinb53bef82008-05-09 02:20:03 +02002008 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
2009 int idx = i & ss->rx_small.mask;
2010 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01002011 mgp->small_bytes + MXGEFW_PAD);
Brice Goglinb53bef82008-05-09 02:20:03 +02002012 put_page(ss->rx_small.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04002013 }
Brice Goglinc7dab992006-12-11 11:25:42 +01002014
Brice Goglinb53bef82008-05-09 02:20:03 +02002015 kfree(ss->rx_big.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04002016
2017abort_with_rx_small_info:
Brice Goglinb53bef82008-05-09 02:20:03 +02002018 kfree(ss->rx_small.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04002019
2020abort_with_tx_info:
Brice Goglinb53bef82008-05-09 02:20:03 +02002021 kfree(ss->tx.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04002022
2023abort_with_rx_big_shadow:
Brice Goglinb53bef82008-05-09 02:20:03 +02002024 kfree(ss->rx_big.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04002025
2026abort_with_rx_small_shadow:
Brice Goglinb53bef82008-05-09 02:20:03 +02002027 kfree(ss->rx_small.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04002028
2029abort_with_tx_req_bytes:
Brice Goglinb53bef82008-05-09 02:20:03 +02002030 kfree(ss->tx.req_bytes);
2031 ss->tx.req_bytes = NULL;
2032 ss->tx.req_list = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04002033
2034abort_with_nothing:
2035 return status;
2036}
2037
Brice Goglinb53bef82008-05-09 02:20:03 +02002038static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
Brice Goglin0da34b62006-05-23 06:10:15 -04002039{
Brice Goglinb53bef82008-05-09 02:20:03 +02002040 struct myri10ge_priv *mgp = ss->mgp;
Brice Goglin0da34b62006-05-23 06:10:15 -04002041 struct sk_buff *skb;
2042 struct myri10ge_tx_buf *tx;
2043 int i, len, idx;
2044
Brice Goglin0dcffac2008-05-09 02:21:49 +02002045 /* If not allocated, skip it */
2046 if (ss->tx.req_list == NULL)
2047 return;
2048
Brice Goglinb53bef82008-05-09 02:20:03 +02002049 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
2050 idx = i & ss->rx_big.mask;
2051 if (i == ss->rx_big.fill_cnt - 1)
2052 ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
2053 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01002054 mgp->big_bytes);
Brice Goglinb53bef82008-05-09 02:20:03 +02002055 put_page(ss->rx_big.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04002056 }
2057
Brice Goglinb53bef82008-05-09 02:20:03 +02002058 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
2059 idx = i & ss->rx_small.mask;
2060 if (i == ss->rx_small.fill_cnt - 1)
2061 ss->rx_small.info[idx].page_offset =
Brice Goglinc7dab992006-12-11 11:25:42 +01002062 MYRI10GE_ALLOC_SIZE;
Brice Goglinb53bef82008-05-09 02:20:03 +02002063 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
Brice Goglinc7dab992006-12-11 11:25:42 +01002064 mgp->small_bytes + MXGEFW_PAD);
Brice Goglinb53bef82008-05-09 02:20:03 +02002065 put_page(ss->rx_small.info[idx].page);
Brice Goglin0da34b62006-05-23 06:10:15 -04002066 }
Brice Goglinb53bef82008-05-09 02:20:03 +02002067 tx = &ss->tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002068 while (tx->done != tx->req) {
2069 idx = tx->done & tx->mask;
2070 skb = tx->info[idx].skb;
2071
2072 /* Mark as free */
2073 tx->info[idx].skb = NULL;
2074 tx->done++;
2075 len = pci_unmap_len(&tx->info[idx], len);
2076 pci_unmap_len_set(&tx->info[idx], len, 0);
2077 if (skb) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002078 ss->stats.tx_dropped++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002079 dev_kfree_skb_any(skb);
2080 if (len)
2081 pci_unmap_single(mgp->pdev,
2082 pci_unmap_addr(&tx->info[idx],
2083 bus), len,
2084 PCI_DMA_TODEVICE);
2085 } else {
2086 if (len)
2087 pci_unmap_page(mgp->pdev,
2088 pci_unmap_addr(&tx->info[idx],
2089 bus), len,
2090 PCI_DMA_TODEVICE);
2091 }
2092 }
Brice Goglinb53bef82008-05-09 02:20:03 +02002093 kfree(ss->rx_big.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04002094
Brice Goglinb53bef82008-05-09 02:20:03 +02002095 kfree(ss->rx_small.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04002096
Brice Goglinb53bef82008-05-09 02:20:03 +02002097 kfree(ss->tx.info);
Brice Goglin0da34b62006-05-23 06:10:15 -04002098
Brice Goglinb53bef82008-05-09 02:20:03 +02002099 kfree(ss->rx_big.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04002100
Brice Goglinb53bef82008-05-09 02:20:03 +02002101 kfree(ss->rx_small.shadow);
Brice Goglin0da34b62006-05-23 06:10:15 -04002102
Brice Goglinb53bef82008-05-09 02:20:03 +02002103 kfree(ss->tx.req_bytes);
2104 ss->tx.req_bytes = NULL;
2105 ss->tx.req_list = NULL;
Brice Goglin0da34b62006-05-23 06:10:15 -04002106}
2107
Brice Goglindf30a742006-12-18 11:50:40 +01002108static int myri10ge_request_irq(struct myri10ge_priv *mgp)
2109{
2110 struct pci_dev *pdev = mgp->pdev;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002111 struct myri10ge_slice_state *ss;
2112 struct net_device *netdev = mgp->dev;
2113 int i;
Brice Goglindf30a742006-12-18 11:50:40 +01002114 int status;
2115
Brice Goglin0dcffac2008-05-09 02:21:49 +02002116 mgp->msi_enabled = 0;
2117 mgp->msix_enabled = 0;
2118 status = 0;
Brice Goglindf30a742006-12-18 11:50:40 +01002119 if (myri10ge_msi) {
Brice Goglin0dcffac2008-05-09 02:21:49 +02002120 if (mgp->num_slices > 1) {
2121 status =
2122 pci_enable_msix(pdev, mgp->msix_vectors,
2123 mgp->num_slices);
2124 if (status == 0) {
2125 mgp->msix_enabled = 1;
2126 } else {
2127 dev_err(&pdev->dev,
2128 "Error %d setting up MSI-X\n", status);
2129 return status;
2130 }
2131 }
2132 if (mgp->msix_enabled == 0) {
2133 status = pci_enable_msi(pdev);
2134 if (status != 0) {
2135 dev_err(&pdev->dev,
2136 "Error %d setting up MSI; falling back to xPIC\n",
2137 status);
2138 } else {
2139 mgp->msi_enabled = 1;
2140 }
2141 }
Brice Goglindf30a742006-12-18 11:50:40 +01002142 }
Brice Goglin0dcffac2008-05-09 02:21:49 +02002143 if (mgp->msix_enabled) {
2144 for (i = 0; i < mgp->num_slices; i++) {
2145 ss = &mgp->ss[i];
2146 snprintf(ss->irq_desc, sizeof(ss->irq_desc),
2147 "%s:slice-%d", netdev->name, i);
2148 status = request_irq(mgp->msix_vectors[i].vector,
2149 myri10ge_intr, 0, ss->irq_desc,
2150 ss);
2151 if (status != 0) {
2152 dev_err(&pdev->dev,
2153 "slice %d failed to allocate IRQ\n", i);
2154 i--;
2155 while (i >= 0) {
2156 free_irq(mgp->msix_vectors[i].vector,
2157 &mgp->ss[i]);
2158 i--;
2159 }
2160 pci_disable_msix(pdev);
2161 return status;
2162 }
2163 }
2164 } else {
2165 status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
2166 mgp->dev->name, &mgp->ss[0]);
2167 if (status != 0) {
2168 dev_err(&pdev->dev, "failed to allocate IRQ\n");
2169 if (mgp->msi_enabled)
2170 pci_disable_msi(pdev);
2171 }
Brice Goglindf30a742006-12-18 11:50:40 +01002172 }
2173 return status;
2174}
2175
2176static void myri10ge_free_irq(struct myri10ge_priv *mgp)
2177{
2178 struct pci_dev *pdev = mgp->pdev;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002179 int i;
Brice Goglindf30a742006-12-18 11:50:40 +01002180
Brice Goglin0dcffac2008-05-09 02:21:49 +02002181 if (mgp->msix_enabled) {
2182 for (i = 0; i < mgp->num_slices; i++)
2183 free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
2184 } else {
2185 free_irq(pdev->irq, &mgp->ss[0]);
2186 }
Brice Goglindf30a742006-12-18 11:50:40 +01002187 if (mgp->msi_enabled)
2188 pci_disable_msi(pdev);
Brice Goglin0dcffac2008-05-09 02:21:49 +02002189 if (mgp->msix_enabled)
2190 pci_disable_msix(pdev);
Brice Goglindf30a742006-12-18 11:50:40 +01002191}
2192
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002193static int
2194myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
2195 void **ip_hdr, void **tcpudp_hdr,
2196 u64 * hdr_flags, void *priv)
2197{
2198 struct ethhdr *eh;
2199 struct vlan_ethhdr *veh;
2200 struct iphdr *iph;
2201 u8 *va = page_address(frag->page) + frag->page_offset;
2202 unsigned long ll_hlen;
Al Viro66341ff2007-12-22 18:56:43 +00002203 /* passed opaque through lro_receive_frags() */
2204 __wsum csum = (__force __wsum) (unsigned long)priv;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002205
2206 /* find the mac header, aborting if not IPv4 */
2207
2208 eh = (struct ethhdr *)va;
2209 *mac_hdr = eh;
2210 ll_hlen = ETH_HLEN;
2211 if (eh->h_proto != htons(ETH_P_IP)) {
2212 if (eh->h_proto == htons(ETH_P_8021Q)) {
2213 veh = (struct vlan_ethhdr *)va;
2214 if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
2215 return -1;
2216
2217 ll_hlen += VLAN_HLEN;
2218
2219 /*
2220 * HW checksum starts ETH_HLEN bytes into
2221 * frame, so we must subtract off the VLAN
2222 * header's checksum before csum can be used
2223 */
2224 csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
2225 VLAN_HLEN, 0));
2226 } else {
2227 return -1;
2228 }
2229 }
2230 *hdr_flags = LRO_IPV4;
2231
2232 iph = (struct iphdr *)(va + ll_hlen);
2233 *ip_hdr = iph;
2234 if (iph->protocol != IPPROTO_TCP)
2235 return -1;
Brice Goglinbcb09dc2008-12-09 00:14:27 -08002236 if (iph->frag_off & htons(IP_MF | IP_OFFSET))
2237 return -1;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002238 *hdr_flags |= LRO_TCP;
2239 *tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);
2240
2241 /* verify the IP checksum */
2242 if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
2243 return -1;
2244
2245 /* verify the checksum */
2246 if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
2247 ntohs(iph->tot_len) - (iph->ihl << 2),
2248 IPPROTO_TCP, csum)))
2249 return -1;
2250
2251 return 0;
2252}
2253
Brice Goglin77929732008-05-09 02:21:10 +02002254static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
2255{
2256 struct myri10ge_cmd cmd;
2257 struct myri10ge_slice_state *ss;
2258 int status;
2259
2260 ss = &mgp->ss[slice];
Brice Goglin236bb5e2008-09-28 15:34:21 +00002261 status = 0;
2262 if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
2263 cmd.data0 = slice;
2264 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
2265 &cmd, 0);
2266 ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
2267 (mgp->sram + cmd.data0);
2268 }
Brice Goglin77929732008-05-09 02:21:10 +02002269 cmd.data0 = slice;
2270 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
2271 &cmd, 0);
2272 ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
2273 (mgp->sram + cmd.data0);
2274
2275 cmd.data0 = slice;
2276 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
2277 ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
2278 (mgp->sram + cmd.data0);
2279
Brice Goglin236bb5e2008-09-28 15:34:21 +00002280 ss->tx.send_go = (__iomem __be32 *)
2281 (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
2282 ss->tx.send_stop = (__iomem __be32 *)
2283 (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
Brice Goglin77929732008-05-09 02:21:10 +02002284 return status;
2285
2286}
2287
2288static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
2289{
2290 struct myri10ge_cmd cmd;
2291 struct myri10ge_slice_state *ss;
2292 int status;
2293
2294 ss = &mgp->ss[slice];
2295 cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
2296 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
Brice Goglin236bb5e2008-09-28 15:34:21 +00002297 cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
Brice Goglin77929732008-05-09 02:21:10 +02002298 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
2299 if (status == -ENOSYS) {
2300 dma_addr_t bus = ss->fw_stats_bus;
2301 if (slice != 0)
2302 return -EINVAL;
2303 bus += offsetof(struct mcp_irq_data, send_done_count);
2304 cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
2305 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
2306 status = myri10ge_send_cmd(mgp,
2307 MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
2308 &cmd, 0);
2309 /* Firmware cannot support multicast without STATS_DMA_V2 */
2310 mgp->fw_multicast_support = 0;
2311 } else {
2312 mgp->fw_multicast_support = 1;
2313 }
2314 return 0;
2315}
Brice Goglin77929732008-05-09 02:21:10 +02002316
Brice Goglin0da34b62006-05-23 06:10:15 -04002317static int myri10ge_open(struct net_device *dev)
2318{
Brice Goglin0dcffac2008-05-09 02:21:49 +02002319 struct myri10ge_slice_state *ss;
Brice Goglinb53bef82008-05-09 02:20:03 +02002320 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002321 struct myri10ge_cmd cmd;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002322 int i, status, big_pow2, slice;
2323 u8 *itable;
Andrew Gallatin1e6e9342007-09-17 11:37:42 -07002324 struct net_lro_mgr *lro_mgr;
Brice Goglin0da34b62006-05-23 06:10:15 -04002325
Brice Goglin0da34b62006-05-23 06:10:15 -04002326 if (mgp->running != MYRI10GE_ETH_STOPPED)
2327 return -EBUSY;
2328
2329 mgp->running = MYRI10GE_ETH_STARTING;
2330 status = myri10ge_reset(mgp);
2331 if (status != 0) {
2332 printk(KERN_ERR "myri10ge: %s: failed reset\n", dev->name);
Brice Goglindf30a742006-12-18 11:50:40 +01002333 goto abort_with_nothing;
Brice Goglin0da34b62006-05-23 06:10:15 -04002334 }
2335
Brice Goglin0dcffac2008-05-09 02:21:49 +02002336 if (mgp->num_slices > 1) {
2337 cmd.data0 = mgp->num_slices;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002338 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
2339 if (mgp->dev->real_num_tx_queues > 1)
2340 cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002341 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
2342 &cmd, 0);
2343 if (status != 0) {
2344 printk(KERN_ERR
2345 "myri10ge: %s: failed to set number of slices\n",
2346 dev->name);
2347 goto abort_with_nothing;
2348 }
2349 /* setup the indirection table */
2350 cmd.data0 = mgp->num_slices;
2351 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
2352 &cmd, 0);
2353
2354 status |= myri10ge_send_cmd(mgp,
2355 MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
2356 &cmd, 0);
2357 if (status != 0) {
2358 printk(KERN_ERR
2359 "myri10ge: %s: failed to setup rss tables\n",
2360 dev->name);
Brice Goglin236bb5e2008-09-28 15:34:21 +00002361 goto abort_with_nothing;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002362 }
2363
2364 /* just enable an identity mapping */
2365 itable = mgp->sram + cmd.data0;
2366 for (i = 0; i < mgp->num_slices; i++)
2367 __raw_writeb(i, &itable[i]);
2368
2369 cmd.data0 = 1;
2370 cmd.data1 = myri10ge_rss_hash;
2371 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
2372 &cmd, 0);
2373 if (status != 0) {
2374 printk(KERN_ERR
2375 "myri10ge: %s: failed to enable slices\n",
2376 dev->name);
2377 goto abort_with_nothing;
2378 }
2379 }
2380
Brice Goglindf30a742006-12-18 11:50:40 +01002381 status = myri10ge_request_irq(mgp);
2382 if (status != 0)
2383 goto abort_with_nothing;
2384
Brice Goglin0da34b62006-05-23 06:10:15 -04002385 /* decide what small buffer size to use. For good TCP rx
2386 * performance, it is important to not receive 1514 byte
2387 * frames into jumbo buffers, as it confuses the socket buffer
2388 * accounting code, leading to drops and erratic performance.
2389 */
2390
2391 if (dev->mtu <= ETH_DATA_LEN)
Brice Goglinc7dab992006-12-11 11:25:42 +01002392 /* enough for a TCP header */
2393 mgp->small_bytes = (128 > SMP_CACHE_BYTES)
2394 ? (128 - MXGEFW_PAD)
2395 : (SMP_CACHE_BYTES - MXGEFW_PAD);
Brice Goglin0da34b62006-05-23 06:10:15 -04002396 else
Brice Goglinde3c4502006-12-11 11:26:38 +01002397 /* enough for a vlan encapsulated ETH_DATA_LEN frame */
2398 mgp->small_bytes = VLAN_ETH_FRAME_LEN;
Brice Goglin0da34b62006-05-23 06:10:15 -04002399
2400 /* Override the small buffer size? */
2401 if (myri10ge_small_bytes > 0)
2402 mgp->small_bytes = myri10ge_small_bytes;
2403
Brice Goglin0da34b62006-05-23 06:10:15 -04002404 /* Firmware needs the big buff size as a power of 2. Lie and
2405 * tell him the buffer is larger, because we only use 1
2406 * buffer/pkt, and the mtu will prevent overruns.
2407 */
Brice Goglin13348be2006-12-11 11:27:19 +01002408 big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
Brice Goglinc7dab992006-12-11 11:25:42 +01002409 if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
vignesh babu199126a2007-07-09 11:50:22 -07002410 while (!is_power_of_2(big_pow2))
Brice Goglinc7dab992006-12-11 11:25:42 +01002411 big_pow2++;
Brice Goglin13348be2006-12-11 11:27:19 +01002412 mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
Brice Goglinc7dab992006-12-11 11:25:42 +01002413 } else {
2414 big_pow2 = MYRI10GE_ALLOC_SIZE;
2415 mgp->big_bytes = big_pow2;
2416 }
2417
Brice Goglin0dcffac2008-05-09 02:21:49 +02002418 /* setup the per-slice data structures */
2419 for (slice = 0; slice < mgp->num_slices; slice++) {
2420 ss = &mgp->ss[slice];
2421
2422 status = myri10ge_get_txrx(mgp, slice);
2423 if (status != 0) {
2424 printk(KERN_ERR
2425 "myri10ge: %s: failed to get ring sizes or locations\n",
2426 dev->name);
2427 goto abort_with_rings;
2428 }
2429 status = myri10ge_allocate_rings(ss);
2430 if (status != 0)
2431 goto abort_with_rings;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002432
2433 /* only firmware which supports multiple TX queues
2434 * supports setting up the tx stats on non-zero
2435 * slices */
2436 if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
Brice Goglin0dcffac2008-05-09 02:21:49 +02002437 status = myri10ge_set_stats(mgp, slice);
2438 if (status) {
2439 printk(KERN_ERR
2440 "myri10ge: %s: Couldn't set stats DMA\n",
2441 dev->name);
2442 goto abort_with_rings;
2443 }
2444
2445 lro_mgr = &ss->rx_done.lro_mgr;
2446 lro_mgr->dev = dev;
2447 lro_mgr->features = LRO_F_NAPI;
2448 lro_mgr->ip_summed = CHECKSUM_COMPLETE;
2449 lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
2450 lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
2451 lro_mgr->lro_arr = ss->rx_done.lro_desc;
2452 lro_mgr->get_frag_header = myri10ge_get_frag_header;
2453 lro_mgr->max_aggr = myri10ge_lro_max_pkts;
Stanislaw Gruszka636d2f62009-04-15 02:26:49 -07002454 lro_mgr->frag_align_pad = 2;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002455 if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
2456 lro_mgr->max_aggr = MAX_SKB_FRAGS;
2457
2458 /* must happen prior to any irq */
2459 napi_enable(&(ss)->napi);
2460 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002461
2462 /* now give firmware buffers sizes, and MTU */
2463 cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
2464 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
2465 cmd.data0 = mgp->small_bytes;
2466 status |=
2467 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
2468 cmd.data0 = big_pow2;
2469 status |=
2470 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
2471 if (status) {
2472 printk(KERN_ERR "myri10ge: %s: Couldn't set buffer sizes\n",
2473 dev->name);
2474 goto abort_with_rings;
2475 }
2476
Brice Goglin0dcffac2008-05-09 02:21:49 +02002477 /*
2478 * Set Linux style TSO mode; this is needed only on newer
2479 * firmware versions. Older versions default to Linux
2480 * style TSO
2481 */
2482 cmd.data0 = 0;
2483 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
2484 if (status && status != -ENOSYS) {
2485 printk(KERN_ERR "myri10ge: %s: Couldn't set TSO mode\n",
Brice Goglin0da34b62006-05-23 06:10:15 -04002486 dev->name);
2487 goto abort_with_rings;
2488 }
2489
Al Viro66341ff2007-12-22 18:56:43 +00002490 mgp->link_state = ~0U;
Brice Goglin0da34b62006-05-23 06:10:15 -04002491 mgp->rdma_tags_available = 15;
2492
Brice Goglin0da34b62006-05-23 06:10:15 -04002493 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
2494 if (status) {
2495 printk(KERN_ERR "myri10ge: %s: Couldn't bring up link\n",
2496 dev->name);
2497 goto abort_with_rings;
2498 }
2499
Brice Goglin0da34b62006-05-23 06:10:15 -04002500 mgp->running = MYRI10GE_ETH_RUNNING;
2501 mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
2502 add_timer(&mgp->watchdog_timer);
Brice Goglin236bb5e2008-09-28 15:34:21 +00002503 netif_tx_wake_all_queues(dev);
2504
Brice Goglin0da34b62006-05-23 06:10:15 -04002505 return 0;
2506
2507abort_with_rings:
Brice Goglin051d36f2008-10-20 13:54:12 +02002508 while (slice) {
2509 slice--;
2510 napi_disable(&mgp->ss[slice].napi);
2511 }
Brice Goglin0dcffac2008-05-09 02:21:49 +02002512 for (i = 0; i < mgp->num_slices; i++)
2513 myri10ge_free_rings(&mgp->ss[i]);
Brice Goglin0da34b62006-05-23 06:10:15 -04002514
Brice Goglindf30a742006-12-18 11:50:40 +01002515 myri10ge_free_irq(mgp);
2516
Brice Goglin0da34b62006-05-23 06:10:15 -04002517abort_with_nothing:
2518 mgp->running = MYRI10GE_ETH_STOPPED;
2519 return -ENOMEM;
2520}
2521
2522static int myri10ge_close(struct net_device *dev)
2523{
Brice Goglinb53bef82008-05-09 02:20:03 +02002524 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002525 struct myri10ge_cmd cmd;
2526 int status, old_down_cnt;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002527 int i;
Brice Goglin0da34b62006-05-23 06:10:15 -04002528
Brice Goglin0da34b62006-05-23 06:10:15 -04002529 if (mgp->running != MYRI10GE_ETH_RUNNING)
2530 return 0;
2531
Brice Goglin0dcffac2008-05-09 02:21:49 +02002532 if (mgp->ss[0].tx.req_bytes == NULL)
Brice Goglin0da34b62006-05-23 06:10:15 -04002533 return 0;
2534
2535 del_timer_sync(&mgp->watchdog_timer);
2536 mgp->running = MYRI10GE_ETH_STOPPING;
Brice Goglin0dcffac2008-05-09 02:21:49 +02002537 for (i = 0; i < mgp->num_slices; i++) {
2538 napi_disable(&mgp->ss[i].napi);
2539 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002540 netif_carrier_off(dev);
Brice Goglin236bb5e2008-09-28 15:34:21 +00002541
2542 netif_tx_stop_all_queues(dev);
Brice Goglin0da34b62006-05-23 06:10:15 -04002543 old_down_cnt = mgp->down_cnt;
2544 mb();
2545 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
2546 if (status)
2547 printk(KERN_ERR "myri10ge: %s: Couldn't bring down link\n",
2548 dev->name);
2549
2550 wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt, HZ);
2551 if (old_down_cnt == mgp->down_cnt)
2552 printk(KERN_ERR "myri10ge: %s never got down irq\n", dev->name);
2553
2554 netif_tx_disable(dev);
Brice Goglindf30a742006-12-18 11:50:40 +01002555 myri10ge_free_irq(mgp);
Brice Goglin0dcffac2008-05-09 02:21:49 +02002556 for (i = 0; i < mgp->num_slices; i++)
2557 myri10ge_free_rings(&mgp->ss[i]);
Brice Goglin0da34b62006-05-23 06:10:15 -04002558
2559 mgp->running = MYRI10GE_ETH_STOPPED;
2560 return 0;
2561}
2562
2563/* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2564 * backwards one at a time and handle ring wraps */
2565
2566static inline void
2567myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
2568 struct mcp_kreq_ether_send *src, int cnt)
2569{
2570 int idx, starting_slot;
2571 starting_slot = tx->req;
2572 while (cnt > 1) {
2573 cnt--;
2574 idx = (starting_slot + cnt) & tx->mask;
2575 myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
2576 mb();
2577 }
2578}
2579
2580/*
2581 * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2582 * at most 32 bytes at a time, so as to avoid involving the software
2583 * pio handler in the nic. We re-write the first segment's flags
2584 * to mark them valid only after writing the entire chain.
2585 */
2586
2587static inline void
2588myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
2589 int cnt)
2590{
2591 int idx, i;
2592 struct mcp_kreq_ether_send __iomem *dstp, *dst;
2593 struct mcp_kreq_ether_send *srcp;
2594 u8 last_flags;
2595
2596 idx = tx->req & tx->mask;
2597
2598 last_flags = src->flags;
2599 src->flags = 0;
2600 mb();
2601 dst = dstp = &tx->lanai[idx];
2602 srcp = src;
2603
2604 if ((idx + cnt) < tx->mask) {
2605 for (i = 0; i < (cnt - 1); i += 2) {
2606 myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
2607 mb(); /* force write every 32 bytes */
2608 srcp += 2;
2609 dstp += 2;
2610 }
2611 } else {
2612 /* submit all but the first request, and ensure
2613 * that it is submitted below */
2614 myri10ge_submit_req_backwards(tx, src, cnt);
2615 i = 0;
2616 }
2617 if (i < cnt) {
2618 /* submit the first request */
2619 myri10ge_pio_copy(dstp, srcp, sizeof(*src));
2620 mb(); /* barrier before setting valid flag */
2621 }
2622
2623 /* re-write the last 32-bits with the valid flags */
2624 src->flags = last_flags;
Al Viro40f6cff2006-11-20 13:48:32 -05002625 put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
Brice Goglin0da34b62006-05-23 06:10:15 -04002626 tx->req += cnt;
2627 mb();
2628}
2629
Brice Goglin0da34b62006-05-23 06:10:15 -04002630/*
2631 * Transmit a packet. We need to split the packet so that a single
Brice Goglinb53bef82008-05-09 02:20:03 +02002632 * segment does not cross myri10ge->tx_boundary, so this makes segment
Brice Goglin0da34b62006-05-23 06:10:15 -04002633 * counting tricky. So rather than try to count segments up front, we
2634 * just give up if there are too few segments to hold a reasonably
2635 * fragmented packet currently available. If we run
2636 * out of segments while preparing a packet for DMA, we just linearize
2637 * it and try again.
2638 */
2639
2640static int myri10ge_xmit(struct sk_buff *skb, struct net_device *dev)
2641{
2642 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglinb53bef82008-05-09 02:20:03 +02002643 struct myri10ge_slice_state *ss;
Brice Goglin0da34b62006-05-23 06:10:15 -04002644 struct mcp_kreq_ether_send *req;
Brice Goglinb53bef82008-05-09 02:20:03 +02002645 struct myri10ge_tx_buf *tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04002646 struct skb_frag_struct *frag;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002647 struct netdev_queue *netdev_queue;
Brice Goglin0da34b62006-05-23 06:10:15 -04002648 dma_addr_t bus;
Al Viro40f6cff2006-11-20 13:48:32 -05002649 u32 low;
2650 __be32 high_swapped;
Brice Goglin0da34b62006-05-23 06:10:15 -04002651 unsigned int len;
2652 int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002653 u16 pseudo_hdr_offset, cksum_offset, queue;
Brice Goglin0da34b62006-05-23 06:10:15 -04002654 int cum_len, seglen, boundary, rdma_count;
2655 u8 flags, odd_flag;
2656
Brice Goglin236bb5e2008-09-28 15:34:21 +00002657 queue = skb_get_queue_mapping(skb);
Brice Goglin236bb5e2008-09-28 15:34:21 +00002658 ss = &mgp->ss[queue];
2659 netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
Brice Goglinb53bef82008-05-09 02:20:03 +02002660 tx = &ss->tx;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002661
Brice Goglin0da34b62006-05-23 06:10:15 -04002662again:
2663 req = tx->req_list;
2664 avail = tx->mask - 1 - (tx->req - tx->done);
2665
2666 mss = 0;
2667 max_segments = MXGEFW_MAX_SEND_DESC;
2668
Brice Goglin917690c2007-03-27 21:54:53 +02002669 if (skb_is_gso(skb)) {
Herbert Xu79671682006-06-22 02:40:14 -07002670 mss = skb_shinfo(skb)->gso_size;
Brice Goglin917690c2007-03-27 21:54:53 +02002671 max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
Brice Goglin0da34b62006-05-23 06:10:15 -04002672 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002673
2674 if ((unlikely(avail < max_segments))) {
2675 /* we are out of transmit resources */
Brice Goglinb53bef82008-05-09 02:20:03 +02002676 tx->stop_queue++;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002677 netif_tx_stop_queue(netdev_queue);
Brice Goglin0da34b62006-05-23 06:10:15 -04002678 return 1;
2679 }
2680
2681 /* Setup checksum offloading, if needed */
2682 cksum_offset = 0;
2683 pseudo_hdr_offset = 0;
2684 odd_flag = 0;
2685 flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
Patrick McHardy84fa7932006-08-29 16:44:56 -07002686 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07002687 cksum_offset = skb_transport_offset(skb);
Al Viroff1dcad2006-11-20 18:07:29 -08002688 pseudo_hdr_offset = cksum_offset + skb->csum_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -04002689 /* If the headers are excessively large, then we must
2690 * fall back to a software checksum */
Brice Goglin4f93fde2007-10-13 12:34:01 +02002691 if (unlikely(!mss && (cksum_offset > 255 ||
2692 pseudo_hdr_offset > 127))) {
Patrick McHardy84fa7932006-08-29 16:44:56 -07002693 if (skb_checksum_help(skb))
Brice Goglin0da34b62006-05-23 06:10:15 -04002694 goto drop;
2695 cksum_offset = 0;
2696 pseudo_hdr_offset = 0;
2697 } else {
Brice Goglin0da34b62006-05-23 06:10:15 -04002698 odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
2699 flags |= MXGEFW_FLAGS_CKSUM;
2700 }
2701 }
2702
2703 cum_len = 0;
2704
Brice Goglin0da34b62006-05-23 06:10:15 -04002705 if (mss) { /* TSO */
2706 /* this removes any CKSUM flag from before */
2707 flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
2708
2709 /* negative cum_len signifies to the
2710 * send loop that we are still in the
2711 * header portion of the TSO packet.
Brice Goglin4f93fde2007-10-13 12:34:01 +02002712 * TSO header can be at most 1KB long */
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07002713 cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
Brice Goglin0da34b62006-05-23 06:10:15 -04002714
Brice Goglin4f93fde2007-10-13 12:34:01 +02002715 /* for IPv6 TSO, the checksum offset stores the
2716 * TCP header length, to save the firmware from
2717 * the need to parse the headers */
2718 if (skb_is_gso_v6(skb)) {
2719 cksum_offset = tcp_hdrlen(skb);
2720 /* Can only handle headers <= max_tso6 long */
2721 if (unlikely(-cum_len > mgp->max_tso6))
2722 return myri10ge_sw_tso(skb, dev);
2723 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002724 /* for TSO, pseudo_hdr_offset holds mss.
2725 * The firmware figures out where to put
2726 * the checksum by parsing the header. */
Al Viro40f6cff2006-11-20 13:48:32 -05002727 pseudo_hdr_offset = mss;
Brice Goglin0da34b62006-05-23 06:10:15 -04002728 } else
Brice Goglin0da34b62006-05-23 06:10:15 -04002729 /* Mark small packets, and pad out tiny packets */
2730 if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
2731 flags |= MXGEFW_FLAGS_SMALL;
2732
2733 /* pad frames to at least ETH_ZLEN bytes */
2734 if (unlikely(skb->len < ETH_ZLEN)) {
Herbert Xu5b057c62006-06-23 02:06:41 -07002735 if (skb_padto(skb, ETH_ZLEN)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002736 /* The packet is gone, so we must
2737 * return 0 */
Brice Goglinb53bef82008-05-09 02:20:03 +02002738 ss->stats.tx_dropped += 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04002739 return 0;
2740 }
2741 /* adjust the len to account for the zero pad
2742 * so that the nic can know how long it is */
2743 skb->len = ETH_ZLEN;
2744 }
2745 }
2746
2747 /* map the skb for DMA */
2748 len = skb->len - skb->data_len;
2749 idx = tx->req & tx->mask;
2750 tx->info[idx].skb = skb;
2751 bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
2752 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2753 pci_unmap_len_set(&tx->info[idx], len, len);
2754
2755 frag_cnt = skb_shinfo(skb)->nr_frags;
2756 frag_idx = 0;
2757 count = 0;
2758 rdma_count = 0;
2759
2760 /* "rdma_count" is the number of RDMAs belonging to the
2761 * current packet BEFORE the current send request. For
2762 * non-TSO packets, this is equal to "count".
2763 * For TSO packets, rdma_count needs to be reset
2764 * to 0 after a segment cut.
2765 *
2766 * The rdma_count field of the send request is
2767 * the number of RDMAs of the packet starting at
2768 * that request. For TSO send requests with one ore more cuts
2769 * in the middle, this is the number of RDMAs starting
2770 * after the last cut in the request. All previous
2771 * segments before the last cut implicitly have 1 RDMA.
2772 *
2773 * Since the number of RDMAs is not known beforehand,
2774 * it must be filled-in retroactively - after each
2775 * segmentation cut or at the end of the entire packet.
2776 */
2777
2778 while (1) {
2779 /* Break the SKB or Fragment up into pieces which
Brice Goglinb53bef82008-05-09 02:20:03 +02002780 * do not cross mgp->tx_boundary */
Brice Goglin0da34b62006-05-23 06:10:15 -04002781 low = MYRI10GE_LOWPART_TO_U32(bus);
2782 high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
2783 while (len) {
2784 u8 flags_next;
2785 int cum_len_next;
2786
2787 if (unlikely(count == max_segments))
2788 goto abort_linearize;
2789
Brice Goglinb53bef82008-05-09 02:20:03 +02002790 boundary =
2791 (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04002792 seglen = boundary - low;
2793 if (seglen > len)
2794 seglen = len;
2795 flags_next = flags & ~MXGEFW_FLAGS_FIRST;
2796 cum_len_next = cum_len + seglen;
Brice Goglin0da34b62006-05-23 06:10:15 -04002797 if (mss) { /* TSO */
2798 (req - rdma_count)->rdma_count = rdma_count + 1;
2799
2800 if (likely(cum_len >= 0)) { /* payload */
2801 int next_is_first, chop;
2802
2803 chop = (cum_len_next > mss);
2804 cum_len_next = cum_len_next % mss;
2805 next_is_first = (cum_len_next == 0);
2806 flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
2807 flags_next |= next_is_first *
2808 MXGEFW_FLAGS_FIRST;
2809 rdma_count |= -(chop | next_is_first);
2810 rdma_count += chop & !next_is_first;
2811 } else if (likely(cum_len_next >= 0)) { /* header ends */
2812 int small;
2813
2814 rdma_count = -1;
2815 cum_len_next = 0;
2816 seglen = -cum_len;
2817 small = (mss <= MXGEFW_SEND_SMALL_SIZE);
2818 flags_next = MXGEFW_FLAGS_TSO_PLD |
2819 MXGEFW_FLAGS_FIRST |
2820 (small * MXGEFW_FLAGS_SMALL);
2821 }
2822 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002823 req->addr_high = high_swapped;
2824 req->addr_low = htonl(low);
Al Viro40f6cff2006-11-20 13:48:32 -05002825 req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
Brice Goglin0da34b62006-05-23 06:10:15 -04002826 req->pad = 0; /* complete solid 16-byte block; does this matter? */
2827 req->rdma_count = 1;
2828 req->length = htons(seglen);
2829 req->cksum_offset = cksum_offset;
2830 req->flags = flags | ((cum_len & 1) * odd_flag);
2831
2832 low += seglen;
2833 len -= seglen;
2834 cum_len = cum_len_next;
2835 flags = flags_next;
2836 req++;
2837 count++;
2838 rdma_count++;
Brice Goglin4f93fde2007-10-13 12:34:01 +02002839 if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
2840 if (unlikely(cksum_offset > seglen))
2841 cksum_offset -= seglen;
2842 else
2843 cksum_offset = 0;
2844 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002845 }
2846 if (frag_idx == frag_cnt)
2847 break;
2848
2849 /* map next fragment for DMA */
2850 idx = (count + tx->req) & tx->mask;
2851 frag = &skb_shinfo(skb)->frags[frag_idx];
2852 frag_idx++;
2853 len = frag->size;
2854 bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
2855 len, PCI_DMA_TODEVICE);
2856 pci_unmap_addr_set(&tx->info[idx], bus, bus);
2857 pci_unmap_len_set(&tx->info[idx], len, len);
2858 }
2859
2860 (req - rdma_count)->rdma_count = rdma_count;
Brice Goglin0da34b62006-05-23 06:10:15 -04002861 if (mss)
2862 do {
2863 req--;
2864 req->flags |= MXGEFW_FLAGS_TSO_LAST;
2865 } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
2866 MXGEFW_FLAGS_FIRST)));
Brice Goglin0da34b62006-05-23 06:10:15 -04002867 idx = ((count - 1) + tx->req) & tx->mask;
2868 tx->info[idx].last = 1;
Brice Gogline454e7e2008-07-21 10:25:50 +02002869 myri10ge_submit_req(tx, tx->req_list, count);
Brice Goglin236bb5e2008-09-28 15:34:21 +00002870 /* if using multiple tx queues, make sure NIC polls the
2871 * current slice */
2872 if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
2873 tx->queue_active = 1;
2874 put_be32(htonl(1), tx->send_go);
Brice Goglin8c2f5fa2008-11-10 13:58:41 +01002875 mb();
Brice Goglin6824a102008-10-30 08:59:33 +01002876 mmiowb();
Brice Goglin236bb5e2008-09-28 15:34:21 +00002877 }
Brice Goglin0da34b62006-05-23 06:10:15 -04002878 tx->pkt_start++;
2879 if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
Brice Goglinb53bef82008-05-09 02:20:03 +02002880 tx->stop_queue++;
Brice Goglin236bb5e2008-09-28 15:34:21 +00002881 netif_tx_stop_queue(netdev_queue);
Brice Goglin0da34b62006-05-23 06:10:15 -04002882 }
2883 dev->trans_start = jiffies;
2884 return 0;
2885
2886abort_linearize:
2887 /* Free any DMA resources we've alloced and clear out the skb
2888 * slot so as to not trip up assertions, and to avoid a
2889 * double-free if linearizing fails */
2890
2891 last_idx = (idx + 1) & tx->mask;
2892 idx = tx->req & tx->mask;
2893 tx->info[idx].skb = NULL;
2894 do {
2895 len = pci_unmap_len(&tx->info[idx], len);
2896 if (len) {
2897 if (tx->info[idx].skb != NULL)
2898 pci_unmap_single(mgp->pdev,
2899 pci_unmap_addr(&tx->info[idx],
2900 bus), len,
2901 PCI_DMA_TODEVICE);
2902 else
2903 pci_unmap_page(mgp->pdev,
2904 pci_unmap_addr(&tx->info[idx],
2905 bus), len,
2906 PCI_DMA_TODEVICE);
2907 pci_unmap_len_set(&tx->info[idx], len, 0);
2908 tx->info[idx].skb = NULL;
2909 }
2910 idx = (idx + 1) & tx->mask;
2911 } while (idx != last_idx);
Herbert Xu89114af2006-07-08 13:34:32 -07002912 if (skb_is_gso(skb)) {
Brice Goglin0da34b62006-05-23 06:10:15 -04002913 printk(KERN_ERR
2914 "myri10ge: %s: TSO but wanted to linearize?!?!?\n",
2915 mgp->dev->name);
2916 goto drop;
2917 }
2918
Andrew Mortonbec0e852006-06-22 14:47:19 -07002919 if (skb_linearize(skb))
Brice Goglin0da34b62006-05-23 06:10:15 -04002920 goto drop;
2921
Brice Goglinb53bef82008-05-09 02:20:03 +02002922 tx->linearized++;
Brice Goglin0da34b62006-05-23 06:10:15 -04002923 goto again;
2924
2925drop:
2926 dev_kfree_skb_any(skb);
Brice Goglinb53bef82008-05-09 02:20:03 +02002927 ss->stats.tx_dropped += 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04002928 return 0;
2929
2930}
2931
Brice Goglin4f93fde2007-10-13 12:34:01 +02002932static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev)
2933{
2934 struct sk_buff *segs, *curr;
Brice Goglinb53bef82008-05-09 02:20:03 +02002935 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglind6279c82008-11-20 01:50:04 -08002936 struct myri10ge_slice_state *ss;
Brice Goglin4f93fde2007-10-13 12:34:01 +02002937 int status;
2938
2939 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07002940 if (IS_ERR(segs))
Brice Goglin4f93fde2007-10-13 12:34:01 +02002941 goto drop;
2942
2943 while (segs) {
2944 curr = segs;
2945 segs = segs->next;
2946 curr->next = NULL;
2947 status = myri10ge_xmit(curr, dev);
2948 if (status != 0) {
2949 dev_kfree_skb_any(curr);
2950 if (segs != NULL) {
2951 curr = segs;
2952 segs = segs->next;
2953 curr->next = NULL;
2954 dev_kfree_skb_any(segs);
2955 }
2956 goto drop;
2957 }
2958 }
2959 dev_kfree_skb_any(skb);
2960 return 0;
2961
2962drop:
Brice Goglind6279c82008-11-20 01:50:04 -08002963 ss = &mgp->ss[skb_get_queue_mapping(skb)];
Brice Goglin4f93fde2007-10-13 12:34:01 +02002964 dev_kfree_skb_any(skb);
Brice Goglind6279c82008-11-20 01:50:04 -08002965 ss->stats.tx_dropped += 1;
Brice Goglin4f93fde2007-10-13 12:34:01 +02002966 return 0;
2967}
2968
Brice Goglin0da34b62006-05-23 06:10:15 -04002969static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
2970{
2971 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin0dcffac2008-05-09 02:21:49 +02002972 struct myri10ge_slice_netstats *slice_stats;
2973 struct net_device_stats *stats = &mgp->stats;
2974 int i;
2975
Brice Goglin59081822009-04-16 02:23:56 +00002976 spin_lock(&mgp->stats_lock);
Brice Goglin0dcffac2008-05-09 02:21:49 +02002977 memset(stats, 0, sizeof(*stats));
2978 for (i = 0; i < mgp->num_slices; i++) {
2979 slice_stats = &mgp->ss[i].stats;
2980 stats->rx_packets += slice_stats->rx_packets;
2981 stats->tx_packets += slice_stats->tx_packets;
2982 stats->rx_bytes += slice_stats->rx_bytes;
2983 stats->tx_bytes += slice_stats->tx_bytes;
2984 stats->rx_dropped += slice_stats->rx_dropped;
2985 stats->tx_dropped += slice_stats->tx_dropped;
2986 }
Brice Goglin59081822009-04-16 02:23:56 +00002987 spin_unlock(&mgp->stats_lock);
Brice Goglin0dcffac2008-05-09 02:21:49 +02002988 return stats;
Brice Goglin0da34b62006-05-23 06:10:15 -04002989}
2990
2991static void myri10ge_set_multicast_list(struct net_device *dev)
2992{
Brice Goglinb53bef82008-05-09 02:20:03 +02002993 struct myri10ge_priv *mgp = netdev_priv(dev);
Brice Goglin85a7ea12006-08-21 17:36:56 -04002994 struct myri10ge_cmd cmd;
Brice Goglin85a7ea12006-08-21 17:36:56 -04002995 struct dev_mc_list *mc_list;
Brice Goglin62502232006-12-11 11:24:37 +01002996 __be32 data[2] = { 0, 0 };
Brice Goglin85a7ea12006-08-21 17:36:56 -04002997 int err;
2998
Brice Goglin0da34b62006-05-23 06:10:15 -04002999 /* can be called from atomic contexts,
3000 * pass 1 to force atomicity in myri10ge_send_cmd() */
Brice Goglin85a7ea12006-08-21 17:36:56 -04003001 myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
3002
3003 /* This firmware is known to not support multicast */
Brice Goglin2f762162007-05-07 23:50:37 +02003004 if (!mgp->fw_multicast_support)
Brice Goglin85a7ea12006-08-21 17:36:56 -04003005 return;
3006
3007 /* Disable multicast filtering */
3008
3009 err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
3010 if (err != 0) {
3011 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_ENABLE_ALLMULTI,"
3012 " error status: %d\n", dev->name, err);
3013 goto abort;
3014 }
3015
Brice Goglin2f762162007-05-07 23:50:37 +02003016 if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
Brice Goglin85a7ea12006-08-21 17:36:56 -04003017 /* request to disable multicast filtering, so quit here */
3018 return;
3019 }
3020
3021 /* Flush the filters */
3022
3023 err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
3024 &cmd, 1);
3025 if (err != 0) {
3026 printk(KERN_ERR
3027 "myri10ge: %s: Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS"
3028 ", error status: %d\n", dev->name, err);
3029 goto abort;
3030 }
3031
3032 /* Walk the multicast list, and add each address */
3033 for (mc_list = dev->mc_list; mc_list != NULL; mc_list = mc_list->next) {
Al Viro40f6cff2006-11-20 13:48:32 -05003034 memcpy(data, &mc_list->dmi_addr, 6);
3035 cmd.data0 = ntohl(data[0]);
3036 cmd.data1 = ntohl(data[1]);
Brice Goglin85a7ea12006-08-21 17:36:56 -04003037 err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
3038 &cmd, 1);
3039
3040 if (err != 0) {
3041 printk(KERN_ERR "myri10ge: %s: Failed "
3042 "MXGEFW_JOIN_MULTICAST_GROUP, error status:"
3043 "%d\t", dev->name, err);
Johannes Berge1749612008-10-27 15:59:26 -07003044 printk(KERN_ERR "MAC %pM\n", mc_list->dmi_addr);
Brice Goglin85a7ea12006-08-21 17:36:56 -04003045 goto abort;
3046 }
3047 }
3048 /* Enable multicast filtering */
3049 err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
3050 if (err != 0) {
3051 printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_DISABLE_ALLMULTI,"
3052 "error status: %d\n", dev->name, err);
3053 goto abort;
3054 }
3055
3056 return;
3057
3058abort:
3059 return;
Brice Goglin0da34b62006-05-23 06:10:15 -04003060}
3061
3062static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
3063{
3064 struct sockaddr *sa = addr;
3065 struct myri10ge_priv *mgp = netdev_priv(dev);
3066 int status;
3067
3068 if (!is_valid_ether_addr(sa->sa_data))
3069 return -EADDRNOTAVAIL;
3070
3071 status = myri10ge_update_mac_address(mgp, sa->sa_data);
3072 if (status != 0) {
3073 printk(KERN_ERR
3074 "myri10ge: %s: changing mac address failed with %d\n",
3075 dev->name, status);
3076 return status;
3077 }
3078
3079 /* change the dev structure */
3080 memcpy(dev->dev_addr, sa->sa_data, 6);
3081 return 0;
3082}
3083
3084static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
3085{
3086 struct myri10ge_priv *mgp = netdev_priv(dev);
3087 int error = 0;
3088
3089 if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
3090 printk(KERN_ERR "myri10ge: %s: new mtu (%d) is not valid\n",
3091 dev->name, new_mtu);
3092 return -EINVAL;
3093 }
3094 printk(KERN_INFO "%s: changing mtu from %d to %d\n",
3095 dev->name, dev->mtu, new_mtu);
3096 if (mgp->running) {
3097 /* if we change the mtu on an active device, we must
3098 * reset the device so the firmware sees the change */
3099 myri10ge_close(dev);
3100 dev->mtu = new_mtu;
3101 myri10ge_open(dev);
3102 } else
3103 dev->mtu = new_mtu;
3104
3105 return error;
3106}
3107
3108/*
3109 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
3110 * Only do it if the bridge is a root port since we don't want to disturb
3111 * any other device, except if forced with myri10ge_ecrc_enable > 1.
3112 */
3113
Brice Goglin0da34b62006-05-23 06:10:15 -04003114static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
3115{
3116 struct pci_dev *bridge = mgp->pdev->bus->self;
3117 struct device *dev = &mgp->pdev->dev;
3118 unsigned cap;
3119 unsigned err_cap;
3120 u16 val;
3121 u8 ext_type;
3122 int ret;
3123
3124 if (!myri10ge_ecrc_enable || !bridge)
3125 return;
3126
3127 /* check that the bridge is a root port */
3128 cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
3129 pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
3130 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
3131 if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
3132 if (myri10ge_ecrc_enable > 1) {
Brice Goglineca3fd82008-05-09 02:19:29 +02003133 struct pci_dev *prev_bridge, *old_bridge = bridge;
Brice Goglin0da34b62006-05-23 06:10:15 -04003134
3135 /* Walk the hierarchy up to the root port
3136 * where ECRC has to be enabled */
3137 do {
Brice Goglineca3fd82008-05-09 02:19:29 +02003138 prev_bridge = bridge;
Brice Goglin0da34b62006-05-23 06:10:15 -04003139 bridge = bridge->bus->self;
Brice Goglineca3fd82008-05-09 02:19:29 +02003140 if (!bridge || prev_bridge == bridge) {
Brice Goglin0da34b62006-05-23 06:10:15 -04003141 dev_err(dev,
3142 "Failed to find root port"
3143 " to force ECRC\n");
3144 return;
3145 }
3146 cap =
3147 pci_find_capability(bridge, PCI_CAP_ID_EXP);
3148 pci_read_config_word(bridge,
3149 cap + PCI_CAP_FLAGS, &val);
3150 ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
3151 } while (ext_type != PCI_EXP_TYPE_ROOT_PORT);
3152
3153 dev_info(dev,
3154 "Forcing ECRC on non-root port %s"
3155 " (enabling on root port %s)\n",
3156 pci_name(old_bridge), pci_name(bridge));
3157 } else {
3158 dev_err(dev,
3159 "Not enabling ECRC on non-root port %s\n",
3160 pci_name(bridge));
3161 return;
3162 }
3163 }
3164
3165 cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
Brice Goglin0da34b62006-05-23 06:10:15 -04003166 if (!cap)
3167 return;
3168
3169 ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
3170 if (ret) {
3171 dev_err(dev, "failed reading ext-conf-space of %s\n",
3172 pci_name(bridge));
3173 dev_err(dev, "\t pci=nommconf in use? "
3174 "or buggy/incomplete/absent ACPI MCFG attr?\n");
3175 return;
3176 }
3177 if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
3178 return;
3179
3180 err_cap |= PCI_ERR_CAP_ECRC_GENE;
3181 pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
3182 dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
Brice Goglin0da34b62006-05-23 06:10:15 -04003183}
3184
3185/*
3186 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
3187 * when the PCI-E Completion packets are aligned on an 8-byte
3188 * boundary. Some PCI-E chip sets always align Completion packets; on
3189 * the ones that do not, the alignment can be enforced by enabling
3190 * ECRC generation (if supported).
3191 *
3192 * When PCI-E Completion packets are not aligned, it is actually more
3193 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
3194 *
3195 * If the driver can neither enable ECRC nor verify that it has
3196 * already been enabled, then it must use a firmware image which works
Brice Goglin0dcffac2008-05-09 02:21:49 +02003197 * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
Brice Goglin0da34b62006-05-23 06:10:15 -04003198 * should also ensure that it never gives the device a Read-DMA which is
Brice Goglinb53bef82008-05-09 02:20:03 +02003199 * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
Brice Goglin0dcffac2008-05-09 02:21:49 +02003200 * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
Brice Goglinb53bef82008-05-09 02:20:03 +02003201 * firmware image, and set tx_boundary to 4KB.
Brice Goglin0da34b62006-05-23 06:10:15 -04003202 */
3203
Brice Goglin5443e9e2007-05-07 23:52:22 +02003204static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
Brice Goglin0da34b62006-05-23 06:10:15 -04003205{
Brice Goglin5443e9e2007-05-07 23:52:22 +02003206 struct pci_dev *pdev = mgp->pdev;
3207 struct device *dev = &pdev->dev;
Brice Goglin302d2422007-08-24 08:57:17 +02003208 int status;
Brice Goglin0da34b62006-05-23 06:10:15 -04003209
Brice Goglinb53bef82008-05-09 02:20:03 +02003210 mgp->tx_boundary = 4096;
Brice Goglin5443e9e2007-05-07 23:52:22 +02003211 /*
3212 * Verify the max read request size was set to 4KB
3213 * before trying the test with 4KB.
3214 */
Brice Goglin302d2422007-08-24 08:57:17 +02003215 status = pcie_get_readrq(pdev);
3216 if (status < 0) {
Brice Goglin5443e9e2007-05-07 23:52:22 +02003217 dev_err(dev, "Couldn't read max read req size: %d\n", status);
3218 goto abort;
3219 }
Brice Goglin302d2422007-08-24 08:57:17 +02003220 if (status != 4096) {
3221 dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
Brice Goglinb53bef82008-05-09 02:20:03 +02003222 mgp->tx_boundary = 2048;
Brice Goglin5443e9e2007-05-07 23:52:22 +02003223 }
3224 /*
3225 * load the optimized firmware (which assumes aligned PCIe
3226 * completions) in order to see if it works on this host.
3227 */
3228 mgp->fw_name = myri10ge_fw_aligned;
Brice Goglin0dcffac2008-05-09 02:21:49 +02003229 status = myri10ge_load_firmware(mgp, 1);
Brice Goglin5443e9e2007-05-07 23:52:22 +02003230 if (status != 0) {
3231 goto abort;
3232 }
3233
3234 /*
3235 * Enable ECRC if possible
3236 */
3237 myri10ge_enable_ecrc(mgp);
3238
3239 /*
3240 * Run a DMA test which watches for unaligned completions and
3241 * aborts on the first one seen.
3242 */
3243
3244 status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
3245 if (status == 0)
3246 return; /* keep the aligned firmware */
3247
3248 if (status != -E2BIG)
3249 dev_warn(dev, "DMA test failed: %d\n", status);
3250 if (status == -ENOSYS)
3251 dev_warn(dev, "Falling back to ethp! "
3252 "Please install up to date fw\n");
3253abort:
3254 /* fall back to using the unaligned firmware */
Brice Goglinb53bef82008-05-09 02:20:03 +02003255 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -04003256 mgp->fw_name = myri10ge_fw_unaligned;
3257
Brice Goglin5443e9e2007-05-07 23:52:22 +02003258}
3259
3260static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
3261{
Brice Goglin0da34b62006-05-23 06:10:15 -04003262 if (myri10ge_force_firmware == 0) {
Brice Goglince7f9362006-08-31 01:32:59 -04003263 int link_width, exp_cap;
3264 u16 lnk;
3265
3266 exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
3267 pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
3268 link_width = (lnk >> 4) & 0x3f;
3269
Brice Goglince7f9362006-08-31 01:32:59 -04003270 /* Check to see if Link is less than 8 or if the
3271 * upstream bridge is known to provide aligned
3272 * completions */
3273 if (link_width < 8) {
3274 dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
3275 link_width);
Brice Goglinb53bef82008-05-09 02:20:03 +02003276 mgp->tx_boundary = 4096;
Brice Goglince7f9362006-08-31 01:32:59 -04003277 mgp->fw_name = myri10ge_fw_aligned;
Brice Goglin5443e9e2007-05-07 23:52:22 +02003278 } else {
3279 myri10ge_firmware_probe(mgp);
Brice Goglin0da34b62006-05-23 06:10:15 -04003280 }
3281 } else {
3282 if (myri10ge_force_firmware == 1) {
3283 dev_info(&mgp->pdev->dev,
3284 "Assuming aligned completions (forced)\n");
Brice Goglinb53bef82008-05-09 02:20:03 +02003285 mgp->tx_boundary = 4096;
Brice Goglin0da34b62006-05-23 06:10:15 -04003286 mgp->fw_name = myri10ge_fw_aligned;
3287 } else {
3288 dev_info(&mgp->pdev->dev,
3289 "Assuming unaligned completions (forced)\n");
Brice Goglinb53bef82008-05-09 02:20:03 +02003290 mgp->tx_boundary = 2048;
Brice Goglin0da34b62006-05-23 06:10:15 -04003291 mgp->fw_name = myri10ge_fw_unaligned;
3292 }
3293 }
3294 if (myri10ge_fw_name != NULL) {
3295 dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
3296 myri10ge_fw_name);
3297 mgp->fw_name = myri10ge_fw_name;
3298 }
3299}
3300
Brice Goglin0da34b62006-05-23 06:10:15 -04003301#ifdef CONFIG_PM
Brice Goglin0da34b62006-05-23 06:10:15 -04003302static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
3303{
3304 struct myri10ge_priv *mgp;
3305 struct net_device *netdev;
3306
3307 mgp = pci_get_drvdata(pdev);
3308 if (mgp == NULL)
3309 return -EINVAL;
3310 netdev = mgp->dev;
3311
3312 netif_device_detach(netdev);
3313 if (netif_running(netdev)) {
3314 printk(KERN_INFO "myri10ge: closing %s\n", netdev->name);
3315 rtnl_lock();
3316 myri10ge_close(netdev);
3317 rtnl_unlock();
3318 }
3319 myri10ge_dummy_rdma(mgp, 0);
Brice Goglin83f6e152006-12-18 11:52:02 +01003320 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003321 pci_disable_device(pdev);
Brice Goglin1a63e842006-12-18 11:52:34 +01003322
3323 return pci_set_power_state(pdev, pci_choose_state(pdev, state));
Brice Goglin0da34b62006-05-23 06:10:15 -04003324}
3325
3326static int myri10ge_resume(struct pci_dev *pdev)
3327{
3328 struct myri10ge_priv *mgp;
3329 struct net_device *netdev;
3330 int status;
3331 u16 vendor;
3332
3333 mgp = pci_get_drvdata(pdev);
3334 if (mgp == NULL)
3335 return -EINVAL;
3336 netdev = mgp->dev;
3337 pci_set_power_state(pdev, 0); /* zeros conf space as a side effect */
3338 msleep(5); /* give card time to respond */
3339 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3340 if (vendor == 0xffff) {
3341 printk(KERN_ERR "myri10ge: %s: device disappeared!\n",
3342 mgp->dev->name);
3343 return -EIO;
3344 }
Brice Goglin83f6e152006-12-18 11:52:02 +01003345
Brice Goglin1a63e842006-12-18 11:52:34 +01003346 status = pci_restore_state(pdev);
3347 if (status)
3348 return status;
Brice Goglin4c2248c2006-07-09 21:10:18 -04003349
3350 status = pci_enable_device(pdev);
Brice Goglin1a63e842006-12-18 11:52:34 +01003351 if (status) {
Brice Goglin4c2248c2006-07-09 21:10:18 -04003352 dev_err(&pdev->dev, "failed to enable device\n");
Brice Goglin1a63e842006-12-18 11:52:34 +01003353 return status;
Brice Goglin4c2248c2006-07-09 21:10:18 -04003354 }
3355
Brice Goglin0da34b62006-05-23 06:10:15 -04003356 pci_set_master(pdev);
3357
Brice Goglin0da34b62006-05-23 06:10:15 -04003358 myri10ge_reset(mgp);
Brice Goglin013b68b2006-08-09 00:07:53 -04003359 myri10ge_dummy_rdma(mgp, 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04003360
3361 /* Save configuration space to be restored if the
3362 * nic resets due to a parity error */
Brice Goglin83f6e152006-12-18 11:52:02 +01003363 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003364
3365 if (netif_running(netdev)) {
3366 rtnl_lock();
Brice Goglindf30a742006-12-18 11:50:40 +01003367 status = myri10ge_open(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003368 rtnl_unlock();
Brice Goglindf30a742006-12-18 11:50:40 +01003369 if (status != 0)
3370 goto abort_with_enabled;
3371
Brice Goglin0da34b62006-05-23 06:10:15 -04003372 }
3373 netif_device_attach(netdev);
3374
3375 return 0;
3376
Brice Goglin4c2248c2006-07-09 21:10:18 -04003377abort_with_enabled:
3378 pci_disable_device(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003379 return -EIO;
3380
3381}
Brice Goglin0da34b62006-05-23 06:10:15 -04003382#endif /* CONFIG_PM */
3383
3384static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
3385{
3386 struct pci_dev *pdev = mgp->pdev;
3387 int vs = mgp->vendor_specific_offset;
3388 u32 reboot;
3389
3390 /*enter read32 mode */
3391 pci_write_config_byte(pdev, vs + 0x10, 0x3);
3392
3393 /*read REBOOT_STATUS (0xfffffff0) */
3394 pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
3395 pci_read_config_dword(pdev, vs + 0x14, &reboot);
3396 return reboot;
3397}
3398
3399/*
3400 * This watchdog is used to check whether the board has suffered
3401 * from a parity error and needs to be recovered.
3402 */
David Howellsc4028952006-11-22 14:57:56 +00003403static void myri10ge_watchdog(struct work_struct *work)
Brice Goglin0da34b62006-05-23 06:10:15 -04003404{
David Howellsc4028952006-11-22 14:57:56 +00003405 struct myri10ge_priv *mgp =
Brice Goglin62502232006-12-11 11:24:37 +01003406 container_of(work, struct myri10ge_priv, watchdog_work);
Brice Goglinb53bef82008-05-09 02:20:03 +02003407 struct myri10ge_tx_buf *tx;
Brice Goglin0da34b62006-05-23 06:10:15 -04003408 u32 reboot;
3409 int status;
Brice Goglin0dcffac2008-05-09 02:21:49 +02003410 int i;
Brice Goglin0da34b62006-05-23 06:10:15 -04003411 u16 cmd, vendor;
3412
3413 mgp->watchdog_resets++;
3414 pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
3415 if ((cmd & PCI_COMMAND_MASTER) == 0) {
3416 /* Bus master DMA disabled? Check to see
3417 * if the card rebooted due to a parity error
3418 * For now, just report it */
3419 reboot = myri10ge_read_reboot(mgp);
3420 printk(KERN_ERR
Brice Goglinf1811372007-06-11 20:26:31 +02003421 "myri10ge: %s: NIC rebooted (0x%x),%s resetting\n",
3422 mgp->dev->name, reboot,
3423 myri10ge_reset_recover ? " " : " not");
3424 if (myri10ge_reset_recover == 0)
3425 return;
3426
3427 myri10ge_reset_recover--;
3428
Brice Goglin0da34b62006-05-23 06:10:15 -04003429 /*
3430 * A rebooted nic will come back with config space as
3431 * it was after power was applied to PCIe bus.
3432 * Attempt to restore config space which was saved
3433 * when the driver was loaded, or the last time the
3434 * nic was resumed from power saving mode.
3435 */
Brice Goglin83f6e152006-12-18 11:52:02 +01003436 pci_restore_state(mgp->pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003437
3438 /* save state again for accounting reasons */
Brice Goglin83f6e152006-12-18 11:52:02 +01003439 pci_save_state(mgp->pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003440
Brice Goglin0da34b62006-05-23 06:10:15 -04003441 } else {
3442 /* if we get back -1's from our slot, perhaps somebody
3443 * powered off our card. Don't try to reset it in
3444 * this case */
3445 if (cmd == 0xffff) {
3446 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3447 if (vendor == 0xffff) {
3448 printk(KERN_ERR
3449 "myri10ge: %s: device disappeared!\n",
3450 mgp->dev->name);
3451 return;
3452 }
3453 }
3454 /* Perhaps it is a software error. Try to reset */
3455
3456 printk(KERN_ERR "myri10ge: %s: device timeout, resetting\n",
3457 mgp->dev->name);
Brice Goglin0dcffac2008-05-09 02:21:49 +02003458 for (i = 0; i < mgp->num_slices; i++) {
3459 tx = &mgp->ss[i].tx;
3460 printk(KERN_INFO
Brice Goglin236bb5e2008-09-28 15:34:21 +00003461 "myri10ge: %s: (%d): %d %d %d %d %d %d\n",
3462 mgp->dev->name, i, tx->queue_active, tx->req,
3463 tx->done, tx->pkt_start, tx->pkt_done,
Brice Goglin0dcffac2008-05-09 02:21:49 +02003464 (int)ntohl(mgp->ss[i].fw_stats->
3465 send_done_count));
3466 msleep(2000);
3467 printk(KERN_INFO
Brice Goglin236bb5e2008-09-28 15:34:21 +00003468 "myri10ge: %s: (%d): %d %d %d %d %d %d\n",
3469 mgp->dev->name, i, tx->queue_active, tx->req,
3470 tx->done, tx->pkt_start, tx->pkt_done,
Brice Goglin0dcffac2008-05-09 02:21:49 +02003471 (int)ntohl(mgp->ss[i].fw_stats->
3472 send_done_count));
3473 }
Brice Goglin0da34b62006-05-23 06:10:15 -04003474 }
Brice Goglin236bb5e2008-09-28 15:34:21 +00003475
Brice Goglin0da34b62006-05-23 06:10:15 -04003476 rtnl_lock();
3477 myri10ge_close(mgp->dev);
Brice Goglin0dcffac2008-05-09 02:21:49 +02003478 status = myri10ge_load_firmware(mgp, 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04003479 if (status != 0)
3480 printk(KERN_ERR "myri10ge: %s: failed to load firmware\n",
3481 mgp->dev->name);
3482 else
3483 myri10ge_open(mgp->dev);
3484 rtnl_unlock();
3485}
3486
3487/*
3488 * We use our own timer routine rather than relying upon
3489 * netdev->tx_timeout because we have a very large hardware transmit
3490 * queue. Due to the large queue, the netdev->tx_timeout function
3491 * cannot detect a NIC with a parity error in a timely fashion if the
3492 * NIC is lightly loaded.
3493 */
3494static void myri10ge_watchdog_timer(unsigned long arg)
3495{
3496 struct myri10ge_priv *mgp;
Brice Goglinb53bef82008-05-09 02:20:03 +02003497 struct myri10ge_slice_state *ss;
Brice Goglin0dcffac2008-05-09 02:21:49 +02003498 int i, reset_needed;
Brice Goglin626fda92007-08-09 09:02:14 +02003499 u32 rx_pause_cnt;
Brice Goglin0da34b62006-05-23 06:10:15 -04003500
3501 mgp = (struct myri10ge_priv *)arg;
Brice Goglinc7dab992006-12-11 11:25:42 +01003502
Brice Goglin0dcffac2008-05-09 02:21:49 +02003503 rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
3504 for (i = 0, reset_needed = 0;
3505 i < mgp->num_slices && reset_needed == 0; ++i) {
Brice Goglinc7dab992006-12-11 11:25:42 +01003506
Brice Goglin0dcffac2008-05-09 02:21:49 +02003507 ss = &mgp->ss[i];
3508 if (ss->rx_small.watchdog_needed) {
3509 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
3510 mgp->small_bytes + MXGEFW_PAD,
3511 1);
3512 if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
3513 myri10ge_fill_thresh)
3514 ss->rx_small.watchdog_needed = 0;
Brice Goglin626fda92007-08-09 09:02:14 +02003515 }
Brice Goglin0dcffac2008-05-09 02:21:49 +02003516 if (ss->rx_big.watchdog_needed) {
3517 myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
3518 mgp->big_bytes, 1);
3519 if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
3520 myri10ge_fill_thresh)
3521 ss->rx_big.watchdog_needed = 0;
3522 }
3523
3524 if (ss->tx.req != ss->tx.done &&
3525 ss->tx.done == ss->watchdog_tx_done &&
3526 ss->watchdog_tx_req != ss->watchdog_tx_done) {
3527 /* nic seems like it might be stuck.. */
3528 if (rx_pause_cnt != mgp->watchdog_pause) {
3529 if (net_ratelimit())
Brice Goglin236bb5e2008-09-28 15:34:21 +00003530 printk(KERN_WARNING
3531 "myri10ge %s slice %d:"
Brice Goglin0dcffac2008-05-09 02:21:49 +02003532 "TX paused, check link partner\n",
Brice Goglin236bb5e2008-09-28 15:34:21 +00003533 mgp->dev->name, i);
Brice Goglin0dcffac2008-05-09 02:21:49 +02003534 } else {
Brice Goglin236bb5e2008-09-28 15:34:21 +00003535 printk(KERN_WARNING
3536 "myri10ge %s slice %d stuck:",
3537 mgp->dev->name, i);
Brice Goglin0dcffac2008-05-09 02:21:49 +02003538 reset_needed = 1;
3539 }
3540 }
3541 ss->watchdog_tx_done = ss->tx.done;
3542 ss->watchdog_tx_req = ss->tx.req;
Brice Goglin626fda92007-08-09 09:02:14 +02003543 }
Brice Goglin626fda92007-08-09 09:02:14 +02003544 mgp->watchdog_pause = rx_pause_cnt;
Brice Goglin0dcffac2008-05-09 02:21:49 +02003545
3546 if (reset_needed) {
3547 schedule_work(&mgp->watchdog_work);
3548 } else {
3549 /* rearm timer */
3550 mod_timer(&mgp->watchdog_timer,
3551 jiffies + myri10ge_watchdog_timeout * HZ);
3552 }
Brice Goglin0da34b62006-05-23 06:10:15 -04003553}
3554
Brice Goglin77929732008-05-09 02:21:10 +02003555static void myri10ge_free_slices(struct myri10ge_priv *mgp)
3556{
3557 struct myri10ge_slice_state *ss;
3558 struct pci_dev *pdev = mgp->pdev;
3559 size_t bytes;
3560 int i;
3561
3562 if (mgp->ss == NULL)
3563 return;
3564
3565 for (i = 0; i < mgp->num_slices; i++) {
3566 ss = &mgp->ss[i];
3567 if (ss->rx_done.entry != NULL) {
3568 bytes = mgp->max_intr_slots *
3569 sizeof(*ss->rx_done.entry);
3570 dma_free_coherent(&pdev->dev, bytes,
3571 ss->rx_done.entry, ss->rx_done.bus);
3572 ss->rx_done.entry = NULL;
3573 }
3574 if (ss->fw_stats != NULL) {
3575 bytes = sizeof(*ss->fw_stats);
3576 dma_free_coherent(&pdev->dev, bytes,
3577 ss->fw_stats, ss->fw_stats_bus);
3578 ss->fw_stats = NULL;
3579 }
3580 }
3581 kfree(mgp->ss);
3582 mgp->ss = NULL;
3583}
3584
3585static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
3586{
3587 struct myri10ge_slice_state *ss;
3588 struct pci_dev *pdev = mgp->pdev;
3589 size_t bytes;
3590 int i;
3591
3592 bytes = sizeof(*mgp->ss) * mgp->num_slices;
3593 mgp->ss = kzalloc(bytes, GFP_KERNEL);
3594 if (mgp->ss == NULL) {
3595 return -ENOMEM;
3596 }
3597
3598 for (i = 0; i < mgp->num_slices; i++) {
3599 ss = &mgp->ss[i];
3600 bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
3601 ss->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
3602 &ss->rx_done.bus,
3603 GFP_KERNEL);
3604 if (ss->rx_done.entry == NULL)
3605 goto abort;
3606 memset(ss->rx_done.entry, 0, bytes);
3607 bytes = sizeof(*ss->fw_stats);
3608 ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
3609 &ss->fw_stats_bus,
3610 GFP_KERNEL);
3611 if (ss->fw_stats == NULL)
3612 goto abort;
3613 ss->mgp = mgp;
3614 ss->dev = mgp->dev;
3615 netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
3616 myri10ge_napi_weight);
3617 }
3618 return 0;
3619abort:
3620 myri10ge_free_slices(mgp);
3621 return -ENOMEM;
3622}
3623
3624/*
3625 * This function determines the number of slices supported.
3626 * The number slices is the minumum of the number of CPUS,
3627 * the number of MSI-X irqs supported, the number of slices
3628 * supported by the firmware
3629 */
3630static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
3631{
3632 struct myri10ge_cmd cmd;
3633 struct pci_dev *pdev = mgp->pdev;
3634 char *old_fw;
3635 int i, status, ncpus, msix_cap;
3636
3637 mgp->num_slices = 1;
3638 msix_cap = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3639 ncpus = num_online_cpus();
3640
3641 if (myri10ge_max_slices == 1 || msix_cap == 0 ||
3642 (myri10ge_max_slices == -1 && ncpus < 2))
3643 return;
3644
3645 /* try to load the slice aware rss firmware */
3646 old_fw = mgp->fw_name;
Brice Goglin13b27382008-08-13 21:05:52 +02003647 if (myri10ge_fw_name != NULL) {
3648 dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
3649 myri10ge_fw_name);
3650 mgp->fw_name = myri10ge_fw_name;
3651 } else if (old_fw == myri10ge_fw_aligned)
Brice Goglin77929732008-05-09 02:21:10 +02003652 mgp->fw_name = myri10ge_fw_rss_aligned;
3653 else
3654 mgp->fw_name = myri10ge_fw_rss_unaligned;
3655 status = myri10ge_load_firmware(mgp, 0);
3656 if (status != 0) {
3657 dev_info(&pdev->dev, "Rss firmware not found\n");
3658 return;
3659 }
3660
3661 /* hit the board with a reset to ensure it is alive */
3662 memset(&cmd, 0, sizeof(cmd));
3663 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
3664 if (status != 0) {
3665 dev_err(&mgp->pdev->dev, "failed reset\n");
3666 goto abort_with_fw;
3667 return;
3668 }
3669
3670 mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
3671
3672 /* tell it the size of the interrupt queues */
3673 cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
3674 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
3675 if (status != 0) {
3676 dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
3677 goto abort_with_fw;
3678 }
3679
3680 /* ask the maximum number of slices it supports */
3681 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
3682 if (status != 0)
3683 goto abort_with_fw;
3684 else
3685 mgp->num_slices = cmd.data0;
3686
3687 /* Only allow multiple slices if MSI-X is usable */
3688 if (!myri10ge_msi) {
3689 goto abort_with_fw;
3690 }
3691
3692 /* if the admin did not specify a limit to how many
3693 * slices we should use, cap it automatically to the
3694 * number of CPUs currently online */
3695 if (myri10ge_max_slices == -1)
3696 myri10ge_max_slices = ncpus;
3697
3698 if (mgp->num_slices > myri10ge_max_slices)
3699 mgp->num_slices = myri10ge_max_slices;
3700
3701 /* Now try to allocate as many MSI-X vectors as we have
3702 * slices. We give up on MSI-X if we can only get a single
3703 * vector. */
3704
3705 mgp->msix_vectors = kzalloc(mgp->num_slices *
3706 sizeof(*mgp->msix_vectors), GFP_KERNEL);
3707 if (mgp->msix_vectors == NULL)
3708 goto disable_msix;
3709 for (i = 0; i < mgp->num_slices; i++) {
3710 mgp->msix_vectors[i].entry = i;
3711 }
3712
3713 while (mgp->num_slices > 1) {
3714 /* make sure it is a power of two */
3715 while (!is_power_of_2(mgp->num_slices))
3716 mgp->num_slices--;
3717 if (mgp->num_slices == 1)
3718 goto disable_msix;
3719 status = pci_enable_msix(pdev, mgp->msix_vectors,
3720 mgp->num_slices);
3721 if (status == 0) {
3722 pci_disable_msix(pdev);
3723 return;
3724 }
3725 if (status > 0)
3726 mgp->num_slices = status;
3727 else
3728 goto disable_msix;
3729 }
3730
3731disable_msix:
3732 if (mgp->msix_vectors != NULL) {
3733 kfree(mgp->msix_vectors);
3734 mgp->msix_vectors = NULL;
3735 }
3736
3737abort_with_fw:
3738 mgp->num_slices = 1;
3739 mgp->fw_name = old_fw;
3740 myri10ge_load_firmware(mgp, 0);
3741}
Brice Goglin77929732008-05-09 02:21:10 +02003742
Stephen Hemminger81260892008-11-21 17:30:35 -08003743static const struct net_device_ops myri10ge_netdev_ops = {
3744 .ndo_open = myri10ge_open,
3745 .ndo_stop = myri10ge_close,
3746 .ndo_start_xmit = myri10ge_xmit,
3747 .ndo_get_stats = myri10ge_get_stats,
3748 .ndo_validate_addr = eth_validate_addr,
3749 .ndo_change_mtu = myri10ge_change_mtu,
3750 .ndo_set_multicast_list = myri10ge_set_multicast_list,
3751 .ndo_set_mac_address = myri10ge_set_mac_address,
3752};
3753
Brice Goglin0da34b62006-05-23 06:10:15 -04003754static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3755{
3756 struct net_device *netdev;
3757 struct myri10ge_priv *mgp;
3758 struct device *dev = &pdev->dev;
Brice Goglin0da34b62006-05-23 06:10:15 -04003759 int i;
3760 int status = -ENXIO;
Brice Goglin0da34b62006-05-23 06:10:15 -04003761 int dac_enabled;
Brice Goglin00b5e502008-11-20 01:50:28 -08003762 unsigned hdr_offset, ss_offset;
Brice Goglin0da34b62006-05-23 06:10:15 -04003763
Brice Goglin236bb5e2008-09-28 15:34:21 +00003764 netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
Brice Goglin0da34b62006-05-23 06:10:15 -04003765 if (netdev == NULL) {
3766 dev_err(dev, "Could not allocate ethernet device\n");
3767 return -ENOMEM;
3768 }
3769
Maik Hampelb245fb62007-06-28 17:07:26 +02003770 SET_NETDEV_DEV(netdev, &pdev->dev);
3771
Brice Goglin0da34b62006-05-23 06:10:15 -04003772 mgp = netdev_priv(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003773 mgp->dev = netdev;
3774 mgp->pdev = pdev;
3775 mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
3776 mgp->pause = myri10ge_flow_control;
3777 mgp->intr_coal_delay = myri10ge_intr_coal_delay;
Brice Goglinc58ac5c2006-08-21 17:36:49 -04003778 mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
Brice Goglin0da34b62006-05-23 06:10:15 -04003779 init_waitqueue_head(&mgp->down_wq);
3780
3781 if (pci_enable_device(pdev)) {
3782 dev_err(&pdev->dev, "pci_enable_device call failed\n");
3783 status = -ENODEV;
3784 goto abort_with_netdev;
3785 }
Brice Goglin0da34b62006-05-23 06:10:15 -04003786
3787 /* Find the vendor-specific cap so we can check
3788 * the reboot register later on */
3789 mgp->vendor_specific_offset
3790 = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
3791
3792 /* Set our max read request to 4KB */
Brice Goglin302d2422007-08-24 08:57:17 +02003793 status = pcie_set_readrq(pdev, 4096);
Brice Goglin0da34b62006-05-23 06:10:15 -04003794 if (status != 0) {
3795 dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
3796 status);
Brice Gogline3fd5532009-01-17 08:27:19 +00003797 goto abort_with_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04003798 }
3799
3800 pci_set_master(pdev);
3801 dac_enabled = 1;
Yang Hongyang6a355282009-04-06 19:01:13 -07003802 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Brice Goglin0da34b62006-05-23 06:10:15 -04003803 if (status != 0) {
3804 dac_enabled = 0;
3805 dev_err(&pdev->dev,
Joe Perches898eb712007-10-18 03:06:30 -07003806 "64-bit pci address mask was refused, "
3807 "trying 32-bit\n");
Yang Hongyang284901a2009-04-06 19:01:15 -07003808 status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Brice Goglin0da34b62006-05-23 06:10:15 -04003809 }
3810 if (status != 0) {
3811 dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
Brice Gogline3fd5532009-01-17 08:27:19 +00003812 goto abort_with_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04003813 }
Yang Hongyang6a355282009-04-06 19:01:13 -07003814 (void)pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Brice Goglinb10c0662006-06-08 10:25:00 -04003815 mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
3816 &mgp->cmd_bus, GFP_KERNEL);
Brice Goglin0da34b62006-05-23 06:10:15 -04003817 if (mgp->cmd == NULL)
Brice Gogline3fd5532009-01-17 08:27:19 +00003818 goto abort_with_enabled;
Brice Goglin0da34b62006-05-23 06:10:15 -04003819
Brice Goglin0da34b62006-05-23 06:10:15 -04003820 mgp->board_span = pci_resource_len(pdev, 0);
3821 mgp->iomem_base = pci_resource_start(pdev, 0);
3822 mgp->mtrr = -1;
Brice Goglin276e26c2007-03-07 20:02:32 +01003823 mgp->wc_enabled = 0;
Brice Goglin0da34b62006-05-23 06:10:15 -04003824#ifdef CONFIG_MTRR
3825 mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
3826 MTRR_TYPE_WRCOMB, 1);
Brice Goglin276e26c2007-03-07 20:02:32 +01003827 if (mgp->mtrr >= 0)
3828 mgp->wc_enabled = 1;
Brice Goglin0da34b62006-05-23 06:10:15 -04003829#endif
Brice Goglinc7f80992008-07-21 10:26:25 +02003830 mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
Brice Goglin0da34b62006-05-23 06:10:15 -04003831 if (mgp->sram == NULL) {
3832 dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
3833 mgp->board_span, mgp->iomem_base);
3834 status = -ENXIO;
Brice Goglinc7f80992008-07-21 10:26:25 +02003835 goto abort_with_mtrr;
Brice Goglin0da34b62006-05-23 06:10:15 -04003836 }
Brice Goglin00b5e502008-11-20 01:50:28 -08003837 hdr_offset =
3838 ntohl(__raw_readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
3839 ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
3840 mgp->sram_size = ntohl(__raw_readl(mgp->sram + ss_offset));
3841 if (mgp->sram_size > mgp->board_span ||
3842 mgp->sram_size <= MYRI10GE_FW_OFFSET) {
3843 dev_err(&pdev->dev,
3844 "invalid sram_size %dB or board span %ldB\n",
3845 mgp->sram_size, mgp->board_span);
3846 goto abort_with_ioremap;
3847 }
Brice Goglin0da34b62006-05-23 06:10:15 -04003848 memcpy_fromio(mgp->eeprom_strings,
Brice Goglin00b5e502008-11-20 01:50:28 -08003849 mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
Brice Goglin0da34b62006-05-23 06:10:15 -04003850 memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
3851 status = myri10ge_read_mac_addr(mgp);
3852 if (status)
3853 goto abort_with_ioremap;
3854
3855 for (i = 0; i < ETH_ALEN; i++)
3856 netdev->dev_addr[i] = mgp->mac_addr[i];
3857
Brice Goglin5443e9e2007-05-07 23:52:22 +02003858 myri10ge_select_firmware(mgp);
3859
Brice Goglin0dcffac2008-05-09 02:21:49 +02003860 status = myri10ge_load_firmware(mgp, 1);
Brice Goglin0da34b62006-05-23 06:10:15 -04003861 if (status != 0) {
3862 dev_err(&pdev->dev, "failed to load firmware\n");
Brice Goglin0dcffac2008-05-09 02:21:49 +02003863 goto abort_with_ioremap;
3864 }
3865 myri10ge_probe_slices(mgp);
3866 status = myri10ge_alloc_slices(mgp);
3867 if (status != 0) {
3868 dev_err(&pdev->dev, "failed to alloc slice state\n");
3869 goto abort_with_firmware;
Brice Goglin0da34b62006-05-23 06:10:15 -04003870 }
Brice Goglin236bb5e2008-09-28 15:34:21 +00003871 netdev->real_num_tx_queues = mgp->num_slices;
Brice Goglin0da34b62006-05-23 06:10:15 -04003872 status = myri10ge_reset(mgp);
3873 if (status != 0) {
3874 dev_err(&pdev->dev, "failed reset\n");
Brice Goglin0dcffac2008-05-09 02:21:49 +02003875 goto abort_with_slices;
Brice Goglin0da34b62006-05-23 06:10:15 -04003876 }
Jeff Garzik5dd2d332008-10-16 05:09:31 -04003877#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02003878 myri10ge_setup_dca(mgp);
3879#endif
Brice Goglin0da34b62006-05-23 06:10:15 -04003880 pci_set_drvdata(pdev, mgp);
3881 if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
3882 myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
3883 if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
3884 myri10ge_initial_mtu = 68;
Stephen Hemminger81260892008-11-21 17:30:35 -08003885
3886 netdev->netdev_ops = &myri10ge_netdev_ops;
Brice Goglin0da34b62006-05-23 06:10:15 -04003887 netdev->mtu = myri10ge_initial_mtu;
Brice Goglin0da34b62006-05-23 06:10:15 -04003888 netdev->base_addr = mgp->iomem_base;
Brice Goglin4f93fde2007-10-13 12:34:01 +02003889 netdev->features = mgp->features;
Brice Goglin236bb5e2008-09-28 15:34:21 +00003890
Brice Goglin0da34b62006-05-23 06:10:15 -04003891 if (dac_enabled)
3892 netdev->features |= NETIF_F_HIGHDMA;
Brice Goglin0da34b62006-05-23 06:10:15 -04003893
Brice Goglin21d05db2007-01-09 21:05:04 +01003894 /* make sure we can get an irq, and that MSI can be
3895 * setup (if available). Also ensure netdev->irq
3896 * is set to correct value if MSI is enabled */
3897 status = myri10ge_request_irq(mgp);
3898 if (status != 0)
3899 goto abort_with_firmware;
3900 netdev->irq = pdev->irq;
3901 myri10ge_free_irq(mgp);
3902
Brice Goglin0da34b62006-05-23 06:10:15 -04003903 /* Save configuration space to be restored if the
3904 * nic resets due to a parity error */
Brice Goglin83f6e152006-12-18 11:52:02 +01003905 pci_save_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003906
3907 /* Setup the watchdog timer */
3908 setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
3909 (unsigned long)mgp);
3910
Brice Goglin59081822009-04-16 02:23:56 +00003911 spin_lock_init(&mgp->stats_lock);
Brice Goglin0da34b62006-05-23 06:10:15 -04003912 SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
David Howellsc4028952006-11-22 14:57:56 +00003913 INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
Brice Goglin0da34b62006-05-23 06:10:15 -04003914 status = register_netdev(netdev);
3915 if (status != 0) {
3916 dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
Brice Goglin7adda302006-12-18 11:50:00 +01003917 goto abort_with_state;
Brice Goglin0da34b62006-05-23 06:10:15 -04003918 }
Brice Goglin0dcffac2008-05-09 02:21:49 +02003919 if (mgp->msix_enabled)
3920 dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, WC %s\n",
3921 mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
3922 (mgp->wc_enabled ? "Enabled" : "Disabled"));
3923 else
3924 dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
3925 mgp->msi_enabled ? "MSI" : "xPIC",
3926 netdev->irq, mgp->tx_boundary, mgp->fw_name,
3927 (mgp->wc_enabled ? "Enabled" : "Disabled"));
Brice Goglin0da34b62006-05-23 06:10:15 -04003928
3929 return 0;
3930
Brice Goglin7adda302006-12-18 11:50:00 +01003931abort_with_state:
Brice Goglin83f6e152006-12-18 11:52:02 +01003932 pci_restore_state(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003933
Brice Goglin0dcffac2008-05-09 02:21:49 +02003934abort_with_slices:
3935 myri10ge_free_slices(mgp);
3936
Brice Goglin0da34b62006-05-23 06:10:15 -04003937abort_with_firmware:
3938 myri10ge_dummy_rdma(mgp, 0);
3939
Brice Goglin0da34b62006-05-23 06:10:15 -04003940abort_with_ioremap:
Brice Goglin0f840012009-01-05 18:16:14 -08003941 if (mgp->mac_addr_string != NULL)
3942 dev_err(&pdev->dev,
3943 "myri10ge_probe() failed: MAC=%s, SN=%ld\n",
3944 mgp->mac_addr_string, mgp->serial_number);
Brice Goglin0da34b62006-05-23 06:10:15 -04003945 iounmap(mgp->sram);
3946
Brice Goglinc7f80992008-07-21 10:26:25 +02003947abort_with_mtrr:
Brice Goglin0da34b62006-05-23 06:10:15 -04003948#ifdef CONFIG_MTRR
3949 if (mgp->mtrr >= 0)
3950 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3951#endif
Brice Goglinb10c0662006-06-08 10:25:00 -04003952 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3953 mgp->cmd, mgp->cmd_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04003954
Brice Gogline3fd5532009-01-17 08:27:19 +00003955abort_with_enabled:
3956 pci_disable_device(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003957
Brice Gogline3fd5532009-01-17 08:27:19 +00003958abort_with_netdev:
Brice Goglin0da34b62006-05-23 06:10:15 -04003959 free_netdev(netdev);
3960 return status;
3961}
3962
3963/*
3964 * myri10ge_remove
3965 *
3966 * Does what is necessary to shutdown one Myrinet device. Called
3967 * once for each Myrinet card by the kernel when a module is
3968 * unloaded.
3969 */
3970static void myri10ge_remove(struct pci_dev *pdev)
3971{
3972 struct myri10ge_priv *mgp;
3973 struct net_device *netdev;
Brice Goglin0da34b62006-05-23 06:10:15 -04003974
3975 mgp = pci_get_drvdata(pdev);
3976 if (mgp == NULL)
3977 return;
3978
3979 flush_scheduled_work();
3980 netdev = mgp->dev;
3981 unregister_netdev(netdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04003982
Jeff Garzik5dd2d332008-10-16 05:09:31 -04003983#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02003984 myri10ge_teardown_dca(mgp);
3985#endif
Brice Goglin0da34b62006-05-23 06:10:15 -04003986 myri10ge_dummy_rdma(mgp, 0);
3987
Brice Goglin7adda302006-12-18 11:50:00 +01003988 /* avoid a memory leak */
Brice Goglin83f6e152006-12-18 11:52:02 +01003989 pci_restore_state(pdev);
Brice Goglin7adda302006-12-18 11:50:00 +01003990
Brice Goglin0da34b62006-05-23 06:10:15 -04003991 iounmap(mgp->sram);
3992
3993#ifdef CONFIG_MTRR
3994 if (mgp->mtrr >= 0)
3995 mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
3996#endif
Brice Goglin0dcffac2008-05-09 02:21:49 +02003997 myri10ge_free_slices(mgp);
3998 if (mgp->msix_vectors != NULL)
3999 kfree(mgp->msix_vectors);
Brice Goglinb10c0662006-06-08 10:25:00 -04004000 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
4001 mgp->cmd, mgp->cmd_bus);
Brice Goglin0da34b62006-05-23 06:10:15 -04004002
4003 free_netdev(netdev);
Brice Gogline3fd5532009-01-17 08:27:19 +00004004 pci_disable_device(pdev);
Brice Goglin0da34b62006-05-23 06:10:15 -04004005 pci_set_drvdata(pdev, NULL);
4006}
4007
Brice Goglinb10c0662006-06-08 10:25:00 -04004008#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
Brice Goglina07bc1f2007-09-14 00:40:14 +02004009#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
Brice Goglin0da34b62006-05-23 06:10:15 -04004010
4011static struct pci_device_id myri10ge_pci_tbl[] = {
Brice Goglinb10c0662006-06-08 10:25:00 -04004012 {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
Brice Goglina07bc1f2007-09-14 00:40:14 +02004013 {PCI_DEVICE
4014 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
Brice Goglin0da34b62006-05-23 06:10:15 -04004015 {0},
4016};
4017
4018static struct pci_driver myri10ge_driver = {
4019 .name = "myri10ge",
4020 .probe = myri10ge_probe,
4021 .remove = myri10ge_remove,
4022 .id_table = myri10ge_pci_tbl,
4023#ifdef CONFIG_PM
4024 .suspend = myri10ge_suspend,
4025 .resume = myri10ge_resume,
4026#endif
4027};
4028
Jeff Garzik5dd2d332008-10-16 05:09:31 -04004029#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02004030static int
4031myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
4032{
4033 int err = driver_for_each_device(&myri10ge_driver.driver,
4034 NULL, &event,
4035 myri10ge_notify_dca_device);
4036
4037 if (err)
4038 return NOTIFY_BAD;
4039 return NOTIFY_DONE;
4040}
4041
4042static struct notifier_block myri10ge_dca_notifier = {
4043 .notifier_call = myri10ge_notify_dca,
4044 .next = NULL,
4045 .priority = 0,
4046};
Brice Goglin4ee2ac52008-11-23 15:49:28 -08004047#endif /* CONFIG_MYRI10GE_DCA */
Brice Goglin981813d2008-05-09 02:22:16 +02004048
Brice Goglin0da34b62006-05-23 06:10:15 -04004049static __init int myri10ge_init_module(void)
4050{
4051 printk(KERN_INFO "%s: Version %s\n", myri10ge_driver.name,
4052 MYRI10GE_VERSION_STR);
Brice Goglin0dcffac2008-05-09 02:21:49 +02004053
Brice Goglin236bb5e2008-09-28 15:34:21 +00004054 if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
Brice Goglin0dcffac2008-05-09 02:21:49 +02004055 printk(KERN_ERR
4056 "%s: Illegal rssh hash type %d, defaulting to source port\n",
4057 myri10ge_driver.name, myri10ge_rss_hash);
4058 myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
4059 }
Jeff Garzik5dd2d332008-10-16 05:09:31 -04004060#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02004061 dca_register_notify(&myri10ge_dca_notifier);
4062#endif
Brice Goglin236bb5e2008-09-28 15:34:21 +00004063 if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
4064 myri10ge_max_slices = MYRI10GE_MAX_SLICES;
Brice Goglin0dcffac2008-05-09 02:21:49 +02004065
Brice Goglin0da34b62006-05-23 06:10:15 -04004066 return pci_register_driver(&myri10ge_driver);
4067}
4068
4069module_init(myri10ge_init_module);
4070
4071static __exit void myri10ge_cleanup_module(void)
4072{
Jeff Garzik5dd2d332008-10-16 05:09:31 -04004073#ifdef CONFIG_MYRI10GE_DCA
Brice Goglin981813d2008-05-09 02:22:16 +02004074 dca_unregister_notify(&myri10ge_dca_notifier);
4075#endif
Brice Goglin0da34b62006-05-23 06:10:15 -04004076 pci_unregister_driver(&myri10ge_driver);
4077}
4078
4079module_exit(myri10ge_cleanup_module);