| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c) 2005 Ammasso, Inc. All rights reserved. | 
|  | 3 | * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved. | 
|  | 4 | * | 
|  | 5 | * This software is available to you under a choice of one of two | 
|  | 6 | * licenses.  You may choose to be licensed under the terms of the GNU | 
|  | 7 | * General Public License (GPL) Version 2, available from the file | 
|  | 8 | * COPYING in the main directory of this source tree, or the | 
|  | 9 | * OpenIB.org BSD license below: | 
|  | 10 | * | 
|  | 11 | *     Redistribution and use in source and binary forms, with or | 
|  | 12 | *     without modification, are permitted provided that the following | 
|  | 13 | *     conditions are met: | 
|  | 14 | * | 
|  | 15 | *      - Redistributions of source code must retain the above | 
|  | 16 | *        copyright notice, this list of conditions and the following | 
|  | 17 | *        disclaimer. | 
|  | 18 | * | 
|  | 19 | *      - Redistributions in binary form must reproduce the above | 
|  | 20 | *        copyright notice, this list of conditions and the following | 
|  | 21 | *        disclaimer in the documentation and/or other materials | 
|  | 22 | *        provided with the distribution. | 
|  | 23 | * | 
|  | 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
|  | 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
|  | 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
|  | 27 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | 
|  | 28 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | 
|  | 29 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
|  | 30 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | 
|  | 31 | * SOFTWARE. | 
|  | 32 | */ | 
|  | 33 | #include <linux/module.h> | 
|  | 34 | #include <linux/moduleparam.h> | 
|  | 35 | #include <linux/pci.h> | 
|  | 36 | #include <linux/netdevice.h> | 
|  | 37 | #include <linux/etherdevice.h> | 
|  | 38 | #include <linux/inetdevice.h> | 
|  | 39 | #include <linux/delay.h> | 
|  | 40 | #include <linux/ethtool.h> | 
|  | 41 | #include <linux/mii.h> | 
|  | 42 | #include <linux/if_vlan.h> | 
|  | 43 | #include <linux/crc32.h> | 
|  | 44 | #include <linux/in.h> | 
|  | 45 | #include <linux/ip.h> | 
|  | 46 | #include <linux/tcp.h> | 
|  | 47 | #include <linux/init.h> | 
|  | 48 | #include <linux/dma-mapping.h> | 
|  | 49 |  | 
|  | 50 | #include <asm/io.h> | 
|  | 51 | #include <asm/irq.h> | 
|  | 52 | #include <asm/byteorder.h> | 
|  | 53 |  | 
|  | 54 | #include <rdma/ib_smi.h> | 
|  | 55 | #include "c2.h" | 
|  | 56 | #include "c2_provider.h" | 
|  | 57 |  | 
|  | 58 | MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>"); | 
|  | 59 | MODULE_DESCRIPTION("Ammasso AMSO1100 Low-level iWARP Driver"); | 
|  | 60 | MODULE_LICENSE("Dual BSD/GPL"); | 
|  | 61 | MODULE_VERSION(DRV_VERSION); | 
|  | 62 |  | 
|  | 63 | static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | 
|  | 64 | | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN; | 
|  | 65 |  | 
|  | 66 | static int debug = -1;		/* defaults above */ | 
|  | 67 | module_param(debug, int, 0); | 
|  | 68 | MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); | 
|  | 69 |  | 
|  | 70 | static int c2_up(struct net_device *netdev); | 
|  | 71 | static int c2_down(struct net_device *netdev); | 
|  | 72 | static int c2_xmit_frame(struct sk_buff *skb, struct net_device *netdev); | 
|  | 73 | static void c2_tx_interrupt(struct net_device *netdev); | 
|  | 74 | static void c2_rx_interrupt(struct net_device *netdev); | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 75 | static irqreturn_t c2_interrupt(int irq, void *dev_id); | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 76 | static void c2_tx_timeout(struct net_device *netdev); | 
|  | 77 | static int c2_change_mtu(struct net_device *netdev, int new_mtu); | 
|  | 78 | static void c2_reset(struct c2_port *c2_port); | 
|  | 79 | static struct net_device_stats *c2_get_stats(struct net_device *netdev); | 
|  | 80 |  | 
|  | 81 | static struct pci_device_id c2_pci_table[] = { | 
|  | 82 | { PCI_DEVICE(0x18b8, 0xb001) }, | 
|  | 83 | { 0 } | 
|  | 84 | }; | 
|  | 85 |  | 
|  | 86 | MODULE_DEVICE_TABLE(pci, c2_pci_table); | 
|  | 87 |  | 
|  | 88 | static void c2_print_macaddr(struct net_device *netdev) | 
|  | 89 | { | 
|  | 90 | pr_debug("%s: MAC %02X:%02X:%02X:%02X:%02X:%02X, " | 
|  | 91 | "IRQ %u\n", netdev->name, | 
|  | 92 | netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2], | 
|  | 93 | netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5], | 
|  | 94 | netdev->irq); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | static void c2_set_rxbufsize(struct c2_port *c2_port) | 
|  | 98 | { | 
|  | 99 | struct net_device *netdev = c2_port->netdev; | 
|  | 100 |  | 
|  | 101 | if (netdev->mtu > RX_BUF_SIZE) | 
|  | 102 | c2_port->rx_buf_size = | 
|  | 103 | netdev->mtu + ETH_HLEN + sizeof(struct c2_rxp_hdr) + | 
|  | 104 | NET_IP_ALIGN; | 
|  | 105 | else | 
|  | 106 | c2_port->rx_buf_size = sizeof(struct c2_rxp_hdr) + RX_BUF_SIZE; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | /* | 
|  | 110 | * Allocate TX ring elements and chain them together. | 
|  | 111 | * One-to-one association of adapter descriptors with ring elements. | 
|  | 112 | */ | 
|  | 113 | static int c2_tx_ring_alloc(struct c2_ring *tx_ring, void *vaddr, | 
|  | 114 | dma_addr_t base, void __iomem * mmio_txp_ring) | 
|  | 115 | { | 
|  | 116 | struct c2_tx_desc *tx_desc; | 
|  | 117 | struct c2_txp_desc __iomem *txp_desc; | 
|  | 118 | struct c2_element *elem; | 
|  | 119 | int i; | 
|  | 120 |  | 
|  | 121 | tx_ring->start = kmalloc(sizeof(*elem) * tx_ring->count, GFP_KERNEL); | 
|  | 122 | if (!tx_ring->start) | 
|  | 123 | return -ENOMEM; | 
|  | 124 |  | 
|  | 125 | elem = tx_ring->start; | 
|  | 126 | tx_desc = vaddr; | 
|  | 127 | txp_desc = mmio_txp_ring; | 
|  | 128 | for (i = 0; i < tx_ring->count; i++, elem++, tx_desc++, txp_desc++) { | 
|  | 129 | tx_desc->len = 0; | 
|  | 130 | tx_desc->status = 0; | 
|  | 131 |  | 
|  | 132 | /* Set TXP_HTXD_UNINIT */ | 
|  | 133 | __raw_writeq(cpu_to_be64(0x1122334455667788ULL), | 
|  | 134 | (void __iomem *) txp_desc + C2_TXP_ADDR); | 
|  | 135 | __raw_writew(0, (void __iomem *) txp_desc + C2_TXP_LEN); | 
|  | 136 | __raw_writew(cpu_to_be16(TXP_HTXD_UNINIT), | 
|  | 137 | (void __iomem *) txp_desc + C2_TXP_FLAGS); | 
|  | 138 |  | 
|  | 139 | elem->skb = NULL; | 
|  | 140 | elem->ht_desc = tx_desc; | 
|  | 141 | elem->hw_desc = txp_desc; | 
|  | 142 |  | 
|  | 143 | if (i == tx_ring->count - 1) { | 
|  | 144 | elem->next = tx_ring->start; | 
|  | 145 | tx_desc->next_offset = base; | 
|  | 146 | } else { | 
|  | 147 | elem->next = elem + 1; | 
|  | 148 | tx_desc->next_offset = | 
|  | 149 | base + (i + 1) * sizeof(*tx_desc); | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | tx_ring->to_use = tx_ring->to_clean = tx_ring->start; | 
|  | 154 |  | 
|  | 155 | return 0; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | /* | 
|  | 159 | * Allocate RX ring elements and chain them together. | 
|  | 160 | * One-to-one association of adapter descriptors with ring elements. | 
|  | 161 | */ | 
|  | 162 | static int c2_rx_ring_alloc(struct c2_ring *rx_ring, void *vaddr, | 
|  | 163 | dma_addr_t base, void __iomem * mmio_rxp_ring) | 
|  | 164 | { | 
|  | 165 | struct c2_rx_desc *rx_desc; | 
|  | 166 | struct c2_rxp_desc __iomem *rxp_desc; | 
|  | 167 | struct c2_element *elem; | 
|  | 168 | int i; | 
|  | 169 |  | 
|  | 170 | rx_ring->start = kmalloc(sizeof(*elem) * rx_ring->count, GFP_KERNEL); | 
|  | 171 | if (!rx_ring->start) | 
|  | 172 | return -ENOMEM; | 
|  | 173 |  | 
|  | 174 | elem = rx_ring->start; | 
|  | 175 | rx_desc = vaddr; | 
|  | 176 | rxp_desc = mmio_rxp_ring; | 
|  | 177 | for (i = 0; i < rx_ring->count; i++, elem++, rx_desc++, rxp_desc++) { | 
|  | 178 | rx_desc->len = 0; | 
|  | 179 | rx_desc->status = 0; | 
|  | 180 |  | 
|  | 181 | /* Set RXP_HRXD_UNINIT */ | 
|  | 182 | __raw_writew(cpu_to_be16(RXP_HRXD_OK), | 
|  | 183 | (void __iomem *) rxp_desc + C2_RXP_STATUS); | 
|  | 184 | __raw_writew(0, (void __iomem *) rxp_desc + C2_RXP_COUNT); | 
|  | 185 | __raw_writew(0, (void __iomem *) rxp_desc + C2_RXP_LEN); | 
|  | 186 | __raw_writeq(cpu_to_be64(0x99aabbccddeeffULL), | 
|  | 187 | (void __iomem *) rxp_desc + C2_RXP_ADDR); | 
|  | 188 | __raw_writew(cpu_to_be16(RXP_HRXD_UNINIT), | 
|  | 189 | (void __iomem *) rxp_desc + C2_RXP_FLAGS); | 
|  | 190 |  | 
|  | 191 | elem->skb = NULL; | 
|  | 192 | elem->ht_desc = rx_desc; | 
|  | 193 | elem->hw_desc = rxp_desc; | 
|  | 194 |  | 
|  | 195 | if (i == rx_ring->count - 1) { | 
|  | 196 | elem->next = rx_ring->start; | 
|  | 197 | rx_desc->next_offset = base; | 
|  | 198 | } else { | 
|  | 199 | elem->next = elem + 1; | 
|  | 200 | rx_desc->next_offset = | 
|  | 201 | base + (i + 1) * sizeof(*rx_desc); | 
|  | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | rx_ring->to_use = rx_ring->to_clean = rx_ring->start; | 
|  | 206 |  | 
|  | 207 | return 0; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | /* Setup buffer for receiving */ | 
|  | 211 | static inline int c2_rx_alloc(struct c2_port *c2_port, struct c2_element *elem) | 
|  | 212 | { | 
|  | 213 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 214 | struct c2_rx_desc *rx_desc = elem->ht_desc; | 
|  | 215 | struct sk_buff *skb; | 
|  | 216 | dma_addr_t mapaddr; | 
|  | 217 | u32 maplen; | 
|  | 218 | struct c2_rxp_hdr *rxp_hdr; | 
|  | 219 |  | 
|  | 220 | skb = dev_alloc_skb(c2_port->rx_buf_size); | 
|  | 221 | if (unlikely(!skb)) { | 
|  | 222 | pr_debug("%s: out of memory for receive\n", | 
|  | 223 | c2_port->netdev->name); | 
|  | 224 | return -ENOMEM; | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | /* Zero out the rxp hdr in the sk_buff */ | 
|  | 228 | memset(skb->data, 0, sizeof(*rxp_hdr)); | 
|  | 229 |  | 
|  | 230 | skb->dev = c2_port->netdev; | 
|  | 231 |  | 
|  | 232 | maplen = c2_port->rx_buf_size; | 
|  | 233 | mapaddr = | 
|  | 234 | pci_map_single(c2dev->pcidev, skb->data, maplen, | 
|  | 235 | PCI_DMA_FROMDEVICE); | 
|  | 236 |  | 
|  | 237 | /* Set the sk_buff RXP_header to RXP_HRXD_READY */ | 
|  | 238 | rxp_hdr = (struct c2_rxp_hdr *) skb->data; | 
|  | 239 | rxp_hdr->flags = RXP_HRXD_READY; | 
|  | 240 |  | 
|  | 241 | __raw_writew(0, elem->hw_desc + C2_RXP_STATUS); | 
|  | 242 | __raw_writew(cpu_to_be16((u16) maplen - sizeof(*rxp_hdr)), | 
|  | 243 | elem->hw_desc + C2_RXP_LEN); | 
|  | 244 | __raw_writeq(cpu_to_be64(mapaddr), elem->hw_desc + C2_RXP_ADDR); | 
|  | 245 | __raw_writew(cpu_to_be16(RXP_HRXD_READY), elem->hw_desc + C2_RXP_FLAGS); | 
|  | 246 |  | 
|  | 247 | elem->skb = skb; | 
|  | 248 | elem->mapaddr = mapaddr; | 
|  | 249 | elem->maplen = maplen; | 
|  | 250 | rx_desc->len = maplen; | 
|  | 251 |  | 
|  | 252 | return 0; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | /* | 
|  | 256 | * Allocate buffers for the Rx ring | 
|  | 257 | * For receive:  rx_ring.to_clean is next received frame | 
|  | 258 | */ | 
|  | 259 | static int c2_rx_fill(struct c2_port *c2_port) | 
|  | 260 | { | 
|  | 261 | struct c2_ring *rx_ring = &c2_port->rx_ring; | 
|  | 262 | struct c2_element *elem; | 
|  | 263 | int ret = 0; | 
|  | 264 |  | 
|  | 265 | elem = rx_ring->start; | 
|  | 266 | do { | 
|  | 267 | if (c2_rx_alloc(c2_port, elem)) { | 
|  | 268 | ret = 1; | 
|  | 269 | break; | 
|  | 270 | } | 
|  | 271 | } while ((elem = elem->next) != rx_ring->start); | 
|  | 272 |  | 
|  | 273 | rx_ring->to_clean = rx_ring->start; | 
|  | 274 | return ret; | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | /* Free all buffers in RX ring, assumes receiver stopped */ | 
|  | 278 | static void c2_rx_clean(struct c2_port *c2_port) | 
|  | 279 | { | 
|  | 280 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 281 | struct c2_ring *rx_ring = &c2_port->rx_ring; | 
|  | 282 | struct c2_element *elem; | 
|  | 283 | struct c2_rx_desc *rx_desc; | 
|  | 284 |  | 
|  | 285 | elem = rx_ring->start; | 
|  | 286 | do { | 
|  | 287 | rx_desc = elem->ht_desc; | 
|  | 288 | rx_desc->len = 0; | 
|  | 289 |  | 
|  | 290 | __raw_writew(0, elem->hw_desc + C2_RXP_STATUS); | 
|  | 291 | __raw_writew(0, elem->hw_desc + C2_RXP_COUNT); | 
|  | 292 | __raw_writew(0, elem->hw_desc + C2_RXP_LEN); | 
|  | 293 | __raw_writeq(cpu_to_be64(0x99aabbccddeeffULL), | 
|  | 294 | elem->hw_desc + C2_RXP_ADDR); | 
|  | 295 | __raw_writew(cpu_to_be16(RXP_HRXD_UNINIT), | 
|  | 296 | elem->hw_desc + C2_RXP_FLAGS); | 
|  | 297 |  | 
|  | 298 | if (elem->skb) { | 
|  | 299 | pci_unmap_single(c2dev->pcidev, elem->mapaddr, | 
|  | 300 | elem->maplen, PCI_DMA_FROMDEVICE); | 
|  | 301 | dev_kfree_skb(elem->skb); | 
|  | 302 | elem->skb = NULL; | 
|  | 303 | } | 
|  | 304 | } while ((elem = elem->next) != rx_ring->start); | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | static inline int c2_tx_free(struct c2_dev *c2dev, struct c2_element *elem) | 
|  | 308 | { | 
|  | 309 | struct c2_tx_desc *tx_desc = elem->ht_desc; | 
|  | 310 |  | 
|  | 311 | tx_desc->len = 0; | 
|  | 312 |  | 
|  | 313 | pci_unmap_single(c2dev->pcidev, elem->mapaddr, elem->maplen, | 
|  | 314 | PCI_DMA_TODEVICE); | 
|  | 315 |  | 
|  | 316 | if (elem->skb) { | 
|  | 317 | dev_kfree_skb_any(elem->skb); | 
|  | 318 | elem->skb = NULL; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | return 0; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | /* Free all buffers in TX ring, assumes transmitter stopped */ | 
|  | 325 | static void c2_tx_clean(struct c2_port *c2_port) | 
|  | 326 | { | 
|  | 327 | struct c2_ring *tx_ring = &c2_port->tx_ring; | 
|  | 328 | struct c2_element *elem; | 
|  | 329 | struct c2_txp_desc txp_htxd; | 
|  | 330 | int retry; | 
|  | 331 | unsigned long flags; | 
|  | 332 |  | 
|  | 333 | spin_lock_irqsave(&c2_port->tx_lock, flags); | 
|  | 334 |  | 
|  | 335 | elem = tx_ring->start; | 
|  | 336 |  | 
|  | 337 | do { | 
|  | 338 | retry = 0; | 
|  | 339 | do { | 
|  | 340 | txp_htxd.flags = | 
|  | 341 | readw(elem->hw_desc + C2_TXP_FLAGS); | 
|  | 342 |  | 
|  | 343 | if (txp_htxd.flags == TXP_HTXD_READY) { | 
|  | 344 | retry = 1; | 
|  | 345 | __raw_writew(0, | 
|  | 346 | elem->hw_desc + C2_TXP_LEN); | 
|  | 347 | __raw_writeq(0, | 
|  | 348 | elem->hw_desc + C2_TXP_ADDR); | 
|  | 349 | __raw_writew(cpu_to_be16(TXP_HTXD_DONE), | 
|  | 350 | elem->hw_desc + C2_TXP_FLAGS); | 
|  | 351 | c2_port->netstats.tx_dropped++; | 
|  | 352 | break; | 
|  | 353 | } else { | 
|  | 354 | __raw_writew(0, | 
|  | 355 | elem->hw_desc + C2_TXP_LEN); | 
|  | 356 | __raw_writeq(cpu_to_be64(0x1122334455667788ULL), | 
|  | 357 | elem->hw_desc + C2_TXP_ADDR); | 
|  | 358 | __raw_writew(cpu_to_be16(TXP_HTXD_UNINIT), | 
|  | 359 | elem->hw_desc + C2_TXP_FLAGS); | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | c2_tx_free(c2_port->c2dev, elem); | 
|  | 363 |  | 
|  | 364 | } while ((elem = elem->next) != tx_ring->start); | 
|  | 365 | } while (retry); | 
|  | 366 |  | 
|  | 367 | c2_port->tx_avail = c2_port->tx_ring.count - 1; | 
|  | 368 | c2_port->c2dev->cur_tx = tx_ring->to_use - tx_ring->start; | 
|  | 369 |  | 
|  | 370 | if (c2_port->tx_avail > MAX_SKB_FRAGS + 1) | 
|  | 371 | netif_wake_queue(c2_port->netdev); | 
|  | 372 |  | 
|  | 373 | spin_unlock_irqrestore(&c2_port->tx_lock, flags); | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | /* | 
|  | 377 | * Process transmit descriptors marked 'DONE' by the firmware, | 
|  | 378 | * freeing up their unneeded sk_buffs. | 
|  | 379 | */ | 
|  | 380 | static void c2_tx_interrupt(struct net_device *netdev) | 
|  | 381 | { | 
|  | 382 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 383 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 384 | struct c2_ring *tx_ring = &c2_port->tx_ring; | 
|  | 385 | struct c2_element *elem; | 
|  | 386 | struct c2_txp_desc txp_htxd; | 
|  | 387 |  | 
|  | 388 | spin_lock(&c2_port->tx_lock); | 
|  | 389 |  | 
|  | 390 | for (elem = tx_ring->to_clean; elem != tx_ring->to_use; | 
|  | 391 | elem = elem->next) { | 
|  | 392 | txp_htxd.flags = | 
|  | 393 | be16_to_cpu(readw(elem->hw_desc + C2_TXP_FLAGS)); | 
|  | 394 |  | 
|  | 395 | if (txp_htxd.flags != TXP_HTXD_DONE) | 
|  | 396 | break; | 
|  | 397 |  | 
|  | 398 | if (netif_msg_tx_done(c2_port)) { | 
|  | 399 | /* PCI reads are expensive in fast path */ | 
|  | 400 | txp_htxd.len = | 
|  | 401 | be16_to_cpu(readw(elem->hw_desc + C2_TXP_LEN)); | 
|  | 402 | pr_debug("%s: tx done slot %3Zu status 0x%x len " | 
|  | 403 | "%5u bytes\n", | 
|  | 404 | netdev->name, elem - tx_ring->start, | 
|  | 405 | txp_htxd.flags, txp_htxd.len); | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | c2_tx_free(c2dev, elem); | 
|  | 409 | ++(c2_port->tx_avail); | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | tx_ring->to_clean = elem; | 
|  | 413 |  | 
|  | 414 | if (netif_queue_stopped(netdev) | 
|  | 415 | && c2_port->tx_avail > MAX_SKB_FRAGS + 1) | 
|  | 416 | netif_wake_queue(netdev); | 
|  | 417 |  | 
|  | 418 | spin_unlock(&c2_port->tx_lock); | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | static void c2_rx_error(struct c2_port *c2_port, struct c2_element *elem) | 
|  | 422 | { | 
|  | 423 | struct c2_rx_desc *rx_desc = elem->ht_desc; | 
|  | 424 | struct c2_rxp_hdr *rxp_hdr = (struct c2_rxp_hdr *) elem->skb->data; | 
|  | 425 |  | 
|  | 426 | if (rxp_hdr->status != RXP_HRXD_OK || | 
|  | 427 | rxp_hdr->len > (rx_desc->len - sizeof(*rxp_hdr))) { | 
|  | 428 | pr_debug("BAD RXP_HRXD\n"); | 
|  | 429 | pr_debug("  rx_desc : %p\n", rx_desc); | 
|  | 430 | pr_debug("    index : %Zu\n", | 
|  | 431 | elem - c2_port->rx_ring.start); | 
|  | 432 | pr_debug("    len   : %u\n", rx_desc->len); | 
|  | 433 | pr_debug("  rxp_hdr : %p [PA %p]\n", rxp_hdr, | 
|  | 434 | (void *) __pa((unsigned long) rxp_hdr)); | 
|  | 435 | pr_debug("    flags : 0x%x\n", rxp_hdr->flags); | 
|  | 436 | pr_debug("    status: 0x%x\n", rxp_hdr->status); | 
|  | 437 | pr_debug("    len   : %u\n", rxp_hdr->len); | 
|  | 438 | pr_debug("    rsvd  : 0x%x\n", rxp_hdr->rsvd); | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | /* Setup the skb for reuse since we're dropping this pkt */ | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 442 | elem->skb->data = elem->skb->head; | 
|  | 443 | skb_reset_tail_pointer(elem->skb); | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 444 |  | 
|  | 445 | /* Zero out the rxp hdr in the sk_buff */ | 
|  | 446 | memset(elem->skb->data, 0, sizeof(*rxp_hdr)); | 
|  | 447 |  | 
|  | 448 | /* Write the descriptor to the adapter's rx ring */ | 
|  | 449 | __raw_writew(0, elem->hw_desc + C2_RXP_STATUS); | 
|  | 450 | __raw_writew(0, elem->hw_desc + C2_RXP_COUNT); | 
|  | 451 | __raw_writew(cpu_to_be16((u16) elem->maplen - sizeof(*rxp_hdr)), | 
|  | 452 | elem->hw_desc + C2_RXP_LEN); | 
|  | 453 | __raw_writeq(cpu_to_be64(elem->mapaddr), elem->hw_desc + C2_RXP_ADDR); | 
|  | 454 | __raw_writew(cpu_to_be16(RXP_HRXD_READY), elem->hw_desc + C2_RXP_FLAGS); | 
|  | 455 |  | 
|  | 456 | pr_debug("packet dropped\n"); | 
|  | 457 | c2_port->netstats.rx_dropped++; | 
|  | 458 | } | 
|  | 459 |  | 
|  | 460 | static void c2_rx_interrupt(struct net_device *netdev) | 
|  | 461 | { | 
|  | 462 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 463 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 464 | struct c2_ring *rx_ring = &c2_port->rx_ring; | 
|  | 465 | struct c2_element *elem; | 
|  | 466 | struct c2_rx_desc *rx_desc; | 
|  | 467 | struct c2_rxp_hdr *rxp_hdr; | 
|  | 468 | struct sk_buff *skb; | 
|  | 469 | dma_addr_t mapaddr; | 
|  | 470 | u32 maplen, buflen; | 
|  | 471 | unsigned long flags; | 
|  | 472 |  | 
|  | 473 | spin_lock_irqsave(&c2dev->lock, flags); | 
|  | 474 |  | 
|  | 475 | /* Begin where we left off */ | 
|  | 476 | rx_ring->to_clean = rx_ring->start + c2dev->cur_rx; | 
|  | 477 |  | 
|  | 478 | for (elem = rx_ring->to_clean; elem->next != rx_ring->to_clean; | 
|  | 479 | elem = elem->next) { | 
|  | 480 | rx_desc = elem->ht_desc; | 
|  | 481 | mapaddr = elem->mapaddr; | 
|  | 482 | maplen = elem->maplen; | 
|  | 483 | skb = elem->skb; | 
|  | 484 | rxp_hdr = (struct c2_rxp_hdr *) skb->data; | 
|  | 485 |  | 
|  | 486 | if (rxp_hdr->flags != RXP_HRXD_DONE) | 
|  | 487 | break; | 
|  | 488 | buflen = rxp_hdr->len; | 
|  | 489 |  | 
|  | 490 | /* Sanity check the RXP header */ | 
|  | 491 | if (rxp_hdr->status != RXP_HRXD_OK || | 
|  | 492 | buflen > (rx_desc->len - sizeof(*rxp_hdr))) { | 
|  | 493 | c2_rx_error(c2_port, elem); | 
|  | 494 | continue; | 
|  | 495 | } | 
|  | 496 |  | 
|  | 497 | /* | 
|  | 498 | * Allocate and map a new skb for replenishing the host | 
|  | 499 | * RX desc | 
|  | 500 | */ | 
|  | 501 | if (c2_rx_alloc(c2_port, elem)) { | 
|  | 502 | c2_rx_error(c2_port, elem); | 
|  | 503 | continue; | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | /* Unmap the old skb */ | 
|  | 507 | pci_unmap_single(c2dev->pcidev, mapaddr, maplen, | 
|  | 508 | PCI_DMA_FROMDEVICE); | 
|  | 509 |  | 
|  | 510 | prefetch(skb->data); | 
|  | 511 |  | 
|  | 512 | /* | 
|  | 513 | * Skip past the leading 8 bytes comprising of the | 
|  | 514 | * "struct c2_rxp_hdr", prepended by the adapter | 
|  | 515 | * to the usual Ethernet header ("struct ethhdr"), | 
|  | 516 | * to the start of the raw Ethernet packet. | 
|  | 517 | * | 
|  | 518 | * Fix up the various fields in the sk_buff before | 
|  | 519 | * passing it up to netif_rx(). The transfer size | 
|  | 520 | * (in bytes) specified by the adapter len field of | 
|  | 521 | * the "struct rxp_hdr_t" does NOT include the | 
|  | 522 | * "sizeof(struct c2_rxp_hdr)". | 
|  | 523 | */ | 
|  | 524 | skb->data += sizeof(*rxp_hdr); | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 525 | skb_set_tail_pointer(skb, buflen); | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 526 | skb->len = buflen; | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 527 | skb->protocol = eth_type_trans(skb, netdev); | 
|  | 528 |  | 
|  | 529 | netif_rx(skb); | 
|  | 530 |  | 
|  | 531 | netdev->last_rx = jiffies; | 
|  | 532 | c2_port->netstats.rx_packets++; | 
|  | 533 | c2_port->netstats.rx_bytes += buflen; | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | /* Save where we left off */ | 
|  | 537 | rx_ring->to_clean = elem; | 
|  | 538 | c2dev->cur_rx = elem - rx_ring->start; | 
|  | 539 | C2_SET_CUR_RX(c2dev, c2dev->cur_rx); | 
|  | 540 |  | 
|  | 541 | spin_unlock_irqrestore(&c2dev->lock, flags); | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | /* | 
|  | 545 | * Handle netisr0 TX & RX interrupts. | 
|  | 546 | */ | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 547 | static irqreturn_t c2_interrupt(int irq, void *dev_id) | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 548 | { | 
|  | 549 | unsigned int netisr0, dmaisr; | 
|  | 550 | int handled = 0; | 
|  | 551 | struct c2_dev *c2dev = (struct c2_dev *) dev_id; | 
|  | 552 |  | 
|  | 553 | /* Process CCILNET interrupts */ | 
|  | 554 | netisr0 = readl(c2dev->regs + C2_NISR0); | 
|  | 555 | if (netisr0) { | 
|  | 556 |  | 
|  | 557 | /* | 
|  | 558 | * There is an issue with the firmware that always | 
|  | 559 | * provides the status of RX for both TX & RX | 
|  | 560 | * interrupts.  So process both queues here. | 
|  | 561 | */ | 
|  | 562 | c2_rx_interrupt(c2dev->netdev); | 
|  | 563 | c2_tx_interrupt(c2dev->netdev); | 
|  | 564 |  | 
|  | 565 | /* Clear the interrupt */ | 
|  | 566 | writel(netisr0, c2dev->regs + C2_NISR0); | 
|  | 567 | handled++; | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 | /* Process RNIC interrupts */ | 
|  | 571 | dmaisr = readl(c2dev->regs + C2_DISR); | 
|  | 572 | if (dmaisr) { | 
|  | 573 | writel(dmaisr, c2dev->regs + C2_DISR); | 
|  | 574 | c2_rnic_interrupt(c2dev); | 
|  | 575 | handled++; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | if (handled) { | 
|  | 579 | return IRQ_HANDLED; | 
|  | 580 | } else { | 
|  | 581 | return IRQ_NONE; | 
|  | 582 | } | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | static int c2_up(struct net_device *netdev) | 
|  | 586 | { | 
|  | 587 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 588 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 589 | struct c2_element *elem; | 
|  | 590 | struct c2_rxp_hdr *rxp_hdr; | 
|  | 591 | struct in_device *in_dev; | 
|  | 592 | size_t rx_size, tx_size; | 
|  | 593 | int ret, i; | 
|  | 594 | unsigned int netimr0; | 
|  | 595 |  | 
|  | 596 | if (netif_msg_ifup(c2_port)) | 
|  | 597 | pr_debug("%s: enabling interface\n", netdev->name); | 
|  | 598 |  | 
|  | 599 | /* Set the Rx buffer size based on MTU */ | 
|  | 600 | c2_set_rxbufsize(c2_port); | 
|  | 601 |  | 
|  | 602 | /* Allocate DMA'able memory for Tx/Rx host descriptor rings */ | 
|  | 603 | rx_size = c2_port->rx_ring.count * sizeof(struct c2_rx_desc); | 
|  | 604 | tx_size = c2_port->tx_ring.count * sizeof(struct c2_tx_desc); | 
|  | 605 |  | 
|  | 606 | c2_port->mem_size = tx_size + rx_size; | 
|  | 607 | c2_port->mem = pci_alloc_consistent(c2dev->pcidev, c2_port->mem_size, | 
|  | 608 | &c2_port->dma); | 
|  | 609 | if (c2_port->mem == NULL) { | 
|  | 610 | pr_debug("Unable to allocate memory for " | 
|  | 611 | "host descriptor rings\n"); | 
|  | 612 | return -ENOMEM; | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | memset(c2_port->mem, 0, c2_port->mem_size); | 
|  | 616 |  | 
|  | 617 | /* Create the Rx host descriptor ring */ | 
|  | 618 | if ((ret = | 
|  | 619 | c2_rx_ring_alloc(&c2_port->rx_ring, c2_port->mem, c2_port->dma, | 
|  | 620 | c2dev->mmio_rxp_ring))) { | 
|  | 621 | pr_debug("Unable to create RX ring\n"); | 
|  | 622 | goto bail0; | 
|  | 623 | } | 
|  | 624 |  | 
|  | 625 | /* Allocate Rx buffers for the host descriptor ring */ | 
|  | 626 | if (c2_rx_fill(c2_port)) { | 
|  | 627 | pr_debug("Unable to fill RX ring\n"); | 
|  | 628 | goto bail1; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | /* Create the Tx host descriptor ring */ | 
|  | 632 | if ((ret = c2_tx_ring_alloc(&c2_port->tx_ring, c2_port->mem + rx_size, | 
|  | 633 | c2_port->dma + rx_size, | 
|  | 634 | c2dev->mmio_txp_ring))) { | 
|  | 635 | pr_debug("Unable to create TX ring\n"); | 
|  | 636 | goto bail1; | 
|  | 637 | } | 
|  | 638 |  | 
|  | 639 | /* Set the TX pointer to where we left off */ | 
|  | 640 | c2_port->tx_avail = c2_port->tx_ring.count - 1; | 
|  | 641 | c2_port->tx_ring.to_use = c2_port->tx_ring.to_clean = | 
|  | 642 | c2_port->tx_ring.start + c2dev->cur_tx; | 
|  | 643 |  | 
|  | 644 | /* missing: Initialize MAC */ | 
|  | 645 |  | 
|  | 646 | BUG_ON(c2_port->tx_ring.to_use != c2_port->tx_ring.to_clean); | 
|  | 647 |  | 
|  | 648 | /* Reset the adapter, ensures the driver is in sync with the RXP */ | 
|  | 649 | c2_reset(c2_port); | 
|  | 650 |  | 
|  | 651 | /* Reset the READY bit in the sk_buff RXP headers & adapter HRXDQ */ | 
|  | 652 | for (i = 0, elem = c2_port->rx_ring.start; i < c2_port->rx_ring.count; | 
|  | 653 | i++, elem++) { | 
|  | 654 | rxp_hdr = (struct c2_rxp_hdr *) elem->skb->data; | 
|  | 655 | rxp_hdr->flags = 0; | 
|  | 656 | __raw_writew(cpu_to_be16(RXP_HRXD_READY), | 
|  | 657 | elem->hw_desc + C2_RXP_FLAGS); | 
|  | 658 | } | 
|  | 659 |  | 
|  | 660 | /* Enable network packets */ | 
|  | 661 | netif_start_queue(netdev); | 
|  | 662 |  | 
|  | 663 | /* Enable IRQ */ | 
|  | 664 | writel(0, c2dev->regs + C2_IDIS); | 
|  | 665 | netimr0 = readl(c2dev->regs + C2_NIMR0); | 
|  | 666 | netimr0 &= ~(C2_PCI_HTX_INT | C2_PCI_HRX_INT); | 
|  | 667 | writel(netimr0, c2dev->regs + C2_NIMR0); | 
|  | 668 |  | 
|  | 669 | /* Tell the stack to ignore arp requests for ipaddrs bound to | 
|  | 670 | * other interfaces.  This is needed to prevent the host stack | 
|  | 671 | * from responding to arp requests to the ipaddr bound on the | 
|  | 672 | * rdma interface. | 
|  | 673 | */ | 
|  | 674 | in_dev = in_dev_get(netdev); | 
| Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 675 | IN_DEV_CONF_SET(in_dev, ARP_IGNORE, 1); | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 676 | in_dev_put(in_dev); | 
|  | 677 |  | 
|  | 678 | return 0; | 
|  | 679 |  | 
|  | 680 | bail1: | 
|  | 681 | c2_rx_clean(c2_port); | 
|  | 682 | kfree(c2_port->rx_ring.start); | 
|  | 683 |  | 
|  | 684 | bail0: | 
|  | 685 | pci_free_consistent(c2dev->pcidev, c2_port->mem_size, c2_port->mem, | 
|  | 686 | c2_port->dma); | 
|  | 687 |  | 
|  | 688 | return ret; | 
|  | 689 | } | 
|  | 690 |  | 
|  | 691 | static int c2_down(struct net_device *netdev) | 
|  | 692 | { | 
|  | 693 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 694 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 695 |  | 
|  | 696 | if (netif_msg_ifdown(c2_port)) | 
|  | 697 | pr_debug("%s: disabling interface\n", | 
|  | 698 | netdev->name); | 
|  | 699 |  | 
|  | 700 | /* Wait for all the queued packets to get sent */ | 
|  | 701 | c2_tx_interrupt(netdev); | 
|  | 702 |  | 
|  | 703 | /* Disable network packets */ | 
|  | 704 | netif_stop_queue(netdev); | 
|  | 705 |  | 
|  | 706 | /* Disable IRQs by clearing the interrupt mask */ | 
|  | 707 | writel(1, c2dev->regs + C2_IDIS); | 
|  | 708 | writel(0, c2dev->regs + C2_NIMR0); | 
|  | 709 |  | 
|  | 710 | /* missing: Stop transmitter */ | 
|  | 711 |  | 
|  | 712 | /* missing: Stop receiver */ | 
|  | 713 |  | 
|  | 714 | /* Reset the adapter, ensures the driver is in sync with the RXP */ | 
|  | 715 | c2_reset(c2_port); | 
|  | 716 |  | 
|  | 717 | /* missing: Turn off LEDs here */ | 
|  | 718 |  | 
|  | 719 | /* Free all buffers in the host descriptor rings */ | 
|  | 720 | c2_tx_clean(c2_port); | 
|  | 721 | c2_rx_clean(c2_port); | 
|  | 722 |  | 
|  | 723 | /* Free the host descriptor rings */ | 
|  | 724 | kfree(c2_port->rx_ring.start); | 
|  | 725 | kfree(c2_port->tx_ring.start); | 
|  | 726 | pci_free_consistent(c2dev->pcidev, c2_port->mem_size, c2_port->mem, | 
|  | 727 | c2_port->dma); | 
|  | 728 |  | 
|  | 729 | return 0; | 
|  | 730 | } | 
|  | 731 |  | 
|  | 732 | static void c2_reset(struct c2_port *c2_port) | 
|  | 733 | { | 
|  | 734 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 735 | unsigned int cur_rx = c2dev->cur_rx; | 
|  | 736 |  | 
|  | 737 | /* Tell the hardware to quiesce */ | 
|  | 738 | C2_SET_CUR_RX(c2dev, cur_rx | C2_PCI_HRX_QUI); | 
|  | 739 |  | 
|  | 740 | /* | 
|  | 741 | * The hardware will reset the C2_PCI_HRX_QUI bit once | 
|  | 742 | * the RXP is quiesced.  Wait 2 seconds for this. | 
|  | 743 | */ | 
|  | 744 | ssleep(2); | 
|  | 745 |  | 
|  | 746 | cur_rx = C2_GET_CUR_RX(c2dev); | 
|  | 747 |  | 
|  | 748 | if (cur_rx & C2_PCI_HRX_QUI) | 
|  | 749 | pr_debug("c2_reset: failed to quiesce the hardware!\n"); | 
|  | 750 |  | 
|  | 751 | cur_rx &= ~C2_PCI_HRX_QUI; | 
|  | 752 |  | 
|  | 753 | c2dev->cur_rx = cur_rx; | 
|  | 754 |  | 
|  | 755 | pr_debug("Current RX: %u\n", c2dev->cur_rx); | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | static int c2_xmit_frame(struct sk_buff *skb, struct net_device *netdev) | 
|  | 759 | { | 
|  | 760 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 761 | struct c2_dev *c2dev = c2_port->c2dev; | 
|  | 762 | struct c2_ring *tx_ring = &c2_port->tx_ring; | 
|  | 763 | struct c2_element *elem; | 
|  | 764 | dma_addr_t mapaddr; | 
|  | 765 | u32 maplen; | 
|  | 766 | unsigned long flags; | 
|  | 767 | unsigned int i; | 
|  | 768 |  | 
|  | 769 | spin_lock_irqsave(&c2_port->tx_lock, flags); | 
|  | 770 |  | 
|  | 771 | if (unlikely(c2_port->tx_avail < (skb_shinfo(skb)->nr_frags + 1))) { | 
|  | 772 | netif_stop_queue(netdev); | 
|  | 773 | spin_unlock_irqrestore(&c2_port->tx_lock, flags); | 
|  | 774 |  | 
|  | 775 | pr_debug("%s: Tx ring full when queue awake!\n", | 
|  | 776 | netdev->name); | 
|  | 777 | return NETDEV_TX_BUSY; | 
|  | 778 | } | 
|  | 779 |  | 
|  | 780 | maplen = skb_headlen(skb); | 
|  | 781 | mapaddr = | 
|  | 782 | pci_map_single(c2dev->pcidev, skb->data, maplen, PCI_DMA_TODEVICE); | 
|  | 783 |  | 
|  | 784 | elem = tx_ring->to_use; | 
|  | 785 | elem->skb = skb; | 
|  | 786 | elem->mapaddr = mapaddr; | 
|  | 787 | elem->maplen = maplen; | 
|  | 788 |  | 
|  | 789 | /* Tell HW to xmit */ | 
|  | 790 | __raw_writeq(cpu_to_be64(mapaddr), elem->hw_desc + C2_TXP_ADDR); | 
|  | 791 | __raw_writew(cpu_to_be16(maplen), elem->hw_desc + C2_TXP_LEN); | 
|  | 792 | __raw_writew(cpu_to_be16(TXP_HTXD_READY), elem->hw_desc + C2_TXP_FLAGS); | 
|  | 793 |  | 
|  | 794 | c2_port->netstats.tx_packets++; | 
|  | 795 | c2_port->netstats.tx_bytes += maplen; | 
|  | 796 |  | 
|  | 797 | /* Loop thru additional data fragments and queue them */ | 
|  | 798 | if (skb_shinfo(skb)->nr_frags) { | 
|  | 799 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
|  | 800 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 
|  | 801 | maplen = frag->size; | 
|  | 802 | mapaddr = | 
|  | 803 | pci_map_page(c2dev->pcidev, frag->page, | 
|  | 804 | frag->page_offset, maplen, | 
|  | 805 | PCI_DMA_TODEVICE); | 
|  | 806 |  | 
|  | 807 | elem = elem->next; | 
|  | 808 | elem->skb = NULL; | 
|  | 809 | elem->mapaddr = mapaddr; | 
|  | 810 | elem->maplen = maplen; | 
|  | 811 |  | 
|  | 812 | /* Tell HW to xmit */ | 
|  | 813 | __raw_writeq(cpu_to_be64(mapaddr), | 
|  | 814 | elem->hw_desc + C2_TXP_ADDR); | 
|  | 815 | __raw_writew(cpu_to_be16(maplen), | 
|  | 816 | elem->hw_desc + C2_TXP_LEN); | 
|  | 817 | __raw_writew(cpu_to_be16(TXP_HTXD_READY), | 
|  | 818 | elem->hw_desc + C2_TXP_FLAGS); | 
|  | 819 |  | 
|  | 820 | c2_port->netstats.tx_packets++; | 
|  | 821 | c2_port->netstats.tx_bytes += maplen; | 
|  | 822 | } | 
|  | 823 | } | 
|  | 824 |  | 
|  | 825 | tx_ring->to_use = elem->next; | 
|  | 826 | c2_port->tx_avail -= (skb_shinfo(skb)->nr_frags + 1); | 
|  | 827 |  | 
|  | 828 | if (c2_port->tx_avail <= MAX_SKB_FRAGS + 1) { | 
|  | 829 | netif_stop_queue(netdev); | 
|  | 830 | if (netif_msg_tx_queued(c2_port)) | 
|  | 831 | pr_debug("%s: transmit queue full\n", | 
|  | 832 | netdev->name); | 
|  | 833 | } | 
|  | 834 |  | 
|  | 835 | spin_unlock_irqrestore(&c2_port->tx_lock, flags); | 
|  | 836 |  | 
|  | 837 | netdev->trans_start = jiffies; | 
|  | 838 |  | 
|  | 839 | return NETDEV_TX_OK; | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | static struct net_device_stats *c2_get_stats(struct net_device *netdev) | 
|  | 843 | { | 
|  | 844 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 845 |  | 
|  | 846 | return &c2_port->netstats; | 
|  | 847 | } | 
|  | 848 |  | 
|  | 849 | static void c2_tx_timeout(struct net_device *netdev) | 
|  | 850 | { | 
|  | 851 | struct c2_port *c2_port = netdev_priv(netdev); | 
|  | 852 |  | 
|  | 853 | if (netif_msg_timer(c2_port)) | 
|  | 854 | pr_debug("%s: tx timeout\n", netdev->name); | 
|  | 855 |  | 
|  | 856 | c2_tx_clean(c2_port); | 
|  | 857 | } | 
|  | 858 |  | 
|  | 859 | static int c2_change_mtu(struct net_device *netdev, int new_mtu) | 
|  | 860 | { | 
|  | 861 | int ret = 0; | 
|  | 862 |  | 
|  | 863 | if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU) | 
|  | 864 | return -EINVAL; | 
|  | 865 |  | 
|  | 866 | netdev->mtu = new_mtu; | 
|  | 867 |  | 
|  | 868 | if (netif_running(netdev)) { | 
|  | 869 | c2_down(netdev); | 
|  | 870 |  | 
|  | 871 | c2_up(netdev); | 
|  | 872 | } | 
|  | 873 |  | 
|  | 874 | return ret; | 
|  | 875 | } | 
|  | 876 |  | 
|  | 877 | /* Initialize network device */ | 
|  | 878 | static struct net_device *c2_devinit(struct c2_dev *c2dev, | 
|  | 879 | void __iomem * mmio_addr) | 
|  | 880 | { | 
|  | 881 | struct c2_port *c2_port = NULL; | 
|  | 882 | struct net_device *netdev = alloc_etherdev(sizeof(*c2_port)); | 
|  | 883 |  | 
|  | 884 | if (!netdev) { | 
|  | 885 | pr_debug("c2_port etherdev alloc failed"); | 
|  | 886 | return NULL; | 
|  | 887 | } | 
|  | 888 |  | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 889 | SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev); | 
|  | 890 |  | 
|  | 891 | netdev->open = c2_up; | 
|  | 892 | netdev->stop = c2_down; | 
|  | 893 | netdev->hard_start_xmit = c2_xmit_frame; | 
|  | 894 | netdev->get_stats = c2_get_stats; | 
|  | 895 | netdev->tx_timeout = c2_tx_timeout; | 
|  | 896 | netdev->change_mtu = c2_change_mtu; | 
|  | 897 | netdev->watchdog_timeo = C2_TX_TIMEOUT; | 
|  | 898 | netdev->irq = c2dev->pcidev->irq; | 
|  | 899 |  | 
|  | 900 | c2_port = netdev_priv(netdev); | 
|  | 901 | c2_port->netdev = netdev; | 
|  | 902 | c2_port->c2dev = c2dev; | 
|  | 903 | c2_port->msg_enable = netif_msg_init(debug, default_msg); | 
|  | 904 | c2_port->tx_ring.count = C2_NUM_TX_DESC; | 
|  | 905 | c2_port->rx_ring.count = C2_NUM_RX_DESC; | 
|  | 906 |  | 
|  | 907 | spin_lock_init(&c2_port->tx_lock); | 
|  | 908 |  | 
|  | 909 | /* Copy our 48-bit ethernet hardware address */ | 
|  | 910 | memcpy_fromio(netdev->dev_addr, mmio_addr + C2_REGS_ENADDR, 6); | 
|  | 911 |  | 
|  | 912 | /* Validate the MAC address */ | 
|  | 913 | if (!is_valid_ether_addr(netdev->dev_addr)) { | 
|  | 914 | pr_debug("Invalid MAC Address\n"); | 
|  | 915 | c2_print_macaddr(netdev); | 
|  | 916 | free_netdev(netdev); | 
|  | 917 | return NULL; | 
|  | 918 | } | 
|  | 919 |  | 
|  | 920 | c2dev->netdev = netdev; | 
|  | 921 |  | 
|  | 922 | return netdev; | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | static int __devinit c2_probe(struct pci_dev *pcidev, | 
|  | 926 | const struct pci_device_id *ent) | 
|  | 927 | { | 
|  | 928 | int ret = 0, i; | 
|  | 929 | unsigned long reg0_start, reg0_flags, reg0_len; | 
|  | 930 | unsigned long reg2_start, reg2_flags, reg2_len; | 
|  | 931 | unsigned long reg4_start, reg4_flags, reg4_len; | 
|  | 932 | unsigned kva_map_size; | 
|  | 933 | struct net_device *netdev = NULL; | 
|  | 934 | struct c2_dev *c2dev = NULL; | 
|  | 935 | void __iomem *mmio_regs = NULL; | 
|  | 936 |  | 
|  | 937 | printk(KERN_INFO PFX "AMSO1100 Gigabit Ethernet driver v%s loaded\n", | 
|  | 938 | DRV_VERSION); | 
|  | 939 |  | 
|  | 940 | /* Enable PCI device */ | 
|  | 941 | ret = pci_enable_device(pcidev); | 
|  | 942 | if (ret) { | 
|  | 943 | printk(KERN_ERR PFX "%s: Unable to enable PCI device\n", | 
|  | 944 | pci_name(pcidev)); | 
|  | 945 | goto bail0; | 
|  | 946 | } | 
|  | 947 |  | 
|  | 948 | reg0_start = pci_resource_start(pcidev, BAR_0); | 
|  | 949 | reg0_len = pci_resource_len(pcidev, BAR_0); | 
|  | 950 | reg0_flags = pci_resource_flags(pcidev, BAR_0); | 
|  | 951 |  | 
|  | 952 | reg2_start = pci_resource_start(pcidev, BAR_2); | 
|  | 953 | reg2_len = pci_resource_len(pcidev, BAR_2); | 
|  | 954 | reg2_flags = pci_resource_flags(pcidev, BAR_2); | 
|  | 955 |  | 
|  | 956 | reg4_start = pci_resource_start(pcidev, BAR_4); | 
|  | 957 | reg4_len = pci_resource_len(pcidev, BAR_4); | 
|  | 958 | reg4_flags = pci_resource_flags(pcidev, BAR_4); | 
|  | 959 |  | 
|  | 960 | pr_debug("BAR0 size = 0x%lX bytes\n", reg0_len); | 
|  | 961 | pr_debug("BAR2 size = 0x%lX bytes\n", reg2_len); | 
|  | 962 | pr_debug("BAR4 size = 0x%lX bytes\n", reg4_len); | 
|  | 963 |  | 
|  | 964 | /* Make sure PCI base addr are MMIO */ | 
|  | 965 | if (!(reg0_flags & IORESOURCE_MEM) || | 
|  | 966 | !(reg2_flags & IORESOURCE_MEM) || !(reg4_flags & IORESOURCE_MEM)) { | 
|  | 967 | printk(KERN_ERR PFX "PCI regions not an MMIO resource\n"); | 
|  | 968 | ret = -ENODEV; | 
|  | 969 | goto bail1; | 
|  | 970 | } | 
|  | 971 |  | 
|  | 972 | /* Check for weird/broken PCI region reporting */ | 
|  | 973 | if ((reg0_len < C2_REG0_SIZE) || | 
|  | 974 | (reg2_len < C2_REG2_SIZE) || (reg4_len < C2_REG4_SIZE)) { | 
|  | 975 | printk(KERN_ERR PFX "Invalid PCI region sizes\n"); | 
|  | 976 | ret = -ENODEV; | 
|  | 977 | goto bail1; | 
|  | 978 | } | 
|  | 979 |  | 
|  | 980 | /* Reserve PCI I/O and memory resources */ | 
|  | 981 | ret = pci_request_regions(pcidev, DRV_NAME); | 
|  | 982 | if (ret) { | 
|  | 983 | printk(KERN_ERR PFX "%s: Unable to request regions\n", | 
|  | 984 | pci_name(pcidev)); | 
|  | 985 | goto bail1; | 
|  | 986 | } | 
|  | 987 |  | 
|  | 988 | if ((sizeof(dma_addr_t) > 4)) { | 
|  | 989 | ret = pci_set_dma_mask(pcidev, DMA_64BIT_MASK); | 
|  | 990 | if (ret < 0) { | 
|  | 991 | printk(KERN_ERR PFX "64b DMA configuration failed\n"); | 
|  | 992 | goto bail2; | 
|  | 993 | } | 
|  | 994 | } else { | 
|  | 995 | ret = pci_set_dma_mask(pcidev, DMA_32BIT_MASK); | 
|  | 996 | if (ret < 0) { | 
|  | 997 | printk(KERN_ERR PFX "32b DMA configuration failed\n"); | 
|  | 998 | goto bail2; | 
|  | 999 | } | 
|  | 1000 | } | 
|  | 1001 |  | 
|  | 1002 | /* Enables bus-mastering on the device */ | 
|  | 1003 | pci_set_master(pcidev); | 
|  | 1004 |  | 
|  | 1005 | /* Remap the adapter PCI registers in BAR4 */ | 
|  | 1006 | mmio_regs = ioremap_nocache(reg4_start + C2_PCI_REGS_OFFSET, | 
|  | 1007 | sizeof(struct c2_adapter_pci_regs)); | 
|  | 1008 | if (mmio_regs == 0UL) { | 
|  | 1009 | printk(KERN_ERR PFX | 
|  | 1010 | "Unable to remap adapter PCI registers in BAR4\n"); | 
|  | 1011 | ret = -EIO; | 
|  | 1012 | goto bail2; | 
|  | 1013 | } | 
|  | 1014 |  | 
|  | 1015 | /* Validate PCI regs magic */ | 
|  | 1016 | for (i = 0; i < sizeof(c2_magic); i++) { | 
|  | 1017 | if (c2_magic[i] != readb(mmio_regs + C2_REGS_MAGIC + i)) { | 
|  | 1018 | printk(KERN_ERR PFX "Downlevel Firmware boot loader " | 
|  | 1019 | "[%d/%Zd: got 0x%x, exp 0x%x]. Use the cc_flash " | 
|  | 1020 | "utility to update your boot loader\n", | 
|  | 1021 | i + 1, sizeof(c2_magic), | 
|  | 1022 | readb(mmio_regs + C2_REGS_MAGIC + i), | 
|  | 1023 | c2_magic[i]); | 
|  | 1024 | printk(KERN_ERR PFX "Adapter not claimed\n"); | 
|  | 1025 | iounmap(mmio_regs); | 
|  | 1026 | ret = -EIO; | 
|  | 1027 | goto bail2; | 
|  | 1028 | } | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | /* Validate the adapter version */ | 
|  | 1032 | if (be32_to_cpu(readl(mmio_regs + C2_REGS_VERS)) != C2_VERSION) { | 
|  | 1033 | printk(KERN_ERR PFX "Version mismatch " | 
|  | 1034 | "[fw=%u, c2=%u], Adapter not claimed\n", | 
|  | 1035 | be32_to_cpu(readl(mmio_regs + C2_REGS_VERS)), | 
|  | 1036 | C2_VERSION); | 
|  | 1037 | ret = -EINVAL; | 
|  | 1038 | iounmap(mmio_regs); | 
|  | 1039 | goto bail2; | 
|  | 1040 | } | 
|  | 1041 |  | 
|  | 1042 | /* Validate the adapter IVN */ | 
|  | 1043 | if (be32_to_cpu(readl(mmio_regs + C2_REGS_IVN)) != C2_IVN) { | 
|  | 1044 | printk(KERN_ERR PFX "Downlevel FIrmware level. You should be using " | 
|  | 1045 | "the OpenIB device support kit. " | 
|  | 1046 | "[fw=0x%x, c2=0x%x], Adapter not claimed\n", | 
|  | 1047 | be32_to_cpu(readl(mmio_regs + C2_REGS_IVN)), | 
|  | 1048 | C2_IVN); | 
|  | 1049 | ret = -EINVAL; | 
|  | 1050 | iounmap(mmio_regs); | 
|  | 1051 | goto bail2; | 
|  | 1052 | } | 
|  | 1053 |  | 
|  | 1054 | /* Allocate hardware structure */ | 
|  | 1055 | c2dev = (struct c2_dev *) ib_alloc_device(sizeof(*c2dev)); | 
|  | 1056 | if (!c2dev) { | 
|  | 1057 | printk(KERN_ERR PFX "%s: Unable to alloc hardware struct\n", | 
|  | 1058 | pci_name(pcidev)); | 
|  | 1059 | ret = -ENOMEM; | 
|  | 1060 | iounmap(mmio_regs); | 
|  | 1061 | goto bail2; | 
|  | 1062 | } | 
|  | 1063 |  | 
|  | 1064 | memset(c2dev, 0, sizeof(*c2dev)); | 
|  | 1065 | spin_lock_init(&c2dev->lock); | 
|  | 1066 | c2dev->pcidev = pcidev; | 
|  | 1067 | c2dev->cur_tx = 0; | 
|  | 1068 |  | 
|  | 1069 | /* Get the last RX index */ | 
|  | 1070 | c2dev->cur_rx = | 
|  | 1071 | (be32_to_cpu(readl(mmio_regs + C2_REGS_HRX_CUR)) - | 
|  | 1072 | 0xffffc000) / sizeof(struct c2_rxp_desc); | 
|  | 1073 |  | 
|  | 1074 | /* Request an interrupt line for the driver */ | 
| Thomas Gleixner | 38515e9 | 2007-02-14 00:33:16 -0800 | [diff] [blame] | 1075 | ret = request_irq(pcidev->irq, c2_interrupt, IRQF_SHARED, DRV_NAME, c2dev); | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1076 | if (ret) { | 
|  | 1077 | printk(KERN_ERR PFX "%s: requested IRQ %u is busy\n", | 
|  | 1078 | pci_name(pcidev), pcidev->irq); | 
|  | 1079 | iounmap(mmio_regs); | 
|  | 1080 | goto bail3; | 
|  | 1081 | } | 
|  | 1082 |  | 
|  | 1083 | /* Set driver specific data */ | 
|  | 1084 | pci_set_drvdata(pcidev, c2dev); | 
|  | 1085 |  | 
|  | 1086 | /* Initialize network device */ | 
|  | 1087 | if ((netdev = c2_devinit(c2dev, mmio_regs)) == NULL) { | 
|  | 1088 | iounmap(mmio_regs); | 
|  | 1089 | goto bail4; | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 | /* Save off the actual size prior to unmapping mmio_regs */ | 
|  | 1093 | kva_map_size = be32_to_cpu(readl(mmio_regs + C2_REGS_PCI_WINSIZE)); | 
|  | 1094 |  | 
|  | 1095 | /* Unmap the adapter PCI registers in BAR4 */ | 
|  | 1096 | iounmap(mmio_regs); | 
|  | 1097 |  | 
|  | 1098 | /* Register network device */ | 
|  | 1099 | ret = register_netdev(netdev); | 
|  | 1100 | if (ret) { | 
|  | 1101 | printk(KERN_ERR PFX "Unable to register netdev, ret = %d\n", | 
|  | 1102 | ret); | 
|  | 1103 | goto bail5; | 
|  | 1104 | } | 
|  | 1105 |  | 
|  | 1106 | /* Disable network packets */ | 
|  | 1107 | netif_stop_queue(netdev); | 
|  | 1108 |  | 
|  | 1109 | /* Remap the adapter HRXDQ PA space to kernel VA space */ | 
|  | 1110 | c2dev->mmio_rxp_ring = ioremap_nocache(reg4_start + C2_RXP_HRXDQ_OFFSET, | 
|  | 1111 | C2_RXP_HRXDQ_SIZE); | 
|  | 1112 | if (c2dev->mmio_rxp_ring == 0UL) { | 
|  | 1113 | printk(KERN_ERR PFX "Unable to remap MMIO HRXDQ region\n"); | 
|  | 1114 | ret = -EIO; | 
|  | 1115 | goto bail6; | 
|  | 1116 | } | 
|  | 1117 |  | 
|  | 1118 | /* Remap the adapter HTXDQ PA space to kernel VA space */ | 
|  | 1119 | c2dev->mmio_txp_ring = ioremap_nocache(reg4_start + C2_TXP_HTXDQ_OFFSET, | 
|  | 1120 | C2_TXP_HTXDQ_SIZE); | 
|  | 1121 | if (c2dev->mmio_txp_ring == 0UL) { | 
|  | 1122 | printk(KERN_ERR PFX "Unable to remap MMIO HTXDQ region\n"); | 
|  | 1123 | ret = -EIO; | 
|  | 1124 | goto bail7; | 
|  | 1125 | } | 
|  | 1126 |  | 
|  | 1127 | /* Save off the current RX index in the last 4 bytes of the TXP Ring */ | 
|  | 1128 | C2_SET_CUR_RX(c2dev, c2dev->cur_rx); | 
|  | 1129 |  | 
|  | 1130 | /* Remap the PCI registers in adapter BAR0 to kernel VA space */ | 
|  | 1131 | c2dev->regs = ioremap_nocache(reg0_start, reg0_len); | 
|  | 1132 | if (c2dev->regs == 0UL) { | 
|  | 1133 | printk(KERN_ERR PFX "Unable to remap BAR0\n"); | 
|  | 1134 | ret = -EIO; | 
|  | 1135 | goto bail8; | 
|  | 1136 | } | 
|  | 1137 |  | 
|  | 1138 | /* Remap the PCI registers in adapter BAR4 to kernel VA space */ | 
|  | 1139 | c2dev->pa = reg4_start + C2_PCI_REGS_OFFSET; | 
|  | 1140 | c2dev->kva = ioremap_nocache(reg4_start + C2_PCI_REGS_OFFSET, | 
|  | 1141 | kva_map_size); | 
|  | 1142 | if (c2dev->kva == 0UL) { | 
|  | 1143 | printk(KERN_ERR PFX "Unable to remap BAR4\n"); | 
|  | 1144 | ret = -EIO; | 
|  | 1145 | goto bail9; | 
|  | 1146 | } | 
|  | 1147 |  | 
|  | 1148 | /* Print out the MAC address */ | 
|  | 1149 | c2_print_macaddr(netdev); | 
|  | 1150 |  | 
|  | 1151 | ret = c2_rnic_init(c2dev); | 
|  | 1152 | if (ret) { | 
|  | 1153 | printk(KERN_ERR PFX "c2_rnic_init failed: %d\n", ret); | 
|  | 1154 | goto bail10; | 
|  | 1155 | } | 
|  | 1156 |  | 
| Tom Tucker | 2ffcab6 | 2006-11-08 14:23:22 -0600 | [diff] [blame] | 1157 | if (c2_register_device(c2dev)) | 
|  | 1158 | goto bail10; | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1159 |  | 
|  | 1160 | return 0; | 
|  | 1161 |  | 
|  | 1162 | bail10: | 
|  | 1163 | iounmap(c2dev->kva); | 
|  | 1164 |  | 
|  | 1165 | bail9: | 
|  | 1166 | iounmap(c2dev->regs); | 
|  | 1167 |  | 
|  | 1168 | bail8: | 
|  | 1169 | iounmap(c2dev->mmio_txp_ring); | 
|  | 1170 |  | 
|  | 1171 | bail7: | 
|  | 1172 | iounmap(c2dev->mmio_rxp_ring); | 
|  | 1173 |  | 
|  | 1174 | bail6: | 
|  | 1175 | unregister_netdev(netdev); | 
|  | 1176 |  | 
|  | 1177 | bail5: | 
|  | 1178 | free_netdev(netdev); | 
|  | 1179 |  | 
|  | 1180 | bail4: | 
|  | 1181 | free_irq(pcidev->irq, c2dev); | 
|  | 1182 |  | 
|  | 1183 | bail3: | 
|  | 1184 | ib_dealloc_device(&c2dev->ibdev); | 
|  | 1185 |  | 
|  | 1186 | bail2: | 
|  | 1187 | pci_release_regions(pcidev); | 
|  | 1188 |  | 
|  | 1189 | bail1: | 
|  | 1190 | pci_disable_device(pcidev); | 
|  | 1191 |  | 
|  | 1192 | bail0: | 
|  | 1193 | return ret; | 
|  | 1194 | } | 
|  | 1195 |  | 
|  | 1196 | static void __devexit c2_remove(struct pci_dev *pcidev) | 
|  | 1197 | { | 
|  | 1198 | struct c2_dev *c2dev = pci_get_drvdata(pcidev); | 
|  | 1199 | struct net_device *netdev = c2dev->netdev; | 
|  | 1200 |  | 
|  | 1201 | /* Unregister with OpenIB */ | 
|  | 1202 | c2_unregister_device(c2dev); | 
|  | 1203 |  | 
|  | 1204 | /* Clean up the RNIC resources */ | 
|  | 1205 | c2_rnic_term(c2dev); | 
|  | 1206 |  | 
|  | 1207 | /* Remove network device from the kernel */ | 
|  | 1208 | unregister_netdev(netdev); | 
|  | 1209 |  | 
|  | 1210 | /* Free network device */ | 
|  | 1211 | free_netdev(netdev); | 
|  | 1212 |  | 
|  | 1213 | /* Free the interrupt line */ | 
|  | 1214 | free_irq(pcidev->irq, c2dev); | 
|  | 1215 |  | 
|  | 1216 | /* missing: Turn LEDs off here */ | 
|  | 1217 |  | 
|  | 1218 | /* Unmap adapter PA space */ | 
|  | 1219 | iounmap(c2dev->kva); | 
|  | 1220 | iounmap(c2dev->regs); | 
|  | 1221 | iounmap(c2dev->mmio_txp_ring); | 
|  | 1222 | iounmap(c2dev->mmio_rxp_ring); | 
|  | 1223 |  | 
|  | 1224 | /* Free the hardware structure */ | 
|  | 1225 | ib_dealloc_device(&c2dev->ibdev); | 
|  | 1226 |  | 
|  | 1227 | /* Release reserved PCI I/O and memory resources */ | 
|  | 1228 | pci_release_regions(pcidev); | 
|  | 1229 |  | 
|  | 1230 | /* Disable PCI device */ | 
|  | 1231 | pci_disable_device(pcidev); | 
|  | 1232 |  | 
|  | 1233 | /* Clear driver specific data */ | 
|  | 1234 | pci_set_drvdata(pcidev, NULL); | 
|  | 1235 | } | 
|  | 1236 |  | 
|  | 1237 | static struct pci_driver c2_pci_driver = { | 
|  | 1238 | .name = DRV_NAME, | 
|  | 1239 | .id_table = c2_pci_table, | 
|  | 1240 | .probe = c2_probe, | 
|  | 1241 | .remove = __devexit_p(c2_remove), | 
|  | 1242 | }; | 
|  | 1243 |  | 
|  | 1244 | static int __init c2_init_module(void) | 
|  | 1245 | { | 
| Henrik Kretzschmar | d986a27 | 2006-10-10 14:26:01 -0700 | [diff] [blame] | 1246 | return pci_register_driver(&c2_pci_driver); | 
| Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | static void __exit c2_exit_module(void) | 
|  | 1250 | { | 
|  | 1251 | pci_unregister_driver(&c2_pci_driver); | 
|  | 1252 | } | 
|  | 1253 |  | 
|  | 1254 | module_init(c2_init_module); | 
|  | 1255 | module_exit(c2_exit_module); |