Merge branch 'fix/pcm-hwptr' into for-linus
[linux/fpc-iii.git] / drivers / infiniband / hw / amso1100 / c2.c
blob0cfbb6d2f762b5699c4edef953ab5757196dae5c
1 /*
2 * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
3 * Copyright (c) 2005 Open Grid Computing, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
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>
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/byteorder.h>
54 #include <rdma/ib_smi.h>
55 #include "c2.h"
56 #include "c2_provider.h"
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);
63 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
64 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
66 static int debug = -1; /* defaults above */
67 module_param(debug, int, 0);
68 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
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);
75 static irqreturn_t c2_interrupt(int irq, void *dev_id);
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);
80 static struct pci_device_id c2_pci_table[] = {
81 { PCI_DEVICE(0x18b8, 0xb001) },
82 { 0 }
85 MODULE_DEVICE_TABLE(pci, c2_pci_table);
87 static void c2_print_macaddr(struct net_device *netdev)
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);
96 static void c2_set_rxbufsize(struct c2_port *c2_port)
98 struct net_device *netdev = c2_port->netdev;
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;
109 * Allocate TX ring elements and chain them together.
110 * One-to-one association of adapter descriptors with ring elements.
112 static int c2_tx_ring_alloc(struct c2_ring *tx_ring, void *vaddr,
113 dma_addr_t base, void __iomem * mmio_txp_ring)
115 struct c2_tx_desc *tx_desc;
116 struct c2_txp_desc __iomem *txp_desc;
117 struct c2_element *elem;
118 int i;
120 tx_ring->start = kmalloc(sizeof(*elem) * tx_ring->count, GFP_KERNEL);
121 if (!tx_ring->start)
122 return -ENOMEM;
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;
131 /* Set TXP_HTXD_UNINIT */
132 __raw_writeq((__force u64) cpu_to_be64(0x1122334455667788ULL),
133 (void __iomem *) txp_desc + C2_TXP_ADDR);
134 __raw_writew(0, (void __iomem *) txp_desc + C2_TXP_LEN);
135 __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_UNINIT),
136 (void __iomem *) txp_desc + C2_TXP_FLAGS);
138 elem->skb = NULL;
139 elem->ht_desc = tx_desc;
140 elem->hw_desc = txp_desc;
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);
152 tx_ring->to_use = tx_ring->to_clean = tx_ring->start;
154 return 0;
158 * Allocate RX ring elements and chain them together.
159 * One-to-one association of adapter descriptors with ring elements.
161 static int c2_rx_ring_alloc(struct c2_ring *rx_ring, void *vaddr,
162 dma_addr_t base, void __iomem * mmio_rxp_ring)
164 struct c2_rx_desc *rx_desc;
165 struct c2_rxp_desc __iomem *rxp_desc;
166 struct c2_element *elem;
167 int i;
169 rx_ring->start = kmalloc(sizeof(*elem) * rx_ring->count, GFP_KERNEL);
170 if (!rx_ring->start)
171 return -ENOMEM;
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;
180 /* Set RXP_HRXD_UNINIT */
181 __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_OK),
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);
185 __raw_writeq((__force u64) cpu_to_be64(0x99aabbccddeeffULL),
186 (void __iomem *) rxp_desc + C2_RXP_ADDR);
187 __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_UNINIT),
188 (void __iomem *) rxp_desc + C2_RXP_FLAGS);
190 elem->skb = NULL;
191 elem->ht_desc = rx_desc;
192 elem->hw_desc = rxp_desc;
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);
204 rx_ring->to_use = rx_ring->to_clean = rx_ring->start;
206 return 0;
209 /* Setup buffer for receiving */
210 static inline int c2_rx_alloc(struct c2_port *c2_port, struct c2_element *elem)
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;
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;
226 /* Zero out the rxp hdr in the sk_buff */
227 memset(skb->data, 0, sizeof(*rxp_hdr));
229 skb->dev = c2_port->netdev;
231 maplen = c2_port->rx_buf_size;
232 mapaddr =
233 pci_map_single(c2dev->pcidev, skb->data, maplen,
234 PCI_DMA_FROMDEVICE);
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;
240 __raw_writew(0, elem->hw_desc + C2_RXP_STATUS);
241 __raw_writew((__force u16) cpu_to_be16((u16) maplen - sizeof(*rxp_hdr)),
242 elem->hw_desc + C2_RXP_LEN);
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);
247 elem->skb = skb;
248 elem->mapaddr = mapaddr;
249 elem->maplen = maplen;
250 rx_desc->len = maplen;
252 return 0;
256 * Allocate buffers for the Rx ring
257 * For receive: rx_ring.to_clean is next received frame
259 static int c2_rx_fill(struct c2_port *c2_port)
261 struct c2_ring *rx_ring = &c2_port->rx_ring;
262 struct c2_element *elem;
263 int ret = 0;
265 elem = rx_ring->start;
266 do {
267 if (c2_rx_alloc(c2_port, elem)) {
268 ret = 1;
269 break;
271 } while ((elem = elem->next) != rx_ring->start);
273 rx_ring->to_clean = rx_ring->start;
274 return ret;
277 /* Free all buffers in RX ring, assumes receiver stopped */
278 static void c2_rx_clean(struct c2_port *c2_port)
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;
285 elem = rx_ring->start;
286 do {
287 rx_desc = elem->ht_desc;
288 rx_desc->len = 0;
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((__force u64) cpu_to_be64(0x99aabbccddeeffULL),
294 elem->hw_desc + C2_RXP_ADDR);
295 __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_UNINIT),
296 elem->hw_desc + C2_RXP_FLAGS);
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;
304 } while ((elem = elem->next) != rx_ring->start);
307 static inline int c2_tx_free(struct c2_dev *c2dev, struct c2_element *elem)
309 struct c2_tx_desc *tx_desc = elem->ht_desc;
311 tx_desc->len = 0;
313 pci_unmap_single(c2dev->pcidev, elem->mapaddr, elem->maplen,
314 PCI_DMA_TODEVICE);
316 if (elem->skb) {
317 dev_kfree_skb_any(elem->skb);
318 elem->skb = NULL;
321 return 0;
324 /* Free all buffers in TX ring, assumes transmitter stopped */
325 static void c2_tx_clean(struct c2_port *c2_port)
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;
333 spin_lock_irqsave(&c2_port->tx_lock, flags);
335 elem = tx_ring->start;
337 do {
338 retry = 0;
339 do {
340 txp_htxd.flags =
341 readw(elem->hw_desc + C2_TXP_FLAGS);
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((__force u16) cpu_to_be16(TXP_HTXD_DONE),
350 elem->hw_desc + C2_TXP_FLAGS);
351 c2_port->netdev->stats.tx_dropped++;
352 break;
353 } else {
354 __raw_writew(0,
355 elem->hw_desc + C2_TXP_LEN);
356 __raw_writeq((__force u64) cpu_to_be64(0x1122334455667788ULL),
357 elem->hw_desc + C2_TXP_ADDR);
358 __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_UNINIT),
359 elem->hw_desc + C2_TXP_FLAGS);
362 c2_tx_free(c2_port->c2dev, elem);
364 } while ((elem = elem->next) != tx_ring->start);
365 } while (retry);
367 c2_port->tx_avail = c2_port->tx_ring.count - 1;
368 c2_port->c2dev->cur_tx = tx_ring->to_use - tx_ring->start;
370 if (c2_port->tx_avail > MAX_SKB_FRAGS + 1)
371 netif_wake_queue(c2_port->netdev);
373 spin_unlock_irqrestore(&c2_port->tx_lock, flags);
377 * Process transmit descriptors marked 'DONE' by the firmware,
378 * freeing up their unneeded sk_buffs.
380 static void c2_tx_interrupt(struct net_device *netdev)
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;
388 spin_lock(&c2_port->tx_lock);
390 for (elem = tx_ring->to_clean; elem != tx_ring->to_use;
391 elem = elem->next) {
392 txp_htxd.flags =
393 be16_to_cpu((__force __be16) readw(elem->hw_desc + C2_TXP_FLAGS));
395 if (txp_htxd.flags != TXP_HTXD_DONE)
396 break;
398 if (netif_msg_tx_done(c2_port)) {
399 /* PCI reads are expensive in fast path */
400 txp_htxd.len =
401 be16_to_cpu((__force __be16) 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);
408 c2_tx_free(c2dev, elem);
409 ++(c2_port->tx_avail);
412 tx_ring->to_clean = elem;
414 if (netif_queue_stopped(netdev)
415 && c2_port->tx_avail > MAX_SKB_FRAGS + 1)
416 netif_wake_queue(netdev);
418 spin_unlock(&c2_port->tx_lock);
421 static void c2_rx_error(struct c2_port *c2_port, struct c2_element *elem)
423 struct c2_rx_desc *rx_desc = elem->ht_desc;
424 struct c2_rxp_hdr *rxp_hdr = (struct c2_rxp_hdr *) elem->skb->data;
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);
441 /* Setup the skb for reuse since we're dropping this pkt */
442 elem->skb->data = elem->skb->head;
443 skb_reset_tail_pointer(elem->skb);
445 /* Zero out the rxp hdr in the sk_buff */
446 memset(elem->skb->data, 0, sizeof(*rxp_hdr));
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((__force u16) cpu_to_be16((u16) elem->maplen - sizeof(*rxp_hdr)),
452 elem->hw_desc + C2_RXP_LEN);
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);
458 pr_debug("packet dropped\n");
459 c2_port->netdev->stats.rx_dropped++;
462 static void c2_rx_interrupt(struct net_device *netdev)
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;
475 spin_lock_irqsave(&c2dev->lock, flags);
477 /* Begin where we left off */
478 rx_ring->to_clean = rx_ring->start + c2dev->cur_rx;
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;
488 if (rxp_hdr->flags != RXP_HRXD_DONE)
489 break;
490 buflen = rxp_hdr->len;
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;
500 * Allocate and map a new skb for replenishing the host
501 * RX desc
503 if (c2_rx_alloc(c2_port, elem)) {
504 c2_rx_error(c2_port, elem);
505 continue;
508 /* Unmap the old skb */
509 pci_unmap_single(c2dev->pcidev, mapaddr, maplen,
510 PCI_DMA_FROMDEVICE);
512 prefetch(skb->data);
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.
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)".
526 skb->data += sizeof(*rxp_hdr);
527 skb_set_tail_pointer(skb, buflen);
528 skb->len = buflen;
529 skb->protocol = eth_type_trans(skb, netdev);
531 netif_rx(skb);
533 netdev->last_rx = jiffies;
534 netdev->stats.rx_packets++;
535 netdev->stats.rx_bytes += buflen;
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);
543 spin_unlock_irqrestore(&c2dev->lock, flags);
547 * Handle netisr0 TX & RX interrupts.
549 static irqreturn_t c2_interrupt(int irq, void *dev_id)
551 unsigned int netisr0, dmaisr;
552 int handled = 0;
553 struct c2_dev *c2dev = (struct c2_dev *) dev_id;
555 /* Process CCILNET interrupts */
556 netisr0 = readl(c2dev->regs + C2_NISR0);
557 if (netisr0) {
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.
564 c2_rx_interrupt(c2dev->netdev);
565 c2_tx_interrupt(c2dev->netdev);
567 /* Clear the interrupt */
568 writel(netisr0, c2dev->regs + C2_NISR0);
569 handled++;
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++;
580 if (handled) {
581 return IRQ_HANDLED;
582 } else {
583 return IRQ_NONE;
587 static int c2_up(struct net_device *netdev)
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;
598 if (netif_msg_ifup(c2_port))
599 pr_debug("%s: enabling interface\n", netdev->name);
601 /* Set the Rx buffer size based on MTU */
602 c2_set_rxbufsize(c2_port);
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);
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;
617 memset(c2_port->mem, 0, c2_port->mem_size);
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;
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;
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;
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;
646 /* missing: Initialize MAC */
648 BUG_ON(c2_port->tx_ring.to_use != c2_port->tx_ring.to_clean);
650 /* Reset the adapter, ensures the driver is in sync with the RXP */
651 c2_reset(c2_port);
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;
658 __raw_writew((__force u16) cpu_to_be16(RXP_HRXD_READY),
659 elem->hw_desc + C2_RXP_FLAGS);
662 /* Enable network packets */
663 netif_start_queue(netdev);
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);
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.
676 in_dev = in_dev_get(netdev);
677 IN_DEV_CONF_SET(in_dev, ARP_IGNORE, 1);
678 in_dev_put(in_dev);
680 return 0;
682 bail1:
683 c2_rx_clean(c2_port);
684 kfree(c2_port->rx_ring.start);
686 bail0:
687 pci_free_consistent(c2dev->pcidev, c2_port->mem_size, c2_port->mem,
688 c2_port->dma);
690 return ret;
693 static int c2_down(struct net_device *netdev)
695 struct c2_port *c2_port = netdev_priv(netdev);
696 struct c2_dev *c2dev = c2_port->c2dev;
698 if (netif_msg_ifdown(c2_port))
699 pr_debug("%s: disabling interface\n",
700 netdev->name);
702 /* Wait for all the queued packets to get sent */
703 c2_tx_interrupt(netdev);
705 /* Disable network packets */
706 netif_stop_queue(netdev);
708 /* Disable IRQs by clearing the interrupt mask */
709 writel(1, c2dev->regs + C2_IDIS);
710 writel(0, c2dev->regs + C2_NIMR0);
712 /* missing: Stop transmitter */
714 /* missing: Stop receiver */
716 /* Reset the adapter, ensures the driver is in sync with the RXP */
717 c2_reset(c2_port);
719 /* missing: Turn off LEDs here */
721 /* Free all buffers in the host descriptor rings */
722 c2_tx_clean(c2_port);
723 c2_rx_clean(c2_port);
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);
731 return 0;
734 static void c2_reset(struct c2_port *c2_port)
736 struct c2_dev *c2dev = c2_port->c2dev;
737 unsigned int cur_rx = c2dev->cur_rx;
739 /* Tell the hardware to quiesce */
740 C2_SET_CUR_RX(c2dev, cur_rx | C2_PCI_HRX_QUI);
743 * The hardware will reset the C2_PCI_HRX_QUI bit once
744 * the RXP is quiesced. Wait 2 seconds for this.
746 ssleep(2);
748 cur_rx = C2_GET_CUR_RX(c2dev);
750 if (cur_rx & C2_PCI_HRX_QUI)
751 pr_debug("c2_reset: failed to quiesce the hardware!\n");
753 cur_rx &= ~C2_PCI_HRX_QUI;
755 c2dev->cur_rx = cur_rx;
757 pr_debug("Current RX: %u\n", c2dev->cur_rx);
760 static int c2_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
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;
771 spin_lock_irqsave(&c2_port->tx_lock, flags);
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);
777 pr_debug("%s: Tx ring full when queue awake!\n",
778 netdev->name);
779 return NETDEV_TX_BUSY;
782 maplen = skb_headlen(skb);
783 mapaddr =
784 pci_map_single(c2dev->pcidev, skb->data, maplen, PCI_DMA_TODEVICE);
786 elem = tx_ring->to_use;
787 elem->skb = skb;
788 elem->mapaddr = mapaddr;
789 elem->maplen = maplen;
791 /* Tell HW to xmit */
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);
799 netdev->stats.tx_packets++;
800 netdev->stats.tx_bytes += maplen;
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);
812 elem = elem->next;
813 elem->skb = NULL;
814 elem->mapaddr = mapaddr;
815 elem->maplen = maplen;
817 /* Tell HW to xmit */
818 __raw_writeq((__force u64) cpu_to_be64(mapaddr),
819 elem->hw_desc + C2_TXP_ADDR);
820 __raw_writew((__force u16) cpu_to_be16(maplen),
821 elem->hw_desc + C2_TXP_LEN);
822 __raw_writew((__force u16) cpu_to_be16(TXP_HTXD_READY),
823 elem->hw_desc + C2_TXP_FLAGS);
825 netdev->stats.tx_packets++;
826 netdev->stats.tx_bytes += maplen;
830 tx_ring->to_use = elem->next;
831 c2_port->tx_avail -= (skb_shinfo(skb)->nr_frags + 1);
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);
840 spin_unlock_irqrestore(&c2_port->tx_lock, flags);
842 netdev->trans_start = jiffies;
844 return NETDEV_TX_OK;
847 static void c2_tx_timeout(struct net_device *netdev)
849 struct c2_port *c2_port = netdev_priv(netdev);
851 if (netif_msg_timer(c2_port))
852 pr_debug("%s: tx timeout\n", netdev->name);
854 c2_tx_clean(c2_port);
857 static int c2_change_mtu(struct net_device *netdev, int new_mtu)
859 int ret = 0;
861 if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
862 return -EINVAL;
864 netdev->mtu = new_mtu;
866 if (netif_running(netdev)) {
867 c2_down(netdev);
869 c2_up(netdev);
872 return ret;
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,
885 /* Initialize network device */
886 static struct net_device *c2_devinit(struct c2_dev *c2dev,
887 void __iomem * mmio_addr)
889 struct c2_port *c2_port = NULL;
890 struct net_device *netdev = alloc_etherdev(sizeof(*c2_port));
892 if (!netdev) {
893 pr_debug("c2_port etherdev alloc failed");
894 return NULL;
897 SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev);
899 netdev->netdev_ops = &c2_netdev;
900 netdev->watchdog_timeo = C2_TX_TIMEOUT;
901 netdev->irq = c2dev->pcidev->irq;
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;
910 spin_lock_init(&c2_port->tx_lock);
912 /* Copy our 48-bit ethernet hardware address */
913 memcpy_fromio(netdev->dev_addr, mmio_addr + C2_REGS_ENADDR, 6);
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;
923 c2dev->netdev = netdev;
925 return netdev;
928 static int __devinit c2_probe(struct pci_dev *pcidev,
929 const struct pci_device_id *ent)
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;
940 printk(KERN_INFO PFX "AMSO1100 Gigabit Ethernet driver v%s loaded\n",
941 DRV_VERSION);
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;
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);
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);
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);
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);
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;
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;
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;
991 if ((sizeof(dma_addr_t) > 4)) {
992 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
993 if (ret < 0) {
994 printk(KERN_ERR PFX "64b DMA configuration failed\n");
995 goto bail2;
997 } else {
998 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
999 if (ret < 0) {
1000 printk(KERN_ERR PFX "32b DMA configuration failed\n");
1001 goto bail2;
1005 /* Enables bus-mastering on the device */
1006 pci_set_master(pcidev);
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));
1011 if (!mmio_regs) {
1012 printk(KERN_ERR PFX
1013 "Unable to remap adapter PCI registers in BAR4\n");
1014 ret = -EIO;
1015 goto bail2;
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;
1034 /* Validate the adapter version */
1035 if (be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_VERS)) != C2_VERSION) {
1036 printk(KERN_ERR PFX "Version mismatch "
1037 "[fw=%u, c2=%u], Adapter not claimed\n",
1038 be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_VERS)),
1039 C2_VERSION);
1040 ret = -EINVAL;
1041 iounmap(mmio_regs);
1042 goto bail2;
1045 /* Validate the adapter IVN */
1046 if (be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_IVN)) != C2_IVN) {
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",
1050 be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_IVN)),
1051 C2_IVN);
1052 ret = -EINVAL;
1053 iounmap(mmio_regs);
1054 goto bail2;
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;
1067 memset(c2dev, 0, sizeof(*c2dev));
1068 spin_lock_init(&c2dev->lock);
1069 c2dev->pcidev = pcidev;
1070 c2dev->cur_tx = 0;
1072 /* Get the last RX index */
1073 c2dev->cur_rx =
1074 (be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_HRX_CUR)) -
1075 0xffffc000) / sizeof(struct c2_rxp_desc);
1077 /* Request an interrupt line for the driver */
1078 ret = request_irq(pcidev->irq, c2_interrupt, IRQF_SHARED, DRV_NAME, c2dev);
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;
1086 /* Set driver specific data */
1087 pci_set_drvdata(pcidev, c2dev);
1089 /* Initialize network device */
1090 if ((netdev = c2_devinit(c2dev, mmio_regs)) == NULL) {
1091 iounmap(mmio_regs);
1092 goto bail4;
1095 /* Save off the actual size prior to unmapping mmio_regs */
1096 kva_map_size = be32_to_cpu((__force __be32) readl(mmio_regs + C2_REGS_PCI_WINSIZE));
1098 /* Unmap the adapter PCI registers in BAR4 */
1099 iounmap(mmio_regs);
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;
1109 /* Disable network packets */
1110 netif_stop_queue(netdev);
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);
1115 if (!c2dev->mmio_rxp_ring) {
1116 printk(KERN_ERR PFX "Unable to remap MMIO HRXDQ region\n");
1117 ret = -EIO;
1118 goto bail6;
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);
1124 if (!c2dev->mmio_txp_ring) {
1125 printk(KERN_ERR PFX "Unable to remap MMIO HTXDQ region\n");
1126 ret = -EIO;
1127 goto bail7;
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);
1133 /* Remap the PCI registers in adapter BAR0 to kernel VA space */
1134 c2dev->regs = ioremap_nocache(reg0_start, reg0_len);
1135 if (!c2dev->regs) {
1136 printk(KERN_ERR PFX "Unable to remap BAR0\n");
1137 ret = -EIO;
1138 goto bail8;
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);
1145 if (!c2dev->kva) {
1146 printk(KERN_ERR PFX "Unable to remap BAR4\n");
1147 ret = -EIO;
1148 goto bail9;
1151 /* Print out the MAC address */
1152 c2_print_macaddr(netdev);
1154 ret = c2_rnic_init(c2dev);
1155 if (ret) {
1156 printk(KERN_ERR PFX "c2_rnic_init failed: %d\n", ret);
1157 goto bail10;
1160 if (c2_register_device(c2dev))
1161 goto bail10;
1163 return 0;
1165 bail10:
1166 iounmap(c2dev->kva);
1168 bail9:
1169 iounmap(c2dev->regs);
1171 bail8:
1172 iounmap(c2dev->mmio_txp_ring);
1174 bail7:
1175 iounmap(c2dev->mmio_rxp_ring);
1177 bail6:
1178 unregister_netdev(netdev);
1180 bail5:
1181 free_netdev(netdev);
1183 bail4:
1184 free_irq(pcidev->irq, c2dev);
1186 bail3:
1187 ib_dealloc_device(&c2dev->ibdev);
1189 bail2:
1190 pci_release_regions(pcidev);
1192 bail1:
1193 pci_disable_device(pcidev);
1195 bail0:
1196 return ret;
1199 static void __devexit c2_remove(struct pci_dev *pcidev)
1201 struct c2_dev *c2dev = pci_get_drvdata(pcidev);
1202 struct net_device *netdev = c2dev->netdev;
1204 /* Unregister with OpenIB */
1205 c2_unregister_device(c2dev);
1207 /* Clean up the RNIC resources */
1208 c2_rnic_term(c2dev);
1210 /* Remove network device from the kernel */
1211 unregister_netdev(netdev);
1213 /* Free network device */
1214 free_netdev(netdev);
1216 /* Free the interrupt line */
1217 free_irq(pcidev->irq, c2dev);
1219 /* missing: Turn LEDs off here */
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);
1227 /* Free the hardware structure */
1228 ib_dealloc_device(&c2dev->ibdev);
1230 /* Release reserved PCI I/O and memory resources */
1231 pci_release_regions(pcidev);
1233 /* Disable PCI device */
1234 pci_disable_device(pcidev);
1236 /* Clear driver specific data */
1237 pci_set_drvdata(pcidev, NULL);
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),
1247 static int __init c2_init_module(void)
1249 return pci_register_driver(&c2_pci_driver);
1252 static void __exit c2_exit_module(void)
1254 pci_unregister_driver(&c2_pci_driver);
1257 module_init(c2_init_module);
1258 module_exit(c2_exit_module);