myri10ge.c 110.6 KB
Newer Older
1 2 3
/*************************************************************************
 * myri10ge.c: Myricom Myri-10G Ethernet driver.
 *
J
Jon Mason 已提交
4
 * Copyright (C) 2005 - 2011 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
 *
 *
 * 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
 *************************************************************************/

41 42
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

43 44 45 46 47 48
#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 已提交
49
#include <linux/dma-mapping.h>
50 51 52
#include <linux/etherdevice.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
53
#include <linux/dca.h>
54 55 56 57 58 59 60 61 62 63 64
#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/timer.h>
#include <linux/vmalloc.h>
#include <linux/crc32.h>
#include <linux/moduleparam.h>
#include <linux/io.h>
65
#include <linux/log2.h>
66
#include <linux/slab.h>
67
#include <linux/prefetch.h>
68
#include <net/checksum.h>
A
Andrew Gallatin 已提交
69 70
#include <net/ip.h>
#include <net/tcp.h>
71 72 73 74 75 76
#include <asm/byteorder.h>
#include <asm/processor.h>

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

J
Jon Mason 已提交
77
#define MYRI10GE_VERSION_STR "1.5.3-1.534"
78 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
Al Viro 已提交
95
#define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
96 97
#define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff

98 99 100 101
#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)

B
Brice Goglin 已提交
102 103
#define MYRI10GE_MAX_SLICES 32

104
struct myri10ge_rx_buffer_state {
105 106
	struct page *page;
	int page_offset;
107 108
	DEFINE_DMA_UNMAP_ADDR(bus);
	DEFINE_DMA_UNMAP_LEN(len);
109 110 111 112 113
};

struct myri10ge_tx_buffer_state {
	struct sk_buff *skb;
	int last;
114 115
	DEFINE_DMA_UNMAP_ADDR(bus);
	DEFINE_DMA_UNMAP_LEN(len);
116 117 118 119 120 121 122 123 124 125 126 127
};

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 */
	struct mcp_kreq_ether_recv *shadow;	/* host shadow of recv ring */
	struct myri10ge_rx_buffer_state *info;
128 129 130
	struct page *page;
	dma_addr_t bus;
	int page_offset;
131
	int cnt;
132
	int fill_cnt;
133 134
	int alloc_fail;
	int mask;		/* number of rx slots -1 */
135
	int watchdog_needed;
136 137 138 139
};

struct myri10ge_tx_buf {
	struct mcp_kreq_ether_send __iomem *lanai;	/* lanai ptr for sendq */
B
Brice Goglin 已提交
140 141
	__be32 __iomem *send_go;	/* "go" doorbell ptr */
	__be32 __iomem *send_stop;	/* "stop" doorbell ptr */
142 143 144 145 146 147
	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 */
148 149
	int stop_queue;
	int linearized;
150 151
	int done ____cacheline_aligned;	/* transmit slots completed     */
	int pkt_done;		/* packets completed */
152
	int wake_queue;
B
Brice Goglin 已提交
153
	int queue_active;
154 155 156 157 158 159 160 161 162
};

struct myri10ge_rx_done {
	struct mcp_slot *entry;
	dma_addr_t bus;
	int cnt;
	int idx;
};

163 164 165 166 167 168 169 170 171 172
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 {
173 174 175 176
	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;
177 178 179 180 181 182 183 184 185
	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;
186
	int watchdog_rx_done;
187
	int stuck;
188
#ifdef CONFIG_MYRI10GE_DCA
189 190 191 192
	int cached_dca_tag;
	int cpu;
	__be32 __iomem *dca_tag;
#endif
B
Brice Goglin 已提交
193
	char irq_desc[32];
194 195 196
};

struct myri10ge_priv {
B
Brice Goglin 已提交
197
	struct myri10ge_slice_state *ss;
198
	int tx_boundary;	/* boundary transmits cannot cross */
B
Brice Goglin 已提交
199
	int num_slices;
200
	int running;		/* running?             */
201
	int small_bytes;
202
	int big_bytes;
203
	int max_intr_slots;
204 205 206 207 208
	struct net_device *dev;
	u8 __iomem *sram;
	int sram_size;
	unsigned long board_span;
	unsigned long iomem_base;
A
Al Viro 已提交
209
	__be32 __iomem *irq_deassert;
210 211 212 213 214
	char *mac_addr_string;
	struct mcp_cmd_response *cmd;
	dma_addr_t cmd_bus;
	struct pci_dev *pdev;
	int msi_enabled;
B
Brice Goglin 已提交
215 216
	int msix_enabled;
	struct msix_entry *msix_vectors;
217
#ifdef CONFIG_MYRI10GE_DCA
218
	int dca_enabled;
219
	int relaxed_order;
220
#endif
A
Al Viro 已提交
221
	u32 link_state;
222 223
	unsigned int rdma_tags_available;
	int intr_coal_delay;
A
Al Viro 已提交
224
	__be32 __iomem *intr_coal_delay_ptr;
225
	int wc_cookie;
226 227 228 229 230
	int down_cnt;
	wait_queue_head_t down_wq;
	struct work_struct watchdog_work;
	struct timer_list watchdog_timer;
	int watchdog_resets;
231
	int watchdog_pause;
232
	int pause;
233
	bool fw_name_allocated;
234 235
	char *fw_name;
	char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
236
	char *product_code_string;
237
	char fw_version[128];
238 239 240 241
	int fw_ver_major;
	int fw_ver_minor;
	int fw_ver_tiny;
	int adopted_rx_filter_bug;
242
	u8 mac_addr[ETH_ALEN];		/* eeprom mac address */
243 244
	unsigned long serial_number;
	int vendor_specific_offset;
245
	int fw_multicast_support;
246
	u32 features;
B
Brice Goglin 已提交
247
	u32 max_tso6;
248 249 250
	u32 read_dma;
	u32 write_dma;
	u32 read_write_dma;
251 252
	u32 link_changes;
	u32 msg_enable;
253
	unsigned int board_number;
254
	int rebooted;
255 256 257 258
};

static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
B
Brice Goglin 已提交
259 260
static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
B
Ben Hutchings 已提交
261 262 263 264
MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
265

266
/* Careful: must be accessed under kernel_param_lock() */
267
static char *myri10ge_fw_name = NULL;
268
module_param(myri10ge_fw_name, charp, 0644);
269
MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
270

271 272
#define MYRI10GE_MAX_BOARDS 8
static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
A
Andrew Gallatin 已提交
273
    {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
274 275
module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
			 0444);
276
MODULE_PARM_DESC(myri10ge_fw_names, "Firmware image names per board");
277

278
static int myri10ge_ecrc_enable = 1;
279
module_param(myri10ge_ecrc_enable, int, 0444);
280
MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
281 282

static int myri10ge_small_bytes = -1;	/* -1 == auto */
283
module_param(myri10ge_small_bytes, int, 0644);
284
MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
285 286

static int myri10ge_msi = 1;	/* enable msi by default */
287
module_param(myri10ge_msi, int, 0644);
288
MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
289

290
static int myri10ge_intr_coal_delay = 75;
291
module_param(myri10ge_intr_coal_delay, int, 0444);
292
MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
293 294

static int myri10ge_flow_control = 1;
295
module_param(myri10ge_flow_control, int, 0444);
296
MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
297 298

static int myri10ge_deassert_wait = 1;
299
module_param(myri10ge_deassert_wait, int, 0644);
300
MODULE_PARM_DESC(myri10ge_deassert_wait,
301
		 "Wait when deasserting legacy interrupts");
302 303

static int myri10ge_force_firmware = 0;
304
module_param(myri10ge_force_firmware, int, 0444);
305
MODULE_PARM_DESC(myri10ge_force_firmware,
306
		 "Force firmware to assume aligned completions");
307 308

static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
309
module_param(myri10ge_initial_mtu, int, 0444);
310
MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
311 312

static int myri10ge_napi_weight = 64;
313
module_param(myri10ge_napi_weight, int, 0444);
314
MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
315 316

static int myri10ge_watchdog_timeout = 1;
317
module_param(myri10ge_watchdog_timeout, int, 0444);
318
MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
319 320

static int myri10ge_max_irq_loops = 1048576;
321
module_param(myri10ge_max_irq_loops, int, 0444);
322
MODULE_PARM_DESC(myri10ge_max_irq_loops,
323
		 "Set stuck legacy IRQ detection threshold");
324

325 326 327 328 329 330
#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)");

331
static int myri10ge_fill_thresh = 256;
332
module_param(myri10ge_fill_thresh, int, 0644);
333
MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
334

335 336
static int myri10ge_reset_recover = 1;

B
Brice Goglin 已提交
337
static int myri10ge_max_slices = 1;
338
module_param(myri10ge_max_slices, int, 0444);
B
Brice Goglin 已提交
339 340
MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");

341
static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
342
module_param(myri10ge_rss_hash, int, 0444);
B
Brice Goglin 已提交
343 344
MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");

345
static int myri10ge_dca = 1;
346
module_param(myri10ge_dca, int, 0444);
347 348
MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");

349 350 351 352 353 354 355
#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)

356
static void myri10ge_set_multicast_list(struct net_device *dev);
357 358
static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
					 struct net_device *dev);
359

360
static inline void put_be32(__be32 val, __be32 __iomem * p)
A
Al Viro 已提交
361
{
362
	__raw_writel((__force __u32) val, (__force void __iomem *)p);
A
Al Viro 已提交
363 364
}

365 366
static void myri10ge_get_stats(struct net_device *dev,
			       struct rtnl_link_stats64 *stats);
367

368 369 370 371 372 373 374 375
static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
{
	if (mgp->fw_name_allocated)
		kfree(mgp->fw_name);
	mgp->fw_name = name;
	mgp->fw_name_allocated = allocated;
}

376 377 378 379 380 381 382
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;
383
	char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	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 已提交
399
	response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
400 401 402 403 404 405 406 407 408 409 410 411 412
	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;
413 414
		     sleep_total < 1000 &&
		     response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
415
		     sleep_total += 10) {
416
			udelay(10);
417 418
			mb();
		}
419 420 421
	} else {
		/* use msleep for most command */
		for (sleep_total = 0;
422 423
		     sleep_total < 15 &&
		     response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
424 425 426 427 428 429 430 431 432 433
		     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;
434 435
		} else if (result == MXGEFW_CMD_UNKNOWN) {
			return -ENOSYS;
436 437
		} else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
			return -E2BIG;
B
Brice Goglin 已提交
438 439 440 441 442 443
		} else if (result == MXGEFW_CMD_ERROR_RANGE &&
			   cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
			   (data->
			    data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
			   0) {
			return -ERANGE;
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
		} 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;
			}
		}
484 485 486 487
		if (memcmp(ptr, "PC=", 3) == 0) {
			ptr += 3;
			mgp->product_code_string = ptr;
		}
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
		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;
510
	__be32 buf[16] __attribute__ ((__aligned__(8)));
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	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 已提交
527
	buf[2] = MYRI10GE_NO_CONFIRM_DATA;	/* confirm data */
528 529 530 531
	buf[3] = htonl(dma_high);	/* dummy addr MSW */
	buf[4] = htonl(dma_low);	/* dummy addr LSW */
	buf[5] = htonl(enable);	/* enable? */

532
	submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	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));
556
	mgp->fw_version[sizeof(mgp->fw_version) - 1] = '\0';
557

558 559
	sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
	       &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
560

561 562
	if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
	      mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
563 564 565 566 567 568 569 570 571 572 573 574 575
		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;
576
	unsigned char *fw_readback;
577 578 579
	struct mcp_gen_header *hdr;
	size_t hdr_offset;
	int status;
580
	unsigned i;
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

	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 已提交
599
	hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
600 601 602 603 604 605 606 607 608 609 610 611
	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);
612 613 614 615 616 617
	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 已提交
618
	}
619 620 621 622 623
	fw_readback = vmalloc(fw->size);
	if (!fw_readback) {
		status = -ENOMEM;
		goto abort_with_fw;
	}
624
	/* corruption checking is good for parity recovery and buggy chipset */
625 626 627
	memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
	reread_crc = crc32(~0, fw_readback, fw->size);
	vfree(fw_readback);
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
	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 已提交
652
	hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
653 654 655 656 657 658 659 660 661 662

	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);
663
	if (hdr == NULL)
664
		return -ENOMEM;
665

666 667 668
	memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
	status = myri10ge_validate_firmware(mgp, hdr);
	kfree(hdr);
669 670 671 672 673 674 675 676 677 678 679 680

	/* 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);
	}
681 682 683
	return status;
}

684
static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
{
	struct myri10ge_cmd cmd;
	int status;

	/* 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;
	}

	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
	if (status != 0) {
		dev_err(&mgp->pdev->dev,
			"failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
		return -ENXIO;
	}

	mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));

	return 0;
}

B
Brice Goglin 已提交
710
static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
711 712
{
	char __iomem *submit;
713
	__be32 buf[16] __attribute__ ((__aligned__(8)));
714 715 716
	u32 dma_low, dma_high, size;
	int status, i;

B
Brice Goglin 已提交
717
	size = 0;
718 719
	status = myri10ge_load_hotplug_firmware(mgp, &size);
	if (status) {
B
Brice Goglin 已提交
720 721
		if (!adopt)
			return status;
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
		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");
737
		if (mgp->tx_boundary == 4096) {
738 739 740 741 742 743 744 745 746
			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");
		}

747
		set_fw_name(mgp, "adopted", false);
748
		mgp->tx_boundary = 2048;
749 750
		myri10ge_dummy_rdma(mgp, 1);
		status = myri10ge_get_firmware_capabilities(mgp);
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
		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 已提交
767
	buf[2] = MYRI10GE_NO_CONFIRM_DATA;	/* confirm data */
