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/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/ethtool.h>
42 #include <linux/mii.h>
43 #include <linux/if_vlan.h>
44 #include <linux/crc32.h>
47 #include <linux/tcp.h>
48 #include <linux/init.h>
49 #include <linux/dma-mapping.h>
50 #include <linux/slab.h>
51 #include <linux/prefetch.h>
55 #include <asm/byteorder.h>
57 #include <rdma/ib_smi.h>
59 #include "c2_provider.h"
61 MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>");
62 MODULE_DESCRIPTION("Ammasso AMSO1100 Low-level iWARP Driver");
63 MODULE_LICENSE("Dual BSD/GPL");
64 MODULE_VERSION(DRV_VERSION
);
66 static const u32 default_msg
= NETIF_MSG_DRV
| NETIF_MSG_PROBE
| NETIF_MSG_LINK
67 | NETIF_MSG_IFUP
| NETIF_MSG_IFDOWN
;
69 static int debug
= -1; /* defaults above */
70 module_param(debug
, int, 0);
71 MODULE_PARM_DESC(debug
, "Debug level (0=none,...,16=all)");
73 static int c2_up(struct net_device
*netdev
);
74 static int c2_down(struct net_device
*netdev
);
75 static int c2_xmit_frame(struct sk_buff
*skb
, struct net_device
*netdev
);
76 static void c2_tx_interrupt(struct net_device
*netdev
);
77 static void c2_rx_interrupt(struct net_device
*netdev
);
78 static irqreturn_t
c2_interrupt(int irq
, void *dev_id
);
79 static void c2_tx_timeout(struct net_device
*netdev
);
80 static int c2_change_mtu(struct net_device
*netdev
, int new_mtu
);
81 static void c2_reset(struct c2_port
*c2_port
);
83 static struct pci_device_id c2_pci_table
[] = {
84 { PCI_DEVICE(0x18b8, 0xb001) },
88 MODULE_DEVICE_TABLE(pci
, c2_pci_table
);
90 static void c2_print_macaddr(struct net_device
*netdev
)
92 pr_debug("%s: MAC %pM, IRQ %u\n", netdev
->name
, netdev
->dev_addr
, netdev
->irq
);
95 static void c2_set_rxbufsize(struct c2_port
*c2_port
)
97 struct net_device
*netdev
= c2_port
->netdev
;
99 if (netdev
->mtu
> RX_BUF_SIZE
)
100 c2_port
->rx_buf_size
=
101 netdev
->mtu
+ ETH_HLEN
+ sizeof(struct c2_rxp_hdr
) +
104 c2_port
->rx_buf_size
= sizeof(struct c2_rxp_hdr
) + RX_BUF_SIZE
;
108 * Allocate TX ring elements and chain them together.
109 * One-to-one association of adapter descriptors with ring elements.
111 static int c2_tx_ring_alloc(struct c2_ring
*tx_ring
, void *vaddr
,
112 dma_addr_t base
, void __iomem
* mmio_txp_ring
)
114 struct c2_tx_desc
*tx_desc
;
115 struct c2_txp_desc __iomem
*txp_desc
;
116 struct c2_element
*elem
;
119 tx_ring
->start
= kmalloc(sizeof(*elem
) * tx_ring
->count
, GFP_KERNEL
);
123 elem
= tx_ring
->start
;
125 txp_desc
= mmio_txp_ring
;
126 for (i
= 0; i
< tx_ring
->count
; i
++, elem
++, tx_desc
++, txp_desc
++) {
130 /* Set TXP_HTXD_UNINIT */
131 __raw_writeq((__force u64
) cpu_to_be64(0x1122334455667788ULL
),
132 (void __iomem
*) txp_desc
+ C2_TXP_ADDR
);
133 __raw_writew(0, (void __iomem
*) txp_desc
+ C2_TXP_LEN
);
134 __raw_writew((__force u16
) cpu_to_be16(TXP_HTXD_UNINIT
),
135 (void __iomem
*) txp_desc
+ C2_TXP_FLAGS
);
138 elem
->ht_desc
= tx_desc
;
139 elem
->hw_desc
= txp_desc
;
141 if (i
== tx_ring
->count
- 1) {
142 elem
->next
= tx_ring
->start
;
143 tx_desc
->next_offset
= base
;
145 elem
->next
= elem
+ 1;
146 tx_desc
->next_offset
=
147 base
+ (i
+ 1) * sizeof(*tx_desc
);
151 tx_ring
->to_use
= tx_ring
->to_clean
= tx_ring
->start
;
157 * Allocate RX ring elements and chain them together.
158 * One-to-one association of adapter descriptors with ring elements.
160 static int c2_rx_ring_alloc(struct c2_ring
*rx_ring
, void *vaddr
,
161 dma_addr_t base
, void __iomem
* mmio_rxp_ring
)
163 struct c2_rx_desc
*rx_desc
;
164 struct c2_rxp_desc __iomem
*rxp_desc
;
165 struct c2_element
*elem
;
168 rx_ring
->start
= kmalloc(sizeof(*elem
) * rx_ring
->count
, GFP_KERNEL
);
172 elem
= rx_ring
->start
;
174 rxp_desc
= mmio_rxp_ring
;
175 for (i
= 0; i
< rx_ring
->count
; i
++, elem
++, rx_desc
++, rxp_desc
++) {
179 /* Set RXP_HRXD_UNINIT */
180 __raw_writew((__force u16
) cpu_to_be16(RXP_HRXD_OK
),
181 (void __iomem
*) rxp_desc
+ C2_RXP_STATUS
);
182 __raw_writew(0, (void __iomem
*) rxp_desc
+ C2_RXP_COUNT
);
183 __raw_writew(0, (void __iomem
*) rxp_desc
+ C2_RXP_LEN
);
184 __raw_writeq((__force u64
) cpu_to_be64(0x99aabbccddeeffULL
),
185 (void __iomem
*) rxp_desc
+ C2_RXP_ADDR
);
186 __raw_writew((__force u16
) cpu_to_be16(RXP_HRXD_UNINIT
),
187 (void __iomem
*) rxp_desc
+ C2_RXP_FLAGS
);
190 elem
->ht_desc
= rx_desc
;
191 elem
->hw_desc
= rxp_desc
;
193 if (i
== rx_ring
->count
- 1) {
194 elem
->next
= rx_ring
->start
;
195 rx_desc
->next_offset
= base
;
197 elem
->next
= elem
+ 1;
198 rx_desc
->next_offset
=
199 base
+ (i
+ 1) * sizeof(*rx_desc
);
203 rx_ring
->to_use
= rx_ring
->to_clean
= rx_ring
->start
;
208 /* Setup buffer for receiving */
209 static inline int c2_rx_alloc(struct c2_port
*c2_port
, struct c2_element
*elem
)
211 struct c2_dev
*c2dev
= c2_port
->c2dev
;
212 struct c2_rx_desc
*rx_desc
= elem
->ht_desc
;
216 struct c2_rxp_hdr
*rxp_hdr
;
218 skb
= dev_alloc_skb(c2_port
->rx_buf_size
);
219 if (unlikely(!skb
)) {
220 pr_debug("%s: out of memory for receive\n",
221 c2_port
->netdev
->name
);
225 /* Zero out the rxp hdr in the sk_buff */
226 memset(skb
->data
, 0, sizeof(*rxp_hdr
));
228 skb
->dev
= c2_port
->netdev
;
230 maplen
= c2_port
->rx_buf_size
;
232 pci_map_single(c2dev
->pcidev
, skb
->data
, maplen
,
235 /* Set the sk_buff RXP_header to RXP_HRXD_READY */
236 rxp_hdr
= (struct c2_rxp_hdr
*) skb
->data
;
237 rxp_hdr
->flags
= RXP_HRXD_READY
;
239 __raw_writew(0, elem
->hw_desc
+ C2_RXP_STATUS
);
240 __raw_writew((__force u16
) cpu_to_be16((u16
) maplen
- sizeof(*rxp_hdr
)),
241 elem
->hw_desc
+ C2_RXP_LEN
);
242 __raw_writeq((__force u64
) cpu_to_be64(mapaddr
), elem
->hw_desc
+ C2_RXP_ADDR
);
243 __raw_writew((__force u16
) cpu_to_be16(RXP_HRXD_READY
),
244 elem
->hw_desc
+ C2_RXP_FLAGS
);
247 elem
->mapaddr
= mapaddr
;
248 elem
->maplen
= maplen
;
249 rx_desc
->len
= maplen
;
255 * Allocate buffers for the Rx ring
256 * For receive: rx_ring.to_clean is next received frame
258 static int c2_rx_fill(struct c2_port
*c2_port
)
260 struct c2_ring
*rx_ring
= &c2_port
->rx_ring
;
261 struct c2_element
*elem
;
264 elem
= rx_ring
->start
;
266 if (c2_rx_alloc(c2_port
, elem
)) {
270 } while ((elem
= elem
->next
) != rx_ring
->start
);
272 rx_ring
->to_clean
= rx_ring
->start
;
276 /* Free all buffers in RX ring, assumes receiver stopped */
277 static void c2_rx_clean(struct c2_port
*c2_port
)
279 struct c2_dev
*c2dev
= c2_port
->c2dev
;
280 struct c2_ring
*rx_ring
= &c2_port
->rx_ring
;
281 struct c2_element
*elem
;
282 struct c2_rx_desc
*rx_desc
;
284 elem
= rx_ring
->start
;
286 rx_desc
= elem
->ht_desc
;
289 __raw_writew(0, elem
->hw_desc
+ C2_RXP_STATUS
);
290 __raw_writew(0, elem
->hw_desc
+ C2_RXP_COUNT
);
291 __raw_writew(0, elem
->hw_desc
+ C2_RXP_LEN
);
292 __raw_writeq((__force u64
) cpu_to_be64(0x99aabbccddeeffULL
),
293 elem
->hw_desc
+ C2_RXP_ADDR
);
294 __raw_writew((__force u16
) cpu_to_be16(RXP_HRXD_UNINIT
),
295 elem
->hw_desc
+ C2_RXP_FLAGS
);
298 pci_unmap_single(c2dev
->pcidev
, elem
->mapaddr
,
299 elem
->maplen
, PCI_DMA_FROMDEVICE
);
300 dev_kfree_skb(elem
->skb
);
303 } while ((elem
= elem
->next
) != rx_ring
->start
);
306 static inline int c2_tx_free(struct c2_dev
*c2dev
, struct c2_element
*elem
)
308 struct c2_tx_desc
*tx_desc
= elem
->ht_desc
;
312 pci_unmap_single(c2dev
->pcidev
, elem
->mapaddr
, elem
->maplen
,
316 dev_kfree_skb_any(elem
->skb
);
323 /* Free all buffers in TX ring, assumes transmitter stopped */
324 static void c2_tx_clean(struct c2_port
*c2_port
)
326 struct c2_ring
*tx_ring
= &c2_port
->tx_ring
;
327 struct c2_element
*elem
;
328 struct c2_txp_desc txp_htxd
;
332 spin_lock_irqsave(&c2_port
->tx_lock
, flags
);
334 elem
= tx_ring
->start
;
340 readw(elem
->hw_desc
+ C2_TXP_FLAGS
);
342 if (txp_htxd
.flags
== TXP_HTXD_READY
) {
345 elem
->hw_desc
+ C2_TXP_LEN
);
347 elem
->hw_desc
+ C2_TXP_ADDR
);
348 __raw_writew((__force u16
) cpu_to_be16(TXP_HTXD_DONE
),
349 elem
->hw_desc
+ C2_TXP_FLAGS
);
350 c2_port
->netdev
->stats
.tx_dropped
++;
354 elem
->hw_desc
+ C2_TXP_LEN
);
355 __raw_writeq((__force u64
) cpu_to_be64(0x1122334455667788ULL
),
356 elem
->hw_desc
+ C2_TXP_ADDR
);
357 __raw_writew((__force u16
) cpu_to_be16(TXP_HTXD_UNINIT
),
358 elem
->hw_desc
+ C2_TXP_FLAGS
);
361 c2_tx_free(c2_port
->c2dev
, elem
);
363 } while ((elem
= elem
->next
) != tx_ring
->start
);
366 c2_port
->tx_avail
= c2_port
->tx_ring
.count
- 1;
367 c2_port
->c2dev
->cur_tx
= tx_ring
->to_use
- tx_ring
->start
;
369 if (c2_port
->tx_avail
> MAX_SKB_FRAGS
+ 1)
370 netif_wake_queue(c2_port
->netdev
);
372 spin_unlock_irqrestore(&c2_port
->tx_lock
, flags
);
376 * Process transmit descriptors marked 'DONE' by the firmware,
377 * freeing up their unneeded sk_buffs.
379 static void c2_tx_interrupt(struct net_device
*netdev
)
381 struct c2_port
*c2_port
= netdev_priv(netdev
);
382 struct c2_dev
*c2dev
= c2_port
->c2dev
;
383 struct c2_ring
*tx_ring
= &c2_port
->tx_ring
;
384 struct c2_element
*elem
;
385 struct c2_txp_desc txp_htxd
;
387 spin_lock(&c2_port
->tx_lock
);
389 for (elem
= tx_ring
->to_clean
; elem
!= tx_ring
->to_use
;
392 be16_to_cpu((__force __be16
) readw(elem
->hw_desc
+ C2_TXP_FLAGS
));
394 if (txp_htxd
.flags
!= TXP_HTXD_DONE
)
397 if (netif_msg_tx_done(c2_port
)) {
398 /* PCI reads are expensive in fast path */
400 be16_to_cpu((__force __be16
) readw(elem
->hw_desc
+ C2_TXP_LEN
));
401 pr_debug("%s: tx done slot %3Zu status 0x%x len "
403 netdev
->name
, elem
- tx_ring
->start
,
404 txp_htxd
.flags
, txp_htxd
.len
);
407 c2_tx_free(c2dev
, elem
);
408 ++(c2_port
->tx_avail
);
411 tx_ring
->to_clean
= elem
;
413 if (netif_queue_stopped(netdev
)
414 && c2_port
->tx_avail
> MAX_SKB_FRAGS
+ 1)
415 netif_wake_queue(netdev
);
417 spin_unlock(&c2_port
->tx_lock
);
420 static void c2_rx_error(struct c2_port
*c2_port
, struct c2_element
*elem
)
422 struct c2_rx_desc
*rx_desc
= elem
->ht_desc
;
423 struct c2_rxp_hdr
*rxp_hdr
= (struct c2_rxp_hdr
*) elem
->skb
->data
;
425 if (rxp_hdr
->status
!= RXP_HRXD_OK
||
426 rxp_hdr
->len
> (rx_desc
->len
- sizeof(*rxp_hdr
))) {
427 pr_debug("BAD RXP_HRXD\n");
428 pr_debug(" rx_desc : %p\n", rx_desc
);
429 pr_debug(" index : %Zu\n",
430 elem
- c2_port
->rx_ring
.start
);
431 pr_debug(" len : %u\n", rx_desc
->len
);
432 pr_debug(" rxp_hdr : %p [PA %p]\n", rxp_hdr
,
433 (void *) __pa((unsigned long) rxp_hdr
));
434 pr_debug(" flags : 0x%x\n", rxp_hdr
->flags
);
435 pr_debug(" status: 0x%x\n", rxp_hdr
->status
);
436 pr_debug(" len : %u\n", rxp_hdr
->len
);
437 pr_debug(" rsvd : 0x%x\n", rxp_hdr
->rsvd
);
440 /* Setup the skb for reuse since we're dropping this pkt */
441 elem
->skb
->data
= elem
->skb
->head
;
442 skb_reset_tail_pointer(elem
->skb
);
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((__force u16
) cpu_to_be16((u16
) elem
->maplen
- sizeof(*rxp_hdr
)),
451 elem
->hw_desc
+ C2_RXP_LEN
);
452 __raw_writeq((__force u64
) cpu_to_be64(elem
->mapaddr
),
453 elem
->hw_desc
+ C2_RXP_ADDR
);
454 __raw_writew((__force u16
) cpu_to_be16(RXP_HRXD_READY
),
455 elem
->hw_desc
+ C2_RXP_FLAGS
);
457 pr_debug("packet dropped\n");
458 c2_port
->netdev
->stats
.rx_dropped
++;
461 static void c2_rx_interrupt(struct net_device
*netdev
)
463 struct c2_port
*c2_port
= netdev_priv(netdev
);
464 struct c2_dev
*c2dev
= c2_port
->c2dev
;
465 struct c2_ring
*rx_ring
= &c2_port
->rx_ring
;
466 struct c2_element
*elem
;
467 struct c2_rx_desc
*rx_desc
;
468 struct c2_rxp_hdr
*rxp_hdr
;
474 spin_lock_irqsave(&c2dev
->lock
, flags
);
476 /* Begin where we left off */
477 rx_ring
->to_clean
= rx_ring
->start
+ c2dev
->cur_rx
;
479 for (elem
= rx_ring
->to_clean
; elem
->next
!= rx_ring
->to_clean
;
481 rx_desc
= elem
->ht_desc
;
482 mapaddr
= elem
->mapaddr
;
483 maplen
= elem
->maplen
;
485 rxp_hdr
= (struct c2_rxp_hdr
*) skb
->data
;
487 if (rxp_hdr
->flags
!= RXP_HRXD_DONE
)
489 buflen
= rxp_hdr
->len
;
491 /* Sanity check the RXP header */
492 if (rxp_hdr
->status
!= RXP_HRXD_OK
||
493 buflen
> (rx_desc
->len
- sizeof(*rxp_hdr
))) {
494 c2_rx_error(c2_port
, elem
);
499 * Allocate and map a new skb for replenishing the host
502 if (c2_rx_alloc(c2_port
, elem
)) {
503 c2_rx_error(c2_port
, elem
);
507 /* Unmap the old skb */
508 pci_unmap_single(c2dev
->pcidev
, mapaddr
, maplen
,
514 * Skip past the leading 8 bytes comprising of the
515 * "struct c2_rxp_hdr", prepended by the adapter
516 * to the usual Ethernet header ("struct ethhdr"),
517 * to the start of the raw Ethernet packet.
519 * Fix up the various fields in the sk_buff before
520 * passing it up to netif_rx(). The transfer size
521 * (in bytes) specified by the adapter len field of
522 * the "struct rxp_hdr_t" does NOT include the
523 * "sizeof(struct c2_rxp_hdr)".
525 skb
->data
+= sizeof(*rxp_hdr
);
526 skb_set_tail_pointer(skb
, buflen
);
528 skb
->protocol
= eth_type_trans(skb
, netdev
);
532 netdev
->stats
.rx_packets
++;
533 netdev
->stats
.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
)
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_zalloc_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 /* Create the Rx host descriptor ring */
617 c2_rx_ring_alloc(&c2_port
->rx_ring
, c2_port
->mem
, c2_port
->dma
,
618 c2dev
->mmio_rxp_ring
))) {
619 pr_debug("Unable to create RX ring\n");
623 /* Allocate Rx buffers for the host descriptor ring */
624 if (c2_rx_fill(c2_port
)) {
625 pr_debug("Unable to fill RX ring\n");
629 /* Create the Tx host descriptor ring */
630 if ((ret
= c2_tx_ring_alloc(&c2_port
->tx_ring
, c2_port
->mem
+ rx_size
,
631 c2_port
->dma
+ rx_size
,
632 c2dev
->mmio_txp_ring
))) {
633 pr_debug("Unable to create TX ring\n");
637 /* Set the TX pointer to where we left off */
638 c2_port
->tx_avail
= c2_port
->tx_ring
.count
- 1;
639 c2_port
->tx_ring
.to_use
= c2_port
->tx_ring
.to_clean
=
640 c2_port
->tx_ring
.start
+ c2dev
->cur_tx
;
642 /* missing: Initialize MAC */
644 BUG_ON(c2_port
->tx_ring
.to_use
!= c2_port
->tx_ring
.to_clean
);
646 /* Reset the adapter, ensures the driver is in sync with the RXP */
649 /* Reset the READY bit in the sk_buff RXP headers & adapter HRXDQ */
650 for (i
= 0, elem
= c2_port
->rx_ring
.start
; i
< c2_port
->rx_ring
.count
;
652 rxp_hdr
= (struct c2_rxp_hdr
*) elem
->skb
->data
;
654 __raw_writew((__force u16
) cpu_to_be16(RXP_HRXD_READY
),
655 elem
->hw_desc
+ C2_RXP_FLAGS
);
658 /* Enable network packets */
659 netif_start_queue(netdev
);
662 writel(0, c2dev
->regs
+ C2_IDIS
);
663 netimr0
= readl(c2dev
->regs
+ C2_NIMR0
);
664 netimr0
&= ~(C2_PCI_HTX_INT
| C2_PCI_HRX_INT
);
665 writel(netimr0
, c2dev
->regs
+ C2_NIMR0
);
667 /* Tell the stack to ignore arp requests for ipaddrs bound to
668 * other interfaces. This is needed to prevent the host stack
669 * from responding to arp requests to the ipaddr bound on the
672 in_dev
= in_dev_get(netdev
);
673 IN_DEV_CONF_SET(in_dev
, ARP_IGNORE
, 1);
679 c2_rx_clean(c2_port
);
680 kfree(c2_port
->rx_ring
.start
);
683 pci_free_consistent(c2dev
->pcidev
, c2_port
->mem_size
, c2_port
->mem
,
689 static int c2_down(struct net_device
*netdev
)
691 struct c2_port
*c2_port
= netdev_priv(netdev
);
692 struct c2_dev
*c2dev
= c2_port
->c2dev
;
694 if (netif_msg_ifdown(c2_port
))
695 pr_debug("%s: disabling interface\n",
698 /* Wait for all the queued packets to get sent */
699 c2_tx_interrupt(netdev
);
701 /* Disable network packets */
702 netif_stop_queue(netdev
);
704 /* Disable IRQs by clearing the interrupt mask */
705 writel(1, c2dev
->regs
+ C2_IDIS
);
706 writel(0, c2dev
->regs
+ C2_NIMR0
);
708 /* missing: Stop transmitter */
710 /* missing: Stop receiver */
712 /* Reset the adapter, ensures the driver is in sync with the RXP */
715 /* missing: Turn off LEDs here */
717 /* Free all buffers in the host descriptor rings */
718 c2_tx_clean(c2_port
);
719 c2_rx_clean(c2_port
);
721 /* Free the host descriptor rings */
722 kfree(c2_port
->rx_ring
.start
);
723 kfree(c2_port
->tx_ring
.start
);
724 pci_free_consistent(c2dev
->pcidev
, c2_port
->mem_size
, c2_port
->mem
,
730 static void c2_reset(struct c2_port
*c2_port
)
732 struct c2_dev
*c2dev
= c2_port
->c2dev
;
733 unsigned int cur_rx
= c2dev
->cur_rx
;
735 /* Tell the hardware to quiesce */
736 C2_SET_CUR_RX(c2dev
, cur_rx
| C2_PCI_HRX_QUI
);
739 * The hardware will reset the C2_PCI_HRX_QUI bit once
740 * the RXP is quiesced. Wait 2 seconds for this.
744 cur_rx
= C2_GET_CUR_RX(c2dev
);
746 if (cur_rx
& C2_PCI_HRX_QUI
)
747 pr_debug("c2_reset: failed to quiesce the hardware!\n");
749 cur_rx
&= ~C2_PCI_HRX_QUI
;
751 c2dev
->cur_rx
= cur_rx
;
753 pr_debug("Current RX: %u\n", c2dev
->cur_rx
);
756 static int c2_xmit_frame(struct sk_buff
*skb
, struct net_device
*netdev
)
758 struct c2_port
*c2_port
= netdev_priv(netdev
);
759 struct c2_dev
*c2dev
= c2_port
->c2dev
;
760 struct c2_ring
*tx_ring
= &c2_port
->tx_ring
;
761 struct c2_element
*elem
;
767 spin_lock_irqsave(&c2_port
->tx_lock
, flags
);
769 if (unlikely(c2_port
->tx_avail
< (skb_shinfo(skb
)->nr_frags
+ 1))) {
770 netif_stop_queue(netdev
);
771 spin_unlock_irqrestore(&c2_port
->tx_lock
, flags
);
773 pr_debug("%s: Tx ring full when queue awake!\n",
775 return NETDEV_TX_BUSY
;
778 maplen
= skb_headlen(skb
);
780 pci_map_single(c2dev
->pcidev
, skb
->data
, maplen
, PCI_DMA_TODEVICE
);
782 elem
= tx_ring
->to_use
;
784 elem
->mapaddr
= mapaddr
;
785 elem
->maplen
= maplen
;
787 /* Tell HW to xmit */
788 __raw_writeq((__force u64
) cpu_to_be64(mapaddr
),
789 elem
->hw_desc
+ C2_TXP_ADDR
);
790 __raw_writew((__force u16
) cpu_to_be16(maplen
),
791 elem
->hw_desc
+ C2_TXP_LEN
);
792 __raw_writew((__force u16
) cpu_to_be16(TXP_HTXD_READY
),
793 elem
->hw_desc
+ C2_TXP_FLAGS
);
795 netdev
->stats
.tx_packets
++;
796 netdev
->stats
.tx_bytes
+= maplen
;
798 /* Loop thru additional data fragments and queue them */
799 if (skb_shinfo(skb
)->nr_frags
) {
800 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
801 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
802 maplen
= skb_frag_size(frag
);
803 mapaddr
= skb_frag_dma_map(&c2dev
->pcidev
->dev
, frag
,
804 0, maplen
, DMA_TO_DEVICE
);
807 elem
->mapaddr
= mapaddr
;
808 elem
->maplen
= maplen
;
810 /* Tell HW to xmit */
811 __raw_writeq((__force u64
) cpu_to_be64(mapaddr
),
812 elem
->hw_desc
+ C2_TXP_ADDR
);
813 __raw_writew((__force u16
) cpu_to_be16(maplen
),
814 elem
->hw_desc
+ C2_TXP_LEN
);
815 __raw_writew((__force u16
) cpu_to_be16(TXP_HTXD_READY
),
816 elem
->hw_desc
+ C2_TXP_FLAGS
);
818 netdev
->stats
.tx_packets
++;
819 netdev
->stats
.tx_bytes
+= maplen
;
823 tx_ring
->to_use
= elem
->next
;
824 c2_port
->tx_avail
-= (skb_shinfo(skb
)->nr_frags
+ 1);
826 if (c2_port
->tx_avail
<= MAX_SKB_FRAGS
+ 1) {
827 netif_stop_queue(netdev
);
828 if (netif_msg_tx_queued(c2_port
))
829 pr_debug("%s: transmit queue full\n",
833 spin_unlock_irqrestore(&c2_port
->tx_lock
, flags
);
835 netdev
->trans_start
= jiffies
;
840 static void c2_tx_timeout(struct net_device
*netdev
)
842 struct c2_port
*c2_port
= netdev_priv(netdev
);
844 if (netif_msg_timer(c2_port
))
845 pr_debug("%s: tx timeout\n", netdev
->name
);
847 c2_tx_clean(c2_port
);
850 static int c2_change_mtu(struct net_device
*netdev
, int new_mtu
)
854 if (new_mtu
< ETH_ZLEN
|| new_mtu
> ETH_JUMBO_MTU
)
857 netdev
->mtu
= new_mtu
;
859 if (netif_running(netdev
)) {
868 static const struct net_device_ops c2_netdev
= {
871 .ndo_start_xmit
= c2_xmit_frame
,
872 .ndo_tx_timeout
= c2_tx_timeout
,
873 .ndo_change_mtu
= c2_change_mtu
,
874 .ndo_set_mac_address
= eth_mac_addr
,
875 .ndo_validate_addr
= eth_validate_addr
,
878 /* Initialize network device */
879 static struct net_device
*c2_devinit(struct c2_dev
*c2dev
,
880 void __iomem
* mmio_addr
)
882 struct c2_port
*c2_port
= NULL
;
883 struct net_device
*netdev
= alloc_etherdev(sizeof(*c2_port
));
886 pr_debug("c2_port etherdev alloc failed");
890 SET_NETDEV_DEV(netdev
, &c2dev
->pcidev
->dev
);
892 netdev
->netdev_ops
= &c2_netdev
;
893 netdev
->watchdog_timeo
= C2_TX_TIMEOUT
;
894 netdev
->irq
= c2dev
->pcidev
->irq
;
896 c2_port
= netdev_priv(netdev
);
897 c2_port
->netdev
= netdev
;
898 c2_port
->c2dev
= c2dev
;
899 c2_port
->msg_enable
= netif_msg_init(debug
, default_msg
);
900 c2_port
->tx_ring
.count
= C2_NUM_TX_DESC
;
901 c2_port
->rx_ring
.count
= C2_NUM_RX_DESC
;
903 spin_lock_init(&c2_port
->tx_lock
);
905 /* Copy our 48-bit ethernet hardware address */
906 memcpy_fromio(netdev
->dev_addr
, mmio_addr
+ C2_REGS_ENADDR
, 6);
908 /* Validate the MAC address */
909 if (!is_valid_ether_addr(netdev
->dev_addr
)) {
910 pr_debug("Invalid MAC Address\n");
911 c2_print_macaddr(netdev
);
916 c2dev
->netdev
= netdev
;
921 static int c2_probe(struct pci_dev
*pcidev
, const struct pci_device_id
*ent
)
924 unsigned long reg0_start
, reg0_flags
, reg0_len
;
925 unsigned long reg2_start
, reg2_flags
, reg2_len
;
926 unsigned long reg4_start
, reg4_flags
, reg4_len
;
927 unsigned kva_map_size
;
928 struct net_device
*netdev
= NULL
;
929 struct c2_dev
*c2dev
= NULL
;
930 void __iomem
*mmio_regs
= NULL
;
932 printk(KERN_INFO PFX
"AMSO1100 Gigabit Ethernet driver v%s loaded\n",
935 /* Enable PCI device */
936 ret
= pci_enable_device(pcidev
);
938 printk(KERN_ERR PFX
"%s: Unable to enable PCI device\n",
943 reg0_start
= pci_resource_start(pcidev
, BAR_0
);
944 reg0_len
= pci_resource_len(pcidev
, BAR_0
);
945 reg0_flags
= pci_resource_flags(pcidev
, BAR_0
);
947 reg2_start
= pci_resource_start(pcidev
, BAR_2
);
948 reg2_len
= pci_resource_len(pcidev
, BAR_2
);
949 reg2_flags
= pci_resource_flags(pcidev
, BAR_2
);
951 reg4_start
= pci_resource_start(pcidev
, BAR_4
);
952 reg4_len
= pci_resource_len(pcidev
, BAR_4
);
953 reg4_flags
= pci_resource_flags(pcidev
, BAR_4
);
955 pr_debug("BAR0 size = 0x%lX bytes\n", reg0_len
);
956 pr_debug("BAR2 size = 0x%lX bytes\n", reg2_len
);
957 pr_debug("BAR4 size = 0x%lX bytes\n", reg4_len
);
959 /* Make sure PCI base addr are MMIO */
960 if (!(reg0_flags
& IORESOURCE_MEM
) ||
961 !(reg2_flags
& IORESOURCE_MEM
) || !(reg4_flags
& IORESOURCE_MEM
)) {
962 printk(KERN_ERR PFX
"PCI regions not an MMIO resource\n");
967 /* Check for weird/broken PCI region reporting */
968 if ((reg0_len
< C2_REG0_SIZE
) ||
969 (reg2_len
< C2_REG2_SIZE
) || (reg4_len
< C2_REG4_SIZE
)) {
970 printk(KERN_ERR PFX
"Invalid PCI region sizes\n");
975 /* Reserve PCI I/O and memory resources */
976 ret
= pci_request_regions(pcidev
, DRV_NAME
);
978 printk(KERN_ERR PFX
"%s: Unable to request regions\n",
983 if ((sizeof(dma_addr_t
) > 4)) {
984 ret
= pci_set_dma_mask(pcidev
, DMA_BIT_MASK(64));
986 printk(KERN_ERR PFX
"64b DMA configuration failed\n");
990 ret
= pci_set_dma_mask(pcidev
, DMA_BIT_MASK(32));
992 printk(KERN_ERR PFX
"32b DMA configuration failed\n");
997 /* Enables bus-mastering on the device */
998 pci_set_master(pcidev
);
1000 /* Remap the adapter PCI registers in BAR4 */
1001 mmio_regs
= ioremap_nocache(reg4_start
+ C2_PCI_REGS_OFFSET
,
1002 sizeof(struct c2_adapter_pci_regs
));
1005 "Unable to remap adapter PCI registers in BAR4\n");
1010 /* Validate PCI regs magic */
1011 for (i
= 0; i
< sizeof(c2_magic
); i
++) {
1012 if (c2_magic
[i
] != readb(mmio_regs
+ C2_REGS_MAGIC
+ i
)) {
1013 printk(KERN_ERR PFX
"Downlevel Firmware boot loader "
1014 "[%d/%Zd: got 0x%x, exp 0x%x]. Use the cc_flash "
1015 "utility to update your boot loader\n",
1016 i
+ 1, sizeof(c2_magic
),
1017 readb(mmio_regs
+ C2_REGS_MAGIC
+ i
),
1019 printk(KERN_ERR PFX
"Adapter not claimed\n");
1026 /* Validate the adapter version */
1027 if (be32_to_cpu((__force __be32
) readl(mmio_regs
+ C2_REGS_VERS
)) != C2_VERSION
) {
1028 printk(KERN_ERR PFX
"Version mismatch "
1029 "[fw=%u, c2=%u], Adapter not claimed\n",
1030 be32_to_cpu((__force __be32
) readl(mmio_regs
+ C2_REGS_VERS
)),
1037 /* Validate the adapter IVN */
1038 if (be32_to_cpu((__force __be32
) readl(mmio_regs
+ C2_REGS_IVN
)) != C2_IVN
) {
1039 printk(KERN_ERR PFX
"Downlevel FIrmware level. You should be using "
1040 "the OpenIB device support kit. "
1041 "[fw=0x%x, c2=0x%x], Adapter not claimed\n",
1042 be32_to_cpu((__force __be32
) readl(mmio_regs
+ C2_REGS_IVN
)),
1049 /* Allocate hardware structure */
1050 c2dev
= (struct c2_dev
*) ib_alloc_device(sizeof(*c2dev
));
1052 printk(KERN_ERR PFX
"%s: Unable to alloc hardware struct\n",
1059 memset(c2dev
, 0, sizeof(*c2dev
));
1060 spin_lock_init(&c2dev
->lock
);
1061 c2dev
->pcidev
= pcidev
;
1064 /* Get the last RX index */
1066 (be32_to_cpu((__force __be32
) readl(mmio_regs
+ C2_REGS_HRX_CUR
)) -
1067 0xffffc000) / sizeof(struct c2_rxp_desc
);
1069 /* Request an interrupt line for the driver */
1070 ret
= request_irq(pcidev
->irq
, c2_interrupt
, IRQF_SHARED
, DRV_NAME
, c2dev
);
1072 printk(KERN_ERR PFX
"%s: requested IRQ %u is busy\n",
1073 pci_name(pcidev
), pcidev
->irq
);
1078 /* Set driver specific data */
1079 pci_set_drvdata(pcidev
, c2dev
);
1081 /* Initialize network device */
1082 if ((netdev
= c2_devinit(c2dev
, mmio_regs
)) == NULL
) {
1088 /* Save off the actual size prior to unmapping mmio_regs */
1089 kva_map_size
= be32_to_cpu((__force __be32
) readl(mmio_regs
+ C2_REGS_PCI_WINSIZE
));
1091 /* Unmap the adapter PCI registers in BAR4 */
1094 /* Register network device */
1095 ret
= register_netdev(netdev
);
1097 printk(KERN_ERR PFX
"Unable to register netdev, ret = %d\n",
1102 /* Disable network packets */
1103 netif_stop_queue(netdev
);
1105 /* Remap the adapter HRXDQ PA space to kernel VA space */
1106 c2dev
->mmio_rxp_ring
= ioremap_nocache(reg4_start
+ C2_RXP_HRXDQ_OFFSET
,
1108 if (!c2dev
->mmio_rxp_ring
) {
1109 printk(KERN_ERR PFX
"Unable to remap MMIO HRXDQ region\n");
1114 /* Remap the adapter HTXDQ PA space to kernel VA space */
1115 c2dev
->mmio_txp_ring
= ioremap_nocache(reg4_start
+ C2_TXP_HTXDQ_OFFSET
,
1117 if (!c2dev
->mmio_txp_ring
) {
1118 printk(KERN_ERR PFX
"Unable to remap MMIO HTXDQ region\n");
1123 /* Save off the current RX index in the last 4 bytes of the TXP Ring */
1124 C2_SET_CUR_RX(c2dev
, c2dev
->cur_rx
);
1126 /* Remap the PCI registers in adapter BAR0 to kernel VA space */
1127 c2dev
->regs
= ioremap_nocache(reg0_start
, reg0_len
);
1129 printk(KERN_ERR PFX
"Unable to remap BAR0\n");
1134 /* Remap the PCI registers in adapter BAR4 to kernel VA space */
1135 c2dev
->pa
= reg4_start
+ C2_PCI_REGS_OFFSET
;
1136 c2dev
->kva
= ioremap_nocache(reg4_start
+ C2_PCI_REGS_OFFSET
,
1139 printk(KERN_ERR PFX
"Unable to remap BAR4\n");
1144 /* Print out the MAC address */
1145 c2_print_macaddr(netdev
);
1147 ret
= c2_rnic_init(c2dev
);
1149 printk(KERN_ERR PFX
"c2_rnic_init failed: %d\n", ret
);
1153 ret
= c2_register_device(c2dev
);
1160 iounmap(c2dev
->kva
);
1163 iounmap(c2dev
->regs
);
1166 iounmap(c2dev
->mmio_txp_ring
);
1169 iounmap(c2dev
->mmio_rxp_ring
);
1172 unregister_netdev(netdev
);
1175 free_netdev(netdev
);
1178 free_irq(pcidev
->irq
, c2dev
);
1181 ib_dealloc_device(&c2dev
->ibdev
);
1184 pci_release_regions(pcidev
);
1187 pci_disable_device(pcidev
);
1193 static void c2_remove(struct pci_dev
*pcidev
)
1195 struct c2_dev
*c2dev
= pci_get_drvdata(pcidev
);
1196 struct net_device
*netdev
= c2dev
->netdev
;
1198 /* Unregister with OpenIB */
1199 c2_unregister_device(c2dev
);
1201 /* Clean up the RNIC resources */
1202 c2_rnic_term(c2dev
);
1204 /* Remove network device from the kernel */
1205 unregister_netdev(netdev
);
1207 /* Free network device */
1208 free_netdev(netdev
);
1210 /* Free the interrupt line */
1211 free_irq(pcidev
->irq
, c2dev
);
1213 /* missing: Turn LEDs off here */
1215 /* Unmap adapter PA space */
1216 iounmap(c2dev
->kva
);
1217 iounmap(c2dev
->regs
);
1218 iounmap(c2dev
->mmio_txp_ring
);
1219 iounmap(c2dev
->mmio_rxp_ring
);
1221 /* Free the hardware structure */
1222 ib_dealloc_device(&c2dev
->ibdev
);
1224 /* Release reserved PCI I/O and memory resources */
1225 pci_release_regions(pcidev
);
1227 /* Disable PCI device */
1228 pci_disable_device(pcidev
);
1230 /* Clear driver specific data */
1231 pci_set_drvdata(pcidev
, NULL
);
1234 static struct pci_driver c2_pci_driver
= {
1236 .id_table
= c2_pci_table
,
1238 .remove
= c2_remove
,
1241 module_pci_driver(c2_pci_driver
);