myri10ge.c 93.9 KB
Newer Older
1 2 3
/*************************************************************************
 * myri10ge.c: Myricom Myri-10G Ethernet driver.
 *
4
 * Copyright (C) 2005 - 2007 Myricom, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Myricom, Inc. nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
19 20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 23 24 25 26 27 28 29
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *
 *
 * If the eeprom on your board is not recent enough, you will need to get a
 * newer firmware image at:
 *   http://www.myri.com/scs/download-Myri10GE.html
 *
 * Contact Information:
 *   <help@myri.com>
 *   Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
 *************************************************************************/

#include <linux/tcp.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/module.h>
#include <linux/pci.h>
B
Brice Goglin 已提交
47
#include <linux/dma-mapping.h>
48 49 50
#include <linux/etherdevice.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
A
Andrew Gallatin 已提交
51
#include <linux/inet_lro.h>
52 53 54 55 56 57 58 59 60 61 62 63
#include <linux/ip.h>
#include <linux/inet.h>
#include <linux/in.h>
#include <linux/ethtool.h>
#include <linux/firmware.h>
#include <linux/delay.h>
#include <linux/version.h>
#include <linux/timer.h>
#include <linux/vmalloc.h>
#include <linux/crc32.h>
#include <linux/moduleparam.h>
#include <linux/io.h>
64
#include <linux/log2.h>
65
#include <net/checksum.h>
A
Andrew Gallatin 已提交
66 67
#include <net/ip.h>
#include <net/tcp.h>
68 69 70 71 72 73 74 75 76 77
#include <asm/byteorder.h>
#include <asm/io.h>
#include <asm/processor.h>
#ifdef CONFIG_MTRR
#include <asm/mtrr.h>
#endif

#include "myri10ge_mcp.h"
#include "myri10ge_mcp_gen_header.h"

78
#define MYRI10GE_VERSION_STR "1.3.2-1.287"
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
MODULE_AUTHOR("Maintainer: help@myri.com");
MODULE_VERSION(MYRI10GE_VERSION_STR);
MODULE_LICENSE("Dual BSD/GPL");

#define MYRI10GE_MAX_ETHER_MTU 9014

#define MYRI10GE_ETH_STOPPED 0
#define MYRI10GE_ETH_STOPPING 1
#define MYRI10GE_ETH_STARTING 2
#define MYRI10GE_ETH_RUNNING 3
#define MYRI10GE_ETH_OPEN_FAILED 4

#define MYRI10GE_EEPROM_STRINGS_SIZE 256
#define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
A
Andrew Gallatin 已提交
95 96
#define MYRI10GE_MAX_LRO_DESCRIPTORS 8
#define MYRI10GE_LRO_MAX_PKTS 64
97

A
Al Viro 已提交
98
#define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
99 100
#define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff

101 102 103 104
#define MYRI10GE_ALLOC_ORDER 0
#define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
#define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)

105
struct myri10ge_rx_buffer_state {
106 107
	struct page *page;
	int page_offset;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	 DECLARE_PCI_UNMAP_ADDR(bus)
	 DECLARE_PCI_UNMAP_LEN(len)
};

struct myri10ge_tx_buffer_state {
	struct sk_buff *skb;
	int last;
	 DECLARE_PCI_UNMAP_ADDR(bus)
	 DECLARE_PCI_UNMAP_LEN(len)
};

struct myri10ge_cmd {
	u32 data0;
	u32 data1;
	u32 data2;
};

struct myri10ge_rx_buf {
	struct mcp_kreq_ether_recv __iomem *lanai;	/* lanai ptr for recv ring */
	u8 __iomem *wc_fifo;	/* w/c rx dma addr fifo address */
	struct mcp_kreq_ether_recv *shadow;	/* host shadow of recv ring */
	struct myri10ge_rx_buffer_state *info;
130 131 132
	struct page *page;
	dma_addr_t bus;
	int page_offset;
133
	int cnt;
134
	int fill_cnt;
135 136
	int alloc_fail;
	int mask;		/* number of rx slots -1 */
137
	int watchdog_needed;
138 139 140 141 142 143 144 145 146 147 148
};

struct myri10ge_tx_buf {
	struct mcp_kreq_ether_send __iomem *lanai;	/* lanai ptr for sendq */
	u8 __iomem *wc_fifo;	/* w/c send fifo address */
	struct mcp_kreq_ether_send *req_list;	/* host shadow of sendq */
	char *req_bytes;
	struct myri10ge_tx_buffer_state *info;
	int mask;		/* number of transmit slots -1  */
	int req ____cacheline_aligned;	/* transmit slots submitted     */
	int pkt_start;		/* packets started */
149 150
	int stop_queue;
	int linearized;
151 152
	int done ____cacheline_aligned;	/* transmit slots completed     */
	int pkt_done;		/* packets completed */
153
	int wake_queue;
154 155 156 157 158 159 160
};

struct myri10ge_rx_done {
	struct mcp_slot *entry;
	dma_addr_t bus;
	int cnt;
	int idx;
A
Andrew Gallatin 已提交
161 162
	struct net_lro_mgr lro_mgr;
	struct net_lro_desc lro_desc[MYRI10GE_MAX_LRO_DESCRIPTORS];
163 164
};

165 166 167 168 169 170 171 172 173 174
struct myri10ge_slice_netstats {
	unsigned long rx_packets;
	unsigned long tx_packets;
	unsigned long rx_bytes;
	unsigned long tx_bytes;
	unsigned long rx_dropped;
	unsigned long tx_dropped;
};

struct myri10ge_slice_state {
175 176 177 178
	struct myri10ge_tx_buf tx;	/* transmit ring        */
	struct myri10ge_rx_buf rx_small;
	struct myri10ge_rx_buf rx_big;
	struct myri10ge_rx_done rx_done;
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	struct net_device *dev;
	struct napi_struct napi;
	struct myri10ge_priv *mgp;
	struct myri10ge_slice_netstats stats;
	__be32 __iomem *irq_claim;
	struct mcp_irq_data *fw_stats;
	dma_addr_t fw_stats_bus;
	int watchdog_tx_done;
	int watchdog_tx_req;
};

struct myri10ge_priv {
	struct myri10ge_slice_state ss;
	int tx_boundary;	/* boundary transmits cannot cross */
	int running;		/* running?             */
	int csum_flag;		/* rx_csums?            */
195
	int small_bytes;
196
	int big_bytes;
197 198
	struct net_device *dev;
	struct net_device_stats stats;
199
	spinlock_t stats_lock;
200 201 202 203
	u8 __iomem *sram;
	int sram_size;
	unsigned long board_span;
	unsigned long iomem_base;
A
Al Viro 已提交
204
	__be32 __iomem *irq_deassert;
205 206 207 208 209
	char *mac_addr_string;
	struct mcp_cmd_response *cmd;
	dma_addr_t cmd_bus;
	struct pci_dev *pdev;
	int msi_enabled;
A
Al Viro 已提交
210
	u32 link_state;
211 212
	unsigned int rdma_tags_available;
	int intr_coal_delay;
A
Al Viro 已提交
213
	__be32 __iomem *intr_coal_delay_ptr;
214
	int mtrr;
215
	int wc_enabled;
216 217 218 219 220
	int down_cnt;
	wait_queue_head_t down_wq;
	struct work_struct watchdog_work;
	struct timer_list watchdog_timer;
	int watchdog_resets;
221
	int watchdog_pause;
222 223 224
	int pause;
	char *fw_name;
	char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
225
	char *product_code_string;
226
	char fw_version[128];
227 228 229 230
	int fw_ver_major;
	int fw_ver_minor;
	int fw_ver_tiny;
	int adopted_rx_filter_bug;
231 232 233
	u8 mac_addr[6];		/* eeprom mac address */
	unsigned long serial_number;
	int vendor_specific_offset;
234
	int fw_multicast_support;
B
Brice Goglin 已提交
235 236
	unsigned long features;
	u32 max_tso6;
237 238 239
	u32 read_dma;
	u32 write_dma;
	u32 read_write_dma;
240 241
	u32 link_changes;
	u32 msg_enable;
242 243 244 245 246 247 248
};

static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";

static char *myri10ge_fw_name = NULL;
module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
249
MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
250 251 252

static int myri10ge_ecrc_enable = 1;
module_param(myri10ge_ecrc_enable, int, S_IRUGO);
253
MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
254 255 256

static int myri10ge_max_intr_slots = 1024;
module_param(myri10ge_max_intr_slots, int, S_IRUGO);
257
MODULE_PARM_DESC(myri10ge_max_intr_slots, "Interrupt queue slots");
258 259 260

static int myri10ge_small_bytes = -1;	/* -1 == auto */
module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
261
MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
262 263

static int myri10ge_msi = 1;	/* enable msi by default */
264
module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
265
MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
266

267
static int myri10ge_intr_coal_delay = 75;
268
module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
269
MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
270 271 272

static int myri10ge_flow_control = 1;
module_param(myri10ge_flow_control, int, S_IRUGO);
273
MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
274 275 276 277

static int myri10ge_deassert_wait = 1;
module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(myri10ge_deassert_wait,
278
		 "Wait when deasserting legacy interrupts");
279 280 281 282

static int myri10ge_force_firmware = 0;
module_param(myri10ge_force_firmware, int, S_IRUGO);
MODULE_PARM_DESC(myri10ge_force_firmware,
283
		 "Force firmware to assume aligned completions");
284 285 286

static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
module_param(myri10ge_initial_mtu, int, S_IRUGO);
287
MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
288 289 290

static int myri10ge_napi_weight = 64;
module_param(myri10ge_napi_weight, int, S_IRUGO);
291
MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
292 293 294

static int myri10ge_watchdog_timeout = 1;
module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
295
MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
296 297 298 299

static int myri10ge_max_irq_loops = 1048576;
module_param(myri10ge_max_irq_loops, int, S_IRUGO);
MODULE_PARM_DESC(myri10ge_max_irq_loops,
300
		 "Set stuck legacy IRQ detection threshold");
301

302 303 304 305 306 307
#define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK

static int myri10ge_debug = -1;	/* defaults above */
module_param(myri10ge_debug, int, 0);
MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");

A
Andrew Gallatin 已提交
308 309
static int myri10ge_lro = 1;
module_param(myri10ge_lro, int, S_IRUGO);
310
MODULE_PARM_DESC(myri10ge_lro, "Enable large receive offload");
A
Andrew Gallatin 已提交
311 312 313

static int myri10ge_lro_max_pkts = MYRI10GE_LRO_MAX_PKTS;
module_param(myri10ge_lro_max_pkts, int, S_IRUGO);
314 315
MODULE_PARM_DESC(myri10ge_lro_max_pkts,
		 "Number of LRO packets to be aggregated");
A
Andrew Gallatin 已提交
316

317 318
static int myri10ge_fill_thresh = 256;
module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
319
MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
320

321 322
static int myri10ge_reset_recover = 1;

323
static int myri10ge_wcfifo = 0;
324
module_param(myri10ge_wcfifo, int, S_IRUGO);
325
MODULE_PARM_DESC(myri10ge_wcfifo, "Enable WC Fifo when WC is enabled");
326

327 328 329 330 331 332 333
#define MYRI10GE_FW_OFFSET 1024*1024
#define MYRI10GE_HIGHPART_TO_U32(X) \
(sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
#define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))

#define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)

334
static void myri10ge_set_multicast_list(struct net_device *dev);
B
Brice Goglin 已提交
335
static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev);
336

337
static inline void put_be32(__be32 val, __be32 __iomem * p)
A
Al Viro 已提交
338
{
339
	__raw_writel((__force __u32) val, (__force void __iomem *)p);
A
Al Viro 已提交
340 341
}

342 343 344 345 346 347 348
static int
myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
		  struct myri10ge_cmd *data, int atomic)
{
	struct mcp_cmd *buf;
	char buf_bytes[sizeof(*buf) + 8];
	struct mcp_cmd_response *response = mgp->cmd;
349
	char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	u32 dma_low, dma_high, result, value;
	int sleep_total = 0;

	/* ensure buf is aligned to 8 bytes */
	buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);

	buf->data0 = htonl(data->data0);
	buf->data1 = htonl(data->data1);
	buf->data2 = htonl(data->data2);
	buf->cmd = htonl(cmd);
	dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
	dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);

	buf->response_addr.low = htonl(dma_low);
	buf->response_addr.high = htonl(dma_high);
A
Al Viro 已提交
365
	response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
366 367 368 369 370 371 372 373 374 375 376 377 378 379
	mb();
	myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));

	/* wait up to 15ms. Longest command is the DMA benchmark,
	 * which is capped at 5ms, but runs from a timeout handler
	 * that runs every 7.8ms. So a 15ms timeout leaves us with
	 * a 2.2ms margin
	 */
	if (atomic) {
		/* if atomic is set, do not sleep,
		 * and try to get the completion quickly
		 * (1ms will be enough for those commands) */
		for (sleep_total = 0;
		     sleep_total < 1000
A
Al Viro 已提交
380
		     && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
381
		     sleep_total += 10) {
382
			udelay(10);
383 384
			mb();
		}
385 386 387 388
	} else {
		/* use msleep for most command */
		for (sleep_total = 0;
		     sleep_total < 15
A
Al Viro 已提交
389
		     && response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
390 391 392 393 394 395 396 397 398 399
		     sleep_total++)
			msleep(1);
	}

	result = ntohl(response->result);
	value = ntohl(response->data);
	if (result != MYRI10GE_NO_RESPONSE_RESULT) {
		if (result == 0) {
			data->data0 = value;
			return 0;
400 401
		} else if (result == MXGEFW_CMD_UNKNOWN) {
			return -ENOSYS;
402 403
		} else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
			return -E2BIG;
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
		} else {
			dev_err(&mgp->pdev->dev,
				"command %d failed, result = %d\n",
				cmd, result);
			return -ENXIO;
		}
	}

	dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
		cmd, result);
	return -EAGAIN;
}

/*
 * The eeprom strings on the lanaiX have the format
 * SN=x\0
 * MAC=x:x:x:x:x:x\0
 * PT:ddd mmm xx xx:xx:xx xx\0
 * PV:ddd mmm xx xx:xx:xx xx\0
 */
