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