768 769 770 771 772 773 774 775 776 777

	/* 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 */

778
	submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
779 780 781 782 783 784

	myri10ge_pio_copy(submit, &buf, sizeof(buf));
	mb();
	msleep(1);
	mb();
	i = 0;
785 786
	while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
		msleep(1 << i);
787 788 789 790 791 792
		i++;
	}
	if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
		dev_err(&mgp->pdev->dev, "handoff failed\n");
		return -ENXIO;
	}
793
	myri10ge_dummy_rdma(mgp, 1);
794
	status = myri10ge_get_firmware_capabilities(mgp);
795

796
	return status;
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
}

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) {
822
		netdev_err(mgp->dev, "Failed to set flow control mode\n");
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
		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)
838
		netdev_err(mgp->dev, "Failed to set promisc mode\n");
839 840
}

841
static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
842 843 844 845
{
	struct myri10ge_cmd cmd;
	int status;
	u32 len;
846 847
	struct page *dmatest_page;
	dma_addr_t dmatest_bus;
848 849 850 851 852
	char *test = " ";

	dmatest_page = alloc_page(GFP_KERNEL);
	if (!dmatest_page)
		return -ENOMEM;
853 854 855
	dmatest_bus = dma_map_page(&mgp->pdev->dev, dmatest_page, 0,
				   PAGE_SIZE, DMA_BIDIRECTIONAL);
	if (unlikely(dma_mapping_error(&mgp->pdev->dev, dmatest_bus))) {
856 857 858
		__free_page(dmatest_page);
		return -ENOMEM;
	}
859 860 861 862 863 864 865 866 867 868

	/* 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.
	 */

869
	len = mgp->tx_boundary;
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

	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:
902 903
	dma_unmap_page(&mgp->pdev->dev, dmatest_bus, PAGE_SIZE,
		       DMA_BIDIRECTIONAL);
904 905 906 907 908 909 910 911 912 913 914 915
	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;
B
Brice Goglin 已提交
916 917
	struct myri10ge_slice_state *ss;
	int i, status;
918
	size_t bytes;
919
#ifdef CONFIG_MYRI10GE_DCA
920 921
	unsigned long dca_tag_off;
#endif
922 923 924 925 926 927 928 929 930

	/* 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;
	}
931 932

	(void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
B
Brice Goglin 已提交
933 934 935 936 937 938 939 940
	/*
	 * Use non-ndis mcp_slot (eg, 4 bytes total,
	 * no toeplitz hash value returned.  Older firmware will
	 * not understand this command, but will use the correct
	 * sized mcp_slot, so we ignore error returns
	 */
	cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
	(void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
941 942 943

	/* Now exchange information about interrupts  */

B
Brice Goglin 已提交
944
	bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
945 946
	cmd.data0 = (u32) bytes;
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
B
Brice Goglin 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

	/*
	 * Even though we already know how many slices are supported
	 * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
	 * has magic side effects, and must be called after a reset.
	 * It must be called prior to calling any RSS related cmds,
	 * including assigning an interrupt queue for anything but
	 * slice 0.  It must also be called *after*
	 * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
	 * the firmware to compute offsets.
	 */

	if (mgp->num_slices > 1) {

		/* ask the maximum number of slices it supports */
		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
					   &cmd, 0);
		if (status != 0) {
			dev_err(&mgp->pdev->dev,
				"failed to get number of slices\n");
		}

		/*
		 * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
		 * to setting up the interrupt queue DMA
		 */

		cmd.data0 = mgp->num_slices;
B
Brice Goglin 已提交
975 976 977
		cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
		if (mgp->dev->real_num_tx_queues > 1)
			cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
B
Brice Goglin 已提交
978 979
		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
					   &cmd, 0);
B
Brice Goglin 已提交
980 981 982 983 984

		/* Firmware older than 1.4.32 only supports multiple
		 * RX queues, so if we get an error, first retry using a
		 * single TX queue before giving up */
		if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
985
			netif_set_real_num_tx_queues(mgp->dev, 1);
B
Brice Goglin 已提交
986 987 988 989 990 991 992
			cmd.data0 = mgp->num_slices;
			cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
			status = myri10ge_send_cmd(mgp,
						   MXGEFW_CMD_ENABLE_RSS_QUEUES,
						   &cmd, 0);
		}

B
Brice Goglin 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		if (status != 0) {
			dev_err(&mgp->pdev->dev,
				"failed to set number of slices\n");

			return status;
		}
	}
	for (i = 0; i < mgp->num_slices; i++) {
		ss = &mgp->ss[i];
		cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
		cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
		cmd.data2 = i;
		status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
					    &cmd, 0);
1007
	}
1008 1009 1010

	status |=
	    myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
B
Brice Goglin 已提交
1011 1012 1013 1014 1015
	for (i = 0; i < mgp->num_slices; i++) {
		ss = &mgp->ss[i];
		ss->irq_claim =
		    (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
	}
1016 1017 1018
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
				    &cmd, 0);
	mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
1019 1020 1021

	status |= myri10ge_send_cmd
	    (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
A
Al Viro 已提交
1022
	mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
1023 1024 1025 1026
	if (status != 0) {
		dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
		return status;
	}
A
Al Viro 已提交
1027
	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1028

1029
#ifdef CONFIG_MYRI10GE_DCA
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
	dca_tag_off = cmd.data0;
	for (i = 0; i < mgp->num_slices; i++) {
		ss = &mgp->ss[i];
		if (status == 0) {
			ss->dca_tag = (__iomem __be32 *)
			    (mgp->sram + dca_tag_off + 4 * i);
		} else {
			ss->dca_tag = NULL;
		}
	}
B
Brice Goglin 已提交
1041
#endif				/* CONFIG_MYRI10GE_DCA */
1042

1043
	/* reset mcp/driver shared state back to 0 */
B
Brice Goglin 已提交
1044

1045
	mgp->link_changes = 0;
B
Brice Goglin 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	for (i = 0; i < mgp->num_slices; i++) {
		ss = &mgp->ss[i];

		memset(ss->rx_done.entry, 0, bytes);
		ss->tx.req = 0;
		ss->tx.done = 0;
		ss->tx.pkt_start = 0;
		ss->tx.pkt_done = 0;
		ss->rx_big.cnt = 0;
		ss->rx_small.cnt = 0;
		ss->rx_done.idx = 0;
		ss->rx_done.cnt = 0;
		ss->tx.wake_queue = 0;
		ss->tx.stop_queue = 0;
	}

1062 1063
	status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
	myri10ge_change_pause(mgp, mgp->pause);
1064
	myri10ge_set_multicast_list(mgp->dev);
1065 1066 1067
	return status;
}

1068
#ifdef CONFIG_MYRI10GE_DCA
1069 1070
static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
{
1071
	int ret;
1072 1073
	u16 ctl;

1074
	pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &ctl);
J
Jon Mason 已提交
1075

1076 1077 1078 1079
	ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
	if (ret != on) {
		ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
		ctl |= (on << 4);
1080
		pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, ctl);
1081 1082 1083 1084
	}
	return ret;
}

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
static void
myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
{
	ss->cached_dca_tag = tag;
	put_be32(htonl(tag), ss->dca_tag);
}

static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
{
	int cpu = get_cpu();
	int tag;

	if (cpu != ss->cpu) {
1098
		tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
1099 1100
		if (ss->cached_dca_tag != tag)
			myri10ge_write_dca(ss, cpu, tag);
1101
		ss->cpu = cpu;
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	}
	put_cpu();
}

static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
{
	int err, i;
	struct pci_dev *pdev = mgp->pdev;

	if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
		return;
	if (!myri10ge_dca) {
		dev_err(&pdev->dev, "dca disabled by administrator\n");
		return;
	}
	err = dca_add_requester(&pdev->dev);
	if (err) {
1119 1120 1121
		if (err != -ENODEV)
			dev_err(&pdev->dev,
				"dca_add_requester() failed, err=%d\n", err);
1122 1123
		return;
	}
1124
	mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
1125
	mgp->dca_enabled = 1;
1126 1127 1128 1129
	for (i = 0; i < mgp->num_slices; i++) {
		mgp->ss[i].cpu = -1;
		mgp->ss[i].cached_dca_tag = -1;
		myri10ge_update_dca(&mgp->ss[i]);
J
Jon Mason 已提交
1130
	}
1131 1132 1133 1134 1135 1136 1137 1138 1139
}

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

	if (!mgp->dca_enabled)
		return;
	mgp->dca_enabled = 0;
1140 1141
	if (mgp->relaxed_order)
		myri10ge_toggle_relaxed(pdev, 1);
J
Jon Mason 已提交
1142
	dca_remove_requester(&pdev->dev);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
}

static int myri10ge_notify_dca_device(struct device *dev, void *data)
{
	struct myri10ge_priv *mgp;
	unsigned long event;

	mgp = dev_get_drvdata(dev);
	event = *(unsigned long *)data;

	if (event == DCA_PROVIDER_ADD)
		myri10ge_setup_dca(mgp);
	else if (event == DCA_PROVIDER_REMOVE)
		myri10ge_teardown_dca(mgp);
	return 0;
}
B
Brice Goglin 已提交
1159
#endif				/* CONFIG_MYRI10GE_DCA */
1160

1161 1162 1163 1164
static inline void
myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
		    struct mcp_kreq_ether_recv *src)
{
A
Al Viro 已提交
1165
	__be32 low;
1166 1167

	low = src->addr_low;
1168
	src->addr_low = htonl(DMA_BIT_MASK(32));
1169 1170 1171
	myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
	mb();
	myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
1172 1173
	mb();
	src->addr_low = low;
A
Al Viro 已提交
1174
	put_be32(low, &dst->addr_low);
1175 1176 1177
	mb();
}

1178 1179 1180 1181 1182
static void
myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
			int bytes, int watchdog)
{
	struct page *page;
1183
	dma_addr_t bus;
1184
	int idx;
1185 1186 1187
#if MYRI10GE_ALLOC_SIZE > 4096
	int end_offset;
#endif
1188 1189 1190 1191 1192 1193 1194

	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;
1195
		if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
			/* 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;
			}
1208

1209
			bus = dma_map_page(&mgp->pdev->dev, page, 0,
1210
					   MYRI10GE_ALLOC_SIZE,
1211 1212
					   DMA_FROM_DEVICE);
			if (unlikely(dma_mapping_error(&mgp->pdev->dev, bus))) {
1213 1214 1215 1216 1217 1218
				__free_pages(page, MYRI10GE_ALLOC_ORDER);
				if (rx->fill_cnt - rx->cnt < 16)
					rx->watchdog_needed = 1;
				return;
			}

1219 1220
			rx->page = page;
			rx->page_offset = 0;
1221 1222
			rx->bus = bus;

1223 1224 1225 1226 1227
		}
		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 */
1228
		dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
1229 1230 1231 1232 1233 1234 1235
		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);
1236 1237 1238

#if MYRI10GE_ALLOC_SIZE > 4096
		/* don't cross a 4KB boundary */
1239 1240 1241
		end_offset = rx->page_offset + bytes - 1;
		if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
			rx->page_offset = end_offset & ~4095;
1242
#endif
1243 1244 1245 1246
		rx->fill_cnt++;

		/* copy 8 descriptors to the firmware at a time */
		if ((idx & 7) == 7) {
B
Brice Goglin 已提交
1247 1248
			myri10ge_submit_8rx(&rx->lanai[idx - 7],
					    &rx->shadow[idx - 7]);
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		}
	}
}

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) {
1260 1261 1262
		dma_unmap_page(&pdev->dev, (dma_unmap_addr(info, bus)
					    & ~(MYRI10GE_ALLOC_SIZE - 1)),
			       MYRI10GE_ALLOC_SIZE, DMA_FROM_DEVICE);
1263 1264 1265
	}
}

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
/*
 * GRO does not support acceleration of tagged vlan frames, and
 * this NIC does not support vlan tag offload, so we must pop
 * the tag ourselves to be able to achieve GRO performance that
 * is comparable to LRO.
 */

static inline void
myri10ge_vlan_rx(struct net_device *dev, void *addr, struct sk_buff *skb)
{
	u8 *va;
	struct vlan_ethhdr *veh;
1278
	skb_frag_t *frag;
1279 1280 1281 1282 1283
	__wsum vsum;

	va = addr;
	va += MXGEFW_PAD;
	veh = (struct vlan_ethhdr *)va;
1284 1285
	if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
	    NETIF_F_HW_VLAN_CTAG_RX &&
1286
	    veh->h_vlan_proto == htons(ETH_P_8021Q)) {
1287 1288 1289 1290 1291 1292
		/* fixup csum if needed */
		if (skb->ip_summed == CHECKSUM_COMPLETE) {
			vsum = csum_partial(va + ETH_HLEN, VLAN_HLEN, 0);
			skb->csum = csum_sub(skb->csum, vsum);
		}
		/* pop tag */
1293
		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(veh->h_vlan_TCI));
1294 1295 1296 1297
		memmove(va + VLAN_HLEN, va, 2 * ETH_ALEN);
		skb->len -= VLAN_HLEN;
		skb->data_len -= VLAN_HLEN;
		frag = skb_shinfo(skb)->frags;
J
Jonathan Lemon 已提交
1298 1299
		skb_frag_off_add(frag, VLAN_HLEN);
		skb_frag_size_sub(frag, VLAN_HLEN);
