2 * eth1394.c -- Ethernet driver for Linux IEEE-1394 Subsystem
4 * Copyright (C) 2001 Ben Collins <bcollins@debian.org>
5 * 2000 Bonin Franck <boninf@free.fr>
7 * Mainly based on work by Emanuel Pirker and Andreas E. Bombe
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* State of this driver:
26 * This driver intends to support RFC 2734, which describes a method for
27 * transporting IPv4 datagrams over IEEE-1394 serial busses. This driver
28 * will ultimately support that method, but currently falls short in
29 * several areas. A few issues are:
31 * - Does not support send/recv over Async streams using GASP
32 * packet formats, as per the RFC for ARP requests.
33 * - Does not yet support fragmented packets.
34 * - Relies on hardware address being equal to the nodeid for some things.
35 * - Does not support multicast
36 * - Hardcoded address for sending packets, instead of using discovery
37 * (ARP, see first item)
40 #include <linux/module.h>
42 #include <linux/sched.h>
43 #include <linux/kernel.h>
44 #include <linux/slab.h>
45 #include <linux/errno.h>
46 #include <linux/types.h>
47 #include <linux/delay.h>
48 #include <linux/init.h>
50 #include <linux/netdevice.h>
51 #include <linux/inetdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_ether.h>
56 #include <linux/tcp.h>
57 #include <linux/skbuff.h>
58 #include <asm/delay.h>
59 #include <asm/semaphore.h>
60 #include <asm/bitops.h>
63 #include "ieee1394_types.h"
64 #include "ieee1394_core.h"
65 #include "ieee1394_transactions.h"
67 #include "highlevel.h"
70 #define ETH1394_PRINT_G(level, fmt, args...) \
71 printk(level ETHER1394_DRIVER_NAME": "fmt, ## args)
73 #define ETH1394_PRINT(level, dev_name, fmt, args...) \
74 printk(level ETHER1394_DRIVER_NAME": %s: " fmt, dev_name, ## args)
76 #define DEBUG(fmt, args...) \
77 printk(KERN_ERR fmt, ## args)
79 static char version[] __devinitdata =
80 "$Rev: 601 $ Ben Collins <bcollins@debian.org>";
82 /* Our ieee1394 highlevel driver */
83 #define ETHER1394_DRIVER_NAME "ether1394"
85 static kmem_cache_t *packet_task_cache;
86 static struct hpsb_highlevel *hl_handle = NULL;
89 static LIST_HEAD (host_info_list);
90 static spinlock_t host_info_lock = SPIN_LOCK_UNLOCKED;
92 /* Use common.lf to determine header len */
93 static int hdr_type_len[] = {
94 sizeof (struct eth1394_uf_hdr),
95 sizeof (struct eth1394_ff_hdr),
96 sizeof (struct eth1394_sf_hdr),
97 sizeof (struct eth1394_sf_hdr)
100 MODULE_AUTHOR("Ben Collins (bcollins@debian.org)");
101 MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)");
102 MODULE_LICENSE("GPL");
104 /* Find our host_info struct for a given host pointer. Must be called
106 static inline struct host_info *find_host_info (struct hpsb_host *host)
108 struct list_head *lh;
109 struct host_info *hi;
111 lh = host_info_list.next;
112 while (lh != &host_info_list) {
113 hi = list_entry (lh, struct host_info, list);
115 if (hi->host == host)
123 /* Find the network device for our host */
124 static inline struct net_device *ether1394_find_dev (struct hpsb_host *host)
126 struct host_info *hi;
128 spin_lock_irq (&host_info_lock);
129 hi = find_host_info (host);
130 spin_unlock_irq (&host_info_lock);
138 /* This is called after an "ifup" */
139 static int ether1394_open (struct net_device *dev)
141 struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
143 /* Set the spinlock before grabbing IRQ! */
144 priv->lock = SPIN_LOCK_UNLOCKED;
146 netif_start_queue (dev);
150 /* This is called after an "ifdown" */
151 static int ether1394_stop (struct net_device *dev)
153 netif_stop_queue (dev);
157 /* Return statistics to the caller */
158 static struct net_device_stats *ether1394_stats (struct net_device *dev)
160 return &(((struct eth1394_priv *)dev->priv)->stats);
163 /* What to do if we timeout. I think a host reset is probably in order, so
164 * that's what we do. Should we increment the stat counters too? */
165 static void ether1394_tx_timeout (struct net_device *dev)
167 ETH1394_PRINT (KERN_ERR, dev->name, "Timeout, resetting host %s\n",
168 ((struct eth1394_priv *)(dev->priv))->host->driver->name);
170 highlevel_host_reset (((struct eth1394_priv *)(dev->priv))->host);
172 netif_wake_queue (dev);
175 /* We need to encapsulate the standard header with our own. We use the
176 * ethernet header's proto for our own.
178 * XXX: This is where we need to create a list of skb's for fragmented
180 static inline void ether1394_encapsulate (struct sk_buff *skb, struct net_device *dev,
183 union eth1394_hdr *hdr =
184 (union eth1394_hdr *)skb_push (skb, hdr_type_len[ETH1394_HDR_LF_UF]);
186 hdr->common.lf = ETH1394_HDR_LF_UF;
187 hdr->words.word1 = htons(hdr->words.word1);
188 hdr->uf.ether_type = proto;
193 /* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the
194 * entire arphdr) is the same format as the ip1394 header, so they
195 * overlap. The rest needs to be munged a bit. The remainder of the
196 * arphdr is formatted based on hwaddr len and ipaddr len. We know what
197 * they'll be, so it's easy to judge. */
198 static inline void ether1394_arp_to_1394arp (struct sk_buff *skb, struct net_device *dev)
200 struct eth1394_priv *priv =
201 (struct eth1394_priv *)(dev->priv);
202 u16 phy_id = priv->host->node_id & NODE_MASK;
204 unsigned char *arp_ptr = (unsigned char *)skb->data;
205 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
206 unsigned char arp_data[2*(dev->addr_len+4)];
208 /* Copy the main data that we need */
209 arp_ptr = memcpy (arp_data, arp_ptr + sizeof(struct arphdr), sizeof (arp_data));
211 /* Extend the buffer enough for our new header */
212 skb_put (skb, sizeof (struct eth1394_arp) -
213 (sizeof (arp_data) + sizeof (struct arphdr)));
215 #define PROCESS_MEMBER(ptr,val,len) \
216 memcpy (val, ptr, len); ptr += len
217 arp_ptr += arp1394->hw_addr_len;
218 PROCESS_MEMBER (arp_ptr, &arp1394->sip, arp1394->ip_addr_len);
219 arp_ptr += arp1394->hw_addr_len;
220 PROCESS_MEMBER (arp_ptr, &arp1394->tip, arp1394->ip_addr_len);
221 #undef PROCESS_MEMBER
223 /* Now add our own flavor of arp header fields to the orig one */
224 arp1394->hw_addr_len = IP1394_HW_ADDR_LEN;
225 arp1394->hw_type = __constant_htons (ARPHRD_IEEE1394);
226 arp1394->s_uniq_id = cpu_to_le64 (priv->eui[phy_id]);
227 arp1394->max_rec = priv->max_rec[phy_id];
228 arp1394->sspd = priv->sspd[phy_id];
229 arp1394->fifo_hi = htons (priv->fifo_hi[phy_id]);
230 arp1394->fifo_lo = htonl (priv->fifo_lo[phy_id]);
235 static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
237 if ((new_mtu < 68) || (new_mtu > ETHER1394_REGION_ADDR_LEN))
243 static inline void ether1394_register_limits (int nodeid, unsigned char max_rec,
244 unsigned char sspd, u64 eui, u16 fifo_hi,
245 u32 fifo_lo, struct eth1394_priv *priv)
249 if (nodeid < 0 || nodeid >= ALL_NODES) {
250 ETH1394_PRINT_G (KERN_ERR, "Cannot register invalid nodeid %d\n", nodeid);
254 priv->max_rec[nodeid] = max_rec;
255 priv->sspd[nodeid] = sspd;
256 priv->fifo_hi[nodeid] = fifo_hi;
257 priv->fifo_lo[nodeid] = fifo_lo;
258 priv->eui[nodeid] = eui;
260 /* 63 is used for broadcasts to all hosts. It is equal to the
261 * minimum of all registered nodes. A registered node is one with
262 * a nonzero offset. Set the values rediculously high to start. We
263 * know we have atleast one to change the default to. */
266 for (i = 0; i < ALL_NODES; i++) {
267 if (!priv->fifo_hi && !priv->fifo_lo) continue; /* Unregistered */
268 if (priv->max_rec[i] < max_rec) max_rec = priv->max_rec[i];
269 if (priv->sspd[i] < sspd) sspd = priv->sspd[i];
272 priv->max_rec[ALL_NODES] = max_rec;
273 priv->sspd[ALL_NODES] = sspd;
278 static void ether1394_reset_priv (struct net_device *dev, int set_mtu)
281 struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
282 int phy_id = priv->host->node_id & NODE_MASK;
284 spin_lock_irqsave (&priv->lock, flags);
286 /* Clear the speed/payload/offset tables */
287 memset (priv->max_rec, 0, sizeof (priv->max_rec));
288 memset (priv->sspd, 0, sizeof (priv->sspd));
289 memset (priv->fifo_hi, 0, sizeof (priv->fifo_hi));
290 memset (priv->fifo_lo, 0, sizeof (priv->fifo_lo));
292 /* Register our limits now */
293 ether1394_register_limits (phy_id, (be32_to_cpu(priv->host->csr.rom[2]) >> 12) & 0xf,
294 priv->host->speed_map[(phy_id << 6) + phy_id],
295 (u64)(((u64)be32_to_cpu(priv->host->csr.rom[3]) << 32) |
296 be32_to_cpu(priv->host->csr.rom[4])),
297 ETHER1394_REGION_ADDR >> 32,
298 ETHER1394_REGION_ADDR & 0xffffffff, priv);
300 /* We'll use our max_rec as the default mtu */
302 dev->mtu = (1 << (priv->max_rec[phy_id] + 1)) - sizeof (union eth1394_hdr);
304 /* Set our hardware address while we're at it */
305 *(nodeid_t *)dev->dev_addr = htons (priv->host->node_id);
307 spin_unlock_irqrestore (&priv->lock, flags);
310 static int ether1394_tx (struct sk_buff *skb, struct net_device *dev);
312 /* This function is called by register_netdev */
313 static int ether1394_init_dev (struct net_device *dev)
316 dev->open = ether1394_open;
317 dev->stop = ether1394_stop;
318 dev->hard_start_xmit = ether1394_tx;
319 dev->get_stats = ether1394_stats;
320 dev->tx_timeout = ether1394_tx_timeout;
321 dev->change_mtu = ether1394_change_mtu;
324 dev->watchdog_timeo = ETHER1394_TIMEOUT;
325 dev->flags = IFF_BROADCAST; /* TODO: Support MCAP */
326 dev->features = NETIF_F_NO_CSUM|NETIF_F_SG|NETIF_F_HIGHDMA|NETIF_F_FRAGLIST;
329 ether1394_reset_priv (dev, 1);
335 * This function is called every time a card is found. It is generally called
336 * when the module is installed. This is where we add all of our ethernet
337 * devices. One for each host.
339 static void ether1394_add_host (struct hpsb_host *host)
341 struct host_info *hi = NULL;
342 struct net_device *dev = NULL;
343 struct eth1394_priv *priv;
344 static int version_printed = 0;
346 if (version_printed++ == 0)
347 ETH1394_PRINT_G (KERN_INFO, "%s\n", version);
349 dev = alloc_etherdev(sizeof (struct eth1394_priv));
354 SET_MODULE_OWNER(dev);
356 dev->init = ether1394_init_dev;
358 priv = (struct eth1394_priv *)dev->priv;
362 hi = (struct host_info *)kmalloc (sizeof (struct host_info),
368 if (register_netdev (dev)) {
369 ETH1394_PRINT (KERN_ERR, dev->name, "Error registering network driver\n");
374 ETH1394_PRINT (KERN_ERR, dev->name, "IEEE-1394 IPv4 over 1394 Ethernet (%s)\n",
377 INIT_LIST_HEAD (&hi->list);
381 spin_lock_irq (&host_info_lock);
382 list_add_tail (&hi->list, &host_info_list);
383 spin_unlock_irq (&host_info_lock);
390 ETH1394_PRINT_G (KERN_ERR, "Out of memory\n");
395 /* Remove a card from our list */
396 static void ether1394_remove_host (struct hpsb_host *host)
398 struct host_info *hi;
400 spin_lock_irq (&host_info_lock);
401 hi = find_host_info (host);
403 unregister_netdev (hi->dev);
405 list_del (&hi->list);
408 spin_unlock_irq (&host_info_lock);
413 /* A reset has just arisen */
414 static void ether1394_host_reset (struct hpsb_host *host)
416 struct net_device *dev = ether1394_find_dev(host);
418 /* This can happen for hosts that we don't use */
422 /* Reset our private host data, but not our mtu */
423 netif_stop_queue (dev);
424 ether1394_reset_priv (dev, 0);
425 netif_wake_queue (dev);
428 /* Copied from net/ethernet/eth.c */
429 static inline unsigned short ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
434 skb->mac.raw = skb->data;
435 skb_pull (skb, ETH_HLEN);
436 eth = skb->mac.ethernet;
438 if(*eth->h_dest & 1) {
439 if(memcmp(eth->h_dest, dev->broadcast, dev->addr_len)==0)
440 skb->pkt_type = PACKET_BROADCAST;
442 skb->pkt_type = PACKET_MULTICAST;
444 if(memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
445 skb->pkt_type = PACKET_OTHERHOST;
448 if (ntohs (eth->h_proto) >= 1536)
453 if (*(unsigned short *)rawp == 0xFFFF)
454 return htons (ETH_P_802_3);
456 return htons (ETH_P_802_2);
459 /* Parse an encapsulated IP1394 header into an ethernet frame packet.
460 * We also perform ARP translation here, if need be. */
461 static inline unsigned short ether1394_parse_encap (struct sk_buff *skb, struct net_device *dev,
462 nodeid_t srcid, nodeid_t destid)
464 union eth1394_hdr *hdr = (union eth1394_hdr *)skb->data;
465 unsigned char src_hw[ETH_ALEN], dest_hw[ETH_ALEN];
466 unsigned short ret = 0;
468 /* Setup our hw addresses. We use these to build the
469 * ethernet header. */
470 *(u16 *)dest_hw = htons(destid);
471 *(u16 *)src_hw = htons(srcid);
473 /* Remove the encapsulation header */
474 hdr->words.word1 = ntohs(hdr->words.word1);
475 skb_pull (skb, hdr_type_len[hdr->common.lf]);
477 /* If this is an ARP packet, convert it. First, we want to make
478 * use of some of the fields, since they tell us a little bit
479 * about the sending machine. */
480 if (hdr->uf.ether_type == __constant_htons (ETH_P_ARP)) {
482 u16 phy_id = srcid & NODE_MASK;
483 struct eth1394_priv *priv =
484 (struct eth1394_priv *)dev->priv;
485 struct eth1394_arp arp1394;
486 struct arphdr *arp = (struct arphdr *)skb->data;
487 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
489 memcpy (&arp1394, arp, sizeof (struct eth1394_arp));
491 /* Update our speed/payload/fifo_offset table */
492 spin_lock_irqsave (&priv->lock, flags);
493 ether1394_register_limits (phy_id, arp1394.max_rec, arp1394.sspd,
494 le64_to_cpu (arp1394.s_uniq_id),
495 ntohs (arp1394.fifo_hi),
496 ntohl (arp1394.fifo_lo), priv);
497 spin_unlock_irqrestore (&priv->lock, flags);
499 #define PROCESS_MEMBER(ptr,val,len) \
500 ptr = memcpy (ptr, val, len) + len
501 PROCESS_MEMBER (arp_ptr, src_hw, dev->addr_len);
502 PROCESS_MEMBER (arp_ptr, &arp1394.sip, 4);
503 PROCESS_MEMBER (arp_ptr, dest_hw, dev->addr_len);
504 PROCESS_MEMBER (arp_ptr, &arp1394.tip, 4);
505 #undef PROCESS_MEMBER
507 arp->ar_hln = dev->addr_len;
508 arp->ar_hrd = __constant_htons (ARPHRD_ETHER);
510 skb_trim (skb, sizeof (struct arphdr) + 2*(dev->addr_len+4));
513 /* Now add the ethernet header. */
514 if (dev->hard_header (skb, dev, __constant_ntohs (hdr->uf.ether_type),
515 dest_hw, src_hw, skb->len) >= 0)
516 ret = ether1394_type_trans(skb, dev);
521 /* Packet reception. We convert the IP1394 encapsulation header to an
522 * ethernet header, and fill it with some of our other fields. This is
523 * an incoming packet from the 1394 bus. */
524 static int ether1394_write (struct hpsb_host *host, int srcid, int destid,
525 quadlet_t *data, u64 addr, unsigned int len, u16 fl)
528 char *buf = (char *)data;
530 struct net_device *dev = ether1394_find_dev (host);
531 struct eth1394_priv *priv;
534 ETH1394_PRINT_G (KERN_ERR, "Could not find net device for host %p\n",
536 return RCODE_ADDRESS_ERROR;
539 priv = (struct eth1394_priv *)dev->priv;
541 /* A packet has been received by the ieee1394 bus. Build an skbuff
542 * around it so we can pass it to the high level network layer. */
544 skb = dev_alloc_skb (len + dev->hard_header_len + 15);
546 HPSB_PRINT (KERN_ERR, "ether1394 rx: low on mem\n");
547 priv->stats.rx_dropped++;
548 return RCODE_ADDRESS_ERROR;
551 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
553 memcpy (skb_put (skb, len), buf, len);
555 /* Write metadata, and then pass to the receive level */
557 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
559 /* Parse the encapsulation header. This actually does the job of
560 * converting to an ethernet frame header, aswell as arp
561 * conversion if needed. ARP conversion is easier in this
562 * direction, since we are using ethernet as our backend. */
563 skb->protocol = ether1394_parse_encap (skb, dev, srcid, destid);
565 spin_lock_irqsave (&priv->lock, flags);
566 if (!skb->protocol) {
567 priv->stats.rx_errors++;
568 priv->stats.rx_dropped++;
569 dev_kfree_skb_any(skb);
573 netif_stop_queue(dev);
574 if (netif_rx (skb) == NET_RX_DROP) {
575 priv->stats.rx_errors++;
576 priv->stats.rx_dropped++;
581 priv->stats.rx_packets++;
582 priv->stats.rx_bytes += skb->len;
585 netif_start_queue(dev);
586 spin_unlock_irqrestore (&priv->lock, flags);
588 dev->last_rx = jiffies;
590 return RCODE_COMPLETE;
593 /* This function is our scheduled write */
594 static void hpsb_write_sched (void *__ptask)
596 struct packet_task *ptask = (struct packet_task *)__ptask;
597 struct sk_buff *skb = ptask->skb;
598 struct net_device *dev = ptask->skb->dev;
599 struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
603 spin_lock_irqsave (&priv->lock, flags);
604 if (!hpsb_write(priv->host, ptask->dest_node,
605 get_hpsb_generation(priv->host),
606 ptask->addr, (quadlet_t *)skb->data, skb->len)) {
607 priv->stats.tx_bytes += skb->len;
608 priv->stats.tx_packets++;
610 //printk("Failed in hpsb_write_sched\n");
611 priv->stats.tx_dropped++;
612 priv->stats.tx_errors++;
613 if (netif_queue_stopped (dev))
614 netif_wake_queue (dev);
616 spin_unlock_irqrestore (&priv->lock, flags);
618 dev->trans_start = jiffies;
620 kmem_cache_free(packet_task_cache, ptask);
625 /* Transmit a packet (called by kernel) */
626 static int ether1394_tx (struct sk_buff *skb, struct net_device *dev)
628 int kmflags = in_interrupt () ? GFP_ATOMIC : GFP_KERNEL;
630 struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
635 struct packet_task *ptask = NULL;
638 if ((skb = skb_share_check (skb, kmflags)) == NULL) {
643 /* Get rid of the ethernet header, but save a pointer */
644 eth = (struct ethhdr *)skb->data;
645 skb_pull (skb, ETH_HLEN);
647 /* Save the destination id, and proto for our encapsulation, then
648 * toss the ethernet header aside like the cheap whore it is. */
649 dest_node = ntohs (*(nodeid_t *)(eth->h_dest));
650 proto = eth->h_proto;
652 /* If this is an ARP packet, convert it */
653 if (proto == __constant_htons (ETH_P_ARP))
654 ether1394_arp_to_1394arp (skb, dev);
656 /* Now add our encapsulation header */
657 ether1394_encapsulate (skb, dev, proto);
659 /* TODO: The above encapsulate function needs to recognize when a
660 * packet needs to be split for a specified node. It should create
661 * a list of skb's that we could then iterate over for the below
662 * call to schedule our writes. */
664 /* XXX: Right now we accept that we don't exactly follow RFC. When
665 * we do, we will send ARP requests via GASP format, and so we wont
668 spin_lock_irqsave (&priv->lock, flags);
669 addr = (u64)priv->fifo_hi[dest_node & NODE_MASK] << 32 |
670 priv->fifo_lo[dest_node & NODE_MASK];
671 spin_unlock_irqrestore (&priv->lock, flags);
674 addr = ETHER1394_REGION_ADDR;
676 ptask = kmem_cache_alloc(packet_task_cache, kmflags);
684 ptask->dest_node = dest_node;
685 HPSB_INIT_WORK(&ptask->tq, hpsb_write_sched, ptask);
686 hpsb_schedule_work(&ptask->tq);
690 printk("Failed in ether1394_tx\n");
695 spin_lock_irqsave (&priv->lock, flags);
696 priv->stats.tx_dropped++;
697 priv->stats.tx_errors++;
698 if (netif_queue_stopped (dev))
699 netif_wake_queue (dev);
700 spin_unlock_irqrestore (&priv->lock, flags);
705 /* Function for incoming 1394 packets */
706 static struct hpsb_address_ops addr_ops = {
707 .write = ether1394_write,
710 /* Ieee1394 highlevel driver functions */
711 static struct hpsb_highlevel_ops hl_ops = {
712 .add_host = ether1394_add_host,
713 .remove_host = ether1394_remove_host,
714 .host_reset = ether1394_host_reset,
717 static int __init ether1394_init_module (void)
719 packet_task_cache = kmem_cache_create("packet_task", sizeof(struct packet_task),
722 /* Register ourselves as a highlevel driver */
723 hl_handle = hpsb_register_highlevel (ETHER1394_DRIVER_NAME, &hl_ops);
725 if (hl_handle == NULL) {
726 ETH1394_PRINT_G (KERN_ERR, "No more memory for driver\n");
730 hpsb_register_addrspace (hl_handle, &addr_ops, ETHER1394_REGION_ADDR,
731 ETHER1394_REGION_ADDR_END);
736 static void __exit ether1394_exit_module (void)
738 hpsb_unregister_highlevel (hl_handle);
739 kmem_cache_destroy(packet_task_cache);
742 module_init(ether1394_init_module);
743 module_exit(ether1394_exit_module);