[PATCH] x86: i8253/i8259A lock cleanup
[pandora-kernel.git] / drivers / ieee1394 / eth1394.c
1 /*
2  * eth1394.c -- Ethernet driver for Linux IEEE-1394 Subsystem
3  *
4  * Copyright (C) 2001-2003 Ben Collins <bcollins@debian.org>
5  *               2000 Bonin Franck <boninf@free.fr>
6  *               2003 Steve Kinneberg <kinnebergsteve@acmsystems.com>
7  *
8  * Mainly based on work by Emanuel Pirker and Andreas E. Bombe
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24
25 /* This driver intends to support RFC 2734, which describes a method for
26  * transporting IPv4 datagrams over IEEE-1394 serial busses. This driver
27  * will ultimately support that method, but currently falls short in
28  * several areas.
29  *
30  * TODO:
31  * RFC 2734 related:
32  * - Add MCAP. Limited Multicast exists only to 224.0.0.1 and 224.0.0.2.
33  *
34  * Non-RFC 2734 related:
35  * - Handle fragmented skb's coming from the networking layer.
36  * - Move generic GASP reception to core 1394 code
37  * - Convert kmalloc/kfree for link fragments to use kmem_cache_* instead
38  * - Stability improvements
39  * - Performance enhancements
40  * - Consider garbage collecting old partial datagrams after X amount of time
41  */
42
43
44 #include <linux/module.h>
45
46 #include <linux/sched.h>
47 #include <linux/kernel.h>
48 #include <linux/slab.h>
49 #include <linux/errno.h>
50 #include <linux/types.h>
51 #include <linux/delay.h>
52 #include <linux/init.h>
53
54 #include <linux/netdevice.h>
55 #include <linux/inetdevice.h>
56 #include <linux/etherdevice.h>
57 #include <linux/if_arp.h>
58 #include <linux/if_ether.h>
59 #include <linux/ip.h>
60 #include <linux/in.h>
61 #include <linux/tcp.h>
62 #include <linux/skbuff.h>
63 #include <linux/bitops.h>
64 #include <linux/ethtool.h>
65 #include <asm/uaccess.h>
66 #include <asm/delay.h>
67 #include <asm/semaphore.h>
68 #include <net/arp.h>
69
70 #include "csr1212.h"
71 #include "ieee1394_types.h"
72 #include "ieee1394_core.h"
73 #include "ieee1394_transactions.h"
74 #include "ieee1394.h"
75 #include "highlevel.h"
76 #include "iso.h"
77 #include "nodemgr.h"
78 #include "eth1394.h"
79 #include "config_roms.h"
80
81 #define ETH1394_PRINT_G(level, fmt, args...) \
82         printk(level "%s: " fmt, driver_name, ## args)
83
84 #define ETH1394_PRINT(level, dev_name, fmt, args...) \
85         printk(level "%s: %s: " fmt, driver_name, dev_name, ## args)
86
87 #define DEBUG(fmt, args...) \
88         printk(KERN_ERR "%s:%s[%d]: " fmt "\n", driver_name, __FUNCTION__, __LINE__, ## args)
89 #define TRACE() printk(KERN_ERR "%s:%s[%d] ---- TRACE\n", driver_name, __FUNCTION__, __LINE__)
90
91 static char version[] __devinitdata =
92         "$Rev: 1247 $ Ben Collins <bcollins@debian.org>";
93
94 struct fragment_info {
95         struct list_head list;
96         int offset;
97         int len;
98 };
99
100 struct partial_datagram {
101         struct list_head list;
102         u16 dgl;
103         u16 dg_size;
104         u16 ether_type;
105         struct sk_buff *skb;
106         char *pbuf;
107         struct list_head frag_info;
108 };
109
110 struct pdg_list {
111         struct list_head list;          /* partial datagram list per node       */
112         unsigned int sz;                /* partial datagram list size per node  */
113         spinlock_t lock;                /* partial datagram lock                */
114 };
115
116 struct eth1394_host_info {
117         struct hpsb_host *host;
118         struct net_device *dev;
119 };
120
121 struct eth1394_node_ref {
122         struct unit_directory *ud;
123         struct list_head list;
124 };
125
126 struct eth1394_node_info {
127         u16 maxpayload;                 /* Max payload                  */
128         u8 sspd;                        /* Max speed                    */
129         u64 fifo;                       /* FIFO address                 */
130         struct pdg_list pdg;            /* partial RX datagram lists    */
131         int dgl;                        /* Outgoing datagram label      */
132 };
133
134 /* Our ieee1394 highlevel driver */
135 #define ETH1394_DRIVER_NAME "eth1394"
136 static const char driver_name[] = ETH1394_DRIVER_NAME;
137
138 static kmem_cache_t *packet_task_cache;
139
140 static struct hpsb_highlevel eth1394_highlevel;
141
142 /* Use common.lf to determine header len */
143 static const int hdr_type_len[] = {
144         sizeof (struct eth1394_uf_hdr),
145         sizeof (struct eth1394_ff_hdr),
146         sizeof (struct eth1394_sf_hdr),
147         sizeof (struct eth1394_sf_hdr)
148 };
149
150 /* Change this to IEEE1394_SPEED_S100 to make testing easier */
151 #define ETH1394_SPEED_DEF       IEEE1394_SPEED_MAX
152
153 /* For now, this needs to be 1500, so that XP works with us */
154 #define ETH1394_DATA_LEN        ETH_DATA_LEN
155
156 static const u16 eth1394_speedto_maxpayload[] = {
157 /*     S100, S200, S400, S800, S1600, S3200 */
158         512, 1024, 2048, 4096,  4096,  4096
159 };
160
161 MODULE_AUTHOR("Ben Collins (bcollins@debian.org)");
162 MODULE_DESCRIPTION("IEEE 1394 IPv4 Driver (IPv4-over-1394 as per RFC 2734)");
163 MODULE_LICENSE("GPL");
164
165 /* The max_partial_datagrams parameter is the maximum number of fragmented
166  * datagrams per node that eth1394 will keep in memory.  Providing an upper
167  * bound allows us to limit the amount of memory that partial datagrams
168  * consume in the event that some partial datagrams are never completed.
169  */
170 static int max_partial_datagrams = 25;
171 module_param(max_partial_datagrams, int, S_IRUGO | S_IWUSR);
172 MODULE_PARM_DESC(max_partial_datagrams,
173                  "Maximum number of partially received fragmented datagrams "
174                  "(default = 25).");
175
176
177 static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
178                             unsigned short type, void *daddr, void *saddr,
179                             unsigned len);
180 static int ether1394_rebuild_header(struct sk_buff *skb);
181 static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr);
182 static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh);
183 static void ether1394_header_cache_update(struct hh_cache *hh,
184                                           struct net_device *dev,
185                                           unsigned char * haddr);
186 static int ether1394_mac_addr(struct net_device *dev, void *p);
187
188 static void purge_partial_datagram(struct list_head *old);
189 static int ether1394_tx(struct sk_buff *skb, struct net_device *dev);
190 static void ether1394_iso(struct hpsb_iso *iso);
191
192 static struct ethtool_ops ethtool_ops;
193
194 static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
195                            quadlet_t *data, u64 addr, size_t len, u16 flags);
196 static void ether1394_add_host (struct hpsb_host *host);
197 static void ether1394_remove_host (struct hpsb_host *host);
198 static void ether1394_host_reset (struct hpsb_host *host);
199
200 /* Function for incoming 1394 packets */
201 static struct hpsb_address_ops addr_ops = {
202         .write =        ether1394_write,
203 };
204
205 /* Ieee1394 highlevel driver functions */
206 static struct hpsb_highlevel eth1394_highlevel = {
207         .name =         driver_name,
208         .add_host =     ether1394_add_host,
209         .remove_host =  ether1394_remove_host,
210         .host_reset =   ether1394_host_reset,
211 };
212
213
214 /* This is called after an "ifup" */
215 static int ether1394_open (struct net_device *dev)
216 {
217         struct eth1394_priv *priv = netdev_priv(dev);
218         int ret = 0;
219
220         /* Something bad happened, don't even try */
221         if (priv->bc_state == ETHER1394_BC_ERROR) {
222                 /* we'll try again */
223                 priv->iso = hpsb_iso_recv_init(priv->host,
224                                                ETHER1394_GASP_BUFFERS * 2 *
225                                                (1 << (priv->host->csr.max_rec +
226                                                       1)),
227                                                ETHER1394_GASP_BUFFERS,
228                                                priv->broadcast_channel,
229                                                HPSB_ISO_DMA_PACKET_PER_BUFFER,
230                                                1, ether1394_iso);
231                 if (priv->iso == NULL) {
232                         ETH1394_PRINT(KERN_ERR, dev->name,
233                                       "Could not allocate isochronous receive "
234                                       "context for the broadcast channel\n");
235                         priv->bc_state = ETHER1394_BC_ERROR;
236                         ret = -EAGAIN;
237                 } else {
238                         if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0)
239                                 priv->bc_state = ETHER1394_BC_STOPPED;
240                         else
241                                 priv->bc_state = ETHER1394_BC_RUNNING;
242                 }
243         }
244
245         if (ret)
246                 return ret;
247
248         netif_start_queue (dev);
249         return 0;
250 }
251
252 /* This is called after an "ifdown" */
253 static int ether1394_stop (struct net_device *dev)
254 {
255         netif_stop_queue (dev);
256         return 0;
257 }
258
259 /* Return statistics to the caller */
260 static struct net_device_stats *ether1394_stats (struct net_device *dev)
261 {
262         return &(((struct eth1394_priv *)netdev_priv(dev))->stats);
263 }
264
265 /* What to do if we timeout. I think a host reset is probably in order, so
266  * that's what we do. Should we increment the stat counters too?  */
267 static void ether1394_tx_timeout (struct net_device *dev)
268 {
269         ETH1394_PRINT (KERN_ERR, dev->name, "Timeout, resetting host %s\n",
270                        ((struct eth1394_priv *)netdev_priv(dev))->host->driver->name);
271
272         highlevel_host_reset (((struct eth1394_priv *)netdev_priv(dev))->host);
273
274         netif_wake_queue (dev);
275 }
276
277 static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
278 {
279         struct eth1394_priv *priv = netdev_priv(dev);
280
281         if ((new_mtu < 68) ||
282             (new_mtu > min(ETH1394_DATA_LEN,
283                            (int)((1 << (priv->host->csr.max_rec + 1)) -
284                                  (sizeof(union eth1394_hdr) +
285                                   ETHER1394_GASP_OVERHEAD)))))
286                 return -EINVAL;
287         dev->mtu = new_mtu;
288         return 0;
289 }
290
291 static void purge_partial_datagram(struct list_head *old)
292 {
293         struct partial_datagram *pd = list_entry(old, struct partial_datagram, list);
294         struct list_head *lh, *n;
295
296         list_for_each_safe(lh, n, &pd->frag_info) {
297                 struct fragment_info *fi = list_entry(lh, struct fragment_info, list);
298                 list_del(lh);
299                 kfree(fi);
300         }
301         list_del(old);
302         kfree_skb(pd->skb);
303         kfree(pd);
304 }
305
306 /******************************************
307  * 1394 bus activity functions
308  ******************************************/
309
310 static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl,
311                                                   struct unit_directory *ud)
312 {
313         struct eth1394_node_ref *node;
314
315         list_for_each_entry(node, inl, list)
316                 if (node->ud == ud)
317                         return node;
318
319         return NULL;
320 }
321
322 static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl,
323                                                        u64 guid)
324 {
325         struct eth1394_node_ref *node;
326
327         list_for_each_entry(node, inl, list)
328                 if (node->ud->ne->guid == guid)
329                         return node;
330
331         return NULL;
332 }
333
334 static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl,
335                                                          nodeid_t nodeid)
336 {
337         struct eth1394_node_ref *node;
338         list_for_each_entry(node, inl, list) {
339                 if (node->ud->ne->nodeid == nodeid)
340                         return node;
341         }
342
343         return NULL;
344 }
345
346 static int eth1394_probe(struct device *dev)
347 {
348         struct unit_directory *ud;
349         struct eth1394_host_info *hi;
350         struct eth1394_priv *priv;
351         struct eth1394_node_ref *new_node;
352         struct eth1394_node_info *node_info;
353
354         ud = container_of(dev, struct unit_directory, device);
355
356         hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
357         if (!hi)
358                 return -ENOENT;
359
360         new_node = kmalloc(sizeof(struct eth1394_node_ref),
361                            in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
362         if (!new_node)
363                 return -ENOMEM;
364
365         node_info = kmalloc(sizeof(struct eth1394_node_info),
366                             in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
367         if (!node_info) {
368                 kfree(new_node);
369                 return -ENOMEM;
370         }
371
372         spin_lock_init(&node_info->pdg.lock);
373         INIT_LIST_HEAD(&node_info->pdg.list);
374         node_info->pdg.sz = 0;
375         node_info->fifo = ETHER1394_INVALID_ADDR;
376
377         ud->device.driver_data = node_info;
378         new_node->ud = ud;
379
380         priv = netdev_priv(hi->dev);
381         list_add_tail(&new_node->list, &priv->ip_node_list);
382
383         return 0;
384 }
385
386 static int eth1394_remove(struct device *dev)
387 {
388         struct unit_directory *ud;
389         struct eth1394_host_info *hi;
390         struct eth1394_priv *priv;
391         struct eth1394_node_ref *old_node;
392         struct eth1394_node_info *node_info;
393         struct list_head *lh, *n;
394         unsigned long flags;
395
396         ud = container_of(dev, struct unit_directory, device);
397         hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
398         if (!hi)
399                 return -ENOENT;
400
401         priv = netdev_priv(hi->dev);
402
403         old_node = eth1394_find_node(&priv->ip_node_list, ud);
404
405         if (old_node) {
406                 list_del(&old_node->list);
407                 kfree(old_node);
408
409                 node_info = (struct eth1394_node_info*)ud->device.driver_data;
410
411                 spin_lock_irqsave(&node_info->pdg.lock, flags);
412                 /* The partial datagram list should be empty, but we'll just
413                  * make sure anyway... */
414                 list_for_each_safe(lh, n, &node_info->pdg.list) {
415                         purge_partial_datagram(lh);
416                 }
417                 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
418
419                 kfree(node_info);
420                 ud->device.driver_data = NULL;
421         }
422         return 0;
423 }
424
425 static int eth1394_update(struct unit_directory *ud)
426 {
427         struct eth1394_host_info *hi;
428         struct eth1394_priv *priv;
429         struct eth1394_node_ref *node;
430         struct eth1394_node_info *node_info;
431
432         hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
433         if (!hi)
434                 return -ENOENT;
435
436         priv = netdev_priv(hi->dev);
437
438         node = eth1394_find_node(&priv->ip_node_list, ud);
439
440         if (!node) {
441                 node = kmalloc(sizeof(struct eth1394_node_ref),
442                                in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
443                 if (!node)
444                         return -ENOMEM;
445
446                 node_info = kmalloc(sizeof(struct eth1394_node_info),
447                                     in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
448                 if (!node_info) {
449                         kfree(node);
450                         return -ENOMEM;
451                 }
452
453                 spin_lock_init(&node_info->pdg.lock);
454                 INIT_LIST_HEAD(&node_info->pdg.list);
455                 node_info->pdg.sz = 0;
456
457                 ud->device.driver_data = node_info;
458                 node->ud = ud;
459
460                 priv = netdev_priv(hi->dev);
461                 list_add_tail(&node->list, &priv->ip_node_list);
462         }
463
464         return 0;
465 }
466
467
468 static struct ieee1394_device_id eth1394_id_table[] = {
469         {
470                 .match_flags = (IEEE1394_MATCH_SPECIFIER_ID |
471                                 IEEE1394_MATCH_VERSION),
472                 .specifier_id = ETHER1394_GASP_SPECIFIER_ID,
473                 .version = ETHER1394_GASP_VERSION,
474         },
475         {}
476 };
477
478 MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table);
479
480 static struct hpsb_protocol_driver eth1394_proto_driver = {
481         .name           = "IPv4 over 1394 Driver",
482         .id_table       = eth1394_id_table,
483         .update         = eth1394_update,
484         .driver         = {
485                 .name           = ETH1394_DRIVER_NAME,
486                 .bus            = &ieee1394_bus_type,
487                 .probe          = eth1394_probe,
488                 .remove         = eth1394_remove,
489         },
490 };
491
492
493 static void ether1394_reset_priv (struct net_device *dev, int set_mtu)
494 {
495         unsigned long flags;
496         int i;
497         struct eth1394_priv *priv = netdev_priv(dev);
498         struct hpsb_host *host = priv->host;
499         u64 guid = *((u64*)&(host->csr.rom->bus_info_data[3]));
500         u16 maxpayload = 1 << (host->csr.max_rec + 1);
501         int max_speed = IEEE1394_SPEED_MAX;
502
503         spin_lock_irqsave (&priv->lock, flags);
504
505         memset(priv->ud_list, 0, sizeof(struct node_entry*) * ALL_NODES);
506         priv->bc_maxpayload = 512;
507
508         /* Determine speed limit */
509         for (i = 0; i < host->node_count; i++)
510                 if (max_speed > host->speed_map[NODEID_TO_NODE(host->node_id) *
511                                                 64 + i])
512                         max_speed = host->speed_map[NODEID_TO_NODE(host->node_id) *
513                                                     64 + i];
514         priv->bc_sspd = max_speed;
515
516         /* We'll use our maxpayload as the default mtu */
517         if (set_mtu) {
518                 dev->mtu = min(ETH1394_DATA_LEN,
519                                (int)(maxpayload -
520                                      (sizeof(union eth1394_hdr) +
521                                       ETHER1394_GASP_OVERHEAD)));
522
523                 /* Set our hardware address while we're at it */
524                 *(u64*)dev->dev_addr = guid;
525                 *(u64*)dev->broadcast = ~0x0ULL;
526         }
527
528         spin_unlock_irqrestore (&priv->lock, flags);
529 }
530
531 /* This function is called right before register_netdev */
532 static void ether1394_init_dev (struct net_device *dev)
533 {
534         /* Our functions */
535         dev->open               = ether1394_open;
536         dev->stop               = ether1394_stop;
537         dev->hard_start_xmit    = ether1394_tx;
538         dev->get_stats          = ether1394_stats;
539         dev->tx_timeout         = ether1394_tx_timeout;
540         dev->change_mtu         = ether1394_change_mtu;
541
542         dev->hard_header        = ether1394_header;
543         dev->rebuild_header     = ether1394_rebuild_header;
544         dev->hard_header_cache  = ether1394_header_cache;
545         dev->header_cache_update= ether1394_header_cache_update;
546         dev->hard_header_parse  = ether1394_header_parse;
547         dev->set_mac_address    = ether1394_mac_addr;
548         SET_ETHTOOL_OPS(dev, &ethtool_ops);
549
550         /* Some constants */
551         dev->watchdog_timeo     = ETHER1394_TIMEOUT;
552         dev->flags              = IFF_BROADCAST | IFF_MULTICAST;
553         dev->features           = NETIF_F_HIGHDMA;
554         dev->addr_len           = ETH1394_ALEN;
555         dev->hard_header_len    = ETH1394_HLEN;
556         dev->type               = ARPHRD_IEEE1394;
557
558         ether1394_reset_priv (dev, 1);
559 }
560
561 /*
562  * This function is called every time a card is found. It is generally called
563  * when the module is installed. This is where we add all of our ethernet
564  * devices. One for each host.
565  */
566 static void ether1394_add_host (struct hpsb_host *host)
567 {
568         struct eth1394_host_info *hi = NULL;
569         struct net_device *dev = NULL;
570         struct eth1394_priv *priv;
571         static int version_printed = 0;
572         u64 fifo_addr;
573
574         if (!(host->config_roms & HPSB_CONFIG_ROM_ENTRY_IP1394))
575                 return;
576
577         fifo_addr = hpsb_allocate_and_register_addrspace(&eth1394_highlevel,
578                                                          host,
579                                                          &addr_ops,
580                                                          ETHER1394_REGION_ADDR_LEN,
581                                                          ETHER1394_REGION_ADDR_LEN,
582                                                          -1, -1);
583         if (fifo_addr == ~0ULL)
584                 goto out;
585
586         if (version_printed++ == 0)
587                 ETH1394_PRINT_G (KERN_INFO, "%s\n", version);
588
589         /* We should really have our own alloc_hpsbdev() function in
590          * net_init.c instead of calling the one for ethernet then hijacking
591          * it for ourselves.  That way we'd be a real networking device. */
592         dev = alloc_etherdev(sizeof (struct eth1394_priv));
593
594         if (dev == NULL) {
595                 ETH1394_PRINT_G (KERN_ERR, "Out of memory trying to allocate "
596                                  "etherdevice for IEEE 1394 device %s-%d\n",
597                                  host->driver->name, host->id);
598                 goto out;
599         }
600
601         SET_MODULE_OWNER(dev);
602         SET_NETDEV_DEV(dev, &host->device);
603
604         priv = netdev_priv(dev);
605
606         INIT_LIST_HEAD(&priv->ip_node_list);
607
608         spin_lock_init(&priv->lock);
609         priv->host = host;
610         priv->local_fifo = fifo_addr;
611
612         hi = hpsb_create_hostinfo(&eth1394_highlevel, host, sizeof(*hi));
613
614         if (hi == NULL) {
615                 ETH1394_PRINT_G (KERN_ERR, "Out of memory trying to create "
616                                  "hostinfo for IEEE 1394 device %s-%d\n",
617                                  host->driver->name, host->id);
618                 goto out;
619         }
620
621         ether1394_init_dev(dev);
622
623         if (register_netdev (dev)) {
624                 ETH1394_PRINT (KERN_ERR, dev->name, "Error registering network driver\n");
625                 goto out;
626         }
627
628         ETH1394_PRINT (KERN_INFO, dev->name, "IEEE-1394 IPv4 over 1394 Ethernet (fw-host%d)\n",
629                        host->id);
630
631         hi->host = host;
632         hi->dev = dev;
633
634         /* Ignore validity in hopes that it will be set in the future.  It'll
635          * be checked when the eth device is opened. */
636         priv->broadcast_channel = host->csr.broadcast_channel & 0x3f;
637
638         priv->iso = hpsb_iso_recv_init(host, (ETHER1394_GASP_BUFFERS * 2 *
639                                               (1 << (host->csr.max_rec + 1))),
640                                        ETHER1394_GASP_BUFFERS,
641                                        priv->broadcast_channel,
642                                        HPSB_ISO_DMA_PACKET_PER_BUFFER,
643                                        1, ether1394_iso);
644         if (priv->iso == NULL) {
645                 ETH1394_PRINT(KERN_ERR, dev->name,
646                               "Could not allocate isochronous receive context "
647                               "for the broadcast channel\n");
648                 priv->bc_state = ETHER1394_BC_ERROR;
649         } else {
650                 if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0)
651                         priv->bc_state = ETHER1394_BC_STOPPED;
652                 else
653                         priv->bc_state = ETHER1394_BC_RUNNING;
654         }
655
656         return;
657
658 out:
659         if (dev != NULL)
660                 free_netdev(dev);
661         if (hi)
662                 hpsb_destroy_hostinfo(&eth1394_highlevel, host);
663
664         return;
665 }
666
667 /* Remove a card from our list */
668 static void ether1394_remove_host (struct hpsb_host *host)
669 {
670         struct eth1394_host_info *hi;
671
672         hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
673         if (hi != NULL) {
674                 struct eth1394_priv *priv = netdev_priv(hi->dev);
675
676                 hpsb_unregister_addrspace(&eth1394_highlevel, host,
677                                           priv->local_fifo);
678
679                 if (priv->iso != NULL)
680                         hpsb_iso_shutdown(priv->iso);
681
682                 if (hi->dev) {
683                         unregister_netdev (hi->dev);
684                         free_netdev(hi->dev);
685                 }
686         }
687
688         return;
689 }
690
691 /* A reset has just arisen */
692 static void ether1394_host_reset (struct hpsb_host *host)
693 {
694         struct eth1394_host_info *hi;
695         struct eth1394_priv *priv;
696         struct net_device *dev;
697         struct list_head *lh, *n;
698         struct eth1394_node_ref *node;
699         struct eth1394_node_info *node_info;
700         unsigned long flags;
701
702         hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
703
704         /* This can happen for hosts that we don't use */
705         if (hi == NULL)
706                 return;
707
708         dev = hi->dev;
709         priv = netdev_priv(dev);
710
711         /* Reset our private host data, but not our mtu */
712         netif_stop_queue (dev);
713         ether1394_reset_priv (dev, 0);
714
715         list_for_each_entry(node, &priv->ip_node_list, list) {
716                 node_info = (struct eth1394_node_info*)node->ud->device.driver_data;
717
718                 spin_lock_irqsave(&node_info->pdg.lock, flags);
719
720                 list_for_each_safe(lh, n, &node_info->pdg.list) {
721                         purge_partial_datagram(lh);
722                 }
723
724                 INIT_LIST_HEAD(&(node_info->pdg.list));
725                 node_info->pdg.sz = 0;
726
727                 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
728         }
729
730         netif_wake_queue (dev);
731 }
732
733 /******************************************
734  * HW Header net device functions
735  ******************************************/
736 /* These functions have been adapted from net/ethernet/eth.c */
737
738
739 /* Create a fake MAC header for an arbitrary protocol layer.
740  * saddr=NULL means use device source address
741  * daddr=NULL means leave destination address (eg unresolved arp). */
742 static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
743                             unsigned short type, void *daddr, void *saddr,
744                             unsigned len)
745 {
746         struct eth1394hdr *eth = (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN);
747
748         eth->h_proto = htons(type);
749
750         if (dev->flags & (IFF_LOOPBACK|IFF_NOARP)) {
751                 memset(eth->h_dest, 0, dev->addr_len);
752                 return(dev->hard_header_len);
753         }
754
755         if (daddr) {
756                 memcpy(eth->h_dest,daddr,dev->addr_len);
757                 return dev->hard_header_len;
758         }
759
760         return -dev->hard_header_len;
761
762 }
763
764
765 /* Rebuild the faked MAC header. This is called after an ARP
766  * (or in future other address resolution) has completed on this
767  * sk_buff. We now let ARP fill in the other fields.
768  *
769  * This routine CANNOT use cached dst->neigh!
770  * Really, it is used only when dst->neigh is wrong.
771  */
772 static int ether1394_rebuild_header(struct sk_buff *skb)
773 {
774         struct eth1394hdr *eth = (struct eth1394hdr *)skb->data;
775         struct net_device *dev = skb->dev;
776
777         switch (eth->h_proto) {
778
779 #ifdef CONFIG_INET
780         case __constant_htons(ETH_P_IP):
781                 return arp_find((unsigned char*)&eth->h_dest, skb);
782 #endif
783         default:
784                 ETH1394_PRINT(KERN_DEBUG, dev->name,
785                               "unable to resolve type %04x addresses.\n",
786                               eth->h_proto);
787                 break;
788         }
789
790         return 0;
791 }
792
793 static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr)
794 {
795         struct net_device *dev = skb->dev;
796         memcpy(haddr, dev->dev_addr, ETH1394_ALEN);
797         return ETH1394_ALEN;
798 }
799
800
801 static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh)
802 {
803         unsigned short type = hh->hh_type;
804         struct eth1394hdr *eth = (struct eth1394hdr*)(((u8*)hh->hh_data) +
805                                                       (16 - ETH1394_HLEN));
806         struct net_device *dev = neigh->dev;
807
808         if (type == __constant_htons(ETH_P_802_3)) {
809                 return -1;
810         }
811
812         eth->h_proto = type;
813         memcpy(eth->h_dest, neigh->ha, dev->addr_len);
814
815         hh->hh_len = ETH1394_HLEN;
816         return 0;
817 }
818
819 /* Called by Address Resolution module to notify changes in address. */
820 static void ether1394_header_cache_update(struct hh_cache *hh,
821                                           struct net_device *dev,
822                                           unsigned char * haddr)
823 {
824         memcpy(((u8*)hh->hh_data) + (16 - ETH1394_HLEN), haddr, dev->addr_len);
825 }
826
827 static int ether1394_mac_addr(struct net_device *dev, void *p)
828 {
829         if (netif_running(dev))
830                 return -EBUSY;
831
832         /* Not going to allow setting the MAC address, we really need to use
833          * the real one supplied by the hardware */
834          return -EINVAL;
835  }
836
837
838
839 /******************************************
840  * Datagram reception code
841  ******************************************/
842
843 /* Copied from net/ethernet/eth.c */
844 static inline u16 ether1394_type_trans(struct sk_buff *skb,
845                                        struct net_device *dev)
846 {
847         struct eth1394hdr *eth;
848         unsigned char *rawp;
849
850         skb->mac.raw = skb->data;
851         skb_pull (skb, ETH1394_HLEN);
852         eth = eth1394_hdr(skb);
853
854         if (*eth->h_dest & 1) {
855                 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len)==0)
856                         skb->pkt_type = PACKET_BROADCAST;
857 #if 0
858                 else
859                         skb->pkt_type = PACKET_MULTICAST;
860 #endif
861         } else {
862                 if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
863                         skb->pkt_type = PACKET_OTHERHOST;
864         }
865
866         if (ntohs (eth->h_proto) >= 1536)
867                 return eth->h_proto;
868
869         rawp = skb->data;
870
871         if (*(unsigned short *)rawp == 0xFFFF)
872                 return htons (ETH_P_802_3);
873
874         return htons (ETH_P_802_2);
875 }
876
877 /* Parse an encapsulated IP1394 header into an ethernet frame packet.
878  * We also perform ARP translation here, if need be.  */
879 static inline u16 ether1394_parse_encap(struct sk_buff *skb,
880                                         struct net_device *dev,
881                                         nodeid_t srcid, nodeid_t destid,
882                                         u16 ether_type)
883 {
884         struct eth1394_priv *priv = netdev_priv(dev);
885         u64 dest_hw;
886         unsigned short ret = 0;
887
888         /* Setup our hw addresses. We use these to build the
889          * ethernet header.  */
890         if (destid == (LOCAL_BUS | ALL_NODES))
891                 dest_hw = ~0ULL;  /* broadcast */
892         else
893                 dest_hw = cpu_to_be64((((u64)priv->host->csr.guid_hi) << 32) |
894                                       priv->host->csr.guid_lo);
895
896         /* If this is an ARP packet, convert it. First, we want to make
897          * use of some of the fields, since they tell us a little bit
898          * about the sending machine.  */
899         if (ether_type == __constant_htons (ETH_P_ARP)) {
900                 struct eth1394_arp *arp1394 = (struct eth1394_arp*)skb->data;
901                 struct arphdr *arp = (struct arphdr *)skb->data;
902                 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
903                 u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
904                         ntohl(arp1394->fifo_lo);
905                 u8 max_rec = min(priv->host->csr.max_rec,
906                                  (u8)(arp1394->max_rec));
907                 int sspd = arp1394->sspd;
908                 u16 maxpayload;
909                 struct eth1394_node_ref *node;
910                 struct eth1394_node_info *node_info;
911
912                 /* Sanity check. MacOSX seems to be sending us 131 in this
913                  * field (atleast on my Panther G5). Not sure why. */
914                 if (sspd > 5 || sspd < 0)
915                         sspd = 0;
916
917                 maxpayload = min(eth1394_speedto_maxpayload[sspd], (u16)(1 << (max_rec + 1)));
918
919                 node = eth1394_find_node_guid(&priv->ip_node_list,
920                                               be64_to_cpu(arp1394->s_uniq_id));
921                 if (!node) {
922                         return 0;
923                 }
924
925                 node_info = (struct eth1394_node_info*)node->ud->device.driver_data;
926
927                 /* Update our speed/payload/fifo_offset table */
928                 node_info->maxpayload = maxpayload;
929                 node_info->sspd =       sspd;
930                 node_info->fifo =       fifo_addr;
931
932                 /* Now that we're done with the 1394 specific stuff, we'll
933                  * need to alter some of the data.  Believe it or not, all
934                  * that needs to be done is sender_IP_address needs to be
935                  * moved, the destination hardware address get stuffed
936                  * in and the hardware address length set to 8.
937                  *
938                  * IMPORTANT: The code below overwrites 1394 specific data
939                  * needed above so keep the munging of the data for the
940                  * higher level IP stack last. */
941
942                 arp->ar_hln = 8;
943                 arp_ptr += arp->ar_hln;         /* skip over sender unique id */
944                 *(u32*)arp_ptr = arp1394->sip;  /* move sender IP addr */
945                 arp_ptr += arp->ar_pln;         /* skip over sender IP addr */
946
947                 if (arp->ar_op == 1)
948                         /* just set ARP req target unique ID to 0 */
949                         *((u64*)arp_ptr) = 0;
950                 else
951                         *((u64*)arp_ptr) = *((u64*)dev->dev_addr);
952         }
953
954         /* Now add the ethernet header. */
955         if (dev->hard_header (skb, dev, __constant_ntohs (ether_type),
956                               &dest_hw, NULL, skb->len) >= 0)
957                 ret = ether1394_type_trans(skb, dev);
958
959         return ret;
960 }
961
962 static inline int fragment_overlap(struct list_head *frag_list, int offset, int len)
963 {
964         struct fragment_info *fi;
965
966         list_for_each_entry(fi, frag_list, list) {
967                 if ( ! ((offset > (fi->offset + fi->len - 1)) ||
968                        ((offset + len - 1) < fi->offset)))
969                         return 1;
970         }
971         return 0;
972 }
973
974 static inline struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
975 {
976         struct partial_datagram *pd;
977
978         list_for_each_entry(pd, pdgl, list) {
979                 if (pd->dgl == dgl)
980                         return &pd->list;
981         }
982         return NULL;
983 }
984
985 /* Assumes that new fragment does not overlap any existing fragments */
986 static inline int new_fragment(struct list_head *frag_info, int offset, int len)
987 {
988         struct list_head *lh;
989         struct fragment_info *fi, *fi2, *new;
990
991         list_for_each(lh, frag_info) {
992                 fi = list_entry(lh, struct fragment_info, list);
993                 if ((fi->offset + fi->len) == offset) {
994                         /* The new fragment can be tacked on to the end */
995                         fi->len += len;
996                         /* Did the new fragment plug a hole? */
997                         fi2 = list_entry(lh->next, struct fragment_info, list);
998                         if ((fi->offset + fi->len) == fi2->offset) {
999                                 /* glue fragments together */
1000                                 fi->len += fi2->len;
1001                                 list_del(lh->next);
1002                                 kfree(fi2);
1003                         }
1004                         return 0;
1005                 } else if ((offset + len) == fi->offset) {
1006                         /* The new fragment can be tacked on to the beginning */
1007                         fi->offset = offset;
1008                         fi->len += len;
1009                         /* Did the new fragment plug a hole? */
1010                         fi2 = list_entry(lh->prev, struct fragment_info, list);
1011                         if ((fi2->offset + fi2->len) == fi->offset) {
1012                                 /* glue fragments together */
1013                                 fi2->len += fi->len;
1014                                 list_del(lh);
1015                                 kfree(fi);
1016                         }
1017                         return 0;
1018                 } else if (offset > (fi->offset + fi->len)) {
1019                         break;
1020                 } else if ((offset + len) < fi->offset) {
1021                         lh = lh->prev;
1022                         break;
1023                 }
1024         }
1025
1026         new = kmalloc(sizeof(struct fragment_info), GFP_ATOMIC);
1027         if (!new)
1028                 return -ENOMEM;
1029
1030         new->offset = offset;
1031         new->len = len;
1032
1033         list_add(&new->list, lh);
1034
1035         return 0;
1036 }
1037
1038 static inline int new_partial_datagram(struct net_device *dev,
1039                                        struct list_head *pdgl, int dgl,
1040                                        int dg_size, char *frag_buf,
1041                                        int frag_off, int frag_len)
1042 {
1043         struct partial_datagram *new;
1044
1045         new = kmalloc(sizeof(struct partial_datagram), GFP_ATOMIC);
1046         if (!new)
1047                 return -ENOMEM;
1048
1049         INIT_LIST_HEAD(&new->frag_info);
1050
1051         if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) {
1052                 kfree(new);
1053                 return -ENOMEM;
1054         }
1055
1056         new->dgl = dgl;
1057         new->dg_size = dg_size;
1058
1059         new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15);
1060         if (!new->skb) {
1061                 struct fragment_info *fi = list_entry(new->frag_info.next,
1062                                                       struct fragment_info,
1063                                                       list);
1064                 kfree(fi);
1065                 kfree(new);
1066                 return -ENOMEM;
1067         }
1068
1069         skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15);
1070         new->pbuf = skb_put(new->skb, dg_size);
1071         memcpy(new->pbuf + frag_off, frag_buf, frag_len);
1072
1073         list_add(&new->list, pdgl);
1074
1075         return 0;
1076 }
1077
1078 static inline int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
1079                                           char *frag_buf, int frag_off, int frag_len)
1080 {
1081         struct partial_datagram *pd = list_entry(lh, struct partial_datagram, list);
1082
1083         if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0) {
1084                 return -ENOMEM;
1085         }
1086
1087         memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
1088
1089         /* Move list entry to beginnig of list so that oldest partial
1090          * datagrams percolate to the end of the list */
1091         list_del(lh);
1092         list_add(lh, pdgl);
1093
1094         return 0;
1095 }
1096
1097 static inline int is_datagram_complete(struct list_head *lh, int dg_size)
1098 {
1099         struct partial_datagram *pd = list_entry(lh, struct partial_datagram, list);
1100         struct fragment_info *fi = list_entry(pd->frag_info.next,
1101                                               struct fragment_info, list);
1102
1103         return (fi->len == dg_size);
1104 }
1105
1106 /* Packet reception. We convert the IP1394 encapsulation header to an
1107  * ethernet header, and fill it with some of our other fields. This is
1108  * an incoming packet from the 1394 bus.  */
1109 static int ether1394_data_handler(struct net_device *dev, int srcid, int destid,
1110                                   char *buf, int len)
1111 {
1112         struct sk_buff *skb;
1113         unsigned long flags;
1114         struct eth1394_priv *priv = netdev_priv(dev);
1115         union eth1394_hdr *hdr = (union eth1394_hdr *)buf;
1116         u16 ether_type = 0;  /* initialized to clear warning */
1117         int hdr_len;
1118         struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)];
1119         struct eth1394_node_info *node_info;
1120
1121         if (!ud) {
1122                 struct eth1394_node_ref *node;
1123                 node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid);
1124                 if (!node) {
1125                         HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid "
1126                                    "lookup failure: " NODE_BUS_FMT,
1127                                    NODE_BUS_ARGS(priv->host, srcid));
1128                         priv->stats.rx_dropped++;
1129                         return -1;
1130                 }
1131                 ud = node->ud;
1132
1133                 priv->ud_list[NODEID_TO_NODE(srcid)] = ud;
1134         }
1135
1136         node_info = (struct eth1394_node_info*)ud->device.driver_data;
1137
1138         /* First, did we receive a fragmented or unfragmented datagram? */
1139         hdr->words.word1 = ntohs(hdr->words.word1);
1140
1141         hdr_len = hdr_type_len[hdr->common.lf];
1142
1143         if (hdr->common.lf == ETH1394_HDR_LF_UF) {
1144                 /* An unfragmented datagram has been received by the ieee1394
1145                  * bus. Build an skbuff around it so we can pass it to the
1146                  * high level network layer. */
1147
1148                 skb = dev_alloc_skb(len + dev->hard_header_len + 15);
1149                 if (!skb) {
1150                         HPSB_PRINT (KERN_ERR, "ether1394 rx: low on mem\n");
1151                         priv->stats.rx_dropped++;
1152                         return -1;
1153                 }
1154                 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
1155                 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len, len - hdr_len);
1156                 ether_type = hdr->uf.ether_type;
1157         } else {
1158                 /* A datagram fragment has been received, now the fun begins. */
1159
1160                 struct list_head *pdgl, *lh;
1161                 struct partial_datagram *pd;
1162                 int fg_off;
1163                 int fg_len = len - hdr_len;
1164                 int dg_size;
1165                 int dgl;
1166                 int retval;
1167                 struct pdg_list *pdg = &(node_info->pdg);
1168
1169                 hdr->words.word3 = ntohs(hdr->words.word3);
1170                 /* The 4th header word is reserved so no need to do ntohs() */
1171
1172                 if (hdr->common.lf == ETH1394_HDR_LF_FF) {
1173                         ether_type = hdr->ff.ether_type;
1174                         dgl = hdr->ff.dgl;
1175                         dg_size = hdr->ff.dg_size + 1;
1176                         fg_off = 0;
1177                 } else {
1178                         hdr->words.word2 = ntohs(hdr->words.word2);
1179                         dgl = hdr->sf.dgl;
1180                         dg_size = hdr->sf.dg_size + 1;
1181                         fg_off = hdr->sf.fg_off;
1182                 }
1183                 spin_lock_irqsave(&pdg->lock, flags);
1184
1185                 pdgl = &(pdg->list);
1186                 lh = find_partial_datagram(pdgl, dgl);
1187
1188                 if (lh == NULL) {
1189                         while (pdg->sz >= max_partial_datagrams) {
1190                                 /* remove the oldest */
1191                                 purge_partial_datagram(pdgl->prev);
1192                                 pdg->sz--;
1193                         }
1194
1195                         retval = new_partial_datagram(dev, pdgl, dgl, dg_size,
1196                                                       buf + hdr_len, fg_off,
1197                                                       fg_len);
1198                         if (retval < 0) {
1199                                 spin_unlock_irqrestore(&pdg->lock, flags);
1200                                 goto bad_proto;
1201                         }
1202                         pdg->sz++;
1203                         lh = find_partial_datagram(pdgl, dgl);
1204                 } else {
1205                         struct partial_datagram *pd;
1206
1207                         pd = list_entry(lh, struct partial_datagram, list);
1208
1209                         if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) {
1210                                 /* Overlapping fragments, obliterate old
1211                                  * datagram and start new one. */
1212                                 purge_partial_datagram(lh);
1213                                 retval = new_partial_datagram(dev, pdgl, dgl,
1214                                                               dg_size,
1215                                                               buf + hdr_len,
1216                                                               fg_off, fg_len);
1217                                 if (retval < 0) {
1218                                         pdg->sz--;
1219                                         spin_unlock_irqrestore(&pdg->lock, flags);
1220                                         goto bad_proto;
1221                                 }
1222                         } else {
1223                                 retval = update_partial_datagram(pdgl, lh,
1224                                                                  buf + hdr_len,
1225                                                                  fg_off, fg_len);
1226                                 if (retval < 0) {
1227                                         /* Couldn't save off fragment anyway
1228                                          * so might as well obliterate the
1229                                          * datagram now. */
1230                                         purge_partial_datagram(lh);
1231                                         pdg->sz--;
1232                                         spin_unlock_irqrestore(&pdg->lock, flags);
1233                                         goto bad_proto;
1234                                 }
1235                         } /* fragment overlap */
1236                 } /* new datagram or add to existing one */
1237
1238                 pd = list_entry(lh, struct partial_datagram, list);
1239
1240                 if (hdr->common.lf == ETH1394_HDR_LF_FF) {
1241                         pd->ether_type = ether_type;
1242                 }
1243
1244                 if (is_datagram_complete(lh, dg_size)) {
1245                         ether_type = pd->ether_type;
1246                         pdg->sz--;
1247                         skb = skb_get(pd->skb);
1248                         purge_partial_datagram(lh);
1249                         spin_unlock_irqrestore(&pdg->lock, flags);
1250                 } else {
1251                         /* Datagram is not complete, we're done for the
1252                          * moment. */
1253                         spin_unlock_irqrestore(&pdg->lock, flags);
1254                         return 0;
1255                 }
1256         } /* unframgented datagram or fragmented one */
1257
1258         /* Write metadata, and then pass to the receive level */
1259         skb->dev = dev;
1260         skb->ip_summed = CHECKSUM_UNNECESSARY;  /* don't check it */
1261
1262         /* Parse the encapsulation header. This actually does the job of
1263          * converting to an ethernet frame header, aswell as arp
1264          * conversion if needed. ARP conversion is easier in this
1265          * direction, since we are using ethernet as our backend.  */
1266         skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid,
1267                                               ether_type);
1268
1269
1270         spin_lock_irqsave(&priv->lock, flags);
1271         if (!skb->protocol) {
1272                 priv->stats.rx_errors++;
1273                 priv->stats.rx_dropped++;
1274                 dev_kfree_skb_any(skb);
1275                 goto bad_proto;
1276         }
1277
1278         if (netif_rx(skb) == NET_RX_DROP) {
1279                 priv->stats.rx_errors++;
1280                 priv->stats.rx_dropped++;
1281                 goto bad_proto;
1282         }
1283
1284         /* Statistics */
1285         priv->stats.rx_packets++;
1286         priv->stats.rx_bytes += skb->len;
1287
1288 bad_proto:
1289         if (netif_queue_stopped(dev))
1290                 netif_wake_queue(dev);
1291         spin_unlock_irqrestore(&priv->lock, flags);
1292
1293         dev->last_rx = jiffies;
1294
1295         return 0;
1296 }
1297
1298 static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
1299                            quadlet_t *data, u64 addr, size_t len, u16 flags)
1300 {
1301         struct eth1394_host_info *hi;
1302
1303         hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
1304         if (hi == NULL) {
1305                 ETH1394_PRINT_G(KERN_ERR, "Could not find net device for host %s\n",
1306                                 host->driver->name);
1307                 return RCODE_ADDRESS_ERROR;
1308         }
1309
1310         if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len))
1311                 return RCODE_ADDRESS_ERROR;
1312         else
1313                 return RCODE_COMPLETE;
1314 }
1315
1316 static void ether1394_iso(struct hpsb_iso *iso)
1317 {
1318         quadlet_t *data;
1319         char *buf;
1320         struct eth1394_host_info *hi;
1321         struct net_device *dev;
1322         struct eth1394_priv *priv;
1323         unsigned int len;
1324         u32 specifier_id;
1325         u16 source_id;
1326         int i;
1327         int nready;
1328
1329         hi = hpsb_get_hostinfo(&eth1394_highlevel, iso->host);
1330         if (hi == NULL) {
1331                 ETH1394_PRINT_G(KERN_ERR, "Could not find net device for host %s\n",
1332                                 iso->host->driver->name);
1333                 return;
1334         }
1335
1336         dev = hi->dev;
1337
1338         nready = hpsb_iso_n_ready(iso);
1339         for (i = 0; i < nready; i++) {
1340                 struct hpsb_iso_packet_info *info =
1341                         &iso->infos[(iso->first_packet + i) % iso->buf_packets];
1342                 data = (quadlet_t*) (iso->data_buf.kvirt + info->offset);
1343
1344                 /* skip over GASP header */
1345                 buf = (char *)data + 8;
1346                 len = info->len - 8;
1347
1348                 specifier_id = (((be32_to_cpu(data[0]) & 0xffff) << 8) |
1349                                 ((be32_to_cpu(data[1]) & 0xff000000) >> 24));
1350                 source_id = be32_to_cpu(data[0]) >> 16;
1351
1352                 priv = netdev_priv(dev);
1353
1354                 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f) ||
1355                    specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
1356                         /* This packet is not for us */
1357                         continue;
1358                 }
1359                 ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES,
1360                                        buf, len);
1361         }
1362
1363         hpsb_iso_recv_release_packets(iso, i);
1364
1365         dev->last_rx = jiffies;
1366 }
1367
1368 /******************************************
1369  * Datagram transmission code
1370  ******************************************/
1371
1372 /* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire
1373  * arphdr) is the same format as the ip1394 header, so they overlap.  The rest
1374  * needs to be munged a bit.  The remainder of the arphdr is formatted based
1375  * on hwaddr len and ipaddr len.  We know what they'll be, so it's easy to
1376  * judge.
1377  *
1378  * Now that the EUI is used for the hardware address all we need to do to make
1379  * this work for 1394 is to insert 2 quadlets that contain max_rec size,
1380  * speed, and unicast FIFO address information between the sender_unique_id
1381  * and the IP addresses.
1382  */
1383 static inline void ether1394_arp_to_1394arp(struct sk_buff *skb,
1384                                             struct net_device *dev)
1385 {
1386         struct eth1394_priv *priv = netdev_priv(dev);
1387
1388         struct arphdr *arp = (struct arphdr *)skb->data;
1389         unsigned char *arp_ptr = (unsigned char *)(arp + 1);
1390         struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
1391
1392         /* Believe it or not, all that need to happen is sender IP get moved
1393          * and set hw_addr_len, max_rec, sspd, fifo_hi and fifo_lo.  */
1394         arp1394->hw_addr_len    = 16;
1395         arp1394->sip            = *(u32*)(arp_ptr + ETH1394_ALEN);
1396         arp1394->max_rec        = priv->host->csr.max_rec;
1397         arp1394->sspd           = priv->host->csr.lnk_spd;
1398         arp1394->fifo_hi        = htons (priv->local_fifo >> 32);
1399         arp1394->fifo_lo        = htonl (priv->local_fifo & ~0x0);
1400
1401         return;
1402 }
1403
1404 /* We need to encapsulate the standard header with our own. We use the
1405  * ethernet header's proto for our own. */
1406 static inline unsigned int ether1394_encapsulate_prep(unsigned int max_payload,
1407                                                       int proto,
1408                                                       union eth1394_hdr *hdr,
1409                                                       u16 dg_size, u16 dgl)
1410 {
1411         unsigned int adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
1412
1413         /* Does it all fit in one packet? */
1414         if (dg_size <= adj_max_payload) {
1415                 hdr->uf.lf = ETH1394_HDR_LF_UF;
1416                 hdr->uf.ether_type = proto;
1417         } else {
1418                 hdr->ff.lf = ETH1394_HDR_LF_FF;
1419                 hdr->ff.ether_type = proto;
1420                 hdr->ff.dg_size = dg_size - 1;
1421                 hdr->ff.dgl = dgl;
1422                 adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF];
1423         }
1424         return((dg_size + (adj_max_payload - 1)) / adj_max_payload);
1425 }
1426
1427 static inline unsigned int ether1394_encapsulate(struct sk_buff *skb,
1428                                                  unsigned int max_payload,
1429                                                  union eth1394_hdr *hdr)
1430 {
1431         union eth1394_hdr *bufhdr;
1432         int ftype = hdr->common.lf;
1433         int hdrsz = hdr_type_len[ftype];
1434         unsigned int adj_max_payload = max_payload - hdrsz;
1435
1436         switch(ftype) {
1437         case ETH1394_HDR_LF_UF:
1438                 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1439                 bufhdr->words.word1 = htons(hdr->words.word1);
1440                 bufhdr->words.word2 = hdr->words.word2;
1441                 break;
1442
1443         case ETH1394_HDR_LF_FF:
1444                 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1445                 bufhdr->words.word1 = htons(hdr->words.word1);
1446                 bufhdr->words.word2 = hdr->words.word2;
1447                 bufhdr->words.word3 = htons(hdr->words.word3);
1448                 bufhdr->words.word4 = 0;
1449
1450                 /* Set frag type here for future interior fragments */
1451                 hdr->common.lf = ETH1394_HDR_LF_IF;
1452                 hdr->sf.fg_off = 0;
1453                 break;
1454
1455         default:
1456                 hdr->sf.fg_off += adj_max_payload;
1457                 bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload);
1458                 if (max_payload >= skb->len)
1459                         hdr->common.lf = ETH1394_HDR_LF_LF;
1460                 bufhdr->words.word1 = htons(hdr->words.word1);
1461                 bufhdr->words.word2 = htons(hdr->words.word2);
1462                 bufhdr->words.word3 = htons(hdr->words.word3);
1463                 bufhdr->words.word4 = 0;
1464         }
1465
1466         return min(max_payload, skb->len);
1467 }
1468
1469 static inline struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
1470 {
1471         struct hpsb_packet *p;
1472
1473         p = hpsb_alloc_packet(0);
1474         if (p) {
1475                 p->host = host;
1476                 p->generation = get_hpsb_generation(host);
1477                 p->type = hpsb_async;
1478         }
1479         return p;
1480 }
1481
1482 static inline int ether1394_prep_write_packet(struct hpsb_packet *p,
1483                                               struct hpsb_host *host,
1484                                               nodeid_t node, u64 addr,
1485                                               void * data, int tx_len)
1486 {
1487         p->node_id = node;
1488         p->data = NULL;
1489
1490         p->tcode = TCODE_WRITEB;
1491         p->header[1] = (host->node_id << 16) | (addr >> 32);
1492         p->header[2] = addr & 0xffffffff;
1493
1494         p->header_size = 16;
1495         p->expect_response = 1;
1496
1497         if (hpsb_get_tlabel(p)) {
1498                 ETH1394_PRINT_G(KERN_ERR, "No more tlabels left while sending "
1499                                 "to node " NODE_BUS_FMT "\n", NODE_BUS_ARGS(host, node));
1500                 return -1;
1501         }
1502         p->header[0] = (p->node_id << 16) | (p->tlabel << 10)
1503                 | (1 << 8) | (TCODE_WRITEB << 4);
1504
1505         p->header[3] = tx_len << 16;
1506         p->data_size = (tx_len + 3) & ~3;
1507         p->data = (quadlet_t*)data;
1508
1509         return 0;
1510 }
1511
1512 static inline void ether1394_prep_gasp_packet(struct hpsb_packet *p,
1513                                               struct eth1394_priv *priv,
1514                                               struct sk_buff *skb, int length)
1515 {
1516         p->header_size = 4;
1517         p->tcode = TCODE_STREAM_DATA;
1518
1519         p->header[0] = (length << 16) | (3 << 14)
1520                 | ((priv->broadcast_channel) << 8)
1521                 | (TCODE_STREAM_DATA << 4);
1522         p->data_size = length;
1523         p->data = ((quadlet_t*)skb->data) - 2;
1524         p->data[0] = cpu_to_be32((priv->host->node_id << 16) |
1525                                  ETHER1394_GASP_SPECIFIER_ID_HI);
1526         p->data[1] = __constant_cpu_to_be32((ETHER1394_GASP_SPECIFIER_ID_LO << 24) |
1527                                             ETHER1394_GASP_VERSION);
1528
1529         /* Setting the node id to ALL_NODES (not LOCAL_BUS | ALL_NODES)
1530          * prevents hpsb_send_packet() from setting the speed to an arbitrary
1531          * value based on packet->node_id if packet->node_id is not set. */
1532         p->node_id = ALL_NODES;
1533         p->speed_code = priv->bc_sspd;
1534 }
1535
1536 static inline void ether1394_free_packet(struct hpsb_packet *packet)
1537 {
1538         if (packet->tcode != TCODE_STREAM_DATA)
1539                 hpsb_free_tlabel(packet);
1540         hpsb_free_packet(packet);
1541 }
1542
1543 static void ether1394_complete_cb(void *__ptask);
1544
1545 static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len)
1546 {
1547         struct eth1394_priv *priv = ptask->priv;
1548         struct hpsb_packet *packet = NULL;
1549
1550         packet = ether1394_alloc_common_packet(priv->host);
1551         if (!packet)
1552                 return -1;
1553
1554         if (ptask->tx_type == ETH1394_GASP) {
1555                 int length = tx_len + (2 * sizeof(quadlet_t));
1556
1557                 ether1394_prep_gasp_packet(packet, priv, ptask->skb, length);
1558         } else if (ether1394_prep_write_packet(packet, priv->host,
1559                                                ptask->dest_node,
1560                                                ptask->addr, ptask->skb->data,
1561                                                tx_len)) {
1562                 hpsb_free_packet(packet);
1563                 return -1;
1564         }
1565
1566         ptask->packet = packet;
1567         hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb,
1568                                       ptask);
1569
1570         if (hpsb_send_packet(packet) < 0) {
1571                 ether1394_free_packet(packet);
1572                 return -1;
1573         }
1574
1575         return 0;
1576 }
1577
1578
1579 /* Task function to be run when a datagram transmission is completed */
1580 static inline void ether1394_dg_complete(struct packet_task *ptask, int fail)
1581 {
1582         struct sk_buff *skb = ptask->skb;
1583         struct net_device *dev = skb->dev;
1584         struct eth1394_priv *priv = netdev_priv(dev);
1585         unsigned long flags;
1586
1587         /* Statistics */
1588         spin_lock_irqsave(&priv->lock, flags);
1589         if (fail) {
1590                 priv->stats.tx_dropped++;
1591                 priv->stats.tx_errors++;
1592         } else {
1593                 priv->stats.tx_bytes += skb->len;
1594                 priv->stats.tx_packets++;
1595         }
1596         spin_unlock_irqrestore(&priv->lock, flags);
1597
1598         dev_kfree_skb_any(skb);
1599         kmem_cache_free(packet_task_cache, ptask);
1600 }
1601
1602
1603 /* Callback for when a packet has been sent and the status of that packet is
1604  * known */
1605 static void ether1394_complete_cb(void *__ptask)
1606 {
1607         struct packet_task *ptask = (struct packet_task *)__ptask;
1608         struct hpsb_packet *packet = ptask->packet;
1609         int fail = 0;
1610
1611         if (packet->tcode != TCODE_STREAM_DATA)
1612                 fail = hpsb_packet_success(packet);
1613
1614         ether1394_free_packet(packet);
1615
1616         ptask->outstanding_pkts--;
1617         if (ptask->outstanding_pkts > 0 && !fail) {
1618                 int tx_len;
1619
1620                 /* Add the encapsulation header to the fragment */
1621                 tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload,
1622                                                &ptask->hdr);
1623                 if (ether1394_send_packet(ptask, tx_len))
1624                         ether1394_dg_complete(ptask, 1);
1625         } else {
1626                 ether1394_dg_complete(ptask, fail);
1627         }
1628 }
1629
1630
1631
1632 /* Transmit a packet (called by kernel) */
1633 static int ether1394_tx (struct sk_buff *skb, struct net_device *dev)
1634 {
1635         int kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
1636         struct eth1394hdr *eth;
1637         struct eth1394_priv *priv = netdev_priv(dev);
1638         int proto;
1639         unsigned long flags;
1640         nodeid_t dest_node;
1641         eth1394_tx_type tx_type;
1642         int ret = 0;
1643         unsigned int tx_len;
1644         unsigned int max_payload;
1645         u16 dg_size;
1646         u16 dgl;
1647         struct packet_task *ptask;
1648         struct eth1394_node_ref *node;
1649         struct eth1394_node_info *node_info = NULL;
1650
1651         ptask = kmem_cache_alloc(packet_task_cache, kmflags);
1652         if (ptask == NULL) {
1653                 ret = -ENOMEM;
1654                 goto fail;
1655         }
1656
1657         /* XXX Ignore this for now. Noticed that when MacOSX is the IRM,
1658          * it does not set our validity bit. We need to compensate for
1659          * that somewhere else, but not in eth1394. */
1660 #if 0
1661         if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000) {
1662                 ret = -EAGAIN;
1663                 goto fail;
1664         }
1665 #endif
1666
1667         if ((skb = skb_share_check (skb, kmflags)) == NULL) {
1668                 ret = -ENOMEM;
1669                 goto fail;
1670         }
1671
1672         /* Get rid of the fake eth1394 header, but save a pointer */
1673         eth = (struct eth1394hdr*)skb->data;
1674         skb_pull(skb, ETH1394_HLEN);
1675
1676         proto = eth->h_proto;
1677         dg_size = skb->len;
1678
1679         /* Set the transmission type for the packet.  ARP packets and IP
1680          * broadcast packets are sent via GASP. */
1681         if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 ||
1682             proto == __constant_htons(ETH_P_ARP) ||
1683             (proto == __constant_htons(ETH_P_IP) &&
1684              IN_MULTICAST(__constant_ntohl(skb->nh.iph->daddr)))) {
1685                 tx_type = ETH1394_GASP;
1686                 dest_node = LOCAL_BUS | ALL_NODES;
1687                 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
1688                 BUG_ON(max_payload < (512 - ETHER1394_GASP_OVERHEAD));
1689                 dgl = priv->bc_dgl;
1690                 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1691                         priv->bc_dgl++;
1692         } else {
1693                 node = eth1394_find_node_guid(&priv->ip_node_list,
1694                                               be64_to_cpu(*(u64*)eth->h_dest));
1695                 if (!node) {
1696                         ret = -EAGAIN;
1697                         goto fail;
1698                 }
1699                 node_info = (struct eth1394_node_info*)node->ud->device.driver_data;
1700                 if (node_info->fifo == ETHER1394_INVALID_ADDR) {
1701                         ret = -EAGAIN;
1702                         goto fail;
1703                 }
1704
1705                 dest_node = node->ud->ne->nodeid;
1706                 max_payload = node_info->maxpayload;
1707                 BUG_ON(max_payload < (512 - ETHER1394_GASP_OVERHEAD));
1708
1709                 dgl = node_info->dgl;
1710                 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1711                         node_info->dgl++;
1712                 tx_type = ETH1394_WRREQ;
1713         }
1714
1715         /* If this is an ARP packet, convert it */
1716         if (proto == __constant_htons (ETH_P_ARP))
1717                 ether1394_arp_to_1394arp (skb, dev);
1718
1719         ptask->hdr.words.word1 = 0;
1720         ptask->hdr.words.word2 = 0;
1721         ptask->hdr.words.word3 = 0;
1722         ptask->hdr.words.word4 = 0;
1723         ptask->skb = skb;
1724         ptask->priv = priv;
1725         ptask->tx_type = tx_type;
1726
1727         if (tx_type != ETH1394_GASP) {
1728                 u64 addr;
1729
1730                 spin_lock_irqsave(&priv->lock, flags);
1731                 addr = node_info->fifo;
1732                 spin_unlock_irqrestore(&priv->lock, flags);
1733
1734                 ptask->addr = addr;
1735                 ptask->dest_node = dest_node;
1736         }
1737
1738         ptask->tx_type = tx_type;
1739         ptask->max_payload = max_payload;
1740         ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload, proto,
1741                                                              &ptask->hdr, dg_size,
1742                                                              dgl);
1743
1744         /* Add the encapsulation header to the fragment */
1745         tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr);
1746         dev->trans_start = jiffies;
1747         if (ether1394_send_packet(ptask, tx_len))
1748                 goto fail;
1749
1750         netif_wake_queue(dev);
1751         return 0;
1752 fail:
1753         if (ptask)
1754                 kmem_cache_free(packet_task_cache, ptask);
1755
1756         if (skb != NULL)
1757                 dev_kfree_skb(skb);
1758
1759         spin_lock_irqsave (&priv->lock, flags);
1760         priv->stats.tx_dropped++;
1761         priv->stats.tx_errors++;
1762         spin_unlock_irqrestore (&priv->lock, flags);
1763
1764         if (netif_queue_stopped(dev))
1765                 netif_wake_queue(dev);
1766
1767         return 0;  /* returning non-zero causes serious problems */
1768 }
1769
1770 static void ether1394_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1771 {
1772         strcpy (info->driver, driver_name);
1773         strcpy (info->version, "$Rev: 1247 $");
1774         /* FIXME XXX provide sane businfo */
1775         strcpy (info->bus_info, "ieee1394");
1776 }
1777
1778 static struct ethtool_ops ethtool_ops = {
1779         .get_drvinfo = ether1394_get_drvinfo
1780 };
1781
1782 static int __init ether1394_init_module (void)
1783 {
1784         packet_task_cache = kmem_cache_create("packet_task", sizeof(struct packet_task),
1785                                               0, 0, NULL, NULL);
1786
1787         /* Register ourselves as a highlevel driver */
1788         hpsb_register_highlevel(&eth1394_highlevel);
1789
1790         return hpsb_register_protocol(&eth1394_proto_driver);
1791 }
1792
1793 static void __exit ether1394_exit_module (void)
1794 {
1795         hpsb_unregister_protocol(&eth1394_proto_driver);
1796         hpsb_unregister_highlevel(&eth1394_highlevel);
1797         kmem_cache_destroy(packet_task_cache);
1798 }
1799
1800 module_init(ether1394_init_module);
1801 module_exit(ether1394_exit_module);