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