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