static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
{
	char *ptr, *limit;
	int i;

	ptr = mgp->eeprom_strings;
	limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;

	while (*ptr != '\0' && ptr < limit) {
		if (memcmp(ptr, "MAC=", 4) == 0) {
			ptr += 4;
			mgp->mac_addr_string = ptr;
			for (i = 0; i < 6; i++) {
				if ((ptr + 2) > limit)
					goto abort;
				mgp->mac_addr[i] =
				    simple_strtoul(ptr, &ptr, 16);
				ptr += 1;
			}
		}
444 445 446 447
		if (memcmp(ptr, "PC=", 3) == 0) {
			ptr += 3;
			mgp->product_code_string = ptr;
		}
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
		if (memcmp((const void *)ptr, "SN=", 3) == 0) {
			ptr += 3;
			mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
		}
		while (ptr < limit && *ptr++) ;
	}

	return 0;

abort:
	dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
	return -ENXIO;
}

/*
 * Enable or disable periodic RDMAs from the host to make certain
 * chipsets resend dropped PCIe messages
 */

static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
{
	char __iomem *submit;
470
	__be32 buf[16] __attribute__ ((__aligned__(8)));
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	u32 dma_low, dma_high;
	int i;

	/* clear confirmation addr */
	mgp->cmd->data = 0;
	mb();

	/* send a rdma command to the PCIe engine, and wait for the
	 * response in the confirmation address.  The firmware should
	 * write a -1 there to indicate it is alive and well
	 */
	dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
	dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);

	buf[0] = htonl(dma_high);	/* confirm addr MSW */
	buf[1] = htonl(dma_low);	/* confirm addr LSW */
A
Al Viro 已提交
487
	buf[2] = MYRI10GE_NO_CONFIRM_DATA;	/* confirm data */
488 489 490 491
	buf[3] = htonl(dma_high);	/* dummy addr MSW */
	buf[4] = htonl(dma_low);	/* dummy addr LSW */
	buf[5] = htonl(enable);	/* enable? */

492
	submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

	myri10ge_pio_copy(submit, &buf, sizeof(buf));
	for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
		msleep(1);
	if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
		dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
			(enable ? "enable" : "disable"));
}

static int
myri10ge_validate_firmware(struct myri10ge_priv *mgp,
			   struct mcp_gen_header *hdr)
{
	struct device *dev = &mgp->pdev->dev;

	/* check firmware type */
	if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
		dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
		return -EINVAL;
	}

	/* save firmware version for ethtool */
	strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));

517 518
	sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
	       &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
519

520 521
	if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR
	      && mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
		dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
		dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
			MXGEFW_VERSION_MINOR);
		return -EINVAL;
	}
	return 0;
}

static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
{
	unsigned crc, reread_crc;
	const struct firmware *fw;
	struct device *dev = &mgp->pdev->dev;
	struct mcp_gen_header *hdr;
	size_t hdr_offset;
	int status;
538
	unsigned i;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

	if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
		dev_err(dev, "Unable to load %s firmware image via hotplug\n",
			mgp->fw_name);
		status = -EINVAL;
		goto abort_with_nothing;
	}

	/* check size */

	if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
	    fw->size < MCP_HEADER_PTR_OFFSET + 4) {
		dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
		status = -EINVAL;
		goto abort_with_fw;
	}

	/* check id */
A
Al Viro 已提交
557
	hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
558 559 560 561 562 563 564 565 566 567 568 569
	if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
		dev_err(dev, "Bad firmware file\n");
		status = -EINVAL;
		goto abort_with_fw;
	}
	hdr = (void *)(fw->data + hdr_offset);

	status = myri10ge_validate_firmware(mgp, hdr);
	if (status != 0)
		goto abort_with_fw;

	crc = crc32(~0, fw->data, fw->size);
570 571 572 573 574 575
	for (i = 0; i < fw->size; i += 256) {
		myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
				  fw->data + i,
				  min(256U, (unsigned)(fw->size - i)));
		mb();
		readb(mgp->sram);
B
Brice Goglin 已提交
576
	}
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	/* corruption checking is good for parity recovery and buggy chipset */
	memcpy_fromio(fw->data, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
	reread_crc = crc32(~0, fw->data, fw->size);
	if (crc != reread_crc) {
		dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
			(unsigned)fw->size, reread_crc, crc);
		status = -EIO;
		goto abort_with_fw;
	}
	*size = (u32) fw->size;

abort_with_fw:
	release_firmware(fw);

abort_with_nothing:
	return status;
}

static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
{
	struct mcp_gen_header *hdr;
	struct device *dev = &mgp->pdev->dev;
	const size_t bytes = sizeof(struct mcp_gen_header);
	size_t hdr_offset;
	int status;

	/* find running firmware header */
A
Al Viro 已提交
604
	hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

	if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
		dev_err(dev, "Running firmware has bad header offset (%d)\n",
			(int)hdr_offset);
		return -EIO;
	}

	/* copy header of running firmware from SRAM to host memory to
	 * validate firmware */
	hdr = kmalloc(bytes, GFP_KERNEL);
	if (hdr == NULL) {
		dev_err(dev, "could not malloc firmware hdr\n");
		return -ENOMEM;
	}
	memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
	status = myri10ge_validate_firmware(mgp, hdr);
	kfree(hdr);
622 623 624 625 626 627 628 629 630 631 632 633

	/* check to see if adopted firmware has bug where adopting
	 * it will cause broadcasts to be filtered unless the NIC
	 * is kept in ALLMULTI mode */
	if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
	    mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
		mgp->adopted_rx_filter_bug = 1;
		dev_warn(dev, "Adopting fw %d.%d.%d: "
			 "working around rx filter bug\n",
			 mgp->fw_ver_major, mgp->fw_ver_minor,
			 mgp->fw_ver_tiny);
	}
634 635 636 637 638 639
	return status;
}

static int myri10ge_load_firmware(struct myri10ge_priv *mgp)
{
	char __iomem *submit;
640
	__be32 buf[16] __attribute__ ((__aligned__(8)));
641 642
	u32 dma_low, dma_high, size;
	int status, i;
B
Brice Goglin 已提交
643
	struct myri10ge_cmd cmd;
644

B
Brice Goglin 已提交
645
	size = 0;
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	status = myri10ge_load_hotplug_firmware(mgp, &size);
	if (status) {
		dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");

		/* Do not attempt to adopt firmware if there
		 * was a bad crc */
		if (status == -EIO)
			return status;

		status = myri10ge_adopt_running_firmware(mgp);
		if (status != 0) {
			dev_err(&mgp->pdev->dev,
				"failed to adopt running firmware\n");
			return status;
		}
		dev_info(&mgp->pdev->dev,
			 "Successfully adopted running firmware\n");
663
		if (mgp->tx_boundary == 4096) {
664 665 666 667 668 669 670 671 672 673
			dev_warn(&mgp->pdev->dev,
				 "Using firmware currently running on NIC"
				 ".  For optimal\n");
			dev_warn(&mgp->pdev->dev,
				 "performance consider loading optimized "
				 "firmware\n");
			dev_warn(&mgp->pdev->dev, "via hotplug\n");
		}

		mgp->fw_name = "adopted";
674
		mgp->tx_boundary = 2048;
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
		return status;
	}

	/* clear confirmation addr */
	mgp->cmd->data = 0;
	mb();

	/* send a reload command to the bootstrap MCP, and wait for the
	 *  response in the confirmation address.  The firmware should
	 * write a -1 there to indicate it is alive and well
	 */
	dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
	dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);

	buf[0] = htonl(dma_high);	/* confirm addr MSW */
	buf[1] = htonl(dma_low);	/* confirm addr LSW */
A
Al Viro 已提交
691
	buf[2] = MYRI10GE_NO_CONFIRM_DATA;	/* confirm data */
692 693 694 695 696 697 698 699 700 701

	/* FIX: All newest firmware should un-protect the bottom of
	 * the sram before handoff. However, the very first interfaces
	 * do not. Therefore the handoff copy must skip the first 8 bytes
	 */
	buf[3] = htonl(MYRI10GE_FW_OFFSET + 8);	/* where the code starts */
	buf[4] = htonl(size - 8);	/* length of code */
	buf[5] = htonl(8);	/* where to copy to */
	buf[6] = htonl(0);	/* where to jump to */

702
	submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
703 704 705 706 707 708

	myri10ge_pio_copy(submit, &buf, sizeof(buf));
	mb();
	msleep(1);
	mb();
	i = 0;
709 710
	while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
		msleep(1 << i);
711 712 713 714 715 716 717
		i++;
	}
	if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
		dev_err(&mgp->pdev->dev, "handoff failed\n");
		return -ENXIO;
	}
	dev_info(&mgp->pdev->dev, "handoff confirmed\n");
718
	myri10ge_dummy_rdma(mgp, 1);
719

B
Brice Goglin 已提交
720 721 722 723 724 725 726 727
	/* probe for IPv6 TSO support */
	mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
				   &cmd, 0);
	if (status == 0) {
		mgp->max_tso6 = cmd.data0;
		mgp->features |= NETIF_F_TSO6;
	}
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
	return 0;
}

static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
{
	struct myri10ge_cmd cmd;
	int status;

	cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
		     | (addr[2] << 8) | addr[3]);

	cmd.data1 = ((addr[4] << 8) | (addr[5]));

	status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
	return status;
}

static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
{
	struct myri10ge_cmd cmd;
	int status, ctl;

	ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
	status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);

	if (status) {
		printk(KERN_ERR
		       "myri10ge: %s: Failed to set flow control mode\n",
		       mgp->dev->name);
		return status;
	}
	mgp->pause = pause;
	return 0;
}

static void
myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
{
	struct myri10ge_cmd cmd;
	int status, ctl;

	ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
	status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
	if (status)
		printk(KERN_ERR "myri10ge: %s: Failed to set promisc mode\n",
		       mgp->dev->name);
}

776
static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
777 778 779 780
{
	struct myri10ge_cmd cmd;
	int status;
	u32 len;
781 782
	struct page *dmatest_page;
	dma_addr_t dmatest_bus;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	char *test = " ";

	dmatest_page = alloc_page(GFP_KERNEL);
	if (!dmatest_page)
		return -ENOMEM;
	dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
				   DMA_BIDIRECTIONAL);

	/* Run a small DMA test.
	 * The magic multipliers to the length tell the firmware
	 * to do DMA read, write, or read+write tests.  The
	 * results are returned in cmd.data0.  The upper 16
	 * bits or the return is the number of transfers completed.
	 * The lower 16 bits is the time in 0.5us ticks that the
	 * transfers took to complete.
	 */

800
	len = mgp->tx_boundary;
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

	cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
	cmd.data2 = len * 0x10000;
	status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
	if (status != 0) {
		test = "read";
		goto abort;
	}
	mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
	cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
	cmd.data2 = len * 0x1;
	status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
	if (status != 0) {
		test = "write";
		goto abort;
	}
	mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);

	cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
	cmd.data2 = len * 0x10001;
	status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
	if (status != 0) {
		test = "read/write";
		goto abort;
	}
	mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
	    (cmd.data0 & 0xffff);

abort:
	pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
	put_page(dmatest_page);

	if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
		dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
			 test, status);

	return status;
}

static int myri10ge_reset(struct myri10ge_priv *mgp)
{
	struct myri10ge_cmd cmd;
	int status;
	size_t bytes;
848 849 850 851 852 853 854 855 856

	/* try to send a reset command to the card to see if it
	 * is alive */
	memset(&cmd, 0, sizeof(cmd));
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
	if (status != 0) {
		dev_err(&mgp->pdev->dev, "failed reset\n");
		return -ENXIO;
	}
857 858

	(void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
859 860 861

	/* Now exchange information about interrupts  */

862 863
	bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
	memset(mgp->ss.rx_done.entry, 0, bytes);
864 865
	cmd.data0 = (u32) bytes;
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
866 867
	cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->ss.rx_done.bus);
	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->ss.rx_done.bus);
868 869 870 871
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA, &cmd, 0);

	status |=
	    myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
872
	mgp->ss.irq_claim = (__iomem __be32 *) (mgp->sram + cmd.data0);
873 874 875
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
				    &cmd, 0);
	mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
876 877 878

	status |= myri10ge_send_cmd
	    (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
A
Al Viro 已提交
879
	mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
880 881 882 883
	if (status != 0) {
		dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
		return status;
	}
A
Al Viro 已提交
884
	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
885

886
	memset(mgp->ss.rx_done.entry, 0, bytes);
887 888

	/* reset mcp/driver shared state back to 0 */
889 890 891 892 893 894 895 896
	mgp->ss.tx.req = 0;
	mgp->ss.tx.done = 0;
	mgp->ss.tx.pkt_start = 0;
	mgp->ss.tx.pkt_done = 0;
	mgp->ss.rx_big.cnt = 0;
	mgp->ss.rx_small.cnt = 0;
	mgp->ss.rx_done.idx = 0;
	mgp->ss.rx_done.cnt = 0;
897
	mgp->link_changes = 0;
898 899
	status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
	myri10ge_change_pause(mgp, mgp->pause);
900
	myri10ge_set_multicast_list(mgp->dev);
901 902 903 904 905 906 907
	return status;
}

static inline void
myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
		    struct mcp_kreq_ether_recv *src)
{
A
Al Viro 已提交
908
	__be32 low;
909 910

	low = src->addr_low;
A
Al Viro 已提交
911
	src->addr_low = htonl(DMA_32BIT_MASK);
912 913 914
	myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
	mb();
	myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
915 916
	mb();
	src->addr_low = low;
A
Al Viro 已提交
917
	put_be32(low, &dst->addr_low);
918 919 920
	mb();
}

A
Al Viro 已提交
921
static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
922 923 924
{
	struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);

A
Al Viro 已提交
925
	if ((skb->protocol == htons(ETH_P_8021Q)) &&
926 927 928
	    (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
	     vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
		skb->csum = hw_csum;
929
		skb->ip_summed = CHECKSUM_COMPLETE;
930 931 932
	}
}

933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
static inline void
myri10ge_rx_skb_build(struct sk_buff *skb, u8 * va,
		      struct skb_frag_struct *rx_frags, int len, int hlen)
{
	struct skb_frag_struct *skb_frags;

	skb->len = skb->data_len = len;
	skb->truesize = len + sizeof(struct sk_buff);
	/* attach the page(s) */