1300 1301 1302
	}
}

1303 1304
#define MYRI10GE_HLEN 64 /* Bytes to copy from page to skb linear memory */

1305
static inline int
1306
myri10ge_rx_done(struct myri10ge_slice_state *ss, int len, __wsum csum)
1307
{
1308
	struct myri10ge_priv *mgp = ss->mgp;
1309
	struct sk_buff *skb;
1310
	skb_frag_t *rx_frags;
1311
	struct myri10ge_rx_buf *rx;
1312
	int i, idx, remainder, bytes;
1313 1314 1315 1316
	struct pci_dev *pdev = mgp->pdev;
	struct net_device *dev = mgp->dev;
	u8 *va;

1317 1318 1319 1320 1321 1322 1323 1324
	if (len <= mgp->small_bytes) {
		rx = &ss->rx_small;
		bytes = mgp->small_bytes;
	} else {
		rx = &ss->rx_big;
		bytes = mgp->big_bytes;
	}

1325 1326 1327 1328
	len += MXGEFW_PAD;
	idx = rx->cnt & rx->mask;
	va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
	prefetch(va);
1329

1330
	skb = napi_get_frags(&ss->napi);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
	if (unlikely(skb == NULL)) {
		ss->stats.rx_dropped++;
		for (i = 0, remainder = len; remainder > 0; i++) {
			myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
			put_page(rx->info[idx].page);
			rx->cnt++;
			idx = rx->cnt & rx->mask;
			remainder -= MYRI10GE_ALLOC_SIZE;
		}
		return 0;
	}
	rx_frags = skb_shinfo(skb)->frags;
1343
	/* Fill skb_frag_t(s) with data from our receive */
1344 1345
	for (i = 0, remainder = len; remainder > 0; i++) {
		myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1346 1347 1348 1349
		skb_fill_page_desc(skb, i, rx->info[idx].page,
				   rx->info[idx].page_offset,
				   remainder < MYRI10GE_ALLOC_SIZE ?
				   remainder : MYRI10GE_ALLOC_SIZE);
1350 1351 1352 1353 1354
		rx->cnt++;
		idx = rx->cnt & rx->mask;
		remainder -= MYRI10GE_ALLOC_SIZE;
	}

1355
	/* remove padding */
J
Jonathan Lemon 已提交
1356
	skb_frag_off_add(&rx_frags[0], MXGEFW_PAD);
1357
	skb_frag_size_sub(&rx_frags[0], MXGEFW_PAD);
1358
	len -= MXGEFW_PAD;
1359

1360 1361 1362 1363 1364 1365
	skb->len = len;
	skb->data_len = len;
	skb->truesize += len;
	if (dev->features & NETIF_F_RXCSUM) {
		skb->ip_summed = CHECKSUM_COMPLETE;
		skb->csum = csum;
1366
	}
1367
	myri10ge_vlan_rx(mgp->dev, va, skb);
1368
	skb_record_rx_queue(skb, ss - &mgp->ss[0]);
1369

1370
	napi_gro_frags(&ss->napi);
1371 1372 1373 1374

	return 1;
}

1375 1376
static inline void
myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
1377
{
1378 1379
	struct pci_dev *pdev = ss->mgp->pdev;
	struct myri10ge_tx_buf *tx = &ss->tx;
B
Brice Goglin 已提交
1380
	struct netdev_queue *dev_queue;
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	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++;
1395 1396
		len = dma_unmap_len(&tx->info[idx], len);
		dma_unmap_len_set(&tx->info[idx], len, 0);
1397
		if (skb) {
1398 1399
			ss->stats.tx_bytes += skb->len;
			ss->stats.tx_packets++;
1400
			dev_consume_skb_irq(skb);
1401
			if (len)
1402
				dma_unmap_single(&pdev->dev,
1403
						 dma_unmap_addr(&tx->info[idx],
1404
								bus), len,
1405
						 DMA_TO_DEVICE);
1406 1407
		} else {
			if (len)
1408
				dma_unmap_page(&pdev->dev,
1409
					       dma_unmap_addr(&tx->info[idx],
1410
							      bus), len,
1411
					       DMA_TO_DEVICE);
1412 1413
		}
	}
B
Brice Goglin 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429

	dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
	/*
	 * Make a minimal effort to prevent the NIC from polling an
	 * idle tx queue.  If we can't get the lock we leave the queue
	 * active. In this case, either a thread was about to start
	 * using the queue anyway, or we lost a race and the NIC will
	 * waste some of its resources polling an inactive queue for a
	 * while.
	 */

	if ((ss->mgp->dev->real_num_tx_queues > 1) &&
	    __netif_tx_trylock(dev_queue)) {
		if (tx->req == tx->done) {
			tx->queue_active = 0;
			put_be32(htonl(1), tx->send_stop);
1430
			mb();
B
Brice Goglin 已提交
1431 1432 1433 1434
		}
		__netif_tx_unlock(dev_queue);
	}

1435
	/* start the queue if we've stopped it */
1436
	if (netif_tx_queue_stopped(dev_queue) &&
1437 1438
	    tx->req - tx->done < (tx->mask >> 1) &&
	    ss->mgp->running == MYRI10GE_ETH_RUNNING) {
1439
		tx->wake_queue++;
B
Brice Goglin 已提交
1440
		netif_tx_wake_queue(dev_queue);
1441 1442 1443
	}
}

1444 1445
static inline int
myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
1446
{
1447 1448
	struct myri10ge_rx_done *rx_done = &ss->rx_done;
	struct myri10ge_priv *mgp = ss->mgp;
1449 1450 1451 1452 1453
	unsigned long rx_bytes = 0;
	unsigned long rx_packets = 0;
	unsigned long rx_ok;
	int idx = rx_done->idx;
	int cnt = rx_done->cnt;
1454
	int work_done = 0;
1455
	u16 length;
A
Al Viro 已提交
1456
	__wsum checksum;
1457

1458
	while (rx_done->entry[idx].length != 0 && work_done < budget) {
1459 1460
		length = ntohs(rx_done->entry[idx].length);
		rx_done->entry[idx].length = 0;
A
Al Viro 已提交
1461
		checksum = csum_unfold(rx_done->entry[idx].checksum);
1462
		rx_ok = myri10ge_rx_done(ss, length, checksum);
1463 1464 1465
		rx_packets += rx_ok;
		rx_bytes += rx_ok * (unsigned long)length;
		cnt++;
1466
		idx = cnt & (mgp->max_intr_slots - 1);
1467
		work_done++;
1468 1469 1470
	}
	rx_done->idx = idx;
	rx_done->cnt = cnt;
1471 1472
	ss->stats.rx_packets += rx_packets;
	ss->stats.rx_bytes += rx_bytes;
1473 1474

	/* restock receive rings if needed */
1475 1476
	if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
		myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
1477
					mgp->small_bytes + MXGEFW_PAD, 0);
1478 1479
	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);
1480

1481
	return work_done;
1482 1483 1484 1485
}

static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
{
B
Brice Goglin 已提交
1486
	struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
1487 1488

	if (unlikely(stats->stats_updated)) {
1489 1490 1491 1492 1493
		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) {
J
Jon Mason 已提交
1494
				netif_info(mgp, link, mgp->dev, "link up\n");
1495
				netif_carrier_on(mgp->dev);
1496
				mgp->link_changes++;
1497
			} else {
J
Jon Mason 已提交
1498 1499
				netif_info(mgp, link, mgp->dev, "link %s\n",
					   (link_up == MXGEFW_LINK_MYRINET ?
1500
					    "mismatch (Myrinet detected)" :
J
Jon Mason 已提交
1501
					    "down"));
1502
				netif_carrier_off(mgp->dev);
1503
				mgp->link_changes++;
1504 1505 1506
			}
		}
		if (mgp->rdma_tags_available !=
1507
		    ntohl(stats->rdma_tags_available)) {
1508
			mgp->rdma_tags_available =
1509
			    ntohl(stats->rdma_tags_available);
1510 1511
			netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
				    mgp->rdma_tags_available);
1512 1513 1514 1515 1516 1517 1518
		}
		mgp->down_cnt += stats->link_down;
		if (stats->link_down)
			wake_up(&mgp->down_wq);
	}
}

1519
static int myri10ge_poll(struct napi_struct *napi, int budget)
1520
{
1521 1522
	struct myri10ge_slice_state *ss =
	    container_of(napi, struct myri10ge_slice_state, napi);
1523
	int work_done;
1524

1525
#ifdef CONFIG_MYRI10GE_DCA
1526 1527 1528
	if (ss->mgp->dca_enabled)
		myri10ge_update_dca(ss);
#endif
1529
	/* process as many rx events as NAPI will allow */
1530
	work_done = myri10ge_clean_rx_done(ss, budget);
1531

1532
	if (work_done < budget) {
1533
		napi_complete_done(napi, work_done);
1534
		put_be32(htonl(3), ss->irq_claim);
1535
	}
1536
	return work_done;
1537 1538
}

1539
static irqreturn_t myri10ge_intr(int irq, void *arg)
1540
{
1541 1542 1543 1544
	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;
1545 1546 1547
	u32 send_done_count;
	int i;

B
Brice Goglin 已提交
1548 1549 1550
	/* an interrupt on a non-zero receive-only slice is implicitly
	 * valid  since MSI-X irqs are not shared */
	if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
1551
		napi_schedule(&ss->napi);
1552
		return IRQ_HANDLED;
B
Brice Goglin 已提交
1553 1554
	}

1555 1556
	/* make sure it is our IRQ, and that the DMA has finished */
	if (unlikely(!stats->valid))
1557
		return IRQ_NONE;
1558 1559 1560 1561

	/* low bit indicates receives are present, so schedule
	 * napi poll handler */
	if (stats->valid & 1)
1562
		napi_schedule(&ss->napi);
1563

B
Brice Goglin 已提交
1564
	if (!mgp->msi_enabled && !mgp->msix_enabled) {
A
Al Viro 已提交
1565
		put_be32(0, mgp->irq_deassert);
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
		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)
1579
			myri10ge_tx_done(ss, (int)send_done_count);
1580
		if (unlikely(i > myri10ge_max_irq_loops)) {
J
Jon Mason 已提交
1581
			netdev_warn(mgp->dev, "irq stuck?\n");
1582 1583 1584 1585 1586 1587 1588 1589 1590
			stats->valid = 0;
			schedule_work(&mgp->watchdog_work);
		}
		if (likely(stats->valid == 0))
			break;
		cpu_relax();
		barrier();
	}

B
Brice Goglin 已提交
1591 1592 1593
	/* Only slice 0 updates stats */
	if (ss == mgp->ss)
		myri10ge_check_statblock(mgp);
1594

1595
	put_be32(htonl(3), ss->irq_claim + 1);
1596
	return IRQ_HANDLED;
1597 1598 1599
}

static int
1600 1601
myri10ge_get_link_ksettings(struct net_device *netdev,
			    struct ethtool_link_ksettings *cmd)
1602
{
1603 1604 1605 1606
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	char *ptr;
	int i;

1607 1608 1609
	cmd->base.autoneg = AUTONEG_DISABLE;
	cmd->base.speed = SPEED_10000;
	cmd->base.duplex = DUPLEX_FULL;
1610 1611 1612 1613 1614 1615 1616 1617 1618

	/*
	 * 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) {
1619
		netdev_err(netdev, "Missing product code\n");
1620 1621 1622 1623 1624
		return 0;
	}
	for (i = 0; i < 3; i++, ptr++) {
		ptr = strchr(ptr, '-');
		if (ptr == NULL) {
1625 1626
			netdev_err(netdev, "Invalid product code %s\n",
				   mgp->product_code_string);
1627 1628 1629
			return 0;
		}
	}
1630 1631 1632 1633
	if (*ptr == '2')
		ptr++;
	if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
		/* We've found either an XFP, quad ribbon fiber, or SFP+ */
1634 1635 1636
		cmd->base.port = PORT_FIBRE;
		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
1637
	} else {
1638
		cmd->base.port = PORT_OTHER;
1639
	}
1640

1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
	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 已提交
1659

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
	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 已提交
1670
	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
	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)
1694
		return myri10ge_change_pause(mgp, pause->rx_pause);
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705
	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);

B
Brice Goglin 已提交
1706 1707
	ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
	ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
1708
	ring->rx_jumbo_max_pending = 0;
1709
	ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
1710 1711 1712 1713 1714 1715
	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;
}

1716
static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
1717 1718 1719 1720 1721 1722 1723
	"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 */
1724
	"tx_boundary", "irq", "MSI", "MSIX",
1725
	"read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
1726
	"serial_number", "watchdog_resets",
1727
#ifdef CONFIG_MYRI10GE_DCA
1728
	"dca_capable_firmware", "dca_device_present",
1729
#endif
1730
	"link_changes", "link_up", "dropped_link_overflow",
1731 1732 1733
	"dropped_link_error_or_filtered",
	"dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
	"dropped_unicast_filtered", "dropped_multicast_filtered",
1734
	"dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
1735 1736 1737 1738 1739 1740 1741
	"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",
J
Jon Mason 已提交
1742
	"wake_queue", "stop_queue", "tx_linearized",
1743 1744 1745
};

#define MYRI10GE_NET_STATS_LEN      21
1746 1747
#define MYRI10GE_MAIN_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_main_stats)
#define MYRI10GE_SLICE_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_slice_stats)
1748 1749 1750 1751

