1 /*************************************************************************
2 * myri10ge.c: Myricom Myri-10G Ethernet driver.
4 * Copyright (C) 2005 - 2011 Myricom, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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
36 * Contact Information:
38 * Myricom, Inc., 325N Santa Anita Avenue, Arcadia, CA 91006
39 *************************************************************************/
41 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
55 #include <linux/inet.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>
65 #include <linux/log2.h>
66 #include <linux/slab.h>
67 #include <linux/prefetch.h>
68 #include <net/checksum.h>
71 #include <asm/byteorder.h>
72 #include <asm/processor.h>
74 #include "myri10ge_mcp.h"
75 #include "myri10ge_mcp_gen_header.h"
77 #define MYRI10GE_VERSION_STR "1.5.3-1.534"
79 MODULE_DESCRIPTION("Myricom 10G driver (10GbE)");
80 MODULE_AUTHOR("Maintainer: help@myri.com");
81 MODULE_VERSION(MYRI10GE_VERSION_STR);
82 MODULE_LICENSE("Dual BSD/GPL");
84 #define MYRI10GE_MAX_ETHER_MTU 9014
86 #define MYRI10GE_ETH_STOPPED 0
87 #define MYRI10GE_ETH_STOPPING 1
88 #define MYRI10GE_ETH_STARTING 2
89 #define MYRI10GE_ETH_RUNNING 3
90 #define MYRI10GE_ETH_OPEN_FAILED 4
92 #define MYRI10GE_EEPROM_STRINGS_SIZE 256
93 #define MYRI10GE_MAX_SEND_DESC_TSO ((65536 / 2048) * 2)
95 #define MYRI10GE_NO_CONFIRM_DATA htonl(0xffffffff)
96 #define MYRI10GE_NO_RESPONSE_RESULT 0xffffffff
98 #define MYRI10GE_ALLOC_ORDER 0
99 #define MYRI10GE_ALLOC_SIZE ((1 << MYRI10GE_ALLOC_ORDER) * PAGE_SIZE)
100 #define MYRI10GE_MAX_FRAGS_PER_FRAME (MYRI10GE_MAX_ETHER_MTU/MYRI10GE_ALLOC_SIZE + 1)
102 #define MYRI10GE_MAX_SLICES 32
104 struct myri10ge_rx_buffer_state {
107 DEFINE_DMA_UNMAP_ADDR(bus);
108 DEFINE_DMA_UNMAP_LEN(len);
111 struct myri10ge_tx_buffer_state {
114 DEFINE_DMA_UNMAP_ADDR(bus);
115 DEFINE_DMA_UNMAP_LEN(len);
118 struct myri10ge_cmd {
124 struct myri10ge_rx_buf {
125 struct mcp_kreq_ether_recv __iomem *lanai; /* lanai ptr for recv ring */
126 struct mcp_kreq_ether_recv *shadow; /* host shadow of recv ring */
127 struct myri10ge_rx_buffer_state *info;
134 int mask; /* number of rx slots -1 */
138 struct myri10ge_tx_buf {
139 struct mcp_kreq_ether_send __iomem *lanai; /* lanai ptr for sendq */
140 __be32 __iomem *send_go; /* "go" doorbell ptr */
141 __be32 __iomem *send_stop; /* "stop" doorbell ptr */
142 struct mcp_kreq_ether_send *req_list; /* host shadow of sendq */
144 struct myri10ge_tx_buffer_state *info;
145 int mask; /* number of transmit slots -1 */
146 int req ____cacheline_aligned; /* transmit slots submitted */
147 int pkt_start; /* packets started */
150 int done ____cacheline_aligned; /* transmit slots completed */
151 int pkt_done; /* packets completed */
156 struct myri10ge_rx_done {
157 struct mcp_slot *entry;
163 struct myri10ge_slice_netstats {
164 unsigned long rx_packets;
165 unsigned long tx_packets;
166 unsigned long rx_bytes;
167 unsigned long tx_bytes;
168 unsigned long rx_dropped;
169 unsigned long tx_dropped;
172 struct myri10ge_slice_state {
173 struct myri10ge_tx_buf tx; /* transmit ring */
174 struct myri10ge_rx_buf rx_small;
175 struct myri10ge_rx_buf rx_big;
176 struct myri10ge_rx_done rx_done;
177 struct net_device *dev;
178 struct napi_struct napi;
179 struct myri10ge_priv *mgp;
180 struct myri10ge_slice_netstats stats;
181 __be32 __iomem *irq_claim;
182 struct mcp_irq_data *fw_stats;
183 dma_addr_t fw_stats_bus;
184 int watchdog_tx_done;
186 int watchdog_rx_done;
188 #ifdef CONFIG_MYRI10GE_DCA
191 __be32 __iomem *dca_tag;
196 struct myri10ge_priv {
197 struct myri10ge_slice_state *ss;
198 int tx_boundary; /* boundary transmits cannot cross */
200 int running; /* running? */
204 struct net_device *dev;
207 unsigned long board_span;
208 unsigned long iomem_base;
209 __be32 __iomem *irq_deassert;
210 char *mac_addr_string;
211 struct mcp_cmd_response *cmd;
213 struct pci_dev *pdev;
216 struct msix_entry *msix_vectors;
217 #ifdef CONFIG_MYRI10GE_DCA
222 unsigned int rdma_tags_available;
224 __be32 __iomem *intr_coal_delay_ptr;
227 wait_queue_head_t down_wq;
228 struct work_struct watchdog_work;
229 struct timer_list watchdog_timer;
233 bool fw_name_allocated;
235 char eeprom_strings[MYRI10GE_EEPROM_STRINGS_SIZE];
236 char *product_code_string;
237 char fw_version[128];
241 int adopted_rx_filter_bug;
242 u8 mac_addr[ETH_ALEN]; /* eeprom mac address */
243 unsigned long serial_number;
244 int vendor_specific_offset;
245 int fw_multicast_support;
253 unsigned int board_number;
257 static char *myri10ge_fw_unaligned = "myri10ge_ethp_z8e.dat";
258 static char *myri10ge_fw_aligned = "myri10ge_eth_z8e.dat";
259 static char *myri10ge_fw_rss_unaligned = "myri10ge_rss_ethp_z8e.dat";
260 static char *myri10ge_fw_rss_aligned = "myri10ge_rss_eth_z8e.dat";
261 MODULE_FIRMWARE("myri10ge_ethp_z8e.dat");
262 MODULE_FIRMWARE("myri10ge_eth_z8e.dat");
263 MODULE_FIRMWARE("myri10ge_rss_ethp_z8e.dat");
264 MODULE_FIRMWARE("myri10ge_rss_eth_z8e.dat");
266 /* Careful: must be accessed under kernel_param_lock() */
267 static char *myri10ge_fw_name = NULL;
268 module_param(myri10ge_fw_name, charp, 0644);
269 MODULE_PARM_DESC(myri10ge_fw_name, "Firmware image name");
271 #define MYRI10GE_MAX_BOARDS 8
272 static char *myri10ge_fw_names[MYRI10GE_MAX_BOARDS] =
273 {[0 ... (MYRI10GE_MAX_BOARDS - 1)] = NULL };
274 module_param_array_named(myri10ge_fw_names, myri10ge_fw_names, charp, NULL,
276 MODULE_PARM_DESC(myri10ge_fw_names, "Firmware image names per board");
278 static int myri10ge_ecrc_enable = 1;
279 module_param(myri10ge_ecrc_enable, int, 0444);
280 MODULE_PARM_DESC(myri10ge_ecrc_enable, "Enable Extended CRC on PCI-E");
282 static int myri10ge_small_bytes = -1; /* -1 == auto */
283 module_param(myri10ge_small_bytes, int, 0644);
284 MODULE_PARM_DESC(myri10ge_small_bytes, "Threshold of small packets");
286 static int myri10ge_msi = 1; /* enable msi by default */
287 module_param(myri10ge_msi, int, 0644);
288 MODULE_PARM_DESC(myri10ge_msi, "Enable Message Signalled Interrupts");
290 static int myri10ge_intr_coal_delay = 75;
291 module_param(myri10ge_intr_coal_delay, int, 0444);
292 MODULE_PARM_DESC(myri10ge_intr_coal_delay, "Interrupt coalescing delay");
294 static int myri10ge_flow_control = 1;
295 module_param(myri10ge_flow_control, int, 0444);
296 MODULE_PARM_DESC(myri10ge_flow_control, "Pause parameter");
298 static int myri10ge_deassert_wait = 1;
299 module_param(myri10ge_deassert_wait, int, 0644);
300 MODULE_PARM_DESC(myri10ge_deassert_wait,
301 "Wait when deasserting legacy interrupts");
303 static int myri10ge_force_firmware = 0;
304 module_param(myri10ge_force_firmware, int, 0444);
305 MODULE_PARM_DESC(myri10ge_force_firmware,
306 "Force firmware to assume aligned completions");
308 static int myri10ge_initial_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
309 module_param(myri10ge_initial_mtu, int, 0444);
310 MODULE_PARM_DESC(myri10ge_initial_mtu, "Initial MTU");
312 static int myri10ge_napi_weight = 64;
313 module_param(myri10ge_napi_weight, int, 0444);
314 MODULE_PARM_DESC(myri10ge_napi_weight, "Set NAPI weight");
316 static int myri10ge_watchdog_timeout = 1;
317 module_param(myri10ge_watchdog_timeout, int, 0444);
318 MODULE_PARM_DESC(myri10ge_watchdog_timeout, "Set watchdog timeout");
320 static int myri10ge_max_irq_loops = 1048576;
321 module_param(myri10ge_max_irq_loops, int, 0444);
322 MODULE_PARM_DESC(myri10ge_max_irq_loops,
323 "Set stuck legacy IRQ detection threshold");
325 #define MYRI10GE_MSG_DEFAULT NETIF_MSG_LINK
327 static int myri10ge_debug = -1; /* defaults above */
328 module_param(myri10ge_debug, int, 0);
329 MODULE_PARM_DESC(myri10ge_debug, "Debug level (0=none,...,16=all)");
331 static int myri10ge_fill_thresh = 256;
332 module_param(myri10ge_fill_thresh, int, 0644);
333 MODULE_PARM_DESC(myri10ge_fill_thresh, "Number of empty rx slots allowed");
335 static int myri10ge_reset_recover = 1;
337 static int myri10ge_max_slices = 1;
338 module_param(myri10ge_max_slices, int, 0444);
339 MODULE_PARM_DESC(myri10ge_max_slices, "Max tx/rx queues");
341 static int myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_DST_PORT;
342 module_param(myri10ge_rss_hash, int, 0444);
343 MODULE_PARM_DESC(myri10ge_rss_hash, "Type of RSS hashing to do");
345 static int myri10ge_dca = 1;
346 module_param(myri10ge_dca, int, 0444);
347 MODULE_PARM_DESC(myri10ge_dca, "Enable DCA if possible");
349 #define MYRI10GE_FW_OFFSET 1024*1024
350 #define MYRI10GE_HIGHPART_TO_U32(X) \
351 (sizeof (X) == 8) ? ((u32)((u64)(X) >> 32)) : (0)
352 #define MYRI10GE_LOWPART_TO_U32(X) ((u32)(X))
354 #define myri10ge_pio_copy(to,from,size) __iowrite64_copy(to,from,size/8)
356 static void myri10ge_set_multicast_list(struct net_device *dev);
357 static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
358 struct net_device *dev);
360 static inline void put_be32(__be32 val, __be32 __iomem * p)
362 __raw_writel((__force __u32) val, (__force void __iomem *)p);
365 static void myri10ge_get_stats(struct net_device *dev,
366 struct rtnl_link_stats64 *stats);
368 static void set_fw_name(struct myri10ge_priv *mgp, char *name, bool allocated)
370 if (mgp->fw_name_allocated)
373 mgp->fw_name_allocated = allocated;
377 myri10ge_send_cmd(struct myri10ge_priv *mgp, u32 cmd,
378 struct myri10ge_cmd *data, int atomic)
381 char buf_bytes[sizeof(*buf) + 8];
382 struct mcp_cmd_response *response = mgp->cmd;
383 char __iomem *cmd_addr = mgp->sram + MXGEFW_ETH_CMD;
384 u32 dma_low, dma_high, result, value;
387 /* ensure buf is aligned to 8 bytes */
388 buf = (struct mcp_cmd *)ALIGN((unsigned long)buf_bytes, 8);
390 buf->data0 = htonl(data->data0);
391 buf->data1 = htonl(data->data1);
392 buf->data2 = htonl(data->data2);
393 buf->cmd = htonl(cmd);
394 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
395 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
397 buf->response_addr.low = htonl(dma_low);
398 buf->response_addr.high = htonl(dma_high);
399 response->result = htonl(MYRI10GE_NO_RESPONSE_RESULT);
401 myri10ge_pio_copy(cmd_addr, buf, sizeof(*buf));
403 /* wait up to 15ms. Longest command is the DMA benchmark,
404 * which is capped at 5ms, but runs from a timeout handler
405 * that runs every 7.8ms. So a 15ms timeout leaves us with
409 /* if atomic is set, do not sleep,
410 * and try to get the completion quickly
411 * (1ms will be enough for those commands) */
412 for (sleep_total = 0;
413 sleep_total < 1000 &&
414 response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
420 /* use msleep for most command */
421 for (sleep_total = 0;
423 response->result == htonl(MYRI10GE_NO_RESPONSE_RESULT);
428 result = ntohl(response->result);
429 value = ntohl(response->data);
430 if (result != MYRI10GE_NO_RESPONSE_RESULT) {
434 } else if (result == MXGEFW_CMD_UNKNOWN) {
436 } else if (result == MXGEFW_CMD_ERROR_UNALIGNED) {
438 } else if (result == MXGEFW_CMD_ERROR_RANGE &&
439 cmd == MXGEFW_CMD_ENABLE_RSS_QUEUES &&
441 data1 & MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES) !=
445 dev_err(&mgp->pdev->dev,
446 "command %d failed, result = %d\n",
452 dev_err(&mgp->pdev->dev, "command %d timed out, result = %d\n",
458 * The eeprom strings on the lanaiX have the format
461 * PT:ddd mmm xx xx:xx:xx xx\0
462 * PV:ddd mmm xx xx:xx:xx xx\0
464 static int myri10ge_read_mac_addr(struct myri10ge_priv *mgp)
469 ptr = mgp->eeprom_strings;
470 limit = mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE;
472 while (*ptr != '\0' && ptr < limit) {
473 if (memcmp(ptr, "MAC=", 4) == 0) {
475 mgp->mac_addr_string = ptr;
476 for (i = 0; i < 6; i++) {
477 if ((ptr + 2) > limit)
480 simple_strtoul(ptr, &ptr, 16);
484 if (memcmp(ptr, "PC=", 3) == 0) {
486 mgp->product_code_string = ptr;
488 if (memcmp((const void *)ptr, "SN=", 3) == 0) {
490 mgp->serial_number = simple_strtoul(ptr, &ptr, 10);
492 while (ptr < limit && *ptr++) ;
498 dev_err(&mgp->pdev->dev, "failed to parse eeprom_strings\n");
503 * Enable or disable periodic RDMAs from the host to make certain
504 * chipsets resend dropped PCIe messages
507 static void myri10ge_dummy_rdma(struct myri10ge_priv *mgp, int enable)
509 char __iomem *submit;
510 __be32 buf[16] __attribute__ ((__aligned__(8)));
511 u32 dma_low, dma_high;
514 /* clear confirmation addr */
518 /* send a rdma command to the PCIe engine, and wait for the
519 * response in the confirmation address. The firmware should
520 * write a -1 there to indicate it is alive and well
522 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
523 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
525 buf[0] = htonl(dma_high); /* confirm addr MSW */
526 buf[1] = htonl(dma_low); /* confirm addr LSW */
527 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
528 buf[3] = htonl(dma_high); /* dummy addr MSW */
529 buf[4] = htonl(dma_low); /* dummy addr LSW */
530 buf[5] = htonl(enable); /* enable? */
532 submit = mgp->sram + MXGEFW_BOOT_DUMMY_RDMA;
534 myri10ge_pio_copy(submit, &buf, sizeof(buf));
535 for (i = 0; mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 20; i++)
537 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA)
538 dev_err(&mgp->pdev->dev, "dummy rdma %s failed\n",
539 (enable ? "enable" : "disable"));
543 myri10ge_validate_firmware(struct myri10ge_priv *mgp,
544 struct mcp_gen_header *hdr)
546 struct device *dev = &mgp->pdev->dev;
548 /* check firmware type */
549 if (ntohl(hdr->mcp_type) != MCP_TYPE_ETH) {
550 dev_err(dev, "Bad firmware type: 0x%x\n", ntohl(hdr->mcp_type));
554 /* save firmware version for ethtool */
555 strncpy(mgp->fw_version, hdr->version, sizeof(mgp->fw_version));
556 mgp->fw_version[sizeof(mgp->fw_version) - 1] = '\0';
558 sscanf(mgp->fw_version, "%d.%d.%d", &mgp->fw_ver_major,
559 &mgp->fw_ver_minor, &mgp->fw_ver_tiny);
561 if (!(mgp->fw_ver_major == MXGEFW_VERSION_MAJOR &&
562 mgp->fw_ver_minor == MXGEFW_VERSION_MINOR)) {
563 dev_err(dev, "Found firmware version %s\n", mgp->fw_version);
564 dev_err(dev, "Driver needs %d.%d\n", MXGEFW_VERSION_MAJOR,
565 MXGEFW_VERSION_MINOR);
571 static int myri10ge_load_hotplug_firmware(struct myri10ge_priv *mgp, u32 * size)
573 unsigned crc, reread_crc;
574 const struct firmware *fw;
575 struct device *dev = &mgp->pdev->dev;
576 unsigned char *fw_readback;
577 struct mcp_gen_header *hdr;
582 if ((status = request_firmware(&fw, mgp->fw_name, dev)) < 0) {
583 dev_err(dev, "Unable to load %s firmware image via hotplug\n",
586 goto abort_with_nothing;
591 if (fw->size >= mgp->sram_size - MYRI10GE_FW_OFFSET ||
592 fw->size < MCP_HEADER_PTR_OFFSET + 4) {
593 dev_err(dev, "Firmware size invalid:%d\n", (int)fw->size);
599 hdr_offset = ntohl(*(__be32 *) (fw->data + MCP_HEADER_PTR_OFFSET));
600 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > fw->size) {
601 dev_err(dev, "Bad firmware file\n");
605 hdr = (void *)(fw->data + hdr_offset);
607 status = myri10ge_validate_firmware(mgp, hdr);
611 crc = crc32(~0, fw->data, fw->size);
612 for (i = 0; i < fw->size; i += 256) {
613 myri10ge_pio_copy(mgp->sram + MYRI10GE_FW_OFFSET + i,
615 min(256U, (unsigned)(fw->size - i)));
619 fw_readback = vmalloc(fw->size);
624 /* corruption checking is good for parity recovery and buggy chipset */
625 memcpy_fromio(fw_readback, mgp->sram + MYRI10GE_FW_OFFSET, fw->size);
626 reread_crc = crc32(~0, fw_readback, fw->size);
628 if (crc != reread_crc) {
629 dev_err(dev, "CRC failed(fw-len=%u), got 0x%x (expect 0x%x)\n",
630 (unsigned)fw->size, reread_crc, crc);
634 *size = (u32) fw->size;
637 release_firmware(fw);
643 static int myri10ge_adopt_running_firmware(struct myri10ge_priv *mgp)
645 struct mcp_gen_header *hdr;
646 struct device *dev = &mgp->pdev->dev;
647 const size_t bytes = sizeof(struct mcp_gen_header);
651 /* find running firmware header */
652 hdr_offset = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
654 if ((hdr_offset & 3) || hdr_offset + sizeof(*hdr) > mgp->sram_size) {
655 dev_err(dev, "Running firmware has bad header offset (%d)\n",
660 /* copy header of running firmware from SRAM to host memory to
661 * validate firmware */
662 hdr = kmalloc(bytes, GFP_KERNEL);
666 memcpy_fromio(hdr, mgp->sram + hdr_offset, bytes);
667 status = myri10ge_validate_firmware(mgp, hdr);
670 /* check to see if adopted firmware has bug where adopting
671 * it will cause broadcasts to be filtered unless the NIC
672 * is kept in ALLMULTI mode */
673 if (mgp->fw_ver_major == 1 && mgp->fw_ver_minor == 4 &&
674 mgp->fw_ver_tiny >= 4 && mgp->fw_ver_tiny <= 11) {
675 mgp->adopted_rx_filter_bug = 1;
676 dev_warn(dev, "Adopting fw %d.%d.%d: "
677 "working around rx filter bug\n",
678 mgp->fw_ver_major, mgp->fw_ver_minor,
684 static int myri10ge_get_firmware_capabilities(struct myri10ge_priv *mgp)
686 struct myri10ge_cmd cmd;
689 /* probe for IPv6 TSO support */
690 mgp->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO;
691 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_TSO6_HDR_SIZE,
694 mgp->max_tso6 = cmd.data0;
695 mgp->features |= NETIF_F_TSO6;
698 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
700 dev_err(&mgp->pdev->dev,
701 "failed MXGEFW_CMD_GET_RX_RING_SIZE\n");
705 mgp->max_intr_slots = 2 * (cmd.data0 / sizeof(struct mcp_dma_addr));
710 static int myri10ge_load_firmware(struct myri10ge_priv *mgp, int adopt)
712 char __iomem *submit;
713 __be32 buf[16] __attribute__ ((__aligned__(8)));
714 u32 dma_low, dma_high, size;
718 status = myri10ge_load_hotplug_firmware(mgp, &size);
722 dev_warn(&mgp->pdev->dev, "hotplug firmware loading failed\n");
724 /* Do not attempt to adopt firmware if there
729 status = myri10ge_adopt_running_firmware(mgp);
731 dev_err(&mgp->pdev->dev,
732 "failed to adopt running firmware\n");
735 dev_info(&mgp->pdev->dev,
736 "Successfully adopted running firmware\n");
737 if (mgp->tx_boundary == 4096) {
738 dev_warn(&mgp->pdev->dev,
739 "Using firmware currently running on NIC"
741 dev_warn(&mgp->pdev->dev,
742 "performance consider loading optimized "
744 dev_warn(&mgp->pdev->dev, "via hotplug\n");
747 set_fw_name(mgp, "adopted", false);
748 mgp->tx_boundary = 2048;
749 myri10ge_dummy_rdma(mgp, 1);
750 status = myri10ge_get_firmware_capabilities(mgp);
754 /* clear confirmation addr */
758 /* send a reload command to the bootstrap MCP, and wait for the
759 * response in the confirmation address. The firmware should
760 * write a -1 there to indicate it is alive and well
762 dma_low = MYRI10GE_LOWPART_TO_U32(mgp->cmd_bus);
763 dma_high = MYRI10GE_HIGHPART_TO_U32(mgp->cmd_bus);
765 buf[0] = htonl(dma_high); /* confirm addr MSW */
766 buf[1] = htonl(dma_low); /* confirm addr LSW */
767 buf[2] = MYRI10GE_NO_CONFIRM_DATA; /* confirm data */
769 /* FIX: All newest firmware should un-protect the bottom of
770 * the sram before handoff. However, the very first interfaces
771 * do not. Therefore the handoff copy must skip the first 8 bytes
773 buf[3] = htonl(MYRI10GE_FW_OFFSET + 8); /* where the code starts */
774 buf[4] = htonl(size - 8); /* length of code */
775 buf[5] = htonl(8); /* where to copy to */
776 buf[6] = htonl(0); /* where to jump to */
778 submit = mgp->sram + MXGEFW_BOOT_HANDOFF;
780 myri10ge_pio_copy(submit, &buf, sizeof(buf));
785 while (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA && i < 9) {
789 if (mgp->cmd->data != MYRI10GE_NO_CONFIRM_DATA) {
790 dev_err(&mgp->pdev->dev, "handoff failed\n");
793 myri10ge_dummy_rdma(mgp, 1);
794 status = myri10ge_get_firmware_capabilities(mgp);
799 static int myri10ge_update_mac_address(struct myri10ge_priv *mgp, u8 * addr)
801 struct myri10ge_cmd cmd;
804 cmd.data0 = ((addr[0] << 24) | (addr[1] << 16)
805 | (addr[2] << 8) | addr[3]);
807 cmd.data1 = ((addr[4] << 8) | (addr[5]));
809 status = myri10ge_send_cmd(mgp, MXGEFW_SET_MAC_ADDRESS, &cmd, 0);
813 static int myri10ge_change_pause(struct myri10ge_priv *mgp, int pause)
815 struct myri10ge_cmd cmd;
818 ctl = pause ? MXGEFW_ENABLE_FLOW_CONTROL : MXGEFW_DISABLE_FLOW_CONTROL;
819 status = myri10ge_send_cmd(mgp, ctl, &cmd, 0);
822 netdev_err(mgp->dev, "Failed to set flow control mode\n");
830 myri10ge_change_promisc(struct myri10ge_priv *mgp, int promisc, int atomic)
832 struct myri10ge_cmd cmd;
835 ctl = promisc ? MXGEFW_ENABLE_PROMISC : MXGEFW_DISABLE_PROMISC;
836 status = myri10ge_send_cmd(mgp, ctl, &cmd, atomic);
838 netdev_err(mgp->dev, "Failed to set promisc mode\n");
841 static int myri10ge_dma_test(struct myri10ge_priv *mgp, int test_type)
843 struct myri10ge_cmd cmd;
846 struct page *dmatest_page;
847 dma_addr_t dmatest_bus;
850 dmatest_page = alloc_page(GFP_KERNEL);
853 dmatest_bus = dma_map_page(&mgp->pdev->dev, dmatest_page, 0,
854 PAGE_SIZE, DMA_BIDIRECTIONAL);
855 if (unlikely(dma_mapping_error(&mgp->pdev->dev, dmatest_bus))) {
856 __free_page(dmatest_page);
860 /* Run a small DMA test.
861 * The magic multipliers to the length tell the firmware
862 * to do DMA read, write, or read+write tests. The
863 * results are returned in cmd.data0. The upper 16
864 * bits or the return is the number of transfers completed.
865 * The lower 16 bits is the time in 0.5us ticks that the
866 * transfers took to complete.
869 len = mgp->tx_boundary;
871 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
872 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
873 cmd.data2 = len * 0x10000;
874 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
879 mgp->read_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
880 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
881 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
882 cmd.data2 = len * 0x1;
883 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
888 mgp->write_dma = ((cmd.data0 >> 16) * len * 2) / (cmd.data0 & 0xffff);
890 cmd.data0 = MYRI10GE_LOWPART_TO_U32(dmatest_bus);
891 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(dmatest_bus);
892 cmd.data2 = len * 0x10001;
893 status = myri10ge_send_cmd(mgp, test_type, &cmd, 0);
898 mgp->read_write_dma = ((cmd.data0 >> 16) * len * 2 * 2) /
899 (cmd.data0 & 0xffff);
902 dma_unmap_page(&mgp->pdev->dev, dmatest_bus, PAGE_SIZE,
904 put_page(dmatest_page);
906 if (status != 0 && test_type != MXGEFW_CMD_UNALIGNED_TEST)
907 dev_warn(&mgp->pdev->dev, "DMA %s benchmark failed: %d\n",
913 static int myri10ge_reset(struct myri10ge_priv *mgp)
915 struct myri10ge_cmd cmd;
916 struct myri10ge_slice_state *ss;
919 #ifdef CONFIG_MYRI10GE_DCA
920 unsigned long dca_tag_off;
923 /* try to send a reset command to the card to see if it
925 memset(&cmd, 0, sizeof(cmd));
926 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
928 dev_err(&mgp->pdev->dev, "failed reset\n");
932 (void)myri10ge_dma_test(mgp, MXGEFW_DMA_TEST);
934 * Use non-ndis mcp_slot (eg, 4 bytes total,
935 * no toeplitz hash value returned. Older firmware will
936 * not understand this command, but will use the correct
937 * sized mcp_slot, so we ignore error returns
939 cmd.data0 = MXGEFW_RSS_MCP_SLOT_TYPE_MIN;
940 (void)myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_MCP_SLOT_TYPE, &cmd, 0);
942 /* Now exchange information about interrupts */
944 bytes = mgp->max_intr_slots * sizeof(*mgp->ss[0].rx_done.entry);
945 cmd.data0 = (u32) bytes;
946 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
949 * Even though we already know how many slices are supported
950 * via myri10ge_probe_slices() MXGEFW_CMD_GET_MAX_RSS_QUEUES
951 * has magic side effects, and must be called after a reset.
952 * It must be called prior to calling any RSS related cmds,
953 * including assigning an interrupt queue for anything but
954 * slice 0. It must also be called *after*
955 * MXGEFW_CMD_SET_INTRQ_SIZE, since the intrq size is used by
956 * the firmware to compute offsets.
959 if (mgp->num_slices > 1) {
961 /* ask the maximum number of slices it supports */
962 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES,
965 dev_err(&mgp->pdev->dev,
966 "failed to get number of slices\n");
970 * MXGEFW_CMD_ENABLE_RSS_QUEUES must be called prior
971 * to setting up the interrupt queue DMA
974 cmd.data0 = mgp->num_slices;
975 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
976 if (mgp->dev->real_num_tx_queues > 1)
977 cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
978 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
981 /* Firmware older than 1.4.32 only supports multiple
982 * RX queues, so if we get an error, first retry using a
983 * single TX queue before giving up */
984 if (status != 0 && mgp->dev->real_num_tx_queues > 1) {
985 netif_set_real_num_tx_queues(mgp->dev, 1);
986 cmd.data0 = mgp->num_slices;
987 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
988 status = myri10ge_send_cmd(mgp,
989 MXGEFW_CMD_ENABLE_RSS_QUEUES,
994 dev_err(&mgp->pdev->dev,
995 "failed to set number of slices\n");
1000 for (i = 0; i < mgp->num_slices; i++) {
1002 cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->rx_done.bus);
1003 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->rx_done.bus);
1005 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_DMA,
1010 myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_ACK_OFFSET, &cmd, 0);
1011 for (i = 0; i < mgp->num_slices; i++) {
1014 (__iomem __be32 *) (mgp->sram + cmd.data0 + 8 * i);
1016 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_IRQ_DEASSERT_OFFSET,
1018 mgp->irq_deassert = (__iomem __be32 *) (mgp->sram + cmd.data0);
1020 status |= myri10ge_send_cmd
1021 (mgp, MXGEFW_CMD_GET_INTR_COAL_DELAY_OFFSET, &cmd, 0);
1022 mgp->intr_coal_delay_ptr = (__iomem __be32 *) (mgp->sram + cmd.data0);
1024 dev_err(&mgp->pdev->dev, "failed set interrupt parameters\n");
1027 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1029 #ifdef CONFIG_MYRI10GE_DCA
1030 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_DCA_OFFSET, &cmd, 0);
1031 dca_tag_off = cmd.data0;
1032 for (i = 0; i < mgp->num_slices; i++) {
1035 ss->dca_tag = (__iomem __be32 *)
1036 (mgp->sram + dca_tag_off + 4 * i);
1041 #endif /* CONFIG_MYRI10GE_DCA */
1043 /* reset mcp/driver shared state back to 0 */
1045 mgp->link_changes = 0;
1046 for (i = 0; i < mgp->num_slices; i++) {
1049 memset(ss->rx_done.entry, 0, bytes);
1052 ss->tx.pkt_start = 0;
1053 ss->tx.pkt_done = 0;
1055 ss->rx_small.cnt = 0;
1056 ss->rx_done.idx = 0;
1057 ss->rx_done.cnt = 0;
1058 ss->tx.wake_queue = 0;
1059 ss->tx.stop_queue = 0;
1062 status = myri10ge_update_mac_address(mgp, mgp->dev->dev_addr);
1063 myri10ge_change_pause(mgp, mgp->pause);
1064 myri10ge_set_multicast_list(mgp->dev);
1068 #ifdef CONFIG_MYRI10GE_DCA
1069 static int myri10ge_toggle_relaxed(struct pci_dev *pdev, int on)
1074 pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &ctl);
1076 ret = (ctl & PCI_EXP_DEVCTL_RELAX_EN) >> 4;
1078 ctl &= ~PCI_EXP_DEVCTL_RELAX_EN;
1080 pcie_capability_write_word(pdev, PCI_EXP_DEVCTL, ctl);
1086 myri10ge_write_dca(struct myri10ge_slice_state *ss, int cpu, int tag)
1088 ss->cached_dca_tag = tag;
1089 put_be32(htonl(tag), ss->dca_tag);
1092 static inline void myri10ge_update_dca(struct myri10ge_slice_state *ss)
1094 int cpu = get_cpu();
1097 if (cpu != ss->cpu) {
1098 tag = dca3_get_tag(&ss->mgp->pdev->dev, cpu);
1099 if (ss->cached_dca_tag != tag)
1100 myri10ge_write_dca(ss, cpu, tag);
1106 static void myri10ge_setup_dca(struct myri10ge_priv *mgp)
1109 struct pci_dev *pdev = mgp->pdev;
1111 if (mgp->ss[0].dca_tag == NULL || mgp->dca_enabled)
1113 if (!myri10ge_dca) {
1114 dev_err(&pdev->dev, "dca disabled by administrator\n");
1117 err = dca_add_requester(&pdev->dev);
1121 "dca_add_requester() failed, err=%d\n", err);
1124 mgp->relaxed_order = myri10ge_toggle_relaxed(pdev, 0);
1125 mgp->dca_enabled = 1;
1126 for (i = 0; i < mgp->num_slices; i++) {
1127 mgp->ss[i].cpu = -1;
1128 mgp->ss[i].cached_dca_tag = -1;
1129 myri10ge_update_dca(&mgp->ss[i]);
1133 static void myri10ge_teardown_dca(struct myri10ge_priv *mgp)
1135 struct pci_dev *pdev = mgp->pdev;
1137 if (!mgp->dca_enabled)
1139 mgp->dca_enabled = 0;
1140 if (mgp->relaxed_order)
1141 myri10ge_toggle_relaxed(pdev, 1);
1142 dca_remove_requester(&pdev->dev);
1145 static int myri10ge_notify_dca_device(struct device *dev, void *data)
1147 struct myri10ge_priv *mgp;
1148 unsigned long event;
1150 mgp = dev_get_drvdata(dev);
1151 event = *(unsigned long *)data;
1153 if (event == DCA_PROVIDER_ADD)
1154 myri10ge_setup_dca(mgp);
1155 else if (event == DCA_PROVIDER_REMOVE)
1156 myri10ge_teardown_dca(mgp);
1159 #endif /* CONFIG_MYRI10GE_DCA */
1162 myri10ge_submit_8rx(struct mcp_kreq_ether_recv __iomem * dst,
1163 struct mcp_kreq_ether_recv *src)
1167 low = src->addr_low;
1168 src->addr_low = htonl(DMA_BIT_MASK(32));
1169 myri10ge_pio_copy(dst, src, 4 * sizeof(*src));
1171 myri10ge_pio_copy(dst + 4, src + 4, 4 * sizeof(*src));
1173 src->addr_low = low;
1174 put_be32(low, &dst->addr_low);
1179 myri10ge_alloc_rx_pages(struct myri10ge_priv *mgp, struct myri10ge_rx_buf *rx,
1180 int bytes, int watchdog)
1185 #if MYRI10GE_ALLOC_SIZE > 4096
1189 if (unlikely(rx->watchdog_needed && !watchdog))
1192 /* try to refill entire ring */
1193 while (rx->fill_cnt != (rx->cnt + rx->mask + 1)) {
1194 idx = rx->fill_cnt & rx->mask;
1195 if (rx->page_offset + bytes <= MYRI10GE_ALLOC_SIZE) {
1196 /* we can use part of previous page */
1199 /* we need a new page */
1201 alloc_pages(GFP_ATOMIC | __GFP_COMP,
1202 MYRI10GE_ALLOC_ORDER);
1203 if (unlikely(page == NULL)) {
1204 if (rx->fill_cnt - rx->cnt < 16)
1205 rx->watchdog_needed = 1;
1209 bus = dma_map_page(&mgp->pdev->dev, page, 0,
1210 MYRI10GE_ALLOC_SIZE,
1212 if (unlikely(dma_mapping_error(&mgp->pdev->dev, bus))) {
1213 __free_pages(page, MYRI10GE_ALLOC_ORDER);
1214 if (rx->fill_cnt - rx->cnt < 16)
1215 rx->watchdog_needed = 1;
1220 rx->page_offset = 0;
1224 rx->info[idx].page = rx->page;
1225 rx->info[idx].page_offset = rx->page_offset;
1226 /* note that this is the address of the start of the
1228 dma_unmap_addr_set(&rx->info[idx], bus, rx->bus);
1229 rx->shadow[idx].addr_low =
1230 htonl(MYRI10GE_LOWPART_TO_U32(rx->bus) + rx->page_offset);
1231 rx->shadow[idx].addr_high =
1232 htonl(MYRI10GE_HIGHPART_TO_U32(rx->bus));
1234 /* start next packet on a cacheline boundary */
1235 rx->page_offset += SKB_DATA_ALIGN(bytes);
1237 #if MYRI10GE_ALLOC_SIZE > 4096
1238 /* don't cross a 4KB boundary */
1239 end_offset = rx->page_offset + bytes - 1;
1240 if ((unsigned)(rx->page_offset ^ end_offset) > 4095)
1241 rx->page_offset = end_offset & ~4095;
1245 /* copy 8 descriptors to the firmware at a time */
1246 if ((idx & 7) == 7) {
1247 myri10ge_submit_8rx(&rx->lanai[idx - 7],
1248 &rx->shadow[idx - 7]);
1254 myri10ge_unmap_rx_page(struct pci_dev *pdev,
1255 struct myri10ge_rx_buffer_state *info, int bytes)
1257 /* unmap the recvd page if we're the only or last user of it */
1258 if (bytes >= MYRI10GE_ALLOC_SIZE / 2 ||
1259 (info->page_offset + 2 * bytes) > MYRI10GE_ALLOC_SIZE) {
1260 dma_unmap_page(&pdev->dev, (dma_unmap_addr(info, bus)
1261 & ~(MYRI10GE_ALLOC_SIZE - 1)),
1262 MYRI10GE_ALLOC_SIZE, DMA_FROM_DEVICE);
1267 * GRO does not support acceleration of tagged vlan frames, and
1268 * this NIC does not support vlan tag offload, so we must pop
1269 * the tag ourselves to be able to achieve GRO performance that
1270 * is comparable to LRO.
1274 myri10ge_vlan_rx(struct net_device *dev, void *addr, struct sk_buff *skb)
1277 struct vlan_ethhdr *veh;
1283 veh = (struct vlan_ethhdr *)va;
1284 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
1285 NETIF_F_HW_VLAN_CTAG_RX &&
1286 veh->h_vlan_proto == htons(ETH_P_8021Q)) {
1287 /* fixup csum if needed */
1288 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1289 vsum = csum_partial(va + ETH_HLEN, VLAN_HLEN, 0);
1290 skb->csum = csum_sub(skb->csum, vsum);
1293 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(veh->h_vlan_TCI));
1294 memmove(va + VLAN_HLEN, va, 2 * ETH_ALEN);
1295 skb->len -= VLAN_HLEN;
1296 skb->data_len -= VLAN_HLEN;
1297 frag = skb_shinfo(skb)->frags;
1298 skb_frag_off_add(frag, VLAN_HLEN);
1299 skb_frag_size_sub(frag, VLAN_HLEN);
1303 #define MYRI10GE_HLEN 64 /* Bytes to copy from page to skb linear memory */
1306 myri10ge_rx_done(struct myri10ge_slice_state *ss, int len, __wsum csum)
1308 struct myri10ge_priv *mgp = ss->mgp;
1309 struct sk_buff *skb;
1310 skb_frag_t *rx_frags;
1311 struct myri10ge_rx_buf *rx;
1312 int i, idx, remainder, bytes;
1313 struct pci_dev *pdev = mgp->pdev;
1314 struct net_device *dev = mgp->dev;
1317 if (len <= mgp->small_bytes) {
1319 bytes = mgp->small_bytes;
1322 bytes = mgp->big_bytes;
1326 idx = rx->cnt & rx->mask;
1327 va = page_address(rx->info[idx].page) + rx->info[idx].page_offset;
1330 skb = napi_get_frags(&ss->napi);
1331 if (unlikely(skb == NULL)) {
1332 ss->stats.rx_dropped++;
1333 for (i = 0, remainder = len; remainder > 0; i++) {
1334 myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1335 put_page(rx->info[idx].page);
1337 idx = rx->cnt & rx->mask;
1338 remainder -= MYRI10GE_ALLOC_SIZE;
1342 rx_frags = skb_shinfo(skb)->frags;
1343 /* Fill skb_frag_t(s) with data from our receive */
1344 for (i = 0, remainder = len; remainder > 0; i++) {
1345 myri10ge_unmap_rx_page(pdev, &rx->info[idx], bytes);
1346 skb_fill_page_desc(skb, i, rx->info[idx].page,
1347 rx->info[idx].page_offset,
1348 remainder < MYRI10GE_ALLOC_SIZE ?
1349 remainder : MYRI10GE_ALLOC_SIZE);
1351 idx = rx->cnt & rx->mask;
1352 remainder -= MYRI10GE_ALLOC_SIZE;
1355 /* remove padding */
1356 skb_frag_off_add(&rx_frags[0], MXGEFW_PAD);
1357 skb_frag_size_sub(&rx_frags[0], MXGEFW_PAD);
1361 skb->data_len = len;
1362 skb->truesize += len;
1363 if (dev->features & NETIF_F_RXCSUM) {
1364 skb->ip_summed = CHECKSUM_COMPLETE;
1367 myri10ge_vlan_rx(mgp->dev, va, skb);
1368 skb_record_rx_queue(skb, ss - &mgp->ss[0]);
1370 napi_gro_frags(&ss->napi);
1376 myri10ge_tx_done(struct myri10ge_slice_state *ss, int mcp_index)
1378 struct pci_dev *pdev = ss->mgp->pdev;
1379 struct myri10ge_tx_buf *tx = &ss->tx;
1380 struct netdev_queue *dev_queue;
1381 struct sk_buff *skb;
1384 while (tx->pkt_done != mcp_index) {
1385 idx = tx->done & tx->mask;
1386 skb = tx->info[idx].skb;
1389 tx->info[idx].skb = NULL;
1390 if (tx->info[idx].last) {
1392 tx->info[idx].last = 0;
1395 len = dma_unmap_len(&tx->info[idx], len);
1396 dma_unmap_len_set(&tx->info[idx], len, 0);
1398 ss->stats.tx_bytes += skb->len;
1399 ss->stats.tx_packets++;
1400 dev_consume_skb_irq(skb);
1402 dma_unmap_single(&pdev->dev,
1403 dma_unmap_addr(&tx->info[idx],
1408 dma_unmap_page(&pdev->dev,
1409 dma_unmap_addr(&tx->info[idx],
1415 dev_queue = netdev_get_tx_queue(ss->dev, ss - ss->mgp->ss);
1417 * Make a minimal effort to prevent the NIC from polling an
1418 * idle tx queue. If we can't get the lock we leave the queue
1419 * active. In this case, either a thread was about to start
1420 * using the queue anyway, or we lost a race and the NIC will
1421 * waste some of its resources polling an inactive queue for a
1425 if ((ss->mgp->dev->real_num_tx_queues > 1) &&
1426 __netif_tx_trylock(dev_queue)) {
1427 if (tx->req == tx->done) {
1428 tx->queue_active = 0;
1429 put_be32(htonl(1), tx->send_stop);
1432 __netif_tx_unlock(dev_queue);
1435 /* start the queue if we've stopped it */
1436 if (netif_tx_queue_stopped(dev_queue) &&
1437 tx->req - tx->done < (tx->mask >> 1) &&
1438 ss->mgp->running == MYRI10GE_ETH_RUNNING) {
1440 netif_tx_wake_queue(dev_queue);
1445 myri10ge_clean_rx_done(struct myri10ge_slice_state *ss, int budget)
1447 struct myri10ge_rx_done *rx_done = &ss->rx_done;
1448 struct myri10ge_priv *mgp = ss->mgp;
1449 unsigned long rx_bytes = 0;
1450 unsigned long rx_packets = 0;
1451 unsigned long rx_ok;
1452 int idx = rx_done->idx;
1453 int cnt = rx_done->cnt;
1458 while (rx_done->entry[idx].length != 0 && work_done < budget) {
1459 length = ntohs(rx_done->entry[idx].length);
1460 rx_done->entry[idx].length = 0;
1461 checksum = csum_unfold(rx_done->entry[idx].checksum);
1462 rx_ok = myri10ge_rx_done(ss, length, checksum);
1463 rx_packets += rx_ok;
1464 rx_bytes += rx_ok * (unsigned long)length;
1466 idx = cnt & (mgp->max_intr_slots - 1);
1471 ss->stats.rx_packets += rx_packets;
1472 ss->stats.rx_bytes += rx_bytes;
1474 /* restock receive rings if needed */
1475 if (ss->rx_small.fill_cnt - ss->rx_small.cnt < myri10ge_fill_thresh)
1476 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
1477 mgp->small_bytes + MXGEFW_PAD, 0);
1478 if (ss->rx_big.fill_cnt - ss->rx_big.cnt < myri10ge_fill_thresh)
1479 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
1484 static inline void myri10ge_check_statblock(struct myri10ge_priv *mgp)
1486 struct mcp_irq_data *stats = mgp->ss[0].fw_stats;
1488 if (unlikely(stats->stats_updated)) {
1489 unsigned link_up = ntohl(stats->link_up);
1490 if (mgp->link_state != link_up) {
1491 mgp->link_state = link_up;
1493 if (mgp->link_state == MXGEFW_LINK_UP) {
1494 netif_info(mgp, link, mgp->dev, "link up\n");
1495 netif_carrier_on(mgp->dev);
1496 mgp->link_changes++;
1498 netif_info(mgp, link, mgp->dev, "link %s\n",
1499 (link_up == MXGEFW_LINK_MYRINET ?
1500 "mismatch (Myrinet detected)" :
1502 netif_carrier_off(mgp->dev);
1503 mgp->link_changes++;
1506 if (mgp->rdma_tags_available !=
1507 ntohl(stats->rdma_tags_available)) {
1508 mgp->rdma_tags_available =
1509 ntohl(stats->rdma_tags_available);
1510 netdev_warn(mgp->dev, "RDMA timed out! %d tags left\n",
1511 mgp->rdma_tags_available);
1513 mgp->down_cnt += stats->link_down;
1514 if (stats->link_down)
1515 wake_up(&mgp->down_wq);
1519 static int myri10ge_poll(struct napi_struct *napi, int budget)
1521 struct myri10ge_slice_state *ss =
1522 container_of(napi, struct myri10ge_slice_state, napi);
1525 #ifdef CONFIG_MYRI10GE_DCA
1526 if (ss->mgp->dca_enabled)
1527 myri10ge_update_dca(ss);
1529 /* process as many rx events as NAPI will allow */
1530 work_done = myri10ge_clean_rx_done(ss, budget);
1532 if (work_done < budget) {
1533 napi_complete_done(napi, work_done);
1534 put_be32(htonl(3), ss->irq_claim);
1539 static irqreturn_t myri10ge_intr(int irq, void *arg)
1541 struct myri10ge_slice_state *ss = arg;
1542 struct myri10ge_priv *mgp = ss->mgp;
1543 struct mcp_irq_data *stats = ss->fw_stats;
1544 struct myri10ge_tx_buf *tx = &ss->tx;
1545 u32 send_done_count;
1548 /* an interrupt on a non-zero receive-only slice is implicitly
1549 * valid since MSI-X irqs are not shared */
1550 if ((mgp->dev->real_num_tx_queues == 1) && (ss != mgp->ss)) {
1551 napi_schedule(&ss->napi);
1555 /* make sure it is our IRQ, and that the DMA has finished */
1556 if (unlikely(!stats->valid))
1559 /* low bit indicates receives are present, so schedule
1560 * napi poll handler */
1561 if (stats->valid & 1)
1562 napi_schedule(&ss->napi);
1564 if (!mgp->msi_enabled && !mgp->msix_enabled) {
1565 put_be32(0, mgp->irq_deassert);
1566 if (!myri10ge_deassert_wait)
1572 /* Wait for IRQ line to go low, if using INTx */
1576 /* check for transmit completes and receives */
1577 send_done_count = ntohl(stats->send_done_count);
1578 if (send_done_count != tx->pkt_done)
1579 myri10ge_tx_done(ss, (int)send_done_count);
1580 if (unlikely(i > myri10ge_max_irq_loops)) {
1581 netdev_warn(mgp->dev, "irq stuck?\n");
1583 schedule_work(&mgp->watchdog_work);
1585 if (likely(stats->valid == 0))
1591 /* Only slice 0 updates stats */
1593 myri10ge_check_statblock(mgp);
1595 put_be32(htonl(3), ss->irq_claim + 1);
1600 myri10ge_get_link_ksettings(struct net_device *netdev,
1601 struct ethtool_link_ksettings *cmd)
1603 struct myri10ge_priv *mgp = netdev_priv(netdev);
1607 cmd->base.autoneg = AUTONEG_DISABLE;
1608 cmd->base.speed = SPEED_10000;
1609 cmd->base.duplex = DUPLEX_FULL;
1612 * parse the product code to deterimine the interface type
1613 * (CX4, XFP, Quad Ribbon Fiber) by looking at the character
1614 * after the 3rd dash in the driver's cached copy of the
1615 * EEPROM's product code string.
1617 ptr = mgp->product_code_string;
1619 netdev_err(netdev, "Missing product code\n");
1622 for (i = 0; i < 3; i++, ptr++) {
1623 ptr = strchr(ptr, '-');
1625 netdev_err(netdev, "Invalid product code %s\n",
1626 mgp->product_code_string);
1632 if (*ptr == 'R' || *ptr == 'Q' || *ptr == 'S') {
1633 /* We've found either an XFP, quad ribbon fiber, or SFP+ */
1634 cmd->base.port = PORT_FIBRE;
1635 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
1636 ethtool_link_ksettings_add_link_mode(cmd, advertising, FIBRE);
1638 cmd->base.port = PORT_OTHER;
1645 myri10ge_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
1647 struct myri10ge_priv *mgp = netdev_priv(netdev);
1649 strlcpy(info->driver, "myri10ge", sizeof(info->driver));
1650 strlcpy(info->version, MYRI10GE_VERSION_STR, sizeof(info->version));
1651 strlcpy(info->fw_version, mgp->fw_version, sizeof(info->fw_version));
1652 strlcpy(info->bus_info, pci_name(mgp->pdev), sizeof(info->bus_info));
1656 myri10ge_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1658 struct myri10ge_priv *mgp = netdev_priv(netdev);
1660 coal->rx_coalesce_usecs = mgp->intr_coal_delay;
1665 myri10ge_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *coal)
1667 struct myri10ge_priv *mgp = netdev_priv(netdev);
1669 mgp->intr_coal_delay = coal->rx_coalesce_usecs;
1670 put_be32(htonl(mgp->intr_coal_delay), mgp->intr_coal_delay_ptr);
1675 myri10ge_get_pauseparam(struct net_device *netdev,
1676 struct ethtool_pauseparam *pause)
1678 struct myri10ge_priv *mgp = netdev_priv(netdev);
1681 pause->rx_pause = mgp->pause;
1682 pause->tx_pause = mgp->pause;
1686 myri10ge_set_pauseparam(struct net_device *netdev,
1687 struct ethtool_pauseparam *pause)
1689 struct myri10ge_priv *mgp = netdev_priv(netdev);
1691 if (pause->tx_pause != mgp->pause)
1692 return myri10ge_change_pause(mgp, pause->tx_pause);
1693 if (pause->rx_pause != mgp->pause)
1694 return myri10ge_change_pause(mgp, pause->rx_pause);
1695 if (pause->autoneg != 0)
1701 myri10ge_get_ringparam(struct net_device *netdev,
1702 struct ethtool_ringparam *ring)
1704 struct myri10ge_priv *mgp = netdev_priv(netdev);
1706 ring->rx_mini_max_pending = mgp->ss[0].rx_small.mask + 1;
1707 ring->rx_max_pending = mgp->ss[0].rx_big.mask + 1;
1708 ring->rx_jumbo_max_pending = 0;
1709 ring->tx_max_pending = mgp->ss[0].tx.mask + 1;
1710 ring->rx_mini_pending = ring->rx_mini_max_pending;
1711 ring->rx_pending = ring->rx_max_pending;
1712 ring->rx_jumbo_pending = ring->rx_jumbo_max_pending;
1713 ring->tx_pending = ring->tx_max_pending;
1716 static const char myri10ge_gstrings_main_stats[][ETH_GSTRING_LEN] = {
1717 "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
1718 "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
1719 "rx_length_errors", "rx_over_errors", "rx_crc_errors",
1720 "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
1721 "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
1722 "tx_heartbeat_errors", "tx_window_errors",
1723 /* device-specific stats */
1724 "tx_boundary", "irq", "MSI", "MSIX",
1725 "read_dma_bw_MBs", "write_dma_bw_MBs", "read_write_dma_bw_MBs",
1726 "serial_number", "watchdog_resets",
1727 #ifdef CONFIG_MYRI10GE_DCA
1728 "dca_capable_firmware", "dca_device_present",
1730 "link_changes", "link_up", "dropped_link_overflow",
1731 "dropped_link_error_or_filtered",
1732 "dropped_pause", "dropped_bad_phy", "dropped_bad_crc32",
1733 "dropped_unicast_filtered", "dropped_multicast_filtered",
1734 "dropped_runt", "dropped_overrun", "dropped_no_small_buffer",
1735 "dropped_no_big_buffer"
1738 static const char myri10ge_gstrings_slice_stats[][ETH_GSTRING_LEN] = {
1739 "----------- slice ---------",
1740 "tx_pkt_start", "tx_pkt_done", "tx_req", "tx_done",
1741 "rx_small_cnt", "rx_big_cnt",
1742 "wake_queue", "stop_queue", "tx_linearized",
1745 #define MYRI10GE_NET_STATS_LEN 21
1746 #define MYRI10GE_MAIN_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_main_stats)
1747 #define MYRI10GE_SLICE_STATS_LEN ARRAY_SIZE(myri10ge_gstrings_slice_stats)
1750 myri10ge_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
1752 struct myri10ge_priv *mgp = netdev_priv(netdev);
1755 switch (stringset) {
1757 memcpy(data, *myri10ge_gstrings_main_stats,
1758 sizeof(myri10ge_gstrings_main_stats));
1759 data += sizeof(myri10ge_gstrings_main_stats);
1760 for (i = 0; i < mgp->num_slices; i++) {
1761 memcpy(data, *myri10ge_gstrings_slice_stats,
1762 sizeof(myri10ge_gstrings_slice_stats));
1763 data += sizeof(myri10ge_gstrings_slice_stats);
1769 static int myri10ge_get_sset_count(struct net_device *netdev, int sset)
1771 struct myri10ge_priv *mgp = netdev_priv(netdev);
1775 return MYRI10GE_MAIN_STATS_LEN +
1776 mgp->num_slices * MYRI10GE_SLICE_STATS_LEN;
1783 myri10ge_get_ethtool_stats(struct net_device *netdev,
1784 struct ethtool_stats *stats, u64 * data)
1786 struct myri10ge_priv *mgp = netdev_priv(netdev);
1787 struct myri10ge_slice_state *ss;
1788 struct rtnl_link_stats64 link_stats;
1792 /* force stats update */
1793 memset(&link_stats, 0, sizeof(link_stats));
1794 (void)myri10ge_get_stats(netdev, &link_stats);
1795 for (i = 0; i < MYRI10GE_NET_STATS_LEN; i++)
1796 data[i] = ((u64 *)&link_stats)[i];
1798 data[i++] = (unsigned int)mgp->tx_boundary;
1799 data[i++] = (unsigned int)mgp->pdev->irq;
1800 data[i++] = (unsigned int)mgp->msi_enabled;
1801 data[i++] = (unsigned int)mgp->msix_enabled;
1802 data[i++] = (unsigned int)mgp->read_dma;
1803 data[i++] = (unsigned int)mgp->write_dma;
1804 data[i++] = (unsigned int)mgp->read_write_dma;
1805 data[i++] = (unsigned int)mgp->serial_number;
1806 data[i++] = (unsigned int)mgp->watchdog_resets;
1807 #ifdef CONFIG_MYRI10GE_DCA
1808 data[i++] = (unsigned int)(mgp->ss[0].dca_tag != NULL);
1809 data[i++] = (unsigned int)(mgp->dca_enabled);
1811 data[i++] = (unsigned int)mgp->link_changes;
1813 /* firmware stats are useful only in the first slice */
1815 data[i++] = (unsigned int)ntohl(ss->fw_stats->link_up);
1816 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_link_overflow);
1818 (unsigned int)ntohl(ss->fw_stats->dropped_link_error_or_filtered);
1819 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_pause);
1820 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_phy);
1821 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_bad_crc32);
1822 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_unicast_filtered);
1824 (unsigned int)ntohl(ss->fw_stats->dropped_multicast_filtered);
1825 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_runt);
1826 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_overrun);
1827 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_small_buffer);
1828 data[i++] = (unsigned int)ntohl(ss->fw_stats->dropped_no_big_buffer);
1830 for (slice = 0; slice < mgp->num_slices; slice++) {
1831 ss = &mgp->ss[slice];
1833 data[i++] = (unsigned int)ss->tx.pkt_start;
1834 data[i++] = (unsigned int)ss->tx.pkt_done;
1835 data[i++] = (unsigned int)ss->tx.req;
1836 data[i++] = (unsigned int)ss->tx.done;
1837 data[i++] = (unsigned int)ss->rx_small.cnt;
1838 data[i++] = (unsigned int)ss->rx_big.cnt;
1839 data[i++] = (unsigned int)ss->tx.wake_queue;
1840 data[i++] = (unsigned int)ss->tx.stop_queue;
1841 data[i++] = (unsigned int)ss->tx.linearized;
1845 static void myri10ge_set_msglevel(struct net_device *netdev, u32 value)
1847 struct myri10ge_priv *mgp = netdev_priv(netdev);
1848 mgp->msg_enable = value;
1851 static u32 myri10ge_get_msglevel(struct net_device *netdev)
1853 struct myri10ge_priv *mgp = netdev_priv(netdev);
1854 return mgp->msg_enable;
1858 * Use a low-level command to change the LED behavior. Rather than
1859 * blinking (which is the normal case), when identify is used, the
1860 * yellow LED turns solid.
1862 static int myri10ge_led(struct myri10ge_priv *mgp, int on)
1864 struct mcp_gen_header *hdr;
1865 struct device *dev = &mgp->pdev->dev;
1866 size_t hdr_off, pattern_off, hdr_len;
1867 u32 pattern = 0xfffffffe;
1869 /* find running firmware header */
1870 hdr_off = swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET));
1871 if ((hdr_off & 3) || hdr_off + sizeof(*hdr) > mgp->sram_size) {
1872 dev_err(dev, "Running firmware has bad header offset (%d)\n",
1876 hdr_len = swab32(readl(mgp->sram + hdr_off +
1877 offsetof(struct mcp_gen_header, header_length)));
1878 pattern_off = hdr_off + offsetof(struct mcp_gen_header, led_pattern);
1879 if (pattern_off >= (hdr_len + hdr_off)) {
1880 dev_info(dev, "Firmware does not support LED identification\n");
1884 pattern = swab32(readl(mgp->sram + pattern_off + 4));
1885 writel(swab32(pattern), mgp->sram + pattern_off);
1890 myri10ge_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state)
1892 struct myri10ge_priv *mgp = netdev_priv(netdev);
1896 case ETHTOOL_ID_ACTIVE:
1897 rc = myri10ge_led(mgp, 1);
1900 case ETHTOOL_ID_INACTIVE:
1901 rc = myri10ge_led(mgp, 0);
1911 static const struct ethtool_ops myri10ge_ethtool_ops = {
1912 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1913 .get_drvinfo = myri10ge_get_drvinfo,
1914 .get_coalesce = myri10ge_get_coalesce,
1915 .set_coalesce = myri10ge_set_coalesce,
1916 .get_pauseparam = myri10ge_get_pauseparam,
1917 .set_pauseparam = myri10ge_set_pauseparam,
1918 .get_ringparam = myri10ge_get_ringparam,
1919 .get_link = ethtool_op_get_link,
1920 .get_strings = myri10ge_get_strings,
1921 .get_sset_count = myri10ge_get_sset_count,
1922 .get_ethtool_stats = myri10ge_get_ethtool_stats,
1923 .set_msglevel = myri10ge_set_msglevel,
1924 .get_msglevel = myri10ge_get_msglevel,
1925 .set_phys_id = myri10ge_phys_id,
1926 .get_link_ksettings = myri10ge_get_link_ksettings,
1929 static int myri10ge_allocate_rings(struct myri10ge_slice_state *ss)
1931 struct myri10ge_priv *mgp = ss->mgp;
1932 struct myri10ge_cmd cmd;
1933 struct net_device *dev = mgp->dev;
1934 int tx_ring_size, rx_ring_size;
1935 int tx_ring_entries, rx_ring_entries;
1936 int i, slice, status;
1939 /* get ring sizes */
1940 slice = ss - mgp->ss;
1942 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_RING_SIZE, &cmd, 0);
1943 tx_ring_size = cmd.data0;
1945 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_RX_RING_SIZE, &cmd, 0);
1948 rx_ring_size = cmd.data0;
1950 tx_ring_entries = tx_ring_size / sizeof(struct mcp_kreq_ether_send);
1951 rx_ring_entries = rx_ring_size / sizeof(struct mcp_dma_addr);
1952 ss->tx.mask = tx_ring_entries - 1;
1953 ss->rx_small.mask = ss->rx_big.mask = rx_ring_entries - 1;
1957 /* allocate the host shadow rings */
1959 bytes = 8 + (MYRI10GE_MAX_SEND_DESC_TSO + 4)
1960 * sizeof(*ss->tx.req_list);
1961 ss->tx.req_bytes = kzalloc(bytes, GFP_KERNEL);
1962 if (ss->tx.req_bytes == NULL)
1963 goto abort_with_nothing;
1965 /* ensure req_list entries are aligned to 8 bytes */
1966 ss->tx.req_list = (struct mcp_kreq_ether_send *)
1967 ALIGN((unsigned long)ss->tx.req_bytes, 8);
1968 ss->tx.queue_active = 0;
1970 bytes = rx_ring_entries * sizeof(*ss->rx_small.shadow);
1971 ss->rx_small.shadow = kzalloc(bytes, GFP_KERNEL);
1972 if (ss->rx_small.shadow == NULL)
1973 goto abort_with_tx_req_bytes;
1975 bytes = rx_ring_entries * sizeof(*ss->rx_big.shadow);
1976 ss->rx_big.shadow = kzalloc(bytes, GFP_KERNEL);
1977 if (ss->rx_big.shadow == NULL)
1978 goto abort_with_rx_small_shadow;
1980 /* allocate the host info rings */
1982 bytes = tx_ring_entries * sizeof(*ss->tx.info);
1983 ss->tx.info = kzalloc(bytes, GFP_KERNEL);
1984 if (ss->tx.info == NULL)
1985 goto abort_with_rx_big_shadow;
1987 bytes = rx_ring_entries * sizeof(*ss->rx_small.info);
1988 ss->rx_small.info = kzalloc(bytes, GFP_KERNEL);
1989 if (ss->rx_small.info == NULL)
1990 goto abort_with_tx_info;
1992 bytes = rx_ring_entries * sizeof(*ss->rx_big.info);
1993 ss->rx_big.info = kzalloc(bytes, GFP_KERNEL);
1994 if (ss->rx_big.info == NULL)
1995 goto abort_with_rx_small_info;
1997 /* Fill the receive rings */
1999 ss->rx_small.cnt = 0;
2000 ss->rx_big.fill_cnt = 0;
2001 ss->rx_small.fill_cnt = 0;
2002 ss->rx_small.page_offset = MYRI10GE_ALLOC_SIZE;
2003 ss->rx_big.page_offset = MYRI10GE_ALLOC_SIZE;
2004 ss->rx_small.watchdog_needed = 0;
2005 ss->rx_big.watchdog_needed = 0;
2006 if (mgp->small_bytes == 0) {
2007 ss->rx_small.fill_cnt = ss->rx_small.mask + 1;
2009 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
2010 mgp->small_bytes + MXGEFW_PAD, 0);
2013 if (ss->rx_small.fill_cnt < ss->rx_small.mask + 1) {
2014 netdev_err(dev, "slice-%d: alloced only %d small bufs\n",
2015 slice, ss->rx_small.fill_cnt);
2016 goto abort_with_rx_small_ring;
2019 myri10ge_alloc_rx_pages(mgp, &ss->rx_big, mgp->big_bytes, 0);
2020 if (ss->rx_big.fill_cnt < ss->rx_big.mask + 1) {
2021 netdev_err(dev, "slice-%d: alloced only %d big bufs\n",
2022 slice, ss->rx_big.fill_cnt);
2023 goto abort_with_rx_big_ring;
2028 abort_with_rx_big_ring:
2029 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
2030 int idx = i & ss->rx_big.mask;
2031 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
2033 put_page(ss->rx_big.info[idx].page);
2036 abort_with_rx_small_ring:
2037 if (mgp->small_bytes == 0)
2038 ss->rx_small.fill_cnt = ss->rx_small.cnt;
2039 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
2040 int idx = i & ss->rx_small.mask;
2041 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
2042 mgp->small_bytes + MXGEFW_PAD);
2043 put_page(ss->rx_small.info[idx].page);
2046 kfree(ss->rx_big.info);
2048 abort_with_rx_small_info:
2049 kfree(ss->rx_small.info);
2054 abort_with_rx_big_shadow:
2055 kfree(ss->rx_big.shadow);
2057 abort_with_rx_small_shadow:
2058 kfree(ss->rx_small.shadow);
2060 abort_with_tx_req_bytes:
2061 kfree(ss->tx.req_bytes);
2062 ss->tx.req_bytes = NULL;
2063 ss->tx.req_list = NULL;
2069 static void myri10ge_free_rings(struct myri10ge_slice_state *ss)
2071 struct myri10ge_priv *mgp = ss->mgp;
2072 struct sk_buff *skb;
2073 struct myri10ge_tx_buf *tx;
2076 /* If not allocated, skip it */
2077 if (ss->tx.req_list == NULL)
2080 for (i = ss->rx_big.cnt; i < ss->rx_big.fill_cnt; i++) {
2081 idx = i & ss->rx_big.mask;
2082 if (i == ss->rx_big.fill_cnt - 1)
2083 ss->rx_big.info[idx].page_offset = MYRI10GE_ALLOC_SIZE;
2084 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_big.info[idx],
2086 put_page(ss->rx_big.info[idx].page);
2089 if (mgp->small_bytes == 0)
2090 ss->rx_small.fill_cnt = ss->rx_small.cnt;
2091 for (i = ss->rx_small.cnt; i < ss->rx_small.fill_cnt; i++) {
2092 idx = i & ss->rx_small.mask;
2093 if (i == ss->rx_small.fill_cnt - 1)
2094 ss->rx_small.info[idx].page_offset =
2095 MYRI10GE_ALLOC_SIZE;
2096 myri10ge_unmap_rx_page(mgp->pdev, &ss->rx_small.info[idx],
2097 mgp->small_bytes + MXGEFW_PAD);
2098 put_page(ss->rx_small.info[idx].page);
2101 while (tx->done != tx->req) {
2102 idx = tx->done & tx->mask;
2103 skb = tx->info[idx].skb;
2106 tx->info[idx].skb = NULL;
2108 len = dma_unmap_len(&tx->info[idx], len);
2109 dma_unmap_len_set(&tx->info[idx], len, 0);
2111 ss->stats.tx_dropped++;
2112 dev_kfree_skb_any(skb);
2114 dma_unmap_single(&mgp->pdev->dev,
2115 dma_unmap_addr(&tx->info[idx],
2120 dma_unmap_page(&mgp->pdev->dev,
2121 dma_unmap_addr(&tx->info[idx],
2126 kfree(ss->rx_big.info);
2128 kfree(ss->rx_small.info);
2132 kfree(ss->rx_big.shadow);
2134 kfree(ss->rx_small.shadow);
2136 kfree(ss->tx.req_bytes);
2137 ss->tx.req_bytes = NULL;
2138 ss->tx.req_list = NULL;
2141 static int myri10ge_request_irq(struct myri10ge_priv *mgp)
2143 struct pci_dev *pdev = mgp->pdev;
2144 struct myri10ge_slice_state *ss;
2145 struct net_device *netdev = mgp->dev;
2149 mgp->msi_enabled = 0;
2150 mgp->msix_enabled = 0;
2153 if (mgp->num_slices > 1) {
2154 status = pci_enable_msix_range(pdev, mgp->msix_vectors,
2155 mgp->num_slices, mgp->num_slices);
2158 "Error %d setting up MSI-X\n", status);
2161 mgp->msix_enabled = 1;
2163 if (mgp->msix_enabled == 0) {
2164 status = pci_enable_msi(pdev);
2167 "Error %d setting up MSI; falling back to xPIC\n",
2170 mgp->msi_enabled = 1;
2174 if (mgp->msix_enabled) {
2175 for (i = 0; i < mgp->num_slices; i++) {
2177 snprintf(ss->irq_desc, sizeof(ss->irq_desc),
2178 "%s:slice-%d", netdev->name, i);
2179 status = request_irq(mgp->msix_vectors[i].vector,
2180 myri10ge_intr, 0, ss->irq_desc,
2184 "slice %d failed to allocate IRQ\n", i);
2187 free_irq(mgp->msix_vectors[i].vector,
2191 pci_disable_msix(pdev);
2196 status = request_irq(pdev->irq, myri10ge_intr, IRQF_SHARED,
2197 mgp->dev->name, &mgp->ss[0]);
2199 dev_err(&pdev->dev, "failed to allocate IRQ\n");
2200 if (mgp->msi_enabled)
2201 pci_disable_msi(pdev);
2207 static void myri10ge_free_irq(struct myri10ge_priv *mgp)
2209 struct pci_dev *pdev = mgp->pdev;
2212 if (mgp->msix_enabled) {
2213 for (i = 0; i < mgp->num_slices; i++)
2214 free_irq(mgp->msix_vectors[i].vector, &mgp->ss[i]);
2216 free_irq(pdev->irq, &mgp->ss[0]);
2218 if (mgp->msi_enabled)
2219 pci_disable_msi(pdev);
2220 if (mgp->msix_enabled)
2221 pci_disable_msix(pdev);
2224 static int myri10ge_get_txrx(struct myri10ge_priv *mgp, int slice)
2226 struct myri10ge_cmd cmd;
2227 struct myri10ge_slice_state *ss;
2230 ss = &mgp->ss[slice];
2232 if (slice == 0 || (mgp->dev->real_num_tx_queues > 1)) {
2234 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SEND_OFFSET,
2236 ss->tx.lanai = (struct mcp_kreq_ether_send __iomem *)
2237 (mgp->sram + cmd.data0);
2240 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_SMALL_RX_OFFSET,
2242 ss->rx_small.lanai = (struct mcp_kreq_ether_recv __iomem *)
2243 (mgp->sram + cmd.data0);
2246 status |= myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_BIG_RX_OFFSET, &cmd, 0);
2247 ss->rx_big.lanai = (struct mcp_kreq_ether_recv __iomem *)
2248 (mgp->sram + cmd.data0);
2250 ss->tx.send_go = (__iomem __be32 *)
2251 (mgp->sram + MXGEFW_ETH_SEND_GO + 64 * slice);
2252 ss->tx.send_stop = (__iomem __be32 *)
2253 (mgp->sram + MXGEFW_ETH_SEND_STOP + 64 * slice);
2258 static int myri10ge_set_stats(struct myri10ge_priv *mgp, int slice)
2260 struct myri10ge_cmd cmd;
2261 struct myri10ge_slice_state *ss;
2264 ss = &mgp->ss[slice];
2265 cmd.data0 = MYRI10GE_LOWPART_TO_U32(ss->fw_stats_bus);
2266 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(ss->fw_stats_bus);
2267 cmd.data2 = sizeof(struct mcp_irq_data) | (slice << 16);
2268 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_STATS_DMA_V2, &cmd, 0);
2269 if (status == -ENOSYS) {
2270 dma_addr_t bus = ss->fw_stats_bus;
2273 bus += offsetof(struct mcp_irq_data, send_done_count);
2274 cmd.data0 = MYRI10GE_LOWPART_TO_U32(bus);
2275 cmd.data1 = MYRI10GE_HIGHPART_TO_U32(bus);
2276 status = myri10ge_send_cmd(mgp,
2277 MXGEFW_CMD_SET_STATS_DMA_OBSOLETE,
2279 /* Firmware cannot support multicast without STATS_DMA_V2 */
2280 mgp->fw_multicast_support = 0;
2282 mgp->fw_multicast_support = 1;
2287 static int myri10ge_open(struct net_device *dev)
2289 struct myri10ge_slice_state *ss;
2290 struct myri10ge_priv *mgp = netdev_priv(dev);
2291 struct myri10ge_cmd cmd;
2292 int i, status, big_pow2, slice;
2295 if (mgp->running != MYRI10GE_ETH_STOPPED)
2298 mgp->running = MYRI10GE_ETH_STARTING;
2299 status = myri10ge_reset(mgp);
2301 netdev_err(dev, "failed reset\n");
2302 goto abort_with_nothing;
2305 if (mgp->num_slices > 1) {
2306 cmd.data0 = mgp->num_slices;
2307 cmd.data1 = MXGEFW_SLICE_INTR_MODE_ONE_PER_SLICE;
2308 if (mgp->dev->real_num_tx_queues > 1)
2309 cmd.data1 |= MXGEFW_SLICE_ENABLE_MULTIPLE_TX_QUEUES;
2310 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ENABLE_RSS_QUEUES,
2313 netdev_err(dev, "failed to set number of slices\n");
2314 goto abort_with_nothing;
2316 /* setup the indirection table */
2317 cmd.data0 = mgp->num_slices;
2318 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_TABLE_SIZE,
2321 status |= myri10ge_send_cmd(mgp,
2322 MXGEFW_CMD_GET_RSS_TABLE_OFFSET,
2325 netdev_err(dev, "failed to setup rss tables\n");
2326 goto abort_with_nothing;
2329 /* just enable an identity mapping */
2330 itable = mgp->sram + cmd.data0;
2331 for (i = 0; i < mgp->num_slices; i++)
2332 __raw_writeb(i, &itable[i]);
2335 cmd.data1 = myri10ge_rss_hash;
2336 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_RSS_ENABLE,
2339 netdev_err(dev, "failed to enable slices\n");
2340 goto abort_with_nothing;
2344 status = myri10ge_request_irq(mgp);
2346 goto abort_with_nothing;
2348 /* decide what small buffer size to use. For good TCP rx
2349 * performance, it is important to not receive 1514 byte
2350 * frames into jumbo buffers, as it confuses the socket buffer
2351 * accounting code, leading to drops and erratic performance.
2354 if (dev->mtu <= ETH_DATA_LEN)
2355 /* enough for a TCP header */
2356 mgp->small_bytes = (128 > SMP_CACHE_BYTES)
2357 ? (128 - MXGEFW_PAD)
2358 : (SMP_CACHE_BYTES - MXGEFW_PAD);
2360 /* enough for a vlan encapsulated ETH_DATA_LEN frame */
2361 mgp->small_bytes = VLAN_ETH_FRAME_LEN;
2363 /* Override the small buffer size? */
2364 if (myri10ge_small_bytes >= 0)
2365 mgp->small_bytes = myri10ge_small_bytes;
2367 /* Firmware needs the big buff size as a power of 2. Lie and
2368 * tell him the buffer is larger, because we only use 1
2369 * buffer/pkt, and the mtu will prevent overruns.
2371 big_pow2 = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
2372 if (big_pow2 < MYRI10GE_ALLOC_SIZE / 2) {
2373 while (!is_power_of_2(big_pow2))
2375 mgp->big_bytes = dev->mtu + ETH_HLEN + VLAN_HLEN + MXGEFW_PAD;
2377 big_pow2 = MYRI10GE_ALLOC_SIZE;
2378 mgp->big_bytes = big_pow2;
2381 /* setup the per-slice data structures */
2382 for (slice = 0; slice < mgp->num_slices; slice++) {
2383 ss = &mgp->ss[slice];
2385 status = myri10ge_get_txrx(mgp, slice);
2387 netdev_err(dev, "failed to get ring sizes or locations\n");
2388 goto abort_with_rings;
2390 status = myri10ge_allocate_rings(ss);
2392 goto abort_with_rings;
2394 /* only firmware which supports multiple TX queues
2395 * supports setting up the tx stats on non-zero
2397 if (slice == 0 || mgp->dev->real_num_tx_queues > 1)
2398 status = myri10ge_set_stats(mgp, slice);
2400 netdev_err(dev, "Couldn't set stats DMA\n");
2401 goto abort_with_rings;
2404 /* must happen prior to any irq */
2405 napi_enable(&(ss)->napi);
2408 /* now give firmware buffers sizes, and MTU */
2409 cmd.data0 = dev->mtu + ETH_HLEN + VLAN_HLEN;
2410 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_MTU, &cmd, 0);
2411 cmd.data0 = mgp->small_bytes;
2413 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_SMALL_BUFFER_SIZE, &cmd, 0);
2414 cmd.data0 = big_pow2;
2416 myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_BIG_BUFFER_SIZE, &cmd, 0);
2418 netdev_err(dev, "Couldn't set buffer sizes\n");
2419 goto abort_with_rings;
2423 * Set Linux style TSO mode; this is needed only on newer
2424 * firmware versions. Older versions default to Linux
2428 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_TSO_MODE, &cmd, 0);
2429 if (status && status != -ENOSYS) {
2430 netdev_err(dev, "Couldn't set TSO mode\n");
2431 goto abort_with_rings;
2434 mgp->link_state = ~0U;
2435 mgp->rdma_tags_available = 15;
2437 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_UP, &cmd, 0);
2439 netdev_err(dev, "Couldn't bring up link\n");
2440 goto abort_with_rings;
2443 mgp->running = MYRI10GE_ETH_RUNNING;
2444 mgp->watchdog_timer.expires = jiffies + myri10ge_watchdog_timeout * HZ;
2445 add_timer(&mgp->watchdog_timer);
2446 netif_tx_wake_all_queues(dev);
2453 napi_disable(&mgp->ss[slice].napi);
2455 for (i = 0; i < mgp->num_slices; i++)
2456 myri10ge_free_rings(&mgp->ss[i]);
2458 myri10ge_free_irq(mgp);
2461 mgp->running = MYRI10GE_ETH_STOPPED;
2465 static int myri10ge_close(struct net_device *dev)
2467 struct myri10ge_priv *mgp = netdev_priv(dev);
2468 struct myri10ge_cmd cmd;
2469 int status, old_down_cnt;
2472 if (mgp->running != MYRI10GE_ETH_RUNNING)
2475 if (mgp->ss[0].tx.req_bytes == NULL)
2478 del_timer_sync(&mgp->watchdog_timer);
2479 mgp->running = MYRI10GE_ETH_STOPPING;
2480 for (i = 0; i < mgp->num_slices; i++)
2481 napi_disable(&mgp->ss[i].napi);
2483 netif_carrier_off(dev);
2485 netif_tx_stop_all_queues(dev);
2486 if (mgp->rebooted == 0) {
2487 old_down_cnt = mgp->down_cnt;
2490 myri10ge_send_cmd(mgp, MXGEFW_CMD_ETHERNET_DOWN, &cmd, 0);
2492 netdev_err(dev, "Couldn't bring down link\n");
2494 wait_event_timeout(mgp->down_wq, old_down_cnt != mgp->down_cnt,
2496 if (old_down_cnt == mgp->down_cnt)
2497 netdev_err(dev, "never got down irq\n");
2499 netif_tx_disable(dev);
2500 myri10ge_free_irq(mgp);
2501 for (i = 0; i < mgp->num_slices; i++)
2502 myri10ge_free_rings(&mgp->ss[i]);
2504 mgp->running = MYRI10GE_ETH_STOPPED;
2508 /* copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2509 * backwards one at a time and handle ring wraps */
2512 myri10ge_submit_req_backwards(struct myri10ge_tx_buf *tx,
2513 struct mcp_kreq_ether_send *src, int cnt)
2515 int idx, starting_slot;
2516 starting_slot = tx->req;
2519 idx = (starting_slot + cnt) & tx->mask;
2520 myri10ge_pio_copy(&tx->lanai[idx], &src[cnt], sizeof(*src));
2526 * copy an array of struct mcp_kreq_ether_send's to the mcp. Copy
2527 * at most 32 bytes at a time, so as to avoid involving the software
2528 * pio handler in the nic. We re-write the first segment's flags
2529 * to mark them valid only after writing the entire chain.
2533 myri10ge_submit_req(struct myri10ge_tx_buf *tx, struct mcp_kreq_ether_send *src,
2537 struct mcp_kreq_ether_send __iomem *dstp, *dst;
2538 struct mcp_kreq_ether_send *srcp;
2541 idx = tx->req & tx->mask;
2543 last_flags = src->flags;
2546 dst = dstp = &tx->lanai[idx];
2549 if ((idx + cnt) < tx->mask) {
2550 for (i = 0; i < (cnt - 1); i += 2) {
2551 myri10ge_pio_copy(dstp, srcp, 2 * sizeof(*src));
2552 mb(); /* force write every 32 bytes */
2557 /* submit all but the first request, and ensure
2558 * that it is submitted below */
2559 myri10ge_submit_req_backwards(tx, src, cnt);
2563 /* submit the first request */
2564 myri10ge_pio_copy(dstp, srcp, sizeof(*src));
2565 mb(); /* barrier before setting valid flag */
2568 /* re-write the last 32-bits with the valid flags */
2569 src->flags = last_flags;
2570 put_be32(*((__be32 *) src + 3), (__be32 __iomem *) dst + 3);
2575 static void myri10ge_unmap_tx_dma(struct myri10ge_priv *mgp,
2576 struct myri10ge_tx_buf *tx, int idx)
2581 /* Free any DMA resources we've alloced and clear out the skb slot */
2582 last_idx = (idx + 1) & tx->mask;
2583 idx = tx->req & tx->mask;
2585 len = dma_unmap_len(&tx->info[idx], len);
2587 if (tx->info[idx].skb != NULL)
2588 dma_unmap_single(&mgp->pdev->dev,
2589 dma_unmap_addr(&tx->info[idx],
2593 dma_unmap_page(&mgp->pdev->dev,
2594 dma_unmap_addr(&tx->info[idx],
2597 dma_unmap_len_set(&tx->info[idx], len, 0);
2598 tx->info[idx].skb = NULL;
2600 idx = (idx + 1) & tx->mask;
2601 } while (idx != last_idx);
2605 * Transmit a packet. We need to split the packet so that a single
2606 * segment does not cross myri10ge->tx_boundary, so this makes segment
2607 * counting tricky. So rather than try to count segments up front, we
2608 * just give up if there are too few segments to hold a reasonably
2609 * fragmented packet currently available. If we run
2610 * out of segments while preparing a packet for DMA, we just linearize
2614 static netdev_tx_t myri10ge_xmit(struct sk_buff *skb,
2615 struct net_device *dev)
2617 struct myri10ge_priv *mgp = netdev_priv(dev);
2618 struct myri10ge_slice_state *ss;
2619 struct mcp_kreq_ether_send *req;
2620 struct myri10ge_tx_buf *tx;
2622 struct netdev_queue *netdev_queue;
2625 __be32 high_swapped;
2627 int idx, avail, frag_cnt, frag_idx, count, mss, max_segments;
2628 u16 pseudo_hdr_offset, cksum_offset, queue;
2629 int cum_len, seglen, boundary, rdma_count;
2632 queue = skb_get_queue_mapping(skb);
2633 ss = &mgp->ss[queue];
2634 netdev_queue = netdev_get_tx_queue(mgp->dev, queue);
2639 avail = tx->mask - 1 - (tx->req - tx->done);
2642 max_segments = MXGEFW_MAX_SEND_DESC;
2644 if (skb_is_gso(skb)) {
2645 mss = skb_shinfo(skb)->gso_size;
2646 max_segments = MYRI10GE_MAX_SEND_DESC_TSO;
2649 if ((unlikely(avail < max_segments))) {
2650 /* we are out of transmit resources */
2652 netif_tx_stop_queue(netdev_queue);
2653 return NETDEV_TX_BUSY;
2656 /* Setup checksum offloading, if needed */
2658 pseudo_hdr_offset = 0;
2660 flags = (MXGEFW_FLAGS_NO_TSO | MXGEFW_FLAGS_FIRST);
2661 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
2662 cksum_offset = skb_checksum_start_offset(skb);
2663 pseudo_hdr_offset = cksum_offset + skb->csum_offset;
2664 /* If the headers are excessively large, then we must
2665 * fall back to a software checksum */
2666 if (unlikely(!mss && (cksum_offset > 255 ||
2667 pseudo_hdr_offset > 127))) {
2668 if (skb_checksum_help(skb))
2671 pseudo_hdr_offset = 0;
2673 odd_flag = MXGEFW_FLAGS_ALIGN_ODD;
2674 flags |= MXGEFW_FLAGS_CKSUM;
2680 if (mss) { /* TSO */
2681 /* this removes any CKSUM flag from before */
2682 flags = (MXGEFW_FLAGS_TSO_HDR | MXGEFW_FLAGS_FIRST);
2684 /* negative cum_len signifies to the
2685 * send loop that we are still in the
2686 * header portion of the TSO packet.
2687 * TSO header can be at most 1KB long */
2688 cum_len = -(skb_transport_offset(skb) + tcp_hdrlen(skb));
2690 /* for IPv6 TSO, the checksum offset stores the
2691 * TCP header length, to save the firmware from
2692 * the need to parse the headers */
2693 if (skb_is_gso_v6(skb)) {
2694 cksum_offset = tcp_hdrlen(skb);
2695 /* Can only handle headers <= max_tso6 long */
2696 if (unlikely(-cum_len > mgp->max_tso6))
2697 return myri10ge_sw_tso(skb, dev);
2699 /* for TSO, pseudo_hdr_offset holds mss.
2700 * The firmware figures out where to put
2701 * the checksum by parsing the header. */
2702 pseudo_hdr_offset = mss;
2704 /* Mark small packets, and pad out tiny packets */
2705 if (skb->len <= MXGEFW_SEND_SMALL_SIZE) {
2706 flags |= MXGEFW_FLAGS_SMALL;
2708 /* pad frames to at least ETH_ZLEN bytes */
2709 if (eth_skb_pad(skb)) {
2710 /* The packet is gone, so we must
2712 ss->stats.tx_dropped += 1;
2713 return NETDEV_TX_OK;
2717 /* map the skb for DMA */
2718 len = skb_headlen(skb);
2719 bus = dma_map_single(&mgp->pdev->dev, skb->data, len, DMA_TO_DEVICE);
2720 if (unlikely(dma_mapping_error(&mgp->pdev->dev, bus)))
2723 idx = tx->req & tx->mask;
2724 tx->info[idx].skb = skb;
2725 dma_unmap_addr_set(&tx->info[idx], bus, bus);
2726 dma_unmap_len_set(&tx->info[idx], len, len);
2728 frag_cnt = skb_shinfo(skb)->nr_frags;
2733 /* "rdma_count" is the number of RDMAs belonging to the
2734 * current packet BEFORE the current send request. For
2735 * non-TSO packets, this is equal to "count".
2736 * For TSO packets, rdma_count needs to be reset
2737 * to 0 after a segment cut.
2739 * The rdma_count field of the send request is
2740 * the number of RDMAs of the packet starting at
2741 * that request. For TSO send requests with one ore more cuts
2742 * in the middle, this is the number of RDMAs starting
2743 * after the last cut in the request. All previous
2744 * segments before the last cut implicitly have 1 RDMA.
2746 * Since the number of RDMAs is not known beforehand,
2747 * it must be filled-in retroactively - after each
2748 * segmentation cut or at the end of the entire packet.
2752 /* Break the SKB or Fragment up into pieces which
2753 * do not cross mgp->tx_boundary */
2754 low = MYRI10GE_LOWPART_TO_U32(bus);
2755 high_swapped = htonl(MYRI10GE_HIGHPART_TO_U32(bus));
2760 if (unlikely(count == max_segments))
2761 goto abort_linearize;
2764 (low + mgp->tx_boundary) & ~(mgp->tx_boundary - 1);
2765 seglen = boundary - low;
2768 flags_next = flags & ~MXGEFW_FLAGS_FIRST;
2769 cum_len_next = cum_len + seglen;
2770 if (mss) { /* TSO */
2771 (req - rdma_count)->rdma_count = rdma_count + 1;
2773 if (likely(cum_len >= 0)) { /* payload */
2774 int next_is_first, chop;
2776 chop = (cum_len_next > mss);
2777 cum_len_next = cum_len_next % mss;
2778 next_is_first = (cum_len_next == 0);
2779 flags |= chop * MXGEFW_FLAGS_TSO_CHOP;
2780 flags_next |= next_is_first *
2782 rdma_count |= -(chop | next_is_first);
2783 rdma_count += chop & ~next_is_first;
2784 } else if (likely(cum_len_next >= 0)) { /* header ends */
2790 small = (mss <= MXGEFW_SEND_SMALL_SIZE);
2791 flags_next = MXGEFW_FLAGS_TSO_PLD |
2792 MXGEFW_FLAGS_FIRST |
2793 (small * MXGEFW_FLAGS_SMALL);
2796 req->addr_high = high_swapped;
2797 req->addr_low = htonl(low);
2798 req->pseudo_hdr_offset = htons(pseudo_hdr_offset);
2799 req->pad = 0; /* complete solid 16-byte block; does this matter? */
2800 req->rdma_count = 1;
2801 req->length = htons(seglen);
2802 req->cksum_offset = cksum_offset;
2803 req->flags = flags | ((cum_len & 1) * odd_flag);
2807 cum_len = cum_len_next;
2812 if (cksum_offset != 0 && !(mss && skb_is_gso_v6(skb))) {
2813 if (unlikely(cksum_offset > seglen))
2814 cksum_offset -= seglen;
2819 if (frag_idx == frag_cnt)
2822 /* map next fragment for DMA */
2823 frag = &skb_shinfo(skb)->frags[frag_idx];
2825 len = skb_frag_size(frag);
2826 bus = skb_frag_dma_map(&mgp->pdev->dev, frag, 0, len,
2828 if (unlikely(dma_mapping_error(&mgp->pdev->dev, bus))) {
2829 myri10ge_unmap_tx_dma(mgp, tx, idx);
2832 idx = (count + tx->req) & tx->mask;
2833 dma_unmap_addr_set(&tx->info[idx], bus, bus);
2834 dma_unmap_len_set(&tx->info[idx], len, len);
2837 (req - rdma_count)->rdma_count = rdma_count;
2841 req->flags |= MXGEFW_FLAGS_TSO_LAST;
2842 } while (!(req->flags & (MXGEFW_FLAGS_TSO_CHOP |
2843 MXGEFW_FLAGS_FIRST)));
2844 idx = ((count - 1) + tx->req) & tx->mask;
2845 tx->info[idx].last = 1;
2846 myri10ge_submit_req(tx, tx->req_list, count);
2847 /* if using multiple tx queues, make sure NIC polls the
2849 if ((mgp->dev->real_num_tx_queues > 1) && tx->queue_active == 0) {
2850 tx->queue_active = 1;
2851 put_be32(htonl(1), tx->send_go);
2855 if ((avail - count) < MXGEFW_MAX_SEND_DESC) {
2857 netif_tx_stop_queue(netdev_queue);
2859 return NETDEV_TX_OK;
2862 myri10ge_unmap_tx_dma(mgp, tx, idx);
2864 if (skb_is_gso(skb)) {
2865 netdev_err(mgp->dev, "TSO but wanted to linearize?!?!?\n");
2869 if (skb_linearize(skb))
2876 dev_kfree_skb_any(skb);
2877 ss->stats.tx_dropped += 1;
2878 return NETDEV_TX_OK;
2882 static netdev_tx_t myri10ge_sw_tso(struct sk_buff *skb,
2883 struct net_device *dev)
2885 struct sk_buff *segs, *curr, *next;
2886 struct myri10ge_priv *mgp = netdev_priv(dev);
2887 struct myri10ge_slice_state *ss;
2890 segs = skb_gso_segment(skb, dev->features & ~NETIF_F_TSO6);
2894 skb_list_walk_safe(segs, curr, next) {
2895 skb_mark_not_on_list(curr);
2896 status = myri10ge_xmit(curr, dev);
2898 dev_kfree_skb_any(curr);
2903 dev_kfree_skb_any(segs);
2908 dev_kfree_skb_any(skb);
2909 return NETDEV_TX_OK;
2912 ss = &mgp->ss[skb_get_queue_mapping(skb)];
2913 dev_kfree_skb_any(skb);
2914 ss->stats.tx_dropped += 1;
2915 return NETDEV_TX_OK;
2918 static void myri10ge_get_stats(struct net_device *dev,
2919 struct rtnl_link_stats64 *stats)
2921 const struct myri10ge_priv *mgp = netdev_priv(dev);
2922 const struct myri10ge_slice_netstats *slice_stats;
2925 for (i = 0; i < mgp->num_slices; i++) {
2926 slice_stats = &mgp->ss[i].stats;
2927 stats->rx_packets += slice_stats->rx_packets;
2928 stats->tx_packets += slice_stats->tx_packets;
2929 stats->rx_bytes += slice_stats->rx_bytes;
2930 stats->tx_bytes += slice_stats->tx_bytes;
2931 stats->rx_dropped += slice_stats->rx_dropped;
2932 stats->tx_dropped += slice_stats->tx_dropped;
2936 static void myri10ge_set_multicast_list(struct net_device *dev)
2938 struct myri10ge_priv *mgp = netdev_priv(dev);
2939 struct myri10ge_cmd cmd;
2940 struct netdev_hw_addr *ha;
2941 __be32 data[2] = { 0, 0 };
2944 /* can be called from atomic contexts,
2945 * pass 1 to force atomicity in myri10ge_send_cmd() */
2946 myri10ge_change_promisc(mgp, dev->flags & IFF_PROMISC, 1);
2948 /* This firmware is known to not support multicast */
2949 if (!mgp->fw_multicast_support)
2952 /* Disable multicast filtering */
2954 err = myri10ge_send_cmd(mgp, MXGEFW_ENABLE_ALLMULTI, &cmd, 1);
2956 netdev_err(dev, "Failed MXGEFW_ENABLE_ALLMULTI, error status: %d\n",
2961 if ((dev->flags & IFF_ALLMULTI) || mgp->adopted_rx_filter_bug) {
2962 /* request to disable multicast filtering, so quit here */
2966 /* Flush the filters */
2968 err = myri10ge_send_cmd(mgp, MXGEFW_LEAVE_ALL_MULTICAST_GROUPS,
2971 netdev_err(dev, "Failed MXGEFW_LEAVE_ALL_MULTICAST_GROUPS, error status: %d\n",
2976 /* Walk the multicast list, and add each address */
2977 netdev_for_each_mc_addr(ha, dev) {
2978 memcpy(data, &ha->addr, ETH_ALEN);
2979 cmd.data0 = ntohl(data[0]);
2980 cmd.data1 = ntohl(data[1]);
2981 err = myri10ge_send_cmd(mgp, MXGEFW_JOIN_MULTICAST_GROUP,
2985 netdev_err(dev, "Failed MXGEFW_JOIN_MULTICAST_GROUP, error status:%d %pM\n",
2990 /* Enable multicast filtering */
2991 err = myri10ge_send_cmd(mgp, MXGEFW_DISABLE_ALLMULTI, &cmd, 1);
2993 netdev_err(dev, "Failed MXGEFW_DISABLE_ALLMULTI, error status: %d\n",
3004 static int myri10ge_set_mac_address(struct net_device *dev, void *addr)
3006 struct sockaddr *sa = addr;
3007 struct myri10ge_priv *mgp = netdev_priv(dev);
3010 if (!is_valid_ether_addr(sa->sa_data))
3011 return -EADDRNOTAVAIL;
3013 status = myri10ge_update_mac_address(mgp, sa->sa_data);
3015 netdev_err(dev, "changing mac address failed with %d\n",
3020 /* change the dev structure */
3021 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
3025 static int myri10ge_change_mtu(struct net_device *dev, int new_mtu)
3027 struct myri10ge_priv *mgp = netdev_priv(dev);
3029 netdev_info(dev, "changing mtu from %d to %d\n", dev->mtu, new_mtu);
3031 /* if we change the mtu on an active device, we must
3032 * reset the device so the firmware sees the change */
3033 myri10ge_close(dev);
3043 * Enable ECRC to align PCI-E Completion packets on an 8-byte boundary.
3044 * Only do it if the bridge is a root port since we don't want to disturb
3045 * any other device, except if forced with myri10ge_ecrc_enable > 1.
3048 static void myri10ge_enable_ecrc(struct myri10ge_priv *mgp)
3050 struct pci_dev *bridge = mgp->pdev->bus->self;
3051 struct device *dev = &mgp->pdev->dev;
3056 if (!myri10ge_ecrc_enable || !bridge)
3059 /* check that the bridge is a root port */
3060 if (pci_pcie_type(bridge) != PCI_EXP_TYPE_ROOT_PORT) {
3061 if (myri10ge_ecrc_enable > 1) {
3062 struct pci_dev *prev_bridge, *old_bridge = bridge;
3064 /* Walk the hierarchy up to the root port
3065 * where ECRC has to be enabled */
3067 prev_bridge = bridge;
3068 bridge = bridge->bus->self;
3069 if (!bridge || prev_bridge == bridge) {
3071 "Failed to find root port"
3072 " to force ECRC\n");
3075 } while (pci_pcie_type(bridge) !=
3076 PCI_EXP_TYPE_ROOT_PORT);
3079 "Forcing ECRC on non-root port %s"
3080 " (enabling on root port %s)\n",
3081 pci_name(old_bridge), pci_name(bridge));
3084 "Not enabling ECRC on non-root port %s\n",
3090 cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
3094 ret = pci_read_config_dword(bridge, cap + PCI_ERR_CAP, &err_cap);
3096 dev_err(dev, "failed reading ext-conf-space of %s\n",
3098 dev_err(dev, "\t pci=nommconf in use? "
3099 "or buggy/incomplete/absent ACPI MCFG attr?\n");
3102 if (!(err_cap & PCI_ERR_CAP_ECRC_GENC))
3105 err_cap |= PCI_ERR_CAP_ECRC_GENE;
3106 pci_write_config_dword(bridge, cap + PCI_ERR_CAP, err_cap);
3107 dev_info(dev, "Enabled ECRC on upstream bridge %s\n", pci_name(bridge));
3111 * The Lanai Z8E PCI-E interface achieves higher Read-DMA throughput
3112 * when the PCI-E Completion packets are aligned on an 8-byte
3113 * boundary. Some PCI-E chip sets always align Completion packets; on
3114 * the ones that do not, the alignment can be enforced by enabling
3115 * ECRC generation (if supported).
3117 * When PCI-E Completion packets are not aligned, it is actually more
3118 * efficient to limit Read-DMA transactions to 2KB, rather than 4KB.
3120 * If the driver can neither enable ECRC nor verify that it has
3121 * already been enabled, then it must use a firmware image which works
3122 * around unaligned completion packets (myri10ge_rss_ethp_z8e.dat), and it
3123 * should also ensure that it never gives the device a Read-DMA which is
3124 * larger than 2KB by setting the tx_boundary to 2KB. If ECRC is
3125 * enabled, then the driver should use the aligned (myri10ge_rss_eth_z8e.dat)
3126 * firmware image, and set tx_boundary to 4KB.
3129 static void myri10ge_firmware_probe(struct myri10ge_priv *mgp)
3131 struct pci_dev *pdev = mgp->pdev;
3132 struct device *dev = &pdev->dev;
3135 mgp->tx_boundary = 4096;
3137 * Verify the max read request size was set to 4KB
3138 * before trying the test with 4KB.
3140 status = pcie_get_readrq(pdev);
3142 dev_err(dev, "Couldn't read max read req size: %d\n", status);
3145 if (status != 4096) {
3146 dev_warn(dev, "Max Read Request size != 4096 (%d)\n", status);
3147 mgp->tx_boundary = 2048;
3150 * load the optimized firmware (which assumes aligned PCIe
3151 * completions) in order to see if it works on this host.
3153 set_fw_name(mgp, myri10ge_fw_aligned, false);
3154 status = myri10ge_load_firmware(mgp, 1);
3160 * Enable ECRC if possible
3162 myri10ge_enable_ecrc(mgp);
3165 * Run a DMA test which watches for unaligned completions and
3166 * aborts on the first one seen.
3169 status = myri10ge_dma_test(mgp, MXGEFW_CMD_UNALIGNED_TEST);
3171 return; /* keep the aligned firmware */
3173 if (status != -E2BIG)
3174 dev_warn(dev, "DMA test failed: %d\n", status);
3175 if (status == -ENOSYS)
3176 dev_warn(dev, "Falling back to ethp! "
3177 "Please install up to date fw\n");
3179 /* fall back to using the unaligned firmware */
3180 mgp->tx_boundary = 2048;
3181 set_fw_name(mgp, myri10ge_fw_unaligned, false);
3184 static void myri10ge_select_firmware(struct myri10ge_priv *mgp)
3188 if (myri10ge_force_firmware == 0) {
3192 pcie_capability_read_word(mgp->pdev, PCI_EXP_LNKSTA, &lnk);
3193 link_width = (lnk >> 4) & 0x3f;
3195 /* Check to see if Link is less than 8 or if the
3196 * upstream bridge is known to provide aligned
3198 if (link_width < 8) {
3199 dev_info(&mgp->pdev->dev, "PCIE x%d Link\n",
3201 mgp->tx_boundary = 4096;
3202 set_fw_name(mgp, myri10ge_fw_aligned, false);
3204 myri10ge_firmware_probe(mgp);
3207 if (myri10ge_force_firmware == 1) {
3208 dev_info(&mgp->pdev->dev,
3209 "Assuming aligned completions (forced)\n");
3210 mgp->tx_boundary = 4096;
3211 set_fw_name(mgp, myri10ge_fw_aligned, false);
3213 dev_info(&mgp->pdev->dev,
3214 "Assuming unaligned completions (forced)\n");
3215 mgp->tx_boundary = 2048;
3216 set_fw_name(mgp, myri10ge_fw_unaligned, false);
3220 kernel_param_lock(THIS_MODULE);
3221 if (myri10ge_fw_name != NULL) {
3222 char *fw_name = kstrdup(myri10ge_fw_name, GFP_KERNEL);
3225 set_fw_name(mgp, fw_name, true);
3228 kernel_param_unlock(THIS_MODULE);
3230 if (mgp->board_number < MYRI10GE_MAX_BOARDS &&
3231 myri10ge_fw_names[mgp->board_number] != NULL &&
3232 strlen(myri10ge_fw_names[mgp->board_number])) {
3233 set_fw_name(mgp, myri10ge_fw_names[mgp->board_number], false);
3237 dev_info(&mgp->pdev->dev, "overriding firmware to %s\n",
3241 static void myri10ge_mask_surprise_down(struct pci_dev *pdev)
3243 struct pci_dev *bridge = pdev->bus->self;
3250 cap = pci_find_ext_capability(bridge, PCI_EXT_CAP_ID_ERR);
3252 /* a sram parity error can cause a surprise link
3253 * down; since we expect and can recover from sram
3254 * parity errors, mask surprise link down events */
3255 pci_read_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, &mask);
3257 pci_write_config_dword(bridge, cap + PCI_ERR_UNCOR_MASK, mask);
3261 static int __maybe_unused myri10ge_suspend(struct device *dev)
3263 struct myri10ge_priv *mgp;
3264 struct net_device *netdev;
3266 mgp = dev_get_drvdata(dev);
3271 netif_device_detach(netdev);
3272 if (netif_running(netdev)) {
3273 netdev_info(netdev, "closing\n");
3275 myri10ge_close(netdev);
3278 myri10ge_dummy_rdma(mgp, 0);
3283 static int __maybe_unused myri10ge_resume(struct device *dev)
3285 struct pci_dev *pdev = to_pci_dev(dev);
3286 struct myri10ge_priv *mgp;
3287 struct net_device *netdev;
3291 mgp = pci_get_drvdata(pdev);
3295 msleep(5); /* give card time to respond */
3296 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3297 if (vendor == 0xffff) {
3298 netdev_err(mgp->dev, "device disappeared!\n");
3302 myri10ge_reset(mgp);
3303 myri10ge_dummy_rdma(mgp, 1);
3305 if (netif_running(netdev)) {
3307 status = myri10ge_open(netdev);
3310 goto abort_with_enabled;
3313 netif_device_attach(netdev);
3321 static u32 myri10ge_read_reboot(struct myri10ge_priv *mgp)
3323 struct pci_dev *pdev = mgp->pdev;
3324 int vs = mgp->vendor_specific_offset;
3327 /*enter read32 mode */
3328 pci_write_config_byte(pdev, vs + 0x10, 0x3);
3330 /*read REBOOT_STATUS (0xfffffff0) */
3331 pci_write_config_dword(pdev, vs + 0x18, 0xfffffff0);
3332 pci_read_config_dword(pdev, vs + 0x14, &reboot);
3337 myri10ge_check_slice(struct myri10ge_slice_state *ss, int *reset_needed,
3338 int *busy_slice_cnt, u32 rx_pause_cnt)
3340 struct myri10ge_priv *mgp = ss->mgp;
3341 int slice = ss - mgp->ss;
3343 if (ss->tx.req != ss->tx.done &&
3344 ss->tx.done == ss->watchdog_tx_done &&
3345 ss->watchdog_tx_req != ss->watchdog_tx_done) {
3346 /* nic seems like it might be stuck.. */
3347 if (rx_pause_cnt != mgp->watchdog_pause) {
3348 if (net_ratelimit())
3349 netdev_warn(mgp->dev, "slice %d: TX paused, "
3350 "check link partner\n", slice);
3352 netdev_warn(mgp->dev,
3353 "slice %d: TX stuck %d %d %d %d %d %d\n",
3354 slice, ss->tx.queue_active, ss->tx.req,
3355 ss->tx.done, ss->tx.pkt_start,
3357 (int)ntohl(mgp->ss[slice].fw_stats->
3363 if (ss->watchdog_tx_done != ss->tx.done ||
3364 ss->watchdog_rx_done != ss->rx_done.cnt) {
3365 *busy_slice_cnt += 1;
3367 ss->watchdog_tx_done = ss->tx.done;
3368 ss->watchdog_tx_req = ss->tx.req;
3369 ss->watchdog_rx_done = ss->rx_done.cnt;
3373 * This watchdog is used to check whether the board has suffered
3374 * from a parity error and needs to be recovered.
3376 static void myri10ge_watchdog(struct work_struct *work)
3378 struct myri10ge_priv *mgp =
3379 container_of(work, struct myri10ge_priv, watchdog_work);
3380 struct myri10ge_slice_state *ss;
3381 u32 reboot, rx_pause_cnt;
3382 int status, rebooted;
3384 int reset_needed = 0;
3385 int busy_slice_cnt = 0;
3388 mgp->watchdog_resets++;
3389 pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
3391 if ((cmd & PCI_COMMAND_MASTER) == 0) {
3392 /* Bus master DMA disabled? Check to see
3393 * if the card rebooted due to a parity error
3394 * For now, just report it */
3395 reboot = myri10ge_read_reboot(mgp);
3396 netdev_err(mgp->dev, "NIC rebooted (0x%x),%s resetting\n",
3397 reboot, myri10ge_reset_recover ? "" : " not");
3398 if (myri10ge_reset_recover == 0)
3403 myri10ge_close(mgp->dev);
3404 myri10ge_reset_recover--;
3407 * A rebooted nic will come back with config space as
3408 * it was after power was applied to PCIe bus.
3409 * Attempt to restore config space which was saved
3410 * when the driver was loaded, or the last time the
3411 * nic was resumed from power saving mode.
3413 pci_restore_state(mgp->pdev);
3415 /* save state again for accounting reasons */
3416 pci_save_state(mgp->pdev);
3419 /* if we get back -1's from our slot, perhaps somebody
3420 * powered off our card. Don't try to reset it in
3422 if (cmd == 0xffff) {
3423 pci_read_config_word(mgp->pdev, PCI_VENDOR_ID, &vendor);
3424 if (vendor == 0xffff) {
3425 netdev_err(mgp->dev, "device disappeared!\n");
3429 /* Perhaps it is a software error. See if stuck slice
3430 * has recovered, reset if not */
3431 rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
3432 for (i = 0; i < mgp->num_slices; i++) {
3435 myri10ge_check_slice(ss, &reset_needed,
3441 if (!reset_needed) {
3442 netdev_dbg(mgp->dev, "not resetting\n");
3446 netdev_err(mgp->dev, "device timeout, resetting\n");
3451 myri10ge_close(mgp->dev);
3453 status = myri10ge_load_firmware(mgp, 1);
3455 netdev_err(mgp->dev, "failed to load firmware\n");
3457 myri10ge_open(mgp->dev);
3462 * We use our own timer routine rather than relying upon
3463 * netdev->tx_timeout because we have a very large hardware transmit
3464 * queue. Due to the large queue, the netdev->tx_timeout function
3465 * cannot detect a NIC with a parity error in a timely fashion if the
3466 * NIC is lightly loaded.
3468 static void myri10ge_watchdog_timer(struct timer_list *t)
3470 struct myri10ge_priv *mgp;
3471 struct myri10ge_slice_state *ss;
3472 int i, reset_needed, busy_slice_cnt;
3476 mgp = from_timer(mgp, t, watchdog_timer);
3478 rx_pause_cnt = ntohl(mgp->ss[0].fw_stats->dropped_pause);
3480 for (i = 0, reset_needed = 0;
3481 i < mgp->num_slices && reset_needed == 0; ++i) {
3484 if (ss->rx_small.watchdog_needed) {
3485 myri10ge_alloc_rx_pages(mgp, &ss->rx_small,
3486 mgp->small_bytes + MXGEFW_PAD,
3488 if (ss->rx_small.fill_cnt - ss->rx_small.cnt >=
3489 myri10ge_fill_thresh)
3490 ss->rx_small.watchdog_needed = 0;
3492 if (ss->rx_big.watchdog_needed) {
3493 myri10ge_alloc_rx_pages(mgp, &ss->rx_big,
3495 if (ss->rx_big.fill_cnt - ss->rx_big.cnt >=
3496 myri10ge_fill_thresh)
3497 ss->rx_big.watchdog_needed = 0;
3499 myri10ge_check_slice(ss, &reset_needed, &busy_slice_cnt,
3502 /* if we've sent or received no traffic, poll the NIC to
3503 * ensure it is still there. Otherwise, we risk not noticing
3504 * an error in a timely fashion */
3505 if (busy_slice_cnt == 0) {
3506 pci_read_config_word(mgp->pdev, PCI_COMMAND, &cmd);
3507 if ((cmd & PCI_COMMAND_MASTER) == 0) {
3511 mgp->watchdog_pause = rx_pause_cnt;
3514 schedule_work(&mgp->watchdog_work);
3517 mod_timer(&mgp->watchdog_timer,
3518 jiffies + myri10ge_watchdog_timeout * HZ);
3522 static void myri10ge_free_slices(struct myri10ge_priv *mgp)
3524 struct myri10ge_slice_state *ss;
3525 struct pci_dev *pdev = mgp->pdev;
3529 if (mgp->ss == NULL)
3532 for (i = 0; i < mgp->num_slices; i++) {
3534 if (ss->rx_done.entry != NULL) {
3535 bytes = mgp->max_intr_slots *
3536 sizeof(*ss->rx_done.entry);
3537 dma_free_coherent(&pdev->dev, bytes,
3538 ss->rx_done.entry, ss->rx_done.bus);
3539 ss->rx_done.entry = NULL;
3541 if (ss->fw_stats != NULL) {
3542 bytes = sizeof(*ss->fw_stats);
3543 dma_free_coherent(&pdev->dev, bytes,
3544 ss->fw_stats, ss->fw_stats_bus);
3545 ss->fw_stats = NULL;
3547 __netif_napi_del(&ss->napi);
3549 /* Wait till napi structs are no longer used, and then free ss. */
3555 static int myri10ge_alloc_slices(struct myri10ge_priv *mgp)
3557 struct myri10ge_slice_state *ss;
3558 struct pci_dev *pdev = mgp->pdev;
3562 bytes = sizeof(*mgp->ss) * mgp->num_slices;
3563 mgp->ss = kzalloc(bytes, GFP_KERNEL);
3564 if (mgp->ss == NULL) {
3568 for (i = 0; i < mgp->num_slices; i++) {
3570 bytes = mgp->max_intr_slots * sizeof(*ss->rx_done.entry);
3571 ss->rx_done.entry = dma_alloc_coherent(&pdev->dev, bytes,
3574 if (ss->rx_done.entry == NULL)
3576 bytes = sizeof(*ss->fw_stats);
3577 ss->fw_stats = dma_alloc_coherent(&pdev->dev, bytes,
3580 if (ss->fw_stats == NULL)
3584 netif_napi_add(ss->dev, &ss->napi, myri10ge_poll,
3585 myri10ge_napi_weight);
3589 myri10ge_free_slices(mgp);
3594 * This function determines the number of slices supported.
3595 * The number slices is the minimum of the number of CPUS,
3596 * the number of MSI-X irqs supported, the number of slices
3597 * supported by the firmware
3599 static void myri10ge_probe_slices(struct myri10ge_priv *mgp)
3601 struct myri10ge_cmd cmd;
3602 struct pci_dev *pdev = mgp->pdev;
3605 int i, status, ncpus;
3607 mgp->num_slices = 1;
3608 ncpus = netif_get_num_default_rss_queues();
3610 if (myri10ge_max_slices == 1 || !pdev->msix_cap ||
3611 (myri10ge_max_slices == -1 && ncpus < 2))
3614 /* try to load the slice aware rss firmware */
3615 old_fw = mgp->fw_name;
3616 old_allocated = mgp->fw_name_allocated;
3617 /* don't free old_fw if we override it. */
3618 mgp->fw_name_allocated = false;
3620 if (myri10ge_fw_name != NULL) {
3621 dev_info(&mgp->pdev->dev, "overriding rss firmware to %s\n",
3623 set_fw_name(mgp, myri10ge_fw_name, false);
3624 } else if (old_fw == myri10ge_fw_aligned)
3625 set_fw_name(mgp, myri10ge_fw_rss_aligned, false);
3627 set_fw_name(mgp, myri10ge_fw_rss_unaligned, false);
3628 status = myri10ge_load_firmware(mgp, 0);
3630 dev_info(&pdev->dev, "Rss firmware not found\n");
3636 /* hit the board with a reset to ensure it is alive */
3637 memset(&cmd, 0, sizeof(cmd));
3638 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_RESET, &cmd, 0);
3640 dev_err(&mgp->pdev->dev, "failed reset\n");
3644 mgp->max_intr_slots = cmd.data0 / sizeof(struct mcp_slot);
3646 /* tell it the size of the interrupt queues */
3647 cmd.data0 = mgp->max_intr_slots * sizeof(struct mcp_slot);
3648 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_SET_INTRQ_SIZE, &cmd, 0);
3650 dev_err(&mgp->pdev->dev, "failed MXGEFW_CMD_SET_INTRQ_SIZE\n");
3654 /* ask the maximum number of slices it supports */
3655 status = myri10ge_send_cmd(mgp, MXGEFW_CMD_GET_MAX_RSS_QUEUES, &cmd, 0);
3659 mgp->num_slices = cmd.data0;
3661 /* Only allow multiple slices if MSI-X is usable */
3662 if (!myri10ge_msi) {
3666 /* if the admin did not specify a limit to how many
3667 * slices we should use, cap it automatically to the
3668 * number of CPUs currently online */
3669 if (myri10ge_max_slices == -1)
3670 myri10ge_max_slices = ncpus;
3672 if (mgp->num_slices > myri10ge_max_slices)
3673 mgp->num_slices = myri10ge_max_slices;
3675 /* Now try to allocate as many MSI-X vectors as we have
3676 * slices. We give up on MSI-X if we can only get a single
3679 mgp->msix_vectors = kcalloc(mgp->num_slices, sizeof(*mgp->msix_vectors),
3681 if (mgp->msix_vectors == NULL)
3683 for (i = 0; i < mgp->num_slices; i++) {
3684 mgp->msix_vectors[i].entry = i;
3687 while (mgp->num_slices > 1) {
3688 mgp->num_slices = rounddown_pow_of_two(mgp->num_slices);
3689 if (mgp->num_slices == 1)
3691 status = pci_enable_msix_range(pdev,
3698 pci_disable_msix(pdev);
3700 if (status == mgp->num_slices) {
3705 mgp->num_slices = status;
3710 if (mgp->msix_vectors != NULL) {
3711 kfree(mgp->msix_vectors);
3712 mgp->msix_vectors = NULL;
3716 mgp->num_slices = 1;
3717 set_fw_name(mgp, old_fw, old_allocated);
3718 myri10ge_load_firmware(mgp, 0);
3721 static const struct net_device_ops myri10ge_netdev_ops = {
3722 .ndo_open = myri10ge_open,
3723 .ndo_stop = myri10ge_close,
3724 .ndo_start_xmit = myri10ge_xmit,
3725 .ndo_get_stats64 = myri10ge_get_stats,
3726 .ndo_validate_addr = eth_validate_addr,
3727 .ndo_change_mtu = myri10ge_change_mtu,
3728 .ndo_set_rx_mode = myri10ge_set_multicast_list,
3729 .ndo_set_mac_address = myri10ge_set_mac_address,
3732 static int myri10ge_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
3734 struct net_device *netdev;
3735 struct myri10ge_priv *mgp;
3736 struct device *dev = &pdev->dev;
3738 int status = -ENXIO;
3740 unsigned hdr_offset, ss_offset;
3741 static int board_number;
3743 netdev = alloc_etherdev_mq(sizeof(*mgp), MYRI10GE_MAX_SLICES);
3747 SET_NETDEV_DEV(netdev, &pdev->dev);
3749 mgp = netdev_priv(netdev);
3752 mgp->pause = myri10ge_flow_control;
3753 mgp->intr_coal_delay = myri10ge_intr_coal_delay;
3754 mgp->msg_enable = netif_msg_init(myri10ge_debug, MYRI10GE_MSG_DEFAULT);
3755 mgp->board_number = board_number;
3756 init_waitqueue_head(&mgp->down_wq);
3758 if (pci_enable_device(pdev)) {
3759 dev_err(&pdev->dev, "pci_enable_device call failed\n");
3761 goto abort_with_netdev;
3764 /* Find the vendor-specific cap so we can check
3765 * the reboot register later on */
3766 mgp->vendor_specific_offset
3767 = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
3769 /* Set our max read request to 4KB */
3770 status = pcie_set_readrq(pdev, 4096);
3772 dev_err(&pdev->dev, "Error %d writing PCI_EXP_DEVCTL\n",
3774 goto abort_with_enabled;
3777 myri10ge_mask_surprise_down(pdev);
3778 pci_set_master(pdev);
3780 status = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3784 "64-bit pci address mask was refused, trying 32-bit\n");
3785 status = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3788 dev_err(&pdev->dev, "Error %d setting DMA mask\n", status);
3789 goto abort_with_enabled;
3791 mgp->cmd = dma_alloc_coherent(&pdev->dev, sizeof(*mgp->cmd),
3792 &mgp->cmd_bus, GFP_KERNEL);
3795 goto abort_with_enabled;
3798 mgp->board_span = pci_resource_len(pdev, 0);
3799 mgp->iomem_base = pci_resource_start(pdev, 0);
3800 mgp->wc_cookie = arch_phys_wc_add(mgp->iomem_base, mgp->board_span);
3801 mgp->sram = ioremap_wc(mgp->iomem_base, mgp->board_span);
3802 if (mgp->sram == NULL) {
3803 dev_err(&pdev->dev, "ioremap failed for %ld bytes at 0x%lx\n",
3804 mgp->board_span, mgp->iomem_base);
3806 goto abort_with_mtrr;
3809 swab32(readl(mgp->sram + MCP_HEADER_PTR_OFFSET)) & 0xffffc;
3810 ss_offset = hdr_offset + offsetof(struct mcp_gen_header, string_specs);
3811 mgp->sram_size = swab32(readl(mgp->sram + ss_offset));
3812 if (mgp->sram_size > mgp->board_span ||
3813 mgp->sram_size <= MYRI10GE_FW_OFFSET) {
3815 "invalid sram_size %dB or board span %ldB\n",
3816 mgp->sram_size, mgp->board_span);
3818 goto abort_with_ioremap;
3820 memcpy_fromio(mgp->eeprom_strings,
3821 mgp->sram + mgp->sram_size, MYRI10GE_EEPROM_STRINGS_SIZE);
3822 memset(mgp->eeprom_strings + MYRI10GE_EEPROM_STRINGS_SIZE - 2, 0, 2);
3823 status = myri10ge_read_mac_addr(mgp);
3825 goto abort_with_ioremap;
3827 for (i = 0; i < ETH_ALEN; i++)
3828 netdev->dev_addr[i] = mgp->mac_addr[i];
3830 myri10ge_select_firmware(mgp);
3832 status = myri10ge_load_firmware(mgp, 1);
3834 dev_err(&pdev->dev, "failed to load firmware\n");
3835 goto abort_with_ioremap;
3837 myri10ge_probe_slices(mgp);
3838 status = myri10ge_alloc_slices(mgp);
3840 dev_err(&pdev->dev, "failed to alloc slice state\n");
3841 goto abort_with_firmware;
3843 netif_set_real_num_tx_queues(netdev, mgp->num_slices);
3844 netif_set_real_num_rx_queues(netdev, mgp->num_slices);
3845 status = myri10ge_reset(mgp);
3847 dev_err(&pdev->dev, "failed reset\n");
3848 goto abort_with_slices;
3850 #ifdef CONFIG_MYRI10GE_DCA
3851 myri10ge_setup_dca(mgp);
3853 pci_set_drvdata(pdev, mgp);
3855 /* MTU range: 68 - 9000 */
3856 netdev->min_mtu = ETH_MIN_MTU;
3857 netdev->max_mtu = MYRI10GE_MAX_ETHER_MTU - ETH_HLEN;
3859 if (myri10ge_initial_mtu > netdev->max_mtu)
3860 myri10ge_initial_mtu = netdev->max_mtu;
3861 if (myri10ge_initial_mtu < netdev->min_mtu)
3862 myri10ge_initial_mtu = netdev->min_mtu;
3864 netdev->mtu = myri10ge_initial_mtu;
3866 netdev->netdev_ops = &myri10ge_netdev_ops;
3867 netdev->hw_features = mgp->features | NETIF_F_RXCSUM;
3869 /* fake NETIF_F_HW_VLAN_CTAG_RX for good GRO performance */
3870 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3872 netdev->features = netdev->hw_features;
3875 netdev->features |= NETIF_F_HIGHDMA;
3877 netdev->vlan_features |= mgp->features;
3878 if (mgp->fw_ver_tiny < 37)
3879 netdev->vlan_features &= ~NETIF_F_TSO6;
3880 if (mgp->fw_ver_tiny < 32)
3881 netdev->vlan_features &= ~NETIF_F_TSO;
3883 /* make sure we can get an irq, and that MSI can be
3884 * setup (if available). */
3885 status = myri10ge_request_irq(mgp);
3887 goto abort_with_slices;
3888 myri10ge_free_irq(mgp);
3890 /* Save configuration space to be restored if the
3891 * nic resets due to a parity error */
3892 pci_save_state(pdev);
3894 /* Setup the watchdog timer */
3895 timer_setup(&mgp->watchdog_timer, myri10ge_watchdog_timer, 0);
3897 netdev->ethtool_ops = &myri10ge_ethtool_ops;
3898 INIT_WORK(&mgp->watchdog_work, myri10ge_watchdog);
3899 status = register_netdev(netdev);
3901 dev_err(&pdev->dev, "register_netdev failed: %d\n", status);
3902 goto abort_with_state;
3904 if (mgp->msix_enabled)
3905 dev_info(dev, "%d MSI-X IRQs, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
3906 mgp->num_slices, mgp->tx_boundary, mgp->fw_name,
3907 (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
3909 dev_info(dev, "%s IRQ %d, tx bndry %d, fw %s, MTRR %s, WC Enabled\n",
3910 mgp->msi_enabled ? "MSI" : "xPIC",
3911 pdev->irq, mgp->tx_boundary, mgp->fw_name,
3912 (mgp->wc_cookie > 0 ? "Enabled" : "Disabled"));
3918 pci_restore_state(pdev);
3921 myri10ge_free_slices(mgp);
3923 abort_with_firmware:
3924 myri10ge_dummy_rdma(mgp, 0);
3927 if (mgp->mac_addr_string != NULL)
3929 "myri10ge_probe() failed: MAC=%s, SN=%ld\n",
3930 mgp->mac_addr_string, mgp->serial_number);
3934 arch_phys_wc_del(mgp->wc_cookie);
3935 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3936 mgp->cmd, mgp->cmd_bus);
3939 pci_disable_device(pdev);
3942 set_fw_name(mgp, NULL, false);
3943 free_netdev(netdev);
3950 * Does what is necessary to shutdown one Myrinet device. Called
3951 * once for each Myrinet card by the kernel when a module is
3954 static void myri10ge_remove(struct pci_dev *pdev)
3956 struct myri10ge_priv *mgp;
3957 struct net_device *netdev;
3959 mgp = pci_get_drvdata(pdev);
3963 cancel_work_sync(&mgp->watchdog_work);
3965 unregister_netdev(netdev);
3967 #ifdef CONFIG_MYRI10GE_DCA
3968 myri10ge_teardown_dca(mgp);
3970 myri10ge_dummy_rdma(mgp, 0);
3972 /* avoid a memory leak */
3973 pci_restore_state(pdev);
3976 arch_phys_wc_del(mgp->wc_cookie);
3977 myri10ge_free_slices(mgp);
3978 kfree(mgp->msix_vectors);
3979 dma_free_coherent(&pdev->dev, sizeof(*mgp->cmd),
3980 mgp->cmd, mgp->cmd_bus);
3982 set_fw_name(mgp, NULL, false);
3983 free_netdev(netdev);
3984 pci_disable_device(pdev);
3987 #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E 0x0008
3988 #define PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9 0x0009
3990 static const struct pci_device_id myri10ge_pci_tbl[] = {
3991 {PCI_DEVICE(PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E)},
3993 (PCI_VENDOR_ID_MYRICOM, PCI_DEVICE_ID_MYRICOM_MYRI10GE_Z8E_9)},
3997 MODULE_DEVICE_TABLE(pci, myri10ge_pci_tbl);
3999 static SIMPLE_DEV_PM_OPS(myri10ge_pm_ops, myri10ge_suspend, myri10ge_resume);
4001 static struct pci_driver myri10ge_driver = {
4003 .probe = myri10ge_probe,
4004 .remove = myri10ge_remove,
4005 .id_table = myri10ge_pci_tbl,
4006 .driver.pm = &myri10ge_pm_ops,
4009 #ifdef CONFIG_MYRI10GE_DCA
4011 myri10ge_notify_dca(struct notifier_block *nb, unsigned long event, void *p)
4013 int err = driver_for_each_device(&myri10ge_driver.driver,
4015 myri10ge_notify_dca_device);
4022 static struct notifier_block myri10ge_dca_notifier = {
4023 .notifier_call = myri10ge_notify_dca,
4027 #endif /* CONFIG_MYRI10GE_DCA */
4029 static __init int myri10ge_init_module(void)
4031 pr_info("Version %s\n", MYRI10GE_VERSION_STR);
4033 if (myri10ge_rss_hash > MXGEFW_RSS_HASH_TYPE_MAX) {
4034 pr_err("Illegal rssh hash type %d, defaulting to source port\n",
4036 myri10ge_rss_hash = MXGEFW_RSS_HASH_TYPE_SRC_PORT;
4038 #ifdef CONFIG_MYRI10GE_DCA
4039 dca_register_notify(&myri10ge_dca_notifier);
4041 if (myri10ge_max_slices > MYRI10GE_MAX_SLICES)
4042 myri10ge_max_slices = MYRI10GE_MAX_SLICES;
4044 return pci_register_driver(&myri10ge_driver);
4047 module_init(myri10ge_init_module);
4049 static __exit void myri10ge_cleanup_module(void)
4051 #ifdef CONFIG_MYRI10GE_DCA
4052 dca_unregister_notify(&myri10ge_dca_notifier);
4054 pci_unregister_driver(&myri10ge_driver);
4057 module_exit(myri10ge_cleanup_module);