	skb_frags = skb_shinfo(skb)->frags;
	while (len > 0) {
		memcpy(skb_frags, rx_frags, sizeof(*skb_frags));
		len -= rx_frags->size;
		skb_frags++;
		rx_frags++;
		skb_shinfo(skb)->nr_frags++;
	}

	/* pskb_may_pull is not available in irq context, but
	 * skb_pull() (for ether_pad and eth_type_trans()) requires
	 * the beginning of the packet in skb_headlen(), move it
	 * manually */
956
	skb_copy_to_linear_data(skb, va, hlen);
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	skb_shinfo(skb)->frags[0].page_offset += hlen;
	skb_shinfo(skb)->frags[0].size -= hlen;
	skb->data_len -= hlen;
	skb->tail += hlen;
	skb_pull(skb, MXGEFW_PAD);
}

static void
myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
			int bytes, int watchdog)
{
	struct page *page;
	int idx;

	if (unlikely(rx->watchdog_needed && !watchdog))
		return;

	/* try to refill entire ring */
	while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
		idx = rx->fill_cnt & rx->mask;
977
		if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
			/* we can use part of previous page */
			get_page(rx->page);
		} else {
			/* we need a new page */
			page =
			    alloc_pages(GFP_ATOMIC | __GFP_COMP,
					MYRI10GE_ALLOC_ORDER);
			if (unlikely(page == NULL)) {
				if (rx->fill_cnt - rx->cnt < 16)
					rx->watchdog_needed = 1;
				return;
			}
			rx->page = page;
			rx->page_offset = 0;
			rx->bus = pci_map_page(mgp->pdev, page, 0,
					       MYRI10GE_ALLOC_SIZE,
					       PCI_DMA_FROMDEVICE);
		}
		rx->info[idx].page = rx->page;
		rx->info[idx].page_offset = rx->page_offset;
		/* note that this is the address of the start of the
		 * page */
		pci_unmap_addr_set(&rx->info[idx], bus, rx->bus);
		rx->shadow[idx].addr_low =
		    htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
		rx->shadow[idx].addr_high =
		    htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));

		/* start next packet on a cacheline boundary */
		rx->page_offset += SKB_DATA_ALIGN(bytes);
1008 1009 1010 1011 1012 1013 1014

#if MYRI10GE_ALLOC_SIZE > 4096
		/* don't cross a 4KB boundary */
		if ((rx->page_offset >> 12) !=
		    ((rx->page_offset + bytes - 1) >> 12))
			rx->page_offset = (rx->page_offset + 4096) & ~4095;
#endif
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
		rx->fill_cnt++;

		/* copy 8 descriptors to the firmware at a time */
		if ((idx & 7) == 7) {
			if (rx->wc_fifo == NULL)
				myri10ge_submit_8rx(&rx->lanai[idx - 7],
						    &rx->shadow[idx - 7]);
			else {
				mb();
				myri10ge_pio_copy(rx->wc_fifo,
						  &rx->shadow[idx - 7], 64);
			}
		}
	}
}

static inline void
myri10ge_unmap_rx_page(struct pci_dev *pdev,
		       struct myri10ge_rx_buffer_state *info, int bytes)
{
	/* unmap the recvd page if we're the only or last user of it */
	if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
	    (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
		pci_unmap_page(pdev, (pci_unmap_addr(info, bus)
				      & ~(MYRI10GE_ALLOC_SIZE - 1)),
			       MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
	}
}

#define MYRI10GE_HLEN 64	/* The number of bytes to copy from a
				 * page into an skb */

static inline int
1048
myri10ge_rx_done(struct myri10ge_slice_state *ss, struct myri10ge_rx_buf *rx,
1049
		 int bytes, int len, __wsum csum)
1050
{
1051
	struct myri10ge_priv *mgp = ss->mgp;
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	struct sk_buff *skb;
	struct skb_frag_struct rx_frags[MYRI10GE_MAX_FRAGS_PER_FRAME];
	int i, idx, hlen, remainder;
	struct pci_dev *pdev = mgp->pdev;
	struct net_device *dev = mgp->dev;
	u8 *va;

	len += MXGEFW_PAD;
	idx = rx->cnt & rx->mask;
	va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
	prefetch(va);
	/* Fill skb_frag_struct(s) with data from our receive */
	for (i = 0, remainder = len; remainder > 0; i++) {
		myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
		rx_frags[i].page = rx->info[idx].page;
		rx_frags[i].page_offset = rx->info[idx].page_offset;
		if (remainder < MYRI10GE_ALLOC_SIZE)
			rx_frags[i].size = remainder;
		else
			rx_frags[i].size = MYRI10GE_ALLOC_SIZE;
		rx->cnt++;
		idx = rx->cnt & rx->mask;
		remainder -= MYRI10GE_ALLOC_SIZE;
	}

A
Andrew Gallatin 已提交
1077 1078 1079 1080
	if (mgp->csum_flag && myri10ge_lro) {
		rx_frags[0].page_offset += MXGEFW_PAD;
		rx_frags[0].size -= MXGEFW_PAD;
		len -= MXGEFW_PAD;
1081
		lro_receive_frags(&ss->rx_done.lro_mgr, rx_frags,
A
Al Viro 已提交
1082
				  len, len,
1083 1084
				  /* opaque, will come back in get_frag_header */
				  (void *)(__force unsigned long)csum, csum);
A
Andrew Gallatin 已提交
1085 1086 1087
		return 1;
	}

1088 1089
	hlen = MYRI10GE_HLEN > len ? len : MYRI10GE_HLEN;

1090 1091
	/* allocate an skb to attach the page(s) to. This is done
	 * after trying LRO, so as to avoid skb allocation overheads */
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
	if (unlikely(skb == NULL)) {
		mgp->stats.rx_dropped++;
		do {
			i--;
			put_page(rx_frags[i].page);
		} while (i != 0);
		return 0;
	}

	/* Attach the pages to the skb, and trim off any padding */
	myri10ge_rx_skb_build(skb, va, rx_frags, len, hlen);
	if (skb_shinfo(skb)->frags[0].size <= 0) {
		put_page(skb_shinfo(skb)->frags[0].page);
		skb_shinfo(skb)->nr_frags = 0;
	}
	skb->protocol = eth_type_trans(skb, dev);

	if (mgp->csum_flag) {
		if ((skb->protocol == htons(ETH_P_IP)) ||
		    (skb->protocol == htons(ETH_P_IPV6))) {
			skb->csum = csum;
			skb->ip_summed = CHECKSUM_COMPLETE;
		} else
			myri10ge_vlan_ip_csum(skb, csum);
	}
	netif_receive_skb(skb);
	dev->last_rx = jiffies;
	return 1;
}

1124 1125
static inline void
myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
1126
{
1127 1128
	struct pci_dev *pdev = ss->mgp->pdev;
	struct myri10ge_tx_buf *tx = &ss->tx;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	struct sk_buff *skb;
	int idx, len;

	while (tx->pkt_done != mcp_index) {
		idx = tx->done & tx->mask;
		skb = tx->info[idx].skb;

		/* Mark as free */
		tx->info[idx].skb = NULL;
		if (tx->info[idx].last) {
			tx->pkt_done++;
			tx->info[idx].last = 0;
		}
		tx->done++;
		len = pci_unmap_len(&tx->info[idx], len);
		pci_unmap_len_set(&tx->info[idx], len, 0);
		if (skb) {
1146 1147
			ss->stats.tx_bytes += skb->len;
			ss->stats.tx_packets++;
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
			dev_kfree_skb_irq(skb);
			if (len)
				pci_unmap_single(pdev,
						 pci_unmap_addr(&tx->info[idx],
								bus), len,
						 PCI_DMA_TODEVICE);
		} else {
			if (len)
				pci_unmap_page(pdev,
					       pci_unmap_addr(&tx->info[idx],
							      bus), len,
					       PCI_DMA_TODEVICE);
		}
	}
	/* start the queue if we've stopped it */
1163
	if (netif_queue_stopped(ss->dev)
1164
	    && tx->req - tx->done < (tx->mask >> 1)) {
1165 1166
		tx->wake_queue++;
		netif_wake_queue(ss->dev);
1167 1168 1169
	}
}

1170 1171
static inline int
myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
1172
{
1173 1174
	struct myri10ge_rx_done *rx_done = &ss->rx_done;
	struct myri10ge_priv *mgp = ss->mgp;
1175 1176 1177 1178 1179 1180
	unsigned long rx_bytes = 0;
	unsigned long rx_packets = 0;
	unsigned long rx_ok;

	int idx = rx_done->idx;
	int cnt = rx_done->cnt;
1181
	int work_done = 0;
1182
	u16 length;
A
Al Viro 已提交
1183
	__wsum checksum;
1184

1185
	while (rx_done->entry[idx].length != 0 && work_done < budget) {
1186 1187
		length = ntohs(rx_done->entry[idx].length);
		rx_done->entry[idx].length = 0;
A
Al Viro 已提交
1188
		checksum = csum_unfold(rx_done->entry[idx].checksum);
1189
		if (length <= mgp->small_bytes)
1190
			rx_ok = myri10ge_rx_done(ss, &ss->rx_small,
1191 1192
						 mgp->small_bytes,
						 length, checksum);
1193
		else
1194
			rx_ok = myri10ge_rx_done(ss, &ss->rx_big,
1195 1196
						 mgp->big_bytes,
						 length, checksum);
1197 1198 1199 1200
		rx_packets += rx_ok;
		rx_bytes += rx_ok * (unsigned long)length;
		cnt++;
		idx = cnt & (myri10ge_max_intr_slots - 1);
1201
		work_done++;
1202 1203 1204
	}
	rx_done->idx = idx;
	rx_done->cnt = cnt;
1205 1206
	ss->stats.rx_packets += rx_packets;
	ss->stats.rx_bytes += rx_bytes;
1207

A
Andrew Gallatin 已提交
1208 1209 1210
	if (myri10ge_lro)
		lro_flush_all(&rx_done->lro_mgr);

1211
	/* restock receive rings if needed */
1212 1213
	if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
		myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
1214
					mgp->small_bytes + MXGEFW_PAD, 0);
1215 1216
	if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
		myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
1217

1218
	return work_done;
1219 1220 1221 1222
}

static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
{
1223
	struct mcp_irq_data *stats = mgp->ss.fw_stats;
1224 1225

	if (unlikely(stats->stats_updated)) {
1226 1227 1228 1229 1230
		unsigned link_up = ntohl(stats->link_up);
		if (mgp->link_state != link_up) {
			mgp->link_state = link_up;

			if (mgp->link_state == MXGEFW_LINK_UP) {
1231 1232 1233 1234
				if (netif_msg_link(mgp))
					printk(KERN_INFO
					       "myri10ge: %s: link up\n",
					       mgp->dev->name);
1235
				netif_carrier_on(mgp->dev);
1236
				mgp->link_changes++;
1237
			} else {
1238 1239
				if (netif_msg_link(mgp))
					printk(KERN_INFO
1240 1241 1242 1243 1244
					       "myri10ge: %s: link %s\n",
					       mgp->dev->name,
					       (link_up == MXGEFW_LINK_MYRINET ?
						"mismatch (Myrinet detected)" :
						"down"));
1245
				netif_carrier_off(mgp->dev);
1246
				mgp->link_changes++;
1247 1248 1249
			}
		}
		if (mgp->rdma_tags_available !=
1250
		    ntohl(stats->rdma_tags_available)) {
1251
			mgp->rdma_tags_available =
1252
			    ntohl(stats->rdma_tags_available);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
			printk(KERN_WARNING "myri10ge: %s: RDMA timed out! "
			       "%d tags left\n", mgp->dev->name,
			       mgp->rdma_tags_available);
		}
		mgp->down_cnt += stats->link_down;
		if (stats->link_down)
			wake_up(&mgp->down_wq);
	}
}

1263
static int myri10ge_poll(struct napi_struct *napi, int budget)
1264
{
1265 1266 1267
	struct myri10ge_slice_state *ss =
	    container_of(napi, struct myri10ge_slice_state, napi);
	struct net_device *netdev = ss->mgp->dev;
1268
	int work_done;
1269 1270

	/* process as many rx events as NAPI will allow */
1271
	work_done = myri10ge_clean_rx_done(ss, budget);
1272

1273
	if (work_done < budget) {
1274
		netif_rx_complete(netdev, napi);
1275
		put_be32(htonl(3), ss->irq_claim);
1276
	}
1277
	return work_done;
1278 1279
}

1280
static irqreturn_t myri10ge_intr(int irq, void *arg)
1281
{
1282 1283 1284 1285
	struct myri10ge_slice_state *ss = arg;
	struct myri10ge_priv *mgp = ss->mgp;
	struct mcp_irq_data *stats = ss->fw_stats;
	struct myri10ge_tx_buf *tx = &ss->tx;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	u32 send_done_count;
	int i;

	/* make sure it is our IRQ, and that the DMA has finished */
	if (unlikely(!stats->valid))
		return (IRQ_NONE);

	/* low bit indicates receives are present, so schedule
	 * napi poll handler */
	if (stats->valid & 1)
1296
		netif_rx_schedule(ss->dev, &ss->napi);
1297 1298

	if (!mgp->msi_enabled) {
A
Al Viro 已提交
1299
		put_be32(0, mgp->irq_deassert);
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
		if (!myri10ge_deassert_wait)
			stats->valid = 0;
		mb();
	} else
		stats->valid = 0;

	/* Wait for IRQ line to go low, if using INTx */
	i = 0;
	while (1) {
		i++;
		/* check for transmit completes and receives */
		send_done_count = ntohl(stats->send_done_count);
		if (send_done_count != tx->pkt_done)
1313
			myri10ge_tx_done(ss, (int)send_done_count);
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
		if (unlikely(i > myri10ge_max_irq_loops)) {
			printk(KERN_WARNING "myri10ge: %s: irq stuck?\n",
			       mgp->dev->name);
			stats->valid = 0;
			schedule_work(&mgp->watchdog_work);
		}
		if (likely(stats->valid == 0))
			break;
		cpu_relax();
		barrier();
	}

	myri10ge_check_statblock(mgp);

1328
	put_be32(htonl(3), ss->irq_claim + 1);
1329 1330 1331 1332 1333 1334
	return (IRQ_HANDLED);
}