static void
myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
{
B
Brice Goglin 已提交
1752 1753 1754
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	int i;

1755 1756
	switch (stringset) {
	case ETH_SS_STATS:
1757 1758 1759
		memcpy(data, *myri10ge_gstrings_main_stats,
		       sizeof(myri10ge_gstrings_main_stats));
		data += sizeof(myri10ge_gstrings_main_stats);
B
Brice Goglin 已提交
1760 1761 1762 1763 1764
		for (i = 0; i < mgp->num_slices; i++) {
			memcpy(data, *myri10ge_gstrings_slice_stats,
			       sizeof(myri10ge_gstrings_slice_stats));
			data += sizeof(myri10ge_gstrings_slice_stats);
		}
1765 1766 1767 1768
		break;
	}
}

1769
static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
1770
{
B
Brice Goglin 已提交
1771 1772
	struct myri10ge_priv *mgp = netdev_priv(netdev);

1773 1774
	switch (sset) {
	case ETH_SS_STATS:
B
Brice Goglin 已提交
1775 1776
		return MYRI10GE_MAIN_STATS_LEN +
		    mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
1777 1778 1779
	default:
		return -EOPNOTSUPP;
	}
1780 1781 1782 1783 1784 1785 1786
}

static void
myri10ge_get_ethtool_stats(struct net_device *netdev,
			   struct ethtool_stats *stats, u64 * data)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
1787
	struct myri10ge_slice_state *ss;
1788
	struct rtnl_link_stats64 link_stats;
B
Brice Goglin 已提交
1789
	int slice;
1790 1791
	int i;

1792
	/* force stats update */
E
Eric Dumazet 已提交
1793
	memset(&link_stats, 0, sizeof(link_stats));
1794
	(void)myri10ge_get_stats(netdev, &link_stats);
1795
	for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1796
		data[i] = ((u64 *)&link_stats)[i];
1797

1798
	data[i++] = (unsigned int)mgp->tx_boundary;
1799 1800
	data[i++] = (unsigned int)mgp->pdev->irq;
	data[i++] = (unsigned int)mgp->msi_enabled;
B
Brice Goglin 已提交
1801
	data[i++] = (unsigned int)mgp->msix_enabled;
1802 1803 1804 1805 1806
	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;
1807
#ifdef CONFIG_MYRI10GE_DCA
1808 1809 1810
	data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
	data[i++] = (unsigned int)(mgp->dca_enabled);
#endif
1811
	data[i++] = (unsigned int)mgp->link_changes;
1812 1813

	/* firmware stats are useful only in the first slice */
B
Brice Goglin 已提交
1814
	ss = &mgp->ss[0];
1815 1816
	data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
1817
	data[i++] =
1818 1819 1820 1821 1822
	    (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);
1823
	data[i++] =
1824 1825 1826 1827 1828 1829
	    (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);

B
Brice Goglin 已提交
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
	for (slice = 0; slice < mgp->num_slices; slice++) {
		ss = &mgp->ss[slice];
		data[i++] = slice;
		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;
	}
1843 1844
}

1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
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;
}

1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
/*
 * Use a low-level command to change the LED behavior. Rather than
 * blinking (which is the normal case), when identify is used, the
 * yellow LED turns solid.
 */
static int myri10ge_led(struct myri10ge_priv *mgp, int on)
{
	struct mcp_gen_header *hdr;
	struct device *dev = &mgp->pdev->dev;
	size_t hdr_off, pattern_off, hdr_len;
	u32 pattern = 0xfffffffe;

	/* find running firmware header */
	hdr_off = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
	if ((hdr_off & 3) || hdr_off + sizeof(*hdr) > mgp->sram_size) {
		dev_err(dev, "Running firmware has bad header offset (%d)\n",
			(int)hdr_off);
		return -EIO;
	}
	hdr_len = swab32(readl(mgp->sram + hdr_off +
			       offsetof(struct mcp_gen_header, header_length)));
	pattern_off = hdr_off + offsetof(struct mcp_gen_header, led_pattern);
	if (pattern_off >= (hdr_len + hdr_off)) {
		dev_info(dev, "Firmware does not support LED identification\n");
		return -EINVAL;
	}
	if (!on)
		pattern = swab32(readl(mgp->sram + pattern_off + 4));
1885
	writel(swab32(pattern), mgp->sram + pattern_off);
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
	return 0;
}

static int
myri10ge_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
{
	struct myri10ge_priv *mgp = netdev_priv(netdev);
	int rc;

	switch (state) {
	case ETHTOOL_ID_ACTIVE:
		rc = myri10ge_led(mgp, 1);
		break;

	case ETHTOOL_ID_INACTIVE:
		rc =  myri10ge_led(mgp, 0);
		break;

	default:
		rc = -EINVAL;
	}

	return rc;
}

1911
static const struct ethtool_ops myri10ge_ethtool_ops = {
1912
	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1913 1914 1915 1916 1917 1918
	.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,
1919
	.get_link = ethtool_op_get_link,
1920
	.get_strings = myri10ge_get_strings,
1921
	.get_sset_count = myri10ge_get_sset_count,
1922 1923
	.get_ethtool_stats = myri10ge_get_ethtool_stats,
	.set_msglevel = myri10ge_set_msglevel,
1924
	.get_msglevel = myri10ge_get_msglevel,
1925
	.set_phys_id = myri10ge_phys_id,
1926
	.get_link_ksettings = myri10ge_get_link_ksettings,
1927 1928
};

1929
static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
1930
{
1931
	struct myri10ge_priv *mgp = ss->mgp;
1932
	struct myri10ge_cmd cmd;
1933
	struct net_device *dev = mgp->dev;
1934 1935
	int tx_ring_size, rx_ring_size;
	int tx_ring_entries, rx_ring_entries;
B
Brice Goglin 已提交
1936
	int i, slice, status;
1937 1938 1939
	size_t bytes;

	/* get ring sizes */
B
Brice Goglin 已提交
1940 1941
	slice = ss - mgp->ss;
	cmd.data0 = slice;
1942 1943
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
	tx_ring_size = cmd.data0;
B
Brice Goglin 已提交
1944
	cmd.data0 = slice;
1945
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
1946 1947
	if (status != 0)
		return status;
1948 1949 1950 1951
	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);
1952 1953
	ss->tx.mask = tx_ring_entries - 1;
	ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
1954

1955 1956
	status = -ENOMEM;

1957 1958 1959
	/* allocate the host shadow rings */

	bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
1960 1961 1962
	    * sizeof(*ss->tx.req_list);
	ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
	if (ss->tx.req_bytes == NULL)
1963 1964 1965
		goto abort_with_nothing;

	/* ensure req_list entries are aligned to 8 bytes */
1966 1967
	ss->tx.req_list = (struct mcp_kreq_ether_send *)
	    ALIGN((unsigned long)ss->tx.req_bytes, 8);
B
Brice Goglin 已提交
1968
	ss->tx.queue_active = 0;
1969

1970 1971 1972
	bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
	ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_small.shadow == NULL)
1973 1974
		goto abort_with_tx_req_bytes;

1975 1976 1977
	bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
	ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_big.shadow == NULL)
1978 1979 1980 1981
		goto abort_with_rx_small_shadow;

	/* allocate the host info rings */

1982 1983 1984
	bytes = tx_ring_entries * sizeof(*ss->tx.info);
	ss->tx.info = kzalloc(bytes, GFP_KERNEL);
	if (ss->tx.info == NULL)
1985 1986
		goto abort_with_rx_big_shadow;

1987 1988 1989
	bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
	ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_small.info == NULL)
1990 1991
		goto abort_with_tx_info;

1992 1993 1994
	bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
	ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
	if (ss->rx_big.info == NULL)
1995 1996 1997
		goto abort_with_rx_small_info;

	/* Fill the receive rings */
1998 1999 2000 2001 2002 2003 2004 2005
	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;
J
Jon Mason 已提交
2006 2007 2008 2009 2010 2011
	if (mgp->small_bytes == 0) {
		ss->rx_small.fill_cnt = ss->rx_small.mask + 1;
	} else {
		myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
					mgp->small_bytes + MXGEFW_PAD, 0);
	}
2012

2013
	if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
2014 2015
		netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
			   slice, ss->rx_small.fill_cnt);
2016
		goto abort_with_rx_small_ring;
2017 2018
	}

2019 2020
	myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
	if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
2021 2022
		netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
			   slice, ss->rx_big.fill_cnt);
2023
		goto abort_with_rx_big_ring;
2024 2025 2026 2027 2028
	}

	return 0;

abort_with_rx_big_ring:
2029 2030 2031
	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],
2032
				       mgp->big_bytes);
2033
		put_page(ss->rx_big.info[idx].page);
2034 2035 2036
	}

abort_with_rx_small_ring:
J
Jon Mason 已提交
2037 2038
	if (mgp->small_bytes == 0)
		ss->rx_small.fill_cnt = ss->rx_small.cnt;
2039 2040 2041
	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],
2042
				       mgp->small_bytes + MXGEFW_PAD);
2043
		put_page(ss->rx_small.info[idx].page);
2044
	}
2045

2046
	kfree(ss->rx_big.info);
2047 2048

abort_with_rx_small_info:
2049
	kfree(ss->rx_small.info);
2050 2051

abort_with_tx_info:
2052
	kfree(ss->tx.info);
2053 2054

abort_with_rx_big_shadow:
2055
	kfree(ss->rx_big.shadow);
2056 2057

abort_with_rx_small_shadow:
2058
	kfree(ss->rx_small.shadow);
2059 2060

abort_with_tx_req_bytes:
2061 2062 2063
	kfree(ss->tx.req_bytes);
	ss->tx.req_bytes = NULL;
	ss->tx.req_list = NULL;
2064 2065 2066 2067 2068

abort_with_nothing:
	return status;
}

2069
static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
2070
{
2071
	struct myri10ge_priv *mgp = ss->mgp;
2072 2073 2074 2075
	struct sk_buff *skb;
	struct myri10ge_tx_buf *tx;
	int i, len, idx;

B
Brice Goglin 已提交
2076 2077 2078 2079
	/* If not allocated, skip it */
	if (ss->tx.req_list == NULL)
		return;

2080 2081 2082 2083 2084
	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],
2085
				       mgp->big_bytes);
2086
		put_page(ss->rx_big.info[idx].page);
2087 2088
	}

J
Jon Mason 已提交
2089 2090
	if (mgp->small_bytes == 0)
		ss->rx_small.fill_cnt = ss->rx_small.cnt;
2091 2092 2093 2094
	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 =
2095
			    MYRI10GE_ALLOC_SIZE;
2096
		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
2097
				       mgp->small_bytes + MXGEFW_PAD);
2098
		put_page(ss->rx_small.info[idx].page);
2099
	}
2100
	tx = &ss->tx;
2101 2102 2103 2104 2105 2106 2107
	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++;
2108 2109
		len = dma_unmap_len(&tx->info[idx], len);
		dma_unmap_len_set(&tx->info[idx], len, 0);
2110
		if (skb) {
2111
			ss->stats.tx_dropped++;
2112 2113
			dev_kfree_skb_any(skb);
			if (len)
2114
				dma_unmap_single(&mgp->pdev->dev,
2115
						 dma_unmap_addr(&tx->info[idx],
2116
								bus), len,
2117
						 DMA_TO_DEVICE);
2118 2119
		} else {
			if (len)
2120
				dma_unmap_page(&mgp->pdev->dev,
2121
					       dma_unmap_addr(&tx->info[idx],
2122
							      bus), len,
2123
					       DMA_TO_DEVICE);
2124 2125
		}
	}
2126
	kfree(ss->rx_big.info);
2127

2128
	kfree(ss->rx_small.info);
2129

2130
	kfree(ss->tx.info);
2131

2132
	kfree(ss->rx_big.shadow);
2133

2134
	kfree(ss->rx_small.shadow);
2135

2136 2137 2138
	kfree(ss->tx.req_bytes);
	ss->tx.req_bytes = NULL;
	ss->tx.req_list = NULL;
2139 2140
}

2141 2142 2143
static int myri10ge_request_irq(struct myri10ge_priv *mgp)
{
	struct pci_dev *pdev = mgp->pdev;
B
Brice Goglin 已提交
2144 2145 2146
	struct myri10ge_slice_state *ss;
	struct net_device *netdev = mgp->dev;
	int i;
2147 2148
	int status;

B
Brice Goglin 已提交
2149 2150 2151
	mgp->msi_enabled = 0;
	mgp->msix_enabled = 0;
	status = 0;
2152
	if (myri10ge_msi) {
B
Brice Goglin 已提交
2153
		if (mgp->num_slices > 1) {
2154 2155 2156
			status = pci_enable_msix_range(pdev, mgp->msix_vectors,
					mgp->num_slices, mgp->num_slices);
			if (status < 0) {
B
Brice Goglin 已提交
2157 2158 2159 2160
				dev_err(&pdev->dev,
					"Error %d setting up MSI-X\n", status);
				return status;
			}
2161
			mgp->msix_enabled = 1;
B
Brice Goglin 已提交
2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
		}
		if (mgp->msix_enabled == 0) {
			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;
			}
		}
2173
	}
