blob: fc6c7fd728d95798f4cd932bf68a115609a470e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stefan Richterefbeccf2007-04-02 02:12:32 +02002 * eth1394.c -- IPv4 driver for Linux IEEE-1394 Subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Stefan Richterefbeccf2007-04-02 02:12:32 +020025/*
26 * This driver intends to support RFC 2734, which describes a method for
27 * transporting IPv4 datagrams over IEEE-1394 serial busses.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/module.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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
51#include <linux/netdevice.h>
52#include <linux/inetdevice.h>
53#include <linux/etherdevice.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>
David S. Millerc20e3942006-10-29 16:14:55 -080064#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/arp.h>
66
Stefan Richterde4394f2006-07-03 12:02:29 -040067#include "config_roms.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include "csr1212.h"
Stefan Richterde4394f2006-07-03 12:02:29 -040069#include "eth1394.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include "highlevel.h"
Stefan Richterde4394f2006-07-03 12:02:29 -040071#include "ieee1394.h"
72#include "ieee1394_core.h"
73#include "ieee1394_hotplug.h"
74#include "ieee1394_transactions.h"
75#include "ieee1394_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include "iso.h"
77#include "nodemgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct 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 u16 ether_type;
96 struct sk_buff *skb;
97 char *pbuf;
98 struct list_head frag_info;
99};
100
101struct pdg_list {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200102 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
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 {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200118 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Stefan Richterefbeccf2007-04-02 02:12:32 +0200125static const char driver_name[] = "eth1394";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Lametere18b8902006-12-06 20:33:20 -0800127static struct kmem_cache *packet_task_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static struct hpsb_highlevel eth1394_highlevel;
130
131/* Use common.lf to determine header len */
132static const int hdr_type_len[] = {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200133 sizeof(struct eth1394_uf_hdr),
134 sizeof(struct eth1394_ff_hdr),
135 sizeof(struct eth1394_sf_hdr),
136 sizeof(struct eth1394_sf_hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static 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
Stefan Richterefbeccf2007-04-02 02:12:32 +0200148/*
149 * The max_partial_datagrams parameter is the maximum number of fragmented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 * 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, void *daddr, void *saddr,
163 unsigned len);
164static int ether1394_rebuild_header(struct sk_buff *skb);
165static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr);
166static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh);
167static void ether1394_header_cache_update(struct hh_cache *hh,
168 struct net_device *dev,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200169 unsigned char *haddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int ether1394_tx(struct sk_buff *skb, struct net_device *dev);
171static void ether1394_iso(struct hpsb_iso *iso);
172
173static struct ethtool_ops ethtool_ops;
174
175static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
176 quadlet_t *data, u64 addr, size_t len, u16 flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200177static void ether1394_add_host(struct hpsb_host *host);
178static void ether1394_remove_host(struct hpsb_host *host);
179static void ether1394_host_reset(struct hpsb_host *host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Function for incoming 1394 packets */
182static struct hpsb_address_ops addr_ops = {
183 .write = ether1394_write,
184};
185
186/* Ieee1394 highlevel driver functions */
187static struct hpsb_highlevel eth1394_highlevel = {
188 .name = driver_name,
189 .add_host = ether1394_add_host,
190 .remove_host = ether1394_remove_host,
191 .host_reset = ether1394_host_reset,
192};
193
Stefan Richter5009d262007-04-02 02:15:53 +0200194static int ether1394_recv_init(struct eth1394_priv *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200196 unsigned int iso_buf_size;
197
198 /* FIXME: rawiso limits us to PAGE_SIZE */
199 iso_buf_size = min((unsigned int)PAGE_SIZE,
200 2 * (1U << (priv->host->csr.max_rec + 1)));
Jean Delvare09d7a962007-04-01 10:06:33 +0200201
202 priv->iso = hpsb_iso_recv_init(priv->host,
Stefan Richterefbeccf2007-04-02 02:12:32 +0200203 ETHER1394_GASP_BUFFERS * iso_buf_size,
Jean Delvare09d7a962007-04-01 10:06:33 +0200204 ETHER1394_GASP_BUFFERS,
205 priv->broadcast_channel,
206 HPSB_ISO_DMA_PACKET_PER_BUFFER,
207 1, ether1394_iso);
208 if (priv->iso == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200209 ETH1394_PRINT_G(KERN_ERR, "Failed to allocate IR context\n");
Jean Delvare09d7a962007-04-01 10:06:33 +0200210 priv->bc_state = ETHER1394_BC_ERROR;
211 return -EAGAIN;
212 }
213
214 if (hpsb_iso_recv_start(priv->iso, -1, (1 << 3), -1) < 0)
215 priv->bc_state = ETHER1394_BC_STOPPED;
216 else
217 priv->bc_state = ETHER1394_BC_RUNNING;
218 return 0;
219}
220
221/* This is called after an "ifup" */
222static int ether1394_open(struct net_device *dev)
223{
224 struct eth1394_priv *priv = netdev_priv(dev);
225 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (priv->bc_state == ETHER1394_BC_ERROR) {
Stefan Richter5009d262007-04-02 02:15:53 +0200228 ret = ether1394_recv_init(priv);
Jean Delvare09d7a962007-04-01 10:06:33 +0200229 if (ret)
230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Stefan Richterefbeccf2007-04-02 02:12:32 +0200232 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234}
235
236/* This is called after an "ifdown" */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200237static int ether1394_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200239 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241}
242
243/* Return statistics to the caller */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200244static struct net_device_stats *ether1394_stats(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 return &(((struct eth1394_priv *)netdev_priv(dev))->stats);
247}
248
Stefan Richterefbeccf2007-04-02 02:12:32 +0200249/* FIXME: What to do if we timeout? I think a host reset is probably in order,
250 * so that's what we do. Should we increment the stat counters too? */
251static void ether1394_tx_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200253 struct hpsb_host *host =
254 ((struct eth1394_priv *)netdev_priv(dev))->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Stefan Richter246a5fd2007-04-02 02:16:40 +0200256 ETH1394_PRINT(KERN_ERR, dev->name, "Timeout, resetting host\n");
257 ether1394_host_reset(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Stefan Richter17bab402007-04-03 23:55:40 +0200260static inline int ether1394_max_mtu(struct hpsb_host* host)
261{
262 return (1 << (host->csr.max_rec + 1))
263 - sizeof(union eth1394_hdr) - ETHER1394_GASP_OVERHEAD;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int ether1394_change_mtu(struct net_device *dev, int new_mtu)
267{
Stefan Richter17bab402007-04-03 23:55:40 +0200268 int max_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Stefan Richter17bab402007-04-03 23:55:40 +0200270 if (new_mtu < 68)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -EINVAL;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200272
Stefan Richter17bab402007-04-03 23:55:40 +0200273 max_mtu = ether1394_max_mtu(
274 ((struct eth1394_priv *)netdev_priv(dev))->host);
275 if (new_mtu > max_mtu) {
276 ETH1394_PRINT(KERN_INFO, dev->name,
277 "Local node constrains MTU to %d\n", max_mtu);
278 return -ERANGE;
279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dev->mtu = new_mtu;
282 return 0;
283}
284
285static void purge_partial_datagram(struct list_head *old)
286{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200287 struct partial_datagram *pd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct list_head *lh, *n;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200289 struct fragment_info *fi;
290
291 pd = list_entry(old, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 list_for_each_safe(lh, n, &pd->frag_info) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200294 fi = list_entry(lh, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 list_del(lh);
296 kfree(fi);
297 }
298 list_del(old);
299 kfree_skb(pd->skb);
300 kfree(pd);
301}
302
303/******************************************
304 * 1394 bus activity functions
305 ******************************************/
306
307static struct eth1394_node_ref *eth1394_find_node(struct list_head *inl,
308 struct unit_directory *ud)
309{
310 struct eth1394_node_ref *node;
311
312 list_for_each_entry(node, inl, list)
313 if (node->ud == ud)
314 return node;
315
316 return NULL;
317}
318
319static struct eth1394_node_ref *eth1394_find_node_guid(struct list_head *inl,
320 u64 guid)
321{
322 struct eth1394_node_ref *node;
323
324 list_for_each_entry(node, inl, list)
325 if (node->ud->ne->guid == guid)
326 return node;
327
328 return NULL;
329}
330
331static struct eth1394_node_ref *eth1394_find_node_nodeid(struct list_head *inl,
332 nodeid_t nodeid)
333{
334 struct eth1394_node_ref *node;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200335
336 list_for_each_entry(node, inl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (node->ud->ne->nodeid == nodeid)
338 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 return NULL;
341}
342
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200343static int eth1394_new_node(struct eth1394_host_info *hi,
344 struct unit_directory *ud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct eth1394_priv *priv;
347 struct eth1394_node_ref *new_node;
348 struct eth1394_node_info *node_info;
349
Stefan Richter5e7abcc2007-04-02 02:13:51 +0200350 new_node = kmalloc(sizeof(*new_node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!new_node)
352 return -ENOMEM;
353
Stefan Richter5e7abcc2007-04-02 02:13:51 +0200354 node_info = kmalloc(sizeof(*node_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!node_info) {
356 kfree(new_node);
357 return -ENOMEM;
358 }
359
360 spin_lock_init(&node_info->pdg.lock);
361 INIT_LIST_HEAD(&node_info->pdg.list);
362 node_info->pdg.sz = 0;
Ben Collins67372312006-06-12 18:15:31 -0400363 node_info->fifo = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 ud->device.driver_data = node_info;
366 new_node->ud = ud;
367
368 priv = netdev_priv(hi->dev);
369 list_add_tail(&new_node->list, &priv->ip_node_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200373static int eth1394_probe(struct device *dev)
374{
375 struct unit_directory *ud;
376 struct eth1394_host_info *hi;
377
378 ud = container_of(dev, struct unit_directory, device);
379 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
380 if (!hi)
381 return -ENOENT;
382
383 return eth1394_new_node(hi, ud);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static int eth1394_remove(struct device *dev)
387{
388 struct unit_directory *ud;
389 struct eth1394_host_info *hi;
390 struct eth1394_priv *priv;
391 struct eth1394_node_ref *old_node;
392 struct eth1394_node_info *node_info;
393 struct list_head *lh, *n;
394 unsigned long flags;
395
396 ud = container_of(dev, struct unit_directory, device);
397 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
398 if (!hi)
399 return -ENOENT;
400
401 priv = netdev_priv(hi->dev);
402
403 old_node = eth1394_find_node(&priv->ip_node_list, ud);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200404 if (!old_node)
405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Stefan Richterefbeccf2007-04-02 02:12:32 +0200407 list_del(&old_node->list);
408 kfree(old_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Stefan Richterefbeccf2007-04-02 02:12:32 +0200410 node_info = (struct eth1394_node_info*)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Stefan Richterefbeccf2007-04-02 02:12:32 +0200412 spin_lock_irqsave(&node_info->pdg.lock, flags);
413 /* The partial datagram list should be empty, but we'll just
414 * make sure anyway... */
415 list_for_each_safe(lh, n, &node_info->pdg.list)
416 purge_partial_datagram(lh);
417 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Stefan Richterefbeccf2007-04-02 02:12:32 +0200419 kfree(node_info);
420 ud->device.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422}
423
424static int eth1394_update(struct unit_directory *ud)
425{
426 struct eth1394_host_info *hi;
427 struct eth1394_priv *priv;
428 struct eth1394_node_ref *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 hi = hpsb_get_hostinfo(&eth1394_highlevel, ud->ne->host);
431 if (!hi)
432 return -ENOENT;
433
434 priv = netdev_priv(hi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 node = eth1394_find_node(&priv->ip_node_list, ud);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200436 if (node)
437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Stefan Richterd06c1dd2007-04-02 02:14:45 +0200439 return eth1394_new_node(hi, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static struct ieee1394_device_id eth1394_id_table[] = {
443 {
444 .match_flags = (IEEE1394_MATCH_SPECIFIER_ID |
445 IEEE1394_MATCH_VERSION),
446 .specifier_id = ETHER1394_GASP_SPECIFIER_ID,
447 .version = ETHER1394_GASP_VERSION,
448 },
449 {}
450};
451
452MODULE_DEVICE_TABLE(ieee1394, eth1394_id_table);
453
454static struct hpsb_protocol_driver eth1394_proto_driver = {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200455 .name = driver_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .id_table = eth1394_id_table,
457 .update = eth1394_update,
458 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 .probe = eth1394_probe,
460 .remove = eth1394_remove,
461 },
462};
463
Stefan Richterefbeccf2007-04-02 02:12:32 +0200464static void ether1394_reset_priv(struct net_device *dev, int set_mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 unsigned long flags;
467 int i;
468 struct eth1394_priv *priv = netdev_priv(dev);
469 struct hpsb_host *host = priv->host;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200470 u64 guid = get_unaligned((u64 *)&(host->csr.rom->bus_info_data[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int max_speed = IEEE1394_SPEED_MAX;
472
Stefan Richterefbeccf2007-04-02 02:12:32 +0200473 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Stefan Richter027611b2007-04-02 02:15:21 +0200475 memset(priv->ud_list, 0, sizeof(priv->ud_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 priv->bc_maxpayload = 512;
477
478 /* Determine speed limit */
479 for (i = 0; i < host->node_count; i++)
Ben Collins647dcb52006-06-12 18:12:37 -0400480 if (max_speed > host->speed[i])
481 max_speed = host->speed[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 priv->bc_sspd = max_speed;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (set_mtu) {
Stefan Richter17bab402007-04-03 23:55:40 +0200485 /* Use the RFC 2734 default 1500 octets or the maximum payload
486 * as initial MTU */
487 dev->mtu = min(1500, ether1394_max_mtu(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* Set our hardware address while we're at it */
David S. Millerc20e3942006-10-29 16:14:55 -0800490 memcpy(dev->dev_addr, &guid, sizeof(u64));
491 memset(dev->broadcast, 0xff, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Stefan Richterefbeccf2007-04-02 02:12:32 +0200494 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/* This function is called right before register_netdev */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200498static void ether1394_init_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 /* Our functions */
501 dev->open = ether1394_open;
502 dev->stop = ether1394_stop;
503 dev->hard_start_xmit = ether1394_tx;
504 dev->get_stats = ether1394_stats;
505 dev->tx_timeout = ether1394_tx_timeout;
506 dev->change_mtu = ether1394_change_mtu;
507
508 dev->hard_header = ether1394_header;
509 dev->rebuild_header = ether1394_rebuild_header;
510 dev->hard_header_cache = ether1394_header_cache;
511 dev->header_cache_update= ether1394_header_cache_update;
512 dev->hard_header_parse = ether1394_header_parse;
Stefan Richter8a62bf72007-04-02 02:19:48 +0200513 dev->set_mac_address = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 SET_ETHTOOL_OPS(dev, &ethtool_ops);
515
516 /* Some constants */
517 dev->watchdog_timeo = ETHER1394_TIMEOUT;
518 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
519 dev->features = NETIF_F_HIGHDMA;
520 dev->addr_len = ETH1394_ALEN;
521 dev->hard_header_len = ETH1394_HLEN;
522 dev->type = ARPHRD_IEEE1394;
523
Stefan Richterefbeccf2007-04-02 02:12:32 +0200524 ether1394_reset_priv(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
528 * This function is called every time a card is found. It is generally called
529 * when the module is installed. This is where we add all of our ethernet
530 * devices. One for each host.
531 */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200532static void ether1394_add_host(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct eth1394_host_info *hi = NULL;
535 struct net_device *dev = NULL;
536 struct eth1394_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 u64 fifo_addr;
538
Stefan Richter70093cf2007-03-27 01:36:50 +0200539 if (hpsb_config_rom_ip1394_add(host) != 0) {
540 ETH1394_PRINT_G(KERN_ERR, "Can't add IP-over-1394 ROM entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return;
Stefan Richter70093cf2007-03-27 01:36:50 +0200542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ben Collins67372312006-06-12 18:15:31 -0400544 fifo_addr = hpsb_allocate_and_register_addrspace(
545 &eth1394_highlevel, host, &addr_ops,
546 ETHER1394_REGION_ADDR_LEN, ETHER1394_REGION_ADDR_LEN,
547 CSR1212_INVALID_ADDR_SPACE, CSR1212_INVALID_ADDR_SPACE);
Stefan Richter157188c2007-02-10 23:56:38 +0100548 if (fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
549 ETH1394_PRINT_G(KERN_ERR, "Cannot register CSR space\n");
Stefan Richter70093cf2007-03-27 01:36:50 +0200550 hpsb_config_rom_ip1394_remove(host);
Stefan Richter157188c2007-02-10 23:56:38 +0100551 return;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* We should really have our own alloc_hpsbdev() function in
555 * net_init.c instead of calling the one for ethernet then hijacking
556 * it for ourselves. That way we'd be a real networking device. */
557 dev = alloc_etherdev(sizeof (struct eth1394_priv));
558
559 if (dev == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200560 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto out;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 SET_MODULE_OWNER(dev);
Stefan Richter7a9eeb22007-03-20 22:43:22 +0100565#if 0
566 /* FIXME - Is this the correct parent device anyway? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 SET_NETDEV_DEV(dev, &host->device);
Stefan Richter7a9eeb22007-03-20 22:43:22 +0100568#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 priv = netdev_priv(dev);
571
572 INIT_LIST_HEAD(&priv->ip_node_list);
573
574 spin_lock_init(&priv->lock);
575 priv->host = host;
576 priv->local_fifo = fifo_addr;
577
578 hi = hpsb_create_hostinfo(&eth1394_highlevel, host, sizeof(*hi));
579
580 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +0200581 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto out;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 ether1394_init_dev(dev);
586
Stefan Richter5009d262007-04-02 02:15:53 +0200587 if (register_netdev(dev)) {
588 ETH1394_PRINT_G(KERN_ERR, "Cannot register the driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto out;
590 }
591
Stefan Richter5009d262007-04-02 02:15:53 +0200592 ETH1394_PRINT(KERN_INFO, dev->name, "IPv4 over IEEE 1394 (fw-host%d)\n",
593 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 hi->host = host;
596 hi->dev = dev;
597
598 /* Ignore validity in hopes that it will be set in the future. It'll
599 * be checked when the eth device is opened. */
600 priv->broadcast_channel = host->csr.broadcast_channel & 0x3f;
601
Stefan Richter5009d262007-04-02 02:15:53 +0200602 ether1394_recv_init(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604out:
Stefan Richter157188c2007-02-10 23:56:38 +0100605 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 free_netdev(dev);
607 if (hi)
608 hpsb_destroy_hostinfo(&eth1394_highlevel, host);
Stefan Richter157188c2007-02-10 23:56:38 +0100609 hpsb_unregister_addrspace(&eth1394_highlevel, host, fifo_addr);
Stefan Richter70093cf2007-03-27 01:36:50 +0200610 hpsb_config_rom_ip1394_remove(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/* Remove a card from our list */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200614static void ether1394_remove_host(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct eth1394_host_info *hi;
Stefan Richter2cd556a2007-02-10 23:57:57 +0100617 struct eth1394_priv *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
Stefan Richter2cd556a2007-02-10 23:57:57 +0100620 if (!hi)
621 return;
622 priv = netdev_priv(hi->dev);
623 hpsb_unregister_addrspace(&eth1394_highlevel, host, priv->local_fifo);
Stefan Richter70093cf2007-03-27 01:36:50 +0200624 hpsb_config_rom_ip1394_remove(host);
Stefan Richter2cd556a2007-02-10 23:57:57 +0100625 if (priv->iso)
626 hpsb_iso_shutdown(priv->iso);
627 unregister_netdev(hi->dev);
628 free_netdev(hi->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Stefan Richterefbeccf2007-04-02 02:12:32 +0200631/* A bus reset happened */
632static void ether1394_host_reset(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct eth1394_host_info *hi;
635 struct eth1394_priv *priv;
636 struct net_device *dev;
637 struct list_head *lh, *n;
638 struct eth1394_node_ref *node;
639 struct eth1394_node_info *node_info;
640 unsigned long flags;
641
642 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
643
644 /* This can happen for hosts that we don't use */
Stefan Richter2cd556a2007-02-10 23:57:57 +0100645 if (!hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647
648 dev = hi->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200649 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Stefan Richterefbeccf2007-04-02 02:12:32 +0200651 /* Reset our private host data, but not our MTU */
652 netif_stop_queue(dev);
653 ether1394_reset_priv(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 list_for_each_entry(node, &priv->ip_node_list, list) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200656 node_info = node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 spin_lock_irqsave(&node_info->pdg.lock, flags);
659
Stefan Richterefbeccf2007-04-02 02:12:32 +0200660 list_for_each_safe(lh, n, &node_info->pdg.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 purge_partial_datagram(lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 INIT_LIST_HEAD(&(node_info->pdg.list));
664 node_info->pdg.sz = 0;
665
666 spin_unlock_irqrestore(&node_info->pdg.lock, flags);
667 }
668
Stefan Richterefbeccf2007-04-02 02:12:32 +0200669 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672/******************************************
673 * HW Header net device functions
674 ******************************************/
675/* These functions have been adapted from net/ethernet/eth.c */
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/* Create a fake MAC header for an arbitrary protocol layer.
678 * saddr=NULL means use device source address
679 * daddr=NULL means leave destination address (eg unresolved arp). */
680static int ether1394_header(struct sk_buff *skb, struct net_device *dev,
681 unsigned short type, void *daddr, void *saddr,
682 unsigned len)
683{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200684 struct eth1394hdr *eth =
685 (struct eth1394hdr *)skb_push(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 eth->h_proto = htons(type);
688
Stefan Richterefbeccf2007-04-02 02:12:32 +0200689 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 memset(eth->h_dest, 0, dev->addr_len);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200691 return dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
694 if (daddr) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200695 memcpy(eth->h_dest, daddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return dev->hard_header_len;
697 }
698
699 return -dev->hard_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* Rebuild the faked MAC header. This is called after an ARP
703 * (or in future other address resolution) has completed on this
704 * sk_buff. We now let ARP fill in the other fields.
705 *
706 * This routine CANNOT use cached dst->neigh!
707 * Really, it is used only when dst->neigh is wrong.
708 */
709static int ether1394_rebuild_header(struct sk_buff *skb)
710{
711 struct eth1394hdr *eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Stefan Richter599bba92007-04-02 02:19:02 +0200713 if (eth->h_proto == htons(ETH_P_IP))
Stefan Richterefbeccf2007-04-02 02:12:32 +0200714 return arp_find((unsigned char *)&eth->h_dest, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Stefan Richter599bba92007-04-02 02:19:02 +0200716 ETH1394_PRINT(KERN_DEBUG, skb->dev->name,
717 "unable to resolve type %04x addresses\n",
718 ntohs(eth->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720}
721
722static int ether1394_header_parse(struct sk_buff *skb, unsigned char *haddr)
723{
724 struct net_device *dev = skb->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 memcpy(haddr, dev->dev_addr, ETH1394_ALEN);
727 return ETH1394_ALEN;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int ether1394_header_cache(struct neighbour *neigh, struct hh_cache *hh)
731{
732 unsigned short type = hh->hh_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct net_device *dev = neigh->dev;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200734 struct eth1394hdr *eth =
735 (struct eth1394hdr *)((u8 *)hh->hh_data + 16 - ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Ben Collins7136b802006-06-12 18:16:25 -0400737 if (type == htons(ETH_P_802_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 eth->h_proto = type;
741 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
742
743 hh->hh_len = ETH1394_HLEN;
744 return 0;
745}
746
747/* Called by Address Resolution module to notify changes in address. */
748static void ether1394_header_cache_update(struct hh_cache *hh,
749 struct net_device *dev,
750 unsigned char * haddr)
751{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200752 memcpy((u8 *)hh->hh_data + 16 - ETH1394_HLEN, haddr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/******************************************
756 * Datagram reception code
757 ******************************************/
758
759/* Copied from net/ethernet/eth.c */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100760static u16 ether1394_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct eth1394hdr *eth;
763 unsigned char *rawp;
764
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700765 skb_reset_mac_header(skb);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200766 skb_pull(skb, ETH1394_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 eth = eth1394_hdr(skb);
768
769 if (*eth->h_dest & 1) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200770 if (memcmp(eth->h_dest, dev->broadcast, dev->addr_len) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 skb->pkt_type = PACKET_BROADCAST;
772#if 0
773 else
774 skb->pkt_type = PACKET_MULTICAST;
775#endif
776 } else {
777 if (memcmp(eth->h_dest, dev->dev_addr, dev->addr_len))
778 skb->pkt_type = PACKET_OTHERHOST;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Stefan Richterefbeccf2007-04-02 02:12:32 +0200781 if (ntohs(eth->h_proto) >= 1536)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return eth->h_proto;
783
784 rawp = skb->data;
785
Stefan Richterefbeccf2007-04-02 02:12:32 +0200786 if (*(unsigned short *)rawp == 0xFFFF)
787 return htons(ETH_P_802_3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Stefan Richterefbeccf2007-04-02 02:12:32 +0200789 return htons(ETH_P_802_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/* Parse an encapsulated IP1394 header into an ethernet frame packet.
793 * We also perform ARP translation here, if need be. */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100794static u16 ether1394_parse_encap(struct sk_buff *skb, struct net_device *dev,
795 nodeid_t srcid, nodeid_t destid,
796 u16 ether_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798 struct eth1394_priv *priv = netdev_priv(dev);
799 u64 dest_hw;
800 unsigned short ret = 0;
801
Stefan Richterefbeccf2007-04-02 02:12:32 +0200802 /* Setup our hw addresses. We use these to build the ethernet header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (destid == (LOCAL_BUS | ALL_NODES))
804 dest_hw = ~0ULL; /* broadcast */
805 else
Stefan Richterefbeccf2007-04-02 02:12:32 +0200806 dest_hw = cpu_to_be64((u64)priv->host->csr.guid_hi << 32 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 priv->host->csr.guid_lo);
808
809 /* If this is an ARP packet, convert it. First, we want to make
810 * use of some of the fields, since they tell us a little bit
811 * about the sending machine. */
Ben Collins7136b802006-06-12 18:16:25 -0400812 if (ether_type == htons(ETH_P_ARP)) {
Stefan Richterefbeccf2007-04-02 02:12:32 +0200813 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct arphdr *arp = (struct arphdr *)skb->data;
815 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
816 u64 fifo_addr = (u64)ntohs(arp1394->fifo_hi) << 32 |
Stefan Richterefbeccf2007-04-02 02:12:32 +0200817 ntohl(arp1394->fifo_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 u8 max_rec = min(priv->host->csr.max_rec,
819 (u8)(arp1394->max_rec));
820 int sspd = arp1394->sspd;
821 u16 maxpayload;
822 struct eth1394_node_ref *node;
823 struct eth1394_node_info *node_info;
David S. Millerc20e3942006-10-29 16:14:55 -0800824 __be64 guid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* Sanity check. MacOSX seems to be sending us 131 in this
827 * field (atleast on my Panther G5). Not sure why. */
828 if (sspd > 5 || sspd < 0)
829 sspd = 0;
830
Stefan Richterefbeccf2007-04-02 02:12:32 +0200831 maxpayload = min(eth1394_speedto_maxpayload[sspd],
832 (u16)(1 << (max_rec + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
David S. Millerc20e3942006-10-29 16:14:55 -0800834 guid = get_unaligned(&arp1394->s_uniq_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -0800836 be64_to_cpu(guid));
Stefan Richterefbeccf2007-04-02 02:12:32 +0200837 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Stefan Richterefbeccf2007-04-02 02:12:32 +0200840 node_info =
841 (struct eth1394_node_info *)node->ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* Update our speed/payload/fifo_offset table */
844 node_info->maxpayload = maxpayload;
845 node_info->sspd = sspd;
846 node_info->fifo = fifo_addr;
847
848 /* Now that we're done with the 1394 specific stuff, we'll
849 * need to alter some of the data. Believe it or not, all
850 * that needs to be done is sender_IP_address needs to be
851 * moved, the destination hardware address get stuffed
852 * in and the hardware address length set to 8.
853 *
854 * IMPORTANT: The code below overwrites 1394 specific data
855 * needed above so keep the munging of the data for the
856 * higher level IP stack last. */
857
858 arp->ar_hln = 8;
859 arp_ptr += arp->ar_hln; /* skip over sender unique id */
Stefan Richterefbeccf2007-04-02 02:12:32 +0200860 *(u32 *)arp_ptr = arp1394->sip; /* move sender IP addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 arp_ptr += arp->ar_pln; /* skip over sender IP addr */
862
Ben Collins02f42132006-06-12 18:15:11 -0400863 if (arp->ar_op == htons(ARPOP_REQUEST))
David S. Millerc20e3942006-10-29 16:14:55 -0800864 memset(arp_ptr, 0, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 else
David S. Millerc20e3942006-10-29 16:14:55 -0800866 memcpy(arp_ptr, dev->dev_addr, sizeof(u64));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 /* Now add the ethernet header. */
Ben Collins7136b802006-06-12 18:16:25 -0400870 if (dev->hard_header(skb, dev, ntohs(ether_type), &dest_hw, NULL,
871 skb->len) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ret = ether1394_type_trans(skb, dev);
873
874 return ret;
875}
876
Stefan Richtere00f04a2007-03-18 12:23:11 +0100877static int fragment_overlap(struct list_head *frag_list, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 struct fragment_info *fi;
880
881 list_for_each_entry(fi, frag_list, list) {
882 if ( ! ((offset > (fi->offset + fi->len - 1)) ||
883 ((offset + len - 1) < fi->offset)))
884 return 1;
885 }
886 return 0;
887}
888
Stefan Richtere00f04a2007-03-18 12:23:11 +0100889static struct list_head *find_partial_datagram(struct list_head *pdgl, int dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 struct partial_datagram *pd;
892
Stefan Richterefbeccf2007-04-02 02:12:32 +0200893 list_for_each_entry(pd, pdgl, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (pd->dgl == dgl)
895 return &pd->list;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return NULL;
898}
899
900/* Assumes that new fragment does not overlap any existing fragments */
Stefan Richtere00f04a2007-03-18 12:23:11 +0100901static int new_fragment(struct list_head *frag_info, int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct list_head *lh;
904 struct fragment_info *fi, *fi2, *new;
905
906 list_for_each(lh, frag_info) {
907 fi = list_entry(lh, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200908 if (fi->offset + fi->len == offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* The new fragment can be tacked on to the end */
910 fi->len += len;
911 /* Did the new fragment plug a hole? */
912 fi2 = list_entry(lh->next, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200913 if (fi->offset + fi->len == fi2->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* glue fragments together */
915 fi->len += fi2->len;
916 list_del(lh->next);
917 kfree(fi2);
918 }
919 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200920 } else if (offset + len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /* The new fragment can be tacked on to the beginning */
922 fi->offset = offset;
923 fi->len += len;
924 /* Did the new fragment plug a hole? */
925 fi2 = list_entry(lh->prev, struct fragment_info, list);
Stefan Richterefbeccf2007-04-02 02:12:32 +0200926 if (fi2->offset + fi2->len == fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* glue fragments together */
928 fi2->len += fi->len;
929 list_del(lh);
930 kfree(fi);
931 }
932 return 0;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200933 } else if (offset > fi->offset + fi->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
Stefan Richterefbeccf2007-04-02 02:12:32 +0200935 } else if (offset + len < fi->offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 lh = lh->prev;
937 break;
938 }
939 }
940
Stefan Richter85511582005-11-07 06:31:45 -0500941 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!new)
943 return -ENOMEM;
944
945 new->offset = offset;
946 new->len = len;
947
948 list_add(&new->list, lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return 0;
950}
951
Stefan Richtere00f04a2007-03-18 12:23:11 +0100952static int new_partial_datagram(struct net_device *dev, struct list_head *pdgl,
953 int dgl, int dg_size, char *frag_buf,
954 int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 struct partial_datagram *new;
957
Stefan Richter85511582005-11-07 06:31:45 -0500958 new = kmalloc(sizeof(*new), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!new)
960 return -ENOMEM;
961
962 INIT_LIST_HEAD(&new->frag_info);
963
964 if (new_fragment(&new->frag_info, frag_off, frag_len) < 0) {
965 kfree(new);
966 return -ENOMEM;
967 }
968
969 new->dgl = dgl;
970 new->dg_size = dg_size;
971
972 new->skb = dev_alloc_skb(dg_size + dev->hard_header_len + 15);
973 if (!new->skb) {
974 struct fragment_info *fi = list_entry(new->frag_info.next,
975 struct fragment_info,
976 list);
977 kfree(fi);
978 kfree(new);
979 return -ENOMEM;
980 }
981
982 skb_reserve(new->skb, (dev->hard_header_len + 15) & ~15);
983 new->pbuf = skb_put(new->skb, dg_size);
984 memcpy(new->pbuf + frag_off, frag_buf, frag_len);
985
986 list_add(&new->list, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return 0;
988}
989
Stefan Richtere00f04a2007-03-18 12:23:11 +0100990static int update_partial_datagram(struct list_head *pdgl, struct list_head *lh,
991 char *frag_buf, int frag_off, int frag_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Stefan Richterefbeccf2007-04-02 02:12:32 +0200993 struct partial_datagram *pd =
994 list_entry(lh, struct partial_datagram, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Stefan Richterefbeccf2007-04-02 02:12:32 +0200996 if (new_fragment(&pd->frag_info, frag_off, frag_len) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 memcpy(pd->pbuf + frag_off, frag_buf, frag_len);
1000
1001 /* Move list entry to beginnig of list so that oldest partial
1002 * datagrams percolate to the end of the list */
Akinobu Mita179e0912006-06-26 00:24:41 -07001003 list_move(lh, pdgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
Stefan Richtere00f04a2007-03-18 12:23:11 +01001007static int is_datagram_complete(struct list_head *lh, int dg_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Stefan Richtere00f04a2007-03-18 12:23:11 +01001009 struct partial_datagram *pd;
1010 struct fragment_info *fi;
1011
1012 pd = list_entry(lh, struct partial_datagram, list);
1013 fi = list_entry(pd->frag_info.next, struct fragment_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 return (fi->len == dg_size);
1016}
1017
1018/* Packet reception. We convert the IP1394 encapsulation header to an
1019 * ethernet header, and fill it with some of our other fields. This is
1020 * an incoming packet from the 1394 bus. */
1021static int ether1394_data_handler(struct net_device *dev, int srcid, int destid,
1022 char *buf, int len)
1023{
1024 struct sk_buff *skb;
1025 unsigned long flags;
1026 struct eth1394_priv *priv = netdev_priv(dev);
1027 union eth1394_hdr *hdr = (union eth1394_hdr *)buf;
1028 u16 ether_type = 0; /* initialized to clear warning */
1029 int hdr_len;
1030 struct unit_directory *ud = priv->ud_list[NODEID_TO_NODE(srcid)];
1031 struct eth1394_node_info *node_info;
1032
1033 if (!ud) {
1034 struct eth1394_node_ref *node;
1035 node = eth1394_find_node_nodeid(&priv->ip_node_list, srcid);
1036 if (!node) {
1037 HPSB_PRINT(KERN_ERR, "ether1394 rx: sender nodeid "
1038 "lookup failure: " NODE_BUS_FMT,
1039 NODE_BUS_ARGS(priv->host, srcid));
1040 priv->stats.rx_dropped++;
1041 return -1;
1042 }
1043 ud = node->ud;
1044
1045 priv->ud_list[NODEID_TO_NODE(srcid)] = ud;
1046 }
1047
Stefan Richterefbeccf2007-04-02 02:12:32 +02001048 node_info = (struct eth1394_node_info *)ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* First, did we receive a fragmented or unfragmented datagram? */
1051 hdr->words.word1 = ntohs(hdr->words.word1);
1052
1053 hdr_len = hdr_type_len[hdr->common.lf];
1054
1055 if (hdr->common.lf == ETH1394_HDR_LF_UF) {
1056 /* An unfragmented datagram has been received by the ieee1394
1057 * bus. Build an skbuff around it so we can pass it to the
1058 * high level network layer. */
1059
1060 skb = dev_alloc_skb(len + dev->hard_header_len + 15);
1061 if (!skb) {
Stefan Richter5009d262007-04-02 02:15:53 +02001062 ETH1394_PRINT_G(KERN_ERR, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 priv->stats.rx_dropped++;
1064 return -1;
1065 }
1066 skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001067 memcpy(skb_put(skb, len - hdr_len), buf + hdr_len,
1068 len - hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 ether_type = hdr->uf.ether_type;
1070 } else {
1071 /* A datagram fragment has been received, now the fun begins. */
1072
1073 struct list_head *pdgl, *lh;
1074 struct partial_datagram *pd;
1075 int fg_off;
1076 int fg_len = len - hdr_len;
1077 int dg_size;
1078 int dgl;
1079 int retval;
1080 struct pdg_list *pdg = &(node_info->pdg);
1081
1082 hdr->words.word3 = ntohs(hdr->words.word3);
1083 /* The 4th header word is reserved so no need to do ntohs() */
1084
1085 if (hdr->common.lf == ETH1394_HDR_LF_FF) {
1086 ether_type = hdr->ff.ether_type;
1087 dgl = hdr->ff.dgl;
1088 dg_size = hdr->ff.dg_size + 1;
1089 fg_off = 0;
1090 } else {
1091 hdr->words.word2 = ntohs(hdr->words.word2);
1092 dgl = hdr->sf.dgl;
1093 dg_size = hdr->sf.dg_size + 1;
1094 fg_off = hdr->sf.fg_off;
1095 }
1096 spin_lock_irqsave(&pdg->lock, flags);
1097
1098 pdgl = &(pdg->list);
1099 lh = find_partial_datagram(pdgl, dgl);
1100
1101 if (lh == NULL) {
1102 while (pdg->sz >= max_partial_datagrams) {
1103 /* remove the oldest */
1104 purge_partial_datagram(pdgl->prev);
1105 pdg->sz--;
1106 }
1107
1108 retval = new_partial_datagram(dev, pdgl, dgl, dg_size,
1109 buf + hdr_len, fg_off,
1110 fg_len);
1111 if (retval < 0) {
1112 spin_unlock_irqrestore(&pdg->lock, flags);
1113 goto bad_proto;
1114 }
1115 pdg->sz++;
1116 lh = find_partial_datagram(pdgl, dgl);
1117 } else {
1118 struct partial_datagram *pd;
1119
1120 pd = list_entry(lh, struct partial_datagram, list);
1121
1122 if (fragment_overlap(&pd->frag_info, fg_off, fg_len)) {
1123 /* Overlapping fragments, obliterate old
1124 * datagram and start new one. */
1125 purge_partial_datagram(lh);
1126 retval = new_partial_datagram(dev, pdgl, dgl,
1127 dg_size,
1128 buf + hdr_len,
1129 fg_off, fg_len);
1130 if (retval < 0) {
1131 pdg->sz--;
1132 spin_unlock_irqrestore(&pdg->lock, flags);
1133 goto bad_proto;
1134 }
1135 } else {
1136 retval = update_partial_datagram(pdgl, lh,
1137 buf + hdr_len,
1138 fg_off, fg_len);
1139 if (retval < 0) {
1140 /* Couldn't save off fragment anyway
1141 * so might as well obliterate the
1142 * datagram now. */
1143 purge_partial_datagram(lh);
1144 pdg->sz--;
1145 spin_unlock_irqrestore(&pdg->lock, flags);
1146 goto bad_proto;
1147 }
1148 } /* fragment overlap */
1149 } /* new datagram or add to existing one */
1150
1151 pd = list_entry(lh, struct partial_datagram, list);
1152
Stefan Richterefbeccf2007-04-02 02:12:32 +02001153 if (hdr->common.lf == ETH1394_HDR_LF_FF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 pd->ether_type = ether_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (is_datagram_complete(lh, dg_size)) {
1157 ether_type = pd->ether_type;
1158 pdg->sz--;
1159 skb = skb_get(pd->skb);
1160 purge_partial_datagram(lh);
1161 spin_unlock_irqrestore(&pdg->lock, flags);
1162 } else {
1163 /* Datagram is not complete, we're done for the
1164 * moment. */
1165 spin_unlock_irqrestore(&pdg->lock, flags);
1166 return 0;
1167 }
1168 } /* unframgented datagram or fragmented one */
1169
1170 /* Write metadata, and then pass to the receive level */
1171 skb->dev = dev;
1172 skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
1173
1174 /* Parse the encapsulation header. This actually does the job of
1175 * converting to an ethernet frame header, aswell as arp
1176 * conversion if needed. ARP conversion is easier in this
1177 * direction, since we are using ethernet as our backend. */
1178 skb->protocol = ether1394_parse_encap(skb, dev, srcid, destid,
1179 ether_type);
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 spin_lock_irqsave(&priv->lock, flags);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (!skb->protocol) {
1184 priv->stats.rx_errors++;
1185 priv->stats.rx_dropped++;
1186 dev_kfree_skb_any(skb);
1187 goto bad_proto;
1188 }
1189
1190 if (netif_rx(skb) == NET_RX_DROP) {
1191 priv->stats.rx_errors++;
1192 priv->stats.rx_dropped++;
1193 goto bad_proto;
1194 }
1195
1196 /* Statistics */
1197 priv->stats.rx_packets++;
1198 priv->stats.rx_bytes += skb->len;
1199
1200bad_proto:
1201 if (netif_queue_stopped(dev))
1202 netif_wake_queue(dev);
1203 spin_unlock_irqrestore(&priv->lock, flags);
1204
1205 dev->last_rx = jiffies;
1206
1207 return 0;
1208}
1209
1210static int ether1394_write(struct hpsb_host *host, int srcid, int destid,
1211 quadlet_t *data, u64 addr, size_t len, u16 flags)
1212{
1213 struct eth1394_host_info *hi;
1214
1215 hi = hpsb_get_hostinfo(&eth1394_highlevel, host);
1216 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +02001217 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1218 host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return RCODE_ADDRESS_ERROR;
1220 }
1221
1222 if (ether1394_data_handler(hi->dev, srcid, destid, (char*)data, len))
1223 return RCODE_ADDRESS_ERROR;
1224 else
1225 return RCODE_COMPLETE;
1226}
1227
1228static void ether1394_iso(struct hpsb_iso *iso)
1229{
1230 quadlet_t *data;
1231 char *buf;
1232 struct eth1394_host_info *hi;
1233 struct net_device *dev;
1234 struct eth1394_priv *priv;
1235 unsigned int len;
1236 u32 specifier_id;
1237 u16 source_id;
1238 int i;
1239 int nready;
1240
1241 hi = hpsb_get_hostinfo(&eth1394_highlevel, iso->host);
1242 if (hi == NULL) {
Stefan Richter5009d262007-04-02 02:15:53 +02001243 ETH1394_PRINT_G(KERN_ERR, "No net device at fw-host%d\n",
1244 iso->host->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return;
1246 }
1247
1248 dev = hi->dev;
1249
1250 nready = hpsb_iso_n_ready(iso);
1251 for (i = 0; i < nready; i++) {
1252 struct hpsb_iso_packet_info *info =
1253 &iso->infos[(iso->first_packet + i) % iso->buf_packets];
Stefan Richterefbeccf2007-04-02 02:12:32 +02001254 data = (quadlet_t *)(iso->data_buf.kvirt + info->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /* skip over GASP header */
1257 buf = (char *)data + 8;
1258 len = info->len - 8;
1259
Stefan Richterefbeccf2007-04-02 02:12:32 +02001260 specifier_id = (be32_to_cpu(data[0]) & 0xffff) << 8 |
1261 (be32_to_cpu(data[1]) & 0xff000000) >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 source_id = be32_to_cpu(data[0]) >> 16;
1263
1264 priv = netdev_priv(dev);
1265
Stefan Richterefbeccf2007-04-02 02:12:32 +02001266 if (info->channel != (iso->host->csr.broadcast_channel & 0x3f)
1267 || specifier_id != ETHER1394_GASP_SPECIFIER_ID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* This packet is not for us */
1269 continue;
1270 }
1271 ether1394_data_handler(dev, source_id, LOCAL_BUS | ALL_NODES,
1272 buf, len);
1273 }
1274
1275 hpsb_iso_recv_release_packets(iso, i);
1276
1277 dev->last_rx = jiffies;
1278}
1279
1280/******************************************
1281 * Datagram transmission code
1282 ******************************************/
1283
1284/* Convert a standard ARP packet to 1394 ARP. The first 8 bytes (the entire
1285 * arphdr) is the same format as the ip1394 header, so they overlap. The rest
1286 * needs to be munged a bit. The remainder of the arphdr is formatted based
1287 * on hwaddr len and ipaddr len. We know what they'll be, so it's easy to
1288 * judge.
1289 *
1290 * Now that the EUI is used for the hardware address all we need to do to make
1291 * this work for 1394 is to insert 2 quadlets that contain max_rec size,
1292 * speed, and unicast FIFO address information between the sender_unique_id
1293 * and the IP addresses.
1294 */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001295static void ether1394_arp_to_1394arp(struct sk_buff *skb,
1296 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 struct eth1394_priv *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 struct arphdr *arp = (struct arphdr *)skb->data;
1300 unsigned char *arp_ptr = (unsigned char *)(arp + 1);
1301 struct eth1394_arp *arp1394 = (struct eth1394_arp *)skb->data;
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 arp1394->hw_addr_len = 16;
1304 arp1394->sip = *(u32*)(arp_ptr + ETH1394_ALEN);
1305 arp1394->max_rec = priv->host->csr.max_rec;
1306 arp1394->sspd = priv->host->csr.lnk_spd;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001307 arp1394->fifo_hi = htons(priv->local_fifo >> 32);
1308 arp1394->fifo_lo = htonl(priv->local_fifo & ~0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311/* We need to encapsulate the standard header with our own. We use the
1312 * ethernet header's proto for our own. */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001313static unsigned int ether1394_encapsulate_prep(unsigned int max_payload,
1314 __be16 proto,
1315 union eth1394_hdr *hdr,
1316 u16 dg_size, u16 dgl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001318 unsigned int adj_max_payload =
1319 max_payload - hdr_type_len[ETH1394_HDR_LF_UF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* Does it all fit in one packet? */
1322 if (dg_size <= adj_max_payload) {
1323 hdr->uf.lf = ETH1394_HDR_LF_UF;
1324 hdr->uf.ether_type = proto;
1325 } else {
1326 hdr->ff.lf = ETH1394_HDR_LF_FF;
1327 hdr->ff.ether_type = proto;
1328 hdr->ff.dg_size = dg_size - 1;
1329 hdr->ff.dgl = dgl;
1330 adj_max_payload = max_payload - hdr_type_len[ETH1394_HDR_LF_FF];
1331 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001332 return (dg_size + adj_max_payload - 1) / adj_max_payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Stefan Richtere00f04a2007-03-18 12:23:11 +01001335static unsigned int ether1394_encapsulate(struct sk_buff *skb,
1336 unsigned int max_payload,
1337 union eth1394_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 union eth1394_hdr *bufhdr;
1340 int ftype = hdr->common.lf;
1341 int hdrsz = hdr_type_len[ftype];
1342 unsigned int adj_max_payload = max_payload - hdrsz;
1343
Stefan Richterefbeccf2007-04-02 02:12:32 +02001344 switch (ftype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 case ETH1394_HDR_LF_UF:
1346 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1347 bufhdr->words.word1 = htons(hdr->words.word1);
1348 bufhdr->words.word2 = hdr->words.word2;
1349 break;
1350
1351 case ETH1394_HDR_LF_FF:
1352 bufhdr = (union eth1394_hdr *)skb_push(skb, hdrsz);
1353 bufhdr->words.word1 = htons(hdr->words.word1);
1354 bufhdr->words.word2 = hdr->words.word2;
1355 bufhdr->words.word3 = htons(hdr->words.word3);
1356 bufhdr->words.word4 = 0;
1357
1358 /* Set frag type here for future interior fragments */
1359 hdr->common.lf = ETH1394_HDR_LF_IF;
1360 hdr->sf.fg_off = 0;
1361 break;
1362
1363 default:
1364 hdr->sf.fg_off += adj_max_payload;
1365 bufhdr = (union eth1394_hdr *)skb_pull(skb, adj_max_payload);
1366 if (max_payload >= skb->len)
1367 hdr->common.lf = ETH1394_HDR_LF_LF;
1368 bufhdr->words.word1 = htons(hdr->words.word1);
1369 bufhdr->words.word2 = htons(hdr->words.word2);
1370 bufhdr->words.word3 = htons(hdr->words.word3);
1371 bufhdr->words.word4 = 0;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return min(max_payload, skb->len);
1374}
1375
Stefan Richtere00f04a2007-03-18 12:23:11 +01001376static struct hpsb_packet *ether1394_alloc_common_packet(struct hpsb_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct hpsb_packet *p;
1379
1380 p = hpsb_alloc_packet(0);
1381 if (p) {
1382 p->host = host;
1383 p->generation = get_hpsb_generation(host);
1384 p->type = hpsb_async;
1385 }
1386 return p;
1387}
1388
Stefan Richtere00f04a2007-03-18 12:23:11 +01001389static int ether1394_prep_write_packet(struct hpsb_packet *p,
1390 struct hpsb_host *host, nodeid_t node,
Stefan Richterefbeccf2007-04-02 02:12:32 +02001391 u64 addr, void *data, int tx_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 p->node_id = node;
1394 p->data = NULL;
1395
1396 p->tcode = TCODE_WRITEB;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001397 p->header[1] = host->node_id << 16 | addr >> 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 p->header[2] = addr & 0xffffffff;
1399
1400 p->header_size = 16;
1401 p->expect_response = 1;
1402
1403 if (hpsb_get_tlabel(p)) {
Stefan Richter5009d262007-04-02 02:15:53 +02001404 ETH1394_PRINT_G(KERN_ERR, "Out of tlabels\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return -1;
1406 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001407 p->header[0] =
1408 p->node_id << 16 | p->tlabel << 10 | 1 << 8 | TCODE_WRITEB << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 p->header[3] = tx_len << 16;
1411 p->data_size = (tx_len + 3) & ~3;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001412 p->data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 return 0;
1415}
1416
Stefan Richtere00f04a2007-03-18 12:23:11 +01001417static void ether1394_prep_gasp_packet(struct hpsb_packet *p,
1418 struct eth1394_priv *priv,
1419 struct sk_buff *skb, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421 p->header_size = 4;
1422 p->tcode = TCODE_STREAM_DATA;
1423
Stefan Richterefbeccf2007-04-02 02:12:32 +02001424 p->header[0] = length << 16 | 3 << 14 | priv->broadcast_channel << 8 |
1425 TCODE_STREAM_DATA << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 p->data_size = length;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001427 p->data = (quadlet_t *)skb->data - 2;
1428 p->data[0] = cpu_to_be32(priv->host->node_id << 16 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ETHER1394_GASP_SPECIFIER_ID_HI);
Stefan Richterefbeccf2007-04-02 02:12:32 +02001430 p->data[1] = cpu_to_be32(ETHER1394_GASP_SPECIFIER_ID_LO << 24 |
Ben Collins7136b802006-06-12 18:16:25 -04001431 ETHER1394_GASP_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* Setting the node id to ALL_NODES (not LOCAL_BUS | ALL_NODES)
1434 * prevents hpsb_send_packet() from setting the speed to an arbitrary
1435 * value based on packet->node_id if packet->node_id is not set. */
1436 p->node_id = ALL_NODES;
1437 p->speed_code = priv->bc_sspd;
1438}
1439
Stefan Richtere00f04a2007-03-18 12:23:11 +01001440static void ether1394_free_packet(struct hpsb_packet *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 if (packet->tcode != TCODE_STREAM_DATA)
1443 hpsb_free_tlabel(packet);
1444 hpsb_free_packet(packet);
1445}
1446
1447static void ether1394_complete_cb(void *__ptask);
1448
1449static int ether1394_send_packet(struct packet_task *ptask, unsigned int tx_len)
1450{
1451 struct eth1394_priv *priv = ptask->priv;
1452 struct hpsb_packet *packet = NULL;
1453
1454 packet = ether1394_alloc_common_packet(priv->host);
1455 if (!packet)
1456 return -1;
1457
1458 if (ptask->tx_type == ETH1394_GASP) {
Stefan Richterefbeccf2007-04-02 02:12:32 +02001459 int length = tx_len + 2 * sizeof(quadlet_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 ether1394_prep_gasp_packet(packet, priv, ptask->skb, length);
1462 } else if (ether1394_prep_write_packet(packet, priv->host,
1463 ptask->dest_node,
1464 ptask->addr, ptask->skb->data,
1465 tx_len)) {
1466 hpsb_free_packet(packet);
1467 return -1;
1468 }
1469
1470 ptask->packet = packet;
1471 hpsb_set_packet_complete_task(ptask->packet, ether1394_complete_cb,
1472 ptask);
1473
1474 if (hpsb_send_packet(packet) < 0) {
1475 ether1394_free_packet(packet);
1476 return -1;
1477 }
1478
1479 return 0;
1480}
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482/* Task function to be run when a datagram transmission is completed */
Stefan Richtere00f04a2007-03-18 12:23:11 +01001483static void ether1394_dg_complete(struct packet_task *ptask, int fail)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct sk_buff *skb = ptask->skb;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001486 struct eth1394_priv *priv = netdev_priv(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 unsigned long flags;
1488
1489 /* Statistics */
1490 spin_lock_irqsave(&priv->lock, flags);
1491 if (fail) {
1492 priv->stats.tx_dropped++;
1493 priv->stats.tx_errors++;
1494 } else {
1495 priv->stats.tx_bytes += skb->len;
1496 priv->stats.tx_packets++;
1497 }
1498 spin_unlock_irqrestore(&priv->lock, flags);
1499
1500 dev_kfree_skb_any(skb);
1501 kmem_cache_free(packet_task_cache, ptask);
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/* Callback for when a packet has been sent and the status of that packet is
1505 * known */
1506static void ether1394_complete_cb(void *__ptask)
1507{
1508 struct packet_task *ptask = (struct packet_task *)__ptask;
1509 struct hpsb_packet *packet = ptask->packet;
1510 int fail = 0;
1511
1512 if (packet->tcode != TCODE_STREAM_DATA)
1513 fail = hpsb_packet_success(packet);
1514
1515 ether1394_free_packet(packet);
1516
1517 ptask->outstanding_pkts--;
1518 if (ptask->outstanding_pkts > 0 && !fail) {
1519 int tx_len;
1520
1521 /* Add the encapsulation header to the fragment */
1522 tx_len = ether1394_encapsulate(ptask->skb, ptask->max_payload,
1523 &ptask->hdr);
1524 if (ether1394_send_packet(ptask, tx_len))
1525 ether1394_dg_complete(ptask, 1);
1526 } else {
1527 ether1394_dg_complete(ptask, fail);
1528 }
1529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531/* Transmit a packet (called by kernel) */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001532static int ether1394_tx(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Al Virob4e3ca12005-10-21 03:22:34 -04001534 gfp_t kmflags = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct eth1394hdr *eth;
1536 struct eth1394_priv *priv = netdev_priv(dev);
Ben Collins02f42132006-06-12 18:15:11 -04001537 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 unsigned long flags;
1539 nodeid_t dest_node;
1540 eth1394_tx_type tx_type;
1541 int ret = 0;
1542 unsigned int tx_len;
1543 unsigned int max_payload;
1544 u16 dg_size;
1545 u16 dgl;
1546 struct packet_task *ptask;
1547 struct eth1394_node_ref *node;
1548 struct eth1394_node_info *node_info = NULL;
1549
1550 ptask = kmem_cache_alloc(packet_task_cache, kmflags);
1551 if (ptask == NULL) {
1552 ret = -ENOMEM;
1553 goto fail;
1554 }
1555
1556 /* XXX Ignore this for now. Noticed that when MacOSX is the IRM,
1557 * it does not set our validity bit. We need to compensate for
1558 * that somewhere else, but not in eth1394. */
1559#if 0
1560 if ((priv->host->csr.broadcast_channel & 0xc0000000) != 0xc0000000) {
1561 ret = -EAGAIN;
1562 goto fail;
1563 }
1564#endif
1565
Stefan Richterefbeccf2007-04-02 02:12:32 +02001566 skb = skb_share_check(skb, kmflags);
1567 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 ret = -ENOMEM;
1569 goto fail;
1570 }
1571
1572 /* Get rid of the fake eth1394 header, but save a pointer */
Stefan Richterefbeccf2007-04-02 02:12:32 +02001573 eth = (struct eth1394hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 skb_pull(skb, ETH1394_HLEN);
1575
1576 proto = eth->h_proto;
1577 dg_size = skb->len;
1578
1579 /* Set the transmission type for the packet. ARP packets and IP
1580 * broadcast packets are sent via GASP. */
1581 if (memcmp(eth->h_dest, dev->broadcast, ETH1394_ALEN) == 0 ||
Ben Collins7136b802006-06-12 18:16:25 -04001582 proto == htons(ETH_P_ARP) ||
1583 (proto == htons(ETH_P_IP) &&
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001584 IN_MULTICAST(ntohl(ip_hdr(skb)->daddr)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 tx_type = ETH1394_GASP;
1586 dest_node = LOCAL_BUS | ALL_NODES;
1587 max_payload = priv->bc_maxpayload - ETHER1394_GASP_OVERHEAD;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001588 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 dgl = priv->bc_dgl;
1590 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1591 priv->bc_dgl++;
1592 } else {
David S. Millerc20e3942006-10-29 16:14:55 -08001593 __be64 guid = get_unaligned((u64 *)eth->h_dest);
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 node = eth1394_find_node_guid(&priv->ip_node_list,
David S. Millerc20e3942006-10-29 16:14:55 -08001596 be64_to_cpu(guid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (!node) {
1598 ret = -EAGAIN;
1599 goto fail;
1600 }
Stefan Richterefbeccf2007-04-02 02:12:32 +02001601 node_info =
1602 (struct eth1394_node_info *)node->ud->device.driver_data;
Ben Collins67372312006-06-12 18:15:31 -04001603 if (node_info->fifo == CSR1212_INVALID_ADDR_SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 ret = -EAGAIN;
1605 goto fail;
1606 }
1607
1608 dest_node = node->ud->ne->nodeid;
1609 max_payload = node_info->maxpayload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001610 BUG_ON(max_payload < 512 - ETHER1394_GASP_OVERHEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 dgl = node_info->dgl;
1613 if (max_payload < dg_size + hdr_type_len[ETH1394_HDR_LF_UF])
1614 node_info->dgl++;
1615 tx_type = ETH1394_WRREQ;
1616 }
1617
1618 /* If this is an ARP packet, convert it */
Ben Collins7136b802006-06-12 18:16:25 -04001619 if (proto == htons(ETH_P_ARP))
Stefan Richterefbeccf2007-04-02 02:12:32 +02001620 ether1394_arp_to_1394arp(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
1622 ptask->hdr.words.word1 = 0;
1623 ptask->hdr.words.word2 = 0;
1624 ptask->hdr.words.word3 = 0;
1625 ptask->hdr.words.word4 = 0;
1626 ptask->skb = skb;
1627 ptask->priv = priv;
1628 ptask->tx_type = tx_type;
1629
1630 if (tx_type != ETH1394_GASP) {
1631 u64 addr;
1632
1633 spin_lock_irqsave(&priv->lock, flags);
1634 addr = node_info->fifo;
1635 spin_unlock_irqrestore(&priv->lock, flags);
1636
1637 ptask->addr = addr;
1638 ptask->dest_node = dest_node;
1639 }
1640
1641 ptask->tx_type = tx_type;
1642 ptask->max_payload = max_payload;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001643 ptask->outstanding_pkts = ether1394_encapsulate_prep(max_payload,
1644 proto, &ptask->hdr, dg_size, dgl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /* Add the encapsulation header to the fragment */
1647 tx_len = ether1394_encapsulate(skb, max_payload, &ptask->hdr);
1648 dev->trans_start = jiffies;
1649 if (ether1394_send_packet(ptask, tx_len))
1650 goto fail;
1651
1652 netif_wake_queue(dev);
1653 return 0;
1654fail:
1655 if (ptask)
1656 kmem_cache_free(packet_task_cache, ptask);
1657
1658 if (skb != NULL)
1659 dev_kfree_skb(skb);
1660
Stefan Richterefbeccf2007-04-02 02:12:32 +02001661 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 priv->stats.tx_dropped++;
1663 priv->stats.tx_errors++;
Stefan Richterefbeccf2007-04-02 02:12:32 +02001664 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 if (netif_queue_stopped(dev))
1667 netif_wake_queue(dev);
1668
1669 return 0; /* returning non-zero causes serious problems */
1670}
1671
Stefan Richterefbeccf2007-04-02 02:12:32 +02001672static void ether1394_get_drvinfo(struct net_device *dev,
1673 struct ethtool_drvinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001675 strcpy(info->driver, driver_name);
1676 strcpy(info->bus_info, "ieee1394"); /* FIXME provide more detail? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679static struct ethtool_ops ethtool_ops = {
1680 .get_drvinfo = ether1394_get_drvinfo
1681};
1682
1683static int __init ether1394_init_module (void)
1684{
Stefan Richterefbeccf2007-04-02 02:12:32 +02001685 packet_task_cache = kmem_cache_create("packet_task",
1686 sizeof(struct packet_task),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 0, 0, NULL, NULL);
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 hpsb_register_highlevel(&eth1394_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return hpsb_register_protocol(&eth1394_proto_driver);
1691}
1692
1693static void __exit ether1394_exit_module (void)
1694{
1695 hpsb_unregister_protocol(&eth1394_proto_driver);
1696 hpsb_unregister_highlevel(&eth1394_highlevel);
1697 kmem_cache_destroy(packet_task_cache);
1698}
1699
1700module_init(ether1394_init_module);
1701module_exit(ether1394_exit_module);