static int
myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
{
1335 1336 1337 1338
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	char *ptr;
	int i;

1339 1340 1341
	cmd->autoneg = AUTONEG_DISABLE;
	cmd->speed = SPEED_10000;
	cmd->duplex = DUPLEX_FULL;
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

	/*
	 * parse the product code to deterimine the interface type
	 * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
	 * after the 3rd dash in the driver's cached copy of the
	 * EEPROM's product code string.
	 */
	ptr = mgp->product_code_string;
	if (ptr == NULL) {
		printk(KERN_ERR "myri10ge: %s: Missing product code\n",
B
Brice Goglin 已提交
1352
		       netdev->name);
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
		return 0;
	}
	for (i = 0; i < 3; i++, ptr++) {
		ptr = strchr(ptr, '-');
		if (ptr == NULL) {
			printk(KERN_ERR "myri10ge: %s: Invalid product "
			       "code %s\n", netdev->name,
			       mgp->product_code_string);
			return 0;
		}
	}
	if (*ptr == 'R' || *ptr == 'Q') {
		/* We've found either an XFP or quad ribbon fiber */
		cmd->port = PORT_FIBRE;
	}
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
	return 0;
}

static void
myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);

	strlcpy(info->driver, "myri10ge", sizeof(info->driver));
	strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
	strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
	strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
}

static int
myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
B
Brice Goglin 已提交
1386

1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
	coal->rx_coalesce_usecs = mgp->intr_coal_delay;
	return 0;
}

static int
myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);

	mgp->intr_coal_delay = coal->rx_coalesce_usecs;
A
Al Viro 已提交
1397
	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	return 0;
}

static void
myri10ge_get_pauseparam(struct net_device *netdev,
			struct ethtool_pauseparam *pause)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);

	pause->autoneg = 0;
	pause->rx_pause = mgp->pause;
	pause->tx_pause = mgp->pause;
}

static int
myri10ge_set_pauseparam(struct net_device *netdev,
			struct ethtool_pauseparam *pause)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);

	if (pause->tx_pause != mgp->pause)
		return myri10ge_change_pause(mgp, pause->tx_pause);
	if (pause->rx_pause != mgp->pause)
		return myri10ge_change_pause(mgp, pause->tx_pause);
	if (pause->autoneg != 0)
		return -EINVAL;
	return 0;
}

static void
myri10ge_get_ringparam(struct net_device *netdev,
		       struct ethtool_ringparam *ring)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);

1433 1434
	ring->rx_mini_max_pending = mgp->ss.rx_small.mask + 1;
	ring->rx_max_pending = mgp->ss.rx_big.mask + 1;
1435
	ring->rx_jumbo_max_pending = 0;
1436
	ring->tx_max_pending = mgp->ss.rx_small.mask + 1;
1437 1438 1439 1440 1441 1442 1443 1444 1445
	ring->rx_mini_pending = ring->rx_mini_max_pending;
	ring->rx_pending = ring->rx_max_pending;
	ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
	ring->tx_pending = ring->tx_max_pending;
}

static u32 myri10ge_get_rx_csum(struct net_device *netdev)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
B
Brice Goglin 已提交
1446

1447 1448 1449 1450 1451 1452 1453 1454 1455
	if (mgp->csum_flag)
		return 1;
	else
		return 0;
}

static int myri10ge_set_rx_csum(struct net_device *netdev, u32 csum_enabled)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
B
Brice Goglin 已提交
1456

1457 1458 1459 1460 1461 1462 1463
	if (csum_enabled)
		mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
	else
		mgp->csum_flag = 0;
	return 0;
}

B
Brice Goglin 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
static int myri10ge_set_tso(struct net_device *netdev, u32 tso_enabled)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	unsigned long flags = mgp->features & (NETIF_F_TSO6 | NETIF_F_TSO);

	if (tso_enabled)
		netdev->features |= flags;
	else
		netdev->features &= ~flags;
	return 0;
}

1476
static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
1477 1478 1479 1480 1481 1482 1483
	"rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
	"tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
	"rx_length_errors", "rx_over_errors", "rx_crc_errors",
	"rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
	"tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
	"tx_heartbeat_errors", "tx_window_errors",
	/* device-specific stats */
1484
	"tx_boundary", "WC", "irq", "MSI",
1485
	"read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
1486
	"serial_number", "watchdog_resets",
1487
	"link_changes", "link_up", "dropped_link_overflow",
1488 1489 1490
	"dropped_link_error_or_filtered",
	"dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
	"dropped_unicast_filtered", "dropped_multicast_filtered",
1491
	"dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
1492 1493 1494 1495 1496 1497 1498 1499 1500
	"dropped_no_big_buffer"
};

static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
	"----------- slice ---------",
	"tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
	"rx_small_cnt", "rx_big_cnt",
	"wake_queue", "stop_queue", "tx_linearized", "LRO aggregated",
	    "LRO flushed",
A
Andrew Gallatin 已提交
1501
	"LRO avg aggr", "LRO no_desc"
1502 1503 1504
};

#define MYRI10GE_NET_STATS_LEN      21
1505 1506
#define MYRI10GE_MAIN_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_main_stats)
#define MYRI10GE_SLICE_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_slice_stats)
1507 1508 1509 1510 1511 1512

static void
myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
{
	switch (stringset) {
	case ETH_SS_STATS:
1513 1514 1515 1516 1517 1518
		memcpy(data, *myri10ge_gstrings_main_stats,
		       sizeof(myri10ge_gstrings_main_stats));
		data += sizeof(myri10ge_gstrings_main_stats);
		memcpy(data, *myri10ge_gstrings_slice_stats,
		       sizeof(myri10ge_gstrings_slice_stats));
		data += sizeof(myri10ge_gstrings_slice_stats);
1519 1520 1521 1522
		break;
	}
}

1523
static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
1524
{
1525 1526
	switch (sset) {
	case ETH_SS_STATS:
1527
		return MYRI10GE_MAIN_STATS_LEN + MYRI10GE_SLICE_STATS_LEN;
1528 1529 1530
	default:
		return -EOPNOTSUPP;
	}
1531 1532 1533 1534 1535 1536 1537
}

static void
myri10ge_get_ethtool_stats(struct net_device *netdev,
			   struct ethtool_stats *stats, u64 * data)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
1538
	struct myri10ge_slice_state *ss;
1539 1540 1541 1542 1543
	int i;

	for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
		data[i] = ((unsigned long *)&mgp->stats)[i];

1544
	data[i++] = (unsigned int)mgp->tx_boundary;
1545
	data[i++] = (unsigned int)mgp->wc_enabled;
1546 1547
	data[i++] = (unsigned int)mgp->pdev->irq;
	data[i++] = (unsigned int)mgp->msi_enabled;
1548 1549 1550 1551 1552
	data[i++] = (unsigned int)mgp->read_dma;
	data[i++] = (unsigned int)mgp->write_dma;
	data[i++] = (unsigned int)mgp->read_write_dma;
	data[i++] = (unsigned int)mgp->serial_number;
	data[i++] = (unsigned int)mgp->watchdog_resets;
1553
	data[i++] = (unsigned int)mgp->link_changes;
1554 1555 1556 1557 1558

	/* firmware stats are useful only in the first slice */
	ss = &mgp->ss;
	data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
1559
	data[i++] =
1560 1561 1562 1563 1564
	    (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
1565
	data[i++] =
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
	    (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);

	data[i++] = 0;
	data[i++] = (unsigned int)ss->tx.pkt_start;
	data[i++] = (unsigned int)ss->tx.pkt_done;
	data[i++] = (unsigned int)ss->tx.req;
	data[i++] = (unsigned int)ss->tx.done;
	data[i++] = (unsigned int)ss->rx_small.cnt;
	data[i++] = (unsigned int)ss->rx_big.cnt;
	data[i++] = (unsigned int)ss->tx.wake_queue;
	data[i++] = (unsigned int)ss->tx.stop_queue;
	data[i++] = (unsigned int)ss->tx.linearized;
	data[i++] = ss->rx_done.lro_mgr.stats.aggregated;
	data[i++] = ss->rx_done.lro_mgr.stats.flushed;
	if (ss->rx_done.lro_mgr.stats.flushed)
		data[i++] = ss->rx_done.lro_mgr.stats.aggregated /
		    ss->rx_done.lro_mgr.stats.flushed;
A
Andrew Gallatin 已提交
1587 1588
	else
		data[i++] = 0;
1589
	data[i++] = ss->rx_done.lro_mgr.stats.no_desc;
1590 1591
}

1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	mgp->msg_enable = value;
}

static u32 myri10ge_get_msglevel(struct net_device *netdev)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	return mgp->msg_enable;
}

1604
static const struct ethtool_ops myri10ge_ethtool_ops = {
1605 1606 1607 1608 1609 1610 1611 1612 1613
	.get_settings = myri10ge_get_settings,
	.get_drvinfo = myri10ge_get_drvinfo,
	.get_coalesce = myri10ge_get_coalesce,
	.set_coalesce = myri10ge_set_coalesce,
	.get_pauseparam = myri10ge_get_pauseparam,
	.set_pauseparam = myri10ge_set_pauseparam,
	.get_ringparam = myri10ge_get_ringparam,
	.get_rx_csum = myri10ge_get_rx_csum,
	.set_rx_csum = myri10ge_set_rx_csum,
B
Brice Goglin 已提交
1614
	.set_tx_csum = ethtool_op_set_tx_hw_csum,
1615
	.set_sg = ethtool_op_set_sg,
B
Brice Goglin 已提交
1616
	.set_tso = myri10ge_set_tso,
1617
	.get_link = ethtool_op_get_link,
1618
	.get_strings = myri10ge_get_strings,
1619
	.get_sset_count = myri10ge_get_sset_count,
1620 1621 1622
	.get_ethtool_stats = myri10ge_get_ethtool_stats,
	.set_msglevel = myri10ge_set_msglevel,
	.get_msglevel = myri10ge_get_msglevel
1623 1624
};

1625
static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
1626
{
1627
	struct myri10ge_priv *mgp = ss->mgp;
1628
	struct myri10ge_cmd cmd;
1629
	struct net_device *dev = mgp->dev;
1630 1631 1632 1633 1634 1635 1636 1637 1638
	int tx_ring_size, rx_ring_size;
	int tx_ring_entries, rx_ring_entries;
	int i, status;
	size_t bytes;

	/* get ring sizes */
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
	tx_ring_size = cmd.data0;
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
1639 1640
	if (status != 0)
		return status;
1641 1642 1643 1644
	rx_ring_size = cmd.data0;

	tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
	rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
1645 1646
	ss->tx.mask = tx_ring_entries - 1;
	ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
1647

1648 1649
	status = -ENOMEM;

1650 1651 1652
	/* allocate the host shadow rings */

	bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
1653 1654 1655
	    * sizeof(*ss->tx.req_list);
	ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
	if (ss->tx.req_bytes == NULL)
1656 1657 1658
		goto abort_with_nothing;

	/* ensure req_list entries are aligned to 8 bytes */
1659 1660
	ss->tx.req_list = (struct mcp_kreq_ether_send *)
	    ALIGN((unsigned long)ss->tx.req_bytes, 8);
1661

1662 1663 1664
	bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
	ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_small.shadow == NULL)
1665 1666
		goto abort_with_tx_req_bytes;

1667 1668 1669
	bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
	ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_big.shadow == NULL)
1670 1671 1672 1673
		goto abort_with_rx_small_shadow;

	/* allocate the host info rings */

1674 1675 1676
	bytes = tx_ring_entries * sizeof(*ss->tx.info);
	ss->tx.info = kzalloc(bytes, GFP_KERNEL);
	if (ss->tx.info == NULL)
1677 1678
		goto abort_with_rx_big_shadow;

1679 1680 1681
	bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
	ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_small.info == NULL)
1682 1683
		goto abort_with_tx_info;

1684 1685 1686
	bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
	ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_big.info == NULL)
1687 1688 1689
		goto abort_with_rx_small_info;

	/* Fill the receive rings */
1690 1691 1692 1693 1694 1695 1696 1697 1698
	ss->rx_big.cnt = 0;
	ss->rx_small.cnt = 0;
	ss->rx_big.fill_cnt = 0;
	ss->rx_small.fill_cnt = 0;
	ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
	ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
	ss->rx_small.watchdog_needed = 0;
	ss->rx_big.watchdog_needed = 0;
	myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
1699
				mgp->small_bytes + MXGEFW_PAD, 0);
1700

1701
	if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
1702
		printk(KERN_ERR "myri10ge: %s: alloced only %d small bufs\n",
1703
		       dev->name, ss->rx_small.fill_cnt);
1704
		goto abort_with_rx_small_ring;
1705 1706
	}

1707 1708
	myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
	if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
1709
		printk(KERN_ERR "myri10ge: %s: alloced only %d big bufs\n",
1710
		       dev->name, ss->rx_big.fill_cnt);
1711
		goto abort_with_rx_big_ring;
1712 1713 1714 1715 1716
	}

	return 0;

abort_with_rx_big_ring:
1717 1718 1719
	for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
		int idx = i & ss->rx_big.mask;
		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
1720
				       mgp->big_bytes);
1721
		put_page(ss->rx_big.info[idx].page);
1722 1723 1724
	}

abort_with_rx_small_ring:
1725 1726 1727
	for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
		int idx = i & ss->rx_small.mask;
		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
1728
				       mgp->small_bytes + MXGEFW_PAD);
1729
		put_page(ss->rx_small.info[idx].page);
1730
	}
1731

1732
	kfree(ss->rx_big.info);
1733 1734

abort_with_rx_small_info:
1735
	kfree(ss->rx_small.info);
1736 1737

abort_with_tx_info:
1738
	kfree(ss->tx.info);
1739 1740

abort_with_rx_big_shadow:
1741
	kfree(ss->rx_big.shadow);
1742 1743

abort_with_rx_small_shadow:
1744
	kfree(ss->rx_small.shadow);
1745 1746

abort_with_tx_req_bytes:
1747 1748 1749
	kfree(ss->tx.req_bytes);
	ss->tx.req_bytes = NULL;
	ss->tx.req_list = NULL;
1750 1751 1752 1753 1754

abort_with_nothing:
	return status;
}