B
Brice Goglin 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
	if (mgp->msix_enabled) {
		for (i = 0; i < mgp->num_slices; i++) {
			ss = &mgp->ss[i];
			snprintf(ss->irq_desc, sizeof(ss->irq_desc),
				 "%s:slice-%d", netdev->name, i);
			status = request_irq(mgp->msix_vectors[i].vector,
					     myri10ge_intr, 0, ss->irq_desc,
					     ss);
			if (status != 0) {
				dev_err(&pdev->dev,
					"slice %d failed to allocate IRQ\n", i);
				i--;
				while (i >= 0) {
					free_irq(mgp->msix_vectors[i].vector,
						 &mgp->ss[i]);
					i--;
				}
				pci_disable_msix(pdev);
				return status;
			}
		}
	} else {
		status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
				     mgp->dev->name, &mgp->ss[0]);
		if (status != 0) {
			dev_err(&pdev->dev, "failed to allocate IRQ\n");
			if (mgp->msi_enabled)
				pci_disable_msi(pdev);
		}
2203 2204 2205 2206 2207 2208 2209
	}
	return status;
}

static void myri10ge_free_irq(struct myri10ge_priv *mgp)
{
	struct pci_dev *pdev = mgp->pdev;
B
Brice Goglin 已提交
2210
	int i;
2211

B
Brice Goglin 已提交
2212 2213 2214 2215 2216 2217
	if (mgp->msix_enabled) {
		for (i = 0; i < mgp->num_slices; i++)
			free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
	} else {
		free_irq(pdev->irq, &mgp->ss[0]);
	}
2218 2219
	if (mgp->msi_enabled)
		pci_disable_msi(pdev);
B
Brice Goglin 已提交
2220 2221
	if (mgp->msix_enabled)
		pci_disable_msix(pdev);
2222 2223
}

2224 2225 2226 2227 2228 2229 2230
static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
{
	struct myri10ge_cmd cmd;
	struct myri10ge_slice_state *ss;
	int status;

	ss = &mgp->ss[slice];
B
Brice Goglin 已提交
2231 2232 2233 2234 2235 2236 2237 2238
	status = 0;
	if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
		cmd.data0 = slice;
		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
					   &cmd, 0);
		ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
		    (mgp->sram + cmd.data0);
	}
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
	cmd.data0 = slice;
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
				    &cmd, 0);
	ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
	    (mgp->sram + cmd.data0);

	cmd.data0 = slice;
	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
	ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
	    (mgp->sram + cmd.data0);

B
Brice Goglin 已提交
2250 2251 2252 2253
	ss->tx.send_go = (__iomem __be32 *)
	    (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
	ss->tx.send_stop = (__iomem __be32 *)
	    (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
	return status;

}

static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
{
	struct myri10ge_cmd cmd;
	struct myri10ge_slice_state *ss;
	int status;

	ss = &mgp->ss[slice];
	cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
B
Brice Goglin 已提交
2267
	cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
	if (status == -ENOSYS) {
		dma_addr_t bus = ss->fw_stats_bus;
		if (slice != 0)
			return -EINVAL;
		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;
	}
	return 0;
}

2287 2288
static int myri10ge_open(struct net_device *dev)
{
B
Brice Goglin 已提交
2289
	struct myri10ge_slice_state *ss;
2290
	struct myri10ge_priv *mgp = netdev_priv(dev);
2291
	struct myri10ge_cmd cmd;
B
Brice Goglin 已提交
2292
	int i, status, big_pow2, slice;
2293
	u8 __iomem *itable;
2294 2295 2296 2297 2298 2299 2300

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

	mgp->running = MYRI10GE_ETH_STARTING;
	status = myri10ge_reset(mgp);
	if (status != 0) {
2301
		netdev_err(dev, "failed reset\n");
2302
		goto abort_with_nothing;
2303 2304
	}

B
Brice Goglin 已提交
2305 2306
	if (mgp->num_slices > 1) {
		cmd.data0 = mgp->num_slices;
B
Brice Goglin 已提交
2307 2308 2309
		cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
		if (mgp->dev->real_num_tx_queues > 1)
			cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
B
Brice Goglin 已提交
2310 2311 2312
		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
					   &cmd, 0);
		if (status != 0) {
2313
			netdev_err(dev, "failed to set number of slices\n");
B
Brice Goglin 已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324
			goto abort_with_nothing;
		}
		/* setup the indirection table */
		cmd.data0 = mgp->num_slices;
		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
					   &cmd, 0);

		status |= myri10ge_send_cmd(mgp,
					    MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
					    &cmd, 0);
		if (status != 0) {
2325
			netdev_err(dev, "failed to setup rss tables\n");
B
Brice Goglin 已提交
2326
			goto abort_with_nothing;
B
Brice Goglin 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
		}

		/* just enable an identity mapping */
		itable = mgp->sram + cmd.data0;
		for (i = 0; i < mgp->num_slices; i++)
			__raw_writeb(i, &itable[i]);

		cmd.data0 = 1;
		cmd.data1 = myri10ge_rss_hash;
		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
					   &cmd, 0);
		if (status != 0) {
2339
			netdev_err(dev, "failed to enable slices\n");
B
Brice Goglin 已提交
2340 2341 2342 2343
			goto abort_with_nothing;
		}
	}

2344 2345 2346 2347
	status = myri10ge_request_irq(mgp);
	if (status != 0)
		goto abort_with_nothing;

2348 2349 2350 2351 2352 2353 2354
	/* 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)
2355 2356 2357 2358
		/* enough for a TCP header */
		mgp->small_bytes = (128 > SMP_CACHE_BYTES)
		    ? (128 - MXGEFW_PAD)
		    : (SMP_CACHE_BYTES - MXGEFW_PAD);
2359
	else
2360 2361
		/* enough for a vlan encapsulated ETH_DATA_LEN frame */
		mgp->small_bytes = VLAN_ETH_FRAME_LEN;
2362 2363

	/* Override the small buffer size? */
J
Jon Mason 已提交
2364
	if (myri10ge_small_bytes >= 0)
2365 2366 2367 2368 2369 2370
		mgp->small_bytes = myri10ge_small_bytes;

	/* 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.
	 */
2371
	big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
2372
	if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
2373
		while (!is_power_of_2(big_pow2))
2374
			big_pow2++;
2375
		mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
2376 2377 2378 2379 2380
	} else {
		big_pow2 = MYRI10GE_ALLOC_SIZE;
		mgp->big_bytes = big_pow2;
	}

B
Brice Goglin 已提交
2381 2382 2383 2384 2385 2386
	/* setup the per-slice data structures */
	for (slice = 0; slice < mgp->num_slices; slice++) {
		ss = &mgp->ss[slice];

		status = myri10ge_get_txrx(mgp, slice);
		if (status != 0) {
2387
			netdev_err(dev, "failed to get ring sizes or locations\n");
B
Brice Goglin 已提交
2388 2389 2390 2391 2392
			goto abort_with_rings;
		}
		status = myri10ge_allocate_rings(ss);
		if (status != 0)
			goto abort_with_rings;
B
Brice Goglin 已提交
2393 2394 2395 2396 2397

		/* only firmware which supports multiple TX queues
		 * supports setting up the tx stats on non-zero
		 * slices */
		if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
B
Brice Goglin 已提交
2398 2399
			status = myri10ge_set_stats(mgp, slice);
		if (status) {
2400
			netdev_err(dev, "Couldn't set stats DMA\n");
B
Brice Goglin 已提交
2401 2402 2403 2404 2405 2406
			goto abort_with_rings;
		}

		/* must happen prior to any irq */
		napi_enable(&(ss)->napi);
	}
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417

	/* 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) {
2418
		netdev_err(dev, "Couldn't set buffer sizes\n");
2419 2420 2421
		goto abort_with_rings;
	}

B
Brice Goglin 已提交
2422 2423 2424 2425 2426 2427 2428 2429
	/*
	 * Set Linux style TSO mode; this is needed only on newer
	 *  firmware versions.  Older versions default to Linux
	 *  style TSO
	 */
	cmd.data0 = 0;
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
	if (status && status != -ENOSYS) {
2430
		netdev_err(dev, "Couldn't set TSO mode\n");
2431 2432 2433
		goto abort_with_rings;
	}

A
Al Viro 已提交
2434
	mgp->link_state = ~0U;
2435 2436 2437 2438
	mgp->rdma_tags_available = 15;

	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
	if (status) {
2439
		netdev_err(dev, "Couldn't bring up link\n");
2440 2441 2442 2443 2444 2445
		goto abort_with_rings;
	}

	mgp->running = MYRI10GE_ETH_RUNNING;
	mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
	add_timer(&mgp->watchdog_timer);
B
Brice Goglin 已提交
2446 2447
	netif_tx_wake_all_queues(dev);

2448 2449 2450
	return 0;

abort_with_rings:
2451 2452 2453 2454
	while (slice) {
		slice--;
		napi_disable(&mgp->ss[slice].napi);
	}
B
Brice Goglin 已提交
2455 2456
	for (i = 0; i < mgp->num_slices; i++)
		myri10ge_free_rings(&mgp->ss[i]);
2457

2458 2459
	myri10ge_free_irq(mgp);

2460 2461 2462 2463 2464 2465 2466
abort_with_nothing:
	mgp->running = MYRI10GE_ETH_STOPPED;
	return -ENOMEM;
}

static int myri10ge_close(struct net_device *dev)
{
2467
	struct myri10ge_priv *mgp = netdev_priv(dev);
2468 2469
	struct myri10ge_cmd cmd;
	int status, old_down_cnt;
B
Brice Goglin 已提交
2470
	int i;
2471 2472 2473 2474

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

B
Brice Goglin 已提交
2475
	if (mgp->ss[0].tx.req_bytes == NULL)
2476 2477 2478 2479
		return 0;

	del_timer_sync(&mgp->watchdog_timer);
	mgp->running = MYRI10GE_ETH_STOPPING;
2480
	for (i = 0; i < mgp->num_slices; i++)
B
Brice Goglin 已提交
2481
		napi_disable(&mgp->ss[i].napi);
2482

2483
	netif_carrier_off(dev);
B
Brice Goglin 已提交
2484 2485

	netif_tx_stop_all_queues(dev);
2486 2487 2488 2489 2490 2491
	if (mgp->rebooted == 0) {
		old_down_cnt = mgp->down_cnt;
		mb();
		status =
		    myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
		if (status)
2492
			netdev_err(dev, "Couldn't bring down link\n");
2493

2494 2495 2496
		wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
				   HZ);
		if (old_down_cnt == mgp->down_cnt)
2497
			netdev_err(dev, "never got down irq\n");
2498
	}
2499
	netif_tx_disable(dev);
2500
	myri10ge_free_irq(mgp);
B
Brice Goglin 已提交
2501 2502
	for (i = 0; i < mgp->num_slices; i++)
		myri10ge_free_rings(&mgp->ss[i]);
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 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 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569

	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 已提交
2570
	put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
2571 2572 2573 2574
	tx->req += cnt;
	mb();
}

2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
static void myri10ge_unmap_tx_dma(struct myri10ge_priv *mgp,
				  struct myri10ge_tx_buf *tx, int idx)
{
	unsigned int len;
	int last_idx;

	/* Free any DMA resources we've alloced and clear out the skb slot */
	last_idx = (idx + 1) & tx->mask;
	idx = tx->req & tx->mask;
	do {
		len = dma_unmap_len(&tx->info[idx], len);
		if (len) {
			if (tx->info[idx].skb != NULL)
2588
				dma_unmap_single(&mgp->pdev->dev,
2589 2590
						 dma_unmap_addr(&tx->info[idx],
								bus), len,
2591
						 DMA_TO_DEVICE);
2592
			else
2593
				dma_unmap_page(&mgp->pdev->dev,
2594 2595
					       dma_unmap_addr(&tx->info[idx],
							      bus), len,
2596
					       DMA_TO_DEVICE);
2597 2598 2599 2600 2601 2602 2603
			dma_unmap_len_set(&tx->info[idx], len, 0);
			tx->info[idx].skb = NULL;
		}
		idx = (idx + 1) & tx->mask;
	} while (idx != last_idx);
}

2604 2605
/*
 * Transmit a packet.  We need to split the packet so that a single
2606
 * segment does not cross myri10ge->tx_boundary, so this makes segment
2607 2608 2609 2610 2611 2612 2613
 * 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.
 */

2614 2615
static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
				       struct net_device *dev)
