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); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 79 | |
| 80 | static struct pci_device_id c2_pci_table[] = { |
| 81 | { PCI_DEVICE(0x18b8, 0xb001) }, |
| 82 | { 0 } |
| 83 | }; |
| 84 | |
| 85 | MODULE_DEVICE_TABLE(pci, c2_pci_table); |
| 86 | |
| 87 | static void c2_print_macaddr(struct net_device *netdev) |
| 88 | { |
| 89 | pr_debug("%s: MAC %02X:%02X:%02X:%02X:%02X:%02X, " |
| 90 | "IRQ %u\n", netdev->name, |
| 91 | netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2], |
| 92 | netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5], |
| 93 | netdev->irq); |
| 94 | } |
| 95 | |
| 96 | static void c2_set_rxbufsize(struct c2_port *c2_port) |
| 97 | { |
| 98 | struct net_device *netdev = c2_port->netdev; |
| 99 | |
| 100 | if (netdev->mtu > RX_BUF_SIZE) |
| 101 | c2_port->rx_buf_size = |
| 102 | netdev->mtu + ETH_HLEN + sizeof(struct c2_rxp_hdr) + |
| 103 | NET_IP_ALIGN; |
| 104 | else |
| 105 | c2_port->rx_buf_size = sizeof(struct c2_rxp_hdr) + RX_BUF_SIZE; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Allocate TX ring elements and chain them together. |
| 110 | * One-to-one association of adapter descriptors with ring elements. |
| 111 | */ |
| 112 | static int c2_tx_ring_alloc(struct c2_ring *tx_ring, void *vaddr, |
| 113 | dma_addr_t base, void __iomem * mmio_txp_ring) |
| 114 | { |
| 115 | struct c2_tx_desc *tx_desc; |
| 116 | struct c2_txp_desc __iomem *txp_desc; |
| 117 | struct c2_element *elem; |
| 118 | int i; |
| 119 | |
| 120 | tx_ring->start = kmalloc(sizeof(*elem) * tx_ring->count, GFP_KERNEL); |
| 121 | if (!tx_ring->start) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | elem = tx_ring->start; |
| 125 | tx_desc = vaddr; |
| 126 | txp_desc = mmio_txp_ring; |
| 127 | for (i = 0; i < tx_ring->count; i++, elem++, tx_desc++, txp_desc++) { |
| 128 | tx_desc->len = 0; |
| 129 | tx_desc->status = 0; |
| 130 | |
| 131 | /* Set TXP_HTXD_UNINIT */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 132 | __raw_writeq((__force u64) cpu_to_be64(0x1122334455667788ULL), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 133 | (void __iomem *) txp_desc + C2_TXP_ADDR); |
| 134 | __raw_writew(0, (void __iomem *) txp_desc + C2_TXP_LEN); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 135 | __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_UNINIT), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 136 | (void __iomem *) txp_desc + C2_TXP_FLAGS); |
| 137 | |
| 138 | elem->skb = NULL; |
| 139 | elem->ht_desc = tx_desc; |
| 140 | elem->hw_desc = txp_desc; |
| 141 | |
| 142 | if (i == tx_ring->count - 1) { |
| 143 | elem->next = tx_ring->start; |
| 144 | tx_desc->next_offset = base; |
| 145 | } else { |
| 146 | elem->next = elem + 1; |
| 147 | tx_desc->next_offset = |
| 148 | base + (i + 1) * sizeof(*tx_desc); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | tx_ring->to_use = tx_ring->to_clean = tx_ring->start; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Allocate RX ring elements and chain them together. |
| 159 | * One-to-one association of adapter descriptors with ring elements. |
| 160 | */ |
| 161 | static int c2_rx_ring_alloc(struct c2_ring *rx_ring, void *vaddr, |
| 162 | dma_addr_t base, void __iomem * mmio_rxp_ring) |
| 163 | { |
| 164 | struct c2_rx_desc *rx_desc; |
| 165 | struct c2_rxp_desc __iomem *rxp_desc; |
| 166 | struct c2_element *elem; |
| 167 | int i; |
| 168 | |
| 169 | rx_ring->start = kmalloc(sizeof(*elem) * rx_ring->count, GFP_KERNEL); |
| 170 | if (!rx_ring->start) |
| 171 | return -ENOMEM; |
| 172 | |
| 173 | elem = rx_ring->start; |
| 174 | rx_desc = vaddr; |
| 175 | rxp_desc = mmio_rxp_ring; |
| 176 | for (i = 0; i < rx_ring->count; i++, elem++, rx_desc++, rxp_desc++) { |
| 177 | rx_desc->len = 0; |
| 178 | rx_desc->status = 0; |
| 179 | |
| 180 | /* Set RXP_HRXD_UNINIT */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 181 | __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_OK), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 182 | (void __iomem *) rxp_desc + C2_RXP_STATUS); |
| 183 | __raw_writew(0, (void __iomem *) rxp_desc + C2_RXP_COUNT); |
| 184 | __raw_writew(0, (void __iomem *) rxp_desc + C2_RXP_LEN); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 185 | __raw_writeq((__force u64) cpu_to_be64(0x99aabbccddeeffULL), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 186 | (void __iomem *) rxp_desc + C2_RXP_ADDR); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 187 | __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_UNINIT), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 188 | (void __iomem *) rxp_desc + C2_RXP_FLAGS); |
| 189 | |
| 190 | elem->skb = NULL; |
| 191 | elem->ht_desc = rx_desc; |
| 192 | elem->hw_desc = rxp_desc; |
| 193 | |
| 194 | if (i == rx_ring->count - 1) { |
| 195 | elem->next = rx_ring->start; |
| 196 | rx_desc->next_offset = base; |
| 197 | } else { |
| 198 | elem->next = elem + 1; |
| 199 | rx_desc->next_offset = |
| 200 | base + (i + 1) * sizeof(*rx_desc); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | rx_ring->to_use = rx_ring->to_clean = rx_ring->start; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* Setup buffer for receiving */ |
| 210 | static inline int c2_rx_alloc(struct c2_port *c2_port, struct c2_element *elem) |
| 211 | { |
| 212 | struct c2_dev *c2dev = c2_port->c2dev; |
| 213 | struct c2_rx_desc *rx_desc = elem->ht_desc; |
| 214 | struct sk_buff *skb; |
| 215 | dma_addr_t mapaddr; |
| 216 | u32 maplen; |
| 217 | struct c2_rxp_hdr *rxp_hdr; |
| 218 | |
| 219 | skb = dev_alloc_skb(c2_port->rx_buf_size); |
| 220 | if (unlikely(!skb)) { |
| 221 | pr_debug("%s: out of memory for receive\n", |
| 222 | c2_port->netdev->name); |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | |
| 226 | /* Zero out the rxp hdr in the sk_buff */ |
| 227 | memset(skb->data, 0, sizeof(*rxp_hdr)); |
| 228 | |
| 229 | skb->dev = c2_port->netdev; |
| 230 | |
| 231 | maplen = c2_port->rx_buf_size; |
| 232 | mapaddr = |
| 233 | pci_map_single(c2dev->pcidev, skb->data, maplen, |
| 234 | PCI_DMA_FROMDEVICE); |
| 235 | |
| 236 | /* Set the sk_buff RXP_header to RXP_HRXD_READY */ |
| 237 | rxp_hdr = (struct c2_rxp_hdr *) skb->data; |
| 238 | rxp_hdr->flags = RXP_HRXD_READY; |
| 239 | |
| 240 | __raw_writew(0, elem->hw_desc + C2_RXP_STATUS); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 241 | __raw_writew((__force u16) cpu_to_be16((u16) maplen - sizeof(*rxp_hdr)), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 242 | elem->hw_desc + C2_RXP_LEN); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 243 | __raw_writeq((__force u64) cpu_to_be64(mapaddr), elem->hw_desc + C2_RXP_ADDR); |
| 244 | __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_READY), |
| 245 | elem->hw_desc + C2_RXP_FLAGS); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 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); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 293 | __raw_writeq((__force u64) cpu_to_be64(0x99aabbccddeeffULL), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 294 | elem->hw_desc + C2_RXP_ADDR); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 295 | __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_UNINIT), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 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); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 349 | __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_DONE), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 350 | elem->hw_desc + C2_TXP_FLAGS); |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 351 | c2_port->netdev->stats.tx_dropped++; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 352 | break; |
| 353 | } else { |
| 354 | __raw_writew(0, |
| 355 | elem->hw_desc + C2_TXP_LEN); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 356 | __raw_writeq((__force u64) cpu_to_be64(0x1122334455667788ULL), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 357 | elem->hw_desc + C2_TXP_ADDR); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 358 | __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_UNINIT), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 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 = |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 393 | be16_to_cpu((__force __be16) readw(elem->hw_desc + C2_TXP_FLAGS)); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 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 = |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 401 | be16_to_cpu((__force __be16) readw(elem->hw_desc + C2_TXP_LEN)); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 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); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 451 | __raw_writew((__force u16) cpu_to_be16((u16) elem->maplen - sizeof(*rxp_hdr)), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 452 | elem->hw_desc + C2_RXP_LEN); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 453 | __raw_writeq((__force u64) cpu_to_be64(elem->mapaddr), |
| 454 | elem->hw_desc + C2_RXP_ADDR); |
| 455 | __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_READY), |
| 456 | elem->hw_desc + C2_RXP_FLAGS); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 457 | |
| 458 | pr_debug("packet dropped\n"); |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 459 | c2_port->netdev->stats.rx_dropped++; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static void c2_rx_interrupt(struct net_device *netdev) |
| 463 | { |
| 464 | struct c2_port *c2_port = netdev_priv(netdev); |
| 465 | struct c2_dev *c2dev = c2_port->c2dev; |
| 466 | struct c2_ring *rx_ring = &c2_port->rx_ring; |
| 467 | struct c2_element *elem; |
| 468 | struct c2_rx_desc *rx_desc; |
| 469 | struct c2_rxp_hdr *rxp_hdr; |
| 470 | struct sk_buff *skb; |
| 471 | dma_addr_t mapaddr; |
| 472 | u32 maplen, buflen; |
| 473 | unsigned long flags; |
| 474 | |
| 475 | spin_lock_irqsave(&c2dev->lock, flags); |
| 476 | |
| 477 | /* Begin where we left off */ |
| 478 | rx_ring->to_clean = rx_ring->start + c2dev->cur_rx; |
| 479 | |
| 480 | for (elem = rx_ring->to_clean; elem->next != rx_ring->to_clean; |
| 481 | elem = elem->next) { |
| 482 | rx_desc = elem->ht_desc; |
| 483 | mapaddr = elem->mapaddr; |
| 484 | maplen = elem->maplen; |
| 485 | skb = elem->skb; |
| 486 | rxp_hdr = (struct c2_rxp_hdr *) skb->data; |
| 487 | |
| 488 | if (rxp_hdr->flags != RXP_HRXD_DONE) |
| 489 | break; |
| 490 | buflen = rxp_hdr->len; |
| 491 | |
| 492 | /* Sanity check the RXP header */ |
| 493 | if (rxp_hdr->status != RXP_HRXD_OK || |
| 494 | buflen > (rx_desc->len - sizeof(*rxp_hdr))) { |
| 495 | c2_rx_error(c2_port, elem); |
| 496 | continue; |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * Allocate and map a new skb for replenishing the host |
| 501 | * RX desc |
| 502 | */ |
| 503 | if (c2_rx_alloc(c2_port, elem)) { |
| 504 | c2_rx_error(c2_port, elem); |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | /* Unmap the old skb */ |
| 509 | pci_unmap_single(c2dev->pcidev, mapaddr, maplen, |
| 510 | PCI_DMA_FROMDEVICE); |
| 511 | |
| 512 | prefetch(skb->data); |
| 513 | |
| 514 | /* |
| 515 | * Skip past the leading 8 bytes comprising of the |
| 516 | * "struct c2_rxp_hdr", prepended by the adapter |
| 517 | * to the usual Ethernet header ("struct ethhdr"), |
| 518 | * to the start of the raw Ethernet packet. |
| 519 | * |
| 520 | * Fix up the various fields in the sk_buff before |
| 521 | * passing it up to netif_rx(). The transfer size |
| 522 | * (in bytes) specified by the adapter len field of |
| 523 | * the "struct rxp_hdr_t" does NOT include the |
| 524 | * "sizeof(struct c2_rxp_hdr)". |
| 525 | */ |
| 526 | skb->data += sizeof(*rxp_hdr); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 527 | skb_set_tail_pointer(skb, buflen); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 528 | skb->len = buflen; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 529 | skb->protocol = eth_type_trans(skb, netdev); |
| 530 | |
| 531 | netif_rx(skb); |
| 532 | |
| 533 | netdev->last_rx = jiffies; |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 534 | netdev->stats.rx_packets++; |
| 535 | netdev->stats.rx_bytes += buflen; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* Save where we left off */ |
| 539 | rx_ring->to_clean = elem; |
| 540 | c2dev->cur_rx = elem - rx_ring->start; |
| 541 | C2_SET_CUR_RX(c2dev, c2dev->cur_rx); |
| 542 | |
| 543 | spin_unlock_irqrestore(&c2dev->lock, flags); |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Handle netisr0 TX & RX interrupts. |
| 548 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 549 | static irqreturn_t c2_interrupt(int irq, void *dev_id) |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 550 | { |
| 551 | unsigned int netisr0, dmaisr; |
| 552 | int handled = 0; |
| 553 | struct c2_dev *c2dev = (struct c2_dev *) dev_id; |
| 554 | |
| 555 | /* Process CCILNET interrupts */ |
| 556 | netisr0 = readl(c2dev->regs + C2_NISR0); |
| 557 | if (netisr0) { |
| 558 | |
| 559 | /* |
| 560 | * There is an issue with the firmware that always |
| 561 | * provides the status of RX for both TX & RX |
| 562 | * interrupts. So process both queues here. |
| 563 | */ |
| 564 | c2_rx_interrupt(c2dev->netdev); |
| 565 | c2_tx_interrupt(c2dev->netdev); |
| 566 | |
| 567 | /* Clear the interrupt */ |
| 568 | writel(netisr0, c2dev->regs + C2_NISR0); |
| 569 | handled++; |
| 570 | } |
| 571 | |
| 572 | /* Process RNIC interrupts */ |
| 573 | dmaisr = readl(c2dev->regs + C2_DISR); |
| 574 | if (dmaisr) { |
| 575 | writel(dmaisr, c2dev->regs + C2_DISR); |
| 576 | c2_rnic_interrupt(c2dev); |
| 577 | handled++; |
| 578 | } |
| 579 | |
| 580 | if (handled) { |
| 581 | return IRQ_HANDLED; |
| 582 | } else { |
| 583 | return IRQ_NONE; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static int c2_up(struct net_device *netdev) |
| 588 | { |
| 589 | struct c2_port *c2_port = netdev_priv(netdev); |
| 590 | struct c2_dev *c2dev = c2_port->c2dev; |
| 591 | struct c2_element *elem; |
| 592 | struct c2_rxp_hdr *rxp_hdr; |
| 593 | struct in_device *in_dev; |
| 594 | size_t rx_size, tx_size; |
| 595 | int ret, i; |
| 596 | unsigned int netimr0; |
| 597 | |
| 598 | if (netif_msg_ifup(c2_port)) |
| 599 | pr_debug("%s: enabling interface\n", netdev->name); |
| 600 | |
| 601 | /* Set the Rx buffer size based on MTU */ |
| 602 | c2_set_rxbufsize(c2_port); |
| 603 | |
| 604 | /* Allocate DMA'able memory for Tx/Rx host descriptor rings */ |
| 605 | rx_size = c2_port->rx_ring.count * sizeof(struct c2_rx_desc); |
| 606 | tx_size = c2_port->tx_ring.count * sizeof(struct c2_tx_desc); |
| 607 | |
| 608 | c2_port->mem_size = tx_size + rx_size; |
| 609 | c2_port->mem = pci_alloc_consistent(c2dev->pcidev, c2_port->mem_size, |
| 610 | &c2_port->dma); |
| 611 | if (c2_port->mem == NULL) { |
| 612 | pr_debug("Unable to allocate memory for " |
| 613 | "host descriptor rings\n"); |
| 614 | return -ENOMEM; |
| 615 | } |
| 616 | |
| 617 | memset(c2_port->mem, 0, c2_port->mem_size); |
| 618 | |
| 619 | /* Create the Rx host descriptor ring */ |
| 620 | if ((ret = |
| 621 | c2_rx_ring_alloc(&c2_port->rx_ring, c2_port->mem, c2_port->dma, |
| 622 | c2dev->mmio_rxp_ring))) { |
| 623 | pr_debug("Unable to create RX ring\n"); |
| 624 | goto bail0; |
| 625 | } |
| 626 | |
| 627 | /* Allocate Rx buffers for the host descriptor ring */ |
| 628 | if (c2_rx_fill(c2_port)) { |
| 629 | pr_debug("Unable to fill RX ring\n"); |
| 630 | goto bail1; |
| 631 | } |
| 632 | |
| 633 | /* Create the Tx host descriptor ring */ |
| 634 | if ((ret = c2_tx_ring_alloc(&c2_port->tx_ring, c2_port->mem + rx_size, |
| 635 | c2_port->dma + rx_size, |
| 636 | c2dev->mmio_txp_ring))) { |
| 637 | pr_debug("Unable to create TX ring\n"); |
| 638 | goto bail1; |
| 639 | } |
| 640 | |
| 641 | /* Set the TX pointer to where we left off */ |
| 642 | c2_port->tx_avail = c2_port->tx_ring.count - 1; |
| 643 | c2_port->tx_ring.to_use = c2_port->tx_ring.to_clean = |
| 644 | c2_port->tx_ring.start + c2dev->cur_tx; |
| 645 | |
| 646 | /* missing: Initialize MAC */ |
| 647 | |
| 648 | BUG_ON(c2_port->tx_ring.to_use != c2_port->tx_ring.to_clean); |
| 649 | |
| 650 | /* Reset the adapter, ensures the driver is in sync with the RXP */ |
| 651 | c2_reset(c2_port); |
| 652 | |
| 653 | /* Reset the READY bit in the sk_buff RXP headers & adapter HRXDQ */ |
| 654 | for (i = 0, elem = c2_port->rx_ring.start; i < c2_port->rx_ring.count; |
| 655 | i++, elem++) { |
| 656 | rxp_hdr = (struct c2_rxp_hdr *) elem->skb->data; |
| 657 | rxp_hdr->flags = 0; |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 658 | __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_READY), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 659 | elem->hw_desc + C2_RXP_FLAGS); |
| 660 | } |
| 661 | |
| 662 | /* Enable network packets */ |
| 663 | netif_start_queue(netdev); |
| 664 | |
| 665 | /* Enable IRQ */ |
| 666 | writel(0, c2dev->regs + C2_IDIS); |
| 667 | netimr0 = readl(c2dev->regs + C2_NIMR0); |
| 668 | netimr0 &= ~(C2_PCI_HTX_INT | C2_PCI_HRX_INT); |
| 669 | writel(netimr0, c2dev->regs + C2_NIMR0); |
| 670 | |
| 671 | /* Tell the stack to ignore arp requests for ipaddrs bound to |
| 672 | * other interfaces. This is needed to prevent the host stack |
| 673 | * from responding to arp requests to the ipaddr bound on the |
| 674 | * rdma interface. |
| 675 | */ |
| 676 | in_dev = in_dev_get(netdev); |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 677 | IN_DEV_CONF_SET(in_dev, ARP_IGNORE, 1); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 678 | in_dev_put(in_dev); |
| 679 | |
| 680 | return 0; |
| 681 | |
| 682 | bail1: |
| 683 | c2_rx_clean(c2_port); |
| 684 | kfree(c2_port->rx_ring.start); |
| 685 | |
| 686 | bail0: |
| 687 | pci_free_consistent(c2dev->pcidev, c2_port->mem_size, c2_port->mem, |
| 688 | c2_port->dma); |
| 689 | |
| 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | static int c2_down(struct net_device *netdev) |
| 694 | { |
| 695 | struct c2_port *c2_port = netdev_priv(netdev); |
| 696 | struct c2_dev *c2dev = c2_port->c2dev; |
| 697 | |
| 698 | if (netif_msg_ifdown(c2_port)) |
| 699 | pr_debug("%s: disabling interface\n", |
| 700 | netdev->name); |
| 701 | |
| 702 | /* Wait for all the queued packets to get sent */ |
| 703 | c2_tx_interrupt(netdev); |
| 704 | |
| 705 | /* Disable network packets */ |
| 706 | netif_stop_queue(netdev); |
| 707 | |
| 708 | /* Disable IRQs by clearing the interrupt mask */ |
| 709 | writel(1, c2dev->regs + C2_IDIS); |
| 710 | writel(0, c2dev->regs + C2_NIMR0); |
| 711 | |
| 712 | /* missing: Stop transmitter */ |
| 713 | |
| 714 | /* missing: Stop receiver */ |
| 715 | |
| 716 | /* Reset the adapter, ensures the driver is in sync with the RXP */ |
| 717 | c2_reset(c2_port); |
| 718 | |
| 719 | /* missing: Turn off LEDs here */ |
| 720 | |
| 721 | /* Free all buffers in the host descriptor rings */ |
| 722 | c2_tx_clean(c2_port); |
| 723 | c2_rx_clean(c2_port); |
| 724 | |
| 725 | /* Free the host descriptor rings */ |
| 726 | kfree(c2_port->rx_ring.start); |
| 727 | kfree(c2_port->tx_ring.start); |
| 728 | pci_free_consistent(c2dev->pcidev, c2_port->mem_size, c2_port->mem, |
| 729 | c2_port->dma); |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static void c2_reset(struct c2_port *c2_port) |
| 735 | { |
| 736 | struct c2_dev *c2dev = c2_port->c2dev; |
| 737 | unsigned int cur_rx = c2dev->cur_rx; |
| 738 | |
| 739 | /* Tell the hardware to quiesce */ |
| 740 | C2_SET_CUR_RX(c2dev, cur_rx | C2_PCI_HRX_QUI); |
| 741 | |
| 742 | /* |
| 743 | * The hardware will reset the C2_PCI_HRX_QUI bit once |
| 744 | * the RXP is quiesced. Wait 2 seconds for this. |
| 745 | */ |
| 746 | ssleep(2); |
| 747 | |
| 748 | cur_rx = C2_GET_CUR_RX(c2dev); |
| 749 | |
| 750 | if (cur_rx & C2_PCI_HRX_QUI) |
| 751 | pr_debug("c2_reset: failed to quiesce the hardware!\n"); |
| 752 | |
| 753 | cur_rx &= ~C2_PCI_HRX_QUI; |
| 754 | |
| 755 | c2dev->cur_rx = cur_rx; |
| 756 | |
| 757 | pr_debug("Current RX: %u\n", c2dev->cur_rx); |
| 758 | } |
| 759 | |
| 760 | static int c2_xmit_frame(struct sk_buff *skb, struct net_device *netdev) |
| 761 | { |
| 762 | struct c2_port *c2_port = netdev_priv(netdev); |
| 763 | struct c2_dev *c2dev = c2_port->c2dev; |
| 764 | struct c2_ring *tx_ring = &c2_port->tx_ring; |
| 765 | struct c2_element *elem; |
| 766 | dma_addr_t mapaddr; |
| 767 | u32 maplen; |
| 768 | unsigned long flags; |
| 769 | unsigned int i; |
| 770 | |
| 771 | spin_lock_irqsave(&c2_port->tx_lock, flags); |
| 772 | |
| 773 | if (unlikely(c2_port->tx_avail < (skb_shinfo(skb)->nr_frags + 1))) { |
| 774 | netif_stop_queue(netdev); |
| 775 | spin_unlock_irqrestore(&c2_port->tx_lock, flags); |
| 776 | |
| 777 | pr_debug("%s: Tx ring full when queue awake!\n", |
| 778 | netdev->name); |
| 779 | return NETDEV_TX_BUSY; |
| 780 | } |
| 781 | |
| 782 | maplen = skb_headlen(skb); |
| 783 | mapaddr = |
| 784 | pci_map_single(c2dev->pcidev, skb->data, maplen, PCI_DMA_TODEVICE); |
| 785 | |
| 786 | elem = tx_ring->to_use; |
| 787 | elem->skb = skb; |
| 788 | elem->mapaddr = mapaddr; |
| 789 | elem->maplen = maplen; |
| 790 | |
| 791 | /* Tell HW to xmit */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 792 | __raw_writeq((__force u64) cpu_to_be64(mapaddr), |
| 793 | elem->hw_desc + C2_TXP_ADDR); |
| 794 | __raw_writew((__force u16) cpu_to_be16(maplen), |
| 795 | elem->hw_desc + C2_TXP_LEN); |
| 796 | __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_READY), |
| 797 | elem->hw_desc + C2_TXP_FLAGS); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 798 | |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 799 | netdev->stats.tx_packets++; |
| 800 | netdev->stats.tx_bytes += maplen; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 801 | |
| 802 | /* Loop thru additional data fragments and queue them */ |
| 803 | if (skb_shinfo(skb)->nr_frags) { |
| 804 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 805 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 806 | maplen = frag->size; |
| 807 | mapaddr = |
| 808 | pci_map_page(c2dev->pcidev, frag->page, |
| 809 | frag->page_offset, maplen, |
| 810 | PCI_DMA_TODEVICE); |
| 811 | |
| 812 | elem = elem->next; |
| 813 | elem->skb = NULL; |
| 814 | elem->mapaddr = mapaddr; |
| 815 | elem->maplen = maplen; |
| 816 | |
| 817 | /* Tell HW to xmit */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 818 | __raw_writeq((__force u64) cpu_to_be64(mapaddr), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 819 | elem->hw_desc + C2_TXP_ADDR); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 820 | __raw_writew((__force u16) cpu_to_be16(maplen), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 821 | elem->hw_desc + C2_TXP_LEN); |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 822 | __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_READY), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 823 | elem->hw_desc + C2_TXP_FLAGS); |
| 824 | |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 825 | netdev->stats.tx_packets++; |
| 826 | netdev->stats.tx_bytes += maplen; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
| 830 | tx_ring->to_use = elem->next; |
| 831 | c2_port->tx_avail -= (skb_shinfo(skb)->nr_frags + 1); |
| 832 | |
| 833 | if (c2_port->tx_avail <= MAX_SKB_FRAGS + 1) { |
| 834 | netif_stop_queue(netdev); |
| 835 | if (netif_msg_tx_queued(c2_port)) |
| 836 | pr_debug("%s: transmit queue full\n", |
| 837 | netdev->name); |
| 838 | } |
| 839 | |
| 840 | spin_unlock_irqrestore(&c2_port->tx_lock, flags); |
| 841 | |
| 842 | netdev->trans_start = jiffies; |
| 843 | |
| 844 | return NETDEV_TX_OK; |
| 845 | } |
| 846 | |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 847 | static void c2_tx_timeout(struct net_device *netdev) |
| 848 | { |
| 849 | struct c2_port *c2_port = netdev_priv(netdev); |
| 850 | |
| 851 | if (netif_msg_timer(c2_port)) |
| 852 | pr_debug("%s: tx timeout\n", netdev->name); |
| 853 | |
| 854 | c2_tx_clean(c2_port); |
| 855 | } |
| 856 | |
| 857 | static int c2_change_mtu(struct net_device *netdev, int new_mtu) |
| 858 | { |
| 859 | int ret = 0; |
| 860 | |
| 861 | if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU) |
| 862 | return -EINVAL; |
| 863 | |
| 864 | netdev->mtu = new_mtu; |
| 865 | |
| 866 | if (netif_running(netdev)) { |
| 867 | c2_down(netdev); |
| 868 | |
| 869 | c2_up(netdev); |
| 870 | } |
| 871 | |
| 872 | return ret; |
| 873 | } |
| 874 | |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 875 | static const struct net_device_ops c2_netdev = { |
| 876 | .ndo_open = c2_up, |
| 877 | .ndo_stop = c2_down, |
| 878 | .ndo_start_xmit = c2_xmit_frame, |
| 879 | .ndo_tx_timeout = c2_tx_timeout, |
| 880 | .ndo_change_mtu = c2_change_mtu, |
| 881 | .ndo_set_mac_address = eth_mac_addr, |
| 882 | .ndo_validate_addr = eth_validate_addr, |
| 883 | }; |
| 884 | |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 885 | /* Initialize network device */ |
| 886 | static struct net_device *c2_devinit(struct c2_dev *c2dev, |
| 887 | void __iomem * mmio_addr) |
| 888 | { |
| 889 | struct c2_port *c2_port = NULL; |
| 890 | struct net_device *netdev = alloc_etherdev(sizeof(*c2_port)); |
| 891 | |
| 892 | if (!netdev) { |
| 893 | pr_debug("c2_port etherdev alloc failed"); |
| 894 | return NULL; |
| 895 | } |
| 896 | |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 897 | SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev); |
| 898 | |
Stephen Hemminger | 687c75d | 2009-03-20 19:35:30 +0000 | [diff] [blame] | 899 | netdev->netdev_ops = &c2_netdev; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 900 | netdev->watchdog_timeo = C2_TX_TIMEOUT; |
| 901 | netdev->irq = c2dev->pcidev->irq; |
| 902 | |
| 903 | c2_port = netdev_priv(netdev); |
| 904 | c2_port->netdev = netdev; |
| 905 | c2_port->c2dev = c2dev; |
| 906 | c2_port->msg_enable = netif_msg_init(debug, default_msg); |
| 907 | c2_port->tx_ring.count = C2_NUM_TX_DESC; |
| 908 | c2_port->rx_ring.count = C2_NUM_RX_DESC; |
| 909 | |
| 910 | spin_lock_init(&c2_port->tx_lock); |
| 911 | |
| 912 | /* Copy our 48-bit ethernet hardware address */ |
| 913 | memcpy_fromio(netdev->dev_addr, mmio_addr + C2_REGS_ENADDR, 6); |
| 914 | |
| 915 | /* Validate the MAC address */ |
| 916 | if (!is_valid_ether_addr(netdev->dev_addr)) { |
| 917 | pr_debug("Invalid MAC Address\n"); |
| 918 | c2_print_macaddr(netdev); |
| 919 | free_netdev(netdev); |
| 920 | return NULL; |
| 921 | } |
| 922 | |
| 923 | c2dev->netdev = netdev; |
| 924 | |
| 925 | return netdev; |
| 926 | } |
| 927 | |
| 928 | static int __devinit c2_probe(struct pci_dev *pcidev, |
| 929 | const struct pci_device_id *ent) |
| 930 | { |
| 931 | int ret = 0, i; |
| 932 | unsigned long reg0_start, reg0_flags, reg0_len; |
| 933 | unsigned long reg2_start, reg2_flags, reg2_len; |
| 934 | unsigned long reg4_start, reg4_flags, reg4_len; |
| 935 | unsigned kva_map_size; |
| 936 | struct net_device *netdev = NULL; |
| 937 | struct c2_dev *c2dev = NULL; |
| 938 | void __iomem *mmio_regs = NULL; |
| 939 | |
| 940 | printk(KERN_INFO PFX "AMSO1100 Gigabit Ethernet driver v%s loaded\n", |
| 941 | DRV_VERSION); |
| 942 | |
| 943 | /* Enable PCI device */ |
| 944 | ret = pci_enable_device(pcidev); |
| 945 | if (ret) { |
| 946 | printk(KERN_ERR PFX "%s: Unable to enable PCI device\n", |
| 947 | pci_name(pcidev)); |
| 948 | goto bail0; |
| 949 | } |
| 950 | |
| 951 | reg0_start = pci_resource_start(pcidev, BAR_0); |
| 952 | reg0_len = pci_resource_len(pcidev, BAR_0); |
| 953 | reg0_flags = pci_resource_flags(pcidev, BAR_0); |
| 954 | |
| 955 | reg2_start = pci_resource_start(pcidev, BAR_2); |
| 956 | reg2_len = pci_resource_len(pcidev, BAR_2); |
| 957 | reg2_flags = pci_resource_flags(pcidev, BAR_2); |
| 958 | |
| 959 | reg4_start = pci_resource_start(pcidev, BAR_4); |
| 960 | reg4_len = pci_resource_len(pcidev, BAR_4); |
| 961 | reg4_flags = pci_resource_flags(pcidev, BAR_4); |
| 962 | |
| 963 | pr_debug("BAR0 size = 0x%lX bytes\n", reg0_len); |
| 964 | pr_debug("BAR2 size = 0x%lX bytes\n", reg2_len); |
| 965 | pr_debug("BAR4 size = 0x%lX bytes\n", reg4_len); |
| 966 | |
| 967 | /* Make sure PCI base addr are MMIO */ |
| 968 | if (!(reg0_flags & IORESOURCE_MEM) || |
| 969 | !(reg2_flags & IORESOURCE_MEM) || !(reg4_flags & IORESOURCE_MEM)) { |
| 970 | printk(KERN_ERR PFX "PCI regions not an MMIO resource\n"); |
| 971 | ret = -ENODEV; |
| 972 | goto bail1; |
| 973 | } |
| 974 | |
| 975 | /* Check for weird/broken PCI region reporting */ |
| 976 | if ((reg0_len < C2_REG0_SIZE) || |
| 977 | (reg2_len < C2_REG2_SIZE) || (reg4_len < C2_REG4_SIZE)) { |
| 978 | printk(KERN_ERR PFX "Invalid PCI region sizes\n"); |
| 979 | ret = -ENODEV; |
| 980 | goto bail1; |
| 981 | } |
| 982 | |
| 983 | /* Reserve PCI I/O and memory resources */ |
| 984 | ret = pci_request_regions(pcidev, DRV_NAME); |
| 985 | if (ret) { |
| 986 | printk(KERN_ERR PFX "%s: Unable to request regions\n", |
| 987 | pci_name(pcidev)); |
| 988 | goto bail1; |
| 989 | } |
| 990 | |
| 991 | if ((sizeof(dma_addr_t) > 4)) { |
Yang Hongyang | 6a35528 | 2009-04-06 19:01:13 -0700 | [diff] [blame] | 992 | ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64)); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 993 | if (ret < 0) { |
| 994 | printk(KERN_ERR PFX "64b DMA configuration failed\n"); |
| 995 | goto bail2; |
| 996 | } |
| 997 | } else { |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 998 | ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32)); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 999 | if (ret < 0) { |
| 1000 | printk(KERN_ERR PFX "32b DMA configuration failed\n"); |
| 1001 | goto bail2; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* Enables bus-mastering on the device */ |
| 1006 | pci_set_master(pcidev); |
| 1007 | |
| 1008 | /* Remap the adapter PCI registers in BAR4 */ |
| 1009 | mmio_regs = ioremap_nocache(reg4_start + C2_PCI_REGS_OFFSET, |
| 1010 | sizeof(struct c2_adapter_pci_regs)); |
Roland Dreier | 4b29043 | 2008-04-16 21:01:06 -0700 | [diff] [blame] | 1011 | if (!mmio_regs) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1012 | printk(KERN_ERR PFX |
| 1013 | "Unable to remap adapter PCI registers in BAR4\n"); |
| 1014 | ret = -EIO; |
| 1015 | goto bail2; |
| 1016 | } |
| 1017 | |
| 1018 | /* Validate PCI regs magic */ |
| 1019 | for (i = 0; i < sizeof(c2_magic); i++) { |
| 1020 | if (c2_magic[i] != readb(mmio_regs + C2_REGS_MAGIC + i)) { |
| 1021 | printk(KERN_ERR PFX "Downlevel Firmware boot loader " |
| 1022 | "[%d/%Zd: got 0x%x, exp 0x%x]. Use the cc_flash " |
| 1023 | "utility to update your boot loader\n", |
| 1024 | i + 1, sizeof(c2_magic), |
| 1025 | readb(mmio_regs + C2_REGS_MAGIC + i), |
| 1026 | c2_magic[i]); |
| 1027 | printk(KERN_ERR PFX "Adapter not claimed\n"); |
| 1028 | iounmap(mmio_regs); |
| 1029 | ret = -EIO; |
| 1030 | goto bail2; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* Validate the adapter version */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 1035 | if (be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_VERS)) != C2_VERSION) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1036 | printk(KERN_ERR PFX "Version mismatch " |
| 1037 | "[fw=%u, c2=%u], Adapter not claimed\n", |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 1038 | be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_VERS)), |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1039 | C2_VERSION); |
| 1040 | ret = -EINVAL; |
| 1041 | iounmap(mmio_regs); |
| 1042 | goto bail2; |
| 1043 | } |
| 1044 | |
| 1045 | /* Validate the adapter IVN */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 1046 | if (be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_IVN)) != C2_IVN) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1047 | printk(KERN_ERR PFX "Downlevel FIrmware level. You should be using " |
| 1048 | "the OpenIB device support kit. " |
| 1049 | "[fw=0x%x, c2=0x%x], Adapter not claimed\n", |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 1050 | be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_IVN)), |
| 1051 | C2_IVN); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1052 | ret = -EINVAL; |
| 1053 | iounmap(mmio_regs); |
| 1054 | goto bail2; |
| 1055 | } |
| 1056 | |
| 1057 | /* Allocate hardware structure */ |
| 1058 | c2dev = (struct c2_dev *) ib_alloc_device(sizeof(*c2dev)); |
| 1059 | if (!c2dev) { |
| 1060 | printk(KERN_ERR PFX "%s: Unable to alloc hardware struct\n", |
| 1061 | pci_name(pcidev)); |
| 1062 | ret = -ENOMEM; |
| 1063 | iounmap(mmio_regs); |
| 1064 | goto bail2; |
| 1065 | } |
| 1066 | |
| 1067 | memset(c2dev, 0, sizeof(*c2dev)); |
| 1068 | spin_lock_init(&c2dev->lock); |
| 1069 | c2dev->pcidev = pcidev; |
| 1070 | c2dev->cur_tx = 0; |
| 1071 | |
| 1072 | /* Get the last RX index */ |
| 1073 | c2dev->cur_rx = |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 1074 | (be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_HRX_CUR)) - |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1075 | 0xffffc000) / sizeof(struct c2_rxp_desc); |
| 1076 | |
| 1077 | /* Request an interrupt line for the driver */ |
Thomas Gleixner | 38515e9 | 2007-02-14 00:33:16 -0800 | [diff] [blame] | 1078 | ret = request_irq(pcidev->irq, c2_interrupt, IRQF_SHARED, DRV_NAME, c2dev); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1079 | if (ret) { |
| 1080 | printk(KERN_ERR PFX "%s: requested IRQ %u is busy\n", |
| 1081 | pci_name(pcidev), pcidev->irq); |
| 1082 | iounmap(mmio_regs); |
| 1083 | goto bail3; |
| 1084 | } |
| 1085 | |
| 1086 | /* Set driver specific data */ |
| 1087 | pci_set_drvdata(pcidev, c2dev); |
| 1088 | |
| 1089 | /* Initialize network device */ |
| 1090 | if ((netdev = c2_devinit(c2dev, mmio_regs)) == NULL) { |
| 1091 | iounmap(mmio_regs); |
| 1092 | goto bail4; |
| 1093 | } |
| 1094 | |
| 1095 | /* Save off the actual size prior to unmapping mmio_regs */ |
Roland Dreier | dc544bc | 2008-04-16 21:01:08 -0700 | [diff] [blame] | 1096 | kva_map_size = be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_PCI_WINSIZE)); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1097 | |
| 1098 | /* Unmap the adapter PCI registers in BAR4 */ |
| 1099 | iounmap(mmio_regs); |
| 1100 | |
| 1101 | /* Register network device */ |
| 1102 | ret = register_netdev(netdev); |
| 1103 | if (ret) { |
| 1104 | printk(KERN_ERR PFX "Unable to register netdev, ret = %d\n", |
| 1105 | ret); |
| 1106 | goto bail5; |
| 1107 | } |
| 1108 | |
| 1109 | /* Disable network packets */ |
| 1110 | netif_stop_queue(netdev); |
| 1111 | |
| 1112 | /* Remap the adapter HRXDQ PA space to kernel VA space */ |
| 1113 | c2dev->mmio_rxp_ring = ioremap_nocache(reg4_start + C2_RXP_HRXDQ_OFFSET, |
| 1114 | C2_RXP_HRXDQ_SIZE); |
Roland Dreier | 4b29043 | 2008-04-16 21:01:06 -0700 | [diff] [blame] | 1115 | if (!c2dev->mmio_rxp_ring) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1116 | printk(KERN_ERR PFX "Unable to remap MMIO HRXDQ region\n"); |
| 1117 | ret = -EIO; |
| 1118 | goto bail6; |
| 1119 | } |
| 1120 | |
| 1121 | /* Remap the adapter HTXDQ PA space to kernel VA space */ |
| 1122 | c2dev->mmio_txp_ring = ioremap_nocache(reg4_start + C2_TXP_HTXDQ_OFFSET, |
| 1123 | C2_TXP_HTXDQ_SIZE); |
Roland Dreier | 4b29043 | 2008-04-16 21:01:06 -0700 | [diff] [blame] | 1124 | if (!c2dev->mmio_txp_ring) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1125 | printk(KERN_ERR PFX "Unable to remap MMIO HTXDQ region\n"); |
| 1126 | ret = -EIO; |
| 1127 | goto bail7; |
| 1128 | } |
| 1129 | |
| 1130 | /* Save off the current RX index in the last 4 bytes of the TXP Ring */ |
| 1131 | C2_SET_CUR_RX(c2dev, c2dev->cur_rx); |
| 1132 | |
| 1133 | /* Remap the PCI registers in adapter BAR0 to kernel VA space */ |
| 1134 | c2dev->regs = ioremap_nocache(reg0_start, reg0_len); |
Roland Dreier | 4b29043 | 2008-04-16 21:01:06 -0700 | [diff] [blame] | 1135 | if (!c2dev->regs) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1136 | printk(KERN_ERR PFX "Unable to remap BAR0\n"); |
| 1137 | ret = -EIO; |
| 1138 | goto bail8; |
| 1139 | } |
| 1140 | |
| 1141 | /* Remap the PCI registers in adapter BAR4 to kernel VA space */ |
| 1142 | c2dev->pa = reg4_start + C2_PCI_REGS_OFFSET; |
| 1143 | c2dev->kva = ioremap_nocache(reg4_start + C2_PCI_REGS_OFFSET, |
| 1144 | kva_map_size); |
Roland Dreier | 4b29043 | 2008-04-16 21:01:06 -0700 | [diff] [blame] | 1145 | if (!c2dev->kva) { |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1146 | printk(KERN_ERR PFX "Unable to remap BAR4\n"); |
| 1147 | ret = -EIO; |
| 1148 | goto bail9; |
| 1149 | } |
| 1150 | |
| 1151 | /* Print out the MAC address */ |
| 1152 | c2_print_macaddr(netdev); |
| 1153 | |
| 1154 | ret = c2_rnic_init(c2dev); |
| 1155 | if (ret) { |
| 1156 | printk(KERN_ERR PFX "c2_rnic_init failed: %d\n", ret); |
| 1157 | goto bail10; |
| 1158 | } |
| 1159 | |
Tom Tucker | 2ffcab6 | 2006-11-08 14:23:22 -0600 | [diff] [blame] | 1160 | if (c2_register_device(c2dev)) |
| 1161 | goto bail10; |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1162 | |
| 1163 | return 0; |
| 1164 | |
| 1165 | bail10: |
| 1166 | iounmap(c2dev->kva); |
| 1167 | |
| 1168 | bail9: |
| 1169 | iounmap(c2dev->regs); |
| 1170 | |
| 1171 | bail8: |
| 1172 | iounmap(c2dev->mmio_txp_ring); |
| 1173 | |
| 1174 | bail7: |
| 1175 | iounmap(c2dev->mmio_rxp_ring); |
| 1176 | |
| 1177 | bail6: |
| 1178 | unregister_netdev(netdev); |
| 1179 | |
| 1180 | bail5: |
| 1181 | free_netdev(netdev); |
| 1182 | |
| 1183 | bail4: |
| 1184 | free_irq(pcidev->irq, c2dev); |
| 1185 | |
| 1186 | bail3: |
| 1187 | ib_dealloc_device(&c2dev->ibdev); |
| 1188 | |
| 1189 | bail2: |
| 1190 | pci_release_regions(pcidev); |
| 1191 | |
| 1192 | bail1: |
| 1193 | pci_disable_device(pcidev); |
| 1194 | |
| 1195 | bail0: |
| 1196 | return ret; |
| 1197 | } |
| 1198 | |
| 1199 | static void __devexit c2_remove(struct pci_dev *pcidev) |
| 1200 | { |
| 1201 | struct c2_dev *c2dev = pci_get_drvdata(pcidev); |
| 1202 | struct net_device *netdev = c2dev->netdev; |
| 1203 | |
| 1204 | /* Unregister with OpenIB */ |
| 1205 | c2_unregister_device(c2dev); |
| 1206 | |
| 1207 | /* Clean up the RNIC resources */ |
| 1208 | c2_rnic_term(c2dev); |
| 1209 | |
| 1210 | /* Remove network device from the kernel */ |
| 1211 | unregister_netdev(netdev); |
| 1212 | |
| 1213 | /* Free network device */ |
| 1214 | free_netdev(netdev); |
| 1215 | |
| 1216 | /* Free the interrupt line */ |
| 1217 | free_irq(pcidev->irq, c2dev); |
| 1218 | |
| 1219 | /* missing: Turn LEDs off here */ |
| 1220 | |
| 1221 | /* Unmap adapter PA space */ |
| 1222 | iounmap(c2dev->kva); |
| 1223 | iounmap(c2dev->regs); |
| 1224 | iounmap(c2dev->mmio_txp_ring); |
| 1225 | iounmap(c2dev->mmio_rxp_ring); |
| 1226 | |
| 1227 | /* Free the hardware structure */ |
| 1228 | ib_dealloc_device(&c2dev->ibdev); |
| 1229 | |
| 1230 | /* Release reserved PCI I/O and memory resources */ |
| 1231 | pci_release_regions(pcidev); |
| 1232 | |
| 1233 | /* Disable PCI device */ |
| 1234 | pci_disable_device(pcidev); |
| 1235 | |
| 1236 | /* Clear driver specific data */ |
| 1237 | pci_set_drvdata(pcidev, NULL); |
| 1238 | } |
| 1239 | |
| 1240 | static struct pci_driver c2_pci_driver = { |
| 1241 | .name = DRV_NAME, |
| 1242 | .id_table = c2_pci_table, |
| 1243 | .probe = c2_probe, |
| 1244 | .remove = __devexit_p(c2_remove), |
| 1245 | }; |
| 1246 | |
| 1247 | static int __init c2_init_module(void) |
| 1248 | { |
Henrik Kretzschmar | d986a27 | 2006-10-10 14:26:01 -0700 | [diff] [blame] | 1249 | return pci_register_driver(&c2_pci_driver); |
Tom Tucker | f94b533 | 2006-09-22 15:22:48 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | static void __exit c2_exit_module(void) |
| 1253 | { |
| 1254 | pci_unregister_driver(&c2_pci_driver); |
| 1255 | } |
| 1256 | |
| 1257 | module_init(c2_init_module); |
| 1258 | module_exit(c2_exit_module); |