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
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
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>
46 #include <linux/tcp.h>
47 #include <linux/init.h>
48 #include <linux/dma-mapping.h>
52 #include <asm/byteorder.h>
54 #include <rdma/ib_smi.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, struct pt_regs *regs);
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);
81 static struct pci_device_id c2_pci_table[] = {
82 { PCI_DEVICE(0x18b8, 0xb001) },
86 MODULE_DEVICE_TABLE(pci, c2_pci_table);
88 static void c2_print_macaddr(struct net_device *netdev)
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],
97 static void c2_set_rxbufsize(struct c2_port *c2_port)
99 struct net_device *netdev = c2_port->netdev;
101 if (netdev->mtu > RX_BUF_SIZE)
102 c2_port->rx_buf_size =
103 netdev->mtu + ETH_HLEN + sizeof(struct c2_rxp_hdr) +
106 c2_port->rx_buf_size = sizeof(struct c2_rxp_hdr) + RX_BUF_SIZE;
110 * Allocate TX ring elements and chain them together.
111 * One-to-one association of adapter descriptors with ring elements.
113 static int c2_tx_ring_alloc(struct c2_ring *tx_ring, void *vaddr,
114 dma_addr_t base, void __iomem * mmio_txp_ring)
116 struct c2_tx_desc *tx_desc;
117 struct c2_txp_desc __iomem *txp_desc;
118 struct c2_element *elem;
121 tx_ring->start = kmalloc(sizeof(*elem) * tx_ring->count, GFP_KERNEL);
125 elem = tx_ring->start;
127 txp_desc = mmio_txp_ring;
128 for (i = 0; i < tx_ring->count; i++, elem++, tx_desc++, txp_desc++) {
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);
140 elem->ht_desc = tx_desc;
141 elem->hw_desc = txp_desc;
143 if (i == tx_ring->count - 1) {
144 elem->next = tx_ring->start;
145 tx_desc->next_offset = base;
147 elem->next = elem + 1;
148 tx_desc->next_offset =
149 base + (i + 1) * sizeof(*tx_desc);
153 tx_ring->to_use = tx_ring->to_clean = tx_ring->start;
159 * Allocate RX ring elements and chain them together.
160 * One-to-one association of adapter descriptors with ring elements.
162 static int c2_rx_ring_alloc(struct c2_ring *rx_ring, void *vaddr,
163 dma_addr_t base, void __iomem * mmio_rxp_ring)
165 struct c2_rx_desc *rx_desc;
166 struct c2_rxp_desc __iomem *rxp_desc;
167 struct c2_element *elem;
170 rx_ring->start = kmalloc(sizeof(*elem) * rx_ring->count, GFP_KERNEL);
174 elem = rx_ring->start;
176 rxp_desc = mmio_rxp_ring;
177 for (i = 0; i < rx_ring->count; i++, elem++, rx_desc++, rxp_desc++) {
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);
192 elem->ht_desc = rx_desc;
193 elem->hw_desc = rxp_desc;
195 if (i == rx_ring->count - 1) {
196 elem->next = rx_ring->start;
197 rx_desc->next_offset = base;
199 elem->next = elem + 1;
200 rx_desc->next_offset =
201 base + (i + 1) * sizeof(*rx_desc);
205 rx_ring->to_use = rx_ring->to_clean = rx_ring->start;
210 /* Setup buffer for receiving */
211 static inline int c2_rx_alloc(struct c2_port *c2_port, struct c2_element *elem)
213 struct c2_dev *c2dev = c2_port->c2dev;
214 struct c2_rx_desc *rx_desc = elem->ht_desc;
218 struct c2_rxp_hdr *rxp_hdr;
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);
227 /* Zero out the rxp hdr in the sk_buff */
228 memset(skb->data, 0, sizeof(*rxp_hdr));
230 skb->dev = c2_port->netdev;
232 maplen = c2_port->rx_buf_size;
234 pci_map_single(c2dev->pcidev, skb->data, maplen,
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;
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);
248 elem->mapaddr = mapaddr;
249 elem->maplen = maplen;
250 rx_desc->len = maplen;
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;
265 elem = rx_ring->start;
267 if (c2_rx_alloc(c2_port, elem)) {
271 } while ((elem = elem->next) != rx_ring->start);
273 rx_ring->to_clean = rx_ring->start;
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;
287 rx_desc = elem->ht_desc;
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);
299 pci_unmap_single(c2dev->pcidev, elem->mapaddr,
300 elem->maplen, PCI_DMA_FROMDEVICE);
301 dev_kfree_skb(elem->skb);
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;
313 pci_unmap_single(c2dev->pcidev, elem->mapaddr, elem->maplen,
317 dev_kfree_skb_any(elem->skb);
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;
333 spin_lock_irqsave(&c2_port->tx_lock, flags);
335 elem = tx_ring->start;
341 readw(elem->hw_desc + C2_TXP_FLAGS);
343 if (txp_htxd.flags == TXP_HTXD_READY) {
346 elem->hw_desc + C2_TXP_LEN);
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++;
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);
362 c2_tx_free(c2_port->c2dev, elem);
364 } while ((elem = elem->next) != tx_ring->start);
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;
393 be16_to_cpu(readw(elem->hw_desc + C2_TXP_FLAGS));
395 if (txp_htxd.flags != TXP_HTXD_DONE)
398 if (netif_msg_tx_done(c2_port)) {
399 /* PCI reads are expensive in fast path */
401 be16_to_cpu(readw(elem->hw_desc + C2_TXP_LEN));
402 pr_debug("%s: tx done slot %3Zu status 0x%x len "
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->tail = elem->skb->data = elem->skb->head;
444 /* Zero out the rxp hdr in the sk_buff */
445 memset(elem->skb->data, 0, sizeof(*rxp_hdr));
447 /* Write the descriptor to the adapter's rx ring */
448 __raw_writew(0, elem->hw_desc + C2_RXP_STATUS);
449 __raw_writew(0, elem->hw_desc + C2_RXP_COUNT);
450 __raw_writew(cpu_to_be16((u16) elem->maplen - sizeof(*rxp_hdr)),
451 elem->hw_desc + C2_RXP_LEN);
452 __raw_writeq(cpu_to_be64(elem->mapaddr), elem->hw_desc + C2_RXP_ADDR);
453 __raw_writew(cpu_to_be16(RXP_HRXD_READY), elem->hw_desc + C2_RXP_FLAGS);
455 pr_debug("packet dropped\n");
456 c2_port->netstats.rx_dropped++;
459 static void c2_rx_interrupt(struct net_device *netdev)
461 struct c2_port *c2_port = netdev_priv(netdev);
462 struct c2_dev *c2dev = c2_port->c2dev;
463 struct c2_ring *rx_ring = &c2_port->rx_ring;
464 struct c2_element *elem;
465 struct c2_rx_desc *rx_desc;
466 struct c2_rxp_hdr *rxp_hdr;
472 spin_lock_irqsave(&c2dev->lock, flags);
474 /* Begin where we left off */
475 rx_ring->to_clean = rx_ring->start + c2dev->cur_rx;
477 for (elem = rx_ring->to_clean; elem->next != rx_ring->to_clean;
479 rx_desc = elem->ht_desc;
480 mapaddr = elem->mapaddr;
481 maplen = elem->maplen;
483 rxp_hdr = (struct c2_rxp_hdr *) skb->data;
485 if (rxp_hdr->flags != RXP_HRXD_DONE)
487 buflen = rxp_hdr->len;
489 /* Sanity check the RXP header */
490 if (rxp_hdr->status != RXP_HRXD_OK ||
491 buflen > (rx_desc->len - sizeof(*rxp_hdr))) {
492 c2_rx_error(c2_port, elem);
497 * Allocate and map a new skb for replenishing the host
500 if (c2_rx_alloc(c2_port, elem)) {
501 c2_rx_error(c2_port, elem);
505 /* Unmap the old skb */
506 pci_unmap_single(c2dev->pcidev, mapaddr, maplen,
512 * Skip past the leading 8 bytes comprising of the
513 * "struct c2_rxp_hdr", prepended by the adapter
514 * to the usual Ethernet header ("struct ethhdr"),
515 * to the start of the raw Ethernet packet.
517 * Fix up the various fields in the sk_buff before
518 * passing it up to netif_rx(). The transfer size
519 * (in bytes) specified by the adapter len field of
520 * the "struct rxp_hdr_t" does NOT include the
521 * "sizeof(struct c2_rxp_hdr)".
523 skb->data += sizeof(*rxp_hdr);
524 skb->tail = skb->data + buflen;
527 skb->protocol = eth_type_trans(skb, netdev);
531 netdev->last_rx = jiffies;
532 c2_port->netstats.rx_packets++;
533 c2_port->netstats.rx_bytes += buflen;
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);
541 spin_unlock_irqrestore(&c2dev->lock, flags);
545 * Handle netisr0 TX & RX interrupts.
547 static irqreturn_t c2_interrupt(int irq, void *dev_id, struct pt_regs *regs)
549 unsigned int netisr0, dmaisr;
551 struct c2_dev *c2dev = (struct c2_dev *) dev_id;
553 /* Process CCILNET interrupts */
554 netisr0 = readl(c2dev->regs + C2_NISR0);
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.
562 c2_rx_interrupt(c2dev->netdev);
563 c2_tx_interrupt(c2dev->netdev);
565 /* Clear the interrupt */
566 writel(netisr0, c2dev->regs + C2_NISR0);
570 /* Process RNIC interrupts */
571 dmaisr = readl(c2dev->regs + C2_DISR);
573 writel(dmaisr, c2dev->regs + C2_DISR);
574 c2_rnic_interrupt(c2dev);
585 static int c2_up(struct net_device *netdev)
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;
594 unsigned int netimr0;
596 if (netif_msg_ifup(c2_port))
597 pr_debug("%s: enabling interface\n", netdev->name);
599 /* Set the Rx buffer size based on MTU */
600 c2_set_rxbufsize(c2_port);
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);
606 c2_port->mem_size = tx_size + rx_size;
607 c2_port->mem = pci_alloc_consistent(c2dev->pcidev, c2_port->mem_size,
609 if (c2_port->mem == NULL) {
610 pr_debug("Unable to allocate memory for "
611 "host descriptor rings\n");
615 memset(c2_port->mem, 0, c2_port->mem_size);
617 /* Create the Rx host descriptor ring */
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");
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");
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");
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;
644 /* missing: Initialize MAC */
646 BUG_ON(c2_port->tx_ring.to_use != c2_port->tx_ring.to_clean);
648 /* Reset the adapter, ensures the driver is in sync with the RXP */
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;
654 rxp_hdr = (struct c2_rxp_hdr *) elem->skb->data;
656 __raw_writew(cpu_to_be16(RXP_HRXD_READY),
657 elem->hw_desc + C2_RXP_FLAGS);
660 /* Enable network packets */
661 netif_start_queue(netdev);
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);
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
674 in_dev = in_dev_get(netdev);
675 in_dev->cnf.arp_ignore = 1;
681 c2_rx_clean(c2_port);
682 kfree(c2_port->rx_ring.start);
685 pci_free_consistent(c2dev->pcidev, c2_port->mem_size, c2_port->mem,
691 static int c2_down(struct net_device *netdev)
693 struct c2_port *c2_port = netdev_priv(netdev);
694 struct c2_dev *c2dev = c2_port->c2dev;
696 if (netif_msg_ifdown(c2_port))
697 pr_debug("%s: disabling interface\n",
700 /* Wait for all the queued packets to get sent */
701 c2_tx_interrupt(netdev);
703 /* Disable network packets */
704 netif_stop_queue(netdev);
706 /* Disable IRQs by clearing the interrupt mask */
707 writel(1, c2dev->regs + C2_IDIS);
708 writel(0, c2dev->regs + C2_NIMR0);
710 /* missing: Stop transmitter */
712 /* missing: Stop receiver */
714 /* Reset the adapter, ensures the driver is in sync with the RXP */
717 /* missing: Turn off LEDs here */
719 /* Free all buffers in the host descriptor rings */
720 c2_tx_clean(c2_port);
721 c2_rx_clean(c2_port);
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,
732 static void c2_reset(struct c2_port *c2_port)
734 struct c2_dev *c2dev = c2_port->c2dev;
735 unsigned int cur_rx = c2dev->cur_rx;
737 /* Tell the hardware to quiesce */
738 C2_SET_CUR_RX(c2dev, cur_rx | C2_PCI_HRX_QUI);
741 * The hardware will reset the C2_PCI_HRX_QUI bit once
742 * the RXP is quiesced. Wait 2 seconds for this.
746 cur_rx = C2_GET_CUR_RX(c2dev);
748 if (cur_rx & C2_PCI_HRX_QUI)
749 pr_debug("c2_reset: failed to quiesce the hardware!\n");
751 cur_rx &= ~C2_PCI_HRX_QUI;
753 c2dev->cur_rx = cur_rx;
755 pr_debug("Current RX: %u\n", c2dev->cur_rx);
758 static int c2_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
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;
769 spin_lock_irqsave(&c2_port->tx_lock, flags);
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);
775 pr_debug("%s: Tx ring full when queue awake!\n",
777 return NETDEV_TX_BUSY;
780 maplen = skb_headlen(skb);
782 pci_map_single(c2dev->pcidev, skb->data, maplen, PCI_DMA_TODEVICE);
784 elem = tx_ring->to_use;
786 elem->mapaddr = mapaddr;
787 elem->maplen = maplen;
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);
794 c2_port->netstats.tx_packets++;
795 c2_port->netstats.tx_bytes += maplen;
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];
803 pci_map_page(c2dev->pcidev, frag->page,
804 frag->page_offset, maplen,
809 elem->mapaddr = mapaddr;
810 elem->maplen = maplen;
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);
820 c2_port->netstats.tx_packets++;
821 c2_port->netstats.tx_bytes += maplen;
825 tx_ring->to_use = elem->next;
826 c2_port->tx_avail -= (skb_shinfo(skb)->nr_frags + 1);
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",
835 spin_unlock_irqrestore(&c2_port->tx_lock, flags);
837 netdev->trans_start = jiffies;
842 static struct net_device_stats *c2_get_stats(struct net_device *netdev)
844 struct c2_port *c2_port = netdev_priv(netdev);
846 return &c2_port->netstats;
849 static void c2_tx_timeout(struct net_device *netdev)
851 struct c2_port *c2_port = netdev_priv(netdev);
853 if (netif_msg_timer(c2_port))
854 pr_debug("%s: tx timeout\n", netdev->name);
856 c2_tx_clean(c2_port);
859 static int c2_change_mtu(struct net_device *netdev, int new_mtu)
863 if (new_mtu < ETH_ZLEN || new_mtu > ETH_JUMBO_MTU)
866 netdev->mtu = new_mtu;
868 if (netif_running(netdev)) {
877 /* Initialize network device */
878 static struct net_device *c2_devinit(struct c2_dev *c2dev,
879 void __iomem * mmio_addr)
881 struct c2_port *c2_port = NULL;
882 struct net_device *netdev = alloc_etherdev(sizeof(*c2_port));
885 pr_debug("c2_port etherdev alloc failed");
889 SET_MODULE_OWNER(netdev);
890 SET_NETDEV_DEV(netdev, &c2dev->pcidev->dev);
892 netdev->open = c2_up;
893 netdev->stop = c2_down;
894 netdev->hard_start_xmit = c2_xmit_frame;
895 netdev->get_stats = c2_get_stats;
896 netdev->tx_timeout = c2_tx_timeout;
897 netdev->change_mtu = c2_change_mtu;
898 netdev->watchdog_timeo = C2_TX_TIMEOUT;
899 netdev->irq = c2dev->pcidev->irq;
901 c2_port = netdev_priv(netdev);
902 c2_port->netdev = netdev;
903 c2_port->c2dev = c2dev;
904 c2_port->msg_enable = netif_msg_init(debug, default_msg);
905 c2_port->tx_ring.count = C2_NUM_TX_DESC;
906 c2_port->rx_ring.count = C2_NUM_RX_DESC;
908 spin_lock_init(&c2_port->tx_lock);
910 /* Copy our 48-bit ethernet hardware address */
911 memcpy_fromio(netdev->dev_addr, mmio_addr + C2_REGS_ENADDR, 6);
913 /* Validate the MAC address */
914 if (!is_valid_ether_addr(netdev->dev_addr)) {
915 pr_debug("Invalid MAC Address\n");
916 c2_print_macaddr(netdev);
921 c2dev->netdev = netdev;
926 static int __devinit c2_probe(struct pci_dev *pcidev,
927 const struct pci_device_id *ent)
930 unsigned long reg0_start, reg0_flags, reg0_len;
931 unsigned long reg2_start, reg2_flags, reg2_len;
932 unsigned long reg4_start, reg4_flags, reg4_len;
933 unsigned kva_map_size;
934 struct net_device *netdev = NULL;
935 struct c2_dev *c2dev = NULL;
936 void __iomem *mmio_regs = NULL;
938 printk(KERN_INFO PFX "AMSO1100 Gigabit Ethernet driver v%s loaded\n",
941 /* Enable PCI device */
942 ret = pci_enable_device(pcidev);
944 printk(KERN_ERR PFX "%s: Unable to enable PCI device\n",
949 reg0_start = pci_resource_start(pcidev, BAR_0);
950 reg0_len = pci_resource_len(pcidev, BAR_0);
951 reg0_flags = pci_resource_flags(pcidev, BAR_0);
953 reg2_start = pci_resource_start(pcidev, BAR_2);
954 reg2_len = pci_resource_len(pcidev, BAR_2);
955 reg2_flags = pci_resource_flags(pcidev, BAR_2);
957 reg4_start = pci_resource_start(pcidev, BAR_4);
958 reg4_len = pci_resource_len(pcidev, BAR_4);
959 reg4_flags = pci_resource_flags(pcidev, BAR_4);
961 pr_debug("BAR0 size = 0x%lX bytes\n", reg0_len);
962 pr_debug("BAR2 size = 0x%lX bytes\n", reg2_len);
963 pr_debug("BAR4 size = 0x%lX bytes\n", reg4_len);
965 /* Make sure PCI base addr are MMIO */
966 if (!(reg0_flags & IORESOURCE_MEM) ||
967 !(reg2_flags & IORESOURCE_MEM) || !(reg4_flags & IORESOURCE_MEM)) {
968 printk(KERN_ERR PFX "PCI regions not an MMIO resource\n");
973 /* Check for weird/broken PCI region reporting */
974 if ((reg0_len < C2_REG0_SIZE) ||
975 (reg2_len < C2_REG2_SIZE) || (reg4_len < C2_REG4_SIZE)) {
976 printk(KERN_ERR PFX "Invalid PCI region sizes\n");
981 /* Reserve PCI I/O and memory resources */
982 ret = pci_request_regions(pcidev, DRV_NAME);
984 printk(KERN_ERR PFX "%s: Unable to request regions\n",
989 if ((sizeof(dma_addr_t) > 4)) {
990 ret = pci_set_dma_mask(pcidev, DMA_64BIT_MASK);
992 printk(KERN_ERR PFX "64b DMA configuration failed\n");
996 ret = pci_set_dma_mask(pcidev, DMA_32BIT_MASK);
998 printk(KERN_ERR PFX "32b DMA configuration failed\n");
1003 /* Enables bus-mastering on the device */
1004 pci_set_master(pcidev);
1006 /* Remap the adapter PCI registers in BAR4 */
1007 mmio_regs = ioremap_nocache(reg4_start + C2_PCI_REGS_OFFSET,
1008 sizeof(struct c2_adapter_pci_regs));
1009 if (mmio_regs == 0UL) {
1011 "Unable to remap adapter PCI registers in BAR4\n");
1016 /* Validate PCI regs magic */
1017 for (i = 0; i < sizeof(c2_magic); i++) {
1018 if (c2_magic[i] != readb(mmio_regs + C2_REGS_MAGIC + i)) {
1019 printk(KERN_ERR PFX "Downlevel Firmware boot loader "
1020 "[%d/%Zd: got 0x%x, exp 0x%x]. Use the cc_flash "
1021 "utility to update your boot loader\n",
1022 i + 1, sizeof(c2_magic),
1023 readb(mmio_regs + C2_REGS_MAGIC + i),
1025 printk(KERN_ERR PFX "Adapter not claimed\n");
1032 /* Validate the adapter version */
1033 if (be32_to_cpu(readl(mmio_regs + C2_REGS_VERS)) != C2_VERSION) {
1034 printk(KERN_ERR PFX "Version mismatch "
1035 "[fw=%u, c2=%u], Adapter not claimed\n",
1036 be32_to_cpu(readl(mmio_regs + C2_REGS_VERS)),
1043 /* Validate the adapter IVN */
1044 if (be32_to_cpu(readl(mmio_regs + C2_REGS_IVN)) != C2_IVN) {
1045 printk(KERN_ERR PFX "Downlevel FIrmware level. You should be using "
1046 "the OpenIB device support kit. "
1047 "[fw=0x%x, c2=0x%x], Adapter not claimed\n",
1048 be32_to_cpu(readl(mmio_regs + C2_REGS_IVN)),
1055 /* Allocate hardware structure */
1056 c2dev = (struct c2_dev *) ib_alloc_device(sizeof(*c2dev));
1058 printk(KERN_ERR PFX "%s: Unable to alloc hardware struct\n",
1065 memset(c2dev, 0, sizeof(*c2dev));
1066 spin_lock_init(&c2dev->lock);
1067 c2dev->pcidev = pcidev;
1070 /* Get the last RX index */
1072 (be32_to_cpu(readl(mmio_regs + C2_REGS_HRX_CUR)) -
1073 0xffffc000) / sizeof(struct c2_rxp_desc);
1075 /* Request an interrupt line for the driver */
1076 ret = request_irq(pcidev->irq, c2_interrupt, SA_SHIRQ, DRV_NAME, c2dev);
1078 printk(KERN_ERR PFX "%s: requested IRQ %u is busy\n",
1079 pci_name(pcidev), pcidev->irq);
1084 /* Set driver specific data */
1085 pci_set_drvdata(pcidev, c2dev);
1087 /* Initialize network device */
1088 if ((netdev = c2_devinit(c2dev, mmio_regs)) == NULL) {
1093 /* Save off the actual size prior to unmapping mmio_regs */
1094 kva_map_size = be32_to_cpu(readl(mmio_regs + C2_REGS_PCI_WINSIZE));
1096 /* Unmap the adapter PCI registers in BAR4 */
1099 /* Register network device */
1100 ret = register_netdev(netdev);
1102 printk(KERN_ERR PFX "Unable to register netdev, ret = %d\n",
1107 /* Disable network packets */
1108 netif_stop_queue(netdev);
1110 /* Remap the adapter HRXDQ PA space to kernel VA space */
1111 c2dev->mmio_rxp_ring = ioremap_nocache(reg4_start + C2_RXP_HRXDQ_OFFSET,
1113 if (c2dev->mmio_rxp_ring == 0UL) {
1114 printk(KERN_ERR PFX "Unable to remap MMIO HRXDQ region\n");
1119 /* Remap the adapter HTXDQ PA space to kernel VA space */
1120 c2dev->mmio_txp_ring = ioremap_nocache(reg4_start + C2_TXP_HTXDQ_OFFSET,
1122 if (c2dev->mmio_txp_ring == 0UL) {
1123 printk(KERN_ERR PFX "Unable to remap MMIO HTXDQ region\n");
1128 /* Save off the current RX index in the last 4 bytes of the TXP Ring */
1129 C2_SET_CUR_RX(c2dev, c2dev->cur_rx);
1131 /* Remap the PCI registers in adapter BAR0 to kernel VA space */
1132 c2dev->regs = ioremap_nocache(reg0_start, reg0_len);
1133 if (c2dev->regs == 0UL) {
1134 printk(KERN_ERR PFX "Unable to remap BAR0\n");
1139 /* Remap the PCI registers in adapter BAR4 to kernel VA space */
1140 c2dev->pa = reg4_start + C2_PCI_REGS_OFFSET;
1141 c2dev->kva = ioremap_nocache(reg4_start + C2_PCI_REGS_OFFSET,
1143 if (c2dev->kva == 0UL) {
1144 printk(KERN_ERR PFX "Unable to remap BAR4\n");
1149 /* Print out the MAC address */
1150 c2_print_macaddr(netdev);
1152 ret = c2_rnic_init(c2dev);
1154 printk(KERN_ERR PFX "c2_rnic_init failed: %d\n", ret);
1158 c2_register_device(c2dev);
1163 iounmap(c2dev->kva);
1166 iounmap(c2dev->regs);
1169 iounmap(c2dev->mmio_txp_ring);
1172 iounmap(c2dev->mmio_rxp_ring);
1175 unregister_netdev(netdev);
1178 free_netdev(netdev);
1181 free_irq(pcidev->irq, c2dev);
1184 ib_dealloc_device(&c2dev->ibdev);
1187 pci_release_regions(pcidev);
1190 pci_disable_device(pcidev);
1196 static void __devexit c2_remove(struct pci_dev *pcidev)
1198 struct c2_dev *c2dev = pci_get_drvdata(pcidev);
1199 struct net_device *netdev = c2dev->netdev;
1201 /* Unregister with OpenIB */
1202 c2_unregister_device(c2dev);
1204 /* Clean up the RNIC resources */
1205 c2_rnic_term(c2dev);
1207 /* Remove network device from the kernel */
1208 unregister_netdev(netdev);
1210 /* Free network device */
1211 free_netdev(netdev);
1213 /* Free the interrupt line */
1214 free_irq(pcidev->irq, c2dev);
1216 /* missing: Turn LEDs off here */
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);
1224 /* Free the hardware structure */
1225 ib_dealloc_device(&c2dev->ibdev);
1227 /* Release reserved PCI I/O and memory resources */
1228 pci_release_regions(pcidev);
1230 /* Disable PCI device */
1231 pci_disable_device(pcidev);
1233 /* Clear driver specific data */
1234 pci_set_drvdata(pcidev, NULL);
1237 static struct pci_driver c2_pci_driver = {
1239 .id_table = c2_pci_table,
1241 .remove = __devexit_p(c2_remove),
1244 static int __init c2_init_module(void)
1246 return pci_module_init(&c2_pci_driver);
1249 static void __exit c2_exit_module(void)
1251 pci_unregister_driver(&c2_pci_driver);
1254 module_init(c2_init_module);
1255 module_exit(c2_exit_module);