2616 2617
{
	struct myri10ge_priv *mgp = netdev_priv(dev);
2618
	struct myri10ge_slice_state *ss;
2619
	struct mcp_kreq_ether_send *req;
2620
	struct myri10ge_tx_buf *tx;
2621
	skb_frag_t *frag;
B
Brice Goglin 已提交
2622
	struct netdev_queue *netdev_queue;
2623
	dma_addr_t bus;
A
Al Viro 已提交
2624 2625
	u32 low;
	__be32 high_swapped;
2626
	unsigned int len;
2627
	int idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
B
Brice Goglin 已提交
2628
	u16 pseudo_hdr_offset, cksum_offset, queue;
2629 2630 2631
	int cum_len, seglen, boundary, rdma_count;
	u8 flags, odd_flag;

B
Brice Goglin 已提交
2632 2633 2634
	queue = skb_get_queue_mapping(skb);
	ss = &mgp->ss[queue];
	netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
2635
	tx = &ss->tx;
B
Brice Goglin 已提交
2636

2637 2638 2639 2640 2641 2642 2643
again:
	req = tx->req_list;
	avail = tx->mask - 1 - (tx->req - tx->done);

	mss = 0;
	max_segments = MXGEFW_MAX_SEND_DESC;

2644
	if (skb_is_gso(skb)) {
2645
		mss = skb_shinfo(skb)->gso_size;
2646
		max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
2647 2648 2649 2650
	}

	if ((unlikely(avail < max_segments))) {
		/* we are out of transmit resources */
2651
		tx->stop_queue++;
B
Brice Goglin 已提交
2652
		netif_tx_stop_queue(netdev_queue);
2653
		return NETDEV_TX_BUSY;
2654 2655 2656 2657 2658 2659 2660
	}

	/* Setup checksum offloading, if needed */
	cksum_offset = 0;
	pseudo_hdr_offset = 0;
	odd_flag = 0;
	flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
2661
	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2662
		cksum_offset = skb_checksum_start_offset(skb);
A
Al Viro 已提交
2663
		pseudo_hdr_offset = cksum_offset + skb->csum_offset;
2664 2665
		/* If the headers are excessively large, then we must
		 * fall back to a software checksum */
B
Brice Goglin 已提交
2666 2667
		if (unlikely(!mss && (cksum_offset > 255 ||
				      pseudo_hdr_offset > 127))) {
2668
			if (skb_checksum_help(skb))
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
				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 已提交
2687
		 * TSO header can be at most 1KB long */
2688
		cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
2689

B
Brice Goglin 已提交
2690 2691 2692 2693 2694 2695 2696 2697 2698
		/* 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);
		}
2699 2700 2701
		/* for TSO, pseudo_hdr_offset holds mss.
		 * The firmware figures out where to put
		 * the checksum by parsing the header. */
A
Al Viro 已提交
2702
		pseudo_hdr_offset = mss;
2703 2704 2705 2706 2707 2708
	} 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 */
A
Alexander Duyck 已提交
2709 2710 2711 2712 2713
		if (eth_skb_pad(skb)) {
			/* The packet is gone, so we must
			 * return 0 */
			ss->stats.tx_dropped += 1;
			return NETDEV_TX_OK;
2714 2715 2716 2717
		}
	}

	/* map the skb for DMA */
E
Eric Dumazet 已提交
2718
	len = skb_headlen(skb);
2719 2720
	bus = dma_map_single(&mgp->pdev->dev, skb->data, len, DMA_TO_DEVICE);
	if (unlikely(dma_mapping_error(&mgp->pdev->dev, bus)))
2721 2722
		goto drop;

2723 2724
	idx = tx->req & tx->mask;
	tx->info[idx].skb = skb;
2725 2726
	dma_unmap_addr_set(&tx->info[idx], bus, bus);
	dma_unmap_len_set(&tx->info[idx], len, len);
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

	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
2753
		 * do not cross mgp->tx_boundary */
2754 2755 2756 2757 2758 2759 2760 2761 2762
		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;

2763 2764
			boundary =
			    (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
			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);
2783
					rdma_count += chop & ~next_is_first;
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
				} 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 已提交
2798
			req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811
			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 已提交
2812 2813 2814 2815 2816 2817
			if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
				if (unlikely(cksum_offset > seglen))
					cksum_offset -= seglen;
				else
					cksum_offset = 0;
			}
2818 2819 2820 2821 2822 2823 2824
		}
		if (frag_idx == frag_cnt)
			break;

		/* map next fragment for DMA */
		frag = &skb_shinfo(skb)->frags[frag_idx];
		frag_idx++;
E
Eric Dumazet 已提交
2825
		len = skb_frag_size(frag);
2826
		bus = skb_frag_dma_map(&mgp->pdev->dev, frag, 0, len,
2827
				       DMA_TO_DEVICE);
2828
		if (unlikely(dma_mapping_error(&mgp->pdev->dev, bus))) {
2829 2830 2831 2832
			myri10ge_unmap_tx_dma(mgp, tx, idx);
			goto drop;
		}
		idx = (count + tx->req) & tx->mask;
2833 2834
		dma_unmap_addr_set(&tx->info[idx], bus, bus);
		dma_unmap_len_set(&tx->info[idx], len, len);
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
	}

	(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;
B
Brice Goglin 已提交
2846
	myri10ge_submit_req(tx, tx->req_list, count);
B
Brice Goglin 已提交
2847 2848 2849 2850 2851
	/* if using multiple tx queues, make sure NIC polls the
	 * current slice */
	if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
		tx->queue_active = 1;
		put_be32(htonl(1), tx->send_go);
2852
		mb();
B
Brice Goglin 已提交
2853
	}
2854 2855
	tx->pkt_start++;
	if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
2856
		tx->stop_queue++;
B
Brice Goglin 已提交
2857
		netif_tx_stop_queue(netdev_queue);
2858
	}
2859
	return NETDEV_TX_OK;
2860 2861

abort_linearize:
2862
	myri10ge_unmap_tx_dma(mgp, tx, idx);
2863

H
Herbert Xu 已提交
2864
	if (skb_is_gso(skb)) {
2865
		netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
2866 2867 2868
		goto drop;
	}

A
Andrew Morton 已提交
2869
	if (skb_linearize(skb))
2870 2871
		goto drop;

2872
	tx->linearized++;
2873 2874 2875 2876
	goto again;

drop:
	dev_kfree_skb_any(skb);
2877
	ss->stats.tx_dropped += 1;
2878
	return NETDEV_TX_OK;
2879 2880 2881

}

2882 2883
static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
					 struct net_device *dev)
B
Brice Goglin 已提交
2884
{
2885
	struct sk_buff *segs, *curr, *next;
2886
	struct myri10ge_priv *mgp = netdev_priv(dev);
2887
	struct myri10ge_slice_state *ss;
2888
	netdev_tx_t status;
B
Brice Goglin 已提交
2889 2890

	segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
2891
	if (IS_ERR(segs))
B
Brice Goglin 已提交
2892 2893
		goto drop;

2894 2895
	skb_list_walk_safe(segs, curr, next) {
		skb_mark_not_on_list(curr);
B
Brice Goglin 已提交
2896 2897 2898 2899 2900
		status = myri10ge_xmit(curr, dev);
		if (status != 0) {
			dev_kfree_skb_any(curr);
			if (segs != NULL) {
				curr = segs;
2901
				segs = next;
B
Brice Goglin 已提交
2902 2903 2904 2905 2906 2907 2908
				curr->next = NULL;
				dev_kfree_skb_any(segs);
			}
			goto drop;
		}
	}
	dev_kfree_skb_any(skb);
2909
	return NETDEV_TX_OK;
B
Brice Goglin 已提交
2910 2911

drop:
2912
	ss = &mgp->ss[skb_get_queue_mapping(skb)];
B
Brice Goglin 已提交
2913
	dev_kfree_skb_any(skb);
2914
	ss->stats.tx_dropped += 1;
2915
	return NETDEV_TX_OK;
B
Brice Goglin 已提交
2916 2917
}

2918 2919
static void myri10ge_get_stats(struct net_device *dev,
			       struct rtnl_link_stats64 *stats)
2920
{
E
Eric Dumazet 已提交
2921 2922
	const struct myri10ge_priv *mgp = netdev_priv(dev);
	const struct myri10ge_slice_netstats *slice_stats;
B
Brice Goglin 已提交
2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933
	int i;

	for (i = 0; i < mgp->num_slices; i++) {
		slice_stats = &mgp->ss[i].stats;
		stats->rx_packets += slice_stats->rx_packets;
		stats->tx_packets += slice_stats->tx_packets;
		stats->rx_bytes += slice_stats->rx_bytes;
		stats->tx_bytes += slice_stats->tx_bytes;
		stats->rx_dropped += slice_stats->rx_dropped;
		stats->tx_dropped += slice_stats->tx_dropped;
	}
2934 2935 2936 2937
}

static void myri10ge_set_multicast_list(struct net_device *dev)
{
2938
	struct myri10ge_priv *mgp = netdev_priv(dev);
2939
	struct myri10ge_cmd cmd;
2940
	struct netdev_hw_addr *ha;
2941
	__be32 data[2] = { 0, 0 };
2942 2943
	int err;

2944 2945
	/* can be called from atomic contexts,
	 * pass 1 to force atomicity in myri10ge_send_cmd() */
2946 2947 2948
	myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);

	/* This firmware is known to not support multicast */
2949
	if (!mgp->fw_multicast_support)
2950 2951 2952 2953 2954 2955
		return;

	/* Disable multicast filtering */

	err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
	if (err != 0) {
2956 2957
		netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
			   err);
2958 2959 2960
		goto abort;
	}

2961
	if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
2962 2963 2964 2965 2966 2967 2968 2969 2970
		/* 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) {
2971 2972
		netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
			   err);
2973 2974 2975 2976
		goto abort;
	}

	/* Walk the multicast list, and add each address */
2977
	netdev_for_each_mc_addr(ha, dev) {
2978
		memcpy(data, &ha->addr, ETH_ALEN);
A
Al Viro 已提交
2979 2980
		cmd.data0 = ntohl(data[0]);
		cmd.data1 = ntohl(data[1]);
2981 2982 2983 2984
		err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
					&cmd, 1);

		if (err != 0) {
2985
			netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
2986
				   err, ha->addr);
2987 2988 2989 2990 2991 2992
			goto abort;
		}
	}
	/* Enable multicast filtering */
	err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
	if (err != 0) {
2993 2994
		netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
			   err);
2995 2996 2997 2998 2999 3000 3001
		goto abort;
	}

	return;

abort:
	return;
3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
}

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) {
3015 3016
		netdev_err(dev, "changing mac address failed with %d\n",
			   status);
3017 3018 3019 3020
		return status;
	}

	/* change the dev structure */
3021
	memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
3022 3023 3024 3025 3026 3027 3028
	return 0;
}

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

3029
	netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
3030 3031 3032 3033 3034 3035 3036 3037 3038
	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;

D
Ding Xiang 已提交
3039
	return 0;
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051
}

/*
 * 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;
3052
	int cap;
3053 3054 3055 3056 3057 3058 3059
	unsigned err_cap;
	int ret;

	if (!myri10ge_ecrc_enable || !bridge)
		return;

	/* check that the bridge is a root port */
3060
	if (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT) {
3061
		if (myri10ge_ecrc_enable > 1) {
3062
			struct pci_dev *prev_bridge, *old_bridge = bridge;
3063 3064 3065 3066

			/* Walk the hierarchy up to the root port
			 * where ECRC has to be enabled */
			do {
3067
				prev_bridge = bridge;
3068
				bridge = bridge->bus->self;
3069
				if (!bridge || prev_bridge == bridge) {
3070 3071 3072 3073 3074
					dev_err(dev,
						"Failed to find root port"
						" to force ECRC\n");
					return;
				}
3075 3076
			} while (pci_pcie_type(bridge) !=
				 PCI_EXP_TYPE_ROOT_PORT);
3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121

			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
B
Brice Goglin 已提交
3122
 * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
3123
 * should also ensure that it never gives the device a Read-DMA which is
3124
 * larger than 2KB by setting the tx_boundary to 2KB.  If ECRC is
B
Brice Goglin 已提交
3125
 * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
3126
 * firmware image, and set tx_boundary to 4KB.
3127 3128
 */

3129
static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
3130
{
3131 3132
	struct pci_dev *pdev = mgp->pdev;
	struct device *dev = &pdev->dev;
B
Brice Goglin 已提交
3133
	int status;
3134

3135
	mgp->tx_boundary = 4096;
3136 3137 3138 3139
	/*
	 * Verify the max read request size was set to 4KB
	 * before trying the test with 4KB.
	 */
B
Brice Goglin 已提交
3140 3141
	status = pcie_get_readrq(pdev);
	if (status < 0) {
3142 3143 3144
		dev_err(dev, "Couldn't read max read req size: %d\n", status);
		goto abort;
	}
B
Brice Goglin 已提交
3145 3146
	if (status != 4096) {
		dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
3147
		mgp->tx_boundary = 2048;
3148 3149 3150 3151 3152
	}
	/*
	 * load the optimized firmware (which assumes aligned PCIe
	 * completions) in order to see if it works on this host.
	 */
3153
	set_fw_name(mgp, myri10ge_fw_aligned, false);
B
Brice Goglin 已提交
3154
	status = myri10ge_load_firmware(mgp, 1);
3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179
	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 */
3180
	mgp->tx_boundary = 2048;
3181
	set_fw_name(mgp, myri10ge_fw_unaligned, false);
3182 3183 3184 3185
}

static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
{
3186 3187
	int overridden = 0;

3188
	if (myri10ge_force_firmware == 0) {
3189
		int link_width;
3190 3191
		u16 lnk;

3192
		pcie_capability_read_word(mgp->pdev, PCI_EXP_LNKSTA, &lnk);
3193 3194 3195 3196 3197 3198 3199 3200
		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);
3201
			mgp->tx_boundary = 4096;
3202
			set_fw_name(mgp, myri10ge_fw_aligned, false);
3203 3204
		} else {
			myri10ge_firmware_probe(mgp);
3205 3206 3207 3208 3209
		}
	} else {
		if (myri10ge_force_firmware == 1) {
			dev_info(&mgp->pdev->dev,
				 "Assuming aligned completions (forced)\n");
3210
			mgp->tx_boundary = 4096;
3211
			set_fw_name(mgp, myri10ge_fw_aligned, false);
3212 3213 3214
		} else {
			dev_info(&mgp->pdev->dev,
				 "Assuming unaligned completions (forced)\n");
3215
			mgp->tx_boundary = 2048;
3216
			set_fw_name(mgp, myri10ge_fw_unaligned, false);
3217 3218
		}
	}
3219

3220
	kernel_param_lock(THIS_MODULE);
3221
	if (myri10ge_fw_name != NULL) {
3222 3223 3224 3225 3226
		char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
		if (fw_name) {
			overridden = 1;
			set_fw_name(mgp, fw_name, true);
		}
3227
	}
3228
	kernel_param_unlock(THIS_MODULE);
3229

3230 3231 3232
	if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
	    myri10ge_fw_names[mgp->board_number] != NULL &&
	    strlen(myri10ge_fw_names[mgp->board_number])) {
3233
		set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
3234 3235 3236 3237 3238
		overridden = 1;
	}
	if (overridden)
		dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
			 mgp->fw_name);