1755
static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
1756
{
1757
	struct myri10ge_priv *mgp = ss->mgp;
1758 1759 1760 1761
	struct sk_buff *skb;
	struct myri10ge_tx_buf *tx;
	int i, len, idx;

1762 1763 1764 1765 1766
	for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
		idx = i & ss->rx_big.mask;
		if (i == ss->rx_big.fill_cnt - 1)
			ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
1767
				       mgp->big_bytes);
1768
		put_page(ss->rx_big.info[idx].page);
1769 1770
	}

1771 1772 1773 1774
	for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
		idx = i & ss->rx_small.mask;
		if (i == ss->rx_small.fill_cnt - 1)
			ss->rx_small.info[idx].page_offset =
1775
			    MYRI10GE_ALLOC_SIZE;
1776
		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
1777
				       mgp->small_bytes + MXGEFW_PAD);
1778
		put_page(ss->rx_small.info[idx].page);
1779
	}
1780
	tx = &ss->tx;
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
	while (tx->done != tx->req) {
		idx = tx->done & tx->mask;
		skb = tx->info[idx].skb;

		/* Mark as free */
		tx->info[idx].skb = NULL;
		tx->done++;
		len = pci_unmap_len(&tx->info[idx], len);
		pci_unmap_len_set(&tx->info[idx], len, 0);
		if (skb) {
1791
			ss->stats.tx_dropped++;
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
			dev_kfree_skb_any(skb);
			if (len)
				pci_unmap_single(mgp->pdev,
						 pci_unmap_addr(&tx->info[idx],
								bus), len,
						 PCI_DMA_TODEVICE);
		} else {
			if (len)
				pci_unmap_page(mgp->pdev,
					       pci_unmap_addr(&tx->info[idx],
							      bus), len,
					       PCI_DMA_TODEVICE);
		}
	}
1806
	kfree(ss->rx_big.info);
1807

1808
	kfree(ss->rx_small.info);
1809

1810
	kfree(ss->tx.info);
1811

1812
	kfree(ss->rx_big.shadow);
1813

1814
	kfree(ss->rx_small.shadow);
1815

1816 1817 1818
	kfree(ss->tx.req_bytes);
	ss->tx.req_bytes = NULL;
	ss->tx.req_list = NULL;
1819 1820
}

1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
static int myri10ge_request_irq(struct myri10ge_priv *mgp)
{
	struct pci_dev *pdev = mgp->pdev;
	int status;

	if (myri10ge_msi) {
		status = pci_enable_msi(pdev);
		if (status != 0)
			dev_err(&pdev->dev,
				"Error %d setting up MSI; falling back to xPIC\n",
				status);
		else
			mgp->msi_enabled = 1;
	} else {
		mgp->msi_enabled = 0;
	}
	status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
			     mgp->dev->name, mgp);
	if (status != 0) {
		dev_err(&pdev->dev, "failed to allocate IRQ\n");
		if (mgp->msi_enabled)
			pci_disable_msi(pdev);
	}
	return status;
}

static void myri10ge_free_irq(struct myri10ge_priv *mgp)
{
	struct pci_dev *pdev = mgp->pdev;

	free_irq(pdev->irq, mgp);
	if (mgp->msi_enabled)
		pci_disable_msi(pdev);
}

A
Andrew Gallatin 已提交
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
static int
myri10ge_get_frag_header(struct skb_frag_struct *frag, void **mac_hdr,
			 void **ip_hdr, void **tcpudp_hdr,
			 u64 * hdr_flags, void *priv)
{
	struct ethhdr *eh;
	struct vlan_ethhdr *veh;
	struct iphdr *iph;
	u8 *va = page_address(frag->page) + frag->page_offset;
	unsigned long ll_hlen;
A
Al Viro 已提交
1866 1867
	/* passed opaque through lro_receive_frags() */
	__wsum csum = (__force __wsum) (unsigned long)priv;
A
Andrew Gallatin 已提交
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914

	/* find the mac header, aborting if not IPv4 */

	eh = (struct ethhdr *)va;
	*mac_hdr = eh;
	ll_hlen = ETH_HLEN;
	if (eh->h_proto != htons(ETH_P_IP)) {
		if (eh->h_proto == htons(ETH_P_8021Q)) {
			veh = (struct vlan_ethhdr *)va;
			if (veh->h_vlan_encapsulated_proto != htons(ETH_P_IP))
				return -1;

			ll_hlen += VLAN_HLEN;

			/*
			 *  HW checksum starts ETH_HLEN bytes into
			 *  frame, so we must subtract off the VLAN
			 *  header's checksum before csum can be used
			 */
			csum = csum_sub(csum, csum_partial(va + ETH_HLEN,
							   VLAN_HLEN, 0));
		} else {
			return -1;
		}
	}
	*hdr_flags = LRO_IPV4;

	iph = (struct iphdr *)(va + ll_hlen);
	*ip_hdr = iph;
	if (iph->protocol != IPPROTO_TCP)
		return -1;
	*hdr_flags |= LRO_TCP;
	*tcpudp_hdr = (u8 *) (*ip_hdr) + (iph->ihl << 2);

	/* verify the IP checksum */
	if (unlikely(ip_fast_csum((u8 *) iph, iph->ihl)))
		return -1;

	/* verify the  checksum */
	if (unlikely(csum_tcpudp_magic(iph->saddr, iph->daddr,
				       ntohs(iph->tot_len) - (iph->ihl << 2),
				       IPPROTO_TCP, csum)))
		return -1;

	return 0;
}

1915 1916
static int myri10ge_open(struct net_device *dev)
{
1917
	struct myri10ge_priv *mgp = netdev_priv(dev);
1918
	struct myri10ge_cmd cmd;
A
Andrew Gallatin 已提交
1919
	struct net_lro_mgr *lro_mgr;
1920 1921 1922 1923 1924 1925 1926 1927 1928
	int status, big_pow2;

	if (mgp->running != MYRI10GE_ETH_STOPPED)
		return -EBUSY;

	mgp->running = MYRI10GE_ETH_STARTING;
	status = myri10ge_reset(mgp);
	if (status != 0) {
		printk(KERN_ERR "myri10ge: %s: failed reset\n", dev->name);
1929
		goto abort_with_nothing;
1930 1931
	}

1932 1933 1934 1935
	status = myri10ge_request_irq(mgp);
	if (status != 0)
		goto abort_with_nothing;

1936 1937 1938 1939 1940 1941 1942
	/* decide what small buffer size to use.  For good TCP rx
	 * performance, it is important to not receive 1514 byte
	 * frames into jumbo buffers, as it confuses the socket buffer
	 * accounting code, leading to drops and erratic performance.
	 */

	if (dev->mtu <= ETH_DATA_LEN)
1943 1944 1945 1946
		/* enough for a TCP header */
		mgp->small_bytes = (128 > SMP_CACHE_BYTES)
		    ? (128 - MXGEFW_PAD)
		    : (SMP_CACHE_BYTES - MXGEFW_PAD);
1947
	else
1948 1949
		/* enough for a vlan encapsulated ETH_DATA_LEN frame */
		mgp->small_bytes = VLAN_ETH_FRAME_LEN;
1950 1951 1952 1953 1954 1955 1956 1957

	/* Override the small buffer size? */
	if (myri10ge_small_bytes > 0)
		mgp->small_bytes = myri10ge_small_bytes;

	/* get the lanai pointers to the send and receive rings */

	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET, &cmd, 0);
1958
	mgp->ss.tx.lanai =
1959 1960 1961 1962
	    (struct mcp_kreq_ether_send __iomem *)(mgp->sram + cmd.data0);

	status |=
	    myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET, &cmd, 0);
1963
	mgp->ss.rx_small.lanai =
1964 1965 1966
	    (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);

	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
1967
	mgp->ss.rx_big.lanai =
1968 1969 1970 1971 1972 1973 1974
	    (struct mcp_kreq_ether_recv __iomem *)(mgp->sram + cmd.data0);

	if (status != 0) {
		printk(KERN_ERR
		       "myri10ge: %s: failed to get ring sizes or locations\n",
		       dev->name);
		mgp->running = MYRI10GE_ETH_STOPPED;
1975
		goto abort_with_irq;
1976 1977
	}

1978
	if (myri10ge_wcfifo && mgp->wc_enabled) {
1979 1980
		mgp->ss.tx.wc_fifo = (u8 __iomem *) mgp->sram + MXGEFW_ETH_SEND_4;
		mgp->ss.rx_small.wc_fifo =
1981
		    (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_SMALL;
1982
		mgp->ss.rx_big.wc_fifo =
1983
		    (u8 __iomem *) mgp->sram + MXGEFW_ETH_RECV_BIG;
1984
	} else {
1985 1986 1987
		mgp->ss.tx.wc_fifo = NULL;
		mgp->ss.rx_small.wc_fifo = NULL;
		mgp->ss.rx_big.wc_fifo = NULL;
1988 1989 1990 1991 1992 1993
	}

	/* Firmware needs the big buff size as a power of 2.  Lie and
	 * tell him the buffer is larger, because we only use 1
	 * buffer/pkt, and the mtu will prevent overruns.
	 */
1994
	big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
1995
	if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
1996
		while (!is_power_of_2(big_pow2))
1997
			big_pow2++;
1998
		mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
1999 2000 2001 2002 2003
	} else {
		big_pow2 = MYRI10GE_ALLOC_SIZE;
		mgp->big_bytes = big_pow2;
	}

2004
	status = myri10ge_allocate_rings(&mgp->ss);
2005
	if (status != 0)
2006
		goto abort_with_irq;
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022

	/* now give firmware buffers sizes, and MTU */
	cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
	cmd.data0 = mgp->small_bytes;
	status |=
	    myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
	cmd.data0 = big_pow2;
	status |=
	    myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
	if (status) {
		printk(KERN_ERR "myri10ge: %s: Couldn't set buffer sizes\n",
		       dev->name);
		goto abort_with_rings;
	}

2023 2024
	cmd.data0 = MYRI10GE_LOWPART_TO_U32(mgp->ss.fw_stats_bus);
	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(mgp->ss.fw_stats_bus);
2025 2026 2027
	cmd.data2 = sizeof(struct mcp_irq_data);
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
	if (status == -ENOSYS) {
2028
		dma_addr_t bus = mgp->ss.fw_stats_bus;
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
		bus += offsetof(struct mcp_irq_data, send_done_count);
		cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
		cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
		status = myri10ge_send_cmd(mgp,
					   MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
					   &cmd, 0);
		/* Firmware cannot support multicast without STATS_DMA_V2 */
		mgp->fw_multicast_support = 0;
	} else {
		mgp->fw_multicast_support = 1;
	}
2040 2041 2042 2043 2044 2045
	if (status) {
		printk(KERN_ERR "myri10ge: %s: Couldn't set stats DMA\n",
		       dev->name);
		goto abort_with_rings;
	}

A
Al Viro 已提交
2046
	mgp->link_state = ~0U;
2047 2048
	mgp->rdma_tags_available = 15;

2049
	lro_mgr = &mgp->ss.rx_done.lro_mgr;
A
Andrew Gallatin 已提交
2050 2051 2052 2053 2054
	lro_mgr->dev = dev;
	lro_mgr->features = LRO_F_NAPI;
	lro_mgr->ip_summed = CHECKSUM_COMPLETE;
	lro_mgr->ip_summed_aggr = CHECKSUM_UNNECESSARY;
	lro_mgr->max_desc = MYRI10GE_MAX_LRO_DESCRIPTORS;
2055
	lro_mgr->lro_arr = mgp->ss.rx_done.lro_desc;
A
Andrew Gallatin 已提交
2056 2057
	lro_mgr->get_frag_header = myri10ge_get_frag_header;
	lro_mgr->max_aggr = myri10ge_lro_max_pkts;
2058
	lro_mgr->frag_align_pad = 2;
A
Andrew Gallatin 已提交
2059 2060 2061
	if (lro_mgr->max_aggr > MAX_SKB_FRAGS)
		lro_mgr->max_aggr = MAX_SKB_FRAGS;

2062
	napi_enable(&mgp->ss.napi);	/* must happen prior to any irq */
2063 2064 2065 2066 2067 2068 2069 2070

	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
	if (status) {
		printk(KERN_ERR "myri10ge: %s: Couldn't bring up link\n",
		       dev->name);
		goto abort_with_rings;
	}

2071 2072
	mgp->ss.tx.wake_queue = 0;
	mgp->ss.tx.stop_queue = 0;
2073 2074 2075 2076 2077 2078 2079
	mgp->running = MYRI10GE_ETH_RUNNING;
	mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
	add_timer(&mgp->watchdog_timer);
	netif_wake_queue(dev);
	return 0;

abort_with_rings:
2080
	myri10ge_free_rings(&mgp->ss);
2081

2082 2083 2084
abort_with_irq:
	myri10ge_free_irq(mgp);

2085 2086 2087 2088 2089 2090 2091
abort_with_nothing:
	mgp->running = MYRI10GE_ETH_STOPPED;
	return -ENOMEM;
}

static int myri10ge_close(struct net_device *dev)
{
2092
	struct myri10ge_priv *mgp = netdev_priv(dev);
2093 2094 2095 2096 2097 2098
	struct myri10ge_cmd cmd;
	int status, old_down_cnt;

	if (mgp->running != MYRI10GE_ETH_RUNNING)
		return 0;

2099
	if (mgp->ss.tx.req_bytes == NULL)
2100 2101 2102 2103
		return 0;

	del_timer_sync(&mgp->watchdog_timer);
	mgp->running = MYRI10GE_ETH_STOPPING;
2104
	napi_disable(&mgp->ss.napi);
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118
	netif_carrier_off(dev);
	netif_stop_queue(dev);
	old_down_cnt = mgp->down_cnt;
	mb();
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
	if (status)
		printk(KERN_ERR "myri10ge: %s: Couldn't bring down link\n",
		       dev->name);

	wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt, HZ);
	if (old_down_cnt == mgp->down_cnt)
		printk(KERN_ERR "myri10ge: %s never got down irq\n", dev->name);

	netif_tx_disable(dev);
2119
	myri10ge_free_irq(mgp);
2120
	myri10ge_free_rings(&mgp->ss);
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187

	mgp->running = MYRI10GE_ETH_STOPPED;
	return 0;
}

/* copy an array of struct mcp_kreq_ether_send's to the mcp.  Copy
 * backwards one at a time and handle ring wraps */

