linux/drivers/net/ethernet/myricom/myri10ge/myri10ge.c
<<
>>
Prefs
   1/*************************************************************************
   2 * myri10ge.c: Myricom Myri-10G Ethernet driver.
   3 *
   4 * Copyright (C) 2005 - 2011 Myricom, Inc.
   5 * All rights reserved.
   6 *
   7 * Redistribution and use in source and binary forms, with or without
   8 * modification, are permitted provided that the following conditions
   9 * are met:
  10 * 1. Redistributions of source code must retain the above copyright
  11 *    notice, this list of conditions and the following disclaimer.
  12 * 2. Redistributions in binary form must reproduce the above copyright
  13 *    notice, this list of conditions and the following disclaimer in the
  14 *    documentation and/or other materials provided with the distribution.
  15 * 3. Neither the name of Myricom, Inc. nor the names of its contributors
  16 *    may be used to endorse or promote products derived from this software
  17 *    without specific prior written permission.
  18 *
  19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29 * POSSIBILITY OF SUCH DAMAGE.
  30 *
  31 *
  32 * If the eeprom on your board is not recent enough, you will need to get a
  33 * newer firmware image at:
  34 *   http://www.myri.com/scs/download-Myri10GE.html
  35 *
  36 * Contact Information:
  37 *   <help@myri.com>
  38 *   Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
  39 *************************************************************************/
  40
  41#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  42
  43#include <linux/tcp.h>
  44#include <linux/netdevice.h>
  45#include <linux/skbuff.h>
  46#include <linux/string.h>
  47#include <linux/module.h>
  48#include <linux/pci.h>
  49#include <linux/dma-mapping.h>
  50#include <linux/etherdevice.h>
  51#include <linux/if_ether.h>
  52#include <linux/if_vlan.h>
  53#include <linux/dca.h>
  54#include <linux/ip.h>
  55#include <linux/inet.h>
  56#include <linux/in.h>
  57#include <linux/ethtool.h>
  58#include <linux/firmware.h>
  59#include <linux/delay.h>
  60#include <linux/timer.h>
  61#include <linux/vmalloc.h>
  62#include <linux/crc32.h>
  63#include <linux/moduleparam.h>
  64#include <linux/io.h>
  65#include <linux/log2.h>
  66#include <linux/slab.h>
  67#include <linux/prefetch.h>
  68#include <net/checksum.h>
  69#include <net/ip.h>
  70#include <net/tcp.h>
  71#include <asm/byteorder.h>
  72#include <asm/io.h>
  73#include <asm/processor.h>
  74#ifdef CONFIG_MTRR
  75#include <asm/mtrr.h>
  76#endif
  77#include <net/busy_poll.h>
  78
  79#include "myri10ge_mcp.h"
  80#include "myri10ge_mcp_gen_header.h"
  81
  82#define MYRI10GE_VERSION_STR "1.5.3-1.534"
  83
  84MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
  85MODULE_AUTHOR("Maintainer: help@myri.com");
  86MODULE_VERSION(MYRI10GE_VERSION_STR);
  87MODULE_LICENSE("Dual BSD/GPL");
  88
  89#define MYRI10GE_MAX_ETHER_MTU 9014
  90
  91#define MYRI10GE_ETH_STOPPED 0
  92#define MYRI10GE_ETH_STOPPING 1
  93#define MYRI10GE_ETH_STARTING 2
  94#define MYRI10GE_ETH_RUNNING 3
  95#define MYRI10GE_ETH_OPEN_FAILED 4
  96
  97#define MYRI10GE_EEPROM_STRINGS_SIZE 256
  98#define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
  99
 100#define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
 101#define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
 102
 103#define MYRI10GE_ALLOC_ORDER 0
 104#define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
 105#define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
 106
 107#define MYRI10GE_MAX_SLICES 32
 108
 109struct myri10ge_rx_buffer_state {
 110        struct page *page;
 111        int page_offset;
 112        DEFINE_DMA_UNMAP_ADDR(bus);
 113        DEFINE_DMA_UNMAP_LEN(len);
 114};
 115
 116struct myri10ge_tx_buffer_state {
 117        struct sk_buff *skb;
 118        int last;
 119        DEFINE_DMA_UNMAP_ADDR(bus);
 120        DEFINE_DMA_UNMAP_LEN(len);
 121};
 122
 123struct myri10ge_cmd {
 124        u32 data0;
 125        u32 data1;
 126        u32 data2;
 127};
 128
 129struct myri10ge_rx_buf {
 130        struct mcp_kreq_ether_recv __iomem *lanai;      /* lanai ptr for recv ring */
 131        struct mcp_kreq_ether_recv *shadow;     /* host shadow of recv ring */
 132        struct myri10ge_rx_buffer_state *info;
 133        struct page *page;
 134        dma_addr_t bus;
 135        int page_offset;
 136        int cnt;
 137        int fill_cnt;
 138        int alloc_fail;
 139        int mask;               /* number of rx slots -1 */
 140        int watchdog_needed;
 141};
 142
 143struct myri10ge_tx_buf {
 144        struct mcp_kreq_ether_send __iomem *lanai;      /* lanai ptr for sendq */
 145        __be32 __iomem *send_go;        /* "go" doorbell ptr */
 146        __be32 __iomem *send_stop;      /* "stop" doorbell ptr */
 147        struct mcp_kreq_ether_send *req_list;   /* host shadow of sendq */
 148        char *req_bytes;
 149        struct myri10ge_tx_buffer_state *info;
 150        int mask;               /* number of transmit slots -1  */
 151        int req ____cacheline_aligned;  /* transmit slots submitted     */
 152        int pkt_start;          /* packets started */
 153        int stop_queue;
 154        int linearized;
 155        int done ____cacheline_aligned; /* transmit slots completed     */
 156        int pkt_done;           /* packets completed */
 157        int wake_queue;
 158        int queue_active;
 159};
 160
 161struct myri10ge_rx_done {
 162        struct mcp_slot *entry;
 163        dma_addr_t bus;
 164        int cnt;
 165        int idx;
 166};
 167
 168struct myri10ge_slice_netstats {
 169        unsigned long rx_packets;
 170        unsigned long tx_packets;
 171        unsigned long rx_bytes;
 172        unsigned long tx_bytes;
 173        unsigned long rx_dropped;
 174        unsigned long tx_dropped;
 175};
 176
 177struct myri10ge_slice_state {
 178        struct myri10ge_tx_buf tx;      /* transmit ring        */
 179        struct myri10ge_rx_buf rx_small;
 180        struct myri10ge_rx_buf rx_big;
 181        struct myri10ge_rx_done rx_done;
 182        struct net_device *dev;
 183        struct napi_struct napi;
 184        struct myri10ge_priv *mgp;
 185        struct myri10ge_slice_netstats stats;
 186        __be32 __iomem *irq_claim;
 187        struct mcp_irq_data *fw_stats;
 188        dma_addr_t fw_stats_bus;
 189        int watchdog_tx_done;
 190        int watchdog_tx_req;
 191        int watchdog_rx_done;
 192        int stuck;
 193#ifdef CONFIG_MYRI10GE_DCA
 194        int cached_dca_tag;
 195        int cpu;
 196        __be32 __iomem *dca_tag;
 197#endif
 198#ifdef CONFIG_NET_RX_BUSY_POLL
 199        unsigned int state;
 200#define SLICE_STATE_IDLE        0
 201#define SLICE_STATE_NAPI        1       /* NAPI owns this slice */
 202#define SLICE_STATE_POLL        2       /* poll owns this slice */
 203#define SLICE_LOCKED (SLICE_STATE_NAPI | SLICE_STATE_POLL)
 204#define SLICE_STATE_NAPI_YIELD  4       /* NAPI yielded this slice */
 205#define SLICE_STATE_POLL_YIELD  8       /* poll yielded this slice */
 206#define SLICE_USER_PEND (SLICE_STATE_POLL | SLICE_STATE_POLL_YIELD)
 207        spinlock_t lock;
 208        unsigned long lock_napi_yield;
 209        unsigned long lock_poll_yield;
 210        unsigned long busy_poll_miss;
 211        unsigned long busy_poll_cnt;
 212#endif /* CONFIG_NET_RX_BUSY_POLL */
 213        char irq_desc[32];
 214};
 215
 216struct myri10ge_priv {
 217        struct myri10ge_slice_state *ss;
 218        int tx_boundary;        /* boundary transmits cannot cross */
 219        int num_slices;
 220        int running;            /* running?             */
 221        int small_bytes;
 222        int big_bytes;
 223        int max_intr_slots;
 224        struct net_device *dev;
 225        u8 __iomem *sram;
 226        int sram_size;
 227        unsigned long board_span;
 228        unsigned long iomem_base;
 229        __be32 __iomem *irq_deassert;
 230        char *mac_addr_string;
 231        struct mcp_cmd_response *cmd;
 232        dma_addr_t cmd_bus;
 233        struct pci_dev *pdev;
 234        int msi_enabled;
 235        int msix_enabled;
 236        struct msix_entry *msix_vectors;
 237#ifdef CONFIG_MYRI10GE_DCA
 238        int dca_enabled;
 239        int relaxed_order;
 240#endif
 241        u32 link_state;
 242        unsigned int rdma_tags_available;
 243        int intr_coal_delay;
 244        __be32 __iomem *intr_coal_delay_ptr;
 245        int mtrr;
 246        int wc_enabled;
 247        int down_cnt;
 248        wait_queue_head_t down_wq;
 249        struct work_struct watchdog_work;
 250        struct timer_list watchdog_timer;
 251        int watchdog_resets;
 252        int watchdog_pause;
 253        int pause;
 254        bool fw_name_allocated;
 255        char *fw_name;
 256        char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
 257        char *product_code_string;
 258        char fw_version[128];
 259        int fw_ver_major;
 260        int fw_ver_minor;
 261        int fw_ver_tiny;
 262        int adopted_rx_filter_bug;
 263        u8 mac_addr[6];         /* eeprom mac address */
 264        unsigned long serial_number;
 265        int vendor_specific_offset;
 266        int fw_multicast_support;
 267        u32 features;
 268        u32 max_tso6;
 269        u32 read_dma;
 270        u32 write_dma;
 271        u32 read_write_dma;
 272        u32 link_changes;
 273        u32 msg_enable;
 274        unsigned int board_number;
 275        int rebooted;
 276};
 277
 278static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
 279static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
 280static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
 281static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
 282MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
 283MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
 284MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
 285MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
 286
 287/* Careful: must be accessed under kparam_block_sysfs_write */
 288static char *myri10ge_fw_name = NULL;
 289module_param(myri10ge_fw_name, charp, S_IRUGO | S_IWUSR);
 290MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
 291
 292#define MYRI10GE_MAX_BOARDS 8
 293static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
 294    {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
 295module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
 296                         0444);
 297MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image names per board");
 298
 299static int myri10ge_ecrc_enable = 1;
 300module_param(myri10ge_ecrc_enable, int, S_IRUGO);
 301MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
 302
 303static int myri10ge_small_bytes = -1;   /* -1 == auto */
 304module_param(myri10ge_small_bytes, int, S_IRUGO | S_IWUSR);
 305MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
 306
 307static int myri10ge_msi = 1;    /* enable msi by default */
 308module_param(myri10ge_msi, int, S_IRUGO | S_IWUSR);
 309MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
 310
 311static int myri10ge_intr_coal_delay = 75;
 312module_param(myri10ge_intr_coal_delay, int, S_IRUGO);
 313MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
 314
 315static int myri10ge_flow_control = 1;
 316module_param(myri10ge_flow_control, int, S_IRUGO);
 317MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
 318
 319static int myri10ge_deassert_wait = 1;
 320module_param(myri10ge_deassert_wait, int, S_IRUGO | S_IWUSR);
 321MODULE_PARM_DESC(myri10ge_deassert_wait,
 322                 "Wait when deasserting legacy interrupts");
 323
 324static int myri10ge_force_firmware = 0;
 325module_param(myri10ge_force_firmware, int, S_IRUGO);
 326MODULE_PARM_DESC(myri10ge_force_firmware,
 327                 "Force firmware to assume aligned completions");
 328
 329static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
 330module_param(myri10ge_initial_mtu, int, S_IRUGO);
 331MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
 332
 333static int myri10ge_napi_weight = 64;
 334module_param(myri10ge_napi_weight, int, S_IRUGO);
 335MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
 336
 337static int myri10ge_watchdog_timeout = 1;
 338module_param(myri10ge_watchdog_timeout, int, S_IRUGO);
 339MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
 340
 341static int myri10ge_max_irq_loops = 1048576;
 342module_param(myri10ge_max_irq_loops, int, S_IRUGO);
 343MODULE_PARM_DESC(myri10ge_max_irq_loops,
 344                 "Set stuck legacy IRQ detection threshold");
 345
 346#define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
 347
 348static int myri10ge_debug = -1; /* defaults above */
 349module_param(myri10ge_debug, int, 0);
 350MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
 351
 352static int myri10ge_fill_thresh = 256;
 353module_param(myri10ge_fill_thresh, int, S_IRUGO | S_IWUSR);
 354MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
 355
 356static int myri10ge_reset_recover = 1;
 357
 358static int myri10ge_max_slices = 1;
 359module_param(myri10ge_max_slices, int, S_IRUGO);
 360MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
 361
 362static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
 363module_param(myri10ge_rss_hash, int, S_IRUGO);
 364MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
 365
 366static int myri10ge_dca = 1;
 367module_param(myri10ge_dca, int, S_IRUGO);
 368MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
 369
 370#define MYRI10GE_FW_OFFSET 1024*1024
 371#define MYRI10GE_HIGHPART_TO_U32(X) \
 372(sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
 373#define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
 374
 375#define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
 376
 377static void myri10ge_set_multicast_list(struct net_device *dev);
 378static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
 379                                         struct net_device *dev);
 380
 381static inline void put_be32(__be32 val, __be32 __iomem * p)
 382{
 383        __raw_writel((__force __u32) val, (__force void __iomem *)p);
 384}
 385
 386static void myri10ge_get_stats(struct net_device *dev,
 387                               struct rtnl_link_stats64 *stats);
 388
 389static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
 390{
 391        if (mgp->fw_name_allocated)
 392                kfree(mgp->fw_name);
 393        mgp->fw_name = name;
 394        mgp->fw_name_allocated = allocated;
 395}
 396
 397static int
 398myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
 399                  struct myri10ge_cmd *data, int atomic)
 400{
 401        struct mcp_cmd *buf;
 402        char buf_bytes[sizeof(*buf) + 8];
 403        struct mcp_cmd_response *response = mgp->cmd;
 404        char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
 405        u32 dma_low, dma_high, result, value;
 406        int sleep_total = 0;
 407
 408        /* ensure buf is aligned to 8 bytes */
 409        buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
 410
 411        buf->data0 = htonl(data->data0);
 412        buf->data1 = htonl(data->data1);
 413        buf->data2 = htonl(data->data2);
 414        buf->cmd = htonl(cmd);
 415        dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
 416        dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
 417
 418        buf->response_addr.low = htonl(dma_low);
 419        buf->response_addr.high = htonl(dma_high);
 420        response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
 421        mb();
 422        myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
 423
 424        /* wait up to 15ms. Longest command is the DMA benchmark,
 425         * which is capped at 5ms, but runs from a timeout handler
 426         * that runs every 7.8ms. So a 15ms timeout leaves us with
 427         * a 2.2ms margin
 428         */
 429        if (atomic) {
 430                /* if atomic is set, do not sleep,
 431                 * and try to get the completion quickly
 432                 * (1ms will be enough for those commands) */
 433                for (sleep_total = 0;
 434                     sleep_total < 1000 &&
 435                     response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
 436                     sleep_total += 10) {
 437                        udelay(10);
 438                        mb();
 439                }
 440        } else {
 441                /* use msleep for most command */
 442                for (sleep_total = 0;
 443                     sleep_total < 15 &&
 444                     response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
 445                     sleep_total++)
 446                        msleep(1);
 447        }
 448
 449        result = ntohl(response->result);
 450        value = ntohl(response->data);
 451        if (result != MYRI10GE_NO_RESPONSE_RESULT) {
 452                if (result == 0) {
 453                        data->data0 = value;
 454                        return 0;
 455                } else if (result == MXGEFW_CMD_UNKNOWN) {
 456                        return -ENOSYS;
 457                } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
 458                        return -E2BIG;
 459                } else if (result == MXGEFW_CMD_ERROR_RANGE &&
 460                           cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
 461                           (data->
 462                            data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
 463                           0) {
 464                        return -ERANGE;
 465                } else {
 466                        dev_err(&mgp->pdev->dev,
 467                                "command %d failed, result = %d\n",
 468                                cmd, result);
 469                        return -ENXIO;
 470                }
 471        }
 472
 473        dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
 474                cmd, result);
 475        return -EAGAIN;
 476}
 477
 478/*
 479 * The eeprom strings on the lanaiX have the format
 480 * SN=x\0
 481 * MAC=x:x:x:x:x:x\0
 482 * PT:ddd mmm xx xx:xx:xx xx\0
 483 * PV:ddd mmm xx xx:xx:xx xx\0
 484 */
 485static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
 486{
 487        char *ptr, *limit;
 488        int i;
 489
 490        ptr = mgp->eeprom_strings;
 491        limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
 492
 493        while (*ptr != '\0' && ptr < limit) {
 494                if (memcmp(ptr, "MAC=", 4) == 0) {
 495                        ptr += 4;
 496                        mgp->mac_addr_string = ptr;
 497                        for (i = 0; i < 6; i++) {
 498                                if ((ptr + 2) > limit)
 499                                        goto abort;
 500                                mgp->mac_addr[i] =
 501                                    simple_strtoul(ptr, &ptr, 16);
 502                                ptr += 1;
 503                        }
 504                }
 505                if (memcmp(ptr, "PC=", 3) == 0) {
 506                        ptr += 3;
 507                        mgp->product_code_string = ptr;
 508                }
 509                if (memcmp((const void *)ptr, "SN=", 3) == 0) {
 510                        ptr += 3;
 511                        mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
 512                }
 513                while (ptr < limit && *ptr++) ;
 514        }
 515
 516        return 0;
 517
 518abort:
 519        dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
 520        return -ENXIO;
 521}
 522
 523/*
 524 * Enable or disable periodic RDMAs from the host to make certain
 525 * chipsets resend dropped PCIe messages
 526 */
 527
 528static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
 529{
 530        char __iomem *submit;
 531        __be32 buf[16] __attribute__ ((__aligned__(8)));
 532        u32 dma_low, dma_high;
 533        int i;
 534
 535        /* clear confirmation addr */
 536        mgp->cmd->data = 0;
 537        mb();
 538
 539        /* send a rdma command to the PCIe engine, and wait for the
 540         * response in the confirmation address.  The firmware should
 541         * write a -1 there to indicate it is alive and well
 542         */
 543        dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
 544        dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
 545
 546        buf[0] = htonl(dma_high);       /* confirm addr MSW */
 547        buf[1] = htonl(dma_low);        /* confirm addr LSW */
 548        buf[2] = MYRI10GE_NO_CONFIRM_DATA;      /* confirm data */
 549        buf[3] = htonl(dma_high);       /* dummy addr MSW */
 550        buf[4] = htonl(dma_low);        /* dummy addr LSW */
 551        buf[5] = htonl(enable); /* enable? */
 552
 553        submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
 554
 555        myri10ge_pio_copy(submit, &buf, sizeof(buf));
 556        for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
 557                msleep(1);
 558        if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
 559                dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
 560                        (enable ? "enable" : "disable"));
 561}
 562
 563static int
 564myri10ge_validate_firmware(struct myri10ge_priv *mgp,
 565                           struct mcp_gen_header *hdr)
 566{
 567        struct device *dev = &mgp->pdev->dev;
 568
 569        /* check firmware type */
 570        if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
 571                dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
 572                return -EINVAL;
 573        }
 574
 575        /* save firmware version for ethtool */
 576        strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
 577
 578        sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
 579               &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
 580
 581        if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
 582              mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
 583                dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
 584                dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
 585                        MXGEFW_VERSION_MINOR);
 586                return -EINVAL;
 587        }
 588        return 0;
 589}
 590
 591static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
 592{
 593        unsigned crc, reread_crc;
 594        const struct firmware *fw;
 595        struct device *dev = &mgp->pdev->dev;
 596        unsigned char *fw_readback;
 597        struct mcp_gen_header *hdr;
 598        size_t hdr_offset;
 599        int status;
 600        unsigned i;
 601
 602        if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
 603                dev_err(dev, "Unable to load %s firmware image via hotplug\n",
 604                        mgp->fw_name);
 605                status = -EINVAL;
 606                goto abort_with_nothing;
 607        }
 608
 609        /* check size */
 610
 611        if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
 612            fw->size < MCP_HEADER_PTR_OFFSET + 4) {
 613                dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
 614                status = -EINVAL;
 615                goto abort_with_fw;
 616        }
 617
 618        /* check id */
 619        hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
 620        if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
 621                dev_err(dev, "Bad firmware file\n");
 622                status = -EINVAL;
 623                goto abort_with_fw;
 624        }
 625        hdr = (void *)(fw->data + hdr_offset);
 626
 627        status = myri10ge_validate_firmware(mgp, hdr);
 628        if (status != 0)
 629                goto abort_with_fw;
 630
 631        crc = crc32(~0, fw->data, fw->size);
 632        for (i = 0; i < fw->size; i += 256) {
 633                myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
 634                                  fw->data + i,
 635                                  min(256U, (unsigned)(fw->size - i)));
 636                mb();
 637                readb(mgp->sram);
 638        }
 639        fw_readback = vmalloc(fw->size);
 640        if (!fw_readback) {
 641                status = -ENOMEM;
 642                goto abort_with_fw;
 643        }
 644        /* corruption checking is good for parity recovery and buggy chipset */
 645        memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
 646        reread_crc = crc32(~0, fw_readback, fw->size);
 647        vfree(fw_readback);
 648        if (crc != reread_crc) {
 649                dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
 650                        (unsigned)fw->size, reread_crc, crc);
 651                status = -EIO;
 652                goto abort_with_fw;
 653        }
 654        *size = (u32) fw->size;
 655
 656abort_with_fw:
 657        release_firmware(fw);
 658
 659abort_with_nothing:
 660        return status;
 661}
 662
 663static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
 664{
 665        struct mcp_gen_header *hdr;
 666        struct device *dev = &mgp->pdev->dev;
 667        const size_t bytes = sizeof(struct mcp_gen_header);
 668        size_t hdr_offset;
 669        int status;
 670
 671        /* find running firmware header */
 672        hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
 673
 674        if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
 675                dev_err(dev, "Running firmware has bad header offset (%d)\n",
 676                        (int)hdr_offset);
 677                return -EIO;
 678        }
 679
 680        /* copy header of running firmware from SRAM to host memory to
 681         * validate firmware */
 682        hdr = kmalloc(bytes, GFP_KERNEL);
 683        if (hdr == NULL)
 684                return -ENOMEM;
 685
 686        memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
 687        status = myri10ge_validate_firmware(mgp, hdr);
 688        kfree(hdr);
 689
 690        /* check to see if adopted firmware has bug where adopting
 691         * it will cause broadcasts to be filtered unless the NIC
 692         * is kept in ALLMULTI mode */
 693        if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
 694            mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
 695                mgp->adopted_rx_filter_bug = 1;
 696                dev_warn(dev, "Adopting fw %d.%d.%d: "
 697                         "working around rx filter bug\n",
 698                         mgp->fw_ver_major, mgp->fw_ver_minor,
 699                         mgp->fw_ver_tiny);
 700        }
 701        return status;
 702}
 703
 704static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
 705{
 706        struct myri10ge_cmd cmd;
 707        int status;
 708
 709        /* probe for IPv6 TSO support */
 710        mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
 711        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
 712                                   &cmd, 0);
 713        if (status == 0) {
 714                mgp->max_tso6 = cmd.data0;
 715                mgp->features |= NETIF_F_TSO6;
 716        }
 717
 718        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
 719        if (status != 0) {
 720                dev_err(&mgp->pdev->dev,
 721                        "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
 722                return -ENXIO;
 723        }
 724
 725        mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
 726
 727        return 0;
 728}
 729
 730static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
 731{
 732        char __iomem *submit;
 733        __be32 buf[16] __attribute__ ((__aligned__(8)));
 734        u32 dma_low, dma_high, size;
 735        int status, i;
 736
 737        size = 0;
 738        status = myri10ge_load_hotplug_firmware(mgp, &size);
 739        if (status) {
 740                if (!adopt)
 741                        return status;
 742                dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
 743
 744                /* Do not attempt to adopt firmware if there
 745                 * was a bad crc */
 746                if (status == -EIO)
 747                        return status;
 748
 749                status = myri10ge_adopt_running_firmware(mgp);
 750                if (status != 0) {
 751                        dev_err(&mgp->pdev->dev,
 752                                "failed to adopt running firmware\n");
 753                        return status;
 754                }
 755                dev_info(&mgp->pdev->dev,
 756                         "Successfully adopted running firmware\n");
 757                if (mgp->tx_boundary == 4096) {
 758                        dev_warn(&mgp->pdev->dev,
 759                                 "Using firmware currently running on NIC"
 760                                 ".  For optimal\n");
 761                        dev_warn(&mgp->pdev->dev,
 762                                 "performance consider loading optimized "
 763                                 "firmware\n");
 764                        dev_warn(&mgp->pdev->dev, "via hotplug\n");
 765                }
 766
 767                set_fw_name(mgp, "adopted", false);
 768                mgp->tx_boundary = 2048;
 769                myri10ge_dummy_rdma(mgp, 1);
 770                status = myri10ge_get_firmware_capabilities(mgp);
 771                return status;
 772        }
 773
 774        /* clear confirmation addr */
 775        mgp->cmd->data = 0;
 776        mb();
 777
 778        /* send a reload command to the bootstrap MCP, and wait for the
 779         *  response in the confirmation address.  The firmware should
 780         * write a -1 there to indicate it is alive and well
 781         */
 782        dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
 783        dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
 784
 785        buf[0] = htonl(dma_high);       /* confirm addr MSW */
 786        buf[1] = htonl(dma_low);        /* confirm addr LSW */
 787        buf[2] = MYRI10GE_NO_CONFIRM_DATA;      /* confirm data */
 788
 789        /* FIX: All newest firmware should un-protect the bottom of
 790         * the sram before handoff. However, the very first interfaces
 791         * do not. Therefore the handoff copy must skip the first 8 bytes
 792         */
 793        buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
 794        buf[4] = htonl(size - 8);       /* length of code */
 795        buf[5] = htonl(8);      /* where to copy to */
 796        buf[6] = htonl(0);      /* where to jump to */
 797
 798        submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
 799
 800        myri10ge_pio_copy(submit, &buf, sizeof(buf));
 801        mb();
 802        msleep(1);
 803        mb();
 804        i = 0;
 805        while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
 806                msleep(1 << i);
 807                i++;
 808        }
 809        if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
 810                dev_err(&mgp->pdev->dev, "handoff failed\n");
 811                return -ENXIO;
 812        }
 813        myri10ge_dummy_rdma(mgp, 1);
 814        status = myri10ge_get_firmware_capabilities(mgp);
 815
 816        return status;
 817}
 818
 819static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
 820{
 821        struct myri10ge_cmd cmd;
 822        int status;
 823
 824        cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
 825                     | (addr[2] << 8) | addr[3]);
 826
 827        cmd.data1 = ((addr[4] << 8) | (addr[5]));
 828
 829        status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
 830        return status;
 831}
 832
 833static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
 834{
 835        struct myri10ge_cmd cmd;
 836        int status, ctl;
 837
 838        ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
 839        status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
 840
 841        if (status) {
 842                netdev_err(mgp->dev, "Failed to set flow control mode\n");
 843                return status;
 844        }
 845        mgp->pause = pause;
 846        return 0;
 847}
 848
 849static void
 850myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
 851{
 852        struct myri10ge_cmd cmd;
 853        int status, ctl;
 854
 855        ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
 856        status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
 857        if (status)
 858                netdev_err(mgp->dev, "Failed to set promisc mode\n");
 859}
 860
 861static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
 862{
 863        struct myri10ge_cmd cmd;
 864        int status;
 865        u32 len;
 866        struct page *dmatest_page;
 867        dma_addr_t dmatest_bus;
 868        char *test = " ";
 869
 870        dmatest_page = alloc_page(GFP_KERNEL);
 871        if (!dmatest_page)
 872                return -ENOMEM;
 873        dmatest_bus = pci_map_page(mgp->pdev, dmatest_page, 0, PAGE_SIZE,
 874                                   DMA_BIDIRECTIONAL);
 875        if (unlikely(pci_dma_mapping_error(mgp->pdev, dmatest_bus))) {
 876                __free_page(dmatest_page);
 877                return -ENOMEM;
 878        }
 879
 880        /* Run a small DMA test.
 881         * The magic multipliers to the length tell the firmware
 882         * to do DMA read, write, or read+write tests.  The
 883         * results are returned in cmd.data0.  The upper 16
 884         * bits or the return is the number of transfers completed.
 885         * The lower 16 bits is the time in 0.5us ticks that the
 886         * transfers took to complete.
 887         */
 888
 889        len = mgp->tx_boundary;
 890
 891        cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
 892        cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
 893        cmd.data2 = len * 0x10000;
 894        status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
 895        if (status != 0) {
 896                test = "read";
 897                goto abort;
 898        }
 899        mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
 900        cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
 901        cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
 902        cmd.data2 = len * 0x1;
 903        status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
 904        if (status != 0) {
 905                test = "write";
 906                goto abort;
 907        }
 908        mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
 909
 910        cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
 911        cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
 912        cmd.data2 = len * 0x10001;
 913        status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
 914        if (status != 0) {
 915                test = "read/write";
 916                goto abort;
 917        }
 918        mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
 919            (cmd.data0 & 0xffff);
 920
 921abort:
 922        pci_unmap_page(mgp->pdev, dmatest_bus, PAGE_SIZE, DMA_BIDIRECTIONAL);
 923        put_page(dmatest_page);
 924
 925        if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
 926                dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
 927                         test, status);
 928
 929        return status;
 930}
 931
 932#ifdef CONFIG_NET_RX_BUSY_POLL
 933static inline void myri10ge_ss_init_lock(struct myri10ge_slice_state *ss)
 934{
 935        spin_lock_init(&ss->lock);
 936        ss->state = SLICE_STATE_IDLE;
 937}
 938
 939static inline bool myri10ge_ss_lock_napi(struct myri10ge_slice_state *ss)
 940{
 941        int rc = true;
 942        spin_lock(&ss->lock);
 943        if ((ss->state & SLICE_LOCKED)) {
 944                WARN_ON((ss->state & SLICE_STATE_NAPI));
 945                ss->state |= SLICE_STATE_NAPI_YIELD;
 946                rc = false;
 947                ss->lock_napi_yield++;
 948        } else
 949                ss->state = SLICE_STATE_NAPI;
 950        spin_unlock(&ss->lock);
 951        return rc;
 952}
 953
 954static inline void myri10ge_ss_unlock_napi(struct myri10ge_slice_state *ss)
 955{
 956        spin_lock(&ss->lock);
 957        WARN_ON((ss->state & (SLICE_STATE_POLL | SLICE_STATE_NAPI_YIELD)));
 958        ss->state = SLICE_STATE_IDLE;
 959        spin_unlock(&ss->lock);
 960}
 961
 962static inline bool myri10ge_ss_lock_poll(struct myri10ge_slice_state *ss)
 963{
 964        int rc = true;
 965        spin_lock_bh(&ss->lock);
 966        if ((ss->state & SLICE_LOCKED)) {
 967                ss->state |= SLICE_STATE_POLL_YIELD;
 968                rc = false;
 969                ss->lock_poll_yield++;
 970        } else
 971                ss->state |= SLICE_STATE_POLL;
 972        spin_unlock_bh(&ss->lock);
 973        return rc;
 974}
 975
 976static inline void myri10ge_ss_unlock_poll(struct myri10ge_slice_state *ss)
 977{
 978        spin_lock_bh(&ss->lock);
 979        WARN_ON((ss->state & SLICE_STATE_NAPI));
 980        ss->state = SLICE_STATE_IDLE;
 981        spin_unlock_bh(&ss->lock);
 982}
 983
 984static inline bool myri10ge_ss_busy_polling(struct myri10ge_slice_state *ss)
 985{
 986        WARN_ON(!(ss->state & SLICE_LOCKED));
 987        return (ss->state & SLICE_USER_PEND);
 988}
 989#else /* CONFIG_NET_RX_BUSY_POLL */
 990static inline void myri10ge_ss_init_lock(struct myri10ge_slice_state *ss)
 991{
 992}
 993
 994static inline bool myri10ge_ss_lock_napi(struct myri10ge_slice_state *ss)
 995{
 996        return false;
 997}
 998
 999static inline void myri10ge_ss_unlock_napi(struct myri10ge_slice_state *ss)
