import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / ieee1394 / eth1394.c
1 /*
2  * eth1394.c -- Ethernet driver for Linux IEEE-1394 Subsystem
3  * 
4  * Copyright (C) 2001 Ben Collins <bcollins@debian.org>
5  *               2000 Bonin Franck <boninf@free.fr>
6  *
7  * Mainly based on work by Emanuel Pirker and Andreas E. Bombe
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /* State of this driver:
25  *
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:
30  *
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)
38  */
39
40 #include <linux/module.h>
41
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>
49
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>
55 #include <linux/ip.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>
61 #include <net/arp.h>
62
63 #include "ieee1394_types.h"
64 #include "ieee1394_core.h"
65 #include "ieee1394_transactions.h"
66 #include "ieee1394.h"
67 #include "highlevel.h"
68 #include "eth1394.h"
69
70 #define ETH1394_PRINT_G(level, fmt, args...) \
71         printk(level ETHER1394_DRIVER_NAME": "fmt, ## args)
72
73 #define ETH1394_PRINT(level, dev_name, fmt, args...) \
74         printk(level ETHER1394_DRIVER_NAME": %s: " fmt, dev_name, ## args)
75
76 #define DEBUG(fmt, args...) \
77         printk(KERN_ERR fmt, ## args)
78
79 static char version[] __devinitdata =
80         "$Rev: 601 $ Ben Collins <bcollins@debian.org>";
81
82 /* Our ieee1394 highlevel driver */
83 #define ETHER1394_DRIVER_NAME "ether1394"
84
85 static kmem_cache_t *packet_task_cache;
86 static struct hpsb_highlevel *hl_handle = NULL;
87
88 /* Card handling */
89 static LIST_HEAD (host_info_list);
90 static spinlock_t host_info_lock = SPIN_LOCK_UNLOCKED;
91
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)
98 };
99
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");
103
104 /* Find our host_info struct for a given host pointer. Must be called
105  * under spinlock.  */
106 static inline struct host_info *find_host_info (struct hpsb_host *host)
107 {
108         struct list_head *lh;
109         struct host_info *hi;
110
111         lh = host_info_list.next;
112         while (lh != &host_info_list) {
113                 hi = list_entry (lh, struct host_info, list);
114
115                 if (hi->host == host)
116                         return hi;
117
118                 lh = lh->next;
119         }
120         return NULL;
121 }
122
123 /* Find the network device for our host */
124 static inline struct net_device *ether1394_find_dev (struct hpsb_host *host)
125 {
126         struct host_info *hi;
127
128         spin_lock_irq (&host_info_lock);
129         hi = find_host_info (host);
130         spin_unlock_irq (&host_info_lock);
131
132         if (hi == NULL)
133                 return NULL;
134
135         return hi->dev;
136 }
137
138 /* This is called after an "ifup" */
139 static int ether1394_open (struct net_device *dev)
140 {
141         struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
142
143         /* Set the spinlock before grabbing IRQ! */
144         priv->lock = SPIN_LOCK_UNLOCKED;
145
146         netif_start_queue (dev);
147         return 0;
148 }
149
150 /* This is called after an "ifdown" */
151 static int ether1394_stop (struct net_device *dev)
152 {
153         netif_stop_queue (dev);
154         return 0;
155 }
156
157 /* Return statistics to the caller */
158 static struct net_device_stats *ether1394_stats (struct net_device *dev)
159 {
160         return &(((struct eth1394_priv *)dev->priv)->stats);
161 }
162
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)
166 {
167         ETH1394_PRINT (KERN_ERR, dev->name, "Timeout, resetting host %s\n",
168                        ((struct eth1394_priv *)(dev->priv))->host->driver->name);
169
170         highlevel_host_reset (((struct eth1394_priv *)(dev->priv))->host);
171
172         netif_wake_queue (dev);
173 }
174
175 /* We need to encapsulate the standard header with our own. We use the
176  * ethernet header's proto for our own.
177  *
178  * XXX: This is where we need to create a list of skb's for fragmented
179  * packets.  */
180 static inline void ether1394_encapsulate (struct sk_buff *skb, struct net_device *dev,
181                             int proto)
182 {
183         union eth1394_hdr *hdr =
184                 (union eth1394_hdr *)skb_push (skb, hdr_type_len[ETH1394_HDR_LF_UF]);
185
186         hdr->common.lf = ETH1394_HDR_LF_UF;
187         hdr->words.word1 = htons(hdr->words.word1);
188         hdr->uf.ether_type = proto;
189
190         return;
191 }
192
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)
199 {
200         struct eth1394_priv *priv =
201                 (struct eth1394_priv *)(dev->priv);
202         u16 phy_id = priv->host->node_id & NODE_MASK;
203
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)];
207
208         /* Copy the main data that we need */
209         arp_ptr = memcpy (arp_data, arp_ptr + sizeof(struct arphdr), sizeof (arp_data));
210
211         /* Extend the buffer enough for our new header */
212         skb_put (skb, sizeof (struct eth1394_arp) -
213                  (sizeof (arp_data) + sizeof (struct arphdr)));
214
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
222
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]);
231
232         return;
233 }
234
235 static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
236 {
237         if ((new_mtu < 68) || (new_mtu > ETHER1394_REGION_ADDR_LEN))
238                 return -EINVAL;
239         dev->mtu = new_mtu;
240         return 0;
241 }
242
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)
246 {
247         int i;
248
249         if (nodeid < 0 || nodeid >= ALL_NODES) {
250                 ETH1394_PRINT_G (KERN_ERR, "Cannot register invalid nodeid %d\n", nodeid);
251                 return;
252         }
253
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;
259
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.  */
264         sspd = 0xff;
265         max_rec = 0xff;
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];
270         }
271
272         priv->max_rec[ALL_NODES] = max_rec;
273         priv->sspd[ALL_NODES] = sspd;
274
275         return;
276 }
277
278 static void ether1394_reset_priv (struct net_device *dev, int set_mtu)
279 {
280         unsigned long flags;
281         struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
282         int phy_id = priv->host->node_id & NODE_MASK;
283
284         spin_lock_irqsave (&priv->lock, flags);
285
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));
291
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);
299
300         /* We'll use our max_rec as the default mtu */
301         if (set_mtu)
302                 dev->mtu = (1 << (priv->max_rec[phy_id] + 1)) - sizeof (union eth1394_hdr);
303
304         /* Set our hardware address while we're at it */
305         *(nodeid_t *)dev->dev_addr = htons (priv->host->node_id);
306
307         spin_unlock_irqrestore (&priv->lock, flags);
308 }
309
310 static int ether1394_tx (struct sk_buff *skb, struct net_device *dev);
311
312 /* This function is called by register_netdev */
313 static int ether1394_init_dev (struct net_device *dev)
314 {
315         /* Our functions */
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;
322
323         /* Some constants */
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;
327         dev->addr_len           = 2;
328
329         ether1394_reset_priv (dev, 1);
330
331         return 0;
332 }
333
334 /*
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.
338  */
339 static void ether1394_add_host (struct hpsb_host *host)
340 {
341         struct host_info *hi = NULL;
342         struct net_device *dev = NULL;
343         struct eth1394_priv *priv;
344         static int version_printed = 0;
345
346         if (version_printed++ == 0)
347                 ETH1394_PRINT_G (KERN_INFO, "%s\n", version);
348
349         dev = alloc_etherdev(sizeof (struct eth1394_priv));
350
351         if (dev == NULL)
352                 goto out;
353
354         SET_MODULE_OWNER(dev);
355
356         dev->init = ether1394_init_dev;
357
358         priv = (struct eth1394_priv *)dev->priv;
359
360         priv->host = host;
361
362         hi = (struct host_info *)kmalloc (sizeof (struct host_info),
363                                           GFP_KERNEL);
364
365         if (hi == NULL)
366                 goto out;
367
368         if (register_netdev (dev)) {
369                 ETH1394_PRINT (KERN_ERR, dev->name, "Error registering network driver\n");
370                 kfree (dev);
371                 return;
372         }
373
374         ETH1394_PRINT (KERN_ERR, dev->name, "IEEE-1394 IPv4 over 1394 Ethernet (%s)\n",
375                        host->driver->name);
376
377         INIT_LIST_HEAD (&hi->list);
378         hi->host = host;
379         hi->dev = dev;
380
381         spin_lock_irq (&host_info_lock);
382         list_add_tail (&hi->list, &host_info_list);
383         spin_unlock_irq (&host_info_lock);
384
385         return;
386
387 out:
388         if (dev != NULL)
389                 kfree (dev);
390         ETH1394_PRINT_G (KERN_ERR, "Out of memory\n");
391
392         return;
393 }
394
395 /* Remove a card from our list */
396 static void ether1394_remove_host (struct hpsb_host *host)
397 {
398         struct host_info *hi;
399
400         spin_lock_irq (&host_info_lock);
401         hi = find_host_info (host);
402         if (hi != NULL) {
403                 unregister_netdev (hi->dev);
404                 kfree (hi->dev);
405                 list_del (&hi->list);
406                 kfree (hi);
407         }
408         spin_unlock_irq (&host_info_lock);
409
410         return;
411 }
412
413 /* A reset has just arisen */
414 static void ether1394_host_reset (struct hpsb_host *host)
415 {
416         struct net_device *dev = ether1394_find_dev(host);
417
418         /* This can happen for hosts that we don't use */
419         if (dev == NULL)
420                 return;
421
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);
426 }
427
428 /* Copied from net/ethernet/eth.c */
429 static inline unsigned short ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
430 {
431         struct ethhdr *eth;
432         unsigned char *rawp;
433
434         skb->mac.raw = skb->data;
435         skb_pull (skb, ETH_HLEN);
436         eth = skb->mac.ethernet;
437 #if 0
438         if(*eth->h_dest & 1) {
439                 if(memcmp(eth->h_dest, dev->broadcast, dev->addr_len)==0)
440                         skb->pkt_type = PACKET_BROADCAST;
441                 else
442                         skb->pkt_type = PACKET_MULTICAST;
443         } else {
444                 if(memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
445                         skb->pkt_type = PACKET_OTHERHOST;
446         }
447 #endif
448         if (ntohs (eth->h_proto) >= 1536)
449                 return eth->h_proto;
450
451         rawp = skb->data;
452
453         if (*(unsigned short *)rawp == 0xFFFF)
454                 return htons (ETH_P_802_3);
455
456         return htons (ETH_P_802_2);
457 }
458
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)
463 {
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;
467
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);
472
473         /* Remove the encapsulation header */
474         hdr->words.word1 = ntohs(hdr->words.word1);
475         skb_pull (skb, hdr_type_len[hdr->common.lf]);
476
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)) {
481                 unsigned long flags;
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);
488
489                 memcpy (&arp1394, arp, sizeof (struct eth1394_arp));
490
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);
498
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
506
507                 arp->ar_hln = dev->addr_len;
508                 arp->ar_hrd = __constant_htons (ARPHRD_ETHER);
509
510                 skb_trim (skb, sizeof (struct arphdr) + 2*(dev->addr_len+4));
511         }
512
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);
517
518         return ret;
519 }
520
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)
526 {
527         struct sk_buff *skb;
528         char *buf = (char *)data;
529         unsigned long flags;
530         struct net_device *dev = ether1394_find_dev (host);
531         struct eth1394_priv *priv;
532
533         if (dev == NULL) {
534                 ETH1394_PRINT_G (KERN_ERR, "Could not find net device for host %p\n",
535                                  host);
536                 return RCODE_ADDRESS_ERROR;
537         }
538
539         priv = (struct eth1394_priv *)dev->priv;
540
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. */
543
544         skb = dev_alloc_skb (len + dev->hard_header_len + 15);
545         if (!skb) {
546                 HPSB_PRINT (KERN_ERR, "ether1394 rx: low on mem\n");
547                 priv->stats.rx_dropped++;
548                 return RCODE_ADDRESS_ERROR;
549         }
550
551         skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
552
553         memcpy (skb_put (skb, len), buf, len);
554
555         /* Write metadata, and then pass to the receive level */
556         skb->dev = dev;
557         skb->ip_summed = CHECKSUM_UNNECESSARY;  /* don't check it */
558
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);
564
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);
570                 goto bad_proto;
571         }
572
573         netif_stop_queue(dev);
574         if (netif_rx (skb) == NET_RX_DROP) {
575                 priv->stats.rx_errors++;
576                 priv->stats.rx_dropped++;
577                 goto bad_proto;
578         }
579
580         /* Statistics */
581         priv->stats.rx_packets++;
582         priv->stats.rx_bytes += skb->len;
583
584 bad_proto:
585         netif_start_queue(dev);
586         spin_unlock_irqrestore (&priv->lock, flags);
587
588         dev->last_rx = jiffies;
589
590         return RCODE_COMPLETE;
591 }
592
593 /* This function is our scheduled write */
594 static void hpsb_write_sched (void *__ptask)
595 {
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;
600         unsigned long flags;
601
602         /* Statistics */
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++;
609         } else {
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);
615         }
616         spin_unlock_irqrestore (&priv->lock, flags);
617
618         dev->trans_start = jiffies;
619         dev_kfree_skb(skb);
620         kmem_cache_free(packet_task_cache, ptask);
621
622         return;
623 }
624
625 /* Transmit a packet (called by kernel) */
626 static int ether1394_tx (struct sk_buff *skb, struct net_device *dev)
627 {
628         int kmflags = in_interrupt () ? GFP_ATOMIC : GFP_KERNEL;
629         struct ethhdr *eth;
630         struct eth1394_priv *priv = (struct eth1394_priv *)dev->priv;
631         int proto;
632         unsigned long flags;
633         nodeid_t dest_node;
634         u64 addr;
635         struct packet_task *ptask = NULL;
636         int ret = 0;
637
638         if ((skb = skb_share_check (skb, kmflags)) == NULL) {
639                 ret = -ENOMEM;
640                 goto fail;
641         }
642
643         /* Get rid of the ethernet header, but save a pointer */
644         eth = (struct ethhdr *)skb->data;
645         skb_pull (skb, ETH_HLEN);
646
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;
651
652         /* If this is an ARP packet, convert it */
653         if (proto == __constant_htons (ETH_P_ARP))
654                 ether1394_arp_to_1394arp (skb, dev);
655
656         /* Now add our encapsulation header */
657         ether1394_encapsulate (skb, dev, proto);
658
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.  */
663
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
666          * need this hack.  */
667
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);
672
673         if (!addr)
674                 addr = ETHER1394_REGION_ADDR;
675
676         ptask = kmem_cache_alloc(packet_task_cache, kmflags);
677         if (ptask == NULL) {
678                 ret = -ENOMEM;
679                 goto fail;
680         }
681
682         ptask->skb = skb;
683         ptask->addr = addr;
684         ptask->dest_node = dest_node;
685         HPSB_INIT_WORK(&ptask->tq, hpsb_write_sched, ptask);
686         hpsb_schedule_work(&ptask->tq);
687
688         return 0;
689 fail:
690         printk("Failed in ether1394_tx\n");
691
692         if (skb != NULL)
693                 dev_kfree_skb (skb);
694
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);
701
702         return ret;
703 }
704
705 /* Function for incoming 1394 packets */
706 static struct hpsb_address_ops addr_ops = {
707         .write =        ether1394_write,
708 };
709
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,
715 };
716
717 static int __init ether1394_init_module (void)
718 {
719         packet_task_cache = kmem_cache_create("packet_task", sizeof(struct packet_task),
720                                               0, 0, NULL, NULL);
721
722         /* Register ourselves as a highlevel driver */
723         hl_handle = hpsb_register_highlevel (ETHER1394_DRIVER_NAME, &hl_ops);
724
725         if (hl_handle == NULL) {
726                 ETH1394_PRINT_G (KERN_ERR, "No more memory for driver\n");
727                 return -ENOMEM;
728         }
729
730         hpsb_register_addrspace (hl_handle, &addr_ops, ETHER1394_REGION_ADDR,
731                                  ETHER1394_REGION_ADDR_END);
732
733         return 0;
734 }
735
736 static void __exit ether1394_exit_module (void)
737 {
738         hpsb_unregister_highlevel (hl_handle);
739         kmem_cache_destroy(packet_task_cache);
740 }
741
742 module_init(ether1394_init_module);
743 module_exit(ether1394_exit_module);