static inline void
myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
			      struct mcp_kreq_ether_send *src, int cnt)
{
	int idx, starting_slot;
	starting_slot = tx->req;
	while (cnt > 1) {
		cnt--;
		idx = (starting_slot + cnt) & tx->mask;
		myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
		mb();
	}
}

/*
 * copy an array of struct mcp_kreq_ether_send's to the mcp.  Copy
 * at most 32 bytes at a time, so as to avoid involving the software
 * pio handler in the nic.   We re-write the first segment's flags
 * to mark them valid only after writing the entire chain.
 */

static inline void
myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
		    int cnt)
{
	int idx, i;
	struct mcp_kreq_ether_send __iomem *dstp, *dst;
	struct mcp_kreq_ether_send *srcp;
	u8 last_flags;

	idx = tx->req & tx->mask;

	last_flags = src->flags;
	src->flags = 0;
	mb();
	dst = dstp = &tx->lanai[idx];
	srcp = src;

	if ((idx + cnt) < tx->mask) {
		for (i = 0; i < (cnt - 1); i += 2) {
			myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
			mb();	/* force write every 32 bytes */
			srcp += 2;
			dstp += 2;
		}
	} else {
		/* submit all but the first request, and ensure
		 * that it is submitted below */
		myri10ge_submit_req_backwards(tx, src, cnt);
		i = 0;
	}
	if (i < cnt) {
		/* submit the first request */
		myri10ge_pio_copy(dstp, srcp, sizeof(*src));
		mb();		/* barrier before setting valid flag */
	}

	/* re-write the last 32-bits with the valid flags */
	src->flags = last_flags;
A
Al Viro 已提交
2188
	put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
	tx->req += cnt;
	mb();
}

static inline void
myri10ge_submit_req_wc(struct myri10ge_tx_buf *tx,
		       struct mcp_kreq_ether_send *src, int cnt)
{
	tx->req += cnt;
	mb();
	while (cnt >= 4) {
		myri10ge_pio_copy(tx->wc_fifo, src, 64);
		mb();
		src += 4;
		cnt -= 4;
	}
	if (cnt > 0) {
		/* pad it to 64 bytes.  The src is 64 bytes bigger than it
		 * needs to be so that we don't overrun it */
2208 2209
		myri10ge_pio_copy(tx->wc_fifo + MXGEFW_ETH_SEND_OFFSET(cnt),
				  src, 64);
2210 2211 2212 2213 2214 2215
		mb();
	}
}

/*
 * Transmit a packet.  We need to split the packet so that a single
2216
 * segment does not cross myri10ge->tx_boundary, so this makes segment
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
 * counting tricky.  So rather than try to count segments up front, we
 * just give up if there are too few segments to hold a reasonably
 * fragmented packet currently available.  If we run
 * out of segments while preparing a packet for DMA, we just linearize
 * it and try again.
 */

static int myri10ge_xmit(struct sk_buff *skb, struct net_device *dev)
{
	struct myri10ge_priv *mgp = netdev_priv(dev);
2227
	struct myri10ge_slice_state *ss;
2228
	struct mcp_kreq_ether_send *req;
2229
	struct myri10ge_tx_buf *tx;
2230 2231
	struct skb_frag_struct *frag;
	dma_addr_t bus;
A
Al Viro 已提交
2232 2233
	u32 low;
	__be32 high_swapped;
2234 2235 2236 2237 2238 2239
	unsigned int len;
	int idx, last_idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
	u16 pseudo_hdr_offset, cksum_offset;
	int cum_len, seglen, boundary, rdma_count;
	u8 flags, odd_flag;

2240 2241 2242
	/* always transmit through slot 0 */
	ss = &mgp->ss;
	tx = &ss->tx;
2243 2244 2245 2246 2247 2248 2249
again:
	req = tx->req_list;
	avail = tx->mask - 1 - (tx->req - tx->done);

	mss = 0;
	max_segments = MXGEFW_MAX_SEND_DESC;

2250
	if (skb_is_gso(skb)) {
2251
		mss = skb_shinfo(skb)->gso_size;
2252
		max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
2253 2254 2255 2256
	}

	if ((unlikely(avail < max_segments))) {
		/* we are out of transmit resources */
2257
		tx->stop_queue++;
2258 2259 2260 2261 2262 2263 2264 2265 2266
		netif_stop_queue(dev);
		return 1;
	}

	/* Setup checksum offloading, if needed */
	cksum_offset = 0;
	pseudo_hdr_offset = 0;
	odd_flag = 0;
	flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
2267
	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2268
		cksum_offset = skb_transport_offset(skb);
A
Al Viro 已提交
2269
		pseudo_hdr_offset = cksum_offset + skb->csum_offset;
2270 2271
		/* If the headers are excessively large, then we must
		 * fall back to a software checksum */
B
Brice Goglin 已提交
2272 2273
		if (unlikely(!mss && (cksum_offset > 255 ||
				      pseudo_hdr_offset > 127))) {
2274
			if (skb_checksum_help(skb))
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
				goto drop;
			cksum_offset = 0;
			pseudo_hdr_offset = 0;
		} else {
			odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
			flags |= MXGEFW_FLAGS_CKSUM;
		}
	}

	cum_len = 0;

	if (mss) {		/* TSO */
		/* this removes any CKSUM flag from before */
		flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);

		/* negative cum_len signifies to the
		 * send loop that we are still in the
		 * header portion of the TSO packet.
B
Brice Goglin 已提交
2293
		 * TSO header can be at most 1KB long */
2294
		cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
2295

B
Brice Goglin 已提交
2296 2297 2298 2299 2300 2301 2302 2303 2304
		/* for IPv6 TSO, the checksum offset stores the
		 * TCP header length, to save the firmware from
		 * the need to parse the headers */
		if (skb_is_gso_v6(skb)) {
			cksum_offset = tcp_hdrlen(skb);
			/* Can only handle headers <= max_tso6 long */
			if (unlikely(-cum_len > mgp->max_tso6))
				return myri10ge_sw_tso(skb, dev);
		}
2305 2306 2307
		/* for TSO, pseudo_hdr_offset holds mss.
		 * The firmware figures out where to put
		 * the checksum by parsing the header. */
A
Al Viro 已提交
2308
		pseudo_hdr_offset = mss;
2309 2310 2311 2312 2313 2314 2315
	} else
		/* Mark small packets, and pad out tiny packets */
	if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
		flags |= MXGEFW_FLAGS_SMALL;

		/* pad frames to at least ETH_ZLEN bytes */
		if (unlikely(skb->len < ETH_ZLEN)) {
2316
			if (skb_padto(skb, ETH_ZLEN)) {
2317 2318
				/* The packet is gone, so we must
				 * return 0 */
2319
				ss->stats.tx_dropped += 1;
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
				return 0;
			}
			/* adjust the len to account for the zero pad
			 * so that the nic can know how long it is */
			skb->len = ETH_ZLEN;
		}
	}

	/* map the skb for DMA */
	len = skb->len - skb->data_len;
	idx = tx->req & tx->mask;
	tx->info[idx].skb = skb;
	bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
	pci_unmap_addr_set(&tx->info[idx], bus, bus);
	pci_unmap_len_set(&tx->info[idx], len, len);

	frag_cnt = skb_shinfo(skb)->nr_frags;
	frag_idx = 0;
	count = 0;
	rdma_count = 0;

	/* "rdma_count" is the number of RDMAs belonging to the
	 * current packet BEFORE the current send request. For
	 * non-TSO packets, this is equal to "count".
	 * For TSO packets, rdma_count needs to be reset
	 * to 0 after a segment cut.
	 *
	 * The rdma_count field of the send request is
	 * the number of RDMAs of the packet starting at
	 * that request. For TSO send requests with one ore more cuts
	 * in the middle, this is the number of RDMAs starting
	 * after the last cut in the request. All previous
	 * segments before the last cut implicitly have 1 RDMA.
	 *
	 * Since the number of RDMAs is not known beforehand,
	 * it must be filled-in retroactively - after each
	 * segmentation cut or at the end of the entire packet.
	 */

	while (1) {
		/* Break the SKB or Fragment up into pieces which
2361
		 * do not cross mgp->tx_boundary */
2362 2363 2364 2365 2366 2367 2368 2369 2370
		low = MYRI10GE_LOWPART_TO_U32(bus);
		high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
		while (len) {
			u8 flags_next;
			int cum_len_next;

			if (unlikely(count == max_segments))
				goto abort_linearize;

2371 2372
			boundary =
			    (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
			seglen = boundary - low;
			if (seglen > len)
				seglen = len;
			flags_next = flags & ~MXGEFW_FLAGS_FIRST;
			cum_len_next = cum_len + seglen;
			if (mss) {	/* TSO */
				(req - rdma_count)->rdma_count = rdma_count + 1;

				if (likely(cum_len >= 0)) {	/* payload */
					int next_is_first, chop;

					chop = (cum_len_next > mss);
					cum_len_next = cum_len_next % mss;
					next_is_first = (cum_len_next == 0);
					flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
					flags_next |= next_is_first *
					    MXGEFW_FLAGS_FIRST;
					rdma_count |= -(chop | next_is_first);
					rdma_count += chop & !next_is_first;
				} else if (likely(cum_len_next >= 0)) {	/* header ends */
					int small;

					rdma_count = -1;
					cum_len_next = 0;
					seglen = -cum_len;
					small = (mss <= MXGEFW_SEND_SMALL_SIZE);
					flags_next = MXGEFW_FLAGS_TSO_PLD |
					    MXGEFW_FLAGS_FIRST |
					    (small * MXGEFW_FLAGS_SMALL);
				}
			}
			req->addr_high = high_swapped;
			req->addr_low = htonl(low);
A
Al Viro 已提交
2406
			req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
			req->pad = 0;	/* complete solid 16-byte block; does this matter? */
			req->rdma_count = 1;
			req->length = htons(seglen);
			req->cksum_offset = cksum_offset;
			req->flags = flags | ((cum_len & 1) * odd_flag);

			low += seglen;
			len -= seglen;
			cum_len = cum_len_next;
			flags = flags_next;
			req++;
			count++;
			rdma_count++;
B
Brice Goglin 已提交
2420 2421 2422 2423 2424 2425
			if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
				if (unlikely(cksum_offset > seglen))
					cksum_offset -= seglen;
				else
					cksum_offset = 0;
			}
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
		}
		if (frag_idx == frag_cnt)
			break;

		/* map next fragment for DMA */
		idx = (count + tx->req) & tx->mask;
		frag = &skb_shinfo(skb)->frags[frag_idx];
		frag_idx++;
		len = frag->size;
		bus = pci_map_page(mgp->pdev, frag->page, frag->page_offset,
				   len, PCI_DMA_TODEVICE);
		pci_unmap_addr_set(&tx->info[idx], bus, bus);
		pci_unmap_len_set(&tx->info[idx], len, len);
	}

	(req - rdma_count)->rdma_count = rdma_count;
	if (mss)
		do {
			req--;
			req->flags |= MXGEFW_FLAGS_TSO_LAST;
		} while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
					 MXGEFW_FLAGS_FIRST)));
	idx = ((count - 1) + tx->req) & tx->mask;
	tx->info[idx].last = 1;
	if (tx->wc_fifo == NULL)
		myri10ge_submit_req(tx, tx->req_list, count);
	else
		myri10ge_submit_req_wc(tx, tx->req_list, count);
	tx->pkt_start++;
	if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
2456
		tx->stop_queue++;
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
		netif_stop_queue(dev);
	}
	dev->trans_start = jiffies;
	return 0;

abort_linearize:
	/* Free any DMA resources we've alloced and clear out the skb
	 * slot so as to not trip up assertions, and to avoid a
	 * double-free if linearizing fails */

	last_idx = (idx + 1) & tx->mask;
	idx = tx->req & tx->mask;
	tx->info[idx].skb = NULL;
	do {
		len = pci_unmap_len(&tx->info[idx], len);
		if (len) {
			if (tx->info[idx].skb != NULL)
				pci_unmap_single(mgp->pdev,
						 pci_unmap_addr(&tx->info[idx],
								bus), len,
						 PCI_DMA_TODEVICE);
			else
				pci_unmap_page(mgp->pdev,
					       pci_unmap_addr(&tx->info[idx],
							      bus), len,
					       PCI_DMA_TODEVICE);
			pci_unmap_len_set(&tx->info[idx], len, 0);
			tx->info[idx].skb = NULL;
		}
		idx = (idx + 1) & tx->mask;
	} while (idx != last_idx);
H
Herbert Xu 已提交
2488
	if (skb_is_gso(skb)) {
2489 2490 2491 2492 2493 2494
		printk(KERN_ERR
		       "myri10ge: %s: TSO but wanted to linearize?!?!?\n",
		       mgp->dev->name);
		goto drop;
	}

A
Andrew Morton 已提交
2495
	if (skb_linearize(skb))
2496 2497
		goto drop;

2498
	tx->linearized++;
2499 2500 2501 2502
	goto again;

drop:
	dev_kfree_skb_any(skb);
2503
	ss->stats.tx_dropped += 1;
2504 2505 2506 2507
	return 0;

}

B
Brice Goglin 已提交
2508 2509 2510
static int myri10ge_sw_tso(struct sk_buff *skb, struct net_device *dev)
{
	struct sk_buff *segs, *curr;
2511
	struct myri10ge_priv *mgp = netdev_priv(dev);
B
Brice Goglin 已提交
2512 2513 2514
	int status;

	segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
2515
	if (IS_ERR(segs))
B
Brice Goglin 已提交
2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542
		goto drop;

	while (segs) {
		curr = segs;
		segs = segs->next;
		curr->next = NULL;
		status = myri10ge_xmit(curr, dev);
		if (status != 0) {
			dev_kfree_skb_any(curr);
			if (segs != NULL) {
				curr = segs;
				segs = segs->next;
				curr->next = NULL;
				dev_kfree_skb_any(segs);
			}
			goto drop;
		}
	}
	dev_kfree_skb_any(skb);
	return 0;

drop:
	dev_kfree_skb_any(skb);
	mgp->stats.tx_dropped += 1;
	return 0;
}