1000{
1001}
1002
1003static inline bool myri10ge_ss_lock_poll(struct myri10ge_slice_state *ss)
1004{
1005        return false;
1006}
1007
1008static inline void myri10ge_ss_unlock_poll(struct myri10ge_slice_state *ss)
1009{
1010}
1011
1012static inline bool myri10ge_ss_busy_polling(struct myri10ge_slice_state *ss)
1013{
1014        return false;
1015}
1016#endif
1017
1018static int myri10ge_reset(struct myri10ge_priv *mgp)
1019{
1020        struct myri10ge_cmd cmd;
1021        struct myri10ge_slice_state *ss;
1022        int i, status;
1023        size_t bytes;
1024#ifdef CONFIG_MYRI10GE_DCA
1025        unsigned long dca_tag_off;
1026#endif
1027
1028        /* try to send a reset command to the card to see if it
1029         * is alive */
1030        memset(&cmd, 0, sizeof(cmd));
1031        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
1032        if (status != 0) {
1033                dev_err(&mgp->pdev->dev, "failed reset\n");
1034                return -ENXIO;
1035        }
1036
1037        (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
1038        /*
1039         * Use non-ndis mcp_slot (eg, 4 bytes total,
1040         * no toeplitz hash value returned.  Older firmware will
1041         * not understand this command, but will use the correct
1042         * sized mcp_slot, so we ignore error returns
1043         */
1044        cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
1045        (void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
1046
1047        /* Now exchange information about interrupts  */
1048
1049        bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
1050        cmd.data0 = (u32) bytes;
1051        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
1052
1053        /*
1054         * Even though we already know how many slices are supported
1055         * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
1056         * has magic side effects, and must be called after a reset.
1057         * It must be called prior to calling any RSS related cmds,
1058         * including assigning an interrupt queue for anything but
1059         * slice 0.  It must also be called *after*
1060         * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
1061         * the firmware to compute offsets.
1062         */
1063
1064        if (mgp->num_slices > 1) {
1065
1066                /* ask the maximum number of slices it supports */
1067                status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
1068                                           &cmd, 0);
1069                if (status != 0) {
1070                        dev_err(&mgp->pdev->dev,
1071                                "failed to get number of slices\n");
1072                }
1073
1074                /*
1075                 * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
1076                 * to setting up the interrupt queue DMA
1077                 */
1078
1079                cmd.data0 = mgp->num_slices;
1080                cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
1081                if (mgp->dev->real_num_tx_queues > 1)
1082                        cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
1083                status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
1084                                           &cmd, 0);
1085
1086                /* Firmware older than 1.4.32 only supports multiple
1087                 * RX queues, so if we get an error, first retry using a
1088                 * single TX queue before giving up */
1089                if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
1090                        netif_set_real_num_tx_queues(mgp->dev, 1);
1091                        cmd.data0 = mgp->num_slices;
1092                        cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
1093                        status = myri10ge_send_cmd(mgp,
1094                                                   MXGEFW_CMD_ENABLE_RSS_QUEUES,
1095                                                   &cmd, 0);
1096                }
1097
1098                if (status != 0) {
1099                        dev_err(&mgp->pdev->dev,
1100                                "failed to set number of slices\n");
1101
1102                        return status;
1103                }
1104        }
1105        for (i = 0; i < mgp->num_slices; i++) {
1106                ss = &mgp->ss[i];
1107                cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
1108                cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
1109                cmd.data2 = i;
1110                status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
1111                                            &cmd, 0);
1112        }
1113
1114        status |=
1115            myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
1116        for (i = 0; i < mgp->num_slices; i++) {
1117                ss = &mgp->ss[i];
1118                ss->irq_claim =
1119                    (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
1120        }
1121        status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
1122                                    &cmd, 0);
1123        mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
1124
1125        status |= myri10ge_send_cmd
1126            (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
1127        mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
1128        if (status != 0) {
1129                dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
1130                return status;
1131        }
1132        put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1133
1134#ifdef CONFIG_MYRI10GE_DCA
1135        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
1136        dca_tag_off = cmd.data0;
1137        for (i = 0; i < mgp->num_slices; i++) {
1138                ss = &mgp->ss[i];
1139                if (status == 0) {
1140                        ss->dca_tag = (__iomem __be32 *)
1141                            (mgp->sram + dca_tag_off + 4 * i);
1142                } else {
1143                        ss->dca_tag = NULL;
1144                }
1145        }
1146#endif                          /* CONFIG_MYRI10GE_DCA */
1147
1148        /* reset mcp/driver shared state back to 0 */
1149
1150        mgp->link_changes = 0;
1151        for (i = 0; i < mgp->num_slices; i++) {
1152                ss = &mgp->ss[i];
1153
1154                memset(ss->rx_done.entry, 0, bytes);
1155                ss->tx.req = 0;
1156                ss->tx.done = 0;
1157                ss->tx.pkt_start = 0;
1158                ss->tx.pkt_done = 0;
1159                ss->rx_big.cnt = 0;
1160                ss->rx_small.cnt = 0;
1161                ss->rx_done.idx = 0;
1162                ss->rx_done.cnt = 0;
1163                ss->tx.wake_queue = 0;
1164                ss->tx.stop_queue = 0;
1165        }
1166
1167        status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
1168        myri10ge_change_pause(mgp, mgp->pause);
1169        myri10ge_set_multicast_list(mgp->dev);
1170        return status;
1171}
1172
1173#ifdef CONFIG_MYRI10GE_DCA
1174static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
1175{
1176        int ret;
1177        u16 ctl;
1178
1179        pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &ctl);
1180
1181        ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
1182        if (ret != on) {
1183                ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
1184                ctl |= (on << 4);
1185                pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, ctl);
1186        }
1187        return ret;
1188}
1189
1190static void
1191myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
1192{
1193        ss->cached_dca_tag = tag;
1194        put_be32(htonl(tag), ss->dca_tag);
1195}
1196
1197static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
1198{
1199        int cpu = get_cpu();
1200        int tag;
1201
1202        if (cpu != ss->cpu) {
1203                tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
1204                if (ss->cached_dca_tag != tag)
1205                        myri10ge_write_dca(ss, cpu, tag);
1206                ss->cpu = cpu;
1207        }
1208        put_cpu();
1209}
1210
1211static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
1212{
1213        int err, i;
1214        struct pci_dev *pdev = mgp->pdev;
1215
1216        if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
1217                return;
1218        if (!myri10ge_dca) {
1219                dev_err(&pdev->dev, "dca disabled by administrator\n");
1220                return;
1221        }
1222        err = dca_add_requester(&pdev->dev);
1223        if (err) {
1224                if (err != -ENODEV)
1225                        dev_err(&pdev->dev,
1226                                "dca_add_requester() failed, err=%d\n", err);
1227                return;
1228        }
1229        mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
1230        mgp->dca_enabled = 1;
1231        for (i = 0; i < mgp->num_slices; i++) {
1232                mgp->ss[i].cpu = -1;
1233                mgp->ss[i].cached_dca_tag = -1;
1234                myri10ge_update_dca(&mgp->ss[i]);
1235        }
1236}
1237
1238static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
1239{
1240        struct pci_dev *pdev = mgp->pdev;
1241
1242        if (!mgp->dca_enabled)
1243                return;
1244        mgp->dca_enabled = 0;
1245        if (mgp->relaxed_order)
1246                myri10ge_toggle_relaxed(pdev, 1);
1247        dca_remove_requester(&pdev->dev);
1248}
1249
1250static int myri10ge_notify_dca_device(struct device *dev, void *data)
1251{
1252        struct myri10ge_priv *mgp;
1253        unsigned long event;
1254
1255        mgp = dev_get_drvdata(dev);
1256        event = *(unsigned long *)data;
1257
1258        if (event == DCA_PROVIDER_ADD)
1259                myri10ge_setup_dca(mgp);
1260        else if (event == DCA_PROVIDER_REMOVE)
1261                myri10ge_teardown_dca(mgp);
1262        return 0;
1263}
1264#endif                          /* CONFIG_MYRI10GE_DCA */
1265
1266static inline void
1267myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
1268                    struct mcp_kreq_ether_recv *src)
1269{
1270        __be32 low;
1271
1272        low = src->addr_low;
1273        src->addr_low = htonl(DMA_BIT_MASK(32));
1274        myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
1275        mb();
1276        myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
1277        mb();
1278        src->addr_low = low;
1279        put_be32(low, &dst->addr_low);
1280        mb();
1281}
1282
1283static inline void myri10ge_vlan_ip_csum(struct sk_buff *skb, __wsum hw_csum)
1284{
1285        struct vlan_hdr *vh = (struct vlan_hdr *)(skb->data);
1286
1287        if ((skb->protocol == htons(ETH_P_8021Q)) &&
1288            (vh->h_vlan_encapsulated_proto == htons(ETH_P_IP) ||
1289             vh->h_vlan_encapsulated_proto == htons(ETH_P_IPV6))) {
1290                skb->csum = hw_csum;
1291                skb->ip_summed = CHECKSUM_COMPLETE;
1292        }
1293}
1294
1295static void
1296myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
1297                        int bytes, int watchdog)
1298{
1299        struct page *page;
1300        dma_addr_t bus;
1301        int idx;
1302#if MYRI10GE_ALLOC_SIZE > 4096
1303        int end_offset;
1304#endif
1305
1306        if (unlikely(rx->watchdog_needed && !watchdog))
1307                return;
1308
1309        /* try to refill entire ring */
1310        while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
1311                idx = rx->fill_cnt & rx->mask;
1312                if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
1313                        /* we can use part of previous page */
1314                        get_page(rx->page);
1315                } else {
1316                        /* we need a new page */
1317                        page =
1318                            alloc_pages(GFP_ATOMIC | __GFP_COMP,
1319                                        MYRI10GE_ALLOC_ORDER);
1320                        if (unlikely(page == NULL)) {
1321                                if (rx->fill_cnt - rx->cnt < 16)
1322                                        rx->watchdog_needed = 1;
1323                                return;
1324                        }
1325
1326                        bus = pci_map_page(mgp->pdev, page, 0,
1327                                           MYRI10GE_ALLOC_SIZE,
1328                                           PCI_DMA_FROMDEVICE);
1329                        if (unlikely(pci_dma_mapping_error(mgp->pdev, bus))) {
1330                                __free_pages(page, MYRI10GE_ALLOC_ORDER);
1331                                if (rx->fill_cnt - rx->cnt < 16)
1332                                        rx->watchdog_needed = 1;
1333                                return;
1334                        }
1335
1336                        rx->page = page;
1337                        rx->page_offset = 0;
1338                        rx->bus = bus;
1339
1340                }
1341                rx->info[idx].page = rx->page;
1342                rx->info[idx].page_offset = rx->page_offset;
1343                /* note that this is the address of the start of the
1344                 * page */
1345                dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
1346                rx->shadow[idx].addr_low =
1347                    htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
1348                rx->shadow[idx].addr_high =
1349                    htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
1350
1351                /* start next packet on a cacheline boundary */
1352                rx->page_offset += SKB_DATA_ALIGN(bytes);
1353
1354#if MYRI10GE_ALLOC_SIZE > 4096
1355                /* don't cross a 4KB boundary */
1356                end_offset = rx->page_offset + bytes - 1;
1357                if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
1358                        rx->page_offset = end_offset & ~4095;
1359#endif
1360                rx->fill_cnt++;
1361
1362                /* copy 8 descriptors to the firmware at a time */
1363                if ((idx & 7) == 7) {
1364                        myri10ge_submit_8rx(&rx->lanai[idx - 7],
1365                                            &rx->shadow[idx - 7]);
1366                }
1367        }
1368}
1369
1370static inline void
1371myri10ge_unmap_rx_page(struct pci_dev *pdev,
1372                       struct myri10ge_rx_buffer_state *info, int bytes)
1373{
1374        /* unmap the recvd page if we're the only or last user of it */
1375        if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
1376            (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
1377                pci_unmap_page(pdev, (dma_unmap_addr(info, bus)
1378                                      & ~(MYRI10GE_ALLOC_SIZE - 1)),
1379                               MYRI10GE_ALLOC_SIZE, PCI_DMA_FROMDEVICE);
1380        }
1381}
1382
1383/*
1384 * GRO does not support acceleration of tagged vlan frames, and
1385 * this NIC does not support vlan tag offload, so we must pop
1386 * the tag ourselves to be able to achieve GRO performance that
1387 * is comparable to LRO.
1388 */
1389
1390static inline void
1391myri10ge_vlan_rx(struct net_device *dev, void *addr, struct sk_buff *skb)
1392{
1393        u8 *va;
1394        struct vlan_ethhdr *veh;
1395        struct skb_frag_struct *frag;
1396        __wsum vsum;
1397
1398        va = addr;
1399        va += MXGEFW_PAD;
1400        veh = (struct vlan_ethhdr *)va;
1401        if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
1402            NETIF_F_HW_VLAN_CTAG_RX &&
1403            veh->h_vlan_proto == htons(ETH_P_8021Q)) {
1404                /* fixup csum if needed */
1405                if (skb->ip_summed == CHECKSUM_COMPLETE) {
1406                        vsum = csum_partial(va + ETH_HLEN, VLAN_HLEN, 0);
1407                        skb->csum = csum_sub(skb->csum, vsum);
1408                }
1409                /* pop tag */
1410                __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(veh->h_vlan_TCI));
1411                memmove(va + VLAN_HLEN, va, 2 * ETH_ALEN);
1412                skb->len -= VLAN_HLEN;
1413                skb->data_len -= VLAN_HLEN;
1414                frag = skb_shinfo(skb)->frags;
1415                frag->page_offset += VLAN_HLEN;
1416                skb_frag_size_set(frag, skb_frag_size(frag) - VLAN_HLEN);
1417        }
1418}
1419
1420#define MYRI10GE_HLEN 64 /* Bytes to copy from page to skb linear memory */
1421
1422static inline int
1423myri10ge_rx_done(struct myri10ge_slice_state *ss, int len, __wsum csum)
1424{
1425        struct myri10ge_priv *mgp = ss->mgp;
1426        struct sk_buff *skb;
1427        struct skb_frag_struct *rx_frags;
1428        struct myri10ge_rx_buf *rx;
1429        int i, idx, remainder, bytes;
1430        struct pci_dev *pdev = mgp->pdev;
1431        struct net_device *dev = mgp->dev;
1432        u8 *va;
1433        bool polling;
1434
1435        if (len <= mgp->small_bytes) {
1436                rx = &ss->rx_small;
1437                bytes = mgp->small_bytes;
1438        } else {
1439                rx = &ss->rx_big;
1440                bytes = mgp->big_bytes;
1441        }
1442
1443        len += MXGEFW_PAD;
1444        idx = rx->cnt & rx->mask;
1445        va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
1446        prefetch(va);
1447
1448        /* When busy polling in user context, allocate skb and copy headers to
1449         * skb's linear memory ourselves.  When not busy polling, use the napi
1450         * gro api.
1451         */
1452        polling = myri10ge_ss_busy_polling(ss);
1453        if (polling)
1454                skb = netdev_alloc_skb(dev, MYRI10GE_HLEN + 16);
1455        else
1456                skb = napi_get_frags(&ss->napi);
1457        if (unlikely(skb == NULL)) {
1458                ss->stats.rx_dropped++;
1459                for (i = 0, remainder = len; remainder > 0; i++) {
1460                        myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1461                        put_page(rx->info[idx].page);
1462                        rx->cnt++;
1463                        idx = rx->cnt & rx->mask;
1464                        remainder -= MYRI10GE_ALLOC_SIZE;
1465                }
1466                return 0;
1467        }
1468        rx_frags = skb_shinfo(skb)->frags;
1469        /* Fill skb_frag_struct(s) with data from our receive */
1470        for (i = 0, remainder = len; remainder > 0; i++) {
1471                myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1472                skb_fill_page_desc(skb, i, rx->info[idx].page,
1473                                   rx->info[idx].page_offset,
1474                                   remainder < MYRI10GE_ALLOC_SIZE ?
1475                                   remainder : MYRI10GE_ALLOC_SIZE);
1476                rx->cnt++;
1477                idx = rx->cnt & rx->mask;
1478                remainder -= MYRI10GE_ALLOC_SIZE;
1479        }
1480
1481        /* remove padding */
1482        rx_frags[0].page_offset += MXGEFW_PAD;
1483        rx_frags[0].size -= MXGEFW_PAD;
1484        len -= MXGEFW_PAD;
1485
1486        skb->len = len;
1487        skb->data_len = len;
1488        skb->truesize += len;
1489        if (dev->features & NETIF_F_RXCSUM) {
1490                skb->ip_summed = CHECKSUM_COMPLETE;
1491                skb->csum = csum;
1492        }
1493        myri10ge_vlan_rx(mgp->dev, va, skb);
1494        skb_record_rx_queue(skb, ss - &mgp->ss[0]);
1495
1496        if (polling) {
1497                int hlen;
1498
1499                /* myri10ge_vlan_rx might have moved the header, so compute
1500                 * length and address again.
1501                 */
1502                hlen = MYRI10GE_HLEN > skb->len ? skb->len : MYRI10GE_HLEN;
1503                va = page_address(skb_frag_page(&rx_frags[0])) +
1504                        rx_frags[0].page_offset;
1505                /* Copy header into the skb linear memory */
1506                skb_copy_to_linear_data(skb, va, hlen);
1507                rx_frags[0].page_offset += hlen;
1508                rx_frags[0].size -= hlen;
1509                skb->data_len -= hlen;
1510                skb->tail += hlen;
1511                skb->protocol = eth_type_trans(skb, dev);
1512                skb_mark_napi_id(skb, &ss->napi);
1513                netif_receive_skb(skb);
1514        }
1515        else
1516                napi_gro_frags(&ss->napi);
1517
1518        return 1;
1519}
1520
1521static inline void
1522myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
1523{
1524        struct pci_dev *pdev = ss->mgp->pdev;
1525        struct myri10ge_tx_buf *tx = &ss->tx;
1526        struct netdev_queue *dev_queue;
1527        struct sk_buff *skb;
1528        int idx, len;
1529
1530        while (tx->pkt_done != mcp_index) {
1531                idx = tx->done & tx->mask;
1532                skb = tx->info[idx].skb;
1533
1534                /* Mark as free */
1535                tx->info[idx].skb = NULL;
1536                if (tx->info[idx].last) {
1537                        tx->pkt_done++;
1538                        tx->info[idx].last = 0;
1539                }
1540                tx->done++;
1541                len = dma_unmap_len(&tx->info[idx], len);
1542                dma_unmap_len_set(&tx->info[idx], len, 0);
1543                if (skb) {
1544                        ss->stats.tx_bytes += skb->len;
1545                        ss->stats.tx_packets++;
1546                        dev_kfree_skb_irq(skb);
1547                        if (len)
1548                                pci_unmap_single(pdev,
1549                                                 dma_unmap_addr(&tx->info[idx],
1550                                                                bus), len,
1551                                                 PCI_DMA_TODEVICE);
1552                } else {
1553                        if (len)
1554                                pci_unmap_page(pdev,
1555                                               dma_unmap_addr(&tx->info[idx],
1556                                                              bus), len,
1557                                               PCI_DMA_TODEVICE);
1558                }
1559        }
1560
1561        dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
1562        /*
1563         * Make a minimal effort to prevent the NIC from polling an
1564         * idle tx queue.  If we can't get the lock we leave the queue
1565         * active. In this case, either a thread was about to start
1566         * using the queue anyway, or we lost a race and the NIC will
1567         * waste some of its resources polling an inactive queue for a
1568         * while.
1569         */
1570
1571        if ((ss->mgp->dev->real_num_tx_queues > 1) &&
1572            __netif_tx_trylock(dev_queue)) {
1573                if (tx->req == tx->done) {
1574                        tx->queue_active = 0;
1575                        put_be32(htonl(1), tx->send_stop);
1576                        mb();
1577                        mmiowb();
1578                }
1579                __netif_tx_unlock(dev_queue);
1580        }
1581
1582        /* start the queue if we've stopped it */
1583        if (netif_tx_queue_stopped(dev_queue) &&
1584            tx->req - tx->done < (tx->mask >> 1) &&
1585            ss->mgp->running == MYRI10GE_ETH_RUNNING) {
1586                tx->wake_queue++;
1587                netif_tx_wake_queue(dev_queue);
1588        }
1589}
1590
1591static inline int
1592myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
1593{
1594        struct myri10ge_rx_done *rx_done = &ss->rx_done;
1595        struct myri10ge_priv *mgp = ss->mgp;
1596        unsigned long rx_bytes = 0;
1597        unsigned long rx_packets = 0;
1598        unsigned long rx_ok;
1599        int idx = rx_done->idx;
1600        int cnt = rx_done->cnt;
1601        int work_done = 0;
1602        u16 length;
1603        __wsum checksum;
1604
1605        while (rx_done->entry[idx].length != 0 && work_done < budget) {
1606                length = ntohs(rx_done->entry[idx].length);
1607                rx_done->entry[idx].length = 0;
1608                checksum = csum_unfold(rx_done->entry[idx].checksum);
1609                rx_ok = myri10ge_rx_done(ss, length, checksum);
1610                rx_packets += rx_ok;
1611                rx_bytes += rx_ok * (unsigned long)length;
1612                cnt++;
1613                idx = cnt & (mgp->max_intr_slots - 1);
1614                work_done++;
1615        }
1616        rx_done->idx = idx;
1617        rx_done->cnt = cnt;
1618        ss->stats.rx_packets += rx_packets;
1619        ss->stats.rx_bytes += rx_bytes;
1620
1621        /* restock receive rings if needed */
1622        if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
1623                myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
1624                                        mgp->small_bytes + MXGEFW_PAD, 0);
1625        if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
1626                myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
1627
1628        return work_done;
1629}
1630
1631static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
1632{
1633        struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
1634
1635        if (unlikely(stats->stats_updated)) {
1636                unsigned link_up = ntohl(stats->link_up);
1637                if (mgp->link_state != link_up) {
1638                        mgp->link_state = link_up;
1639
1640                        if (mgp->link_state == MXGEFW_LINK_UP) {
1641                                netif_info(mgp, link, mgp->dev, "link up\n");
1642                                netif_carrier_on(mgp->dev);
1643                                mgp->link_changes++;
1644                        } else {
1645                                netif_info(mgp, link, mgp->dev, "link %s\n",
1646                                           (link_up == MXGEFW_LINK_MYRINET ?
1647                                            "mismatch (Myrinet detected)" :
1648                                            "down"));
1649                                netif_carrier_off(mgp->dev);
1650                                mgp->link_changes++;
1651                        }
1652                }
1653                if (mgp->rdma_tags_available !=
1654                    ntohl(stats->rdma_tags_available)) {
1655                        mgp->rdma_tags_available =
1656                            ntohl(stats->rdma_tags_available);
1657                        netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
1658                                    mgp->rdma_tags_available);
1659                }
1660                mgp->down_cnt += stats->link_down;
1661                if (stats->link_down)
1662                        wake_up(&mgp->down_wq);
1663        }
1664}
1665
1666static int myri10ge_poll(struct napi_struct *napi, int budget)
1667{
1668        struct myri10ge_slice_state *ss =
1669            container_of(napi, struct myri10ge_slice_state, napi);
1670        int work_done;
1671
1672#ifdef CONFIG_MYRI10GE_DCA
1673        if (ss->mgp->dca_enabled)
1674                myri10ge_update_dca(ss);
1675#endif
1676        /* Try later if the busy_poll handler is running. */
1677        if (!myri10ge_ss_lock_napi(ss))
1678                return budget;
1679
1680        /* process as many rx events as NAPI will allow */
1681        work_done = myri10ge_clean_rx_done(ss, budget);
1682
1683        myri10ge_ss_unlock_napi(ss);
1684        if (work_done < budget) {
1685                napi_complete(napi);
1686                put_be32(htonl(3), ss->irq_claim);
1687        }
1688        return work_done;
1689}
1690
1691#ifdef CONFIG_NET_RX_BUSY_POLL
1692static int myri10ge_busy_poll(struct napi_struct *napi)
1693{
1694        struct myri10ge_slice_state *ss =
1695            container_of(napi, struct myri10ge_slice_state, napi);
1696        struct myri10ge_priv *mgp = ss->mgp;
1697        int work_done;
1698
1699        /* Poll only when the link is up */
1700        if (mgp->link_state != MXGEFW_LINK_UP)
1701                return LL_FLUSH_FAILED;
1702
1703        if (!myri10ge_ss_lock_poll(ss))
1704                return LL_FLUSH_BUSY;
1705
1706        /* Process a small number of packets */
1707        work_done = myri10ge_clean_rx_done(ss, 4);
1708        if (work_done)
1709                ss->busy_poll_cnt += work_done;
1710        else
1711                ss->busy_poll_miss++;
1712
1713        myri10ge_ss_unlock_poll(ss);
1714
1715        return work_done;
1716}
1717#endif /* CONFIG_NET_RX_BUSY_POLL */
1718
1719static irqreturn_t myri10ge_intr(int irq, void *arg)
1720{
1721        struct myri10ge_slice_state *ss = arg;
1722        struct myri10ge_priv *mgp = ss->mgp;
1723        struct mcp_irq_data *stats = ss->fw_stats;
1724        struct myri10ge_tx_buf *tx = &ss->tx;
1725        u32 send_done_count;
1726        int i;
1727
1728        /* an interrupt on a non-zero receive-only slice is implicitly
1729         * valid  since MSI-X irqs are not shared */
1730        if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
1731                napi_schedule(&ss->napi);
1732                return IRQ_HANDLED;
1733        }
1734
1735        /* make sure it is our IRQ, and that the DMA has finished */
1736        if (unlikely(!stats->valid))
1737                return IRQ_NONE;
1738
1739        /* low bit indicates receives are present, so schedule
1740         * napi poll handler */
1741        if (stats->valid & 1)
1742                napi_schedule(&ss->napi);
1743
1744        if (!mgp->msi_enabled && !mgp->msix_enabled) {
1745                put_be32(0, mgp->irq_deassert);
1746                if (!myri10ge_deassert_wait)
1747                        stats->valid = 0;
1748                mb();
1749        } else
1750                stats->valid = 0;
1751
1752        /* Wait for IRQ line to go low, if using INTx */
1753        i = 0;
1754        while (1) {
1755                i++;
1756                /* check for transmit completes and receives */
1757                send_done_count = ntohl(stats->send_done_count);
1758                if (send_done_count != tx->pkt_done)
1759                        myri10ge_tx_done(ss, (int)send_done_count);
1760                if (unlikely(i > myri10ge_max_irq_loops)) {
1761                        netdev_warn(mgp->dev, "irq stuck?\n");
1762                        stats->valid = 0;
1763                        schedule_work(&mgp->watchdog_work);
1764                }
1765                if (likely(stats->valid == 0))
1766                        break;
1767                cpu_relax();
1768                barrier();
1769        }
1770
1771        /* Only slice 0 updates stats */
1772        if (ss == mgp->ss)
1773                myri10ge_check_statblock(mgp);
1774
1775        put_be32(htonl(3), ss->irq_claim + 1);
1776        return IRQ_HANDLED;
1777}
1778
1779static int
1780myri10ge_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1781{
1782        struct myri10ge_priv *mgp = netdev_priv(netdev);
1783        char *ptr;
1784        int i;
1785
1786        cmd->autoneg = AUTONEG_DISABLE;
1787        ethtool_cmd_speed_set(cmd, SPEED_10000);
1788        cmd->duplex = DUPLEX_FULL;
1789
1790        /*
1791         * parse the product code to deterimine the interface type
1792         * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
1793         * after the 3rd dash in the driver's cached copy of the
1794         * EEPROM's product code string.
1795         */
1796        ptr = mgp->product_code_string;
1797        if (ptr == NULL) {
1798                netdev_err(netdev, "Missing product code\n");
1799                return 0;
1800        }
1801        for (i = 0; i < 3; i++, ptr++) {
1802                ptr = strchr(ptr, '-');
1803                if (ptr == NULL) {
1804                        netdev_err(netdev, "Invalid product code %s\n",
1805                                   mgp->product_code_string);
1806                        return 0;
1807                }
1808        }
1809        if (*ptr == '2')
1810                ptr++;
1811        if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
1812                /* We've found either an XFP, quad ribbon fiber, or SFP+ */
1813                cmd->port = PORT_FIBRE;
1814                cmd->supported |= SUPPORTED_FIBRE;
1815                cmd->advertising |= ADVERTISED_FIBRE;
1816        } else {
1817                cmd->port = PORT_OTHER;
1818        }
1819        if (*ptr == 'R' || *ptr == 'S')
1820                cmd->transceiver = XCVR_EXTERNAL;
1821        else
1822                cmd->transceiver = XCVR_INTERNAL;
1823
1824        return 0;
1825}
1826
1827static void
1828myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
1829{
1830        struct myri10ge_priv *mgp = netdev_priv(netdev);
1831
1832        strlcpy(info->driver, "myri10ge", sizeof(info->driver));
1833        strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
1834        strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
1835        strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
1836}
1837
1838static int
1839myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1840{
1841        struct myri10ge_priv *mgp = netdev_priv(netdev);
1842
1843        coal->rx_coalesce_usecs = mgp->intr_coal_delay;
1844        return 0;
1845}
1846
1847static int
1848myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1849{
1850        struct myri10ge_priv *mgp = netdev_priv(netdev);
1851
1852        mgp->intr_coal_delay = coal->rx_coalesce_usecs;
1853        put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1854        return 0;
1855}
1856
1857static void
1858myri10ge_get_pauseparam(struct net_device *netdev,
1859                        struct ethtool_pauseparam *pause)
1860{
1861        struct myri10ge_priv *mgp = netdev_priv(netdev);
1862
1863        pause->autoneg = 0;
1864        pause->rx_pause = mgp->pause;
1865        pause->tx_pause = mgp->pause;
1866}
1867
1868static int
1869myri10ge_set_pauseparam(struct net_device *netdev,
1870                        struct ethtool_pauseparam *pause)
1871{
1872        struct myri10ge_priv *mgp = netdev_priv(netdev);
1873
1874        if (pause->tx_pause != mgp->pause)
1875                return myri10ge_change_pause(mgp, pause->tx_pause);
1876        if (pause->rx_pause != mgp->pause)
1877                return myri10ge_change_pause(mgp, pause->rx_pause);
1878        if (pause->autoneg != 0)
1879                return -EINVAL;
1880        return 0;
1881}
1882
1883static void
1884myri10ge_get_ringparam(struct net_device *netdev,
1885                       struct ethtool_ringparam *ring)
1886{
1887        struct myri10ge_priv *mgp = netdev_priv(netdev);
1888
1889        ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
1890        ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
1891        ring->rx_jumbo_max_pending = 0;
1892        ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
1893        ring->rx_mini_pending = ring->rx_mini_max_pending;
1894        ring->rx_pending = ring->rx_max_pending;
1895        ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
1896        ring->tx_pending = ring->tx_max_pending;
1897}
1898
1899static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
1900        "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
1901        "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
1902        "rx_length_errors", "rx_over_errors", "rx_crc_errors",
1903        "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
1904        "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
1905        "tx_heartbeat_errors", "tx_window_errors",
1906        /* device-specific stats */
1907        "tx_boundary", "WC", "irq", "MSI", "MSIX",
1908        "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
1909        "serial_number", "watchdog_resets",
1910#ifdef CONFIG_MYRI10GE_DCA
1911        "dca_capable_firmware", "dca_device_present",
1912#endif
1913        "link_changes", "link_up", "dropped_link_overflow",
1914        "dropped_link_error_or_filtered",
1915        "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
1916        "dropped_unicast_filtered", "dropped_multicast_filtered",
1917        "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
1918        "dropped_no_big_buffer"
1919};
1920
1921static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
1922        "----------- slice ---------",
1923        "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
1924        "rx_small_cnt", "rx_big_cnt",
1925        "wake_queue", "stop_queue", "tx_linearized",
1926#ifdef CONFIG_NET_RX_BUSY_POLL
1927        "rx_lock_napi_yield", "rx_lock_poll_yield", "rx_busy_poll_miss",
1928        "rx_busy_poll_cnt",
1929#endif
1930};
1931
1932#define MYRI10GE_NET_STATS_LEN      21
1933#define MYRI10GE_MAIN_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_main_stats)
1934#define MYRI10GE_SLICE_STATS_LEN  ARRAY_SIZE(myri10ge_gstrings_slice_stats)
1935
1936static void
1937myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
1938{
1939        struct myri10ge_priv *mgp = netdev_priv(netdev);
1940        int i;
1941
1942        switch (stringset) {
1943        case ETH_SS_STATS:
1944                memcpy(data, *myri10ge_gstrings_main_stats,
1945                       sizeof(myri10ge_gstrings_main_stats));
1946                data += sizeof(myri10ge_gstrings_main_stats);
1947                for (i = 0; i < mgp->num_slices; i++) {
1948                        memcpy(data, *myri10ge_gstrings_slice_stats,
1949                               sizeof(myri10ge_gstrings_slice_stats));
1950                        data += sizeof(myri10ge_gstrings_slice_stats);
1951                }
1952                break;
1953        }
1954}
1955
1956static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
1957{
1958        struct myri10ge_priv *mgp = netdev_priv(netdev);
1959
1960        switch (sset) {
1961        case ETH_SS_STATS:
1962                return MYRI10GE_MAIN_STATS_LEN +
1963                    mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
1964        default:
1965                return -EOPNOTSUPP;
1966        }
1967}
1968
1969static void
1970myri10ge_get_ethtool_stats(struct net_device *netdev,
1971                           struct ethtool_stats *stats, u64 * data)
1972{
1973        struct myri10ge_priv *mgp = netdev_priv(netdev);
1974        struct myri10ge_slice_state *ss;
1975        struct rtnl_link_stats64 link_stats;
1976        int slice;
1977        int i;
1978
1979        /* force stats update */
1980        memset(&link_stats, 0, sizeof(link_stats));
1981        (void)myri10ge_get_stats(netdev, &link_stats);
1982        for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1983                data[i] = ((u64 *)&link_stats)[i];
1984
1985        data[i++] = (unsigned int)mgp->tx_boundary;
1986        data[i++] = (unsigned int)mgp->wc_enabled;
1987        data[i++] = (unsigned int)mgp->pdev->irq;
1988        data[i++] = (unsigned int)mgp->msi_enabled;
1989        data[i++] = (unsigned int)mgp->msix_enabled;
1990        data[i++] = (unsigned int)mgp->read_dma;
1991        data[i++] = (unsigned int)mgp->write_dma;
1992        data[i++] = (unsigned int)mgp->read_write_dma;
1993        data[i++] = (unsigned int)mgp->serial_number;
1994        data[i++] = (unsigned int)mgp->watchdog_resets;
1995#ifdef CONFIG_MYRI10GE_DCA
1996        data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
1997        data[i++] = (unsigned int)(mgp->dca_enabled);
1998#endif
1999        data[i++] = (unsigned int)mgp->link_changes;
2000
2001        /* firmware stats are useful only in the first slice */
2002        ss = &mgp->ss[0];
2003        data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
2004        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
2005        data[i++] =
2006            (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
2007        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
2008        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
2009        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
2010        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
2011        data[i++] =
2012            (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
2013        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
2014        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
2015        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
2016        data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
2017
2018        for (slice = 0; slice < mgp->num_slices; slice++) {
2019                ss = &mgp->ss[slice];
2020                data[i++] = slice;
2021                data[i++] = (unsigned int)ss->tx.pkt_start;
2022                data[i++] = (unsigned int)ss->tx.pkt_done;
2023                data[i++] = (unsigned int)ss->tx.req;
2024                data[i++] = (unsigned int)ss->tx.done;
2025                data[i++] = (unsigned int)ss->rx_small.cnt;
2026                data[i++] = (unsigned int)ss->rx_big.cnt;
2027                data[i++] = (unsigned int)ss->tx.wake_queue;
2028                data[i++] = (unsigned int)ss->tx.stop_queue;
2029                data[i++] = (unsigned int)ss->tx.linearized;
2030#ifdef CONFIG_NET_RX_BUSY_POLL
2031                data[i++] = ss->lock_napi_yield;
2032                data[i++] = ss->lock_poll_yield;
2033                data[i++] = ss->busy_poll_miss;
2034                data[i++] = ss->busy_poll_cnt;
2035#endif
2036        }
2037}
2038
2039static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
2040{
2041        struct myri10ge_priv *mgp = netdev_priv(netdev);
2042        mgp->msg_enable = value;
2043}
2044
2045static u32 myri10ge_get_msglevel(struct net_device *netdev)
2046{
2047        struct myri10ge_priv *mgp = netdev_priv(netdev);
2048        return mgp->msg_enable;
2049}
2050
2051/*
2052 * Use a low-level command to change the LED behavior. Rather than
2053 * blinking (which is the normal case), when identify is used, the
2054 * yellow LED turns solid.
2055 */
2056static int myri10ge_led(struct myri10ge_priv *mgp, int on)
2057{
2058        struct mcp_gen_header *hdr;
2059        struct device *dev = &mgp->pdev->dev;
2060        size_t hdr_off, pattern_off, hdr_len;
2061        u32 pattern = 0xfffffffe;
2062
2063        /* find running firmware header */
2064        hdr_off = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
2065        if ((hdr_off & 3) || hdr_off + sizeof(*hdr) > mgp->sram_size) {
2066                dev_err(dev, "Running firmware has bad header offset (%d)\n",
2067                        (int)hdr_off);
2068                return -EIO;
2069        }
2070        hdr_len = swab32(readl(mgp->sram + hdr_off +
2071                               offsetof(struct mcp_gen_header, header_length)));
2072        pattern_off = hdr_off + offsetof(struct mcp_gen_header, led_pattern);
2073        if (pattern_off >= (hdr_len + hdr_off)) {
2074                dev_info(dev, "Firmware does not support LED identification\n");
2075                return -EINVAL;
2076        }
2077        if (!on)
2078                pattern = swab32(readl(mgp->sram + pattern_off + 4));
2079        writel(swab32(pattern), mgp->sram + pattern_off);
2080        return 0;
2081}
2082
2083static int
2084myri10ge_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
2085{
2086        struct myri10ge_priv *mgp = netdev_priv(netdev);
2087        int rc;
2088
2089        switch (state) {
2090        case ETHTOOL_ID_ACTIVE:
2091                rc = myri10ge_led(mgp, 1);
2092                break;
2093
2094        case ETHTOOL_ID_INACTIVE:
2095                rc =  myri10ge_led(mgp, 0);
2096                break;
2097
2098        default:
2099                rc = -EINVAL;
2100        }
2101
2102        return rc;
2103}
2104
2105static const struct ethtool_ops myri10ge_ethtool_ops = {
2106        .get_settings = myri10ge_get_settings,
2107        .get_drvinfo = myri10ge_get_drvinfo,
2108        .get_coalesce = myri10ge_get_coalesce,
2109        .set_coalesce = myri10ge_set_coalesce,
2110        .get_pauseparam = myri10ge_get_pauseparam,
2111        .set_pauseparam = myri10ge_set_pauseparam,
2112        .get_ringparam = myri10ge_get_ringparam,
2113        .get_link = ethtool_op_get_link,
2114        .get_strings = myri10ge_get_strings,
2115        .get_sset_count = myri10ge_get_sset_count,
2116        .get_ethtool_stats = myri10ge_get_ethtool_stats,
2117        .set_msglevel = myri10ge_set_msglevel,
2118        .get_msglevel = myri10ge_get_msglevel,
2119        .set_phys_id = myri10ge_phys_id,
2120};
2121
2122static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
2123{
2124        struct myri10ge_priv *mgp = ss->mgp;
2125        struct myri10ge_cmd cmd;
2126        struct net_device *dev = mgp->dev;
2127        int tx_ring_size, rx_ring_size;
2128        int tx_ring_entries, rx_ring_entries;
2129        int i, slice, status;
2130        size_t bytes;
2131
2132        /* get ring sizes */
2133        slice = ss - mgp->ss;
2134        cmd.data0 = slice;
2135        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
2136        tx_ring_size = cmd.data0;
2137        cmd.data0 = slice;
2138        status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
2139        if (status != 0)
2140                return status;
2141        rx_ring_size = cmd.data0;
2142
2143        tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
2144        rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
2145        ss->tx.mask = tx_ring_entries - 1;
2146        ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
2147
2148        status = -ENOMEM;
2149
2150        /* allocate the host shadow rings */
2151
2152        bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
2153            * sizeof(*ss->tx.req_list);
2154        ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
2155        if (ss->tx.req_bytes == NULL)
2156                goto abort_with_nothing;
2157
2158        /* ensure req_list entries are aligned to 8 bytes */
2159        ss->tx.req_list = (struct mcp_kreq_ether_send *)
2160            ALIGN((unsigned long)ss->tx.req_bytes, 8);
2161        ss->tx.queue_active = 0;
2162
2163        bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
2164        ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
2165        if (ss->rx_small.shadow == NULL)
2166                goto abort_with_tx_req_bytes;
2167
2168        bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
2169        ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
2170        if (ss->rx_big.shadow == NULL)
2171                goto abort_with_rx_small_shadow;
2172
2173        /* allocate the host info rings */
2174
2175        bytes = tx_ring_entries * sizeof(*ss->tx.info);
2176        ss->tx.info = kzalloc(bytes, GFP_KERNEL);
2177        if (ss->tx.info == NULL)
2178                goto abort_with_rx_big_shadow;
2179
2180        bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
2181        ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
2182        if (ss->rx_small.info == NULL)
2183                goto abort_with_tx_info;
2184
2185        bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
2186        ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
2187        if (ss->rx_big.info == NULL)
2188                goto abort_with_rx_small_info;
2189
2190        /* Fill the receive rings */
2191        ss->rx_big.cnt = 0;
2192        ss->rx_small.cnt = 0;
2193        ss->rx_big.fill_cnt = 0;
2194        ss->rx_small.fill_cnt = 0;
2195        ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
2196        ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
2197        ss->rx_small.watchdog_needed = 0;
2198        ss->rx_big.watchdog_needed = 0;
2199        if (mgp->small_bytes == 0) {
2200                ss->rx_small.fill_cnt = ss->rx_small.mask + 1;
2201        } else {
2202                myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
2203                                        mgp->small_bytes + MXGEFW_PAD, 0);
2204        }
2205
2206        if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
2207                netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
2208                           slice, ss->rx_small.fill_cnt);
2209                goto abort_with_rx_small_ring;
2210        }
2211
2212        myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
2213        if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
2214                netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
2215                           slice, ss->rx_big.fill_cnt);
2216                goto abort_with_rx_big_ring;
2217        }
2218
2219        return 0;
2220
2221abort_with_rx_big_ring:
2222        for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
2223                int idx = i & ss->rx_big.mask;
2224                myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
2225                                       mgp->big_bytes);
2226                put_page(ss->rx_big.info[idx].page);
2227        }
2228
2229abort_with_rx_small_ring:
2230        if (mgp->small_bytes == 0)
2231                ss->rx_small.fill_cnt = ss->rx_small.cnt;
2232        for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
2233                int idx = i & ss->rx_small.mask;
2234                myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
2235                                       mgp->small_bytes + MXGEFW_PAD);
2236                put_page(ss->rx_small.info[idx].page);
2237        }
2238
2239        kfree(ss->rx_big.info);
2240
2241abort_with_rx_small_info:
2242        kfree(ss->rx_small.info);
2243
2244abort_with_tx_info:
2245        kfree(ss->tx.info);
2246
2247abort_with_rx_big_shadow:
2248        kfree(ss->rx_big.shadow);
2249
2250abort_with_rx_small_shadow:
2251        kfree(ss->rx_small.shadow);
2252
2253abort_with_tx_req_bytes:
2254        kfree(ss->tx.req_bytes);
2255        ss->tx.req_bytes = NULL;
2256        ss->tx.req_list = NULL;
2257
2258abort_with_nothing:
2259        return status;
2260}
2261
2262static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
2263{
2264        struct myri10ge_priv *mgp = ss->mgp;
2265        struct sk_buff *skb;
2266        struct myri10ge_tx_buf *tx;
2267        int i, len, idx;
2268
2269        /* If not allocated, skip it */
2270        if (ss->tx.req_list == NULL)
2271                return;
2272
2273        for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
2274                idx = i & ss->rx_big.mask;
2275                if (i == ss->rx_big.fill_cnt - 1)
2276                        ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
2277                myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
2278                                       mgp->big_bytes);
2279                put_page(ss->rx_big.info[idx].page);
2280        }
2281
2282        if (mgp->small_bytes == 0)
2283                ss->rx_small.fill_cnt = ss->rx_small.cnt;
2284        for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
2285                idx = i & ss->rx_small.mask;
2286                if (i == ss->rx_small.fill_cnt - 1)
2287                        ss->rx_small.info[idx].page_offset =
2288                            MYRI10GE_ALLOC_SIZE;
2289                myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
2290                                       mgp->small_bytes + MXGEFW_PAD);
2291                put_page(ss->rx_small.info[idx].page);
2292        }
2293        tx = &ss->tx;
2294        while (tx->done != tx->req) {
2295                idx = tx->done & tx->mask;
2296                skb = tx->info[idx].skb;
2297
2298                /* Mark as free */
2299                tx->info[idx].skb = NULL;
2300                tx->done++;
2301                len = dma_unmap_len(&tx->info[idx], len);
2302                dma_unmap_len_set(&tx->info[idx], len, 0);
2303                if (skb) {
2304                        ss->stats.tx_dropped++;
2305                        dev_kfree_skb_any(skb);
2306                        if (len)
2307                                pci_unmap_single(mgp->pdev,
2308                                                 dma_unmap_addr(&tx->info[idx],
2309                                                                bus), len,
2310                                                 PCI_DMA_TODEVICE);
2311                } else {
2312                        if (len)
2313                                pci_unmap_page(mgp->pdev,
2314                                               dma_unmap_addr(&tx->info[idx],
2315                                                              bus), len,
2316                                               PCI_DMA_TODEVICE);
2317                }
2318        }
2319        kfree(ss->rx_big.info);
2320
2321        kfree(ss->rx_small.info);
2322
2323        kfree(ss->tx.info);
2324
2325        kfree(ss->rx_big.shadow);
2326
2327        kfree(ss->rx_small.shadow);
2328
2329        kfree(ss->tx.req_bytes);
2330        ss->tx.req_bytes = NULL;
2331        ss->tx.req_list = NULL;
2332}
2333
2334static int myri10ge_request_irq(struct myri10ge_priv *mgp)
2335{
2336        struct pci_dev *pdev = mgp->pdev;
2337        struct myri10ge_slice_state *ss;
2338        struct net_device *netdev = mgp->dev;
2339        int i;
2340        int status;
2341
2342        mgp->msi_enabled = 0;
2343        mgp->msix_enabled = 0;
2344        status = 0;
2345        if (myri10ge_msi) {
2346                if (mgp->num_slices > 1) {
2347                        status =
2348                            pci_enable_msix(pdev, mgp->msix_vectors,
2349                                            mgp->num_slices);
2350                        if (status == 0) {
2351                                mgp->msix_enabled = 1;
2352                        } else {
2353                                dev_err(&pdev->dev,
2354                                        "Error %d setting up MSI-X\n", status);
2355                                return status;
2356                        }
2357                }
2358                if (mgp->msix_enabled == 0) {
2359                        status = pci_enable_msi(pdev);
2360                        if (status != 0) {
2361                                dev_err(&pdev->dev,
2362                                        "Error %d setting up MSI; falling back to xPIC\n",
2363                                        status);
2364                        } else {
2365                                mgp->msi_enabled = 1;
2366                        }
2367                }
2368        }
2369        if (mgp->msix_enabled) {
2370                for (i = 0; i < mgp->num_slices; i++) {
2371                        ss = &mgp->ss[i];
2372                        snprintf(ss->irq_desc, sizeof(ss->irq_desc),
2373                                 "%s:slice-%d", netdev->name, i);
2374                        status = request_irq(mgp->msix_vectors[i].vector,
2375                                             myri10ge_intr, 0, ss->irq_desc,
2376                                             ss);
2377                        if (status != 0) {
2378                                dev_err(&pdev->dev,
2379                                        "slice %d failed to allocate IRQ\n", i);
2380                                i--;
2381                                while (i >= 0) {
2382                                        free_irq(mgp->msix_vectors[i].vector,
2383                                                 &mgp->ss[i]);
2384                                        i--;
2385                                }
2386                                pci_disable_msix(pdev);
2387                                return status;
2388                        }
2389                }
2390        } else {
2391                status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
2392                                     mgp->dev->name, &mgp->ss[0]);
2393                if (status != 0) {
2394                        dev_err(&pdev->dev, "failed to allocate IRQ\n");
2395                        if (mgp->msi_enabled)
2396                                pci_disable_msi(pdev);
2397                }
2398        }
2399        return status;
2400}
2401
2402static void myri10ge_free_irq(struct myri10ge_priv *mgp)
2403{
2404        struct pci_dev *pdev = mgp->pdev;
2405        int i;
2406
2407        if (mgp->msix_enabled) {
2408                for (i = 0; i < mgp->num_slices; i++)
2409                        free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
2410        } else {
2411                free_irq(pdev->irq, &mgp->ss[0]);
2412        }
2413        if (mgp->msi_enabled)
2414                pci_disable_msi(pdev);
2415        if (mgp->msix_enabled)
2416                pci_disable_msix(pdev);
2417}
2418
2419static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
2420{
2421        struct myri10ge_cmd cmd;
2422        struct myri10ge_slice_state *ss;
2423        int status;
2424
2425        ss = &mgp->ss[slice];
2426        status = 0;
2427        if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
2428                cmd.data0 = slice;
2429                status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
2430                                           &cmd, 0);
2431                ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
2432                    (mgp->sram + cmd.data0);
2433        }
2434        cmd.data0 = slice;
2435        status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
2436                                    &cmd, 0);
2437        ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
2438            (mgp->sram + cmd.data0);
2439
2440        cmd.data0 = slice;
2441        status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
2442        ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
2443            (mgp->sram + cmd.data0);
2444
2445        ss->tx.send_go = (__iomem __be32 *)
2446            (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
2447        ss->tx.send_stop = (__iomem __be32 *)
2448            (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
2449        return status;
2450
2451}
2452
2453static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
2454{
2455        struct myri10ge_cmd cmd;
2456        struct myri10ge_slice_state *ss;
2457        int status;
2458
2459        ss = &mgp->ss[slice];
2460        cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
2461        cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
2462        cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
2463        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
2464        if (status == -ENOSYS) {
2465                dma_addr_t bus = ss->fw_stats_bus;
2466                if (slice != 0)
2467                        return -EINVAL;
2468                bus += offsetof(struct mcp_irq_data, send_done_count);
2469                cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
2470                cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
2471                status = myri10ge_send_cmd(mgp,
2472                                           MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
2473                                           &cmd, 0);
2474                /* Firmware cannot support multicast without STATS_DMA_V2 */
2475                mgp->fw_multicast_support = 0;
2476        } else {
2477                mgp->fw_multicast_support = 1;
2478        }
2479        return 0;
2480}
2481
2482static int myri10ge_open(struct net_device *dev)
2483{
2484        struct myri10ge_slice_state *ss;
2485        struct myri10ge_priv *mgp = netdev_priv(dev);
2486        struct myri10ge_cmd cmd;
2487        int i, status, big_pow2, slice;
2488        u8 __iomem *itable;
2489
2490        if (mgp->running != MYRI10GE_ETH_STOPPED)
2491                return -EBUSY;
2492
2493        mgp->running = MYRI10GE_ETH_STARTING;
2494        status = myri10ge_reset(mgp);
2495        if (status != 0) {
2496                netdev_err(dev, "failed reset\n");
2497                goto abort_with_nothing;
2498        }
2499
2500        if (mgp->num_slices > 1) {
2501                cmd.data0 = mgp->num_slices;
2502                cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
2503                if (mgp->dev->real_num_tx_queues > 1)
2504                        cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
2505                status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
2506                                           &cmd, 0);
2507                if (status != 0) {
2508                        netdev_err(dev, "failed to set number of slices\n");
2509                        goto abort_with_nothing;
2510                }
2511                /* setup the indirection table */
2512                cmd.data0 = mgp->num_slices;
2513                status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
2514                                           &cmd, 0);
2515
2516                status |= myri10ge_send_cmd(mgp,
2517                                            MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
2518                                            &cmd, 0);
2519                if (status != 0) {
2520                        netdev_err(dev, "failed to setup rss tables\n");
2521                        goto abort_with_nothing;
2522                }
2523
2524                /* just enable an identity mapping */
2525                itable = mgp->sram + cmd.data0;
2526                for (i = 0; i < mgp->num_slices; i++)
2527                        __raw_writeb(i, &itable[i]);
2528
2529                cmd.data0 = 1;
2530                cmd.data1 = myri10ge_rss_hash;
2531                status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
2532                                           &cmd, 0);
2533                if (status != 0) {
2534                        netdev_err(dev, "failed to enable slices\n");
2535                        goto abort_with_nothing;
2536                }
2537        }
2538
2539        status = myri10ge_request_irq(mgp);
2540        if (status != 0)
2541                goto abort_with_nothing;
2542
2543        /* decide what small buffer size to use.  For good TCP rx
2544         * performance, it is important to not receive 1514 byte
2545         * frames into jumbo buffers, as it confuses the socket buffer
2546         * accounting code, leading to drops and erratic performance.
2547         */
2548
2549        if (dev->mtu <= ETH_DATA_LEN)
2550                /* enough for a TCP header */
2551                mgp->small_bytes = (128 > SMP_CACHE_BYTES)
2552                    ? (128 - MXGEFW_PAD)
2553                    : (SMP_CACHE_BYTES - MXGEFW_PAD);
2554        else
2555                /* enough for a vlan encapsulated ETH_DATA_LEN frame */
2556                mgp->small_bytes = VLAN_ETH_FRAME_LEN;
2557
2558        /* Override the small buffer size? */
2559        if (myri10ge_small_bytes >= 0)
2560                mgp->small_bytes = myri10ge_small_bytes;
2561
2562        /* Firmware needs the big buff size as a power of 2.  Lie and
2563         * tell him the buffer is larger, because we only use 1
2564         * buffer/pkt, and the mtu will prevent overruns.
2565         */
2566        big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
2567        if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
2568                while (!is_power_of_2(big_pow2))
2569                        big_pow2++;
2570                mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
2571        } else {
2572                big_pow2 = MYRI10GE_ALLOC_SIZE;
2573                mgp->big_bytes = big_pow2;
2574        }
2575
2576        /* setup the per-slice data structures */
2577        for (slice = 0; slice < mgp->num_slices; slice++) {
2578                ss = &mgp->ss[slice];
2579
2580                status = myri10ge_get_txrx(mgp, slice);
2581                if (status != 0) {
2582                        netdev_err(dev, "failed to get ring sizes or locations\n");
2583                        goto abort_with_rings;
2584                }
2585                status = myri10ge_allocate_rings(ss);
2586                if (status != 0)
2587                        goto abort_with_rings;
2588
2589                /* only firmware which supports multiple TX queues
2590                 * supports setting up the tx stats on non-zero
2591                 * slices */
2592                if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
2593                        status = myri10ge_set_stats(mgp, slice);
2594                if (status) {
2595                        netdev_err(dev, "Couldn't set stats DMA\n");
2596                        goto abort_with_rings;
2597                }
2598
2599                /* Initialize the slice spinlock and state used for polling */
2600                myri10ge_ss_init_lock(ss);
2601
2602                /* must happen prior to any irq */
2603                napi_enable(&(ss)->napi);
2604        }
2605
2606        /* now give firmware buffers sizes, and MTU */
2607        cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
2608        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
2609        cmd.data0 = mgp->small_bytes;
2610        status |=
2611            myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
2612        cmd.data0 = big_pow2;
2613        status |=
2614            myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
2615        if (status) {
2616                netdev_err(dev, "Couldn't set buffer sizes\n");
2617                goto abort_with_rings;
2618        }
2619
2620        /*
2621         * Set Linux style TSO mode; this is needed only on newer
2622         *  firmware versions.  Older versions default to Linux
2623         *  style TSO
2624         */
2625        cmd.data0 = 0;
2626        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
2627        if (status && status != -ENOSYS) {
2628                netdev_err(dev, "Couldn't set TSO mode\n");
2629                goto abort_with_rings;
2630        }
2631
2632        mgp->link_state = ~0U;
2633        mgp->rdma_tags_available = 15;
2634
2635        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
2636        if (status) {
2637                netdev_err(dev, "Couldn't bring up link\n");
2638                goto abort_with_rings;
2639        }
2640
2641        mgp->running = MYRI10GE_ETH_RUNNING;
2642        mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
2643        add_timer(&mgp->watchdog_timer);
2644        netif_tx_wake_all_queues(dev);
2645
2646        return 0;
2647
2648abort_with_rings:
2649        while (slice) {
2650                slice--;
2651                napi_disable(&mgp->ss[slice].napi);
2652        }
2653        for (i = 0; i < mgp->num_slices; i++)
2654                myri10ge_free_rings(&mgp->ss[i]);
2655
2656        myri10ge_free_irq(mgp);
2657
2658abort_with_nothing:
2659        mgp->running = MYRI10GE_ETH_STOPPED;
2660        return -ENOMEM;
2661}
2662
2663static int myri10ge_close(struct net_device *dev)
2664{
2665        struct myri10ge_priv *mgp = netdev_priv(dev);
2666        struct myri10ge_cmd cmd;
2667        int status, old_down_cnt;
2668        int i;
2669
2670        if (mgp->running != MYRI10GE_ETH_RUNNING)
2671                return 0;
2672
2673        if (mgp->ss[0].tx.req_bytes == NULL)
2674                return 0;
2675
2676        del_timer_sync(&mgp->watchdog_timer);
2677        mgp->running = MYRI10GE_ETH_STOPPING;
2678        for (i = 0; i < mgp->num_slices; i++) {
2679                napi_disable(&mgp->ss[i].napi);
2680                local_bh_disable(); /* myri10ge_ss_lock_napi needs this */
2681                /* Lock the slice to prevent the busy_poll handler from
2682                 * accessing it.  Later when we bring the NIC up, myri10ge_open
2683                 * resets the slice including this lock.
2684                 */
2685                while (!myri10ge_ss_lock_napi(&mgp->ss[i])) {
2686                        pr_info("Slice %d locked\n", i);
2687                        mdelay(1);
2688                }
2689                local_bh_enable();
2690        }
2691        netif_carrier_off(dev);
2692
2693        netif_tx_stop_all_queues(dev);
2694        if (mgp->rebooted == 0) {
2695                old_down_cnt = mgp->down_cnt;
2696                mb();
2697                status =
2698                    myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
2699                if (status)
2700                        netdev_err(dev, "Couldn't bring down link\n");
2701
2702                wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
2703                                   HZ);
2704                if (old_down_cnt == mgp->down_cnt)
2705                        netdev_err(dev, "never got down irq\n");
2706        }
2707        netif_tx_disable(dev);
2708        myri10ge_free_irq(mgp);
2709        for (i = 0; i < mgp->num_slices; i++)
2710                myri10ge_free_rings(&mgp->ss[i]);
2711
2712        mgp->running = MYRI10GE_ETH_STOPPED;
2713        return 0;
2714}
2715
2716/* copy an array of struct mcp_kreq_ether_send's to the mcp.  Copy
2717 * backwards one at a time and handle ring wraps */
2718
2719static inline void
2720myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
2721                              struct mcp_kreq_ether_send *src, int cnt)
2722{
2723        int idx, starting_slot;
2724        starting_slot = tx->req;
2725        while (cnt > 1) {
2726                cnt--;
2727                idx = (starting_slot + cnt) & tx->mask;
2728                myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
2729                mb();
2730        }
2731}
2732
2733/*
2734 * copy an array of struct mcp_kreq_ether_send's to the mcp.  Copy
2735 * at most 32 bytes at a time, so as to avoid involving the software
2736 * pio handler in the nic.   We re-write the first segment's flags
2737 * to mark them valid only after writing the entire chain.
2738 */
2739
2740static inline void
2741myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
2742                    int cnt)
2743{
2744        int idx, i;
2745        struct mcp_kreq_ether_send __iomem *dstp, *dst;
2746        struct mcp_kreq_ether_send *srcp;
2747        u8 last_flags;
2748
2749        idx = tx->req & tx->mask;
2750
2751        last_flags = src->flags;
2752        src->flags = 0;
2753        mb();
2754        dst = dstp = &tx->lanai[idx];
2755        srcp = src;
2756
2757        if ((idx + cnt) < tx->mask) {
2758                for (i = 0; i < (cnt - 1); i += 2) {
2759                        myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
2760                        mb();   /* force write every 32 bytes */
2761                        srcp += 2;
2762                        dstp += 2;
2763                }
2764        } else {
2765                /* submit all but the first request, and ensure
2766                 * that it is submitted below */
2767                myri10ge_submit_req_backwards(tx, src, cnt);
2768                i = 0;
2769        }
2770        if (i < cnt) {
2771                /* submit the first request */
2772                myri10ge_pio_copy(dstp, srcp, sizeof(*src));
2773                mb();           /* barrier before setting valid flag */
2774        }
2775
2776        /* re-write the last 32-bits with the valid flags */
2777        src->flags = last_flags;
2778        put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
2779        tx->req += cnt;
2780        mb();
2781}
2782
2783static void myri10ge_unmap_tx_dma(struct myri10ge_priv *mgp,
2784                                  struct myri10ge_tx_buf *tx, int idx)
2785{
2786        unsigned int len;
2787        int last_idx;
2788
2789        /* Free any DMA resources we've alloced and clear out the skb slot */
2790        last_idx = (idx + 1) & tx->mask;
2791        idx = tx->req & tx->mask;
2792        do {
2793                len = dma_unmap_len(&tx->info[idx], len);
2794                if (len) {
2795                        if (tx->info[idx].skb != NULL)
2796                                pci_unmap_single(mgp->pdev,
2797                                                 dma_unmap_addr(&tx->info[idx],
2798                                                                bus), len,
2799                                                 PCI_DMA_TODEVICE);
2800                        else
2801                                pci_unmap_page(mgp->pdev,
2802                                               dma_unmap_addr(&tx->info[idx],
2803                                                              bus), len,
2804                                               PCI_DMA_TODEVICE);
2805                        dma_unmap_len_set(&tx->info[idx], len, 0);
2806                        tx->info[idx].skb = NULL;
2807                }
2808                idx = (idx + 1) & tx->mask;
2809        } while (idx != last_idx);
2810}
2811
2812/*
2813 * Transmit a packet.  We need to split the packet so that a single
2814 * segment does not cross myri10ge->tx_boundary, so this makes segment
2815 * counting tricky.  So rather than try to count segments up front, we
2816 * just give up if there are too few segments to hold a reasonably
2817 * fragmented packet currently available.  If we run
2818 * out of segments while preparing a packet for DMA, we just linearize
2819 * it and try again.
2820 */
2821
2822static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
2823                                       struct net_device *dev)
2824{
2825        struct myri10ge_priv *mgp = netdev_priv(dev);
2826        struct myri10ge_slice_state *ss;
2827        struct mcp_kreq_ether_send *req;
2828        struct myri10ge_tx_buf *tx;
2829        struct skb_frag_struct *frag;
2830        struct netdev_queue *netdev_queue;
2831        dma_addr_t bus;
2832        u32 low;
2833        __be32 high_swapped;
2834        unsigned int len;
2835        int idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
2836        u16 pseudo_hdr_offset, cksum_offset, queue;
2837        int cum_len, seglen, boundary, rdma_count;
2838        u8 flags, odd_flag;
2839
2840        queue = skb_get_queue_mapping(skb);
2841        ss = &mgp->ss[queue];
2842        netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
2843        tx = &ss->tx;
2844
2845again:
2846        req = tx->req_list;
2847        avail = tx->mask - 1 - (tx->req - tx->done);
2848
2849        mss = 0;
2850        max_segments = MXGEFW_MAX_SEND_DESC;
2851
2852        if (skb_is_gso(skb)) {
2853                mss = skb_shinfo(skb)->gso_size;
2854                max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
2855        }
2856
2857        if ((unlikely(avail < max_segments))) {
2858                /* we are out of transmit resources */
2859                tx->stop_queue++;
2860                netif_tx_stop_queue(netdev_queue);
2861                return NETDEV_TX_BUSY;
2862        }
2863
2864        /* Setup checksum offloading, if needed */
2865        cksum_offset = 0;
2866        pseudo_hdr_offset = 0;
2867        odd_flag = 0;
2868        flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
2869        if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2870                cksum_offset = skb_checksum_start_offset(skb);
2871                pseudo_hdr_offset = cksum_offset + skb->csum_offset;
2872                /* If the headers are excessively large, then we must
2873                 * fall back to a software checksum */
2874                if (unlikely(!mss && (cksum_offset > 255 ||
2875                                      pseudo_hdr_offset > 127))) {
2876                        if (skb_checksum_help(skb))
2877                                goto drop;
2878                        cksum_offset = 0;
2879                        pseudo_hdr_offset = 0;
2880                } else {
2881                        odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
2882                        flags |= MXGEFW_FLAGS_CKSUM;
2883                }
2884        }
2885
2886        cum_len = 0;
2887
2888        if (mss) {              /* TSO */
2889                /* this removes any CKSUM flag from before */
2890                flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
2891
2892                /* negative cum_len signifies to the
2893                 * send loop that we are still in the
2894                 * header portion of the TSO packet.
2895                 * TSO header can be at most 1KB long */
2896                cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
2897
2898                /* for IPv6 TSO, the checksum offset stores the
2899                 * TCP header length, to save the firmware from
2900                 * the need to parse the headers */
2901                if (skb_is_gso_v6(skb)) {
2902                        cksum_offset = tcp_hdrlen(skb);
2903                        /* Can only handle headers <= max_tso6 long */
2904                        if (unlikely(-cum_len > mgp->max_tso6))
2905                                return myri10ge_sw_tso(skb, dev);
2906                }
2907                /* for TSO, pseudo_hdr_offset holds mss.
2908                 * The firmware figures out where to put
2909                 * the checksum by parsing the header. */
2910                pseudo_hdr_offset = mss;
2911        } else
2912                /* Mark small packets, and pad out tiny packets */
2913        if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
2914                flags |= MXGEFW_FLAGS_SMALL;
2915
2916                /* pad frames to at least ETH_ZLEN bytes */
2917                if (unlikely(skb->len < ETH_ZLEN)) {
2918                        if (skb_padto(skb, ETH_ZLEN)) {
2919                                /* The packet is gone, so we must
2920                                 * return 0 */
2921                                ss->stats.tx_dropped += 1;
2922                                return NETDEV_TX_OK;
2923                        }
2924                        /* adjust the len to account for the zero pad
2925                         * so that the nic can know how long it is */
2926                        skb->len = ETH_ZLEN;
2927                }
2928        }
2929
2930        /* map the skb for DMA */
2931        len = skb_headlen(skb);
2932        bus = pci_map_single(mgp->pdev, skb->data, len, PCI_DMA_TODEVICE);
2933        if (unlikely(pci_dma_mapping_error(mgp->pdev, bus)))
2934                goto drop;
2935
2936        idx = tx->req & tx->mask;
2937        tx->info[idx].skb = skb;
2938        dma_unmap_addr_set(&tx->info[idx], bus, bus);
2939        dma_unmap_len_set(&tx->info[idx], len, len);
2940
2941        frag_cnt = skb_shinfo(skb)->nr_frags;
2942        frag_idx = 0;
2943        count = 0;
2944        rdma_count = 0;
2945
2946        /* "rdma_count" is the number of RDMAs belonging to the
2947         * current packet BEFORE the current send request. For
2948         * non-TSO packets, this is equal to "count".
2949         * For TSO packets, rdma_count needs to be reset
2950         * to 0 after a segment cut.
2951         *
2952         * The rdma_count field of the send request is
2953         * the number of RDMAs of the packet starting at
2954         * that request. For TSO send requests with one ore more cuts
2955         * in the middle, this is the number of RDMAs starting
2956         * after the last cut in the request. All previous
2957         * segments before the last cut implicitly have 1 RDMA.
2958         *
2959         * Since the number of RDMAs is not known beforehand,
2960         * it must be filled-in retroactively - after each
2961         * segmentation cut or at the end of the entire packet.
2962         */
2963
2964        while (1) {
2965                /* Break the SKB or Fragment up into pieces which
2966                 * do not cross mgp->tx_boundary */
2967                low = MYRI10GE_LOWPART_TO_U32(bus);
2968                high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
2969                while (len) {
2970                        u8 flags_next;
2971                        int cum_len_next;
2972
2973                        if (unlikely(count == max_segments))
2974                                goto abort_linearize;
2975
2976                        boundary =
2977                            (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
2978                        seglen = boundary - low;
2979                        if (seglen > len)
2980                                seglen = len;
2981                        flags_next = flags & ~MXGEFW_FLAGS_FIRST;
2982                        cum_len_next = cum_len + seglen;
2983                        if (mss) {      /* TSO */
2984                                (req - rdma_count)->rdma_count = rdma_count + 1;
2985
2986                                if (likely(cum_len >= 0)) {     /* payload */
2987                                        int next_is_first, chop;
2988
2989                                        chop = (cum_len_next > mss);
2990                                        cum_len_next = cum_len_next % mss;
2991                                        next_is_first = (cum_len_next == 0);
2992                                        flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
2993                                        flags_next |= next_is_first *
2994                                            MXGEFW_FLAGS_FIRST;
2995                                        rdma_count |= -(chop | next_is_first);
2996                                        rdma_count += chop & ~next_is_first;
2997                                } else if (likely(cum_len_next >= 0)) { /* header ends */
2998                                        int small;
2999
3000                                        rdma_count = -1;
3001                                        cum_len_next = 0;
3002                                        seglen = -cum_len;
3003                                        small = (mss <= MXGEFW_SEND_SMALL_SIZE);
3004                                        flags_next = MXGEFW_FLAGS_TSO_PLD |
3005                                            MXGEFW_FLAGS_FIRST |
3006                                            (small * MXGEFW_FLAGS_SMALL);
3007                                }
3008                        }
3009                        req->addr_high = high_swapped;
3010                        req->addr_low = htonl(low);
3011                        req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
3012                        req->pad = 0;   /* complete solid 16-byte block; does this matter? */
3013                        req->rdma_count = 1;
3014                        req->length = htons(seglen);
3015                        req->cksum_offset = cksum_offset;
3016                        req->flags = flags | ((cum_len & 1) * odd_flag);
3017
3018                        low += seglen;
3019                        len -= seglen;
3020                        cum_len = cum_len_next;
3021                        flags = flags_next;
3022                        req++;
3023                        count++;
3024                        rdma_count++;
3025                        if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
3026                                if (unlikely(cksum_offset > seglen))
3027                                        cksum_offset -= seglen;
3028                                else
3029                                        cksum_offset = 0;
3030                        }
3031                }
3032                if (frag_idx == frag_cnt)
3033                        break;
3034
3035                /* map next fragment for DMA */
3036                frag = &skb_shinfo(skb)->frags[frag_idx];
3037                frag_idx++;
3038                len = skb_frag_size(frag);
3039                bus = skb_frag_dma_map(&mgp->pdev->dev, frag, 0, len,
3040                                       DMA_TO_DEVICE);
3041                if (unlikely(pci_dma_mapping_error(mgp->pdev, bus))) {
3042                        myri10ge_unmap_tx_dma(mgp, tx, idx);
3043                        goto drop;
3044                }
3045                idx = (count + tx->req) & tx->mask;
3046                dma_unmap_addr_set(&tx->info[idx], bus, bus);
3047                dma_unmap_len_set(&tx->info[idx], len, len);
3048        }
3049
3050        (req - rdma_count)->rdma_count = rdma_count;
3051        if (mss)
3052                do {
3053                        req--;
3054                        req->flags |= MXGEFW_FLAGS_TSO_LAST;
3055                } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
3056                                         MXGEFW_FLAGS_FIRST)));
3057        idx = ((count - 1) + tx->req) & tx->mask;
3058        tx->info[idx].last = 1;
3059        myri10ge_submit_req(tx, tx->req_list, count);
3060        /* if using multiple tx queues, make sure NIC polls the
3061         * current slice */
3062        if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
3063                tx->queue_active = 1;
3064                put_be32(htonl(1), tx->send_go);
3065                mb();
3066                mmiowb();
3067        }
3068        tx->pkt_start++;
3069        if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
3070                tx->stop_queue++;
3071                netif_tx_stop_queue(netdev_queue);
3072        }
3073        return NETDEV_TX_OK;
3074
3075abort_linearize:
3076        myri10ge_unmap_tx_dma(mgp, tx, idx);
3077
3078        if (skb_is_gso(skb)) {
3079                netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
3080                goto drop;
3081        }
3082
3083        if (skb_linearize(skb))
3084                goto drop;
3085
3086        tx->linearized++;
3087        goto again;
3088
3089drop:
3090        dev_kfree_skb_any(skb);
3091        ss->stats.tx_dropped += 1;
3092        return NETDEV_TX_OK;
3093
3094}
3095
3096static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
3097                                         struct net_device *dev)
3098{
3099        struct sk_buff *segs, *curr;
3100        struct myri10ge_priv *mgp = netdev_priv(dev);
3101        struct myri10ge_slice_state *ss;
3102        netdev_tx_t status;
3103
3104        segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
3105        if (IS_ERR(segs))
3106                goto drop;
3107
3108        while (segs) {
3109                curr = segs;
3110                segs = segs->next;
3111                curr->next = NULL;
3112                status = myri10ge_xmit(curr, dev);
3113                if (status != 0) {
3114                        dev_kfree_skb_any(curr);
3115                        if (segs != NULL) {
3116                                curr = segs;
3117                                segs = segs->next;
3118                                curr->next = NULL;
3119                                dev_kfree_skb_any(segs);
3120                        }
3121                        goto drop;
3122                }
3123        }
3124        dev_kfree_skb_any(skb);
3125        return NETDEV_TX_OK;
3126
3127drop:
3128        ss = &mgp->ss[skb_get_queue_mapping(skb)];
3129        dev_kfree_skb_any(skb);
3130        ss->stats.tx_dropped += 1;
3131        return NETDEV_TX_OK;
3132}
3133
3134static void myri10ge_get_stats(struct net_device *dev,
3135                               struct rtnl_link_stats64 *stats)
3136{
3137        const struct myri10ge_priv *mgp = netdev_priv(dev);
3138        const struct myri10ge_slice_netstats *slice_stats;
3139        int i;
3140
3141        for (i = 0; i < mgp->num_slices; i++) {
3142                slice_stats = &mgp->ss[i].stats;
3143                stats->rx_packets += slice_stats->rx_packets;
3144                stats->tx_packets += slice_stats->tx_packets;
3145                stats->rx_bytes += slice_stats->rx_bytes;
3146                stats->tx_bytes += slice_stats->tx_bytes;
3147                stats->rx_dropped += slice_stats->rx_dropped;
3148                stats->tx_dropped += slice_stats->tx_dropped;
3149        }
3150}
3151
3152static void myri10ge_set_multicast_list(struct net_device *dev)
3153{
3154        struct myri10ge_priv *mgp = netdev_priv(dev);
3155        struct myri10ge_cmd cmd;
3156        struct netdev_hw_addr *ha;
3157        __be32 data[2] = { 0, 0 };
3158        int err;
3159
3160        /* can be called from atomic contexts,
3161         * pass 1 to force atomicity in myri10ge_send_cmd() */
3162        myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
3163
3164        /* This firmware is known to not support multicast */
3165        if (!mgp->fw_multicast_support)
3166                return;
3167
3168        /* Disable multicast filtering */
3169
3170        err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
3171        if (err != 0) {
3172                netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
3173                           err);
3174                goto abort;
3175        }
3176
3177        if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
3178                /* request to disable multicast filtering, so quit here */
3179                return;
3180        }
3181
3182        /* Flush the filters */
3183
3184        err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
3185                                &cmd, 1);
3186        if (err != 0) {
3187                netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
3188                           err);
3189                goto abort;
3190        }
3191
3192        /* Walk the multicast list, and add each address */
3193        netdev_for_each_mc_addr(ha, dev) {
3194                memcpy(data, &ha->addr, ETH_ALEN);
3195                cmd.data0 = ntohl(data[0]);
3196                cmd.data1 = ntohl(data[1]);
3197                err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
3198                                        &cmd, 1);
3199
3200                if (err != 0) {
3201                        netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
3202                                   err, ha->addr);
3203                        goto abort;
3204                }
3205        }
3206        /* Enable multicast filtering */
3207        err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
3208        if (err != 0) {
3209                netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
3210                           err);
3211                goto abort;
3212        }
3213
3214        return;
3215
3216abort:
3217        return;
3218}
3219
3220static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
3221{
3222        struct sockaddr *sa = addr;
3223        struct myri10ge_priv *mgp = netdev_priv(dev);
3224        int status;
3225
3226        if (!is_valid_ether_addr(sa->sa_data))
3227                return -EADDRNOTAVAIL;
3228
3229        status = myri10ge_update_mac_address(mgp, sa->sa_data);
3230        if (status != 0) {
3231                netdev_err(dev, "changing mac address failed with %d\n",
3232                           status);
3233                return status;
3234        }
3235
3236        /* change the dev structure */
3237        memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
3238        return 0;
3239}
3240
3241static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
3242{
3243        struct myri10ge_priv *mgp = netdev_priv(dev);
3244        int error = 0;
3245
3246        if ((new_mtu < 68) || (ETH_HLEN + new_mtu > MYRI10GE_MAX_ETHER_MTU)) {
3247                netdev_err(dev, "new mtu (%d) is not valid\n", new_mtu);
3248                return -EINVAL;
3249        }
3250        netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
3251        if (mgp->running) {
3252                /* if we change the mtu on an active device, we must
3253                 * reset the device so the firmware sees the change */
3254                myri10ge_close(dev);
3255                dev->mtu = new_mtu;
3256                myri10ge_open(dev);
3257        } else
3258                dev->mtu = new_mtu;
3259
3260        return error;
3261}
3262
3263/*
3264 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
3265 * Only do it if the bridge is a root port since we don't want to disturb
3266 * any other device, except if forced with myri10ge_ecrc_enable > 1.
3267 */
3268
3269static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
3270{
3271        struct pci_dev *bridge = mgp->pdev->bus->self;
3272        struct device *dev = &mgp->pdev->dev;
3273        int cap;
3274        unsigned err_cap;
3275        int ret;
3276
3277        if (!myri10ge_ecrc_enable || !bridge)
3278                return;
3279
3280        /* check that the bridge is a root port */
3281        if (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT) {
3282                if (myri10ge_ecrc_enable > 1) {
3283                        struct pci_dev *prev_bridge, *old_bridge = bridge;
3284
3285                        /* Walk the hierarchy up to the root port
3286                         * where ECRC has to be enabled */
3287                        do {
3288                                prev_bridge = bridge;
3289                                bridge = bridge->bus->self;
3290                                if (!bridge || prev_bridge == bridge) {
3291                                        dev_err(dev,
3292                                                "Failed to find root port"
3293                                                " to force ECRC\n");
3294                                        return;
3295                                }
3296                        } while (pci_pcie_type(bridge) !=
3297                                 PCI_EXP_TYPE_ROOT_PORT);
3298
3299                        dev_info(dev,
3300                                 "Forcing ECRC on non-root port %s"
3301                                 " (enabling on root port %s)\n",
3302                                 pci_name(old_bridge), pci_name(bridge));
3303                } else {
3304                        dev_err(dev,
3305                                "Not enabling ECRC on non-root port %s\n",
3306                                pci_name(bridge));
3307                        return;
3308                }
3309        }
3310
3311        cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
3312        if (!cap)
3313                return;
3314
3315        ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
3316        if (ret) {
3317                dev_err(dev, "failed reading ext-conf-space of %s\n",
3318                        pci_name(bridge));
3319                dev_err(dev, "\t pci=nommconf in use? "
3320                        "or buggy/incomplete/absent ACPI MCFG attr?\n");
3321                return;
3322        }
3323        if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
3324                return;
3325
3326        err_cap |= PCI_ERR_CAP_ECRC_GENE;
3327        pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
3328        dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
3329}
3330
3331/*
3332 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
3333 * when the PCI-E Completion packets are aligned on an 8-byte
3334 * boundary.  Some PCI-E chip sets always align Completion packets; on
3335 * the ones that do not, the alignment can be enforced by enabling
3336 * ECRC generation (if supported).
3337 *
3338 * When PCI-E Completion packets are not aligned, it is actually more
3339 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
3340 *
3341 * If the driver can neither enable ECRC nor verify that it has
3342 * already been enabled, then it must use a firmware image which works
3343 * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
3344 * should also ensure that it never gives the device a Read-DMA which is
3345 * larger than 2KB by setting the tx_boundary to 2KB.  If ECRC is
3346 * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
3347 * firmware image, and set tx_boundary to 4KB.
3348 */
3349
3350static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
3351{
3352        struct pci_dev *pdev = mgp->pdev;
3353        struct device *dev = &pdev->dev;
3354        int status;
3355
3356        mgp->tx_boundary = 4096;
3357        /*
3358         * Verify the max read request size was set to 4KB
3359         * before trying the test with 4KB.
3360         */
3361        status = pcie_get_readrq(pdev);
3362        if (status < 0) {
3363                dev_err(dev, "Couldn't read max read req size: %d\n", status);
3364                goto abort;
3365        }
3366        if (status != 4096) {
3367                dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
3368                mgp->tx_boundary = 2048;
3369        }
3370        /*
3371         * load the optimized firmware (which assumes aligned PCIe
3372         * completions) in order to see if it works on this host.
3373         */
3374        set_fw_name(mgp, myri10ge_fw_aligned, false);
3375        status = myri10ge_load_firmware(mgp, 1);
3376        if (status != 0) {
3377                goto abort;
3378        }
3379
3380        /*
3381         * Enable ECRC if possible
3382         */
3383        myri10ge_enable_ecrc(mgp);
3384
3385        /*
3386         * Run a DMA test which watches for unaligned completions and
3387         * aborts on the first one seen.
3388         */
3389
3390        status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
3391        if (status == 0)
3392                return;         /* keep the aligned firmware */
3393
3394        if (status != -E2BIG)
3395                dev_warn(dev, "DMA test failed: %d\n", status);
3396        if (status == -ENOSYS)
3397                dev_warn(dev, "Falling back to ethp! "
3398                         "Please install up to date fw\n");
3399abort:
3400        /* fall back to using the unaligned firmware */
3401        mgp->tx_boundary = 2048;
3402        set_fw_name(mgp, myri10ge_fw_unaligned, false);
3403}
3404
3405static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
3406{
3407        int overridden = 0;
3408
3409        if (myri10ge_force_firmware == 0) {
3410                int link_width;
3411                u16 lnk;
3412
3413                pcie_capability_read_word(mgp->pdev, PCI_EXP_LNKSTA, &lnk);
3414                link_width = (lnk >> 4) & 0x3f;
3415
3416                /* Check to see if Link is less than 8 or if the
3417                 * upstream bridge is known to provide aligned
3418                 * completions */
3419                if (link_width < 8) {
3420                        dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
3421                                 link_width);
3422                        mgp->tx_boundary = 4096;
3423                        set_fw_name(mgp, myri10ge_fw_aligned, false);
3424                } else {
3425                        myri10ge_firmware_probe(mgp);
3426                }
3427        } else {
3428                if (myri10ge_force_firmware == 1) {
3429                        dev_info(&mgp->pdev->dev,
3430                                 "Assuming aligned completions (forced)\n");
3431                        mgp->tx_boundary = 4096;
3432                        set_fw_name(mgp, myri10ge_fw_aligned, false);
3433                } else {
3434                        dev_info(&mgp->pdev->dev,
3435                                 "Assuming unaligned completions (forced)\n");
3436                        mgp->tx_boundary = 2048;
3437                        set_fw_name(mgp, myri10ge_fw_unaligned, false);
3438                }
3439        }
3440
3441        kparam_block_sysfs_write(myri10ge_fw_name);
3442        if (myri10ge_fw_name != NULL) {
3443                char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
3444                if (fw_name) {
3445                        overridden = 1;
3446                        set_fw_name(mgp, fw_name, true);
3447                }
3448        }
3449        kparam_unblock_sysfs_write(myri10ge_fw_name);
3450
3451        if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
3452            myri10ge_fw_names[mgp->board_number] != NULL &&
3453            strlen(myri10ge_fw_names[mgp->board_number])) {
3454                set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
3455                overridden = 1;
3456        }
3457        if (overridden)
3458                dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
3459                         mgp->fw_name);
3460}
3461
3462static void myri10ge_mask_surprise_down(struct pci_dev *pdev)
3463{
3464        struct pci_dev *bridge = pdev->bus->self;
3465        int cap;
3466        u32 mask;
3467
3468        if (bridge == NULL)
3469                return;
3470
3471        cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
3472        if (cap) {
3473                /* a sram parity error can cause a surprise link
3474                 * down; since we expect and can recover from sram
3475                 * parity errors, mask surprise link down events */
3476                pci_read_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, &mask);
3477                mask |= 0x20;
3478                pci_write_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, mask);
3479        }
3480}
3481
3482#ifdef CONFIG_PM
3483static int myri10ge_suspend(struct pci_dev *pdev, pm_message_t state)
3484{
3485        struct myri10ge_priv *mgp;
3486        struct net_device *netdev;
3487
3488        mgp = pci_get_drvdata(pdev);
3489        if (mgp == NULL)
3490                return -EINVAL;
3491        netdev = mgp->dev;
3492
3493        netif_device_detach(netdev);
3494        if (netif_running(netdev)) {
3495                netdev_info(netdev, "closing\n");
3496                rtnl_lock();
3497                myri10ge_close(netdev);
3498                rtnl_unlock();
3499        }
3500        myri10ge_dummy_rdma(mgp, 0);
3501        pci_save_state(pdev);
3502        pci_disable_device(pdev);
3503
3504        return pci_set_power_state(pdev, pci_choose_state(pdev, state));
3505}
3506
3507static int myri10ge_resume(struct pci_dev *pdev)
3508{
3509        struct myri10ge_priv *mgp;
3510        struct net_device *netdev;
3511        int status;
3512        u16 vendor;
3513
3514        mgp = pci_get_drvdata(pdev);
3515        if (mgp == NULL)
3516                return -EINVAL;
3517        netdev = mgp->dev;
3518        pci_set_power_state(pdev, 0);   /* zeros conf space as a side effect */
3519        msleep(5);              /* give card time to respond */
3520        pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3521        if (vendor == 0xffff) {
3522                netdev_err(mgp->dev, "device disappeared!\n");
3523                return -EIO;
3524        }
3525
3526        pci_restore_state(pdev);
3527
3528        status = pci_enable_device(pdev);
3529        if (status) {
3530                dev_err(&pdev->dev, "failed to enable device\n");
3531                return status;
3532        }
3533
3534        pci_set_master(pdev);
3535
3536        myri10ge_reset(mgp);
3537        myri10ge_dummy_rdma(mgp, 1);
3538
3539        /* Save configuration space to be restored if the
3540         * nic resets due to a parity error */
3541        pci_save_state(pdev);
3542
3543        if (netif_running(netdev)) {
3544                rtnl_lock();
3545                status = myri10ge_open(netdev);
3546                rtnl_unlock();
3547                if (status != 0)
3548                        goto abort_with_enabled;
3549
3550        }
3551        netif_device_attach(netdev);
3552
3553        return 0;
3554
3555abort_with_enabled:
3556        pci_disable_device(pdev);
3557        return -EIO;
3558
3559}
3560#endif                          /* CONFIG_PM */
3561
3562static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
3563{
3564        struct pci_dev *pdev = mgp->pdev;
3565        int vs = mgp->vendor_specific_offset;
3566        u32 reboot;
3567
3568        /*enter read32 mode */
3569        pci_write_config_byte(pdev, vs + 0x10, 0x3);
3570
3571        /*read REBOOT_STATUS (0xfffffff0) */
3572        pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
3573        pci_read_config_dword(pdev, vs + 0x14, &reboot);
3574        return reboot;
3575}
3576
3577static void
3578myri10ge_check_slice(struct myri10ge_slice_state *ss, int *reset_needed,
3579                     int *busy_slice_cnt, u32 rx_pause_cnt)
3580{
3581        struct myri10ge_priv *mgp = ss->mgp;
3582        int slice = ss - mgp->ss;
3583
3584        if (ss->tx.req != ss->tx.done &&
3585            ss->tx.done == ss->watchdog_tx_done &&
3586            ss->watchdog_tx_req != ss->watchdog_tx_done) {
3587                /* nic seems like it might be stuck.. */
3588                if (rx_pause_cnt != mgp->watchdog_pause) {
3589                        if (net_ratelimit())
3590                                netdev_warn(mgp->dev, "slice %d: TX paused, "
3591                                            "check link partner\n", slice);
3592                } else {
3593                        netdev_warn(mgp->dev,
3594                                    "slice %d: TX stuck %d %d %d %d %d %d\n",
3595                                    slice, ss->tx.queue_active, ss->tx.req,
3596                                    ss->tx.done, ss->tx.pkt_start,
3597                                    ss->tx.pkt_done,
3598                                    (int)ntohl(mgp->ss[slice].fw_stats->
3599                                               send_done_count));
3600                        *reset_needed = 1;
3601                        ss->stuck = 1;
3602                }
3603        }
3604        if (ss->watchdog_tx_done != ss->tx.done ||
3605            ss->watchdog_rx_done != ss->rx_done.cnt) {
3606                *busy_slice_cnt += 1;
3607        }
3608        ss->watchdog_tx_done = ss->tx.done;
3609        ss->watchdog_tx_req = ss->tx.req;
3610        ss->watchdog_rx_done = ss->rx_done.cnt;
3611}
3612
3613/*
3614 * This watchdog is used to check whether the board has suffered
3615 * from a parity error and needs to be recovered.
3616 */
3617static void myri10ge_watchdog(struct work_struct *work)
3618{
3619        struct myri10ge_priv *mgp =
3620            container_of(work, struct myri10ge_priv, watchdog_work);
3621        struct myri10ge_slice_state *ss;
3622        u32 reboot, rx_pause_cnt;
3623        int status, rebooted;
3624        int i;
3625        int reset_needed = 0;
3626        int busy_slice_cnt = 0;
3627        u16 cmd, vendor;
3628
3629        mgp->watchdog_resets++;
3630        pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
3631        rebooted = 0;
3632        if ((cmd & PCI_COMMAND_MASTER) == 0) {
3633                /* Bus master DMA disabled?  Check to see
3634                 * if the card rebooted due to a parity error
3635                 * For now, just report it */
3636                reboot = myri10ge_read_reboot(mgp);
3637                netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
3638                           reboot, myri10ge_reset_recover ? "" : " not");
3639                if (myri10ge_reset_recover == 0)
3640                        return;
3641                rtnl_lock();
3642                mgp->rebooted = 1;
3643                rebooted = 1;
3644                myri10ge_close(mgp->dev);
3645                myri10ge_reset_recover--;
3646                mgp->rebooted = 0;
3647                /*
3648                 * A rebooted nic will come back with config space as
3649                 * it was after power was applied to PCIe bus.
3650                 * Attempt to restore config space which was saved
3651                 * when the driver was loaded, or the last time the
3652                 * nic was resumed from power saving mode.
3653                 */
3654                pci_restore_state(mgp->pdev);
3655
3656                /* save state again for accounting reasons */
3657                pci_save_state(mgp->pdev);
3658
3659        } else {
3660                /* if we get back -1's from our slot, perhaps somebody
3661                 * powered off our card.  Don't try to reset it in
3662                 * this case */
3663                if (cmd == 0xffff) {
3664                        pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3665                        if (vendor == 0xffff) {
3666                                netdev_err(mgp->dev, "device disappeared!\n");
3667                                return;
3668                        }
3669                }
3670                /* Perhaps it is a software error. See if stuck slice
3671                 * has recovered, reset if not */
3672                rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
3673                for (i = 0; i < mgp->num_slices; i++) {
3674                        ss = mgp->ss;
3675                        if (ss->stuck) {
3676                                myri10ge_check_slice(ss, &reset_needed,
3677                                                     &busy_slice_cnt,
3678                                                     rx_pause_cnt);
3679                                ss->stuck = 0;
3680                        }
3681                }
3682                if (!reset_needed) {
3683                        netdev_dbg(mgp->dev, "not resetting\n");
3684                        return;
3685                }
3686
3687                netdev_err(mgp->dev, "device timeout, resetting\n");
3688        }
3689
3690        if (!rebooted) {
3691                rtnl_lock();
3692                myri10ge_close(mgp->dev);
3693        }
3694        status = myri10ge_load_firmware(mgp, 1);
3695        if (status != 0)
3696                netdev_err(mgp->dev, "failed to load firmware\n");
3697        else
3698                myri10ge_open(mgp->dev);
3699        rtnl_unlock();
3700}
3701
3702/*
3703 * We use our own timer routine rather than relying upon
3704 * netdev->tx_timeout because we have a very large hardware transmit
3705 * queue.  Due to the large queue, the netdev->tx_timeout function
3706 * cannot detect a NIC with a parity error in a timely fashion if the
3707 * NIC is lightly loaded.
3708 */
3709static void myri10ge_watchdog_timer(unsigned long arg)
3710{
3711        struct myri10ge_priv *mgp;
3712        struct myri10ge_slice_state *ss;
3713        int i, reset_needed, busy_slice_cnt;
3714        u32 rx_pause_cnt;
3715        u16 cmd;
3716
3717        mgp = (struct myri10ge_priv *)arg;
3718
3719        rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
3720        busy_slice_cnt = 0;
3721        for (i = 0, reset_needed = 0;
3722             i < mgp->num_slices && reset_needed == 0; ++i) {
3723
3724                ss = &mgp->ss[i];
3725                if (ss->rx_small.watchdog_needed) {
3726                        myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
3727                                                mgp->small_bytes + MXGEFW_PAD,
3728                                                1);
3729                        if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
3730                            myri10ge_fill_thresh)
3731                                ss->rx_small.watchdog_needed = 0;
3732                }
3733                if (ss->rx_big.watchdog_needed) {
3734                        myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
3735                                                mgp->big_bytes, 1);
3736                        if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
3737                            myri10ge_fill_thresh)
3738                                ss->rx_big.watchdog_needed = 0;
3739                }
3740                myri10ge_check_slice(ss, &reset_needed, &busy_slice_cnt,
3741                                     rx_pause_cnt);
3742        }
3743        /* if we've sent or received no traffic, poll the NIC to
3744         * ensure it is still there.  Otherwise, we risk not noticing
3745         * an error in a timely fashion */
3746        if (busy_slice_cnt == 0) {
3747                pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
3748                if ((cmd & PCI_COMMAND_MASTER) == 0) {
3749                        reset_needed = 1;
3750                }
3751        }
3752        mgp->watchdog_pause = rx_pause_cnt;
3753
3754        if (reset_needed) {
3755                schedule_work(&mgp->watchdog_work);
3756        } else {
3757                /* rearm timer */
3758                mod_timer(&mgp->watchdog_timer,
3759                          jiffies + myri10ge_watchdog_timeout * HZ);
3760        }
3761}
3762
3763static void myri10ge_free_slices(struct myri10ge_priv *mgp)
3764{
3765        struct myri10ge_slice_state *ss;
3766        struct pci_dev *pdev = mgp->pdev;
3767        size_t bytes;
3768        int i;
3769
3770        if (mgp->ss == NULL)
3771                return;
3772
3773        for (i = 0; i < mgp->num_slices; i++) {
3774                ss = &mgp->ss[i];
3775                if (ss->rx_done.entry != NULL) {
3776                        bytes = mgp->max_intr_slots *
3777                            sizeof(*ss->rx_done.entry);
3778                        dma_free_coherent(&pdev->dev, bytes,
3779                                          ss->rx_done.entry, ss->rx_done.bus);
3780                        ss->rx_done.entry = NULL;
3781                }
3782                if (ss->fw_stats != NULL) {
3783                        bytes = sizeof(*ss->fw_stats);
3784                        dma_free_coherent(&pdev->dev, bytes,
3785                                          ss->fw_stats, ss->fw_stats_bus);
3786                        ss->fw_stats = NULL;
3787                }
3788                napi_hash_del(&ss->napi);
3789                netif_napi_del(&ss->napi);
3790        }
3791        /* Wait till napi structs are no longer used, and then free ss. */
3792        synchronize_rcu();
3793        kfree(mgp->ss);
3794        mgp->ss = NULL;
3795}
3796
3797static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
3798{
3799        struct myri10ge_slice_state *ss;
3800        struct pci_dev *pdev = mgp->pdev;
3801        size_t bytes;
3802        int i;
3803
3804        bytes = sizeof(*mgp->ss) * mgp->num_slices;
3805        mgp->ss = kzalloc(bytes, GFP_KERNEL);
3806        if (mgp->ss == NULL) {
3807                return -ENOMEM;
3808        }
3809
3810        for (i = 0; i < mgp->num_slices; i++) {
3811                ss = &mgp->ss[i];
3812                bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
3813                ss->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
3814                                                       &ss->rx_done.bus,
3815                                                       GFP_KERNEL | __GFP_ZERO);
3816                if (ss->rx_done.entry == NULL)
3817                        goto abort;
3818                bytes = sizeof(*ss->fw_stats);
3819                ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
3820                                                  &ss->fw_stats_bus,
3821                                                  GFP_KERNEL);
3822                if (ss->fw_stats == NULL)
3823                        goto abort;
3824                ss->mgp = mgp;
3825                ss->dev = mgp->dev;
3826                netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
3827                               myri10ge_napi_weight);
3828        }
3829        return 0;
3830abort:
3831        myri10ge_free_slices(mgp);
3832        return -ENOMEM;
3833}
3834
3835/*
3836 * This function determines the number of slices supported.
3837 * The number slices is the minimum of the number of CPUS,
3838 * the number of MSI-X irqs supported, the number of slices
3839 * supported by the firmware
3840 */
3841static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
3842{
3843        struct myri10ge_cmd cmd;
3844        struct pci_dev *pdev = mgp->pdev;
3845        char *old_fw;
3846        bool old_allocated;
3847        int i, status, ncpus, msix_cap;
3848
3849        mgp->num_slices = 1;
3850        msix_cap = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3851        ncpus = netif_get_num_default_rss_queues();
3852
3853        if (myri10ge_max_slices == 1 || msix_cap == 0 ||
3854            (myri10ge_max_slices == -1 && ncpus < 2))
3855                return;
3856
3857        /* try to load the slice aware rss firmware */
3858        old_fw = mgp->fw_name;
3859        old_allocated = mgp->fw_name_allocated;
3860        /* don't free old_fw if we override it. */
3861        mgp->fw_name_allocated = false;
3862
3863        if (myri10ge_fw_name != NULL) {
3864                dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
3865                         myri10ge_fw_name);
3866                set_fw_name(mgp, myri10ge_fw_name, false);
3867        } else if (old_fw == myri10ge_fw_aligned)
3868                set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
3869        else
3870                set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
3871        status = myri10ge_load_firmware(mgp, 0);
3872        if (status != 0) {
3873                dev_info(&pdev->dev, "Rss firmware not found\n");
3874                if (old_allocated)
3875                        kfree(old_fw);
3876                return;
3877        }
3878
3879        /* hit the board with a reset to ensure it is alive */
3880        memset(&cmd, 0, sizeof(cmd));
3881        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
3882        if (status != 0) {
3883                dev_err(&mgp->pdev->dev, "failed reset\n");
3884                goto abort_with_fw;
3885        }
3886
3887        mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
3888
3889        /* tell it the size of the interrupt queues */
3890        cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
3891        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
3892        if (status != 0) {
3893                dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
3894                goto abort_with_fw;
3895        }
3896
3897        /* ask the maximum number of slices it supports */
3898        status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
3899        if (status != 0)
3900                goto abort_with_fw;
3901        else
3902                mgp->num_slices = cmd.data0;
3903
3904        /* Only allow multiple slices if MSI-X is usable */
3905        if (!myri10ge_msi) {
3906                goto abort_with_fw;
3907        }
3908
3909        /* if the admin did not specify a limit to how many
3910         * slices we should use, cap it automatically to the
3911         * number of CPUs currently online */
3912        if (myri10ge_max_slices == -1)
3913                myri10ge_max_slices = ncpus;
3914
3915        if (mgp->num_slices > myri10ge_max_slices)
3916                mgp->num_slices = myri10ge_max_slices;
3917
3918        /* Now try to allocate as many MSI-X vectors as we have
3919         * slices. We give up on MSI-X if we can only get a single
3920         * vector. */
3921
3922        mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
3923                                    GFP_KERNEL);
3924        if (mgp->msix_vectors == NULL)
3925                goto disable_msix;
3926        for (i = 0; i < mgp->num_slices; i++) {
3927                mgp->msix_vectors[i].entry = i;
3928        }
3929
3930        while (mgp->num_slices > 1) {
3931                /* make sure it is a power of two */
3932                while (!is_power_of_2(mgp->num_slices))
3933                        mgp->num_slices--;
3934                if (mgp->num_slices == 1)
3935                        goto disable_msix;
3936                status = pci_enable_msix(pdev, mgp->msix_vectors,
3937                                         mgp->num_slices);
3938                if (status == 0) {
3939                        pci_disable_msix(pdev);
3940                        if (old_allocated)
3941                                kfree(old_fw);
3942                        return;
3943                }
3944                if (status > 0)
3945                        mgp->num_slices = status;
3946                else
3947                        goto disable_msix;
3948        }
3949
3950disable_msix:
3951        if (mgp->msix_vectors != NULL) {
3952                kfree(mgp->msix_vectors);
3953                mgp->msix_vectors = NULL;
3954        }
3955
3956abort_with_fw:
3957        mgp->num_slices = 1;
3958        set_fw_name(mgp, old_fw, old_allocated);
3959        myri10ge_load_firmware(mgp, 0);
3960}
3961
3962static const struct net_device_ops myri10ge_netdev_ops = {
3963        .ndo_open               = myri10ge_open,
3964        .ndo_stop               = myri10ge_close,
3965        .ndo_start_xmit         = myri10ge_xmit,
3966        .ndo_get_stats64        = myri10ge_get_stats,
3967        .ndo_validate_addr      = eth_validate_addr,
3968        .ndo_change_mtu_rh74    = myri10ge_change_mtu,
3969        .ndo_set_rx_mode        = myri10ge_set_multicast_list,
3970        .ndo_set_mac_address    = myri10ge_set_mac_address,
3971#ifdef CONFIG_NET_RX_BUSY_POLL
3972        .ndo_busy_poll          = myri10ge_busy_poll,
3973#endif
3974};
3975
3976static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3977{
3978        struct net_device *netdev;
3979        struct myri10ge_priv *mgp;
3980        struct device *dev = &pdev->dev;
3981        int i;
3982        int status = -ENXIO;
3983        int dac_enabled;
3984        unsigned hdr_offset, ss_offset;
3985        static int board_number;
3986
3987        netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
3988        if (netdev == NULL)
3989                return -ENOMEM;
3990
3991        SET_NETDEV_DEV(netdev, &pdev->dev);
3992
3993        mgp = netdev_priv(netdev);
3994        mgp->dev = netdev;
3995        mgp->pdev = pdev;
3996        mgp->pause = myri10ge_flow_control;
3997        mgp->intr_coal_delay = myri10ge_intr_coal_delay;
3998        mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
3999        mgp->board_number = board_number;
4000        init_waitqueue_head(&mgp->down_wq);
4001
4002        if (pci_enable_device(pdev)) {
4003                dev_err(&pdev->dev, "pci_enable_device call failed\n");
4004                status = -ENODEV;
4005                goto abort_with_netdev;
4006        }
4007
4008        /* Find the vendor-specific cap so we can check
4009         * the reboot register later on */
4010        mgp->vendor_specific_offset
4011            = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
4012
4013        /* Set our max read request to 4KB */
4014        status = pcie_set_readrq(pdev, 4096);
4015        if (status != 0) {
4016                dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
4017                        status);
4018                goto abort_with_enabled;
4019        }
4020
4021        myri10ge_mask_surprise_down(pdev);
4022        pci_set_master(pdev);
4023        dac_enabled = 1;
4024        status = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
4025        if (status != 0) {
4026                dac_enabled = 0;
4027                dev_err(&pdev->dev,
4028                        "64-bit pci address mask was refused, "
4029                        "trying 32-bit\n");
4030                status = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
4031        }
4032        if (status != 0) {
4033                dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
4034                goto abort_with_enabled;
4035        }
4036        (void)pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4037        mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
4038                                      &mgp->cmd_bus, GFP_KERNEL);
4039        if (mgp->cmd == NULL)
4040                goto abort_with_enabled;
4041
4042        mgp->board_span = pci_resource_len(pdev, 0);
4043        mgp->iomem_base = pci_resource_start(pdev, 0);
4044        mgp->mtrr = -1;
4045        mgp->wc_enabled = 0;
4046#ifdef CONFIG_MTRR
4047        mgp->mtrr = mtrr_add(mgp->iomem_base, mgp->board_span,
4048                             MTRR_TYPE_WRCOMB, 1);
4049        if (mgp->mtrr >= 0)
4050                mgp->wc_enabled = 1;
4051#endif
4052        mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
4053        if (mgp->sram == NULL) {
4054                dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
4055                        mgp->board_span, mgp->iomem_base);
4056                status = -ENXIO;
4057                goto abort_with_mtrr;
4058        }
4059        hdr_offset =
4060            swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
4061        ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
4062        mgp->sram_size = swab32(readl(mgp->sram + ss_offset));
4063        if (mgp->sram_size > mgp->board_span ||
4064            mgp->sram_size <= MYRI10GE_FW_OFFSET) {
4065                dev_err(&pdev->dev,
4066                        "invalid sram_size %dB or board span %ldB\n",
4067                        mgp->sram_size, mgp->board_span);
4068                goto abort_with_ioremap;
4069        }
4070        memcpy_fromio(mgp->eeprom_strings,
4071                      mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
4072        memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
4073        status = myri10ge_read_mac_addr(mgp);
4074        if (status)
4075                goto abort_with_ioremap;
4076
4077        for (i = 0; i < ETH_ALEN; i++)
4078                netdev->dev_addr[i] = mgp->mac_addr[i];
4079
4080        myri10ge_select_firmware(mgp);
4081
4082        status = myri10ge_load_firmware(mgp, 1);
4083        if (status != 0) {
4084                dev_err(&pdev->dev, "failed to load firmware\n");
4085                goto abort_with_ioremap;
4086        }
4087        myri10ge_probe_slices(mgp);
4088        status = myri10ge_alloc_slices(mgp);
4089        if (status != 0) {
4090                dev_err(&pdev->dev, "failed to alloc slice state\n");
4091                goto abort_with_firmware;
4092        }
4093        netif_set_real_num_tx_queues(netdev, mgp->num_slices);
4094        netif_set_real_num_rx_queues(netdev, mgp->num_slices);
4095        status = myri10ge_reset(mgp);
4096        if (status != 0) {
4097                dev_err(&pdev->dev, "failed reset\n");
4098                goto abort_with_slices;
4099        }
4100#ifdef CONFIG_MYRI10GE_DCA
4101        myri10ge_setup_dca(mgp);
4102#endif
4103        pci_set_drvdata(pdev, mgp);
4104        if ((myri10ge_initial_mtu + ETH_HLEN) > MYRI10GE_MAX_ETHER_MTU)
4105                myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
4106        if ((myri10ge_initial_mtu + ETH_HLEN) < 68)
4107                myri10ge_initial_mtu = 68;
4108
4109        netdev->netdev_ops = &myri10ge_netdev_ops;
4110        netdev->mtu = myri10ge_initial_mtu;
4111        netdev->hw_features = mgp->features | NETIF_F_RXCSUM;
4112
4113        /* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
4114        netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4115
4116        netdev->features = netdev->hw_features;
4117
4118        if (dac_enabled)
4119                netdev->features |= NETIF_F_HIGHDMA;
4120
4121        netdev->vlan_features |= mgp->features;
4122        if (mgp->fw_ver_tiny < 37)
4123                netdev->vlan_features &= ~NETIF_F_TSO6;
4124        if (mgp->fw_ver_tiny < 32)
4125                netdev->vlan_features &= ~NETIF_F_TSO;
4126
4127        /* make sure we can get an irq, and that MSI can be
4128         * setup (if available). */
4129        status = myri10ge_request_irq(mgp);
4130        if (status != 0)
4131                goto abort_with_firmware;
4132        myri10ge_free_irq(mgp);
4133
4134        /* Save configuration space to be restored if the
4135         * nic resets due to a parity error */
4136        pci_save_state(pdev);
4137
4138        /* Setup the watchdog timer */
4139        setup_timer(&mgp->watchdog_timer, myri10ge_watchdog_timer,
4140                    (unsigned long)mgp);
4141
4142        SET_ETHTOOL_OPS(netdev, &myri10ge_ethtool_ops);
4143        INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
4144        status = register_netdev(netdev);
4145        if (status != 0) {
4146                dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
4147                goto abort_with_state;
4148        }
4149        if (mgp->msix_enabled)
4150                dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, WC %s\n",
4151                         mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
4152                         (mgp->wc_enabled ? "Enabled" : "Disabled"));
4153        else
4154                dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, WC %s\n",
4155                         mgp->msi_enabled ? "MSI" : "xPIC",
4156                         pdev->irq, mgp->tx_boundary, mgp->fw_name,
4157                         (mgp->wc_enabled ? "Enabled" : "Disabled"));
4158
4159        board_number++;
4160        return 0;
4161
4162abort_with_state:
4163        pci_restore_state(pdev);
4164
4165abort_with_slices:
4166        myri10ge_free_slices(mgp);
4167
4168abort_with_firmware:
4169        myri10ge_dummy_rdma(mgp, 0);
4170
4171abort_with_ioremap:
4172        if (mgp->mac_addr_string != NULL)
4173                dev_err(&pdev->dev,
4174                        "myri10ge_probe() failed: MAC=%s, SN=%ld\n",
4175                        mgp->mac_addr_string, mgp->serial_number);
4176        iounmap(mgp->sram);
4177
4178abort_with_mtrr:
4179#ifdef CONFIG_MTRR
4180        if (mgp->mtrr >= 0)
4181                mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
4182#endif
4183        dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
4184                          mgp->cmd, mgp->cmd_bus);
4185
4186abort_with_enabled:
4187        pci_disable_device(pdev);
4188
4189abort_with_netdev:
4190        set_fw_name(mgp, NULL, false);
4191        free_netdev(netdev);
4192        return status;
4193}
4194
4195/*
4196 * myri10ge_remove
4197 *
4198 * Does what is necessary to shutdown one Myrinet device. Called
4199 *   once for each Myrinet card by the kernel when a module is
4200 *   unloaded.
4201 */
4202static void myri10ge_remove(struct pci_dev *pdev)
4203{
4204        struct myri10ge_priv *mgp;
4205        struct net_device *netdev;
4206
4207        mgp = pci_get_drvdata(pdev);
4208        if (mgp == NULL)
4209                return;
4210
4211        cancel_work_sync(&mgp->watchdog_work);
4212        netdev = mgp->dev;
4213        unregister_netdev(netdev);
4214
4215#ifdef CONFIG_MYRI10GE_DCA
4216        myri10ge_teardown_dca(mgp);
4217#endif
4218        myri10ge_dummy_rdma(mgp, 0);
4219
4220        /* avoid a memory leak */
4221        pci_restore_state(pdev);
4222
4223        iounmap(mgp->sram);
4224
4225#ifdef CONFIG_MTRR
4226        if (mgp->mtrr >= 0)
4227                mtrr_del(mgp->mtrr, mgp->iomem_base, mgp->board_span);
4228#endif
4229        myri10ge_free_slices(mgp);
4230        if (mgp->msix_vectors != NULL)
4231                kfree(mgp->msix_vectors);
4232        dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
4233                          mgp->cmd, mgp->cmd_bus);
4234
4235        set_fw_name(mgp, NULL, false);
4236        free_netdev(netdev);
4237        pci_disable_device(pdev);
4238        pci_set_drvdata(pdev, NULL);
4239}
4240
4241#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E      0x0008
4242#define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9    0x0009
4243
4244static const struct pci_device_id myri10ge_pci_tbl[] = {
4245        {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
4246        {PCI_DEVICE
4247         (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
4248        {0},
4249};
4250
4251MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);
4252
4253static struct pci_driver myri10ge_driver = {
4254        .name = "myri10ge",
4255        .probe = myri10ge_probe,
4256        .remove = myri10ge_remove,
4257        .id_table = myri10ge_pci_tbl,
4258#ifdef CONFIG_PM
4259        .suspend = myri10ge_suspend,
4260        .resume = myri10ge_resume,
4261#endif
4262};
4263
4264#ifdef CONFIG_MYRI10GE_DCA
4265static int
4266myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
4267{
4268        int err = driver_for_each_device(&myri10ge_driver.driver,
4269                                         NULL, &event,
4270                                         myri10ge_notify_dca_device);
4271
4272        if (err)
4273                return NOTIFY_BAD;
4274        return NOTIFY_DONE;
4275}
4276
4277static struct notifier_block myri10ge_dca_notifier = {
4278        .notifier_call = myri10ge_notify_dca,
4279        .next = NULL,
4280        .priority = 0,
4281};
4282#endif                          /* CONFIG_MYRI10GE_DCA */
4283
4284static __init int myri10ge_init_module(void)
4285{
4286        pr_info("Version %s\n", MYRI10GE_VERSION_STR);
4287
4288        if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
4289                pr_err("Illegal rssh hash type %d, defaulting to source port\n",
4290                       myri10ge_rss_hash);
4291                myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
4292        }
4293#ifdef CONFIG_MYRI10GE_DCA
4294        dca_register_notify(&myri10ge_dca_notifier);
4295#endif
4296        if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
4297                myri10ge_max_slices = MYRI10GE_MAX_SLICES;
4298
4299        return pci_register_driver(&myri10ge_driver);
4300}
4301
4302module_init(myri10ge_init_module);
4303
4304static __exit void myri10ge_cleanup_module(void)
4305{
4306#ifdef CONFIG_MYRI10GE_DCA
4307        dca_unregister_notify(&myri10ge_dca_notifier);
4308#endif
4309        pci_unregister_driver(&myri10ge_driver);
4310}
4311
4312module_exit(myri10ge_cleanup_module);
4313