3239 3240
}

3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260
static void myri10ge_mask_surprise_down(struct pci_dev *pdev)
{
	struct pci_dev *bridge = pdev->bus->self;
	int cap;
	u32 mask;

	if (bridge == NULL)
		return;

	cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
	if (cap) {
		/* a sram parity error can cause a surprise link
		 * down; since we expect and can recover from sram
		 * parity errors, mask surprise link down events */
		pci_read_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, &mask);
		mask |= 0x20;
		pci_write_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, mask);
	}
}

3261
static int __maybe_unused myri10ge_suspend(struct device *dev)
3262 3263 3264 3265
{
	struct myri10ge_priv *mgp;
	struct net_device *netdev;

3266
	mgp = dev_get_drvdata(dev);
3267 3268 3269 3270 3271 3272
	if (mgp == NULL)
		return -EINVAL;
	netdev = mgp->dev;

	netif_device_detach(netdev);
	if (netif_running(netdev)) {
3273
		netdev_info(netdev, "closing\n");
3274 3275 3276 3277 3278
		rtnl_lock();
		myri10ge_close(netdev);
		rtnl_unlock();
	}
	myri10ge_dummy_rdma(mgp, 0);
3279

3280
	return 0;
3281 3282
}

3283
static int __maybe_unused myri10ge_resume(struct device *dev)
3284
{
3285
	struct pci_dev *pdev = to_pci_dev(dev);
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297
	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;
	msleep(5);		/* give card time to respond */
	pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
	if (vendor == 0xffff) {
3298
		netdev_err(mgp->dev, "device disappeared!\n");
3299 3300
		return -EIO;
	}
3301

3302
	myri10ge_reset(mgp);
3303
	myri10ge_dummy_rdma(mgp, 1);
3304 3305 3306

	if (netif_running(netdev)) {
		rtnl_lock();
3307
		status = myri10ge_open(netdev);
3308
		rtnl_unlock();
3309 3310 3311
		if (status != 0)
			goto abort_with_enabled;

3312 3313 3314 3315 3316
	}
	netif_device_attach(netdev);

	return 0;

B
Brice Goglin 已提交
3317
abort_with_enabled:
3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335
	return -EIO;
}

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;
}

3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
static void
myri10ge_check_slice(struct myri10ge_slice_state *ss, int *reset_needed,
		     int *busy_slice_cnt, u32 rx_pause_cnt)
{
	struct myri10ge_priv *mgp = ss->mgp;
	int slice = ss - mgp->ss;

	if (ss->tx.req != ss->tx.done &&
	    ss->tx.done == ss->watchdog_tx_done &&
	    ss->watchdog_tx_req != ss->watchdog_tx_done) {
		/* nic seems like it might be stuck.. */
		if (rx_pause_cnt != mgp->watchdog_pause) {
			if (net_ratelimit())
				netdev_warn(mgp->dev, "slice %d: TX paused, "
					    "check link partner\n", slice);
		} else {
			netdev_warn(mgp->dev,
				    "slice %d: TX stuck %d %d %d %d %d %d\n",
				    slice, ss->tx.queue_active, ss->tx.req,
				    ss->tx.done, ss->tx.pkt_start,
				    ss->tx.pkt_done,
				    (int)ntohl(mgp->ss[slice].fw_stats->
					       send_done_count));
			*reset_needed = 1;
			ss->stuck = 1;
		}
	}
	if (ss->watchdog_tx_done != ss->tx.done ||
	    ss->watchdog_rx_done != ss->rx_done.cnt) {
		*busy_slice_cnt += 1;
	}
	ss->watchdog_tx_done = ss->tx.done;
	ss->watchdog_tx_req = ss->tx.req;
	ss->watchdog_rx_done = ss->rx_done.cnt;
}

3372 3373 3374 3375
/*
 * This watchdog is used to check whether the board has suffered
 * from a parity error and needs to be recovered.
 */
D
David Howells 已提交
3376
static void myri10ge_watchdog(struct work_struct *work)
3377
{
D
David Howells 已提交
3378
	struct myri10ge_priv *mgp =
3379
	    container_of(work, struct myri10ge_priv, watchdog_work);
3380 3381
	struct myri10ge_slice_state *ss;
	u32 reboot, rx_pause_cnt;
3382
	int status, rebooted;
B
Brice Goglin 已提交
3383
	int i;
3384 3385
	int reset_needed = 0;
	int busy_slice_cnt = 0;
3386 3387 3388 3389
	u16 cmd, vendor;

	mgp->watchdog_resets++;
	pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
3390
	rebooted = 0;
3391 3392 3393 3394 3395
	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);
3396
		netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
3397
			   reboot, myri10ge_reset_recover ? "" : " not");
3398 3399
		if (myri10ge_reset_recover == 0)
			return;
3400 3401 3402 3403
		rtnl_lock();
		mgp->rebooted = 1;
		rebooted = 1;
		myri10ge_close(mgp->dev);
3404
		myri10ge_reset_recover--;
3405
		mgp->rebooted = 0;
3406 3407 3408 3409 3410 3411 3412
		/*
		 * 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.
		 */
3413
		pci_restore_state(mgp->pdev);
3414 3415

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

3418 3419 3420 3421 3422 3423 3424
	} 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) {
3425
				netdev_err(mgp->dev, "device disappeared!\n");
3426 3427 3428
				return;
			}
		}
3429 3430 3431
		/* Perhaps it is a software error. See if stuck slice
		 * has recovered, reset if not */
		rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
B
Brice Goglin 已提交
3432
		for (i = 0; i < mgp->num_slices; i++) {
3433 3434 3435 3436 3437 3438 3439
			ss = mgp->ss;
			if (ss->stuck) {
				myri10ge_check_slice(ss, &reset_needed,
						     &busy_slice_cnt,
						     rx_pause_cnt);
				ss->stuck = 0;
			}
B
Brice Goglin 已提交
3440
		}
3441 3442 3443 3444 3445 3446
		if (!reset_needed) {
			netdev_dbg(mgp->dev, "not resetting\n");
			return;
		}

		netdev_err(mgp->dev, "device timeout, resetting\n");
3447
	}
B
Brice Goglin 已提交
3448

3449 3450 3451 3452
	if (!rebooted) {
		rtnl_lock();
		myri10ge_close(mgp->dev);
	}
B
Brice Goglin 已提交
3453
	status = myri10ge_load_firmware(mgp, 1);
3454
	if (status != 0)
3455
		netdev_err(mgp->dev, "failed to load firmware\n");
3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
	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.
 */
3468
static void myri10ge_watchdog_timer(struct timer_list *t)
3469 3470
{
	struct myri10ge_priv *mgp;
3471
	struct myri10ge_slice_state *ss;
3472
	int i, reset_needed, busy_slice_cnt;
3473
	u32 rx_pause_cnt;
3474
	u16 cmd;
3475

3476
	mgp = from_timer(mgp, t, watchdog_timer);
3477

B
Brice Goglin 已提交
3478
	rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
3479
	busy_slice_cnt = 0;
B
Brice Goglin 已提交
3480 3481
	for (i = 0, reset_needed = 0;
	     i < mgp->num_slices && reset_needed == 0; ++i) {
3482

B
Brice Goglin 已提交
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498
		ss = &mgp->ss[i];
		if (ss->rx_small.watchdog_needed) {
			myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
						mgp->small_bytes + MXGEFW_PAD,
						1);
			if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
			    myri10ge_fill_thresh)
				ss->rx_small.watchdog_needed = 0;
		}
		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 >=
			    myri10ge_fill_thresh)
				ss->rx_big.watchdog_needed = 0;
		}
3499 3500
		myri10ge_check_slice(ss, &reset_needed, &busy_slice_cnt,
				     rx_pause_cnt);
3501 3502 3503 3504 3505 3506 3507 3508 3509
	}
	/* if we've sent or received no traffic, poll the NIC to
	 * ensure it is still there.  Otherwise, we risk not noticing
	 * an error in a timely fashion */
	if (busy_slice_cnt == 0) {
		pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
		if ((cmd & PCI_COMMAND_MASTER) == 0) {
			reset_needed = 1;
		}
3510 3511
	}
	mgp->watchdog_pause = rx_pause_cnt;
B
Brice Goglin 已提交
3512 3513 3514 3515 3516 3517 3518 3519

	if (reset_needed) {
		schedule_work(&mgp->watchdog_work);
	} else {
		/* rearm timer */
		mod_timer(&mgp->watchdog_timer,
			  jiffies + myri10ge_watchdog_timeout * HZ);
	}
3520 3521
}

3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
static void myri10ge_free_slices(struct myri10ge_priv *mgp)
{
	struct myri10ge_slice_state *ss;
	struct pci_dev *pdev = mgp->pdev;
	size_t bytes;
	int i;

	if (mgp->ss == NULL)
		return;

	for (i = 0; i < mgp->num_slices; i++) {
		ss = &mgp->ss[i];
		if (ss->rx_done.entry != NULL) {
			bytes = mgp->max_intr_slots *
			    sizeof(*ss->rx_done.entry);
			dma_free_coherent(&pdev->dev, bytes,
					  ss->rx_done.entry, ss->rx_done.bus);
			ss->rx_done.entry = NULL;
		}
		if (ss->fw_stats != NULL) {
			bytes = sizeof(*ss->fw_stats);
			dma_free_coherent(&pdev->dev, bytes,
					  ss->fw_stats, ss->fw_stats_bus);
			ss->fw_stats = NULL;
		}
3547
		__netif_napi_del(&ss->napi);
3548
	}
3549
	/* Wait till napi structs are no longer used, and then free ss. */
3550
	synchronize_net();
3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570
	kfree(mgp->ss);
	mgp->ss = NULL;
}

static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
{
	struct myri10ge_slice_state *ss;
	struct pci_dev *pdev = mgp->pdev;
	size_t bytes;
	int i;

	bytes = sizeof(*mgp->ss) * mgp->num_slices;
	mgp->ss = kzalloc(bytes, GFP_KERNEL);
	if (mgp->ss == NULL) {
		return -ENOMEM;
	}

	for (i = 0; i < mgp->num_slices; i++) {
		ss = &mgp->ss[i];
		bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
3571 3572 3573
		ss->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
						       &ss->rx_done.bus,
						       GFP_KERNEL);
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
		if (ss->rx_done.entry == NULL)
			goto abort;
		bytes = sizeof(*ss->fw_stats);
		ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
						  &ss->fw_stats_bus,
						  GFP_KERNEL);
		if (ss->fw_stats == NULL)
			goto abort;
		ss->mgp = mgp;
		ss->dev = mgp->dev;
		netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
			       myri10ge_napi_weight);
	}
	return 0;
abort:
	myri10ge_free_slices(mgp);
	return -ENOMEM;
}

/*
 * This function determines the number of slices supported.
L
Lucas De Marchi 已提交
3595
 * The number slices is the minimum of the number of CPUS,
3596 3597 3598 3599 3600 3601 3602 3603
 * the number of MSI-X irqs supported, the number of slices
 * supported by the firmware
 */
static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
{
	struct myri10ge_cmd cmd;
	struct pci_dev *pdev = mgp->pdev;
	char *old_fw;
3604
	bool old_allocated;
3605
	int i, status, ncpus;
3606 3607

	mgp->num_slices = 1;
3608
	ncpus = netif_get_num_default_rss_queues();
3609

3610
	if (myri10ge_max_slices == 1 || !pdev->msix_cap ||
3611 3612 3613 3614 3615
	    (myri10ge_max_slices == -1 && ncpus < 2))
		return;

	/* try to load the slice aware rss firmware */
	old_fw = mgp->fw_name;
3616 3617 3618 3619
	old_allocated = mgp->fw_name_allocated;
	/* don't free old_fw if we override it. */
	mgp->fw_name_allocated = false;

3620 3621 3622
	if (myri10ge_fw_name != NULL) {
		dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
			 myri10ge_fw_name);
3623
		set_fw_name(mgp, myri10ge_fw_name, false);
3624
	} else if (old_fw == myri10ge_fw_aligned)
3625
		set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
3626
	else
3627
		set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
3628 3629 3630
	status = myri10ge_load_firmware(mgp, 0);
	if (status != 0) {
		dev_info(&pdev->dev, "Rss firmware not found\n");
3631 3632
		if (old_allocated)
			kfree(old_fw);
3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
		return;
	}

	/* hit the board with a reset to ensure 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");
		goto abort_with_fw;
	}

	mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);

	/* tell it the size of the interrupt queues */
	cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
	if (status != 0) {
		dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
		goto abort_with_fw;
	}

	/* ask the maximum number of slices it supports */
	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
	if (status != 0)
		goto abort_with_fw;
	else
		mgp->num_slices = cmd.data0;

	/* Only allow multiple slices if MSI-X is usable */
	if (!myri10ge_msi) {
		goto abort_with_fw;
	}

	/* if the admin did not specify a limit to how many
	 * slices we should use, cap it automatically to the
	 * number of CPUs currently online */
	if (myri10ge_max_slices == -1)
		myri10ge_max_slices = ncpus;

	if (mgp->num_slices > myri10ge_max_slices)
		mgp->num_slices = myri10ge_max_slices;

	/* Now try to allocate as many MSI-X vectors as we have
	 * slices. We give up on MSI-X if we can only get a single
	 * vector. */