2543 2544 2545 2546 2547 2548 2549 2550
static struct net_device_stats *myri10ge_get_stats(struct net_device *dev)
{
	struct myri10ge_priv *mgp = netdev_priv(dev);
	return &mgp->stats;
}

static void myri10ge_set_multicast_list(struct net_device *dev)
{
2551
	struct myri10ge_priv *mgp = netdev_priv(dev);
2552 2553
	struct myri10ge_cmd cmd;
	struct dev_mc_list *mc_list;
2554
	__be32 data[2] = { 0, 0 };
2555
	int err;
2556
	DECLARE_MAC_BUF(mac);
2557

2558 2559
	/* can be called from atomic contexts,
	 * pass 1 to force atomicity in myri10ge_send_cmd() */
2560 2561 2562
	myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);

	/* This firmware is known to not support multicast */
2563
	if (!mgp->fw_multicast_support)
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574
		return;

	/* Disable multicast filtering */

	err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
	if (err != 0) {
		printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_ENABLE_ALLMULTI,"
		       " error status: %d\n", dev->name, err);
		goto abort;
	}

2575
	if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
		/* request to disable multicast filtering, so quit here */
		return;
	}

	/* Flush the filters */

	err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
				&cmd, 1);
	if (err != 0) {
		printk(KERN_ERR
		       "myri10ge: %s: Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS"
		       ", error status: %d\n", dev->name, err);
		goto abort;
	}

	/* Walk the multicast list, and add each address */
	for (mc_list = dev->mc_list; mc_list != NULL; mc_list = mc_list->next) {
A
Al Viro 已提交
2593 2594 2595
		memcpy(data, &mc_list->dmi_addr, 6);
		cmd.data0 = ntohl(data[0]);
		cmd.data1 = ntohl(data[1]);
2596 2597 2598 2599 2600 2601 2602
		err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
					&cmd, 1);

		if (err != 0) {
			printk(KERN_ERR "myri10ge: %s: Failed "
			       "MXGEFW_JOIN_MULTICAST_GROUP, error status:"
			       "%d\t", dev->name, err);
2603 2604
			printk(KERN_ERR "MAC %s\n",
			       print_mac(mac, mc_list->dmi_addr));
2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
			goto abort;
		}
	}
	/* Enable multicast filtering */
	err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
	if (err != 0) {
		printk(KERN_ERR "myri10ge: %s: Failed MXGEFW_DISABLE_ALLMULTI,"
		       "error status: %d\n", dev->name, err);
		goto abort;
	}

	return;

abort:
	return;
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
}

static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
{
	struct sockaddr *sa = addr;
	struct myri10ge_priv *mgp = netdev_priv(dev);
	int status;

	if (!is_valid_ether_addr(sa->sa_data))
		return -EADDRNOTAVAIL;

	status = myri10ge_update_mac_address(mgp, sa->sa_data);
	if (status != 0) {
		printk(KERN_ERR
		       "myri10ge: %s: changing mac address failed with %d\n",
		       dev->name, status);
		return status;
	}

	/* change the dev structure */
	memcpy(dev->dev_addr, sa->sa_data, 6);
	return 0;
}

static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
{
	struct myri10ge_priv *mgp = netdev_priv(dev);
	int error = 0;

	if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
		printk(KERN_ERR "myri10ge: %s: new mtu (%d) is not valid\n",
		       dev->name, new_mtu);
		return -EINVAL;
	}
	printk(KERN_INFO "%s: changing mtu from %d to %d\n",
	       dev->name, dev->mtu, new_mtu);
	if (mgp->running) {
		/* if we change the mtu on an active device, we must
		 * reset the device so the firmware sees the change */
		myri10ge_close(dev);
		dev->mtu = new_mtu;
		myri10ge_open(dev);
	} else
		dev->mtu = new_mtu;

	return error;
}

/*
 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
 * Only do it if the bridge is a root port since we don't want to disturb
 * any other device, except if forced with myri10ge_ecrc_enable > 1.
 */

static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
{
	struct pci_dev *bridge = mgp->pdev->bus->self;
	struct device *dev = &mgp->pdev->dev;
	unsigned cap;
	unsigned err_cap;
	u16 val;
	u8 ext_type;
	int ret;

	if (!myri10ge_ecrc_enable || !bridge)
		return;

	/* check that the bridge is a root port */
	cap = pci_find_capability(bridge, PCI_CAP_ID_EXP);
	pci_read_config_word(bridge, cap + PCI_CAP_FLAGS, &val);
	ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
	if (ext_type != PCI_EXP_TYPE_ROOT_PORT) {
		if (myri10ge_ecrc_enable > 1) {
2693
			struct pci_dev *prev_bridge, *old_bridge = bridge;
2694 2695 2696 2697

			/* Walk the hierarchy up to the root port
			 * where ECRC has to be enabled */
			do {
2698
				prev_bridge = bridge;
2699
				bridge = bridge->bus->self;
2700
				if (!bridge || prev_bridge == bridge) {
2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
					dev_err(dev,
						"Failed to find root port"
						" to force ECRC\n");
					return;
				}
				cap =
				    pci_find_capability(bridge, PCI_CAP_ID_EXP);
				pci_read_config_word(bridge,
						     cap + PCI_CAP_FLAGS, &val);
				ext_type = (val & PCI_EXP_FLAGS_TYPE) >> 4;
			} while (ext_type != PCI_EXP_TYPE_ROOT_PORT);

			dev_info(dev,
				 "Forcing ECRC on non-root port %s"
				 " (enabling on root port %s)\n",
				 pci_name(old_bridge), pci_name(bridge));
		} else {
			dev_err(dev,
				"Not enabling ECRC on non-root port %s\n",
				pci_name(bridge));
			return;
		}
	}

	cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
	if (!cap)
		return;

	ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
	if (ret) {
		dev_err(dev, "failed reading ext-conf-space of %s\n",
			pci_name(bridge));
		dev_err(dev, "\t pci=nommconf in use? "
			"or buggy/incomplete/absent ACPI MCFG attr?\n");
		return;
	}
	if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
		return;

	err_cap |= PCI_ERR_CAP_ECRC_GENE;
	pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
	dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
}

/*
 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
 * when the PCI-E Completion packets are aligned on an 8-byte
 * boundary.  Some PCI-E chip sets always align Completion packets; on
 * the ones that do not, the alignment can be enforced by enabling
 * ECRC generation (if supported).
 *
 * When PCI-E Completion packets are not aligned, it is actually more
 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
 *
 * If the driver can neither enable ECRC nor verify that it has
 * already been enabled, then it must use a firmware image which works
 * around unaligned completion packets (myri10ge_ethp_z8e.dat), and it
 * should also ensure that it never gives the device a Read-DMA which is
2759
 * larger than 2KB by setting the tx_boundary to 2KB.  If ECRC is
2760
 * enabled, then the driver should use the aligned (myri10ge_eth_z8e.dat)
2761
 * firmware image, and set tx_boundary to 4KB.
2762 2763
 */

2764
static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
2765
{
2766 2767
	struct pci_dev *pdev = mgp->pdev;
	struct device *dev = &pdev->dev;
B
Brice Goglin 已提交
2768
	int status;
2769

2770
	mgp->tx_boundary = 4096;
2771 2772 2773 2774
	/*
	 * Verify the max read request size was set to 4KB
	 * before trying the test with 4KB.
	 */
B
Brice Goglin 已提交
2775 2776
	status = pcie_get_readrq(pdev);
	if (status < 0) {
2777 2778 2779
		dev_err(dev, "Couldn't read max read req size: %d\n", status);
		goto abort;
	}
B
Brice Goglin 已提交
2780 2781
	if (status != 4096) {
		dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
2782
		mgp->tx_boundary = 2048;
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
	}
	/*
	 * load the optimized firmware (which assumes aligned PCIe
	 * completions) in order to see if it works on this host.
	 */
	mgp->fw_name = myri10ge_fw_aligned;
	status = myri10ge_load_firmware(mgp);
	if (status != 0) {
		goto abort;
	}

	/*
	 * Enable ECRC if possible
	 */
	myri10ge_enable_ecrc(mgp);

	/*
	 * Run a DMA test which watches for unaligned completions and
	 * aborts on the first one seen.
	 */

	status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
	if (status == 0)
		return;		/* keep the aligned firmware */

	if (status != -E2BIG)
		dev_warn(dev, "DMA test failed: %d\n", status);
	if (status == -ENOSYS)
		dev_warn(dev, "Falling back to ethp! "
			 "Please install up to date fw\n");
abort:
	/* fall back to using the unaligned firmware */
2815
	mgp->tx_boundary = 2048;
2816 2817
	mgp->fw_name = myri10ge_fw_unaligned;

2818 2819 2820 2821
}

static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
{
2822
	if (myri10ge_force_firmware == 0) {
2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
		int link_width, exp_cap;
		u16 lnk;

		exp_cap = pci_find_capability(mgp->pdev, PCI_CAP_ID_EXP);
		pci_read_config_word(mgp->pdev, exp_cap + PCI_EXP_LNKSTA, &lnk);
		link_width = (lnk >> 4) & 0x3f;

		/* Check to see if Link is less than 8 or if the
		 * upstream bridge is known to provide aligned
		 * completions */
		if (link_width < 8) {
			dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
				 link_width);
2836
			mgp->tx_boundary = 4096;
2837
			mgp->fw_name = myri10ge_fw_aligned;
2838 2839
		} else {
			myri10ge_firmware_probe(mgp);
2840 2841 2842 2843 2844
		}
	} else {
		if (myri10ge_force_firmware == 1) {
			dev_info(&mgp->pdev->dev,
				 "Assuming aligned completions (forced)\n");
2845
			mgp->tx_boundary = 4096;
2846 2847 2848 2849
			mgp->fw_name = myri10ge_fw_aligned;
		} else {
			dev_info(&mgp->pdev->dev,
				 "Assuming unaligned completions (forced)\n");
2850
			mgp->tx_boundary = 2048;
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
			mgp->fw_name = myri10ge_fw_unaligned;
		}
	}
	if (myri10ge_fw_name != NULL) {
		dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
			 myri10ge_fw_name);
		mgp->fw_name = myri10ge_fw_name;
	}
}

#ifdef CONFIG_PM
static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct myri10ge_priv *mgp;
	struct net_device *netdev;

	mgp = pci_get_drvdata(pdev);
	if (mgp == NULL)
		return -EINVAL;
	netdev = mgp->dev;

	netif_device_detach(netdev);
	if (netif_running(netdev)) {
		printk(KERN_INFO "myri10ge: closing %s\n", netdev->name);
		rtnl_lock();
		myri10ge_close(netdev);
		rtnl_unlock();
	}
	myri10ge_dummy_rdma(mgp, 0);
2880
	pci_save_state(pdev);
2881
	pci_disable_device(pdev);
2882 2883

	return pci_set_power_state(pdev, pci_choose_state(pdev, state));
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
}

static int myri10ge_resume(struct pci_dev *pdev)
{
	struct myri10ge_priv *mgp;
	struct net_device *netdev;
	int status;
	u16 vendor;

	mgp = pci_get_drvdata(pdev);
	if (mgp == NULL)
		return -EINVAL;
	netdev = mgp->dev;
	pci_set_power_state(pdev, 0);	/* zeros conf space as a side effect */
	msleep(5);		/* give card time to respond */
	pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
	if (vendor == 0xffff) {
		printk(KERN_ERR "myri10ge: %s: device disappeared!\n",
		       mgp->dev->name);
		return -EIO;
	}
2905

2906 2907 2908
	status = pci_restore_state(pdev);
	if (status)
		return status;
B
Brice Goglin 已提交
2909 2910

	status = pci_enable_device(pdev);
2911
	if (status) {
B
Brice Goglin 已提交
2912
		dev_err(&pdev->dev, "failed to enable device\n");
2913
		return status;
B
Brice Goglin 已提交
2914 2915
	}

2916 2917 2918
	pci_set_master(pdev);

	myri10ge_reset(mgp);
2919
	myri10ge_dummy_rdma(mgp, 1);
2920 2921 2922

	/* Save configuration space to be restored if the
	 * nic resets due to a parity error */
2923
	pci_save_state(pdev);
2924 2925 2926

	if (netif_running(netdev)) {
		rtnl_lock();
2927
		status = myri10ge_open(netdev);
2928
		rtnl_unlock();
2929 2930 2931
		if (status != 0)
			goto abort_with_enabled;

2932 2933 2934 2935 2936
	}
	netif_device_attach(netdev);

	return 0;

B
Brice Goglin 已提交
2937 2938
abort_with_enabled:
	pci_disable_device(pdev);
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962
	return -EIO;

}
#endif				/* CONFIG_PM */

static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
{
	struct pci_dev *pdev = mgp->pdev;
	int vs = mgp->vendor_specific_offset;
	u32 reboot;

	/*enter read32 mode */
	pci_write_config_byte(pdev, vs + 0x10, 0x3);

	/*read REBOOT_STATUS (0xfffffff0) */
	pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
	pci_read_config_dword(pdev, vs + 0x14, &reboot);
	return reboot;
}

/*
 * This watchdog is used to check whether the board has suffered
 * from a parity error and needs to be recovered.
 */
