xref: /openbmc/linux/drivers/net/ethernet/myricom/myri10ge/myri10ge.c (revision d3757ba4c1421d2ad00d2bf97119005e37ad2902)
193f7848bSJeff Kirsher /*************************************************************************
293f7848bSJeff Kirsher  * myri10ge.c: Myricom Myri-10G Ethernet driver.
393f7848bSJeff Kirsher  *
493f7848bSJeff Kirsher  * Copyright (C) 2005 - 2011 Myricom, Inc.
593f7848bSJeff Kirsher  * All rights reserved.
693f7848bSJeff Kirsher  *
793f7848bSJeff Kirsher  * Redistribution and use in source and binary forms, with or without
893f7848bSJeff Kirsher  * modification, are permitted provided that the following conditions
993f7848bSJeff Kirsher  * are met:
1093f7848bSJeff Kirsher  * 1. Redistributions of source code must retain the above copyright
1193f7848bSJeff Kirsher  *    notice, this list of conditions and the following disclaimer.
1293f7848bSJeff Kirsher  * 2. Redistributions in binary form must reproduce the above copyright
1393f7848bSJeff Kirsher  *    notice, this list of conditions and the following disclaimer in the
1493f7848bSJeff Kirsher  *    documentation and/or other materials provided with the distribution.
1593f7848bSJeff Kirsher  * 3. Neither the name of Myricom, Inc. nor the names of its contributors
1693f7848bSJeff Kirsher  *    may be used to endorse or promote products derived from this software
1793f7848bSJeff Kirsher  *    without specific prior written permission.
1893f7848bSJeff Kirsher  *
1993f7848bSJeff Kirsher  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2093f7848bSJeff Kirsher  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2193f7848bSJeff Kirsher  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2293f7848bSJeff Kirsher  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2393f7848bSJeff Kirsher  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2493f7848bSJeff Kirsher  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2593f7848bSJeff Kirsher  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2693f7848bSJeff Kirsher  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2793f7848bSJeff Kirsher  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2893f7848bSJeff Kirsher  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2993f7848bSJeff Kirsher  * POSSIBILITY OF SUCH DAMAGE.
3093f7848bSJeff Kirsher  *
3193f7848bSJeff Kirsher  *
3293f7848bSJeff Kirsher  * If the eeprom on your board is not recent enough, you will need to get a
3393f7848bSJeff Kirsher  * newer firmware image at:
3493f7848bSJeff Kirsher  *   http://www.myri.com/scs/download-Myri10GE.html
3593f7848bSJeff Kirsher  *
3693f7848bSJeff Kirsher  * Contact Information:
3793f7848bSJeff Kirsher  *   <help@myri.com>
3893f7848bSJeff Kirsher  *   Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
3993f7848bSJeff Kirsher  *************************************************************************/
4093f7848bSJeff Kirsher 
4193f7848bSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4293f7848bSJeff Kirsher 
4393f7848bSJeff Kirsher #include <linux/tcp.h>
4493f7848bSJeff Kirsher #include <linux/netdevice.h>
4593f7848bSJeff Kirsher #include <linux/skbuff.h>
4693f7848bSJeff Kirsher #include <linux/string.h>
4793f7848bSJeff Kirsher #include <linux/module.h>
4893f7848bSJeff Kirsher #include <linux/pci.h>
4993f7848bSJeff Kirsher #include <linux/dma-mapping.h>
5093f7848bSJeff Kirsher #include <linux/etherdevice.h>
5193f7848bSJeff Kirsher #include <linux/if_ether.h>
5293f7848bSJeff Kirsher #include <linux/if_vlan.h>
5393f7848bSJeff Kirsher #include <linux/dca.h>
5493f7848bSJeff Kirsher #include <linux/ip.h>
5593f7848bSJeff Kirsher #include <linux/inet.h>
5693f7848bSJeff Kirsher #include <linux/in.h>
5793f7848bSJeff Kirsher #include <linux/ethtool.h>
5893f7848bSJeff Kirsher #include <linux/firmware.h>
5993f7848bSJeff Kirsher #include <linux/delay.h>
6093f7848bSJeff Kirsher #include <linux/timer.h>
6193f7848bSJeff Kirsher #include <linux/vmalloc.h>
6293f7848bSJeff Kirsher #include <linux/crc32.h>
6393f7848bSJeff Kirsher #include <linux/moduleparam.h>
6493f7848bSJeff Kirsher #include <linux/io.h>
6593f7848bSJeff Kirsher #include <linux/log2.h>
6693f7848bSJeff Kirsher #include <linux/slab.h>
6793f7848bSJeff Kirsher #include <linux/prefetch.h>
6893f7848bSJeff Kirsher #include <net/checksum.h>
6993f7848bSJeff Kirsher #include <net/ip.h>
7093f7848bSJeff Kirsher #include <net/tcp.h>
7193f7848bSJeff Kirsher #include <asm/byteorder.h>
7293f7848bSJeff Kirsher #include <asm/processor.h>
730dde8026SHyong-Youb Kim #include <net/busy_poll.h>
7493f7848bSJeff Kirsher 
7593f7848bSJeff Kirsher #include "myri10ge_mcp.h"
7693f7848bSJeff Kirsher #include "myri10ge_mcp_gen_header.h"
7793f7848bSJeff Kirsher 
7893f7848bSJeff Kirsher #define MYRI10GE_VERSION_STR "1.5.3-1.534"
7993f7848bSJeff Kirsher 
8093f7848bSJeff Kirsher MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
8193f7848bSJeff Kirsher MODULE_AUTHOR("Maintainer: help@myri.com");
8293f7848bSJeff Kirsher MODULE_VERSION(MYRI10GE_VERSION_STR);
8393f7848bSJeff Kirsher MODULE_LICENSE("Dual BSD/GPL");
8493f7848bSJeff Kirsher 
8593f7848bSJeff Kirsher #define MYRI10GE_MAX_ETHER_MTU 9014
8693f7848bSJeff Kirsher 
8793f7848bSJeff Kirsher #define MYRI10GE_ETH_STOPPED 0
8893f7848bSJeff Kirsher #define MYRI10GE_ETH_STOPPING 1
8993f7848bSJeff Kirsher #define MYRI10GE_ETH_STARTING 2
9093f7848bSJeff Kirsher #define MYRI10GE_ETH_RUNNING 3
9193f7848bSJeff Kirsher #define MYRI10GE_ETH_OPEN_FAILED 4
9293f7848bSJeff Kirsher 
9393f7848bSJeff Kirsher #define MYRI10GE_EEPROM_STRINGS_SIZE 256
9493f7848bSJeff Kirsher #define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
9593f7848bSJeff Kirsher 
9693f7848bSJeff Kirsher #define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
9793f7848bSJeff Kirsher #define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
9893f7848bSJeff Kirsher 
9993f7848bSJeff Kirsher #define MYRI10GE_ALLOC_ORDER 0
10093f7848bSJeff Kirsher #define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
10193f7848bSJeff Kirsher #define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
10293f7848bSJeff Kirsher 
10393f7848bSJeff Kirsher #define MYRI10GE_MAX_SLICES 32
10493f7848bSJeff Kirsher 
10593f7848bSJeff Kirsher struct myri10ge_rx_buffer_state {
10693f7848bSJeff Kirsher 	struct page *page;
10793f7848bSJeff Kirsher 	int page_offset;
10893f7848bSJeff Kirsher 	DEFINE_DMA_UNMAP_ADDR(bus);
10993f7848bSJeff Kirsher 	DEFINE_DMA_UNMAP_LEN(len);
11093f7848bSJeff Kirsher };
11193f7848bSJeff Kirsher 
11293f7848bSJeff Kirsher struct myri10ge_tx_buffer_state {
11393f7848bSJeff Kirsher 	struct sk_buff *skb;
11493f7848bSJeff Kirsher 	int last;
11593f7848bSJeff Kirsher 	DEFINE_DMA_UNMAP_ADDR(bus);
11693f7848bSJeff Kirsher 	DEFINE_DMA_UNMAP_LEN(len);
11793f7848bSJeff Kirsher };
11893f7848bSJeff Kirsher 
11993f7848bSJeff Kirsher struct myri10ge_cmd {
12093f7848bSJeff Kirsher 	u32 data0;
12193f7848bSJeff Kirsher 	u32 data1;
12293f7848bSJeff Kirsher 	u32 data2;
12393f7848bSJeff Kirsher };
12493f7848bSJeff Kirsher 
12593f7848bSJeff Kirsher struct myri10ge_rx_buf {
12693f7848bSJeff Kirsher 	struct mcp_kreq_ether_recv __iomem *lanai;	/* lanai ptr for recv ring */
12793f7848bSJeff Kirsher 	struct mcp_kreq_ether_recv *shadow;	/* host shadow of recv ring */
12893f7848bSJeff Kirsher 	struct myri10ge_rx_buffer_state *info;
12993f7848bSJeff Kirsher 	struct page *page;
13093f7848bSJeff Kirsher 	dma_addr_t bus;
13193f7848bSJeff Kirsher 	int page_offset;
13293f7848bSJeff Kirsher 	int cnt;
13393f7848bSJeff Kirsher 	int fill_cnt;
13493f7848bSJeff Kirsher 	int alloc_fail;
13593f7848bSJeff Kirsher 	int mask;		/* number of rx slots -1 */
13693f7848bSJeff Kirsher 	int watchdog_needed;
13793f7848bSJeff Kirsher };
13893f7848bSJeff Kirsher 
13993f7848bSJeff Kirsher struct myri10ge_tx_buf {
14093f7848bSJeff Kirsher 	struct mcp_kreq_ether_send __iomem *lanai;	/* lanai ptr for sendq */
14193f7848bSJeff Kirsher 	__be32 __iomem *send_go;	/* "go" doorbell ptr */
14293f7848bSJeff Kirsher 	__be32 __iomem *send_stop;	/* "stop" doorbell ptr */
14393f7848bSJeff Kirsher 	struct mcp_kreq_ether_send *req_list;	/* host shadow of sendq */
14493f7848bSJeff Kirsher 	char *req_bytes;
14593f7848bSJeff Kirsher 	struct myri10ge_tx_buffer_state *info;
14693f7848bSJeff Kirsher 	int mask;		/* number of transmit slots -1  */
14793f7848bSJeff Kirsher 	int req ____cacheline_aligned;	/* transmit slots submitted     */
14893f7848bSJeff Kirsher 	int pkt_start;		/* packets started */
14993f7848bSJeff Kirsher 	int stop_queue;
15093f7848bSJeff Kirsher 	int linearized;
15193f7848bSJeff Kirsher 	int done ____cacheline_aligned;	/* transmit slots completed     */
15293f7848bSJeff Kirsher 	int pkt_done;		/* packets completed */
15393f7848bSJeff Kirsher 	int wake_queue;
15493f7848bSJeff Kirsher 	int queue_active;
15593f7848bSJeff Kirsher };
15693f7848bSJeff Kirsher 
15793f7848bSJeff Kirsher struct myri10ge_rx_done {
15893f7848bSJeff Kirsher 	struct mcp_slot *entry;
15993f7848bSJeff Kirsher 	dma_addr_t bus;
16093f7848bSJeff Kirsher 	int cnt;
16193f7848bSJeff Kirsher 	int idx;
16293f7848bSJeff Kirsher };
16393f7848bSJeff Kirsher 
16493f7848bSJeff Kirsher struct myri10ge_slice_netstats {
16593f7848bSJeff Kirsher 	unsigned long rx_packets;
16693f7848bSJeff Kirsher 	unsigned long tx_packets;
16793f7848bSJeff Kirsher 	unsigned long rx_bytes;
16893f7848bSJeff Kirsher 	unsigned long tx_bytes;
16993f7848bSJeff Kirsher 	unsigned long rx_dropped;
17093f7848bSJeff Kirsher 	unsigned long tx_dropped;
17193f7848bSJeff Kirsher };
17293f7848bSJeff Kirsher 
17393f7848bSJeff Kirsher struct myri10ge_slice_state {
17493f7848bSJeff Kirsher 	struct myri10ge_tx_buf tx;	/* transmit ring        */
17593f7848bSJeff Kirsher 	struct myri10ge_rx_buf rx_small;
17693f7848bSJeff Kirsher 	struct myri10ge_rx_buf rx_big;
17793f7848bSJeff Kirsher 	struct myri10ge_rx_done rx_done;
17893f7848bSJeff Kirsher 	struct net_device *dev;
17993f7848bSJeff Kirsher 	struct napi_struct napi;
18093f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
18193f7848bSJeff Kirsher 	struct myri10ge_slice_netstats stats;
18293f7848bSJeff Kirsher 	__be32 __iomem *irq_claim;
18393f7848bSJeff Kirsher 	struct mcp_irq_data *fw_stats;
18493f7848bSJeff Kirsher 	dma_addr_t fw_stats_bus;
18593f7848bSJeff Kirsher 	int watchdog_tx_done;
18693f7848bSJeff Kirsher 	int watchdog_tx_req;
18793f7848bSJeff Kirsher 	int watchdog_rx_done;
18893f7848bSJeff Kirsher 	int stuck;
18993f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
19093f7848bSJeff Kirsher 	int cached_dca_tag;
19193f7848bSJeff Kirsher 	int cpu;
19293f7848bSJeff Kirsher 	__be32 __iomem *dca_tag;
19393f7848bSJeff Kirsher #endif
19493f7848bSJeff Kirsher 	char irq_desc[32];
19593f7848bSJeff Kirsher };
19693f7848bSJeff Kirsher 
19793f7848bSJeff Kirsher struct myri10ge_priv {
19893f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
19993f7848bSJeff Kirsher 	int tx_boundary;	/* boundary transmits cannot cross */
20093f7848bSJeff Kirsher 	int num_slices;
20193f7848bSJeff Kirsher 	int running;		/* running?             */
20293f7848bSJeff Kirsher 	int small_bytes;
20393f7848bSJeff Kirsher 	int big_bytes;
20493f7848bSJeff Kirsher 	int max_intr_slots;
20593f7848bSJeff Kirsher 	struct net_device *dev;
20693f7848bSJeff Kirsher 	u8 __iomem *sram;
20793f7848bSJeff Kirsher 	int sram_size;
20893f7848bSJeff Kirsher 	unsigned long board_span;
20993f7848bSJeff Kirsher 	unsigned long iomem_base;
21093f7848bSJeff Kirsher 	__be32 __iomem *irq_deassert;
21193f7848bSJeff Kirsher 	char *mac_addr_string;
21293f7848bSJeff Kirsher 	struct mcp_cmd_response *cmd;
21393f7848bSJeff Kirsher 	dma_addr_t cmd_bus;
21493f7848bSJeff Kirsher 	struct pci_dev *pdev;
21593f7848bSJeff Kirsher 	int msi_enabled;
21693f7848bSJeff Kirsher 	int msix_enabled;
21793f7848bSJeff Kirsher 	struct msix_entry *msix_vectors;
21893f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
21993f7848bSJeff Kirsher 	int dca_enabled;
22093f7848bSJeff Kirsher 	int relaxed_order;
22193f7848bSJeff Kirsher #endif
22293f7848bSJeff Kirsher 	u32 link_state;
22393f7848bSJeff Kirsher 	unsigned int rdma_tags_available;
22493f7848bSJeff Kirsher 	int intr_coal_delay;
22593f7848bSJeff Kirsher 	__be32 __iomem *intr_coal_delay_ptr;
226e4b6c303SLuis R. Rodriguez 	int wc_cookie;
22793f7848bSJeff Kirsher 	int down_cnt;
22893f7848bSJeff Kirsher 	wait_queue_head_t down_wq;
22993f7848bSJeff Kirsher 	struct work_struct watchdog_work;
23093f7848bSJeff Kirsher 	struct timer_list watchdog_timer;
23193f7848bSJeff Kirsher 	int watchdog_resets;
23293f7848bSJeff Kirsher 	int watchdog_pause;
23393f7848bSJeff Kirsher 	int pause;
23493f7848bSJeff Kirsher 	bool fw_name_allocated;
23593f7848bSJeff Kirsher 	char *fw_name;
23693f7848bSJeff Kirsher 	char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
23793f7848bSJeff Kirsher 	char *product_code_string;
23893f7848bSJeff Kirsher 	char fw_version[128];
23993f7848bSJeff Kirsher 	int fw_ver_major;
24093f7848bSJeff Kirsher 	int fw_ver_minor;
24193f7848bSJeff Kirsher 	int fw_ver_tiny;
24293f7848bSJeff Kirsher 	int adopted_rx_filter_bug;
2431409a932SJoe Perches 	u8 mac_addr[ETH_ALEN];		/* eeprom mac address */
24493f7848bSJeff Kirsher 	unsigned long serial_number;
24593f7848bSJeff Kirsher 	int vendor_specific_offset;
24693f7848bSJeff Kirsher 	int fw_multicast_support;
24793f7848bSJeff Kirsher 	u32 features;
24893f7848bSJeff Kirsher 	u32 max_tso6;
24993f7848bSJeff Kirsher 	u32 read_dma;
25093f7848bSJeff Kirsher 	u32 write_dma;
25193f7848bSJeff Kirsher 	u32 read_write_dma;
25293f7848bSJeff Kirsher 	u32 link_changes;
25393f7848bSJeff Kirsher 	u32 msg_enable;
25493f7848bSJeff Kirsher 	unsigned int board_number;
25593f7848bSJeff Kirsher 	int rebooted;
25693f7848bSJeff Kirsher };
25793f7848bSJeff Kirsher 
25893f7848bSJeff Kirsher static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
25993f7848bSJeff Kirsher static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
26093f7848bSJeff Kirsher static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
26193f7848bSJeff Kirsher static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
26293f7848bSJeff Kirsher MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
26393f7848bSJeff Kirsher MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
26493f7848bSJeff Kirsher MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
26593f7848bSJeff Kirsher MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
26693f7848bSJeff Kirsher 
267b51d23e4SDan Streetman /* Careful: must be accessed under kernel_param_lock() */
26893f7848bSJeff Kirsher static char *myri10ge_fw_name = NULL;
269*d3757ba4SJoe Perches module_param(myri10ge_fw_name, charp, 0644);
27093f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
27193f7848bSJeff Kirsher 
27293f7848bSJeff Kirsher #define MYRI10GE_MAX_BOARDS 8
27393f7848bSJeff Kirsher static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
27493f7848bSJeff Kirsher     {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
27593f7848bSJeff Kirsher module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
27693f7848bSJeff Kirsher 			 0444);
2771a61a8f1SWei Yongjun MODULE_PARM_DESC(myri10ge_fw_names, "Firmware image names per board");
27893f7848bSJeff Kirsher 
27993f7848bSJeff Kirsher static int myri10ge_ecrc_enable = 1;
280*d3757ba4SJoe Perches module_param(myri10ge_ecrc_enable, int, 0444);
28193f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
28293f7848bSJeff Kirsher 
28393f7848bSJeff Kirsher static int myri10ge_small_bytes = -1;	/* -1 == auto */
284*d3757ba4SJoe Perches module_param(myri10ge_small_bytes, int, 0644);
28593f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
28693f7848bSJeff Kirsher 
28793f7848bSJeff Kirsher static int myri10ge_msi = 1;	/* enable msi by default */
288*d3757ba4SJoe Perches module_param(myri10ge_msi, int, 0644);
28993f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
29093f7848bSJeff Kirsher 
29193f7848bSJeff Kirsher static int myri10ge_intr_coal_delay = 75;
292*d3757ba4SJoe Perches module_param(myri10ge_intr_coal_delay, int, 0444);
29393f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
29493f7848bSJeff Kirsher 
29593f7848bSJeff Kirsher static int myri10ge_flow_control = 1;
296*d3757ba4SJoe Perches module_param(myri10ge_flow_control, int, 0444);
29793f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
29893f7848bSJeff Kirsher 
29993f7848bSJeff Kirsher static int myri10ge_deassert_wait = 1;
300*d3757ba4SJoe Perches module_param(myri10ge_deassert_wait, int, 0644);
30193f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_deassert_wait,
30293f7848bSJeff Kirsher 		 "Wait when deasserting legacy interrupts");
30393f7848bSJeff Kirsher 
30493f7848bSJeff Kirsher static int myri10ge_force_firmware = 0;
305*d3757ba4SJoe Perches module_param(myri10ge_force_firmware, int, 0444);
30693f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_force_firmware,
30793f7848bSJeff Kirsher 		 "Force firmware to assume aligned completions");
30893f7848bSJeff Kirsher 
30993f7848bSJeff Kirsher static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
310*d3757ba4SJoe Perches module_param(myri10ge_initial_mtu, int, 0444);
31193f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
31293f7848bSJeff Kirsher 
31393f7848bSJeff Kirsher static int myri10ge_napi_weight = 64;
314*d3757ba4SJoe Perches module_param(myri10ge_napi_weight, int, 0444);
31593f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
31693f7848bSJeff Kirsher 
31793f7848bSJeff Kirsher static int myri10ge_watchdog_timeout = 1;
318*d3757ba4SJoe Perches module_param(myri10ge_watchdog_timeout, int, 0444);
31993f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
32093f7848bSJeff Kirsher 
32193f7848bSJeff Kirsher static int myri10ge_max_irq_loops = 1048576;
322*d3757ba4SJoe Perches module_param(myri10ge_max_irq_loops, int, 0444);
32393f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_max_irq_loops,
32493f7848bSJeff Kirsher 		 "Set stuck legacy IRQ detection threshold");
32593f7848bSJeff Kirsher 
32693f7848bSJeff Kirsher #define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
32793f7848bSJeff Kirsher 
32893f7848bSJeff Kirsher static int myri10ge_debug = -1;	/* defaults above */
32993f7848bSJeff Kirsher module_param(myri10ge_debug, int, 0);
33093f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
33193f7848bSJeff Kirsher 
33293f7848bSJeff Kirsher static int myri10ge_fill_thresh = 256;
333*d3757ba4SJoe Perches module_param(myri10ge_fill_thresh, int, 0644);
33493f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
33593f7848bSJeff Kirsher 
33693f7848bSJeff Kirsher static int myri10ge_reset_recover = 1;
33793f7848bSJeff Kirsher 
33893f7848bSJeff Kirsher static int myri10ge_max_slices = 1;
339*d3757ba4SJoe Perches module_param(myri10ge_max_slices, int, 0444);
34093f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
34193f7848bSJeff Kirsher 
34293f7848bSJeff Kirsher static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
343*d3757ba4SJoe Perches module_param(myri10ge_rss_hash, int, 0444);
34493f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
34593f7848bSJeff Kirsher 
34693f7848bSJeff Kirsher static int myri10ge_dca = 1;
347*d3757ba4SJoe Perches module_param(myri10ge_dca, int, 0444);
34893f7848bSJeff Kirsher MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
34993f7848bSJeff Kirsher 
35093f7848bSJeff Kirsher #define MYRI10GE_FW_OFFSET 1024*1024
35193f7848bSJeff Kirsher #define MYRI10GE_HIGHPART_TO_U32(X) \
35293f7848bSJeff Kirsher (sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
35393f7848bSJeff Kirsher #define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
35493f7848bSJeff Kirsher 
35593f7848bSJeff Kirsher #define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
35693f7848bSJeff Kirsher 
35793f7848bSJeff Kirsher static void myri10ge_set_multicast_list(struct net_device *dev);
35893f7848bSJeff Kirsher static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
35993f7848bSJeff Kirsher 					 struct net_device *dev);
36093f7848bSJeff Kirsher 
36193f7848bSJeff Kirsher static inline void put_be32(__be32 val, __be32 __iomem * p)
36293f7848bSJeff Kirsher {
36393f7848bSJeff Kirsher 	__raw_writel((__force __u32) val, (__force void __iomem *)p);
36493f7848bSJeff Kirsher }
36593f7848bSJeff Kirsher 
366bc1f4470Sstephen hemminger static void myri10ge_get_stats(struct net_device *dev,
36793f7848bSJeff Kirsher 			       struct rtnl_link_stats64 *stats);
36893f7848bSJeff Kirsher 
36993f7848bSJeff Kirsher static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
37093f7848bSJeff Kirsher {
37193f7848bSJeff Kirsher 	if (mgp->fw_name_allocated)
37293f7848bSJeff Kirsher 		kfree(mgp->fw_name);
37393f7848bSJeff Kirsher 	mgp->fw_name = name;
37493f7848bSJeff Kirsher 	mgp->fw_name_allocated = allocated;
37593f7848bSJeff Kirsher }
37693f7848bSJeff Kirsher 
37793f7848bSJeff Kirsher static int
37893f7848bSJeff Kirsher myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
37993f7848bSJeff Kirsher 		  struct myri10ge_cmd *data, int atomic)
38093f7848bSJeff Kirsher {
38193f7848bSJeff Kirsher 	struct mcp_cmd *buf;
38293f7848bSJeff Kirsher 	char buf_bytes[sizeof(*buf) + 8];
38393f7848bSJeff Kirsher 	struct mcp_cmd_response *response = mgp->cmd;
38493f7848bSJeff Kirsher 	char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
38593f7848bSJeff Kirsher 	u32 dma_low, dma_high, result, value;
38693f7848bSJeff Kirsher 	int sleep_total = 0;
38793f7848bSJeff Kirsher 
38893f7848bSJeff Kirsher 	/* ensure buf is aligned to 8 bytes */
38993f7848bSJeff Kirsher 	buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
39093f7848bSJeff Kirsher 
39193f7848bSJeff Kirsher 	buf->data0 = htonl(data->data0);
39293f7848bSJeff Kirsher 	buf->data1 = htonl(data->data1);
39393f7848bSJeff Kirsher 	buf->data2 = htonl(data->data2);
39493f7848bSJeff Kirsher 	buf->cmd = htonl(cmd);
39593f7848bSJeff Kirsher 	dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
39693f7848bSJeff Kirsher 	dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
39793f7848bSJeff Kirsher 
39893f7848bSJeff Kirsher 	buf->response_addr.low = htonl(dma_low);
39993f7848bSJeff Kirsher 	buf->response_addr.high = htonl(dma_high);
40093f7848bSJeff Kirsher 	response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
40193f7848bSJeff Kirsher 	mb();
40293f7848bSJeff Kirsher 	myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
40393f7848bSJeff Kirsher 
40493f7848bSJeff Kirsher 	/* wait up to 15ms. Longest command is the DMA benchmark,
40593f7848bSJeff Kirsher 	 * which is capped at 5ms, but runs from a timeout handler
40693f7848bSJeff Kirsher 	 * that runs every 7.8ms. So a 15ms timeout leaves us with
40793f7848bSJeff Kirsher 	 * a 2.2ms margin
40893f7848bSJeff Kirsher 	 */
40993f7848bSJeff Kirsher 	if (atomic) {
41093f7848bSJeff Kirsher 		/* if atomic is set, do not sleep,
41193f7848bSJeff Kirsher 		 * and try to get the completion quickly
41293f7848bSJeff Kirsher 		 * (1ms will be enough for those commands) */
41393f7848bSJeff Kirsher 		for (sleep_total = 0;
41493f7848bSJeff Kirsher 		     sleep_total < 1000 &&
41593f7848bSJeff Kirsher 		     response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
41693f7848bSJeff Kirsher 		     sleep_total += 10) {
41793f7848bSJeff Kirsher 			udelay(10);
41893f7848bSJeff Kirsher 			mb();
41993f7848bSJeff Kirsher 		}
42093f7848bSJeff Kirsher 	} else {
42193f7848bSJeff Kirsher 		/* use msleep for most command */
42293f7848bSJeff Kirsher 		for (sleep_total = 0;
42393f7848bSJeff Kirsher 		     sleep_total < 15 &&
42493f7848bSJeff Kirsher 		     response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
42593f7848bSJeff Kirsher 		     sleep_total++)
42693f7848bSJeff Kirsher 			msleep(1);
42793f7848bSJeff Kirsher 	}
42893f7848bSJeff Kirsher 
42993f7848bSJeff Kirsher 	result = ntohl(response->result);
43093f7848bSJeff Kirsher 	value = ntohl(response->data);
43193f7848bSJeff Kirsher 	if (result != MYRI10GE_NO_RESPONSE_RESULT) {
43293f7848bSJeff Kirsher 		if (result == 0) {
43393f7848bSJeff Kirsher 			data->data0 = value;
43493f7848bSJeff Kirsher 			return 0;
43593f7848bSJeff Kirsher 		} else if (result == MXGEFW_CMD_UNKNOWN) {
43693f7848bSJeff Kirsher 			return -ENOSYS;
43793f7848bSJeff Kirsher 		} else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
43893f7848bSJeff Kirsher 			return -E2BIG;
43993f7848bSJeff Kirsher 		} else if (result == MXGEFW_CMD_ERROR_RANGE &&
44093f7848bSJeff Kirsher 			   cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
44193f7848bSJeff Kirsher 			   (data->
44293f7848bSJeff Kirsher 			    data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
44393f7848bSJeff Kirsher 			   0) {
44493f7848bSJeff Kirsher 			return -ERANGE;
44593f7848bSJeff Kirsher 		} else {
44693f7848bSJeff Kirsher 			dev_err(&mgp->pdev->dev,
44793f7848bSJeff Kirsher 				"command %d failed, result = %d\n",
44893f7848bSJeff Kirsher 				cmd, result);
44993f7848bSJeff Kirsher 			return -ENXIO;
45093f7848bSJeff Kirsher 		}
45193f7848bSJeff Kirsher 	}
45293f7848bSJeff Kirsher 
45393f7848bSJeff Kirsher 	dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
45493f7848bSJeff Kirsher 		cmd, result);
45593f7848bSJeff Kirsher 	return -EAGAIN;
45693f7848bSJeff Kirsher }
45793f7848bSJeff Kirsher 
45893f7848bSJeff Kirsher /*
45993f7848bSJeff Kirsher  * The eeprom strings on the lanaiX have the format
46093f7848bSJeff Kirsher  * SN=x\0
46193f7848bSJeff Kirsher  * MAC=x:x:x:x:x:x\0
46293f7848bSJeff Kirsher  * PT:ddd mmm xx xx:xx:xx xx\0
46393f7848bSJeff Kirsher  * PV:ddd mmm xx xx:xx:xx xx\0
46493f7848bSJeff Kirsher  */
46593f7848bSJeff Kirsher static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
46693f7848bSJeff Kirsher {
46793f7848bSJeff Kirsher 	char *ptr, *limit;
46893f7848bSJeff Kirsher 	int i;
46993f7848bSJeff Kirsher 
47093f7848bSJeff Kirsher 	ptr = mgp->eeprom_strings;
47193f7848bSJeff Kirsher 	limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
47293f7848bSJeff Kirsher 
47393f7848bSJeff Kirsher 	while (*ptr != '\0' && ptr < limit) {
47493f7848bSJeff Kirsher 		if (memcmp(ptr, "MAC=", 4) == 0) {
47593f7848bSJeff Kirsher 			ptr += 4;
47693f7848bSJeff Kirsher 			mgp->mac_addr_string = ptr;
47793f7848bSJeff Kirsher 			for (i = 0; i < 6; i++) {
47893f7848bSJeff Kirsher 				if ((ptr + 2) > limit)
47993f7848bSJeff Kirsher 					goto abort;
48093f7848bSJeff Kirsher 				mgp->mac_addr[i] =
48193f7848bSJeff Kirsher 				    simple_strtoul(ptr, &ptr, 16);
48293f7848bSJeff Kirsher 				ptr += 1;
48393f7848bSJeff Kirsher 			}
48493f7848bSJeff Kirsher 		}
48593f7848bSJeff Kirsher 		if (memcmp(ptr, "PC=", 3) == 0) {
48693f7848bSJeff Kirsher 			ptr += 3;
48793f7848bSJeff Kirsher 			mgp->product_code_string = ptr;
48893f7848bSJeff Kirsher 		}
48993f7848bSJeff Kirsher 		if (memcmp((const void *)ptr, "SN=", 3) == 0) {
49093f7848bSJeff Kirsher 			ptr += 3;
49193f7848bSJeff Kirsher 			mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
49293f7848bSJeff Kirsher 		}
49393f7848bSJeff Kirsher 		while (ptr < limit && *ptr++) ;
49493f7848bSJeff Kirsher 	}
49593f7848bSJeff Kirsher 
49693f7848bSJeff Kirsher 	return 0;
49793f7848bSJeff Kirsher 
49893f7848bSJeff Kirsher abort:
49993f7848bSJeff Kirsher 	dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
50093f7848bSJeff Kirsher 	return -ENXIO;
50193f7848bSJeff Kirsher }
50293f7848bSJeff Kirsher 
50393f7848bSJeff Kirsher /*
50493f7848bSJeff Kirsher  * Enable or disable periodic RDMAs from the host to make certain
50593f7848bSJeff Kirsher  * chipsets resend dropped PCIe messages
50693f7848bSJeff Kirsher  */
50793f7848bSJeff Kirsher 
50893f7848bSJeff Kirsher static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
50993f7848bSJeff Kirsher {
51093f7848bSJeff Kirsher 	char __iomem *submit;
51193f7848bSJeff Kirsher 	__be32 buf[16] __attribute__ ((__aligned__(8)));
51293f7848bSJeff Kirsher 	u32 dma_low, dma_high;
51393f7848bSJeff Kirsher 	int i;
51493f7848bSJeff Kirsher 
51593f7848bSJeff Kirsher 	/* clear confirmation addr */
51693f7848bSJeff Kirsher 	mgp->cmd->data = 0;
51793f7848bSJeff Kirsher 	mb();
51893f7848bSJeff Kirsher 
51993f7848bSJeff Kirsher 	/* send a rdma command to the PCIe engine, and wait for the
52093f7848bSJeff Kirsher 	 * response in the confirmation address.  The firmware should
52193f7848bSJeff Kirsher 	 * write a -1 there to indicate it is alive and well
52293f7848bSJeff Kirsher 	 */
52393f7848bSJeff Kirsher 	dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
52493f7848bSJeff Kirsher 	dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
52593f7848bSJeff Kirsher 
52693f7848bSJeff Kirsher 	buf[0] = htonl(dma_high);	/* confirm addr MSW */
52793f7848bSJeff Kirsher 	buf[1] = htonl(dma_low);	/* confirm addr LSW */
52893f7848bSJeff Kirsher 	buf[2] = MYRI10GE_NO_CONFIRM_DATA;	/* confirm data */
52993f7848bSJeff Kirsher 	buf[3] = htonl(dma_high);	/* dummy addr MSW */
53093f7848bSJeff Kirsher 	buf[4] = htonl(dma_low);	/* dummy addr LSW */
53193f7848bSJeff Kirsher 	buf[5] = htonl(enable);	/* enable? */
53293f7848bSJeff Kirsher 
53393f7848bSJeff Kirsher 	submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
53493f7848bSJeff Kirsher 
53593f7848bSJeff Kirsher 	myri10ge_pio_copy(submit, &buf, sizeof(buf));
53693f7848bSJeff Kirsher 	for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
53793f7848bSJeff Kirsher 		msleep(1);
53893f7848bSJeff Kirsher 	if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
53993f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
54093f7848bSJeff Kirsher 			(enable ? "enable" : "disable"));
54193f7848bSJeff Kirsher }
54293f7848bSJeff Kirsher 
54393f7848bSJeff Kirsher static int
54493f7848bSJeff Kirsher myri10ge_validate_firmware(struct myri10ge_priv *mgp,
54593f7848bSJeff Kirsher 			   struct mcp_gen_header *hdr)
54693f7848bSJeff Kirsher {
54793f7848bSJeff Kirsher 	struct device *dev = &mgp->pdev->dev;
54893f7848bSJeff Kirsher 
54993f7848bSJeff Kirsher 	/* check firmware type */
55093f7848bSJeff Kirsher 	if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
55193f7848bSJeff Kirsher 		dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
55293f7848bSJeff Kirsher 		return -EINVAL;
55393f7848bSJeff Kirsher 	}
55493f7848bSJeff Kirsher 
55593f7848bSJeff Kirsher 	/* save firmware version for ethtool */
55693f7848bSJeff Kirsher 	strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
5578dff81e8SRickard Strandqvist 	mgp->fw_version[sizeof(mgp->fw_version) - 1] = '\0';
55893f7848bSJeff Kirsher 
55993f7848bSJeff Kirsher 	sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
56093f7848bSJeff Kirsher 	       &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
56193f7848bSJeff Kirsher 
56293f7848bSJeff Kirsher 	if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
56393f7848bSJeff Kirsher 	      mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
56493f7848bSJeff Kirsher 		dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
56593f7848bSJeff Kirsher 		dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
56693f7848bSJeff Kirsher 			MXGEFW_VERSION_MINOR);
56793f7848bSJeff Kirsher 		return -EINVAL;
56893f7848bSJeff Kirsher 	}
56993f7848bSJeff Kirsher 	return 0;
57093f7848bSJeff Kirsher }
57193f7848bSJeff Kirsher 
57293f7848bSJeff Kirsher static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
57393f7848bSJeff Kirsher {
57493f7848bSJeff Kirsher 	unsigned crc, reread_crc;
57593f7848bSJeff Kirsher 	const struct firmware *fw;
57693f7848bSJeff Kirsher 	struct device *dev = &mgp->pdev->dev;
57793f7848bSJeff Kirsher 	unsigned char *fw_readback;
57893f7848bSJeff Kirsher 	struct mcp_gen_header *hdr;
57993f7848bSJeff Kirsher 	size_t hdr_offset;
58093f7848bSJeff Kirsher 	int status;
58193f7848bSJeff Kirsher 	unsigned i;
58293f7848bSJeff Kirsher 
58393f7848bSJeff Kirsher 	if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
58493f7848bSJeff Kirsher 		dev_err(dev, "Unable to load %s firmware image via hotplug\n",
58593f7848bSJeff Kirsher 			mgp->fw_name);
58693f7848bSJeff Kirsher 		status = -EINVAL;
58793f7848bSJeff Kirsher 		goto abort_with_nothing;
58893f7848bSJeff Kirsher 	}
58993f7848bSJeff Kirsher 
59093f7848bSJeff Kirsher 	/* check size */
59193f7848bSJeff Kirsher 
59293f7848bSJeff Kirsher 	if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
59393f7848bSJeff Kirsher 	    fw->size < MCP_HEADER_PTR_OFFSET + 4) {
59493f7848bSJeff Kirsher 		dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
59593f7848bSJeff Kirsher 		status = -EINVAL;
59693f7848bSJeff Kirsher 		goto abort_with_fw;
59793f7848bSJeff Kirsher 	}
59893f7848bSJeff Kirsher 
59993f7848bSJeff Kirsher 	/* check id */
60093f7848bSJeff Kirsher 	hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
60193f7848bSJeff Kirsher 	if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
60293f7848bSJeff Kirsher 		dev_err(dev, "Bad firmware file\n");
60393f7848bSJeff Kirsher 		status = -EINVAL;
60493f7848bSJeff Kirsher 		goto abort_with_fw;
60593f7848bSJeff Kirsher 	}
60693f7848bSJeff Kirsher 	hdr = (void *)(fw->data + hdr_offset);
60793f7848bSJeff Kirsher 
60893f7848bSJeff Kirsher 	status = myri10ge_validate_firmware(mgp, hdr);
60993f7848bSJeff Kirsher 	if (status != 0)
61093f7848bSJeff Kirsher 		goto abort_with_fw;
61193f7848bSJeff Kirsher 
61293f7848bSJeff Kirsher 	crc = crc32(~0, fw->data, fw->size);
61393f7848bSJeff Kirsher 	for (i = 0; i < fw->size; i += 256) {
61493f7848bSJeff Kirsher 		myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
61593f7848bSJeff Kirsher 				  fw->data + i,
61693f7848bSJeff Kirsher 				  min(256U, (unsigned)(fw->size - i)));
61793f7848bSJeff Kirsher 		mb();
61893f7848bSJeff Kirsher 		readb(mgp->sram);
61993f7848bSJeff Kirsher 	}
62093f7848bSJeff Kirsher 	fw_readback = vmalloc(fw->size);
62193f7848bSJeff Kirsher 	if (!fw_readback) {
62293f7848bSJeff Kirsher 		status = -ENOMEM;
62393f7848bSJeff Kirsher 		goto abort_with_fw;
62493f7848bSJeff Kirsher 	}
62593f7848bSJeff Kirsher 	/* corruption checking is good for parity recovery and buggy chipset */
62693f7848bSJeff Kirsher 	memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
62793f7848bSJeff Kirsher 	reread_crc = crc32(~0, fw_readback, fw->size);
62893f7848bSJeff Kirsher 	vfree(fw_readback);
62993f7848bSJeff Kirsher 	if (crc != reread_crc) {
63093f7848bSJeff Kirsher 		dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
63193f7848bSJeff Kirsher 			(unsigned)fw->size, reread_crc, crc);
63293f7848bSJeff Kirsher 		status = -EIO;
63393f7848bSJeff Kirsher 		goto abort_with_fw;
63493f7848bSJeff Kirsher 	}
63593f7848bSJeff Kirsher 	*size = (u32) fw->size;
63693f7848bSJeff Kirsher 
63793f7848bSJeff Kirsher abort_with_fw:
63893f7848bSJeff Kirsher 	release_firmware(fw);
63993f7848bSJeff Kirsher 
64093f7848bSJeff Kirsher abort_with_nothing:
64193f7848bSJeff Kirsher 	return status;
64293f7848bSJeff Kirsher }
64393f7848bSJeff Kirsher 
64493f7848bSJeff Kirsher static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
64593f7848bSJeff Kirsher {
64693f7848bSJeff Kirsher 	struct mcp_gen_header *hdr;
64793f7848bSJeff Kirsher 	struct device *dev = &mgp->pdev->dev;
64893f7848bSJeff Kirsher 	const size_t bytes = sizeof(struct mcp_gen_header);
64993f7848bSJeff Kirsher 	size_t hdr_offset;
65093f7848bSJeff Kirsher 	int status;
65193f7848bSJeff Kirsher 
65293f7848bSJeff Kirsher 	/* find running firmware header */
65393f7848bSJeff Kirsher 	hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
65493f7848bSJeff Kirsher 
65593f7848bSJeff Kirsher 	if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
65693f7848bSJeff Kirsher 		dev_err(dev, "Running firmware has bad header offset (%d)\n",
65793f7848bSJeff Kirsher 			(int)hdr_offset);
65893f7848bSJeff Kirsher 		return -EIO;
65993f7848bSJeff Kirsher 	}
66093f7848bSJeff Kirsher 
66193f7848bSJeff Kirsher 	/* copy header of running firmware from SRAM to host memory to
66293f7848bSJeff Kirsher 	 * validate firmware */
66393f7848bSJeff Kirsher 	hdr = kmalloc(bytes, GFP_KERNEL);
664b2adaca9SJoe Perches 	if (hdr == NULL)
66593f7848bSJeff Kirsher 		return -ENOMEM;
666b2adaca9SJoe Perches 
66793f7848bSJeff Kirsher 	memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
66893f7848bSJeff Kirsher 	status = myri10ge_validate_firmware(mgp, hdr);
66993f7848bSJeff Kirsher 	kfree(hdr);
67093f7848bSJeff Kirsher 
67193f7848bSJeff Kirsher 	/* check to see if adopted firmware has bug where adopting
67293f7848bSJeff Kirsher 	 * it will cause broadcasts to be filtered unless the NIC
67393f7848bSJeff Kirsher 	 * is kept in ALLMULTI mode */
67493f7848bSJeff Kirsher 	if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
67593f7848bSJeff Kirsher 	    mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
67693f7848bSJeff Kirsher 		mgp->adopted_rx_filter_bug = 1;
67793f7848bSJeff Kirsher 		dev_warn(dev, "Adopting fw %d.%d.%d: "
67893f7848bSJeff Kirsher 			 "working around rx filter bug\n",
67993f7848bSJeff Kirsher 			 mgp->fw_ver_major, mgp->fw_ver_minor,
68093f7848bSJeff Kirsher 			 mgp->fw_ver_tiny);
68193f7848bSJeff Kirsher 	}
68293f7848bSJeff Kirsher 	return status;
68393f7848bSJeff Kirsher }
68493f7848bSJeff Kirsher 
68593f7848bSJeff Kirsher static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
68693f7848bSJeff Kirsher {
68793f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
68893f7848bSJeff Kirsher 	int status;
68993f7848bSJeff Kirsher 
69093f7848bSJeff Kirsher 	/* probe for IPv6 TSO support */
69193f7848bSJeff Kirsher 	mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
69293f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
69393f7848bSJeff Kirsher 				   &cmd, 0);
69493f7848bSJeff Kirsher 	if (status == 0) {
69593f7848bSJeff Kirsher 		mgp->max_tso6 = cmd.data0;
69693f7848bSJeff Kirsher 		mgp->features |= NETIF_F_TSO6;
69793f7848bSJeff Kirsher 	}
69893f7848bSJeff Kirsher 
69993f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
70093f7848bSJeff Kirsher 	if (status != 0) {
70193f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev,
70293f7848bSJeff Kirsher 			"failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
70393f7848bSJeff Kirsher 		return -ENXIO;
70493f7848bSJeff Kirsher 	}
70593f7848bSJeff Kirsher 
70693f7848bSJeff Kirsher 	mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
70793f7848bSJeff Kirsher 
70893f7848bSJeff Kirsher 	return 0;
70993f7848bSJeff Kirsher }
71093f7848bSJeff Kirsher 
71193f7848bSJeff Kirsher static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
71293f7848bSJeff Kirsher {
71393f7848bSJeff Kirsher 	char __iomem *submit;
71493f7848bSJeff Kirsher 	__be32 buf[16] __attribute__ ((__aligned__(8)));
71593f7848bSJeff Kirsher 	u32 dma_low, dma_high, size;
71693f7848bSJeff Kirsher 	int status, i;
71793f7848bSJeff Kirsher 
71893f7848bSJeff Kirsher 	size = 0;
71993f7848bSJeff Kirsher 	status = myri10ge_load_hotplug_firmware(mgp, &size);
72093f7848bSJeff Kirsher 	if (status) {
72193f7848bSJeff Kirsher 		if (!adopt)
72293f7848bSJeff Kirsher 			return status;
72393f7848bSJeff Kirsher 		dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
72493f7848bSJeff Kirsher 
72593f7848bSJeff Kirsher 		/* Do not attempt to adopt firmware if there
72693f7848bSJeff Kirsher 		 * was a bad crc */
72793f7848bSJeff Kirsher 		if (status == -EIO)
72893f7848bSJeff Kirsher 			return status;
72993f7848bSJeff Kirsher 
73093f7848bSJeff Kirsher 		status = myri10ge_adopt_running_firmware(mgp);
73193f7848bSJeff Kirsher 		if (status != 0) {
73293f7848bSJeff Kirsher 			dev_err(&mgp->pdev->dev,
73393f7848bSJeff Kirsher 				"failed to adopt running firmware\n");
73493f7848bSJeff Kirsher 			return status;
73593f7848bSJeff Kirsher 		}
73693f7848bSJeff Kirsher 		dev_info(&mgp->pdev->dev,
73793f7848bSJeff Kirsher 			 "Successfully adopted running firmware\n");
73893f7848bSJeff Kirsher 		if (mgp->tx_boundary == 4096) {
73993f7848bSJeff Kirsher 			dev_warn(&mgp->pdev->dev,
74093f7848bSJeff Kirsher 				 "Using firmware currently running on NIC"
74193f7848bSJeff Kirsher 				 ".  For optimal\n");
74293f7848bSJeff Kirsher 			dev_warn(&mgp->pdev->dev,
74393f7848bSJeff Kirsher 				 "performance consider loading optimized "
74493f7848bSJeff Kirsher 				 "firmware\n");
74593f7848bSJeff Kirsher 			dev_warn(&mgp->pdev->dev, "via hotplug\n");
74693f7848bSJeff Kirsher 		}
74793f7848bSJeff Kirsher 
74893f7848bSJeff Kirsher 		set_fw_name(mgp, "adopted", false);
74993f7848bSJeff Kirsher 		mgp->tx_boundary = 2048;
75093f7848bSJeff Kirsher 		myri10ge_dummy_rdma(mgp, 1);
75193f7848bSJeff Kirsher 		status = myri10ge_get_firmware_capabilities(mgp);
75293f7848bSJeff Kirsher 		return status;
75393f7848bSJeff Kirsher 	}
75493f7848bSJeff Kirsher 
75593f7848bSJeff Kirsher 	/* clear confirmation addr */
75693f7848bSJeff Kirsher 	mgp->cmd->data = 0;
75793f7848bSJeff Kirsher 	mb();
75893f7848bSJeff Kirsher 
75993f7848bSJeff Kirsher 	/* send a reload command to the bootstrap MCP, and wait for the
76093f7848bSJeff Kirsher 	 *  response in the confirmation address.  The firmware should
76193f7848bSJeff Kirsher 	 * write a -1 there to indicate it is alive and well
76293f7848bSJeff Kirsher 	 */
76393f7848bSJeff Kirsher 	dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
76493f7848bSJeff Kirsher 	dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
76593f7848bSJeff Kirsher 
76693f7848bSJeff Kirsher 	buf[0] = htonl(dma_high);	/* confirm addr MSW */
76793f7848bSJeff Kirsher 	buf[1] = htonl(dma_low);	/* confirm addr LSW */
76893f7848bSJeff Kirsher 	buf[2] = MYRI10GE_NO_CONFIRM_DATA;	/* confirm data */
76993f7848bSJeff Kirsher 
77093f7848bSJeff Kirsher 	/* FIX: All newest firmware should un-protect the bottom of
77193f7848bSJeff Kirsher 	 * the sram before handoff. However, the very first interfaces
77293f7848bSJeff Kirsher 	 * do not. Therefore the handoff copy must skip the first 8 bytes
77393f7848bSJeff Kirsher 	 */
77493f7848bSJeff Kirsher 	buf[3] = htonl(MYRI10GE_FW_OFFSET + 8);	/* where the code starts */
77593f7848bSJeff Kirsher 	buf[4] = htonl(size - 8);	/* length of code */
77693f7848bSJeff Kirsher 	buf[5] = htonl(8);	/* where to copy to */
77793f7848bSJeff Kirsher 	buf[6] = htonl(0);	/* where to jump to */
77893f7848bSJeff Kirsher 
77993f7848bSJeff Kirsher 	submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
78093f7848bSJeff Kirsher 
78193f7848bSJeff Kirsher 	myri10ge_pio_copy(submit, &buf, sizeof(buf));
78293f7848bSJeff Kirsher 	mb();
78393f7848bSJeff Kirsher 	msleep(1);
78493f7848bSJeff Kirsher 	mb();
78593f7848bSJeff Kirsher 	i = 0;
78693f7848bSJeff Kirsher 	while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
78793f7848bSJeff Kirsher 		msleep(1 << i);
78893f7848bSJeff Kirsher 		i++;
78993f7848bSJeff Kirsher 	}
79093f7848bSJeff Kirsher 	if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
79193f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev, "handoff failed\n");
79293f7848bSJeff Kirsher 		return -ENXIO;
79393f7848bSJeff Kirsher 	}
79493f7848bSJeff Kirsher 	myri10ge_dummy_rdma(mgp, 1);
79593f7848bSJeff Kirsher 	status = myri10ge_get_firmware_capabilities(mgp);
79693f7848bSJeff Kirsher 
79793f7848bSJeff Kirsher 	return status;
79893f7848bSJeff Kirsher }
79993f7848bSJeff Kirsher 
80093f7848bSJeff Kirsher static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
80193f7848bSJeff Kirsher {
80293f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
80393f7848bSJeff Kirsher 	int status;
80493f7848bSJeff Kirsher 
80593f7848bSJeff Kirsher 	cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
80693f7848bSJeff Kirsher 		     | (addr[2] << 8) | addr[3]);
80793f7848bSJeff Kirsher 
80893f7848bSJeff Kirsher 	cmd.data1 = ((addr[4] << 8) | (addr[5]));
80993f7848bSJeff Kirsher 
81093f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
81193f7848bSJeff Kirsher 	return status;
81293f7848bSJeff Kirsher }
81393f7848bSJeff Kirsher 
81493f7848bSJeff Kirsher static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
81593f7848bSJeff Kirsher {
81693f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
81793f7848bSJeff Kirsher 	int status, ctl;
81893f7848bSJeff Kirsher 
81993f7848bSJeff Kirsher 	ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
82093f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
82193f7848bSJeff Kirsher 
82293f7848bSJeff Kirsher 	if (status) {
82393f7848bSJeff Kirsher 		netdev_err(mgp->dev, "Failed to set flow control mode\n");
82493f7848bSJeff Kirsher 		return status;
82593f7848bSJeff Kirsher 	}
82693f7848bSJeff Kirsher 	mgp->pause = pause;
82793f7848bSJeff Kirsher 	return 0;
82893f7848bSJeff Kirsher }
82993f7848bSJeff Kirsher 
83093f7848bSJeff Kirsher static void
83193f7848bSJeff Kirsher myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
83293f7848bSJeff Kirsher {
83393f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
83493f7848bSJeff Kirsher 	int status, ctl;
83593f7848bSJeff Kirsher 
83693f7848bSJeff Kirsher 	ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
83793f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
83893f7848bSJeff Kirsher 	if (status)
83993f7848bSJeff Kirsher 		netdev_err(mgp->dev, "Failed to set promisc mode\n");
84093f7848bSJeff Kirsher }
84193f7848bSJeff Kirsher 
84293f7848bSJeff Kirsher static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
84393f7848bSJeff Kirsher {
84493f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
84593f7848bSJeff Kirsher 	int status;
84693f7848bSJeff Kirsher 	u32 len;
84793f7848bSJeff Kirsher 	struct page *dmatest_page;
84893f7848bSJeff Kirsher 	dma_addr_t dmatest_bus;
84993f7848bSJeff Kirsher 	char *test = " ";
85093f7848bSJeff Kirsher 
85193f7848bSJeff Kirsher 	dmatest_page = alloc_page(GFP_KERNEL);
85293f7848bSJeff Kirsher 	if (!dmatest_page)
85393f7848bSJeff Kirsher 		return -ENOMEM;
85493f7848bSJeff Kirsher 	dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
85593f7848bSJeff Kirsher 				   DMA_BIDIRECTIONAL);
85610545937SStanislaw Gruszka 	if (unlikely(pci_dma_mapping_error(mgp->pdev, dmatest_bus))) {
85710545937SStanislaw Gruszka 		__free_page(dmatest_page);
85810545937SStanislaw Gruszka 		return -ENOMEM;
85910545937SStanislaw Gruszka 	}
86093f7848bSJeff Kirsher 
86193f7848bSJeff Kirsher 	/* Run a small DMA test.
86293f7848bSJeff Kirsher 	 * The magic multipliers to the length tell the firmware
86393f7848bSJeff Kirsher 	 * to do DMA read, write, or read+write tests.  The
86493f7848bSJeff Kirsher 	 * results are returned in cmd.data0.  The upper 16
86593f7848bSJeff Kirsher 	 * bits or the return is the number of transfers completed.
86693f7848bSJeff Kirsher 	 * The lower 16 bits is the time in 0.5us ticks that the
86793f7848bSJeff Kirsher 	 * transfers took to complete.
86893f7848bSJeff Kirsher 	 */
86993f7848bSJeff Kirsher 
87093f7848bSJeff Kirsher 	len = mgp->tx_boundary;
87193f7848bSJeff Kirsher 
87293f7848bSJeff Kirsher 	cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
87393f7848bSJeff Kirsher 	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
87493f7848bSJeff Kirsher 	cmd.data2 = len * 0x10000;
87593f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
87693f7848bSJeff Kirsher 	if (status != 0) {
87793f7848bSJeff Kirsher 		test = "read";
87893f7848bSJeff Kirsher 		goto abort;
87993f7848bSJeff Kirsher 	}
88093f7848bSJeff Kirsher 	mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
88193f7848bSJeff Kirsher 	cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
88293f7848bSJeff Kirsher 	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
88393f7848bSJeff Kirsher 	cmd.data2 = len * 0x1;
88493f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
88593f7848bSJeff Kirsher 	if (status != 0) {
88693f7848bSJeff Kirsher 		test = "write";
88793f7848bSJeff Kirsher 		goto abort;
88893f7848bSJeff Kirsher 	}
88993f7848bSJeff Kirsher 	mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
89093f7848bSJeff Kirsher 
89193f7848bSJeff Kirsher 	cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
89293f7848bSJeff Kirsher 	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
89393f7848bSJeff Kirsher 	cmd.data2 = len * 0x10001;
89493f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
89593f7848bSJeff Kirsher 	if (status != 0) {
89693f7848bSJeff Kirsher 		test = "read/write";
89793f7848bSJeff Kirsher 		goto abort;
89893f7848bSJeff Kirsher 	}
89993f7848bSJeff Kirsher 	mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
90093f7848bSJeff Kirsher 	    (cmd.data0 & 0xffff);
90193f7848bSJeff Kirsher 
90293f7848bSJeff Kirsher abort:
90393f7848bSJeff Kirsher 	pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
90493f7848bSJeff Kirsher 	put_page(dmatest_page);
90593f7848bSJeff Kirsher 
90693f7848bSJeff Kirsher 	if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
90793f7848bSJeff Kirsher 		dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
90893f7848bSJeff Kirsher 			 test, status);
90993f7848bSJeff Kirsher 
91093f7848bSJeff Kirsher 	return status;
91193f7848bSJeff Kirsher }
91293f7848bSJeff Kirsher 
91393f7848bSJeff Kirsher static int myri10ge_reset(struct myri10ge_priv *mgp)
91493f7848bSJeff Kirsher {
91593f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
91693f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
91793f7848bSJeff Kirsher 	int i, status;
91893f7848bSJeff Kirsher 	size_t bytes;
91993f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
92093f7848bSJeff Kirsher 	unsigned long dca_tag_off;
92193f7848bSJeff Kirsher #endif
92293f7848bSJeff Kirsher 
92393f7848bSJeff Kirsher 	/* try to send a reset command to the card to see if it
92493f7848bSJeff Kirsher 	 * is alive */
92593f7848bSJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
92693f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
92793f7848bSJeff Kirsher 	if (status != 0) {
92893f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev, "failed reset\n");
92993f7848bSJeff Kirsher 		return -ENXIO;
93093f7848bSJeff Kirsher 	}
93193f7848bSJeff Kirsher 
93293f7848bSJeff Kirsher 	(void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
93393f7848bSJeff Kirsher 	/*
93493f7848bSJeff Kirsher 	 * Use non-ndis mcp_slot (eg, 4 bytes total,
93593f7848bSJeff Kirsher 	 * no toeplitz hash value returned.  Older firmware will
93693f7848bSJeff Kirsher 	 * not understand this command, but will use the correct
93793f7848bSJeff Kirsher 	 * sized mcp_slot, so we ignore error returns
93893f7848bSJeff Kirsher 	 */
93993f7848bSJeff Kirsher 	cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
94093f7848bSJeff Kirsher 	(void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
94193f7848bSJeff Kirsher 
94293f7848bSJeff Kirsher 	/* Now exchange information about interrupts  */
94393f7848bSJeff Kirsher 
94493f7848bSJeff Kirsher 	bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
94593f7848bSJeff Kirsher 	cmd.data0 = (u32) bytes;
94693f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
94793f7848bSJeff Kirsher 
94893f7848bSJeff Kirsher 	/*
94993f7848bSJeff Kirsher 	 * Even though we already know how many slices are supported
95093f7848bSJeff Kirsher 	 * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
95193f7848bSJeff Kirsher 	 * has magic side effects, and must be called after a reset.
95293f7848bSJeff Kirsher 	 * It must be called prior to calling any RSS related cmds,
95393f7848bSJeff Kirsher 	 * including assigning an interrupt queue for anything but
95493f7848bSJeff Kirsher 	 * slice 0.  It must also be called *after*
95593f7848bSJeff Kirsher 	 * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
95693f7848bSJeff Kirsher 	 * the firmware to compute offsets.
95793f7848bSJeff Kirsher 	 */
95893f7848bSJeff Kirsher 
95993f7848bSJeff Kirsher 	if (mgp->num_slices > 1) {
96093f7848bSJeff Kirsher 
96193f7848bSJeff Kirsher 		/* ask the maximum number of slices it supports */
96293f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
96393f7848bSJeff Kirsher 					   &cmd, 0);
96493f7848bSJeff Kirsher 		if (status != 0) {
96593f7848bSJeff Kirsher 			dev_err(&mgp->pdev->dev,
96693f7848bSJeff Kirsher 				"failed to get number of slices\n");
96793f7848bSJeff Kirsher 		}
96893f7848bSJeff Kirsher 
96993f7848bSJeff Kirsher 		/*
97093f7848bSJeff Kirsher 		 * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
97193f7848bSJeff Kirsher 		 * to setting up the interrupt queue DMA
97293f7848bSJeff Kirsher 		 */
97393f7848bSJeff Kirsher 
97493f7848bSJeff Kirsher 		cmd.data0 = mgp->num_slices;
97593f7848bSJeff Kirsher 		cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
97693f7848bSJeff Kirsher 		if (mgp->dev->real_num_tx_queues > 1)
97793f7848bSJeff Kirsher 			cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
97893f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
97993f7848bSJeff Kirsher 					   &cmd, 0);
98093f7848bSJeff Kirsher 
98193f7848bSJeff Kirsher 		/* Firmware older than 1.4.32 only supports multiple
98293f7848bSJeff Kirsher 		 * RX queues, so if we get an error, first retry using a
98393f7848bSJeff Kirsher 		 * single TX queue before giving up */
98493f7848bSJeff Kirsher 		if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
98593f7848bSJeff Kirsher 			netif_set_real_num_tx_queues(mgp->dev, 1);
98693f7848bSJeff Kirsher 			cmd.data0 = mgp->num_slices;
98793f7848bSJeff Kirsher 			cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
98893f7848bSJeff Kirsher 			status = myri10ge_send_cmd(mgp,
98993f7848bSJeff Kirsher 						   MXGEFW_CMD_ENABLE_RSS_QUEUES,
99093f7848bSJeff Kirsher 						   &cmd, 0);
99193f7848bSJeff Kirsher 		}
99293f7848bSJeff Kirsher 
99393f7848bSJeff Kirsher 		if (status != 0) {
99493f7848bSJeff Kirsher 			dev_err(&mgp->pdev->dev,
99593f7848bSJeff Kirsher 				"failed to set number of slices\n");
99693f7848bSJeff Kirsher 
99793f7848bSJeff Kirsher 			return status;
99893f7848bSJeff Kirsher 		}
99993f7848bSJeff Kirsher 	}
100093f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
100193f7848bSJeff Kirsher 		ss = &mgp->ss[i];
100293f7848bSJeff Kirsher 		cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
100393f7848bSJeff Kirsher 		cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
100493f7848bSJeff Kirsher 		cmd.data2 = i;
100593f7848bSJeff Kirsher 		status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
100693f7848bSJeff Kirsher 					    &cmd, 0);
100793f7848bSJeff Kirsher 	}
100893f7848bSJeff Kirsher 
100993f7848bSJeff Kirsher 	status |=
101093f7848bSJeff Kirsher 	    myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
101193f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
101293f7848bSJeff Kirsher 		ss = &mgp->ss[i];
101393f7848bSJeff Kirsher 		ss->irq_claim =
101493f7848bSJeff Kirsher 		    (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
101593f7848bSJeff Kirsher 	}
101693f7848bSJeff Kirsher 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
101793f7848bSJeff Kirsher 				    &cmd, 0);
101893f7848bSJeff Kirsher 	mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
101993f7848bSJeff Kirsher 
102093f7848bSJeff Kirsher 	status |= myri10ge_send_cmd
102193f7848bSJeff Kirsher 	    (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
102293f7848bSJeff Kirsher 	mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
102393f7848bSJeff Kirsher 	if (status != 0) {
102493f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
102593f7848bSJeff Kirsher 		return status;
102693f7848bSJeff Kirsher 	}
102793f7848bSJeff Kirsher 	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
102893f7848bSJeff Kirsher 
102993f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
103093f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
103193f7848bSJeff Kirsher 	dca_tag_off = cmd.data0;
103293f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
103393f7848bSJeff Kirsher 		ss = &mgp->ss[i];
103493f7848bSJeff Kirsher 		if (status == 0) {
103593f7848bSJeff Kirsher 			ss->dca_tag = (__iomem __be32 *)
103693f7848bSJeff Kirsher 			    (mgp->sram + dca_tag_off + 4 * i);
103793f7848bSJeff Kirsher 		} else {
103893f7848bSJeff Kirsher 			ss->dca_tag = NULL;
103993f7848bSJeff Kirsher 		}
104093f7848bSJeff Kirsher 	}
104193f7848bSJeff Kirsher #endif				/* CONFIG_MYRI10GE_DCA */
104293f7848bSJeff Kirsher 
104393f7848bSJeff Kirsher 	/* reset mcp/driver shared state back to 0 */
104493f7848bSJeff Kirsher 
104593f7848bSJeff Kirsher 	mgp->link_changes = 0;
104693f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
104793f7848bSJeff Kirsher 		ss = &mgp->ss[i];
104893f7848bSJeff Kirsher 
104993f7848bSJeff Kirsher 		memset(ss->rx_done.entry, 0, bytes);
105093f7848bSJeff Kirsher 		ss->tx.req = 0;
105193f7848bSJeff Kirsher 		ss->tx.done = 0;
105293f7848bSJeff Kirsher 		ss->tx.pkt_start = 0;
105393f7848bSJeff Kirsher 		ss->tx.pkt_done = 0;
105493f7848bSJeff Kirsher 		ss->rx_big.cnt = 0;
105593f7848bSJeff Kirsher 		ss->rx_small.cnt = 0;
105693f7848bSJeff Kirsher 		ss->rx_done.idx = 0;
105793f7848bSJeff Kirsher 		ss->rx_done.cnt = 0;
105893f7848bSJeff Kirsher 		ss->tx.wake_queue = 0;
105993f7848bSJeff Kirsher 		ss->tx.stop_queue = 0;
106093f7848bSJeff Kirsher 	}
106193f7848bSJeff Kirsher 
106293f7848bSJeff Kirsher 	status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
106393f7848bSJeff Kirsher 	myri10ge_change_pause(mgp, mgp->pause);
106493f7848bSJeff Kirsher 	myri10ge_set_multicast_list(mgp->dev);
106593f7848bSJeff Kirsher 	return status;
106693f7848bSJeff Kirsher }
106793f7848bSJeff Kirsher 
106893f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
106993f7848bSJeff Kirsher static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
107093f7848bSJeff Kirsher {
10719503e255SJiang Liu 	int ret;
107293f7848bSJeff Kirsher 	u16 ctl;
107393f7848bSJeff Kirsher 
10749503e255SJiang Liu 	pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &ctl);
107593f7848bSJeff Kirsher 
107693f7848bSJeff Kirsher 	ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
107793f7848bSJeff Kirsher 	if (ret != on) {
107893f7848bSJeff Kirsher 		ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
107993f7848bSJeff Kirsher 		ctl |= (on << 4);
10809503e255SJiang Liu 		pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, ctl);
108193f7848bSJeff Kirsher 	}
108293f7848bSJeff Kirsher 	return ret;
108393f7848bSJeff Kirsher }
108493f7848bSJeff Kirsher 
108593f7848bSJeff Kirsher static void
108693f7848bSJeff Kirsher myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
108793f7848bSJeff Kirsher {
108893f7848bSJeff Kirsher 	ss->cached_dca_tag = tag;
108993f7848bSJeff Kirsher 	put_be32(htonl(tag), ss->dca_tag);
109093f7848bSJeff Kirsher }
109193f7848bSJeff Kirsher 
109293f7848bSJeff Kirsher static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
109393f7848bSJeff Kirsher {
109493f7848bSJeff Kirsher 	int cpu = get_cpu();
109593f7848bSJeff Kirsher 	int tag;
109693f7848bSJeff Kirsher 
109793f7848bSJeff Kirsher 	if (cpu != ss->cpu) {
109893f7848bSJeff Kirsher 		tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
109993f7848bSJeff Kirsher 		if (ss->cached_dca_tag != tag)
110093f7848bSJeff Kirsher 			myri10ge_write_dca(ss, cpu, tag);
110193f7848bSJeff Kirsher 		ss->cpu = cpu;
110293f7848bSJeff Kirsher 	}
110393f7848bSJeff Kirsher 	put_cpu();
110493f7848bSJeff Kirsher }
110593f7848bSJeff Kirsher 
110693f7848bSJeff Kirsher static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
110793f7848bSJeff Kirsher {
110893f7848bSJeff Kirsher 	int err, i;
110993f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
111093f7848bSJeff Kirsher 
111193f7848bSJeff Kirsher 	if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
111293f7848bSJeff Kirsher 		return;
111393f7848bSJeff Kirsher 	if (!myri10ge_dca) {
111493f7848bSJeff Kirsher 		dev_err(&pdev->dev, "dca disabled by administrator\n");
111593f7848bSJeff Kirsher 		return;
111693f7848bSJeff Kirsher 	}
111793f7848bSJeff Kirsher 	err = dca_add_requester(&pdev->dev);
111893f7848bSJeff Kirsher 	if (err) {
111993f7848bSJeff Kirsher 		if (err != -ENODEV)
112093f7848bSJeff Kirsher 			dev_err(&pdev->dev,
112193f7848bSJeff Kirsher 				"dca_add_requester() failed, err=%d\n", err);
112293f7848bSJeff Kirsher 		return;
112393f7848bSJeff Kirsher 	}
112493f7848bSJeff Kirsher 	mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
112593f7848bSJeff Kirsher 	mgp->dca_enabled = 1;
112693f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
112793f7848bSJeff Kirsher 		mgp->ss[i].cpu = -1;
112893f7848bSJeff Kirsher 		mgp->ss[i].cached_dca_tag = -1;
112993f7848bSJeff Kirsher 		myri10ge_update_dca(&mgp->ss[i]);
113093f7848bSJeff Kirsher 	}
113193f7848bSJeff Kirsher }
113293f7848bSJeff Kirsher 
113393f7848bSJeff Kirsher static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
113493f7848bSJeff Kirsher {
113593f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
113693f7848bSJeff Kirsher 
113793f7848bSJeff Kirsher 	if (!mgp->dca_enabled)
113893f7848bSJeff Kirsher 		return;
113993f7848bSJeff Kirsher 	mgp->dca_enabled = 0;
114093f7848bSJeff Kirsher 	if (mgp->relaxed_order)
114193f7848bSJeff Kirsher 		myri10ge_toggle_relaxed(pdev, 1);
114293f7848bSJeff Kirsher 	dca_remove_requester(&pdev->dev);
114393f7848bSJeff Kirsher }
114493f7848bSJeff Kirsher 
114593f7848bSJeff Kirsher static int myri10ge_notify_dca_device(struct device *dev, void *data)
114693f7848bSJeff Kirsher {
114793f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
114893f7848bSJeff Kirsher 	unsigned long event;
114993f7848bSJeff Kirsher 
115093f7848bSJeff Kirsher 	mgp = dev_get_drvdata(dev);
115193f7848bSJeff Kirsher 	event = *(unsigned long *)data;
115293f7848bSJeff Kirsher 
115393f7848bSJeff Kirsher 	if (event == DCA_PROVIDER_ADD)
115493f7848bSJeff Kirsher 		myri10ge_setup_dca(mgp);
115593f7848bSJeff Kirsher 	else if (event == DCA_PROVIDER_REMOVE)
115693f7848bSJeff Kirsher 		myri10ge_teardown_dca(mgp);
115793f7848bSJeff Kirsher 	return 0;
115893f7848bSJeff Kirsher }
115993f7848bSJeff Kirsher #endif				/* CONFIG_MYRI10GE_DCA */
116093f7848bSJeff Kirsher 
116193f7848bSJeff Kirsher static inline void
116293f7848bSJeff Kirsher myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
116393f7848bSJeff Kirsher 		    struct mcp_kreq_ether_recv *src)
116493f7848bSJeff Kirsher {
116593f7848bSJeff Kirsher 	__be32 low;
116693f7848bSJeff Kirsher 
116793f7848bSJeff Kirsher 	low = src->addr_low;
116893f7848bSJeff Kirsher 	src->addr_low = htonl(DMA_BIT_MASK(32));
116993f7848bSJeff Kirsher 	myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
117093f7848bSJeff Kirsher 	mb();
117193f7848bSJeff Kirsher 	myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
117293f7848bSJeff Kirsher 	mb();
117393f7848bSJeff Kirsher 	src->addr_low = low;
117493f7848bSJeff Kirsher 	put_be32(low, &dst->addr_low);
117593f7848bSJeff Kirsher 	mb();
117693f7848bSJeff Kirsher }
117793f7848bSJeff Kirsher 
117893f7848bSJeff Kirsher static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
117993f7848bSJeff Kirsher {
118093f7848bSJeff Kirsher 	struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
118193f7848bSJeff Kirsher 
118293f7848bSJeff Kirsher 	if ((skb->protocol == htons(ETH_P_8021Q)) &&
118393f7848bSJeff Kirsher 	    (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
118493f7848bSJeff Kirsher 	     vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
118593f7848bSJeff Kirsher 		skb->csum = hw_csum;
118693f7848bSJeff Kirsher 		skb->ip_summed = CHECKSUM_COMPLETE;
118793f7848bSJeff Kirsher 	}
118893f7848bSJeff Kirsher }
118993f7848bSJeff Kirsher 
119093f7848bSJeff Kirsher static void
119193f7848bSJeff Kirsher myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
119293f7848bSJeff Kirsher 			int bytes, int watchdog)
119393f7848bSJeff Kirsher {
119493f7848bSJeff Kirsher 	struct page *page;
119510545937SStanislaw Gruszka 	dma_addr_t bus;
119693f7848bSJeff Kirsher 	int idx;
119793f7848bSJeff Kirsher #if MYRI10GE_ALLOC_SIZE > 4096
119893f7848bSJeff Kirsher 	int end_offset;
119993f7848bSJeff Kirsher #endif
120093f7848bSJeff Kirsher 
120193f7848bSJeff Kirsher 	if (unlikely(rx->watchdog_needed && !watchdog))
120293f7848bSJeff Kirsher 		return;
120393f7848bSJeff Kirsher 
120493f7848bSJeff Kirsher 	/* try to refill entire ring */
120593f7848bSJeff Kirsher 	while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
120693f7848bSJeff Kirsher 		idx = rx->fill_cnt & rx->mask;
120793f7848bSJeff Kirsher 		if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
120893f7848bSJeff Kirsher 			/* we can use part of previous page */
120993f7848bSJeff Kirsher 			get_page(rx->page);
121093f7848bSJeff Kirsher 		} else {
121193f7848bSJeff Kirsher 			/* we need a new page */
121293f7848bSJeff Kirsher 			page =
121393f7848bSJeff Kirsher 			    alloc_pages(GFP_ATOMIC | __GFP_COMP,
121493f7848bSJeff Kirsher 					MYRI10GE_ALLOC_ORDER);
121593f7848bSJeff Kirsher 			if (unlikely(page == NULL)) {
121693f7848bSJeff Kirsher 				if (rx->fill_cnt - rx->cnt < 16)
121793f7848bSJeff Kirsher 					rx->watchdog_needed = 1;
121893f7848bSJeff Kirsher 				return;
121993f7848bSJeff Kirsher 			}
122010545937SStanislaw Gruszka 
122110545937SStanislaw Gruszka 			bus = pci_map_page(mgp->pdev, page, 0,
122293f7848bSJeff Kirsher 					   MYRI10GE_ALLOC_SIZE,
122393f7848bSJeff Kirsher 					   PCI_DMA_FROMDEVICE);
122410545937SStanislaw Gruszka 			if (unlikely(pci_dma_mapping_error(mgp->pdev, bus))) {
122510545937SStanislaw Gruszka 				__free_pages(page, MYRI10GE_ALLOC_ORDER);
122610545937SStanislaw Gruszka 				if (rx->fill_cnt - rx->cnt < 16)
122710545937SStanislaw Gruszka 					rx->watchdog_needed = 1;
122810545937SStanislaw Gruszka 				return;
122910545937SStanislaw Gruszka 			}
123010545937SStanislaw Gruszka 
123110545937SStanislaw Gruszka 			rx->page = page;
123210545937SStanislaw Gruszka 			rx->page_offset = 0;
123310545937SStanislaw Gruszka 			rx->bus = bus;
123410545937SStanislaw Gruszka 
123593f7848bSJeff Kirsher 		}
123693f7848bSJeff Kirsher 		rx->info[idx].page = rx->page;
123793f7848bSJeff Kirsher 		rx->info[idx].page_offset = rx->page_offset;
123893f7848bSJeff Kirsher 		/* note that this is the address of the start of the
123993f7848bSJeff Kirsher 		 * page */
124093f7848bSJeff Kirsher 		dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
124193f7848bSJeff Kirsher 		rx->shadow[idx].addr_low =
124293f7848bSJeff Kirsher 		    htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
124393f7848bSJeff Kirsher 		rx->shadow[idx].addr_high =
124493f7848bSJeff Kirsher 		    htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
124593f7848bSJeff Kirsher 
124693f7848bSJeff Kirsher 		/* start next packet on a cacheline boundary */
124793f7848bSJeff Kirsher 		rx->page_offset += SKB_DATA_ALIGN(bytes);
124893f7848bSJeff Kirsher 
124993f7848bSJeff Kirsher #if MYRI10GE_ALLOC_SIZE > 4096
125093f7848bSJeff Kirsher 		/* don't cross a 4KB boundary */
125193f7848bSJeff Kirsher 		end_offset = rx->page_offset + bytes - 1;
125293f7848bSJeff Kirsher 		if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
125393f7848bSJeff Kirsher 			rx->page_offset = end_offset & ~4095;
125493f7848bSJeff Kirsher #endif
125593f7848bSJeff Kirsher 		rx->fill_cnt++;
125693f7848bSJeff Kirsher 
125793f7848bSJeff Kirsher 		/* copy 8 descriptors to the firmware at a time */
125893f7848bSJeff Kirsher 		if ((idx & 7) == 7) {
125993f7848bSJeff Kirsher 			myri10ge_submit_8rx(&rx->lanai[idx - 7],
126093f7848bSJeff Kirsher 					    &rx->shadow[idx - 7]);
126193f7848bSJeff Kirsher 		}
126293f7848bSJeff Kirsher 	}
126393f7848bSJeff Kirsher }
126493f7848bSJeff Kirsher 
126593f7848bSJeff Kirsher static inline void
126693f7848bSJeff Kirsher myri10ge_unmap_rx_page(struct pci_dev *pdev,
126793f7848bSJeff Kirsher 		       struct myri10ge_rx_buffer_state *info, int bytes)
126893f7848bSJeff Kirsher {
126993f7848bSJeff Kirsher 	/* unmap the recvd page if we're the only or last user of it */
127093f7848bSJeff Kirsher 	if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
127193f7848bSJeff Kirsher 	    (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
127293f7848bSJeff Kirsher 		pci_unmap_page(pdev, (dma_unmap_addr(info, bus)
127393f7848bSJeff Kirsher 				      & ~(MYRI10GE_ALLOC_SIZE - 1)),
127493f7848bSJeff Kirsher 			       MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
127593f7848bSJeff Kirsher 	}
127693f7848bSJeff Kirsher }
127793f7848bSJeff Kirsher 
12781b4c44e6SAndrew Gallatin /*
12791b4c44e6SAndrew Gallatin  * GRO does not support acceleration of tagged vlan frames, and
12801b4c44e6SAndrew Gallatin  * this NIC does not support vlan tag offload, so we must pop
12811b4c44e6SAndrew Gallatin  * the tag ourselves to be able to achieve GRO performance that
12821b4c44e6SAndrew Gallatin  * is comparable to LRO.
12831b4c44e6SAndrew Gallatin  */
12841b4c44e6SAndrew Gallatin 
12851b4c44e6SAndrew Gallatin static inline void
12861b4c44e6SAndrew Gallatin myri10ge_vlan_rx(struct net_device *dev, void *addr, struct sk_buff *skb)
12871b4c44e6SAndrew Gallatin {
12881b4c44e6SAndrew Gallatin 	u8 *va;
12891b4c44e6SAndrew Gallatin 	struct vlan_ethhdr *veh;
12901b4c44e6SAndrew Gallatin 	struct skb_frag_struct *frag;
12911b4c44e6SAndrew Gallatin 	__wsum vsum;
12921b4c44e6SAndrew Gallatin 
12931b4c44e6SAndrew Gallatin 	va = addr;
12941b4c44e6SAndrew Gallatin 	va += MXGEFW_PAD;
12951b4c44e6SAndrew Gallatin 	veh = (struct vlan_ethhdr *)va;
1296f646968fSPatrick McHardy 	if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
1297f646968fSPatrick McHardy 	    NETIF_F_HW_VLAN_CTAG_RX &&
129830828d2bSAndrew Gallatin 	    veh->h_vlan_proto == htons(ETH_P_8021Q)) {
12991b4c44e6SAndrew Gallatin 		/* fixup csum if needed */
13001b4c44e6SAndrew Gallatin 		if (skb->ip_summed == CHECKSUM_COMPLETE) {
13011b4c44e6SAndrew Gallatin 			vsum = csum_partial(va + ETH_HLEN, VLAN_HLEN, 0);
13021b4c44e6SAndrew Gallatin 			skb->csum = csum_sub(skb->csum, vsum);
13031b4c44e6SAndrew Gallatin 		}
13041b4c44e6SAndrew Gallatin 		/* pop tag */
130586a9bad3SPatrick McHardy 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(veh->h_vlan_TCI));
13061b4c44e6SAndrew Gallatin 		memmove(va + VLAN_HLEN, va, 2 * ETH_ALEN);
13071b4c44e6SAndrew Gallatin 		skb->len -= VLAN_HLEN;
13081b4c44e6SAndrew Gallatin 		skb->data_len -= VLAN_HLEN;
13091b4c44e6SAndrew Gallatin 		frag = skb_shinfo(skb)->frags;
13101b4c44e6SAndrew Gallatin 		frag->page_offset += VLAN_HLEN;
13111b4c44e6SAndrew Gallatin 		skb_frag_size_set(frag, skb_frag_size(frag) - VLAN_HLEN);
13121b4c44e6SAndrew Gallatin 	}
13131b4c44e6SAndrew Gallatin }
13141b4c44e6SAndrew Gallatin 
13150dde8026SHyong-Youb Kim #define MYRI10GE_HLEN 64 /* Bytes to copy from page to skb linear memory */
13160dde8026SHyong-Youb Kim 
131793f7848bSJeff Kirsher static inline int
13184ca3221fSAndrew Gallatin myri10ge_rx_done(struct myri10ge_slice_state *ss, int len, __wsum csum)
131993f7848bSJeff Kirsher {
132093f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = ss->mgp;
132193f7848bSJeff Kirsher 	struct sk_buff *skb;
13224ca3221fSAndrew Gallatin 	struct skb_frag_struct *rx_frags;
132393f7848bSJeff Kirsher 	struct myri10ge_rx_buf *rx;
13244ca3221fSAndrew Gallatin 	int i, idx, remainder, bytes;
132593f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
132693f7848bSJeff Kirsher 	struct net_device *dev = mgp->dev;
132793f7848bSJeff Kirsher 	u8 *va;
132893f7848bSJeff Kirsher 
132993f7848bSJeff Kirsher 	if (len <= mgp->small_bytes) {
133093f7848bSJeff Kirsher 		rx = &ss->rx_small;
133193f7848bSJeff Kirsher 		bytes = mgp->small_bytes;
133293f7848bSJeff Kirsher 	} else {
133393f7848bSJeff Kirsher 		rx = &ss->rx_big;
133493f7848bSJeff Kirsher 		bytes = mgp->big_bytes;
133593f7848bSJeff Kirsher 	}
133693f7848bSJeff Kirsher 
133793f7848bSJeff Kirsher 	len += MXGEFW_PAD;
133893f7848bSJeff Kirsher 	idx = rx->cnt & rx->mask;
133993f7848bSJeff Kirsher 	va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
134093f7848bSJeff Kirsher 	prefetch(va);
13414ca3221fSAndrew Gallatin 
13424ca3221fSAndrew Gallatin 	skb = napi_get_frags(&ss->napi);
13434ca3221fSAndrew Gallatin 	if (unlikely(skb == NULL)) {
13444ca3221fSAndrew Gallatin 		ss->stats.rx_dropped++;
13454ca3221fSAndrew Gallatin 		for (i = 0, remainder = len; remainder > 0; i++) {
13464ca3221fSAndrew Gallatin 			myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
13474ca3221fSAndrew Gallatin 			put_page(rx->info[idx].page);
13484ca3221fSAndrew Gallatin 			rx->cnt++;
13494ca3221fSAndrew Gallatin 			idx = rx->cnt & rx->mask;
13504ca3221fSAndrew Gallatin 			remainder -= MYRI10GE_ALLOC_SIZE;
13514ca3221fSAndrew Gallatin 		}
13524ca3221fSAndrew Gallatin 		return 0;
13534ca3221fSAndrew Gallatin 	}
13544ca3221fSAndrew Gallatin 	rx_frags = skb_shinfo(skb)->frags;
135593f7848bSJeff Kirsher 	/* Fill skb_frag_struct(s) with data from our receive */
135693f7848bSJeff Kirsher 	for (i = 0, remainder = len; remainder > 0; i++) {
135793f7848bSJeff Kirsher 		myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
13584ca3221fSAndrew Gallatin 		skb_fill_page_desc(skb, i, rx->info[idx].page,
13594ca3221fSAndrew Gallatin 				   rx->info[idx].page_offset,
13604ca3221fSAndrew Gallatin 				   remainder < MYRI10GE_ALLOC_SIZE ?
13614ca3221fSAndrew Gallatin 				   remainder : MYRI10GE_ALLOC_SIZE);
136293f7848bSJeff Kirsher 		rx->cnt++;
136393f7848bSJeff Kirsher 		idx = rx->cnt & rx->mask;
136493f7848bSJeff Kirsher 		remainder -= MYRI10GE_ALLOC_SIZE;
136593f7848bSJeff Kirsher 	}
136693f7848bSJeff Kirsher 
13674ca3221fSAndrew Gallatin 	/* remove padding */
136893f7848bSJeff Kirsher 	rx_frags[0].page_offset += MXGEFW_PAD;
13694ca3221fSAndrew Gallatin 	rx_frags[0].size -= MXGEFW_PAD;
137093f7848bSJeff Kirsher 	len -= MXGEFW_PAD;
137193f7848bSJeff Kirsher 
13724ca3221fSAndrew Gallatin 	skb->len = len;
13734ca3221fSAndrew Gallatin 	skb->data_len = len;
13744ca3221fSAndrew Gallatin 	skb->truesize += len;
13754ca3221fSAndrew Gallatin 	if (dev->features & NETIF_F_RXCSUM) {
13764ca3221fSAndrew Gallatin 		skb->ip_summed = CHECKSUM_COMPLETE;
13774ca3221fSAndrew Gallatin 		skb->csum = csum;
137893f7848bSJeff Kirsher 	}
13791b4c44e6SAndrew Gallatin 	myri10ge_vlan_rx(mgp->dev, va, skb);
138093f7848bSJeff Kirsher 	skb_record_rx_queue(skb, ss - &mgp->ss[0]);
138193f7848bSJeff Kirsher 
13824ca3221fSAndrew Gallatin 	napi_gro_frags(&ss->napi);
13830dde8026SHyong-Youb Kim 
138493f7848bSJeff Kirsher 	return 1;
138593f7848bSJeff Kirsher }
138693f7848bSJeff Kirsher 
138793f7848bSJeff Kirsher static inline void
138893f7848bSJeff Kirsher myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
138993f7848bSJeff Kirsher {
139093f7848bSJeff Kirsher 	struct pci_dev *pdev = ss->mgp->pdev;
139193f7848bSJeff Kirsher 	struct myri10ge_tx_buf *tx = &ss->tx;
139293f7848bSJeff Kirsher 	struct netdev_queue *dev_queue;
139393f7848bSJeff Kirsher 	struct sk_buff *skb;
139493f7848bSJeff Kirsher 	int idx, len;
139593f7848bSJeff Kirsher 
139693f7848bSJeff Kirsher 	while (tx->pkt_done != mcp_index) {
139793f7848bSJeff Kirsher 		idx = tx->done & tx->mask;
139893f7848bSJeff Kirsher 		skb = tx->info[idx].skb;
139993f7848bSJeff Kirsher 
140093f7848bSJeff Kirsher 		/* Mark as free */
140193f7848bSJeff Kirsher 		tx->info[idx].skb = NULL;
140293f7848bSJeff Kirsher 		if (tx->info[idx].last) {
140393f7848bSJeff Kirsher 			tx->pkt_done++;
140493f7848bSJeff Kirsher 			tx->info[idx].last = 0;
140593f7848bSJeff Kirsher 		}
140693f7848bSJeff Kirsher 		tx->done++;
140793f7848bSJeff Kirsher 		len = dma_unmap_len(&tx->info[idx], len);
140893f7848bSJeff Kirsher 		dma_unmap_len_set(&tx->info[idx], len, 0);
140993f7848bSJeff Kirsher 		if (skb) {
141093f7848bSJeff Kirsher 			ss->stats.tx_bytes += skb->len;
141193f7848bSJeff Kirsher 			ss->stats.tx_packets++;
141293f7848bSJeff Kirsher 			dev_kfree_skb_irq(skb);
141393f7848bSJeff Kirsher 			if (len)
141493f7848bSJeff Kirsher 				pci_unmap_single(pdev,
141593f7848bSJeff Kirsher 						 dma_unmap_addr(&tx->info[idx],
141693f7848bSJeff Kirsher 								bus), len,
141793f7848bSJeff Kirsher 						 PCI_DMA_TODEVICE);
141893f7848bSJeff Kirsher 		} else {
141993f7848bSJeff Kirsher 			if (len)
142093f7848bSJeff Kirsher 				pci_unmap_page(pdev,
142193f7848bSJeff Kirsher 					       dma_unmap_addr(&tx->info[idx],
142293f7848bSJeff Kirsher 							      bus), len,
142393f7848bSJeff Kirsher 					       PCI_DMA_TODEVICE);
142493f7848bSJeff Kirsher 		}
142593f7848bSJeff Kirsher 	}
142693f7848bSJeff Kirsher 
142793f7848bSJeff Kirsher 	dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
142893f7848bSJeff Kirsher 	/*
142993f7848bSJeff Kirsher 	 * Make a minimal effort to prevent the NIC from polling an
143093f7848bSJeff Kirsher 	 * idle tx queue.  If we can't get the lock we leave the queue
143193f7848bSJeff Kirsher 	 * active. In this case, either a thread was about to start
143293f7848bSJeff Kirsher 	 * using the queue anyway, or we lost a race and the NIC will
143393f7848bSJeff Kirsher 	 * waste some of its resources polling an inactive queue for a
143493f7848bSJeff Kirsher 	 * while.
143593f7848bSJeff Kirsher 	 */
143693f7848bSJeff Kirsher 
143793f7848bSJeff Kirsher 	if ((ss->mgp->dev->real_num_tx_queues > 1) &&
143893f7848bSJeff Kirsher 	    __netif_tx_trylock(dev_queue)) {
143993f7848bSJeff Kirsher 		if (tx->req == tx->done) {
144093f7848bSJeff Kirsher 			tx->queue_active = 0;
144193f7848bSJeff Kirsher 			put_be32(htonl(1), tx->send_stop);
144293f7848bSJeff Kirsher 			mb();
144393f7848bSJeff Kirsher 			mmiowb();
144493f7848bSJeff Kirsher 		}
144593f7848bSJeff Kirsher 		__netif_tx_unlock(dev_queue);
144693f7848bSJeff Kirsher 	}
144793f7848bSJeff Kirsher 
144893f7848bSJeff Kirsher 	/* start the queue if we've stopped it */
144993f7848bSJeff Kirsher 	if (netif_tx_queue_stopped(dev_queue) &&
145093f7848bSJeff Kirsher 	    tx->req - tx->done < (tx->mask >> 1) &&
145193f7848bSJeff Kirsher 	    ss->mgp->running == MYRI10GE_ETH_RUNNING) {
145293f7848bSJeff Kirsher 		tx->wake_queue++;
145393f7848bSJeff Kirsher 		netif_tx_wake_queue(dev_queue);
145493f7848bSJeff Kirsher 	}
145593f7848bSJeff Kirsher }
145693f7848bSJeff Kirsher 
145793f7848bSJeff Kirsher static inline int
145893f7848bSJeff Kirsher myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
145993f7848bSJeff Kirsher {
146093f7848bSJeff Kirsher 	struct myri10ge_rx_done *rx_done = &ss->rx_done;
146193f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = ss->mgp;
146293f7848bSJeff Kirsher 	unsigned long rx_bytes = 0;
146393f7848bSJeff Kirsher 	unsigned long rx_packets = 0;
146493f7848bSJeff Kirsher 	unsigned long rx_ok;
146593f7848bSJeff Kirsher 	int idx = rx_done->idx;
146693f7848bSJeff Kirsher 	int cnt = rx_done->cnt;
146793f7848bSJeff Kirsher 	int work_done = 0;
146893f7848bSJeff Kirsher 	u16 length;
146993f7848bSJeff Kirsher 	__wsum checksum;
147093f7848bSJeff Kirsher 
147193f7848bSJeff Kirsher 	while (rx_done->entry[idx].length != 0 && work_done < budget) {
147293f7848bSJeff Kirsher 		length = ntohs(rx_done->entry[idx].length);
147393f7848bSJeff Kirsher 		rx_done->entry[idx].length = 0;
147493f7848bSJeff Kirsher 		checksum = csum_unfold(rx_done->entry[idx].checksum);
14754ca3221fSAndrew Gallatin 		rx_ok = myri10ge_rx_done(ss, length, checksum);
147693f7848bSJeff Kirsher 		rx_packets += rx_ok;
147793f7848bSJeff Kirsher 		rx_bytes += rx_ok * (unsigned long)length;
147893f7848bSJeff Kirsher 		cnt++;
147993f7848bSJeff Kirsher 		idx = cnt & (mgp->max_intr_slots - 1);
148093f7848bSJeff Kirsher 		work_done++;
148193f7848bSJeff Kirsher 	}
148293f7848bSJeff Kirsher 	rx_done->idx = idx;
148393f7848bSJeff Kirsher 	rx_done->cnt = cnt;
148493f7848bSJeff Kirsher 	ss->stats.rx_packets += rx_packets;
148593f7848bSJeff Kirsher 	ss->stats.rx_bytes += rx_bytes;
148693f7848bSJeff Kirsher 
148793f7848bSJeff Kirsher 	/* restock receive rings if needed */
148893f7848bSJeff Kirsher 	if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
148993f7848bSJeff Kirsher 		myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
149093f7848bSJeff Kirsher 					mgp->small_bytes + MXGEFW_PAD, 0);
149193f7848bSJeff Kirsher 	if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
149293f7848bSJeff Kirsher 		myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
149393f7848bSJeff Kirsher 
149493f7848bSJeff Kirsher 	return work_done;
149593f7848bSJeff Kirsher }
149693f7848bSJeff Kirsher 
149793f7848bSJeff Kirsher static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
149893f7848bSJeff Kirsher {
149993f7848bSJeff Kirsher 	struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
150093f7848bSJeff Kirsher 
150193f7848bSJeff Kirsher 	if (unlikely(stats->stats_updated)) {
150293f7848bSJeff Kirsher 		unsigned link_up = ntohl(stats->link_up);
150393f7848bSJeff Kirsher 		if (mgp->link_state != link_up) {
150493f7848bSJeff Kirsher 			mgp->link_state = link_up;
150593f7848bSJeff Kirsher 
150693f7848bSJeff Kirsher 			if (mgp->link_state == MXGEFW_LINK_UP) {
150793f7848bSJeff Kirsher 				netif_info(mgp, link, mgp->dev, "link up\n");
150893f7848bSJeff Kirsher 				netif_carrier_on(mgp->dev);
150993f7848bSJeff Kirsher 				mgp->link_changes++;
151093f7848bSJeff Kirsher 			} else {
151193f7848bSJeff Kirsher 				netif_info(mgp, link, mgp->dev, "link %s\n",
151293f7848bSJeff Kirsher 					   (link_up == MXGEFW_LINK_MYRINET ?
151393f7848bSJeff Kirsher 					    "mismatch (Myrinet detected)" :
151493f7848bSJeff Kirsher 					    "down"));
151593f7848bSJeff Kirsher 				netif_carrier_off(mgp->dev);
151693f7848bSJeff Kirsher 				mgp->link_changes++;
151793f7848bSJeff Kirsher 			}
151893f7848bSJeff Kirsher 		}
151993f7848bSJeff Kirsher 		if (mgp->rdma_tags_available !=
152093f7848bSJeff Kirsher 		    ntohl(stats->rdma_tags_available)) {
152193f7848bSJeff Kirsher 			mgp->rdma_tags_available =
152293f7848bSJeff Kirsher 			    ntohl(stats->rdma_tags_available);
152393f7848bSJeff Kirsher 			netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
152493f7848bSJeff Kirsher 				    mgp->rdma_tags_available);
152593f7848bSJeff Kirsher 		}
152693f7848bSJeff Kirsher 		mgp->down_cnt += stats->link_down;
152793f7848bSJeff Kirsher 		if (stats->link_down)
152893f7848bSJeff Kirsher 			wake_up(&mgp->down_wq);
152993f7848bSJeff Kirsher 	}
153093f7848bSJeff Kirsher }
153193f7848bSJeff Kirsher 
153293f7848bSJeff Kirsher static int myri10ge_poll(struct napi_struct *napi, int budget)
153393f7848bSJeff Kirsher {
153493f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss =
153593f7848bSJeff Kirsher 	    container_of(napi, struct myri10ge_slice_state, napi);
153693f7848bSJeff Kirsher 	int work_done;
153793f7848bSJeff Kirsher 
153893f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
153993f7848bSJeff Kirsher 	if (ss->mgp->dca_enabled)
154093f7848bSJeff Kirsher 		myri10ge_update_dca(ss);
154193f7848bSJeff Kirsher #endif
154293f7848bSJeff Kirsher 	/* process as many rx events as NAPI will allow */
154393f7848bSJeff Kirsher 	work_done = myri10ge_clean_rx_done(ss, budget);
154493f7848bSJeff Kirsher 
154593f7848bSJeff Kirsher 	if (work_done < budget) {
15466ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
154793f7848bSJeff Kirsher 		put_be32(htonl(3), ss->irq_claim);
154893f7848bSJeff Kirsher 	}
154993f7848bSJeff Kirsher 	return work_done;
155093f7848bSJeff Kirsher }
155193f7848bSJeff Kirsher 
155293f7848bSJeff Kirsher static irqreturn_t myri10ge_intr(int irq, void *arg)
155393f7848bSJeff Kirsher {
155493f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss = arg;
155593f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = ss->mgp;
155693f7848bSJeff Kirsher 	struct mcp_irq_data *stats = ss->fw_stats;
155793f7848bSJeff Kirsher 	struct myri10ge_tx_buf *tx = &ss->tx;
155893f7848bSJeff Kirsher 	u32 send_done_count;
155993f7848bSJeff Kirsher 	int i;
156093f7848bSJeff Kirsher 
156193f7848bSJeff Kirsher 	/* an interrupt on a non-zero receive-only slice is implicitly
156293f7848bSJeff Kirsher 	 * valid  since MSI-X irqs are not shared */
156393f7848bSJeff Kirsher 	if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
156493f7848bSJeff Kirsher 		napi_schedule(&ss->napi);
156593f7848bSJeff Kirsher 		return IRQ_HANDLED;
156693f7848bSJeff Kirsher 	}
156793f7848bSJeff Kirsher 
156893f7848bSJeff Kirsher 	/* make sure it is our IRQ, and that the DMA has finished */
156993f7848bSJeff Kirsher 	if (unlikely(!stats->valid))
157093f7848bSJeff Kirsher 		return IRQ_NONE;
157193f7848bSJeff Kirsher 
157293f7848bSJeff Kirsher 	/* low bit indicates receives are present, so schedule
157393f7848bSJeff Kirsher 	 * napi poll handler */
157493f7848bSJeff Kirsher 	if (stats->valid & 1)
157593f7848bSJeff Kirsher 		napi_schedule(&ss->napi);
157693f7848bSJeff Kirsher 
157793f7848bSJeff Kirsher 	if (!mgp->msi_enabled && !mgp->msix_enabled) {
157893f7848bSJeff Kirsher 		put_be32(0, mgp->irq_deassert);
157993f7848bSJeff Kirsher 		if (!myri10ge_deassert_wait)
158093f7848bSJeff Kirsher 			stats->valid = 0;
158193f7848bSJeff Kirsher 		mb();
158293f7848bSJeff Kirsher 	} else
158393f7848bSJeff Kirsher 		stats->valid = 0;
158493f7848bSJeff Kirsher 
158593f7848bSJeff Kirsher 	/* Wait for IRQ line to go low, if using INTx */
158693f7848bSJeff Kirsher 	i = 0;
158793f7848bSJeff Kirsher 	while (1) {
158893f7848bSJeff Kirsher 		i++;
158993f7848bSJeff Kirsher 		/* check for transmit completes and receives */
159093f7848bSJeff Kirsher 		send_done_count = ntohl(stats->send_done_count);
159193f7848bSJeff Kirsher 		if (send_done_count != tx->pkt_done)
159293f7848bSJeff Kirsher 			myri10ge_tx_done(ss, (int)send_done_count);
159393f7848bSJeff Kirsher 		if (unlikely(i > myri10ge_max_irq_loops)) {
159493f7848bSJeff Kirsher 			netdev_warn(mgp->dev, "irq stuck?\n");
159593f7848bSJeff Kirsher 			stats->valid = 0;
159693f7848bSJeff Kirsher 			schedule_work(&mgp->watchdog_work);
159793f7848bSJeff Kirsher 		}
159893f7848bSJeff Kirsher 		if (likely(stats->valid == 0))
159993f7848bSJeff Kirsher 			break;
160093f7848bSJeff Kirsher 		cpu_relax();
160193f7848bSJeff Kirsher 		barrier();
160293f7848bSJeff Kirsher 	}
160393f7848bSJeff Kirsher 
160493f7848bSJeff Kirsher 	/* Only slice 0 updates stats */
160593f7848bSJeff Kirsher 	if (ss == mgp->ss)
160693f7848bSJeff Kirsher 		myri10ge_check_statblock(mgp);
160793f7848bSJeff Kirsher 
160893f7848bSJeff Kirsher 	put_be32(htonl(3), ss->irq_claim + 1);
160993f7848bSJeff Kirsher 	return IRQ_HANDLED;
161093f7848bSJeff Kirsher }
161193f7848bSJeff Kirsher 
161293f7848bSJeff Kirsher static int
1613cf901656SPhilippe Reynes myri10ge_get_link_ksettings(struct net_device *netdev,
1614cf901656SPhilippe Reynes 			    struct ethtool_link_ksettings *cmd)
161593f7848bSJeff Kirsher {
161693f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
161793f7848bSJeff Kirsher 	char *ptr;
161893f7848bSJeff Kirsher 	int i;
161993f7848bSJeff Kirsher 
1620cf901656SPhilippe Reynes 	cmd->base.autoneg = AUTONEG_DISABLE;
1621cf901656SPhilippe Reynes 	cmd->base.speed = SPEED_10000;
1622cf901656SPhilippe Reynes 	cmd->base.duplex = DUPLEX_FULL;
162393f7848bSJeff Kirsher 
162493f7848bSJeff Kirsher 	/*
162593f7848bSJeff Kirsher 	 * parse the product code to deterimine the interface type
162693f7848bSJeff Kirsher 	 * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
162793f7848bSJeff Kirsher 	 * after the 3rd dash in the driver's cached copy of the
162893f7848bSJeff Kirsher 	 * EEPROM's product code string.
162993f7848bSJeff Kirsher 	 */
163093f7848bSJeff Kirsher 	ptr = mgp->product_code_string;
163193f7848bSJeff Kirsher 	if (ptr == NULL) {
163293f7848bSJeff Kirsher 		netdev_err(netdev, "Missing product code\n");
163393f7848bSJeff Kirsher 		return 0;
163493f7848bSJeff Kirsher 	}
163593f7848bSJeff Kirsher 	for (i = 0; i < 3; i++, ptr++) {
163693f7848bSJeff Kirsher 		ptr = strchr(ptr, '-');
163793f7848bSJeff Kirsher 		if (ptr == NULL) {
163893f7848bSJeff Kirsher 			netdev_err(netdev, "Invalid product code %s\n",
163993f7848bSJeff Kirsher 				   mgp->product_code_string);
164093f7848bSJeff Kirsher 			return 0;
164193f7848bSJeff Kirsher 		}
164293f7848bSJeff Kirsher 	}
164393f7848bSJeff Kirsher 	if (*ptr == '2')
164493f7848bSJeff Kirsher 		ptr++;
164593f7848bSJeff Kirsher 	if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
164693f7848bSJeff Kirsher 		/* We've found either an XFP, quad ribbon fiber, or SFP+ */
1647cf901656SPhilippe Reynes 		cmd->base.port = PORT_FIBRE;
1648cf901656SPhilippe Reynes 		ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
1649cf901656SPhilippe Reynes 		ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
165093f7848bSJeff Kirsher 	} else {
1651cf901656SPhilippe Reynes 		cmd->base.port = PORT_OTHER;
165293f7848bSJeff Kirsher 	}
165393f7848bSJeff Kirsher 
165493f7848bSJeff Kirsher 	return 0;
165593f7848bSJeff Kirsher }
165693f7848bSJeff Kirsher 
165793f7848bSJeff Kirsher static void
165893f7848bSJeff Kirsher myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
165993f7848bSJeff Kirsher {
166093f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
166193f7848bSJeff Kirsher 
166293f7848bSJeff Kirsher 	strlcpy(info->driver, "myri10ge", sizeof(info->driver));
166393f7848bSJeff Kirsher 	strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
166493f7848bSJeff Kirsher 	strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
166593f7848bSJeff Kirsher 	strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
166693f7848bSJeff Kirsher }
166793f7848bSJeff Kirsher 
166893f7848bSJeff Kirsher static int
166993f7848bSJeff Kirsher myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
167093f7848bSJeff Kirsher {
167193f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
167293f7848bSJeff Kirsher 
167393f7848bSJeff Kirsher 	coal->rx_coalesce_usecs = mgp->intr_coal_delay;
167493f7848bSJeff Kirsher 	return 0;
167593f7848bSJeff Kirsher }
167693f7848bSJeff Kirsher 
167793f7848bSJeff Kirsher static int
167893f7848bSJeff Kirsher myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
167993f7848bSJeff Kirsher {
168093f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
168193f7848bSJeff Kirsher 
168293f7848bSJeff Kirsher 	mgp->intr_coal_delay = coal->rx_coalesce_usecs;
168393f7848bSJeff Kirsher 	put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
168493f7848bSJeff Kirsher 	return 0;
168593f7848bSJeff Kirsher }
168693f7848bSJeff Kirsher 
168793f7848bSJeff Kirsher static void
168893f7848bSJeff Kirsher myri10ge_get_pauseparam(struct net_device *netdev,
168993f7848bSJeff Kirsher 			struct ethtool_pauseparam *pause)
169093f7848bSJeff Kirsher {
169193f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
169293f7848bSJeff Kirsher 
169393f7848bSJeff Kirsher 	pause->autoneg = 0;
169493f7848bSJeff Kirsher 	pause->rx_pause = mgp->pause;
169593f7848bSJeff Kirsher 	pause->tx_pause = mgp->pause;
169693f7848bSJeff Kirsher }
169793f7848bSJeff Kirsher 
169893f7848bSJeff Kirsher static int
169993f7848bSJeff Kirsher myri10ge_set_pauseparam(struct net_device *netdev,
170093f7848bSJeff Kirsher 			struct ethtool_pauseparam *pause)
170193f7848bSJeff Kirsher {
170293f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
170393f7848bSJeff Kirsher 
170493f7848bSJeff Kirsher 	if (pause->tx_pause != mgp->pause)
170593f7848bSJeff Kirsher 		return myri10ge_change_pause(mgp, pause->tx_pause);
170693f7848bSJeff Kirsher 	if (pause->rx_pause != mgp->pause)
170793f7848bSJeff Kirsher 		return myri10ge_change_pause(mgp, pause->rx_pause);
170893f7848bSJeff Kirsher 	if (pause->autoneg != 0)
170993f7848bSJeff Kirsher 		return -EINVAL;
171093f7848bSJeff Kirsher 	return 0;
171193f7848bSJeff Kirsher }
171293f7848bSJeff Kirsher 
171393f7848bSJeff Kirsher static void
171493f7848bSJeff Kirsher myri10ge_get_ringparam(struct net_device *netdev,
171593f7848bSJeff Kirsher 		       struct ethtool_ringparam *ring)
171693f7848bSJeff Kirsher {
171793f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
171893f7848bSJeff Kirsher 
171993f7848bSJeff Kirsher 	ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
172093f7848bSJeff Kirsher 	ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
172193f7848bSJeff Kirsher 	ring->rx_jumbo_max_pending = 0;
172293f7848bSJeff Kirsher 	ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
172393f7848bSJeff Kirsher 	ring->rx_mini_pending = ring->rx_mini_max_pending;
172493f7848bSJeff Kirsher 	ring->rx_pending = ring->rx_max_pending;
172593f7848bSJeff Kirsher 	ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
172693f7848bSJeff Kirsher 	ring->tx_pending = ring->tx_max_pending;
172793f7848bSJeff Kirsher }
172893f7848bSJeff Kirsher 
172993f7848bSJeff Kirsher static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
173093f7848bSJeff Kirsher 	"rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
173193f7848bSJeff Kirsher 	"tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
173293f7848bSJeff Kirsher 	"rx_length_errors", "rx_over_errors", "rx_crc_errors",
173393f7848bSJeff Kirsher 	"rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
173493f7848bSJeff Kirsher 	"tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
173593f7848bSJeff Kirsher 	"tx_heartbeat_errors", "tx_window_errors",
173693f7848bSJeff Kirsher 	/* device-specific stats */
1737e4b6c303SLuis R. Rodriguez 	"tx_boundary", "irq", "MSI", "MSIX",
173893f7848bSJeff Kirsher 	"read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
173993f7848bSJeff Kirsher 	"serial_number", "watchdog_resets",
174093f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
174193f7848bSJeff Kirsher 	"dca_capable_firmware", "dca_device_present",
174293f7848bSJeff Kirsher #endif
174393f7848bSJeff Kirsher 	"link_changes", "link_up", "dropped_link_overflow",
174493f7848bSJeff Kirsher 	"dropped_link_error_or_filtered",
174593f7848bSJeff Kirsher 	"dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
174693f7848bSJeff Kirsher 	"dropped_unicast_filtered", "dropped_multicast_filtered",
174793f7848bSJeff Kirsher 	"dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
174893f7848bSJeff Kirsher 	"dropped_no_big_buffer"
174993f7848bSJeff Kirsher };
175093f7848bSJeff Kirsher 
175193f7848bSJeff Kirsher static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
175293f7848bSJeff Kirsher 	"----------- slice ---------",
175393f7848bSJeff Kirsher 	"tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
175493f7848bSJeff Kirsher 	"rx_small_cnt", "rx_big_cnt",
175593f7848bSJeff Kirsher 	"wake_queue", "stop_queue", "tx_linearized",
175693f7848bSJeff Kirsher };
175793f7848bSJeff Kirsher 
175893f7848bSJeff Kirsher #define MYRI10GE_NET_STATS_LEN      21
175993f7848bSJeff Kirsher #define MYRI10GE_MAIN_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_main_stats)
176093f7848bSJeff Kirsher #define MYRI10GE_SLICE_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_slice_stats)
176193f7848bSJeff Kirsher 
176293f7848bSJeff Kirsher static void
176393f7848bSJeff Kirsher myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
176493f7848bSJeff Kirsher {
176593f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
176693f7848bSJeff Kirsher 	int i;
176793f7848bSJeff Kirsher 
176893f7848bSJeff Kirsher 	switch (stringset) {
176993f7848bSJeff Kirsher 	case ETH_SS_STATS:
177093f7848bSJeff Kirsher 		memcpy(data, *myri10ge_gstrings_main_stats,
177193f7848bSJeff Kirsher 		       sizeof(myri10ge_gstrings_main_stats));
177293f7848bSJeff Kirsher 		data += sizeof(myri10ge_gstrings_main_stats);
177393f7848bSJeff Kirsher 		for (i = 0; i < mgp->num_slices; i++) {
177493f7848bSJeff Kirsher 			memcpy(data, *myri10ge_gstrings_slice_stats,
177593f7848bSJeff Kirsher 			       sizeof(myri10ge_gstrings_slice_stats));
177693f7848bSJeff Kirsher 			data += sizeof(myri10ge_gstrings_slice_stats);
177793f7848bSJeff Kirsher 		}
177893f7848bSJeff Kirsher 		break;
177993f7848bSJeff Kirsher 	}
178093f7848bSJeff Kirsher }
178193f7848bSJeff Kirsher 
178293f7848bSJeff Kirsher static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
178393f7848bSJeff Kirsher {
178493f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
178593f7848bSJeff Kirsher 
178693f7848bSJeff Kirsher 	switch (sset) {
178793f7848bSJeff Kirsher 	case ETH_SS_STATS:
178893f7848bSJeff Kirsher 		return MYRI10GE_MAIN_STATS_LEN +
178993f7848bSJeff Kirsher 		    mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
179093f7848bSJeff Kirsher 	default:
179193f7848bSJeff Kirsher 		return -EOPNOTSUPP;
179293f7848bSJeff Kirsher 	}
179393f7848bSJeff Kirsher }
179493f7848bSJeff Kirsher 
179593f7848bSJeff Kirsher static void
179693f7848bSJeff Kirsher myri10ge_get_ethtool_stats(struct net_device *netdev,
179793f7848bSJeff Kirsher 			   struct ethtool_stats *stats, u64 * data)
179893f7848bSJeff Kirsher {
179993f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
180093f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
180193f7848bSJeff Kirsher 	struct rtnl_link_stats64 link_stats;
180293f7848bSJeff Kirsher 	int slice;
180393f7848bSJeff Kirsher 	int i;
180493f7848bSJeff Kirsher 
180593f7848bSJeff Kirsher 	/* force stats update */
180693f7848bSJeff Kirsher 	memset(&link_stats, 0, sizeof(link_stats));
180793f7848bSJeff Kirsher 	(void)myri10ge_get_stats(netdev, &link_stats);
180893f7848bSJeff Kirsher 	for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
180993f7848bSJeff Kirsher 		data[i] = ((u64 *)&link_stats)[i];
181093f7848bSJeff Kirsher 
181193f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->tx_boundary;
181293f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->pdev->irq;
181393f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->msi_enabled;
181493f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->msix_enabled;
181593f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->read_dma;
181693f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->write_dma;
181793f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->read_write_dma;
181893f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->serial_number;
181993f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->watchdog_resets;
182093f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
182193f7848bSJeff Kirsher 	data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
182293f7848bSJeff Kirsher 	data[i++] = (unsigned int)(mgp->dca_enabled);
182393f7848bSJeff Kirsher #endif
182493f7848bSJeff Kirsher 	data[i++] = (unsigned int)mgp->link_changes;
182593f7848bSJeff Kirsher 
182693f7848bSJeff Kirsher 	/* firmware stats are useful only in the first slice */
182793f7848bSJeff Kirsher 	ss = &mgp->ss[0];
182893f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
182993f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
183093f7848bSJeff Kirsher 	data[i++] =
183193f7848bSJeff Kirsher 	    (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
183293f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
183393f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
183493f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
183593f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
183693f7848bSJeff Kirsher 	data[i++] =
183793f7848bSJeff Kirsher 	    (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
183893f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
183993f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
184093f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
184193f7848bSJeff Kirsher 	data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
184293f7848bSJeff Kirsher 
184393f7848bSJeff Kirsher 	for (slice = 0; slice < mgp->num_slices; slice++) {
184493f7848bSJeff Kirsher 		ss = &mgp->ss[slice];
184593f7848bSJeff Kirsher 		data[i++] = slice;
184693f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.pkt_start;
184793f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.pkt_done;
184893f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.req;
184993f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.done;
185093f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->rx_small.cnt;
185193f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->rx_big.cnt;
185293f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.wake_queue;
185393f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.stop_queue;
185493f7848bSJeff Kirsher 		data[i++] = (unsigned int)ss->tx.linearized;
185593f7848bSJeff Kirsher 	}
185693f7848bSJeff Kirsher }
185793f7848bSJeff Kirsher 
185893f7848bSJeff Kirsher static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
185993f7848bSJeff Kirsher {
186093f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
186193f7848bSJeff Kirsher 	mgp->msg_enable = value;
186293f7848bSJeff Kirsher }
186393f7848bSJeff Kirsher 
186493f7848bSJeff Kirsher static u32 myri10ge_get_msglevel(struct net_device *netdev)
186593f7848bSJeff Kirsher {
186693f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
186793f7848bSJeff Kirsher 	return mgp->msg_enable;
186893f7848bSJeff Kirsher }
186993f7848bSJeff Kirsher 
187093f7848bSJeff Kirsher /*
187193f7848bSJeff Kirsher  * Use a low-level command to change the LED behavior. Rather than
187293f7848bSJeff Kirsher  * blinking (which is the normal case), when identify is used, the
187393f7848bSJeff Kirsher  * yellow LED turns solid.
187493f7848bSJeff Kirsher  */
187593f7848bSJeff Kirsher static int myri10ge_led(struct myri10ge_priv *mgp, int on)
187693f7848bSJeff Kirsher {
187793f7848bSJeff Kirsher 	struct mcp_gen_header *hdr;
187893f7848bSJeff Kirsher 	struct device *dev = &mgp->pdev->dev;
187993f7848bSJeff Kirsher 	size_t hdr_off, pattern_off, hdr_len;
188093f7848bSJeff Kirsher 	u32 pattern = 0xfffffffe;
188193f7848bSJeff Kirsher 
188293f7848bSJeff Kirsher 	/* find running firmware header */
188393f7848bSJeff Kirsher 	hdr_off = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
188493f7848bSJeff Kirsher 	if ((hdr_off & 3) || hdr_off + sizeof(*hdr) > mgp->sram_size) {
188593f7848bSJeff Kirsher 		dev_err(dev, "Running firmware has bad header offset (%d)\n",
188693f7848bSJeff Kirsher 			(int)hdr_off);
188793f7848bSJeff Kirsher 		return -EIO;
188893f7848bSJeff Kirsher 	}
188993f7848bSJeff Kirsher 	hdr_len = swab32(readl(mgp->sram + hdr_off +
189093f7848bSJeff Kirsher 			       offsetof(struct mcp_gen_header, header_length)));
189193f7848bSJeff Kirsher 	pattern_off = hdr_off + offsetof(struct mcp_gen_header, led_pattern);
189293f7848bSJeff Kirsher 	if (pattern_off >= (hdr_len + hdr_off)) {
189393f7848bSJeff Kirsher 		dev_info(dev, "Firmware does not support LED identification\n");
189493f7848bSJeff Kirsher 		return -EINVAL;
189593f7848bSJeff Kirsher 	}
189693f7848bSJeff Kirsher 	if (!on)
189793f7848bSJeff Kirsher 		pattern = swab32(readl(mgp->sram + pattern_off + 4));
189859e955edSAndrew Gallatin 	writel(swab32(pattern), mgp->sram + pattern_off);
189993f7848bSJeff Kirsher 	return 0;
190093f7848bSJeff Kirsher }
190193f7848bSJeff Kirsher 
190293f7848bSJeff Kirsher static int
190393f7848bSJeff Kirsher myri10ge_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
190493f7848bSJeff Kirsher {
190593f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(netdev);
190693f7848bSJeff Kirsher 	int rc;
190793f7848bSJeff Kirsher 
190893f7848bSJeff Kirsher 	switch (state) {
190993f7848bSJeff Kirsher 	case ETHTOOL_ID_ACTIVE:
191093f7848bSJeff Kirsher 		rc = myri10ge_led(mgp, 1);
191193f7848bSJeff Kirsher 		break;
191293f7848bSJeff Kirsher 
191393f7848bSJeff Kirsher 	case ETHTOOL_ID_INACTIVE:
191493f7848bSJeff Kirsher 		rc =  myri10ge_led(mgp, 0);
191593f7848bSJeff Kirsher 		break;
191693f7848bSJeff Kirsher 
191793f7848bSJeff Kirsher 	default:
191893f7848bSJeff Kirsher 		rc = -EINVAL;
191993f7848bSJeff Kirsher 	}
192093f7848bSJeff Kirsher 
192193f7848bSJeff Kirsher 	return rc;
192293f7848bSJeff Kirsher }
192393f7848bSJeff Kirsher 
192493f7848bSJeff Kirsher static const struct ethtool_ops myri10ge_ethtool_ops = {
192593f7848bSJeff Kirsher 	.get_drvinfo = myri10ge_get_drvinfo,
192693f7848bSJeff Kirsher 	.get_coalesce = myri10ge_get_coalesce,
192793f7848bSJeff Kirsher 	.set_coalesce = myri10ge_set_coalesce,
192893f7848bSJeff Kirsher 	.get_pauseparam = myri10ge_get_pauseparam,
192993f7848bSJeff Kirsher 	.set_pauseparam = myri10ge_set_pauseparam,
193093f7848bSJeff Kirsher 	.get_ringparam = myri10ge_get_ringparam,
193193f7848bSJeff Kirsher 	.get_link = ethtool_op_get_link,
193293f7848bSJeff Kirsher 	.get_strings = myri10ge_get_strings,
193393f7848bSJeff Kirsher 	.get_sset_count = myri10ge_get_sset_count,
193493f7848bSJeff Kirsher 	.get_ethtool_stats = myri10ge_get_ethtool_stats,
193593f7848bSJeff Kirsher 	.set_msglevel = myri10ge_set_msglevel,
193693f7848bSJeff Kirsher 	.get_msglevel = myri10ge_get_msglevel,
193793f7848bSJeff Kirsher 	.set_phys_id = myri10ge_phys_id,
1938cf901656SPhilippe Reynes 	.get_link_ksettings = myri10ge_get_link_ksettings,
193993f7848bSJeff Kirsher };
194093f7848bSJeff Kirsher 
194193f7848bSJeff Kirsher static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
194293f7848bSJeff Kirsher {
194393f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = ss->mgp;
194493f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
194593f7848bSJeff Kirsher 	struct net_device *dev = mgp->dev;
194693f7848bSJeff Kirsher 	int tx_ring_size, rx_ring_size;
194793f7848bSJeff Kirsher 	int tx_ring_entries, rx_ring_entries;
194893f7848bSJeff Kirsher 	int i, slice, status;
194993f7848bSJeff Kirsher 	size_t bytes;
195093f7848bSJeff Kirsher 
195193f7848bSJeff Kirsher 	/* get ring sizes */
195293f7848bSJeff Kirsher 	slice = ss - mgp->ss;
195393f7848bSJeff Kirsher 	cmd.data0 = slice;
195493f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
195593f7848bSJeff Kirsher 	tx_ring_size = cmd.data0;
195693f7848bSJeff Kirsher 	cmd.data0 = slice;
195793f7848bSJeff Kirsher 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
195893f7848bSJeff Kirsher 	if (status != 0)
195993f7848bSJeff Kirsher 		return status;
196093f7848bSJeff Kirsher 	rx_ring_size = cmd.data0;
196193f7848bSJeff Kirsher 
196293f7848bSJeff Kirsher 	tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
196393f7848bSJeff Kirsher 	rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
196493f7848bSJeff Kirsher 	ss->tx.mask = tx_ring_entries - 1;
196593f7848bSJeff Kirsher 	ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
196693f7848bSJeff Kirsher 
196793f7848bSJeff Kirsher 	status = -ENOMEM;
196893f7848bSJeff Kirsher 
196993f7848bSJeff Kirsher 	/* allocate the host shadow rings */
197093f7848bSJeff Kirsher 
197193f7848bSJeff Kirsher 	bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
197293f7848bSJeff Kirsher 	    * sizeof(*ss->tx.req_list);
197393f7848bSJeff Kirsher 	ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
197493f7848bSJeff Kirsher 	if (ss->tx.req_bytes == NULL)
197593f7848bSJeff Kirsher 		goto abort_with_nothing;
197693f7848bSJeff Kirsher 
197793f7848bSJeff Kirsher 	/* ensure req_list entries are aligned to 8 bytes */
197893f7848bSJeff Kirsher 	ss->tx.req_list = (struct mcp_kreq_ether_send *)
197993f7848bSJeff Kirsher 	    ALIGN((unsigned long)ss->tx.req_bytes, 8);
198093f7848bSJeff Kirsher 	ss->tx.queue_active = 0;
198193f7848bSJeff Kirsher 
198293f7848bSJeff Kirsher 	bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
198393f7848bSJeff Kirsher 	ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
198493f7848bSJeff Kirsher 	if (ss->rx_small.shadow == NULL)
198593f7848bSJeff Kirsher 		goto abort_with_tx_req_bytes;
198693f7848bSJeff Kirsher 
198793f7848bSJeff Kirsher 	bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
198893f7848bSJeff Kirsher 	ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
198993f7848bSJeff Kirsher 	if (ss->rx_big.shadow == NULL)
199093f7848bSJeff Kirsher 		goto abort_with_rx_small_shadow;
199193f7848bSJeff Kirsher 
199293f7848bSJeff Kirsher 	/* allocate the host info rings */
199393f7848bSJeff Kirsher 
199493f7848bSJeff Kirsher 	bytes = tx_ring_entries * sizeof(*ss->tx.info);
199593f7848bSJeff Kirsher 	ss->tx.info = kzalloc(bytes, GFP_KERNEL);
199693f7848bSJeff Kirsher 	if (ss->tx.info == NULL)
199793f7848bSJeff Kirsher 		goto abort_with_rx_big_shadow;
199893f7848bSJeff Kirsher 
199993f7848bSJeff Kirsher 	bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
200093f7848bSJeff Kirsher 	ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
200193f7848bSJeff Kirsher 	if (ss->rx_small.info == NULL)
200293f7848bSJeff Kirsher 		goto abort_with_tx_info;
200393f7848bSJeff Kirsher 
200493f7848bSJeff Kirsher 	bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
200593f7848bSJeff Kirsher 	ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
200693f7848bSJeff Kirsher 	if (ss->rx_big.info == NULL)
200793f7848bSJeff Kirsher 		goto abort_with_rx_small_info;
200893f7848bSJeff Kirsher 
200993f7848bSJeff Kirsher 	/* Fill the receive rings */
201093f7848bSJeff Kirsher 	ss->rx_big.cnt = 0;
201193f7848bSJeff Kirsher 	ss->rx_small.cnt = 0;
201293f7848bSJeff Kirsher 	ss->rx_big.fill_cnt = 0;
201393f7848bSJeff Kirsher 	ss->rx_small.fill_cnt = 0;
201493f7848bSJeff Kirsher 	ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
201593f7848bSJeff Kirsher 	ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
201693f7848bSJeff Kirsher 	ss->rx_small.watchdog_needed = 0;
201793f7848bSJeff Kirsher 	ss->rx_big.watchdog_needed = 0;
201893f7848bSJeff Kirsher 	if (mgp->small_bytes == 0) {
201993f7848bSJeff Kirsher 		ss->rx_small.fill_cnt = ss->rx_small.mask + 1;
202093f7848bSJeff Kirsher 	} else {
202193f7848bSJeff Kirsher 		myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
202293f7848bSJeff Kirsher 					mgp->small_bytes + MXGEFW_PAD, 0);
202393f7848bSJeff Kirsher 	}
202493f7848bSJeff Kirsher 
202593f7848bSJeff Kirsher 	if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
202693f7848bSJeff Kirsher 		netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
202793f7848bSJeff Kirsher 			   slice, ss->rx_small.fill_cnt);
202893f7848bSJeff Kirsher 		goto abort_with_rx_small_ring;
202993f7848bSJeff Kirsher 	}
203093f7848bSJeff Kirsher 
203193f7848bSJeff Kirsher 	myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
203293f7848bSJeff Kirsher 	if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
203393f7848bSJeff Kirsher 		netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
203493f7848bSJeff Kirsher 			   slice, ss->rx_big.fill_cnt);
203593f7848bSJeff Kirsher 		goto abort_with_rx_big_ring;
203693f7848bSJeff Kirsher 	}
203793f7848bSJeff Kirsher 
203893f7848bSJeff Kirsher 	return 0;
203993f7848bSJeff Kirsher 
204093f7848bSJeff Kirsher abort_with_rx_big_ring:
204193f7848bSJeff Kirsher 	for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
204293f7848bSJeff Kirsher 		int idx = i & ss->rx_big.mask;
204393f7848bSJeff Kirsher 		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
204493f7848bSJeff Kirsher 				       mgp->big_bytes);
204593f7848bSJeff Kirsher 		put_page(ss->rx_big.info[idx].page);
204693f7848bSJeff Kirsher 	}
204793f7848bSJeff Kirsher 
204893f7848bSJeff Kirsher abort_with_rx_small_ring:
204993f7848bSJeff Kirsher 	if (mgp->small_bytes == 0)
205093f7848bSJeff Kirsher 		ss->rx_small.fill_cnt = ss->rx_small.cnt;
205193f7848bSJeff Kirsher 	for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
205293f7848bSJeff Kirsher 		int idx = i & ss->rx_small.mask;
205393f7848bSJeff Kirsher 		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
205493f7848bSJeff Kirsher 				       mgp->small_bytes + MXGEFW_PAD);
205593f7848bSJeff Kirsher 		put_page(ss->rx_small.info[idx].page);
205693f7848bSJeff Kirsher 	}
205793f7848bSJeff Kirsher 
205893f7848bSJeff Kirsher 	kfree(ss->rx_big.info);
205993f7848bSJeff Kirsher 
206093f7848bSJeff Kirsher abort_with_rx_small_info:
206193f7848bSJeff Kirsher 	kfree(ss->rx_small.info);
206293f7848bSJeff Kirsher 
206393f7848bSJeff Kirsher abort_with_tx_info:
206493f7848bSJeff Kirsher 	kfree(ss->tx.info);
206593f7848bSJeff Kirsher 
206693f7848bSJeff Kirsher abort_with_rx_big_shadow:
206793f7848bSJeff Kirsher 	kfree(ss->rx_big.shadow);
206893f7848bSJeff Kirsher 
206993f7848bSJeff Kirsher abort_with_rx_small_shadow:
207093f7848bSJeff Kirsher 	kfree(ss->rx_small.shadow);
207193f7848bSJeff Kirsher 
207293f7848bSJeff Kirsher abort_with_tx_req_bytes:
207393f7848bSJeff Kirsher 	kfree(ss->tx.req_bytes);
207493f7848bSJeff Kirsher 	ss->tx.req_bytes = NULL;
207593f7848bSJeff Kirsher 	ss->tx.req_list = NULL;
207693f7848bSJeff Kirsher 
207793f7848bSJeff Kirsher abort_with_nothing:
207893f7848bSJeff Kirsher 	return status;
207993f7848bSJeff Kirsher }
208093f7848bSJeff Kirsher 
208193f7848bSJeff Kirsher static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
208293f7848bSJeff Kirsher {
208393f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = ss->mgp;
208493f7848bSJeff Kirsher 	struct sk_buff *skb;
208593f7848bSJeff Kirsher 	struct myri10ge_tx_buf *tx;
208693f7848bSJeff Kirsher 	int i, len, idx;
208793f7848bSJeff Kirsher 
208893f7848bSJeff Kirsher 	/* If not allocated, skip it */
208993f7848bSJeff Kirsher 	if (ss->tx.req_list == NULL)
209093f7848bSJeff Kirsher 		return;
209193f7848bSJeff Kirsher 
209293f7848bSJeff Kirsher 	for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
209393f7848bSJeff Kirsher 		idx = i & ss->rx_big.mask;
209493f7848bSJeff Kirsher 		if (i == ss->rx_big.fill_cnt - 1)
209593f7848bSJeff Kirsher 			ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
209693f7848bSJeff Kirsher 		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
209793f7848bSJeff Kirsher 				       mgp->big_bytes);
209893f7848bSJeff Kirsher 		put_page(ss->rx_big.info[idx].page);
209993f7848bSJeff Kirsher 	}
210093f7848bSJeff Kirsher 
210193f7848bSJeff Kirsher 	if (mgp->small_bytes == 0)
210293f7848bSJeff Kirsher 		ss->rx_small.fill_cnt = ss->rx_small.cnt;
210393f7848bSJeff Kirsher 	for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
210493f7848bSJeff Kirsher 		idx = i & ss->rx_small.mask;
210593f7848bSJeff Kirsher 		if (i == ss->rx_small.fill_cnt - 1)
210693f7848bSJeff Kirsher 			ss->rx_small.info[idx].page_offset =
210793f7848bSJeff Kirsher 			    MYRI10GE_ALLOC_SIZE;
210893f7848bSJeff Kirsher 		myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
210993f7848bSJeff Kirsher 				       mgp->small_bytes + MXGEFW_PAD);
211093f7848bSJeff Kirsher 		put_page(ss->rx_small.info[idx].page);
211193f7848bSJeff Kirsher 	}
211293f7848bSJeff Kirsher 	tx = &ss->tx;
211393f7848bSJeff Kirsher 	while (tx->done != tx->req) {
211493f7848bSJeff Kirsher 		idx = tx->done & tx->mask;
211593f7848bSJeff Kirsher 		skb = tx->info[idx].skb;
211693f7848bSJeff Kirsher 
211793f7848bSJeff Kirsher 		/* Mark as free */
211893f7848bSJeff Kirsher 		tx->info[idx].skb = NULL;
211993f7848bSJeff Kirsher 		tx->done++;
212093f7848bSJeff Kirsher 		len = dma_unmap_len(&tx->info[idx], len);
212193f7848bSJeff Kirsher 		dma_unmap_len_set(&tx->info[idx], len, 0);
212293f7848bSJeff Kirsher 		if (skb) {
212393f7848bSJeff Kirsher 			ss->stats.tx_dropped++;
212493f7848bSJeff Kirsher 			dev_kfree_skb_any(skb);
212593f7848bSJeff Kirsher 			if (len)
212693f7848bSJeff Kirsher 				pci_unmap_single(mgp->pdev,
212793f7848bSJeff Kirsher 						 dma_unmap_addr(&tx->info[idx],
212893f7848bSJeff Kirsher 								bus), len,
212993f7848bSJeff Kirsher 						 PCI_DMA_TODEVICE);
213093f7848bSJeff Kirsher 		} else {
213193f7848bSJeff Kirsher 			if (len)
213293f7848bSJeff Kirsher 				pci_unmap_page(mgp->pdev,
213393f7848bSJeff Kirsher 					       dma_unmap_addr(&tx->info[idx],
213493f7848bSJeff Kirsher 							      bus), len,
213593f7848bSJeff Kirsher 					       PCI_DMA_TODEVICE);
213693f7848bSJeff Kirsher 		}
213793f7848bSJeff Kirsher 	}
213893f7848bSJeff Kirsher 	kfree(ss->rx_big.info);
213993f7848bSJeff Kirsher 
214093f7848bSJeff Kirsher 	kfree(ss->rx_small.info);
214193f7848bSJeff Kirsher 
214293f7848bSJeff Kirsher 	kfree(ss->tx.info);
214393f7848bSJeff Kirsher 
214493f7848bSJeff Kirsher 	kfree(ss->rx_big.shadow);
214593f7848bSJeff Kirsher 
214693f7848bSJeff Kirsher 	kfree(ss->rx_small.shadow);
214793f7848bSJeff Kirsher 
214893f7848bSJeff Kirsher 	kfree(ss->tx.req_bytes);
214993f7848bSJeff Kirsher 	ss->tx.req_bytes = NULL;
215093f7848bSJeff Kirsher 	ss->tx.req_list = NULL;
215193f7848bSJeff Kirsher }
215293f7848bSJeff Kirsher 
215393f7848bSJeff Kirsher static int myri10ge_request_irq(struct myri10ge_priv *mgp)
215493f7848bSJeff Kirsher {
215593f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
215693f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
215793f7848bSJeff Kirsher 	struct net_device *netdev = mgp->dev;
215893f7848bSJeff Kirsher 	int i;
215993f7848bSJeff Kirsher 	int status;
216093f7848bSJeff Kirsher 
216193f7848bSJeff Kirsher 	mgp->msi_enabled = 0;
216293f7848bSJeff Kirsher 	mgp->msix_enabled = 0;
216393f7848bSJeff Kirsher 	status = 0;
216493f7848bSJeff Kirsher 	if (myri10ge_msi) {
216593f7848bSJeff Kirsher 		if (mgp->num_slices > 1) {
21660729cc0cSAlexander Gordeev 			status = pci_enable_msix_range(pdev, mgp->msix_vectors,
21670729cc0cSAlexander Gordeev 					mgp->num_slices, mgp->num_slices);
21680729cc0cSAlexander Gordeev 			if (status < 0) {
216993f7848bSJeff Kirsher 				dev_err(&pdev->dev,
217093f7848bSJeff Kirsher 					"Error %d setting up MSI-X\n", status);
217193f7848bSJeff Kirsher 				return status;
217293f7848bSJeff Kirsher 			}
21730729cc0cSAlexander Gordeev 			mgp->msix_enabled = 1;
217493f7848bSJeff Kirsher 		}
217593f7848bSJeff Kirsher 		if (mgp->msix_enabled == 0) {
217693f7848bSJeff Kirsher 			status = pci_enable_msi(pdev);
217793f7848bSJeff Kirsher 			if (status != 0) {
217893f7848bSJeff Kirsher 				dev_err(&pdev->dev,
217993f7848bSJeff Kirsher 					"Error %d setting up MSI; falling back to xPIC\n",
218093f7848bSJeff Kirsher 					status);
218193f7848bSJeff Kirsher 			} else {
218293f7848bSJeff Kirsher 				mgp->msi_enabled = 1;
218393f7848bSJeff Kirsher 			}
218493f7848bSJeff Kirsher 		}
218593f7848bSJeff Kirsher 	}
218693f7848bSJeff Kirsher 	if (mgp->msix_enabled) {
218793f7848bSJeff Kirsher 		for (i = 0; i < mgp->num_slices; i++) {
218893f7848bSJeff Kirsher 			ss = &mgp->ss[i];
218993f7848bSJeff Kirsher 			snprintf(ss->irq_desc, sizeof(ss->irq_desc),
219093f7848bSJeff Kirsher 				 "%s:slice-%d", netdev->name, i);
219193f7848bSJeff Kirsher 			status = request_irq(mgp->msix_vectors[i].vector,
219293f7848bSJeff Kirsher 					     myri10ge_intr, 0, ss->irq_desc,
219393f7848bSJeff Kirsher 					     ss);
219493f7848bSJeff Kirsher 			if (status != 0) {
219593f7848bSJeff Kirsher 				dev_err(&pdev->dev,
219693f7848bSJeff Kirsher 					"slice %d failed to allocate IRQ\n", i);
219793f7848bSJeff Kirsher 				i--;
219893f7848bSJeff Kirsher 				while (i >= 0) {
219993f7848bSJeff Kirsher 					free_irq(mgp->msix_vectors[i].vector,
220093f7848bSJeff Kirsher 						 &mgp->ss[i]);
220193f7848bSJeff Kirsher 					i--;
220293f7848bSJeff Kirsher 				}
220393f7848bSJeff Kirsher 				pci_disable_msix(pdev);
220493f7848bSJeff Kirsher 				return status;
220593f7848bSJeff Kirsher 			}
220693f7848bSJeff Kirsher 		}
220793f7848bSJeff Kirsher 	} else {
220893f7848bSJeff Kirsher 		status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
220993f7848bSJeff Kirsher 				     mgp->dev->name, &mgp->ss[0]);
221093f7848bSJeff Kirsher 		if (status != 0) {
221193f7848bSJeff Kirsher 			dev_err(&pdev->dev, "failed to allocate IRQ\n");
221293f7848bSJeff Kirsher 			if (mgp->msi_enabled)
221393f7848bSJeff Kirsher 				pci_disable_msi(pdev);
221493f7848bSJeff Kirsher 		}
221593f7848bSJeff Kirsher 	}
221693f7848bSJeff Kirsher 	return status;
221793f7848bSJeff Kirsher }
221893f7848bSJeff Kirsher 
221993f7848bSJeff Kirsher static void myri10ge_free_irq(struct myri10ge_priv *mgp)
222093f7848bSJeff Kirsher {
222193f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
222293f7848bSJeff Kirsher 	int i;
222393f7848bSJeff Kirsher 
222493f7848bSJeff Kirsher 	if (mgp->msix_enabled) {
222593f7848bSJeff Kirsher 		for (i = 0; i < mgp->num_slices; i++)
222693f7848bSJeff Kirsher 			free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
222793f7848bSJeff Kirsher 	} else {
222893f7848bSJeff Kirsher 		free_irq(pdev->irq, &mgp->ss[0]);
222993f7848bSJeff Kirsher 	}
223093f7848bSJeff Kirsher 	if (mgp->msi_enabled)
223193f7848bSJeff Kirsher 		pci_disable_msi(pdev);
223293f7848bSJeff Kirsher 	if (mgp->msix_enabled)
223393f7848bSJeff Kirsher 		pci_disable_msix(pdev);
223493f7848bSJeff Kirsher }
223593f7848bSJeff Kirsher 
223693f7848bSJeff Kirsher static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
223793f7848bSJeff Kirsher {
223893f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
223993f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
224093f7848bSJeff Kirsher 	int status;
224193f7848bSJeff Kirsher 
224293f7848bSJeff Kirsher 	ss = &mgp->ss[slice];
224393f7848bSJeff Kirsher 	status = 0;
224493f7848bSJeff Kirsher 	if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
224593f7848bSJeff Kirsher 		cmd.data0 = slice;
224693f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
224793f7848bSJeff Kirsher 					   &cmd, 0);
224893f7848bSJeff Kirsher 		ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
224993f7848bSJeff Kirsher 		    (mgp->sram + cmd.data0);
225093f7848bSJeff Kirsher 	}
225193f7848bSJeff Kirsher 	cmd.data0 = slice;
225293f7848bSJeff Kirsher 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
225393f7848bSJeff Kirsher 				    &cmd, 0);
225493f7848bSJeff Kirsher 	ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
225593f7848bSJeff Kirsher 	    (mgp->sram + cmd.data0);
225693f7848bSJeff Kirsher 
225793f7848bSJeff Kirsher 	cmd.data0 = slice;
225893f7848bSJeff Kirsher 	status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
225993f7848bSJeff Kirsher 	ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
226093f7848bSJeff Kirsher 	    (mgp->sram + cmd.data0);
226193f7848bSJeff Kirsher 
226293f7848bSJeff Kirsher 	ss->tx.send_go = (__iomem __be32 *)
226393f7848bSJeff Kirsher 	    (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
226493f7848bSJeff Kirsher 	ss->tx.send_stop = (__iomem __be32 *)
226593f7848bSJeff Kirsher 	    (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
226693f7848bSJeff Kirsher 	return status;
226793f7848bSJeff Kirsher 
226893f7848bSJeff Kirsher }
226993f7848bSJeff Kirsher 
227093f7848bSJeff Kirsher static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
227193f7848bSJeff Kirsher {
227293f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
227393f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
227493f7848bSJeff Kirsher 	int status;
227593f7848bSJeff Kirsher 
227693f7848bSJeff Kirsher 	ss = &mgp->ss[slice];
227793f7848bSJeff Kirsher 	cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
227893f7848bSJeff Kirsher 	cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
227993f7848bSJeff Kirsher 	cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
228093f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
228193f7848bSJeff Kirsher 	if (status == -ENOSYS) {
228293f7848bSJeff Kirsher 		dma_addr_t bus = ss->fw_stats_bus;
228393f7848bSJeff Kirsher 		if (slice != 0)
228493f7848bSJeff Kirsher 			return -EINVAL;
228593f7848bSJeff Kirsher 		bus += offsetof(struct mcp_irq_data, send_done_count);
228693f7848bSJeff Kirsher 		cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
228793f7848bSJeff Kirsher 		cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
228893f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp,
228993f7848bSJeff Kirsher 					   MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
229093f7848bSJeff Kirsher 					   &cmd, 0);
229193f7848bSJeff Kirsher 		/* Firmware cannot support multicast without STATS_DMA_V2 */
229293f7848bSJeff Kirsher 		mgp->fw_multicast_support = 0;
229393f7848bSJeff Kirsher 	} else {
229493f7848bSJeff Kirsher 		mgp->fw_multicast_support = 1;
229593f7848bSJeff Kirsher 	}
229693f7848bSJeff Kirsher 	return 0;
229793f7848bSJeff Kirsher }
229893f7848bSJeff Kirsher 
229993f7848bSJeff Kirsher static int myri10ge_open(struct net_device *dev)
230093f7848bSJeff Kirsher {
230193f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
230293f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
230393f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
230493f7848bSJeff Kirsher 	int i, status, big_pow2, slice;
230559e955edSAndrew Gallatin 	u8 __iomem *itable;
230693f7848bSJeff Kirsher 
230793f7848bSJeff Kirsher 	if (mgp->running != MYRI10GE_ETH_STOPPED)
230893f7848bSJeff Kirsher 		return -EBUSY;
230993f7848bSJeff Kirsher 
231093f7848bSJeff Kirsher 	mgp->running = MYRI10GE_ETH_STARTING;
231193f7848bSJeff Kirsher 	status = myri10ge_reset(mgp);
231293f7848bSJeff Kirsher 	if (status != 0) {
231393f7848bSJeff Kirsher 		netdev_err(dev, "failed reset\n");
231493f7848bSJeff Kirsher 		goto abort_with_nothing;
231593f7848bSJeff Kirsher 	}
231693f7848bSJeff Kirsher 
231793f7848bSJeff Kirsher 	if (mgp->num_slices > 1) {
231893f7848bSJeff Kirsher 		cmd.data0 = mgp->num_slices;
231993f7848bSJeff Kirsher 		cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
232093f7848bSJeff Kirsher 		if (mgp->dev->real_num_tx_queues > 1)
232193f7848bSJeff Kirsher 			cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
232293f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
232393f7848bSJeff Kirsher 					   &cmd, 0);
232493f7848bSJeff Kirsher 		if (status != 0) {
232593f7848bSJeff Kirsher 			netdev_err(dev, "failed to set number of slices\n");
232693f7848bSJeff Kirsher 			goto abort_with_nothing;
232793f7848bSJeff Kirsher 		}
232893f7848bSJeff Kirsher 		/* setup the indirection table */
232993f7848bSJeff Kirsher 		cmd.data0 = mgp->num_slices;
233093f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
233193f7848bSJeff Kirsher 					   &cmd, 0);
233293f7848bSJeff Kirsher 
233393f7848bSJeff Kirsher 		status |= myri10ge_send_cmd(mgp,
233493f7848bSJeff Kirsher 					    MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
233593f7848bSJeff Kirsher 					    &cmd, 0);
233693f7848bSJeff Kirsher 		if (status != 0) {
233793f7848bSJeff Kirsher 			netdev_err(dev, "failed to setup rss tables\n");
233893f7848bSJeff Kirsher 			goto abort_with_nothing;
233993f7848bSJeff Kirsher 		}
234093f7848bSJeff Kirsher 
234193f7848bSJeff Kirsher 		/* just enable an identity mapping */
234293f7848bSJeff Kirsher 		itable = mgp->sram + cmd.data0;
234393f7848bSJeff Kirsher 		for (i = 0; i < mgp->num_slices; i++)
234493f7848bSJeff Kirsher 			__raw_writeb(i, &itable[i]);
234593f7848bSJeff Kirsher 
234693f7848bSJeff Kirsher 		cmd.data0 = 1;
234793f7848bSJeff Kirsher 		cmd.data1 = myri10ge_rss_hash;
234893f7848bSJeff Kirsher 		status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
234993f7848bSJeff Kirsher 					   &cmd, 0);
235093f7848bSJeff Kirsher 		if (status != 0) {
235193f7848bSJeff Kirsher 			netdev_err(dev, "failed to enable slices\n");
235293f7848bSJeff Kirsher 			goto abort_with_nothing;
235393f7848bSJeff Kirsher 		}
235493f7848bSJeff Kirsher 	}
235593f7848bSJeff Kirsher 
235693f7848bSJeff Kirsher 	status = myri10ge_request_irq(mgp);
235793f7848bSJeff Kirsher 	if (status != 0)
235893f7848bSJeff Kirsher 		goto abort_with_nothing;
235993f7848bSJeff Kirsher 
236093f7848bSJeff Kirsher 	/* decide what small buffer size to use.  For good TCP rx
236193f7848bSJeff Kirsher 	 * performance, it is important to not receive 1514 byte
236293f7848bSJeff Kirsher 	 * frames into jumbo buffers, as it confuses the socket buffer
236393f7848bSJeff Kirsher 	 * accounting code, leading to drops and erratic performance.
236493f7848bSJeff Kirsher 	 */
236593f7848bSJeff Kirsher 
236693f7848bSJeff Kirsher 	if (dev->mtu <= ETH_DATA_LEN)
236793f7848bSJeff Kirsher 		/* enough for a TCP header */
236893f7848bSJeff Kirsher 		mgp->small_bytes = (128 > SMP_CACHE_BYTES)
236993f7848bSJeff Kirsher 		    ? (128 - MXGEFW_PAD)
237093f7848bSJeff Kirsher 		    : (SMP_CACHE_BYTES - MXGEFW_PAD);
237193f7848bSJeff Kirsher 	else
237293f7848bSJeff Kirsher 		/* enough for a vlan encapsulated ETH_DATA_LEN frame */
237393f7848bSJeff Kirsher 		mgp->small_bytes = VLAN_ETH_FRAME_LEN;
237493f7848bSJeff Kirsher 
237593f7848bSJeff Kirsher 	/* Override the small buffer size? */
237693f7848bSJeff Kirsher 	if (myri10ge_small_bytes >= 0)
237793f7848bSJeff Kirsher 		mgp->small_bytes = myri10ge_small_bytes;
237893f7848bSJeff Kirsher 
237993f7848bSJeff Kirsher 	/* Firmware needs the big buff size as a power of 2.  Lie and
238093f7848bSJeff Kirsher 	 * tell him the buffer is larger, because we only use 1
238193f7848bSJeff Kirsher 	 * buffer/pkt, and the mtu will prevent overruns.
238293f7848bSJeff Kirsher 	 */
238393f7848bSJeff Kirsher 	big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
238493f7848bSJeff Kirsher 	if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
238593f7848bSJeff Kirsher 		while (!is_power_of_2(big_pow2))
238693f7848bSJeff Kirsher 			big_pow2++;
238793f7848bSJeff Kirsher 		mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
238893f7848bSJeff Kirsher 	} else {
238993f7848bSJeff Kirsher 		big_pow2 = MYRI10GE_ALLOC_SIZE;
239093f7848bSJeff Kirsher 		mgp->big_bytes = big_pow2;
239193f7848bSJeff Kirsher 	}
239293f7848bSJeff Kirsher 
239393f7848bSJeff Kirsher 	/* setup the per-slice data structures */
239493f7848bSJeff Kirsher 	for (slice = 0; slice < mgp->num_slices; slice++) {
239593f7848bSJeff Kirsher 		ss = &mgp->ss[slice];
239693f7848bSJeff Kirsher 
239793f7848bSJeff Kirsher 		status = myri10ge_get_txrx(mgp, slice);
239893f7848bSJeff Kirsher 		if (status != 0) {
239993f7848bSJeff Kirsher 			netdev_err(dev, "failed to get ring sizes or locations\n");
240093f7848bSJeff Kirsher 			goto abort_with_rings;
240193f7848bSJeff Kirsher 		}
240293f7848bSJeff Kirsher 		status = myri10ge_allocate_rings(ss);
240393f7848bSJeff Kirsher 		if (status != 0)
240493f7848bSJeff Kirsher 			goto abort_with_rings;
240593f7848bSJeff Kirsher 
240693f7848bSJeff Kirsher 		/* only firmware which supports multiple TX queues
240793f7848bSJeff Kirsher 		 * supports setting up the tx stats on non-zero
240893f7848bSJeff Kirsher 		 * slices */
240993f7848bSJeff Kirsher 		if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
241093f7848bSJeff Kirsher 			status = myri10ge_set_stats(mgp, slice);
241193f7848bSJeff Kirsher 		if (status) {
241293f7848bSJeff Kirsher 			netdev_err(dev, "Couldn't set stats DMA\n");
241393f7848bSJeff Kirsher 			goto abort_with_rings;
241493f7848bSJeff Kirsher 		}
241593f7848bSJeff Kirsher 
241693f7848bSJeff Kirsher 		/* must happen prior to any irq */
241793f7848bSJeff Kirsher 		napi_enable(&(ss)->napi);
241893f7848bSJeff Kirsher 	}
241993f7848bSJeff Kirsher 
242093f7848bSJeff Kirsher 	/* now give firmware buffers sizes, and MTU */
242193f7848bSJeff Kirsher 	cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
242293f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
242393f7848bSJeff Kirsher 	cmd.data0 = mgp->small_bytes;
242493f7848bSJeff Kirsher 	status |=
242593f7848bSJeff Kirsher 	    myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
242693f7848bSJeff Kirsher 	cmd.data0 = big_pow2;
242793f7848bSJeff Kirsher 	status |=
242893f7848bSJeff Kirsher 	    myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
242993f7848bSJeff Kirsher 	if (status) {
243093f7848bSJeff Kirsher 		netdev_err(dev, "Couldn't set buffer sizes\n");
243193f7848bSJeff Kirsher 		goto abort_with_rings;
243293f7848bSJeff Kirsher 	}
243393f7848bSJeff Kirsher 
243493f7848bSJeff Kirsher 	/*
243593f7848bSJeff Kirsher 	 * Set Linux style TSO mode; this is needed only on newer
243693f7848bSJeff Kirsher 	 *  firmware versions.  Older versions default to Linux
243793f7848bSJeff Kirsher 	 *  style TSO
243893f7848bSJeff Kirsher 	 */
243993f7848bSJeff Kirsher 	cmd.data0 = 0;
244093f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
244193f7848bSJeff Kirsher 	if (status && status != -ENOSYS) {
244293f7848bSJeff Kirsher 		netdev_err(dev, "Couldn't set TSO mode\n");
244393f7848bSJeff Kirsher 		goto abort_with_rings;
244493f7848bSJeff Kirsher 	}
244593f7848bSJeff Kirsher 
244693f7848bSJeff Kirsher 	mgp->link_state = ~0U;
244793f7848bSJeff Kirsher 	mgp->rdma_tags_available = 15;
244893f7848bSJeff Kirsher 
244993f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
245093f7848bSJeff Kirsher 	if (status) {
245193f7848bSJeff Kirsher 		netdev_err(dev, "Couldn't bring up link\n");
245293f7848bSJeff Kirsher 		goto abort_with_rings;
245393f7848bSJeff Kirsher 	}
245493f7848bSJeff Kirsher 
245593f7848bSJeff Kirsher 	mgp->running = MYRI10GE_ETH_RUNNING;
245693f7848bSJeff Kirsher 	mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
245793f7848bSJeff Kirsher 	add_timer(&mgp->watchdog_timer);
245893f7848bSJeff Kirsher 	netif_tx_wake_all_queues(dev);
245993f7848bSJeff Kirsher 
246093f7848bSJeff Kirsher 	return 0;
246193f7848bSJeff Kirsher 
246293f7848bSJeff Kirsher abort_with_rings:
246393f7848bSJeff Kirsher 	while (slice) {
246493f7848bSJeff Kirsher 		slice--;
246593f7848bSJeff Kirsher 		napi_disable(&mgp->ss[slice].napi);
246693f7848bSJeff Kirsher 	}
246793f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++)
246893f7848bSJeff Kirsher 		myri10ge_free_rings(&mgp->ss[i]);
246993f7848bSJeff Kirsher 
247093f7848bSJeff Kirsher 	myri10ge_free_irq(mgp);
247193f7848bSJeff Kirsher 
247293f7848bSJeff Kirsher abort_with_nothing:
247393f7848bSJeff Kirsher 	mgp->running = MYRI10GE_ETH_STOPPED;
247493f7848bSJeff Kirsher 	return -ENOMEM;
247593f7848bSJeff Kirsher }
247693f7848bSJeff Kirsher 
247793f7848bSJeff Kirsher static int myri10ge_close(struct net_device *dev)
247893f7848bSJeff Kirsher {
247993f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
248093f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
248193f7848bSJeff Kirsher 	int status, old_down_cnt;
248293f7848bSJeff Kirsher 	int i;
248393f7848bSJeff Kirsher 
248493f7848bSJeff Kirsher 	if (mgp->running != MYRI10GE_ETH_RUNNING)
248593f7848bSJeff Kirsher 		return 0;
248693f7848bSJeff Kirsher 
248793f7848bSJeff Kirsher 	if (mgp->ss[0].tx.req_bytes == NULL)
248893f7848bSJeff Kirsher 		return 0;
248993f7848bSJeff Kirsher 
249093f7848bSJeff Kirsher 	del_timer_sync(&mgp->watchdog_timer);
249193f7848bSJeff Kirsher 	mgp->running = MYRI10GE_ETH_STOPPING;
2492362108b5SEric Dumazet 	for (i = 0; i < mgp->num_slices; i++)
249393f7848bSJeff Kirsher 		napi_disable(&mgp->ss[i].napi);
2494362108b5SEric Dumazet 
249593f7848bSJeff Kirsher 	netif_carrier_off(dev);
249693f7848bSJeff Kirsher 
249793f7848bSJeff Kirsher 	netif_tx_stop_all_queues(dev);
249893f7848bSJeff Kirsher 	if (mgp->rebooted == 0) {
249993f7848bSJeff Kirsher 		old_down_cnt = mgp->down_cnt;
250093f7848bSJeff Kirsher 		mb();
250193f7848bSJeff Kirsher 		status =
250293f7848bSJeff Kirsher 		    myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
250393f7848bSJeff Kirsher 		if (status)
250493f7848bSJeff Kirsher 			netdev_err(dev, "Couldn't bring down link\n");
250593f7848bSJeff Kirsher 
250693f7848bSJeff Kirsher 		wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
250793f7848bSJeff Kirsher 				   HZ);
250893f7848bSJeff Kirsher 		if (old_down_cnt == mgp->down_cnt)
250993f7848bSJeff Kirsher 			netdev_err(dev, "never got down irq\n");
251093f7848bSJeff Kirsher 	}
251193f7848bSJeff Kirsher 	netif_tx_disable(dev);
251293f7848bSJeff Kirsher 	myri10ge_free_irq(mgp);
251393f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++)
251493f7848bSJeff Kirsher 		myri10ge_free_rings(&mgp->ss[i]);
251593f7848bSJeff Kirsher 
251693f7848bSJeff Kirsher 	mgp->running = MYRI10GE_ETH_STOPPED;
251793f7848bSJeff Kirsher 	return 0;
251893f7848bSJeff Kirsher }
251993f7848bSJeff Kirsher 
252093f7848bSJeff Kirsher /* copy an array of struct mcp_kreq_ether_send's to the mcp.  Copy
252193f7848bSJeff Kirsher  * backwards one at a time and handle ring wraps */
252293f7848bSJeff Kirsher 
252393f7848bSJeff Kirsher static inline void
252493f7848bSJeff Kirsher myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
252593f7848bSJeff Kirsher 			      struct mcp_kreq_ether_send *src, int cnt)
252693f7848bSJeff Kirsher {
252793f7848bSJeff Kirsher 	int idx, starting_slot;
252893f7848bSJeff Kirsher 	starting_slot = tx->req;
252993f7848bSJeff Kirsher 	while (cnt > 1) {
253093f7848bSJeff Kirsher 		cnt--;
253193f7848bSJeff Kirsher 		idx = (starting_slot + cnt) & tx->mask;
253293f7848bSJeff Kirsher 		myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
253393f7848bSJeff Kirsher 		mb();
253493f7848bSJeff Kirsher 	}
253593f7848bSJeff Kirsher }
253693f7848bSJeff Kirsher 
253793f7848bSJeff Kirsher /*
253893f7848bSJeff Kirsher  * copy an array of struct mcp_kreq_ether_send's to the mcp.  Copy
253993f7848bSJeff Kirsher  * at most 32 bytes at a time, so as to avoid involving the software
254093f7848bSJeff Kirsher  * pio handler in the nic.   We re-write the first segment's flags
254193f7848bSJeff Kirsher  * to mark them valid only after writing the entire chain.
254293f7848bSJeff Kirsher  */
254393f7848bSJeff Kirsher 
254493f7848bSJeff Kirsher static inline void
254593f7848bSJeff Kirsher myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
254693f7848bSJeff Kirsher 		    int cnt)
254793f7848bSJeff Kirsher {
254893f7848bSJeff Kirsher 	int idx, i;
254993f7848bSJeff Kirsher 	struct mcp_kreq_ether_send __iomem *dstp, *dst;
255093f7848bSJeff Kirsher 	struct mcp_kreq_ether_send *srcp;
255193f7848bSJeff Kirsher 	u8 last_flags;
255293f7848bSJeff Kirsher 
255393f7848bSJeff Kirsher 	idx = tx->req & tx->mask;
255493f7848bSJeff Kirsher 
255593f7848bSJeff Kirsher 	last_flags = src->flags;
255693f7848bSJeff Kirsher 	src->flags = 0;
255793f7848bSJeff Kirsher 	mb();
255893f7848bSJeff Kirsher 	dst = dstp = &tx->lanai[idx];
255993f7848bSJeff Kirsher 	srcp = src;
256093f7848bSJeff Kirsher 
256193f7848bSJeff Kirsher 	if ((idx + cnt) < tx->mask) {
256293f7848bSJeff Kirsher 		for (i = 0; i < (cnt - 1); i += 2) {
256393f7848bSJeff Kirsher 			myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
256493f7848bSJeff Kirsher 			mb();	/* force write every 32 bytes */
256593f7848bSJeff Kirsher 			srcp += 2;
256693f7848bSJeff Kirsher 			dstp += 2;
256793f7848bSJeff Kirsher 		}
256893f7848bSJeff Kirsher 	} else {
256993f7848bSJeff Kirsher 		/* submit all but the first request, and ensure
257093f7848bSJeff Kirsher 		 * that it is submitted below */
257193f7848bSJeff Kirsher 		myri10ge_submit_req_backwards(tx, src, cnt);
257293f7848bSJeff Kirsher 		i = 0;
257393f7848bSJeff Kirsher 	}
257493f7848bSJeff Kirsher 	if (i < cnt) {
257593f7848bSJeff Kirsher 		/* submit the first request */
257693f7848bSJeff Kirsher 		myri10ge_pio_copy(dstp, srcp, sizeof(*src));
257793f7848bSJeff Kirsher 		mb();		/* barrier before setting valid flag */
257893f7848bSJeff Kirsher 	}
257993f7848bSJeff Kirsher 
258093f7848bSJeff Kirsher 	/* re-write the last 32-bits with the valid flags */
258193f7848bSJeff Kirsher 	src->flags = last_flags;
258293f7848bSJeff Kirsher 	put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
258393f7848bSJeff Kirsher 	tx->req += cnt;
258493f7848bSJeff Kirsher 	mb();
258593f7848bSJeff Kirsher }
258693f7848bSJeff Kirsher 
258710545937SStanislaw Gruszka static void myri10ge_unmap_tx_dma(struct myri10ge_priv *mgp,
258810545937SStanislaw Gruszka 				  struct myri10ge_tx_buf *tx, int idx)
258910545937SStanislaw Gruszka {
259010545937SStanislaw Gruszka 	unsigned int len;
259110545937SStanislaw Gruszka 	int last_idx;
259210545937SStanislaw Gruszka 
259310545937SStanislaw Gruszka 	/* Free any DMA resources we've alloced and clear out the skb slot */
259410545937SStanislaw Gruszka 	last_idx = (idx + 1) & tx->mask;
259510545937SStanislaw Gruszka 	idx = tx->req & tx->mask;
259610545937SStanislaw Gruszka 	do {
259710545937SStanislaw Gruszka 		len = dma_unmap_len(&tx->info[idx], len);
259810545937SStanislaw Gruszka 		if (len) {
259910545937SStanislaw Gruszka 			if (tx->info[idx].skb != NULL)
260010545937SStanislaw Gruszka 				pci_unmap_single(mgp->pdev,
260110545937SStanislaw Gruszka 						 dma_unmap_addr(&tx->info[idx],
260210545937SStanislaw Gruszka 								bus), len,
260310545937SStanislaw Gruszka 						 PCI_DMA_TODEVICE);
260410545937SStanislaw Gruszka 			else
260510545937SStanislaw Gruszka 				pci_unmap_page(mgp->pdev,
260610545937SStanislaw Gruszka 					       dma_unmap_addr(&tx->info[idx],
260710545937SStanislaw Gruszka 							      bus), len,
260810545937SStanislaw Gruszka 					       PCI_DMA_TODEVICE);
260910545937SStanislaw Gruszka 			dma_unmap_len_set(&tx->info[idx], len, 0);
261010545937SStanislaw Gruszka 			tx->info[idx].skb = NULL;
261110545937SStanislaw Gruszka 		}
261210545937SStanislaw Gruszka 		idx = (idx + 1) & tx->mask;
261310545937SStanislaw Gruszka 	} while (idx != last_idx);
261410545937SStanislaw Gruszka }
261510545937SStanislaw Gruszka 
261693f7848bSJeff Kirsher /*
261793f7848bSJeff Kirsher  * Transmit a packet.  We need to split the packet so that a single
261893f7848bSJeff Kirsher  * segment does not cross myri10ge->tx_boundary, so this makes segment
261993f7848bSJeff Kirsher  * counting tricky.  So rather than try to count segments up front, we
262093f7848bSJeff Kirsher  * just give up if there are too few segments to hold a reasonably
262193f7848bSJeff Kirsher  * fragmented packet currently available.  If we run
262293f7848bSJeff Kirsher  * out of segments while preparing a packet for DMA, we just linearize
262393f7848bSJeff Kirsher  * it and try again.
262493f7848bSJeff Kirsher  */
262593f7848bSJeff Kirsher 
262693f7848bSJeff Kirsher static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
262793f7848bSJeff Kirsher 				       struct net_device *dev)
262893f7848bSJeff Kirsher {
262993f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
263093f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
263193f7848bSJeff Kirsher 	struct mcp_kreq_ether_send *req;
263293f7848bSJeff Kirsher 	struct myri10ge_tx_buf *tx;
263393f7848bSJeff Kirsher 	struct skb_frag_struct *frag;
263493f7848bSJeff Kirsher 	struct netdev_queue *netdev_queue;
263593f7848bSJeff Kirsher 	dma_addr_t bus;
263693f7848bSJeff Kirsher 	u32 low;
263793f7848bSJeff Kirsher 	__be32 high_swapped;
263893f7848bSJeff Kirsher 	unsigned int len;
263910545937SStanislaw Gruszka 	int idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
264093f7848bSJeff Kirsher 	u16 pseudo_hdr_offset, cksum_offset, queue;
264193f7848bSJeff Kirsher 	int cum_len, seglen, boundary, rdma_count;
264293f7848bSJeff Kirsher 	u8 flags, odd_flag;
264393f7848bSJeff Kirsher 
264493f7848bSJeff Kirsher 	queue = skb_get_queue_mapping(skb);
264593f7848bSJeff Kirsher 	ss = &mgp->ss[queue];
264693f7848bSJeff Kirsher 	netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
264793f7848bSJeff Kirsher 	tx = &ss->tx;
264893f7848bSJeff Kirsher 
264993f7848bSJeff Kirsher again:
265093f7848bSJeff Kirsher 	req = tx->req_list;
265193f7848bSJeff Kirsher 	avail = tx->mask - 1 - (tx->req - tx->done);
265293f7848bSJeff Kirsher 
265393f7848bSJeff Kirsher 	mss = 0;
265493f7848bSJeff Kirsher 	max_segments = MXGEFW_MAX_SEND_DESC;
265593f7848bSJeff Kirsher 
265693f7848bSJeff Kirsher 	if (skb_is_gso(skb)) {
265793f7848bSJeff Kirsher 		mss = skb_shinfo(skb)->gso_size;
265893f7848bSJeff Kirsher 		max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
265993f7848bSJeff Kirsher 	}
266093f7848bSJeff Kirsher 
266193f7848bSJeff Kirsher 	if ((unlikely(avail < max_segments))) {
266293f7848bSJeff Kirsher 		/* we are out of transmit resources */
266393f7848bSJeff Kirsher 		tx->stop_queue++;
266493f7848bSJeff Kirsher 		netif_tx_stop_queue(netdev_queue);
266593f7848bSJeff Kirsher 		return NETDEV_TX_BUSY;
266693f7848bSJeff Kirsher 	}
266793f7848bSJeff Kirsher 
266893f7848bSJeff Kirsher 	/* Setup checksum offloading, if needed */
266993f7848bSJeff Kirsher 	cksum_offset = 0;
267093f7848bSJeff Kirsher 	pseudo_hdr_offset = 0;
267193f7848bSJeff Kirsher 	odd_flag = 0;
267293f7848bSJeff Kirsher 	flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
267393f7848bSJeff Kirsher 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
267493f7848bSJeff Kirsher 		cksum_offset = skb_checksum_start_offset(skb);
267593f7848bSJeff Kirsher 		pseudo_hdr_offset = cksum_offset + skb->csum_offset;
267693f7848bSJeff Kirsher 		/* If the headers are excessively large, then we must
267793f7848bSJeff Kirsher 		 * fall back to a software checksum */
267893f7848bSJeff Kirsher 		if (unlikely(!mss && (cksum_offset > 255 ||
267993f7848bSJeff Kirsher 				      pseudo_hdr_offset > 127))) {
268093f7848bSJeff Kirsher 			if (skb_checksum_help(skb))
268193f7848bSJeff Kirsher 				goto drop;
268293f7848bSJeff Kirsher 			cksum_offset = 0;
268393f7848bSJeff Kirsher 			pseudo_hdr_offset = 0;
268493f7848bSJeff Kirsher 		} else {
268593f7848bSJeff Kirsher 			odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
268693f7848bSJeff Kirsher 			flags |= MXGEFW_FLAGS_CKSUM;
268793f7848bSJeff Kirsher 		}
268893f7848bSJeff Kirsher 	}
268993f7848bSJeff Kirsher 
269093f7848bSJeff Kirsher 	cum_len = 0;
269193f7848bSJeff Kirsher 
269293f7848bSJeff Kirsher 	if (mss) {		/* TSO */
269393f7848bSJeff Kirsher 		/* this removes any CKSUM flag from before */
269493f7848bSJeff Kirsher 		flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
269593f7848bSJeff Kirsher 
269693f7848bSJeff Kirsher 		/* negative cum_len signifies to the
269793f7848bSJeff Kirsher 		 * send loop that we are still in the
269893f7848bSJeff Kirsher 		 * header portion of the TSO packet.
269993f7848bSJeff Kirsher 		 * TSO header can be at most 1KB long */
270093f7848bSJeff Kirsher 		cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
270193f7848bSJeff Kirsher 
270293f7848bSJeff Kirsher 		/* for IPv6 TSO, the checksum offset stores the
270393f7848bSJeff Kirsher 		 * TCP header length, to save the firmware from
270493f7848bSJeff Kirsher 		 * the need to parse the headers */
270593f7848bSJeff Kirsher 		if (skb_is_gso_v6(skb)) {
270693f7848bSJeff Kirsher 			cksum_offset = tcp_hdrlen(skb);
270793f7848bSJeff Kirsher 			/* Can only handle headers <= max_tso6 long */
270893f7848bSJeff Kirsher 			if (unlikely(-cum_len > mgp->max_tso6))
270993f7848bSJeff Kirsher 				return myri10ge_sw_tso(skb, dev);
271093f7848bSJeff Kirsher 		}
271193f7848bSJeff Kirsher 		/* for TSO, pseudo_hdr_offset holds mss.
271293f7848bSJeff Kirsher 		 * The firmware figures out where to put
271393f7848bSJeff Kirsher 		 * the checksum by parsing the header. */
271493f7848bSJeff Kirsher 		pseudo_hdr_offset = mss;
271593f7848bSJeff Kirsher 	} else
271693f7848bSJeff Kirsher 		/* Mark small packets, and pad out tiny packets */
271793f7848bSJeff Kirsher 	if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
271893f7848bSJeff Kirsher 		flags |= MXGEFW_FLAGS_SMALL;
271993f7848bSJeff Kirsher 
272093f7848bSJeff Kirsher 		/* pad frames to at least ETH_ZLEN bytes */
2721b0b9f333SAlexander Duyck 		if (eth_skb_pad(skb)) {
272293f7848bSJeff Kirsher 			/* The packet is gone, so we must
272393f7848bSJeff Kirsher 			 * return 0 */
272493f7848bSJeff Kirsher 			ss->stats.tx_dropped += 1;
272593f7848bSJeff Kirsher 			return NETDEV_TX_OK;
272693f7848bSJeff Kirsher 		}
272793f7848bSJeff Kirsher 	}
272893f7848bSJeff Kirsher 
272993f7848bSJeff Kirsher 	/* map the skb for DMA */
273093f7848bSJeff Kirsher 	len = skb_headlen(skb);
273110545937SStanislaw Gruszka 	bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
273210545937SStanislaw Gruszka 	if (unlikely(pci_dma_mapping_error(mgp->pdev, bus)))
273310545937SStanislaw Gruszka 		goto drop;
273410545937SStanislaw Gruszka 
273593f7848bSJeff Kirsher 	idx = tx->req & tx->mask;
273693f7848bSJeff Kirsher 	tx->info[idx].skb = skb;
273793f7848bSJeff Kirsher 	dma_unmap_addr_set(&tx->info[idx], bus, bus);
273893f7848bSJeff Kirsher 	dma_unmap_len_set(&tx->info[idx], len, len);
273993f7848bSJeff Kirsher 
274093f7848bSJeff Kirsher 	frag_cnt = skb_shinfo(skb)->nr_frags;
274193f7848bSJeff Kirsher 	frag_idx = 0;
274293f7848bSJeff Kirsher 	count = 0;
274393f7848bSJeff Kirsher 	rdma_count = 0;
274493f7848bSJeff Kirsher 
274593f7848bSJeff Kirsher 	/* "rdma_count" is the number of RDMAs belonging to the
274693f7848bSJeff Kirsher 	 * current packet BEFORE the current send request. For
274793f7848bSJeff Kirsher 	 * non-TSO packets, this is equal to "count".
274893f7848bSJeff Kirsher 	 * For TSO packets, rdma_count needs to be reset
274993f7848bSJeff Kirsher 	 * to 0 after a segment cut.
275093f7848bSJeff Kirsher 	 *
275193f7848bSJeff Kirsher 	 * The rdma_count field of the send request is
275293f7848bSJeff Kirsher 	 * the number of RDMAs of the packet starting at
275393f7848bSJeff Kirsher 	 * that request. For TSO send requests with one ore more cuts
275493f7848bSJeff Kirsher 	 * in the middle, this is the number of RDMAs starting
275593f7848bSJeff Kirsher 	 * after the last cut in the request. All previous
275693f7848bSJeff Kirsher 	 * segments before the last cut implicitly have 1 RDMA.
275793f7848bSJeff Kirsher 	 *
275893f7848bSJeff Kirsher 	 * Since the number of RDMAs is not known beforehand,
275993f7848bSJeff Kirsher 	 * it must be filled-in retroactively - after each
276093f7848bSJeff Kirsher 	 * segmentation cut or at the end of the entire packet.
276193f7848bSJeff Kirsher 	 */
276293f7848bSJeff Kirsher 
276393f7848bSJeff Kirsher 	while (1) {
276493f7848bSJeff Kirsher 		/* Break the SKB or Fragment up into pieces which
276593f7848bSJeff Kirsher 		 * do not cross mgp->tx_boundary */
276693f7848bSJeff Kirsher 		low = MYRI10GE_LOWPART_TO_U32(bus);
276793f7848bSJeff Kirsher 		high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
276893f7848bSJeff Kirsher 		while (len) {
276993f7848bSJeff Kirsher 			u8 flags_next;
277093f7848bSJeff Kirsher 			int cum_len_next;
277193f7848bSJeff Kirsher 
277293f7848bSJeff Kirsher 			if (unlikely(count == max_segments))
277393f7848bSJeff Kirsher 				goto abort_linearize;
277493f7848bSJeff Kirsher 
277593f7848bSJeff Kirsher 			boundary =
277693f7848bSJeff Kirsher 			    (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
277793f7848bSJeff Kirsher 			seglen = boundary - low;
277893f7848bSJeff Kirsher 			if (seglen > len)
277993f7848bSJeff Kirsher 				seglen = len;
278093f7848bSJeff Kirsher 			flags_next = flags & ~MXGEFW_FLAGS_FIRST;
278193f7848bSJeff Kirsher 			cum_len_next = cum_len + seglen;
278293f7848bSJeff Kirsher 			if (mss) {	/* TSO */
278393f7848bSJeff Kirsher 				(req - rdma_count)->rdma_count = rdma_count + 1;
278493f7848bSJeff Kirsher 
278593f7848bSJeff Kirsher 				if (likely(cum_len >= 0)) {	/* payload */
278693f7848bSJeff Kirsher 					int next_is_first, chop;
278793f7848bSJeff Kirsher 
278893f7848bSJeff Kirsher 					chop = (cum_len_next > mss);
278993f7848bSJeff Kirsher 					cum_len_next = cum_len_next % mss;
279093f7848bSJeff Kirsher 					next_is_first = (cum_len_next == 0);
279193f7848bSJeff Kirsher 					flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
279293f7848bSJeff Kirsher 					flags_next |= next_is_first *
279393f7848bSJeff Kirsher 					    MXGEFW_FLAGS_FIRST;
279493f7848bSJeff Kirsher 					rdma_count |= -(chop | next_is_first);
279559e955edSAndrew Gallatin 					rdma_count += chop & ~next_is_first;
279693f7848bSJeff Kirsher 				} else if (likely(cum_len_next >= 0)) {	/* header ends */
279793f7848bSJeff Kirsher 					int small;
279893f7848bSJeff Kirsher 
279993f7848bSJeff Kirsher 					rdma_count = -1;
280093f7848bSJeff Kirsher 					cum_len_next = 0;
280193f7848bSJeff Kirsher 					seglen = -cum_len;
280293f7848bSJeff Kirsher 					small = (mss <= MXGEFW_SEND_SMALL_SIZE);
280393f7848bSJeff Kirsher 					flags_next = MXGEFW_FLAGS_TSO_PLD |
280493f7848bSJeff Kirsher 					    MXGEFW_FLAGS_FIRST |
280593f7848bSJeff Kirsher 					    (small * MXGEFW_FLAGS_SMALL);
280693f7848bSJeff Kirsher 				}
280793f7848bSJeff Kirsher 			}
280893f7848bSJeff Kirsher 			req->addr_high = high_swapped;
280993f7848bSJeff Kirsher 			req->addr_low = htonl(low);
281093f7848bSJeff Kirsher 			req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
281193f7848bSJeff Kirsher 			req->pad = 0;	/* complete solid 16-byte block; does this matter? */
281293f7848bSJeff Kirsher 			req->rdma_count = 1;
281393f7848bSJeff Kirsher 			req->length = htons(seglen);
281493f7848bSJeff Kirsher 			req->cksum_offset = cksum_offset;
281593f7848bSJeff Kirsher 			req->flags = flags | ((cum_len & 1) * odd_flag);
281693f7848bSJeff Kirsher 
281793f7848bSJeff Kirsher 			low += seglen;
281893f7848bSJeff Kirsher 			len -= seglen;
281993f7848bSJeff Kirsher 			cum_len = cum_len_next;
282093f7848bSJeff Kirsher 			flags = flags_next;
282193f7848bSJeff Kirsher 			req++;
282293f7848bSJeff Kirsher 			count++;
282393f7848bSJeff Kirsher 			rdma_count++;
282493f7848bSJeff Kirsher 			if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
282593f7848bSJeff Kirsher 				if (unlikely(cksum_offset > seglen))
282693f7848bSJeff Kirsher 					cksum_offset -= seglen;
282793f7848bSJeff Kirsher 				else
282893f7848bSJeff Kirsher 					cksum_offset = 0;
282993f7848bSJeff Kirsher 			}
283093f7848bSJeff Kirsher 		}
283193f7848bSJeff Kirsher 		if (frag_idx == frag_cnt)
283293f7848bSJeff Kirsher 			break;
283393f7848bSJeff Kirsher 
283493f7848bSJeff Kirsher 		/* map next fragment for DMA */
283593f7848bSJeff Kirsher 		frag = &skb_shinfo(skb)->frags[frag_idx];
283693f7848bSJeff Kirsher 		frag_idx++;
28379e903e08SEric Dumazet 		len = skb_frag_size(frag);
28385dc3e196SIan Campbell 		bus = skb_frag_dma_map(&mgp->pdev->dev, frag, 0, len,
28395d6bcdfeSIan Campbell 				       DMA_TO_DEVICE);
284010545937SStanislaw Gruszka 		if (unlikely(pci_dma_mapping_error(mgp->pdev, bus))) {
284110545937SStanislaw Gruszka 			myri10ge_unmap_tx_dma(mgp, tx, idx);
284210545937SStanislaw Gruszka 			goto drop;
284310545937SStanislaw Gruszka 		}
284410545937SStanislaw Gruszka 		idx = (count + tx->req) & tx->mask;
284593f7848bSJeff Kirsher 		dma_unmap_addr_set(&tx->info[idx], bus, bus);
284693f7848bSJeff Kirsher 		dma_unmap_len_set(&tx->info[idx], len, len);
284793f7848bSJeff Kirsher 	}
284893f7848bSJeff Kirsher 
284993f7848bSJeff Kirsher 	(req - rdma_count)->rdma_count = rdma_count;
285093f7848bSJeff Kirsher 	if (mss)
285193f7848bSJeff Kirsher 		do {
285293f7848bSJeff Kirsher 			req--;
285393f7848bSJeff Kirsher 			req->flags |= MXGEFW_FLAGS_TSO_LAST;
285493f7848bSJeff Kirsher 		} while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
285593f7848bSJeff Kirsher 					 MXGEFW_FLAGS_FIRST)));
285693f7848bSJeff Kirsher 	idx = ((count - 1) + tx->req) & tx->mask;
285793f7848bSJeff Kirsher 	tx->info[idx].last = 1;
285893f7848bSJeff Kirsher 	myri10ge_submit_req(tx, tx->req_list, count);
285993f7848bSJeff Kirsher 	/* if using multiple tx queues, make sure NIC polls the
286093f7848bSJeff Kirsher 	 * current slice */
286193f7848bSJeff Kirsher 	if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
286293f7848bSJeff Kirsher 		tx->queue_active = 1;
286393f7848bSJeff Kirsher 		put_be32(htonl(1), tx->send_go);
286493f7848bSJeff Kirsher 		mb();
286593f7848bSJeff Kirsher 		mmiowb();
286693f7848bSJeff Kirsher 	}
286793f7848bSJeff Kirsher 	tx->pkt_start++;
286893f7848bSJeff Kirsher 	if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
286993f7848bSJeff Kirsher 		tx->stop_queue++;
287093f7848bSJeff Kirsher 		netif_tx_stop_queue(netdev_queue);
287193f7848bSJeff Kirsher 	}
287293f7848bSJeff Kirsher 	return NETDEV_TX_OK;
287393f7848bSJeff Kirsher 
287493f7848bSJeff Kirsher abort_linearize:
287510545937SStanislaw Gruszka 	myri10ge_unmap_tx_dma(mgp, tx, idx);
287693f7848bSJeff Kirsher 
287793f7848bSJeff Kirsher 	if (skb_is_gso(skb)) {
287893f7848bSJeff Kirsher 		netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
287993f7848bSJeff Kirsher 		goto drop;
288093f7848bSJeff Kirsher 	}
288193f7848bSJeff Kirsher 
288293f7848bSJeff Kirsher 	if (skb_linearize(skb))
288393f7848bSJeff Kirsher 		goto drop;
288493f7848bSJeff Kirsher 
288593f7848bSJeff Kirsher 	tx->linearized++;
288693f7848bSJeff Kirsher 	goto again;
288793f7848bSJeff Kirsher 
288893f7848bSJeff Kirsher drop:
288993f7848bSJeff Kirsher 	dev_kfree_skb_any(skb);
289093f7848bSJeff Kirsher 	ss->stats.tx_dropped += 1;
289193f7848bSJeff Kirsher 	return NETDEV_TX_OK;
289293f7848bSJeff Kirsher 
289393f7848bSJeff Kirsher }
289493f7848bSJeff Kirsher 
289593f7848bSJeff Kirsher static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
289693f7848bSJeff Kirsher 					 struct net_device *dev)
289793f7848bSJeff Kirsher {
289893f7848bSJeff Kirsher 	struct sk_buff *segs, *curr;
289993f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
290093f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
290193f7848bSJeff Kirsher 	netdev_tx_t status;
290293f7848bSJeff Kirsher 
290393f7848bSJeff Kirsher 	segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
290493f7848bSJeff Kirsher 	if (IS_ERR(segs))
290593f7848bSJeff Kirsher 		goto drop;
290693f7848bSJeff Kirsher 
290793f7848bSJeff Kirsher 	while (segs) {
290893f7848bSJeff Kirsher 		curr = segs;
290993f7848bSJeff Kirsher 		segs = segs->next;
291093f7848bSJeff Kirsher 		curr->next = NULL;
291193f7848bSJeff Kirsher 		status = myri10ge_xmit(curr, dev);
291293f7848bSJeff Kirsher 		if (status != 0) {
291393f7848bSJeff Kirsher 			dev_kfree_skb_any(curr);
291493f7848bSJeff Kirsher 			if (segs != NULL) {
291593f7848bSJeff Kirsher 				curr = segs;
291693f7848bSJeff Kirsher 				segs = segs->next;
291793f7848bSJeff Kirsher 				curr->next = NULL;
291893f7848bSJeff Kirsher 				dev_kfree_skb_any(segs);
291993f7848bSJeff Kirsher 			}
292093f7848bSJeff Kirsher 			goto drop;
292193f7848bSJeff Kirsher 		}
292293f7848bSJeff Kirsher 	}
292393f7848bSJeff Kirsher 	dev_kfree_skb_any(skb);
292493f7848bSJeff Kirsher 	return NETDEV_TX_OK;
292593f7848bSJeff Kirsher 
292693f7848bSJeff Kirsher drop:
292793f7848bSJeff Kirsher 	ss = &mgp->ss[skb_get_queue_mapping(skb)];
292893f7848bSJeff Kirsher 	dev_kfree_skb_any(skb);
292993f7848bSJeff Kirsher 	ss->stats.tx_dropped += 1;
293093f7848bSJeff Kirsher 	return NETDEV_TX_OK;
293193f7848bSJeff Kirsher }
293293f7848bSJeff Kirsher 
2933bc1f4470Sstephen hemminger static void myri10ge_get_stats(struct net_device *dev,
293493f7848bSJeff Kirsher 			       struct rtnl_link_stats64 *stats)
293593f7848bSJeff Kirsher {
293693f7848bSJeff Kirsher 	const struct myri10ge_priv *mgp = netdev_priv(dev);
293793f7848bSJeff Kirsher 	const struct myri10ge_slice_netstats *slice_stats;
293893f7848bSJeff Kirsher 	int i;
293993f7848bSJeff Kirsher 
294093f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
294193f7848bSJeff Kirsher 		slice_stats = &mgp->ss[i].stats;
294293f7848bSJeff Kirsher 		stats->rx_packets += slice_stats->rx_packets;
294393f7848bSJeff Kirsher 		stats->tx_packets += slice_stats->tx_packets;
294493f7848bSJeff Kirsher 		stats->rx_bytes += slice_stats->rx_bytes;
294593f7848bSJeff Kirsher 		stats->tx_bytes += slice_stats->tx_bytes;
294693f7848bSJeff Kirsher 		stats->rx_dropped += slice_stats->rx_dropped;
294793f7848bSJeff Kirsher 		stats->tx_dropped += slice_stats->tx_dropped;
294893f7848bSJeff Kirsher 	}
294993f7848bSJeff Kirsher }
295093f7848bSJeff Kirsher 
295193f7848bSJeff Kirsher static void myri10ge_set_multicast_list(struct net_device *dev)
295293f7848bSJeff Kirsher {
295393f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
295493f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
295593f7848bSJeff Kirsher 	struct netdev_hw_addr *ha;
295693f7848bSJeff Kirsher 	__be32 data[2] = { 0, 0 };
295793f7848bSJeff Kirsher 	int err;
295893f7848bSJeff Kirsher 
295993f7848bSJeff Kirsher 	/* can be called from atomic contexts,
296093f7848bSJeff Kirsher 	 * pass 1 to force atomicity in myri10ge_send_cmd() */
296193f7848bSJeff Kirsher 	myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
296293f7848bSJeff Kirsher 
296393f7848bSJeff Kirsher 	/* This firmware is known to not support multicast */
296493f7848bSJeff Kirsher 	if (!mgp->fw_multicast_support)
296593f7848bSJeff Kirsher 		return;
296693f7848bSJeff Kirsher 
296793f7848bSJeff Kirsher 	/* Disable multicast filtering */
296893f7848bSJeff Kirsher 
296993f7848bSJeff Kirsher 	err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
297093f7848bSJeff Kirsher 	if (err != 0) {
297193f7848bSJeff Kirsher 		netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
297293f7848bSJeff Kirsher 			   err);
297393f7848bSJeff Kirsher 		goto abort;
297493f7848bSJeff Kirsher 	}
297593f7848bSJeff Kirsher 
297693f7848bSJeff Kirsher 	if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
297793f7848bSJeff Kirsher 		/* request to disable multicast filtering, so quit here */
297893f7848bSJeff Kirsher 		return;
297993f7848bSJeff Kirsher 	}
298093f7848bSJeff Kirsher 
298193f7848bSJeff Kirsher 	/* Flush the filters */
298293f7848bSJeff Kirsher 
298393f7848bSJeff Kirsher 	err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
298493f7848bSJeff Kirsher 				&cmd, 1);
298593f7848bSJeff Kirsher 	if (err != 0) {
298693f7848bSJeff Kirsher 		netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
298793f7848bSJeff Kirsher 			   err);
298893f7848bSJeff Kirsher 		goto abort;
298993f7848bSJeff Kirsher 	}
299093f7848bSJeff Kirsher 
299193f7848bSJeff Kirsher 	/* Walk the multicast list, and add each address */
299293f7848bSJeff Kirsher 	netdev_for_each_mc_addr(ha, dev) {
2993d458cdf7SJoe Perches 		memcpy(data, &ha->addr, ETH_ALEN);
299493f7848bSJeff Kirsher 		cmd.data0 = ntohl(data[0]);
299593f7848bSJeff Kirsher 		cmd.data1 = ntohl(data[1]);
299693f7848bSJeff Kirsher 		err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
299793f7848bSJeff Kirsher 					&cmd, 1);
299893f7848bSJeff Kirsher 
299993f7848bSJeff Kirsher 		if (err != 0) {
300093f7848bSJeff Kirsher 			netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
300193f7848bSJeff Kirsher 				   err, ha->addr);
300293f7848bSJeff Kirsher 			goto abort;
300393f7848bSJeff Kirsher 		}
300493f7848bSJeff Kirsher 	}
300593f7848bSJeff Kirsher 	/* Enable multicast filtering */
300693f7848bSJeff Kirsher 	err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
300793f7848bSJeff Kirsher 	if (err != 0) {
300893f7848bSJeff Kirsher 		netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
300993f7848bSJeff Kirsher 			   err);
301093f7848bSJeff Kirsher 		goto abort;
301193f7848bSJeff Kirsher 	}
301293f7848bSJeff Kirsher 
301393f7848bSJeff Kirsher 	return;
301493f7848bSJeff Kirsher 
301593f7848bSJeff Kirsher abort:
301693f7848bSJeff Kirsher 	return;
301793f7848bSJeff Kirsher }
301893f7848bSJeff Kirsher 
301993f7848bSJeff Kirsher static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
302093f7848bSJeff Kirsher {
302193f7848bSJeff Kirsher 	struct sockaddr *sa = addr;
302293f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
302393f7848bSJeff Kirsher 	int status;
302493f7848bSJeff Kirsher 
302593f7848bSJeff Kirsher 	if (!is_valid_ether_addr(sa->sa_data))
302693f7848bSJeff Kirsher 		return -EADDRNOTAVAIL;
302793f7848bSJeff Kirsher 
302893f7848bSJeff Kirsher 	status = myri10ge_update_mac_address(mgp, sa->sa_data);
302993f7848bSJeff Kirsher 	if (status != 0) {
303093f7848bSJeff Kirsher 		netdev_err(dev, "changing mac address failed with %d\n",
303193f7848bSJeff Kirsher 			   status);
303293f7848bSJeff Kirsher 		return status;
303393f7848bSJeff Kirsher 	}
303493f7848bSJeff Kirsher 
303593f7848bSJeff Kirsher 	/* change the dev structure */
3036d458cdf7SJoe Perches 	memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
303793f7848bSJeff Kirsher 	return 0;
303893f7848bSJeff Kirsher }
303993f7848bSJeff Kirsher 
304093f7848bSJeff Kirsher static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
304193f7848bSJeff Kirsher {
304293f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = netdev_priv(dev);
304393f7848bSJeff Kirsher 	int error = 0;
304493f7848bSJeff Kirsher 
304593f7848bSJeff Kirsher 	netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
304693f7848bSJeff Kirsher 	if (mgp->running) {
304793f7848bSJeff Kirsher 		/* if we change the mtu on an active device, we must
304893f7848bSJeff Kirsher 		 * reset the device so the firmware sees the change */
304993f7848bSJeff Kirsher 		myri10ge_close(dev);
305093f7848bSJeff Kirsher 		dev->mtu = new_mtu;
305193f7848bSJeff Kirsher 		myri10ge_open(dev);
305293f7848bSJeff Kirsher 	} else
305393f7848bSJeff Kirsher 		dev->mtu = new_mtu;
305493f7848bSJeff Kirsher 
305593f7848bSJeff Kirsher 	return error;
305693f7848bSJeff Kirsher }
305793f7848bSJeff Kirsher 
305893f7848bSJeff Kirsher /*
305993f7848bSJeff Kirsher  * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
306093f7848bSJeff Kirsher  * Only do it if the bridge is a root port since we don't want to disturb
306193f7848bSJeff Kirsher  * any other device, except if forced with myri10ge_ecrc_enable > 1.
306293f7848bSJeff Kirsher  */
306393f7848bSJeff Kirsher 
306493f7848bSJeff Kirsher static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
306593f7848bSJeff Kirsher {
306693f7848bSJeff Kirsher 	struct pci_dev *bridge = mgp->pdev->bus->self;
306793f7848bSJeff Kirsher 	struct device *dev = &mgp->pdev->dev;
306893f7848bSJeff Kirsher 	int cap;
306993f7848bSJeff Kirsher 	unsigned err_cap;
307093f7848bSJeff Kirsher 	int ret;
307193f7848bSJeff Kirsher 
307293f7848bSJeff Kirsher 	if (!myri10ge_ecrc_enable || !bridge)
307393f7848bSJeff Kirsher 		return;
307493f7848bSJeff Kirsher 
307593f7848bSJeff Kirsher 	/* check that the bridge is a root port */
30769503e255SJiang Liu 	if (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT) {
307793f7848bSJeff Kirsher 		if (myri10ge_ecrc_enable > 1) {
307893f7848bSJeff Kirsher 			struct pci_dev *prev_bridge, *old_bridge = bridge;
307993f7848bSJeff Kirsher 
308093f7848bSJeff Kirsher 			/* Walk the hierarchy up to the root port
308193f7848bSJeff Kirsher 			 * where ECRC has to be enabled */
308293f7848bSJeff Kirsher 			do {
308393f7848bSJeff Kirsher 				prev_bridge = bridge;
308493f7848bSJeff Kirsher 				bridge = bridge->bus->self;
308593f7848bSJeff Kirsher 				if (!bridge || prev_bridge == bridge) {
308693f7848bSJeff Kirsher 					dev_err(dev,
308793f7848bSJeff Kirsher 						"Failed to find root port"
308893f7848bSJeff Kirsher 						" to force ECRC\n");
308993f7848bSJeff Kirsher 					return;
309093f7848bSJeff Kirsher 				}
30919503e255SJiang Liu 			} while (pci_pcie_type(bridge) !=
30929503e255SJiang Liu 				 PCI_EXP_TYPE_ROOT_PORT);
309393f7848bSJeff Kirsher 
309493f7848bSJeff Kirsher 			dev_info(dev,
309593f7848bSJeff Kirsher 				 "Forcing ECRC on non-root port %s"
309693f7848bSJeff Kirsher 				 " (enabling on root port %s)\n",
309793f7848bSJeff Kirsher 				 pci_name(old_bridge), pci_name(bridge));
309893f7848bSJeff Kirsher 		} else {
309993f7848bSJeff Kirsher 			dev_err(dev,
310093f7848bSJeff Kirsher 				"Not enabling ECRC on non-root port %s\n",
310193f7848bSJeff Kirsher 				pci_name(bridge));
310293f7848bSJeff Kirsher 			return;
310393f7848bSJeff Kirsher 		}
310493f7848bSJeff Kirsher 	}
310593f7848bSJeff Kirsher 
310693f7848bSJeff Kirsher 	cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
310793f7848bSJeff Kirsher 	if (!cap)
310893f7848bSJeff Kirsher 		return;
310993f7848bSJeff Kirsher 
311093f7848bSJeff Kirsher 	ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
311193f7848bSJeff Kirsher 	if (ret) {
311293f7848bSJeff Kirsher 		dev_err(dev, "failed reading ext-conf-space of %s\n",
311393f7848bSJeff Kirsher 			pci_name(bridge));
311493f7848bSJeff Kirsher 		dev_err(dev, "\t pci=nommconf in use? "
311593f7848bSJeff Kirsher 			"or buggy/incomplete/absent ACPI MCFG attr?\n");
311693f7848bSJeff Kirsher 		return;
311793f7848bSJeff Kirsher 	}
311893f7848bSJeff Kirsher 	if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
311993f7848bSJeff Kirsher 		return;
312093f7848bSJeff Kirsher 
312193f7848bSJeff Kirsher 	err_cap |= PCI_ERR_CAP_ECRC_GENE;
312293f7848bSJeff Kirsher 	pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
312393f7848bSJeff Kirsher 	dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
312493f7848bSJeff Kirsher }
312593f7848bSJeff Kirsher 
312693f7848bSJeff Kirsher /*
312793f7848bSJeff Kirsher  * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
312893f7848bSJeff Kirsher  * when the PCI-E Completion packets are aligned on an 8-byte
312993f7848bSJeff Kirsher  * boundary.  Some PCI-E chip sets always align Completion packets; on
313093f7848bSJeff Kirsher  * the ones that do not, the alignment can be enforced by enabling
313193f7848bSJeff Kirsher  * ECRC generation (if supported).
313293f7848bSJeff Kirsher  *
313393f7848bSJeff Kirsher  * When PCI-E Completion packets are not aligned, it is actually more
313493f7848bSJeff Kirsher  * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
313593f7848bSJeff Kirsher  *
313693f7848bSJeff Kirsher  * If the driver can neither enable ECRC nor verify that it has
313793f7848bSJeff Kirsher  * already been enabled, then it must use a firmware image which works
313893f7848bSJeff Kirsher  * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
313993f7848bSJeff Kirsher  * should also ensure that it never gives the device a Read-DMA which is
314093f7848bSJeff Kirsher  * larger than 2KB by setting the tx_boundary to 2KB.  If ECRC is
314193f7848bSJeff Kirsher  * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
314293f7848bSJeff Kirsher  * firmware image, and set tx_boundary to 4KB.
314393f7848bSJeff Kirsher  */
314493f7848bSJeff Kirsher 
314593f7848bSJeff Kirsher static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
314693f7848bSJeff Kirsher {
314793f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
314893f7848bSJeff Kirsher 	struct device *dev = &pdev->dev;
314993f7848bSJeff Kirsher 	int status;
315093f7848bSJeff Kirsher 
315193f7848bSJeff Kirsher 	mgp->tx_boundary = 4096;
315293f7848bSJeff Kirsher 	/*
315393f7848bSJeff Kirsher 	 * Verify the max read request size was set to 4KB
315493f7848bSJeff Kirsher 	 * before trying the test with 4KB.
315593f7848bSJeff Kirsher 	 */
315693f7848bSJeff Kirsher 	status = pcie_get_readrq(pdev);
315793f7848bSJeff Kirsher 	if (status < 0) {
315893f7848bSJeff Kirsher 		dev_err(dev, "Couldn't read max read req size: %d\n", status);
315993f7848bSJeff Kirsher 		goto abort;
316093f7848bSJeff Kirsher 	}
316193f7848bSJeff Kirsher 	if (status != 4096) {
316293f7848bSJeff Kirsher 		dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
316393f7848bSJeff Kirsher 		mgp->tx_boundary = 2048;
316493f7848bSJeff Kirsher 	}
316593f7848bSJeff Kirsher 	/*
316693f7848bSJeff Kirsher 	 * load the optimized firmware (which assumes aligned PCIe
316793f7848bSJeff Kirsher 	 * completions) in order to see if it works on this host.
316893f7848bSJeff Kirsher 	 */
316993f7848bSJeff Kirsher 	set_fw_name(mgp, myri10ge_fw_aligned, false);
317093f7848bSJeff Kirsher 	status = myri10ge_load_firmware(mgp, 1);
317193f7848bSJeff Kirsher 	if (status != 0) {
317293f7848bSJeff Kirsher 		goto abort;
317393f7848bSJeff Kirsher 	}
317493f7848bSJeff Kirsher 
317593f7848bSJeff Kirsher 	/*
317693f7848bSJeff Kirsher 	 * Enable ECRC if possible
317793f7848bSJeff Kirsher 	 */
317893f7848bSJeff Kirsher 	myri10ge_enable_ecrc(mgp);
317993f7848bSJeff Kirsher 
318093f7848bSJeff Kirsher 	/*
318193f7848bSJeff Kirsher 	 * Run a DMA test which watches for unaligned completions and
318293f7848bSJeff Kirsher 	 * aborts on the first one seen.
318393f7848bSJeff Kirsher 	 */
318493f7848bSJeff Kirsher 
318593f7848bSJeff Kirsher 	status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
318693f7848bSJeff Kirsher 	if (status == 0)
318793f7848bSJeff Kirsher 		return;		/* keep the aligned firmware */
318893f7848bSJeff Kirsher 
318993f7848bSJeff Kirsher 	if (status != -E2BIG)
319093f7848bSJeff Kirsher 		dev_warn(dev, "DMA test failed: %d\n", status);
319193f7848bSJeff Kirsher 	if (status == -ENOSYS)
319293f7848bSJeff Kirsher 		dev_warn(dev, "Falling back to ethp! "
319393f7848bSJeff Kirsher 			 "Please install up to date fw\n");
319493f7848bSJeff Kirsher abort:
319593f7848bSJeff Kirsher 	/* fall back to using the unaligned firmware */
319693f7848bSJeff Kirsher 	mgp->tx_boundary = 2048;
319793f7848bSJeff Kirsher 	set_fw_name(mgp, myri10ge_fw_unaligned, false);
319893f7848bSJeff Kirsher }
319993f7848bSJeff Kirsher 
320093f7848bSJeff Kirsher static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
320193f7848bSJeff Kirsher {
320293f7848bSJeff Kirsher 	int overridden = 0;
320393f7848bSJeff Kirsher 
320493f7848bSJeff Kirsher 	if (myri10ge_force_firmware == 0) {
32059503e255SJiang Liu 		int link_width;
320693f7848bSJeff Kirsher 		u16 lnk;
320793f7848bSJeff Kirsher 
32089503e255SJiang Liu 		pcie_capability_read_word(mgp->pdev, PCI_EXP_LNKSTA, &lnk);
320993f7848bSJeff Kirsher 		link_width = (lnk >> 4) & 0x3f;
321093f7848bSJeff Kirsher 
321193f7848bSJeff Kirsher 		/* Check to see if Link is less than 8 or if the
321293f7848bSJeff Kirsher 		 * upstream bridge is known to provide aligned
321393f7848bSJeff Kirsher 		 * completions */
321493f7848bSJeff Kirsher 		if (link_width < 8) {
321593f7848bSJeff Kirsher 			dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
321693f7848bSJeff Kirsher 				 link_width);
321793f7848bSJeff Kirsher 			mgp->tx_boundary = 4096;
321893f7848bSJeff Kirsher 			set_fw_name(mgp, myri10ge_fw_aligned, false);
321993f7848bSJeff Kirsher 		} else {
322093f7848bSJeff Kirsher 			myri10ge_firmware_probe(mgp);
322193f7848bSJeff Kirsher 		}
322293f7848bSJeff Kirsher 	} else {
322393f7848bSJeff Kirsher 		if (myri10ge_force_firmware == 1) {
322493f7848bSJeff Kirsher 			dev_info(&mgp->pdev->dev,
322593f7848bSJeff Kirsher 				 "Assuming aligned completions (forced)\n");
322693f7848bSJeff Kirsher 			mgp->tx_boundary = 4096;
322793f7848bSJeff Kirsher 			set_fw_name(mgp, myri10ge_fw_aligned, false);
322893f7848bSJeff Kirsher 		} else {
322993f7848bSJeff Kirsher 			dev_info(&mgp->pdev->dev,
323093f7848bSJeff Kirsher 				 "Assuming unaligned completions (forced)\n");
323193f7848bSJeff Kirsher 			mgp->tx_boundary = 2048;
323293f7848bSJeff Kirsher 			set_fw_name(mgp, myri10ge_fw_unaligned, false);
323393f7848bSJeff Kirsher 		}
323493f7848bSJeff Kirsher 	}
323593f7848bSJeff Kirsher 
3236b51d23e4SDan Streetman 	kernel_param_lock(THIS_MODULE);
323793f7848bSJeff Kirsher 	if (myri10ge_fw_name != NULL) {
323893f7848bSJeff Kirsher 		char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
323993f7848bSJeff Kirsher 		if (fw_name) {
324093f7848bSJeff Kirsher 			overridden = 1;
324193f7848bSJeff Kirsher 			set_fw_name(mgp, fw_name, true);
324293f7848bSJeff Kirsher 		}
324393f7848bSJeff Kirsher 	}
3244b51d23e4SDan Streetman 	kernel_param_unlock(THIS_MODULE);
324593f7848bSJeff Kirsher 
324693f7848bSJeff Kirsher 	if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
324793f7848bSJeff Kirsher 	    myri10ge_fw_names[mgp->board_number] != NULL &&
324893f7848bSJeff Kirsher 	    strlen(myri10ge_fw_names[mgp->board_number])) {
324993f7848bSJeff Kirsher 		set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
325093f7848bSJeff Kirsher 		overridden = 1;
325193f7848bSJeff Kirsher 	}
325293f7848bSJeff Kirsher 	if (overridden)
325393f7848bSJeff Kirsher 		dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
325493f7848bSJeff Kirsher 			 mgp->fw_name);
325593f7848bSJeff Kirsher }
325693f7848bSJeff Kirsher 
325793f7848bSJeff Kirsher static void myri10ge_mask_surprise_down(struct pci_dev *pdev)
325893f7848bSJeff Kirsher {
325993f7848bSJeff Kirsher 	struct pci_dev *bridge = pdev->bus->self;
326093f7848bSJeff Kirsher 	int cap;
326193f7848bSJeff Kirsher 	u32 mask;
326293f7848bSJeff Kirsher 
326393f7848bSJeff Kirsher 	if (bridge == NULL)
326493f7848bSJeff Kirsher 		return;
326593f7848bSJeff Kirsher 
326693f7848bSJeff Kirsher 	cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
326793f7848bSJeff Kirsher 	if (cap) {
326893f7848bSJeff Kirsher 		/* a sram parity error can cause a surprise link
326993f7848bSJeff Kirsher 		 * down; since we expect and can recover from sram
327093f7848bSJeff Kirsher 		 * parity errors, mask surprise link down events */
327193f7848bSJeff Kirsher 		pci_read_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, &mask);
327293f7848bSJeff Kirsher 		mask |= 0x20;
327393f7848bSJeff Kirsher 		pci_write_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, mask);
327493f7848bSJeff Kirsher 	}
327593f7848bSJeff Kirsher }
327693f7848bSJeff Kirsher 
327793f7848bSJeff Kirsher #ifdef CONFIG_PM
327893f7848bSJeff Kirsher static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
327993f7848bSJeff Kirsher {
328093f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
328193f7848bSJeff Kirsher 	struct net_device *netdev;
328293f7848bSJeff Kirsher 
328393f7848bSJeff Kirsher 	mgp = pci_get_drvdata(pdev);
328493f7848bSJeff Kirsher 	if (mgp == NULL)
328593f7848bSJeff Kirsher 		return -EINVAL;
328693f7848bSJeff Kirsher 	netdev = mgp->dev;
328793f7848bSJeff Kirsher 
328893f7848bSJeff Kirsher 	netif_device_detach(netdev);
328993f7848bSJeff Kirsher 	if (netif_running(netdev)) {
329093f7848bSJeff Kirsher 		netdev_info(netdev, "closing\n");
329193f7848bSJeff Kirsher 		rtnl_lock();
329293f7848bSJeff Kirsher 		myri10ge_close(netdev);
329393f7848bSJeff Kirsher 		rtnl_unlock();
329493f7848bSJeff Kirsher 	}
329593f7848bSJeff Kirsher 	myri10ge_dummy_rdma(mgp, 0);
329693f7848bSJeff Kirsher 	pci_save_state(pdev);
329793f7848bSJeff Kirsher 	pci_disable_device(pdev);
329893f7848bSJeff Kirsher 
329993f7848bSJeff Kirsher 	return pci_set_power_state(pdev, pci_choose_state(pdev, state));
330093f7848bSJeff Kirsher }
330193f7848bSJeff Kirsher 
330293f7848bSJeff Kirsher static int myri10ge_resume(struct pci_dev *pdev)
330393f7848bSJeff Kirsher {
330493f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
330593f7848bSJeff Kirsher 	struct net_device *netdev;
330693f7848bSJeff Kirsher 	int status;
330793f7848bSJeff Kirsher 	u16 vendor;
330893f7848bSJeff Kirsher 
330993f7848bSJeff Kirsher 	mgp = pci_get_drvdata(pdev);
331093f7848bSJeff Kirsher 	if (mgp == NULL)
331193f7848bSJeff Kirsher 		return -EINVAL;
331293f7848bSJeff Kirsher 	netdev = mgp->dev;
33131ca01512SYijing Wang 	pci_set_power_state(pdev, PCI_D0);	/* zeros conf space as a side effect */
331493f7848bSJeff Kirsher 	msleep(5);		/* give card time to respond */
331593f7848bSJeff Kirsher 	pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
331693f7848bSJeff Kirsher 	if (vendor == 0xffff) {
331793f7848bSJeff Kirsher 		netdev_err(mgp->dev, "device disappeared!\n");
331893f7848bSJeff Kirsher 		return -EIO;
331993f7848bSJeff Kirsher 	}
332093f7848bSJeff Kirsher 
332193f7848bSJeff Kirsher 	pci_restore_state(pdev);
332293f7848bSJeff Kirsher 
332393f7848bSJeff Kirsher 	status = pci_enable_device(pdev);
332493f7848bSJeff Kirsher 	if (status) {
332593f7848bSJeff Kirsher 		dev_err(&pdev->dev, "failed to enable device\n");
332693f7848bSJeff Kirsher 		return status;
332793f7848bSJeff Kirsher 	}
332893f7848bSJeff Kirsher 
332993f7848bSJeff Kirsher 	pci_set_master(pdev);
333093f7848bSJeff Kirsher 
333193f7848bSJeff Kirsher 	myri10ge_reset(mgp);
333293f7848bSJeff Kirsher 	myri10ge_dummy_rdma(mgp, 1);
333393f7848bSJeff Kirsher 
333493f7848bSJeff Kirsher 	/* Save configuration space to be restored if the
333593f7848bSJeff Kirsher 	 * nic resets due to a parity error */
333693f7848bSJeff Kirsher 	pci_save_state(pdev);
333793f7848bSJeff Kirsher 
333893f7848bSJeff Kirsher 	if (netif_running(netdev)) {
333993f7848bSJeff Kirsher 		rtnl_lock();
334093f7848bSJeff Kirsher 		status = myri10ge_open(netdev);
334193f7848bSJeff Kirsher 		rtnl_unlock();
334293f7848bSJeff Kirsher 		if (status != 0)
334393f7848bSJeff Kirsher 			goto abort_with_enabled;
334493f7848bSJeff Kirsher 
334593f7848bSJeff Kirsher 	}
334693f7848bSJeff Kirsher 	netif_device_attach(netdev);
334793f7848bSJeff Kirsher 
334893f7848bSJeff Kirsher 	return 0;
334993f7848bSJeff Kirsher 
335093f7848bSJeff Kirsher abort_with_enabled:
335193f7848bSJeff Kirsher 	pci_disable_device(pdev);
335293f7848bSJeff Kirsher 	return -EIO;
335393f7848bSJeff Kirsher 
335493f7848bSJeff Kirsher }
335593f7848bSJeff Kirsher #endif				/* CONFIG_PM */
335693f7848bSJeff Kirsher 
335793f7848bSJeff Kirsher static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
335893f7848bSJeff Kirsher {
335993f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
336093f7848bSJeff Kirsher 	int vs = mgp->vendor_specific_offset;
336193f7848bSJeff Kirsher 	u32 reboot;
336293f7848bSJeff Kirsher 
336393f7848bSJeff Kirsher 	/*enter read32 mode */
336493f7848bSJeff Kirsher 	pci_write_config_byte(pdev, vs + 0x10, 0x3);
336593f7848bSJeff Kirsher 
336693f7848bSJeff Kirsher 	/*read REBOOT_STATUS (0xfffffff0) */
336793f7848bSJeff Kirsher 	pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
336893f7848bSJeff Kirsher 	pci_read_config_dword(pdev, vs + 0x14, &reboot);
336993f7848bSJeff Kirsher 	return reboot;
337093f7848bSJeff Kirsher }
337193f7848bSJeff Kirsher 
337293f7848bSJeff Kirsher static void
337393f7848bSJeff Kirsher myri10ge_check_slice(struct myri10ge_slice_state *ss, int *reset_needed,
337493f7848bSJeff Kirsher 		     int *busy_slice_cnt, u32 rx_pause_cnt)
337593f7848bSJeff Kirsher {
337693f7848bSJeff Kirsher 	struct myri10ge_priv *mgp = ss->mgp;
337793f7848bSJeff Kirsher 	int slice = ss - mgp->ss;
337893f7848bSJeff Kirsher 
337993f7848bSJeff Kirsher 	if (ss->tx.req != ss->tx.done &&
338093f7848bSJeff Kirsher 	    ss->tx.done == ss->watchdog_tx_done &&
338193f7848bSJeff Kirsher 	    ss->watchdog_tx_req != ss->watchdog_tx_done) {
338293f7848bSJeff Kirsher 		/* nic seems like it might be stuck.. */
338393f7848bSJeff Kirsher 		if (rx_pause_cnt != mgp->watchdog_pause) {
338493f7848bSJeff Kirsher 			if (net_ratelimit())
338593f7848bSJeff Kirsher 				netdev_warn(mgp->dev, "slice %d: TX paused, "
338693f7848bSJeff Kirsher 					    "check link partner\n", slice);
338793f7848bSJeff Kirsher 		} else {
338893f7848bSJeff Kirsher 			netdev_warn(mgp->dev,
338993f7848bSJeff Kirsher 				    "slice %d: TX stuck %d %d %d %d %d %d\n",
339093f7848bSJeff Kirsher 				    slice, ss->tx.queue_active, ss->tx.req,
339193f7848bSJeff Kirsher 				    ss->tx.done, ss->tx.pkt_start,
339293f7848bSJeff Kirsher 				    ss->tx.pkt_done,
339393f7848bSJeff Kirsher 				    (int)ntohl(mgp->ss[slice].fw_stats->
339493f7848bSJeff Kirsher 					       send_done_count));
339593f7848bSJeff Kirsher 			*reset_needed = 1;
339693f7848bSJeff Kirsher 			ss->stuck = 1;
339793f7848bSJeff Kirsher 		}
339893f7848bSJeff Kirsher 	}
339993f7848bSJeff Kirsher 	if (ss->watchdog_tx_done != ss->tx.done ||
340093f7848bSJeff Kirsher 	    ss->watchdog_rx_done != ss->rx_done.cnt) {
340193f7848bSJeff Kirsher 		*busy_slice_cnt += 1;
340293f7848bSJeff Kirsher 	}
340393f7848bSJeff Kirsher 	ss->watchdog_tx_done = ss->tx.done;
340493f7848bSJeff Kirsher 	ss->watchdog_tx_req = ss->tx.req;
340593f7848bSJeff Kirsher 	ss->watchdog_rx_done = ss->rx_done.cnt;
340693f7848bSJeff Kirsher }
340793f7848bSJeff Kirsher 
340893f7848bSJeff Kirsher /*
340993f7848bSJeff Kirsher  * This watchdog is used to check whether the board has suffered
341093f7848bSJeff Kirsher  * from a parity error and needs to be recovered.
341193f7848bSJeff Kirsher  */
341293f7848bSJeff Kirsher static void myri10ge_watchdog(struct work_struct *work)
341393f7848bSJeff Kirsher {
341493f7848bSJeff Kirsher 	struct myri10ge_priv *mgp =
341593f7848bSJeff Kirsher 	    container_of(work, struct myri10ge_priv, watchdog_work);
341693f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
341793f7848bSJeff Kirsher 	u32 reboot, rx_pause_cnt;
341893f7848bSJeff Kirsher 	int status, rebooted;
341993f7848bSJeff Kirsher 	int i;
342093f7848bSJeff Kirsher 	int reset_needed = 0;
342193f7848bSJeff Kirsher 	int busy_slice_cnt = 0;
342293f7848bSJeff Kirsher 	u16 cmd, vendor;
342393f7848bSJeff Kirsher 
342493f7848bSJeff Kirsher 	mgp->watchdog_resets++;
342593f7848bSJeff Kirsher 	pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
342693f7848bSJeff Kirsher 	rebooted = 0;
342793f7848bSJeff Kirsher 	if ((cmd & PCI_COMMAND_MASTER) == 0) {
342893f7848bSJeff Kirsher 		/* Bus master DMA disabled?  Check to see
342993f7848bSJeff Kirsher 		 * if the card rebooted due to a parity error
343093f7848bSJeff Kirsher 		 * For now, just report it */
343193f7848bSJeff Kirsher 		reboot = myri10ge_read_reboot(mgp);
343293f7848bSJeff Kirsher 		netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
343393f7848bSJeff Kirsher 			   reboot, myri10ge_reset_recover ? "" : " not");
343493f7848bSJeff Kirsher 		if (myri10ge_reset_recover == 0)
343593f7848bSJeff Kirsher 			return;
343693f7848bSJeff Kirsher 		rtnl_lock();
343793f7848bSJeff Kirsher 		mgp->rebooted = 1;
343893f7848bSJeff Kirsher 		rebooted = 1;
343993f7848bSJeff Kirsher 		myri10ge_close(mgp->dev);
344093f7848bSJeff Kirsher 		myri10ge_reset_recover--;
344193f7848bSJeff Kirsher 		mgp->rebooted = 0;
344293f7848bSJeff Kirsher 		/*
344393f7848bSJeff Kirsher 		 * A rebooted nic will come back with config space as
344493f7848bSJeff Kirsher 		 * it was after power was applied to PCIe bus.
344593f7848bSJeff Kirsher 		 * Attempt to restore config space which was saved
344693f7848bSJeff Kirsher 		 * when the driver was loaded, or the last time the
344793f7848bSJeff Kirsher 		 * nic was resumed from power saving mode.
344893f7848bSJeff Kirsher 		 */
344993f7848bSJeff Kirsher 		pci_restore_state(mgp->pdev);
345093f7848bSJeff Kirsher 
345193f7848bSJeff Kirsher 		/* save state again for accounting reasons */
345293f7848bSJeff Kirsher 		pci_save_state(mgp->pdev);
345393f7848bSJeff Kirsher 
345493f7848bSJeff Kirsher 	} else {
345593f7848bSJeff Kirsher 		/* if we get back -1's from our slot, perhaps somebody
345693f7848bSJeff Kirsher 		 * powered off our card.  Don't try to reset it in
345793f7848bSJeff Kirsher 		 * this case */
345893f7848bSJeff Kirsher 		if (cmd == 0xffff) {
345993f7848bSJeff Kirsher 			pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
346093f7848bSJeff Kirsher 			if (vendor == 0xffff) {
346193f7848bSJeff Kirsher 				netdev_err(mgp->dev, "device disappeared!\n");
346293f7848bSJeff Kirsher 				return;
346393f7848bSJeff Kirsher 			}
346493f7848bSJeff Kirsher 		}
346593f7848bSJeff Kirsher 		/* Perhaps it is a software error. See if stuck slice
346693f7848bSJeff Kirsher 		 * has recovered, reset if not */
346793f7848bSJeff Kirsher 		rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
346893f7848bSJeff Kirsher 		for (i = 0; i < mgp->num_slices; i++) {
346993f7848bSJeff Kirsher 			ss = mgp->ss;
347093f7848bSJeff Kirsher 			if (ss->stuck) {
347193f7848bSJeff Kirsher 				myri10ge_check_slice(ss, &reset_needed,
347293f7848bSJeff Kirsher 						     &busy_slice_cnt,
347393f7848bSJeff Kirsher 						     rx_pause_cnt);
347493f7848bSJeff Kirsher 				ss->stuck = 0;
347593f7848bSJeff Kirsher 			}
347693f7848bSJeff Kirsher 		}
347793f7848bSJeff Kirsher 		if (!reset_needed) {
347893f7848bSJeff Kirsher 			netdev_dbg(mgp->dev, "not resetting\n");
347993f7848bSJeff Kirsher 			return;
348093f7848bSJeff Kirsher 		}
348193f7848bSJeff Kirsher 
348293f7848bSJeff Kirsher 		netdev_err(mgp->dev, "device timeout, resetting\n");
348393f7848bSJeff Kirsher 	}
348493f7848bSJeff Kirsher 
348593f7848bSJeff Kirsher 	if (!rebooted) {
348693f7848bSJeff Kirsher 		rtnl_lock();
348793f7848bSJeff Kirsher 		myri10ge_close(mgp->dev);
348893f7848bSJeff Kirsher 	}
348993f7848bSJeff Kirsher 	status = myri10ge_load_firmware(mgp, 1);
349093f7848bSJeff Kirsher 	if (status != 0)
349193f7848bSJeff Kirsher 		netdev_err(mgp->dev, "failed to load firmware\n");
349293f7848bSJeff Kirsher 	else
349393f7848bSJeff Kirsher 		myri10ge_open(mgp->dev);
349493f7848bSJeff Kirsher 	rtnl_unlock();
349593f7848bSJeff Kirsher }
349693f7848bSJeff Kirsher 
349793f7848bSJeff Kirsher /*
349893f7848bSJeff Kirsher  * We use our own timer routine rather than relying upon
349993f7848bSJeff Kirsher  * netdev->tx_timeout because we have a very large hardware transmit
350093f7848bSJeff Kirsher  * queue.  Due to the large queue, the netdev->tx_timeout function
350193f7848bSJeff Kirsher  * cannot detect a NIC with a parity error in a timely fashion if the
350293f7848bSJeff Kirsher  * NIC is lightly loaded.
350393f7848bSJeff Kirsher  */
3504e99e88a9SKees Cook static void myri10ge_watchdog_timer(struct timer_list *t)
350593f7848bSJeff Kirsher {
350693f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
350793f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
350893f7848bSJeff Kirsher 	int i, reset_needed, busy_slice_cnt;
350993f7848bSJeff Kirsher 	u32 rx_pause_cnt;
351093f7848bSJeff Kirsher 	u16 cmd;
351193f7848bSJeff Kirsher 
3512e99e88a9SKees Cook 	mgp = from_timer(mgp, t, watchdog_timer);
351393f7848bSJeff Kirsher 
351493f7848bSJeff Kirsher 	rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
351593f7848bSJeff Kirsher 	busy_slice_cnt = 0;
351693f7848bSJeff Kirsher 	for (i = 0, reset_needed = 0;
351793f7848bSJeff Kirsher 	     i < mgp->num_slices && reset_needed == 0; ++i) {
351893f7848bSJeff Kirsher 
351993f7848bSJeff Kirsher 		ss = &mgp->ss[i];
352093f7848bSJeff Kirsher 		if (ss->rx_small.watchdog_needed) {
352193f7848bSJeff Kirsher 			myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
352293f7848bSJeff Kirsher 						mgp->small_bytes + MXGEFW_PAD,
352393f7848bSJeff Kirsher 						1);
352493f7848bSJeff Kirsher 			if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
352593f7848bSJeff Kirsher 			    myri10ge_fill_thresh)
352693f7848bSJeff Kirsher 				ss->rx_small.watchdog_needed = 0;
352793f7848bSJeff Kirsher 		}
352893f7848bSJeff Kirsher 		if (ss->rx_big.watchdog_needed) {
352993f7848bSJeff Kirsher 			myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
353093f7848bSJeff Kirsher 						mgp->big_bytes, 1);
353193f7848bSJeff Kirsher 			if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
353293f7848bSJeff Kirsher 			    myri10ge_fill_thresh)
353393f7848bSJeff Kirsher 				ss->rx_big.watchdog_needed = 0;
353493f7848bSJeff Kirsher 		}
353593f7848bSJeff Kirsher 		myri10ge_check_slice(ss, &reset_needed, &busy_slice_cnt,
353693f7848bSJeff Kirsher 				     rx_pause_cnt);
353793f7848bSJeff Kirsher 	}
353893f7848bSJeff Kirsher 	/* if we've sent or received no traffic, poll the NIC to
353993f7848bSJeff Kirsher 	 * ensure it is still there.  Otherwise, we risk not noticing
354093f7848bSJeff Kirsher 	 * an error in a timely fashion */
354193f7848bSJeff Kirsher 	if (busy_slice_cnt == 0) {
354293f7848bSJeff Kirsher 		pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
354393f7848bSJeff Kirsher 		if ((cmd & PCI_COMMAND_MASTER) == 0) {
354493f7848bSJeff Kirsher 			reset_needed = 1;
354593f7848bSJeff Kirsher 		}
354693f7848bSJeff Kirsher 	}
354793f7848bSJeff Kirsher 	mgp->watchdog_pause = rx_pause_cnt;
354893f7848bSJeff Kirsher 
354993f7848bSJeff Kirsher 	if (reset_needed) {
355093f7848bSJeff Kirsher 		schedule_work(&mgp->watchdog_work);
355193f7848bSJeff Kirsher 	} else {
355293f7848bSJeff Kirsher 		/* rearm timer */
355393f7848bSJeff Kirsher 		mod_timer(&mgp->watchdog_timer,
355493f7848bSJeff Kirsher 			  jiffies + myri10ge_watchdog_timeout * HZ);
355593f7848bSJeff Kirsher 	}
355693f7848bSJeff Kirsher }
355793f7848bSJeff Kirsher 
355893f7848bSJeff Kirsher static void myri10ge_free_slices(struct myri10ge_priv *mgp)
355993f7848bSJeff Kirsher {
356093f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
356193f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
356293f7848bSJeff Kirsher 	size_t bytes;
356393f7848bSJeff Kirsher 	int i;
356493f7848bSJeff Kirsher 
356593f7848bSJeff Kirsher 	if (mgp->ss == NULL)
356693f7848bSJeff Kirsher 		return;
356793f7848bSJeff Kirsher 
356893f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
356993f7848bSJeff Kirsher 		ss = &mgp->ss[i];
357093f7848bSJeff Kirsher 		if (ss->rx_done.entry != NULL) {
357193f7848bSJeff Kirsher 			bytes = mgp->max_intr_slots *
357293f7848bSJeff Kirsher 			    sizeof(*ss->rx_done.entry);
357393f7848bSJeff Kirsher 			dma_free_coherent(&pdev->dev, bytes,
357493f7848bSJeff Kirsher 					  ss->rx_done.entry, ss->rx_done.bus);
357593f7848bSJeff Kirsher 			ss->rx_done.entry = NULL;
357693f7848bSJeff Kirsher 		}
357793f7848bSJeff Kirsher 		if (ss->fw_stats != NULL) {
357893f7848bSJeff Kirsher 			bytes = sizeof(*ss->fw_stats);
357993f7848bSJeff Kirsher 			dma_free_coherent(&pdev->dev, bytes,
358093f7848bSJeff Kirsher 					  ss->fw_stats, ss->fw_stats_bus);
358193f7848bSJeff Kirsher 			ss->fw_stats = NULL;
358293f7848bSJeff Kirsher 		}
35830dde8026SHyong-Youb Kim 		napi_hash_del(&ss->napi);
358493f7848bSJeff Kirsher 		netif_napi_del(&ss->napi);
358593f7848bSJeff Kirsher 	}
35860dde8026SHyong-Youb Kim 	/* Wait till napi structs are no longer used, and then free ss. */
35870dde8026SHyong-Youb Kim 	synchronize_rcu();
358893f7848bSJeff Kirsher 	kfree(mgp->ss);
358993f7848bSJeff Kirsher 	mgp->ss = NULL;
359093f7848bSJeff Kirsher }
359193f7848bSJeff Kirsher 
359293f7848bSJeff Kirsher static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
359393f7848bSJeff Kirsher {
359493f7848bSJeff Kirsher 	struct myri10ge_slice_state *ss;
359593f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
359693f7848bSJeff Kirsher 	size_t bytes;
359793f7848bSJeff Kirsher 	int i;
359893f7848bSJeff Kirsher 
359993f7848bSJeff Kirsher 	bytes = sizeof(*mgp->ss) * mgp->num_slices;
360093f7848bSJeff Kirsher 	mgp->ss = kzalloc(bytes, GFP_KERNEL);
360193f7848bSJeff Kirsher 	if (mgp->ss == NULL) {
360293f7848bSJeff Kirsher 		return -ENOMEM;
360393f7848bSJeff Kirsher 	}
360493f7848bSJeff Kirsher 
360593f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
360693f7848bSJeff Kirsher 		ss = &mgp->ss[i];
360793f7848bSJeff Kirsher 		bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
3608ede23fa8SJoe Perches 		ss->rx_done.entry = dma_zalloc_coherent(&pdev->dev, bytes,
360993f7848bSJeff Kirsher 							&ss->rx_done.bus,
3610ede23fa8SJoe Perches 							GFP_KERNEL);
361193f7848bSJeff Kirsher 		if (ss->rx_done.entry == NULL)
361293f7848bSJeff Kirsher 			goto abort;
361393f7848bSJeff Kirsher 		bytes = sizeof(*ss->fw_stats);
361493f7848bSJeff Kirsher 		ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
361593f7848bSJeff Kirsher 						  &ss->fw_stats_bus,
361693f7848bSJeff Kirsher 						  GFP_KERNEL);
361793f7848bSJeff Kirsher 		if (ss->fw_stats == NULL)
361893f7848bSJeff Kirsher 			goto abort;
361993f7848bSJeff Kirsher 		ss->mgp = mgp;
362093f7848bSJeff Kirsher 		ss->dev = mgp->dev;
362193f7848bSJeff Kirsher 		netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
362293f7848bSJeff Kirsher 			       myri10ge_napi_weight);
362393f7848bSJeff Kirsher 	}
362493f7848bSJeff Kirsher 	return 0;
362593f7848bSJeff Kirsher abort:
362693f7848bSJeff Kirsher 	myri10ge_free_slices(mgp);
362793f7848bSJeff Kirsher 	return -ENOMEM;
362893f7848bSJeff Kirsher }
362993f7848bSJeff Kirsher 
363093f7848bSJeff Kirsher /*
363193f7848bSJeff Kirsher  * This function determines the number of slices supported.
363293f7848bSJeff Kirsher  * The number slices is the minimum of the number of CPUS,
363393f7848bSJeff Kirsher  * the number of MSI-X irqs supported, the number of slices
363493f7848bSJeff Kirsher  * supported by the firmware
363593f7848bSJeff Kirsher  */
363693f7848bSJeff Kirsher static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
363793f7848bSJeff Kirsher {
363893f7848bSJeff Kirsher 	struct myri10ge_cmd cmd;
363993f7848bSJeff Kirsher 	struct pci_dev *pdev = mgp->pdev;
364093f7848bSJeff Kirsher 	char *old_fw;
364193f7848bSJeff Kirsher 	bool old_allocated;
364240b29562SYijing Wang 	int i, status, ncpus;
364393f7848bSJeff Kirsher 
364493f7848bSJeff Kirsher 	mgp->num_slices = 1;
364598f2d21fSYuval Mintz 	ncpus = netif_get_num_default_rss_queues();
364693f7848bSJeff Kirsher 
364740b29562SYijing Wang 	if (myri10ge_max_slices == 1 || !pdev->msix_cap ||
364893f7848bSJeff Kirsher 	    (myri10ge_max_slices == -1 && ncpus < 2))
364993f7848bSJeff Kirsher 		return;
365093f7848bSJeff Kirsher 
365193f7848bSJeff Kirsher 	/* try to load the slice aware rss firmware */
365293f7848bSJeff Kirsher 	old_fw = mgp->fw_name;
365393f7848bSJeff Kirsher 	old_allocated = mgp->fw_name_allocated;
365493f7848bSJeff Kirsher 	/* don't free old_fw if we override it. */
365593f7848bSJeff Kirsher 	mgp->fw_name_allocated = false;
365693f7848bSJeff Kirsher 
365793f7848bSJeff Kirsher 	if (myri10ge_fw_name != NULL) {
365893f7848bSJeff Kirsher 		dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
365993f7848bSJeff Kirsher 			 myri10ge_fw_name);
366093f7848bSJeff Kirsher 		set_fw_name(mgp, myri10ge_fw_name, false);
366193f7848bSJeff Kirsher 	} else if (old_fw == myri10ge_fw_aligned)
366293f7848bSJeff Kirsher 		set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
366393f7848bSJeff Kirsher 	else
366493f7848bSJeff Kirsher 		set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
366593f7848bSJeff Kirsher 	status = myri10ge_load_firmware(mgp, 0);
366693f7848bSJeff Kirsher 	if (status != 0) {
366793f7848bSJeff Kirsher 		dev_info(&pdev->dev, "Rss firmware not found\n");
366893f7848bSJeff Kirsher 		if (old_allocated)
366993f7848bSJeff Kirsher 			kfree(old_fw);
367093f7848bSJeff Kirsher 		return;
367193f7848bSJeff Kirsher 	}
367293f7848bSJeff Kirsher 
367393f7848bSJeff Kirsher 	/* hit the board with a reset to ensure it is alive */
367493f7848bSJeff Kirsher 	memset(&cmd, 0, sizeof(cmd));
367593f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
367693f7848bSJeff Kirsher 	if (status != 0) {
367793f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev, "failed reset\n");
367893f7848bSJeff Kirsher 		goto abort_with_fw;
367993f7848bSJeff Kirsher 	}
368093f7848bSJeff Kirsher 
368193f7848bSJeff Kirsher 	mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
368293f7848bSJeff Kirsher 
368393f7848bSJeff Kirsher 	/* tell it the size of the interrupt queues */
368493f7848bSJeff Kirsher 	cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
368593f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
368693f7848bSJeff Kirsher 	if (status != 0) {
368793f7848bSJeff Kirsher 		dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
368893f7848bSJeff Kirsher 		goto abort_with_fw;
368993f7848bSJeff Kirsher 	}
369093f7848bSJeff Kirsher 
369193f7848bSJeff Kirsher 	/* ask the maximum number of slices it supports */
369293f7848bSJeff Kirsher 	status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
369393f7848bSJeff Kirsher 	if (status != 0)
369493f7848bSJeff Kirsher 		goto abort_with_fw;
369593f7848bSJeff Kirsher 	else
369693f7848bSJeff Kirsher 		mgp->num_slices = cmd.data0;
369793f7848bSJeff Kirsher 
369893f7848bSJeff Kirsher 	/* Only allow multiple slices if MSI-X is usable */
369993f7848bSJeff Kirsher 	if (!myri10ge_msi) {
370093f7848bSJeff Kirsher 		goto abort_with_fw;
370193f7848bSJeff Kirsher 	}
370293f7848bSJeff Kirsher 
370393f7848bSJeff Kirsher 	/* if the admin did not specify a limit to how many
370493f7848bSJeff Kirsher 	 * slices we should use, cap it automatically to the
370593f7848bSJeff Kirsher 	 * number of CPUs currently online */
370693f7848bSJeff Kirsher 	if (myri10ge_max_slices == -1)
370793f7848bSJeff Kirsher 		myri10ge_max_slices = ncpus;
370893f7848bSJeff Kirsher 
370993f7848bSJeff Kirsher 	if (mgp->num_slices > myri10ge_max_slices)
371093f7848bSJeff Kirsher 		mgp->num_slices = myri10ge_max_slices;
371193f7848bSJeff Kirsher 
371293f7848bSJeff Kirsher 	/* Now try to allocate as many MSI-X vectors as we have
371393f7848bSJeff Kirsher 	 * slices. We give up on MSI-X if we can only get a single
371493f7848bSJeff Kirsher 	 * vector. */
371593f7848bSJeff Kirsher 
371693f7848bSJeff Kirsher 	mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
371793f7848bSJeff Kirsher 				    GFP_KERNEL);
371893f7848bSJeff Kirsher 	if (mgp->msix_vectors == NULL)
37190729cc0cSAlexander Gordeev 		goto no_msix;
372093f7848bSJeff Kirsher 	for (i = 0; i < mgp->num_slices; i++) {
372193f7848bSJeff Kirsher 		mgp->msix_vectors[i].entry = i;
372293f7848bSJeff Kirsher 	}
372393f7848bSJeff Kirsher 
372493f7848bSJeff Kirsher 	while (mgp->num_slices > 1) {
37250729cc0cSAlexander Gordeev 		mgp->num_slices = rounddown_pow_of_two(mgp->num_slices);
372693f7848bSJeff Kirsher 		if (mgp->num_slices == 1)
37270729cc0cSAlexander Gordeev 			goto no_msix;
37280729cc0cSAlexander Gordeev 		status = pci_enable_msix_range(pdev,
37290729cc0cSAlexander Gordeev 					       mgp->msix_vectors,
37300729cc0cSAlexander Gordeev 					       mgp->num_slices,
373193f7848bSJeff Kirsher 					       mgp->num_slices);
37320729cc0cSAlexander Gordeev 		if (status < 0)
37330729cc0cSAlexander Gordeev 			goto no_msix;
37340729cc0cSAlexander Gordeev 
373593f7848bSJeff Kirsher 		pci_disable_msix(pdev);
37360729cc0cSAlexander Gordeev 
37370729cc0cSAlexander Gordeev 		if (status == mgp->num_slices) {
373893f7848bSJeff Kirsher 			if (old_allocated)
373993f7848bSJeff Kirsher 				kfree(old_fw);
374093f7848bSJeff Kirsher 			return;
37410729cc0cSAlexander Gordeev 		} else {
374293f7848bSJeff Kirsher 			mgp->num_slices = status;
37430729cc0cSAlexander Gordeev 		}
374493f7848bSJeff Kirsher 	}
374593f7848bSJeff Kirsher 
37460729cc0cSAlexander Gordeev no_msix:
374793f7848bSJeff Kirsher 	if (mgp->msix_vectors != NULL) {
374893f7848bSJeff Kirsher 		kfree(mgp->msix_vectors);
374993f7848bSJeff Kirsher 		mgp->msix_vectors = NULL;
375093f7848bSJeff Kirsher 	}
375193f7848bSJeff Kirsher 
375293f7848bSJeff Kirsher abort_with_fw:
375393f7848bSJeff Kirsher 	mgp->num_slices = 1;
375493f7848bSJeff Kirsher 	set_fw_name(mgp, old_fw, old_allocated);
375593f7848bSJeff Kirsher 	myri10ge_load_firmware(mgp, 0);
375693f7848bSJeff Kirsher }
375793f7848bSJeff Kirsher 
375893f7848bSJeff Kirsher static const struct net_device_ops myri10ge_netdev_ops = {
375993f7848bSJeff Kirsher 	.ndo_open		= myri10ge_open,
376093f7848bSJeff Kirsher 	.ndo_stop		= myri10ge_close,
376193f7848bSJeff Kirsher 	.ndo_start_xmit		= myri10ge_xmit,
376293f7848bSJeff Kirsher 	.ndo_get_stats64	= myri10ge_get_stats,
376393f7848bSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
376493f7848bSJeff Kirsher 	.ndo_change_mtu		= myri10ge_change_mtu,
3765afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= myri10ge_set_multicast_list,
376693f7848bSJeff Kirsher 	.ndo_set_mac_address	= myri10ge_set_mac_address,
376793f7848bSJeff Kirsher };
376893f7848bSJeff Kirsher 
376993f7848bSJeff Kirsher static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
377093f7848bSJeff Kirsher {
377193f7848bSJeff Kirsher 	struct net_device *netdev;
377293f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
377393f7848bSJeff Kirsher 	struct device *dev = &pdev->dev;
377493f7848bSJeff Kirsher 	int i;
377593f7848bSJeff Kirsher 	int status = -ENXIO;
377693f7848bSJeff Kirsher 	int dac_enabled;
377793f7848bSJeff Kirsher 	unsigned hdr_offset, ss_offset;
377893f7848bSJeff Kirsher 	static int board_number;
377993f7848bSJeff Kirsher 
378093f7848bSJeff Kirsher 	netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
378141de8d4cSJoe Perches 	if (netdev == NULL)
378293f7848bSJeff Kirsher 		return -ENOMEM;
378393f7848bSJeff Kirsher 
378493f7848bSJeff Kirsher 	SET_NETDEV_DEV(netdev, &pdev->dev);
378593f7848bSJeff Kirsher 
378693f7848bSJeff Kirsher 	mgp = netdev_priv(netdev);
378793f7848bSJeff Kirsher 	mgp->dev = netdev;
378893f7848bSJeff Kirsher 	mgp->pdev = pdev;
378993f7848bSJeff Kirsher 	mgp->pause = myri10ge_flow_control;
379093f7848bSJeff Kirsher 	mgp->intr_coal_delay = myri10ge_intr_coal_delay;
379193f7848bSJeff Kirsher 	mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
379293f7848bSJeff Kirsher 	mgp->board_number = board_number;
379393f7848bSJeff Kirsher 	init_waitqueue_head(&mgp->down_wq);
379493f7848bSJeff Kirsher 
379593f7848bSJeff Kirsher 	if (pci_enable_device(pdev)) {
379693f7848bSJeff Kirsher 		dev_err(&pdev->dev, "pci_enable_device call failed\n");
379793f7848bSJeff Kirsher 		status = -ENODEV;
379893f7848bSJeff Kirsher 		goto abort_with_netdev;
379993f7848bSJeff Kirsher 	}
380093f7848bSJeff Kirsher 
380193f7848bSJeff Kirsher 	/* Find the vendor-specific cap so we can check
380293f7848bSJeff Kirsher 	 * the reboot register later on */
380393f7848bSJeff Kirsher 	mgp->vendor_specific_offset
380493f7848bSJeff Kirsher 	    = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
380593f7848bSJeff Kirsher 
380693f7848bSJeff Kirsher 	/* Set our max read request to 4KB */
380793f7848bSJeff Kirsher 	status = pcie_set_readrq(pdev, 4096);
380893f7848bSJeff Kirsher 	if (status != 0) {
380993f7848bSJeff Kirsher 		dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
381093f7848bSJeff Kirsher 			status);
381193f7848bSJeff Kirsher 		goto abort_with_enabled;
381293f7848bSJeff Kirsher 	}
381393f7848bSJeff Kirsher 
381493f7848bSJeff Kirsher 	myri10ge_mask_surprise_down(pdev);
381593f7848bSJeff Kirsher 	pci_set_master(pdev);
381693f7848bSJeff Kirsher 	dac_enabled = 1;
381793f7848bSJeff Kirsher 	status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
381893f7848bSJeff Kirsher 	if (status != 0) {
381993f7848bSJeff Kirsher 		dac_enabled = 0;
382093f7848bSJeff Kirsher 		dev_err(&pdev->dev,
382193f7848bSJeff Kirsher 			"64-bit pci address mask was refused, "
382293f7848bSJeff Kirsher 			"trying 32-bit\n");
382393f7848bSJeff Kirsher 		status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
382493f7848bSJeff Kirsher 	}
382593f7848bSJeff Kirsher 	if (status != 0) {
382693f7848bSJeff Kirsher 		dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
382793f7848bSJeff Kirsher 		goto abort_with_enabled;
382893f7848bSJeff Kirsher 	}
382993f7848bSJeff Kirsher 	(void)pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
383093f7848bSJeff Kirsher 	mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
383193f7848bSJeff Kirsher 				      &mgp->cmd_bus, GFP_KERNEL);
3832f12e77caSJulia Lawall 	if (!mgp->cmd) {
3833f12e77caSJulia Lawall 		status = -ENOMEM;
383493f7848bSJeff Kirsher 		goto abort_with_enabled;
3835f12e77caSJulia Lawall 	}
383693f7848bSJeff Kirsher 
383793f7848bSJeff Kirsher 	mgp->board_span = pci_resource_len(pdev, 0);
383893f7848bSJeff Kirsher 	mgp->iomem_base = pci_resource_start(pdev, 0);
3839e4b6c303SLuis R. Rodriguez 	mgp->wc_cookie = arch_phys_wc_add(mgp->iomem_base, mgp->board_span);
384093f7848bSJeff Kirsher 	mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
384193f7848bSJeff Kirsher 	if (mgp->sram == NULL) {
384293f7848bSJeff Kirsher 		dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
384393f7848bSJeff Kirsher 			mgp->board_span, mgp->iomem_base);
384493f7848bSJeff Kirsher 		status = -ENXIO;
384593f7848bSJeff Kirsher 		goto abort_with_mtrr;
384693f7848bSJeff Kirsher 	}
384793f7848bSJeff Kirsher 	hdr_offset =
384859e955edSAndrew Gallatin 	    swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
384993f7848bSJeff Kirsher 	ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
385059e955edSAndrew Gallatin 	mgp->sram_size = swab32(readl(mgp->sram + ss_offset));
385193f7848bSJeff Kirsher 	if (mgp->sram_size > mgp->board_span ||
385293f7848bSJeff Kirsher 	    mgp->sram_size <= MYRI10GE_FW_OFFSET) {
385393f7848bSJeff Kirsher 		dev_err(&pdev->dev,
385493f7848bSJeff Kirsher 			"invalid sram_size %dB or board span %ldB\n",
385593f7848bSJeff Kirsher 			mgp->sram_size, mgp->board_span);
385693f7848bSJeff Kirsher 		goto abort_with_ioremap;
385793f7848bSJeff Kirsher 	}
385893f7848bSJeff Kirsher 	memcpy_fromio(mgp->eeprom_strings,
385993f7848bSJeff Kirsher 		      mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
386093f7848bSJeff Kirsher 	memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
386193f7848bSJeff Kirsher 	status = myri10ge_read_mac_addr(mgp);
386293f7848bSJeff Kirsher 	if (status)
386393f7848bSJeff Kirsher 		goto abort_with_ioremap;
386493f7848bSJeff Kirsher 
386593f7848bSJeff Kirsher 	for (i = 0; i < ETH_ALEN; i++)
386693f7848bSJeff Kirsher 		netdev->dev_addr[i] = mgp->mac_addr[i];
386793f7848bSJeff Kirsher 
386893f7848bSJeff Kirsher 	myri10ge_select_firmware(mgp);
386993f7848bSJeff Kirsher 
387093f7848bSJeff Kirsher 	status = myri10ge_load_firmware(mgp, 1);
387193f7848bSJeff Kirsher 	if (status != 0) {
387293f7848bSJeff Kirsher 		dev_err(&pdev->dev, "failed to load firmware\n");
387393f7848bSJeff Kirsher 		goto abort_with_ioremap;
387493f7848bSJeff Kirsher 	}
387593f7848bSJeff Kirsher 	myri10ge_probe_slices(mgp);
387693f7848bSJeff Kirsher 	status = myri10ge_alloc_slices(mgp);
387793f7848bSJeff Kirsher 	if (status != 0) {
387893f7848bSJeff Kirsher 		dev_err(&pdev->dev, "failed to alloc slice state\n");
387993f7848bSJeff Kirsher 		goto abort_with_firmware;
388093f7848bSJeff Kirsher 	}
388193f7848bSJeff Kirsher 	netif_set_real_num_tx_queues(netdev, mgp->num_slices);
388293f7848bSJeff Kirsher 	netif_set_real_num_rx_queues(netdev, mgp->num_slices);
388393f7848bSJeff Kirsher 	status = myri10ge_reset(mgp);
388493f7848bSJeff Kirsher 	if (status != 0) {
388593f7848bSJeff Kirsher 		dev_err(&pdev->dev, "failed reset\n");
388693f7848bSJeff Kirsher 		goto abort_with_slices;
388793f7848bSJeff Kirsher 	}
388893f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
388993f7848bSJeff Kirsher 	myri10ge_setup_dca(mgp);
389093f7848bSJeff Kirsher #endif
389193f7848bSJeff Kirsher 	pci_set_drvdata(pdev, mgp);
389244770e11SJarod Wilson 
389344770e11SJarod Wilson 	/* MTU range: 68 - 9000 */
389444770e11SJarod Wilson 	netdev->min_mtu = ETH_MIN_MTU;
389544770e11SJarod Wilson 	netdev->max_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
389644770e11SJarod Wilson 
389744770e11SJarod Wilson 	if (myri10ge_initial_mtu > netdev->max_mtu)
389844770e11SJarod Wilson 		myri10ge_initial_mtu = netdev->max_mtu;
389944770e11SJarod Wilson 	if (myri10ge_initial_mtu < netdev->min_mtu)
390044770e11SJarod Wilson 		myri10ge_initial_mtu = netdev->min_mtu;
390144770e11SJarod Wilson 
390244770e11SJarod Wilson 	netdev->mtu = myri10ge_initial_mtu;
390393f7848bSJeff Kirsher 
390493f7848bSJeff Kirsher 	netdev->netdev_ops = &myri10ge_netdev_ops;
39054ca3221fSAndrew Gallatin 	netdev->hw_features = mgp->features | NETIF_F_RXCSUM;
39061b4c44e6SAndrew Gallatin 
3907f646968fSPatrick McHardy 	/* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
3908f646968fSPatrick McHardy 	netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
39091b4c44e6SAndrew Gallatin 
391093f7848bSJeff Kirsher 	netdev->features = netdev->hw_features;
391193f7848bSJeff Kirsher 
391293f7848bSJeff Kirsher 	if (dac_enabled)
391393f7848bSJeff Kirsher 		netdev->features |= NETIF_F_HIGHDMA;
391493f7848bSJeff Kirsher 
391593f7848bSJeff Kirsher 	netdev->vlan_features |= mgp->features;
391693f7848bSJeff Kirsher 	if (mgp->fw_ver_tiny < 37)
391793f7848bSJeff Kirsher 		netdev->vlan_features &= ~NETIF_F_TSO6;
391893f7848bSJeff Kirsher 	if (mgp->fw_ver_tiny < 32)
391993f7848bSJeff Kirsher 		netdev->vlan_features &= ~NETIF_F_TSO;
392093f7848bSJeff Kirsher 
392193f7848bSJeff Kirsher 	/* make sure we can get an irq, and that MSI can be
3922a7425458SFrancois Romieu 	 * setup (if available). */
392393f7848bSJeff Kirsher 	status = myri10ge_request_irq(mgp);
392493f7848bSJeff Kirsher 	if (status != 0)
392593f7848bSJeff Kirsher 		goto abort_with_firmware;
392693f7848bSJeff Kirsher 	myri10ge_free_irq(mgp);
392793f7848bSJeff Kirsher 
392893f7848bSJeff Kirsher 	/* Save configuration space to be restored if the
392993f7848bSJeff Kirsher 	 * nic resets due to a parity error */
393093f7848bSJeff Kirsher 	pci_save_state(pdev);
393193f7848bSJeff Kirsher 
393293f7848bSJeff Kirsher 	/* Setup the watchdog timer */
3933e99e88a9SKees Cook 	timer_setup(&mgp->watchdog_timer, myri10ge_watchdog_timer, 0);
393493f7848bSJeff Kirsher 
39357ad24ea4SWilfried Klaebe 	netdev->ethtool_ops = &myri10ge_ethtool_ops;
393693f7848bSJeff Kirsher 	INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
393793f7848bSJeff Kirsher 	status = register_netdev(netdev);
393893f7848bSJeff Kirsher 	if (status != 0) {
393993f7848bSJeff Kirsher 		dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
394093f7848bSJeff Kirsher 		goto abort_with_state;
394193f7848bSJeff Kirsher 	}
394293f7848bSJeff Kirsher 	if (mgp->msix_enabled)
3943e4b6c303SLuis R. Rodriguez 		dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
394493f7848bSJeff Kirsher 			 mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
3945e4b6c303SLuis R. Rodriguez 			 (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
394693f7848bSJeff Kirsher 	else
3947e4b6c303SLuis R. Rodriguez 		dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
394893f7848bSJeff Kirsher 			 mgp->msi_enabled ? "MSI" : "xPIC",
3949a7425458SFrancois Romieu 			 pdev->irq, mgp->tx_boundary, mgp->fw_name,
3950e4b6c303SLuis R. Rodriguez 			 (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
395193f7848bSJeff Kirsher 
395293f7848bSJeff Kirsher 	board_number++;
395393f7848bSJeff Kirsher 	return 0;
395493f7848bSJeff Kirsher 
395593f7848bSJeff Kirsher abort_with_state:
395693f7848bSJeff Kirsher 	pci_restore_state(pdev);
395793f7848bSJeff Kirsher 
395893f7848bSJeff Kirsher abort_with_slices:
395993f7848bSJeff Kirsher 	myri10ge_free_slices(mgp);
396093f7848bSJeff Kirsher 
396193f7848bSJeff Kirsher abort_with_firmware:
396293f7848bSJeff Kirsher 	myri10ge_dummy_rdma(mgp, 0);
396393f7848bSJeff Kirsher 
396493f7848bSJeff Kirsher abort_with_ioremap:
396593f7848bSJeff Kirsher 	if (mgp->mac_addr_string != NULL)
396693f7848bSJeff Kirsher 		dev_err(&pdev->dev,
396793f7848bSJeff Kirsher 			"myri10ge_probe() failed: MAC=%s, SN=%ld\n",
396893f7848bSJeff Kirsher 			mgp->mac_addr_string, mgp->serial_number);
396993f7848bSJeff Kirsher 	iounmap(mgp->sram);
397093f7848bSJeff Kirsher 
397193f7848bSJeff Kirsher abort_with_mtrr:
3972e4b6c303SLuis R. Rodriguez 	arch_phys_wc_del(mgp->wc_cookie);
397393f7848bSJeff Kirsher 	dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
397493f7848bSJeff Kirsher 			  mgp->cmd, mgp->cmd_bus);
397593f7848bSJeff Kirsher 
397693f7848bSJeff Kirsher abort_with_enabled:
397793f7848bSJeff Kirsher 	pci_disable_device(pdev);
397893f7848bSJeff Kirsher 
397993f7848bSJeff Kirsher abort_with_netdev:
398093f7848bSJeff Kirsher 	set_fw_name(mgp, NULL, false);
398193f7848bSJeff Kirsher 	free_netdev(netdev);
398293f7848bSJeff Kirsher 	return status;
398393f7848bSJeff Kirsher }
398493f7848bSJeff Kirsher 
398593f7848bSJeff Kirsher /*
398693f7848bSJeff Kirsher  * myri10ge_remove
398793f7848bSJeff Kirsher  *
398893f7848bSJeff Kirsher  * Does what is necessary to shutdown one Myrinet device. Called
398993f7848bSJeff Kirsher  *   once for each Myrinet card by the kernel when a module is
399093f7848bSJeff Kirsher  *   unloaded.
399193f7848bSJeff Kirsher  */
399293f7848bSJeff Kirsher static void myri10ge_remove(struct pci_dev *pdev)
399393f7848bSJeff Kirsher {
399493f7848bSJeff Kirsher 	struct myri10ge_priv *mgp;
399593f7848bSJeff Kirsher 	struct net_device *netdev;
399693f7848bSJeff Kirsher 
399793f7848bSJeff Kirsher 	mgp = pci_get_drvdata(pdev);
399893f7848bSJeff Kirsher 	if (mgp == NULL)
399993f7848bSJeff Kirsher 		return;
400093f7848bSJeff Kirsher 
400193f7848bSJeff Kirsher 	cancel_work_sync(&mgp->watchdog_work);
400293f7848bSJeff Kirsher 	netdev = mgp->dev;
400393f7848bSJeff Kirsher 	unregister_netdev(netdev);
400493f7848bSJeff Kirsher 
400593f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
400693f7848bSJeff Kirsher 	myri10ge_teardown_dca(mgp);
400793f7848bSJeff Kirsher #endif
400893f7848bSJeff Kirsher 	myri10ge_dummy_rdma(mgp, 0);
400993f7848bSJeff Kirsher 
401093f7848bSJeff Kirsher 	/* avoid a memory leak */
401193f7848bSJeff Kirsher 	pci_restore_state(pdev);
401293f7848bSJeff Kirsher 
401393f7848bSJeff Kirsher 	iounmap(mgp->sram);
4014e4b6c303SLuis R. Rodriguez 	arch_phys_wc_del(mgp->wc_cookie);
401593f7848bSJeff Kirsher 	myri10ge_free_slices(mgp);
401693f7848bSJeff Kirsher 	kfree(mgp->msix_vectors);
401793f7848bSJeff Kirsher 	dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
401893f7848bSJeff Kirsher 			  mgp->cmd, mgp->cmd_bus);
401993f7848bSJeff Kirsher 
402093f7848bSJeff Kirsher 	set_fw_name(mgp, NULL, false);
402193f7848bSJeff Kirsher 	free_netdev(netdev);
402293f7848bSJeff Kirsher 	pci_disable_device(pdev);
402393f7848bSJeff Kirsher }
402493f7848bSJeff Kirsher 
402593f7848bSJeff Kirsher #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 	0x0008
402693f7848bSJeff Kirsher #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9	0x0009
402793f7848bSJeff Kirsher 
40289baa3c34SBenoit Taine static const struct pci_device_id myri10ge_pci_tbl[] = {
402993f7848bSJeff Kirsher 	{PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
403093f7848bSJeff Kirsher 	{PCI_DEVICE
403193f7848bSJeff Kirsher 	 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
403293f7848bSJeff Kirsher 	{0},
403393f7848bSJeff Kirsher };
403493f7848bSJeff Kirsher 
403593f7848bSJeff Kirsher MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);
403693f7848bSJeff Kirsher 
403793f7848bSJeff Kirsher static struct pci_driver myri10ge_driver = {
403893f7848bSJeff Kirsher 	.name = "myri10ge",
403993f7848bSJeff Kirsher 	.probe = myri10ge_probe,
404093f7848bSJeff Kirsher 	.remove = myri10ge_remove,
404193f7848bSJeff Kirsher 	.id_table = myri10ge_pci_tbl,
404293f7848bSJeff Kirsher #ifdef CONFIG_PM
404393f7848bSJeff Kirsher 	.suspend = myri10ge_suspend,
404493f7848bSJeff Kirsher 	.resume = myri10ge_resume,
404593f7848bSJeff Kirsher #endif
404693f7848bSJeff Kirsher };
404793f7848bSJeff Kirsher 
404893f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
404993f7848bSJeff Kirsher static int
405093f7848bSJeff Kirsher myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
405193f7848bSJeff Kirsher {
405293f7848bSJeff Kirsher 	int err = driver_for_each_device(&myri10ge_driver.driver,
405393f7848bSJeff Kirsher 					 NULL, &event,
405493f7848bSJeff Kirsher 					 myri10ge_notify_dca_device);
405593f7848bSJeff Kirsher 
405693f7848bSJeff Kirsher 	if (err)
405793f7848bSJeff Kirsher 		return NOTIFY_BAD;
405893f7848bSJeff Kirsher 	return NOTIFY_DONE;
405993f7848bSJeff Kirsher }
406093f7848bSJeff Kirsher 
406193f7848bSJeff Kirsher static struct notifier_block myri10ge_dca_notifier = {
406293f7848bSJeff Kirsher 	.notifier_call = myri10ge_notify_dca,
406393f7848bSJeff Kirsher 	.next = NULL,
406493f7848bSJeff Kirsher 	.priority = 0,
406593f7848bSJeff Kirsher };
406693f7848bSJeff Kirsher #endif				/* CONFIG_MYRI10GE_DCA */
406793f7848bSJeff Kirsher 
406893f7848bSJeff Kirsher static __init int myri10ge_init_module(void)
406993f7848bSJeff Kirsher {
407093f7848bSJeff Kirsher 	pr_info("Version %s\n", MYRI10GE_VERSION_STR);
407193f7848bSJeff Kirsher 
407293f7848bSJeff Kirsher 	if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
407393f7848bSJeff Kirsher 		pr_err("Illegal rssh hash type %d, defaulting to source port\n",
407493f7848bSJeff Kirsher 		       myri10ge_rss_hash);
407593f7848bSJeff Kirsher 		myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
407693f7848bSJeff Kirsher 	}
407793f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
407893f7848bSJeff Kirsher 	dca_register_notify(&myri10ge_dca_notifier);
407993f7848bSJeff Kirsher #endif
408093f7848bSJeff Kirsher 	if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
408193f7848bSJeff Kirsher 		myri10ge_max_slices = MYRI10GE_MAX_SLICES;
408293f7848bSJeff Kirsher 
408393f7848bSJeff Kirsher 	return pci_register_driver(&myri10ge_driver);
408493f7848bSJeff Kirsher }
408593f7848bSJeff Kirsher 
408693f7848bSJeff Kirsher module_init(myri10ge_init_module);
408793f7848bSJeff Kirsher 
408893f7848bSJeff Kirsher static __exit void myri10ge_cleanup_module(void)
408993f7848bSJeff Kirsher {
409093f7848bSJeff Kirsher #ifdef CONFIG_MYRI10GE_DCA
409193f7848bSJeff Kirsher 	dca_unregister_notify(&myri10ge_dca_notifier);
409293f7848bSJeff Kirsher #endif
409393f7848bSJeff Kirsher 	pci_unregister_driver(&myri10ge_driver);
409493f7848bSJeff Kirsher }
409593f7848bSJeff Kirsher 
409693f7848bSJeff Kirsher module_exit(myri10ge_cleanup_module);
4097