3679 3680
	mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
				    GFP_KERNEL);
3681
	if (mgp->msix_vectors == NULL)
3682
		goto no_msix;
3683 3684 3685 3686 3687
	for (i = 0; i < mgp->num_slices; i++) {
		mgp->msix_vectors[i].entry = i;
	}

	while (mgp->num_slices > 1) {
3688
		mgp->num_slices = rounddown_pow_of_two(mgp->num_slices);
3689
		if (mgp->num_slices == 1)
3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700
			goto no_msix;
		status = pci_enable_msix_range(pdev,
					       mgp->msix_vectors,
					       mgp->num_slices,
					       mgp->num_slices);
		if (status < 0)
			goto no_msix;

		pci_disable_msix(pdev);

		if (status == mgp->num_slices) {
3701 3702
			if (old_allocated)
				kfree(old_fw);
3703
			return;
3704
		} else {
3705
			mgp->num_slices = status;
3706
		}
3707 3708
	}

3709
no_msix:
3710 3711 3712 3713 3714 3715 3716
	if (mgp->msix_vectors != NULL) {
		kfree(mgp->msix_vectors);
		mgp->msix_vectors = NULL;
	}

abort_with_fw:
	mgp->num_slices = 1;
3717
	set_fw_name(mgp, old_fw, old_allocated);
3718 3719 3720
	myri10ge_load_firmware(mgp, 0);
}

3721 3722 3723 3724
static const struct net_device_ops myri10ge_netdev_ops = {
	.ndo_open		= myri10ge_open,
	.ndo_stop		= myri10ge_close,
	.ndo_start_xmit		= myri10ge_xmit,
3725
	.ndo_get_stats64	= myri10ge_get_stats,
3726 3727
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_change_mtu		= myri10ge_change_mtu,
3728
	.ndo_set_rx_mode	= myri10ge_set_multicast_list,
3729 3730 3731
	.ndo_set_mac_address	= myri10ge_set_mac_address,
};

3732 3733 3734 3735 3736 3737 3738 3739
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;
	int i;
	int status = -ENXIO;
	int dac_enabled;
3740
	unsigned hdr_offset, ss_offset;
3741
	static int board_number;
3742

B
Brice Goglin 已提交
3743
	netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
3744
	if (netdev == NULL)
3745 3746
		return -ENOMEM;

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

3749 3750 3751 3752 3753
	mgp = netdev_priv(netdev);
	mgp->dev = netdev;
	mgp->pdev = pdev;
	mgp->pause = myri10ge_flow_control;
	mgp->intr_coal_delay = myri10ge_intr_coal_delay;
3754
	mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
3755
	mgp->board_number = board_number;
3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769
	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 已提交
3770
	status = pcie_set_readrq(pdev, 4096);
3771 3772 3773
	if (status != 0) {
		dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
			status);
3774
		goto abort_with_enabled;
3775 3776
	}

3777
	myri10ge_mask_surprise_down(pdev);
3778 3779
	pci_set_master(pdev);
	dac_enabled = 1;
3780
	status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3781 3782 3783
	if (status != 0) {
		dac_enabled = 0;
		dev_err(&pdev->dev,
3784 3785
			"64-bit pci address mask was refused, trying 32-bit\n");
		status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3786 3787 3788
	}
	if (status != 0) {
		dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
3789
		goto abort_with_enabled;
3790
	}
B
Brice Goglin 已提交
3791 3792
	mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
				      &mgp->cmd_bus, GFP_KERNEL);
J
Julia Lawall 已提交
3793 3794
	if (!mgp->cmd) {
		status = -ENOMEM;
3795
		goto abort_with_enabled;
J
Julia Lawall 已提交
3796
	}
3797 3798 3799

	mgp->board_span = pci_resource_len(pdev, 0);
	mgp->iomem_base = pci_resource_start(pdev, 0);
3800
	mgp->wc_cookie = arch_phys_wc_add(mgp->iomem_base, mgp->board_span);
B
Brice Goglin 已提交
3801
	mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
3802 3803 3804 3805
	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;
B
Brice Goglin 已提交
3806
		goto abort_with_mtrr;
3807
	}
3808
	hdr_offset =
3809
	    swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
3810
	ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
3811
	mgp->sram_size = swab32(readl(mgp->sram + ss_offset));
3812 3813 3814 3815 3816
	if (mgp->sram_size > mgp->board_span ||
	    mgp->sram_size <= MYRI10GE_FW_OFFSET) {
		dev_err(&pdev->dev,
			"invalid sram_size %dB or board span %ldB\n",
			mgp->sram_size, mgp->board_span);
3817
		status = -EINVAL;
3818 3819
		goto abort_with_ioremap;
	}
3820
	memcpy_fromio(mgp->eeprom_strings,
3821
		      mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
3822 3823 3824 3825 3826 3827 3828 3829
	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];

3830 3831
	myri10ge_select_firmware(mgp);

B
Brice Goglin 已提交
3832
	status = myri10ge_load_firmware(mgp, 1);
3833 3834
	if (status != 0) {
		dev_err(&pdev->dev, "failed to load firmware\n");
B
Brice Goglin 已提交
3835 3836 3837 3838 3839 3840 3841
		goto abort_with_ioremap;
	}
	myri10ge_probe_slices(mgp);
	status = myri10ge_alloc_slices(mgp);
	if (status != 0) {
		dev_err(&pdev->dev, "failed to alloc slice state\n");
		goto abort_with_firmware;
3842
	}
3843 3844
	netif_set_real_num_tx_queues(netdev, mgp->num_slices);
	netif_set_real_num_rx_queues(netdev, mgp->num_slices);
3845 3846 3847
	status = myri10ge_reset(mgp);
	if (status != 0) {
		dev_err(&pdev->dev, "failed reset\n");
B
Brice Goglin 已提交
3848
		goto abort_with_slices;
3849
	}
3850
#ifdef CONFIG_MYRI10GE_DCA
3851 3852
	myri10ge_setup_dca(mgp);
#endif
3853
	pci_set_drvdata(pdev, mgp);
3854

3855 3856 3857 3858 3859 3860 3861 3862 3863
	/* MTU range: 68 - 9000 */
	netdev->min_mtu = ETH_MIN_MTU;
	netdev->max_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;

	if (myri10ge_initial_mtu > netdev->max_mtu)
		myri10ge_initial_mtu = netdev->max_mtu;
	if (myri10ge_initial_mtu < netdev->min_mtu)
		myri10ge_initial_mtu = netdev->min_mtu;

3864
	netdev->mtu = myri10ge_initial_mtu;
3865 3866

	netdev->netdev_ops = &myri10ge_netdev_ops;
3867
	netdev->hw_features = mgp->features | NETIF_F_RXCSUM;
3868

3869 3870
	/* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
	netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3871

3872
	netdev->features = netdev->hw_features;
B
Brice Goglin 已提交
3873

3874 3875 3876
	if (dac_enabled)
		netdev->features |= NETIF_F_HIGHDMA;

3877 3878 3879 3880 3881 3882
	netdev->vlan_features |= mgp->features;
	if (mgp->fw_ver_tiny < 37)
		netdev->vlan_features &= ~NETIF_F_TSO6;
	if (mgp->fw_ver_tiny < 32)
		netdev->vlan_features &= ~NETIF_F_TSO;

3883
	/* make sure we can get an irq, and that MSI can be
3884
	 * setup (if available). */
3885 3886
	status = myri10ge_request_irq(mgp);
	if (status != 0)
W
Wenwen Wang 已提交
3887
		goto abort_with_slices;
3888 3889
	myri10ge_free_irq(mgp);

3890 3891
	/* Save configuration space to be restored if the
	 * nic resets due to a parity error */
3892
	pci_save_state(pdev);
3893 3894

	/* Setup the watchdog timer */
3895
	timer_setup(&mgp->watchdog_timer, myri10ge_watchdog_timer, 0);
3896

3897
	netdev->ethtool_ops = &myri10ge_ethtool_ops;
D
David Howells 已提交
3898
	INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
3899 3900 3901
	status = register_netdev(netdev);
	if (status != 0) {
		dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
3902
		goto abort_with_state;
3903
	}
B
Brice Goglin 已提交
3904
	if (mgp->msix_enabled)
3905
		dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
B
Brice Goglin 已提交
3906
			 mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
3907
			 (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
B
Brice Goglin 已提交
3908
	else
3909
		dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
B
Brice Goglin 已提交
3910
			 mgp->msi_enabled ? "MSI" : "xPIC",
3911
			 pdev->irq, mgp->tx_boundary, mgp->fw_name,
3912
			 (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
3913

3914
	board_number++;
3915 3916
	return 0;

3917
abort_with_state:
3918
	pci_restore_state(pdev);
3919

B
Brice Goglin 已提交
3920 3921 3922
abort_with_slices:
	myri10ge_free_slices(mgp);

3923 3924 3925 3926
abort_with_firmware:
	myri10ge_dummy_rdma(mgp, 0);

abort_with_ioremap:
3927 3928 3929 3930
	if (mgp->mac_addr_string != NULL)
		dev_err(&pdev->dev,
			"myri10ge_probe() failed: MAC=%s, SN=%ld\n",
			mgp->mac_addr_string, mgp->serial_number);
3931 3932
	iounmap(mgp->sram);

B
Brice Goglin 已提交
3933
abort_with_mtrr:
3934
	arch_phys_wc_del(mgp->wc_cookie);
B
Brice Goglin 已提交
3935 3936
	dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
			  mgp->cmd, mgp->cmd_bus);
3937

3938 3939
abort_with_enabled:
	pci_disable_device(pdev);
3940

3941
abort_with_netdev:
3942
	set_fw_name(mgp, NULL, false);
3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962
	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;

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

3963
	cancel_work_sync(&mgp->watchdog_work);
3964 3965 3966
	netdev = mgp->dev;
	unregister_netdev(netdev);

3967
#ifdef CONFIG_MYRI10GE_DCA
3968 3969
	myri10ge_teardown_dca(mgp);
#endif
3970 3971
	myri10ge_dummy_rdma(mgp, 0);

3972
	/* avoid a memory leak */
3973
	pci_restore_state(pdev);
3974

3975
	iounmap(mgp->sram);
3976
	arch_phys_wc_del(mgp->wc_cookie);
B
Brice Goglin 已提交
3977
	myri10ge_free_slices(mgp);
3978
	kfree(mgp->msix_vectors);
B
Brice Goglin 已提交
3979 3980
	dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
			  mgp->cmd, mgp->cmd_bus);
3981

3982
	set_fw_name(mgp, NULL, false);
3983
	free_netdev(netdev);
3984
	pci_disable_device(pdev);
3985 3986
}

B
Brice Goglin 已提交
3987
#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 	0x0008
3988
#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9	0x0009
3989

3990
static const struct pci_device_id myri10ge_pci_tbl[] = {
B
Brice Goglin 已提交
3991
	{PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
3992 3993
	{PCI_DEVICE
	 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
3994 3995 3996
	{0},
};

B
Brice Goglin 已提交
3997 3998
MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);

3999 4000
static SIMPLE_DEV_PM_OPS(myri10ge_pm_ops, myri10ge_suspend, myri10ge_resume);

4001 4002 4003 4004 4005
static struct pci_driver myri10ge_driver = {
	.name = "myri10ge",
	.probe = myri10ge_probe,
	.remove = myri10ge_remove,
	.id_table = myri10ge_pci_tbl,
4006
	.driver.pm = &myri10ge_pm_ops,
4007 4008
};

4009
#ifdef CONFIG_MYRI10GE_DCA
4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026
static int
myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
{
	int err = driver_for_each_device(&myri10ge_driver.driver,
					 NULL, &event,
					 myri10ge_notify_dca_device);

	if (err)
		return NOTIFY_BAD;
	return NOTIFY_DONE;
}

static struct notifier_block myri10ge_dca_notifier = {
	.notifier_call = myri10ge_notify_dca,
	.next = NULL,
	.priority = 0,
};
B
Brice Goglin 已提交
4027
#endif				/* CONFIG_MYRI10GE_DCA */
4028

4029 4030
static __init int myri10ge_init_module(void)
{
4031
	pr_info("Version %s\n", MYRI10GE_VERSION_STR);
B
Brice Goglin 已提交
4032

B
Brice Goglin 已提交
4033
	if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
4034 4035
		pr_err("Illegal rssh hash type %d, defaulting to source port\n",
		       myri10ge_rss_hash);
B
Brice Goglin 已提交
4036 4037
		myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
	}
4038
#ifdef CONFIG_MYRI10GE_DCA
4039 4040
	dca_register_notify(&myri10ge_dca_notifier);
#endif
B
Brice Goglin 已提交
4041 4042
	if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
		myri10ge_max_slices = MYRI10GE_MAX_SLICES;
B
Brice Goglin 已提交
4043

4044 4045 4046 4047 4048 4049 4050
	return pci_register_driver(&myri10ge_driver);
}

module_init(myri10ge_init_module);

static __exit void myri10ge_cleanup_module(void)
{
4051
#ifdef CONFIG_MYRI10GE_DCA
4052 4053
	dca_unregister_notify(&myri10ge_dca_notifier);
#endif
4054 4055 4056 4057
	pci_unregister_driver(&myri10ge_driver);
}

module_exit(myri10ge_cleanup_module);