D
David Howells 已提交
2963
static void myri10ge_watchdog(struct work_struct *work)
2964
{
D
David Howells 已提交
2965
	struct myri10ge_priv *mgp =
2966
	    container_of(work, struct myri10ge_priv, watchdog_work);
2967
	struct myri10ge_tx_buf *tx;
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
	u32 reboot;
	int status;
	u16 cmd, vendor;

	mgp->watchdog_resets++;
	pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
	if ((cmd & PCI_COMMAND_MASTER) == 0) {
		/* Bus master DMA disabled?  Check to see
		 * if the card rebooted due to a parity error
		 * For now, just report it */
		reboot = myri10ge_read_reboot(mgp);
		printk(KERN_ERR
2980 2981 2982 2983 2984 2985 2986 2987
		       "myri10ge: %s: NIC rebooted (0x%x),%s resetting\n",
		       mgp->dev->name, reboot,
		       myri10ge_reset_recover ? " " : " not");
		if (myri10ge_reset_recover == 0)
			return;

		myri10ge_reset_recover--;

2988 2989 2990 2991 2992 2993 2994
		/*
		 * A rebooted nic will come back with config space as
		 * it was after power was applied to PCIe bus.
		 * Attempt to restore config space which was saved
		 * when the driver was loaded, or the last time the
		 * nic was resumed from power saving mode.
		 */
2995
		pci_restore_state(mgp->pdev);
2996 2997

		/* save state again for accounting reasons */
2998
		pci_save_state(mgp->pdev);
2999

3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
	} else {
		/* if we get back -1's from our slot, perhaps somebody
		 * powered off our card.  Don't try to reset it in
		 * this case */
		if (cmd == 0xffff) {
			pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
			if (vendor == 0xffff) {
				printk(KERN_ERR
				       "myri10ge: %s: device disappeared!\n",
				       mgp->dev->name);
				return;
			}
		}
		/* Perhaps it is a software error.  Try to reset */

		printk(KERN_ERR "myri10ge: %s: device timeout, resetting\n",
		       mgp->dev->name);
3017
		tx = &mgp->ss.tx;
3018
		printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
3019 3020 3021
		       mgp->dev->name, tx->req, tx->done,
		       tx->pkt_start, tx->pkt_done,
		       (int)ntohl(mgp->ss.fw_stats->send_done_count));
3022 3023
		msleep(2000);
		printk(KERN_INFO "myri10ge: %s: %d %d %d %d %d\n",
3024 3025 3026
		       mgp->dev->name, tx->req, tx->done,
		       tx->pkt_start, tx->pkt_done,
		       (int)ntohl(mgp->ss.fw_stats->send_done_count));
3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
	}
	rtnl_lock();
	myri10ge_close(mgp->dev);
	status = myri10ge_load_firmware(mgp);
	if (status != 0)
		printk(KERN_ERR "myri10ge: %s: failed to load firmware\n",
		       mgp->dev->name);
	else
		myri10ge_open(mgp->dev);
	rtnl_unlock();
}

/*
 * We use our own timer routine rather than relying upon
 * netdev->tx_timeout because we have a very large hardware transmit
 * queue.  Due to the large queue, the netdev->tx_timeout function
 * cannot detect a NIC with a parity error in a timely fashion if the
 * NIC is lightly loaded.
 */
static void myri10ge_watchdog_timer(unsigned long arg)
{
	struct myri10ge_priv *mgp;
3049
	struct myri10ge_slice_state *ss;
3050
	u32 rx_pause_cnt;
3051 3052

	mgp = (struct myri10ge_priv *)arg;
3053

3054 3055 3056 3057 3058
	rx_pause_cnt = ntohl(mgp->ss.fw_stats->dropped_pause);

	ss = &mgp->ss;
	if (ss->rx_small.watchdog_needed) {
		myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
3059
					mgp->small_bytes + MXGEFW_PAD, 1);
3060
		if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
3061
		    myri10ge_fill_thresh)
3062
			ss->rx_small.watchdog_needed = 0;
3063
	}
3064 3065 3066
	if (ss->rx_big.watchdog_needed) {
		myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 1);
		if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
3067
		    myri10ge_fill_thresh)
3068
			ss->rx_big.watchdog_needed = 0;
3069 3070
	}

3071 3072 3073
	if (ss->tx.req != ss->tx.done &&
	    ss->tx.done == ss->watchdog_tx_done &&
	    ss->watchdog_tx_req != ss->watchdog_tx_done) {
3074
		/* nic seems like it might be stuck.. */
3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
		if (rx_pause_cnt != mgp->watchdog_pause) {
			if (net_ratelimit())
				printk(KERN_WARNING "myri10ge %s:"
				       "TX paused, check link partner\n",
				       mgp->dev->name);
		} else {
			schedule_work(&mgp->watchdog_work);
			return;
		}
	}
	/* rearm timer */
	mod_timer(&mgp->watchdog_timer,
		  jiffies + myri10ge_watchdog_timeout * HZ);
3088 3089
	ss->watchdog_tx_done = ss->tx.done;
	ss->watchdog_tx_req = ss->tx.req;
3090
	mgp->watchdog_pause = rx_pause_cnt;
3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
}

static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct net_device *netdev;
	struct myri10ge_priv *mgp;
	struct device *dev = &pdev->dev;
	size_t bytes;
	int i;
	int status = -ENXIO;
	int dac_enabled;

	netdev = alloc_etherdev(sizeof(*mgp));
	if (netdev == NULL) {
		dev_err(dev, "Could not allocate ethernet device\n");
		return -ENOMEM;
	}

M
Maik Hampel 已提交
3109 3110
	SET_NETDEV_DEV(netdev, &pdev->dev);

3111 3112
	mgp = netdev_priv(netdev);
	mgp->dev = netdev;
3113
	netif_napi_add(netdev, &mgp->ss.napi, myri10ge_poll, myri10ge_napi_weight);
3114 3115 3116 3117
	mgp->pdev = pdev;
	mgp->csum_flag = MXGEFW_FLAGS_CKSUM;
	mgp->pause = myri10ge_flow_control;
	mgp->intr_coal_delay = myri10ge_intr_coal_delay;
3118
	mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
	init_waitqueue_head(&mgp->down_wq);

	if (pci_enable_device(pdev)) {
		dev_err(&pdev->dev, "pci_enable_device call failed\n");
		status = -ENODEV;
		goto abort_with_netdev;
	}

	/* Find the vendor-specific cap so we can check
	 * the reboot register later on */
	mgp->vendor_specific_offset
	    = pci_find_capability(pdev, PCI_CAP_ID_VNDR);

	/* Set our max read request to 4KB */
B
Brice Goglin 已提交
3133
	status = pcie_set_readrq(pdev, 4096);
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145
	if (status != 0) {
		dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
			status);
		goto abort_with_netdev;
	}

	pci_set_master(pdev);
	dac_enabled = 1;
	status = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
	if (status != 0) {
		dac_enabled = 0;
		dev_err(&pdev->dev,
3146 3147
			"64-bit pci address mask was refused, "
			"trying 32-bit\n");
3148 3149 3150 3151 3152 3153
		status = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
	}
	if (status != 0) {
		dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
		goto abort_with_netdev;
	}
B
Brice Goglin 已提交
3154 3155
	mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
				      &mgp->cmd_bus, GFP_KERNEL);
3156 3157 3158
	if (mgp->cmd == NULL)
		goto abort_with_netdev;

3159 3160 3161
	mgp->ss.fw_stats = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
					   &mgp->ss.fw_stats_bus, GFP_KERNEL);
	if (mgp->ss.fw_stats == NULL)
3162 3163 3164 3165 3166
		goto abort_with_cmd;

	mgp->board_span = pci_resource_len(pdev, 0);
	mgp->iomem_base = pci_resource_start(pdev, 0);
	mgp->mtrr = -1;
3167
	mgp->wc_enabled = 0;
3168 3169 3170
#ifdef CONFIG_MTRR
	mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
			     MTRR_TYPE_WRCOMB, 1);
3171 3172
	if (mgp->mtrr >= 0)
		mgp->wc_enabled = 1;
3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
#endif
	/* Hack.  need to get rid of these magic numbers */
	mgp->sram_size =
	    2 * 1024 * 1024 - (2 * (48 * 1024) + (32 * 1024)) - 0x100;
	if (mgp->sram_size > mgp->board_span) {
		dev_err(&pdev->dev, "board span %ld bytes too small\n",
			mgp->board_span);
		goto abort_with_wc;
	}
	mgp->sram = ioremap(mgp->iomem_base, mgp->board_span);
	if (mgp->sram == NULL) {
		dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
			mgp->board_span, mgp->iomem_base);
		status = -ENXIO;
		goto abort_with_wc;
	}
	memcpy_fromio(mgp->eeprom_strings,
		      mgp->sram + mgp->sram_size - MYRI10GE_EEPROM_STRINGS_SIZE,
		      MYRI10GE_EEPROM_STRINGS_SIZE);
	memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
	status = myri10ge_read_mac_addr(mgp);
	if (status)
		goto abort_with_ioremap;

	for (i = 0; i < ETH_ALEN; i++)
		netdev->dev_addr[i] = mgp->mac_addr[i];

	/* allocate rx done ring */
3201 3202 3203 3204
	bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
	mgp->ss.rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
						&mgp->ss.rx_done.bus, GFP_KERNEL);
	if (mgp->ss.rx_done.entry == NULL)
3205
		goto abort_with_ioremap;
3206
	memset(mgp->ss.rx_done.entry, 0, bytes);
3207

3208 3209
	myri10ge_select_firmware(mgp);

3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
	status = myri10ge_load_firmware(mgp);
	if (status != 0) {
		dev_err(&pdev->dev, "failed to load firmware\n");
		goto abort_with_rx_done;
	}

	status = myri10ge_reset(mgp);
	if (status != 0) {
		dev_err(&pdev->dev, "failed reset\n");
		goto abort_with_firmware;
	}

	pci_set_drvdata(pdev, mgp);
	if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
		myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
	if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
		myri10ge_initial_mtu = 68;
	netdev->mtu = myri10ge_initial_mtu;
	netdev->open = myri10ge_open;
	netdev->stop = myri10ge_close;
	netdev->hard_start_xmit = myri10ge_xmit;
	netdev->get_stats = myri10ge_get_stats;
	netdev->base_addr = mgp->iomem_base;
	netdev->change_mtu = myri10ge_change_mtu;
	netdev->set_multicast_list = myri10ge_set_multicast_list;
	netdev->set_mac_address = myri10ge_set_mac_address;
B
Brice Goglin 已提交
3236
	netdev->features = mgp->features;
3237 3238 3239
	if (dac_enabled)
		netdev->features |= NETIF_F_HIGHDMA;

3240 3241 3242 3243 3244 3245 3246 3247 3248
	/* make sure we can get an irq, and that MSI can be
	 * setup (if available).  Also ensure netdev->irq
	 * is set to correct value if MSI is enabled */
	status = myri10ge_request_irq(mgp);
	if (status != 0)
		goto abort_with_firmware;
	netdev->irq = pdev->irq;
	myri10ge_free_irq(mgp);

3249 3250
	/* Save configuration space to be restored if the
	 * nic resets due to a parity error */
3251
	pci_save_state(pdev);
3252 3253 3254 3255 3256 3257

	/* Setup the watchdog timer */
	setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
		    (unsigned long)mgp);

	SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
D
David Howells 已提交
3258
	INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
3259 3260 3261
	status = register_netdev(netdev);
	if (status != 0) {
		dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
3262
		goto abort_with_state;
3263
	}
3264 3265
	dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
		 (mgp->msi_enabled ? "MSI" : "xPIC"),
3266
		 netdev->irq, mgp->tx_boundary, mgp->fw_name,
3267
		 (mgp->wc_enabled ? "Enabled" : "Disabled"));
3268 3269 3270

	return 0;

3271
abort_with_state:
3272
	pci_restore_state(pdev);
3273 3274 3275 3276 3277

abort_with_firmware:
	myri10ge_dummy_rdma(mgp, 0);

abort_with_rx_done:
3278
	bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
B
Brice Goglin 已提交
3279
	dma_free_coherent(&pdev->dev, bytes,
3280
			  mgp->ss.rx_done.entry, mgp->ss.rx_done.bus);
3281 3282 3283 3284 3285 3286 3287 3288 3289

abort_with_ioremap:
	iounmap(mgp->sram);

abort_with_wc:
#ifdef CONFIG_MTRR
	if (mgp->mtrr >= 0)
		mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
#endif
3290 3291
	dma_free_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
			  mgp->ss.fw_stats, mgp->ss.fw_stats_bus);
3292 3293

abort_with_cmd:
B
Brice Goglin 已提交
3294 3295
	dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
			  mgp->cmd, mgp->cmd_bus);
3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325

abort_with_netdev:

	free_netdev(netdev);
	return status;
}

/*
 * myri10ge_remove
 *
 * Does what is necessary to shutdown one Myrinet device. Called
 *   once for each Myrinet card by the kernel when a module is
 *   unloaded.
 */
static void myri10ge_remove(struct pci_dev *pdev)
{
	struct myri10ge_priv *mgp;
	struct net_device *netdev;
	size_t bytes;

	mgp = pci_get_drvdata(pdev);
	if (mgp == NULL)
		return;

	flush_scheduled_work();
	netdev = mgp->dev;
	unregister_netdev(netdev);

	myri10ge_dummy_rdma(mgp, 0);

3326
	/* avoid a memory leak */
3327
	pci_restore_state(pdev);
3328

3329
	bytes = myri10ge_max_intr_slots * sizeof(*mgp->ss.rx_done.entry);
B
Brice Goglin 已提交
3330
	dma_free_coherent(&pdev->dev, bytes,
3331
			  mgp->ss.rx_done.entry, mgp->ss.rx_done.bus);
3332 3333 3334 3335 3336 3337 3338

	iounmap(mgp->sram);

#ifdef CONFIG_MTRR
	if (mgp->mtrr >= 0)
		mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
#endif
3339 3340
	dma_free_coherent(&pdev->dev, sizeof(*mgp->ss.fw_stats),
			  mgp->ss.fw_stats, mgp->ss.fw_stats_bus);
3341

B
Brice Goglin 已提交
3342 3343
	dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
			  mgp->cmd, mgp->cmd_bus);
3344 3345 3346 3347 3348

	free_netdev(netdev);
	pci_set_drvdata(pdev, NULL);
}

B
Brice Goglin 已提交
3349
#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 	0x0008
3350
#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9	0x0009
3351 3352

static struct pci_device_id myri10ge_pci_tbl[] = {
B
Brice Goglin 已提交
3353
	{PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
3354 3355
	{PCI_DEVICE
	 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
	{0},
};

static struct pci_driver myri10ge_driver = {
	.name = "myri10ge",
	.probe = myri10ge_probe,
	.remove = myri10ge_remove,
	.id_table = myri10ge_pci_tbl,
#ifdef CONFIG_PM
	.suspend = myri10ge_suspend,
	.resume = myri10ge_resume,
#endif
};

static __init int myri10ge_init_module(void)
{
	printk(KERN_INFO "%s: Version %s\n", myri10ge_driver.name,
	       MYRI10GE_VERSION_STR);
	return pci_register_driver(&myri10ge_driver);
}

module_init(myri10ge_init_module);

static __exit void myri10ge_cleanup_module(void)
{
	pci_unregister_driver(&myri10ge_driver);
}

module_exit(myri10ge_cleanup_module);